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