1 /* $NetBSD: rpcb_clnt.c,v 1.6 2000/07/16 06:41:43 itojun Exp $ */
4 * Copyright (c) 2010, Oracle America, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 * - Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 * - Neither the name of the "Oracle America, Inc." nor the names of its
15 * contributors may be used to endorse or promote products derived
16 * from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
31 /* #ident "@(#)rpcb_clnt.c 1.27 94/04/24 SMI" */
34 #if defined(LIBC_SCCS) && !defined(lint)
35 static char sccsid[] = "@(#)rpcb_clnt.c 1.30 89/06/21 Copyr 1988 Sun Micro";
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
42 * interface to rpcbind rpc service.
45 #include "namespace.h"
46 #include "reentrant.h"
47 #include <sys/types.h>
48 #include <sys/socket.h>
50 #include <sys/utsname.h>
52 #include <rpc/rpcb_prot.h>
53 #include <rpc/nettype.h>
54 #include <netconfig.h>
56 #include <netinet/in.h> /* FOR IPPROTO_TCP/UDP definitions */
57 #include <rpc/pmap_prot.h>
66 #include "un-namespace.h"
71 static struct timeval tottimeout = { 60, 0 };
72 static const struct timeval rmttimeout = { 3, 0 };
73 static struct timeval rpcbrmttime = { 15, 0 };
75 extern bool_t xdr_wrapstring(XDR *, char **);
77 static const char nullstring[] = "\000";
81 struct address_cache {
85 struct netbuf *ac_taddr;
86 struct address_cache *ac_next;
89 static struct address_cache *front;
92 #define CLCR_GET_RPCB_TIMEOUT 1
93 #define CLCR_SET_RPCB_TIMEOUT 2
96 extern int __rpc_lowvers;
98 static struct address_cache *check_cache(const char *, const char *);
99 static void delete_cache(struct netbuf *);
100 static void add_cache(const char *, const char *, struct netbuf *, char *);
101 static CLIENT *getclnthandle(const char *, const struct netconfig *, char **);
102 static CLIENT *local_rpcb(void);
103 static struct netbuf *got_entry(rpcb_entry_list_ptr, const struct netconfig *);
106 * This routine adjusts the timeout used for calls to the remote rpcbind.
107 * Also, this routine can be used to set the use of portmapper version 2
108 * only when doing rpc_broadcasts
109 * These are private routines that may not be provided in future releases.
112 __rpc_control(request, info)
117 case CLCR_GET_RPCB_TIMEOUT:
118 *(struct timeval *)info = tottimeout;
120 case CLCR_SET_RPCB_TIMEOUT:
121 tottimeout = *(struct timeval *)info;
123 case CLCR_SET_LOWVERS:
124 __rpc_lowvers = *(int *)info;
126 case CLCR_GET_LOWVERS:
127 *(int *)info = __rpc_lowvers;
136 * It might seem that a reader/writer lock would be more reasonable here.
137 * However because getclnthandle(), the only user of the cache functions,
138 * may do a delete_cache() operation if a check_cache() fails to return an
139 * address useful to clnt_tli_create(), we may as well use a mutex.
142 * As it turns out, if the cache lock is *not* a reader/writer lock, we will
143 * block all clnt_create's if we are trying to connect to a host that's down,
144 * since the lock will be held all during that time.
148 * The routines check_cache(), add_cache(), delete_cache() manage the
149 * cache of rpcbind addresses for (host, netid).
152 static struct address_cache *
153 check_cache(host, netid)
154 const char *host, *netid;
156 struct address_cache *cptr;
158 /* READ LOCK HELD ON ENTRY: rpcbaddr_cache_lock */
160 for (cptr = front; cptr != NULL; cptr = cptr->ac_next) {
161 if (!strcmp(cptr->ac_host, host) &&
162 !strcmp(cptr->ac_netid, netid)) {
164 fprintf(stderr, "Found cache entry for %s: %s\n",
170 return ((struct address_cache *) NULL);
177 struct address_cache *cptr, *prevptr = NULL;
179 /* WRITE LOCK HELD ON ENTRY: rpcbaddr_cache_lock */
180 for (cptr = front; cptr != NULL; cptr = cptr->ac_next) {
181 if (!memcmp(cptr->ac_taddr->buf, addr->buf, addr->len)) {
183 free(cptr->ac_netid);
184 free(cptr->ac_taddr->buf);
185 free(cptr->ac_taddr);
187 free(cptr->ac_uaddr);
189 prevptr->ac_next = cptr->ac_next;
191 front = cptr->ac_next;
201 add_cache(host, netid, taddr, uaddr)
202 const char *host, *netid;
204 struct netbuf *taddr;
206 struct address_cache *ad_cache, *cptr, *prevptr;
208 ad_cache = (struct address_cache *)
209 malloc(sizeof (struct address_cache));
213 ad_cache->ac_host = strdup(host);
214 ad_cache->ac_netid = strdup(netid);
215 ad_cache->ac_uaddr = uaddr ? strdup(uaddr) : NULL;
216 ad_cache->ac_taddr = (struct netbuf *)malloc(sizeof (struct netbuf));
217 if (!ad_cache->ac_host || !ad_cache->ac_netid || !ad_cache->ac_taddr ||
218 (uaddr && !ad_cache->ac_uaddr)) {
221 ad_cache->ac_taddr->len = ad_cache->ac_taddr->maxlen = taddr->len;
222 ad_cache->ac_taddr->buf = (char *) malloc(taddr->len);
223 if (ad_cache->ac_taddr->buf == NULL) {
225 if (ad_cache->ac_host)
226 free(ad_cache->ac_host);
227 if (ad_cache->ac_netid)
228 free(ad_cache->ac_netid);
229 if (ad_cache->ac_uaddr)
230 free(ad_cache->ac_uaddr);
231 if (ad_cache->ac_taddr)
232 free(ad_cache->ac_taddr);
236 memcpy(ad_cache->ac_taddr->buf, taddr->buf, taddr->len);
238 fprintf(stderr, "Added to cache: %s : %s\n", host, netid);
241 /* VARIABLES PROTECTED BY rpcbaddr_cache_lock: cptr */
243 rwlock_wrlock(&rpcbaddr_cache_lock);
244 if (cachesize < CACHESIZE) {
245 ad_cache->ac_next = front;
249 /* Free the last entry */
252 while (cptr->ac_next) {
254 cptr = cptr->ac_next;
258 fprintf(stderr, "Deleted from cache: %s : %s\n",
259 cptr->ac_host, cptr->ac_netid);
262 free(cptr->ac_netid);
263 free(cptr->ac_taddr->buf);
264 free(cptr->ac_taddr);
266 free(cptr->ac_uaddr);
269 prevptr->ac_next = NULL;
270 ad_cache->ac_next = front;
274 ad_cache->ac_next = NULL;
278 rwlock_unlock(&rpcbaddr_cache_lock);
282 * This routine will return a client handle that is connected to the
283 * rpcbind. If targaddr is non-NULL, the "universal address" of the
284 * host will be stored in *targaddr; the caller is responsible for
285 * freeing this string.
286 * On error, returns NULL and free's everything.
289 getclnthandle(host, nconf, targaddr)
291 const struct netconfig *nconf;
295 struct netbuf *addr, taddr;
296 struct netbuf addr_to_delete;
297 struct __rpc_sockinfo si;
298 struct addrinfo hints, *res, *tres;
299 struct address_cache *ad_cache;
302 /* VARIABLES PROTECTED BY rpcbaddr_cache_lock: ad_cache */
304 /* Get the address of the rpcbind. Check cache first */
306 addr_to_delete.len = 0;
307 rwlock_rdlock(&rpcbaddr_cache_lock);
310 ad_cache = check_cache(host, nconf->nc_netid);
311 if (ad_cache != NULL) {
312 addr = ad_cache->ac_taddr;
313 client = clnt_tli_create(RPC_ANYFD, nconf, addr,
314 (rpcprog_t)RPCBPROG, (rpcvers_t)RPCBVERS4, 0, 0);
315 if (client != NULL) {
317 *targaddr = strdup(ad_cache->ac_uaddr);
318 rwlock_unlock(&rpcbaddr_cache_lock);
321 addr_to_delete.len = addr->len;
322 addr_to_delete.buf = (char *)malloc(addr->len);
323 if (addr_to_delete.buf == NULL) {
324 addr_to_delete.len = 0;
326 memcpy(addr_to_delete.buf, addr->buf, addr->len);
329 rwlock_unlock(&rpcbaddr_cache_lock);
330 if (addr_to_delete.len != 0) {
332 * Assume this may be due to cache data being
335 rwlock_wrlock(&rpcbaddr_cache_lock);
336 delete_cache(&addr_to_delete);
337 rwlock_unlock(&rpcbaddr_cache_lock);
338 free(addr_to_delete.buf);
340 if (!__rpc_nconf2sockinfo(nconf, &si)) {
341 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
345 memset(&hints, 0, sizeof hints);
346 hints.ai_family = si.si_af;
347 hints.ai_socktype = si.si_socktype;
348 hints.ai_protocol = si.si_proto;
351 printf("trying netid %s family %d proto %d socktype %d\n",
352 nconf->nc_netid, si.si_af, si.si_proto, si.si_socktype);
355 if (nconf->nc_protofmly != NULL && strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0) {
356 client = local_rpcb();
359 clnt_pcreateerror("rpcbind clnt interface");
363 struct sockaddr_un sun;
365 *targaddr = malloc(sizeof(sun.sun_path));
366 if (*targaddr == NULL) {
367 CLNT_DESTROY(client);
370 strncpy(*targaddr, _PATH_RPCBINDSOCK,
371 sizeof(sun.sun_path));
376 if (getaddrinfo(host, "sunrpc", &hints, &res) != 0) {
377 rpc_createerr.cf_stat = RPC_UNKNOWNHOST;
382 for (tres = res; tres != NULL; tres = tres->ai_next) {
383 taddr.buf = tres->ai_addr;
384 taddr.len = taddr.maxlen = tres->ai_addrlen;
390 ua = taddr2uaddr(nconf, &taddr);
391 fprintf(stderr, "Got it [%s]\n", ua);
400 fprintf(stderr, "\tnetbuf len = %d, maxlen = %d\n",
401 taddr.len, taddr.maxlen);
402 fprintf(stderr, "\tAddress is ");
403 for (i = 0; i < taddr.len; i++)
404 fprintf(stderr, "%u.", ((char *)(taddr.buf))[i]);
405 fprintf(stderr, "\n");
408 client = clnt_tli_create(RPC_ANYFD, nconf, &taddr,
409 (rpcprog_t)RPCBPROG, (rpcvers_t)RPCBVERS4, 0, 0);
412 clnt_pcreateerror("rpcbind clnt interface");
417 tmpaddr = targaddr ? taddr2uaddr(nconf, &taddr) : NULL;
418 add_cache(host, nconf->nc_netid, &taddr, tmpaddr);
430 #define IN4_LOCALHOST_STRING "127.0.0.1"
431 #define IN6_LOCALHOST_STRING "::1"
434 * This routine will return a client handle that is connected to the local
435 * rpcbind. Returns NULL on error and free's everything.
441 static struct netconfig *loopnconf;
442 static char *hostname;
446 struct sockaddr_un sun;
449 * Try connecting to the local rpcbind through a local socket
450 * first. If this doesn't work, try all transports defined in
451 * the netconfig file.
453 memset(&sun, 0, sizeof sun);
454 sock = _socket(AF_LOCAL, SOCK_STREAM, 0);
457 sun.sun_family = AF_LOCAL;
458 strcpy(sun.sun_path, _PATH_RPCBINDSOCK);
459 nbuf.len = sun.sun_len = SUN_LEN(&sun);
460 nbuf.maxlen = sizeof (struct sockaddr_un);
463 tsize = __rpc_get_t_size(AF_LOCAL, 0, 0);
464 client = clnt_vc_create(sock, &nbuf, (rpcprog_t)RPCBPROG,
465 (rpcvers_t)RPCBVERS, tsize, tsize);
467 if (client != NULL) {
468 /* Mark the socket to be closed in destructor */
469 (void) CLNT_CONTROL(client, CLSET_FD_CLOSE, NULL);
473 /* Nobody needs this socket anymore; free the descriptor. */
478 /* VARIABLES PROTECTED BY loopnconf_lock: loopnconf */
479 mutex_lock(&loopnconf_lock);
480 if (loopnconf == NULL) {
481 struct netconfig *nconf, *tmpnconf = NULL;
485 nc_handle = setnetconfig();
486 if (nc_handle == NULL) {
487 /* fails to open netconfig file */
488 syslog (LOG_ERR, "rpc: failed to open " NETCONFIG);
489 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
490 mutex_unlock(&loopnconf_lock);
493 while ((nconf = getnetconfig(nc_handle)) != NULL) {
495 if ((strcmp(nconf->nc_protofmly, NC_INET6) == 0 ||
499 strcmp(nconf->nc_protofmly, NC_INET) == 0) &&
500 (nconf->nc_semantics == NC_TPI_COTS ||
501 nconf->nc_semantics == NC_TPI_COTS_ORD)) {
502 fd = __rpc_nconf2fd(nconf);
504 * Can't create a socket, assume that
505 * this family isn't configured in the kernel.
511 if (!strcmp(nconf->nc_protofmly, NC_INET))
512 hostname = IN4_LOCALHOST_STRING;
514 hostname = IN6_LOCALHOST_STRING;
517 if (tmpnconf == NULL) {
518 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
519 mutex_unlock(&loopnconf_lock);
522 loopnconf = getnetconfigent(tmpnconf->nc_netid);
523 /* loopnconf is never freed */
524 endnetconfig(nc_handle);
526 mutex_unlock(&loopnconf_lock);
527 client = getclnthandle(hostname, loopnconf, NULL);
532 * Set a mapping between program, version and address.
533 * Calls the rpcbind service to do the mapping.
536 rpcb_set(program, version, nconf, address)
539 const struct netconfig *nconf; /* Network structure of transport */
540 const struct netbuf *address; /* Services netconfig address */
547 /* parameter checking */
549 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
552 if (address == NULL) {
553 rpc_createerr.cf_stat = RPC_UNKNOWNADDR;
556 client = local_rpcb();
561 /* convert to universal */
562 /*LINTED const castaway*/
563 parms.r_addr = taddr2uaddr((struct netconfig *) nconf,
564 (struct netbuf *)address);
566 CLNT_DESTROY(client);
567 rpc_createerr.cf_stat = RPC_N2AXLATEFAILURE;
568 return (FALSE); /* no universal address */
570 parms.r_prog = program;
571 parms.r_vers = version;
572 parms.r_netid = nconf->nc_netid;
574 * Though uid is not being used directly, we still send it for
575 * completeness. For non-unix platforms, perhaps some other
576 * string or an empty string can be sent.
578 (void) snprintf(uidbuf, sizeof uidbuf, "%d", geteuid());
579 parms.r_owner = uidbuf;
581 CLNT_CALL(client, (rpcproc_t)RPCBPROC_SET, (xdrproc_t) xdr_rpcb,
582 (char *)(void *)&parms, (xdrproc_t) xdr_bool,
583 (char *)(void *)&rslt, tottimeout);
585 CLNT_DESTROY(client);
591 * Remove the mapping between program, version and netbuf address.
592 * Calls the rpcbind service to do the un-mapping.
593 * If netbuf is NULL, unset for all the transports, otherwise unset
594 * only for the given transport.
597 rpcb_unset(program, version, nconf)
600 const struct netconfig *nconf;
607 client = local_rpcb();
612 parms.r_prog = program;
613 parms.r_vers = version;
615 parms.r_netid = nconf->nc_netid;
617 /*LINTED const castaway*/
618 parms.r_netid = (char *) &nullstring[0]; /* unsets all */
620 /*LINTED const castaway*/
621 parms.r_addr = (char *) &nullstring[0];
622 (void) snprintf(uidbuf, sizeof uidbuf, "%d", geteuid());
623 parms.r_owner = uidbuf;
625 CLNT_CALL(client, (rpcproc_t)RPCBPROC_UNSET, (xdrproc_t) xdr_rpcb,
626 (char *)(void *)&parms, (xdrproc_t) xdr_bool,
627 (char *)(void *)&rslt, tottimeout);
629 CLNT_DESTROY(client);
634 * From the merged list, find the appropriate entry
636 static struct netbuf *
637 got_entry(relp, nconf)
638 rpcb_entry_list_ptr relp;
639 const struct netconfig *nconf;
641 struct netbuf *na = NULL;
642 rpcb_entry_list_ptr sp;
645 for (sp = relp; sp != NULL; sp = sp->rpcb_entry_next) {
646 rmap = &sp->rpcb_entry_map;
647 if ((strcmp(nconf->nc_proto, rmap->r_nc_proto) == 0) &&
648 (strcmp(nconf->nc_protofmly, rmap->r_nc_protofmly) == 0) &&
649 (nconf->nc_semantics == rmap->r_nc_semantics) &&
650 (rmap->r_maddr != NULL) && (rmap->r_maddr[0] != 0)) {
651 na = uaddr2taddr(nconf, rmap->r_maddr);
653 fprintf(stderr, "\tRemote address is [%s].\n",
657 "\tCouldn't resolve remote address!\n");
666 * Quick check to see if rpcbind is up. Tries to connect over
672 struct netconfig *nconf;
673 struct sockaddr_un sun;
678 localhandle = setnetconfig();
679 while ((nconf = getnetconfig(localhandle)) != NULL) {
680 if (nconf->nc_protofmly != NULL &&
681 strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0)
687 endnetconfig(localhandle);
689 memset(&sun, 0, sizeof sun);
690 sock = _socket(AF_LOCAL, SOCK_STREAM, 0);
693 sun.sun_family = AF_LOCAL;
694 strncpy(sun.sun_path, _PATH_RPCBINDSOCK, sizeof(sun.sun_path));
695 sun.sun_len = SUN_LEN(&sun);
697 if (_connect(sock, (struct sockaddr *)&sun, sun.sun_len) < 0) {
707 * An internal function which optimizes rpcb_getaddr function. It also
708 * returns the client handle that it uses to contact the remote rpcbind.
710 * The algorithm used: If the transports is TCP or UDP, it first tries
711 * version 2 (portmap), 4 and then 3 (svr4). This order should be
712 * changed in the next OS release to 4, 2 and 3. We are assuming that by
713 * that time, version 4 would be available on many machines on the network.
714 * With this algorithm, we get performance as well as a plan for
715 * obsoleting version 2.
717 * For all other transports, the algorithm remains as 4 and then 3.
719 * XXX: Due to some problems with t_connect(), we do not reuse the same client
720 * handle for COTS cases and hence in these cases we do not return the
721 * client handle. This code will change if t_connect() ever
722 * starts working properly. Also look under clnt_vc.c.
725 __rpcb_findaddr_timed(program, version, nconf, host, clpp, tp)
728 const struct netconfig *nconf;
733 static bool_t check_rpcbind = TRUE;
734 CLIENT *client = NULL;
736 enum clnt_stat clnt_st;
739 struct netbuf *address = NULL;
740 rpcvers_t start_vers = RPCBVERS4;
741 struct netbuf servaddr;
743 /* parameter checking */
745 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
752 * Use default total timeout if no timeout is specified.
758 /* Try version 2 for TCP or UDP */
759 if (strcmp(nconf->nc_protofmly, NC_INET) == 0) {
761 struct netbuf remote;
762 rpcvers_t pmapvers = 2;
763 struct pmap pmapparms;
766 * Try UDP only - there are some portmappers out
767 * there that use UDP only.
769 if (strcmp(nconf->nc_proto, NC_TCP) == 0) {
770 struct netconfig *newnconf;
772 if ((newnconf = getnetconfigent("udp")) == NULL) {
773 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
776 client = getclnthandle(host, newnconf, &parms.r_addr);
777 freenetconfigent(newnconf);
779 client = getclnthandle(host, nconf, &parms.r_addr);
785 * Set version and retry timeout.
787 CLNT_CONTROL(client, CLSET_RETRY_TIMEOUT, (char *)&rpcbrmttime);
788 CLNT_CONTROL(client, CLSET_VERS, (char *)&pmapvers);
790 pmapparms.pm_prog = program;
791 pmapparms.pm_vers = version;
792 pmapparms.pm_prot = strcmp(nconf->nc_proto, NC_TCP) ?
793 IPPROTO_UDP : IPPROTO_TCP;
794 pmapparms.pm_port = 0; /* not needed */
795 clnt_st = CLNT_CALL(client, (rpcproc_t)PMAPPROC_GETPORT,
796 (xdrproc_t) xdr_pmap, (caddr_t)(void *)&pmapparms,
797 (xdrproc_t) xdr_u_short, (caddr_t)(void *)&port,
799 if (clnt_st != RPC_SUCCESS) {
800 if ((clnt_st == RPC_PROGVERSMISMATCH) ||
801 (clnt_st == RPC_PROGUNAVAIL))
802 goto try_rpcbind; /* Try different versions */
803 rpc_createerr.cf_stat = RPC_PMAPFAILURE;
804 clnt_geterr(client, &rpc_createerr.cf_error);
806 } else if (port == 0) {
808 rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
812 CLNT_CONTROL(client, CLGET_SVC_ADDR, (char *)&remote);
813 if (((address = (struct netbuf *)
814 malloc(sizeof (struct netbuf))) == NULL) ||
815 ((address->buf = (char *)
816 malloc(remote.len)) == NULL)) {
817 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
818 clnt_geterr(client, &rpc_createerr.cf_error);
825 memcpy(address->buf, remote.buf, remote.len);
826 memcpy(&((char *)address->buf)[sizeof (short)],
827 (char *)(void *)&port, sizeof (short));
828 address->len = address->maxlen = remote.len;
835 * Check if rpcbind is up. This prevents needless delays when
836 * accessing applications such as the keyserver while booting
839 if (check_rpcbind && strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0) {
840 if (!__rpcbind_is_up()) {
841 rpc_createerr.cf_stat = RPC_PMAPFAILURE;
842 rpc_createerr.cf_error.re_errno = 0;
845 check_rpcbind = FALSE;
849 * Now we try version 4 and then 3.
850 * We also send the remote system the address we used to
851 * contact it in case it can help to connect back with us
853 parms.r_prog = program;
854 parms.r_vers = version;
855 /*LINTED const castaway*/
856 parms.r_owner = (char *) &nullstring[0]; /* not needed; */
857 /* just for xdring */
858 parms.r_netid = nconf->nc_netid; /* not really needed */
861 * If a COTS transport is being used, try getting address via CLTS
862 * transport. This works only with version 4.
864 if (nconf->nc_semantics == NC_TPI_COTS_ORD ||
865 nconf->nc_semantics == NC_TPI_COTS) {
868 struct netconfig *nconf_clts;
869 rpcb_entry_list_ptr relp = NULL;
871 if (client == NULL) {
872 /* This did not go through the above PORTMAP/TCP code */
873 if ((handle = __rpc_setconf("datagram_v")) != NULL) {
874 while ((nconf_clts = __rpc_getconf(handle))
876 if (strcmp(nconf_clts->nc_protofmly,
877 nconf->nc_protofmly) != 0) {
880 client = getclnthandle(host, nconf_clts,
884 __rpc_endconf(handle);
887 goto regular_rpcbind; /* Go the regular way */
889 /* This is a UDP PORTMAP handle. Change to version 4 */
891 CLNT_CONTROL(client, CLSET_VERS, (char *)(void *)&vers);
894 * We also send the remote system the address we used to
895 * contact it in case it can help it connect back with us
897 if (parms.r_addr == NULL) {
898 /*LINTED const castaway*/
899 parms.r_addr = (char *) &nullstring[0]; /* for XDRing */
902 CLNT_CONTROL(client, CLSET_RETRY_TIMEOUT, (char *)&rpcbrmttime);
904 clnt_st = CLNT_CALL(client, (rpcproc_t)RPCBPROC_GETADDRLIST,
905 (xdrproc_t) xdr_rpcb, (char *)(void *)&parms,
906 (xdrproc_t) xdr_rpcb_entry_list_ptr,
907 (char *)(void *)&relp, *tp);
908 if (clnt_st == RPC_SUCCESS) {
909 if ((address = got_entry(relp, nconf)) != NULL) {
910 xdr_free((xdrproc_t) xdr_rpcb_entry_list_ptr,
911 (char *)(void *)&relp);
912 CLNT_CONTROL(client, CLGET_SVC_ADDR,
913 (char *)(void *)&servaddr);
914 __rpc_fixup_addr(address, &servaddr);
917 /* Entry not found for this transport */
918 xdr_free((xdrproc_t) xdr_rpcb_entry_list_ptr,
919 (char *)(void *)&relp);
921 * XXX: should have perhaps returned with error but
922 * since the remote machine might not always be able
923 * to send the address on all transports, we try the
924 * regular way with regular_rpcbind
926 goto regular_rpcbind;
927 } else if ((clnt_st == RPC_PROGVERSMISMATCH) ||
928 (clnt_st == RPC_PROGUNAVAIL)) {
929 start_vers = RPCBVERS; /* Try version 3 now */
930 goto regular_rpcbind; /* Try different versions */
932 rpc_createerr.cf_stat = RPC_PMAPFAILURE;
933 clnt_geterr(client, &rpc_createerr.cf_error);
940 /* Now the same transport is to be used to get the address */
941 if (client && ((nconf->nc_semantics == NC_TPI_COTS_ORD) ||
942 (nconf->nc_semantics == NC_TPI_COTS))) {
943 /* A CLTS type of client - destroy it */
944 CLNT_DESTROY(client);
948 if (client == NULL) {
949 client = getclnthandle(host, nconf, &parms.r_addr);
950 if (client == NULL) {
954 if (parms.r_addr == NULL) {
955 /*LINTED const castaway*/
956 parms.r_addr = (char *) &nullstring[0];
959 /* First try from start_vers and then version 3 (RPCBVERS) */
961 CLNT_CONTROL(client, CLSET_RETRY_TIMEOUT, (char *) &rpcbrmttime);
962 for (vers = start_vers; vers >= RPCBVERS; vers--) {
963 /* Set the version */
964 CLNT_CONTROL(client, CLSET_VERS, (char *)(void *)&vers);
965 clnt_st = CLNT_CALL(client, (rpcproc_t)RPCBPROC_GETADDR,
966 (xdrproc_t) xdr_rpcb, (char *)(void *)&parms,
967 (xdrproc_t) xdr_wrapstring, (char *)(void *) &ua, *tp);
968 if (clnt_st == RPC_SUCCESS) {
969 if ((ua == NULL) || (ua[0] == 0)) {
970 /* address unknown */
971 rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
974 address = uaddr2taddr(nconf, ua);
976 fprintf(stderr, "\tRemote address is [%s]\n", ua);
979 "\tCouldn't resolve remote address!\n");
981 xdr_free((xdrproc_t)xdr_wrapstring,
982 (char *)(void *)&ua);
985 /* We don't know about your universal address */
986 rpc_createerr.cf_stat = RPC_N2AXLATEFAILURE;
989 CLNT_CONTROL(client, CLGET_SVC_ADDR,
990 (char *)(void *)&servaddr);
991 __rpc_fixup_addr(address, &servaddr);
993 } else if (clnt_st == RPC_PROGVERSMISMATCH) {
994 struct rpc_err rpcerr;
996 clnt_geterr(client, &rpcerr);
997 if (rpcerr.re_vers.low > RPCBVERS4)
998 goto error; /* a new version, can't handle */
999 } else if (clnt_st != RPC_PROGUNAVAIL) {
1000 /* Cant handle this error */
1001 rpc_createerr.cf_stat = clnt_st;
1002 clnt_geterr(client, &rpc_createerr.cf_error);
1009 CLNT_DESTROY(client);
1013 if (nconf->nc_semantics != NC_TPI_CLTS) {
1014 /* This client is the connectionless one */
1016 CLNT_DESTROY(client);
1022 } else if (client) {
1023 CLNT_DESTROY(client);
1025 if (parms.r_addr != NULL && parms.r_addr != nullstring)
1032 * Find the mapped address for program, version.
1033 * Calls the rpcbind service remotely to do the lookup.
1034 * Uses the transport specified in nconf.
1035 * Returns FALSE (0) if no map exists, else returns 1.
1037 * Assuming that the address is all properly allocated
1040 rpcb_getaddr(program, version, nconf, address, host)
1043 const struct netconfig *nconf;
1044 struct netbuf *address;
1049 if ((na = __rpcb_findaddr_timed(program, version,
1050 (struct netconfig *) nconf, (char *) host,
1051 (CLIENT **) NULL, (struct timeval *) NULL)) == NULL)
1054 if (na->len > address->maxlen) {
1055 /* Too long address */
1058 rpc_createerr.cf_stat = RPC_FAILED;
1061 memcpy(address->buf, na->buf, (size_t)na->len);
1062 address->len = na->len;
1069 * Get a copy of the current maps.
1070 * Calls the rpcbind service remotely to get the maps.
1072 * It returns only a list of the services
1073 * It returns NULL on failure.
1076 rpcb_getmaps(nconf, host)
1077 const struct netconfig *nconf;
1080 rpcblist_ptr head = NULL;
1082 enum clnt_stat clnt_st;
1085 client = getclnthandle(host, nconf, NULL);
1086 if (client == NULL) {
1089 clnt_st = CLNT_CALL(client, (rpcproc_t)RPCBPROC_DUMP,
1090 (xdrproc_t) xdr_void, NULL, (xdrproc_t) xdr_rpcblist_ptr,
1091 (char *)(void *)&head, tottimeout);
1092 if (clnt_st == RPC_SUCCESS)
1095 if ((clnt_st != RPC_PROGVERSMISMATCH) &&
1096 (clnt_st != RPC_PROGUNAVAIL)) {
1097 rpc_createerr.cf_stat = RPC_RPCBFAILURE;
1098 clnt_geterr(client, &rpc_createerr.cf_error);
1102 /* fall back to earlier version */
1103 CLNT_CONTROL(client, CLGET_VERS, (char *)(void *)&vers);
1104 if (vers == RPCBVERS4) {
1106 CLNT_CONTROL(client, CLSET_VERS, (char *)(void *)&vers);
1107 if (CLNT_CALL(client, (rpcproc_t)RPCBPROC_DUMP,
1108 (xdrproc_t) xdr_void, NULL, (xdrproc_t) xdr_rpcblist_ptr,
1109 (char *)(void *)&head, tottimeout) == RPC_SUCCESS)
1112 rpc_createerr.cf_stat = RPC_RPCBFAILURE;
1113 clnt_geterr(client, &rpc_createerr.cf_error);
1116 CLNT_DESTROY(client);
1121 * rpcbinder remote-call-service interface.
1122 * This routine is used to call the rpcbind remote call service
1123 * which will look up a service program in the address maps, and then
1124 * remotely call that routine with the given parameters. This allows
1125 * programs to do a lookup and call in one step.
1128 rpcb_rmtcall(nconf, host, prog, vers, proc, xdrargs, argsp,
1129 xdrres, resp, tout, addr_ptr)
1130 const struct netconfig *nconf; /* Netconfig structure */
1131 const char *host; /* Remote host name */
1134 rpcproc_t proc; /* Remote proc identifiers */
1135 xdrproc_t xdrargs, xdrres; /* XDR routines */
1136 caddr_t argsp, resp; /* Argument and Result */
1137 struct timeval tout; /* Timeout value for this call */
1138 const struct netbuf *addr_ptr; /* Preallocated netbuf address */
1141 enum clnt_stat stat;
1142 struct r_rpcb_rmtcallargs a;
1143 struct r_rpcb_rmtcallres r;
1144 rpcvers_t rpcb_vers;
1147 client = getclnthandle(host, nconf, NULL);
1148 if (client == NULL) {
1149 return (RPC_FAILED);
1151 /*LINTED const castaway*/
1152 CLNT_CONTROL(client, CLSET_RETRY_TIMEOUT, (char *)(void *)&rmttimeout);
1156 a.args.args_val = argsp;
1157 a.xdr_args = xdrargs;
1159 r.results.results_val = resp;
1162 for (rpcb_vers = RPCBVERS4; rpcb_vers >= RPCBVERS; rpcb_vers--) {
1163 CLNT_CONTROL(client, CLSET_VERS, (char *)(void *)&rpcb_vers);
1164 stat = CLNT_CALL(client, (rpcproc_t)RPCBPROC_CALLIT,
1165 (xdrproc_t) xdr_rpcb_rmtcallargs, (char *)(void *)&a,
1166 (xdrproc_t) xdr_rpcb_rmtcallres, (char *)(void *)&r, tout);
1167 if ((stat == RPC_SUCCESS) && (addr_ptr != NULL)) {
1169 /*LINTED const castaway*/
1170 na = uaddr2taddr((struct netconfig *) nconf, r.addr);
1172 stat = RPC_N2AXLATEFAILURE;
1173 /*LINTED const castaway*/
1174 ((struct netbuf *) addr_ptr)->len = 0;
1177 if (na->len > addr_ptr->maxlen) {
1178 /* Too long address */
1179 stat = RPC_FAILED; /* XXX A better error no */
1182 /*LINTED const castaway*/
1183 ((struct netbuf *) addr_ptr)->len = 0;
1186 memcpy(addr_ptr->buf, na->buf, (size_t)na->len);
1187 /*LINTED const castaway*/
1188 ((struct netbuf *)addr_ptr)->len = na->len;
1192 } else if ((stat != RPC_PROGVERSMISMATCH) &&
1193 (stat != RPC_PROGUNAVAIL)) {
1198 CLNT_DESTROY(client);
1200 xdr_free((xdrproc_t) xdr_wrapstring, (char *)(void *)&r.addr);
1205 * Gets the time on the remote host.
1206 * Returns 1 if succeeds else 0.
1209 rpcb_gettime(host, timep)
1213 CLIENT *client = NULL;
1215 struct netconfig *nconf;
1220 if ((host == NULL) || (host[0] == 0)) {
1225 if ((handle = __rpc_setconf("netpath")) == NULL) {
1226 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
1229 rpc_createerr.cf_stat = RPC_SUCCESS;
1230 while (client == NULL) {
1231 if ((nconf = __rpc_getconf(handle)) == NULL) {
1232 if (rpc_createerr.cf_stat == RPC_SUCCESS)
1233 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
1236 client = getclnthandle(host, nconf, NULL);
1240 __rpc_endconf(handle);
1241 if (client == (CLIENT *) NULL) {
1245 st = CLNT_CALL(client, (rpcproc_t)RPCBPROC_GETTIME,
1246 (xdrproc_t) xdr_void, NULL,
1247 (xdrproc_t) xdr_int, (char *)(void *)timep, tottimeout);
1249 if ((st == RPC_PROGVERSMISMATCH) || (st == RPC_PROGUNAVAIL)) {
1250 CLNT_CONTROL(client, CLGET_VERS, (char *)(void *)&vers);
1251 if (vers == RPCBVERS4) {
1252 /* fall back to earlier version */
1254 CLNT_CONTROL(client, CLSET_VERS, (char *)(void *)&vers);
1255 st = CLNT_CALL(client, (rpcproc_t)RPCBPROC_GETTIME,
1256 (xdrproc_t) xdr_void, NULL,
1257 (xdrproc_t) xdr_int, (char *)(void *)timep,
1261 CLNT_DESTROY(client);
1262 return (st == RPC_SUCCESS? TRUE: FALSE);
1266 * Converts taddr to universal address. This routine should never
1267 * really be called because local n2a libraries are always provided.
1270 rpcb_taddr2uaddr(nconf, taddr)
1271 struct netconfig *nconf;
1272 struct netbuf *taddr;
1278 /* parameter checking */
1279 if (nconf == NULL) {
1280 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
1283 if (taddr == NULL) {
1284 rpc_createerr.cf_stat = RPC_UNKNOWNADDR;
1287 client = local_rpcb();
1292 CLNT_CALL(client, (rpcproc_t)RPCBPROC_TADDR2UADDR,
1293 (xdrproc_t) xdr_netbuf, (char *)(void *)taddr,
1294 (xdrproc_t) xdr_wrapstring, (char *)(void *)&uaddr, tottimeout);
1295 CLNT_DESTROY(client);
1300 * Converts universal address to netbuf. This routine should never
1301 * really be called because local n2a libraries are always provided.
1304 rpcb_uaddr2taddr(nconf, uaddr)
1305 struct netconfig *nconf;
1309 struct netbuf *taddr;
1312 /* parameter checking */
1313 if (nconf == NULL) {
1314 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
1317 if (uaddr == NULL) {
1318 rpc_createerr.cf_stat = RPC_UNKNOWNADDR;
1321 client = local_rpcb();
1326 taddr = (struct netbuf *)calloc(1, sizeof (struct netbuf));
1327 if (taddr == NULL) {
1328 CLNT_DESTROY(client);
1331 if (CLNT_CALL(client, (rpcproc_t)RPCBPROC_UADDR2TADDR,
1332 (xdrproc_t) xdr_wrapstring, (char *)(void *)&uaddr,
1333 (xdrproc_t) xdr_netbuf, (char *)(void *)taddr,
1334 tottimeout) != RPC_SUCCESS) {
1338 CLNT_DESTROY(client);