rpcb_clnt.c revision 1.9 1 /* $NetBSD: rpcb_clnt.c,v 1.9 2001/09/27 18:59:37 jdolecek 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 return client;
468
469 try_nconf:
470
471 /* VARIABLES PROTECTED BY loopnconf_lock: loopnconf */
472 mutex_lock(&loopnconf_lock);
473 if (loopnconf == NULL) {
474 struct netconfig *nconf, *tmpnconf = NULL;
475 void *nc_handle;
476 int fd;
477
478 nc_handle = setnetconfig();
479 if (nc_handle == NULL) {
480 /* fails to open netconfig file */
481 syslog (LOG_ERR, "rpc: failed to open " NETCONFIG);
482 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
483 mutex_unlock(&loopnconf_lock);
484 return (NULL);
485 }
486 while ((nconf = getnetconfig(nc_handle)) != NULL) {
487 #ifdef INET6
488 if ((strcmp(nconf->nc_protofmly, NC_INET6) == 0 ||
489 #else
490 if ((
491 #endif
492 strcmp(nconf->nc_protofmly, NC_INET) == 0) &&
493 (nconf->nc_semantics == NC_TPI_COTS ||
494 nconf->nc_semantics == NC_TPI_COTS_ORD)) {
495 fd = __rpc_nconf2fd(nconf);
496 /*
497 * Can't create a socket, assume that
498 * this family isn't configured in the kernel.
499 */
500 if (fd < 0)
501 continue;
502 close(fd);
503 tmpnconf = nconf;
504 if (!strcmp(nconf->nc_protofmly, NC_INET))
505 hostname = IN4_LOCALHOST_STRING;
506 else
507 hostname = IN6_LOCALHOST_STRING;
508 }
509 }
510 if (tmpnconf == NULL) {
511 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
512 mutex_unlock(&loopnconf_lock);
513 return (NULL);
514 }
515 loopnconf = getnetconfigent(tmpnconf->nc_netid);
516 /* loopnconf is never freed */
517 endnetconfig(nc_handle);
518 }
519 mutex_unlock(&loopnconf_lock);
520 client = getclnthandle(hostname, loopnconf, NULL);
521 return (client);
522 }
523
524 /*
525 * Set a mapping between program, version and address.
526 * Calls the rpcbind service to do the mapping.
527 */
528 bool_t
529 rpcb_set(program, version, nconf, address)
530 rpcprog_t program;
531 rpcvers_t version;
532 const struct netconfig *nconf; /* Network structure of transport */
533 const struct netbuf *address; /* Services netconfig address */
534 {
535 CLIENT *client;
536 bool_t rslt = FALSE;
537 RPCB parms;
538 char uidbuf[32];
539
540 /* parameter checking */
541 if (nconf == NULL) {
542 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
543 return (FALSE);
544 }
545 if (address == NULL) {
546 rpc_createerr.cf_stat = RPC_UNKNOWNADDR;
547 return (FALSE);
548 }
549 client = local_rpcb();
550 if (! client) {
551 return (FALSE);
552 }
553
554 /* convert to universal */
555 /*LINTED const castaway*/
556 parms.r_addr = taddr2uaddr((struct netconfig *) nconf,
557 (struct netbuf *)address);
558 if (!parms.r_addr) {
559 rpc_createerr.cf_stat = RPC_N2AXLATEFAILURE;
560 return (FALSE); /* no universal address */
561 }
562 parms.r_prog = program;
563 parms.r_vers = version;
564 parms.r_netid = nconf->nc_netid;
565 /*
566 * Though uid is not being used directly, we still send it for
567 * completeness. For non-unix platforms, perhaps some other
568 * string or an empty string can be sent.
569 */
570 (void) snprintf(uidbuf, sizeof uidbuf, "%d", geteuid());
571 parms.r_owner = uidbuf;
572
573 CLNT_CALL(client, (rpcproc_t)RPCBPROC_SET, (xdrproc_t) xdr_rpcb,
574 (char *)(void *)&parms, (xdrproc_t) xdr_bool,
575 (char *)(void *)&rslt, tottimeout);
576
577 CLNT_DESTROY(client);
578 free(parms.r_addr);
579 return (rslt);
580 }
581
582 /*
583 * Remove the mapping between program, version and netbuf address.
584 * Calls the rpcbind service to do the un-mapping.
585 * If netbuf is NULL, unset for all the transports, otherwise unset
586 * only for the given transport.
587 */
588 bool_t
589 rpcb_unset(program, version, nconf)
590 rpcprog_t program;
591 rpcvers_t version;
592 const struct netconfig *nconf;
593 {
594 CLIENT *client;
595 bool_t rslt = FALSE;
596 RPCB parms;
597 char uidbuf[32];
598
599 client = local_rpcb();
600 if (! client) {
601 return (FALSE);
602 }
603
604 parms.r_prog = program;
605 parms.r_vers = version;
606 if (nconf)
607 parms.r_netid = nconf->nc_netid;
608 else {
609 /*LINTED const castaway*/
610 parms.r_netid = (char *) &nullstring[0]; /* unsets all */
611 }
612 /*LINTED const castaway*/
613 parms.r_addr = (char *) &nullstring[0];
614 (void) snprintf(uidbuf, sizeof uidbuf, "%d", geteuid());
615 parms.r_owner = uidbuf;
616
617 CLNT_CALL(client, (rpcproc_t)RPCBPROC_UNSET, (xdrproc_t) xdr_rpcb,
618 (char *)(void *)&parms, (xdrproc_t) xdr_bool,
619 (char *)(void *)&rslt, tottimeout);
620
621 CLNT_DESTROY(client);
622 return (rslt);
623 }
624
625 /*
626 * From the merged list, find the appropriate entry
627 */
628 static struct netbuf *
629 got_entry(relp, nconf)
630 rpcb_entry_list_ptr relp;
631 const struct netconfig *nconf;
632 {
633 struct netbuf *na = NULL;
634 rpcb_entry_list_ptr sp;
635 rpcb_entry *rmap;
636
637 _DIAGASSERT(nconf != NULL);
638
639 for (sp = relp; sp != NULL; sp = sp->rpcb_entry_next) {
640 rmap = &sp->rpcb_entry_map;
641 if ((strcmp(nconf->nc_proto, rmap->r_nc_proto) == 0) &&
642 (strcmp(nconf->nc_protofmly, rmap->r_nc_protofmly) == 0) &&
643 (nconf->nc_semantics == rmap->r_nc_semantics) &&
644 (rmap->r_maddr != NULL) && (rmap->r_maddr[0] != NULL)) {
645 na = uaddr2taddr(nconf, rmap->r_maddr);
646 #ifdef ND_DEBUG
647 fprintf(stderr, "\tRemote address is [%s].\n",
648 rmap->r_maddr);
649 if (!na)
650 fprintf(stderr,
651 "\tCouldn't resolve remote address!\n");
652 #endif
653 break;
654 }
655 }
656 return (na);
657 }
658
659 /*
660 * An internal function which optimizes rpcb_getaddr function. It also
661 * returns the client handle that it uses to contact the remote rpcbind.
662 *
663 * The algorithm used: If the transports is TCP or UDP, it first tries
664 * version 2 (portmap), 4 and then 3 (svr4). This order should be
665 * changed in the next OS release to 4, 2 and 3. We are assuming that by
666 * that time, version 4 would be available on many machines on the network.
667 * With this algorithm, we get performance as well as a plan for
668 * obsoleting version 2.
669 *
670 * For all other transports, the algorithm remains as 4 and then 3.
671 *
672 * XXX: Due to some problems with t_connect(), we do not reuse the same client
673 * handle for COTS cases and hence in these cases we do not return the
674 * client handle. This code will change if t_connect() ever
675 * starts working properly. Also look under clnt_vc.c.
676 */
677 struct netbuf *
678 __rpcb_findaddr(program, version, nconf, host, clpp)
679 rpcprog_t program;
680 rpcvers_t version;
681 const struct netconfig *nconf;
682 const char *host;
683 CLIENT **clpp;
684 {
685 CLIENT *client = NULL;
686 RPCB parms;
687 enum clnt_stat clnt_st;
688 char *ua = NULL;
689 rpcvers_t vers;
690 struct netbuf *address = NULL;
691 rpcvers_t start_vers = RPCBVERS4;
692 struct netbuf servaddr;
693
694 /* nconf is handled below */
695 _DIAGASSERT(host != NULL);
696 /* clpp may be NULL */
697
698 /* parameter checking */
699 if (nconf == NULL) {
700 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
701 return (NULL);
702 }
703
704 parms.r_addr = NULL;
705
706 #ifdef PORTMAP
707 /* Try version 2 for TCP or UDP */
708 if (strcmp(nconf->nc_protofmly, NC_INET) == 0) {
709 u_short port = 0;
710 struct netbuf remote;
711 rpcvers_t pmapvers = 2;
712 struct pmap pmapparms;
713
714 /*
715 * Try UDP only - there are some portmappers out
716 * there that use UDP only.
717 */
718 if (strcmp(nconf->nc_proto, NC_TCP) == 0) {
719 struct netconfig *newnconf;
720
721 if ((newnconf = getnetconfigent("udp")) == NULL) {
722 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
723 return (NULL);
724 }
725 client = getclnthandle(host, newnconf, &parms.r_addr);
726 freenetconfigent(newnconf);
727 } else {
728 client = getclnthandle(host, nconf, &parms.r_addr);
729 }
730 if (client == NULL) {
731 return (NULL);
732 }
733
734 /* Set the version */
735 CLNT_CONTROL(client, CLSET_VERS, (char *)(void *)&pmapvers);
736 pmapparms.pm_prog = program;
737 pmapparms.pm_vers = version;
738 pmapparms.pm_prot = strcmp(nconf->nc_proto, NC_TCP) ?
739 IPPROTO_UDP : IPPROTO_TCP;
740 pmapparms.pm_port = 0; /* not needed */
741 clnt_st = CLNT_CALL(client, (rpcproc_t)PMAPPROC_GETPORT,
742 (xdrproc_t) xdr_pmap, (caddr_t)(void *)&pmapparms,
743 (xdrproc_t) xdr_u_short, (caddr_t)(void *)&port,
744 tottimeout);
745 if (clnt_st != RPC_SUCCESS) {
746 if ((clnt_st == RPC_PROGVERSMISMATCH) ||
747 (clnt_st == RPC_PROGUNAVAIL))
748 goto try_rpcbind; /* Try different versions */
749 rpc_createerr.cf_stat = RPC_PMAPFAILURE;
750 clnt_geterr(client, &rpc_createerr.cf_error);
751 goto error;
752 } else if (port == 0) {
753 address = NULL;
754 rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
755 goto error;
756 }
757 port = htons(port);
758 CLNT_CONTROL(client, CLGET_SVC_ADDR, (char *)(void *)&remote);
759 if (((address = (struct netbuf *)
760 malloc(sizeof (struct netbuf))) == NULL) ||
761 ((address->buf = (char *)
762 malloc(remote.len)) == NULL)) {
763 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
764 clnt_geterr(client, &rpc_createerr.cf_error);
765 if (address) {
766 free(address);
767 address = NULL;
768 }
769 goto error;
770 }
771 memcpy(address->buf, remote.buf, remote.len);
772 memcpy(&((char *)address->buf)[sizeof (short)],
773 (char *)(void *)&port, sizeof (short));
774 address->len = address->maxlen = remote.len;
775 goto done;
776 }
777 #endif
778
779 try_rpcbind:
780 /*
781 * Now we try version 4 and then 3.
782 * We also send the remote system the address we used to
783 * contact it in case it can help to connect back with us
784 */
785 parms.r_prog = program;
786 parms.r_vers = version;
787 /*LINTED const castaway*/
788 parms.r_owner = (char *) &nullstring[0]; /* not needed; */
789 /* just for xdring */
790 parms.r_netid = nconf->nc_netid; /* not really needed */
791
792 /*
793 * If a COTS transport is being used, try getting address via CLTS
794 * transport. This works only with version 4.
795 * NOTE: This is being done for all transports EXCEPT LOOPBACK
796 * because with loopback the cost to go to a COTS is same as
797 * the cost to go through CLTS, plus you get the advantage of
798 * finding out immediately if the local rpcbind process is dead.
799 */
800 #if 1
801 if ((nconf->nc_semantics == NC_TPI_COTS_ORD ||
802 nconf->nc_semantics == NC_TPI_COTS) &&
803 (strcmp(nconf->nc_protofmly, NC_LOOPBACK) != 0))
804 #else
805 if (client != NULL) {
806 CLNT_DESTROY(client);
807 client = NULL;
808 }
809 if (nconf->nc_semantics == NC_TPI_CLTS)
810 #endif
811 {
812 void *handle;
813 struct netconfig *nconf_clts;
814 rpcb_entry_list_ptr relp = NULL;
815
816 if (client == NULL) {
817 /* This did not go through the above PORTMAP/TCP code */
818 #if 1
819 if ((handle = __rpc_setconf("datagram_v")) != NULL)
820 #else
821 if ((handle = __rpc_setconf("circuit_v")) != NULL)
822 #endif
823 {
824 while ((nconf_clts = __rpc_getconf(handle))
825 != NULL) {
826 if (strcmp(nconf_clts->nc_protofmly,
827 nconf->nc_protofmly) != 0) {
828 continue;
829 }
830 client = getclnthandle(host, nconf_clts,
831 &parms.r_addr);
832 break;
833 }
834 __rpc_endconf(handle);
835 }
836 if (client == NULL)
837 goto regular_rpcbind; /* Go the regular way */
838 } else {
839 /* This is a UDP PORTMAP handle. Change to version 4 */
840 vers = RPCBVERS4;
841 CLNT_CONTROL(client, CLSET_VERS, (char *)(void *)&vers);
842 }
843 /*
844 * We also send the remote system the address we used to
845 * contact it in case it can help it connect back with us
846 */
847 if (parms.r_addr == NULL) {
848 /*LINTED const castaway*/
849 parms.r_addr = (char *) &nullstring[0]; /* for XDRing */
850 }
851 clnt_st = CLNT_CALL(client, (rpcproc_t)RPCBPROC_GETADDRLIST,
852 (xdrproc_t) xdr_rpcb, (char *)(void *)&parms,
853 (xdrproc_t) xdr_rpcb_entry_list_ptr,
854 (char *)(void *)&relp, tottimeout);
855 if (clnt_st == RPC_SUCCESS) {
856 if ((address = got_entry(relp, nconf)) != NULL) {
857 xdr_free((xdrproc_t) xdr_rpcb_entry_list_ptr,
858 (char *)(void *)&relp);
859 goto done;
860 }
861 /* Entry not found for this transport */
862 xdr_free((xdrproc_t) xdr_rpcb_entry_list_ptr,
863 (char *)(void *)&relp);
864 /*
865 * XXX: should have perhaps returned with error but
866 * since the remote machine might not always be able
867 * to send the address on all transports, we try the
868 * regular way with regular_rpcbind
869 */
870 goto regular_rpcbind;
871 } else if ((clnt_st == RPC_PROGVERSMISMATCH) ||
872 (clnt_st == RPC_PROGUNAVAIL)) {
873 start_vers = RPCBVERS; /* Try version 3 now */
874 goto regular_rpcbind; /* Try different versions */
875 } else {
876 rpc_createerr.cf_stat = RPC_PMAPFAILURE;
877 clnt_geterr(client, &rpc_createerr.cf_error);
878 goto error;
879 }
880 }
881
882 regular_rpcbind:
883
884 /* Now the same transport is to be used to get the address */
885 #if 1
886 if (client && ((nconf->nc_semantics == NC_TPI_COTS_ORD) ||
887 (nconf->nc_semantics == NC_TPI_COTS)))
888 #else
889 if (client && nconf->nc_semantics == NC_TPI_CLTS)
890 #endif
891 {
892 /* A CLTS type of client - destroy it */
893 CLNT_DESTROY(client);
894 client = NULL;
895 }
896
897 if (client == NULL) {
898 client = getclnthandle(host, nconf, &parms.r_addr);
899 if (client == NULL) {
900 goto error;
901 }
902 }
903 if (parms.r_addr == NULL) {
904 /*LINTED const castaway*/
905 parms.r_addr = (char *) &nullstring[0];
906 }
907
908 /* First try from start_vers and then version 3 (RPCBVERS) */
909 for (vers = start_vers; vers >= RPCBVERS; vers--) {
910 /* Set the version */
911 CLNT_CONTROL(client, CLSET_VERS, (char *)(void *)&vers);
912 clnt_st = CLNT_CALL(client, (rpcproc_t)RPCBPROC_GETADDR,
913 (xdrproc_t) xdr_rpcb, (char *)(void *)&parms,
914 (xdrproc_t) xdr_wrapstring, (char *)(void *) &ua,
915 tottimeout);
916 if (clnt_st == RPC_SUCCESS) {
917 if ((ua == NULL) || (ua[0] == NULL)) {
918 /* address unknown */
919 rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
920 goto error;
921 }
922 address = uaddr2taddr(nconf, ua);
923 #ifdef ND_DEBUG
924 fprintf(stderr, "\tRemote address is [%s]\n", ua);
925 if (!address)
926 fprintf(stderr,
927 "\tCouldn't resolve remote address!\n");
928 #endif
929 xdr_free((xdrproc_t)xdr_wrapstring,
930 (char *)(void *)&ua);
931
932 if (! address) {
933 /* We don't know about your universal address */
934 rpc_createerr.cf_stat = RPC_N2AXLATEFAILURE;
935 goto error;
936 }
937 CLNT_CONTROL(client, CLGET_SVC_ADDR,
938 (char *)(void *)&servaddr);
939 __rpc_fixup_addr(address, &servaddr);
940 goto done;
941 } else if (clnt_st == RPC_PROGVERSMISMATCH) {
942 struct rpc_err rpcerr;
943
944 clnt_geterr(client, &rpcerr);
945 if (rpcerr.re_vers.low > RPCBVERS4)
946 goto error; /* a new version, can't handle */
947 } else if (clnt_st != RPC_PROGUNAVAIL) {
948 /* Cant handle this error */
949 rpc_createerr.cf_stat = clnt_st;
950 clnt_geterr(client, &rpc_createerr.cf_error);
951 goto error;
952 }
953 }
954
955 if ((address == NULL) || (address->len == 0)) {
956 rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
957 clnt_geterr(client, &rpc_createerr.cf_error);
958 }
959
960 error:
961 if (client) {
962 CLNT_DESTROY(client);
963 client = NULL;
964 }
965 done:
966 if (nconf->nc_semantics != NC_TPI_CLTS) {
967 /* This client is the connectionless one */
968 if (client) {
969 CLNT_DESTROY(client);
970 client = NULL;
971 }
972 }
973 if (clpp) {
974 *clpp = client;
975 } else if (client) {
976 CLNT_DESTROY(client);
977 }
978 return (address);
979 }
980
981
982 /*
983 * Find the mapped address for program, version.
984 * Calls the rpcbind service remotely to do the lookup.
985 * Uses the transport specified in nconf.
986 * Returns FALSE (0) if no map exists, else returns 1.
987 *
988 * Assuming that the address is all properly allocated
989 */
990 int
991 rpcb_getaddr(program, version, nconf, address, host)
992 rpcprog_t program;
993 rpcvers_t version;
994 const struct netconfig *nconf;
995 struct netbuf *address;
996 const char *host;
997 {
998 struct netbuf *na;
999
1000 _DIAGASSERT(address != NULL);
1001
1002 if ((na = __rpcb_findaddr(program, version, nconf,
1003 host, (CLIENT **) NULL)) == NULL)
1004 return (FALSE);
1005
1006 if (na->len > address->maxlen) {
1007 /* Too long address */
1008 free(na->buf);
1009 free(na);
1010 rpc_createerr.cf_stat = RPC_FAILED;
1011 return (FALSE);
1012 }
1013 memcpy(address->buf, na->buf, (size_t)na->len);
1014 address->len = na->len;
1015 free(na->buf);
1016 free(na);
1017 return (TRUE);
1018 }
1019
1020 /*
1021 * Get a copy of the current maps.
1022 * Calls the rpcbind service remotely to get the maps.
1023 *
1024 * It returns only a list of the services
1025 * It returns NULL on failure.
1026 */
1027 rpcblist *
1028 rpcb_getmaps(nconf, host)
1029 const struct netconfig *nconf;
1030 const char *host;
1031 {
1032 rpcblist_ptr head = NULL;
1033 CLIENT *client;
1034 enum clnt_stat clnt_st;
1035 rpcvers_t vers = 0;
1036
1037 client = getclnthandle(host, nconf, NULL);
1038 if (client == NULL) {
1039 return (head);
1040 }
1041 clnt_st = CLNT_CALL(client, (rpcproc_t)RPCBPROC_DUMP,
1042 (xdrproc_t) xdr_void, NULL, (xdrproc_t) xdr_rpcblist_ptr,
1043 (char *)(void *)&head, tottimeout);
1044 if (clnt_st == RPC_SUCCESS)
1045 goto done;
1046
1047 if ((clnt_st != RPC_PROGVERSMISMATCH) &&
1048 (clnt_st != RPC_PROGUNAVAIL)) {
1049 rpc_createerr.cf_stat = RPC_RPCBFAILURE;
1050 clnt_geterr(client, &rpc_createerr.cf_error);
1051 goto done;
1052 }
1053
1054 /* fall back to earlier version */
1055 CLNT_CONTROL(client, CLGET_VERS, (char *)(void *)&vers);
1056 if (vers == RPCBVERS4) {
1057 vers = RPCBVERS;
1058 CLNT_CONTROL(client, CLSET_VERS, (char *)(void *)&vers);
1059 if (CLNT_CALL(client, (rpcproc_t)RPCBPROC_DUMP,
1060 (xdrproc_t) xdr_void, NULL, (xdrproc_t) xdr_rpcblist_ptr,
1061 (char *)(void *)&head, tottimeout) == RPC_SUCCESS)
1062 goto done;
1063 }
1064 rpc_createerr.cf_stat = RPC_RPCBFAILURE;
1065 clnt_geterr(client, &rpc_createerr.cf_error);
1066
1067 done:
1068 CLNT_DESTROY(client);
1069 return (head);
1070 }
1071
1072 /*
1073 * rpcbinder remote-call-service interface.
1074 * This routine is used to call the rpcbind remote call service
1075 * which will look up a service program in the address maps, and then
1076 * remotely call that routine with the given parameters. This allows
1077 * programs to do a lookup and call in one step.
1078 */
1079 enum clnt_stat
1080 rpcb_rmtcall(nconf, host, prog, vers, proc, xdrargs, argsp,
1081 xdrres, resp, tout, addr_ptr)
1082 const struct netconfig *nconf; /* Netconfig structure */
1083 const char *host; /* Remote host name */
1084 rpcprog_t prog;
1085 rpcvers_t vers;
1086 rpcproc_t proc; /* Remote proc identifiers */
1087 xdrproc_t xdrargs, xdrres; /* XDR routines */
1088 caddr_t argsp, resp; /* Argument and Result */
1089 struct timeval tout; /* Timeout value for this call */
1090 const struct netbuf *addr_ptr; /* Preallocated netbuf address */
1091 {
1092 CLIENT *client;
1093 enum clnt_stat stat;
1094 struct r_rpcb_rmtcallargs a;
1095 struct r_rpcb_rmtcallres r;
1096 rpcvers_t rpcb_vers;
1097
1098 client = getclnthandle(host, nconf, NULL);
1099 if (client == NULL) {
1100 return (RPC_FAILED);
1101 }
1102 /*LINTED const castaway*/
1103 CLNT_CONTROL(client, CLSET_RETRY_TIMEOUT, (char *)(void *)&rmttimeout);
1104 a.prog = prog;
1105 a.vers = vers;
1106 a.proc = proc;
1107 a.args.args_val = argsp;
1108 a.xdr_args = xdrargs;
1109 r.addr = NULL;
1110 r.results.results_val = resp;
1111 r.xdr_res = xdrres;
1112
1113 for (rpcb_vers = RPCBVERS4; rpcb_vers >= RPCBVERS; rpcb_vers--) {
1114 CLNT_CONTROL(client, CLSET_VERS, (char *)(void *)&rpcb_vers);
1115 stat = CLNT_CALL(client, (rpcproc_t)RPCBPROC_CALLIT,
1116 (xdrproc_t) xdr_rpcb_rmtcallargs, (char *)(void *)&a,
1117 (xdrproc_t) xdr_rpcb_rmtcallres, (char *)(void *)&r, tout);
1118 if ((stat == RPC_SUCCESS) && (addr_ptr != NULL)) {
1119 struct netbuf *na;
1120 /*LINTED const castaway*/
1121 na = uaddr2taddr((struct netconfig *) nconf, r.addr);
1122 if (!na) {
1123 stat = RPC_N2AXLATEFAILURE;
1124 /*LINTED const castaway*/
1125 ((struct netbuf *) addr_ptr)->len = 0;
1126 goto error;
1127 }
1128 if (na->len > addr_ptr->maxlen) {
1129 /* Too long address */
1130 stat = RPC_FAILED; /* XXX A better error no */
1131 free(na->buf);
1132 free(na);
1133 /*LINTED const castaway*/
1134 ((struct netbuf *) addr_ptr)->len = 0;
1135 goto error;
1136 }
1137 memcpy(addr_ptr->buf, na->buf, (size_t)na->len);
1138 /*LINTED const castaway*/
1139 ((struct netbuf *)addr_ptr)->len = na->len;
1140 free(na->buf);
1141 free(na);
1142 break;
1143 } else if ((stat != RPC_PROGVERSMISMATCH) &&
1144 (stat != RPC_PROGUNAVAIL)) {
1145 goto error;
1146 }
1147 }
1148 error:
1149 CLNT_DESTROY(client);
1150 if (r.addr)
1151 xdr_free((xdrproc_t) xdr_wrapstring, (char *)(void *)&r.addr);
1152 return (stat);
1153 }
1154
1155 /*
1156 * Gets the time on the remote host.
1157 * Returns 1 if succeeds else 0.
1158 */
1159 bool_t
1160 rpcb_gettime(host, timep)
1161 const char *host;
1162 time_t *timep;
1163 {
1164 CLIENT *client = NULL;
1165 void *handle;
1166 struct netconfig *nconf;
1167 rpcvers_t vers;
1168 enum clnt_stat st;
1169
1170
1171 if ((host == NULL) || (host[0] == NULL)) {
1172 time(timep);
1173 return (TRUE);
1174 }
1175
1176 if ((handle = __rpc_setconf("netpath")) == NULL) {
1177 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
1178 return (FALSE);
1179 }
1180 rpc_createerr.cf_stat = RPC_SUCCESS;
1181 while (client == NULL) {
1182 if ((nconf = __rpc_getconf(handle)) == NULL) {
1183 if (rpc_createerr.cf_stat == RPC_SUCCESS)
1184 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
1185 break;
1186 }
1187 client = getclnthandle(host, nconf, NULL);
1188 if (client)
1189 break;
1190 }
1191 __rpc_endconf(handle);
1192 if (client == (CLIENT *) NULL) {
1193 return (FALSE);
1194 }
1195
1196 st = CLNT_CALL(client, (rpcproc_t)RPCBPROC_GETTIME,
1197 (xdrproc_t) xdr_void, NULL,
1198 (xdrproc_t) xdr_int, (char *)(void *)timep, tottimeout);
1199
1200 if ((st == RPC_PROGVERSMISMATCH) || (st == RPC_PROGUNAVAIL)) {
1201 CLNT_CONTROL(client, CLGET_VERS, (char *)(void *)&vers);
1202 if (vers == RPCBVERS4) {
1203 /* fall back to earlier version */
1204 vers = RPCBVERS;
1205 CLNT_CONTROL(client, CLSET_VERS, (char *)(void *)&vers);
1206 st = CLNT_CALL(client, (rpcproc_t)RPCBPROC_GETTIME,
1207 (xdrproc_t) xdr_void, NULL,
1208 (xdrproc_t) xdr_int, (char *)(void *)timep,
1209 tottimeout);
1210 }
1211 }
1212 CLNT_DESTROY(client);
1213 return (st == RPC_SUCCESS? TRUE: FALSE);
1214 }
1215
1216 /*
1217 * Converts taddr to universal address. This routine should never
1218 * really be called because local n2a libraries are always provided.
1219 */
1220 char *
1221 rpcb_taddr2uaddr(nconf, taddr)
1222 struct netconfig *nconf;
1223 struct netbuf *taddr;
1224 {
1225 CLIENT *client;
1226 char *uaddr = NULL;
1227
1228
1229 /* parameter checking */
1230 if (nconf == NULL) {
1231 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
1232 return (NULL);
1233 }
1234 if (taddr == NULL) {
1235 rpc_createerr.cf_stat = RPC_UNKNOWNADDR;
1236 return (NULL);
1237 }
1238 client = local_rpcb();
1239 if (! client) {
1240 return (NULL);
1241 }
1242
1243 CLNT_CALL(client, (rpcproc_t)RPCBPROC_TADDR2UADDR,
1244 (xdrproc_t) xdr_netbuf, (char *)(void *)taddr,
1245 (xdrproc_t) xdr_wrapstring, (char *)(void *)&uaddr, tottimeout);
1246 CLNT_DESTROY(client);
1247 return (uaddr);
1248 }
1249
1250 /*
1251 * Converts universal address to netbuf. This routine should never
1252 * really be called because local n2a libraries are always provided.
1253 */
1254 struct netbuf *
1255 rpcb_uaddr2taddr(nconf, uaddr)
1256 struct netconfig *nconf;
1257 char *uaddr;
1258 {
1259 CLIENT *client;
1260 struct netbuf *taddr;
1261
1262
1263 /* parameter checking */
1264 if (nconf == NULL) {
1265 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
1266 return (NULL);
1267 }
1268 if (uaddr == NULL) {
1269 rpc_createerr.cf_stat = RPC_UNKNOWNADDR;
1270 return (NULL);
1271 }
1272 client = local_rpcb();
1273 if (! client) {
1274 return (NULL);
1275 }
1276
1277 taddr = (struct netbuf *)calloc(1, sizeof (struct netbuf));
1278 if (taddr == NULL) {
1279 CLNT_DESTROY(client);
1280 return (NULL);
1281 }
1282 if (CLNT_CALL(client, (rpcproc_t)RPCBPROC_UADDR2TADDR,
1283 (xdrproc_t) xdr_wrapstring, (char *)(void *)&uaddr,
1284 (xdrproc_t) xdr_netbuf, (char *)(void *)taddr,
1285 tottimeout) != RPC_SUCCESS) {
1286 free(taddr);
1287 taddr = NULL;
1288 }
1289 CLNT_DESTROY(client);
1290 return (taddr);
1291 }
1292