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