]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - lib/libc/rpc/rpcb_clnt.c
MFC r320216: Fix use-after-free introduced in r300388.
[FreeBSD/stable/9.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                         endnetconfig(nc_handle);
513                         rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
514                         mutex_unlock(&loopnconf_lock);
515                         return (NULL);
516                 }
517                 loopnconf = getnetconfigent(tmpnconf->nc_netid);
518                 /* loopnconf is never freed */
519                 endnetconfig(nc_handle);
520         }
521         mutex_unlock(&loopnconf_lock);
522         client = getclnthandle(hostname, loopnconf, NULL);
523         return (client);
524 }
525
526 /*
527  * Set a mapping between program, version and address.
528  * Calls the rpcbind service to do the mapping.
529  */
530 bool_t
531 rpcb_set(program, version, nconf, address)
532         rpcprog_t program;
533         rpcvers_t version;
534         const struct netconfig *nconf;  /* Network structure of transport */
535         const struct netbuf *address;           /* Services netconfig address */
536 {
537         CLIENT *client;
538         bool_t rslt = FALSE;
539         RPCB parms;
540         char uidbuf[32];
541
542         /* parameter checking */
543         if (nconf == NULL) {
544                 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
545                 return (FALSE);
546         }
547         if (address == NULL) {
548                 rpc_createerr.cf_stat = RPC_UNKNOWNADDR;
549                 return (FALSE);
550         }
551         client = local_rpcb();
552         if (! client) {
553                 return (FALSE);
554         }
555
556         /* convert to universal */
557         /*LINTED const castaway*/
558         parms.r_addr = taddr2uaddr((struct netconfig *) nconf,
559                                    (struct netbuf *)address);
560         if (!parms.r_addr) {
561                 CLNT_DESTROY(client);
562                 rpc_createerr.cf_stat = RPC_N2AXLATEFAILURE;
563                 return (FALSE); /* no universal address */
564         }
565         parms.r_prog = program;
566         parms.r_vers = version;
567         parms.r_netid = nconf->nc_netid;
568         /*
569          * Though uid is not being used directly, we still send it for
570          * completeness.  For non-unix platforms, perhaps some other
571          * string or an empty string can be sent.
572          */
573         (void) snprintf(uidbuf, sizeof uidbuf, "%d", geteuid());
574         parms.r_owner = uidbuf;
575
576         CLNT_CALL(client, (rpcproc_t)RPCBPROC_SET, (xdrproc_t) xdr_rpcb,
577             (char *)(void *)&parms, (xdrproc_t) xdr_bool,
578             (char *)(void *)&rslt, tottimeout);
579
580         CLNT_DESTROY(client);
581         free(parms.r_addr);
582         return (rslt);
583 }
584
585 /*
586  * Remove the mapping between program, version and netbuf address.
587  * Calls the rpcbind service to do the un-mapping.
588  * If netbuf is NULL, unset for all the transports, otherwise unset
589  * only for the given transport.
590  */
591 bool_t
592 rpcb_unset(program, version, nconf)
593         rpcprog_t program;
594         rpcvers_t version;
595         const struct netconfig *nconf;
596 {
597         CLIENT *client;
598         bool_t rslt = FALSE;
599         RPCB parms;
600         char uidbuf[32];
601
602         client = local_rpcb();
603         if (! client) {
604                 return (FALSE);
605         }
606
607         parms.r_prog = program;
608         parms.r_vers = version;
609         if (nconf)
610                 parms.r_netid = nconf->nc_netid;
611         else {
612                 /*LINTED const castaway*/
613                 parms.r_netid = (char *) &nullstring[0]; /* unsets  all */
614         }
615         /*LINTED const castaway*/
616         parms.r_addr = (char *) &nullstring[0];
617         (void) snprintf(uidbuf, sizeof uidbuf, "%d", geteuid());
618         parms.r_owner = uidbuf;
619
620         CLNT_CALL(client, (rpcproc_t)RPCBPROC_UNSET, (xdrproc_t) xdr_rpcb,
621             (char *)(void *)&parms, (xdrproc_t) xdr_bool,
622             (char *)(void *)&rslt, tottimeout);
623
624         CLNT_DESTROY(client);
625         return (rslt);
626 }
627
628 /*
629  * From the merged list, find the appropriate entry
630  */
631 static struct netbuf *
632 got_entry(relp, nconf)
633         rpcb_entry_list_ptr relp;
634         const struct netconfig *nconf;
635 {
636         struct netbuf *na = NULL;
637         rpcb_entry_list_ptr sp;
638         rpcb_entry *rmap;
639
640         for (sp = relp; sp != NULL; sp = sp->rpcb_entry_next) {
641                 rmap = &sp->rpcb_entry_map;
642                 if ((strcmp(nconf->nc_proto, rmap->r_nc_proto) == 0) &&
643                     (strcmp(nconf->nc_protofmly, rmap->r_nc_protofmly) == 0) &&
644                     (nconf->nc_semantics == rmap->r_nc_semantics) &&
645                     (rmap->r_maddr != NULL) && (rmap->r_maddr[0] != 0)) {
646                         na = uaddr2taddr(nconf, rmap->r_maddr);
647 #ifdef ND_DEBUG
648                         fprintf(stderr, "\tRemote address is [%s].\n",
649                                 rmap->r_maddr);
650                         if (!na)
651                                 fprintf(stderr,
652                                     "\tCouldn't resolve remote address!\n");
653 #endif
654                         break;
655                 }
656         }
657         return (na);
658 }
659
660 /*
661  * Quick check to see if rpcbind is up.  Tries to connect over
662  * local transport.
663  */
664 static bool_t
665 __rpcbind_is_up()
666 {
667         struct netconfig *nconf;
668         struct sockaddr_un sun;
669         void *localhandle;
670         int sock;
671
672         nconf = NULL;
673         localhandle = setnetconfig();
674         while ((nconf = getnetconfig(localhandle)) != NULL) {
675                 if (nconf->nc_protofmly != NULL &&
676                     strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0)
677                          break;
678         }
679         endnetconfig(localhandle);
680
681         if (nconf == NULL)
682                 return (FALSE);
683
684         memset(&sun, 0, sizeof sun);
685         sock = _socket(AF_LOCAL, SOCK_STREAM, 0);
686         if (sock < 0)
687                 return (FALSE);
688         sun.sun_family = AF_LOCAL;
689         strncpy(sun.sun_path, _PATH_RPCBINDSOCK, sizeof(sun.sun_path));
690         sun.sun_len = SUN_LEN(&sun);
691
692         if (_connect(sock, (struct sockaddr *)&sun, sun.sun_len) < 0) {
693                 _close(sock);
694                 return (FALSE);
695         }
696
697         _close(sock);
698         return (TRUE);
699 }
700
701 /*
702  * An internal function which optimizes rpcb_getaddr function.  It also
703  * returns the client handle that it uses to contact the remote rpcbind.
704  *
705  * The algorithm used: If the transports is TCP or UDP, it first tries
706  * version 2 (portmap), 4 and then 3 (svr4).  This order should be
707  * changed in the next OS release to 4, 2 and 3.  We are assuming that by
708  * that time, version 4 would be available on many machines on the network.
709  * With this algorithm, we get performance as well as a plan for
710  * obsoleting version 2.
711  *
712  * For all other transports, the algorithm remains as 4 and then 3.
713  *
714  * XXX: Due to some problems with t_connect(), we do not reuse the same client
715  * handle for COTS cases and hence in these cases we do not return the
716  * client handle.  This code will change if t_connect() ever
717  * starts working properly.  Also look under clnt_vc.c.
718  */
719 struct netbuf *
720 __rpcb_findaddr_timed(program, version, nconf, host, clpp, tp)
721         rpcprog_t program;
722         rpcvers_t version;
723         const struct netconfig *nconf;
724         const char *host;
725         CLIENT **clpp;
726         struct timeval *tp;
727 {
728         static bool_t check_rpcbind = TRUE;
729         CLIENT *client = NULL;
730         RPCB parms;
731         enum clnt_stat clnt_st;
732         char *ua = NULL;
733         rpcvers_t vers;
734         struct netbuf *address = NULL;
735         rpcvers_t start_vers = RPCBVERS4;
736         struct netbuf servaddr;
737
738         /* parameter checking */
739         if (nconf == NULL) {
740                 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
741                 return (NULL);
742         }
743
744         parms.r_addr = NULL;
745
746         /*
747          * Use default total timeout if no timeout is specified.
748          */
749         if (tp == NULL)
750                 tp = &tottimeout;
751
752 #ifdef PORTMAP
753         /* Try version 2 for TCP or UDP */
754         if (strcmp(nconf->nc_protofmly, NC_INET) == 0) {
755                 u_short port = 0;
756                 struct netbuf remote;
757                 rpcvers_t pmapvers = 2;
758                 struct pmap pmapparms;
759
760                 /*
761                  * Try UDP only - there are some portmappers out
762                  * there that use UDP only.
763                  */
764                 if (strcmp(nconf->nc_proto, NC_TCP) == 0) {
765                         struct netconfig *newnconf;
766
767                         if ((newnconf = getnetconfigent("udp")) == NULL) {
768                                 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
769                                 return (NULL);
770                         }
771                         client = getclnthandle(host, newnconf, &parms.r_addr);
772                         freenetconfigent(newnconf);
773                 } else {
774                         client = getclnthandle(host, nconf, &parms.r_addr);
775                 }
776                 if (client == NULL)
777                         return (NULL);
778
779                 /*
780                  * Set version and retry timeout.
781                  */
782                 CLNT_CONTROL(client, CLSET_RETRY_TIMEOUT, (char *)&rpcbrmttime);
783                 CLNT_CONTROL(client, CLSET_VERS, (char *)&pmapvers);
784
785                 pmapparms.pm_prog = program;
786                 pmapparms.pm_vers = version;
787                 pmapparms.pm_prot = strcmp(nconf->nc_proto, NC_TCP) ?
788                                         IPPROTO_UDP : IPPROTO_TCP;
789                 pmapparms.pm_port = 0;  /* not needed */
790                 clnt_st = CLNT_CALL(client, (rpcproc_t)PMAPPROC_GETPORT,
791                     (xdrproc_t) xdr_pmap, (caddr_t)(void *)&pmapparms,
792                     (xdrproc_t) xdr_u_short, (caddr_t)(void *)&port,
793                     *tp);
794                 if (clnt_st != RPC_SUCCESS) {
795                         if ((clnt_st == RPC_PROGVERSMISMATCH) ||
796                                 (clnt_st == RPC_PROGUNAVAIL))
797                                 goto try_rpcbind; /* Try different versions */
798                         rpc_createerr.cf_stat = RPC_PMAPFAILURE;
799                         clnt_geterr(client, &rpc_createerr.cf_error);
800                         goto error;
801                 } else if (port == 0) {
802                         address = NULL;
803                         rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
804                         goto error;
805                 }
806                 port = htons(port);
807                 CLNT_CONTROL(client, CLGET_SVC_ADDR, (char *)&remote);
808                 if (((address = (struct netbuf *)
809                         malloc(sizeof (struct netbuf))) == NULL) ||
810                     ((address->buf = (char *)
811                         malloc(remote.len)) == NULL)) {
812                         rpc_createerr.cf_stat = RPC_SYSTEMERROR;
813                         clnt_geterr(client, &rpc_createerr.cf_error);
814                         free(address);
815                         address = NULL;
816                         goto error;
817                 }
818                 memcpy(address->buf, remote.buf, remote.len);
819                 memcpy(&((char *)address->buf)[sizeof (short)],
820                                 (char *)(void *)&port, sizeof (short));
821                 address->len = address->maxlen = remote.len;
822                 goto done;
823         }
824 #endif                          /* PORTMAP */
825
826 try_rpcbind:
827         /*
828          * Check if rpcbind is up.  This prevents needless delays when
829          * accessing applications such as the keyserver while booting
830          * disklessly.
831          */
832         if (check_rpcbind && strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0) {
833                 if (!__rpcbind_is_up()) {
834                         rpc_createerr.cf_stat = RPC_PMAPFAILURE;
835                         rpc_createerr.cf_error.re_errno = 0;
836                         goto error;
837                 }
838                 check_rpcbind = FALSE;
839         }
840
841         /*
842          * Now we try version 4 and then 3.
843          * We also send the remote system the address we used to
844          * contact it in case it can help to connect back with us
845          */
846         parms.r_prog = program;
847         parms.r_vers = version;
848         /*LINTED const castaway*/
849         parms.r_owner = (char *) &nullstring[0];        /* not needed; */
850                                                         /* just for xdring */
851         parms.r_netid = nconf->nc_netid; /* not really needed */
852
853         /*
854          * If a COTS transport is being used, try getting address via CLTS
855          * transport.  This works only with version 4.
856          */
857         if (nconf->nc_semantics == NC_TPI_COTS_ORD ||
858                         nconf->nc_semantics == NC_TPI_COTS) {
859
860                 void *handle;
861                 struct netconfig *nconf_clts;
862                 rpcb_entry_list_ptr relp = NULL;
863
864                 if (client == NULL) {
865                         /* This did not go through the above PORTMAP/TCP code */
866                         if ((handle = __rpc_setconf("datagram_v")) != NULL) {
867                                 while ((nconf_clts = __rpc_getconf(handle))
868                                         != NULL) {
869                                         if (strcmp(nconf_clts->nc_protofmly,
870                                                 nconf->nc_protofmly) != 0) {
871                                                 continue;
872                                         }
873                                         client = getclnthandle(host, nconf_clts,
874                                                         &parms.r_addr);
875                                         break;
876                                 }
877                                 __rpc_endconf(handle);
878                         }
879                         if (client == NULL)
880                                 goto regular_rpcbind;   /* Go the regular way */
881                 } else {
882                         /* This is a UDP PORTMAP handle.  Change to version 4 */
883                         vers = RPCBVERS4;
884                         CLNT_CONTROL(client, CLSET_VERS, (char *)(void *)&vers);
885                 }
886                 /*
887                  * We also send the remote system the address we used to
888                  * contact it in case it can help it connect back with us
889                  */
890                 if (parms.r_addr == NULL) {
891                         /*LINTED const castaway*/
892                         parms.r_addr = (char *) &nullstring[0]; /* for XDRing */
893                 }
894
895                 CLNT_CONTROL(client, CLSET_RETRY_TIMEOUT, (char *)&rpcbrmttime);
896
897                 clnt_st = CLNT_CALL(client, (rpcproc_t)RPCBPROC_GETADDRLIST,
898                     (xdrproc_t) xdr_rpcb, (char *)(void *)&parms,
899                     (xdrproc_t) xdr_rpcb_entry_list_ptr,
900                     (char *)(void *)&relp, *tp);
901                 if (clnt_st == RPC_SUCCESS) {
902                         if ((address = got_entry(relp, nconf)) != NULL) {
903                                 xdr_free((xdrproc_t) xdr_rpcb_entry_list_ptr,
904                                     (char *)(void *)&relp);
905                                 CLNT_CONTROL(client, CLGET_SVC_ADDR,
906                                         (char *)(void *)&servaddr);
907                                 __rpc_fixup_addr(address, &servaddr);
908                                 goto done;
909                         }
910                         /* Entry not found for this transport */
911                         xdr_free((xdrproc_t) xdr_rpcb_entry_list_ptr,
912                             (char *)(void *)&relp);
913                         /*
914                          * XXX: should have perhaps returned with error but
915                          * since the remote machine might not always be able
916                          * to send the address on all transports, we try the
917                          * regular way with regular_rpcbind
918                          */
919                         goto regular_rpcbind;
920                 } else if ((clnt_st == RPC_PROGVERSMISMATCH) ||
921                         (clnt_st == RPC_PROGUNAVAIL)) {
922                         start_vers = RPCBVERS;  /* Try version 3 now */
923                         goto regular_rpcbind; /* Try different versions */
924                 } else {
925                         rpc_createerr.cf_stat = RPC_PMAPFAILURE;
926                         clnt_geterr(client, &rpc_createerr.cf_error);
927                         goto error;
928                 }
929         }
930
931 regular_rpcbind:
932
933         /* Now the same transport is to be used to get the address */
934         if (client && ((nconf->nc_semantics == NC_TPI_COTS_ORD) ||
935                         (nconf->nc_semantics == NC_TPI_COTS))) {
936                 /* A CLTS type of client - destroy it */
937                 CLNT_DESTROY(client);
938                 client = NULL;
939         }
940
941         if (client == NULL) {
942                 client = getclnthandle(host, nconf, &parms.r_addr);
943                 if (client == NULL) {
944                         goto error;
945                 }
946         }
947         if (parms.r_addr == NULL) {
948                 /*LINTED const castaway*/
949                 parms.r_addr = (char *) &nullstring[0];
950         }
951
952         /* First try from start_vers and then version 3 (RPCBVERS) */
953
954         CLNT_CONTROL(client, CLSET_RETRY_TIMEOUT, (char *) &rpcbrmttime);
955         for (vers = start_vers;  vers >= RPCBVERS; vers--) {
956                 /* Set the version */
957                 CLNT_CONTROL(client, CLSET_VERS, (char *)(void *)&vers);
958                 clnt_st = CLNT_CALL(client, (rpcproc_t)RPCBPROC_GETADDR,
959                     (xdrproc_t) xdr_rpcb, (char *)(void *)&parms,
960                     (xdrproc_t) xdr_wrapstring, (char *)(void *) &ua, *tp);
961                 if (clnt_st == RPC_SUCCESS) {
962                         if ((ua == NULL) || (ua[0] == 0)) {
963                                 /* address unknown */
964                                 rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
965                                 goto error;
966                         }
967                         address = uaddr2taddr(nconf, ua);
968 #ifdef ND_DEBUG
969                         fprintf(stderr, "\tRemote address is [%s]\n", ua);
970                         if (!address)
971                                 fprintf(stderr,
972                                         "\tCouldn't resolve remote address!\n");
973 #endif
974                         xdr_free((xdrproc_t)xdr_wrapstring,
975                             (char *)(void *)&ua);
976
977                         if (! address) {
978                                 /* We don't know about your universal address */
979                                 rpc_createerr.cf_stat = RPC_N2AXLATEFAILURE;
980                                 goto error;
981                         }
982                         CLNT_CONTROL(client, CLGET_SVC_ADDR,
983                             (char *)(void *)&servaddr);
984                         __rpc_fixup_addr(address, &servaddr);
985                         goto done;
986                 } else if (clnt_st == RPC_PROGVERSMISMATCH) {
987                         struct rpc_err rpcerr;
988
989                         clnt_geterr(client, &rpcerr);
990                         if (rpcerr.re_vers.low > RPCBVERS4)
991                                 goto error;  /* a new version, can't handle */
992                 } else if (clnt_st != RPC_PROGUNAVAIL) {
993                         /* Cant handle this error */
994                         rpc_createerr.cf_stat = clnt_st;
995                         clnt_geterr(client, &rpc_createerr.cf_error);
996                         goto error;
997                 }
998         }
999
1000 error:
1001         if (client) {
1002                 CLNT_DESTROY(client);
1003                 client = NULL;
1004         }
1005 done:
1006         if (nconf->nc_semantics != NC_TPI_CLTS) {
1007                 /* This client is the connectionless one */
1008                 if (client) {
1009                         CLNT_DESTROY(client);
1010                         client = NULL;
1011                 }
1012         }
1013         if (clpp) {
1014                 *clpp = client;
1015         } else if (client) {
1016                 CLNT_DESTROY(client);
1017         }
1018         if (parms.r_addr != NULL && parms.r_addr != nullstring)
1019                 free(parms.r_addr);
1020         return (address);
1021 }
1022
1023
1024 /*
1025  * Find the mapped address for program, version.
1026  * Calls the rpcbind service remotely to do the lookup.
1027  * Uses the transport specified in nconf.
1028  * Returns FALSE (0) if no map exists, else returns 1.
1029  *
1030  * Assuming that the address is all properly allocated
1031  */
1032 int
1033 rpcb_getaddr(program, version, nconf, address, host)
1034         rpcprog_t program;
1035         rpcvers_t version;
1036         const struct netconfig *nconf;
1037         struct netbuf *address;
1038         const char *host;
1039 {
1040         struct netbuf *na;
1041
1042         if ((na = __rpcb_findaddr_timed(program, version,
1043             (struct netconfig *) nconf, (char *) host,
1044             (CLIENT **) NULL, (struct timeval *) NULL)) == NULL)
1045                 return (FALSE);
1046
1047         if (na->len > address->maxlen) {
1048                 /* Too long address */
1049                 free(na->buf);
1050                 free(na);
1051                 rpc_createerr.cf_stat = RPC_FAILED;
1052                 return (FALSE);
1053         }
1054         memcpy(address->buf, na->buf, (size_t)na->len);
1055         address->len = na->len;
1056         free(na->buf);
1057         free(na);
1058         return (TRUE);
1059 }
1060
1061 /*
1062  * Get a copy of the current maps.
1063  * Calls the rpcbind service remotely to get the maps.
1064  *
1065  * It returns only a list of the services
1066  * It returns NULL on failure.
1067  */
1068 rpcblist *
1069 rpcb_getmaps(nconf, host)
1070         const struct netconfig *nconf;
1071         const char *host;
1072 {
1073         rpcblist_ptr head = NULL;
1074         CLIENT *client;
1075         enum clnt_stat clnt_st;
1076         rpcvers_t vers = 0;
1077
1078         client = getclnthandle(host, nconf, NULL);
1079         if (client == NULL) {
1080                 return (head);
1081         }
1082         clnt_st = CLNT_CALL(client, (rpcproc_t)RPCBPROC_DUMP,
1083             (xdrproc_t) xdr_void, NULL, (xdrproc_t) xdr_rpcblist_ptr,
1084             (char *)(void *)&head, tottimeout);
1085         if (clnt_st == RPC_SUCCESS)
1086                 goto done;
1087
1088         if ((clnt_st != RPC_PROGVERSMISMATCH) &&
1089             (clnt_st != RPC_PROGUNAVAIL)) {
1090                 rpc_createerr.cf_stat = RPC_RPCBFAILURE;
1091                 clnt_geterr(client, &rpc_createerr.cf_error);
1092                 goto done;
1093         }
1094
1095         /* fall back to earlier version */
1096         CLNT_CONTROL(client, CLGET_VERS, (char *)(void *)&vers);
1097         if (vers == RPCBVERS4) {
1098                 vers = RPCBVERS;
1099                 CLNT_CONTROL(client, CLSET_VERS, (char *)(void *)&vers);
1100                 if (CLNT_CALL(client, (rpcproc_t)RPCBPROC_DUMP,
1101                     (xdrproc_t) xdr_void, NULL, (xdrproc_t) xdr_rpcblist_ptr,
1102                     (char *)(void *)&head, tottimeout) == RPC_SUCCESS)
1103                         goto done;
1104         }
1105         rpc_createerr.cf_stat = RPC_RPCBFAILURE;
1106         clnt_geterr(client, &rpc_createerr.cf_error);
1107
1108 done:
1109         CLNT_DESTROY(client);
1110         return (head);
1111 }
1112
1113 /*
1114  * rpcbinder remote-call-service interface.
1115  * This routine is used to call the rpcbind remote call service
1116  * which will look up a service program in the address maps, and then
1117  * remotely call that routine with the given parameters. This allows
1118  * programs to do a lookup and call in one step.
1119 */
1120 enum clnt_stat
1121 rpcb_rmtcall(nconf, host, prog, vers, proc, xdrargs, argsp,
1122                 xdrres, resp, tout, addr_ptr)
1123         const struct netconfig *nconf;  /* Netconfig structure */
1124         const char *host;                       /* Remote host name */
1125         rpcprog_t prog;
1126         rpcvers_t vers;
1127         rpcproc_t proc;                 /* Remote proc identifiers */
1128         xdrproc_t xdrargs, xdrres;      /* XDR routines */
1129         caddr_t argsp, resp;            /* Argument and Result */
1130         struct timeval tout;            /* Timeout value for this call */
1131         const struct netbuf *addr_ptr;  /* Preallocated netbuf address */
1132 {
1133         CLIENT *client;
1134         enum clnt_stat stat;
1135         struct r_rpcb_rmtcallargs a;
1136         struct r_rpcb_rmtcallres r;
1137         rpcvers_t rpcb_vers;
1138
1139         stat = 0;
1140         client = getclnthandle(host, nconf, NULL);
1141         if (client == NULL) {
1142                 return (RPC_FAILED);
1143         }
1144         /*LINTED const castaway*/
1145         CLNT_CONTROL(client, CLSET_RETRY_TIMEOUT, (char *)(void *)&rmttimeout);
1146         a.prog = prog;
1147         a.vers = vers;
1148         a.proc = proc;
1149         a.args.args_val = argsp;
1150         a.xdr_args = xdrargs;
1151         r.addr = NULL;
1152         r.results.results_val = resp;
1153         r.xdr_res = xdrres;
1154
1155         for (rpcb_vers = RPCBVERS4; rpcb_vers >= RPCBVERS; rpcb_vers--) {
1156                 CLNT_CONTROL(client, CLSET_VERS, (char *)(void *)&rpcb_vers);
1157                 stat = CLNT_CALL(client, (rpcproc_t)RPCBPROC_CALLIT,
1158                     (xdrproc_t) xdr_rpcb_rmtcallargs, (char *)(void *)&a,
1159                     (xdrproc_t) xdr_rpcb_rmtcallres, (char *)(void *)&r, tout);
1160                 if ((stat == RPC_SUCCESS) && (addr_ptr != NULL)) {
1161                         struct netbuf *na;
1162                         /*LINTED const castaway*/
1163                         na = uaddr2taddr((struct netconfig *) nconf, r.addr);
1164                         if (!na) {
1165                                 stat = RPC_N2AXLATEFAILURE;
1166                                 /*LINTED const castaway*/
1167                                 ((struct netbuf *) addr_ptr)->len = 0;
1168                                 goto error;
1169                         }
1170                         if (na->len > addr_ptr->maxlen) {
1171                                 /* Too long address */
1172                                 stat = RPC_FAILED; /* XXX A better error no */
1173                                 free(na->buf);
1174                                 free(na);
1175                                 /*LINTED const castaway*/
1176                                 ((struct netbuf *) addr_ptr)->len = 0;
1177                                 goto error;
1178                         }
1179                         memcpy(addr_ptr->buf, na->buf, (size_t)na->len);
1180                         /*LINTED const castaway*/
1181                         ((struct netbuf *)addr_ptr)->len = na->len;
1182                         free(na->buf);
1183                         free(na);
1184                         break;
1185                 } else if ((stat != RPC_PROGVERSMISMATCH) &&
1186                             (stat != RPC_PROGUNAVAIL)) {
1187                         goto error;
1188                 }
1189         }
1190 error:
1191         CLNT_DESTROY(client);
1192         if (r.addr)
1193                 xdr_free((xdrproc_t) xdr_wrapstring, (char *)(void *)&r.addr);
1194         return (stat);
1195 }
1196
1197 /*
1198  * Gets the time on the remote host.
1199  * Returns 1 if succeeds else 0.
1200  */
1201 bool_t
1202 rpcb_gettime(host, timep)
1203         const char *host;
1204         time_t *timep;
1205 {
1206         CLIENT *client = NULL;
1207         void *handle;
1208         struct netconfig *nconf;
1209         rpcvers_t vers;
1210         enum clnt_stat st;
1211
1212
1213         if ((host == NULL) || (host[0] == 0)) {
1214                 time(timep);
1215                 return (TRUE);
1216         }
1217
1218         if ((handle = __rpc_setconf("netpath")) == NULL) {
1219                 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
1220                 return (FALSE);
1221         }
1222         rpc_createerr.cf_stat = RPC_SUCCESS;
1223         while (client == NULL) {
1224                 if ((nconf = __rpc_getconf(handle)) == NULL) {
1225                         if (rpc_createerr.cf_stat == RPC_SUCCESS)
1226                                 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
1227                         break;
1228                 }
1229                 client = getclnthandle(host, nconf, NULL);
1230                 if (client)
1231                         break;
1232         }
1233         __rpc_endconf(handle);
1234         if (client == (CLIENT *) NULL) {
1235                 return (FALSE);
1236         }
1237
1238         st = CLNT_CALL(client, (rpcproc_t)RPCBPROC_GETTIME,
1239                 (xdrproc_t) xdr_void, NULL,
1240                 (xdrproc_t) xdr_int, (char *)(void *)timep, tottimeout);
1241
1242         if ((st == RPC_PROGVERSMISMATCH) || (st == RPC_PROGUNAVAIL)) {
1243                 CLNT_CONTROL(client, CLGET_VERS, (char *)(void *)&vers);
1244                 if (vers == RPCBVERS4) {
1245                         /* fall back to earlier version */
1246                         vers = RPCBVERS;
1247                         CLNT_CONTROL(client, CLSET_VERS, (char *)(void *)&vers);
1248                         st = CLNT_CALL(client, (rpcproc_t)RPCBPROC_GETTIME,
1249                                 (xdrproc_t) xdr_void, NULL,
1250                                 (xdrproc_t) xdr_int, (char *)(void *)timep,
1251                                 tottimeout);
1252                 }
1253         }
1254         CLNT_DESTROY(client);
1255         return (st == RPC_SUCCESS? TRUE: FALSE);
1256 }
1257
1258 /*
1259  * Converts taddr to universal address.  This routine should never
1260  * really be called because local n2a libraries are always provided.
1261  */
1262 char *
1263 rpcb_taddr2uaddr(nconf, taddr)
1264         struct netconfig *nconf;
1265         struct netbuf *taddr;
1266 {
1267         CLIENT *client;
1268         char *uaddr = NULL;
1269
1270
1271         /* parameter checking */
1272         if (nconf == NULL) {
1273                 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
1274                 return (NULL);
1275         }
1276         if (taddr == NULL) {
1277                 rpc_createerr.cf_stat = RPC_UNKNOWNADDR;
1278                 return (NULL);
1279         }
1280         client = local_rpcb();
1281         if (! client) {
1282                 return (NULL);
1283         }
1284
1285         CLNT_CALL(client, (rpcproc_t)RPCBPROC_TADDR2UADDR,
1286             (xdrproc_t) xdr_netbuf, (char *)(void *)taddr,
1287             (xdrproc_t) xdr_wrapstring, (char *)(void *)&uaddr, tottimeout);
1288         CLNT_DESTROY(client);
1289         return (uaddr);
1290 }
1291
1292 /*
1293  * Converts universal address to netbuf.  This routine should never
1294  * really be called because local n2a libraries are always provided.
1295  */
1296 struct netbuf *
1297 rpcb_uaddr2taddr(nconf, uaddr)
1298         struct netconfig *nconf;
1299         char *uaddr;
1300 {
1301         CLIENT *client;
1302         struct netbuf *taddr;
1303
1304
1305         /* parameter checking */
1306         if (nconf == NULL) {
1307                 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
1308                 return (NULL);
1309         }
1310         if (uaddr == NULL) {
1311                 rpc_createerr.cf_stat = RPC_UNKNOWNADDR;
1312                 return (NULL);
1313         }
1314         client = local_rpcb();
1315         if (! client) {
1316                 return (NULL);
1317         }
1318
1319         taddr = (struct netbuf *)calloc(1, sizeof (struct netbuf));
1320         if (taddr == NULL) {
1321                 CLNT_DESTROY(client);
1322                 return (NULL);
1323         }
1324         if (CLNT_CALL(client, (rpcproc_t)RPCBPROC_UADDR2TADDR,
1325             (xdrproc_t) xdr_wrapstring, (char *)(void *)&uaddr,
1326             (xdrproc_t) xdr_netbuf, (char *)(void *)taddr,
1327             tottimeout) != RPC_SUCCESS) {
1328                 free(taddr);
1329                 taddr = NULL;
1330         }
1331         CLNT_DESTROY(client);
1332         return (taddr);
1333 }