Debian Codegraph @

extra/tirpc/libtirpc-1.3.5/src/rpcb_clnt.c

raw symbols SCIP
1 /*
2 * Copyright (c) 2010, Oracle America, Inc.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * - Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
9 * - Redistributions in binary form must reproduce the above copyright notice,
10 * this list of conditions and the following disclaimer in the documentation
11 * and/or other materials provided with the distribution.
12 * - Neither the name of the "Oracle America, Inc." nor the names of its
13 * contributors may be used to endorse or promote products derived
14 * from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 /*
30 * rpcb_clnt.c
31 * interface to rpcbind rpc service.
32 */
33 #include <pthread.h>
34 #include <reentrant.h>
35 #include <sys/types.h>
36 #include <sys/socket.h>
37 #include <sys/un.h>
38 #include <sys/utsname.h>
39 #include <rpc/rpc.h>
40 #include <rpc/rpcb_prot.h>
41 #include <rpc/nettype.h>
42 #include <netconfig.h>
43 #ifdef PORTMAP
44 #include <netinet/in.h> /* FOR IPPROTO_TCP/UDP definitions */
45 #include <rpc/pmap_prot.h>
46 #endif /* PORTMAP */
47 #include <stdio.h>
48 #include <errno.h>
49 #include <stdlib.h>
50 #include <string.h>
51 #include <unistd.h>
52 #include <netdb.h>
53 #include <syslog.h>
54 #include <assert.h>
55
56 #include "rpc_com.h"
57 #include "debug.h"
58
59 static struct timeval tottimeout = { 60, 0 };
60 static const struct timeval rmttimeout = { 3, 0 };
61 static struct timeval rpcbrmttime = { 15, 0 };
62
63 extern bool_t xdr_wrapstring(XDR *, char **);
64
65 static const char nullstring[] = "\000";
66
67 #define RPCB_OWNER_STRING "libtirpc"
68
69 #define CACHESIZE 6
70
71 struct address_cache {
72 char *ac_host;
73 char *ac_netid;
74 char *ac_uaddr;
75 struct netbuf *ac_taddr;
76 struct address_cache *ac_next;
77 };
78
79 static struct address_cache *front;
80 static int cachesize;
81
82 #define CLCR_GET_RPCB_TIMEOUT 1
83 #define CLCR_SET_RPCB_TIMEOUT 2
84
85
86 extern int __rpc_lowvers;
87
88 static struct address_cache *copy_of_cached(const char *, char *);
89 static void delete_cache(struct netbuf *);
90 static void add_cache(const char *, const char *, struct netbuf *, char *);
91 static CLIENT *getclnthandle(const char *, const struct netconfig *, char **);
92 static CLIENT *local_rpcb(char **targaddr);
93 #ifdef NOTUSED
94 static struct netbuf *got_entry(rpcb_entry_list_ptr, const struct netconfig *);
95 #endif
96
97 /*
98 * Destroys a cached address entry structure.
99 *
100 */
101 static void
102 destroy_addr(addr)
103 struct address_cache *addr;
104 {
105 if (addr == NULL)
106 return;
107 if (addr->ac_host != NULL) {
108 free(addr->ac_host);
109 addr->ac_host = NULL;
110 }
111 if (addr->ac_netid != NULL) {
112 free(addr->ac_netid);
113 addr->ac_netid = NULL;
114 }
115 if (addr->ac_uaddr != NULL) {
116 free(addr->ac_uaddr);
117 addr->ac_uaddr = NULL;
118 }
119 if (addr->ac_taddr != NULL) {
120 if(addr->ac_taddr->buf != NULL) {
121 free(addr->ac_taddr->buf);
122 addr->ac_taddr->buf = NULL;
123 }
124 free(addr->ac_taddr);
125 addr->ac_taddr = NULL;
126 }
127 free(addr);
128 addr = NULL;
129 }
130
131 /*
132 * Creates an unlinked copy of an address cache entry. If the argument is NULL
133 * or the new entry cannot be allocated then NULL is returned.
134 */
135 static struct address_cache *
136 copy_addr(addr)
137 const struct address_cache *addr;
138 {
139 struct address_cache *copy;
140
141 if (addr == NULL)
142 return (NULL);
143
144 copy = calloc(1, sizeof(*addr));
145 if (copy == NULL)
146 return (NULL);
147
148 if (addr->ac_host != NULL) {
149 copy->ac_host = strdup(addr->ac_host);
150 if (copy->ac_host == NULL)
151 goto err;
152 }
153 if (addr->ac_netid != NULL) {
154 copy->ac_netid = strdup(addr->ac_netid);
155 if (copy->ac_netid == NULL)
156 goto err;
157 }
158 if (addr->ac_uaddr != NULL) {
159 copy->ac_uaddr = strdup(addr->ac_uaddr);
160 if (copy->ac_uaddr == NULL)
161 goto err;
162 }
163
164 if (addr->ac_taddr == NULL)
165 return (copy);
166
167 copy->ac_taddr = calloc(1, sizeof(*addr->ac_taddr));
168 if (copy->ac_taddr == NULL)
169 goto err;
170
171 memcpy(copy->ac_taddr, addr->ac_taddr, sizeof(*addr->ac_taddr));
172 copy->ac_taddr->buf = malloc(addr->ac_taddr->len);
173 if (copy->ac_taddr->buf == NULL)
174 goto err;
175
176 memcpy(copy->ac_taddr->buf, addr->ac_taddr->buf, addr->ac_taddr->len);
177 return (copy);
178
179 err:
180 destroy_addr(copy);
181 return (NULL);
182 }
183
184 /*
185 * This routine adjusts the timeout used for calls to the remote rpcbind.
186 * Also, this routine can be used to set the use of portmapper version 2
187 * only when doing rpc_broadcasts
188 * These are private routines that may not be provided in future releases.
189 */
190 bool_t
191 __rpc_control(request, info)
192 int request;
193 void *info;
194 {
195 switch (request) {
196 case CLCR_GET_RPCB_TIMEOUT:
197 *(struct timeval *)info = tottimeout;
198 break;
199 case CLCR_SET_RPCB_TIMEOUT:
200 tottimeout = *(struct timeval *)info;
201 break;
202 case CLCR_SET_LOWVERS:
203 __rpc_lowvers = *(int *)info;
204 break;
205 case CLCR_GET_LOWVERS:
206 *(int *)info = __rpc_lowvers;
207 break;
208 default:
209 return (FALSE);
210 }
211 return (TRUE);
212 }
213
214 /*
215 * Protect against concurrent access to the address cache and modifications
216 * (esp. deletions) of cache entries.
217 *
218 * Previously a bidirectional R/W lock was used. However, R/W locking is
219 * dangerous as it allows concurrent modification (e.g. deletion with write
220 * lock) at the same time as the deleted element is accessed via check_cache()
221 * and a read lock). We absolutely need a single mutex for all access to
222 * prevent cache corruption. If the mutexing is restricted to only the
223 * relevant code sections, deadlocking should be avoided even with recursed
224 * client creation.
225 */
226 extern pthread_mutex_t rpcbaddr_cache_lock;
227
228 /*
229 * The routines check_cache(), add_cache(), delete_cache() manage the
230 * cache of rpcbind addresses for (host, netid).
231 */
232
233 static struct address_cache *
234 copy_of_cached(host, netid)
235 const char *host;
236 char *netid;
237 {
238 struct address_cache *cptr, *copy = NULL;
239
240 mutex_lock(&rpcbaddr_cache_lock);
241 for (cptr = front; cptr != NULL; cptr = cptr->ac_next) {
242 if (!strcmp(cptr->ac_host, host) &&
243 !strcmp(cptr->ac_netid, netid)) {
244 LIBTIRPC_DEBUG(3, ("check_cache: Found cache entry for %s: %s\n",
245 host, netid));
246 copy = copy_addr(cptr);
247 break;
248 }
249 }
250 mutex_unlock(&rpcbaddr_cache_lock);
251 return copy;
252 }
253
254 static void
255 delete_cache(addr)
256 struct netbuf *addr;
257 {
258 struct address_cache *cptr = NULL, *prevptr = NULL;
259
260 /* LOCK HELD ON ENTRY: rpcbaddr_cache_lock */
261 mutex_lock(&rpcbaddr_cache_lock);
262
263 for (cptr = front; cptr != NULL; cptr = cptr->ac_next) {
264 if (!memcmp(cptr->ac_taddr->buf, addr->buf, addr->len)) {
265 /* Unlink from cache. We'll destroy it after releasing the mutex. */
266 if (cptr->ac_uaddr) {
267 free(cptr->ac_uaddr);
268 cptr->ac_uaddr = NULL;
269 }
270 if (prevptr) {
271 prevptr->ac_next = cptr->ac_next;
272 } else {
273 front = cptr->ac_next;
274 }
275 cachesize--;
276 break;
277 }
278 prevptr = cptr;
279 }
280
281 mutex_unlock(&rpcbaddr_cache_lock);
282 destroy_addr(cptr);
283 }
284
285 static void
286 add_cache(host, netid, taddr, uaddr)
287 const char *host, *netid;
288 char *uaddr;
289 struct netbuf *taddr;
290 {
291 struct address_cache *ad_cache, *cptr, *prevptr;
292
293 ad_cache = (struct address_cache *)
294 malloc(sizeof (struct address_cache));
295 if (!ad_cache) {
296 return;
297 }
298 ad_cache->ac_host = strdup(host);
299 ad_cache->ac_netid = strdup(netid);
300 ad_cache->ac_uaddr = uaddr ? strdup(uaddr) : NULL;
301 ad_cache->ac_taddr = (struct netbuf *)malloc(sizeof (struct netbuf));
302 if (!ad_cache->ac_host || !ad_cache->ac_netid || !ad_cache->ac_taddr ||
303 (uaddr && !ad_cache->ac_uaddr))
304 goto out_free;
305 ad_cache->ac_taddr->len = ad_cache->ac_taddr->maxlen = taddr->len;
306 ad_cache->ac_taddr->buf = (char *) malloc(taddr->len);
307 if (ad_cache->ac_taddr->buf == NULL)
308 goto out_free;
309 memcpy(ad_cache->ac_taddr->buf, taddr->buf, taddr->len);
310 LIBTIRPC_DEBUG(3, ("add_cache: Added to cache: %s : %s\n", host, netid));
311
312 /* VARIABLES PROTECTED BY rpcbaddr_cache_lock: cptr */
313
314 mutex_lock(&rpcbaddr_cache_lock);
315 if (cachesize < CACHESIZE) {
316 ad_cache->ac_next = front;
317 front = ad_cache;
318 cachesize++;
319 } else {
320 /* Free the last entry */
321 cptr = front;
322 prevptr = NULL;
323 while (cptr->ac_next) {
324 prevptr = cptr;
325 cptr = cptr->ac_next;
326 }
327
328 LIBTIRPC_DEBUG(3, ("add_cache: Deleted from cache: %s : %s\n",
329 cptr->ac_host, cptr->ac_netid));
330 free(cptr->ac_host);
331 free(cptr->ac_netid);
332 free(cptr->ac_taddr->buf);
333 free(cptr->ac_taddr);
334 if (cptr->ac_uaddr)
335 free(cptr->ac_uaddr);
336
337 if (prevptr) {
338 prevptr->ac_next = NULL;
339 ad_cache->ac_next = front;
340 front = ad_cache;
341 } else {
342 front = ad_cache;
343 ad_cache->ac_next = NULL;
344 }
345 free(cptr);
346 }
347 mutex_unlock(&rpcbaddr_cache_lock);
348 return;
349
350 out_free:
351 free(ad_cache->ac_host);
352 free(ad_cache->ac_netid);
353 free(ad_cache->ac_uaddr);
354 free(ad_cache->ac_taddr);
355 free(ad_cache);
356 }
357
358
359 /*
360 * This routine will return a client handle that is connected to the
361 * rpcbind. If targaddr is non-NULL, the "universal address" of the
362 * host will be stored in *targaddr; the caller is responsible for
363 * freeing this string.
364 * On error, returns NULL and free's everything.
365 */
366 static CLIENT *
367 getclnthandle(host, nconf, targaddr)
368 const char *host;
369 const struct netconfig *nconf;
370 char **targaddr;
371 {
372 CLIENT *client;
373 struct netbuf taddr;
374 struct __rpc_sockinfo si;
375 struct addrinfo hints, *res, *tres;
376 char *tmpaddr;
377
378 if (nconf == NULL) {
379 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
380 return NULL;
381 }
382
383 if (nconf->nc_protofmly != NULL &&
384 strcmp(nconf->nc_protofmly, NC_LOOPBACK) != 0 &&
385 host == NULL) {
386 rpc_createerr.cf_stat = RPC_UNKNOWNHOST;
387 return NULL;
388 }
389
390
391
392 /* Get the address of the rpcbind. Check cache first */
393 client = NULL;
394 if (targaddr)
395 *targaddr = NULL;
396
397 if (host != NULL) {
398 struct address_cache *ad_cache;
399
400 /* Get an MT-safe copy of the cached address (if any) */
401 ad_cache = copy_of_cached(host, nconf->nc_netid);
402 if (ad_cache != NULL) {
403 client = clnt_tli_create(RPC_ANYFD, nconf, ad_cache->ac_taddr,
404 (rpcprog_t)RPCBPROG, (rpcvers_t)RPCBVERS4, 0, 0);
405 if (client != NULL) {
406 if (targaddr && ad_cache->ac_uaddr) {
407 *targaddr = ad_cache->ac_uaddr;
408 ad_cache->ac_uaddr = NULL; /* De-reference before destruction */
409 }
410 destroy_addr(ad_cache);
411 return (client);
412 }
413
414 delete_cache(ad_cache->ac_taddr);
415 destroy_addr(ad_cache);
416 }
417 }
418
419 if (!__rpc_nconf2sockinfo(nconf, &si)) {
420 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
421 assert(client == NULL);
422 goto out_err;
423 }
424
425 memset(&hints, 0, sizeof hints);
426 hints.ai_family = si.si_af;
427 hints.ai_socktype = si.si_socktype;
428 hints.ai_protocol = si.si_proto;
429
430 LIBTIRPC_DEBUG(3, ("getclnthandle: trying netid %s family %d proto %d socktype %d\n",
431 nconf->nc_netid, si.si_af, si.si_proto, si.si_socktype));
432
433 if (nconf->nc_protofmly != NULL && strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0) {
434 client = local_rpcb(targaddr);
435 if (! client) {
436 LIBTIRPC_DEBUG(1, ("getclnthandle: %s",
437 clnt_spcreateerror("local_rpcb failed")));
438 goto out_err;
439 } else {
440 return (client);
441 }
442 } else {
443 if (getaddrinfo(host, "sunrpc", &hints, &res) != 0) {
444 rpc_createerr.cf_stat = RPC_UNKNOWNHOST;
445 assert(client == NULL);
446 goto out_err;
447 }
448 }
449
450 for (tres = res; tres != NULL; tres = tres->ai_next) {
451 taddr.buf = tres->ai_addr;
452 taddr.len = taddr.maxlen = tres->ai_addrlen;
453
454 if (libtirpc_debug_level > 3 && log_stderr) {
455 char *ua;
456 int i;
457
458 ua = taddr2uaddr(nconf, &taddr);
459 fprintf(stderr, "Got it [%s]\n", ua);
460 free(ua);
461
462 fprintf(stderr, "\tnetbuf len = %d, maxlen = %d\n",
463 taddr.len, taddr.maxlen);
464 fprintf(stderr, "\tAddress is ");
465 for (i = 0; i < taddr.len; i++)
466 fprintf(stderr, "%u.", ((char *)(taddr.buf))[i]);
467 fprintf(stderr, "\n");
468 }
469
470 client = clnt_tli_create(RPC_ANYFD, nconf, &taddr,
471 (rpcprog_t)RPCBPROG, (rpcvers_t)RPCBVERS4, 0, 0);
472 if (! client) {
473 LIBTIRPC_DEBUG(1, ("getclnthandle: %s",
474 clnt_spcreateerror("clnt_tli_create failed")));
475 }
476
477 if (client) {
478 tmpaddr = targaddr ? taddr2uaddr(nconf, &taddr) : NULL;
479 if (host)
480 add_cache(host, nconf->nc_netid, &taddr, tmpaddr);
481 if (targaddr)
482 *targaddr = tmpaddr;
483 break;
484 }
485 }
486 if (res)
487 freeaddrinfo(res);
488 out_err:
489 if (client && targaddr &&!*targaddr)
490 fprintf(stderr, "No targaddr provided\n");
491 if (!client && targaddr)
492 free(*targaddr);
493 return (client);
494 }
495
496 /*
497 * Create a PMAP client handle.
498 */
499 static CLIENT *
500 getpmaphandle(nconf, hostname, tgtaddr)
501 const struct netconfig *nconf;
502 const char *hostname;
503 char **tgtaddr;
504 {
505 CLIENT *client = NULL;
506 rpcvers_t pmapvers = 2;
507
508 if (nconf == NULL) {
509 struct netconfig *newnconf;
510
511 if ((newnconf = getnetconfigent("udp")) == NULL) {
512 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
513 return NULL;
514 }
515 client = getclnthandle(hostname, newnconf, tgtaddr);
516 freenetconfigent(newnconf);
517 } else if (strcmp(nconf->nc_proto, NC_UDP) == 0 ||
518 strcmp(nconf->nc_proto, NC_TCP) == 0) {
519 if (strcmp(nconf->nc_protofmly, NC_INET) != 0)
520 return NULL;
521 client = getclnthandle(hostname, nconf, tgtaddr);
522 }
523
524 /* Set version */
525 if (client != NULL)
526 CLNT_CONTROL(client, CLSET_VERS, (char *)&pmapvers);
527
528 return client;
529 }
530
531 /* XXX */
532 #define IN4_LOCALHOST_STRING "127.0.0.1"
533 #define IN6_LOCALHOST_STRING "::1"
534
535 /*
536 * This routine will return a client handle that is connected to the local
537 * rpcbind. Returns NULL on error and free's everything.
538 */
539 static CLIENT *
540 local_rpcb(targaddr)
541 char **targaddr;
542 {
543 CLIENT *client;
544 static struct netconfig *loopnconf;
545 static char *hostname;
546 extern mutex_t loopnconf_lock;
547 int sock;
548 size_t tsize;
549 struct netbuf nbuf;
550 struct sockaddr_un sun;
551 int i;
552
553 /*
554 * Try connecting to the local rpcbind through a local socket
555 * first - trying both addresses. If this doesn't work, try all
556 * non-local transports defined in the netconfig file.
557 */
558 for (i = 0; i < 2; i++) {
559 memset(&sun, 0, sizeof sun);
560 sock = socket(AF_LOCAL, SOCK_STREAM, 0);
561 if (sock < 0)
562 goto try_nconf;
563 sun.sun_family = AF_LOCAL;
564 switch (i) {
565 case 0:
566 memcpy(sun.sun_path, _PATH_RPCBINDSOCK_ABSTRACT,
567 sizeof(_PATH_RPCBINDSOCK_ABSTRACT));
568 break;
569 case 1:
570 strcpy(sun.sun_path, _PATH_RPCBINDSOCK);
571 break;
572 }
573 nbuf.len = SUN_LEN_A(&sun);
574 nbuf.maxlen = sizeof (struct sockaddr_un);
575 nbuf.buf = &sun;
576
577 tsize = __rpc_get_t_size(AF_LOCAL, 0, 0);
578 client = clnt_vc_create(sock, &nbuf, (rpcprog_t)RPCBPROG,
579 (rpcvers_t)RPCBVERS, tsize, tsize);
580
581 if (client != NULL) {
582 /* Mark the socket to be closed in destructor */
583 (void) CLNT_CONTROL(client, CLSET_FD_CLOSE, NULL);
584 if (targaddr) {
585 if (sun.sun_path[0] == 0)
586 sun.sun_path[0] = '@';
587 *targaddr = strdup(sun.sun_path);
588 }
589 return client;
590 }
591
592 /* Nobody needs this socket anymore; free the descriptor. */
593 close(sock);
594 }
595
596 try_nconf:
597
598 /* VARIABLES PROTECTED BY loopnconf_lock: loopnconf */
599 mutex_lock(&loopnconf_lock);
600 if (loopnconf == NULL) {
601 struct netconfig *nconf, *tmpnconf = NULL;
602 void *nc_handle;
603 int fd;
604
605 nc_handle = setnetconfig();
606 if (nc_handle == NULL) {
607 /* fails to open netconfig file */
608 syslog (LOG_ERR, "rpc: failed to open " NETCONFIG);
609 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
610 mutex_unlock(&loopnconf_lock);
611 return (NULL);
612 }
613 while ((nconf = getnetconfig(nc_handle)) != NULL) {
614 #ifdef INET6
615 if ((strcmp(nconf->nc_protofmly, NC_INET6) == 0 ||
616 #else
617 if ((
618 #endif
619 strcmp(nconf->nc_protofmly, NC_INET) == 0) &&
620 (nconf->nc_semantics == NC_TPI_COTS ||
621 nconf->nc_semantics == NC_TPI_COTS_ORD)) {
622 fd = __rpc_nconf2fd(nconf);
623 /*
624 * Can't create a socket, assume that
625 * this family isn't configured in the kernel.
626 */
627 if (fd < 0)
628 continue;
629 close(fd);
630 tmpnconf = nconf;
631 if (!strcmp(nconf->nc_protofmly, NC_INET))
632 hostname = IN4_LOCALHOST_STRING;
633 else
634 hostname = IN6_LOCALHOST_STRING;
635 }
636 }
637 if (tmpnconf == NULL) {
638 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
639 mutex_unlock(&loopnconf_lock);
640 endnetconfig(nc_handle);
641 return (NULL);
642 }
643 loopnconf = getnetconfigent(tmpnconf->nc_netid);
644 /* loopnconf is never freed */
645 endnetconfig(nc_handle);
646 }
647 mutex_unlock(&loopnconf_lock);
648 client = getclnthandle(hostname, loopnconf, targaddr);
649 return (client);
650 }
651
652 /*
653 * Set a mapping between program, version and address.
654 * Calls the rpcbind service to do the mapping.
655 */
656 bool_t
657 rpcb_set(program, version, nconf, address)
658 rpcprog_t program;
659 rpcvers_t version;
660 const struct netconfig *nconf; /* Network structure of transport */
661 const struct netbuf *address; /* Services netconfig address */
662 {
663 CLIENT *client;
664 bool_t rslt = FALSE;
665 RPCB parms;
666 char uidbuf[32];
667
668 /* parameter checking */
669 if (nconf == NULL) {
670 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
671 return (FALSE);
672 }
673 if (address == NULL) {
674 rpc_createerr.cf_stat = RPC_UNKNOWNADDR;
675 return (FALSE);
676 }
677 client = local_rpcb(NULL);
678 if (! client) {
679 return (FALSE);
680 }
681
682 /* convert to universal */
683 /*LINTED const castaway*/
684 parms.r_addr = taddr2uaddr((struct netconfig *) nconf,
685 (struct netbuf *)address);
686 if (!parms.r_addr) {
687 CLNT_DESTROY(client);
688 rpc_createerr.cf_stat = RPC_N2AXLATEFAILURE;
689 return (FALSE); /* no universal address */
690 }
691 parms.r_prog = program;
692 parms.r_vers = version;
693 parms.r_netid = nconf->nc_netid;
694 /*
695 * Though uid is not being used directly, we still send it for
696 * completeness. For non-unix platforms, perhaps some other
697 * string or an empty string can be sent.
698 */
699 (void) snprintf(uidbuf, sizeof uidbuf, "%d", geteuid());
700 parms.r_owner = uidbuf;
701
702 CLNT_CALL(client, (rpcproc_t)RPCBPROC_SET, (xdrproc_t) xdr_rpcb,
703 (char *)&parms, (xdrproc_t) xdr_bool,
704 (char *)&rslt, tottimeout);
705
706 CLNT_DESTROY(client);
707 free(parms.r_addr);
708 return (rslt);
709 }
710
711 /*
712 * Remove the mapping between program, version and netbuf address.
713 * Calls the rpcbind service to do the un-mapping.
714 * If netbuf is NULL, unset for all the transports, otherwise unset
715 * only for the given transport.
716 */
717 bool_t
718 rpcb_unset(program, version, nconf)
719 rpcprog_t program;
720 rpcvers_t version;
721 const struct netconfig *nconf;
722 {
723 CLIENT *client;
724 bool_t rslt = FALSE;
725 RPCB parms;
726 char uidbuf[32];
727
728 client = local_rpcb(NULL);
729 if (! client) {
730 return (FALSE);
731 }
732
733 parms.r_prog = program;
734 parms.r_vers = version;
735 if (nconf)
736 parms.r_netid = nconf->nc_netid;
737 else {
738 /*LINTED const castaway*/
739 parms.r_netid = (char *) &nullstring[0]; /* unsets all */
740 }
741 /*LINTED const castaway*/
742 parms.r_addr = (char *) &nullstring[0];
743 (void) snprintf(uidbuf, sizeof uidbuf, "%d", geteuid());
744 parms.r_owner = uidbuf;
745
746 CLNT_CALL(client, (rpcproc_t)RPCBPROC_UNSET, (xdrproc_t) xdr_rpcb,
747 (char *)(void *)&parms, (xdrproc_t) xdr_bool,
748 (char *)(void *)&rslt, tottimeout);
749
750 CLNT_DESTROY(client);
751 return (rslt);
752 }
753
754 #ifdef NOTUSED
755 /*
756 * From the merged list, find the appropriate entry
757 */
758 static struct netbuf *
759 got_entry(relp, nconf)
760 rpcb_entry_list_ptr relp;
761 const struct netconfig *nconf;
762 {
763 struct netbuf *na = NULL;
764 rpcb_entry_list_ptr sp;
765 rpcb_entry *rmap;
766
767 for (sp = relp; sp != NULL; sp = sp->rpcb_entry_next) {
768 rmap = &sp->rpcb_entry_map;
769 if ((strcmp(nconf->nc_proto, rmap->r_nc_proto) == 0) &&
770 (strcmp(nconf->nc_protofmly, rmap->r_nc_protofmly) == 0) &&
771 (nconf->nc_semantics == rmap->r_nc_semantics) &&
772 (rmap->r_maddr != NULL) && (rmap->r_maddr[0] != 0)) {
773 na = uaddr2taddr(nconf, rmap->r_maddr);
774 LIBTIRPC_DEBUG(3, ("got_entry: Remote address is [%s] %s",
775 rmap->r_maddr, (na ? "Resolvable" : "Not Resolvable")));
776 break;
777 }
778 }
779 return (na);
780 }
781
782 /*
783 * Quick check to see if rpcbind is up. Tries to connect over
784 * local transport - first abstract, then regular.
785 */
786 bool_t
787 __rpcbind_is_up()
788 {
789 struct netconfig *nconf;
790 struct sockaddr_un sun;
791 void *localhandle;
792 int sock;
793
794 nconf = NULL;
795 localhandle = setnetconfig();
796 while ((nconf = getnetconfig(localhandle)) != NULL) {
797 if (nconf->nc_protofmly != NULL &&
798 strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0)
799 break;
800 }
801 if (nconf == NULL)
802 return (FALSE);
803
804 endnetconfig(localhandle);
805
806 memset(&sun, 0, sizeof sun);
807 sock = socket(AF_LOCAL, SOCK_STREAM, 0);
808 if (sock < 0)
809 return (FALSE);
810 sun.sun_family = AF_LOCAL;
811
812 memcpy(sun.sun_path, _PATH_RPCBINDSOCK_ABSTRACT,
813 sizeof(_PATH_RPCBINDSOCK_ABSTRACT));
814 if (connect(sock, (struct sockaddr *)&sun, SUN_LEN_A(&sun)) == 0) {
815 close(sock);
816 return (TRUE);
817 }
818
819 strncpy(sun.sun_path, _PATH_RPCBINDSOCK, sizeof(sun.sun_path));
820 if (connect(sock, (struct sockaddr *)&sun, sizeof(sun)) == 0) {
821 close(sock);
822 return (TRUE);
823 }
824
825 close(sock);
826 return (FALSE);
827 }
828 #endif
829
830 #ifdef PORTMAP
831 static struct netbuf *
832 __try_protocol_version_2(program, version, nconf, host, tp)
833 rpcprog_t program;
834 rpcvers_t version;
835 const struct netconfig *nconf;
836 const char *host;
837 struct timeval *tp;
838 {
839 u_short port = 0;
840 struct netbuf remote;
841 struct pmap pmapparms;
842 CLIENT *client = NULL;
843 enum clnt_stat clnt_st;
844 struct netbuf *pmapaddress;
845 RPCB parms;
846
847 if (strcmp(nconf->nc_proto, NC_UDP) != 0
848 && strcmp(nconf->nc_proto, NC_TCP) != 0)
849 return (NULL);
850
851 client = getpmaphandle(nconf, host, &parms.r_addr);
852 if (client == NULL)
853 goto error;
854
855 /*
856 * Set retry timeout.
857 */
858 CLNT_CONTROL(client, CLSET_RETRY_TIMEOUT, (char *)&rpcbrmttime);
859
860 pmapparms.pm_prog = program;
861 pmapparms.pm_vers = version;
862 pmapparms.pm_prot = strcmp(nconf->nc_proto, NC_TCP) ?
863 IPPROTO_UDP : IPPROTO_TCP;
864 pmapparms.pm_port = 0; /* not needed */
865 clnt_st = CLNT_CALL(client, (rpcproc_t)PMAPPROC_GETPORT,
866 (xdrproc_t) xdr_pmap, (caddr_t)(void *)&pmapparms,
867 (xdrproc_t) xdr_u_short, (caddr_t)(void *)&port,
868 *tp);
869 if (clnt_st != RPC_SUCCESS) {
870 rpc_createerr.cf_stat = RPC_PMAPFAILURE;
871 clnt_geterr(client, &rpc_createerr.cf_error);
872 goto error;
873 } else if (port == 0) {
874 pmapaddress = NULL;
875 rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
876 goto error;
877 }
878 port = htons(port);
879 CLNT_CONTROL(client, CLGET_SVC_ADDR, (char *)&remote);
880 if (((pmapaddress = (struct netbuf *)
881 malloc(sizeof (struct netbuf))) == NULL) ||
882 ((pmapaddress->buf = (char *)
883 malloc(remote.len)) == NULL)) {
884 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
885 clnt_geterr(client, &rpc_createerr.cf_error);
886 if (pmapaddress) {
887 free(pmapaddress);
888 pmapaddress = NULL;
889 }
890 goto error;
891 }
892 memcpy(pmapaddress->buf, remote.buf, remote.len);
893 memcpy(&((char *)pmapaddress->buf)[sizeof (short)],
894 (char *)(void *)&port, sizeof (short));
895 pmapaddress->len = pmapaddress->maxlen = remote.len;
896
897 CLNT_DESTROY(client);
898
899 if (parms.r_addr != NULL && parms.r_addr != nullstring)
900 free(parms.r_addr);
901
902 return pmapaddress;
903
904 error:
905 if (client) {
906 CLNT_DESTROY(client);
907 client = NULL;
908
909 }
910
911 if (parms.r_addr != NULL && parms.r_addr != nullstring)
912 free(parms.r_addr);
913
914 return (NULL);
915
916 }
917 #endif
918
919 /*
920 * An internal function which optimizes rpcb_getaddr function. It also
921 * returns the client handle that it uses to contact the remote rpcbind.
922 *
923 * The algorithm used: If the transports is TCP or UDP, it first tries
924 * version 4 (srv4), then 3 and then fall back to version 2 (portmap).
925 * With this algorithm, we get performance as well as a plan for
926 * obsoleting version 2. This behaviour is reverted to old algorithm
927 * if RPCB_V2FIRST environment var is defined
928 *
929 * For all other transports, the algorithm remains as 4 and then 3.
930 *
931 * XXX: Due to some problems with t_connect(), we do not reuse the same client
932 * handle for COTS cases and hence in these cases we do not return the
933 * client handle. This code will change if t_connect() ever
934 * starts working properly. Also look under clnt_vc.c.
935 */
936 struct netbuf *
937 __rpcb_findaddr_timed(program, version, nconf, host, clpp, tp)
938 rpcprog_t program;
939 rpcvers_t version;
940 const struct netconfig *nconf;
941 const char *host;
942 CLIENT **clpp;
943 struct timeval *tp;
944 {
945 #ifdef NOTUSED
946 static bool_t check_rpcbind = TRUE;
947 #endif
948
949 #ifdef PORTMAP
950 static bool_t portmap_first = FALSE;
951 #endif
952 CLIENT *client = NULL;
953 RPCB parms;
954 enum clnt_stat clnt_st;
955 char *ua = NULL;
956 rpcvers_t vers;
957 struct netbuf *address = NULL;
958 rpcvers_t start_vers = RPCBVERS4;
959 struct netbuf servaddr;
960 struct rpc_err rpcerr;
961
962 /* parameter checking */
963 if (nconf == NULL) {
964 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
965 return (NULL);
966 }
967
968 parms.r_addr = NULL;
969
970 /*
971 * Use default total timeout if no timeout is specified.
972 */
973 if (tp == NULL)
974 tp = &tottimeout;
975
976 parms.r_prog = program;
977 parms.r_vers = version;
978 parms.r_netid = nconf->nc_netid;
979
980 /*
981 * rpcbind ignores the r_owner field in GETADDR requests, but we
982 * need to give xdr_rpcb something to gnaw on. Might as well make
983 * it something human readable for when we see these in captures.
984 */
985 parms.r_owner = RPCB_OWNER_STRING;
986
987 /* Now the same transport is to be used to get the address */
988 if (client && ((nconf->nc_semantics == NC_TPI_COTS_ORD) ||
989 (nconf->nc_semantics == NC_TPI_COTS))) {
990 /* A CLTS type of client - destroy it */
991 CLNT_DESTROY(client);
992 client = NULL;
993 free(parms.r_addr);
994 parms.r_addr = NULL;
995 }
996
997 if (client == NULL) {
998 client = getclnthandle(host, nconf, &parms.r_addr);
999 if (client == NULL) {
1000 goto error;
1001 }
1002 }
1003 if (parms.r_addr == NULL) {
1004 /*LINTED const castaway*/
1005 parms.r_addr = (char *) &nullstring[0];
1006 }
1007
1008 /* First try from start_vers(4) and then version 3 (RPCBVERS), except
1009 * if env. var RPCB_V2FIRST is defined */
1010
1011 #ifdef PORTMAP
1012 if (getenv(V2FIRST)) {
1013 portmap_first = TRUE;
1014 LIBTIRPC_DEBUG(3, ("__rpcb_findaddr_timed: trying v2-port first\n"));
1015 goto portmap;
1016 }
1017 #endif
1018
1019 rpcbind:
1020 CLNT_CONTROL(client, CLSET_RETRY_TIMEOUT, (char *) &rpcbrmttime);
1021 for (vers = start_vers; vers >= RPCBVERS; vers--) {
1022 /* Set the version */
1023 CLNT_CONTROL(client, CLSET_VERS, (char *)(void *)&vers);
1024 clnt_st = CLNT_CALL(client, (rpcproc_t)RPCBPROC_GETADDR,
1025 (xdrproc_t) xdr_rpcb, (char *)(void *)&parms,
1026 (xdrproc_t) xdr_wrapstring, (char *)(void *) &ua, *tp);
1027 switch (clnt_st) {
1028 case RPC_SUCCESS:
1029 if ((ua == NULL) || (ua[0] == 0)) {
1030 /* address unknown */
1031 rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
1032 goto error;
1033 }
1034 address = uaddr2taddr(nconf, ua);
1035 LIBTIRPC_DEBUG(3, ("__rpcb_findaddr_timed: Remote address is [%s] %s",
1036 ua, (address ? "Resolvable" : "Not Resolvable")));
1037
1038 xdr_free((xdrproc_t)xdr_wrapstring,
1039 (char *)(void *)&ua);
1040
1041 if (! address) {
1042 /* We don't know about your universal address */
1043 rpc_createerr.cf_stat = RPC_N2AXLATEFAILURE;
1044 goto error;
1045 }
1046 CLNT_CONTROL(client, CLGET_SVC_ADDR,
1047 (char *)(void *)&servaddr);
1048 __rpc_fixup_addr(address, &servaddr);
1049 goto done;
1050 case RPC_PROGVERSMISMATCH:
1051 clnt_geterr(client, &rpcerr);
1052 if (rpcerr.re_vers.low > RPCBVERS4)
1053 goto error; /* a new version, can't handle */
1054 /* Try the next lower version */
1055 case RPC_PROGUNAVAIL:
1056 case RPC_CANTDECODEARGS:
1057 break;
1058 default:
1059 /* Cant handle this error */
1060 rpc_createerr.cf_stat = clnt_st;
1061 clnt_geterr(client, &rpc_createerr.cf_error);
1062 goto error;
1063 }
1064 }
1065
1066 #ifdef PORTMAP /* Try version 2 for TCP or UDP */
1067 if (portmap_first)
1068 goto error; /* we tried all versions if reached here */
1069 portmap:
1070 if (strcmp(nconf->nc_protofmly, NC_INET) == 0) {
1071 address = __try_protocol_version_2(program, version, nconf, host, tp);
1072 if (address == NULL) {
1073 if (portmap_first)
1074 goto rpcbind;
1075 else
1076 goto error;
1077 }
1078 }
1079 #endif /* PORTMAP */
1080
1081 if ((address == NULL) || (address->len == 0)) {
1082 rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
1083 clnt_geterr(client, &rpc_createerr.cf_error);
1084 }
1085
1086 error:
1087 if (client) {
1088 CLNT_DESTROY(client);
1089 client = NULL;
1090 }
1091 done:
1092 if (nconf->nc_semantics != NC_TPI_CLTS) {
1093 /* This client is the connectionless one */
1094 if (client) {
1095 CLNT_DESTROY(client);
1096 client = NULL;
1097 }
1098 }
1099 if (clpp) {
1100 *clpp = client;
1101 } else if (client) {
1102 CLNT_DESTROY(client);
1103 }
1104 if (parms.r_addr != NULL && parms.r_addr != nullstring)
1105 free(parms.r_addr);
1106 return (address);
1107 }
1108
1109
1110 /*
1111 * Find the mapped address for program, version.
1112 * Calls the rpcbind service remotely to do the lookup.
1113 * Uses the transport specified in nconf.
1114 * Returns FALSE (0) if no map exists, else returns 1.
1115 *
1116 * Assuming that the address is all properly allocated
1117 */
1118 int
1119 rpcb_getaddr(program, version, nconf, address, host)
1120 rpcprog_t program;
1121 rpcvers_t version;
1122 const struct netconfig *nconf;
1123 struct netbuf *address;
1124 const char *host;
1125 {
1126 struct netbuf *na;
1127
1128 if ((na = __rpcb_findaddr_timed(program, version,
1129 (struct netconfig *) nconf, (char *) host,
1130 (CLIENT **) NULL, (struct timeval *) NULL)) == NULL)
1131 return (FALSE);
1132
1133 if (na->len > address->maxlen) {
1134 /* Too long address */
1135 free(na->buf);
1136 free(na);
1137 rpc_createerr.cf_stat = RPC_FAILED;
1138 return (FALSE);
1139 }
1140 memcpy(address->buf, na->buf, (size_t)na->len);
1141 address->len = na->len;
1142 free(na->buf);
1143 free(na);
1144 return (TRUE);
1145 }
1146
1147 /*
1148 * Get a copy of the current maps.
1149 * Calls the rpcbind service remotely to get the maps.
1150 *
1151 * It returns only a list of the services
1152 * It returns NULL on failure.
1153 */
1154 rpcblist *
1155 rpcb_getmaps(nconf, host)
1156 const struct netconfig *nconf;
1157 const char *host;
1158 {
1159 rpcblist_ptr head = NULL;
1160 CLIENT *client;
1161 enum clnt_stat clnt_st;
1162 rpcvers_t vers = 0;
1163
1164 client = getclnthandle(host, nconf, NULL);
1165 if (client == NULL) {
1166 return (head);
1167 }
1168 clnt_st = CLNT_CALL(client, (rpcproc_t)RPCBPROC_DUMP,
1169 (xdrproc_t) xdr_void, NULL, (xdrproc_t) xdr_rpcblist_ptr,
1170 (char *)(void *)&head, tottimeout);
1171 if (clnt_st == RPC_SUCCESS)
1172 goto done;
1173
1174 if ((clnt_st != RPC_PROGVERSMISMATCH) &&
1175 (clnt_st != RPC_PROGUNAVAIL)) {
1176 rpc_createerr.cf_stat = RPC_RPCBFAILURE;
1177 clnt_geterr(client, &rpc_createerr.cf_error);
1178 goto done;
1179 }
1180
1181 /* fall back to earlier version */
1182 CLNT_CONTROL(client, CLGET_VERS, (char *)(void *)&vers);
1183 if (vers == RPCBVERS4) {
1184 vers = RPCBVERS;
1185 CLNT_CONTROL(client, CLSET_VERS, (char *)(void *)&vers);
1186 if (CLNT_CALL(client, (rpcproc_t)RPCBPROC_DUMP,
1187 (xdrproc_t) xdr_void, NULL, (xdrproc_t) xdr_rpcblist_ptr,
1188 (char *)(void *)&head, tottimeout) == RPC_SUCCESS)
1189 goto done;
1190 }
1191 rpc_createerr.cf_stat = RPC_RPCBFAILURE;
1192 clnt_geterr(client, &rpc_createerr.cf_error);
1193
1194 done:
1195 CLNT_DESTROY(client);
1196 return (head);
1197 }
1198
1199 /*
1200 * rpcbinder remote-call-service interface.
1201 * This routine is used to call the rpcbind remote call service
1202 * which will look up a service program in the address maps, and then
1203 * remotely call that routine with the given parameters. This allows
1204 * programs to do a lookup and call in one step.
1205 */
1206 enum clnt_stat
1207 rpcb_rmtcall(nconf, host, prog, vers, proc, xdrargs, argsp,
1208 xdrres, resp, tout, addr_ptr)
1209 const struct netconfig *nconf; /* Netconfig structure */
1210 const char *host; /* Remote host name */
1211 rpcprog_t prog;
1212 rpcvers_t vers;
1213 rpcproc_t proc; /* Remote proc identifiers */
1214 xdrproc_t xdrargs, xdrres; /* XDR routines */
1215 caddr_t argsp, resp; /* Argument and Result */
1216 struct timeval tout; /* Timeout value for this call */
1217 const struct netbuf *addr_ptr; /* Preallocated netbuf address */
1218 {
1219 CLIENT *client;
1220 enum clnt_stat stat;
1221 struct r_rpcb_rmtcallargs a;
1222 struct r_rpcb_rmtcallres r;
1223 rpcvers_t rpcb_vers;
1224
1225 stat = 0;
1226 client = getclnthandle(host, nconf, NULL);
1227 if (client == NULL) {
1228 return (RPC_FAILED);
1229 }
1230 /*LINTED const castaway*/
1231 CLNT_CONTROL(client, CLSET_RETRY_TIMEOUT, (char *)(void *)&rmttimeout);
1232 a.prog = prog;
1233 a.vers = vers;
1234 a.proc = proc;
1235 a.args.args_val = argsp;
1236 a.xdr_args = xdrargs;
1237 r.addr = NULL;
1238 r.results.results_val = resp;
1239 r.xdr_res = xdrres;
1240
1241 for (rpcb_vers = RPCBVERS4; rpcb_vers >= RPCBVERS; rpcb_vers--) {
1242 CLNT_CONTROL(client, CLSET_VERS, (char *)(void *)&rpcb_vers);
1243 stat = CLNT_CALL(client, (rpcproc_t)RPCBPROC_CALLIT,
1244 (xdrproc_t) xdr_rpcb_rmtcallargs, (char *)(void *)&a,
1245 (xdrproc_t) xdr_rpcb_rmtcallres, (char *)(void *)&r, tout);
1246 if ((stat == RPC_SUCCESS) && (addr_ptr != NULL)) {
1247 struct netbuf *na;
1248 /*LINTED const castaway*/
1249 na = uaddr2taddr((struct netconfig *) nconf, r.addr);
1250 if (!na) {
1251 stat = RPC_N2AXLATEFAILURE;
1252 /*LINTED const castaway*/
1253 ((struct netbuf *) addr_ptr)->len = 0;
1254 goto error;
1255 }
1256 if (na->len > addr_ptr->maxlen) {
1257 /* Too long address */
1258 stat = RPC_FAILED; /* XXX A better error no */
1259 free(na->buf);
1260 free(na);
1261 /*LINTED const castaway*/
1262 ((struct netbuf *) addr_ptr)->len = 0;
1263 goto error;
1264 }
1265 memcpy(addr_ptr->buf, na->buf, (size_t)na->len);
1266 /*LINTED const castaway*/
1267 ((struct netbuf *)addr_ptr)->len = na->len;
1268 free(na->buf);
1269 free(na);
1270 break;
1271 } else if ((stat != RPC_PROGVERSMISMATCH) &&
1272 (stat != RPC_PROGUNAVAIL)) {
1273 goto error;
1274 }
1275 }
1276 error:
1277 CLNT_DESTROY(client);
1278 if (r.addr)
1279 xdr_free((xdrproc_t) xdr_wrapstring, (char *)(void *)&r.addr);
1280 return (stat);
1281 }
1282
1283 /*
1284 * Gets the time on the remote host.
1285 * Returns 1 if succeeds else 0.
1286 */
1287 bool_t
1288 rpcb_gettime(host, timep)
1289 const char *host;
1290 time_t *timep;
1291 {
1292 CLIENT *client = NULL;
1293 void *handle;
1294 struct netconfig *nconf;
1295 rpcvers_t vers;
1296 enum clnt_stat st;
1297
1298 if ((host == NULL) || (host[0] == 0)) {
1299 time(timep);
1300 return (TRUE);
1301 }
1302
1303 if ((handle = __rpc_setconf("netpath")) == NULL) {
1304 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
1305 return (FALSE);
1306 }
1307 rpc_createerr.cf_stat = RPC_SUCCESS;
1308 while (client == NULL) {
1309 if ((nconf = __rpc_getconf(handle)) == NULL) {
1310 if (rpc_createerr.cf_stat == RPC_SUCCESS)
1311 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
1312 break;
1313 }
1314 client = getclnthandle(host, nconf, NULL);
1315 if (client)
1316 break;
1317 }
1318 __rpc_endconf(handle);
1319 if (client == (CLIENT *) NULL) {
1320 return (FALSE);
1321 }
1322
1323 st = CLNT_CALL(client, (rpcproc_t)RPCBPROC_GETTIME,
1324 (xdrproc_t) xdr_void, NULL,
1325 (xdrproc_t) xdr_int, (char *)(void *)timep, tottimeout);
1326
1327 if ((st == RPC_PROGVERSMISMATCH) || (st == RPC_PROGUNAVAIL)) {
1328 CLNT_CONTROL(client, CLGET_VERS, (char *)(void *)&vers);
1329 if (vers == RPCBVERS4) {
1330 /* fall back to earlier version */
1331 vers = RPCBVERS;
1332 CLNT_CONTROL(client, CLSET_VERS, (char *)(void *)&vers);
1333 st = CLNT_CALL(client, (rpcproc_t)RPCBPROC_GETTIME,
1334 (xdrproc_t) xdr_void, NULL,
1335 (xdrproc_t) xdr_int, (char *)(void *)timep,
1336 tottimeout);
1337 }
1338 }
1339 CLNT_DESTROY(client);
1340 return (st == RPC_SUCCESS? TRUE: FALSE);
1341 }
1342
1343 /*
1344 * Converts taddr to universal address. This routine should never
1345 * really be called because local n2a libraries are always provided.
1346 */
1347 char *
1348 rpcb_taddr2uaddr(nconf, taddr)
1349 struct netconfig *nconf;
1350 struct netbuf *taddr;
1351 {
1352 CLIENT *client;
1353 char *uaddr = NULL;
1354
1355
1356 /* parameter checking */
1357 if (nconf == NULL) {
1358 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
1359 return (NULL);
1360 }
1361 if (taddr == NULL) {
1362 rpc_createerr.cf_stat = RPC_UNKNOWNADDR;
1363 return (NULL);
1364 }
1365 client = local_rpcb(NULL);
1366 if (! client) {
1367 return (NULL);
1368 }
1369
1370 CLNT_CALL(client, (rpcproc_t)RPCBPROC_TADDR2UADDR,
1371 (xdrproc_t) xdr_netbuf, (char *)(void *)taddr,
1372 (xdrproc_t) xdr_wrapstring, (char *)(void *)&uaddr, tottimeout);
1373 CLNT_DESTROY(client);
1374 return (uaddr);
1375 }
1376
1377 /*
1378 * Converts universal address to netbuf. This routine should never
1379 * really be called because local n2a libraries are always provided.
1380 */
1381 struct netbuf *
1382 rpcb_uaddr2taddr(nconf, uaddr)
1383 struct netconfig *nconf;
1384 char *uaddr;
1385 {
1386 CLIENT *client;
1387 struct netbuf *taddr;
1388
1389
1390 /* parameter checking */
1391 if (nconf == NULL) {
1392 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
1393 return (NULL);
1394 }
1395 if (uaddr == NULL) {
1396 rpc_createerr.cf_stat = RPC_UNKNOWNADDR;
1397 return (NULL);
1398 }
1399 client = local_rpcb(NULL);
1400 if (! client) {
1401 return (NULL);
1402 }
1403
1404 taddr = (struct netbuf *)calloc(1, sizeof (struct netbuf));
1405 if (taddr == NULL) {
1406 CLNT_DESTROY(client);
1407 return (NULL);
1408 }
1409 if (CLNT_CALL(client, (rpcproc_t)RPCBPROC_UADDR2TADDR,
1410 (xdrproc_t) xdr_wrapstring, (char *)(void *)&uaddr,
1411 (xdrproc_t) xdr_netbuf, (char *)(void *)taddr,
1412 tottimeout) != RPC_SUCCESS) {
1413 free(taddr);
1414 taddr = NULL;
1415 }
1416 CLNT_DESTROY(client);
1417 return (taddr);
1418 }