1 /* $NetBSD: rpcb_clnt.c,v 1.31 2015/03/26 11:31:57 justin 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.31 2015/03/26 11:31:57 justin 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 if (CLNT_CALL(client
, (rpcproc_t
)RPCBPROC_SET
, (xdrproc_t
) xdr_rpcb
,
582 (char *)(void *)&parms
, (xdrproc_t
) xdr_bool
,
583 (char *)(void *)&rslt
, tottimeout
) != RPC_SUCCESS
) {
584 rpc_createerr
.cf_stat
= RPC_PMAPFAILURE
;
585 clnt_geterr(client
, &rpc_createerr
.cf_error
);
588 CLNT_DESTROY(client
);
594 * Remove the mapping between program, version and netbuf address.
595 * Calls the rpcbind service to do the un-mapping.
596 * If netbuf is NULL, unset for all the transports, otherwise unset
597 * only for the given transport.
600 rpcb_unset(rpcprog_t program
, rpcvers_t version
, const struct netconfig
*nconf
)
607 client
= local_rpcb();
612 parms
.r_prog
= program
;
613 parms
.r_vers
= version
;
615 parms
.r_netid
= nconf
->nc_netid
;
617 parms
.r_netid
= __UNCONST(&nullstring
[0]); /* unsets all */
619 parms
.r_addr
= __UNCONST(&nullstring
[0]);
620 (void) snprintf(uidbuf
, sizeof uidbuf
, "%d", geteuid());
621 parms
.r_owner
= uidbuf
;
623 if (CLNT_CALL(client
, (rpcproc_t
)RPCBPROC_UNSET
, (xdrproc_t
) xdr_rpcb
,
624 (char *)(void *)&parms
, (xdrproc_t
) xdr_bool
,
625 (char *)(void *)&rslt
, tottimeout
) != RPC_SUCCESS
) {
626 rpc_createerr
.cf_stat
= RPC_PMAPFAILURE
;
627 clnt_geterr(client
, &rpc_createerr
.cf_error
);
630 CLNT_DESTROY(client
);
635 * From the merged list, find the appropriate entry
637 static struct netbuf
*
638 got_entry(rpcb_entry_list_ptr relp
, const struct netconfig
*nconf
)
640 struct netbuf
*na
= NULL
;
641 rpcb_entry_list_ptr sp
;
644 _DIAGASSERT(nconf
!= NULL
);
646 for (sp
= relp
; sp
!= NULL
; sp
= sp
->rpcb_entry_next
) {
647 rmap
= &sp
->rpcb_entry_map
;
648 if ((strcmp(nconf
->nc_proto
, rmap
->r_nc_proto
) == 0) &&
649 (strcmp(nconf
->nc_protofmly
, rmap
->r_nc_protofmly
) == 0) &&
650 (nconf
->nc_semantics
== rmap
->r_nc_semantics
) &&
651 (rmap
->r_maddr
!= NULL
) && (rmap
->r_maddr
[0] != 0)) {
652 na
= uaddr2taddr(nconf
, rmap
->r_maddr
);
654 fprintf(stderr
, "\tRemote address is [%s].\n",
658 "\tCouldn't resolve remote address!\n");
667 * An internal function which optimizes rpcb_getaddr function. It also
668 * returns the client handle that it uses to contact the remote rpcbind.
670 * The algorithm used: If the transports is TCP or UDP, it first tries
671 * version 2 (portmap), 4 and then 3 (svr4). This order should be
672 * changed in the next OS release to 4, 2 and 3. We are assuming that by
673 * that time, version 4 would be available on many machines on the network.
674 * With this algorithm, we get performance as well as a plan for
675 * obsoleting version 2.
677 * For all other transports, the algorithm remains as 4 and then 3.
679 * XXX: Due to some problems with t_connect(), we do not reuse the same client
680 * handle for COTS cases and hence in these cases we do not return the
681 * client handle. This code will change if t_connect() ever
682 * starts working properly. Also look under clnt_vc.c.
685 __rpcb_findaddr(rpcprog_t program
, rpcvers_t version
,
686 const struct netconfig
*nconf
, const char *host
, CLIENT
**clpp
)
688 CLIENT
*client
= NULL
;
690 enum clnt_stat clnt_st
;
693 struct netbuf
*address
= NULL
;
694 rpcvers_t start_vers
= RPCBVERS4
;
695 struct netbuf servaddr
;
697 /* nconf is handled below */
698 _DIAGASSERT(host
!= NULL
);
699 /* clpp may be NULL */
701 /* parameter checking */
703 rpc_createerr
.cf_stat
= RPC_UNKNOWNPROTO
;
710 /* Try version 2 for TCP or UDP */
711 if (strcmp(nconf
->nc_protofmly
, NC_INET
) == 0) {
713 struct netbuf remote
;
714 rpcvers_t pmapvers
= 2;
715 struct pmap pmapparms
;
718 * Try UDP only - there are some portmappers out
719 * there that use UDP only.
721 if (strcmp(nconf
->nc_proto
, NC_TCP
) == 0) {
722 struct netconfig
*newnconf
;
724 if ((newnconf
= getnetconfigent("udp")) == NULL
) {
725 rpc_createerr
.cf_stat
= RPC_UNKNOWNPROTO
;
728 client
= getclnthandle(host
, newnconf
, &parms
.r_addr
);
729 freenetconfigent(newnconf
);
731 client
= getclnthandle(host
, nconf
, &parms
.r_addr
);
733 if (client
== NULL
) {
737 /* Set the version */
738 CLNT_CONTROL(client
, CLSET_VERS
, (char *)(void *)&pmapvers
);
739 pmapparms
.pm_prog
= program
;
740 pmapparms
.pm_vers
= version
;
741 pmapparms
.pm_prot
= strcmp(nconf
->nc_proto
, NC_TCP
) ?
742 IPPROTO_UDP
: IPPROTO_TCP
;
743 pmapparms
.pm_port
= 0; /* not needed */
744 clnt_st
= CLNT_CALL(client
, (rpcproc_t
)PMAPPROC_GETPORT
,
745 (xdrproc_t
) xdr_pmap
, (caddr_t
)(void *)&pmapparms
,
746 (xdrproc_t
) xdr_u_short
, (caddr_t
)(void *)&port
,
748 if (clnt_st
!= RPC_SUCCESS
) {
749 if ((clnt_st
== RPC_PROGVERSMISMATCH
) ||
750 (clnt_st
== RPC_PROGUNAVAIL
))
751 goto try_rpcbind
; /* Try different versions */
752 rpc_createerr
.cf_stat
= RPC_PMAPFAILURE
;
753 clnt_geterr(client
, &rpc_createerr
.cf_error
);
755 } else if (port
== 0) {
757 rpc_createerr
.cf_stat
= RPC_PROGNOTREGISTERED
;
761 CLNT_CONTROL(client
, CLGET_SVC_ADDR
, (char *)(void *)&remote
);
762 if (((address
= malloc(sizeof(struct netbuf
))) == NULL
) ||
763 ((address
->buf
= malloc(remote
.len
)) == NULL
)) {
764 rpc_createerr
.cf_stat
= RPC_SYSTEMERROR
;
765 clnt_geterr(client
, &rpc_createerr
.cf_error
);
772 memcpy(address
->buf
, remote
.buf
, remote
.len
);
773 memcpy(&((char *)address
->buf
)[sizeof (short)],
774 (char *)(void *)&port
, sizeof (short));
775 address
->len
= address
->maxlen
= remote
.len
;
782 * Now we try version 4 and then 3.
783 * We also send the remote system the address we used to
784 * contact it in case it can help to connect back with us
786 parms
.r_prog
= program
;
787 parms
.r_vers
= version
;
788 parms
.r_owner
= __UNCONST(&nullstring
[0]); /* not needed; */
789 /* just for xdring */
790 parms
.r_netid
= nconf
->nc_netid
; /* not really needed */
793 * If a COTS transport is being used, try getting address via CLTS
794 * transport. This works only with version 4.
795 * NOTE: This is being done for all transports EXCEPT LOOPBACK
796 * because with loopback the cost to go to a COTS is same as
797 * the cost to go through CLTS, plus you get the advantage of
798 * finding out immediately if the local rpcbind process is dead.
801 if ((nconf
->nc_semantics
== NC_TPI_COTS_ORD
||
802 nconf
->nc_semantics
== NC_TPI_COTS
) &&
803 (strcmp(nconf
->nc_protofmly
, NC_LOOPBACK
) != 0))
805 if (client
!= NULL
) {
806 CLNT_DESTROY(client
);
809 if (nconf
->nc_semantics
== NC_TPI_CLTS
)
813 struct netconfig
*nconf_clts
;
814 rpcb_entry_list_ptr relp
= NULL
;
816 if (client
== NULL
) {
817 /* This did not go through the above PORTMAP/TCP code */
819 if ((handle
= __rpc_setconf("datagram_v")) != NULL
)
821 if ((handle
= __rpc_setconf("circuit_v")) != NULL
)
824 while ((nconf_clts
= __rpc_getconf(handle
))
826 if (strcmp(nconf_clts
->nc_protofmly
,
827 nconf
->nc_protofmly
) != 0) {
830 client
= getclnthandle(host
, nconf_clts
,
834 __rpc_endconf(handle
);
837 goto regular_rpcbind
; /* Go the regular way */
839 /* This is a UDP PORTMAP handle. Change to version 4 */
841 CLNT_CONTROL(client
, CLSET_VERS
, (char *)(void *)&vers
);
844 * We also send the remote system the address we used to
845 * contact it in case it can help it connect back with us
847 if (parms
.r_addr
== NULL
) {
849 parms
.r_addr
= __UNCONST(&nullstring
[0]);
851 clnt_st
= CLNT_CALL(client
, (rpcproc_t
)RPCBPROC_GETADDRLIST
,
852 (xdrproc_t
) xdr_rpcb
, (char *)(void *)&parms
,
853 (xdrproc_t
) xdr_rpcb_entry_list_ptr
,
854 (char *)(void *)&relp
, tottimeout
);
855 if (clnt_st
== RPC_SUCCESS
) {
856 if ((address
= got_entry(relp
, nconf
)) != NULL
) {
857 xdr_free((xdrproc_t
) xdr_rpcb_entry_list_ptr
,
858 (char *)(void *)&relp
);
859 CLNT_CONTROL(client
, CLGET_SVC_ADDR
,
860 (char *)(void *)&servaddr
);
861 __rpc_fixup_addr(address
, &servaddr
);
864 /* Entry not found for this transport */
865 xdr_free((xdrproc_t
) xdr_rpcb_entry_list_ptr
,
866 (char *)(void *)&relp
);
868 * XXX: should have perhaps returned with error but
869 * since the remote machine might not always be able
870 * to send the address on all transports, we try the
871 * regular way with regular_rpcbind
873 goto regular_rpcbind
;
874 } else if ((clnt_st
== RPC_PROGVERSMISMATCH
) ||
875 (clnt_st
== RPC_PROGUNAVAIL
)) {
876 start_vers
= RPCBVERS
; /* Try version 3 now */
877 goto regular_rpcbind
; /* Try different versions */
879 rpc_createerr
.cf_stat
= RPC_PMAPFAILURE
;
880 clnt_geterr(client
, &rpc_createerr
.cf_error
);
887 /* Now the same transport is to be used to get the address */
889 if (client
&& ((nconf
->nc_semantics
== NC_TPI_COTS_ORD
) ||
890 (nconf
->nc_semantics
== NC_TPI_COTS
)))
892 if (client
&& nconf
->nc_semantics
== NC_TPI_CLTS
)
895 /* A CLTS type of client - destroy it */
896 CLNT_DESTROY(client
);
900 if (client
== NULL
) {
901 client
= getclnthandle(host
, nconf
, &parms
.r_addr
);
902 if (client
== NULL
) {
906 if (parms
.r_addr
== NULL
)
907 parms
.r_addr
= __UNCONST(&nullstring
[0]);
909 /* First try from start_vers and then version 3 (RPCBVERS) */
910 for (vers
= start_vers
; vers
>= RPCBVERS
; vers
--) {
911 /* Set the version */
912 CLNT_CONTROL(client
, CLSET_VERS
, (char *)(void *)&vers
);
913 clnt_st
= CLNT_CALL(client
, (rpcproc_t
)RPCBPROC_GETADDR
,
914 (xdrproc_t
) xdr_rpcb
, (char *)(void *)&parms
,
915 (xdrproc_t
) xdr_wrapstring
, (char *)(void *) &ua
,
917 if (clnt_st
== RPC_SUCCESS
) {
918 if ((ua
== NULL
) || (ua
[0] == 0)) {
919 /* address unknown */
920 rpc_createerr
.cf_stat
= RPC_PROGNOTREGISTERED
;
923 address
= uaddr2taddr(nconf
, ua
);
925 fprintf(stderr
, "\tRemote address is [%s]\n", ua
);
928 "\tCouldn't resolve remote address!\n");
930 xdr_free((xdrproc_t
)xdr_wrapstring
,
931 (char *)(void *)&ua
);
934 /* We don't know about your universal address */
935 rpc_createerr
.cf_stat
= RPC_N2AXLATEFAILURE
;
938 CLNT_CONTROL(client
, CLGET_SVC_ADDR
,
939 (char *)(void *)&servaddr
);
940 __rpc_fixup_addr(address
, &servaddr
);
942 } else if (clnt_st
== RPC_PROGVERSMISMATCH
) {
943 struct rpc_err rpcerr
;
945 clnt_geterr(client
, &rpcerr
);
946 if (rpcerr
.re_vers
.low
> RPCBVERS4
)
947 goto error
; /* a new version, can't handle */
948 } else if (clnt_st
!= RPC_PROGUNAVAIL
) {
949 /* Cant handle this error */
950 rpc_createerr
.cf_stat
= clnt_st
;
951 clnt_geterr(client
, &rpc_createerr
.cf_error
);
958 CLNT_DESTROY(client
);
962 if (nconf
->nc_semantics
!= NC_TPI_CLTS
) {
963 /* This client is the connectionless one */
965 CLNT_DESTROY(client
);
972 CLNT_DESTROY(client
);
979 * Find the mapped address for program, version.
980 * Calls the rpcbind service remotely to do the lookup.
981 * Uses the transport specified in nconf.
982 * Returns FALSE (0) if no map exists, else returns 1.
984 * Assuming that the address is all properly allocated
987 rpcb_getaddr(const rpcprog_t program
, const rpcvers_t version
,
988 const struct netconfig
*nconf
, struct netbuf
*address
,
993 _DIAGASSERT(address
!= NULL
);
995 if ((na
= __rpcb_findaddr(program
, version
, nconf
,
996 host
, NULL
)) == NULL
)
999 if (na
->len
> address
->maxlen
) {
1000 /* Too long address */
1003 rpc_createerr
.cf_stat
= RPC_FAILED
;
1006 memcpy(address
->buf
, na
->buf
, (size_t)na
->len
);
1007 address
->len
= na
->len
;
1014 * Get a copy of the current maps.
1015 * Calls the rpcbind service remotely to get the maps.
1017 * It returns only a list of the services
1018 * It returns NULL on failure.
1021 rpcb_getmaps(const struct netconfig
*nconf
, const char *host
)
1023 rpcblist_ptr head
= NULL
;
1025 enum clnt_stat clnt_st
;
1028 client
= getclnthandle(host
, nconf
, NULL
);
1029 if (client
== NULL
) {
1032 clnt_st
= CLNT_CALL(client
, (rpcproc_t
)RPCBPROC_DUMP
,
1033 (xdrproc_t
) xdr_void
, NULL
, (xdrproc_t
) xdr_rpcblist_ptr
,
1034 (char *)(void *)&head
, tottimeout
);
1035 if (clnt_st
== RPC_SUCCESS
)
1038 if ((clnt_st
!= RPC_PROGVERSMISMATCH
) &&
1039 (clnt_st
!= RPC_PROGUNAVAIL
)) {
1040 rpc_createerr
.cf_stat
= RPC_RPCBFAILURE
;
1041 clnt_geterr(client
, &rpc_createerr
.cf_error
);
1045 /* fall back to earlier version */
1046 CLNT_CONTROL(client
, CLGET_VERS
, (char *)(void *)&vers
);
1047 if (vers
== RPCBVERS4
) {
1049 CLNT_CONTROL(client
, CLSET_VERS
, (char *)(void *)&vers
);
1050 if (CLNT_CALL(client
, (rpcproc_t
)RPCBPROC_DUMP
,
1051 (xdrproc_t
) xdr_void
, NULL
, (xdrproc_t
) xdr_rpcblist_ptr
,
1052 (char *)(void *)&head
, tottimeout
) == RPC_SUCCESS
)
1055 rpc_createerr
.cf_stat
= RPC_RPCBFAILURE
;
1056 clnt_geterr(client
, &rpc_createerr
.cf_error
);
1059 CLNT_DESTROY(client
);
1064 * rpcbinder remote-call-service interface.
1065 * This routine is used to call the rpcbind remote call service
1066 * which will look up a service program in the address maps, and then
1067 * remotely call that routine with the given parameters. This allows
1068 * programs to do a lookup and call in one step.
1072 const struct netconfig
*nconf
, /* Netconfig structure */
1073 const char *host
, /* Remote host name */
1076 rpcproc_t proc
, /* Remote proc identifiers */
1078 const char *argsp
, /* Argument */
1079 xdrproc_t xdrres
, /* XDR routines */
1080 caddr_t resp
, /* Result */
1081 struct timeval tout
, /* Timeout value for this call */
1082 const struct netbuf
*addr_ptr
) /* Preallocated netbuf address */
1085 enum clnt_stat stat
;
1086 struct r_rpcb_rmtcallargs a
;
1087 struct r_rpcb_rmtcallres r
;
1088 rpcvers_t rpcb_vers
;
1090 stat
= RPC_FAILED
; /* XXXGCC -Wuninitialized [dreamcast] */
1092 client
= getclnthandle(host
, nconf
, NULL
);
1093 if (client
== NULL
) {
1094 return (RPC_FAILED
);
1096 CLNT_CONTROL(client
, CLSET_RETRY_TIMEOUT
, __UNCONST(&rmttimeout
));
1100 a
.args
.args_val
= argsp
;
1101 a
.xdr_args
= xdrargs
;
1103 r
.results
.results_val
= resp
;
1106 for (rpcb_vers
= RPCBVERS4
; rpcb_vers
>= RPCBVERS
; rpcb_vers
--) {
1107 CLNT_CONTROL(client
, CLSET_VERS
, (char *)(void *)&rpcb_vers
);
1108 stat
= CLNT_CALL(client
, (rpcproc_t
)RPCBPROC_CALLIT
,
1109 (xdrproc_t
) xdr_rpcb_rmtcallargs
, (char *)(void *)&a
,
1110 (xdrproc_t
) xdr_rpcb_rmtcallres
, (char *)(void *)&r
, tout
);
1111 if ((stat
== RPC_SUCCESS
) && (addr_ptr
!= NULL
)) {
1113 na
= uaddr2taddr(__UNCONST(nconf
), r
.addr
);
1115 stat
= RPC_N2AXLATEFAILURE
;
1116 ((struct netbuf
*)__UNCONST(addr_ptr
))->len
= 0;
1119 if (na
->len
> addr_ptr
->maxlen
) {
1120 /* Too long address */
1121 stat
= RPC_FAILED
; /* XXX A better error no */
1124 ((struct netbuf
*)__UNCONST(addr_ptr
))->len
= 0;
1127 memcpy(addr_ptr
->buf
, na
->buf
, (size_t)na
->len
);
1128 ((struct netbuf
*)__UNCONST(addr_ptr
))->len
= na
->len
;
1132 } else if ((stat
!= RPC_PROGVERSMISMATCH
) &&
1133 (stat
!= RPC_PROGUNAVAIL
)) {
1138 CLNT_DESTROY(client
);
1140 xdr_free((xdrproc_t
) xdr_wrapstring
, (char *)(void *)&r
.addr
);
1145 * Gets the time on the remote host.
1146 * Returns 1 if succeeds else 0.
1149 rpcb_gettime(const char *host
, time_t *timep
)
1151 CLIENT
*client
= NULL
;
1153 struct netconfig
*nconf
;
1158 if ((host
== NULL
) || (host
[0] == 0)) {
1163 if ((handle
= __rpc_setconf("netpath")) == NULL
) {
1164 rpc_createerr
.cf_stat
= RPC_UNKNOWNPROTO
;
1167 rpc_createerr
.cf_stat
= RPC_SUCCESS
;
1168 while (client
== NULL
) {
1169 if ((nconf
= __rpc_getconf(handle
)) == NULL
) {
1170 if (rpc_createerr
.cf_stat
== RPC_SUCCESS
)
1171 rpc_createerr
.cf_stat
= RPC_UNKNOWNPROTO
;
1174 client
= getclnthandle(host
, nconf
, NULL
);
1178 __rpc_endconf(handle
);
1179 if (client
== NULL
) {
1183 st
= CLNT_CALL(client
, (rpcproc_t
)RPCBPROC_GETTIME
,
1184 (xdrproc_t
) xdr_void
, NULL
,
1185 (xdrproc_t
) xdr_int
, (char *)(void *)timep
, tottimeout
);
1187 if ((st
== RPC_PROGVERSMISMATCH
) || (st
== RPC_PROGUNAVAIL
)) {
1188 CLNT_CONTROL(client
, CLGET_VERS
, (char *)(void *)&vers
);
1189 if (vers
== RPCBVERS4
) {
1190 /* fall back to earlier version */
1192 CLNT_CONTROL(client
, CLSET_VERS
, (char *)(void *)&vers
);
1193 st
= CLNT_CALL(client
, (rpcproc_t
)RPCBPROC_GETTIME
,
1194 (xdrproc_t
) xdr_void
, NULL
,
1195 (xdrproc_t
) xdr_int
, (char *)(void *)timep
,
1199 CLNT_DESTROY(client
);
1200 return (st
== RPC_SUCCESS
? TRUE
: FALSE
);
1204 * Converts taddr to universal address. This routine should never
1205 * really be called because local n2a libraries are always provided.
1208 rpcb_taddr2uaddr(struct netconfig
*nconf
, struct netbuf
*taddr
)
1213 /* parameter checking */
1214 if (nconf
== NULL
) {
1215 rpc_createerr
.cf_stat
= RPC_UNKNOWNPROTO
;
1218 if (taddr
== NULL
) {
1219 rpc_createerr
.cf_stat
= RPC_UNKNOWNADDR
;
1222 client
= local_rpcb();
1227 if (CLNT_CALL(client
, (rpcproc_t
)RPCBPROC_TADDR2UADDR
,
1228 (xdrproc_t
) xdr_netbuf
, (char *)(void *)taddr
,
1229 (xdrproc_t
) xdr_wrapstring
, (char *)(void *)&uaddr
, tottimeout
)
1231 rpc_createerr
.cf_stat
= RPC_PMAPFAILURE
;
1232 clnt_geterr(client
, &rpc_createerr
.cf_error
);
1234 CLNT_DESTROY(client
);
1239 * Converts universal address to netbuf. This routine should never
1240 * really be called because local n2a libraries are always provided.
1243 rpcb_uaddr2taddr(struct netconfig
*nconf
, char *uaddr
)
1246 struct netbuf
*taddr
;
1249 /* parameter checking */
1250 if (nconf
== NULL
) {
1251 rpc_createerr
.cf_stat
= RPC_UNKNOWNPROTO
;
1254 if (uaddr
== NULL
) {
1255 rpc_createerr
.cf_stat
= RPC_UNKNOWNADDR
;
1258 client
= local_rpcb();
1263 taddr
= (struct netbuf
*)calloc(1, sizeof (struct netbuf
));
1264 if (taddr
== NULL
) {
1265 CLNT_DESTROY(client
);
1268 if (CLNT_CALL(client
, (rpcproc_t
)RPCBPROC_UADDR2TADDR
,
1269 (xdrproc_t
) xdr_wrapstring
, (char *)(void *)&uaddr
,
1270 (xdrproc_t
) xdr_netbuf
, (char *)(void *)taddr
,
1271 tottimeout
) != RPC_SUCCESS
) {
1272 rpc_createerr
.cf_stat
= RPC_PMAPFAILURE
;
1273 clnt_geterr(client
, &rpc_createerr
.cf_error
);
1277 CLNT_DESTROY(client
);