1 /* $NetBSD: rpcb_clnt.c,v 1.6 2000/07/16 06:41:43 itojun Exp $ */
4 * The contents of this file are subject to the Sun Standards
5 * License Version 1.0 the (the "License";) You may not use
6 * this file except in compliance with the License. You may
7 * obtain a copy of the License at lib/libc/rpc/LICENSE
9 * Software distributed under the License is distributed on
10 * an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either
11 * express or implied. See the License for the specific
12 * language governing rights and limitations under the License.
14 * The Original Code is Copyright 1998 by Sun Microsystems, Inc
16 * The Initial Developer of the Original Code is: Sun
19 * All Rights Reserved.
21 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
22 * unrestricted use provided that this legend is included on all tape
23 * media and as a part of the software program in whole or part. Users
24 * may copy or modify Sun RPC without charge, but are not authorized
25 * to license or distribute it to anyone else except as part of a product or
26 * program developed by the user.
28 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
29 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
32 * Sun RPC is provided with no support and without any obligation on the
33 * part of Sun Microsystems, Inc. to assist in its use, correction,
34 * modification or enhancement.
36 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
37 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
38 * OR ANY PART THEREOF.
40 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
41 * or profits or other special, indirect and consequential damages, even if
42 * Sun has been advised of the possibility of such damages.
44 * Sun Microsystems, Inc.
46 * Mountain View, California 94043
49 * Copyright (c) 1986-1991 by Sun Microsystems Inc.
52 /* #ident "@(#)rpcb_clnt.c 1.27 94/04/24 SMI" */
55 #if defined(LIBC_SCCS) && !defined(lint)
56 static char sccsid[] = "@(#)rpcb_clnt.c 1.30 89/06/21 Copyr 1988 Sun Micro";
58 #include <sys/cdefs.h>
59 __FBSDID("$FreeBSD$");
63 * interface to rpcbind rpc service.
65 * Copyright (C) 1988, Sun Microsystems, Inc.
68 #include "opt_inet6.h"
70 #include <sys/param.h>
71 #include <sys/systm.h>
72 #include <sys/kernel.h>
73 #include <sys/malloc.h>
75 #include <sys/socket.h>
76 #include <sys/socketvar.h>
79 #include <rpc/rpcb_clnt.h>
80 #include <rpc/rpcb_prot.h>
82 #include <rpc/rpc_com.h>
84 static struct timeval tottimeout = { 60, 0 };
85 static const struct timeval rmttimeout = { 3, 0 };
86 static const char nullstring[] = "\000";
88 static CLIENT *local_rpcb(void);
92 static struct timeval rpcbrmttime = { 15, 0 };
96 struct address_cache {
100 struct netbuf *ac_taddr;
101 struct address_cache *ac_next;
104 static struct address_cache *front;
105 static int cachesize;
107 #define CLCR_GET_RPCB_TIMEOUT 1
108 #define CLCR_SET_RPCB_TIMEOUT 2
111 extern int __rpc_lowvers;
113 static struct address_cache *check_cache(const char *, const char *);
114 static void delete_cache(struct netbuf *);
115 static void add_cache(const char *, const char *, struct netbuf *, char *);
116 static CLIENT *getclnthandle(const char *, const struct netconfig *, char **);
117 static CLIENT *local_rpcb(void);
118 static struct netbuf *got_entry(rpcb_entry_list_ptr, const struct netconfig *);
121 * This routine adjusts the timeout used for calls to the remote rpcbind.
122 * Also, this routine can be used to set the use of portmapper version 2
123 * only when doing rpc_broadcasts
124 * These are private routines that may not be provided in future releases.
127 __rpc_control(request, info)
132 case CLCR_GET_RPCB_TIMEOUT:
133 *(struct timeval *)info = tottimeout;
135 case CLCR_SET_RPCB_TIMEOUT:
136 tottimeout = *(struct timeval *)info;
138 case CLCR_SET_LOWVERS:
139 __rpc_lowvers = *(int *)info;
141 case CLCR_GET_LOWVERS:
142 *(int *)info = __rpc_lowvers;
151 * It might seem that a reader/writer lock would be more reasonable here.
152 * However because getclnthandle(), the only user of the cache functions,
153 * may do a delete_cache() operation if a check_cache() fails to return an
154 * address useful to clnt_tli_create(), we may as well use a mutex.
157 * As it turns out, if the cache lock is *not* a reader/writer lock, we will
158 * block all clnt_create's if we are trying to connect to a host that's down,
159 * since the lock will be held all during that time.
163 * The routines check_cache(), add_cache(), delete_cache() manage the
164 * cache of rpcbind addresses for (host, netid).
167 static struct address_cache *
168 check_cache(host, netid)
169 const char *host, *netid;
171 struct address_cache *cptr;
173 /* READ LOCK HELD ON ENTRY: rpcbaddr_cache_lock */
175 for (cptr = front; cptr != NULL; cptr = cptr->ac_next) {
176 if (!strcmp(cptr->ac_host, host) &&
177 !strcmp(cptr->ac_netid, netid)) {
179 fprintf(stderr, "Found cache entry for %s: %s\n",
185 return ((struct address_cache *) NULL);
192 struct address_cache *cptr, *prevptr = NULL;
194 /* WRITE LOCK HELD ON ENTRY: rpcbaddr_cache_lock */
195 for (cptr = front; cptr != NULL; cptr = cptr->ac_next) {
196 if (!memcmp(cptr->ac_taddr->buf, addr->buf, addr->len)) {
198 free(cptr->ac_netid);
199 free(cptr->ac_taddr->buf);
200 free(cptr->ac_taddr);
202 free(cptr->ac_uaddr);
204 prevptr->ac_next = cptr->ac_next;
206 front = cptr->ac_next;
216 add_cache(host, netid, taddr, uaddr)
217 const char *host, *netid;
219 struct netbuf *taddr;
221 struct address_cache *ad_cache, *cptr, *prevptr;
223 ad_cache = (struct address_cache *)
224 malloc(sizeof (struct address_cache));
228 ad_cache->ac_host = strdup(host);
229 ad_cache->ac_netid = strdup(netid);
230 ad_cache->ac_uaddr = uaddr ? strdup(uaddr) : NULL;
231 ad_cache->ac_taddr = (struct netbuf *)malloc(sizeof (struct netbuf));
232 if (!ad_cache->ac_host || !ad_cache->ac_netid || !ad_cache->ac_taddr ||
233 (uaddr && !ad_cache->ac_uaddr)) {
236 ad_cache->ac_taddr->len = ad_cache->ac_taddr->maxlen = taddr->len;
237 ad_cache->ac_taddr->buf = (char *) malloc(taddr->len);
238 if (ad_cache->ac_taddr->buf == NULL) {
240 if (ad_cache->ac_host)
241 free(ad_cache->ac_host);
242 if (ad_cache->ac_netid)
243 free(ad_cache->ac_netid);
244 if (ad_cache->ac_uaddr)
245 free(ad_cache->ac_uaddr);
246 if (ad_cache->ac_taddr)
247 free(ad_cache->ac_taddr);
251 memcpy(ad_cache->ac_taddr->buf, taddr->buf, taddr->len);
253 fprintf(stderr, "Added to cache: %s : %s\n", host, netid);
256 /* VARIABLES PROTECTED BY rpcbaddr_cache_lock: cptr */
258 rwlock_wrlock(&rpcbaddr_cache_lock);
259 if (cachesize < CACHESIZE) {
260 ad_cache->ac_next = front;
264 /* Free the last entry */
267 while (cptr->ac_next) {
269 cptr = cptr->ac_next;
273 fprintf(stderr, "Deleted from cache: %s : %s\n",
274 cptr->ac_host, cptr->ac_netid);
277 free(cptr->ac_netid);
278 free(cptr->ac_taddr->buf);
279 free(cptr->ac_taddr);
281 free(cptr->ac_uaddr);
284 prevptr->ac_next = NULL;
285 ad_cache->ac_next = front;
289 ad_cache->ac_next = NULL;
293 rwlock_unlock(&rpcbaddr_cache_lock);
297 * This routine will return a client handle that is connected to the
298 * rpcbind. If targaddr is non-NULL, the "universal address" of the
299 * host will be stored in *targaddr; the caller is responsible for
300 * freeing this string.
301 * On error, returns NULL and free's everything.
304 getclnthandle(host, nconf, targaddr)
306 const struct netconfig *nconf;
310 struct netbuf *addr, taddr;
311 struct netbuf addr_to_delete;
312 struct __rpc_sockinfo si;
313 struct addrinfo hints, *res, *tres;
314 struct address_cache *ad_cache;
317 /* VARIABLES PROTECTED BY rpcbaddr_cache_lock: ad_cache */
319 /* Get the address of the rpcbind. Check cache first */
321 addr_to_delete.len = 0;
322 rwlock_rdlock(&rpcbaddr_cache_lock);
325 ad_cache = check_cache(host, nconf->nc_netid);
326 if (ad_cache != NULL) {
327 addr = ad_cache->ac_taddr;
328 client = clnt_tli_create(RPC_ANYFD, nconf, addr,
329 (rpcprog_t)RPCBPROG, (rpcvers_t)RPCBVERS4, 0, 0);
330 if (client != NULL) {
332 *targaddr = strdup(ad_cache->ac_uaddr);
333 rwlock_unlock(&rpcbaddr_cache_lock);
336 addr_to_delete.len = addr->len;
337 addr_to_delete.buf = (char *)malloc(addr->len);
338 if (addr_to_delete.buf == NULL) {
339 addr_to_delete.len = 0;
341 memcpy(addr_to_delete.buf, addr->buf, addr->len);
344 rwlock_unlock(&rpcbaddr_cache_lock);
345 if (addr_to_delete.len != 0) {
347 * Assume this may be due to cache data being
350 rwlock_wrlock(&rpcbaddr_cache_lock);
351 delete_cache(&addr_to_delete);
352 rwlock_unlock(&rpcbaddr_cache_lock);
353 free(addr_to_delete.buf);
355 if (!__rpc_nconf2sockinfo(nconf, &si)) {
356 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
360 memset(&hints, 0, sizeof hints);
361 hints.ai_family = si.si_af;
362 hints.ai_socktype = si.si_socktype;
363 hints.ai_protocol = si.si_proto;
366 printf("trying netid %s family %d proto %d socktype %d\n",
367 nconf->nc_netid, si.si_af, si.si_proto, si.si_socktype);
370 if (nconf->nc_protofmly != NULL && strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0) {
371 client = local_rpcb();
374 clnt_pcreateerror("rpcbind clnt interface");
378 struct sockaddr_un sun;
380 *targaddr = malloc(sizeof(sun.sun_path));
381 if (*targaddr == NULL) {
382 CLNT_DESTROY(client);
385 strncpy(*targaddr, _PATH_RPCBINDSOCK,
386 sizeof(sun.sun_path));
391 if (getaddrinfo(host, "sunrpc", &hints, &res) != 0) {
392 rpc_createerr.cf_stat = RPC_UNKNOWNHOST;
397 for (tres = res; tres != NULL; tres = tres->ai_next) {
398 taddr.buf = tres->ai_addr;
399 taddr.len = taddr.maxlen = tres->ai_addrlen;
405 ua = taddr2uaddr(nconf, &taddr);
406 fprintf(stderr, "Got it [%s]\n", ua);
415 fprintf(stderr, "\tnetbuf len = %d, maxlen = %d\n",
416 taddr.len, taddr.maxlen);
417 fprintf(stderr, "\tAddress is ");
418 for (i = 0; i < taddr.len; i++)
419 fprintf(stderr, "%u.", ((char *)(taddr.buf))[i]);
420 fprintf(stderr, "\n");
423 client = clnt_tli_create(RPC_ANYFD, nconf, &taddr,
424 (rpcprog_t)RPCBPROG, (rpcvers_t)RPCBVERS4, 0, 0);
427 clnt_pcreateerror("rpcbind clnt interface");
432 tmpaddr = targaddr ? taddr2uaddr(nconf, &taddr) : NULL;
433 add_cache(host, nconf->nc_netid, &taddr, tmpaddr);
447 #define IN4_LOCALHOST_STRING "127.0.0.1"
448 #define IN6_LOCALHOST_STRING "::1"
451 * This routine will return a client handle that is connected to the local
452 * rpcbind. Returns NULL on error and free's everything.
460 struct sockaddr_un sun;
464 * Try connecting to the local rpcbind through a local socket
465 * first. If this doesn't work, try all transports defined in
466 * the netconfig file.
468 memset(&sun, 0, sizeof sun);
470 error = socreate(AF_LOCAL, &so, SOCK_STREAM, 0, curthread->td_ucred,
474 sun.sun_family = AF_LOCAL;
475 strcpy(sun.sun_path, _PATH_RPCBINDSOCK);
476 sun.sun_len = SUN_LEN(&sun);
478 tsize = __rpc_get_t_size(AF_LOCAL, 0, 0);
479 client = clnt_vc_create(so, (struct sockaddr *)&sun, (rpcprog_t)RPCBPROG,
480 (rpcvers_t)RPCBVERS, tsize, tsize, 1);
482 if (client != NULL) {
483 /* Mark the socket to be closed in destructor */
484 (void) CLNT_CONTROL(client, CLSET_FD_CLOSE, NULL);
488 /* Nobody needs this socket anymore; free the descriptor. */
494 static struct netconfig *loopnconf;
495 static char *localhostname;
497 /* VARIABLES PROTECTED BY loopnconf_lock: loopnconf */
498 mutex_lock(&loopnconf_lock);
499 if (loopnconf == NULL) {
500 struct netconfig *nconf, *tmpnconf = NULL;
504 nc_handle = setnetconfig();
505 if (nc_handle == NULL) {
506 /* fails to open netconfig file */
507 syslog (LOG_ERR, "rpc: failed to open " NETCONFIG);
508 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
509 mutex_unlock(&loopnconf_lock);
512 while ((nconf = getnetconfig(nc_handle)) != NULL) {
515 strcmp(nconf->nc_protofmly, NC_INET6) == 0 ||
517 strcmp(nconf->nc_protofmly, NC_INET) == 0) &&
518 (nconf->nc_semantics == NC_TPI_COTS ||
519 nconf->nc_semantics == NC_TPI_COTS_ORD)) {
520 fd = __rpc_nconf2fd(nconf);
522 * Can't create a socket, assume that
523 * this family isn't configured in the kernel.
529 if (!strcmp(nconf->nc_protofmly, NC_INET))
530 localhostname = IN4_LOCALHOST_STRING;
532 localhostname = IN6_LOCALHOST_STRING;
535 if (tmpnconf == NULL) {
536 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
537 mutex_unlock(&loopnconf_lock);
540 loopnconf = getnetconfigent(tmpnconf->nc_netid);
541 /* loopnconf is never freed */
542 endnetconfig(nc_handle);
544 mutex_unlock(&loopnconf_lock);
545 client = getclnthandle(localhostname, loopnconf, NULL);
553 * Set a mapping between program, version and address.
554 * Calls the rpcbind service to do the mapping.
557 rpcb_set(rpcprog_t program, rpcvers_t version,
558 const struct netconfig *nconf, /* Network structure of transport */
559 const struct netbuf *address) /* Services netconfig address */
567 struct netconfig nconfcopy;
568 struct netbuf addresscopy;
570 /* parameter checking */
572 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
575 if (address == NULL) {
576 rpc_createerr.cf_stat = RPC_UNKNOWNADDR;
579 client = local_rpcb();
584 /* convert to universal */
585 /*LINTED const castaway*/
587 addresscopy = *address;
588 parms.r_addr = taddr2uaddr(&nconfcopy, &addresscopy);
590 CLNT_DESTROY(client);
591 rpc_createerr.cf_stat = RPC_N2AXLATEFAILURE;
592 return (FALSE); /* no universal address */
594 parms.r_prog = program;
595 parms.r_vers = version;
596 parms.r_netid = nconf->nc_netid;
599 * Though uid is not being used directly, we still send it for
600 * completeness. For non-unix platforms, perhaps some other
601 * string or an empty string can be sent.
603 (void) snprintf(uidbuf, sizeof uidbuf, "%d", geteuid());
604 parms.r_owner = uidbuf;
609 CLNT_CALL(client, (rpcproc_t)RPCBPROC_SET, (xdrproc_t) xdr_rpcb,
610 (char *)(void *)&parms, (xdrproc_t) xdr_bool,
611 (char *)(void *)&rslt, tottimeout);
613 CLNT_DESTROY(client);
614 free(parms.r_addr, M_RPC);
619 * Remove the mapping between program, version and netbuf address.
620 * Calls the rpcbind service to do the un-mapping.
621 * If netbuf is NULL, unset for all the transports, otherwise unset
622 * only for the given transport.
625 rpcb_unset(rpcprog_t program, rpcvers_t version, const struct netconfig *nconf)
634 client = local_rpcb();
639 parms.r_prog = program;
640 parms.r_vers = version;
642 parms.r_netid = nconf->nc_netid;
644 /*LINTED const castaway*/
645 parms.r_netid = (char *)(uintptr_t) &nullstring[0]; /* unsets all */
647 /*LINTED const castaway*/
648 parms.r_addr = (char *)(uintptr_t) &nullstring[0];
650 (void) snprintf(uidbuf, sizeof uidbuf, "%d", geteuid());
651 parms.r_owner = uidbuf;
656 CLNT_CALL(client, (rpcproc_t)RPCBPROC_UNSET, (xdrproc_t) xdr_rpcb,
657 (char *)(void *)&parms, (xdrproc_t) xdr_bool,
658 (char *)(void *)&rslt, tottimeout);
660 CLNT_DESTROY(client);
667 * From the merged list, find the appropriate entry
669 static struct netbuf *
670 got_entry(relp, nconf)
671 rpcb_entry_list_ptr relp;
672 const struct netconfig *nconf;
674 struct netbuf *na = NULL;
675 rpcb_entry_list_ptr sp;
678 for (sp = relp; sp != NULL; sp = sp->rpcb_entry_next) {
679 rmap = &sp->rpcb_entry_map;
680 if ((strcmp(nconf->nc_proto, rmap->r_nc_proto) == 0) &&
681 (strcmp(nconf->nc_protofmly, rmap->r_nc_protofmly) == 0) &&
682 (nconf->nc_semantics == rmap->r_nc_semantics) &&
683 (rmap->r_maddr != NULL) && (rmap->r_maddr[0] != 0)) {
684 na = uaddr2taddr(nconf, rmap->r_maddr);
686 fprintf(stderr, "\tRemote address is [%s].\n",
690 "\tCouldn't resolve remote address!\n");
699 * Quick check to see if rpcbind is up. Tries to connect over
705 struct netconfig *nconf;
706 struct sockaddr_un sun;
711 localhandle = setnetconfig();
712 while ((nconf = getnetconfig(localhandle)) != NULL) {
713 if (nconf->nc_protofmly != NULL &&
714 strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0)
720 endnetconfig(localhandle);
722 memset(&sun, 0, sizeof sun);
723 sock = _socket(AF_LOCAL, SOCK_STREAM, 0);
726 sun.sun_family = AF_LOCAL;
727 strncpy(sun.sun_path, _PATH_RPCBINDSOCK, sizeof(sun.sun_path));
728 sun.sun_len = SUN_LEN(&sun);
730 if (_connect(sock, (struct sockaddr *)&sun, sun.sun_len) < 0) {
740 * An internal function which optimizes rpcb_getaddr function. It also
741 * returns the client handle that it uses to contact the remote rpcbind.
743 * The algorithm used: If the transports is TCP or UDP, it first tries
744 * version 2 (portmap), 4 and then 3 (svr4). This order should be
745 * changed in the next OS release to 4, 2 and 3. We are assuming that by
746 * that time, version 4 would be available on many machines on the network.
747 * With this algorithm, we get performance as well as a plan for
748 * obsoleting version 2.
750 * For all other transports, the algorithm remains as 4 and then 3.
752 * XXX: Due to some problems with t_connect(), we do not reuse the same client
753 * handle for COTS cases and hence in these cases we do not return the
754 * client handle. This code will change if t_connect() ever
755 * starts working properly. Also look under clnt_vc.c.
758 __rpcb_findaddr_timed(program, version, nconf, host, clpp, tp)
761 const struct netconfig *nconf;
766 static bool_t check_rpcbind = TRUE;
767 CLIENT *client = NULL;
769 enum clnt_stat clnt_st;
772 struct netbuf *address = NULL;
773 rpcvers_t start_vers = RPCBVERS4;
774 struct netbuf servaddr;
776 /* parameter checking */
778 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
783 parms.r_netid = nconf->nc_netid;
786 * According to wire captures, the reference implementation
787 * (OpenSolaris) sends a blank string here too.
792 * Use default total timeout if no timeout is specified.
798 /* Try version 2 for TCP or UDP */
799 if (strcmp(nconf->nc_protofmly, NC_INET) == 0) {
801 struct netbuf remote;
802 rpcvers_t pmapvers = 2;
803 struct pmap pmapparms;
806 * Try UDP only - there are some portmappers out
807 * there that use UDP only.
809 if (strcmp(nconf->nc_proto, NC_TCP) == 0) {
810 struct netconfig *newnconf;
812 if ((newnconf = getnetconfigent("udp")) == NULL) {
813 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
816 client = getclnthandle(host, newnconf, &parms.r_addr);
817 freenetconfigent(newnconf);
819 client = getclnthandle(host, nconf, &parms.r_addr);
825 * Set version and retry timeout.
827 CLNT_CONTROL(client, CLSET_RETRY_TIMEOUT, (char *)&rpcbrmttime);
828 CLNT_CONTROL(client, CLSET_VERS, (char *)&pmapvers);
830 pmapparms.pm_prog = program;
831 pmapparms.pm_vers = version;
832 pmapparms.pm_prot = strcmp(nconf->nc_proto, NC_TCP) ?
833 IPPROTO_UDP : IPPROTO_TCP;
834 pmapparms.pm_port = 0; /* not needed */
835 clnt_st = CLNT_CALL(client, (rpcproc_t)PMAPPROC_GETPORT,
836 (xdrproc_t) xdr_pmap, (caddr_t)(void *)&pmapparms,
837 (xdrproc_t) xdr_u_short, (caddr_t)(void *)&port,
839 if (clnt_st != RPC_SUCCESS) {
840 if ((clnt_st == RPC_PROGVERSMISMATCH) ||
841 (clnt_st == RPC_PROGUNAVAIL))
842 goto try_rpcbind; /* Try different versions */
843 rpc_createerr.cf_stat = RPC_PMAPFAILURE;
844 clnt_geterr(client, &rpc_createerr.cf_error);
846 } else if (port == 0) {
848 rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
852 CLNT_CONTROL(client, CLGET_SVC_ADDR, (char *)&remote);
853 if (((address = (struct netbuf *)
854 malloc(sizeof (struct netbuf))) == NULL) ||
855 ((address->buf = (char *)
856 malloc(remote.len)) == NULL)) {
857 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
858 clnt_geterr(client, &rpc_createerr.cf_error);
865 memcpy(address->buf, remote.buf, remote.len);
866 memcpy(&((char *)address->buf)[sizeof (short)],
867 (char *)(void *)&port, sizeof (short));
868 address->len = address->maxlen = remote.len;
875 * Check if rpcbind is up. This prevents needless delays when
876 * accessing applications such as the keyserver while booting
879 if (check_rpcbind && strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0) {
880 if (!__rpcbind_is_up()) {
881 rpc_createerr.cf_stat = RPC_PMAPFAILURE;
882 rpc_createerr.cf_error.re_errno = 0;
885 check_rpcbind = FALSE;
889 * Now we try version 4 and then 3.
890 * We also send the remote system the address we used to
891 * contact it in case it can help to connect back with us
893 parms.r_prog = program;
894 parms.r_vers = version;
895 /*LINTED const castaway*/
896 parms.r_owner = (char *) &nullstring[0]; /* not needed; */
897 /* just for xdring */
898 parms.r_netid = nconf->nc_netid; /* not really needed */
901 * If a COTS transport is being used, try getting address via CLTS
902 * transport. This works only with version 4.
904 if (nconf->nc_semantics == NC_TPI_COTS_ORD ||
905 nconf->nc_semantics == NC_TPI_COTS) {
908 struct netconfig *nconf_clts;
909 rpcb_entry_list_ptr relp = NULL;
911 if (client == NULL) {
912 /* This did not go through the above PORTMAP/TCP code */
913 if ((handle = __rpc_setconf("datagram_v")) != NULL) {
914 while ((nconf_clts = __rpc_getconf(handle))
916 if (strcmp(nconf_clts->nc_protofmly,
917 nconf->nc_protofmly) != 0) {
920 client = getclnthandle(host, nconf_clts,
924 __rpc_endconf(handle);
927 goto regular_rpcbind; /* Go the regular way */
929 /* This is a UDP PORTMAP handle. Change to version 4 */
931 CLNT_CONTROL(client, CLSET_VERS, (char *)(void *)&vers);
934 * We also send the remote system the address we used to
935 * contact it in case it can help it connect back with us
937 if (parms.r_addr == NULL) {
938 /*LINTED const castaway*/
939 parms.r_addr = (char *) &nullstring[0]; /* for XDRing */
942 CLNT_CONTROL(client, CLSET_RETRY_TIMEOUT, (char *)&rpcbrmttime);
944 clnt_st = CLNT_CALL(client, (rpcproc_t)RPCBPROC_GETADDRLIST,
945 (xdrproc_t) xdr_rpcb, (char *)(void *)&parms,
946 (xdrproc_t) xdr_rpcb_entry_list_ptr,
947 (char *)(void *)&relp, *tp);
948 if (clnt_st == RPC_SUCCESS) {
949 if ((address = got_entry(relp, nconf)) != NULL) {
950 xdr_free((xdrproc_t) xdr_rpcb_entry_list_ptr,
951 (char *)(void *)&relp);
952 CLNT_CONTROL(client, CLGET_SVC_ADDR,
953 (char *)(void *)&servaddr);
954 __rpc_fixup_addr(address, &servaddr);
957 /* Entry not found for this transport */
958 xdr_free((xdrproc_t) xdr_rpcb_entry_list_ptr,
959 (char *)(void *)&relp);
961 * XXX: should have perhaps returned with error but
962 * since the remote machine might not always be able
963 * to send the address on all transports, we try the
964 * regular way with regular_rpcbind
966 goto regular_rpcbind;
967 } else if ((clnt_st == RPC_PROGVERSMISMATCH) ||
968 (clnt_st == RPC_PROGUNAVAIL)) {
969 start_vers = RPCBVERS; /* Try version 3 now */
970 goto regular_rpcbind; /* Try different versions */
972 rpc_createerr.cf_stat = RPC_PMAPFAILURE;
973 clnt_geterr(client, &rpc_createerr.cf_error);
980 /* Now the same transport is to be used to get the address */
981 if (client && ((nconf->nc_semantics == NC_TPI_COTS_ORD) ||
982 (nconf->nc_semantics == NC_TPI_COTS))) {
983 /* A CLTS type of client - destroy it */
984 CLNT_DESTROY(client);
988 if (client == NULL) {
989 client = getclnthandle(host, nconf, &parms.r_addr);
990 if (client == NULL) {
994 if (parms.r_addr == NULL) {
995 /*LINTED const castaway*/
996 parms.r_addr = (char *) &nullstring[0];
999 /* First try from start_vers and then version 3 (RPCBVERS) */
1001 CLNT_CONTROL(client, CLSET_RETRY_TIMEOUT, (char *) &rpcbrmttime);
1002 for (vers = start_vers; vers >= RPCBVERS; vers--) {
1003 /* Set the version */
1004 CLNT_CONTROL(client, CLSET_VERS, (char *)(void *)&vers);
1005 clnt_st = CLNT_CALL(client, (rpcproc_t)RPCBPROC_GETADDR,
1006 (xdrproc_t) xdr_rpcb, (char *)(void *)&parms,
1007 (xdrproc_t) xdr_wrapstring, (char *)(void *) &ua, *tp);
1008 if (clnt_st == RPC_SUCCESS) {
1009 if ((ua == NULL) || (ua[0] == 0)) {
1010 /* address unknown */
1011 rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
1014 address = uaddr2taddr(nconf, ua);
1016 fprintf(stderr, "\tRemote address is [%s]\n", ua);
1019 "\tCouldn't resolve remote address!\n");
1021 xdr_free((xdrproc_t)xdr_wrapstring,
1022 (char *)(void *)&ua);
1025 /* We don't know about your universal address */
1026 rpc_createerr.cf_stat = RPC_N2AXLATEFAILURE;
1029 CLNT_CONTROL(client, CLGET_SVC_ADDR,
1030 (char *)(void *)&servaddr);
1031 __rpc_fixup_addr(address, &servaddr);
1033 } else if (clnt_st == RPC_PROGVERSMISMATCH) {
1034 struct rpc_err rpcerr;
1036 clnt_geterr(client, &rpcerr);
1037 if (rpcerr.re_vers.low > RPCBVERS4)
1038 goto error; /* a new version, can't handle */
1039 } else if (clnt_st != RPC_PROGUNAVAIL) {
1040 /* Cant handle this error */
1041 rpc_createerr.cf_stat = clnt_st;
1042 clnt_geterr(client, &rpc_createerr.cf_error);
1049 CLNT_DESTROY(client);
1053 if (nconf->nc_semantics != NC_TPI_CLTS) {
1054 /* This client is the connectionless one */
1056 CLNT_DESTROY(client);
1062 } else if (client) {
1063 CLNT_DESTROY(client);
1065 if (parms.r_addr != NULL && parms.r_addr != nullstring)
1072 * Find the mapped address for program, version.
1073 * Calls the rpcbind service remotely to do the lookup.
1074 * Uses the transport specified in nconf.
1075 * Returns FALSE (0) if no map exists, else returns 1.
1077 * Assuming that the address is all properly allocated
1080 rpcb_getaddr(program, version, nconf, address, host)
1083 const struct netconfig *nconf;
1084 struct netbuf *address;
1089 if ((na = __rpcb_findaddr_timed(program, version,
1090 (struct netconfig *) nconf, (char *) host,
1091 (CLIENT **) NULL, (struct timeval *) NULL)) == NULL)
1094 if (na->len > address->maxlen) {
1095 /* Too long address */
1098 rpc_createerr.cf_stat = RPC_FAILED;
1101 memcpy(address->buf, na->buf, (size_t)na->len);
1102 address->len = na->len;
1109 * Get a copy of the current maps.
1110 * Calls the rpcbind service remotely to get the maps.
1112 * It returns only a list of the services
1113 * It returns NULL on failure.
1116 rpcb_getmaps(nconf, host)
1117 const struct netconfig *nconf;
1120 rpcblist_ptr head = NULL;
1122 enum clnt_stat clnt_st;
1125 client = getclnthandle(host, nconf, NULL);
1126 if (client == NULL) {
1129 clnt_st = CLNT_CALL(client, (rpcproc_t)RPCBPROC_DUMP,
1130 (xdrproc_t) xdr_void, NULL, (xdrproc_t) xdr_rpcblist_ptr,
1131 (char *)(void *)&head, tottimeout);
1132 if (clnt_st == RPC_SUCCESS)
1135 if ((clnt_st != RPC_PROGVERSMISMATCH) &&
1136 (clnt_st != RPC_PROGUNAVAIL)) {
1137 rpc_createerr.cf_stat = RPC_RPCBFAILURE;
1138 clnt_geterr(client, &rpc_createerr.cf_error);
1142 /* fall back to earlier version */
1143 CLNT_CONTROL(client, CLGET_VERS, (char *)(void *)&vers);
1144 if (vers == RPCBVERS4) {
1146 CLNT_CONTROL(client, CLSET_VERS, (char *)(void *)&vers);
1147 if (CLNT_CALL(client, (rpcproc_t)RPCBPROC_DUMP,
1148 (xdrproc_t) xdr_void, NULL, (xdrproc_t) xdr_rpcblist_ptr,
1149 (char *)(void *)&head, tottimeout) == RPC_SUCCESS)
1152 rpc_createerr.cf_stat = RPC_RPCBFAILURE;
1153 clnt_geterr(client, &rpc_createerr.cf_error);
1156 CLNT_DESTROY(client);
1161 * rpcbinder remote-call-service interface.
1162 * This routine is used to call the rpcbind remote call service
1163 * which will look up a service program in the address maps, and then
1164 * remotely call that routine with the given parameters. This allows
1165 * programs to do a lookup and call in one step.
1168 rpcb_rmtcall(nconf, host, prog, vers, proc, xdrargs, argsp,
1169 xdrres, resp, tout, addr_ptr)
1170 const struct netconfig *nconf; /* Netconfig structure */
1171 const char *host; /* Remote host name */
1174 rpcproc_t proc; /* Remote proc identifiers */
1175 xdrproc_t xdrargs, xdrres; /* XDR routines */
1176 caddr_t argsp, resp; /* Argument and Result */
1177 struct timeval tout; /* Timeout value for this call */
1178 const struct netbuf *addr_ptr; /* Preallocated netbuf address */
1181 enum clnt_stat stat;
1182 struct r_rpcb_rmtcallargs a;
1183 struct r_rpcb_rmtcallres r;
1184 rpcvers_t rpcb_vers;
1187 client = getclnthandle(host, nconf, NULL);
1188 if (client == NULL) {
1189 return (RPC_FAILED);
1191 /*LINTED const castaway*/
1192 CLNT_CONTROL(client, CLSET_RETRY_TIMEOUT, (char *)(void *)&rmttimeout);
1196 a.args.args_val = argsp;
1197 a.xdr_args = xdrargs;
1199 r.results.results_val = resp;
1202 for (rpcb_vers = RPCBVERS4; rpcb_vers >= RPCBVERS; rpcb_vers--) {
1203 CLNT_CONTROL(client, CLSET_VERS, (char *)(void *)&rpcb_vers);
1204 stat = CLNT_CALL(client, (rpcproc_t)RPCBPROC_CALLIT,
1205 (xdrproc_t) xdr_rpcb_rmtcallargs, (char *)(void *)&a,
1206 (xdrproc_t) xdr_rpcb_rmtcallres, (char *)(void *)&r, tout);
1207 if ((stat == RPC_SUCCESS) && (addr_ptr != NULL)) {
1209 /*LINTED const castaway*/
1210 na = uaddr2taddr((struct netconfig *) nconf, r.addr);
1212 stat = RPC_N2AXLATEFAILURE;
1213 /*LINTED const castaway*/
1214 ((struct netbuf *) addr_ptr)->len = 0;
1217 if (na->len > addr_ptr->maxlen) {
1218 /* Too long address */
1219 stat = RPC_FAILED; /* XXX A better error no */
1222 /*LINTED const castaway*/
1223 ((struct netbuf *) addr_ptr)->len = 0;
1226 memcpy(addr_ptr->buf, na->buf, (size_t)na->len);
1227 /*LINTED const castaway*/
1228 ((struct netbuf *)addr_ptr)->len = na->len;
1232 } else if ((stat != RPC_PROGVERSMISMATCH) &&
1233 (stat != RPC_PROGUNAVAIL)) {
1238 CLNT_DESTROY(client);
1240 xdr_free((xdrproc_t) xdr_wrapstring, (char *)(void *)&r.addr);
1245 * Gets the time on the remote host.
1246 * Returns 1 if succeeds else 0.
1249 rpcb_gettime(host, timep)
1253 CLIENT *client = NULL;
1255 struct netconfig *nconf;
1260 if ((host == NULL) || (host[0] == 0)) {
1265 if ((handle = __rpc_setconf("netpath")) == NULL) {
1266 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
1269 rpc_createerr.cf_stat = RPC_SUCCESS;
1270 while (client == NULL) {
1271 if ((nconf = __rpc_getconf(handle)) == NULL) {
1272 if (rpc_createerr.cf_stat == RPC_SUCCESS)
1273 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
1276 client = getclnthandle(host, nconf, NULL);
1280 __rpc_endconf(handle);
1281 if (client == (CLIENT *) NULL) {
1285 st = CLNT_CALL(client, (rpcproc_t)RPCBPROC_GETTIME,
1286 (xdrproc_t) xdr_void, NULL,
1287 (xdrproc_t) xdr_int, (char *)(void *)timep, tottimeout);
1289 if ((st == RPC_PROGVERSMISMATCH) || (st == RPC_PROGUNAVAIL)) {
1290 CLNT_CONTROL(client, CLGET_VERS, (char *)(void *)&vers);
1291 if (vers == RPCBVERS4) {
1292 /* fall back to earlier version */
1294 CLNT_CONTROL(client, CLSET_VERS, (char *)(void *)&vers);
1295 st = CLNT_CALL(client, (rpcproc_t)RPCBPROC_GETTIME,
1296 (xdrproc_t) xdr_void, NULL,
1297 (xdrproc_t) xdr_int, (char *)(void *)timep,
1301 CLNT_DESTROY(client);
1302 return (st == RPC_SUCCESS? TRUE: FALSE);
1306 xdr_netbuf(XDR *xdrs, struct netbuf *objp)
1311 if (!xdr_uint32_t(xdrs, (uint32_t *) &objp->maxlen)) {
1315 dummy = xdr_bytes(xdrs, (char **) pp,
1316 (u_int *)&(objp->len), objp->maxlen);
1321 * Converts taddr to universal address. This routine should never
1322 * really be called because local n2a libraries are always provided.
1325 rpcb_taddr2uaddr(struct netconfig *nconf, struct netbuf *taddr)
1331 /* parameter checking */
1332 if (nconf == NULL) {
1333 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
1336 if (taddr == NULL) {
1337 rpc_createerr.cf_stat = RPC_UNKNOWNADDR;
1340 client = local_rpcb();
1345 CLNT_CALL(client, (rpcproc_t)RPCBPROC_TADDR2UADDR,
1346 (xdrproc_t) xdr_netbuf, (char *)(void *)taddr,
1347 (xdrproc_t) xdr_wrapstring, (char *)(void *)&uaddr, tottimeout);
1348 CLNT_DESTROY(client);
1353 * Converts universal address to netbuf. This routine should never
1354 * really be called because local n2a libraries are always provided.
1357 rpcb_uaddr2taddr(struct netconfig *nconf, char *uaddr)
1360 struct netbuf *taddr;
1363 /* parameter checking */
1364 if (nconf == NULL) {
1365 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
1368 if (uaddr == NULL) {
1369 rpc_createerr.cf_stat = RPC_UNKNOWNADDR;
1372 client = local_rpcb();
1377 taddr = (struct netbuf *)malloc(sizeof (struct netbuf), M_RPC, M_WAITOK|M_ZERO);
1378 if (CLNT_CALL(client, (rpcproc_t)RPCBPROC_UADDR2TADDR,
1379 (xdrproc_t) xdr_wrapstring, (char *)(void *)&uaddr,
1380 (xdrproc_t) xdr_netbuf, (char *)(void *)taddr,
1381 tottimeout) != RPC_SUCCESS) {
1385 CLNT_DESTROY(client);