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