2 * Copyright (c) 2010, Oracle America, Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * - Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
9 * - Redistributions in binary form must reproduce the above copyright notice,
10 * this list of conditions and the following disclaimer in the documentation
11 * and/or other materials provided with the distribution.
12 * - Neither the name of the "Oracle America, Inc." nor the names of its
13 * contributors may be used to endorse or promote products derived
14 * from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
31 * interface to rpcbind rpc service.
34 #include <reentrant.h>
35 #include <sys/types.h>
36 #include <sys/socket.h>
38 #include <sys/utsname.h>
40 #include <rpc/rpcb_prot.h>
41 #include <rpc/nettype.h>
42 #include <netconfig.h>
44 #include <netinet/in.h> /* FOR IPPROTO_TCP/UDP definitions */
45 #include <rpc/pmap_prot.h>
59 static struct timeval tottimeout = { 60, 0 };
60 static const struct timeval rmttimeout = { 3, 0 };
61 static struct timeval rpcbrmttime = { 15, 0 };
63 extern bool_t xdr_wrapstring(XDR *, char **);
65 static const char nullstring[] = "\000";
67 #define RPCB_OWNER_STRING "libtirpc"
71 struct address_cache {
75 struct netbuf *ac_taddr;
76 struct address_cache *ac_next;
79 static struct address_cache *front;
82 #define CLCR_GET_RPCB_TIMEOUT 1
83 #define CLCR_SET_RPCB_TIMEOUT 2
86 extern int __rpc_lowvers;
88 static struct address_cache *check_cache(const char *, const char *);
89 static void delete_cache(struct netbuf *);
90 static void add_cache(const char *, const char *, struct netbuf *, char *);
91 static CLIENT *getclnthandle(const char *, const struct netconfig *, char **);
92 static CLIENT *local_rpcb(void);
94 static struct netbuf *got_entry(rpcb_entry_list_ptr, const struct netconfig *);
98 * This routine adjusts the timeout used for calls to the remote rpcbind.
99 * Also, this routine can be used to set the use of portmapper version 2
100 * only when doing rpc_broadcasts
101 * These are private routines that may not be provided in future releases.
104 __rpc_control(request, info)
109 case CLCR_GET_RPCB_TIMEOUT:
110 *(struct timeval *)info = tottimeout;
112 case CLCR_SET_RPCB_TIMEOUT:
113 tottimeout = *(struct timeval *)info;
115 case CLCR_SET_LOWVERS:
116 __rpc_lowvers = *(int *)info;
118 case CLCR_GET_LOWVERS:
119 *(int *)info = __rpc_lowvers;
128 * It might seem that a reader/writer lock would be more reasonable here.
129 * However because getclnthandle(), the only user of the cache functions,
130 * may do a delete_cache() operation if a check_cache() fails to return an
131 * address useful to clnt_tli_create(), we may as well use a mutex.
134 * As it turns out, if the cache lock is *not* a reader/writer lock, we will
135 * block all clnt_create's if we are trying to connect to a host that's down,
136 * since the lock will be held all during that time.
138 extern rwlock_t rpcbaddr_cache_lock;
141 * The routines check_cache(), add_cache(), delete_cache() manage the
142 * cache of rpcbind addresses for (host, netid).
145 static struct address_cache *
146 check_cache(host, netid)
147 const char *host, *netid;
149 struct address_cache *cptr;
151 /* READ LOCK HELD ON ENTRY: rpcbaddr_cache_lock */
153 for (cptr = front; cptr != NULL; cptr = cptr->ac_next) {
154 if (!strcmp(cptr->ac_host, host) &&
155 !strcmp(cptr->ac_netid, netid)) {
156 LIBTIRPC_DEBUG(3, ("check_cache: Found cache entry for %s: %s\n",
161 return ((struct address_cache *) NULL);
168 struct address_cache *cptr, *prevptr = NULL;
170 /* WRITE LOCK HELD ON ENTRY: rpcbaddr_cache_lock */
171 for (cptr = front; cptr != NULL; cptr = cptr->ac_next) {
172 if (!memcmp(cptr->ac_taddr->buf, addr->buf, addr->len)) {
174 free(cptr->ac_netid);
175 free(cptr->ac_taddr->buf);
176 free(cptr->ac_taddr);
178 free(cptr->ac_uaddr);
180 prevptr->ac_next = cptr->ac_next;
182 front = cptr->ac_next;
192 add_cache(host, netid, taddr, uaddr)
193 const char *host, *netid;
195 struct netbuf *taddr;
197 struct address_cache *ad_cache, *cptr, *prevptr;
199 ad_cache = (struct address_cache *)
200 malloc(sizeof (struct address_cache));
204 ad_cache->ac_host = strdup(host);
205 ad_cache->ac_netid = strdup(netid);
206 ad_cache->ac_uaddr = uaddr ? strdup(uaddr) : NULL;
207 ad_cache->ac_taddr = (struct netbuf *)malloc(sizeof (struct netbuf));
208 if (!ad_cache->ac_host || !ad_cache->ac_netid || !ad_cache->ac_taddr ||
209 (uaddr && !ad_cache->ac_uaddr))
211 ad_cache->ac_taddr->len = ad_cache->ac_taddr->maxlen = taddr->len;
212 ad_cache->ac_taddr->buf = (char *) malloc(taddr->len);
213 if (ad_cache->ac_taddr->buf == NULL)
215 memcpy(ad_cache->ac_taddr->buf, taddr->buf, taddr->len);
216 LIBTIRPC_DEBUG(3, ("add_cache: Added to cache: %s : %s\n", host, netid));
218 /* VARIABLES PROTECTED BY rpcbaddr_cache_lock: cptr */
220 rwlock_wrlock(&rpcbaddr_cache_lock);
221 if (cachesize < CACHESIZE) {
222 ad_cache->ac_next = front;
226 /* Free the last entry */
229 while (cptr->ac_next) {
231 cptr = cptr->ac_next;
234 LIBTIRPC_DEBUG(3, ("add_cache: Deleted from cache: %s : %s\n",
235 cptr->ac_host, cptr->ac_netid));
237 free(cptr->ac_netid);
238 free(cptr->ac_taddr->buf);
239 free(cptr->ac_taddr);
241 free(cptr->ac_uaddr);
244 prevptr->ac_next = NULL;
245 ad_cache->ac_next = front;
249 ad_cache->ac_next = NULL;
253 rwlock_unlock(&rpcbaddr_cache_lock);
257 free(ad_cache->ac_host);
258 free(ad_cache->ac_netid);
259 free(ad_cache->ac_uaddr);
260 free(ad_cache->ac_taddr);
265 * This routine will return a client handle that is connected to the
266 * rpcbind. If targaddr is non-NULL, the "universal address" of the
267 * host will be stored in *targaddr; the caller is responsible for
268 * freeing this string.
269 * On error, returns NULL and free's everything.
272 getclnthandle(host, nconf, targaddr)
274 const struct netconfig *nconf;
278 struct netbuf *addr, taddr;
279 struct netbuf addr_to_delete;
280 struct __rpc_sockinfo si;
281 struct addrinfo hints, *res, *tres;
282 struct address_cache *ad_cache;
285 /* VARIABLES PROTECTED BY rpcbaddr_cache_lock: ad_cache */
287 /* Get the address of the rpcbind. Check cache first */
291 addr_to_delete.len = 0;
292 rwlock_rdlock(&rpcbaddr_cache_lock);
295 ad_cache = check_cache(host, nconf->nc_netid);
296 if (ad_cache != NULL) {
297 addr = ad_cache->ac_taddr;
298 client = clnt_tli_create(RPC_ANYFD, nconf, addr,
299 (rpcprog_t)RPCBPROG, (rpcvers_t)RPCBVERS4, 0, 0);
300 if (client != NULL) {
302 *targaddr = strdup(ad_cache->ac_uaddr);
303 rwlock_unlock(&rpcbaddr_cache_lock);
306 addr_to_delete.len = addr->len;
307 addr_to_delete.buf = (char *)malloc(addr->len);
308 if (addr_to_delete.buf == NULL) {
309 addr_to_delete.len = 0;
311 memcpy(addr_to_delete.buf, addr->buf, addr->len);
314 rwlock_unlock(&rpcbaddr_cache_lock);
315 if (addr_to_delete.len != 0) {
317 * Assume this may be due to cache data being
320 rwlock_wrlock(&rpcbaddr_cache_lock);
321 delete_cache(&addr_to_delete);
322 rwlock_unlock(&rpcbaddr_cache_lock);
323 free(addr_to_delete.buf);
325 if (!__rpc_nconf2sockinfo(nconf, &si)) {
326 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
327 assert(client == NULL);
331 memset(&hints, 0, sizeof hints);
332 hints.ai_family = si.si_af;
333 hints.ai_socktype = si.si_socktype;
334 hints.ai_protocol = si.si_proto;
336 LIBTIRPC_DEBUG(3, ("getclnthandle: trying netid %s family %d proto %d socktype %d\n",
337 nconf->nc_netid, si.si_af, si.si_proto, si.si_socktype));
339 if (nconf->nc_protofmly != NULL && strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0) {
340 client = local_rpcb();
342 LIBTIRPC_DEBUG(1, ("getclnthandle: %s",
343 clnt_spcreateerror("local_rpcb failed")));
346 struct sockaddr_un sun;
348 *targaddr = malloc(sizeof(sun.sun_path));
349 strncpy(*targaddr, _PATH_RPCBINDSOCK,
350 sizeof(sun.sun_path));
354 if (getaddrinfo(host, "sunrpc", &hints, &res) != 0) {
355 rpc_createerr.cf_stat = RPC_UNKNOWNHOST;
356 assert(client == NULL);
361 for (tres = res; tres != NULL; tres = tres->ai_next) {
362 taddr.buf = tres->ai_addr;
363 taddr.len = taddr.maxlen = tres->ai_addrlen;
365 if (libtirpc_debug_level > 3 && log_stderr) {
369 ua = taddr2uaddr(nconf, &taddr);
370 fprintf(stderr, "Got it [%s]\n", ua);
373 fprintf(stderr, "\tnetbuf len = %d, maxlen = %d\n",
374 taddr.len, taddr.maxlen);
375 fprintf(stderr, "\tAddress is ");
376 for (i = 0; i < taddr.len; i++)
377 fprintf(stderr, "%u.", ((char *)(taddr.buf))[i]);
378 fprintf(stderr, "\n");
381 client = clnt_tli_create(RPC_ANYFD, nconf, &taddr,
382 (rpcprog_t)RPCBPROG, (rpcvers_t)RPCBVERS4, 0, 0);
384 LIBTIRPC_DEBUG(1, ("getclnthandle: %s",
385 clnt_spcreateerror("clnt_tli_create failed")));
389 tmpaddr = targaddr ? taddr2uaddr(nconf, &taddr) : NULL;
390 add_cache(host, nconf->nc_netid, &taddr, tmpaddr);
399 if (!client && targaddr)
405 #define IN4_LOCALHOST_STRING "127.0.0.1"
406 #define IN6_LOCALHOST_STRING "::1"
409 * This routine will return a client handle that is connected to the local
410 * rpcbind. Returns NULL on error and free's everything.
416 static struct netconfig *loopnconf;
417 static char *hostname;
418 extern mutex_t loopnconf_lock;
422 struct sockaddr_un sun;
425 * Try connecting to the local rpcbind through a local socket
426 * first. If this doesn't work, try all transports defined in
427 * the netconfig file.
429 memset(&sun, 0, sizeof sun);
430 sock = socket(AF_LOCAL, SOCK_STREAM, 0);
433 sun.sun_family = AF_LOCAL;
434 strcpy(sun.sun_path, _PATH_RPCBINDSOCK);
435 nbuf.len = SUN_LEN(&sun);
436 nbuf.maxlen = sizeof (struct sockaddr_un);
439 tsize = __rpc_get_t_size(AF_LOCAL, 0, 0);
440 client = clnt_vc_create(sock, &nbuf, (rpcprog_t)RPCBPROG,
441 (rpcvers_t)RPCBVERS, tsize, tsize);
443 if (client != NULL) {
444 /* Mark the socket to be closed in destructor */
445 (void) CLNT_CONTROL(client, CLSET_FD_CLOSE, NULL);
449 /* Nobody needs this socket anymore; free the descriptor. */
454 /* VARIABLES PROTECTED BY loopnconf_lock: loopnconf */
455 mutex_lock(&loopnconf_lock);
456 if (loopnconf == NULL) {
457 struct netconfig *nconf, *tmpnconf = NULL;
461 nc_handle = setnetconfig();
462 if (nc_handle == NULL) {
463 /* fails to open netconfig file */
464 syslog (LOG_ERR, "rpc: failed to open " NETCONFIG);
465 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
466 mutex_unlock(&loopnconf_lock);
469 while ((nconf = getnetconfig(nc_handle)) != NULL) {
471 if ((strcmp(nconf->nc_protofmly, NC_INET6) == 0 ||
475 strcmp(nconf->nc_protofmly, NC_INET) == 0) &&
476 (nconf->nc_semantics == NC_TPI_COTS ||
477 nconf->nc_semantics == NC_TPI_COTS_ORD)) {
478 fd = __rpc_nconf2fd(nconf);
480 * Can't create a socket, assume that
481 * this family isn't configured in the kernel.
487 if (!strcmp(nconf->nc_protofmly, NC_INET))
488 hostname = IN4_LOCALHOST_STRING;
490 hostname = IN6_LOCALHOST_STRING;
493 if (tmpnconf == NULL) {
494 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
495 mutex_unlock(&loopnconf_lock);
498 loopnconf = getnetconfigent(tmpnconf->nc_netid);
499 /* loopnconf is never freed */
500 endnetconfig(nc_handle);
502 mutex_unlock(&loopnconf_lock);
503 client = getclnthandle(hostname, loopnconf, NULL);
508 * Set a mapping between program, version and address.
509 * Calls the rpcbind service to do the mapping.
512 rpcb_set(program, version, nconf, address)
515 const struct netconfig *nconf; /* Network structure of transport */
516 const struct netbuf *address; /* Services netconfig address */
523 /* parameter checking */
525 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
528 if (address == NULL) {
529 rpc_createerr.cf_stat = RPC_UNKNOWNADDR;
532 client = local_rpcb();
537 /* convert to universal */
538 /*LINTED const castaway*/
539 parms.r_addr = taddr2uaddr((struct netconfig *) nconf,
540 (struct netbuf *)address);
542 CLNT_DESTROY(client);
543 rpc_createerr.cf_stat = RPC_N2AXLATEFAILURE;
544 return (FALSE); /* no universal address */
546 parms.r_prog = program;
547 parms.r_vers = version;
548 parms.r_netid = nconf->nc_netid;
550 * Though uid is not being used directly, we still send it for
551 * completeness. For non-unix platforms, perhaps some other
552 * string or an empty string can be sent.
554 (void) snprintf(uidbuf, sizeof uidbuf, "%d", geteuid());
555 parms.r_owner = uidbuf;
557 CLNT_CALL(client, (rpcproc_t)RPCBPROC_SET, (xdrproc_t) xdr_rpcb,
558 (char *)&parms, (xdrproc_t) xdr_bool,
559 (char *)&rslt, tottimeout);
561 CLNT_DESTROY(client);
567 * Remove the mapping between program, version and netbuf address.
568 * Calls the rpcbind service to do the un-mapping.
569 * If netbuf is NULL, unset for all the transports, otherwise unset
570 * only for the given transport.
573 rpcb_unset(program, version, nconf)
576 const struct netconfig *nconf;
583 client = local_rpcb();
588 parms.r_prog = program;
589 parms.r_vers = version;
591 parms.r_netid = nconf->nc_netid;
593 /*LINTED const castaway*/
594 parms.r_netid = (char *) &nullstring[0]; /* unsets all */
596 /*LINTED const castaway*/
597 parms.r_addr = (char *) &nullstring[0];
598 (void) snprintf(uidbuf, sizeof uidbuf, "%d", geteuid());
599 parms.r_owner = uidbuf;
601 CLNT_CALL(client, (rpcproc_t)RPCBPROC_UNSET, (xdrproc_t) xdr_rpcb,
602 (char *)(void *)&parms, (xdrproc_t) xdr_bool,
603 (char *)(void *)&rslt, tottimeout);
605 CLNT_DESTROY(client);
611 * From the merged list, find the appropriate entry
613 static struct netbuf *
614 got_entry(relp, nconf)
615 rpcb_entry_list_ptr relp;
616 const struct netconfig *nconf;
618 struct netbuf *na = NULL;
619 rpcb_entry_list_ptr sp;
622 for (sp = relp; sp != NULL; sp = sp->rpcb_entry_next) {
623 rmap = &sp->rpcb_entry_map;
624 if ((strcmp(nconf->nc_proto, rmap->r_nc_proto) == 0) &&
625 (strcmp(nconf->nc_protofmly, rmap->r_nc_protofmly) == 0) &&
626 (nconf->nc_semantics == rmap->r_nc_semantics) &&
627 (rmap->r_maddr != NULL) && (rmap->r_maddr[0] != 0)) {
628 na = uaddr2taddr(nconf, rmap->r_maddr);
629 LIBTIRPC_DEBUG(3, ("got_entry: Remote address is [%s] %s",
630 rmap->r_maddr, (na ? "Resolvable" : "Not Resolvable")));
638 * Quick check to see if rpcbind is up. Tries to connect over
644 struct netconfig *nconf;
645 struct sockaddr_un sun;
650 localhandle = setnetconfig();
651 while ((nconf = getnetconfig(localhandle)) != NULL) {
652 if (nconf->nc_protofmly != NULL &&
653 strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0)
659 endnetconfig(localhandle);
661 memset(&sun, 0, sizeof sun);
662 sock = socket(AF_LOCAL, SOCK_STREAM, 0);
665 sun.sun_family = AF_LOCAL;
666 strncpy(sun.sun_path, _PATH_RPCBINDSOCK, sizeof(sun.sun_path));
668 if (connect(sock, (struct sockaddr *)&sun, sizeof(sun)) < 0) {
679 * An internal function which optimizes rpcb_getaddr function. It also
680 * returns the client handle that it uses to contact the remote rpcbind.
682 * The algorithm used: If the transports is TCP or UDP, it first tries
683 * version 2 (portmap), 4 and then 3 (svr4). This order should be
684 * changed in the next OS release to 4, 2 and 3. We are assuming that by
685 * that time, version 4 would be available on many machines on the network.
686 * With this algorithm, we get performance as well as a plan for
687 * obsoleting version 2.
689 * For all other transports, the algorithm remains as 4 and then 3.
691 * XXX: Due to some problems with t_connect(), we do not reuse the same client
692 * handle for COTS cases and hence in these cases we do not return the
693 * client handle. This code will change if t_connect() ever
694 * starts working properly. Also look under clnt_vc.c.
697 __rpcb_findaddr_timed(program, version, nconf, host, clpp, tp)
700 const struct netconfig *nconf;
706 static bool_t check_rpcbind = TRUE;
708 CLIENT *client = NULL;
710 enum clnt_stat clnt_st;
713 struct netbuf *address = NULL;
714 rpcvers_t start_vers = RPCBVERS4;
715 struct netbuf servaddr;
717 /* parameter checking */
719 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
726 * Use default total timeout if no timeout is specified.
732 /* Try version 2 for TCP or UDP */
733 if (strcmp(nconf->nc_protofmly, NC_INET) == 0) {
735 struct netbuf remote;
736 rpcvers_t pmapvers = 2;
737 struct pmap pmapparms;
740 * Try UDP only - there are some portmappers out
741 * there that use UDP only.
743 if (strcmp(nconf->nc_proto, NC_TCP) == 0) {
744 struct netconfig *newnconf;
746 if ((newnconf = getnetconfigent("udp")) == NULL) {
747 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
750 client = getclnthandle(host, newnconf, &parms.r_addr);
751 freenetconfigent(newnconf);
752 } else if (strcmp(nconf->nc_proto, NC_UDP) == 0)
753 client = getclnthandle(host, nconf, &parms.r_addr);
760 * Set version and retry timeout.
762 CLNT_CONTROL(client, CLSET_RETRY_TIMEOUT, (char *)&rpcbrmttime);
763 CLNT_CONTROL(client, CLSET_VERS, (char *)&pmapvers);
765 pmapparms.pm_prog = program;
766 pmapparms.pm_vers = version;
767 pmapparms.pm_prot = strcmp(nconf->nc_proto, NC_TCP) ?
768 IPPROTO_UDP : IPPROTO_TCP;
769 pmapparms.pm_port = 0; /* not needed */
770 clnt_st = CLNT_CALL(client, (rpcproc_t)PMAPPROC_GETPORT,
771 (xdrproc_t) xdr_pmap, (caddr_t)(void *)&pmapparms,
772 (xdrproc_t) xdr_u_short, (caddr_t)(void *)&port,
774 if (clnt_st != RPC_SUCCESS) {
775 if ((clnt_st == RPC_PROGVERSMISMATCH) ||
776 (clnt_st == RPC_PROGUNAVAIL))
778 rpc_createerr.cf_stat = RPC_PMAPFAILURE;
779 clnt_geterr(client, &rpc_createerr.cf_error);
781 } else if (port == 0) {
783 rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
787 CLNT_CONTROL(client, CLGET_SVC_ADDR, (char *)&remote);
788 if (((address = (struct netbuf *)
789 malloc(sizeof (struct netbuf))) == NULL) ||
790 ((address->buf = (char *)
791 malloc(remote.len)) == NULL)) {
792 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
793 clnt_geterr(client, &rpc_createerr.cf_error);
800 memcpy(address->buf, remote.buf, remote.len);
801 memcpy(&((char *)address->buf)[sizeof (short)],
802 (char *)(void *)&port, sizeof (short));
803 address->len = address->maxlen = remote.len;
810 parms.r_prog = program;
811 parms.r_vers = version;
812 parms.r_netid = nconf->nc_netid;
815 * rpcbind ignores the r_owner field in GETADDR requests, but we
816 * need to give xdr_rpcb something to gnaw on. Might as well make
817 * it something human readable for when we see these in captures.
819 parms.r_owner = RPCB_OWNER_STRING;
821 /* Now the same transport is to be used to get the address */
822 if (client && ((nconf->nc_semantics == NC_TPI_COTS_ORD) ||
823 (nconf->nc_semantics == NC_TPI_COTS))) {
824 /* A CLTS type of client - destroy it */
825 CLNT_DESTROY(client);
831 if (client == NULL) {
832 client = getclnthandle(host, nconf, &parms.r_addr);
833 if (client == NULL) {
837 if (parms.r_addr == NULL) {
838 /*LINTED const castaway*/
839 parms.r_addr = (char *) &nullstring[0];
842 /* First try from start_vers(4) and then version 3 (RPCBVERS) */
844 CLNT_CONTROL(client, CLSET_RETRY_TIMEOUT, (char *) &rpcbrmttime);
845 for (vers = start_vers; vers >= RPCBVERS; vers--) {
846 /* Set the version */
847 CLNT_CONTROL(client, CLSET_VERS, (char *)(void *)&vers);
848 clnt_st = CLNT_CALL(client, (rpcproc_t)RPCBPROC_GETADDR,
849 (xdrproc_t) xdr_rpcb, (char *)(void *)&parms,
850 (xdrproc_t) xdr_wrapstring, (char *)(void *) &ua, *tp);
851 if (clnt_st == RPC_SUCCESS) {
852 if ((ua == NULL) || (ua[0] == 0)) {
853 /* address unknown */
854 rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
857 address = uaddr2taddr(nconf, ua);
858 LIBTIRPC_DEBUG(3, ("__rpcb_findaddr_timed: Remote address is [%s] %s",
859 ua, (address ? "Resolvable" : "Not Resolvable")));
861 xdr_free((xdrproc_t)xdr_wrapstring,
862 (char *)(void *)&ua);
865 /* We don't know about your universal address */
866 rpc_createerr.cf_stat = RPC_N2AXLATEFAILURE;
869 CLNT_CONTROL(client, CLGET_SVC_ADDR,
870 (char *)(void *)&servaddr);
871 __rpc_fixup_addr(address, &servaddr);
873 } else if (clnt_st == RPC_PROGVERSMISMATCH) {
874 struct rpc_err rpcerr;
875 clnt_geterr(client, &rpcerr);
876 if (rpcerr.re_vers.low > RPCBVERS4)
877 goto error; /* a new version, can't handle */
878 } else if (clnt_st != RPC_PROGUNAVAIL) {
879 /* Cant handle this error */
880 rpc_createerr.cf_stat = clnt_st;
881 clnt_geterr(client, &rpc_createerr.cf_error);
886 if ((address == NULL) || (address->len == 0)) {
887 rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
888 clnt_geterr(client, &rpc_createerr.cf_error);
893 CLNT_DESTROY(client);
897 if (nconf->nc_semantics != NC_TPI_CLTS) {
898 /* This client is the connectionless one */
900 CLNT_DESTROY(client);
907 CLNT_DESTROY(client);
909 if (parms.r_addr != NULL && parms.r_addr != nullstring)
916 * Find the mapped address for program, version.
917 * Calls the rpcbind service remotely to do the lookup.
918 * Uses the transport specified in nconf.
919 * Returns FALSE (0) if no map exists, else returns 1.
921 * Assuming that the address is all properly allocated
924 rpcb_getaddr(program, version, nconf, address, host)
927 const struct netconfig *nconf;
928 struct netbuf *address;
933 if ((na = __rpcb_findaddr_timed(program, version,
934 (struct netconfig *) nconf, (char *) host,
935 (CLIENT **) NULL, (struct timeval *) NULL)) == NULL)
938 if (na->len > address->maxlen) {
939 /* Too long address */
942 rpc_createerr.cf_stat = RPC_FAILED;
945 memcpy(address->buf, na->buf, (size_t)na->len);
946 address->len = na->len;
953 * Get a copy of the current maps.
954 * Calls the rpcbind service remotely to get the maps.
956 * It returns only a list of the services
957 * It returns NULL on failure.
960 rpcb_getmaps(nconf, host)
961 const struct netconfig *nconf;
964 rpcblist_ptr head = NULL;
966 enum clnt_stat clnt_st;
969 client = getclnthandle(host, nconf, NULL);
970 if (client == NULL) {
973 clnt_st = CLNT_CALL(client, (rpcproc_t)RPCBPROC_DUMP,
974 (xdrproc_t) xdr_void, NULL, (xdrproc_t) xdr_rpcblist_ptr,
975 (char *)(void *)&head, tottimeout);
976 if (clnt_st == RPC_SUCCESS)
979 if ((clnt_st != RPC_PROGVERSMISMATCH) &&
980 (clnt_st != RPC_PROGUNAVAIL)) {
981 rpc_createerr.cf_stat = RPC_RPCBFAILURE;
982 clnt_geterr(client, &rpc_createerr.cf_error);
986 /* fall back to earlier version */
987 CLNT_CONTROL(client, CLGET_VERS, (char *)(void *)&vers);
988 if (vers == RPCBVERS4) {
990 CLNT_CONTROL(client, CLSET_VERS, (char *)(void *)&vers);
991 if (CLNT_CALL(client, (rpcproc_t)RPCBPROC_DUMP,
992 (xdrproc_t) xdr_void, NULL, (xdrproc_t) xdr_rpcblist_ptr,
993 (char *)(void *)&head, tottimeout) == RPC_SUCCESS)
996 rpc_createerr.cf_stat = RPC_RPCBFAILURE;
997 clnt_geterr(client, &rpc_createerr.cf_error);
1000 CLNT_DESTROY(client);
1005 * rpcbinder remote-call-service interface.
1006 * This routine is used to call the rpcbind remote call service
1007 * which will look up a service program in the address maps, and then
1008 * remotely call that routine with the given parameters. This allows
1009 * programs to do a lookup and call in one step.
1012 rpcb_rmtcall(nconf, host, prog, vers, proc, xdrargs, argsp,
1013 xdrres, resp, tout, addr_ptr)
1014 const struct netconfig *nconf; /* Netconfig structure */
1015 const char *host; /* Remote host name */
1018 rpcproc_t proc; /* Remote proc identifiers */
1019 xdrproc_t xdrargs, xdrres; /* XDR routines */
1020 caddr_t argsp, resp; /* Argument and Result */
1021 struct timeval tout; /* Timeout value for this call */
1022 const struct netbuf *addr_ptr; /* Preallocated netbuf address */
1025 enum clnt_stat stat;
1026 struct r_rpcb_rmtcallargs a;
1027 struct r_rpcb_rmtcallres r;
1028 rpcvers_t rpcb_vers;
1031 client = getclnthandle(host, nconf, NULL);
1032 if (client == NULL) {
1033 return (RPC_FAILED);
1035 /*LINTED const castaway*/
1036 CLNT_CONTROL(client, CLSET_RETRY_TIMEOUT, (char *)(void *)&rmttimeout);
1040 a.args.args_val = argsp;
1041 a.xdr_args = xdrargs;
1043 r.results.results_val = resp;
1046 for (rpcb_vers = RPCBVERS4; rpcb_vers >= RPCBVERS; rpcb_vers--) {
1047 CLNT_CONTROL(client, CLSET_VERS, (char *)(void *)&rpcb_vers);
1048 stat = CLNT_CALL(client, (rpcproc_t)RPCBPROC_CALLIT,
1049 (xdrproc_t) xdr_rpcb_rmtcallargs, (char *)(void *)&a,
1050 (xdrproc_t) xdr_rpcb_rmtcallres, (char *)(void *)&r, tout);
1051 if ((stat == RPC_SUCCESS) && (addr_ptr != NULL)) {
1053 /*LINTED const castaway*/
1054 na = uaddr2taddr((struct netconfig *) nconf, r.addr);
1056 stat = RPC_N2AXLATEFAILURE;
1057 /*LINTED const castaway*/
1058 ((struct netbuf *) addr_ptr)->len = 0;
1061 if (na->len > addr_ptr->maxlen) {
1062 /* Too long address */
1063 stat = RPC_FAILED; /* XXX A better error no */
1066 /*LINTED const castaway*/
1067 ((struct netbuf *) addr_ptr)->len = 0;
1070 memcpy(addr_ptr->buf, na->buf, (size_t)na->len);
1071 /*LINTED const castaway*/
1072 ((struct netbuf *)addr_ptr)->len = na->len;
1076 } else if ((stat != RPC_PROGVERSMISMATCH) &&
1077 (stat != RPC_PROGUNAVAIL)) {
1082 CLNT_DESTROY(client);
1084 xdr_free((xdrproc_t) xdr_wrapstring, (char *)(void *)&r.addr);
1089 * Gets the time on the remote host.
1090 * Returns 1 if succeeds else 0.
1093 rpcb_gettime(host, timep)
1097 CLIENT *client = NULL;
1099 struct netconfig *nconf;
1103 if ((host == NULL) || (host[0] == 0)) {
1108 if ((handle = __rpc_setconf("netpath")) == NULL) {
1109 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
1112 rpc_createerr.cf_stat = RPC_SUCCESS;
1113 while (client == NULL) {
1114 if ((nconf = __rpc_getconf(handle)) == NULL) {
1115 if (rpc_createerr.cf_stat == RPC_SUCCESS)
1116 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
1119 client = getclnthandle(host, nconf, NULL);
1123 __rpc_endconf(handle);
1124 if (client == (CLIENT *) NULL) {
1128 st = CLNT_CALL(client, (rpcproc_t)RPCBPROC_GETTIME,
1129 (xdrproc_t) xdr_void, NULL,
1130 (xdrproc_t) xdr_int, (char *)(void *)timep, tottimeout);
1132 if ((st == RPC_PROGVERSMISMATCH) || (st == RPC_PROGUNAVAIL)) {
1133 CLNT_CONTROL(client, CLGET_VERS, (char *)(void *)&vers);
1134 if (vers == RPCBVERS4) {
1135 /* fall back to earlier version */
1137 CLNT_CONTROL(client, CLSET_VERS, (char *)(void *)&vers);
1138 st = CLNT_CALL(client, (rpcproc_t)RPCBPROC_GETTIME,
1139 (xdrproc_t) xdr_void, NULL,
1140 (xdrproc_t) xdr_int, (char *)(void *)timep,
1144 CLNT_DESTROY(client);
1145 return (st == RPC_SUCCESS? TRUE: FALSE);
1149 * Converts taddr to universal address. This routine should never
1150 * really be called because local n2a libraries are always provided.
1153 rpcb_taddr2uaddr(nconf, taddr)
1154 struct netconfig *nconf;
1155 struct netbuf *taddr;
1161 /* parameter checking */
1162 if (nconf == NULL) {
1163 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
1166 if (taddr == NULL) {
1167 rpc_createerr.cf_stat = RPC_UNKNOWNADDR;
1170 client = local_rpcb();
1175 CLNT_CALL(client, (rpcproc_t)RPCBPROC_TADDR2UADDR,
1176 (xdrproc_t) xdr_netbuf, (char *)(void *)taddr,
1177 (xdrproc_t) xdr_wrapstring, (char *)(void *)&uaddr, tottimeout);
1178 CLNT_DESTROY(client);
1183 * Converts universal address to netbuf. This routine should never
1184 * really be called because local n2a libraries are always provided.
1187 rpcb_uaddr2taddr(nconf, uaddr)
1188 struct netconfig *nconf;
1192 struct netbuf *taddr;
1195 /* parameter checking */
1196 if (nconf == NULL) {
1197 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
1200 if (uaddr == NULL) {
1201 rpc_createerr.cf_stat = RPC_UNKNOWNADDR;
1204 client = local_rpcb();
1209 taddr = (struct netbuf *)calloc(1, sizeof (struct netbuf));
1210 if (taddr == NULL) {
1211 CLNT_DESTROY(client);
1214 if (CLNT_CALL(client, (rpcproc_t)RPCBPROC_UADDR2TADDR,
1215 (xdrproc_t) xdr_wrapstring, (char *)(void *)&uaddr,
1216 (xdrproc_t) xdr_netbuf, (char *)(void *)taddr,
1217 tottimeout) != RPC_SUCCESS) {
1221 CLNT_DESTROY(client);