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);
186 free(cptr->ac_uaddr);
188 prevptr->ac_next = cptr->ac_next;
190 front = cptr->ac_next;
200 add_cache(host, netid, taddr, uaddr)
201 const char *host, *netid;
203 struct netbuf *taddr;
205 struct address_cache *ad_cache, *cptr, *prevptr;
207 ad_cache = (struct address_cache *)
208 malloc(sizeof (struct address_cache));
212 ad_cache->ac_host = strdup(host);
213 ad_cache->ac_netid = strdup(netid);
214 ad_cache->ac_uaddr = uaddr ? strdup(uaddr) : NULL;
215 ad_cache->ac_taddr = (struct netbuf *)malloc(sizeof (struct netbuf));
216 if (!ad_cache->ac_host || !ad_cache->ac_netid || !ad_cache->ac_taddr ||
217 (uaddr && !ad_cache->ac_uaddr)) {
220 ad_cache->ac_taddr->len = ad_cache->ac_taddr->maxlen = taddr->len;
221 ad_cache->ac_taddr->buf = (char *) malloc(taddr->len);
222 if (ad_cache->ac_taddr->buf == NULL) {
224 free(ad_cache->ac_host);
225 free(ad_cache->ac_netid);
226 free(ad_cache->ac_uaddr);
227 free(ad_cache->ac_taddr);
231 memcpy(ad_cache->ac_taddr->buf, taddr->buf, taddr->len);
233 fprintf(stderr, "Added to cache: %s : %s\n", host, netid);
236 /* VARIABLES PROTECTED BY rpcbaddr_cache_lock: cptr */
238 rwlock_wrlock(&rpcbaddr_cache_lock);
239 if (cachesize < CACHESIZE) {
240 ad_cache->ac_next = front;
244 /* Free the last entry */
247 while (cptr->ac_next) {
249 cptr = cptr->ac_next;
253 fprintf(stderr, "Deleted from cache: %s : %s\n",
254 cptr->ac_host, cptr->ac_netid);
257 free(cptr->ac_netid);
258 free(cptr->ac_taddr->buf);
259 free(cptr->ac_taddr);
260 free(cptr->ac_uaddr);
263 prevptr->ac_next = NULL;
264 ad_cache->ac_next = front;
268 ad_cache->ac_next = NULL;
272 rwlock_unlock(&rpcbaddr_cache_lock);
276 * This routine will return a client handle that is connected to the
277 * rpcbind. If targaddr is non-NULL, the "universal address" of the
278 * host will be stored in *targaddr; the caller is responsible for
279 * freeing this string.
280 * On error, returns NULL and free's everything.
283 getclnthandle(host, nconf, targaddr)
285 const struct netconfig *nconf;
289 struct netbuf *addr, taddr;
290 struct netbuf addr_to_delete;
291 struct __rpc_sockinfo si;
292 struct addrinfo hints, *res, *tres;
293 struct address_cache *ad_cache;
296 /* VARIABLES PROTECTED BY rpcbaddr_cache_lock: ad_cache */
298 /* Get the address of the rpcbind. Check cache first */
300 addr_to_delete.len = 0;
301 rwlock_rdlock(&rpcbaddr_cache_lock);
304 ad_cache = check_cache(host, nconf->nc_netid);
305 if (ad_cache != NULL) {
306 addr = ad_cache->ac_taddr;
307 client = clnt_tli_create(RPC_ANYFD, nconf, addr,
308 (rpcprog_t)RPCBPROG, (rpcvers_t)RPCBVERS4, 0, 0);
309 if (client != NULL) {
311 *targaddr = strdup(ad_cache->ac_uaddr);
312 rwlock_unlock(&rpcbaddr_cache_lock);
315 addr_to_delete.len = addr->len;
316 addr_to_delete.buf = (char *)malloc(addr->len);
317 if (addr_to_delete.buf == NULL) {
318 addr_to_delete.len = 0;
320 memcpy(addr_to_delete.buf, addr->buf, addr->len);
323 rwlock_unlock(&rpcbaddr_cache_lock);
324 if (addr_to_delete.len != 0) {
326 * Assume this may be due to cache data being
329 rwlock_wrlock(&rpcbaddr_cache_lock);
330 delete_cache(&addr_to_delete);
331 rwlock_unlock(&rpcbaddr_cache_lock);
332 free(addr_to_delete.buf);
334 if (!__rpc_nconf2sockinfo(nconf, &si)) {
335 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
339 memset(&hints, 0, sizeof hints);
340 hints.ai_family = si.si_af;
341 hints.ai_socktype = si.si_socktype;
342 hints.ai_protocol = si.si_proto;
345 printf("trying netid %s family %d proto %d socktype %d\n",
346 nconf->nc_netid, si.si_af, si.si_proto, si.si_socktype);
349 if (nconf->nc_protofmly != NULL && strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0) {
350 client = local_rpcb();
353 clnt_pcreateerror("rpcbind clnt interface");
357 struct sockaddr_un sun;
359 *targaddr = malloc(sizeof(sun.sun_path));
360 if (*targaddr == NULL) {
361 CLNT_DESTROY(client);
364 strncpy(*targaddr, _PATH_RPCBINDSOCK,
365 sizeof(sun.sun_path));
370 if (getaddrinfo(host, "sunrpc", &hints, &res) != 0) {
371 rpc_createerr.cf_stat = RPC_UNKNOWNHOST;
376 for (tres = res; tres != NULL; tres = tres->ai_next) {
377 taddr.buf = tres->ai_addr;
378 taddr.len = taddr.maxlen = tres->ai_addrlen;
384 ua = taddr2uaddr(nconf, &taddr);
385 fprintf(stderr, "Got it [%s]\n", ua);
394 fprintf(stderr, "\tnetbuf len = %d, maxlen = %d\n",
395 taddr.len, taddr.maxlen);
396 fprintf(stderr, "\tAddress is ");
397 for (i = 0; i < taddr.len; i++)
398 fprintf(stderr, "%u.", ((char *)(taddr.buf))[i]);
399 fprintf(stderr, "\n");
402 client = clnt_tli_create(RPC_ANYFD, nconf, &taddr,
403 (rpcprog_t)RPCBPROG, (rpcvers_t)RPCBVERS4, 0, 0);
406 clnt_pcreateerror("rpcbind clnt interface");
411 tmpaddr = targaddr ? taddr2uaddr(nconf, &taddr) : NULL;
412 add_cache(host, nconf->nc_netid, &taddr, tmpaddr);
424 #define IN4_LOCALHOST_STRING "127.0.0.1"
425 #define IN6_LOCALHOST_STRING "::1"
428 * This routine will return a client handle that is connected to the local
429 * rpcbind. Returns NULL on error and free's everything.
435 static struct netconfig *loopnconf;
436 static char *hostname;
440 struct sockaddr_un sun;
443 * Try connecting to the local rpcbind through a local socket
444 * first. If this doesn't work, try all transports defined in
445 * the netconfig file.
447 memset(&sun, 0, sizeof sun);
448 sock = _socket(AF_LOCAL, SOCK_STREAM, 0);
451 sun.sun_family = AF_LOCAL;
452 strcpy(sun.sun_path, _PATH_RPCBINDSOCK);
453 nbuf.len = sun.sun_len = SUN_LEN(&sun);
454 nbuf.maxlen = sizeof (struct sockaddr_un);
457 tsize = __rpc_get_t_size(AF_LOCAL, 0, 0);
458 client = clnt_vc_create(sock, &nbuf, (rpcprog_t)RPCBPROG,
459 (rpcvers_t)RPCBVERS, tsize, tsize);
461 if (client != NULL) {
462 /* Mark the socket to be closed in destructor */
463 (void) CLNT_CONTROL(client, CLSET_FD_CLOSE, NULL);
467 /* Nobody needs this socket anymore; free the descriptor. */
472 /* VARIABLES PROTECTED BY loopnconf_lock: loopnconf */
473 mutex_lock(&loopnconf_lock);
474 if (loopnconf == NULL) {
475 struct netconfig *nconf, *tmpnconf = NULL;
479 nc_handle = setnetconfig();
480 if (nc_handle == NULL) {
481 /* fails to open netconfig file */
482 syslog (LOG_ERR, "rpc: failed to open " NETCONFIG);
483 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
484 mutex_unlock(&loopnconf_lock);
487 while ((nconf = getnetconfig(nc_handle)) != NULL) {
489 if ((strcmp(nconf->nc_protofmly, NC_INET6) == 0 ||
493 strcmp(nconf->nc_protofmly, NC_INET) == 0) &&
494 (nconf->nc_semantics == NC_TPI_COTS ||
495 nconf->nc_semantics == NC_TPI_COTS_ORD)) {
496 fd = __rpc_nconf2fd(nconf);
498 * Can't create a socket, assume that
499 * this family isn't configured in the kernel.
505 if (!strcmp(nconf->nc_protofmly, NC_INET))
506 hostname = IN4_LOCALHOST_STRING;
508 hostname = IN6_LOCALHOST_STRING;
511 if (tmpnconf == NULL) {
512 endnetconfig(nc_handle);
513 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
514 mutex_unlock(&loopnconf_lock);
517 loopnconf = getnetconfigent(tmpnconf->nc_netid);
518 /* loopnconf is never freed */
519 endnetconfig(nc_handle);
521 mutex_unlock(&loopnconf_lock);
522 client = getclnthandle(hostname, loopnconf, NULL);
527 * Set a mapping between program, version and address.
528 * Calls the rpcbind service to do the mapping.
531 rpcb_set(program, version, nconf, address)
534 const struct netconfig *nconf; /* Network structure of transport */
535 const struct netbuf *address; /* Services netconfig address */
542 /* parameter checking */
544 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
547 if (address == NULL) {
548 rpc_createerr.cf_stat = RPC_UNKNOWNADDR;
551 client = local_rpcb();
556 /* convert to universal */
557 /*LINTED const castaway*/
558 parms.r_addr = taddr2uaddr((struct netconfig *) nconf,
559 (struct netbuf *)address);
561 CLNT_DESTROY(client);
562 rpc_createerr.cf_stat = RPC_N2AXLATEFAILURE;
563 return (FALSE); /* no universal address */
565 parms.r_prog = program;
566 parms.r_vers = version;
567 parms.r_netid = nconf->nc_netid;
569 * Though uid is not being used directly, we still send it for
570 * completeness. For non-unix platforms, perhaps some other
571 * string or an empty string can be sent.
573 (void) snprintf(uidbuf, sizeof uidbuf, "%d", geteuid());
574 parms.r_owner = uidbuf;
576 CLNT_CALL(client, (rpcproc_t)RPCBPROC_SET, (xdrproc_t) xdr_rpcb,
577 (char *)(void *)&parms, (xdrproc_t) xdr_bool,
578 (char *)(void *)&rslt, tottimeout);
580 CLNT_DESTROY(client);
586 * Remove the mapping between program, version and netbuf address.
587 * Calls the rpcbind service to do the un-mapping.
588 * If netbuf is NULL, unset for all the transports, otherwise unset
589 * only for the given transport.
592 rpcb_unset(program, version, nconf)
595 const struct netconfig *nconf;
602 client = local_rpcb();
607 parms.r_prog = program;
608 parms.r_vers = version;
610 parms.r_netid = nconf->nc_netid;
612 /*LINTED const castaway*/
613 parms.r_netid = (char *) &nullstring[0]; /* unsets all */
615 /*LINTED const castaway*/
616 parms.r_addr = (char *) &nullstring[0];
617 (void) snprintf(uidbuf, sizeof uidbuf, "%d", geteuid());
618 parms.r_owner = uidbuf;
620 CLNT_CALL(client, (rpcproc_t)RPCBPROC_UNSET, (xdrproc_t) xdr_rpcb,
621 (char *)(void *)&parms, (xdrproc_t) xdr_bool,
622 (char *)(void *)&rslt, tottimeout);
624 CLNT_DESTROY(client);
629 * From the merged list, find the appropriate entry
631 static struct netbuf *
632 got_entry(relp, nconf)
633 rpcb_entry_list_ptr relp;
634 const struct netconfig *nconf;
636 struct netbuf *na = NULL;
637 rpcb_entry_list_ptr sp;
640 for (sp = relp; sp != NULL; sp = sp->rpcb_entry_next) {
641 rmap = &sp->rpcb_entry_map;
642 if ((strcmp(nconf->nc_proto, rmap->r_nc_proto) == 0) &&
643 (strcmp(nconf->nc_protofmly, rmap->r_nc_protofmly) == 0) &&
644 (nconf->nc_semantics == rmap->r_nc_semantics) &&
645 (rmap->r_maddr != NULL) && (rmap->r_maddr[0] != 0)) {
646 na = uaddr2taddr(nconf, rmap->r_maddr);
648 fprintf(stderr, "\tRemote address is [%s].\n",
652 "\tCouldn't resolve remote address!\n");
661 * Quick check to see if rpcbind is up. Tries to connect over
667 struct netconfig *nconf;
668 struct sockaddr_un sun;
673 localhandle = setnetconfig();
674 while ((nconf = getnetconfig(localhandle)) != NULL) {
675 if (nconf->nc_protofmly != NULL &&
676 strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0)
679 endnetconfig(localhandle);
684 memset(&sun, 0, sizeof sun);
685 sock = _socket(AF_LOCAL, SOCK_STREAM, 0);
688 sun.sun_family = AF_LOCAL;
689 strncpy(sun.sun_path, _PATH_RPCBINDSOCK, sizeof(sun.sun_path));
690 sun.sun_len = SUN_LEN(&sun);
692 if (_connect(sock, (struct sockaddr *)&sun, sun.sun_len) < 0) {
702 * An internal function which optimizes rpcb_getaddr function. It also
703 * returns the client handle that it uses to contact the remote rpcbind.
705 * The algorithm used: If the transports is TCP or UDP, it first tries
706 * version 2 (portmap), 4 and then 3 (svr4). This order should be
707 * changed in the next OS release to 4, 2 and 3. We are assuming that by
708 * that time, version 4 would be available on many machines on the network.
709 * With this algorithm, we get performance as well as a plan for
710 * obsoleting version 2.
712 * For all other transports, the algorithm remains as 4 and then 3.
714 * XXX: Due to some problems with t_connect(), we do not reuse the same client
715 * handle for COTS cases and hence in these cases we do not return the
716 * client handle. This code will change if t_connect() ever
717 * starts working properly. Also look under clnt_vc.c.
720 __rpcb_findaddr_timed(program, version, nconf, host, clpp, tp)
723 const struct netconfig *nconf;
728 static bool_t check_rpcbind = TRUE;
729 CLIENT *client = NULL;
731 enum clnt_stat clnt_st;
734 struct netbuf *address = NULL;
735 rpcvers_t start_vers = RPCBVERS4;
736 struct netbuf servaddr;
738 /* parameter checking */
740 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
747 * Use default total timeout if no timeout is specified.
753 /* Try version 2 for TCP or UDP */
754 if (strcmp(nconf->nc_protofmly, NC_INET) == 0) {
756 struct netbuf remote;
757 rpcvers_t pmapvers = 2;
758 struct pmap pmapparms;
761 * Try UDP only - there are some portmappers out
762 * there that use UDP only.
764 if (strcmp(nconf->nc_proto, NC_TCP) == 0) {
765 struct netconfig *newnconf;
767 if ((newnconf = getnetconfigent("udp")) == NULL) {
768 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
771 client = getclnthandle(host, newnconf, &parms.r_addr);
772 freenetconfigent(newnconf);
774 client = getclnthandle(host, nconf, &parms.r_addr);
780 * Set version and retry timeout.
782 CLNT_CONTROL(client, CLSET_RETRY_TIMEOUT, (char *)&rpcbrmttime);
783 CLNT_CONTROL(client, CLSET_VERS, (char *)&pmapvers);
785 pmapparms.pm_prog = program;
786 pmapparms.pm_vers = version;
787 pmapparms.pm_prot = strcmp(nconf->nc_proto, NC_TCP) ?
788 IPPROTO_UDP : IPPROTO_TCP;
789 pmapparms.pm_port = 0; /* not needed */
790 clnt_st = CLNT_CALL(client, (rpcproc_t)PMAPPROC_GETPORT,
791 (xdrproc_t) xdr_pmap, (caddr_t)(void *)&pmapparms,
792 (xdrproc_t) xdr_u_short, (caddr_t)(void *)&port,
794 if (clnt_st != RPC_SUCCESS) {
795 if ((clnt_st == RPC_PROGVERSMISMATCH) ||
796 (clnt_st == RPC_PROGUNAVAIL))
797 goto try_rpcbind; /* Try different versions */
798 rpc_createerr.cf_stat = RPC_PMAPFAILURE;
799 clnt_geterr(client, &rpc_createerr.cf_error);
801 } else if (port == 0) {
803 rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
807 CLNT_CONTROL(client, CLGET_SVC_ADDR, (char *)&remote);
808 if (((address = (struct netbuf *)
809 malloc(sizeof (struct netbuf))) == NULL) ||
810 ((address->buf = (char *)
811 malloc(remote.len)) == NULL)) {
812 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
813 clnt_geterr(client, &rpc_createerr.cf_error);
818 memcpy(address->buf, remote.buf, remote.len);
819 memcpy(&((char *)address->buf)[sizeof (short)],
820 (char *)(void *)&port, sizeof (short));
821 address->len = address->maxlen = remote.len;
828 * Check if rpcbind is up. This prevents needless delays when
829 * accessing applications such as the keyserver while booting
832 if (check_rpcbind && strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0) {
833 if (!__rpcbind_is_up()) {
834 rpc_createerr.cf_stat = RPC_PMAPFAILURE;
835 rpc_createerr.cf_error.re_errno = 0;
838 check_rpcbind = FALSE;
842 * Now we try version 4 and then 3.
843 * We also send the remote system the address we used to
844 * contact it in case it can help to connect back with us
846 parms.r_prog = program;
847 parms.r_vers = version;
848 /*LINTED const castaway*/
849 parms.r_owner = (char *) &nullstring[0]; /* not needed; */
850 /* just for xdring */
851 parms.r_netid = nconf->nc_netid; /* not really needed */
854 * If a COTS transport is being used, try getting address via CLTS
855 * transport. This works only with version 4.
857 if (nconf->nc_semantics == NC_TPI_COTS_ORD ||
858 nconf->nc_semantics == NC_TPI_COTS) {
861 struct netconfig *nconf_clts;
862 rpcb_entry_list_ptr relp = NULL;
864 if (client == NULL) {
865 /* This did not go through the above PORTMAP/TCP code */
866 if ((handle = __rpc_setconf("datagram_v")) != NULL) {
867 while ((nconf_clts = __rpc_getconf(handle))
869 if (strcmp(nconf_clts->nc_protofmly,
870 nconf->nc_protofmly) != 0) {
873 client = getclnthandle(host, nconf_clts,
877 __rpc_endconf(handle);
880 goto regular_rpcbind; /* Go the regular way */
882 /* This is a UDP PORTMAP handle. Change to version 4 */
884 CLNT_CONTROL(client, CLSET_VERS, (char *)(void *)&vers);
887 * We also send the remote system the address we used to
888 * contact it in case it can help it connect back with us
890 if (parms.r_addr == NULL) {
891 /*LINTED const castaway*/
892 parms.r_addr = (char *) &nullstring[0]; /* for XDRing */
895 CLNT_CONTROL(client, CLSET_RETRY_TIMEOUT, (char *)&rpcbrmttime);
897 clnt_st = CLNT_CALL(client, (rpcproc_t)RPCBPROC_GETADDRLIST,
898 (xdrproc_t) xdr_rpcb, (char *)(void *)&parms,
899 (xdrproc_t) xdr_rpcb_entry_list_ptr,
900 (char *)(void *)&relp, *tp);
901 if (clnt_st == RPC_SUCCESS) {
902 if ((address = got_entry(relp, nconf)) != NULL) {
903 xdr_free((xdrproc_t) xdr_rpcb_entry_list_ptr,
904 (char *)(void *)&relp);
905 CLNT_CONTROL(client, CLGET_SVC_ADDR,
906 (char *)(void *)&servaddr);
907 __rpc_fixup_addr(address, &servaddr);
910 /* Entry not found for this transport */
911 xdr_free((xdrproc_t) xdr_rpcb_entry_list_ptr,
912 (char *)(void *)&relp);
914 * XXX: should have perhaps returned with error but
915 * since the remote machine might not always be able
916 * to send the address on all transports, we try the
917 * regular way with regular_rpcbind
919 goto regular_rpcbind;
920 } else if ((clnt_st == RPC_PROGVERSMISMATCH) ||
921 (clnt_st == RPC_PROGUNAVAIL)) {
922 start_vers = RPCBVERS; /* Try version 3 now */
923 goto regular_rpcbind; /* Try different versions */
925 rpc_createerr.cf_stat = RPC_PMAPFAILURE;
926 clnt_geterr(client, &rpc_createerr.cf_error);
933 /* Now the same transport is to be used to get the address */
934 if (client && ((nconf->nc_semantics == NC_TPI_COTS_ORD) ||
935 (nconf->nc_semantics == NC_TPI_COTS))) {
936 /* A CLTS type of client - destroy it */
937 CLNT_DESTROY(client);
941 if (client == NULL) {
942 client = getclnthandle(host, nconf, &parms.r_addr);
943 if (client == NULL) {
947 if (parms.r_addr == NULL) {
948 /*LINTED const castaway*/
949 parms.r_addr = (char *) &nullstring[0];
952 /* First try from start_vers and then version 3 (RPCBVERS) */
954 CLNT_CONTROL(client, CLSET_RETRY_TIMEOUT, (char *) &rpcbrmttime);
955 for (vers = start_vers; vers >= RPCBVERS; vers--) {
956 /* Set the version */
957 CLNT_CONTROL(client, CLSET_VERS, (char *)(void *)&vers);
958 clnt_st = CLNT_CALL(client, (rpcproc_t)RPCBPROC_GETADDR,
959 (xdrproc_t) xdr_rpcb, (char *)(void *)&parms,
960 (xdrproc_t) xdr_wrapstring, (char *)(void *) &ua, *tp);
961 if (clnt_st == RPC_SUCCESS) {
962 if ((ua == NULL) || (ua[0] == 0)) {
963 /* address unknown */
964 rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
967 address = uaddr2taddr(nconf, ua);
969 fprintf(stderr, "\tRemote address is [%s]\n", ua);
972 "\tCouldn't resolve remote address!\n");
974 xdr_free((xdrproc_t)xdr_wrapstring,
975 (char *)(void *)&ua);
978 /* We don't know about your universal address */
979 rpc_createerr.cf_stat = RPC_N2AXLATEFAILURE;
982 CLNT_CONTROL(client, CLGET_SVC_ADDR,
983 (char *)(void *)&servaddr);
984 __rpc_fixup_addr(address, &servaddr);
986 } else if (clnt_st == RPC_PROGVERSMISMATCH) {
987 struct rpc_err rpcerr;
989 clnt_geterr(client, &rpcerr);
990 if (rpcerr.re_vers.low > RPCBVERS4)
991 goto error; /* a new version, can't handle */
992 } else if (clnt_st != RPC_PROGUNAVAIL) {
993 /* Cant handle this error */
994 rpc_createerr.cf_stat = clnt_st;
995 clnt_geterr(client, &rpc_createerr.cf_error);
1002 CLNT_DESTROY(client);
1006 if (nconf->nc_semantics != NC_TPI_CLTS) {
1007 /* This client is the connectionless one */
1009 CLNT_DESTROY(client);
1015 } else if (client) {
1016 CLNT_DESTROY(client);
1018 if (parms.r_addr != NULL && parms.r_addr != nullstring)
1025 * Find the mapped address for program, version.
1026 * Calls the rpcbind service remotely to do the lookup.
1027 * Uses the transport specified in nconf.
1028 * Returns FALSE (0) if no map exists, else returns 1.
1030 * Assuming that the address is all properly allocated
1033 rpcb_getaddr(program, version, nconf, address, host)
1036 const struct netconfig *nconf;
1037 struct netbuf *address;
1042 if ((na = __rpcb_findaddr_timed(program, version,
1043 (struct netconfig *) nconf, (char *) host,
1044 (CLIENT **) NULL, (struct timeval *) NULL)) == NULL)
1047 if (na->len > address->maxlen) {
1048 /* Too long address */
1051 rpc_createerr.cf_stat = RPC_FAILED;
1054 memcpy(address->buf, na->buf, (size_t)na->len);
1055 address->len = na->len;
1062 * Get a copy of the current maps.
1063 * Calls the rpcbind service remotely to get the maps.
1065 * It returns only a list of the services
1066 * It returns NULL on failure.
1069 rpcb_getmaps(nconf, host)
1070 const struct netconfig *nconf;
1073 rpcblist_ptr head = NULL;
1075 enum clnt_stat clnt_st;
1078 client = getclnthandle(host, nconf, NULL);
1079 if (client == NULL) {
1082 clnt_st = CLNT_CALL(client, (rpcproc_t)RPCBPROC_DUMP,
1083 (xdrproc_t) xdr_void, NULL, (xdrproc_t) xdr_rpcblist_ptr,
1084 (char *)(void *)&head, tottimeout);
1085 if (clnt_st == RPC_SUCCESS)
1088 if ((clnt_st != RPC_PROGVERSMISMATCH) &&
1089 (clnt_st != RPC_PROGUNAVAIL)) {
1090 rpc_createerr.cf_stat = RPC_RPCBFAILURE;
1091 clnt_geterr(client, &rpc_createerr.cf_error);
1095 /* fall back to earlier version */
1096 CLNT_CONTROL(client, CLGET_VERS, (char *)(void *)&vers);
1097 if (vers == RPCBVERS4) {
1099 CLNT_CONTROL(client, CLSET_VERS, (char *)(void *)&vers);
1100 if (CLNT_CALL(client, (rpcproc_t)RPCBPROC_DUMP,
1101 (xdrproc_t) xdr_void, NULL, (xdrproc_t) xdr_rpcblist_ptr,
1102 (char *)(void *)&head, tottimeout) == RPC_SUCCESS)
1105 rpc_createerr.cf_stat = RPC_RPCBFAILURE;
1106 clnt_geterr(client, &rpc_createerr.cf_error);
1109 CLNT_DESTROY(client);
1114 * rpcbinder remote-call-service interface.
1115 * This routine is used to call the rpcbind remote call service
1116 * which will look up a service program in the address maps, and then
1117 * remotely call that routine with the given parameters. This allows
1118 * programs to do a lookup and call in one step.
1121 rpcb_rmtcall(nconf, host, prog, vers, proc, xdrargs, argsp,
1122 xdrres, resp, tout, addr_ptr)
1123 const struct netconfig *nconf; /* Netconfig structure */
1124 const char *host; /* Remote host name */
1127 rpcproc_t proc; /* Remote proc identifiers */
1128 xdrproc_t xdrargs, xdrres; /* XDR routines */
1129 caddr_t argsp, resp; /* Argument and Result */
1130 struct timeval tout; /* Timeout value for this call */
1131 const struct netbuf *addr_ptr; /* Preallocated netbuf address */
1134 enum clnt_stat stat;
1135 struct r_rpcb_rmtcallargs a;
1136 struct r_rpcb_rmtcallres r;
1137 rpcvers_t rpcb_vers;
1140 client = getclnthandle(host, nconf, NULL);
1141 if (client == NULL) {
1142 return (RPC_FAILED);
1144 /*LINTED const castaway*/
1145 CLNT_CONTROL(client, CLSET_RETRY_TIMEOUT, (char *)(void *)&rmttimeout);
1149 a.args.args_val = argsp;
1150 a.xdr_args = xdrargs;
1152 r.results.results_val = resp;
1155 for (rpcb_vers = RPCBVERS4; rpcb_vers >= RPCBVERS; rpcb_vers--) {
1156 CLNT_CONTROL(client, CLSET_VERS, (char *)(void *)&rpcb_vers);
1157 stat = CLNT_CALL(client, (rpcproc_t)RPCBPROC_CALLIT,
1158 (xdrproc_t) xdr_rpcb_rmtcallargs, (char *)(void *)&a,
1159 (xdrproc_t) xdr_rpcb_rmtcallres, (char *)(void *)&r, tout);
1160 if ((stat == RPC_SUCCESS) && (addr_ptr != NULL)) {
1162 /*LINTED const castaway*/
1163 na = uaddr2taddr((struct netconfig *) nconf, r.addr);
1165 stat = RPC_N2AXLATEFAILURE;
1166 /*LINTED const castaway*/
1167 ((struct netbuf *) addr_ptr)->len = 0;
1170 if (na->len > addr_ptr->maxlen) {
1171 /* Too long address */
1172 stat = RPC_FAILED; /* XXX A better error no */
1175 /*LINTED const castaway*/
1176 ((struct netbuf *) addr_ptr)->len = 0;
1179 memcpy(addr_ptr->buf, na->buf, (size_t)na->len);
1180 /*LINTED const castaway*/
1181 ((struct netbuf *)addr_ptr)->len = na->len;
1185 } else if ((stat != RPC_PROGVERSMISMATCH) &&
1186 (stat != RPC_PROGUNAVAIL)) {
1191 CLNT_DESTROY(client);
1193 xdr_free((xdrproc_t) xdr_wrapstring, (char *)(void *)&r.addr);
1198 * Gets the time on the remote host.
1199 * Returns 1 if succeeds else 0.
1202 rpcb_gettime(host, timep)
1206 CLIENT *client = NULL;
1208 struct netconfig *nconf;
1213 if ((host == NULL) || (host[0] == 0)) {
1218 if ((handle = __rpc_setconf("netpath")) == NULL) {
1219 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
1222 rpc_createerr.cf_stat = RPC_SUCCESS;
1223 while (client == NULL) {
1224 if ((nconf = __rpc_getconf(handle)) == NULL) {
1225 if (rpc_createerr.cf_stat == RPC_SUCCESS)
1226 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
1229 client = getclnthandle(host, nconf, NULL);
1233 __rpc_endconf(handle);
1234 if (client == (CLIENT *) NULL) {
1238 st = CLNT_CALL(client, (rpcproc_t)RPCBPROC_GETTIME,
1239 (xdrproc_t) xdr_void, NULL,
1240 (xdrproc_t) xdr_int, (char *)(void *)timep, tottimeout);
1242 if ((st == RPC_PROGVERSMISMATCH) || (st == RPC_PROGUNAVAIL)) {
1243 CLNT_CONTROL(client, CLGET_VERS, (char *)(void *)&vers);
1244 if (vers == RPCBVERS4) {
1245 /* fall back to earlier version */
1247 CLNT_CONTROL(client, CLSET_VERS, (char *)(void *)&vers);
1248 st = CLNT_CALL(client, (rpcproc_t)RPCBPROC_GETTIME,
1249 (xdrproc_t) xdr_void, NULL,
1250 (xdrproc_t) xdr_int, (char *)(void *)timep,
1254 CLNT_DESTROY(client);
1255 return (st == RPC_SUCCESS? TRUE: FALSE);
1259 * Converts taddr to universal address. This routine should never
1260 * really be called because local n2a libraries are always provided.
1263 rpcb_taddr2uaddr(nconf, taddr)
1264 struct netconfig *nconf;
1265 struct netbuf *taddr;
1271 /* parameter checking */
1272 if (nconf == NULL) {
1273 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
1276 if (taddr == NULL) {
1277 rpc_createerr.cf_stat = RPC_UNKNOWNADDR;
1280 client = local_rpcb();
1285 CLNT_CALL(client, (rpcproc_t)RPCBPROC_TADDR2UADDR,
1286 (xdrproc_t) xdr_netbuf, (char *)(void *)taddr,
1287 (xdrproc_t) xdr_wrapstring, (char *)(void *)&uaddr, tottimeout);
1288 CLNT_DESTROY(client);
1293 * Converts universal address to netbuf. This routine should never
1294 * really be called because local n2a libraries are always provided.
1297 rpcb_uaddr2taddr(nconf, uaddr)
1298 struct netconfig *nconf;
1302 struct netbuf *taddr;
1305 /* parameter checking */
1306 if (nconf == NULL) {
1307 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
1310 if (uaddr == NULL) {
1311 rpc_createerr.cf_stat = RPC_UNKNOWNADDR;
1314 client = local_rpcb();
1319 taddr = (struct netbuf *)calloc(1, sizeof (struct netbuf));
1320 if (taddr == NULL) {
1321 CLNT_DESTROY(client);
1324 if (CLNT_CALL(client, (rpcproc_t)RPCBPROC_UADDR2TADDR,
1325 (xdrproc_t) xdr_wrapstring, (char *)(void *)&uaddr,
1326 (xdrproc_t) xdr_netbuf, (char *)(void *)taddr,
1327 tottimeout) != RPC_SUCCESS) {
1331 CLNT_DESTROY(client);