1 /* $NetBSD: rpcb_clnt.c,v 1.6 2000/07/16 06:41:43 itojun Exp $ */
4 * SPDX-License-Identifier: BSD-3-Clause
6 * Copyright (c) 2010, Oracle America, Inc.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are met:
11 * - Redistributions of source code must retain the above copyright notice,
12 * this list of conditions and the following disclaimer.
13 * - Redistributions in binary form must reproduce the above copyright notice,
14 * this list of conditions and the following disclaimer in the documentation
15 * and/or other materials provided with the distribution.
16 * - Neither the name of the "Oracle America, Inc." nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
33 * Copyright (c) 1986-1991 by Sun Microsystems Inc.
36 /* #ident "@(#)rpcb_clnt.c 1.27 94/04/24 SMI" */
39 #if defined(LIBC_SCCS) && !defined(lint)
40 static char sccsid[] = "@(#)rpcb_clnt.c 1.30 89/06/21 Copyr 1988 Sun Micro";
42 #include <sys/cdefs.h>
43 __FBSDID("$FreeBSD$");
47 * interface to rpcbind rpc service.
49 * Copyright (C) 1988, Sun Microsystems, Inc.
52 #include "opt_inet6.h"
54 #include <sys/param.h>
55 #include <sys/systm.h>
56 #include <sys/kernel.h>
57 #include <sys/malloc.h>
59 #include <sys/socket.h>
60 #include <sys/socketvar.h>
63 #include <rpc/rpcb_clnt.h>
64 #include <rpc/rpcb_prot.h>
66 #include <rpc/rpc_com.h>
68 static struct timeval tottimeout = { 60, 0 };
69 static const char nullstring[] = "\000";
71 static CLIENT *local_rpcb(void);
75 static const struct timeval rmttimeout = { 3, 0 };
76 static struct timeval rpcbrmttime = { 15, 0 };
80 struct address_cache {
84 struct netbuf *ac_taddr;
85 struct address_cache *ac_next;
88 static struct address_cache *front;
91 #define CLCR_GET_RPCB_TIMEOUT 1
92 #define CLCR_SET_RPCB_TIMEOUT 2
95 extern int __rpc_lowvers;
97 static struct address_cache *check_cache(const char *, const char *);
98 static void delete_cache(struct netbuf *);
99 static void add_cache(const char *, const char *, struct netbuf *, char *);
100 static CLIENT *getclnthandle(const char *, const struct netconfig *, char **);
101 static CLIENT *local_rpcb(void);
102 static struct netbuf *got_entry(rpcb_entry_list_ptr, const struct netconfig *);
105 * This routine adjusts the timeout used for calls to the remote rpcbind.
106 * Also, this routine can be used to set the use of portmapper version 2
107 * only when doing rpc_broadcasts
108 * These are private routines that may not be provided in future releases.
111 __rpc_control(request, info)
116 case CLCR_GET_RPCB_TIMEOUT:
117 *(struct timeval *)info = tottimeout;
119 case CLCR_SET_RPCB_TIMEOUT:
120 tottimeout = *(struct timeval *)info;
122 case CLCR_SET_LOWVERS:
123 __rpc_lowvers = *(int *)info;
125 case CLCR_GET_LOWVERS:
126 *(int *)info = __rpc_lowvers;
135 * It might seem that a reader/writer lock would be more reasonable here.
136 * However because getclnthandle(), the only user of the cache functions,
137 * may do a delete_cache() operation if a check_cache() fails to return an
138 * address useful to clnt_tli_create(), we may as well use a mutex.
141 * As it turns out, if the cache lock is *not* a reader/writer lock, we will
142 * block all clnt_create's if we are trying to connect to a host that's down,
143 * since the lock will be held all during that time.
147 * The routines check_cache(), add_cache(), delete_cache() manage the
148 * cache of rpcbind addresses for (host, netid).
151 static struct address_cache *
152 check_cache(host, netid)
153 const char *host, *netid;
155 struct address_cache *cptr;
157 /* READ LOCK HELD ON ENTRY: rpcbaddr_cache_lock */
159 for (cptr = front; cptr != NULL; cptr = cptr->ac_next) {
160 if (!strcmp(cptr->ac_host, host) &&
161 !strcmp(cptr->ac_netid, netid)) {
163 fprintf(stderr, "Found cache entry for %s: %s\n",
169 return ((struct address_cache *) NULL);
176 struct address_cache *cptr, *prevptr = NULL;
178 /* WRITE LOCK HELD ON ENTRY: rpcbaddr_cache_lock */
179 for (cptr = front; cptr != NULL; cptr = cptr->ac_next) {
180 if (!memcmp(cptr->ac_taddr->buf, addr->buf, addr->len)) {
182 free(cptr->ac_netid);
183 free(cptr->ac_taddr->buf);
184 free(cptr->ac_taddr);
186 free(cptr->ac_uaddr);
188 prevptr->ac_next = cptr->ac_next;
190 front = cptr->ac_next;
200 add_cache(host, netid, taddr, uaddr)
201 const char *host, *netid;
203 struct netbuf *taddr;
205 struct address_cache *ad_cache, *cptr, *prevptr;
207 ad_cache = (struct address_cache *)
208 malloc(sizeof (struct address_cache));
212 ad_cache->ac_host = strdup(host);
213 ad_cache->ac_netid = strdup(netid);
214 ad_cache->ac_uaddr = uaddr ? strdup(uaddr) : NULL;
215 ad_cache->ac_taddr = (struct netbuf *)malloc(sizeof (struct netbuf));
216 if (!ad_cache->ac_host || !ad_cache->ac_netid || !ad_cache->ac_taddr ||
217 (uaddr && !ad_cache->ac_uaddr)) {
220 ad_cache->ac_taddr->len = ad_cache->ac_taddr->maxlen = taddr->len;
221 ad_cache->ac_taddr->buf = (char *) malloc(taddr->len);
222 if (ad_cache->ac_taddr->buf == NULL) {
224 if (ad_cache->ac_host)
225 free(ad_cache->ac_host);
226 if (ad_cache->ac_netid)
227 free(ad_cache->ac_netid);
228 if (ad_cache->ac_uaddr)
229 free(ad_cache->ac_uaddr);
230 if (ad_cache->ac_taddr)
231 free(ad_cache->ac_taddr);
235 memcpy(ad_cache->ac_taddr->buf, taddr->buf, taddr->len);
237 fprintf(stderr, "Added to cache: %s : %s\n", host, netid);
240 /* VARIABLES PROTECTED BY rpcbaddr_cache_lock: cptr */
242 rwlock_wrlock(&rpcbaddr_cache_lock);
243 if (cachesize < CACHESIZE) {
244 ad_cache->ac_next = front;
248 /* Free the last entry */
251 while (cptr->ac_next) {
253 cptr = cptr->ac_next;
257 fprintf(stderr, "Deleted from cache: %s : %s\n",
258 cptr->ac_host, cptr->ac_netid);
261 free(cptr->ac_netid);
262 free(cptr->ac_taddr->buf);
263 free(cptr->ac_taddr);
265 free(cptr->ac_uaddr);
268 prevptr->ac_next = NULL;
269 ad_cache->ac_next = front;
273 ad_cache->ac_next = NULL;
277 rwlock_unlock(&rpcbaddr_cache_lock);
281 * This routine will return a client handle that is connected to the
282 * rpcbind. If targaddr is non-NULL, the "universal address" of the
283 * host will be stored in *targaddr; the caller is responsible for
284 * freeing this string.
285 * On error, returns NULL and free's everything.
288 getclnthandle(host, nconf, targaddr)
290 const struct netconfig *nconf;
294 struct netbuf *addr, taddr;
295 struct netbuf addr_to_delete;
296 struct __rpc_sockinfo si;
297 struct addrinfo hints, *res, *tres;
298 struct address_cache *ad_cache;
301 /* VARIABLES PROTECTED BY rpcbaddr_cache_lock: ad_cache */
303 /* Get the address of the rpcbind. Check cache first */
305 addr_to_delete.len = 0;
306 rwlock_rdlock(&rpcbaddr_cache_lock);
309 ad_cache = check_cache(host, nconf->nc_netid);
310 if (ad_cache != NULL) {
311 addr = ad_cache->ac_taddr;
312 client = clnt_tli_create(RPC_ANYFD, nconf, addr,
313 (rpcprog_t)RPCBPROG, (rpcvers_t)RPCBVERS4, 0, 0);
314 if (client != NULL) {
316 *targaddr = strdup(ad_cache->ac_uaddr);
317 rwlock_unlock(&rpcbaddr_cache_lock);
320 addr_to_delete.len = addr->len;
321 addr_to_delete.buf = (char *)malloc(addr->len);
322 if (addr_to_delete.buf == NULL) {
323 addr_to_delete.len = 0;
325 memcpy(addr_to_delete.buf, addr->buf, addr->len);
328 rwlock_unlock(&rpcbaddr_cache_lock);
329 if (addr_to_delete.len != 0) {
331 * Assume this may be due to cache data being
334 rwlock_wrlock(&rpcbaddr_cache_lock);
335 delete_cache(&addr_to_delete);
336 rwlock_unlock(&rpcbaddr_cache_lock);
337 free(addr_to_delete.buf);
339 if (!__rpc_nconf2sockinfo(nconf, &si)) {
340 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
344 memset(&hints, 0, sizeof hints);
345 hints.ai_family = si.si_af;
346 hints.ai_socktype = si.si_socktype;
347 hints.ai_protocol = si.si_proto;
350 printf("trying netid %s family %d proto %d socktype %d\n",
351 nconf->nc_netid, si.si_af, si.si_proto, si.si_socktype);
354 if (nconf->nc_protofmly != NULL && strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0) {
355 client = local_rpcb();
358 clnt_pcreateerror("rpcbind clnt interface");
362 struct sockaddr_un sun;
364 *targaddr = malloc(sizeof(sun.sun_path));
365 if (*targaddr == NULL) {
366 CLNT_DESTROY(client);
369 strncpy(*targaddr, _PATH_RPCBINDSOCK,
370 sizeof(sun.sun_path));
375 if (getaddrinfo(host, "sunrpc", &hints, &res) != 0) {
376 rpc_createerr.cf_stat = RPC_UNKNOWNHOST;
381 for (tres = res; tres != NULL; tres = tres->ai_next) {
382 taddr.buf = tres->ai_addr;
383 taddr.len = taddr.maxlen = tres->ai_addrlen;
389 ua = taddr2uaddr(nconf, &taddr);
390 fprintf(stderr, "Got it [%s]\n", ua);
399 fprintf(stderr, "\tnetbuf len = %d, maxlen = %d\n",
400 taddr.len, taddr.maxlen);
401 fprintf(stderr, "\tAddress is ");
402 for (i = 0; i < taddr.len; i++)
403 fprintf(stderr, "%u.", ((char *)(taddr.buf))[i]);
404 fprintf(stderr, "\n");
407 client = clnt_tli_create(RPC_ANYFD, nconf, &taddr,
408 (rpcprog_t)RPCBPROG, (rpcvers_t)RPCBVERS4, 0, 0);
411 clnt_pcreateerror("rpcbind clnt interface");
416 tmpaddr = targaddr ? taddr2uaddr(nconf, &taddr) : NULL;
417 add_cache(host, nconf->nc_netid, &taddr, tmpaddr);
431 #define IN4_LOCALHOST_STRING "127.0.0.1"
432 #define IN6_LOCALHOST_STRING "::1"
435 * This routine will return a client handle that is connected to the local
436 * rpcbind. Returns NULL on error and free's everything.
444 struct sockaddr_un sun;
448 * Try connecting to the local rpcbind through a local socket
449 * first. If this doesn't work, try all transports defined in
450 * the netconfig file.
452 memset(&sun, 0, sizeof sun);
454 error = socreate(AF_LOCAL, &so, SOCK_STREAM, 0, curthread->td_ucred,
458 sun.sun_family = AF_LOCAL;
459 strcpy(sun.sun_path, _PATH_RPCBINDSOCK);
460 sun.sun_len = SUN_LEN(&sun);
462 tsize = __rpc_get_t_size(AF_LOCAL, 0, 0);
463 client = clnt_vc_create(so, (struct sockaddr *)&sun, (rpcprog_t)RPCBPROG,
464 (rpcvers_t)RPCBVERS, tsize, tsize, 1);
466 if (client != NULL) {
467 /* Mark the socket to be closed in destructor */
468 (void) CLNT_CONTROL(client, CLSET_FD_CLOSE, NULL);
472 /* Nobody needs this socket anymore; free the descriptor. */
478 static struct netconfig *loopnconf;
479 static char *localhostname;
481 /* VARIABLES PROTECTED BY loopnconf_lock: loopnconf */
482 mutex_lock(&loopnconf_lock);
483 if (loopnconf == NULL) {
484 struct netconfig *nconf, *tmpnconf = NULL;
488 nc_handle = setnetconfig();
489 if (nc_handle == NULL) {
490 /* fails to open netconfig file */
491 syslog (LOG_ERR, "rpc: failed to open " NETCONFIG);
492 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
493 mutex_unlock(&loopnconf_lock);
496 while ((nconf = getnetconfig(nc_handle)) != NULL) {
499 strcmp(nconf->nc_protofmly, NC_INET6) == 0 ||
501 strcmp(nconf->nc_protofmly, NC_INET) == 0) &&
502 (nconf->nc_semantics == NC_TPI_COTS ||
503 nconf->nc_semantics == NC_TPI_COTS_ORD)) {
504 fd = __rpc_nconf2fd(nconf);
506 * Can't create a socket, assume that
507 * this family isn't configured in the kernel.
513 if (!strcmp(nconf->nc_protofmly, NC_INET))
514 localhostname = IN4_LOCALHOST_STRING;
516 localhostname = IN6_LOCALHOST_STRING;
519 if (tmpnconf == NULL) {
520 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
521 mutex_unlock(&loopnconf_lock);
524 loopnconf = getnetconfigent(tmpnconf->nc_netid);
525 /* loopnconf is never freed */
526 endnetconfig(nc_handle);
528 mutex_unlock(&loopnconf_lock);
529 client = getclnthandle(localhostname, loopnconf, NULL);
537 * Set a mapping between program, version and address.
538 * Calls the rpcbind service to do the mapping.
541 rpcb_set(rpcprog_t program, rpcvers_t version,
542 const struct netconfig *nconf, /* Network structure of transport */
543 const struct netbuf *address) /* Services netconfig address */
551 struct netconfig nconfcopy;
552 struct netbuf addresscopy;
554 /* parameter checking */
556 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
559 if (address == NULL) {
560 rpc_createerr.cf_stat = RPC_UNKNOWNADDR;
563 client = local_rpcb();
568 /* convert to universal */
569 /*LINTED const castaway*/
571 addresscopy = *address;
572 parms.r_addr = taddr2uaddr(&nconfcopy, &addresscopy);
574 CLNT_DESTROY(client);
575 rpc_createerr.cf_stat = RPC_N2AXLATEFAILURE;
576 return (FALSE); /* no universal address */
578 parms.r_prog = program;
579 parms.r_vers = version;
580 parms.r_netid = nconf->nc_netid;
583 * Though uid is not being used directly, we still send it for
584 * completeness. For non-unix platforms, perhaps some other
585 * string or an empty string can be sent.
587 (void) snprintf(uidbuf, sizeof uidbuf, "%d", geteuid());
588 parms.r_owner = uidbuf;
593 CLNT_CALL(client, (rpcproc_t)RPCBPROC_SET, (xdrproc_t) xdr_rpcb,
594 (char *)(void *)&parms, (xdrproc_t) xdr_bool,
595 (char *)(void *)&rslt, tottimeout);
597 CLNT_DESTROY(client);
598 free(parms.r_addr, M_RPC);
603 * Remove the mapping between program, version and netbuf address.
604 * Calls the rpcbind service to do the un-mapping.
605 * If netbuf is NULL, unset for all the transports, otherwise unset
606 * only for the given transport.
609 rpcb_unset(rpcprog_t program, rpcvers_t version, const struct netconfig *nconf)
618 client = local_rpcb();
623 parms.r_prog = program;
624 parms.r_vers = version;
626 parms.r_netid = nconf->nc_netid;
628 /*LINTED const castaway*/
629 parms.r_netid = (char *)(uintptr_t) &nullstring[0]; /* unsets all */
631 /*LINTED const castaway*/
632 parms.r_addr = (char *)(uintptr_t) &nullstring[0];
634 (void) snprintf(uidbuf, sizeof uidbuf, "%d", geteuid());
635 parms.r_owner = uidbuf;
640 CLNT_CALL(client, (rpcproc_t)RPCBPROC_UNSET, (xdrproc_t) xdr_rpcb,
641 (char *)(void *)&parms, (xdrproc_t) xdr_bool,
642 (char *)(void *)&rslt, tottimeout);
644 CLNT_DESTROY(client);
651 * From the merged list, find the appropriate entry
653 static struct netbuf *
654 got_entry(relp, nconf)
655 rpcb_entry_list_ptr relp;
656 const struct netconfig *nconf;
658 struct netbuf *na = NULL;
659 rpcb_entry_list_ptr sp;
662 for (sp = relp; sp != NULL; sp = sp->rpcb_entry_next) {
663 rmap = &sp->rpcb_entry_map;
664 if ((strcmp(nconf->nc_proto, rmap->r_nc_proto) == 0) &&
665 (strcmp(nconf->nc_protofmly, rmap->r_nc_protofmly) == 0) &&
666 (nconf->nc_semantics == rmap->r_nc_semantics) &&
667 (rmap->r_maddr != NULL) && (rmap->r_maddr[0] != 0)) {
668 na = uaddr2taddr(nconf, rmap->r_maddr);
670 fprintf(stderr, "\tRemote address is [%s].\n",
674 "\tCouldn't resolve remote address!\n");
683 * Quick check to see if rpcbind is up. Tries to connect over
689 struct netconfig *nconf;
690 struct sockaddr_un sun;
695 localhandle = setnetconfig();
696 while ((nconf = getnetconfig(localhandle)) != NULL) {
697 if (nconf->nc_protofmly != NULL &&
698 strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0)
704 endnetconfig(localhandle);
706 memset(&sun, 0, sizeof sun);
707 sock = _socket(AF_LOCAL, SOCK_STREAM, 0);
710 sun.sun_family = AF_LOCAL;
711 strncpy(sun.sun_path, _PATH_RPCBINDSOCK, sizeof(sun.sun_path));
712 sun.sun_len = SUN_LEN(&sun);
714 if (_connect(sock, (struct sockaddr *)&sun, sun.sun_len) < 0) {
724 * An internal function which optimizes rpcb_getaddr function. It also
725 * returns the client handle that it uses to contact the remote rpcbind.
727 * The algorithm used: If the transports is TCP or UDP, it first tries
728 * version 2 (portmap), 4 and then 3 (svr4). This order should be
729 * changed in the next OS release to 4, 2 and 3. We are assuming that by
730 * that time, version 4 would be available on many machines on the network.
731 * With this algorithm, we get performance as well as a plan for
732 * obsoleting version 2.
734 * For all other transports, the algorithm remains as 4 and then 3.
736 * XXX: Due to some problems with t_connect(), we do not reuse the same client
737 * handle for COTS cases and hence in these cases we do not return the
738 * client handle. This code will change if t_connect() ever
739 * starts working properly. Also look under clnt_vc.c.
742 __rpcb_findaddr_timed(program, version, nconf, host, clpp, tp)
745 const struct netconfig *nconf;
750 static bool_t check_rpcbind = TRUE;
751 CLIENT *client = NULL;
753 enum clnt_stat clnt_st;
756 struct netbuf *address = NULL;
757 rpcvers_t start_vers = RPCBVERS4;
758 struct netbuf servaddr;
760 /* parameter checking */
762 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
769 * Use default total timeout if no timeout is specified.
775 /* Try version 2 for TCP or UDP */
776 if (strcmp(nconf->nc_protofmly, NC_INET) == 0) {
778 struct netbuf remote;
779 rpcvers_t pmapvers = 2;
780 struct pmap pmapparms;
783 * Try UDP only - there are some portmappers out
784 * there that use UDP only.
786 if (strcmp(nconf->nc_proto, NC_TCP) == 0) {
787 struct netconfig *newnconf;
789 if ((newnconf = getnetconfigent("udp")) == NULL) {
790 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
793 client = getclnthandle(host, newnconf, &parms.r_addr);
794 freenetconfigent(newnconf);
796 client = getclnthandle(host, nconf, &parms.r_addr);
802 * Set version and retry timeout.
804 CLNT_CONTROL(client, CLSET_RETRY_TIMEOUT, (char *)&rpcbrmttime);
805 CLNT_CONTROL(client, CLSET_VERS, (char *)&pmapvers);
807 pmapparms.pm_prog = program;
808 pmapparms.pm_vers = version;
809 pmapparms.pm_prot = strcmp(nconf->nc_proto, NC_TCP) ?
810 IPPROTO_UDP : IPPROTO_TCP;
811 pmapparms.pm_port = 0; /* not needed */
812 clnt_st = CLNT_CALL(client, (rpcproc_t)PMAPPROC_GETPORT,
813 (xdrproc_t) xdr_pmap, (caddr_t)(void *)&pmapparms,
814 (xdrproc_t) xdr_u_short, (caddr_t)(void *)&port,
816 if (clnt_st != RPC_SUCCESS) {
817 if ((clnt_st == RPC_PROGVERSMISMATCH) ||
818 (clnt_st == RPC_PROGUNAVAIL))
819 goto try_rpcbind; /* Try different versions */
820 rpc_createerr.cf_stat = RPC_PMAPFAILURE;
821 clnt_geterr(client, &rpc_createerr.cf_error);
823 } else if (port == 0) {
825 rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
829 CLNT_CONTROL(client, CLGET_SVC_ADDR, (char *)&remote);
830 if (((address = (struct netbuf *)
831 malloc(sizeof (struct netbuf))) == NULL) ||
832 ((address->buf = (char *)
833 malloc(remote.len)) == NULL)) {
834 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
835 clnt_geterr(client, &rpc_createerr.cf_error);
842 memcpy(address->buf, remote.buf, remote.len);
843 memcpy(&((char *)address->buf)[sizeof (short)],
844 (char *)(void *)&port, sizeof (short));
845 address->len = address->maxlen = remote.len;
852 * Check if rpcbind is up. This prevents needless delays when
853 * accessing applications such as the keyserver while booting
856 if (check_rpcbind && strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0) {
857 if (!__rpcbind_is_up()) {
858 rpc_createerr.cf_stat = RPC_PMAPFAILURE;
859 rpc_createerr.cf_error.re_errno = 0;
862 check_rpcbind = FALSE;
866 * Now we try version 4 and then 3.
867 * We also send the remote system the address we used to
868 * contact it in case it can help to connect back with us
870 parms.r_prog = program;
871 parms.r_vers = version;
872 /*LINTED const castaway*/
873 parms.r_owner = (char *) &nullstring[0]; /* not needed; */
874 /* just for xdring */
875 parms.r_netid = nconf->nc_netid; /* not really needed */
878 * If a COTS transport is being used, try getting address via CLTS
879 * transport. This works only with version 4.
881 if (nconf->nc_semantics == NC_TPI_COTS_ORD ||
882 nconf->nc_semantics == NC_TPI_COTS) {
885 struct netconfig *nconf_clts;
886 rpcb_entry_list_ptr relp = NULL;
888 if (client == NULL) {
889 /* This did not go through the above PORTMAP/TCP code */
890 if ((handle = __rpc_setconf("datagram_v")) != NULL) {
891 while ((nconf_clts = __rpc_getconf(handle))
893 if (strcmp(nconf_clts->nc_protofmly,
894 nconf->nc_protofmly) != 0) {
897 client = getclnthandle(host, nconf_clts,
901 __rpc_endconf(handle);
904 goto regular_rpcbind; /* Go the regular way */
906 /* This is a UDP PORTMAP handle. Change to version 4 */
908 CLNT_CONTROL(client, CLSET_VERS, (char *)(void *)&vers);
911 * We also send the remote system the address we used to
912 * contact it in case it can help it connect back with us
914 if (parms.r_addr == NULL) {
915 /*LINTED const castaway*/
916 parms.r_addr = (char *) &nullstring[0]; /* for XDRing */
919 CLNT_CONTROL(client, CLSET_RETRY_TIMEOUT, (char *)&rpcbrmttime);
921 clnt_st = CLNT_CALL(client, (rpcproc_t)RPCBPROC_GETADDRLIST,
922 (xdrproc_t) xdr_rpcb, (char *)(void *)&parms,
923 (xdrproc_t) xdr_rpcb_entry_list_ptr,
924 (char *)(void *)&relp, *tp);
925 if (clnt_st == RPC_SUCCESS) {
926 if ((address = got_entry(relp, nconf)) != NULL) {
927 xdr_free((xdrproc_t) xdr_rpcb_entry_list_ptr,
928 (char *)(void *)&relp);
929 CLNT_CONTROL(client, CLGET_SVC_ADDR,
930 (char *)(void *)&servaddr);
931 __rpc_fixup_addr(address, &servaddr);
934 /* Entry not found for this transport */
935 xdr_free((xdrproc_t) xdr_rpcb_entry_list_ptr,
936 (char *)(void *)&relp);
938 * XXX: should have perhaps returned with error but
939 * since the remote machine might not always be able
940 * to send the address on all transports, we try the
941 * regular way with regular_rpcbind
943 goto regular_rpcbind;
944 } else if ((clnt_st == RPC_PROGVERSMISMATCH) ||
945 (clnt_st == RPC_PROGUNAVAIL)) {
946 start_vers = RPCBVERS; /* Try version 3 now */
947 goto regular_rpcbind; /* Try different versions */
949 rpc_createerr.cf_stat = RPC_PMAPFAILURE;
950 clnt_geterr(client, &rpc_createerr.cf_error);
957 /* Now the same transport is to be used to get the address */
958 if (client && ((nconf->nc_semantics == NC_TPI_COTS_ORD) ||
959 (nconf->nc_semantics == NC_TPI_COTS))) {
960 /* A CLTS type of client - destroy it */
961 CLNT_DESTROY(client);
965 if (client == NULL) {
966 client = getclnthandle(host, nconf, &parms.r_addr);
967 if (client == NULL) {
971 if (parms.r_addr == NULL) {
972 /*LINTED const castaway*/
973 parms.r_addr = (char *) &nullstring[0];
976 /* First try from start_vers and then version 3 (RPCBVERS) */
978 CLNT_CONTROL(client, CLSET_RETRY_TIMEOUT, (char *) &rpcbrmttime);
979 for (vers = start_vers; vers >= RPCBVERS; vers--) {
980 /* Set the version */
981 CLNT_CONTROL(client, CLSET_VERS, (char *)(void *)&vers);
982 clnt_st = CLNT_CALL(client, (rpcproc_t)RPCBPROC_GETADDR,
983 (xdrproc_t) xdr_rpcb, (char *)(void *)&parms,
984 (xdrproc_t) xdr_wrapstring, (char *)(void *) &ua, *tp);
985 if (clnt_st == RPC_SUCCESS) {
986 if ((ua == NULL) || (ua[0] == 0)) {
987 /* address unknown */
988 rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
991 address = uaddr2taddr(nconf, ua);
993 fprintf(stderr, "\tRemote address is [%s]\n", ua);
996 "\tCouldn't resolve remote address!\n");
998 xdr_free((xdrproc_t)xdr_wrapstring,
999 (char *)(void *)&ua);
1002 /* We don't know about your universal address */
1003 rpc_createerr.cf_stat = RPC_N2AXLATEFAILURE;
1006 CLNT_CONTROL(client, CLGET_SVC_ADDR,
1007 (char *)(void *)&servaddr);
1008 __rpc_fixup_addr(address, &servaddr);
1010 } else if (clnt_st == RPC_PROGVERSMISMATCH) {
1011 struct rpc_err rpcerr;
1013 clnt_geterr(client, &rpcerr);
1014 if (rpcerr.re_vers.low > RPCBVERS4)
1015 goto error; /* a new version, can't handle */
1016 } else if (clnt_st != RPC_PROGUNAVAIL) {
1017 /* Cant handle this error */
1018 rpc_createerr.cf_stat = clnt_st;
1019 clnt_geterr(client, &rpc_createerr.cf_error);
1026 CLNT_DESTROY(client);
1030 if (nconf->nc_semantics != NC_TPI_CLTS) {
1031 /* This client is the connectionless one */
1033 CLNT_DESTROY(client);
1039 } else if (client) {
1040 CLNT_DESTROY(client);
1042 if (parms.r_addr != NULL && parms.r_addr != nullstring)
1049 * Find the mapped address for program, version.
1050 * Calls the rpcbind service remotely to do the lookup.
1051 * Uses the transport specified in nconf.
1052 * Returns FALSE (0) if no map exists, else returns 1.
1054 * Assuming that the address is all properly allocated
1057 rpcb_getaddr(program, version, nconf, address, host)
1060 const struct netconfig *nconf;
1061 struct netbuf *address;
1066 if ((na = __rpcb_findaddr_timed(program, version,
1067 (struct netconfig *) nconf, (char *) host,
1068 (CLIENT **) NULL, (struct timeval *) NULL)) == NULL)
1071 if (na->len > address->maxlen) {
1072 /* Too long address */
1075 rpc_createerr.cf_stat = RPC_FAILED;
1078 memcpy(address->buf, na->buf, (size_t)na->len);
1079 address->len = na->len;
1086 * Get a copy of the current maps.
1087 * Calls the rpcbind service remotely to get the maps.
1089 * It returns only a list of the services
1090 * It returns NULL on failure.
1093 rpcb_getmaps(nconf, host)
1094 const struct netconfig *nconf;
1097 rpcblist_ptr head = NULL;
1099 enum clnt_stat clnt_st;
1102 client = getclnthandle(host, nconf, NULL);
1103 if (client == NULL) {
1106 clnt_st = CLNT_CALL(client, (rpcproc_t)RPCBPROC_DUMP,
1107 (xdrproc_t) xdr_void, NULL, (xdrproc_t) xdr_rpcblist_ptr,
1108 (char *)(void *)&head, tottimeout);
1109 if (clnt_st == RPC_SUCCESS)
1112 if ((clnt_st != RPC_PROGVERSMISMATCH) &&
1113 (clnt_st != RPC_PROGUNAVAIL)) {
1114 rpc_createerr.cf_stat = RPC_RPCBFAILURE;
1115 clnt_geterr(client, &rpc_createerr.cf_error);
1119 /* fall back to earlier version */
1120 CLNT_CONTROL(client, CLGET_VERS, (char *)(void *)&vers);
1121 if (vers == RPCBVERS4) {
1123 CLNT_CONTROL(client, CLSET_VERS, (char *)(void *)&vers);
1124 if (CLNT_CALL(client, (rpcproc_t)RPCBPROC_DUMP,
1125 (xdrproc_t) xdr_void, NULL, (xdrproc_t) xdr_rpcblist_ptr,
1126 (char *)(void *)&head, tottimeout) == RPC_SUCCESS)
1129 rpc_createerr.cf_stat = RPC_RPCBFAILURE;
1130 clnt_geterr(client, &rpc_createerr.cf_error);
1133 CLNT_DESTROY(client);
1138 * rpcbinder remote-call-service interface.
1139 * This routine is used to call the rpcbind remote call service
1140 * which will look up a service program in the address maps, and then
1141 * remotely call that routine with the given parameters. This allows
1142 * programs to do a lookup and call in one step.
1145 rpcb_rmtcall(nconf, host, prog, vers, proc, xdrargs, argsp,
1146 xdrres, resp, tout, addr_ptr)
1147 const struct netconfig *nconf; /* Netconfig structure */
1148 const char *host; /* Remote host name */
1151 rpcproc_t proc; /* Remote proc identifiers */
1152 xdrproc_t xdrargs, xdrres; /* XDR routines */
1153 caddr_t argsp, resp; /* Argument and Result */
1154 struct timeval tout; /* Timeout value for this call */
1155 const struct netbuf *addr_ptr; /* Preallocated netbuf address */
1158 enum clnt_stat stat;
1159 struct r_rpcb_rmtcallargs a;
1160 struct r_rpcb_rmtcallres r;
1161 rpcvers_t rpcb_vers;
1164 client = getclnthandle(host, nconf, NULL);
1165 if (client == NULL) {
1166 return (RPC_FAILED);
1168 /*LINTED const castaway*/
1169 CLNT_CONTROL(client, CLSET_RETRY_TIMEOUT, (char *)(void *)&rmttimeout);
1173 a.args.args_val = argsp;
1174 a.xdr_args = xdrargs;
1176 r.results.results_val = resp;
1179 for (rpcb_vers = RPCBVERS4; rpcb_vers >= RPCBVERS; rpcb_vers--) {
1180 CLNT_CONTROL(client, CLSET_VERS, (char *)(void *)&rpcb_vers);
1181 stat = CLNT_CALL(client, (rpcproc_t)RPCBPROC_CALLIT,
1182 (xdrproc_t) xdr_rpcb_rmtcallargs, (char *)(void *)&a,
1183 (xdrproc_t) xdr_rpcb_rmtcallres, (char *)(void *)&r, tout);
1184 if ((stat == RPC_SUCCESS) && (addr_ptr != NULL)) {
1186 /*LINTED const castaway*/
1187 na = uaddr2taddr((struct netconfig *) nconf, r.addr);
1189 stat = RPC_N2AXLATEFAILURE;
1190 /*LINTED const castaway*/
1191 ((struct netbuf *) addr_ptr)->len = 0;
1194 if (na->len > addr_ptr->maxlen) {
1195 /* Too long address */
1196 stat = RPC_FAILED; /* XXX A better error no */
1199 /*LINTED const castaway*/
1200 ((struct netbuf *) addr_ptr)->len = 0;
1203 memcpy(addr_ptr->buf, na->buf, (size_t)na->len);
1204 /*LINTED const castaway*/
1205 ((struct netbuf *)addr_ptr)->len = na->len;
1209 } else if ((stat != RPC_PROGVERSMISMATCH) &&
1210 (stat != RPC_PROGUNAVAIL)) {
1215 CLNT_DESTROY(client);
1217 xdr_free((xdrproc_t) xdr_wrapstring, (char *)(void *)&r.addr);
1222 * Gets the time on the remote host.
1223 * Returns 1 if succeeds else 0.
1226 rpcb_gettime(host, timep)
1230 CLIENT *client = NULL;
1232 struct netconfig *nconf;
1237 if ((host == NULL) || (host[0] == 0)) {
1242 if ((handle = __rpc_setconf("netpath")) == NULL) {
1243 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
1246 rpc_createerr.cf_stat = RPC_SUCCESS;
1247 while (client == NULL) {
1248 if ((nconf = __rpc_getconf(handle)) == NULL) {
1249 if (rpc_createerr.cf_stat == RPC_SUCCESS)
1250 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
1253 client = getclnthandle(host, nconf, NULL);
1257 __rpc_endconf(handle);
1258 if (client == (CLIENT *) NULL) {
1262 st = CLNT_CALL(client, (rpcproc_t)RPCBPROC_GETTIME,
1263 (xdrproc_t) xdr_void, NULL,
1264 (xdrproc_t) xdr_int, (char *)(void *)timep, tottimeout);
1266 if ((st == RPC_PROGVERSMISMATCH) || (st == RPC_PROGUNAVAIL)) {
1267 CLNT_CONTROL(client, CLGET_VERS, (char *)(void *)&vers);
1268 if (vers == RPCBVERS4) {
1269 /* fall back to earlier version */
1271 CLNT_CONTROL(client, CLSET_VERS, (char *)(void *)&vers);
1272 st = CLNT_CALL(client, (rpcproc_t)RPCBPROC_GETTIME,
1273 (xdrproc_t) xdr_void, NULL,
1274 (xdrproc_t) xdr_int, (char *)(void *)timep,
1278 CLNT_DESTROY(client);
1279 return (st == RPC_SUCCESS? TRUE: FALSE);
1283 xdr_netbuf(XDR *xdrs, struct netbuf *objp)
1288 if (!xdr_uint32_t(xdrs, (uint32_t *) &objp->maxlen)) {
1293 if (objp->maxlen > RPC_MAXDATASIZE) {
1297 dummy = xdr_bytes(xdrs, (char **) pp,
1298 (u_int *)&(objp->len), objp->maxlen);
1303 * Converts taddr to universal address. This routine should never
1304 * really be called because local n2a libraries are always provided.
1307 rpcb_taddr2uaddr(struct netconfig *nconf, struct netbuf *taddr)
1313 /* parameter checking */
1314 if (nconf == NULL) {
1315 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
1318 if (taddr == NULL) {
1319 rpc_createerr.cf_stat = RPC_UNKNOWNADDR;
1322 client = local_rpcb();
1327 CLNT_CALL(client, (rpcproc_t)RPCBPROC_TADDR2UADDR,
1328 (xdrproc_t) xdr_netbuf, (char *)(void *)taddr,
1329 (xdrproc_t) xdr_wrapstring, (char *)(void *)&uaddr, tottimeout);
1330 CLNT_DESTROY(client);
1335 * Converts universal address to netbuf. This routine should never
1336 * really be called because local n2a libraries are always provided.
1339 rpcb_uaddr2taddr(struct netconfig *nconf, char *uaddr)
1342 struct netbuf *taddr;
1345 /* parameter checking */
1346 if (nconf == NULL) {
1347 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
1350 if (uaddr == NULL) {
1351 rpc_createerr.cf_stat = RPC_UNKNOWNADDR;
1354 client = local_rpcb();
1359 taddr = (struct netbuf *)malloc(sizeof (struct netbuf), M_RPC, M_WAITOK|M_ZERO);
1360 if (CLNT_CALL(client, (rpcproc_t)RPCBPROC_UADDR2TADDR,
1361 (xdrproc_t) xdr_wrapstring, (char *)(void *)&uaddr,
1362 (xdrproc_t) xdr_netbuf, (char *)(void *)taddr,
1363 tottimeout) != RPC_SUCCESS) {
1367 CLNT_DESTROY(client);