1 /*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
7 * with the License.
8 *
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
13 *
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
19 *
20 * CDDL HEADER END
21 */
22
23 /*
24 * Copyright 2014 Gary Mills
25 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
26 * Use is subject to license terms.
27 */
28
29 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
30 /* All Rights Reserved */
31 /*
32 * Portions of this source code were derived from Berkeley
33 * 4.3 BSD under license from the Regents of the University of
34 * California.
35 */
36
37 /*
38 * interface to rpcbind rpc service.
39 */
40
41 #include "mt.h"
42 #include "rpc_mt.h"
43 #include <assert.h>
44 #include <rpc/rpc.h>
45 #include <rpc/rpcb_prot.h>
46 #include <netconfig.h>
47 #include <netdir.h>
48 #include <rpc/nettype.h>
49 #include <syslog.h>
50 #ifdef PORTMAP
51 #include <netinet/in.h> /* FOR IPPROTO_TCP/UDP definitions */
52 #include <rpc/pmap_prot.h>
53 #endif
54 #include <sys/utsname.h>
55 #include <errno.h>
56 #include <stdlib.h>
57 #include <string.h>
58 #include <unistd.h>
59
60 static struct timeval tottimeout = { 60, 0 };
61 static const struct timeval rmttimeout = { 3, 0 };
62 static struct timeval rpcbrmttime = { 15, 0 };
63
64 extern bool_t xdr_wrapstring(XDR *, char **);
65
66 static const char nullstring[] = "\000";
67
68 extern CLIENT *_clnt_tli_create_timed(int, const struct netconfig *,
69 struct netbuf *, rpcprog_t, rpcvers_t, uint_t, uint_t,
70 const struct timeval *);
71
72 static CLIENT *_getclnthandle_timed(char *, struct netconfig *, char **,
73 struct timeval *);
74
75
76 /*
77 * The life time of a cached entry should not exceed 5 minutes
78 * since automountd attempts an unmount every 5 minutes.
79 * It is arbitrarily set a little lower (3 min = 180 sec)
80 * to reduce the time during which an entry is stale.
81 */
82 #define CACHE_TTL 180
83 #define CACHESIZE 6
84
85 struct address_cache {
86 char *ac_host;
87 char *ac_netid;
88 char *ac_uaddr;
89 struct netbuf *ac_taddr;
90 struct address_cache *ac_next;
91 time_t ac_maxtime;
92 };
93
94 static struct address_cache *front;
95 static int cachesize;
96
97 extern int lowvers;
98 extern int authdes_cachesz;
99 /*
100 * This routine adjusts the timeout used for calls to the remote rpcbind.
101 * Also, this routine can be used to set the use of portmapper version 2
102 * only when doing rpc_broadcasts
103 * These are private routines that may not be provided in future releases.
104 */
105 bool_t
106 __rpc_control(int request, void *info)
107 {
108 switch (request) {
109 case CLCR_GET_RPCB_TIMEOUT:
110 *(struct timeval *)info = tottimeout;
111 break;
112 case CLCR_SET_RPCB_TIMEOUT:
113 tottimeout = *(struct timeval *)info;
114 break;
115 case CLCR_GET_LOWVERS:
116 *(int *)info = lowvers;
117 break;
118 case CLCR_SET_LOWVERS:
119 lowvers = *(int *)info;
120 break;
121 case CLCR_GET_RPCB_RMTTIME:
122 *(struct timeval *)info = rpcbrmttime;
123 break;
124 case CLCR_SET_RPCB_RMTTIME:
125 rpcbrmttime = *(struct timeval *)info;
126 break;
127 case CLCR_GET_CRED_CACHE_SZ:
128 *(int *)info = authdes_cachesz;
129 break;
130 case CLCR_SET_CRED_CACHE_SZ:
131 authdes_cachesz = *(int *)info;
132 break;
133 default:
134 return (FALSE);
135 }
136 return (TRUE);
137 }
138
139 /*
140 * It might seem that a reader/writer lock would be more reasonable here.
141 * However because getclnthandle(), the only user of the cache functions,
142 * may do a delete_cache() operation if a check_cache() fails to return an
143 * address useful to clnt_tli_create(), we may as well use a mutex.
144 */
145 /*
146 * As it turns out, if the cache lock is *not* a reader/writer lock, we will
147 * block all clnt_create's if we are trying to connect to a host that's down,
148 * since the lock will be held all during that time.
149 */
150 extern rwlock_t rpcbaddr_cache_lock;
151
152 /*
153 * The routines check_cache(), add_cache(), delete_cache() manage the
154 * cache of rpcbind addresses for (host, netid).
155 */
156
157 static struct address_cache *
158 check_cache(char *host, char *netid)
159 {
160 struct address_cache *cptr;
161
162 /* READ LOCK HELD ON ENTRY: rpcbaddr_cache_lock */
163
164 assert(RW_READ_HELD(&rpcbaddr_cache_lock));
165 for (cptr = front; cptr != NULL; cptr = cptr->ac_next) {
166 if ((strcmp(cptr->ac_host, host) == 0) &&
167 (strcmp(cptr->ac_netid, netid) == 0) &&
168 (time(NULL) <= cptr->ac_maxtime)) {
169 return (cptr);
170 }
171 }
172 return (NULL);
173 }
174
175 static void
176 delete_cache(struct netbuf *addr)
177 {
178 struct address_cache *cptr, *prevptr = NULL;
179
180 /* WRITE LOCK HELD ON ENTRY: rpcbaddr_cache_lock */
181 assert(RW_WRITE_HELD(&rpcbaddr_cache_lock));
182 for (cptr = front; cptr != NULL; cptr = cptr->ac_next) {
183 if (!memcmp(cptr->ac_taddr->buf, addr->buf, addr->len)) {
184 free(cptr->ac_host);
185 free(cptr->ac_netid);
186 free(cptr->ac_taddr->buf);
187 free(cptr->ac_taddr);
188 if (cptr->ac_uaddr)
189 free(cptr->ac_uaddr);
190 if (prevptr)
191 prevptr->ac_next = cptr->ac_next;
192 else
193 front = cptr->ac_next;
194 free(cptr);
195 cachesize--;
196 break;
197 }
198 prevptr = cptr;
199 }
200 }
201
202 static void
203 add_cache(char *host, char *netid, struct netbuf *taddr, char *uaddr)
204 {
205 struct address_cache *ad_cache, *cptr, *prevptr;
206
207 ad_cache = malloc(sizeof (struct address_cache));
208 if (!ad_cache) {
209 goto memerr;
210 }
211 ad_cache->ac_maxtime = time(NULL) + CACHE_TTL;
212 ad_cache->ac_host = strdup(host);
213 ad_cache->ac_netid = strdup(netid);
214 ad_cache->ac_uaddr = uaddr ? strdup(uaddr) : NULL;
215 ad_cache->ac_taddr = malloc(sizeof (struct netbuf));
216 if (!ad_cache->ac_host || !ad_cache->ac_netid || !ad_cache->ac_taddr ||
217 (uaddr && !ad_cache->ac_uaddr)) {
218 goto memerr1;
219 }
220
221 ad_cache->ac_taddr->len = ad_cache->ac_taddr->maxlen = taddr->len;
222 ad_cache->ac_taddr->buf = malloc(taddr->len);
223 if (ad_cache->ac_taddr->buf == NULL) {
224 goto memerr1;
225 }
226
227 (void) memcpy(ad_cache->ac_taddr->buf, taddr->buf, taddr->len);
228
229 /* VARIABLES PROTECTED BY rpcbaddr_cache_lock: cptr */
230
231 (void) rw_wrlock(&rpcbaddr_cache_lock);
232 if (cachesize < CACHESIZE) {
233 ad_cache->ac_next = front;
234 front = ad_cache;
235 cachesize++;
236 } else {
237 /* Free the last entry */
238 cptr = front;
239 prevptr = NULL;
240 while (cptr->ac_next) {
241 prevptr = cptr;
242 cptr = cptr->ac_next;
243 }
244
245 free(cptr->ac_host);
246 free(cptr->ac_netid);
247 free(cptr->ac_taddr->buf);
248 free(cptr->ac_taddr);
249 if (cptr->ac_uaddr)
250 free(cptr->ac_uaddr);
251
252 if (prevptr) {
253 prevptr->ac_next = NULL;
254 ad_cache->ac_next = front;
255 front = ad_cache;
256 } else {
257 front = ad_cache;
258 ad_cache->ac_next = NULL;
259 }
260 free(cptr);
261 }
262 (void) rw_unlock(&rpcbaddr_cache_lock);
263 return;
264 memerr1:
265 if (ad_cache->ac_host)
266 free(ad_cache->ac_host);
267 if (ad_cache->ac_netid)
268 free(ad_cache->ac_netid);
269 if (ad_cache->ac_uaddr)
270 free(ad_cache->ac_uaddr);
271 if (ad_cache->ac_taddr)
272 free(ad_cache->ac_taddr);
273 free(ad_cache);
274 memerr:
275 syslog(LOG_ERR, "add_cache : out of memory.");
276 }
277
278 /*
279 * This routine will return a client handle that is connected to the
280 * rpcbind. Returns NULL on error and free's everything.
281 */
282 static CLIENT *
283 getclnthandle(char *host, struct netconfig *nconf, char **targaddr)
284 {
285 return (_getclnthandle_timed(host, nconf, targaddr, NULL));
286 }
287
288 /*
289 * Same as getclnthandle() except it takes an extra timeout argument.
290 * This is for bug 4049792: clnt_create_timed does not timeout.
291 *
292 * If tp is NULL, use default timeout to get a client handle.
293 */
294 static CLIENT *
295 _getclnthandle_timed(char *host, struct netconfig *nconf, char **targaddr,
296 struct timeval *tp)
297 {
298 CLIENT *client = NULL;
299 struct netbuf *addr;
300 struct netbuf addr_to_delete;
301 struct nd_addrlist *nas;
302 struct nd_hostserv rpcbind_hs;
303 struct address_cache *ad_cache;
304 char *tmpaddr;
305 int neterr;
306 int j;
307
308 /* VARIABLES PROTECTED BY rpcbaddr_cache_lock: ad_cache */
309
310 /* Get the address of the rpcbind. Check cache first */
311 addr_to_delete.len = 0;
312 (void) rw_rdlock(&rpcbaddr_cache_lock);
313 ad_cache = check_cache(host, nconf->nc_netid);
314 if (ad_cache != NULL) {
315 addr = ad_cache->ac_taddr;
316 client = _clnt_tli_create_timed(RPC_ANYFD, nconf, addr,
317 RPCBPROG, RPCBVERS4, 0, 0, tp);
318 if (client != NULL) {
319 if (targaddr) {
320 /*
321 * case where a client handle is created
322 * without a targaddr and the handle is
323 * requested with a targaddr
324 */
325 if (ad_cache->ac_uaddr != NULL) {
326 *targaddr = strdup(ad_cache->ac_uaddr);
327 if (*targaddr == NULL) {
328 syslog(LOG_ERR,
329 "_getclnthandle_timed: strdup "
330 "failed.");
331 rpc_createerr.cf_stat =
332 RPC_SYSTEMERROR;
333 (void) rw_unlock(
334 &rpcbaddr_cache_lock);
335 return (NULL);
336 }
337 } else {
338 *targaddr = NULL;
339 }
340 }
341 (void) rw_unlock(&rpcbaddr_cache_lock);
342 return (client);
343 }
344 if (rpc_createerr.cf_stat == RPC_SYSTEMERROR) {
345 (void) rw_unlock(&rpcbaddr_cache_lock);
346 return (NULL);
347 }
348 addr_to_delete.len = addr->len;
349 addr_to_delete.buf = malloc(addr->len);
350 if (addr_to_delete.buf == NULL) {
351 addr_to_delete.len = 0;
352 } else {
353 (void) memcpy(addr_to_delete.buf, addr->buf, addr->len);
354 }
355 }
356 (void) rw_unlock(&rpcbaddr_cache_lock);
357 if (addr_to_delete.len != 0) {
358 /*
359 * Assume this may be due to cache data being
360 * outdated
361 */
362 (void) rw_wrlock(&rpcbaddr_cache_lock);
363 delete_cache(&addr_to_delete);
364 (void) rw_unlock(&rpcbaddr_cache_lock);
365 free(addr_to_delete.buf);
366 }
367 rpcbind_hs.h_host = host;
368 rpcbind_hs.h_serv = "rpcbind";
369
370 if ((neterr = netdir_getbyname(nconf, &rpcbind_hs, &nas)) != 0) {
371 if (neterr == ND_NOHOST)
372 rpc_createerr.cf_stat = RPC_UNKNOWNHOST;
373 else
374 rpc_createerr.cf_stat = RPC_N2AXLATEFAILURE;
375 return (NULL);
376 }
377 /* XXX nas should perhaps be cached for better performance */
378
379 for (j = 0; j < nas->n_cnt; j++) {
380 addr = &(nas->n_addrs[j]);
381 client = _clnt_tli_create_timed(RPC_ANYFD, nconf, addr, RPCBPROG,
382 RPCBVERS4, 0, 0, tp);
383 if (client)
384 break;
385 }
386
387 if (client) {
388 tmpaddr = targaddr ? taddr2uaddr(nconf, addr) : NULL;
389 add_cache(host, nconf->nc_netid, addr, tmpaddr);
390 if (targaddr) {
391 *targaddr = tmpaddr;
392 }
393 }
394 netdir_free((char *)nas, ND_ADDRLIST);
395 return (client);
396 }
397
398 /*
399 * This routine will return a client handle that is connected to the local
400 * rpcbind. Returns NULL on error.
401 */
402 static CLIENT *
403 local_rpcb(void)
404 {
405 static struct netconfig *loopnconf;
406 static char *hostname;
407 extern mutex_t loopnconf_lock;
408
409 /* VARIABLES PROTECTED BY loopnconf_lock: hostname loopnconf */
410 (void) mutex_lock(&loopnconf_lock);
411 if (loopnconf == NULL) {
412 struct utsname utsname;
413 struct netconfig *nconf, *tmpnconf = NULL;
414 void *nc_handle;
415
416 if (hostname == NULL) {
417 #if defined(__i386) && !defined(__amd64)
418 if ((_nuname(&utsname) == -1) ||
419 ((hostname = strdup(utsname.nodename)) == NULL)) {
420 #else
421 if ((uname(&utsname) == -1) ||
422 ((hostname = strdup(utsname.nodename)) == NULL)) {
423 #endif
424 syslog(LOG_ERR, "local_rpcb : strdup failed.");
425 rpc_createerr.cf_stat = RPC_UNKNOWNHOST;
426 (void) mutex_unlock(&loopnconf_lock);
427 return (NULL);
428 }
429 /* hostname is never freed */
430 }
431 nc_handle = setnetconfig();
432 if (nc_handle == NULL) {
433 /* fails to open netconfig file */
434 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
435 (void) mutex_unlock(&loopnconf_lock);
436 return (NULL);
437 }
438 while (nconf = getnetconfig(nc_handle)) {
439 if (strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0) {
440 tmpnconf = nconf;
441 if (nconf->nc_semantics == NC_TPI_CLTS)
442 break;
443 }
444 }
445 if (tmpnconf == NULL) {
446 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
447 (void) mutex_unlock(&loopnconf_lock);
448 return (NULL);
449 }
450 loopnconf = getnetconfigent(tmpnconf->nc_netid);
451 /* loopnconf is never freed */
452 (void) endnetconfig(nc_handle);
453 }
454 (void) mutex_unlock(&loopnconf_lock);
455 return (getclnthandle(hostname, loopnconf, NULL));
456 }
457
458 /*
459 * Set a mapping between program, version and address.
460 * Calls the rpcbind service to do the mapping.
461 */
462 bool_t
463 rpcb_set(const rpcprog_t program, const rpcvers_t version,
464 const struct netconfig *nconf, const struct netbuf *address)
465 {
466 CLIENT *client;
467 bool_t rslt = FALSE;
468 RPCB parms;
469 char uidbuf[32];
470
471 /* parameter checking */
472 if (nconf == NULL) {
473 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
474 return (FALSE);
475 }
476 if (address == NULL) {
477 rpc_createerr.cf_stat = RPC_UNKNOWNADDR;
478 return (FALSE);
479 }
480 client = local_rpcb();
481 if (!client)
482 return (FALSE);
483
484 parms.r_addr = taddr2uaddr((struct netconfig *)nconf,
485 (struct netbuf *)address); /* convert to universal */
486 if (!parms.r_addr) {
487 rpc_createerr.cf_stat = RPC_N2AXLATEFAILURE;
488 return (FALSE); /* no universal address */
489 }
490 parms.r_prog = program;
491 parms.r_vers = version;
492 parms.r_netid = nconf->nc_netid;
493 /*
494 * Though uid is not being used directly, we still send it for
495 * completeness. For non-unix platforms, perhaps some other
496 * string or an empty string can be sent.
497 */
498 (void) sprintf(uidbuf, "%d", (int)geteuid());
499 parms.r_owner = uidbuf;
500
501 CLNT_CALL(client, RPCBPROC_SET, (xdrproc_t)xdr_rpcb, (char *)&parms,
502 (xdrproc_t)xdr_bool, (char *)&rslt, tottimeout);
503
504 CLNT_DESTROY(client);
505 free(parms.r_addr);
506 return (rslt);
507 }
508
509 /*
510 * Remove the mapping between program, version and netbuf address.
511 * Calls the rpcbind service to do the un-mapping.
512 * If netbuf is NULL, unset for all the transports, otherwise unset
513 * only for the given transport.
514 */
515 bool_t
516 rpcb_unset(const rpcprog_t program, const rpcvers_t version,
517 const struct netconfig *nconf)
518 {
519 CLIENT *client;
520 bool_t rslt = FALSE;
521 RPCB parms;
522 char uidbuf[32];
523
524 client = local_rpcb();
525 if (!client)
526 return (FALSE);
527
528 parms.r_prog = program;
529 parms.r_vers = version;
530 if (nconf)
531 parms.r_netid = nconf->nc_netid;
532 else
533 parms.r_netid = (char *)&nullstring[0]; /* unsets all */
534 parms.r_addr = (char *)&nullstring[0];
535 (void) sprintf(uidbuf, "%d", (int)geteuid());
536 parms.r_owner = uidbuf;
537
538 CLNT_CALL(client, RPCBPROC_UNSET, (xdrproc_t)xdr_rpcb, (char *)&parms,
539 (xdrproc_t)xdr_bool, (char *)&rslt, tottimeout);
540
541 CLNT_DESTROY(client);
542 return (rslt);
543 }
544
545 /*
546 * From the merged list, find the appropriate entry
547 */
548 static struct netbuf *
549 got_entry(rpcb_entry_list_ptr relp, struct netconfig *nconf)
550 {
551 struct netbuf *na = NULL;
552 rpcb_entry_list_ptr sp;
553 rpcb_entry *rmap;
554
555 for (sp = relp; sp != NULL; sp = sp->rpcb_entry_next) {
556 rmap = &sp->rpcb_entry_map;
557 if ((strcmp(nconf->nc_proto, rmap->r_nc_proto) == 0) &&
558 (strcmp(nconf->nc_protofmly, rmap->r_nc_protofmly) == 0) &&
559 (nconf->nc_semantics == rmap->r_nc_semantics) &&
560 (rmap->r_maddr != NULL) && (rmap->r_maddr[0] != NULL)) {
561 na = uaddr2taddr(nconf, rmap->r_maddr);
562 break;
563 }
564 }
565 return (na);
566 }
567
568 /*
569 * Quick check to see if rpcbind is up. Tries to connect over
570 * local transport.
571 */
572 bool_t
573 __rpcbind_is_up(void)
574 {
575 struct utsname name;
576 char uaddr[SYS_NMLN];
577 struct netbuf *addr;
578 int fd;
579 struct t_call *sndcall;
580 struct netconfig *netconf;
581 bool_t res;
582
583 #if defined(__i386) && !defined(__amd64)
584 if (_nuname(&name) == -1)
585 #else
586 if (uname(&name) == -1)
587 #endif
588 return (TRUE);
589
590 if ((fd = t_open("/dev/ticotsord", O_RDWR, NULL)) == -1)
591 return (TRUE);
592
593 if (t_bind(fd, NULL, NULL) == -1) {
594 (void) t_close(fd);
595 return (TRUE);
596 }
597
598 /* LINTED pointer cast */
599 if ((sndcall = (struct t_call *)t_alloc(fd, T_CALL, 0)) == NULL) {
600 (void) t_close(fd);
601 return (TRUE);
602 }
603
604 uaddr[0] = '\0';
605 (void) strcpy(uaddr, name.nodename);
606 (void) strcat(uaddr, ".rpc");
607 if ((netconf = getnetconfigent("ticotsord")) == NULL) {
608 (void) t_free((char *)sndcall, T_CALL);
609 (void) t_close(fd);
610 return (FALSE);
611 }
612 addr = uaddr2taddr(netconf, uaddr);
613 freenetconfigent(netconf);
614 if (addr == NULL || addr->buf == NULL) {
615 if (addr)
616 free(addr);
617 (void) t_free((char *)sndcall, T_CALL);
618 (void) t_close(fd);
619 return (FALSE);
620 }
621 sndcall->addr.maxlen = addr->maxlen;
622 sndcall->addr.len = addr->len;
623 sndcall->addr.buf = addr->buf;
624
625 if (t_connect(fd, sndcall, NULL) == -1)
626 res = FALSE;
627 else
628 res = TRUE;
629
630 sndcall->addr.maxlen = sndcall->addr.len = 0;
631 sndcall->addr.buf = NULL;
632 (void) t_free((char *)sndcall, T_CALL);
633 free(addr->buf);
634 free(addr);
635 (void) t_close(fd);
636
637 return (res);
638 }
639
640
641 /*
642 * An internal function which optimizes rpcb_getaddr function. It returns
643 * the universal address of the remote service or NULL. It also optionally
644 * returns the client handle that it uses to contact the remote rpcbind.
645 * The caller will re-purpose the client to contact the remote service.
646 *
647 * The algorithm used: First try version 4. Then try version 3 (svr4).
648 * Finally, if the transport is TCP or UDP, try version 2 (portmap).
649 * Version 4 is now available with all current systems on the network.
650 * With this algorithm, we get performance as well as a plan for
651 * obsoleting version 2.
652 *
653 * XXX: Due to some problems with t_connect(), we do not reuse the same client
654 * handle for COTS cases and hence in these cases we do not return the
655 * client handle. This code will change if t_connect() ever
656 * starts working properly. Also look under clnt_vc.c.
657 */
658 struct netbuf *
659 __rpcb_findaddr_timed(rpcprog_t program, rpcvers_t version,
660 struct netconfig *nconf, char *host, CLIENT **clpp, struct timeval *tp)
661 {
662 static bool_t check_rpcbind = TRUE;
663 CLIENT *client = NULL;
664 RPCB parms;
665 enum clnt_stat clnt_st;
666 char *ua = NULL;
667 uint_t vers;
668 struct netbuf *address = NULL;
669 void *handle;
670 rpcb_entry_list_ptr relp = NULL;
671 bool_t tmp_client = FALSE;
672
673 /* parameter checking */
674 if (nconf == NULL) {
675 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
676 return (NULL);
677 }
678
679 parms.r_addr = NULL;
680
681 /*
682 * Use default total timeout if no timeout is specified.
683 */
684 if (tp == NULL)
685 tp = &tottimeout;
686
687 /*
688 * Check if rpcbind is up. This prevents needless delays when
689 * accessing applications such as the keyserver while booting
690 * disklessly.
691 */
692 if (check_rpcbind && strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0) {
693 if (!__rpcbind_is_up()) {
694 rpc_createerr.cf_stat = RPC_PMAPFAILURE;
695 rpc_createerr.cf_error.re_errno = 0;
696 rpc_createerr.cf_error.re_terrno = 0;
697 goto error;
698 }
699 check_rpcbind = FALSE;
700 }
701
702 /*
703 * First try version 4.
704 */
705 parms.r_prog = program;
706 parms.r_vers = version;
707 parms.r_owner = (char *)&nullstring[0]; /* not needed; */
708 /* just for xdring */
709 parms.r_netid = nconf->nc_netid; /* not really needed */
710
711 /*
712 * If a COTS transport is being used, try getting address via CLTS
713 * transport. This works only with version 4.
714 */
715 if (nconf->nc_semantics == NC_TPI_COTS_ORD ||
716 nconf->nc_semantics == NC_TPI_COTS) {
717 tmp_client = TRUE;
718 handle = __rpc_setconf("datagram_v");
719 } else {
720 handle = __rpc_setconf(nconf->nc_proto);
721 }
722
723 if (handle != NULL) {
724 struct netconfig *nconf_clts;
725
726 while ((nconf_clts = __rpc_getconf(handle)) != NULL) {
727 if (strcmp(nconf_clts->nc_protofmly,
728 nconf->nc_protofmly) != 0) {
729 continue;
730 }
731 client = _getclnthandle_timed(host, nconf_clts,
732 &parms.r_addr, tp);
733 break;
734 }
735 __rpc_endconf(handle);
736 }
737 if (client != NULL) {
738
739 /* Set rpcbind version 4 */
740 vers = RPCBVERS4;
741 CLNT_CONTROL(client, CLSET_VERS, (char *)&vers);
742
743 /*
744 * We also send the remote system the address we used to
745 * contact it in case it can help it connect back with us
746 */
747 if (parms.r_addr == NULL) {
748 parms.r_addr = strdup(""); /* for XDRing */
749 if (parms.r_addr == NULL) {
750 syslog(LOG_ERR, "__rpcb_findaddr_timed: "
751 "strdup failed.");
752 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
753 goto error;
754 }
755 }
756
757 CLNT_CONTROL(client, CLSET_RETRY_TIMEOUT,
758 (char *)&rpcbrmttime);
759
760 clnt_st = CLNT_CALL(client, RPCBPROC_GETADDRLIST,
761 (xdrproc_t)xdr_rpcb, (char *)&parms,
762 (xdrproc_t)xdr_rpcb_entry_list_ptr, (char *)&relp, *tp);
763 switch (clnt_st) {
764 case RPC_SUCCESS: /* Call succeeded */
765 address = got_entry(relp, nconf);
766 xdr_free((xdrproc_t)xdr_rpcb_entry_list_ptr,
767 (char *)&relp);
768 if (address != NULL) {
769 /* Program number and version number matched */
770 goto done;
771 }
772 /* Program and version not found for this transport */
773 /*
774 * XXX: should have returned with RPC_PROGUNAVAIL
775 * or perhaps RPC_PROGNOTREGISTERED error but
776 * since the remote machine might not always be able
777 * to send the address on all transports, we try the
778 * regular way with version 3, then 2
779 */
780 /* Try the next version */
781 break;
782 case RPC_PROGVERSMISMATCH: /* RPC protocol mismatch */
783 clnt_geterr(client, &rpc_createerr.cf_error);
784 if (rpc_createerr.cf_error.re_vers.low > vers) {
785 rpc_createerr.cf_stat = RPC_PROGVERSMISMATCH;
786 goto error; /* a new version, can't handle */
787 }
788 /* Try the next version */
789 break;
790 case RPC_PROCUNAVAIL: /* Procedure unavailable */
791 case RPC_PROGUNAVAIL: /* Program not available */
792 case RPC_TIMEDOUT: /* Call timed out */
793 /* Try the next version */
794 break;
795 default:
796 rpc_createerr.cf_stat = RPC_PMAPFAILURE;
797 clnt_geterr(client, &rpc_createerr.cf_error);
798 goto error;
799 break;
800 }
801
802 } else {
803
804 /* No client */
805 tmp_client = FALSE;
806
807 } /* End of version 4 */
808
809 /* Destroy a temporary client */
810 if (client != NULL && tmp_client) {
811 CLNT_DESTROY(client);
812 client = NULL;
813 free(parms.r_addr);
814 parms.r_addr = NULL;
815 }
816 tmp_client = FALSE;
817
818 /*
819 * Try version 3
820 */
821
822 /* Now the same transport is to be used to get the address */
823 if (client == NULL) {
824 client = _getclnthandle_timed(host, nconf, &parms.r_addr, tp);
825 }
826 address = NULL;
827 if (client != NULL) {
828 if (parms.r_addr == NULL) {
829 parms.r_addr = strdup(""); /* for XDRing */
830 if (parms.r_addr == NULL) {
831 syslog(LOG_ERR, "__rpcb_findaddr_timed: "
832 "strdup failed.");
833 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
834 goto error;
835 }
836 }
837
838 CLNT_CONTROL(client, CLSET_RETRY_TIMEOUT,
839 (char *)&rpcbrmttime);
840 vers = RPCBVERS; /* Set the version */
841 CLNT_CONTROL(client, CLSET_VERS, (char *)&vers);
842 clnt_st = CLNT_CALL(client, RPCBPROC_GETADDR,
843 (xdrproc_t)xdr_rpcb, (char *)&parms,
844 (xdrproc_t)xdr_wrapstring, (char *)&ua, *tp);
845 switch (clnt_st) {
846 case RPC_SUCCESS: /* Call succeeded */
847 if (ua != NULL) {
848 if (ua[0] != '\0') {
849 address = uaddr2taddr(nconf, ua);
850 }
851 xdr_free((xdrproc_t)xdr_wrapstring,
852 (char *)&ua);
853
854 if (address != NULL) {
855 goto done;
856 }
857 /* We don't know about your universal addr */
858 rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
859 goto error;
860 }
861 /* Try the next version */
862 break;
863 case RPC_PROGVERSMISMATCH: /* RPC protocol mismatch */
864 clnt_geterr(client, &rpc_createerr.cf_error);
865 if (rpc_createerr.cf_error.re_vers.low > vers)
866 goto error; /* a new version, can't handle */
867 /* Try the next version */
868 break;
869 case RPC_PROCUNAVAIL: /* Procedure unavailable */
870 case RPC_PROGUNAVAIL: /* Program not available */
871 case RPC_TIMEDOUT: /* Call timed out */
872 /* Try the next version */
873 break;
874 default:
875 clnt_geterr(client, &rpc_createerr.cf_error);
876 rpc_createerr.cf_stat = RPC_PMAPFAILURE;
877 goto error;
878 break;
879 }
880 } /* End of version 3 */
881
882 /*
883 * Try version 2
884 */
885
886 #ifdef PORTMAP
887 /* Try version 2 for TCP or UDP */
888 if (strcmp(nconf->nc_protofmly, NC_INET) == 0) {
889 ushort_t port = 0;
890 struct netbuf remote;
891 uint_t pmapvers = 2;
892 struct pmap pmapparms;
893
894 /*
895 * Try UDP only - there are some portmappers out
896 * there that use UDP only.
897 */
898 if (strcmp(nconf->nc_proto, NC_TCP) == 0) {
899 struct netconfig *newnconf;
900
901 if (client) {
902 CLNT_DESTROY(client);
903 client = NULL;
904 free(parms.r_addr);
905 parms.r_addr = NULL;
906 }
907 if ((handle = __rpc_setconf("udp")) == NULL) {
908 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
909 goto error;
910 }
911
912 /*
913 * The following to reinforce that you can
914 * only request for remote address through
915 * the same transport you are requesting.
916 * ie. requesting unversial address
917 * of IPv4 has to be carried through IPv4.
918 * Can't use IPv6 to send out the request.
919 * The mergeaddr in rpcbind can't handle
920 * this.
921 */
922 for (;;) {
923 if ((newnconf = __rpc_getconf(handle))
924 == NULL) {
925 __rpc_endconf(handle);
926 rpc_createerr.cf_stat =
927 RPC_UNKNOWNPROTO;
928 goto error;
929 }
930 /*
931 * here check the protocol family to
932 * be consistent with the request one
933 */
934 if (strcmp(newnconf->nc_protofmly,
935 nconf->nc_protofmly) == NULL)
936 break;
937 }
938
939 client = _getclnthandle_timed(host, newnconf,
940 &parms.r_addr, tp);
941 __rpc_endconf(handle);
942 tmp_client = TRUE;
943 }
944 if (client == NULL)
945 rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
946 tmp_client = FALSE;
947 goto error;
948
949 /*
950 * Set version and retry timeout.
951 */
952 CLNT_CONTROL(client, CLSET_RETRY_TIMEOUT, (char *)&rpcbrmttime);
953 CLNT_CONTROL(client, CLSET_VERS, (char *)&pmapvers);
954
955 pmapparms.pm_prog = program;
956 pmapparms.pm_vers = version;
957 pmapparms.pm_prot = strcmp(nconf->nc_proto, NC_TCP) ?
958 IPPROTO_UDP : IPPROTO_TCP;
959 pmapparms.pm_port = 0; /* not needed */
960 clnt_st = CLNT_CALL(client, PMAPPROC_GETPORT,
961 (xdrproc_t)xdr_pmap, (caddr_t)&pmapparms,
962 (xdrproc_t)xdr_u_short, (caddr_t)&port, *tp);
963 if (clnt_st != RPC_SUCCESS) {
964 rpc_createerr.cf_stat = RPC_PMAPFAILURE;
965 clnt_geterr(client, &rpc_createerr.cf_error);
966 goto error;
967 } else if (port == 0) {
968 rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
969 goto error;
970 }
971 port = htons(port);
972 CLNT_CONTROL(client, CLGET_SVC_ADDR, (char *)&remote);
973 if (((address = malloc(sizeof (struct netbuf))) == NULL) ||
974 ((address->buf = malloc(remote.len)) == NULL)) {
975 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
976 clnt_geterr(client, &rpc_createerr.cf_error);
977 if (address != NULL) {
978 free(address);
979 address = NULL;
980 }
981 goto error;
982 }
983 (void) memcpy(address->buf, remote.buf, remote.len);
984 (void) memcpy(&address->buf[sizeof (short)], &port,
985 sizeof (short));
986 address->len = address->maxlen = remote.len;
987 goto done;
988 }
989 #endif
990
991 error:
992 /* Return NULL address and NULL client */
993 address = NULL;
994 if (client) {
995 CLNT_DESTROY(client);
996 client = NULL;
997 }
998
999 done:
1000 /* Return an address and optional client */
1001 if (client != NULL && tmp_client) {
1002 /* This client is the temporary one */
1003 CLNT_DESTROY(client);
1004 client = NULL;
1005 }
1006 if (clpp) {
1007 *clpp = client;
1008 } else if (client) {
1009 CLNT_DESTROY(client);
1010 }
1011 if (parms.r_addr)
1012 free(parms.r_addr);
1013 return (address);
1014 }
1015
1016
1017 /*
1018 * Find the mapped address for program, version.
1019 * Calls the rpcbind service remotely to do the lookup.
1020 * Uses the transport specified in nconf.
1021 * Returns FALSE (0) if no map exists, else returns 1.
1022 *
1023 * Assuming that the address is all properly allocated
1024 */
1025 int
1026 rpcb_getaddr(const rpcprog_t program, const rpcvers_t version,
1027 const struct netconfig *nconf, struct netbuf *address, const char *host)
1028 {
1029 struct netbuf *na;
1030
1031 if ((na = __rpcb_findaddr_timed(program, version,
1032 (struct netconfig *)nconf, (char *)host, NULL, NULL)) == NULL)
1033 return (FALSE);
1034
1035 if (na->len > address->maxlen) {
1036 /* Too long address */
1037 netdir_free((char *)na, ND_ADDR);
1038 rpc_createerr.cf_stat = RPC_FAILED;
1039 return (FALSE);
1040 }
1041 (void) memcpy(address->buf, na->buf, (int)na->len);
1042 address->len = na->len;
1043 netdir_free((char *)na, ND_ADDR);
1044 return (TRUE);
1045 }
1046
1047 /*
1048 * Get a copy of the current maps.
1049 * Calls the rpcbind service remotely to get the maps.
1050 *
1051 * It returns only a list of the services
1052 * It returns NULL on failure.
1053 */
1054 rpcblist *
1055 rpcb_getmaps(const struct netconfig *nconf, const char *host)
1056 {
1057 rpcblist_ptr head = NULL;
1058 CLIENT *client;
1059 enum clnt_stat clnt_st;
1060 int vers = 0;
1061
1062 client = getclnthandle((char *)host,
1063 (struct netconfig *)nconf, NULL);
1064 if (client == NULL)
1065 return (NULL);
1066
1067 clnt_st = CLNT_CALL(client, RPCBPROC_DUMP,
1068 (xdrproc_t)xdr_void, NULL,
1069 (xdrproc_t)xdr_rpcblist_ptr,
1070 (char *)&head, tottimeout);
1071 if (clnt_st == RPC_SUCCESS)
1072 goto done;
1073
1074 if ((clnt_st != RPC_PROGVERSMISMATCH) &&
1075 (clnt_st != RPC_PROGUNAVAIL)) {
1076 rpc_createerr.cf_stat = RPC_RPCBFAILURE;
1077 clnt_geterr(client, &rpc_createerr.cf_error);
1078 goto done;
1079 }
1080
1081 /* fall back to earlier version */
1082 CLNT_CONTROL(client, CLGET_VERS, (char *)&vers);
1083 if (vers == RPCBVERS4) {
1084 vers = RPCBVERS;
1085 CLNT_CONTROL(client, CLSET_VERS, (char *)&vers);
1086 if (CLNT_CALL(client, RPCBPROC_DUMP,
1087 (xdrproc_t)xdr_void,
1088 NULL, (xdrproc_t)xdr_rpcblist_ptr,
1089 (char *)&head, tottimeout) == RPC_SUCCESS)
1090 goto done;
1091 }
1092 rpc_createerr.cf_stat = RPC_RPCBFAILURE;
1093 clnt_geterr(client, &rpc_createerr.cf_error);
1094
1095 done:
1096 CLNT_DESTROY(client);
1097 return (head);
1098 }
1099
1100 /*
1101 * rpcbinder remote-call-service interface.
1102 * This routine is used to call the rpcbind remote call service
1103 * which will look up a service program in the address maps, and then
1104 * remotely call that routine with the given parameters. This allows
1105 * programs to do a lookup and call in one step.
1106 */
1107 enum clnt_stat
1108 rpcb_rmtcall(const struct netconfig *nconf, const char *host,
1109 const rpcprog_t prog, const rpcvers_t vers, const rpcproc_t proc,
1110 const xdrproc_t xdrargs, const caddr_t argsp, const xdrproc_t xdrres,
1111 const caddr_t resp, const struct timeval tout, struct netbuf *addr_ptr)
1112 {
1113 CLIENT *client;
1114 enum clnt_stat stat;
1115 struct r_rpcb_rmtcallargs a;
1116 struct r_rpcb_rmtcallres r;
1117 int rpcb_vers;
1118
1119 client = getclnthandle((char *)host, (struct netconfig *)nconf, NULL);
1120 if (client == NULL)
1121 return (RPC_FAILED);
1122 CLNT_CONTROL(client, CLSET_RETRY_TIMEOUT, (char *)&rmttimeout);
1123 a.prog = prog;
1124 a.vers = vers;
1125 a.proc = proc;
1126 a.args.args_val = argsp;
1127 a.xdr_args = xdrargs;
1128 r.addr = NULL;
1129 r.results.results_val = resp;
1130 r.xdr_res = xdrres;
1131
1132 for (rpcb_vers = RPCBVERS4; rpcb_vers >= RPCBVERS; rpcb_vers--) {
1133 CLNT_CONTROL(client, CLSET_VERS, (char *)&rpcb_vers);
1134 stat = CLNT_CALL(client, RPCBPROC_CALLIT,
1135 (xdrproc_t)xdr_rpcb_rmtcallargs, (char *)&a,
1136 (xdrproc_t)xdr_rpcb_rmtcallres, (char *)&r, tout);
1137 if ((stat == RPC_SUCCESS) && (addr_ptr != NULL)) {
1138 struct netbuf *na;
1139
1140 na = uaddr2taddr((struct netconfig *)nconf, r.addr);
1141 if (!na) {
1142 stat = RPC_N2AXLATEFAILURE;
1143 ((struct netbuf *)addr_ptr)->len = 0;
1144 goto error;
1145 }
1146 if (na->len > addr_ptr->maxlen) {
1147 /* Too long address */
1148 stat = RPC_FAILED; /* XXX A better error no */
1149 netdir_free((char *)na, ND_ADDR);
1150 ((struct netbuf *)addr_ptr)->len = 0;
1151 goto error;
1152 }
1153 (void) memcpy(addr_ptr->buf, na->buf, (int)na->len);
1154 ((struct netbuf *)addr_ptr)->len = na->len;
1155 netdir_free((char *)na, ND_ADDR);
1156 break;
1157 }
1158 if ((stat != RPC_PROGVERSMISMATCH) &&
1159 (stat != RPC_PROGUNAVAIL))
1160 goto error;
1161 }
1162 error:
1163 CLNT_DESTROY(client);
1164 if (r.addr)
1165 xdr_free((xdrproc_t)xdr_wrapstring, (char *)&r.addr);
1166 return (stat);
1167 }
1168
1169 /*
1170 * Gets the time on the remote host.
1171 * Returns 1 if succeeds else 0.
1172 */
1173 bool_t
1174 rpcb_gettime(const char *host, time_t *timep)
1175 {
1176 CLIENT *client = NULL;
1177 void *handle;
1178 struct netconfig *nconf;
1179 int vers;
1180 enum clnt_stat st;
1181
1182 if ((host == NULL) || (host[0] == NULL)) {
1183 (void) time(timep);
1184 return (TRUE);
1185 }
1186
1187 if ((handle = __rpc_setconf("netpath")) == NULL) {
1188 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
1189 return (FALSE);
1190 }
1191 rpc_createerr.cf_stat = RPC_SUCCESS;
1192 while (client == NULL) {
1193 if ((nconf = __rpc_getconf(handle)) == NULL) {
1194 if (rpc_createerr.cf_stat == RPC_SUCCESS)
1195 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
1196 break;
1197 }
1198 client = getclnthandle((char *)host, nconf, NULL);
1199 if (client)
1200 break;
1201 }
1202 __rpc_endconf(handle);
1203 if (client == NULL)
1204 return (FALSE);
1205
1206 st = CLNT_CALL(client, RPCBPROC_GETTIME,
1207 (xdrproc_t)xdr_void, NULL,
1208 (xdrproc_t)xdr_time_t, (char *)timep, tottimeout);
1209
1210 if ((st == RPC_PROGVERSMISMATCH) || (st == RPC_PROGUNAVAIL)) {
1211 CLNT_CONTROL(client, CLGET_VERS, (char *)&vers);
1212 if (vers == RPCBVERS4) {
1213 /* fall back to earlier version */
1214 vers = RPCBVERS;
1215 CLNT_CONTROL(client, CLSET_VERS, (char *)&vers);
1216 st = CLNT_CALL(client, RPCBPROC_GETTIME,
1217 (xdrproc_t)xdr_void, NULL,
1218 (xdrproc_t)xdr_time_t, (char *)timep,
1219 tottimeout);
1220 }
1221 }
1222 CLNT_DESTROY(client);
1223 return (st == RPC_SUCCESS? TRUE : FALSE);
1224 }
1225
1226 /*
1227 * Converts taddr to universal address. This routine should never
1228 * really be called because local n2a libraries are always provided.
1229 */
1230 char *
1231 rpcb_taddr2uaddr(struct netconfig *nconf, struct netbuf *taddr)
1232 {
1233 CLIENT *client;
1234 char *uaddr = NULL;
1235
1236 /* parameter checking */
1237 if (nconf == NULL) {
1238 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
1239 return (NULL);
1240 }
1241 if (taddr == NULL) {
1242 rpc_createerr.cf_stat = RPC_UNKNOWNADDR;
1243 return (NULL);
1244 }
1245 client = local_rpcb();
1246 if (!client)
1247 return (NULL);
1248
1249 CLNT_CALL(client, RPCBPROC_TADDR2UADDR, (xdrproc_t)xdr_netbuf,
1250 (char *)taddr, (xdrproc_t)xdr_wrapstring, (char *)&uaddr,
1251 tottimeout);
1252 CLNT_DESTROY(client);
1253 return (uaddr);
1254 }
1255
1256 /*
1257 * Converts universal address to netbuf. This routine should never
1258 * really be called because local n2a libraries are always provided.
1259 */
1260 struct netbuf *
1261 rpcb_uaddr2taddr(struct netconfig *nconf, char *uaddr)
1262 {
1263 CLIENT *client;
1264 struct netbuf *taddr;
1265
1266 /* parameter checking */
1267 if (nconf == NULL) {
1268 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
1269 return (NULL);
1270 }
1271 if (uaddr == NULL) {
1272 rpc_createerr.cf_stat = RPC_UNKNOWNADDR;
1273 return (NULL);
1274 }
1275 client = local_rpcb();
1276 if (!client)
1277 return (NULL);
1278
1279 taddr = calloc(1, sizeof (struct netbuf));
1280 if (taddr == NULL) {
1281 CLNT_DESTROY(client);
1282 return (NULL);
1283 }
1284
1285 if (CLNT_CALL(client, RPCBPROC_UADDR2TADDR, (xdrproc_t)xdr_wrapstring,
1286 (char *)&uaddr, (xdrproc_t)xdr_netbuf, (char *)taddr,
1287 tottimeout) != RPC_SUCCESS) {
1288 free(taddr);
1289 taddr = NULL;
1290 }
1291 CLNT_DESTROY(client);
1292 return (taddr);
1293 }