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 * Copyright (c) 1986-1991 by Sun Microsystems Inc.
34 /* #ident "@(#)rpcb_clnt.c 1.27 94/04/24 SMI" */
37 #if defined(LIBC_SCCS) && !defined(lint)
38 static char sccsid[] = "@(#)rpcb_clnt.c 1.30 89/06/21 Copyr 1988 Sun Micro";
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
45 * interface to rpcbind rpc service.
47 * Copyright (C) 1988, Sun Microsystems, Inc.
50 #include "opt_inet6.h"
52 #include <sys/param.h>
53 #include <sys/systm.h>
54 #include <sys/kernel.h>
55 #include <sys/malloc.h>
57 #include <sys/socket.h>
58 #include <sys/socketvar.h>
61 #include <rpc/rpcb_clnt.h>
62 #include <rpc/rpcb_prot.h>
64 #include <rpc/rpc_com.h>
66 static struct timeval tottimeout = { 60, 0 };
67 static const char nullstring[] = "\000";
69 static CLIENT *local_rpcb(void);
73 static const struct timeval rmttimeout = { 3, 0 };
74 static struct timeval rpcbrmttime = { 15, 0 };
78 struct address_cache {
82 struct netbuf *ac_taddr;
83 struct address_cache *ac_next;
86 static struct address_cache *front;
89 #define CLCR_GET_RPCB_TIMEOUT 1
90 #define CLCR_SET_RPCB_TIMEOUT 2
93 extern int __rpc_lowvers;
95 static struct address_cache *check_cache(const char *, const char *);
96 static void delete_cache(struct netbuf *);
97 static void add_cache(const char *, const char *, struct netbuf *, char *);
98 static CLIENT *getclnthandle(const char *, const struct netconfig *, char **);
99 static CLIENT *local_rpcb(void);
100 static struct netbuf *got_entry(rpcb_entry_list_ptr, const struct netconfig *);
103 * This routine adjusts the timeout used for calls to the remote rpcbind.
104 * Also, this routine can be used to set the use of portmapper version 2
105 * only when doing rpc_broadcasts
106 * These are private routines that may not be provided in future releases.
109 __rpc_control(request, info)
114 case CLCR_GET_RPCB_TIMEOUT:
115 *(struct timeval *)info = tottimeout;
117 case CLCR_SET_RPCB_TIMEOUT:
118 tottimeout = *(struct timeval *)info;
120 case CLCR_SET_LOWVERS:
121 __rpc_lowvers = *(int *)info;
123 case CLCR_GET_LOWVERS:
124 *(int *)info = __rpc_lowvers;
133 * It might seem that a reader/writer lock would be more reasonable here.
134 * However because getclnthandle(), the only user of the cache functions,
135 * may do a delete_cache() operation if a check_cache() fails to return an
136 * address useful to clnt_tli_create(), we may as well use a mutex.
139 * As it turns out, if the cache lock is *not* a reader/writer lock, we will
140 * block all clnt_create's if we are trying to connect to a host that's down,
141 * since the lock will be held all during that time.
145 * The routines check_cache(), add_cache(), delete_cache() manage the
146 * cache of rpcbind addresses for (host, netid).
149 static struct address_cache *
150 check_cache(host, netid)
151 const char *host, *netid;
153 struct address_cache *cptr;
155 /* READ LOCK HELD ON ENTRY: rpcbaddr_cache_lock */
157 for (cptr = front; cptr != NULL; cptr = cptr->ac_next) {
158 if (!strcmp(cptr->ac_host, host) &&
159 !strcmp(cptr->ac_netid, netid)) {
161 fprintf(stderr, "Found cache entry for %s: %s\n",
167 return ((struct address_cache *) NULL);
174 struct address_cache *cptr, *prevptr = NULL;
176 /* WRITE LOCK HELD ON ENTRY: rpcbaddr_cache_lock */
177 for (cptr = front; cptr != NULL; cptr = cptr->ac_next) {
178 if (!memcmp(cptr->ac_taddr->buf, addr->buf, addr->len)) {
180 free(cptr->ac_netid);
181 free(cptr->ac_taddr->buf);
182 free(cptr->ac_taddr);
184 free(cptr->ac_uaddr);
186 prevptr->ac_next = cptr->ac_next;
188 front = cptr->ac_next;
198 add_cache(host, netid, taddr, uaddr)
199 const char *host, *netid;
201 struct netbuf *taddr;
203 struct address_cache *ad_cache, *cptr, *prevptr;
205 ad_cache = (struct address_cache *)
206 malloc(sizeof (struct address_cache));
210 ad_cache->ac_host = strdup(host);
211 ad_cache->ac_netid = strdup(netid);
212 ad_cache->ac_uaddr = uaddr ? strdup(uaddr) : NULL;
213 ad_cache->ac_taddr = (struct netbuf *)malloc(sizeof (struct netbuf));
214 if (!ad_cache->ac_host || !ad_cache->ac_netid || !ad_cache->ac_taddr ||
215 (uaddr && !ad_cache->ac_uaddr)) {
218 ad_cache->ac_taddr->len = ad_cache->ac_taddr->maxlen = taddr->len;
219 ad_cache->ac_taddr->buf = (char *) malloc(taddr->len);
220 if (ad_cache->ac_taddr->buf == NULL) {
222 if (ad_cache->ac_host)
223 free(ad_cache->ac_host);
224 if (ad_cache->ac_netid)
225 free(ad_cache->ac_netid);
226 if (ad_cache->ac_uaddr)
227 free(ad_cache->ac_uaddr);
228 if (ad_cache->ac_taddr)
229 free(ad_cache->ac_taddr);
233 memcpy(ad_cache->ac_taddr->buf, taddr->buf, taddr->len);
235 fprintf(stderr, "Added to cache: %s : %s\n", host, netid);
238 /* VARIABLES PROTECTED BY rpcbaddr_cache_lock: cptr */
240 rwlock_wrlock(&rpcbaddr_cache_lock);
241 if (cachesize < CACHESIZE) {
242 ad_cache->ac_next = front;
246 /* Free the last entry */
249 while (cptr->ac_next) {
251 cptr = cptr->ac_next;
255 fprintf(stderr, "Deleted from cache: %s : %s\n",
256 cptr->ac_host, cptr->ac_netid);
259 free(cptr->ac_netid);
260 free(cptr->ac_taddr->buf);
261 free(cptr->ac_taddr);
263 free(cptr->ac_uaddr);
266 prevptr->ac_next = NULL;
267 ad_cache->ac_next = front;
271 ad_cache->ac_next = NULL;
275 rwlock_unlock(&rpcbaddr_cache_lock);
279 * This routine will return a client handle that is connected to the
280 * rpcbind. If targaddr is non-NULL, the "universal address" of the
281 * host will be stored in *targaddr; the caller is responsible for
282 * freeing this string.
283 * On error, returns NULL and free's everything.
286 getclnthandle(host, nconf, targaddr)
288 const struct netconfig *nconf;
292 struct netbuf *addr, taddr;
293 struct netbuf addr_to_delete;
294 struct __rpc_sockinfo si;
295 struct addrinfo hints, *res, *tres;
296 struct address_cache *ad_cache;
299 /* VARIABLES PROTECTED BY rpcbaddr_cache_lock: ad_cache */
301 /* Get the address of the rpcbind. Check cache first */
303 addr_to_delete.len = 0;
304 rwlock_rdlock(&rpcbaddr_cache_lock);
307 ad_cache = check_cache(host, nconf->nc_netid);
308 if (ad_cache != NULL) {
309 addr = ad_cache->ac_taddr;
310 client = clnt_tli_create(RPC_ANYFD, nconf, addr,
311 (rpcprog_t)RPCBPROG, (rpcvers_t)RPCBVERS4, 0, 0);
312 if (client != NULL) {
314 *targaddr = strdup(ad_cache->ac_uaddr);
315 rwlock_unlock(&rpcbaddr_cache_lock);
318 addr_to_delete.len = addr->len;
319 addr_to_delete.buf = (char *)malloc(addr->len);
320 if (addr_to_delete.buf == NULL) {
321 addr_to_delete.len = 0;
323 memcpy(addr_to_delete.buf, addr->buf, addr->len);
326 rwlock_unlock(&rpcbaddr_cache_lock);
327 if (addr_to_delete.len != 0) {
329 * Assume this may be due to cache data being
332 rwlock_wrlock(&rpcbaddr_cache_lock);
333 delete_cache(&addr_to_delete);
334 rwlock_unlock(&rpcbaddr_cache_lock);
335 free(addr_to_delete.buf);
337 if (!__rpc_nconf2sockinfo(nconf, &si)) {
338 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
342 memset(&hints, 0, sizeof hints);
343 hints.ai_family = si.si_af;
344 hints.ai_socktype = si.si_socktype;
345 hints.ai_protocol = si.si_proto;
348 printf("trying netid %s family %d proto %d socktype %d\n",
349 nconf->nc_netid, si.si_af, si.si_proto, si.si_socktype);
352 if (nconf->nc_protofmly != NULL && strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0) {
353 client = local_rpcb();
356 clnt_pcreateerror("rpcbind clnt interface");
360 struct sockaddr_un sun;
362 *targaddr = malloc(sizeof(sun.sun_path));
363 if (*targaddr == NULL) {
364 CLNT_DESTROY(client);
367 strncpy(*targaddr, _PATH_RPCBINDSOCK,
368 sizeof(sun.sun_path));
373 if (getaddrinfo(host, "sunrpc", &hints, &res) != 0) {
374 rpc_createerr.cf_stat = RPC_UNKNOWNHOST;
379 for (tres = res; tres != NULL; tres = tres->ai_next) {
380 taddr.buf = tres->ai_addr;
381 taddr.len = taddr.maxlen = tres->ai_addrlen;
387 ua = taddr2uaddr(nconf, &taddr);
388 fprintf(stderr, "Got it [%s]\n", ua);
397 fprintf(stderr, "\tnetbuf len = %d, maxlen = %d\n",
398 taddr.len, taddr.maxlen);
399 fprintf(stderr, "\tAddress is ");
400 for (i = 0; i < taddr.len; i++)
401 fprintf(stderr, "%u.", ((char *)(taddr.buf))[i]);
402 fprintf(stderr, "\n");
405 client = clnt_tli_create(RPC_ANYFD, nconf, &taddr,
406 (rpcprog_t)RPCBPROG, (rpcvers_t)RPCBVERS4, 0, 0);
409 clnt_pcreateerror("rpcbind clnt interface");
414 tmpaddr = targaddr ? taddr2uaddr(nconf, &taddr) : NULL;
415 add_cache(host, nconf->nc_netid, &taddr, tmpaddr);
429 #define IN4_LOCALHOST_STRING "127.0.0.1"
430 #define IN6_LOCALHOST_STRING "::1"
433 * This routine will return a client handle that is connected to the local
434 * rpcbind. Returns NULL on error and free's everything.
442 struct sockaddr_un sun;
446 * Try connecting to the local rpcbind through a local socket
447 * first. If this doesn't work, try all transports defined in
448 * the netconfig file.
450 memset(&sun, 0, sizeof sun);
452 error = socreate(AF_LOCAL, &so, SOCK_STREAM, 0, curthread->td_ucred,
456 sun.sun_family = AF_LOCAL;
457 strcpy(sun.sun_path, _PATH_RPCBINDSOCK);
458 sun.sun_len = SUN_LEN(&sun);
460 tsize = __rpc_get_t_size(AF_LOCAL, 0, 0);
461 client = clnt_vc_create(so, (struct sockaddr *)&sun, (rpcprog_t)RPCBPROG,
462 (rpcvers_t)RPCBVERS, tsize, tsize, 1);
464 if (client != NULL) {
465 /* Mark the socket to be closed in destructor */
466 (void) CLNT_CONTROL(client, CLSET_FD_CLOSE, NULL);
470 /* Nobody needs this socket anymore; free the descriptor. */
476 static struct netconfig *loopnconf;
477 static char *localhostname;
479 /* VARIABLES PROTECTED BY loopnconf_lock: loopnconf */
480 mutex_lock(&loopnconf_lock);
481 if (loopnconf == NULL) {
482 struct netconfig *nconf, *tmpnconf = NULL;
486 nc_handle = setnetconfig();
487 if (nc_handle == NULL) {
488 /* fails to open netconfig file */
489 syslog (LOG_ERR, "rpc: failed to open " NETCONFIG);
490 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
491 mutex_unlock(&loopnconf_lock);
494 while ((nconf = getnetconfig(nc_handle)) != NULL) {
497 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 localhostname = IN4_LOCALHOST_STRING;
514 localhostname = 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(localhostname, loopnconf, NULL);
535 * Set a mapping between program, version and address.
536 * Calls the rpcbind service to do the mapping.
539 rpcb_set(rpcprog_t program, rpcvers_t version,
540 const struct netconfig *nconf, /* Network structure of transport */
541 const struct netbuf *address) /* Services netconfig address */
549 struct netconfig nconfcopy;
550 struct netbuf addresscopy;
552 /* parameter checking */
554 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
557 if (address == NULL) {
558 rpc_createerr.cf_stat = RPC_UNKNOWNADDR;
561 client = local_rpcb();
566 /* convert to universal */
567 /*LINTED const castaway*/
569 addresscopy = *address;
570 parms.r_addr = taddr2uaddr(&nconfcopy, &addresscopy);
572 CLNT_DESTROY(client);
573 rpc_createerr.cf_stat = RPC_N2AXLATEFAILURE;
574 return (FALSE); /* no universal address */
576 parms.r_prog = program;
577 parms.r_vers = version;
578 parms.r_netid = nconf->nc_netid;
581 * Though uid is not being used directly, we still send it for
582 * completeness. For non-unix platforms, perhaps some other
583 * string or an empty string can be sent.
585 (void) snprintf(uidbuf, sizeof uidbuf, "%d", geteuid());
586 parms.r_owner = uidbuf;
591 CLNT_CALL(client, (rpcproc_t)RPCBPROC_SET, (xdrproc_t) xdr_rpcb,
592 (char *)(void *)&parms, (xdrproc_t) xdr_bool,
593 (char *)(void *)&rslt, tottimeout);
595 CLNT_DESTROY(client);
596 free(parms.r_addr, M_RPC);
601 * Remove the mapping between program, version and netbuf address.
602 * Calls the rpcbind service to do the un-mapping.
603 * If netbuf is NULL, unset for all the transports, otherwise unset
604 * only for the given transport.
607 rpcb_unset(rpcprog_t program, rpcvers_t version, const struct netconfig *nconf)
616 client = local_rpcb();
621 parms.r_prog = program;
622 parms.r_vers = version;
624 parms.r_netid = nconf->nc_netid;
626 /*LINTED const castaway*/
627 parms.r_netid = (char *)(uintptr_t) &nullstring[0]; /* unsets all */
629 /*LINTED const castaway*/
630 parms.r_addr = (char *)(uintptr_t) &nullstring[0];
632 (void) snprintf(uidbuf, sizeof uidbuf, "%d", geteuid());
633 parms.r_owner = uidbuf;
638 CLNT_CALL(client, (rpcproc_t)RPCBPROC_UNSET, (xdrproc_t) xdr_rpcb,
639 (char *)(void *)&parms, (xdrproc_t) xdr_bool,
640 (char *)(void *)&rslt, tottimeout);
642 CLNT_DESTROY(client);
649 * From the merged list, find the appropriate entry
651 static struct netbuf *
652 got_entry(relp, nconf)
653 rpcb_entry_list_ptr relp;
654 const struct netconfig *nconf;
656 struct netbuf *na = NULL;
657 rpcb_entry_list_ptr sp;
660 for (sp = relp; sp != NULL; sp = sp->rpcb_entry_next) {
661 rmap = &sp->rpcb_entry_map;
662 if ((strcmp(nconf->nc_proto, rmap->r_nc_proto) == 0) &&
663 (strcmp(nconf->nc_protofmly, rmap->r_nc_protofmly) == 0) &&
664 (nconf->nc_semantics == rmap->r_nc_semantics) &&
665 (rmap->r_maddr != NULL) && (rmap->r_maddr[0] != 0)) {
666 na = uaddr2taddr(nconf, rmap->r_maddr);
668 fprintf(stderr, "\tRemote address is [%s].\n",
672 "\tCouldn't resolve remote address!\n");
681 * Quick check to see if rpcbind is up. Tries to connect over
687 struct netconfig *nconf;
688 struct sockaddr_un sun;
693 localhandle = setnetconfig();
694 while ((nconf = getnetconfig(localhandle)) != NULL) {
695 if (nconf->nc_protofmly != NULL &&
696 strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0)
702 endnetconfig(localhandle);
704 memset(&sun, 0, sizeof sun);
705 sock = _socket(AF_LOCAL, SOCK_STREAM, 0);
708 sun.sun_family = AF_LOCAL;
709 strncpy(sun.sun_path, _PATH_RPCBINDSOCK, sizeof(sun.sun_path));
710 sun.sun_len = SUN_LEN(&sun);
712 if (_connect(sock, (struct sockaddr *)&sun, sun.sun_len) < 0) {
722 * An internal function which optimizes rpcb_getaddr function. It also
723 * returns the client handle that it uses to contact the remote rpcbind.
725 * The algorithm used: If the transports is TCP or UDP, it first tries
726 * version 2 (portmap), 4 and then 3 (svr4). This order should be
727 * changed in the next OS release to 4, 2 and 3. We are assuming that by
728 * that time, version 4 would be available on many machines on the network.
729 * With this algorithm, we get performance as well as a plan for
730 * obsoleting version 2.
732 * For all other transports, the algorithm remains as 4 and then 3.
734 * XXX: Due to some problems with t_connect(), we do not reuse the same client
735 * handle for COTS cases and hence in these cases we do not return the
736 * client handle. This code will change if t_connect() ever
737 * starts working properly. Also look under clnt_vc.c.
740 __rpcb_findaddr_timed(program, version, nconf, host, clpp, tp)
743 const struct netconfig *nconf;
748 static bool_t check_rpcbind = TRUE;
749 CLIENT *client = NULL;
751 enum clnt_stat clnt_st;
754 struct netbuf *address = NULL;
755 rpcvers_t start_vers = RPCBVERS4;
756 struct netbuf servaddr;
758 /* parameter checking */
760 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
767 * Use default total timeout if no timeout is specified.
773 /* Try version 2 for TCP or UDP */
774 if (strcmp(nconf->nc_protofmly, NC_INET) == 0) {
776 struct netbuf remote;
777 rpcvers_t pmapvers = 2;
778 struct pmap pmapparms;
781 * Try UDP only - there are some portmappers out
782 * there that use UDP only.
784 if (strcmp(nconf->nc_proto, NC_TCP) == 0) {
785 struct netconfig *newnconf;
787 if ((newnconf = getnetconfigent("udp")) == NULL) {
788 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
791 client = getclnthandle(host, newnconf, &parms.r_addr);
792 freenetconfigent(newnconf);
794 client = getclnthandle(host, nconf, &parms.r_addr);
800 * Set version and retry timeout.
802 CLNT_CONTROL(client, CLSET_RETRY_TIMEOUT, (char *)&rpcbrmttime);
803 CLNT_CONTROL(client, CLSET_VERS, (char *)&pmapvers);
805 pmapparms.pm_prog = program;
806 pmapparms.pm_vers = version;
807 pmapparms.pm_prot = strcmp(nconf->nc_proto, NC_TCP) ?
808 IPPROTO_UDP : IPPROTO_TCP;
809 pmapparms.pm_port = 0; /* not needed */
810 clnt_st = CLNT_CALL(client, (rpcproc_t)PMAPPROC_GETPORT,
811 (xdrproc_t) xdr_pmap, (caddr_t)(void *)&pmapparms,
812 (xdrproc_t) xdr_u_short, (caddr_t)(void *)&port,
814 if (clnt_st != RPC_SUCCESS) {
815 if ((clnt_st == RPC_PROGVERSMISMATCH) ||
816 (clnt_st == RPC_PROGUNAVAIL))
817 goto try_rpcbind; /* Try different versions */
818 rpc_createerr.cf_stat = RPC_PMAPFAILURE;
819 clnt_geterr(client, &rpc_createerr.cf_error);
821 } else if (port == 0) {
823 rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
827 CLNT_CONTROL(client, CLGET_SVC_ADDR, (char *)&remote);
828 if (((address = (struct netbuf *)
829 malloc(sizeof (struct netbuf))) == NULL) ||
830 ((address->buf = (char *)
831 malloc(remote.len)) == NULL)) {
832 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
833 clnt_geterr(client, &rpc_createerr.cf_error);
840 memcpy(address->buf, remote.buf, remote.len);
841 memcpy(&((char *)address->buf)[sizeof (short)],
842 (char *)(void *)&port, sizeof (short));
843 address->len = address->maxlen = remote.len;
850 * Check if rpcbind is up. This prevents needless delays when
851 * accessing applications such as the keyserver while booting
854 if (check_rpcbind && strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0) {
855 if (!__rpcbind_is_up()) {
856 rpc_createerr.cf_stat = RPC_PMAPFAILURE;
857 rpc_createerr.cf_error.re_errno = 0;
860 check_rpcbind = FALSE;
864 * Now we try version 4 and then 3.
865 * We also send the remote system the address we used to
866 * contact it in case it can help to connect back with us
868 parms.r_prog = program;
869 parms.r_vers = version;
870 /*LINTED const castaway*/
871 parms.r_owner = (char *) &nullstring[0]; /* not needed; */
872 /* just for xdring */
873 parms.r_netid = nconf->nc_netid; /* not really needed */
876 * If a COTS transport is being used, try getting address via CLTS
877 * transport. This works only with version 4.
879 if (nconf->nc_semantics == NC_TPI_COTS_ORD ||
880 nconf->nc_semantics == NC_TPI_COTS) {
883 struct netconfig *nconf_clts;
884 rpcb_entry_list_ptr relp = NULL;
886 if (client == NULL) {
887 /* This did not go through the above PORTMAP/TCP code */
888 if ((handle = __rpc_setconf("datagram_v")) != NULL) {
889 while ((nconf_clts = __rpc_getconf(handle))
891 if (strcmp(nconf_clts->nc_protofmly,
892 nconf->nc_protofmly) != 0) {
895 client = getclnthandle(host, nconf_clts,
899 __rpc_endconf(handle);
902 goto regular_rpcbind; /* Go the regular way */
904 /* This is a UDP PORTMAP handle. Change to version 4 */
906 CLNT_CONTROL(client, CLSET_VERS, (char *)(void *)&vers);
909 * We also send the remote system the address we used to
910 * contact it in case it can help it connect back with us
912 if (parms.r_addr == NULL) {
913 /*LINTED const castaway*/
914 parms.r_addr = (char *) &nullstring[0]; /* for XDRing */
917 CLNT_CONTROL(client, CLSET_RETRY_TIMEOUT, (char *)&rpcbrmttime);
919 clnt_st = CLNT_CALL(client, (rpcproc_t)RPCBPROC_GETADDRLIST,
920 (xdrproc_t) xdr_rpcb, (char *)(void *)&parms,
921 (xdrproc_t) xdr_rpcb_entry_list_ptr,
922 (char *)(void *)&relp, *tp);
923 if (clnt_st == RPC_SUCCESS) {
924 if ((address = got_entry(relp, nconf)) != NULL) {
925 xdr_free((xdrproc_t) xdr_rpcb_entry_list_ptr,
926 (char *)(void *)&relp);
927 CLNT_CONTROL(client, CLGET_SVC_ADDR,
928 (char *)(void *)&servaddr);
929 __rpc_fixup_addr(address, &servaddr);
932 /* Entry not found for this transport */
933 xdr_free((xdrproc_t) xdr_rpcb_entry_list_ptr,
934 (char *)(void *)&relp);
936 * XXX: should have perhaps returned with error but
937 * since the remote machine might not always be able
938 * to send the address on all transports, we try the
939 * regular way with regular_rpcbind
941 goto regular_rpcbind;
942 } else if ((clnt_st == RPC_PROGVERSMISMATCH) ||
943 (clnt_st == RPC_PROGUNAVAIL)) {
944 start_vers = RPCBVERS; /* Try version 3 now */
945 goto regular_rpcbind; /* Try different versions */
947 rpc_createerr.cf_stat = RPC_PMAPFAILURE;
948 clnt_geterr(client, &rpc_createerr.cf_error);
955 /* Now the same transport is to be used to get the address */
956 if (client && ((nconf->nc_semantics == NC_TPI_COTS_ORD) ||
957 (nconf->nc_semantics == NC_TPI_COTS))) {
958 /* A CLTS type of client - destroy it */
959 CLNT_DESTROY(client);
963 if (client == NULL) {
964 client = getclnthandle(host, nconf, &parms.r_addr);
965 if (client == NULL) {
969 if (parms.r_addr == NULL) {
970 /*LINTED const castaway*/
971 parms.r_addr = (char *) &nullstring[0];
974 /* First try from start_vers and then version 3 (RPCBVERS) */
976 CLNT_CONTROL(client, CLSET_RETRY_TIMEOUT, (char *) &rpcbrmttime);
977 for (vers = start_vers; vers >= RPCBVERS; vers--) {
978 /* Set the version */
979 CLNT_CONTROL(client, CLSET_VERS, (char *)(void *)&vers);
980 clnt_st = CLNT_CALL(client, (rpcproc_t)RPCBPROC_GETADDR,
981 (xdrproc_t) xdr_rpcb, (char *)(void *)&parms,
982 (xdrproc_t) xdr_wrapstring, (char *)(void *) &ua, *tp);
983 if (clnt_st == RPC_SUCCESS) {
984 if ((ua == NULL) || (ua[0] == 0)) {
985 /* address unknown */
986 rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
989 address = uaddr2taddr(nconf, ua);
991 fprintf(stderr, "\tRemote address is [%s]\n", ua);
994 "\tCouldn't resolve remote address!\n");
996 xdr_free((xdrproc_t)xdr_wrapstring,
997 (char *)(void *)&ua);
1000 /* We don't know about your universal address */
1001 rpc_createerr.cf_stat = RPC_N2AXLATEFAILURE;
1004 CLNT_CONTROL(client, CLGET_SVC_ADDR,
1005 (char *)(void *)&servaddr);
1006 __rpc_fixup_addr(address, &servaddr);
1008 } else if (clnt_st == RPC_PROGVERSMISMATCH) {
1009 struct rpc_err rpcerr;
1011 clnt_geterr(client, &rpcerr);
1012 if (rpcerr.re_vers.low > RPCBVERS4)
1013 goto error; /* a new version, can't handle */
1014 } else if (clnt_st != RPC_PROGUNAVAIL) {
1015 /* Cant handle this error */
1016 rpc_createerr.cf_stat = clnt_st;
1017 clnt_geterr(client, &rpc_createerr.cf_error);
1024 CLNT_DESTROY(client);
1028 if (nconf->nc_semantics != NC_TPI_CLTS) {
1029 /* This client is the connectionless one */
1031 CLNT_DESTROY(client);
1037 } else if (client) {
1038 CLNT_DESTROY(client);
1040 if (parms.r_addr != NULL && parms.r_addr != nullstring)
1047 * Find the mapped address for program, version.
1048 * Calls the rpcbind service remotely to do the lookup.
1049 * Uses the transport specified in nconf.
1050 * Returns FALSE (0) if no map exists, else returns 1.
1052 * Assuming that the address is all properly allocated
1055 rpcb_getaddr(program, version, nconf, address, host)
1058 const struct netconfig *nconf;
1059 struct netbuf *address;
1064 if ((na = __rpcb_findaddr_timed(program, version,
1065 (struct netconfig *) nconf, (char *) host,
1066 (CLIENT **) NULL, (struct timeval *) NULL)) == NULL)
1069 if (na->len > address->maxlen) {
1070 /* Too long address */
1073 rpc_createerr.cf_stat = RPC_FAILED;
1076 memcpy(address->buf, na->buf, (size_t)na->len);
1077 address->len = na->len;
1084 * Get a copy of the current maps.
1085 * Calls the rpcbind service remotely to get the maps.
1087 * It returns only a list of the services
1088 * It returns NULL on failure.
1091 rpcb_getmaps(nconf, host)
1092 const struct netconfig *nconf;
1095 rpcblist_ptr head = NULL;
1097 enum clnt_stat clnt_st;
1100 client = getclnthandle(host, nconf, NULL);
1101 if (client == NULL) {
1104 clnt_st = CLNT_CALL(client, (rpcproc_t)RPCBPROC_DUMP,
1105 (xdrproc_t) xdr_void, NULL, (xdrproc_t) xdr_rpcblist_ptr,
1106 (char *)(void *)&head, tottimeout);
1107 if (clnt_st == RPC_SUCCESS)
1110 if ((clnt_st != RPC_PROGVERSMISMATCH) &&
1111 (clnt_st != RPC_PROGUNAVAIL)) {
1112 rpc_createerr.cf_stat = RPC_RPCBFAILURE;
1113 clnt_geterr(client, &rpc_createerr.cf_error);
1117 /* fall back to earlier version */
1118 CLNT_CONTROL(client, CLGET_VERS, (char *)(void *)&vers);
1119 if (vers == RPCBVERS4) {
1121 CLNT_CONTROL(client, CLSET_VERS, (char *)(void *)&vers);
1122 if (CLNT_CALL(client, (rpcproc_t)RPCBPROC_DUMP,
1123 (xdrproc_t) xdr_void, NULL, (xdrproc_t) xdr_rpcblist_ptr,
1124 (char *)(void *)&head, tottimeout) == RPC_SUCCESS)
1127 rpc_createerr.cf_stat = RPC_RPCBFAILURE;
1128 clnt_geterr(client, &rpc_createerr.cf_error);
1131 CLNT_DESTROY(client);
1136 * rpcbinder remote-call-service interface.
1137 * This routine is used to call the rpcbind remote call service
1138 * which will look up a service program in the address maps, and then
1139 * remotely call that routine with the given parameters. This allows
1140 * programs to do a lookup and call in one step.
1143 rpcb_rmtcall(nconf, host, prog, vers, proc, xdrargs, argsp,
1144 xdrres, resp, tout, addr_ptr)
1145 const struct netconfig *nconf; /* Netconfig structure */
1146 const char *host; /* Remote host name */
1149 rpcproc_t proc; /* Remote proc identifiers */
1150 xdrproc_t xdrargs, xdrres; /* XDR routines */
1151 caddr_t argsp, resp; /* Argument and Result */
1152 struct timeval tout; /* Timeout value for this call */
1153 const struct netbuf *addr_ptr; /* Preallocated netbuf address */
1156 enum clnt_stat stat;
1157 struct r_rpcb_rmtcallargs a;
1158 struct r_rpcb_rmtcallres r;
1159 rpcvers_t rpcb_vers;
1162 client = getclnthandle(host, nconf, NULL);
1163 if (client == NULL) {
1164 return (RPC_FAILED);
1166 /*LINTED const castaway*/
1167 CLNT_CONTROL(client, CLSET_RETRY_TIMEOUT, (char *)(void *)&rmttimeout);
1171 a.args.args_val = argsp;
1172 a.xdr_args = xdrargs;
1174 r.results.results_val = resp;
1177 for (rpcb_vers = RPCBVERS4; rpcb_vers >= RPCBVERS; rpcb_vers--) {
1178 CLNT_CONTROL(client, CLSET_VERS, (char *)(void *)&rpcb_vers);
1179 stat = CLNT_CALL(client, (rpcproc_t)RPCBPROC_CALLIT,
1180 (xdrproc_t) xdr_rpcb_rmtcallargs, (char *)(void *)&a,
1181 (xdrproc_t) xdr_rpcb_rmtcallres, (char *)(void *)&r, tout);
1182 if ((stat == RPC_SUCCESS) && (addr_ptr != NULL)) {
1184 /*LINTED const castaway*/
1185 na = uaddr2taddr((struct netconfig *) nconf, r.addr);
1187 stat = RPC_N2AXLATEFAILURE;
1188 /*LINTED const castaway*/
1189 ((struct netbuf *) addr_ptr)->len = 0;
1192 if (na->len > addr_ptr->maxlen) {
1193 /* Too long address */
1194 stat = RPC_FAILED; /* XXX A better error no */
1197 /*LINTED const castaway*/
1198 ((struct netbuf *) addr_ptr)->len = 0;
1201 memcpy(addr_ptr->buf, na->buf, (size_t)na->len);
1202 /*LINTED const castaway*/
1203 ((struct netbuf *)addr_ptr)->len = na->len;
1207 } else if ((stat != RPC_PROGVERSMISMATCH) &&
1208 (stat != RPC_PROGUNAVAIL)) {
1213 CLNT_DESTROY(client);
1215 xdr_free((xdrproc_t) xdr_wrapstring, (char *)(void *)&r.addr);
1220 * Gets the time on the remote host.
1221 * Returns 1 if succeeds else 0.
1224 rpcb_gettime(host, timep)
1228 CLIENT *client = NULL;
1230 struct netconfig *nconf;
1235 if ((host == NULL) || (host[0] == 0)) {
1240 if ((handle = __rpc_setconf("netpath")) == NULL) {
1241 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
1244 rpc_createerr.cf_stat = RPC_SUCCESS;
1245 while (client == NULL) {
1246 if ((nconf = __rpc_getconf(handle)) == NULL) {
1247 if (rpc_createerr.cf_stat == RPC_SUCCESS)
1248 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
1251 client = getclnthandle(host, nconf, NULL);
1255 __rpc_endconf(handle);
1256 if (client == (CLIENT *) NULL) {
1260 st = CLNT_CALL(client, (rpcproc_t)RPCBPROC_GETTIME,
1261 (xdrproc_t) xdr_void, NULL,
1262 (xdrproc_t) xdr_int, (char *)(void *)timep, tottimeout);
1264 if ((st == RPC_PROGVERSMISMATCH) || (st == RPC_PROGUNAVAIL)) {
1265 CLNT_CONTROL(client, CLGET_VERS, (char *)(void *)&vers);
1266 if (vers == RPCBVERS4) {
1267 /* fall back to earlier version */
1269 CLNT_CONTROL(client, CLSET_VERS, (char *)(void *)&vers);
1270 st = CLNT_CALL(client, (rpcproc_t)RPCBPROC_GETTIME,
1271 (xdrproc_t) xdr_void, NULL,
1272 (xdrproc_t) xdr_int, (char *)(void *)timep,
1276 CLNT_DESTROY(client);
1277 return (st == RPC_SUCCESS? TRUE: FALSE);
1281 xdr_netbuf(XDR *xdrs, struct netbuf *objp)
1286 if (!xdr_uint32_t(xdrs, (uint32_t *) &objp->maxlen)) {
1290 dummy = xdr_bytes(xdrs, (char **) pp,
1291 (u_int *)&(objp->len), objp->maxlen);
1296 * Converts taddr to universal address. This routine should never
1297 * really be called because local n2a libraries are always provided.
1300 rpcb_taddr2uaddr(struct netconfig *nconf, struct netbuf *taddr)
1306 /* parameter checking */
1307 if (nconf == NULL) {
1308 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
1311 if (taddr == NULL) {
1312 rpc_createerr.cf_stat = RPC_UNKNOWNADDR;
1315 client = local_rpcb();
1320 CLNT_CALL(client, (rpcproc_t)RPCBPROC_TADDR2UADDR,
1321 (xdrproc_t) xdr_netbuf, (char *)(void *)taddr,
1322 (xdrproc_t) xdr_wrapstring, (char *)(void *)&uaddr, tottimeout);
1323 CLNT_DESTROY(client);
1328 * Converts universal address to netbuf. This routine should never
1329 * really be called because local n2a libraries are always provided.
1332 rpcb_uaddr2taddr(struct netconfig *nconf, char *uaddr)
1335 struct netbuf *taddr;
1338 /* parameter checking */
1339 if (nconf == NULL) {
1340 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
1343 if (uaddr == NULL) {
1344 rpc_createerr.cf_stat = RPC_UNKNOWNADDR;
1347 client = local_rpcb();
1352 taddr = (struct netbuf *)malloc(sizeof (struct netbuf), M_RPC, M_WAITOK|M_ZERO);
1353 if (CLNT_CALL(client, (rpcproc_t)RPCBPROC_UADDR2TADDR,
1354 (xdrproc_t) xdr_wrapstring, (char *)(void *)&uaddr,
1355 (xdrproc_t) xdr_netbuf, (char *)(void *)taddr,
1356 tottimeout) != RPC_SUCCESS) {
1360 CLNT_DESTROY(client);