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