rpcb_clnt.c revision 1.10 1 /* $NetBSD: rpcb_clnt.c,v 1.10 2001/11/04 14:43:55 lukem Exp $ */
2
3 /*
4 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
5 * unrestricted use provided that this legend is included on all tape
6 * media and as a part of the software program in whole or part. Users
7 * may copy or modify Sun RPC without charge, but are not authorized
8 * to license or distribute it to anyone else except as part of a product or
9 * program developed by the user.
10 *
11 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
12 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
13 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
14 *
15 * Sun RPC is provided with no support and without any obligation on the
16 * part of Sun Microsystems, Inc. to assist in its use, correction,
17 * modification or enhancement.
18 *
19 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
20 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
21 * OR ANY PART THEREOF.
22 *
23 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
24 * or profits or other special, indirect and consequential damages, even if
25 * Sun has been advised of the possibility of such damages.
26 *
27 * Sun Microsystems, Inc.
28 * 2550 Garcia Avenue
29 * Mountain View, California 94043
30 */
31 /*
32 * Copyright (c) 1986-1991 by Sun Microsystems Inc.
33 */
34
35 /* #ident "@(#)rpcb_clnt.c 1.27 94/04/24 SMI" */
36
37
38 #if 0
39 #if !defined(lint) && defined(SCCSIDS)
40 static char sccsid[] = "@(#)rpcb_clnt.c 1.30 89/06/21 Copyr 1988 Sun Micro";
41 #endif
42 #endif
43
44 /*
45 * rpcb_clnt.c
46 * interface to rpcbind rpc service.
47 *
48 * Copyright (C) 1988, Sun Microsystems, Inc.
49 */
50
51 #include "namespace.h"
52 #include "reentrant.h"
53 #include <sys/types.h>
54 #include <sys/socket.h>
55 #include <sys/un.h>
56 #include <sys/utsname.h>
57 #include <rpc/rpc.h>
58 #include <rpc/rpcb_prot.h>
59 #include <rpc/nettype.h>
60 #include <netconfig.h>
61 #ifdef PORTMAP
62 #include <netinet/in.h> /* FOR IPPROTO_TCP/UDP definitions */
63 #include <rpc/pmap_prot.h>
64 #endif
65 #include <assert.h>
66 #include <errno.h>
67 #include <netdb.h>
68 #include <stdio.h>
69 #include <stdlib.h>
70 #include <string.h>
71 #include <syslog.h>
72 #include <unistd.h>
73
74 #include "rpc_com.h"
75
76 #ifdef __weak_alias
77 __weak_alias(rpcb_set,_rpcb_set)
78 __weak_alias(rpcb_unset,_rpcb_unset)
79 __weak_alias(rpcb_getmaps,_rpcb_getmaps)
80 __weak_alias(rpcb_rmtcall,_rpcb_rmtcall)
81 __weak_alias(rpcb_gettime,_rpcb_gettime)
82 __weak_alias(rpcb_taddr2uaddr,_rpcb_taddr2uaddr)
83 __weak_alias(rpcb_uaddr2taddr,_rpcb_uaddr2taddr)
84 #endif
85
86 static struct timeval tottimeout = { 60, 0 };
87 static const struct timeval rmttimeout = { 3, 0 };
88
89 static const char nullstring[] = "\000";
90
91 #define CACHESIZE 6
92
93 struct address_cache {
94 char *ac_host;
95 char *ac_netid;
96 char *ac_uaddr;
97 struct netbuf *ac_taddr;
98 struct address_cache *ac_next;
99 };
100
101 static struct address_cache *front;
102 static int cachesize;
103
104 #define CLCR_GET_RPCB_TIMEOUT 1
105 #define CLCR_SET_RPCB_TIMEOUT 2
106
107
108 extern int __rpc_lowvers;
109
110 static struct address_cache *check_cache __P((const char *, const char *));
111 static void delete_cache __P((struct netbuf *));
112 static void add_cache __P((const char *, const char *, struct netbuf *,
113 char *));
114 static CLIENT *getclnthandle __P((const char *, const struct netconfig *,
115 char **));
116 static CLIENT *local_rpcb __P((void));
117 static struct netbuf *got_entry __P((rpcb_entry_list_ptr,
118 const struct netconfig *));
119
120 /*
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.
125 */
126 bool_t
127 __rpc_control(request, info)
128 int request;
129 void *info;
130 {
131
132 _DIAGASSERT(info != NULL);
133
134 switch (request) {
135 case CLCR_GET_RPCB_TIMEOUT:
136 *(struct timeval *)info = tottimeout;
137 break;
138 case CLCR_SET_RPCB_TIMEOUT:
139 tottimeout = *(struct timeval *)info;
140 break;
141 case CLCR_SET_LOWVERS:
142 __rpc_lowvers = *(int *)info;
143 break;
144 case CLCR_GET_LOWVERS:
145 *(int *)info = __rpc_lowvers;
146 break;
147 default:
148 return (FALSE);
149 }
150 return (TRUE);
151 }
152
153 /*
154 * It might seem that a reader/writer lock would be more reasonable here.
155 * However because getclnthandle(), the only user of the cache functions,
156 * may do a delete_cache() operation if a check_cache() fails to return an
157 * address useful to clnt_tli_create(), we may as well use a mutex.
158 */
159 /*
160 * As it turns out, if the cache lock is *not* a reader/writer lock, we will
161 * block all clnt_create's if we are trying to connect to a host that's down,
162 * since the lock will be held all during that time.
163 */
164 #ifdef __REENT
165 extern rwlock_t rpcbaddr_cache_lock;
166 #endif
167
168 /*
169 * The routines check_cache(), add_cache(), delete_cache() manage the
170 * cache of rpcbind addresses for (host, netid).
171 */
172
173 static struct address_cache *
174 check_cache(host, netid)
175 const char *host, *netid;
176 {
177 struct address_cache *cptr;
178
179 _DIAGASSERT(host != NULL);
180 _DIAGASSERT(netid != NULL);
181
182 /* READ LOCK HELD ON ENTRY: rpcbaddr_cache_lock */
183
184 for (cptr = front; cptr != NULL; cptr = cptr->ac_next) {
185 if (!strcmp(cptr->ac_host, host) &&
186 !strcmp(cptr->ac_netid, netid)) {
187 #ifdef ND_DEBUG
188 fprintf(stderr, "Found cache entry for %s: %s\n",
189 host, netid);
190 #endif
191 return (cptr);
192 }
193 }
194 return ((struct address_cache *) NULL);
195 }
196
197 static void
198 delete_cache(addr)
199 struct netbuf *addr;
200 {
201 struct address_cache *cptr, *prevptr = NULL;
202
203 _DIAGASSERT(addr != NULL);
204
205 /* WRITE LOCK HELD ON ENTRY: rpcbaddr_cache_lock */
206 for (cptr = front; cptr != NULL; cptr = cptr->ac_next) {
207 if (!memcmp(cptr->ac_taddr->buf, addr->buf, addr->len)) {
208 free(cptr->ac_host);
209 free(cptr->ac_netid);
210 free(cptr->ac_taddr->buf);
211 free(cptr->ac_taddr);
212 if (cptr->ac_uaddr)
213 free(cptr->ac_uaddr);
214 if (prevptr)
215 prevptr->ac_next = cptr->ac_next;
216 else
217 front = cptr->ac_next;
218 free(cptr);
219 cachesize--;
220 break;
221 }
222 prevptr = cptr;
223 }
224 }
225
226 static void
227 add_cache(host, netid, taddr, uaddr)
228 const char *host, *netid;
229 char *uaddr;
230 struct netbuf *taddr;
231 {
232 struct address_cache *ad_cache, *cptr, *prevptr;
233
234 _DIAGASSERT(host != NULL);
235 _DIAGASSERT(netid != NULL);
236 /* uaddr may be NULL */
237 /* taddr may be NULL ??? */
238
239 ad_cache = (struct address_cache *)
240 malloc(sizeof (struct address_cache));
241 if (!ad_cache) {
242 return;
243 }
244 ad_cache->ac_host = strdup(host);
245 ad_cache->ac_netid = strdup(netid);
246 ad_cache->ac_uaddr = uaddr ? strdup(uaddr) : NULL;
247 ad_cache->ac_taddr = (struct netbuf *)malloc(sizeof (struct netbuf));
248 if (!ad_cache->ac_host || !ad_cache->ac_netid || !ad_cache->ac_taddr ||
249 (uaddr && !ad_cache->ac_uaddr)) {
250 return;
251 }
252 ad_cache->ac_taddr->len = ad_cache->ac_taddr->maxlen = taddr->len;
253 ad_cache->ac_taddr->buf = (char *) malloc(taddr->len);
254 if (ad_cache->ac_taddr->buf == NULL) {
255 return;
256 }
257 memcpy(ad_cache->ac_taddr->buf, taddr->buf, taddr->len);
258 #ifdef ND_DEBUG
259 fprintf(stderr, "Added to cache: %s : %s\n", host, netid);
260 #endif
261
262 /* VARIABLES PROTECTED BY rpcbaddr_cache_lock: cptr */
263
264 rwlock_wrlock(&rpcbaddr_cache_lock);
265 if (cachesize < CACHESIZE) {
266 ad_cache->ac_next = front;
267 front = ad_cache;
268 cachesize++;
269 } else {
270 /* Free the last entry */
271 cptr = front;
272 prevptr = NULL;
273 while (cptr->ac_next) {
274 prevptr = cptr;
275 cptr = cptr->ac_next;
276 }
277
278 #ifdef ND_DEBUG
279 fprintf(stderr, "Deleted from cache: %s : %s\n",
280 cptr->ac_host, cptr->ac_netid);
281 #endif
282 free(cptr->ac_host);
283 free(cptr->ac_netid);
284 free(cptr->ac_taddr->buf);
285 free(cptr->ac_taddr);
286 if (cptr->ac_uaddr)
287 free(cptr->ac_uaddr);
288
289 if (prevptr) {
290 prevptr->ac_next = NULL;
291 ad_cache->ac_next = front;
292 front = ad_cache;
293 } else {
294 front = ad_cache;
295 ad_cache->ac_next = NULL;
296 }
297 free(cptr);
298 }
299 rwlock_unlock(&rpcbaddr_cache_lock);
300 }
301
302 /*
303 * This routine will return a client handle that is connected to the
304 * rpcbind. Returns NULL on error and free's everything.
305 */
306 static CLIENT *
307 getclnthandle(host, nconf, targaddr)
308 const char *host;
309 const struct netconfig *nconf;
310 char **targaddr;
311 {
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;
319
320 _DIAGASSERT(host != NULL);
321 _DIAGASSERT(nconf != NULL);
322 /* targaddr may be NULL */
323
324 /* VARIABLES PROTECTED BY rpcbaddr_cache_lock: ad_cache */
325
326 /* Get the address of the rpcbind. Check cache first */
327 addr_to_delete.len = 0;
328 rwlock_rdlock(&rpcbaddr_cache_lock);
329 ad_cache = check_cache(host, nconf->nc_netid);
330 if (ad_cache != NULL) {
331 addr = ad_cache->ac_taddr;
332 client = clnt_tli_create(RPC_ANYFD, nconf, addr,
333 (rpcprog_t)RPCBPROG, (rpcvers_t)RPCBVERS4, 0, 0);
334 if (client != NULL) {
335 if (targaddr)
336 *targaddr = ad_cache->ac_uaddr;
337 rwlock_unlock(&rpcbaddr_cache_lock);
338 return (client);
339 }
340 addr_to_delete.len = addr->len;
341 addr_to_delete.buf = (char *)malloc(addr->len);
342 if (addr_to_delete.buf == NULL) {
343 addr_to_delete.len = 0;
344 } else {
345 memcpy(addr_to_delete.buf, addr->buf, addr->len);
346 }
347 }
348 rwlock_unlock(&rpcbaddr_cache_lock);
349 if (addr_to_delete.len != 0) {
350 /*
351 * Assume this may be due to cache data being
352 * outdated
353 */
354 rwlock_wrlock(&rpcbaddr_cache_lock);
355 delete_cache(&addr_to_delete);
356 rwlock_unlock(&rpcbaddr_cache_lock);
357 free(addr_to_delete.buf);
358 }
359 if (!__rpc_nconf2sockinfo(nconf, &si)) {
360 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
361 return NULL;
362 }
363
364 memset(&hints, 0, sizeof hints);
365 hints.ai_family = si.si_af;
366 hints.ai_socktype = si.si_socktype;
367 hints.ai_protocol = si.si_proto;
368
369 #ifdef CLNT_DEBUG
370 printf("trying netid %s family %d proto %d socktype %d\n",
371 nconf->nc_netid, si.si_af, si.si_proto, si.si_socktype);
372 #endif
373
374 if (getaddrinfo(host, "sunrpc", &hints, &res) != 0) {
375 rpc_createerr.cf_stat = RPC_UNKNOWNHOST;
376 return NULL;
377 }
378
379 for (tres = res; tres != NULL; tres = tres->ai_next) {
380 taddr.buf = tres->ai_addr;
381 taddr.len = taddr.maxlen = tres->ai_addrlen;
382
383 #ifdef ND_DEBUG
384 {
385 char *ua;
386
387 ua = taddr2uaddr(nconf, &taddr);
388 fprintf(stderr, "Got it [%s]\n", ua);
389 free(ua);
390 }
391 #endif
392
393 #ifdef ND_DEBUG
394 {
395 int i;
396
397 fprintf(stderr, "\tnetbuf len = %d, maxlen = %d\n",
398 taddr.len, taddr.maxlen);
399 fprintf(stderr, "\tAddress is ");
400 for (i = 0; i < taddr.len; i++)
401 fprintf(stderr, "%u.", ((char *)(taddr.buf))[i]);
402 fprintf(stderr, "\n");
403 }
404 #endif
405 client = clnt_tli_create(RPC_ANYFD, nconf, &taddr,
406 (rpcprog_t)RPCBPROG, (rpcvers_t)RPCBVERS4, 0, 0);
407 #ifdef ND_DEBUG
408 if (! client) {
409 clnt_pcreateerror("rpcbind clnt interface");
410 }
411 #endif
412
413 if (client) {
414 tmpaddr = targaddr ? taddr2uaddr(nconf, &taddr) : NULL;
415 add_cache(host, nconf->nc_netid, &taddr, tmpaddr);
416 if (targaddr)
417 *targaddr = tmpaddr;
418 break;
419 }
420 }
421 freeaddrinfo(res);
422 return (client);
423 }
424
425 /* XXX */
426 #define IN4_LOCALHOST_STRING "127.0.0.1"
427 #define IN6_LOCALHOST_STRING "::1"
428
429 /*
430 * This routine will return a client handle that is connected to the local
431 * rpcbind. Returns NULL on error and free's everything.
432 */
433 static CLIENT *
434 local_rpcb()
435 {
436 CLIENT *client;
437 static struct netconfig *loopnconf;
438 static char *hostname;
439 #ifdef __REENT
440 extern mutex_t loopnconf_lock;
441 #endif
442 int sock;
443 size_t tsize;
444 struct netbuf nbuf;
445 struct sockaddr_un sun;
446
447 /*
448 * Try connecting to the local rpcbind through a local socket
449 * first. If this doesn't work, try all transports defined in
450 * the netconfig file.
451 */
452 memset(&sun, 0, sizeof sun);
453 sock = socket(AF_LOCAL, SOCK_STREAM, 0);
454 if (sock < 0)
455 goto try_nconf;
456 sun.sun_family = AF_LOCAL;
457 strcpy(sun.sun_path, _PATH_RPCBINDSOCK);
458 nbuf.len = sun.sun_len = SUN_LEN(&sun);
459 nbuf.maxlen = sizeof (struct sockaddr_un);
460 nbuf.buf = &sun;
461
462 tsize = __rpc_get_t_size(AF_LOCAL, 0, 0);
463 client = clnt_vc_create(sock, &nbuf, (rpcprog_t)RPCBPROG,
464 (rpcvers_t)RPCBVERS, tsize, tsize);
465
466 if (client != NULL) {
467 /* XXX - mark the socket to be closed in destructor */
468 (void) CLNT_CONTROL(client, CLSET_FD_CLOSE, NULL);
469 return client;
470 }
471
472 /* XXX - nobody needs this socket anymore, free the descriptor */
473 close(sock);
474
475 try_nconf:
476
477 /* VARIABLES PROTECTED BY loopnconf_lock: loopnconf */
478 mutex_lock(&loopnconf_lock);
479 if (loopnconf == NULL) {
480 struct netconfig *nconf, *tmpnconf = NULL;
481 void *nc_handle;
482 int fd;
483
484 nc_handle = setnetconfig();
485 if (nc_handle == NULL) {
486 /* fails to open netconfig file */
487 syslog (LOG_ERR, "rpc: failed to open " NETCONFIG);
488 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
489 mutex_unlock(&loopnconf_lock);
490 return (NULL);
491 }
492 while ((nconf = getnetconfig(nc_handle)) != NULL) {
493 #ifdef INET6
494 if ((strcmp(nconf->nc_protofmly, NC_INET6) == 0 ||
495 #else
496 if ((
497 #endif
498 strcmp(nconf->nc_protofmly, NC_INET) == 0) &&
499 (nconf->nc_semantics == NC_TPI_COTS ||
500 nconf->nc_semantics == NC_TPI_COTS_ORD)) {
501 fd = __rpc_nconf2fd(nconf);
502 /*
503 * Can't create a socket, assume that
504 * this family isn't configured in the kernel.
505 */
506 if (fd < 0)
507 continue;
508 close(fd);
509 tmpnconf = nconf;
510 if (!strcmp(nconf->nc_protofmly, NC_INET))
511 hostname = IN4_LOCALHOST_STRING;
512 else
513 hostname = IN6_LOCALHOST_STRING;
514 }
515 }
516 if (tmpnconf == NULL) {
517 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
518 mutex_unlock(&loopnconf_lock);
519 return (NULL);
520 }
521 loopnconf = getnetconfigent(tmpnconf->nc_netid);
522 /* loopnconf is never freed */
523 endnetconfig(nc_handle);
524 }
525 mutex_unlock(&loopnconf_lock);
526 client = getclnthandle(hostname, loopnconf, NULL);
527 return (client);
528 }
529
530 /*
531 * Set a mapping between program, version and address.
532 * Calls the rpcbind service to do the mapping.
533 */
534 bool_t
535 rpcb_set(program, version, nconf, address)
536 rpcprog_t program;
537 rpcvers_t version;
538 const struct netconfig *nconf; /* Network structure of transport */
539 const struct netbuf *address; /* Services netconfig address */
540 {
541 CLIENT *client;
542 bool_t rslt = FALSE;
543 RPCB parms;
544 char uidbuf[32];
545
546 /* parameter checking */
547 if (nconf == NULL) {
548 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
549 return (FALSE);
550 }
551 if (address == NULL) {
552 rpc_createerr.cf_stat = RPC_UNKNOWNADDR;
553 return (FALSE);
554 }
555 client = local_rpcb();
556 if (! client) {
557 return (FALSE);
558 }
559
560 /* convert to universal */
561 /*LINTED const castaway*/
562 parms.r_addr = taddr2uaddr((struct netconfig *) nconf,
563 (struct netbuf *)address);
564 if (!parms.r_addr) {
565 CLNT_DESTROY(client);
566 rpc_createerr.cf_stat = RPC_N2AXLATEFAILURE;
567 return (FALSE); /* no universal address */
568 }
569 parms.r_prog = program;
570 parms.r_vers = version;
571 parms.r_netid = nconf->nc_netid;
572 /*
573 * Though uid is not being used directly, we still send it for
574 * completeness. For non-unix platforms, perhaps some other
575 * string or an empty string can be sent.
576 */
577 (void) snprintf(uidbuf, sizeof uidbuf, "%d", geteuid());
578 parms.r_owner = uidbuf;
579
580 CLNT_CALL(client, (rpcproc_t)RPCBPROC_SET, (xdrproc_t) xdr_rpcb,
581 (char *)(void *)&parms, (xdrproc_t) xdr_bool,
582 (char *)(void *)&rslt, tottimeout);
583
584 CLNT_DESTROY(client);
585 free(parms.r_addr);
586 return (rslt);
587 }
588
589 /*
590 * Remove the mapping between program, version and netbuf address.
591 * Calls the rpcbind service to do the un-mapping.
592 * If netbuf is NULL, unset for all the transports, otherwise unset
593 * only for the given transport.
594 */
595 bool_t
596 rpcb_unset(program, version, nconf)
597 rpcprog_t program;
598 rpcvers_t version;
599 const struct netconfig *nconf;
600 {
601 CLIENT *client;
602 bool_t rslt = FALSE;
603 RPCB parms;
604 char uidbuf[32];
605
606 client = local_rpcb();
607 if (! client) {
608 return (FALSE);
609 }
610
611 parms.r_prog = program;
612 parms.r_vers = version;
613 if (nconf)
614 parms.r_netid = nconf->nc_netid;
615 else {
616 /*LINTED const castaway*/
617 parms.r_netid = (char *) &nullstring[0]; /* unsets all */
618 }
619 /*LINTED const castaway*/
620 parms.r_addr = (char *) &nullstring[0];
621 (void) snprintf(uidbuf, sizeof uidbuf, "%d", geteuid());
622 parms.r_owner = uidbuf;
623
624 CLNT_CALL(client, (rpcproc_t)RPCBPROC_UNSET, (xdrproc_t) xdr_rpcb,
625 (char *)(void *)&parms, (xdrproc_t) xdr_bool,
626 (char *)(void *)&rslt, tottimeout);
627
628 CLNT_DESTROY(client);
629 return (rslt);
630 }
631
632 /*
633 * From the merged list, find the appropriate entry
634 */
635 static struct netbuf *
636 got_entry(relp, nconf)
637 rpcb_entry_list_ptr relp;
638 const struct netconfig *nconf;
639 {
640 struct netbuf *na = NULL;
641 rpcb_entry_list_ptr sp;
642 rpcb_entry *rmap;
643
644 _DIAGASSERT(nconf != NULL);
645
646 for (sp = relp; sp != NULL; sp = sp->rpcb_entry_next) {
647 rmap = &sp->rpcb_entry_map;
648 if ((strcmp(nconf->nc_proto, rmap->r_nc_proto) == 0) &&
649 (strcmp(nconf->nc_protofmly, rmap->r_nc_protofmly) == 0) &&
650 (nconf->nc_semantics == rmap->r_nc_semantics) &&
651 (rmap->r_maddr != NULL) && (rmap->r_maddr[0] != NULL)) {
652 na = uaddr2taddr(nconf, rmap->r_maddr);
653 #ifdef ND_DEBUG
654 fprintf(stderr, "\tRemote address is [%s].\n",
655 rmap->r_maddr);
656 if (!na)
657 fprintf(stderr,
658 "\tCouldn't resolve remote address!\n");
659 #endif
660 break;
661 }
662 }
663 return (na);
664 }
665
666 /*
667 * An internal function which optimizes rpcb_getaddr function. It also
668 * returns the client handle that it uses to contact the remote rpcbind.
669 *
670 * The algorithm used: If the transports is TCP or UDP, it first tries
671 * version 2 (portmap), 4 and then 3 (svr4). This order should be
672 * changed in the next OS release to 4, 2 and 3. We are assuming that by
673 * that time, version 4 would be available on many machines on the network.
674 * With this algorithm, we get performance as well as a plan for
675 * obsoleting version 2.
676 *
677 * For all other transports, the algorithm remains as 4 and then 3.
678 *
679 * XXX: Due to some problems with t_connect(), we do not reuse the same client
680 * handle for COTS cases and hence in these cases we do not return the
681 * client handle. This code will change if t_connect() ever
682 * starts working properly. Also look under clnt_vc.c.
683 */
684 struct netbuf *
685 __rpcb_findaddr(program, version, nconf, host, clpp)
686 rpcprog_t program;
687 rpcvers_t version;
688 const struct netconfig *nconf;
689 const char *host;
690 CLIENT **clpp;
691 {
692 CLIENT *client = NULL;
693 RPCB parms;
694 enum clnt_stat clnt_st;
695 char *ua = NULL;
696 rpcvers_t vers;
697 struct netbuf *address = NULL;
698 rpcvers_t start_vers = RPCBVERS4;
699 struct netbuf servaddr;
700
701 /* nconf is handled below */
702 _DIAGASSERT(host != NULL);
703 /* clpp may be NULL */
704
705 /* parameter checking */
706 if (nconf == NULL) {
707 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
708 return (NULL);
709 }
710
711 parms.r_addr = NULL;
712
713 #ifdef PORTMAP
714 /* Try version 2 for TCP or UDP */
715 if (strcmp(nconf->nc_protofmly, NC_INET) == 0) {
716 u_short port = 0;
717 struct netbuf remote;
718 rpcvers_t pmapvers = 2;
719 struct pmap pmapparms;
720
721 /*
722 * Try UDP only - there are some portmappers out
723 * there that use UDP only.
724 */
725 if (strcmp(nconf->nc_proto, NC_TCP) == 0) {
726 struct netconfig *newnconf;
727
728 if ((newnconf = getnetconfigent("udp")) == NULL) {
729 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
730 return (NULL);
731 }
732 client = getclnthandle(host, newnconf, &parms.r_addr);
733 freenetconfigent(newnconf);
734 } else {
735 client = getclnthandle(host, nconf, &parms.r_addr);
736 }
737 if (client == NULL) {
738 return (NULL);
739 }
740
741 /* Set the version */
742 CLNT_CONTROL(client, CLSET_VERS, (char *)(void *)&pmapvers);
743 pmapparms.pm_prog = program;
744 pmapparms.pm_vers = version;
745 pmapparms.pm_prot = strcmp(nconf->nc_proto, NC_TCP) ?
746 IPPROTO_UDP : IPPROTO_TCP;
747 pmapparms.pm_port = 0; /* not needed */
748 clnt_st = CLNT_CALL(client, (rpcproc_t)PMAPPROC_GETPORT,
749 (xdrproc_t) xdr_pmap, (caddr_t)(void *)&pmapparms,
750 (xdrproc_t) xdr_u_short, (caddr_t)(void *)&port,
751 tottimeout);
752 if (clnt_st != RPC_SUCCESS) {
753 if ((clnt_st == RPC_PROGVERSMISMATCH) ||
754 (clnt_st == RPC_PROGUNAVAIL))
755 goto try_rpcbind; /* Try different versions */
756 rpc_createerr.cf_stat = RPC_PMAPFAILURE;
757 clnt_geterr(client, &rpc_createerr.cf_error);
758 goto error;
759 } else if (port == 0) {
760 address = NULL;
761 rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
762 goto error;
763 }
764 port = htons(port);
765 CLNT_CONTROL(client, CLGET_SVC_ADDR, (char *)(void *)&remote);
766 if (((address = (struct netbuf *)
767 malloc(sizeof (struct netbuf))) == NULL) ||
768 ((address->buf = (char *)
769 malloc(remote.len)) == NULL)) {
770 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
771 clnt_geterr(client, &rpc_createerr.cf_error);
772 if (address) {
773 free(address);
774 address = NULL;
775 }
776 goto error;
777 }
778 memcpy(address->buf, remote.buf, remote.len);
779 memcpy(&((char *)address->buf)[sizeof (short)],
780 (char *)(void *)&port, sizeof (short));
781 address->len = address->maxlen = remote.len;
782 goto done;
783 }
784 #endif
785
786 try_rpcbind:
787 /*
788 * Now we try version 4 and then 3.
789 * We also send the remote system the address we used to
790 * contact it in case it can help to connect back with us
791 */
792 parms.r_prog = program;
793 parms.r_vers = version;
794 /*LINTED const castaway*/
795 parms.r_owner = (char *) &nullstring[0]; /* not needed; */
796 /* just for xdring */
797 parms.r_netid = nconf->nc_netid; /* not really needed */
798
799 /*
800 * If a COTS transport is being used, try getting address via CLTS
801 * transport. This works only with version 4.
802 * NOTE: This is being done for all transports EXCEPT LOOPBACK
803 * because with loopback the cost to go to a COTS is same as
804 * the cost to go through CLTS, plus you get the advantage of
805 * finding out immediately if the local rpcbind process is dead.
806 */
807 #if 1
808 if ((nconf->nc_semantics == NC_TPI_COTS_ORD ||
809 nconf->nc_semantics == NC_TPI_COTS) &&
810 (strcmp(nconf->nc_protofmly, NC_LOOPBACK) != 0))
811 #else
812 if (client != NULL) {
813 CLNT_DESTROY(client);
814 client = NULL;
815 }
816 if (nconf->nc_semantics == NC_TPI_CLTS)
817 #endif
818 {
819 void *handle;
820 struct netconfig *nconf_clts;
821 rpcb_entry_list_ptr relp = NULL;
822
823 if (client == NULL) {
824 /* This did not go through the above PORTMAP/TCP code */
825 #if 1
826 if ((handle = __rpc_setconf("datagram_v")) != NULL)
827 #else
828 if ((handle = __rpc_setconf("circuit_v")) != NULL)
829 #endif
830 {
831 while ((nconf_clts = __rpc_getconf(handle))
832 != NULL) {
833 if (strcmp(nconf_clts->nc_protofmly,
834 nconf->nc_protofmly) != 0) {
835 continue;
836 }
837 client = getclnthandle(host, nconf_clts,
838 &parms.r_addr);
839 break;
840 }
841 __rpc_endconf(handle);
842 }
843 if (client == NULL)
844 goto regular_rpcbind; /* Go the regular way */
845 } else {
846 /* This is a UDP PORTMAP handle. Change to version 4 */
847 vers = RPCBVERS4;
848 CLNT_CONTROL(client, CLSET_VERS, (char *)(void *)&vers);
849 }
850 /*
851 * We also send the remote system the address we used to
852 * contact it in case it can help it connect back with us
853 */
854 if (parms.r_addr == NULL) {
855 /*LINTED const castaway*/
856 parms.r_addr = (char *) &nullstring[0]; /* for XDRing */
857 }
858 clnt_st = CLNT_CALL(client, (rpcproc_t)RPCBPROC_GETADDRLIST,
859 (xdrproc_t) xdr_rpcb, (char *)(void *)&parms,
860 (xdrproc_t) xdr_rpcb_entry_list_ptr,
861 (char *)(void *)&relp, tottimeout);
862 if (clnt_st == RPC_SUCCESS) {
863 if ((address = got_entry(relp, nconf)) != NULL) {
864 xdr_free((xdrproc_t) xdr_rpcb_entry_list_ptr,
865 (char *)(void *)&relp);
866 goto done;
867 }
868 /* Entry not found for this transport */
869 xdr_free((xdrproc_t) xdr_rpcb_entry_list_ptr,
870 (char *)(void *)&relp);
871 /*
872 * XXX: should have perhaps returned with error but
873 * since the remote machine might not always be able
874 * to send the address on all transports, we try the
875 * regular way with regular_rpcbind
876 */
877 goto regular_rpcbind;
878 } else if ((clnt_st == RPC_PROGVERSMISMATCH) ||
879 (clnt_st == RPC_PROGUNAVAIL)) {
880 start_vers = RPCBVERS; /* Try version 3 now */
881 goto regular_rpcbind; /* Try different versions */
882 } else {
883 rpc_createerr.cf_stat = RPC_PMAPFAILURE;
884 clnt_geterr(client, &rpc_createerr.cf_error);
885 goto error;
886 }
887 }
888
889 regular_rpcbind:
890
891 /* Now the same transport is to be used to get the address */
892 #if 1
893 if (client && ((nconf->nc_semantics == NC_TPI_COTS_ORD) ||
894 (nconf->nc_semantics == NC_TPI_COTS)))
895 #else
896 if (client && nconf->nc_semantics == NC_TPI_CLTS)
897 #endif
898 {
899 /* A CLTS type of client - destroy it */
900 CLNT_DESTROY(client);
901 client = NULL;
902 }
903
904 if (client == NULL) {
905 client = getclnthandle(host, nconf, &parms.r_addr);
906 if (client == NULL) {
907 goto error;
908 }
909 }
910 if (parms.r_addr == NULL) {
911 /*LINTED const castaway*/
912 parms.r_addr = (char *) &nullstring[0];
913 }
914
915 /* First try from start_vers and then version 3 (RPCBVERS) */
916 for (vers = start_vers; vers >= RPCBVERS; vers--) {
917 /* Set the version */
918 CLNT_CONTROL(client, CLSET_VERS, (char *)(void *)&vers);
919 clnt_st = CLNT_CALL(client, (rpcproc_t)RPCBPROC_GETADDR,
920 (xdrproc_t) xdr_rpcb, (char *)(void *)&parms,
921 (xdrproc_t) xdr_wrapstring, (char *)(void *) &ua,
922 tottimeout);
923 if (clnt_st == RPC_SUCCESS) {
924 if ((ua == NULL) || (ua[0] == NULL)) {
925 /* address unknown */
926 rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
927 goto error;
928 }
929 address = uaddr2taddr(nconf, ua);
930 #ifdef ND_DEBUG
931 fprintf(stderr, "\tRemote address is [%s]\n", ua);
932 if (!address)
933 fprintf(stderr,
934 "\tCouldn't resolve remote address!\n");
935 #endif
936 xdr_free((xdrproc_t)xdr_wrapstring,
937 (char *)(void *)&ua);
938
939 if (! address) {
940 /* We don't know about your universal address */
941 rpc_createerr.cf_stat = RPC_N2AXLATEFAILURE;
942 goto error;
943 }
944 CLNT_CONTROL(client, CLGET_SVC_ADDR,
945 (char *)(void *)&servaddr);
946 __rpc_fixup_addr(address, &servaddr);
947 goto done;
948 } else if (clnt_st == RPC_PROGVERSMISMATCH) {
949 struct rpc_err rpcerr;
950
951 clnt_geterr(client, &rpcerr);
952 if (rpcerr.re_vers.low > RPCBVERS4)
953 goto error; /* a new version, can't handle */
954 } else if (clnt_st != RPC_PROGUNAVAIL) {
955 /* Cant handle this error */
956 rpc_createerr.cf_stat = clnt_st;
957 clnt_geterr(client, &rpc_createerr.cf_error);
958 goto error;
959 }
960 }
961
962 if ((address == NULL) || (address->len == 0)) {
963 rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
964 clnt_geterr(client, &rpc_createerr.cf_error);
965 }
966
967 error:
968 if (client) {
969 CLNT_DESTROY(client);
970 client = NULL;
971 }
972 done:
973 if (nconf->nc_semantics != NC_TPI_CLTS) {
974 /* This client is the connectionless one */
975 if (client) {
976 CLNT_DESTROY(client);
977 client = NULL;
978 }
979 }
980 if (clpp) {
981 *clpp = client;
982 } else if (client) {
983 CLNT_DESTROY(client);
984 }
985 return (address);
986 }
987
988
989 /*
990 * Find the mapped address for program, version.
991 * Calls the rpcbind service remotely to do the lookup.
992 * Uses the transport specified in nconf.
993 * Returns FALSE (0) if no map exists, else returns 1.
994 *
995 * Assuming that the address is all properly allocated
996 */
997 int
998 rpcb_getaddr(program, version, nconf, address, host)
999 rpcprog_t program;
1000 rpcvers_t version;
1001 const struct netconfig *nconf;
1002 struct netbuf *address;
1003 const char *host;
1004 {
1005 struct netbuf *na;
1006
1007 _DIAGASSERT(address != NULL);
1008
1009 if ((na = __rpcb_findaddr(program, version, nconf,
1010 host, (CLIENT **) NULL)) == NULL)
1011 return (FALSE);
1012
1013 if (na->len > address->maxlen) {
1014 /* Too long address */
1015 free(na->buf);
1016 free(na);
1017 rpc_createerr.cf_stat = RPC_FAILED;
1018 return (FALSE);
1019 }
1020 memcpy(address->buf, na->buf, (size_t)na->len);
1021 address->len = na->len;
1022 free(na->buf);
1023 free(na);
1024 return (TRUE);
1025 }
1026
1027 /*
1028 * Get a copy of the current maps.
1029 * Calls the rpcbind service remotely to get the maps.
1030 *
1031 * It returns only a list of the services
1032 * It returns NULL on failure.
1033 */
1034 rpcblist *
1035 rpcb_getmaps(nconf, host)
1036 const struct netconfig *nconf;
1037 const char *host;
1038 {
1039 rpcblist_ptr head = NULL;
1040 CLIENT *client;
1041 enum clnt_stat clnt_st;
1042 rpcvers_t vers = 0;
1043
1044 client = getclnthandle(host, nconf, NULL);
1045 if (client == NULL) {
1046 return (head);
1047 }
1048 clnt_st = CLNT_CALL(client, (rpcproc_t)RPCBPROC_DUMP,
1049 (xdrproc_t) xdr_void, NULL, (xdrproc_t) xdr_rpcblist_ptr,
1050 (char *)(void *)&head, tottimeout);
1051 if (clnt_st == RPC_SUCCESS)
1052 goto done;
1053
1054 if ((clnt_st != RPC_PROGVERSMISMATCH) &&
1055 (clnt_st != RPC_PROGUNAVAIL)) {
1056 rpc_createerr.cf_stat = RPC_RPCBFAILURE;
1057 clnt_geterr(client, &rpc_createerr.cf_error);
1058 goto done;
1059 }
1060
1061 /* fall back to earlier version */
1062 CLNT_CONTROL(client, CLGET_VERS, (char *)(void *)&vers);
1063 if (vers == RPCBVERS4) {
1064 vers = RPCBVERS;
1065 CLNT_CONTROL(client, CLSET_VERS, (char *)(void *)&vers);
1066 if (CLNT_CALL(client, (rpcproc_t)RPCBPROC_DUMP,
1067 (xdrproc_t) xdr_void, NULL, (xdrproc_t) xdr_rpcblist_ptr,
1068 (char *)(void *)&head, tottimeout) == RPC_SUCCESS)
1069 goto done;
1070 }
1071 rpc_createerr.cf_stat = RPC_RPCBFAILURE;
1072 clnt_geterr(client, &rpc_createerr.cf_error);
1073
1074 done:
1075 CLNT_DESTROY(client);
1076 return (head);
1077 }
1078
1079 /*
1080 * rpcbinder remote-call-service interface.
1081 * This routine is used to call the rpcbind remote call service
1082 * which will look up a service program in the address maps, and then
1083 * remotely call that routine with the given parameters. This allows
1084 * programs to do a lookup and call in one step.
1085 */
1086 enum clnt_stat
1087 rpcb_rmtcall(nconf, host, prog, vers, proc, xdrargs, argsp,
1088 xdrres, resp, tout, addr_ptr)
1089 const struct netconfig *nconf; /* Netconfig structure */
1090 const char *host; /* Remote host name */
1091 rpcprog_t prog;
1092 rpcvers_t vers;
1093 rpcproc_t proc; /* Remote proc identifiers */
1094 xdrproc_t xdrargs, xdrres; /* XDR routines */
1095 caddr_t argsp, resp; /* Argument and Result */
1096 struct timeval tout; /* Timeout value for this call */
1097 const struct netbuf *addr_ptr; /* Preallocated netbuf address */
1098 {
1099 CLIENT *client;
1100 enum clnt_stat stat;
1101 struct r_rpcb_rmtcallargs a;
1102 struct r_rpcb_rmtcallres r;
1103 rpcvers_t rpcb_vers;
1104
1105 client = getclnthandle(host, nconf, NULL);
1106 if (client == NULL) {
1107 return (RPC_FAILED);
1108 }
1109 /*LINTED const castaway*/
1110 CLNT_CONTROL(client, CLSET_RETRY_TIMEOUT, (char *)(void *)&rmttimeout);
1111 a.prog = prog;
1112 a.vers = vers;
1113 a.proc = proc;
1114 a.args.args_val = argsp;
1115 a.xdr_args = xdrargs;
1116 r.addr = NULL;
1117 r.results.results_val = resp;
1118 r.xdr_res = xdrres;
1119
1120 for (rpcb_vers = RPCBVERS4; rpcb_vers >= RPCBVERS; rpcb_vers--) {
1121 CLNT_CONTROL(client, CLSET_VERS, (char *)(void *)&rpcb_vers);
1122 stat = CLNT_CALL(client, (rpcproc_t)RPCBPROC_CALLIT,
1123 (xdrproc_t) xdr_rpcb_rmtcallargs, (char *)(void *)&a,
1124 (xdrproc_t) xdr_rpcb_rmtcallres, (char *)(void *)&r, tout);
1125 if ((stat == RPC_SUCCESS) && (addr_ptr != NULL)) {
1126 struct netbuf *na;
1127 /*LINTED const castaway*/
1128 na = uaddr2taddr((struct netconfig *) nconf, r.addr);
1129 if (!na) {
1130 stat = RPC_N2AXLATEFAILURE;
1131 /*LINTED const castaway*/
1132 ((struct netbuf *) addr_ptr)->len = 0;
1133 goto error;
1134 }
1135 if (na->len > addr_ptr->maxlen) {
1136 /* Too long address */
1137 stat = RPC_FAILED; /* XXX A better error no */
1138 free(na->buf);
1139 free(na);
1140 /*LINTED const castaway*/
1141 ((struct netbuf *) addr_ptr)->len = 0;
1142 goto error;
1143 }
1144 memcpy(addr_ptr->buf, na->buf, (size_t)na->len);
1145 /*LINTED const castaway*/
1146 ((struct netbuf *)addr_ptr)->len = na->len;
1147 free(na->buf);
1148 free(na);
1149 break;
1150 } else if ((stat != RPC_PROGVERSMISMATCH) &&
1151 (stat != RPC_PROGUNAVAIL)) {
1152 goto error;
1153 }
1154 }
1155 error:
1156 CLNT_DESTROY(client);
1157 if (r.addr)
1158 xdr_free((xdrproc_t) xdr_wrapstring, (char *)(void *)&r.addr);
1159 return (stat);
1160 }
1161
1162 /*
1163 * Gets the time on the remote host.
1164 * Returns 1 if succeeds else 0.
1165 */
1166 bool_t
1167 rpcb_gettime(host, timep)
1168 const char *host;
1169 time_t *timep;
1170 {
1171 CLIENT *client = NULL;
1172 void *handle;
1173 struct netconfig *nconf;
1174 rpcvers_t vers;
1175 enum clnt_stat st;
1176
1177
1178 if ((host == NULL) || (host[0] == NULL)) {
1179 time(timep);
1180 return (TRUE);
1181 }
1182
1183 if ((handle = __rpc_setconf("netpath")) == NULL) {
1184 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
1185 return (FALSE);
1186 }
1187 rpc_createerr.cf_stat = RPC_SUCCESS;
1188 while (client == NULL) {
1189 if ((nconf = __rpc_getconf(handle)) == NULL) {
1190 if (rpc_createerr.cf_stat == RPC_SUCCESS)
1191 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
1192 break;
1193 }
1194 client = getclnthandle(host, nconf, NULL);
1195 if (client)
1196 break;
1197 }
1198 __rpc_endconf(handle);
1199 if (client == (CLIENT *) NULL) {
1200 return (FALSE);
1201 }
1202
1203 st = CLNT_CALL(client, (rpcproc_t)RPCBPROC_GETTIME,
1204 (xdrproc_t) xdr_void, NULL,
1205 (xdrproc_t) xdr_int, (char *)(void *)timep, tottimeout);
1206
1207 if ((st == RPC_PROGVERSMISMATCH) || (st == RPC_PROGUNAVAIL)) {
1208 CLNT_CONTROL(client, CLGET_VERS, (char *)(void *)&vers);
1209 if (vers == RPCBVERS4) {
1210 /* fall back to earlier version */
1211 vers = RPCBVERS;
1212 CLNT_CONTROL(client, CLSET_VERS, (char *)(void *)&vers);
1213 st = CLNT_CALL(client, (rpcproc_t)RPCBPROC_GETTIME,
1214 (xdrproc_t) xdr_void, NULL,
1215 (xdrproc_t) xdr_int, (char *)(void *)timep,
1216 tottimeout);
1217 }
1218 }
1219 CLNT_DESTROY(client);
1220 return (st == RPC_SUCCESS? TRUE: FALSE);
1221 }
1222
1223 /*
1224 * Converts taddr to universal address. This routine should never
1225 * really be called because local n2a libraries are always provided.
1226 */
1227 char *
1228 rpcb_taddr2uaddr(nconf, taddr)
1229 struct netconfig *nconf;
1230 struct netbuf *taddr;
1231 {
1232 CLIENT *client;
1233 char *uaddr = NULL;
1234
1235
1236 /* parameter checking */
1237 if (nconf == NULL) {
1238 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
1239 return (NULL);
1240 }
1241 if (taddr == NULL) {
1242 rpc_createerr.cf_stat = RPC_UNKNOWNADDR;
1243 return (NULL);
1244 }
1245 client = local_rpcb();
1246 if (! client) {
1247 return (NULL);
1248 }
1249
1250 CLNT_CALL(client, (rpcproc_t)RPCBPROC_TADDR2UADDR,
1251 (xdrproc_t) xdr_netbuf, (char *)(void *)taddr,
1252 (xdrproc_t) xdr_wrapstring, (char *)(void *)&uaddr, tottimeout);
1253 CLNT_DESTROY(client);
1254 return (uaddr);
1255 }
1256
1257 /*
1258 * Converts universal address to netbuf. This routine should never
1259 * really be called because local n2a libraries are always provided.
1260 */
1261 struct netbuf *
1262 rpcb_uaddr2taddr(nconf, uaddr)
1263 struct netconfig *nconf;
1264 char *uaddr;
1265 {
1266 CLIENT *client;
1267 struct netbuf *taddr;
1268
1269
1270 /* parameter checking */
1271 if (nconf == NULL) {
1272 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
1273 return (NULL);
1274 }
1275 if (uaddr == NULL) {
1276 rpc_createerr.cf_stat = RPC_UNKNOWNADDR;
1277 return (NULL);
1278 }
1279 client = local_rpcb();
1280 if (! client) {
1281 return (NULL);
1282 }
1283
1284 taddr = (struct netbuf *)calloc(1, sizeof (struct netbuf));
1285 if (taddr == NULL) {
1286 CLNT_DESTROY(client);
1287 return (NULL);
1288 }
1289 if (CLNT_CALL(client, (rpcproc_t)RPCBPROC_UADDR2TADDR,
1290 (xdrproc_t) xdr_wrapstring, (char *)(void *)&uaddr,
1291 (xdrproc_t) xdr_netbuf, (char *)(void *)taddr,
1292 tottimeout) != RPC_SUCCESS) {
1293 free(taddr);
1294 taddr = NULL;
1295 }
1296 CLNT_DESTROY(client);
1297 return (taddr);
1298 }
1299