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