tools/llvm: Do not build with symbols
[minix3.git] / lib / libc / rpc / rpcb_clnt.c
blobd5aa9f1239b01a32369ccdc743be4d27e3a740fc
1 /* $NetBSD: rpcb_clnt.c,v 1.29 2013/03/11 20:19:29 tron Exp $ */
3 /*
4 * Copyright (c) 2010, Oracle America, Inc.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials
15 * provided with the distribution.
16 * * Neither the name of the "Oracle America, Inc." nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 * Copyright (c) 1986-1991 by Sun Microsystems Inc.
37 /* #ident "@(#)rpcb_clnt.c 1.27 94/04/24 SMI" */
39 #include <sys/cdefs.h>
40 #if defined(LIBC_SCCS) && !defined(lint)
41 #if 0
42 static char sccsid[] = "@(#)rpcb_clnt.c 1.30 89/06/21 Copyr 1988 Sun Micro";
43 #else
44 __RCSID("$NetBSD: rpcb_clnt.c,v 1.29 2013/03/11 20:19:29 tron Exp $");
45 #endif
46 #endif
49 * rpcb_clnt.c
50 * interface to rpcbind rpc service.
52 * Copyright (C) 1988, Sun Microsystems, Inc.
55 #include "namespace.h"
56 #include "reentrant.h"
57 #include <sys/types.h>
58 #include <sys/socket.h>
59 #include <sys/un.h>
60 #include <sys/utsname.h>
61 #include <rpc/rpc.h>
62 #include <rpc/rpcb_prot.h>
63 #include <rpc/nettype.h>
64 #include <netconfig.h>
65 #ifdef PORTMAP
66 #include <netinet/in.h> /* FOR IPPROTO_TCP/UDP definitions */
67 #include <rpc/pmap_prot.h>
68 #endif
69 #include <assert.h>
70 #include <errno.h>
71 #include <netdb.h>
72 #include <stdio.h>
73 #include <stdlib.h>
74 #include <string.h>
75 #include <syslog.h>
76 #include <unistd.h>
78 #include "svc_fdset.h"
79 #include "rpc_internal.h"
81 #ifdef __weak_alias
82 __weak_alias(rpcb_set,_rpcb_set)
83 __weak_alias(rpcb_unset,_rpcb_unset)
84 __weak_alias(rpcb_getmaps,_rpcb_getmaps)
85 __weak_alias(rpcb_taddr2uaddr,_rpcb_taddr2uaddr)
86 __weak_alias(rpcb_uaddr2taddr,_rpcb_uaddr2taddr)
87 #endif
89 static struct timeval tottimeout = { 60, 0 };
90 static const struct timeval rmttimeout = { 3, 0 };
92 static const char nullstring[] = "\000";
94 #define CACHESIZE 6
96 struct address_cache {
97 char *ac_host;
98 char *ac_netid;
99 char *ac_uaddr;
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.
126 bool_t
127 __rpc_control(int request, void *info)
130 _DIAGASSERT(info != NULL);
132 switch (request) {
133 case CLCR_GET_RPCB_TIMEOUT:
134 *(struct timeval *)info = tottimeout;
135 break;
136 case CLCR_SET_RPCB_TIMEOUT:
137 tottimeout = *(struct timeval *)info;
138 break;
139 case CLCR_SET_LOWVERS:
140 __rpc_lowvers = *(int *)info;
141 break;
142 case CLCR_GET_LOWVERS:
143 *(int *)info = __rpc_lowvers;
144 break;
145 default:
146 return (FALSE);
148 return (TRUE);
152 * It might seem that a reader/writer lock would be more reasonable here.
153 * However because getclnthandle(), the only user of the cache functions,
154 * may do a delete_cache() operation if a check_cache() fails to return an
155 * address useful to clnt_tli_create(), we may as well use a mutex.
158 * As it turns out, if the cache lock is *not* a reader/writer lock, we will
159 * block all clnt_create's if we are trying to connect to a host that's down,
160 * since the lock will be held all during that time.
162 #ifdef _REENTRANT
163 extern rwlock_t rpcbaddr_cache_lock;
164 #endif
167 * The routines check_cache(), add_cache(), delete_cache() manage the
168 * cache of rpcbind addresses for (host, netid).
171 static struct address_cache *
172 check_cache(const char *host, const char *netid)
174 struct address_cache *cptr;
176 _DIAGASSERT(host != NULL);
177 _DIAGASSERT(netid != NULL);
179 /* READ LOCK HELD ON ENTRY: rpcbaddr_cache_lock */
181 for (cptr = front; cptr != NULL; cptr = cptr->ac_next) {
182 if (!strcmp(cptr->ac_host, host) &&
183 !strcmp(cptr->ac_netid, netid)) {
184 #ifdef ND_DEBUG
185 fprintf(stderr, "Found cache entry for %s: %s\n",
186 host, netid);
187 #endif
188 return (cptr);
191 return NULL;
194 static void
195 delete_cache(struct netbuf *addr)
197 struct address_cache *cptr, *prevptr = NULL;
199 _DIAGASSERT(addr != NULL);
201 /* WRITE LOCK HELD ON ENTRY: rpcbaddr_cache_lock */
202 for (cptr = front; cptr != NULL; cptr = cptr->ac_next) {
203 if (!memcmp(cptr->ac_taddr->buf, addr->buf, addr->len)) {
204 free(cptr->ac_host);
205 free(cptr->ac_netid);
206 free(cptr->ac_taddr->buf);
207 free(cptr->ac_taddr);
208 if (cptr->ac_uaddr)
209 free(cptr->ac_uaddr);
210 if (prevptr)
211 prevptr->ac_next = cptr->ac_next;
212 else
213 front = cptr->ac_next;
214 free(cptr);
215 cachesize--;
216 break;
218 prevptr = cptr;
222 static void
223 add_cache(const char *host, const char *netid, struct netbuf *taddr,
224 char *uaddr)
226 struct address_cache *ad_cache, *cptr, *prevptr;
228 _DIAGASSERT(host != NULL);
229 _DIAGASSERT(netid != NULL);
230 /* uaddr may be NULL */
231 /* taddr may be NULL ??? */
233 ad_cache = malloc(sizeof(*ad_cache));
234 if (!ad_cache) {
235 return;
237 ad_cache->ac_host = strdup(host);
238 ad_cache->ac_netid = strdup(netid);
239 ad_cache->ac_uaddr = uaddr ? strdup(uaddr) : NULL;
240 ad_cache->ac_taddr = malloc(sizeof(*ad_cache->ac_taddr));
241 if (!ad_cache->ac_host || !ad_cache->ac_netid || !ad_cache->ac_taddr ||
242 (uaddr && !ad_cache->ac_uaddr)) {
243 goto out;
245 ad_cache->ac_taddr->len = ad_cache->ac_taddr->maxlen = taddr->len;
246 ad_cache->ac_taddr->buf = malloc(taddr->len);
247 if (ad_cache->ac_taddr->buf == NULL) {
248 out:
249 if (ad_cache->ac_host)
250 free(ad_cache->ac_host);
251 if (ad_cache->ac_netid)
252 free(ad_cache->ac_netid);
253 if (ad_cache->ac_uaddr)
254 free(ad_cache->ac_uaddr);
255 if (ad_cache->ac_taddr)
256 free(ad_cache->ac_taddr);
257 free(ad_cache);
258 return;
260 memcpy(ad_cache->ac_taddr->buf, taddr->buf, taddr->len);
261 #ifdef ND_DEBUG
262 fprintf(stderr, "Added to cache: %s : %s\n", host, netid);
263 #endif
265 /* VARIABLES PROTECTED BY rpcbaddr_cache_lock: cptr */
267 rwlock_wrlock(&rpcbaddr_cache_lock);
268 if (cachesize < CACHESIZE) {
269 ad_cache->ac_next = front;
270 front = ad_cache;
271 cachesize++;
272 } else {
273 /* Free the last entry */
274 cptr = front;
275 prevptr = NULL;
276 while (cptr->ac_next) {
277 prevptr = cptr;
278 cptr = cptr->ac_next;
281 #ifdef ND_DEBUG
282 fprintf(stderr, "Deleted from cache: %s : %s\n",
283 cptr->ac_host, cptr->ac_netid);
284 #endif
285 free(cptr->ac_host);
286 free(cptr->ac_netid);
287 free(cptr->ac_taddr->buf);
288 free(cptr->ac_taddr);
289 if (cptr->ac_uaddr)
290 free(cptr->ac_uaddr);
292 if (prevptr) {
293 prevptr->ac_next = NULL;
294 ad_cache->ac_next = front;
295 front = ad_cache;
296 } else {
297 front = ad_cache;
298 ad_cache->ac_next = NULL;
300 free(cptr);
302 rwlock_unlock(&rpcbaddr_cache_lock);
306 * This routine will return a client handle that is connected to the
307 * rpcbind. Returns NULL on error and free's everything.
309 static CLIENT *
310 getclnthandle(const char *host, const struct netconfig *nconf, char **targaddr)
312 CLIENT *client;
313 struct netbuf *addr, taddr;
314 struct netbuf addr_to_delete;
315 struct __rpc_sockinfo si;
316 struct addrinfo hints, *res, *tres;
317 struct address_cache *ad_cache;
318 char *tmpaddr;
320 _DIAGASSERT(host != NULL);
321 _DIAGASSERT(nconf != NULL);
322 /* targaddr may be NULL */
324 /* VARIABLES PROTECTED BY rpcbaddr_cache_lock: ad_cache */
326 /* Get the address of the rpcbind. Check cache first */
327 client = NULL;
328 addr_to_delete.len = 0;
329 addr_to_delete.buf = NULL;
330 rwlock_rdlock(&rpcbaddr_cache_lock);
331 ad_cache = check_cache(host, nconf->nc_netid);
332 if (ad_cache != NULL) {
333 addr = ad_cache->ac_taddr;
334 client = clnt_tli_create(RPC_ANYFD, nconf, addr,
335 (rpcprog_t)RPCBPROG, (rpcvers_t)RPCBVERS4, 0, 0);
336 if (client != NULL) {
337 if (targaddr)
338 *targaddr = ad_cache->ac_uaddr;
339 rwlock_unlock(&rpcbaddr_cache_lock);
340 return (client);
342 addr_to_delete.len = addr->len;
343 addr_to_delete.buf = malloc(addr->len);
344 if (addr_to_delete.buf == NULL) {
345 addr_to_delete.len = 0;
346 } else {
347 memcpy(addr_to_delete.buf, addr->buf, addr->len);
350 rwlock_unlock(&rpcbaddr_cache_lock);
351 if (addr_to_delete.len != 0) {
353 * Assume this may be due to cache data being
354 * outdated
356 rwlock_wrlock(&rpcbaddr_cache_lock);
357 delete_cache(&addr_to_delete);
358 rwlock_unlock(&rpcbaddr_cache_lock);
359 free(addr_to_delete.buf);
361 if (!__rpc_nconf2sockinfo(nconf, &si)) {
362 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
363 return NULL;
366 memset(&hints, 0, sizeof hints);
367 hints.ai_family = si.si_af;
368 hints.ai_socktype = si.si_socktype;
369 hints.ai_protocol = si.si_proto;
371 #ifdef CLNT_DEBUG
372 printf("trying netid %s family %d proto %d socktype %d\n",
373 nconf->nc_netid, si.si_af, si.si_proto, si.si_socktype);
374 #endif
376 if (getaddrinfo(host, "sunrpc", &hints, &res) != 0) {
377 rpc_createerr.cf_stat = RPC_UNKNOWNHOST;
378 return NULL;
381 for (tres = res; tres != NULL; tres = tres->ai_next) {
382 taddr.buf = tres->ai_addr;
383 taddr.len = taddr.maxlen = tres->ai_addrlen;
385 #ifdef ND_DEBUG
387 char *ua;
389 ua = taddr2uaddr(nconf, &taddr);
390 fprintf(stderr, "Got it [%s]\n", ua);
391 free(ua);
393 #endif
395 #ifdef ND_DEBUG
397 int i;
399 fprintf(stderr, "\tnetbuf len = %d, maxlen = %d\n",
400 taddr.len, taddr.maxlen);
401 fprintf(stderr, "\tAddress is ");
402 for (i = 0; i < taddr.len; i++)
403 fprintf(stderr, "%u.", ((char *)(taddr.buf))[i]);
404 fprintf(stderr, "\n");
406 #endif
407 client = clnt_tli_create(RPC_ANYFD, nconf, &taddr,
408 (rpcprog_t)RPCBPROG, (rpcvers_t)RPCBVERS4, 0, 0);
409 #ifdef ND_DEBUG
410 if (! client) {
411 clnt_pcreateerror("rpcbind clnt interface");
413 #endif
415 if (client) {
416 tmpaddr = targaddr ? taddr2uaddr(nconf, &taddr) : NULL;
417 add_cache(host, nconf->nc_netid, &taddr, tmpaddr);
418 if (targaddr)
419 *targaddr = tmpaddr;
420 break;
423 freeaddrinfo(res);
424 return (client);
427 /* XXX */
428 #define IN4_LOCALHOST_STRING "127.0.0.1"
429 #define IN6_LOCALHOST_STRING "::1"
432 * This routine will return a client handle that is connected to the local
433 * rpcbind. Returns NULL on error and free's everything.
435 static CLIENT *
436 local_rpcb(void)
438 CLIENT *client;
439 static struct netconfig *loopnconf;
440 static const char *hostname;
441 #ifdef _REENTRANT
442 extern mutex_t loopnconf_lock;
443 #endif
444 int sock;
445 size_t tsize;
446 struct netbuf nbuf;
447 struct sockaddr_un sun;
450 * Try connecting to the local rpcbind through a local socket
451 * first. If this doesn't work, try all transports defined in
452 * the netconfig file.
454 memset(&sun, 0, sizeof sun);
455 sock = socket(AF_LOCAL, SOCK_STREAM, 0);
456 if (sock < 0)
457 goto try_nconf;
458 sun.sun_family = AF_LOCAL;
459 strcpy(sun.sun_path, _PATH_RPCBINDSOCK);
460 tsize = SUN_LEN(&sun);
461 _DIAGASSERT(__type_fit(uint8_t, tsize));
462 nbuf.len = sun.sun_len = (uint8_t)tsize;
463 nbuf.maxlen = sizeof (struct sockaddr_un);
464 nbuf.buf = &sun;
466 tsize = __rpc_get_t_size(AF_LOCAL, 0, 0);
467 _DIAGASSERT(__type_fit(u_int, tsize));
468 client = clnt_vc_create(sock, &nbuf, (rpcprog_t)RPCBPROG,
469 (rpcvers_t)RPCBVERS, (u_int)tsize, (u_int)tsize);
471 if (client != NULL) {
472 /* XXX - mark the socket to be closed in destructor */
473 (void) CLNT_CONTROL(client, CLSET_FD_CLOSE, NULL);
474 return client;
477 /* XXX - nobody needs this socket anymore, free the descriptor */
478 close(sock);
480 try_nconf:
482 /* VARIABLES PROTECTED BY loopnconf_lock: loopnconf */
483 mutex_lock(&loopnconf_lock);
484 if (loopnconf == NULL) {
485 struct netconfig *nconf, *tmpnconf = NULL;
486 void *nc_handle;
487 int fd;
489 nc_handle = setnetconfig();
490 if (nc_handle == NULL) {
491 /* fails to open netconfig file */
492 syslog (LOG_ERR, "rpc: failed to open " NETCONFIG);
493 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
494 mutex_unlock(&loopnconf_lock);
495 return (NULL);
497 while ((nconf = getnetconfig(nc_handle)) != NULL) {
498 #ifdef INET6
499 if ((strcmp(nconf->nc_protofmly, NC_INET6) == 0 ||
500 #else
501 if ((
502 #endif
503 strcmp(nconf->nc_protofmly, NC_INET) == 0) &&
504 (nconf->nc_semantics == NC_TPI_COTS ||
505 nconf->nc_semantics == NC_TPI_COTS_ORD)) {
506 fd = __rpc_nconf2fd(nconf);
508 * Can't create a socket, assume that
509 * this family isn't configured in the kernel.
511 if (fd < 0)
512 continue;
513 close(fd);
514 tmpnconf = nconf;
515 if (!strcmp(nconf->nc_protofmly, NC_INET))
516 hostname = IN4_LOCALHOST_STRING;
517 else
518 hostname = IN6_LOCALHOST_STRING;
521 if (tmpnconf == NULL) {
522 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
523 mutex_unlock(&loopnconf_lock);
524 return (NULL);
526 loopnconf = getnetconfigent(tmpnconf->nc_netid);
527 /* loopnconf is never freed */
528 endnetconfig(nc_handle);
530 mutex_unlock(&loopnconf_lock);
531 client = getclnthandle(hostname, loopnconf, NULL);
532 return (client);
536 * Set a mapping between program, version and address.
537 * Calls the rpcbind service to do the mapping.
539 bool_t
540 rpcb_set(rpcprog_t program, rpcvers_t version,
541 const struct netconfig *nconf, /* Network structure of transport */
542 const struct netbuf *address) /* Services netconfig address */
544 CLIENT *client;
545 bool_t rslt = FALSE;
546 RPCB parms;
547 char uidbuf[32];
549 /* parameter checking */
550 if (nconf == NULL) {
551 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
552 return (FALSE);
554 if (address == NULL) {
555 rpc_createerr.cf_stat = RPC_UNKNOWNADDR;
556 return (FALSE);
558 client = local_rpcb();
559 if (! client) {
560 return (FALSE);
563 /* convert to universal */
564 parms.r_addr = taddr2uaddr(__UNCONST(nconf), __UNCONST(address));
565 if (!parms.r_addr) {
566 CLNT_DESTROY(client);
567 rpc_createerr.cf_stat = RPC_N2AXLATEFAILURE;
568 return (FALSE); /* no universal address */
570 parms.r_prog = program;
571 parms.r_vers = version;
572 parms.r_netid = nconf->nc_netid;
574 * Though uid is not being used directly, we still send it for
575 * completeness. For non-unix platforms, perhaps some other
576 * string or an empty string can be sent.
578 (void) snprintf(uidbuf, sizeof uidbuf, "%d", geteuid());
579 parms.r_owner = uidbuf;
581 CLNT_CALL(client, (rpcproc_t)RPCBPROC_SET, (xdrproc_t) xdr_rpcb,
582 (char *)(void *)&parms, (xdrproc_t) xdr_bool,
583 (char *)(void *)&rslt, tottimeout);
585 CLNT_DESTROY(client);
586 free(parms.r_addr);
587 return (rslt);
591 * Remove the mapping between program, version and netbuf address.
592 * Calls the rpcbind service to do the un-mapping.
593 * If netbuf is NULL, unset for all the transports, otherwise unset
594 * only for the given transport.
596 bool_t
597 rpcb_unset(rpcprog_t program, rpcvers_t version, const struct netconfig *nconf)
599 CLIENT *client;
600 bool_t rslt = FALSE;
601 RPCB parms;
602 char uidbuf[32];
604 client = local_rpcb();
605 if (! client) {
606 return (FALSE);
609 parms.r_prog = program;
610 parms.r_vers = version;
611 if (nconf)
612 parms.r_netid = nconf->nc_netid;
613 else {
614 parms.r_netid = __UNCONST(&nullstring[0]); /* unsets all */
616 parms.r_addr = __UNCONST(&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);
625 return (rslt);
629 * From the merged list, find the appropriate entry
631 static struct netbuf *
632 got_entry(rpcb_entry_list_ptr relp, const struct netconfig *nconf)
634 struct netbuf *na = NULL;
635 rpcb_entry_list_ptr sp;
636 rpcb_entry *rmap;
638 _DIAGASSERT(nconf != NULL);
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);
647 #ifdef ND_DEBUG
648 fprintf(stderr, "\tRemote address is [%s].\n",
649 rmap->r_maddr);
650 if (!na)
651 fprintf(stderr,
652 "\tCouldn't resolve remote address!\n");
653 #endif
654 break;
657 return (na);
661 * An internal function which optimizes rpcb_getaddr function. It also
662 * returns the client handle that it uses to contact the remote rpcbind.
664 * The algorithm used: If the transports is TCP or UDP, it first tries
665 * version 2 (portmap), 4 and then 3 (svr4). This order should be
666 * changed in the next OS release to 4, 2 and 3. We are assuming that by
667 * that time, version 4 would be available on many machines on the network.
668 * With this algorithm, we get performance as well as a plan for
669 * obsoleting version 2.
671 * For all other transports, the algorithm remains as 4 and then 3.
673 * XXX: Due to some problems with t_connect(), we do not reuse the same client
674 * handle for COTS cases and hence in these cases we do not return the
675 * client handle. This code will change if t_connect() ever
676 * starts working properly. Also look under clnt_vc.c.
678 struct netbuf *
679 __rpcb_findaddr(rpcprog_t program, rpcvers_t version,
680 const struct netconfig *nconf, const char *host, CLIENT **clpp)
682 CLIENT *client = NULL;
683 RPCB parms;
684 enum clnt_stat clnt_st;
685 char *ua = NULL;
686 rpcvers_t vers;
687 struct netbuf *address = NULL;
688 rpcvers_t start_vers = RPCBVERS4;
689 struct netbuf servaddr;
691 /* nconf is handled below */
692 _DIAGASSERT(host != NULL);
693 /* clpp may be NULL */
695 /* parameter checking */
696 if (nconf == NULL) {
697 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
698 return (NULL);
701 parms.r_addr = NULL;
703 #ifdef PORTMAP
704 /* Try version 2 for TCP or UDP */
705 if (strcmp(nconf->nc_protofmly, NC_INET) == 0) {
706 u_short port = 0;
707 struct netbuf remote;
708 rpcvers_t pmapvers = 2;
709 struct pmap pmapparms;
712 * Try UDP only - there are some portmappers out
713 * there that use UDP only.
715 if (strcmp(nconf->nc_proto, NC_TCP) == 0) {
716 struct netconfig *newnconf;
718 if ((newnconf = getnetconfigent("udp")) == NULL) {
719 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
720 return (NULL);
722 client = getclnthandle(host, newnconf, &parms.r_addr);
723 freenetconfigent(newnconf);
724 } else {
725 client = getclnthandle(host, nconf, &parms.r_addr);
727 if (client == NULL) {
728 return (NULL);
731 /* Set the version */
732 CLNT_CONTROL(client, CLSET_VERS, (char *)(void *)&pmapvers);
733 pmapparms.pm_prog = program;
734 pmapparms.pm_vers = version;
735 pmapparms.pm_prot = strcmp(nconf->nc_proto, NC_TCP) ?
736 IPPROTO_UDP : IPPROTO_TCP;
737 pmapparms.pm_port = 0; /* not needed */
738 clnt_st = CLNT_CALL(client, (rpcproc_t)PMAPPROC_GETPORT,
739 (xdrproc_t) xdr_pmap, (caddr_t)(void *)&pmapparms,
740 (xdrproc_t) xdr_u_short, (caddr_t)(void *)&port,
741 tottimeout);
742 if (clnt_st != RPC_SUCCESS) {
743 if ((clnt_st == RPC_PROGVERSMISMATCH) ||
744 (clnt_st == RPC_PROGUNAVAIL))
745 goto try_rpcbind; /* Try different versions */
746 rpc_createerr.cf_stat = RPC_PMAPFAILURE;
747 clnt_geterr(client, &rpc_createerr.cf_error);
748 goto error;
749 } else if (port == 0) {
750 address = NULL;
751 rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
752 goto error;
754 port = htons(port);
755 CLNT_CONTROL(client, CLGET_SVC_ADDR, (char *)(void *)&remote);
756 if (((address = malloc(sizeof(struct netbuf))) == NULL) ||
757 ((address->buf = malloc(remote.len)) == NULL)) {
758 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
759 clnt_geterr(client, &rpc_createerr.cf_error);
760 if (address) {
761 free(address);
762 address = NULL;
764 goto error;
766 memcpy(address->buf, remote.buf, remote.len);
767 memcpy(&((char *)address->buf)[sizeof (short)],
768 (char *)(void *)&port, sizeof (short));
769 address->len = address->maxlen = remote.len;
770 goto done;
772 #endif
774 try_rpcbind:
776 * Now we try version 4 and then 3.
777 * We also send the remote system the address we used to
778 * contact it in case it can help to connect back with us
780 parms.r_prog = program;
781 parms.r_vers = version;
782 parms.r_owner = __UNCONST(&nullstring[0]); /* not needed; */
783 /* just for xdring */
784 parms.r_netid = nconf->nc_netid; /* not really needed */
787 * If a COTS transport is being used, try getting address via CLTS
788 * transport. This works only with version 4.
789 * NOTE: This is being done for all transports EXCEPT LOOPBACK
790 * because with loopback the cost to go to a COTS is same as
791 * the cost to go through CLTS, plus you get the advantage of
792 * finding out immediately if the local rpcbind process is dead.
794 #if 1
795 if ((nconf->nc_semantics == NC_TPI_COTS_ORD ||
796 nconf->nc_semantics == NC_TPI_COTS) &&
797 (strcmp(nconf->nc_protofmly, NC_LOOPBACK) != 0))
798 #else
799 if (client != NULL) {
800 CLNT_DESTROY(client);
801 client = NULL;
803 if (nconf->nc_semantics == NC_TPI_CLTS)
804 #endif
806 void *handle;
807 struct netconfig *nconf_clts;
808 rpcb_entry_list_ptr relp = NULL;
810 if (client == NULL) {
811 /* This did not go through the above PORTMAP/TCP code */
812 #if 1
813 if ((handle = __rpc_setconf("datagram_v")) != NULL)
814 #else
815 if ((handle = __rpc_setconf("circuit_v")) != NULL)
816 #endif
818 while ((nconf_clts = __rpc_getconf(handle))
819 != NULL) {
820 if (strcmp(nconf_clts->nc_protofmly,
821 nconf->nc_protofmly) != 0) {
822 continue;
824 client = getclnthandle(host, nconf_clts,
825 &parms.r_addr);
826 break;
828 __rpc_endconf(handle);
830 if (client == NULL)
831 goto regular_rpcbind; /* Go the regular way */
832 } else {
833 /* This is a UDP PORTMAP handle. Change to version 4 */
834 vers = RPCBVERS4;
835 CLNT_CONTROL(client, CLSET_VERS, (char *)(void *)&vers);
838 * We also send the remote system the address we used to
839 * contact it in case it can help it connect back with us
841 if (parms.r_addr == NULL) {
842 /* for XDRing */
843 parms.r_addr = __UNCONST(&nullstring[0]);
845 clnt_st = CLNT_CALL(client, (rpcproc_t)RPCBPROC_GETADDRLIST,
846 (xdrproc_t) xdr_rpcb, (char *)(void *)&parms,
847 (xdrproc_t) xdr_rpcb_entry_list_ptr,
848 (char *)(void *)&relp, tottimeout);
849 if (clnt_st == RPC_SUCCESS) {
850 if ((address = got_entry(relp, nconf)) != NULL) {
851 xdr_free((xdrproc_t) xdr_rpcb_entry_list_ptr,
852 (char *)(void *)&relp);
853 CLNT_CONTROL(client, CLGET_SVC_ADDR,
854 (char *)(void *)&servaddr);
855 __rpc_fixup_addr(address, &servaddr);
856 goto done;
858 /* Entry not found for this transport */
859 xdr_free((xdrproc_t) xdr_rpcb_entry_list_ptr,
860 (char *)(void *)&relp);
862 * XXX: should have perhaps returned with error but
863 * since the remote machine might not always be able
864 * to send the address on all transports, we try the
865 * regular way with regular_rpcbind
867 goto regular_rpcbind;
868 } else if ((clnt_st == RPC_PROGVERSMISMATCH) ||
869 (clnt_st == RPC_PROGUNAVAIL)) {
870 start_vers = RPCBVERS; /* Try version 3 now */
871 goto regular_rpcbind; /* Try different versions */
872 } else {
873 rpc_createerr.cf_stat = RPC_PMAPFAILURE;
874 clnt_geterr(client, &rpc_createerr.cf_error);
875 goto error;
879 regular_rpcbind:
881 /* Now the same transport is to be used to get the address */
882 #if 1
883 if (client && ((nconf->nc_semantics == NC_TPI_COTS_ORD) ||
884 (nconf->nc_semantics == NC_TPI_COTS)))
885 #else
886 if (client && nconf->nc_semantics == NC_TPI_CLTS)
887 #endif
889 /* A CLTS type of client - destroy it */
890 CLNT_DESTROY(client);
891 client = NULL;
894 if (client == NULL) {
895 client = getclnthandle(host, nconf, &parms.r_addr);
896 if (client == NULL) {
897 goto error;
900 if (parms.r_addr == NULL)
901 parms.r_addr = __UNCONST(&nullstring[0]);
903 /* First try from start_vers and then version 3 (RPCBVERS) */
904 for (vers = start_vers; vers >= RPCBVERS; vers--) {
905 /* Set the version */
906 CLNT_CONTROL(client, CLSET_VERS, (char *)(void *)&vers);
907 clnt_st = CLNT_CALL(client, (rpcproc_t)RPCBPROC_GETADDR,
908 (xdrproc_t) xdr_rpcb, (char *)(void *)&parms,
909 (xdrproc_t) xdr_wrapstring, (char *)(void *) &ua,
910 tottimeout);
911 if (clnt_st == RPC_SUCCESS) {
912 if ((ua == NULL) || (ua[0] == 0)) {
913 /* address unknown */
914 rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
915 goto error;
917 address = uaddr2taddr(nconf, ua);
918 #ifdef ND_DEBUG
919 fprintf(stderr, "\tRemote address is [%s]\n", ua);
920 if (!address)
921 fprintf(stderr,
922 "\tCouldn't resolve remote address!\n");
923 #endif
924 xdr_free((xdrproc_t)xdr_wrapstring,
925 (char *)(void *)&ua);
927 if (! address) {
928 /* We don't know about your universal address */
929 rpc_createerr.cf_stat = RPC_N2AXLATEFAILURE;
930 goto error;
932 CLNT_CONTROL(client, CLGET_SVC_ADDR,
933 (char *)(void *)&servaddr);
934 __rpc_fixup_addr(address, &servaddr);
935 goto done;
936 } else if (clnt_st == RPC_PROGVERSMISMATCH) {
937 struct rpc_err rpcerr;
939 clnt_geterr(client, &rpcerr);
940 if (rpcerr.re_vers.low > RPCBVERS4)
941 goto error; /* a new version, can't handle */
942 } else if (clnt_st != RPC_PROGUNAVAIL) {
943 /* Cant handle this error */
944 rpc_createerr.cf_stat = clnt_st;
945 clnt_geterr(client, &rpc_createerr.cf_error);
946 goto error;
950 error:
951 if (client) {
952 CLNT_DESTROY(client);
953 client = NULL;
955 done:
956 if (nconf->nc_semantics != NC_TPI_CLTS) {
957 /* This client is the connectionless one */
958 if (client) {
959 CLNT_DESTROY(client);
960 client = NULL;
963 if (clpp) {
964 *clpp = client;
965 } else if (client) {
966 CLNT_DESTROY(client);
968 return (address);
973 * Find the mapped address for program, version.
974 * Calls the rpcbind service remotely to do the lookup.
975 * Uses the transport specified in nconf.
976 * Returns FALSE (0) if no map exists, else returns 1.
978 * Assuming that the address is all properly allocated
981 rpcb_getaddr(rpcprog_t program, rpcvers_t version,
982 const struct netconfig *nconf, struct netbuf *address,
983 const char *host)
985 struct netbuf *na;
987 _DIAGASSERT(address != NULL);
989 if ((na = __rpcb_findaddr(program, version, nconf,
990 host, NULL)) == NULL)
991 return (FALSE);
993 if (na->len > address->maxlen) {
994 /* Too long address */
995 free(na->buf);
996 free(na);
997 rpc_createerr.cf_stat = RPC_FAILED;
998 return (FALSE);
1000 memcpy(address->buf, na->buf, (size_t)na->len);
1001 address->len = na->len;
1002 free(na->buf);
1003 free(na);
1004 return (TRUE);
1008 * Get a copy of the current maps.
1009 * Calls the rpcbind service remotely to get the maps.
1011 * It returns only a list of the services
1012 * It returns NULL on failure.
1014 rpcblist *
1015 rpcb_getmaps(const struct netconfig *nconf, const char *host)
1017 rpcblist_ptr head = NULL;
1018 CLIENT *client;
1019 enum clnt_stat clnt_st;
1020 rpcvers_t vers = 0;
1022 client = getclnthandle(host, nconf, NULL);
1023 if (client == NULL) {
1024 return (head);
1026 clnt_st = CLNT_CALL(client, (rpcproc_t)RPCBPROC_DUMP,
1027 (xdrproc_t) xdr_void, NULL, (xdrproc_t) xdr_rpcblist_ptr,
1028 (char *)(void *)&head, tottimeout);
1029 if (clnt_st == RPC_SUCCESS)
1030 goto done;
1032 if ((clnt_st != RPC_PROGVERSMISMATCH) &&
1033 (clnt_st != RPC_PROGUNAVAIL)) {
1034 rpc_createerr.cf_stat = RPC_RPCBFAILURE;
1035 clnt_geterr(client, &rpc_createerr.cf_error);
1036 goto done;
1039 /* fall back to earlier version */
1040 CLNT_CONTROL(client, CLGET_VERS, (char *)(void *)&vers);
1041 if (vers == RPCBVERS4) {
1042 vers = RPCBVERS;
1043 CLNT_CONTROL(client, CLSET_VERS, (char *)(void *)&vers);
1044 if (CLNT_CALL(client, (rpcproc_t)RPCBPROC_DUMP,
1045 (xdrproc_t) xdr_void, NULL, (xdrproc_t) xdr_rpcblist_ptr,
1046 (char *)(void *)&head, tottimeout) == RPC_SUCCESS)
1047 goto done;
1049 rpc_createerr.cf_stat = RPC_RPCBFAILURE;
1050 clnt_geterr(client, &rpc_createerr.cf_error);
1052 done:
1053 CLNT_DESTROY(client);
1054 return (head);
1058 * rpcbinder remote-call-service interface.
1059 * This routine is used to call the rpcbind remote call service
1060 * which will look up a service program in the address maps, and then
1061 * remotely call that routine with the given parameters. This allows
1062 * programs to do a lookup and call in one step.
1064 enum clnt_stat
1065 rpcb_rmtcall(
1066 const struct netconfig *nconf, /* Netconfig structure */
1067 const char *host, /* Remote host name */
1068 rpcprog_t prog,
1069 rpcvers_t vers,
1070 rpcproc_t proc, /* Remote proc identifiers */
1071 xdrproc_t xdrargs,
1072 const char *argsp, /* Argument */
1073 xdrproc_t xdrres, /* XDR routines */
1074 caddr_t resp, /* Result */
1075 struct timeval tout, /* Timeout value for this call */
1076 const struct netbuf *addr_ptr) /* Preallocated netbuf address */
1078 CLIENT *client;
1079 enum clnt_stat stat;
1080 struct r_rpcb_rmtcallargs a;
1081 struct r_rpcb_rmtcallres r;
1082 rpcvers_t rpcb_vers;
1084 stat = RPC_FAILED; /* XXXGCC -Wuninitialized [dreamcast] */
1086 client = getclnthandle(host, nconf, NULL);
1087 if (client == NULL) {
1088 return (RPC_FAILED);
1090 CLNT_CONTROL(client, CLSET_RETRY_TIMEOUT, __UNCONST(&rmttimeout));
1091 a.prog = prog;
1092 a.vers = vers;
1093 a.proc = proc;
1094 a.args.args_val = argsp;
1095 a.xdr_args = xdrargs;
1096 r.addr = NULL;
1097 r.results.results_val = resp;
1098 r.xdr_res = xdrres;
1100 for (rpcb_vers = RPCBVERS4; rpcb_vers >= RPCBVERS; rpcb_vers--) {
1101 CLNT_CONTROL(client, CLSET_VERS, (char *)(void *)&rpcb_vers);
1102 stat = CLNT_CALL(client, (rpcproc_t)RPCBPROC_CALLIT,
1103 (xdrproc_t) xdr_rpcb_rmtcallargs, (char *)(void *)&a,
1104 (xdrproc_t) xdr_rpcb_rmtcallres, (char *)(void *)&r, tout);
1105 if ((stat == RPC_SUCCESS) && (addr_ptr != NULL)) {
1106 struct netbuf *na;
1107 na = uaddr2taddr(__UNCONST(nconf), r.addr);
1108 if (!na) {
1109 stat = RPC_N2AXLATEFAILURE;
1110 ((struct netbuf *)__UNCONST(addr_ptr))->len = 0;
1111 goto error;
1113 if (na->len > addr_ptr->maxlen) {
1114 /* Too long address */
1115 stat = RPC_FAILED; /* XXX A better error no */
1116 free(na->buf);
1117 free(na);
1118 ((struct netbuf *)__UNCONST(addr_ptr))->len = 0;
1119 goto error;
1121 memcpy(addr_ptr->buf, na->buf, (size_t)na->len);
1122 ((struct netbuf *)__UNCONST(addr_ptr))->len = na->len;
1123 free(na->buf);
1124 free(na);
1125 break;
1126 } else if ((stat != RPC_PROGVERSMISMATCH) &&
1127 (stat != RPC_PROGUNAVAIL)) {
1128 goto error;
1131 error:
1132 CLNT_DESTROY(client);
1133 if (r.addr)
1134 xdr_free((xdrproc_t) xdr_wrapstring, (char *)(void *)&r.addr);
1135 return (stat);
1139 * Gets the time on the remote host.
1140 * Returns 1 if succeeds else 0.
1142 bool_t
1143 rpcb_gettime(const char *host, time_t *timep)
1145 CLIENT *client = NULL;
1146 void *handle;
1147 struct netconfig *nconf;
1148 rpcvers_t vers;
1149 enum clnt_stat st;
1152 if ((host == NULL) || (host[0] == 0)) {
1153 time(timep);
1154 return (TRUE);
1157 if ((handle = __rpc_setconf("netpath")) == NULL) {
1158 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
1159 return (FALSE);
1161 rpc_createerr.cf_stat = RPC_SUCCESS;
1162 while (client == NULL) {
1163 if ((nconf = __rpc_getconf(handle)) == NULL) {
1164 if (rpc_createerr.cf_stat == RPC_SUCCESS)
1165 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
1166 break;
1168 client = getclnthandle(host, nconf, NULL);
1169 if (client)
1170 break;
1172 __rpc_endconf(handle);
1173 if (client == NULL) {
1174 return (FALSE);
1177 st = CLNT_CALL(client, (rpcproc_t)RPCBPROC_GETTIME,
1178 (xdrproc_t) xdr_void, NULL,
1179 (xdrproc_t) xdr_int, (char *)(void *)timep, tottimeout);
1181 if ((st == RPC_PROGVERSMISMATCH) || (st == RPC_PROGUNAVAIL)) {
1182 CLNT_CONTROL(client, CLGET_VERS, (char *)(void *)&vers);
1183 if (vers == RPCBVERS4) {
1184 /* fall back to earlier version */
1185 vers = RPCBVERS;
1186 CLNT_CONTROL(client, CLSET_VERS, (char *)(void *)&vers);
1187 st = CLNT_CALL(client, (rpcproc_t)RPCBPROC_GETTIME,
1188 (xdrproc_t) xdr_void, NULL,
1189 (xdrproc_t) xdr_int, (char *)(void *)timep,
1190 tottimeout);
1193 CLNT_DESTROY(client);
1194 return (st == RPC_SUCCESS? TRUE: FALSE);
1198 * Converts taddr to universal address. This routine should never
1199 * really be called because local n2a libraries are always provided.
1201 char *
1202 rpcb_taddr2uaddr(struct netconfig *nconf, struct netbuf *taddr)
1204 CLIENT *client;
1205 char *uaddr = NULL;
1207 /* parameter checking */
1208 if (nconf == NULL) {
1209 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
1210 return (NULL);
1212 if (taddr == NULL) {
1213 rpc_createerr.cf_stat = RPC_UNKNOWNADDR;
1214 return (NULL);
1216 client = local_rpcb();
1217 if (! client) {
1218 return (NULL);
1221 CLNT_CALL(client, (rpcproc_t)RPCBPROC_TADDR2UADDR,
1222 (xdrproc_t) xdr_netbuf, (char *)(void *)taddr,
1223 (xdrproc_t) xdr_wrapstring, (char *)(void *)&uaddr, tottimeout);
1224 CLNT_DESTROY(client);
1225 return (uaddr);
1229 * Converts universal address to netbuf. This routine should never
1230 * really be called because local n2a libraries are always provided.
1232 struct netbuf *
1233 rpcb_uaddr2taddr(struct netconfig *nconf, char *uaddr)
1235 CLIENT *client;
1236 struct netbuf *taddr;
1239 /* parameter checking */
1240 if (nconf == NULL) {
1241 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
1242 return (NULL);
1244 if (uaddr == NULL) {
1245 rpc_createerr.cf_stat = RPC_UNKNOWNADDR;
1246 return (NULL);
1248 client = local_rpcb();
1249 if (! client) {
1250 return (NULL);
1253 taddr = (struct netbuf *)calloc(1, sizeof (struct netbuf));
1254 if (taddr == NULL) {
1255 CLNT_DESTROY(client);
1256 return (NULL);
1258 if (CLNT_CALL(client, (rpcproc_t)RPCBPROC_UADDR2TADDR,
1259 (xdrproc_t) xdr_wrapstring, (char *)(void *)&uaddr,
1260 (xdrproc_t) xdr_netbuf, (char *)(void *)taddr,
1261 tottimeout) != RPC_SUCCESS) {
1262 free(taddr);
1263 taddr = NULL;
1265 CLNT_DESTROY(client);
1266 return (taddr);