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 "namespace.h"
69 #include "reentrant.h"
70 #include <sys/types.h>
71 #include <sys/socket.h>
73 #include <sys/utsname.h>
75 #include <rpc/rpcb_prot.h>
76 #include <rpc/nettype.h>
77 #include <netconfig.h>
79 #include <netinet/in.h> /* FOR IPPROTO_TCP/UDP definitions */
80 #include <rpc/pmap_prot.h>
89 #include "un-namespace.h"
94 static struct timeval tottimeout = { 60, 0 };
95 static const struct timeval rmttimeout = { 3, 0 };
96 static struct timeval rpcbrmttime = { 15, 0 };
98 extern bool_t xdr_wrapstring(XDR *, char **);
100 static const char nullstring[] = "\000";
104 struct address_cache {
108 struct netbuf *ac_taddr;
109 struct address_cache *ac_next;
112 static struct address_cache *front;
113 static int cachesize;
115 #define CLCR_GET_RPCB_TIMEOUT 1
116 #define CLCR_SET_RPCB_TIMEOUT 2
119 extern int __rpc_lowvers;
121 static struct address_cache *check_cache(const char *, const char *);
122 static void delete_cache(struct netbuf *);
123 static void add_cache(const char *, const char *, struct netbuf *, char *);
124 static CLIENT *getclnthandle(const char *, const struct netconfig *, char **);
125 static CLIENT *local_rpcb(void);
126 static struct netbuf *got_entry(rpcb_entry_list_ptr, const struct netconfig *);
129 * This routine adjusts the timeout used for calls to the remote rpcbind.
130 * Also, this routine can be used to set the use of portmapper version 2
131 * only when doing rpc_broadcasts
132 * These are private routines that may not be provided in future releases.
135 __rpc_control(request, info)
140 case CLCR_GET_RPCB_TIMEOUT:
141 *(struct timeval *)info = tottimeout;
143 case CLCR_SET_RPCB_TIMEOUT:
144 tottimeout = *(struct timeval *)info;
146 case CLCR_SET_LOWVERS:
147 __rpc_lowvers = *(int *)info;
149 case CLCR_GET_LOWVERS:
150 *(int *)info = __rpc_lowvers;
159 * It might seem that a reader/writer lock would be more reasonable here.
160 * However because getclnthandle(), the only user of the cache functions,
161 * may do a delete_cache() operation if a check_cache() fails to return an
162 * address useful to clnt_tli_create(), we may as well use a mutex.
165 * As it turns out, if the cache lock is *not* a reader/writer lock, we will
166 * block all clnt_create's if we are trying to connect to a host that's down,
167 * since the lock will be held all during that time.
171 * The routines check_cache(), add_cache(), delete_cache() manage the
172 * cache of rpcbind addresses for (host, netid).
175 static struct address_cache *
176 check_cache(host, netid)
177 const char *host, *netid;
179 struct address_cache *cptr;
181 /* READ LOCK HELD ON ENTRY: rpcbaddr_cache_lock */
183 for (cptr = front; cptr != NULL; cptr = cptr->ac_next) {
184 if (!strcmp(cptr->ac_host, host) &&
185 !strcmp(cptr->ac_netid, netid)) {
187 fprintf(stderr, "Found cache entry for %s: %s\n",
193 return ((struct address_cache *) NULL);
200 struct address_cache *cptr, *prevptr = NULL;
202 /* WRITE LOCK HELD ON ENTRY: rpcbaddr_cache_lock */
203 for (cptr = front; cptr != NULL; cptr = cptr->ac_next) {
204 if (!memcmp(cptr->ac_taddr->buf, addr->buf, addr->len)) {
206 free(cptr->ac_netid);
207 free(cptr->ac_taddr->buf);
208 free(cptr->ac_taddr);
210 free(cptr->ac_uaddr);
212 prevptr->ac_next = cptr->ac_next;
214 front = cptr->ac_next;
224 add_cache(host, netid, taddr, uaddr)
225 const char *host, *netid;
227 struct netbuf *taddr;
229 struct address_cache *ad_cache, *cptr, *prevptr;
231 ad_cache = (struct address_cache *)
232 malloc(sizeof (struct address_cache));
236 ad_cache->ac_host = strdup(host);
237 ad_cache->ac_netid = strdup(netid);
238 ad_cache->ac_uaddr = uaddr ? strdup(uaddr) : NULL;
239 ad_cache->ac_taddr = (struct netbuf *)malloc(sizeof (struct netbuf));
240 if (!ad_cache->ac_host || !ad_cache->ac_netid || !ad_cache->ac_taddr ||
241 (uaddr && !ad_cache->ac_uaddr)) {
244 ad_cache->ac_taddr->len = ad_cache->ac_taddr->maxlen = taddr->len;
245 ad_cache->ac_taddr->buf = (char *) malloc(taddr->len);
246 if (ad_cache->ac_taddr->buf == NULL) {
249 memcpy(ad_cache->ac_taddr->buf, taddr->buf, taddr->len);
251 fprintf(stderr, "Added to cache: %s : %s\n", host, netid);
254 /* VARIABLES PROTECTED BY rpcbaddr_cache_lock: cptr */
256 rwlock_wrlock(&rpcbaddr_cache_lock);
257 if (cachesize < CACHESIZE) {
258 ad_cache->ac_next = front;
262 /* Free the last entry */
265 while (cptr->ac_next) {
267 cptr = cptr->ac_next;
271 fprintf(stderr, "Deleted from cache: %s : %s\n",
272 cptr->ac_host, cptr->ac_netid);
275 free(cptr->ac_netid);
276 free(cptr->ac_taddr->buf);
277 free(cptr->ac_taddr);
279 free(cptr->ac_uaddr);
282 prevptr->ac_next = NULL;
283 ad_cache->ac_next = front;
287 ad_cache->ac_next = NULL;
291 rwlock_unlock(&rpcbaddr_cache_lock);
295 * This routine will return a client handle that is connected to the
296 * rpcbind. If targaddr is non-NULL, the "universal address" of the
297 * host will be stored in *targaddr; the caller is responsible for
298 * freeing this string.
299 * On error, returns NULL and free's everything.
302 getclnthandle(host, nconf, targaddr)
304 const struct netconfig *nconf;
308 struct netbuf *addr, taddr;
309 struct netbuf addr_to_delete;
310 struct __rpc_sockinfo si;
311 struct addrinfo hints, *res, *tres;
312 struct address_cache *ad_cache;
315 /* VARIABLES PROTECTED BY rpcbaddr_cache_lock: ad_cache */
317 /* Get the address of the rpcbind. Check cache first */
319 addr_to_delete.len = 0;
320 rwlock_rdlock(&rpcbaddr_cache_lock);
323 ad_cache = check_cache(host, nconf->nc_netid);
324 if (ad_cache != NULL) {
325 addr = ad_cache->ac_taddr;
326 client = clnt_tli_create(RPC_ANYFD, nconf, addr,
327 (rpcprog_t)RPCBPROG, (rpcvers_t)RPCBVERS4, 0, 0);
328 if (client != NULL) {
330 *targaddr = strdup(ad_cache->ac_uaddr);
331 rwlock_unlock(&rpcbaddr_cache_lock);
334 addr_to_delete.len = addr->len;
335 addr_to_delete.buf = (char *)malloc(addr->len);
336 if (addr_to_delete.buf == NULL) {
337 addr_to_delete.len = 0;
339 memcpy(addr_to_delete.buf, addr->buf, addr->len);
342 rwlock_unlock(&rpcbaddr_cache_lock);
343 if (addr_to_delete.len != 0) {
345 * Assume this may be due to cache data being
348 rwlock_wrlock(&rpcbaddr_cache_lock);
349 delete_cache(&addr_to_delete);
350 rwlock_unlock(&rpcbaddr_cache_lock);
351 free(addr_to_delete.buf);
353 if (!__rpc_nconf2sockinfo(nconf, &si)) {
354 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
358 memset(&hints, 0, sizeof hints);
359 hints.ai_family = si.si_af;
360 hints.ai_socktype = si.si_socktype;
361 hints.ai_protocol = si.si_proto;
364 printf("trying netid %s family %d proto %d socktype %d\n",
365 nconf->nc_netid, si.si_af, si.si_proto, si.si_socktype);
368 if (nconf->nc_protofmly != NULL && strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0) {
369 client = local_rpcb();
372 clnt_pcreateerror("rpcbind clnt interface");
376 struct sockaddr_un sun;
378 *targaddr = malloc(sizeof(sun.sun_path));
379 strncpy(*targaddr, _PATH_RPCBINDSOCK,
380 sizeof(sun.sun_path));
384 if (getaddrinfo(host, "sunrpc", &hints, &res) != 0) {
385 rpc_createerr.cf_stat = RPC_UNKNOWNHOST;
390 for (tres = res; tres != NULL; tres = tres->ai_next) {
391 taddr.buf = tres->ai_addr;
392 taddr.len = taddr.maxlen = tres->ai_addrlen;
398 ua = taddr2uaddr(nconf, &taddr);
399 fprintf(stderr, "Got it [%s]\n", ua);
408 fprintf(stderr, "\tnetbuf len = %d, maxlen = %d\n",
409 taddr.len, taddr.maxlen);
410 fprintf(stderr, "\tAddress is ");
411 for (i = 0; i < taddr.len; i++)
412 fprintf(stderr, "%u.", ((char *)(taddr.buf))[i]);
413 fprintf(stderr, "\n");
416 client = clnt_tli_create(RPC_ANYFD, nconf, &taddr,
417 (rpcprog_t)RPCBPROG, (rpcvers_t)RPCBVERS4, 0, 0);
420 clnt_pcreateerror("rpcbind clnt interface");
425 tmpaddr = targaddr ? taddr2uaddr(nconf, &taddr) : NULL;
426 add_cache(host, nconf->nc_netid, &taddr, tmpaddr);
438 #define IN4_LOCALHOST_STRING "127.0.0.1"
439 #define IN6_LOCALHOST_STRING "::1"
442 * This routine will return a client handle that is connected to the local
443 * rpcbind. Returns NULL on error and free's everything.
449 static struct netconfig *loopnconf;
450 static char *hostname;
454 struct sockaddr_un sun;
457 * Try connecting to the local rpcbind through a local socket
458 * first. If this doesn't work, try all transports defined in
459 * the netconfig file.
461 memset(&sun, 0, sizeof sun);
462 sock = _socket(AF_LOCAL, SOCK_STREAM, 0);
465 sun.sun_family = AF_LOCAL;
466 strcpy(sun.sun_path, _PATH_RPCBINDSOCK);
467 nbuf.len = sun.sun_len = SUN_LEN(&sun);
468 nbuf.maxlen = sizeof (struct sockaddr_un);
471 tsize = __rpc_get_t_size(AF_LOCAL, 0, 0);
472 client = clnt_vc_create(sock, &nbuf, (rpcprog_t)RPCBPROG,
473 (rpcvers_t)RPCBVERS, tsize, tsize);
475 if (client != NULL) {
476 /* Mark the socket to be closed in destructor */
477 (void) CLNT_CONTROL(client, CLSET_FD_CLOSE, NULL);
481 /* Nobody needs this socket anymore; free the descriptor. */
486 /* VARIABLES PROTECTED BY loopnconf_lock: loopnconf */
487 mutex_lock(&loopnconf_lock);
488 if (loopnconf == NULL) {
489 struct netconfig *nconf, *tmpnconf = NULL;
493 nc_handle = setnetconfig();
494 if (nc_handle == NULL) {
495 /* fails to open netconfig file */
496 syslog (LOG_ERR, "rpc: failed to open " NETCONFIG);
497 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
498 mutex_unlock(&loopnconf_lock);
501 while ((nconf = getnetconfig(nc_handle)) != NULL) {
503 if ((strcmp(nconf->nc_protofmly, NC_INET6) == 0 ||
507 strcmp(nconf->nc_protofmly, NC_INET) == 0) &&
508 (nconf->nc_semantics == NC_TPI_COTS ||
509 nconf->nc_semantics == NC_TPI_COTS_ORD)) {
510 fd = __rpc_nconf2fd(nconf);
512 * Can't create a socket, assume that
513 * this family isn't configured in the kernel.
519 if (!strcmp(nconf->nc_protofmly, NC_INET))
520 hostname = IN4_LOCALHOST_STRING;
522 hostname = IN6_LOCALHOST_STRING;
525 if (tmpnconf == NULL) {
526 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
527 mutex_unlock(&loopnconf_lock);
530 loopnconf = getnetconfigent(tmpnconf->nc_netid);
531 /* loopnconf is never freed */
532 endnetconfig(nc_handle);
534 mutex_unlock(&loopnconf_lock);
535 client = getclnthandle(hostname, loopnconf, NULL);
540 * Set a mapping between program, version and address.
541 * Calls the rpcbind service to do the mapping.
544 rpcb_set(program, version, nconf, address)
547 const struct netconfig *nconf; /* Network structure of transport */
548 const struct netbuf *address; /* Services netconfig address */
555 /* parameter checking */
557 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
560 if (address == NULL) {
561 rpc_createerr.cf_stat = RPC_UNKNOWNADDR;
564 client = local_rpcb();
569 /* convert to universal */
570 /*LINTED const castaway*/
571 parms.r_addr = taddr2uaddr((struct netconfig *) nconf,
572 (struct netbuf *)address);
574 CLNT_DESTROY(client);
575 rpc_createerr.cf_stat = RPC_N2AXLATEFAILURE;
576 return (FALSE); /* no universal address */
578 parms.r_prog = program;
579 parms.r_vers = version;
580 parms.r_netid = nconf->nc_netid;
582 * Though uid is not being used directly, we still send it for
583 * completeness. For non-unix platforms, perhaps some other
584 * string or an empty string can be sent.
586 (void) snprintf(uidbuf, sizeof uidbuf, "%d", geteuid());
587 parms.r_owner = uidbuf;
589 CLNT_CALL(client, (rpcproc_t)RPCBPROC_SET, (xdrproc_t) xdr_rpcb,
590 (char *)(void *)&parms, (xdrproc_t) xdr_bool,
591 (char *)(void *)&rslt, tottimeout);
593 CLNT_DESTROY(client);
599 * Remove the mapping between program, version and netbuf address.
600 * Calls the rpcbind service to do the un-mapping.
601 * If netbuf is NULL, unset for all the transports, otherwise unset
602 * only for the given transport.
605 rpcb_unset(program, version, nconf)
608 const struct netconfig *nconf;
615 client = local_rpcb();
620 parms.r_prog = program;
621 parms.r_vers = version;
623 parms.r_netid = nconf->nc_netid;
625 /*LINTED const castaway*/
626 parms.r_netid = (char *) &nullstring[0]; /* unsets all */
628 /*LINTED const castaway*/
629 parms.r_addr = (char *) &nullstring[0];
630 (void) snprintf(uidbuf, sizeof uidbuf, "%d", geteuid());
631 parms.r_owner = uidbuf;
633 CLNT_CALL(client, (rpcproc_t)RPCBPROC_UNSET, (xdrproc_t) xdr_rpcb,
634 (char *)(void *)&parms, (xdrproc_t) xdr_bool,
635 (char *)(void *)&rslt, tottimeout);
637 CLNT_DESTROY(client);
642 * From the merged list, find the appropriate entry
644 static struct netbuf *
645 got_entry(relp, nconf)
646 rpcb_entry_list_ptr relp;
647 const struct netconfig *nconf;
649 struct netbuf *na = NULL;
650 rpcb_entry_list_ptr sp;
653 for (sp = relp; sp != NULL; sp = sp->rpcb_entry_next) {
654 rmap = &sp->rpcb_entry_map;
655 if ((strcmp(nconf->nc_proto, rmap->r_nc_proto) == 0) &&
656 (strcmp(nconf->nc_protofmly, rmap->r_nc_protofmly) == 0) &&
657 (nconf->nc_semantics == rmap->r_nc_semantics) &&
658 (rmap->r_maddr != NULL) && (rmap->r_maddr[0] != 0)) {
659 na = uaddr2taddr(nconf, rmap->r_maddr);
661 fprintf(stderr, "\tRemote address is [%s].\n",
665 "\tCouldn't resolve remote address!\n");
674 * Quick check to see if rpcbind is up. Tries to connect over
680 struct netconfig *nconf;
681 struct sockaddr_un sun;
686 localhandle = setnetconfig();
687 while ((nconf = getnetconfig(localhandle)) != NULL) {
688 if (nconf->nc_protofmly != NULL &&
689 strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0)
695 endnetconfig(localhandle);
697 memset(&sun, 0, sizeof sun);
698 sock = _socket(AF_LOCAL, SOCK_STREAM, 0);
701 sun.sun_family = AF_LOCAL;
702 strncpy(sun.sun_path, _PATH_RPCBINDSOCK, sizeof(sun.sun_path));
703 sun.sun_len = SUN_LEN(&sun);
705 if (_connect(sock, (struct sockaddr *)&sun, sun.sun_len) < 0) {
715 * An internal function which optimizes rpcb_getaddr function. It also
716 * returns the client handle that it uses to contact the remote rpcbind.
718 * The algorithm used: If the transports is TCP or UDP, it first tries
719 * version 2 (portmap), 4 and then 3 (svr4). This order should be
720 * changed in the next OS release to 4, 2 and 3. We are assuming that by
721 * that time, version 4 would be available on many machines on the network.
722 * With this algorithm, we get performance as well as a plan for
723 * obsoleting version 2.
725 * For all other transports, the algorithm remains as 4 and then 3.
727 * XXX: Due to some problems with t_connect(), we do not reuse the same client
728 * handle for COTS cases and hence in these cases we do not return the
729 * client handle. This code will change if t_connect() ever
730 * starts working properly. Also look under clnt_vc.c.
733 __rpcb_findaddr_timed(program, version, nconf, host, clpp, tp)
736 const struct netconfig *nconf;
741 static bool_t check_rpcbind = TRUE;
742 CLIENT *client = NULL;
744 enum clnt_stat clnt_st;
747 struct netbuf *address = NULL;
748 rpcvers_t start_vers = RPCBVERS4;
749 struct netbuf servaddr;
751 /* parameter checking */
753 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
760 * Use default total timeout if no timeout is specified.
766 /* Try version 2 for TCP or UDP */
767 if (strcmp(nconf->nc_protofmly, NC_INET) == 0) {
769 struct netbuf remote;
770 rpcvers_t pmapvers = 2;
771 struct pmap pmapparms;
774 * Try UDP only - there are some portmappers out
775 * there that use UDP only.
777 if (strcmp(nconf->nc_proto, NC_TCP) == 0) {
778 struct netconfig *newnconf;
780 if ((newnconf = getnetconfigent("udp")) == NULL) {
781 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
784 client = getclnthandle(host, newnconf, &parms.r_addr);
785 freenetconfigent(newnconf);
787 client = getclnthandle(host, nconf, &parms.r_addr);
793 * Set version and retry timeout.
795 CLNT_CONTROL(client, CLSET_RETRY_TIMEOUT, (char *)&rpcbrmttime);
796 CLNT_CONTROL(client, CLSET_VERS, (char *)&pmapvers);
798 pmapparms.pm_prog = program;
799 pmapparms.pm_vers = version;
800 pmapparms.pm_prot = strcmp(nconf->nc_proto, NC_TCP) ?
801 IPPROTO_UDP : IPPROTO_TCP;
802 pmapparms.pm_port = 0; /* not needed */
803 clnt_st = CLNT_CALL(client, (rpcproc_t)PMAPPROC_GETPORT,
804 (xdrproc_t) xdr_pmap, (caddr_t)(void *)&pmapparms,
805 (xdrproc_t) xdr_u_short, (caddr_t)(void *)&port,
807 if (clnt_st != RPC_SUCCESS) {
808 if ((clnt_st == RPC_PROGVERSMISMATCH) ||
809 (clnt_st == RPC_PROGUNAVAIL))
810 goto try_rpcbind; /* Try different versions */
811 rpc_createerr.cf_stat = RPC_PMAPFAILURE;
812 clnt_geterr(client, &rpc_createerr.cf_error);
814 } else if (port == 0) {
816 rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
820 CLNT_CONTROL(client, CLGET_SVC_ADDR, (char *)&remote);
821 if (((address = (struct netbuf *)
822 malloc(sizeof (struct netbuf))) == NULL) ||
823 ((address->buf = (char *)
824 malloc(remote.len)) == NULL)) {
825 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
826 clnt_geterr(client, &rpc_createerr.cf_error);
833 memcpy(address->buf, remote.buf, remote.len);
834 memcpy(&((char *)address->buf)[sizeof (short)],
835 (char *)(void *)&port, sizeof (short));
836 address->len = address->maxlen = remote.len;
843 * Check if rpcbind is up. This prevents needless delays when
844 * accessing applications such as the keyserver while booting
847 if (check_rpcbind && strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0) {
848 if (!__rpcbind_is_up()) {
849 rpc_createerr.cf_stat = RPC_PMAPFAILURE;
850 rpc_createerr.cf_error.re_errno = 0;
853 check_rpcbind = FALSE;
857 * Now we try version 4 and then 3.
858 * We also send the remote system the address we used to
859 * contact it in case it can help to connect back with us
861 parms.r_prog = program;
862 parms.r_vers = version;
863 /*LINTED const castaway*/
864 parms.r_owner = (char *) &nullstring[0]; /* not needed; */
865 /* just for xdring */
866 parms.r_netid = nconf->nc_netid; /* not really needed */
869 * If a COTS transport is being used, try getting address via CLTS
870 * transport. This works only with version 4.
872 if (nconf->nc_semantics == NC_TPI_COTS_ORD ||
873 nconf->nc_semantics == NC_TPI_COTS) {
876 struct netconfig *nconf_clts;
877 rpcb_entry_list_ptr relp = NULL;
879 if (client == NULL) {
880 /* This did not go through the above PORTMAP/TCP code */
881 if ((handle = __rpc_setconf("datagram_v")) != NULL) {
882 while ((nconf_clts = __rpc_getconf(handle))
884 if (strcmp(nconf_clts->nc_protofmly,
885 nconf->nc_protofmly) != 0) {
888 client = getclnthandle(host, nconf_clts,
892 __rpc_endconf(handle);
895 goto regular_rpcbind; /* Go the regular way */
897 /* This is a UDP PORTMAP handle. Change to version 4 */
899 CLNT_CONTROL(client, CLSET_VERS, (char *)(void *)&vers);
902 * We also send the remote system the address we used to
903 * contact it in case it can help it connect back with us
905 if (parms.r_addr == NULL) {
906 /*LINTED const castaway*/
907 parms.r_addr = (char *) &nullstring[0]; /* for XDRing */
910 CLNT_CONTROL(client, CLSET_RETRY_TIMEOUT, (char *)&rpcbrmttime);
912 clnt_st = CLNT_CALL(client, (rpcproc_t)RPCBPROC_GETADDRLIST,
913 (xdrproc_t) xdr_rpcb, (char *)(void *)&parms,
914 (xdrproc_t) xdr_rpcb_entry_list_ptr,
915 (char *)(void *)&relp, *tp);
916 if (clnt_st == RPC_SUCCESS) {
917 if ((address = got_entry(relp, nconf)) != NULL) {
918 xdr_free((xdrproc_t) xdr_rpcb_entry_list_ptr,
919 (char *)(void *)&relp);
920 CLNT_CONTROL(client, CLGET_SVC_ADDR,
921 (char *)(void *)&servaddr);
922 __rpc_fixup_addr(address, &servaddr);
925 /* Entry not found for this transport */
926 xdr_free((xdrproc_t) xdr_rpcb_entry_list_ptr,
927 (char *)(void *)&relp);
929 * XXX: should have perhaps returned with error but
930 * since the remote machine might not always be able
931 * to send the address on all transports, we try the
932 * regular way with regular_rpcbind
934 goto regular_rpcbind;
935 } else if ((clnt_st == RPC_PROGVERSMISMATCH) ||
936 (clnt_st == RPC_PROGUNAVAIL)) {
937 start_vers = RPCBVERS; /* Try version 3 now */
938 goto regular_rpcbind; /* Try different versions */
940 rpc_createerr.cf_stat = RPC_PMAPFAILURE;
941 clnt_geterr(client, &rpc_createerr.cf_error);
948 /* Now the same transport is to be used to get the address */
949 if (client && ((nconf->nc_semantics == NC_TPI_COTS_ORD) ||
950 (nconf->nc_semantics == NC_TPI_COTS))) {
951 /* A CLTS type of client - destroy it */
952 CLNT_DESTROY(client);
956 if (client == NULL) {
957 client = getclnthandle(host, nconf, &parms.r_addr);
958 if (client == NULL) {
962 if (parms.r_addr == NULL) {
963 /*LINTED const castaway*/
964 parms.r_addr = (char *) &nullstring[0];
967 /* First try from start_vers and then version 3 (RPCBVERS) */
969 CLNT_CONTROL(client, CLSET_RETRY_TIMEOUT, (char *) &rpcbrmttime);
970 for (vers = start_vers; vers >= RPCBVERS; vers--) {
971 /* Set the version */
972 CLNT_CONTROL(client, CLSET_VERS, (char *)(void *)&vers);
973 clnt_st = CLNT_CALL(client, (rpcproc_t)RPCBPROC_GETADDR,
974 (xdrproc_t) xdr_rpcb, (char *)(void *)&parms,
975 (xdrproc_t) xdr_wrapstring, (char *)(void *) &ua, *tp);
976 if (clnt_st == RPC_SUCCESS) {
977 if ((ua == NULL) || (ua[0] == 0)) {
978 /* address unknown */
979 rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
982 address = uaddr2taddr(nconf, ua);
984 fprintf(stderr, "\tRemote address is [%s]\n", ua);
987 "\tCouldn't resolve remote address!\n");
989 xdr_free((xdrproc_t)xdr_wrapstring,
990 (char *)(void *)&ua);
993 /* We don't know about your universal address */
994 rpc_createerr.cf_stat = RPC_N2AXLATEFAILURE;
997 CLNT_CONTROL(client, CLGET_SVC_ADDR,
998 (char *)(void *)&servaddr);
999 __rpc_fixup_addr(address, &servaddr);
1001 } else if (clnt_st == RPC_PROGVERSMISMATCH) {
1002 struct rpc_err rpcerr;
1004 clnt_geterr(client, &rpcerr);
1005 if (rpcerr.re_vers.low > RPCBVERS4)
1006 goto error; /* a new version, can't handle */
1007 } else if (clnt_st != RPC_PROGUNAVAIL) {
1008 /* Cant handle this error */
1009 rpc_createerr.cf_stat = clnt_st;
1010 clnt_geterr(client, &rpc_createerr.cf_error);
1015 if ((address == NULL) || (address->len == 0)) {
1016 rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
1017 clnt_geterr(client, &rpc_createerr.cf_error);
1022 CLNT_DESTROY(client);
1026 if (nconf->nc_semantics != NC_TPI_CLTS) {
1027 /* This client is the connectionless one */
1029 CLNT_DESTROY(client);
1035 } else if (client) {
1036 CLNT_DESTROY(client);
1038 if (parms.r_addr != NULL && parms.r_addr != nullstring)
1045 * Find the mapped address for program, version.
1046 * Calls the rpcbind service remotely to do the lookup.
1047 * Uses the transport specified in nconf.
1048 * Returns FALSE (0) if no map exists, else returns 1.
1050 * Assuming that the address is all properly allocated
1053 rpcb_getaddr(program, version, nconf, address, host)
1056 const struct netconfig *nconf;
1057 struct netbuf *address;
1062 if ((na = __rpcb_findaddr_timed(program, version,
1063 (struct netconfig *) nconf, (char *) host,
1064 (CLIENT **) NULL, (struct timeval *) NULL)) == NULL)
1067 if (na->len > address->maxlen) {
1068 /* Too long address */
1071 rpc_createerr.cf_stat = RPC_FAILED;
1074 memcpy(address->buf, na->buf, (size_t)na->len);
1075 address->len = na->len;
1082 * Get a copy of the current maps.
1083 * Calls the rpcbind service remotely to get the maps.
1085 * It returns only a list of the services
1086 * It returns NULL on failure.
1089 rpcb_getmaps(nconf, host)
1090 const struct netconfig *nconf;
1093 rpcblist_ptr head = NULL;
1095 enum clnt_stat clnt_st;
1098 client = getclnthandle(host, nconf, NULL);
1099 if (client == NULL) {
1102 clnt_st = CLNT_CALL(client, (rpcproc_t)RPCBPROC_DUMP,
1103 (xdrproc_t) xdr_void, NULL, (xdrproc_t) xdr_rpcblist_ptr,
1104 (char *)(void *)&head, tottimeout);
1105 if (clnt_st == RPC_SUCCESS)
1108 if ((clnt_st != RPC_PROGVERSMISMATCH) &&
1109 (clnt_st != RPC_PROGUNAVAIL)) {
1110 rpc_createerr.cf_stat = RPC_RPCBFAILURE;
1111 clnt_geterr(client, &rpc_createerr.cf_error);
1115 /* fall back to earlier version */
1116 CLNT_CONTROL(client, CLGET_VERS, (char *)(void *)&vers);
1117 if (vers == RPCBVERS4) {
1119 CLNT_CONTROL(client, CLSET_VERS, (char *)(void *)&vers);
1120 if (CLNT_CALL(client, (rpcproc_t)RPCBPROC_DUMP,
1121 (xdrproc_t) xdr_void, NULL, (xdrproc_t) xdr_rpcblist_ptr,
1122 (char *)(void *)&head, tottimeout) == RPC_SUCCESS)
1125 rpc_createerr.cf_stat = RPC_RPCBFAILURE;
1126 clnt_geterr(client, &rpc_createerr.cf_error);
1129 CLNT_DESTROY(client);
1134 * rpcbinder remote-call-service interface.
1135 * This routine is used to call the rpcbind remote call service
1136 * which will look up a service program in the address maps, and then
1137 * remotely call that routine with the given parameters. This allows
1138 * programs to do a lookup and call in one step.
1141 rpcb_rmtcall(nconf, host, prog, vers, proc, xdrargs, argsp,
1142 xdrres, resp, tout, addr_ptr)
1143 const struct netconfig *nconf; /* Netconfig structure */
1144 const char *host; /* Remote host name */
1147 rpcproc_t proc; /* Remote proc identifiers */
1148 xdrproc_t xdrargs, xdrres; /* XDR routines */
1149 caddr_t argsp, resp; /* Argument and Result */
1150 struct timeval tout; /* Timeout value for this call */
1151 const struct netbuf *addr_ptr; /* Preallocated netbuf address */
1154 enum clnt_stat stat;
1155 struct r_rpcb_rmtcallargs a;
1156 struct r_rpcb_rmtcallres r;
1157 rpcvers_t rpcb_vers;
1160 client = getclnthandle(host, nconf, NULL);
1161 if (client == NULL) {
1162 return (RPC_FAILED);
1164 /*LINTED const castaway*/
1165 CLNT_CONTROL(client, CLSET_RETRY_TIMEOUT, (char *)(void *)&rmttimeout);
1169 a.args.args_val = argsp;
1170 a.xdr_args = xdrargs;
1172 r.results.results_val = resp;
1175 for (rpcb_vers = RPCBVERS4; rpcb_vers >= RPCBVERS; rpcb_vers--) {
1176 CLNT_CONTROL(client, CLSET_VERS, (char *)(void *)&rpcb_vers);
1177 stat = CLNT_CALL(client, (rpcproc_t)RPCBPROC_CALLIT,
1178 (xdrproc_t) xdr_rpcb_rmtcallargs, (char *)(void *)&a,
1179 (xdrproc_t) xdr_rpcb_rmtcallres, (char *)(void *)&r, tout);
1180 if ((stat == RPC_SUCCESS) && (addr_ptr != NULL)) {
1182 /*LINTED const castaway*/
1183 na = uaddr2taddr((struct netconfig *) nconf, r.addr);
1185 stat = RPC_N2AXLATEFAILURE;
1186 /*LINTED const castaway*/
1187 ((struct netbuf *) addr_ptr)->len = 0;
1190 if (na->len > addr_ptr->maxlen) {
1191 /* Too long address */
1192 stat = RPC_FAILED; /* XXX A better error no */
1195 /*LINTED const castaway*/
1196 ((struct netbuf *) addr_ptr)->len = 0;
1199 memcpy(addr_ptr->buf, na->buf, (size_t)na->len);
1200 /*LINTED const castaway*/
1201 ((struct netbuf *)addr_ptr)->len = na->len;
1205 } else if ((stat != RPC_PROGVERSMISMATCH) &&
1206 (stat != RPC_PROGUNAVAIL)) {
1211 CLNT_DESTROY(client);
1213 xdr_free((xdrproc_t) xdr_wrapstring, (char *)(void *)&r.addr);
1218 * Gets the time on the remote host.
1219 * Returns 1 if succeeds else 0.
1222 rpcb_gettime(host, timep)
1226 CLIENT *client = NULL;
1228 struct netconfig *nconf;
1233 if ((host == NULL) || (host[0] == 0)) {
1238 if ((handle = __rpc_setconf("netpath")) == NULL) {
1239 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
1242 rpc_createerr.cf_stat = RPC_SUCCESS;
1243 while (client == NULL) {
1244 if ((nconf = __rpc_getconf(handle)) == NULL) {
1245 if (rpc_createerr.cf_stat == RPC_SUCCESS)
1246 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
1249 client = getclnthandle(host, nconf, NULL);
1253 __rpc_endconf(handle);
1254 if (client == (CLIENT *) NULL) {
1258 st = CLNT_CALL(client, (rpcproc_t)RPCBPROC_GETTIME,
1259 (xdrproc_t) xdr_void, NULL,
1260 (xdrproc_t) xdr_int, (char *)(void *)timep, tottimeout);
1262 if ((st == RPC_PROGVERSMISMATCH) || (st == RPC_PROGUNAVAIL)) {
1263 CLNT_CONTROL(client, CLGET_VERS, (char *)(void *)&vers);
1264 if (vers == RPCBVERS4) {
1265 /* fall back to earlier version */
1267 CLNT_CONTROL(client, CLSET_VERS, (char *)(void *)&vers);
1268 st = CLNT_CALL(client, (rpcproc_t)RPCBPROC_GETTIME,
1269 (xdrproc_t) xdr_void, NULL,
1270 (xdrproc_t) xdr_int, (char *)(void *)timep,
1274 CLNT_DESTROY(client);
1275 return (st == RPC_SUCCESS? TRUE: FALSE);
1279 * Converts taddr to universal address. This routine should never
1280 * really be called because local n2a libraries are always provided.
1283 rpcb_taddr2uaddr(nconf, taddr)
1284 struct netconfig *nconf;
1285 struct netbuf *taddr;
1291 /* parameter checking */
1292 if (nconf == NULL) {
1293 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
1296 if (taddr == NULL) {
1297 rpc_createerr.cf_stat = RPC_UNKNOWNADDR;
1300 client = local_rpcb();
1305 CLNT_CALL(client, (rpcproc_t)RPCBPROC_TADDR2UADDR,
1306 (xdrproc_t) xdr_netbuf, (char *)(void *)taddr,
1307 (xdrproc_t) xdr_wrapstring, (char *)(void *)&uaddr, tottimeout);
1308 CLNT_DESTROY(client);
1313 * Converts universal address to netbuf. This routine should never
1314 * really be called because local n2a libraries are always provided.
1317 rpcb_uaddr2taddr(nconf, uaddr)
1318 struct netconfig *nconf;
1322 struct netbuf *taddr;
1325 /* parameter checking */
1326 if (nconf == NULL) {
1327 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
1330 if (uaddr == NULL) {
1331 rpc_createerr.cf_stat = RPC_UNKNOWNADDR;
1334 client = local_rpcb();
1339 taddr = (struct netbuf *)calloc(1, sizeof (struct netbuf));
1340 if (taddr == NULL) {
1341 CLNT_DESTROY(client);
1344 if (CLNT_CALL(client, (rpcproc_t)RPCBPROC_UADDR2TADDR,
1345 (xdrproc_t) xdr_wrapstring, (char *)(void *)&uaddr,
1346 (xdrproc_t) xdr_netbuf, (char *)(void *)taddr,
1347 tottimeout) != RPC_SUCCESS) {
1351 CLNT_DESTROY(client);