1 /* $NetBSD: rpcb_clnt.c,v 1.29 2013/03/11 20:19:29 tron Exp $ */
4 * Copyright (c) 2010, Oracle America, Inc.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials
15 * provided with the distribution.
16 * * Neither the name of the "Oracle America, Inc." nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 * Copyright (c) 1986-1991 by Sun Microsystems Inc.
37 /* #ident "@(#)rpcb_clnt.c 1.27 94/04/24 SMI" */
39 #include <sys/cdefs.h>
40 #if defined(LIBC_SCCS) && !defined(lint)
42 static char sccsid
[] = "@(#)rpcb_clnt.c 1.30 89/06/21 Copyr 1988 Sun Micro";
44 __RCSID("$NetBSD: rpcb_clnt.c,v 1.29 2013/03/11 20:19:29 tron Exp $");
50 * interface to rpcbind rpc service.
52 * Copyright (C) 1988, Sun Microsystems, Inc.
55 #include "namespace.h"
56 #include "reentrant.h"
57 #include <sys/types.h>
58 #include <sys/socket.h>
60 #include <sys/utsname.h>
62 #include <rpc/rpcb_prot.h>
63 #include <rpc/nettype.h>
64 #include <netconfig.h>
66 #include <netinet/in.h> /* FOR IPPROTO_TCP/UDP definitions */
67 #include <rpc/pmap_prot.h>
78 #include "svc_fdset.h"
79 #include "rpc_internal.h"
82 __weak_alias(rpcb_set
,_rpcb_set
)
83 __weak_alias(rpcb_unset
,_rpcb_unset
)
84 __weak_alias(rpcb_getmaps
,_rpcb_getmaps
)
85 __weak_alias(rpcb_taddr2uaddr
,_rpcb_taddr2uaddr
)
86 __weak_alias(rpcb_uaddr2taddr
,_rpcb_uaddr2taddr
)
89 static struct timeval tottimeout
= { 60, 0 };
90 static const struct timeval rmttimeout
= { 3, 0 };
92 static const char nullstring
[] = "\000";
96 struct address_cache
{
100 struct netbuf
*ac_taddr
;
101 struct address_cache
*ac_next
;
104 static struct address_cache
*front
;
105 static int cachesize
;
107 #define CLCR_GET_RPCB_TIMEOUT 1
108 #define CLCR_SET_RPCB_TIMEOUT 2
111 extern int __rpc_lowvers
;
113 static struct address_cache
*check_cache(const char *, const char *);
114 static void delete_cache(struct netbuf
*);
115 static void add_cache(const char *, const char *, struct netbuf
*, char *);
116 static CLIENT
*getclnthandle(const char *, const struct netconfig
*, char **);
117 static CLIENT
*local_rpcb(void);
118 static struct netbuf
*got_entry(rpcb_entry_list_ptr
, const struct netconfig
*);
121 * This routine adjusts the timeout used for calls to the remote rpcbind.
122 * Also, this routine can be used to set the use of portmapper version 2
123 * only when doing rpc_broadcasts
124 * These are private routines that may not be provided in future releases.
127 __rpc_control(int request
, void *info
)
130 _DIAGASSERT(info
!= NULL
);
133 case CLCR_GET_RPCB_TIMEOUT
:
134 *(struct timeval
*)info
= tottimeout
;
136 case CLCR_SET_RPCB_TIMEOUT
:
137 tottimeout
= *(struct timeval
*)info
;
139 case CLCR_SET_LOWVERS
:
140 __rpc_lowvers
= *(int *)info
;
142 case CLCR_GET_LOWVERS
:
143 *(int *)info
= __rpc_lowvers
;
152 * It might seem that a reader/writer lock would be more reasonable here.
153 * However because getclnthandle(), the only user of the cache functions,
154 * may do a delete_cache() operation if a check_cache() fails to return an
155 * address useful to clnt_tli_create(), we may as well use a mutex.
158 * As it turns out, if the cache lock is *not* a reader/writer lock, we will
159 * block all clnt_create's if we are trying to connect to a host that's down,
160 * since the lock will be held all during that time.
163 extern rwlock_t rpcbaddr_cache_lock
;
167 * The routines check_cache(), add_cache(), delete_cache() manage the
168 * cache of rpcbind addresses for (host, netid).
171 static struct address_cache
*
172 check_cache(const char *host
, const char *netid
)
174 struct address_cache
*cptr
;
176 _DIAGASSERT(host
!= NULL
);
177 _DIAGASSERT(netid
!= NULL
);
179 /* READ LOCK HELD ON ENTRY: rpcbaddr_cache_lock */
181 for (cptr
= front
; cptr
!= NULL
; cptr
= cptr
->ac_next
) {
182 if (!strcmp(cptr
->ac_host
, host
) &&
183 !strcmp(cptr
->ac_netid
, netid
)) {
185 fprintf(stderr
, "Found cache entry for %s: %s\n",
195 delete_cache(struct netbuf
*addr
)
197 struct address_cache
*cptr
, *prevptr
= NULL
;
199 _DIAGASSERT(addr
!= NULL
);
201 /* WRITE LOCK HELD ON ENTRY: rpcbaddr_cache_lock */
202 for (cptr
= front
; cptr
!= NULL
; cptr
= cptr
->ac_next
) {
203 if (!memcmp(cptr
->ac_taddr
->buf
, addr
->buf
, addr
->len
)) {
205 free(cptr
->ac_netid
);
206 free(cptr
->ac_taddr
->buf
);
207 free(cptr
->ac_taddr
);
209 free(cptr
->ac_uaddr
);
211 prevptr
->ac_next
= cptr
->ac_next
;
213 front
= cptr
->ac_next
;
223 add_cache(const char *host
, const char *netid
, struct netbuf
*taddr
,
226 struct address_cache
*ad_cache
, *cptr
, *prevptr
;
228 _DIAGASSERT(host
!= NULL
);
229 _DIAGASSERT(netid
!= NULL
);
230 /* uaddr may be NULL */
231 /* taddr may be NULL ??? */
233 ad_cache
= malloc(sizeof(*ad_cache
));
237 ad_cache
->ac_host
= strdup(host
);
238 ad_cache
->ac_netid
= strdup(netid
);
239 ad_cache
->ac_uaddr
= uaddr
? strdup(uaddr
) : NULL
;
240 ad_cache
->ac_taddr
= malloc(sizeof(*ad_cache
->ac_taddr
));
241 if (!ad_cache
->ac_host
|| !ad_cache
->ac_netid
|| !ad_cache
->ac_taddr
||
242 (uaddr
&& !ad_cache
->ac_uaddr
)) {
245 ad_cache
->ac_taddr
->len
= ad_cache
->ac_taddr
->maxlen
= taddr
->len
;
246 ad_cache
->ac_taddr
->buf
= malloc(taddr
->len
);
247 if (ad_cache
->ac_taddr
->buf
== NULL
) {
249 if (ad_cache
->ac_host
)
250 free(ad_cache
->ac_host
);
251 if (ad_cache
->ac_netid
)
252 free(ad_cache
->ac_netid
);
253 if (ad_cache
->ac_uaddr
)
254 free(ad_cache
->ac_uaddr
);
255 if (ad_cache
->ac_taddr
)
256 free(ad_cache
->ac_taddr
);
260 memcpy(ad_cache
->ac_taddr
->buf
, taddr
->buf
, taddr
->len
);
262 fprintf(stderr
, "Added to cache: %s : %s\n", host
, netid
);
265 /* VARIABLES PROTECTED BY rpcbaddr_cache_lock: cptr */
267 rwlock_wrlock(&rpcbaddr_cache_lock
);
268 if (cachesize
< CACHESIZE
) {
269 ad_cache
->ac_next
= front
;
273 /* Free the last entry */
276 while (cptr
->ac_next
) {
278 cptr
= cptr
->ac_next
;
282 fprintf(stderr
, "Deleted from cache: %s : %s\n",
283 cptr
->ac_host
, cptr
->ac_netid
);
286 free(cptr
->ac_netid
);
287 free(cptr
->ac_taddr
->buf
);
288 free(cptr
->ac_taddr
);
290 free(cptr
->ac_uaddr
);
293 prevptr
->ac_next
= NULL
;
294 ad_cache
->ac_next
= front
;
298 ad_cache
->ac_next
= NULL
;
302 rwlock_unlock(&rpcbaddr_cache_lock
);
306 * This routine will return a client handle that is connected to the
307 * rpcbind. Returns NULL on error and free's everything.
310 getclnthandle(const char *host
, const struct netconfig
*nconf
, char **targaddr
)
313 struct netbuf
*addr
, taddr
;
314 struct netbuf addr_to_delete
;
315 struct __rpc_sockinfo si
;
316 struct addrinfo hints
, *res
, *tres
;
317 struct address_cache
*ad_cache
;
320 _DIAGASSERT(host
!= NULL
);
321 _DIAGASSERT(nconf
!= NULL
);
322 /* targaddr may be NULL */
324 /* VARIABLES PROTECTED BY rpcbaddr_cache_lock: ad_cache */
326 /* Get the address of the rpcbind. Check cache first */
328 addr_to_delete
.len
= 0;
329 addr_to_delete
.buf
= NULL
;
330 rwlock_rdlock(&rpcbaddr_cache_lock
);
331 ad_cache
= check_cache(host
, nconf
->nc_netid
);
332 if (ad_cache
!= NULL
) {
333 addr
= ad_cache
->ac_taddr
;
334 client
= clnt_tli_create(RPC_ANYFD
, nconf
, addr
,
335 (rpcprog_t
)RPCBPROG
, (rpcvers_t
)RPCBVERS4
, 0, 0);
336 if (client
!= NULL
) {
338 *targaddr
= ad_cache
->ac_uaddr
;
339 rwlock_unlock(&rpcbaddr_cache_lock
);
342 addr_to_delete
.len
= addr
->len
;
343 addr_to_delete
.buf
= malloc(addr
->len
);
344 if (addr_to_delete
.buf
== NULL
) {
345 addr_to_delete
.len
= 0;
347 memcpy(addr_to_delete
.buf
, addr
->buf
, addr
->len
);
350 rwlock_unlock(&rpcbaddr_cache_lock
);
351 if (addr_to_delete
.len
!= 0) {
353 * Assume this may be due to cache data being
356 rwlock_wrlock(&rpcbaddr_cache_lock
);
357 delete_cache(&addr_to_delete
);
358 rwlock_unlock(&rpcbaddr_cache_lock
);
359 free(addr_to_delete
.buf
);
361 if (!__rpc_nconf2sockinfo(nconf
, &si
)) {
362 rpc_createerr
.cf_stat
= RPC_UNKNOWNPROTO
;
366 memset(&hints
, 0, sizeof hints
);
367 hints
.ai_family
= si
.si_af
;
368 hints
.ai_socktype
= si
.si_socktype
;
369 hints
.ai_protocol
= si
.si_proto
;
372 printf("trying netid %s family %d proto %d socktype %d\n",
373 nconf
->nc_netid
, si
.si_af
, si
.si_proto
, si
.si_socktype
);
376 if (getaddrinfo(host
, "sunrpc", &hints
, &res
) != 0) {
377 rpc_createerr
.cf_stat
= RPC_UNKNOWNHOST
;
381 for (tres
= res
; tres
!= NULL
; tres
= tres
->ai_next
) {
382 taddr
.buf
= tres
->ai_addr
;
383 taddr
.len
= taddr
.maxlen
= tres
->ai_addrlen
;
389 ua
= taddr2uaddr(nconf
, &taddr
);
390 fprintf(stderr
, "Got it [%s]\n", ua
);
399 fprintf(stderr
, "\tnetbuf len = %d, maxlen = %d\n",
400 taddr
.len
, taddr
.maxlen
);
401 fprintf(stderr
, "\tAddress is ");
402 for (i
= 0; i
< taddr
.len
; i
++)
403 fprintf(stderr
, "%u.", ((char *)(taddr
.buf
))[i
]);
404 fprintf(stderr
, "\n");
407 client
= clnt_tli_create(RPC_ANYFD
, nconf
, &taddr
,
408 (rpcprog_t
)RPCBPROG
, (rpcvers_t
)RPCBVERS4
, 0, 0);
411 clnt_pcreateerror("rpcbind clnt interface");
416 tmpaddr
= targaddr
? taddr2uaddr(nconf
, &taddr
) : NULL
;
417 add_cache(host
, nconf
->nc_netid
, &taddr
, tmpaddr
);
428 #define IN4_LOCALHOST_STRING "127.0.0.1"
429 #define IN6_LOCALHOST_STRING "::1"
432 * This routine will return a client handle that is connected to the local
433 * rpcbind. Returns NULL on error and free's everything.
439 static struct netconfig
*loopnconf
;
440 static const char *hostname
;
442 extern mutex_t loopnconf_lock
;
447 struct sockaddr_un sun
;
450 * Try connecting to the local rpcbind through a local socket
451 * first. If this doesn't work, try all transports defined in
452 * the netconfig file.
454 memset(&sun
, 0, sizeof sun
);
455 sock
= socket(AF_LOCAL
, SOCK_STREAM
, 0);
458 sun
.sun_family
= AF_LOCAL
;
459 strcpy(sun
.sun_path
, _PATH_RPCBINDSOCK
);
460 tsize
= SUN_LEN(&sun
);
461 _DIAGASSERT(__type_fit(uint8_t, tsize
));
462 nbuf
.len
= sun
.sun_len
= (uint8_t)tsize
;
463 nbuf
.maxlen
= sizeof (struct sockaddr_un
);
466 tsize
= __rpc_get_t_size(AF_LOCAL
, 0, 0);
467 _DIAGASSERT(__type_fit(u_int
, tsize
));
468 client
= clnt_vc_create(sock
, &nbuf
, (rpcprog_t
)RPCBPROG
,
469 (rpcvers_t
)RPCBVERS
, (u_int
)tsize
, (u_int
)tsize
);
471 if (client
!= NULL
) {
472 /* XXX - mark the socket to be closed in destructor */
473 (void) CLNT_CONTROL(client
, CLSET_FD_CLOSE
, NULL
);
477 /* XXX - nobody needs this socket anymore, free the descriptor */
482 /* VARIABLES PROTECTED BY loopnconf_lock: loopnconf */
483 mutex_lock(&loopnconf_lock
);
484 if (loopnconf
== NULL
) {
485 struct netconfig
*nconf
, *tmpnconf
= NULL
;
489 nc_handle
= setnetconfig();
490 if (nc_handle
== NULL
) {
491 /* fails to open netconfig file */
492 syslog (LOG_ERR
, "rpc: failed to open " NETCONFIG
);
493 rpc_createerr
.cf_stat
= RPC_UNKNOWNPROTO
;
494 mutex_unlock(&loopnconf_lock
);
497 while ((nconf
= getnetconfig(nc_handle
)) != NULL
) {
499 if ((strcmp(nconf
->nc_protofmly
, NC_INET6
) == 0 ||
503 strcmp(nconf
->nc_protofmly
, NC_INET
) == 0) &&
504 (nconf
->nc_semantics
== NC_TPI_COTS
||
505 nconf
->nc_semantics
== NC_TPI_COTS_ORD
)) {
506 fd
= __rpc_nconf2fd(nconf
);
508 * Can't create a socket, assume that
509 * this family isn't configured in the kernel.
515 if (!strcmp(nconf
->nc_protofmly
, NC_INET
))
516 hostname
= IN4_LOCALHOST_STRING
;
518 hostname
= IN6_LOCALHOST_STRING
;
521 if (tmpnconf
== NULL
) {
522 rpc_createerr
.cf_stat
= RPC_UNKNOWNPROTO
;
523 mutex_unlock(&loopnconf_lock
);
526 loopnconf
= getnetconfigent(tmpnconf
->nc_netid
);
527 /* loopnconf is never freed */
528 endnetconfig(nc_handle
);
530 mutex_unlock(&loopnconf_lock
);
531 client
= getclnthandle(hostname
, loopnconf
, NULL
);
536 * Set a mapping between program, version and address.
537 * Calls the rpcbind service to do the mapping.
540 rpcb_set(rpcprog_t program
, rpcvers_t version
,
541 const struct netconfig
*nconf
, /* Network structure of transport */
542 const struct netbuf
*address
) /* Services netconfig address */
549 /* parameter checking */
551 rpc_createerr
.cf_stat
= RPC_UNKNOWNPROTO
;
554 if (address
== NULL
) {
555 rpc_createerr
.cf_stat
= RPC_UNKNOWNADDR
;
558 client
= local_rpcb();
563 /* convert to universal */
564 parms
.r_addr
= taddr2uaddr(__UNCONST(nconf
), __UNCONST(address
));
566 CLNT_DESTROY(client
);
567 rpc_createerr
.cf_stat
= RPC_N2AXLATEFAILURE
;
568 return (FALSE
); /* no universal address */
570 parms
.r_prog
= program
;
571 parms
.r_vers
= version
;
572 parms
.r_netid
= nconf
->nc_netid
;
574 * Though uid is not being used directly, we still send it for
575 * completeness. For non-unix platforms, perhaps some other
576 * string or an empty string can be sent.
578 (void) snprintf(uidbuf
, sizeof uidbuf
, "%d", geteuid());
579 parms
.r_owner
= uidbuf
;
581 CLNT_CALL(client
, (rpcproc_t
)RPCBPROC_SET
, (xdrproc_t
) xdr_rpcb
,
582 (char *)(void *)&parms
, (xdrproc_t
) xdr_bool
,
583 (char *)(void *)&rslt
, tottimeout
);
585 CLNT_DESTROY(client
);
591 * Remove the mapping between program, version and netbuf address.
592 * Calls the rpcbind service to do the un-mapping.
593 * If netbuf is NULL, unset for all the transports, otherwise unset
594 * only for the given transport.
597 rpcb_unset(rpcprog_t program
, rpcvers_t version
, const struct netconfig
*nconf
)
604 client
= local_rpcb();
609 parms
.r_prog
= program
;
610 parms
.r_vers
= version
;
612 parms
.r_netid
= nconf
->nc_netid
;
614 parms
.r_netid
= __UNCONST(&nullstring
[0]); /* unsets all */
616 parms
.r_addr
= __UNCONST(&nullstring
[0]);
617 (void) snprintf(uidbuf
, sizeof uidbuf
, "%d", geteuid());
618 parms
.r_owner
= uidbuf
;
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
);
624 CLNT_DESTROY(client
);
629 * From the merged list, find the appropriate entry
631 static struct netbuf
*
632 got_entry(rpcb_entry_list_ptr relp
, const struct netconfig
*nconf
)
634 struct netbuf
*na
= NULL
;
635 rpcb_entry_list_ptr sp
;
638 _DIAGASSERT(nconf
!= NULL
);
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
);
648 fprintf(stderr
, "\tRemote address is [%s].\n",
652 "\tCouldn't resolve remote address!\n");
661 * An internal function which optimizes rpcb_getaddr function. It also
662 * returns the client handle that it uses to contact the remote rpcbind.
664 * The algorithm used: If the transports is TCP or UDP, it first tries
665 * version 2 (portmap), 4 and then 3 (svr4). This order should be
666 * changed in the next OS release to 4, 2 and 3. We are assuming that by
667 * that time, version 4 would be available on many machines on the network.
668 * With this algorithm, we get performance as well as a plan for
669 * obsoleting version 2.
671 * For all other transports, the algorithm remains as 4 and then 3.
673 * XXX: Due to some problems with t_connect(), we do not reuse the same client
674 * handle for COTS cases and hence in these cases we do not return the
675 * client handle. This code will change if t_connect() ever
676 * starts working properly. Also look under clnt_vc.c.
679 __rpcb_findaddr(rpcprog_t program
, rpcvers_t version
,
680 const struct netconfig
*nconf
, const char *host
, CLIENT
**clpp
)
682 CLIENT
*client
= NULL
;
684 enum clnt_stat clnt_st
;
687 struct netbuf
*address
= NULL
;
688 rpcvers_t start_vers
= RPCBVERS4
;
689 struct netbuf servaddr
;
691 /* nconf is handled below */
692 _DIAGASSERT(host
!= NULL
);
693 /* clpp may be NULL */
695 /* parameter checking */
697 rpc_createerr
.cf_stat
= RPC_UNKNOWNPROTO
;
704 /* Try version 2 for TCP or UDP */
705 if (strcmp(nconf
->nc_protofmly
, NC_INET
) == 0) {
707 struct netbuf remote
;
708 rpcvers_t pmapvers
= 2;
709 struct pmap pmapparms
;
712 * Try UDP only - there are some portmappers out
713 * there that use UDP only.
715 if (strcmp(nconf
->nc_proto
, NC_TCP
) == 0) {
716 struct netconfig
*newnconf
;
718 if ((newnconf
= getnetconfigent("udp")) == NULL
) {
719 rpc_createerr
.cf_stat
= RPC_UNKNOWNPROTO
;
722 client
= getclnthandle(host
, newnconf
, &parms
.r_addr
);
723 freenetconfigent(newnconf
);
725 client
= getclnthandle(host
, nconf
, &parms
.r_addr
);
727 if (client
== NULL
) {
731 /* Set the version */
732 CLNT_CONTROL(client
, CLSET_VERS
, (char *)(void *)&pmapvers
);
733 pmapparms
.pm_prog
= program
;
734 pmapparms
.pm_vers
= version
;
735 pmapparms
.pm_prot
= strcmp(nconf
->nc_proto
, NC_TCP
) ?
736 IPPROTO_UDP
: IPPROTO_TCP
;
737 pmapparms
.pm_port
= 0; /* not needed */
738 clnt_st
= CLNT_CALL(client
, (rpcproc_t
)PMAPPROC_GETPORT
,
739 (xdrproc_t
) xdr_pmap
, (caddr_t
)(void *)&pmapparms
,
740 (xdrproc_t
) xdr_u_short
, (caddr_t
)(void *)&port
,
742 if (clnt_st
!= RPC_SUCCESS
) {
743 if ((clnt_st
== RPC_PROGVERSMISMATCH
) ||
744 (clnt_st
== RPC_PROGUNAVAIL
))
745 goto try_rpcbind
; /* Try different versions */
746 rpc_createerr
.cf_stat
= RPC_PMAPFAILURE
;
747 clnt_geterr(client
, &rpc_createerr
.cf_error
);
749 } else if (port
== 0) {
751 rpc_createerr
.cf_stat
= RPC_PROGNOTREGISTERED
;
755 CLNT_CONTROL(client
, CLGET_SVC_ADDR
, (char *)(void *)&remote
);
756 if (((address
= malloc(sizeof(struct netbuf
))) == NULL
) ||
757 ((address
->buf
= malloc(remote
.len
)) == NULL
)) {
758 rpc_createerr
.cf_stat
= RPC_SYSTEMERROR
;
759 clnt_geterr(client
, &rpc_createerr
.cf_error
);
766 memcpy(address
->buf
, remote
.buf
, remote
.len
);
767 memcpy(&((char *)address
->buf
)[sizeof (short)],
768 (char *)(void *)&port
, sizeof (short));
769 address
->len
= address
->maxlen
= remote
.len
;
776 * Now we try version 4 and then 3.
777 * We also send the remote system the address we used to
778 * contact it in case it can help to connect back with us
780 parms
.r_prog
= program
;
781 parms
.r_vers
= version
;
782 parms
.r_owner
= __UNCONST(&nullstring
[0]); /* not needed; */
783 /* just for xdring */
784 parms
.r_netid
= nconf
->nc_netid
; /* not really needed */
787 * If a COTS transport is being used, try getting address via CLTS
788 * transport. This works only with version 4.
789 * NOTE: This is being done for all transports EXCEPT LOOPBACK
790 * because with loopback the cost to go to a COTS is same as
791 * the cost to go through CLTS, plus you get the advantage of
792 * finding out immediately if the local rpcbind process is dead.
795 if ((nconf
->nc_semantics
== NC_TPI_COTS_ORD
||
796 nconf
->nc_semantics
== NC_TPI_COTS
) &&
797 (strcmp(nconf
->nc_protofmly
, NC_LOOPBACK
) != 0))
799 if (client
!= NULL
) {
800 CLNT_DESTROY(client
);
803 if (nconf
->nc_semantics
== NC_TPI_CLTS
)
807 struct netconfig
*nconf_clts
;
808 rpcb_entry_list_ptr relp
= NULL
;
810 if (client
== NULL
) {
811 /* This did not go through the above PORTMAP/TCP code */
813 if ((handle
= __rpc_setconf("datagram_v")) != NULL
)
815 if ((handle
= __rpc_setconf("circuit_v")) != NULL
)
818 while ((nconf_clts
= __rpc_getconf(handle
))
820 if (strcmp(nconf_clts
->nc_protofmly
,
821 nconf
->nc_protofmly
) != 0) {
824 client
= getclnthandle(host
, nconf_clts
,
828 __rpc_endconf(handle
);
831 goto regular_rpcbind
; /* Go the regular way */
833 /* This is a UDP PORTMAP handle. Change to version 4 */
835 CLNT_CONTROL(client
, CLSET_VERS
, (char *)(void *)&vers
);
838 * We also send the remote system the address we used to
839 * contact it in case it can help it connect back with us
841 if (parms
.r_addr
== NULL
) {
843 parms
.r_addr
= __UNCONST(&nullstring
[0]);
845 clnt_st
= CLNT_CALL(client
, (rpcproc_t
)RPCBPROC_GETADDRLIST
,
846 (xdrproc_t
) xdr_rpcb
, (char *)(void *)&parms
,
847 (xdrproc_t
) xdr_rpcb_entry_list_ptr
,
848 (char *)(void *)&relp
, tottimeout
);
849 if (clnt_st
== RPC_SUCCESS
) {
850 if ((address
= got_entry(relp
, nconf
)) != NULL
) {
851 xdr_free((xdrproc_t
) xdr_rpcb_entry_list_ptr
,
852 (char *)(void *)&relp
);
853 CLNT_CONTROL(client
, CLGET_SVC_ADDR
,
854 (char *)(void *)&servaddr
);
855 __rpc_fixup_addr(address
, &servaddr
);
858 /* Entry not found for this transport */
859 xdr_free((xdrproc_t
) xdr_rpcb_entry_list_ptr
,
860 (char *)(void *)&relp
);
862 * XXX: should have perhaps returned with error but
863 * since the remote machine might not always be able
864 * to send the address on all transports, we try the
865 * regular way with regular_rpcbind
867 goto regular_rpcbind
;
868 } else if ((clnt_st
== RPC_PROGVERSMISMATCH
) ||
869 (clnt_st
== RPC_PROGUNAVAIL
)) {
870 start_vers
= RPCBVERS
; /* Try version 3 now */
871 goto regular_rpcbind
; /* Try different versions */
873 rpc_createerr
.cf_stat
= RPC_PMAPFAILURE
;
874 clnt_geterr(client
, &rpc_createerr
.cf_error
);
881 /* Now the same transport is to be used to get the address */
883 if (client
&& ((nconf
->nc_semantics
== NC_TPI_COTS_ORD
) ||
884 (nconf
->nc_semantics
== NC_TPI_COTS
)))
886 if (client
&& nconf
->nc_semantics
== NC_TPI_CLTS
)
889 /* A CLTS type of client - destroy it */
890 CLNT_DESTROY(client
);
894 if (client
== NULL
) {
895 client
= getclnthandle(host
, nconf
, &parms
.r_addr
);
896 if (client
== NULL
) {
900 if (parms
.r_addr
== NULL
)
901 parms
.r_addr
= __UNCONST(&nullstring
[0]);
903 /* First try from start_vers and then version 3 (RPCBVERS) */
904 for (vers
= start_vers
; vers
>= RPCBVERS
; vers
--) {
905 /* Set the version */
906 CLNT_CONTROL(client
, CLSET_VERS
, (char *)(void *)&vers
);
907 clnt_st
= CLNT_CALL(client
, (rpcproc_t
)RPCBPROC_GETADDR
,
908 (xdrproc_t
) xdr_rpcb
, (char *)(void *)&parms
,
909 (xdrproc_t
) xdr_wrapstring
, (char *)(void *) &ua
,
911 if (clnt_st
== RPC_SUCCESS
) {
912 if ((ua
== NULL
) || (ua
[0] == 0)) {
913 /* address unknown */
914 rpc_createerr
.cf_stat
= RPC_PROGNOTREGISTERED
;
917 address
= uaddr2taddr(nconf
, ua
);
919 fprintf(stderr
, "\tRemote address is [%s]\n", ua
);
922 "\tCouldn't resolve remote address!\n");
924 xdr_free((xdrproc_t
)xdr_wrapstring
,
925 (char *)(void *)&ua
);
928 /* We don't know about your universal address */
929 rpc_createerr
.cf_stat
= RPC_N2AXLATEFAILURE
;
932 CLNT_CONTROL(client
, CLGET_SVC_ADDR
,
933 (char *)(void *)&servaddr
);
934 __rpc_fixup_addr(address
, &servaddr
);
936 } else if (clnt_st
== RPC_PROGVERSMISMATCH
) {
937 struct rpc_err rpcerr
;
939 clnt_geterr(client
, &rpcerr
);
940 if (rpcerr
.re_vers
.low
> RPCBVERS4
)
941 goto error
; /* a new version, can't handle */
942 } else if (clnt_st
!= RPC_PROGUNAVAIL
) {
943 /* Cant handle this error */
944 rpc_createerr
.cf_stat
= clnt_st
;
945 clnt_geterr(client
, &rpc_createerr
.cf_error
);
952 CLNT_DESTROY(client
);
956 if (nconf
->nc_semantics
!= NC_TPI_CLTS
) {
957 /* This client is the connectionless one */
959 CLNT_DESTROY(client
);
966 CLNT_DESTROY(client
);
973 * Find the mapped address for program, version.
974 * Calls the rpcbind service remotely to do the lookup.
975 * Uses the transport specified in nconf.
976 * Returns FALSE (0) if no map exists, else returns 1.
978 * Assuming that the address is all properly allocated
981 rpcb_getaddr(rpcprog_t program
, rpcvers_t version
,
982 const struct netconfig
*nconf
, struct netbuf
*address
,
987 _DIAGASSERT(address
!= NULL
);
989 if ((na
= __rpcb_findaddr(program
, version
, nconf
,
990 host
, NULL
)) == NULL
)
993 if (na
->len
> address
->maxlen
) {
994 /* Too long address */
997 rpc_createerr
.cf_stat
= RPC_FAILED
;
1000 memcpy(address
->buf
, na
->buf
, (size_t)na
->len
);
1001 address
->len
= na
->len
;
1008 * Get a copy of the current maps.
1009 * Calls the rpcbind service remotely to get the maps.
1011 * It returns only a list of the services
1012 * It returns NULL on failure.
1015 rpcb_getmaps(const struct netconfig
*nconf
, const char *host
)
1017 rpcblist_ptr head
= NULL
;
1019 enum clnt_stat clnt_st
;
1022 client
= getclnthandle(host
, nconf
, NULL
);
1023 if (client
== NULL
) {
1026 clnt_st
= CLNT_CALL(client
, (rpcproc_t
)RPCBPROC_DUMP
,
1027 (xdrproc_t
) xdr_void
, NULL
, (xdrproc_t
) xdr_rpcblist_ptr
,
1028 (char *)(void *)&head
, tottimeout
);
1029 if (clnt_st
== RPC_SUCCESS
)
1032 if ((clnt_st
!= RPC_PROGVERSMISMATCH
) &&
1033 (clnt_st
!= RPC_PROGUNAVAIL
)) {
1034 rpc_createerr
.cf_stat
= RPC_RPCBFAILURE
;
1035 clnt_geterr(client
, &rpc_createerr
.cf_error
);
1039 /* fall back to earlier version */
1040 CLNT_CONTROL(client
, CLGET_VERS
, (char *)(void *)&vers
);
1041 if (vers
== RPCBVERS4
) {
1043 CLNT_CONTROL(client
, CLSET_VERS
, (char *)(void *)&vers
);
1044 if (CLNT_CALL(client
, (rpcproc_t
)RPCBPROC_DUMP
,
1045 (xdrproc_t
) xdr_void
, NULL
, (xdrproc_t
) xdr_rpcblist_ptr
,
1046 (char *)(void *)&head
, tottimeout
) == RPC_SUCCESS
)
1049 rpc_createerr
.cf_stat
= RPC_RPCBFAILURE
;
1050 clnt_geterr(client
, &rpc_createerr
.cf_error
);
1053 CLNT_DESTROY(client
);
1058 * rpcbinder remote-call-service interface.
1059 * This routine is used to call the rpcbind remote call service
1060 * which will look up a service program in the address maps, and then
1061 * remotely call that routine with the given parameters. This allows
1062 * programs to do a lookup and call in one step.
1066 const struct netconfig
*nconf
, /* Netconfig structure */
1067 const char *host
, /* Remote host name */
1070 rpcproc_t proc
, /* Remote proc identifiers */
1072 const char *argsp
, /* Argument */
1073 xdrproc_t xdrres
, /* XDR routines */
1074 caddr_t resp
, /* Result */
1075 struct timeval tout
, /* Timeout value for this call */
1076 const struct netbuf
*addr_ptr
) /* Preallocated netbuf address */
1079 enum clnt_stat stat
;
1080 struct r_rpcb_rmtcallargs a
;
1081 struct r_rpcb_rmtcallres r
;
1082 rpcvers_t rpcb_vers
;
1084 stat
= RPC_FAILED
; /* XXXGCC -Wuninitialized [dreamcast] */
1086 client
= getclnthandle(host
, nconf
, NULL
);
1087 if (client
== NULL
) {
1088 return (RPC_FAILED
);
1090 CLNT_CONTROL(client
, CLSET_RETRY_TIMEOUT
, __UNCONST(&rmttimeout
));
1094 a
.args
.args_val
= argsp
;
1095 a
.xdr_args
= xdrargs
;
1097 r
.results
.results_val
= resp
;
1100 for (rpcb_vers
= RPCBVERS4
; rpcb_vers
>= RPCBVERS
; rpcb_vers
--) {
1101 CLNT_CONTROL(client
, CLSET_VERS
, (char *)(void *)&rpcb_vers
);
1102 stat
= CLNT_CALL(client
, (rpcproc_t
)RPCBPROC_CALLIT
,
1103 (xdrproc_t
) xdr_rpcb_rmtcallargs
, (char *)(void *)&a
,
1104 (xdrproc_t
) xdr_rpcb_rmtcallres
, (char *)(void *)&r
, tout
);
1105 if ((stat
== RPC_SUCCESS
) && (addr_ptr
!= NULL
)) {
1107 na
= uaddr2taddr(__UNCONST(nconf
), r
.addr
);
1109 stat
= RPC_N2AXLATEFAILURE
;
1110 ((struct netbuf
*)__UNCONST(addr_ptr
))->len
= 0;
1113 if (na
->len
> addr_ptr
->maxlen
) {
1114 /* Too long address */
1115 stat
= RPC_FAILED
; /* XXX A better error no */
1118 ((struct netbuf
*)__UNCONST(addr_ptr
))->len
= 0;
1121 memcpy(addr_ptr
->buf
, na
->buf
, (size_t)na
->len
);
1122 ((struct netbuf
*)__UNCONST(addr_ptr
))->len
= na
->len
;
1126 } else if ((stat
!= RPC_PROGVERSMISMATCH
) &&
1127 (stat
!= RPC_PROGUNAVAIL
)) {
1132 CLNT_DESTROY(client
);
1134 xdr_free((xdrproc_t
) xdr_wrapstring
, (char *)(void *)&r
.addr
);
1139 * Gets the time on the remote host.
1140 * Returns 1 if succeeds else 0.
1143 rpcb_gettime(const char *host
, time_t *timep
)
1145 CLIENT
*client
= NULL
;
1147 struct netconfig
*nconf
;
1152 if ((host
== NULL
) || (host
[0] == 0)) {
1157 if ((handle
= __rpc_setconf("netpath")) == NULL
) {
1158 rpc_createerr
.cf_stat
= RPC_UNKNOWNPROTO
;
1161 rpc_createerr
.cf_stat
= RPC_SUCCESS
;
1162 while (client
== NULL
) {
1163 if ((nconf
= __rpc_getconf(handle
)) == NULL
) {
1164 if (rpc_createerr
.cf_stat
== RPC_SUCCESS
)
1165 rpc_createerr
.cf_stat
= RPC_UNKNOWNPROTO
;
1168 client
= getclnthandle(host
, nconf
, NULL
);
1172 __rpc_endconf(handle
);
1173 if (client
== NULL
) {
1177 st
= CLNT_CALL(client
, (rpcproc_t
)RPCBPROC_GETTIME
,
1178 (xdrproc_t
) xdr_void
, NULL
,
1179 (xdrproc_t
) xdr_int
, (char *)(void *)timep
, tottimeout
);
1181 if ((st
== RPC_PROGVERSMISMATCH
) || (st
== RPC_PROGUNAVAIL
)) {
1182 CLNT_CONTROL(client
, CLGET_VERS
, (char *)(void *)&vers
);
1183 if (vers
== RPCBVERS4
) {
1184 /* fall back to earlier version */
1186 CLNT_CONTROL(client
, CLSET_VERS
, (char *)(void *)&vers
);
1187 st
= CLNT_CALL(client
, (rpcproc_t
)RPCBPROC_GETTIME
,
1188 (xdrproc_t
) xdr_void
, NULL
,
1189 (xdrproc_t
) xdr_int
, (char *)(void *)timep
,
1193 CLNT_DESTROY(client
);
1194 return (st
== RPC_SUCCESS
? TRUE
: FALSE
);
1198 * Converts taddr to universal address. This routine should never
1199 * really be called because local n2a libraries are always provided.
1202 rpcb_taddr2uaddr(struct netconfig
*nconf
, struct netbuf
*taddr
)
1207 /* parameter checking */
1208 if (nconf
== NULL
) {
1209 rpc_createerr
.cf_stat
= RPC_UNKNOWNPROTO
;
1212 if (taddr
== NULL
) {
1213 rpc_createerr
.cf_stat
= RPC_UNKNOWNADDR
;
1216 client
= local_rpcb();
1221 CLNT_CALL(client
, (rpcproc_t
)RPCBPROC_TADDR2UADDR
,
1222 (xdrproc_t
) xdr_netbuf
, (char *)(void *)taddr
,
1223 (xdrproc_t
) xdr_wrapstring
, (char *)(void *)&uaddr
, tottimeout
);
1224 CLNT_DESTROY(client
);
1229 * Converts universal address to netbuf. This routine should never
1230 * really be called because local n2a libraries are always provided.
1233 rpcb_uaddr2taddr(struct netconfig
*nconf
, char *uaddr
)
1236 struct netbuf
*taddr
;
1239 /* parameter checking */
1240 if (nconf
== NULL
) {
1241 rpc_createerr
.cf_stat
= RPC_UNKNOWNPROTO
;
1244 if (uaddr
== NULL
) {
1245 rpc_createerr
.cf_stat
= RPC_UNKNOWNADDR
;
1248 client
= local_rpcb();
1253 taddr
= (struct netbuf
*)calloc(1, sizeof (struct netbuf
));
1254 if (taddr
== NULL
) {
1255 CLNT_DESTROY(client
);
1258 if (CLNT_CALL(client
, (rpcproc_t
)RPCBPROC_UADDR2TADDR
,
1259 (xdrproc_t
) xdr_wrapstring
, (char *)(void *)&uaddr
,
1260 (xdrproc_t
) xdr_netbuf
, (char *)(void *)taddr
,
1261 tottimeout
) != RPC_SUCCESS
) {
1265 CLNT_DESTROY(client
);