2 * The contents of this file are subject to the Sun Standards
3 * License Version 1.0 the (the "License";) You may not use
4 * this file except in compliance with the License. You may
5 * obtain a copy of the License at lib/libc/rpc/LICENSE
7 * Software distributed under the License is distributed on
8 * an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either
9 * express or implied. See the License for the specific
10 * language governing rights and limitations under the License.
12 * The Original Code is Copyright 1998 by Sun Microsystems, Inc
14 * The Initial Developer of the Original Code is: Sun
17 * All Rights Reserved.
19 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
20 * unrestricted use provided that this legend is included on all tape
21 * media and as a part of the software program in whole or part. Users
22 * may copy or modify Sun RPC without charge, but are not authorized
23 * to license or distribute it to anyone else except as part of a product or
24 * program developed by the user.
26 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
27 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
28 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
30 * Sun RPC is provided with no support and without any obligation on the
31 * part of Sun Microsystems, Inc. to assist in its use, correction,
32 * modification or enhancement.
34 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
35 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
36 * OR ANY PART THEREOF.
38 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
39 * or profits or other special, indirect and consequential damages, even if
40 * Sun has been advised of the possibility of such damages.
42 * Sun Microsystems, Inc.
44 * Mountain View, California 94043
45 * @(#)rpcb_clnt.c 1.27 94/04/24 SMI; 1.30 89/06/21 Copyr 1988 Sun Micro
46 * $NetBSD: rpcb_clnt.c,v 1.6 2000/07/16 06:41:43 itojun Exp $
47 * $FreeBSD: src/lib/libc/rpc/rpcb_clnt.c,v 1.17 2007/09/20 22:35:24 matteo Exp $
50 * Copyright (c) 1986-1991 by Sun Microsystems Inc.
55 * interface to rpcbind rpc service.
57 * Copyright (C) 1988, Sun Microsystems, Inc.
60 #include "namespace.h"
61 #include "reentrant.h"
62 #include <sys/types.h>
63 #include <sys/socket.h>
65 #include <sys/utsname.h>
67 #include <rpc/rpcb_prot.h>
68 #include <rpc/nettype.h>
69 #include <netconfig.h>
71 #include <netinet/in.h> /* FOR IPPROTO_TCP/UDP definitions */
72 #include <rpc/pmap_prot.h>
81 #include "un-namespace.h"
86 static struct timeval tottimeout
= { 60, 0 };
87 static const struct timeval rmttimeout
= { 3, 0 };
88 static struct timeval rpcbrmttime
= { 15, 0 };
90 extern bool_t
xdr_wrapstring(XDR
*, char **);
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 case CLCR_GET_RPCB_TIMEOUT
:
131 *(struct timeval
*)info
= tottimeout
;
133 case CLCR_SET_RPCB_TIMEOUT
:
134 tottimeout
= *(struct timeval
*)info
;
136 case CLCR_SET_LOWVERS
:
137 __rpc_lowvers
= *(int *)info
;
139 case CLCR_GET_LOWVERS
:
140 *(int *)info
= __rpc_lowvers
;
149 * It might seem that a reader/writer lock would be more reasonable here.
150 * However because getclnthandle(), the only user of the cache functions,
151 * may do a delete_cache() operation if a check_cache() fails to return an
152 * address useful to clnt_tli_create(), we may as well use a mutex.
155 * As it turns out, if the cache lock is *not* a reader/writer lock, we will
156 * block all clnt_create's if we are trying to connect to a host that's down,
157 * since the lock will be held all during that time.
161 * The routines check_cache(), add_cache(), delete_cache() manage the
162 * cache of rpcbind addresses for (host, netid).
165 static struct address_cache
*
166 check_cache(const char *host
, const char *netid
)
168 struct address_cache
*cptr
;
170 /* READ LOCK HELD ON ENTRY: rpcbaddr_cache_lock */
172 for (cptr
= front
; cptr
!= NULL
; cptr
= cptr
->ac_next
) {
173 if (!strcmp(cptr
->ac_host
, host
) &&
174 !strcmp(cptr
->ac_netid
, netid
)) {
176 fprintf(stderr
, "Found cache entry for %s: %s\n",
182 return ((struct address_cache
*) NULL
);
186 delete_cache(struct netbuf
*addr
)
188 struct address_cache
*cptr
, *prevptr
= NULL
;
190 /* WRITE LOCK HELD ON ENTRY: rpcbaddr_cache_lock */
191 for (cptr
= front
; cptr
!= NULL
; cptr
= cptr
->ac_next
) {
192 if (!memcmp(cptr
->ac_taddr
->buf
, addr
->buf
, addr
->len
)) {
194 free(cptr
->ac_netid
);
195 free(cptr
->ac_taddr
->buf
);
196 free(cptr
->ac_taddr
);
198 free(cptr
->ac_uaddr
);
200 prevptr
->ac_next
= cptr
->ac_next
;
202 front
= cptr
->ac_next
;
212 add_cache(const char *host
, const char *netid
, struct netbuf
*taddr
,
215 struct address_cache
*ad_cache
, *cptr
, *prevptr
;
217 ad_cache
= (struct address_cache
*)
218 malloc(sizeof (struct address_cache
));
222 ad_cache
->ac_host
= strdup(host
);
223 ad_cache
->ac_netid
= strdup(netid
);
224 ad_cache
->ac_uaddr
= uaddr
? strdup(uaddr
) : NULL
;
225 ad_cache
->ac_taddr
= (struct netbuf
*)malloc(sizeof (struct netbuf
));
226 if (!ad_cache
->ac_host
|| !ad_cache
->ac_netid
|| !ad_cache
->ac_taddr
||
227 (uaddr
&& !ad_cache
->ac_uaddr
)) {
230 ad_cache
->ac_taddr
->len
= ad_cache
->ac_taddr
->maxlen
= taddr
->len
;
231 ad_cache
->ac_taddr
->buf
= (char *) malloc(taddr
->len
);
232 if (ad_cache
->ac_taddr
->buf
== NULL
) {
234 if (ad_cache
->ac_host
)
235 free(ad_cache
->ac_host
);
236 if (ad_cache
->ac_netid
)
237 free(ad_cache
->ac_netid
);
238 if (ad_cache
->ac_uaddr
)
239 free(ad_cache
->ac_uaddr
);
240 if (ad_cache
->ac_taddr
)
241 free(ad_cache
->ac_taddr
);
245 memcpy(ad_cache
->ac_taddr
->buf
, taddr
->buf
, taddr
->len
);
247 fprintf(stderr
, "Added to cache: %s : %s\n", host
, netid
);
250 /* VARIABLES PROTECTED BY rpcbaddr_cache_lock: cptr */
252 rwlock_wrlock(&rpcbaddr_cache_lock
);
253 if (cachesize
< CACHESIZE
) {
254 ad_cache
->ac_next
= front
;
258 /* Free the last entry */
261 while (cptr
->ac_next
) {
263 cptr
= cptr
->ac_next
;
267 fprintf(stderr
, "Deleted from cache: %s : %s\n",
268 cptr
->ac_host
, cptr
->ac_netid
);
271 free(cptr
->ac_netid
);
272 free(cptr
->ac_taddr
->buf
);
273 free(cptr
->ac_taddr
);
275 free(cptr
->ac_uaddr
);
278 prevptr
->ac_next
= NULL
;
279 ad_cache
->ac_next
= front
;
283 ad_cache
->ac_next
= NULL
;
287 rwlock_unlock(&rpcbaddr_cache_lock
);
291 * This routine will return a client handle that is connected to the
292 * rpcbind. If targaddr is non-NULL, the "universal address" of the
293 * host will be stored in *targaddr; the caller is responsible for
294 * freeing this string.
295 * On error, returns NULL and free's everything.
298 getclnthandle(const char *host
, const struct netconfig
*nconf
, char **targaddr
)
301 struct netbuf
*addr
, taddr
;
302 struct netbuf addr_to_delete
;
303 struct __rpc_sockinfo si
;
304 struct addrinfo hints
, *res
, *tres
;
305 struct address_cache
*ad_cache
;
308 /* VARIABLES PROTECTED BY rpcbaddr_cache_lock: ad_cache */
310 /* Get the address of the rpcbind. Check cache first */
312 addr_to_delete
.len
= 0;
313 rwlock_rdlock(&rpcbaddr_cache_lock
);
316 ad_cache
= check_cache(host
, nconf
->nc_netid
);
317 if (ad_cache
!= NULL
) {
318 addr
= ad_cache
->ac_taddr
;
319 client
= clnt_tli_create(RPC_ANYFD
, nconf
, addr
,
320 (rpcprog_t
)RPCBPROG
, (rpcvers_t
)RPCBVERS4
, 0, 0);
321 if (client
!= NULL
) {
323 *targaddr
= strdup(ad_cache
->ac_uaddr
);
324 rwlock_unlock(&rpcbaddr_cache_lock
);
327 addr_to_delete
.len
= addr
->len
;
328 addr_to_delete
.buf
= (char *)malloc(addr
->len
);
329 if (addr_to_delete
.buf
== NULL
) {
330 addr_to_delete
.len
= 0;
332 memcpy(addr_to_delete
.buf
, addr
->buf
, addr
->len
);
335 rwlock_unlock(&rpcbaddr_cache_lock
);
336 if (addr_to_delete
.len
!= 0) {
338 * Assume this may be due to cache data being
341 rwlock_wrlock(&rpcbaddr_cache_lock
);
342 delete_cache(&addr_to_delete
);
343 rwlock_unlock(&rpcbaddr_cache_lock
);
344 free(addr_to_delete
.buf
);
346 if (!__rpc_nconf2sockinfo(nconf
, &si
)) {
347 rpc_createerr
.cf_stat
= RPC_UNKNOWNPROTO
;
351 memset(&hints
, 0, sizeof hints
);
352 hints
.ai_family
= si
.si_af
;
353 hints
.ai_socktype
= si
.si_socktype
;
354 hints
.ai_protocol
= si
.si_proto
;
357 printf("trying netid %s family %d proto %d socktype %d\n",
358 nconf
->nc_netid
, si
.si_af
, si
.si_proto
, si
.si_socktype
);
361 if (nconf
->nc_protofmly
!= NULL
&& strcmp(nconf
->nc_protofmly
, NC_LOOPBACK
) == 0) {
362 client
= local_rpcb();
365 clnt_pcreateerror("rpcbind clnt interface");
369 struct sockaddr_un sun
;
371 *targaddr
= malloc(sizeof(sun
.sun_path
));
372 if (*targaddr
== NULL
) {
373 CLNT_DESTROY(client
);
376 strncpy(*targaddr
, _PATH_RPCBINDSOCK
,
377 sizeof(sun
.sun_path
));
382 if (getaddrinfo(host
, "sunrpc", &hints
, &res
) != 0) {
383 rpc_createerr
.cf_stat
= RPC_UNKNOWNHOST
;
388 for (tres
= res
; tres
!= NULL
; tres
= tres
->ai_next
) {
389 taddr
.buf
= tres
->ai_addr
;
390 taddr
.len
= taddr
.maxlen
= tres
->ai_addrlen
;
396 ua
= taddr2uaddr(nconf
, &taddr
);
397 fprintf(stderr
, "Got it [%s]\n", ua
);
406 fprintf(stderr
, "\tnetbuf len = %d, maxlen = %d\n",
407 taddr
.len
, taddr
.maxlen
);
408 fprintf(stderr
, "\tAddress is ");
409 for (i
= 0; i
< taddr
.len
; i
++)
410 fprintf(stderr
, "%u.", ((char *)(taddr
.buf
))[i
]);
411 fprintf(stderr
, "\n");
414 client
= clnt_tli_create(RPC_ANYFD
, nconf
, &taddr
,
415 (rpcprog_t
)RPCBPROG
, (rpcvers_t
)RPCBVERS4
, 0, 0);
418 clnt_pcreateerror("rpcbind clnt interface");
423 tmpaddr
= targaddr
? taddr2uaddr(nconf
, &taddr
) : NULL
;
424 add_cache(host
, nconf
->nc_netid
, &taddr
, tmpaddr
);
436 #define IN4_LOCALHOST_STRING "127.0.0.1"
437 #define IN6_LOCALHOST_STRING "::1"
440 * This routine will return a client handle that is connected to the local
441 * rpcbind. Returns NULL on error and free's everything.
447 static struct netconfig
*loopnconf
;
448 static char *hostname
;
452 struct sockaddr_un sun
;
455 * Try connecting to the local rpcbind through a local socket
456 * first. If this doesn't work, try all transports defined in
457 * the netconfig file.
459 memset(&sun
, 0, sizeof sun
);
460 sock
= _socket(AF_LOCAL
, SOCK_STREAM
, 0);
463 sun
.sun_family
= AF_LOCAL
;
464 strcpy(sun
.sun_path
, _PATH_RPCBINDSOCK
);
465 nbuf
.len
= sun
.sun_len
= SUN_LEN(&sun
);
466 nbuf
.maxlen
= sizeof (struct sockaddr_un
);
469 tsize
= __rpc_get_t_size(AF_LOCAL
, 0, 0);
470 client
= clnt_vc_create(sock
, &nbuf
, (rpcprog_t
)RPCBPROG
,
471 (rpcvers_t
)RPCBVERS
, tsize
, tsize
);
473 if (client
!= NULL
) {
474 /* Mark the socket to be closed in destructor */
475 CLNT_CONTROL(client
, CLSET_FD_CLOSE
, NULL
);
479 /* Nobody needs this socket anymore; free the descriptor. */
484 /* VARIABLES PROTECTED BY loopnconf_lock: loopnconf */
485 mutex_lock(&loopnconf_lock
);
486 if (loopnconf
== NULL
) {
487 struct netconfig
*nconf
, *tmpnconf
= NULL
;
491 nc_handle
= setnetconfig();
492 if (nc_handle
== NULL
) {
493 /* fails to open netconfig file */
494 syslog (LOG_ERR
, "rpc: failed to open " NETCONFIG
);
495 rpc_createerr
.cf_stat
= RPC_UNKNOWNPROTO
;
496 mutex_unlock(&loopnconf_lock
);
499 while ((nconf
= getnetconfig(nc_handle
)) != NULL
) {
501 if ((strcmp(nconf
->nc_protofmly
, NC_INET6
) == 0 ||
505 strcmp(nconf
->nc_protofmly
, NC_INET
) == 0) &&
506 (nconf
->nc_semantics
== NC_TPI_COTS
||
507 nconf
->nc_semantics
== NC_TPI_COTS_ORD
)) {
508 fd
= __rpc_nconf2fd(nconf
);
510 * Can't create a socket, assume that
511 * this family isn't configured in the kernel.
517 if (!strcmp(nconf
->nc_protofmly
, NC_INET
))
518 hostname
= IN4_LOCALHOST_STRING
;
520 hostname
= IN6_LOCALHOST_STRING
;
523 if (tmpnconf
== NULL
) {
524 rpc_createerr
.cf_stat
= RPC_UNKNOWNPROTO
;
525 mutex_unlock(&loopnconf_lock
);
528 loopnconf
= getnetconfigent(tmpnconf
->nc_netid
);
529 /* loopnconf is never freed */
530 endnetconfig(nc_handle
);
532 mutex_unlock(&loopnconf_lock
);
533 client
= getclnthandle(hostname
, loopnconf
, NULL
);
538 * Set a mapping between program, version and address.
539 * Calls the rpcbind service to do the mapping.
542 rpcb_set(rpcprog_t program
, rpcvers_t version
,
543 const struct netconfig
*nconf
, /* Network structure of transport */
544 const struct netbuf
*address
) /* Services netconfig address */
551 /* parameter checking */
553 rpc_createerr
.cf_stat
= RPC_UNKNOWNPROTO
;
556 if (address
== NULL
) {
557 rpc_createerr
.cf_stat
= RPC_UNKNOWNADDR
;
560 client
= local_rpcb();
565 /* convert to universal */
566 /*LINTED const castaway*/
567 parms
.r_addr
= taddr2uaddr((struct netconfig
*) nconf
,
568 (struct netbuf
*)address
);
570 CLNT_DESTROY(client
);
571 rpc_createerr
.cf_stat
= RPC_N2AXLATEFAILURE
;
572 return (FALSE
); /* no universal address */
574 parms
.r_prog
= program
;
575 parms
.r_vers
= version
;
576 parms
.r_netid
= nconf
->nc_netid
;
578 * Though uid is not being used directly, we still send it for
579 * completeness. For non-unix platforms, perhaps some other
580 * string or an empty string can be sent.
582 snprintf(uidbuf
, sizeof uidbuf
, "%d", geteuid());
583 parms
.r_owner
= uidbuf
;
585 CLNT_CALL(client
, (rpcproc_t
)RPCBPROC_SET
, (xdrproc_t
) xdr_rpcb
,
586 (char *)(void *)&parms
, (xdrproc_t
) xdr_bool
,
587 (char *)(void *)&rslt
, tottimeout
);
589 CLNT_DESTROY(client
);
595 * Remove the mapping between program, version and netbuf address.
596 * Calls the rpcbind service to do the un-mapping.
597 * If netbuf is NULL, unset for all the transports, otherwise unset
598 * only for the given transport.
601 rpcb_unset(rpcprog_t program
, rpcvers_t version
, const struct netconfig
*nconf
)
608 client
= local_rpcb();
613 parms
.r_prog
= program
;
614 parms
.r_vers
= version
;
616 parms
.r_netid
= nconf
->nc_netid
;
618 /*LINTED const castaway*/
619 parms
.r_netid
= (char *) &nullstring
[0]; /* unsets all */
621 /*LINTED const castaway*/
622 parms
.r_addr
= (char *) &nullstring
[0];
623 snprintf(uidbuf
, sizeof uidbuf
, "%d", geteuid());
624 parms
.r_owner
= uidbuf
;
626 CLNT_CALL(client
, (rpcproc_t
)RPCBPROC_UNSET
, (xdrproc_t
) xdr_rpcb
,
627 (char *)(void *)&parms
, (xdrproc_t
) xdr_bool
,
628 (char *)(void *)&rslt
, tottimeout
);
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 for (sp
= relp
; sp
!= NULL
; sp
= sp
->rpcb_entry_next
) {
645 rmap
= &sp
->rpcb_entry_map
;
646 if ((strcmp(nconf
->nc_proto
, rmap
->r_nc_proto
) == 0) &&
647 (strcmp(nconf
->nc_protofmly
, rmap
->r_nc_protofmly
) == 0) &&
648 (nconf
->nc_semantics
== rmap
->r_nc_semantics
) &&
649 (rmap
->r_maddr
!= NULL
) && (rmap
->r_maddr
[0] != 0)) {
650 na
= uaddr2taddr(nconf
, rmap
->r_maddr
);
652 fprintf(stderr
, "\tRemote address is [%s].\n",
656 "\tCouldn't resolve remote address!\n");
665 * Quick check to see if rpcbind is up. Tries to connect over
669 __rpcbind_is_up(void)
671 struct netconfig
*nconf
;
672 struct sockaddr_un sun
;
677 localhandle
= setnetconfig();
678 while ((nconf
= getnetconfig(localhandle
)) != NULL
) {
679 if (nconf
->nc_protofmly
!= NULL
&&
680 strcmp(nconf
->nc_protofmly
, NC_LOOPBACK
) == 0)
686 endnetconfig(localhandle
);
688 memset(&sun
, 0, sizeof sun
);
689 sock
= _socket(AF_LOCAL
, SOCK_STREAM
, 0);
692 sun
.sun_family
= AF_LOCAL
;
693 strncpy(sun
.sun_path
, _PATH_RPCBINDSOCK
, sizeof(sun
.sun_path
));
694 sun
.sun_len
= SUN_LEN(&sun
);
696 if (_connect(sock
, (struct sockaddr
*)&sun
, sun
.sun_len
) < 0) {
706 * An internal function which optimizes rpcb_getaddr function. It also
707 * returns the client handle that it uses to contact the remote rpcbind.
709 * The algorithm used: If the transports is TCP or UDP, it first tries
710 * version 2 (portmap), 4 and then 3 (svr4). This order should be
711 * changed in the next OS release to 4, 2 and 3. We are assuming that by
712 * that time, version 4 would be available on many machines on the network.
713 * With this algorithm, we get performance as well as a plan for
714 * obsoleting version 2.
716 * For all other transports, the algorithm remains as 4 and then 3.
718 * XXX: Due to some problems with t_connect(), we do not reuse the same client
719 * handle for COTS cases and hence in these cases we do not return the
720 * client handle. This code will change if t_connect() ever
721 * starts working properly. Also look under clnt_vc.c.
724 __rpcb_findaddr_timed(rpcprog_t program
, rpcvers_t version
,
725 const struct netconfig
*nconf
, const char *host
,
726 CLIENT
**clpp
, struct timeval
*tp
)
728 static bool_t check_rpcbind
= TRUE
;
729 CLIENT
*client
= NULL
;
731 enum clnt_stat clnt_st
;
734 struct netbuf
*address
= NULL
;
735 rpcvers_t start_vers
= RPCBVERS4
;
736 struct netbuf servaddr
;
738 /* parameter checking */
740 rpc_createerr
.cf_stat
= RPC_UNKNOWNPROTO
;
747 * Use default total timeout if no timeout is specified.
753 /* Try version 2 for TCP or UDP */
754 if (strcmp(nconf
->nc_protofmly
, NC_INET
) == 0) {
756 struct netbuf remote
;
757 rpcvers_t pmapvers
= 2;
758 struct pmap pmapparms
;
761 * Try UDP only - there are some portmappers out
762 * there that use UDP only.
764 if (strcmp(nconf
->nc_proto
, NC_TCP
) == 0) {
765 struct netconfig
*newnconf
;
767 if ((newnconf
= getnetconfigent("udp")) == NULL
) {
768 rpc_createerr
.cf_stat
= RPC_UNKNOWNPROTO
;
771 client
= getclnthandle(host
, newnconf
, &parms
.r_addr
);
772 freenetconfigent(newnconf
);
774 client
= getclnthandle(host
, nconf
, &parms
.r_addr
);
780 * Set version and retry timeout.
782 CLNT_CONTROL(client
, CLSET_RETRY_TIMEOUT
, (char *)&rpcbrmttime
);
783 CLNT_CONTROL(client
, CLSET_VERS
, (char *)&pmapvers
);
785 pmapparms
.pm_prog
= program
;
786 pmapparms
.pm_vers
= version
;
787 pmapparms
.pm_prot
= strcmp(nconf
->nc_proto
, NC_TCP
) ?
788 IPPROTO_UDP
: IPPROTO_TCP
;
789 pmapparms
.pm_port
= 0; /* not needed */
790 clnt_st
= CLNT_CALL(client
, (rpcproc_t
)PMAPPROC_GETPORT
,
791 (xdrproc_t
) xdr_pmap
, (caddr_t
)(void *)&pmapparms
,
792 (xdrproc_t
) xdr_u_short
, (caddr_t
)(void *)&port
,
794 if (clnt_st
!= RPC_SUCCESS
) {
795 if ((clnt_st
== RPC_PROGVERSMISMATCH
) ||
796 (clnt_st
== RPC_PROGUNAVAIL
))
797 goto try_rpcbind
; /* Try different versions */
798 rpc_createerr
.cf_stat
= RPC_PMAPFAILURE
;
799 clnt_geterr(client
, &rpc_createerr
.cf_error
);
801 } else if (port
== 0) {
803 rpc_createerr
.cf_stat
= RPC_PROGNOTREGISTERED
;
807 CLNT_CONTROL(client
, CLGET_SVC_ADDR
, (char *)&remote
);
808 if (((address
= (struct netbuf
*)
809 malloc(sizeof (struct netbuf
))) == NULL
) ||
810 ((address
->buf
= (char *)
811 malloc(remote
.len
)) == NULL
)) {
812 rpc_createerr
.cf_stat
= RPC_SYSTEMERROR
;
813 clnt_geterr(client
, &rpc_createerr
.cf_error
);
820 memcpy(address
->buf
, remote
.buf
, remote
.len
);
821 memcpy(&((char *)address
->buf
)[sizeof (short)],
822 (char *)(void *)&port
, sizeof (short));
823 address
->len
= address
->maxlen
= remote
.len
;
830 * Check if rpcbind is up. This prevents needless delays when
831 * accessing applications such as the keyserver while booting
834 if (check_rpcbind
&& strcmp(nconf
->nc_protofmly
, NC_LOOPBACK
) == 0) {
835 if (!__rpcbind_is_up()) {
836 rpc_createerr
.cf_stat
= RPC_PMAPFAILURE
;
837 rpc_createerr
.cf_error
.re_errno
= 0;
840 check_rpcbind
= FALSE
;
844 * Now we try version 4 and then 3.
845 * We also send the remote system the address we used to
846 * contact it in case it can help to connect back with us
848 parms
.r_prog
= program
;
849 parms
.r_vers
= version
;
850 /*LINTED const castaway*/
851 parms
.r_owner
= (char *) &nullstring
[0]; /* not needed; */
852 /* just for xdring */
853 parms
.r_netid
= nconf
->nc_netid
; /* not really needed */
856 * If a COTS transport is being used, try getting address via CLTS
857 * transport. This works only with version 4.
859 if (nconf
->nc_semantics
== NC_TPI_COTS_ORD
||
860 nconf
->nc_semantics
== NC_TPI_COTS
) {
863 struct netconfig
*nconf_clts
;
864 rpcb_entry_list_ptr relp
= NULL
;
866 if (client
== NULL
) {
867 /* This did not go through the above PORTMAP/TCP code */
868 if ((handle
= __rpc_setconf("datagram_v")) != NULL
) {
869 while ((nconf_clts
= __rpc_getconf(handle
))
871 if (strcmp(nconf_clts
->nc_protofmly
,
872 nconf
->nc_protofmly
) != 0) {
875 client
= getclnthandle(host
, nconf_clts
,
879 __rpc_endconf(handle
);
882 goto regular_rpcbind
; /* Go the regular way */
884 /* This is a UDP PORTMAP handle. Change to version 4 */
886 CLNT_CONTROL(client
, CLSET_VERS
, (char *)(void *)&vers
);
889 * We also send the remote system the address we used to
890 * contact it in case it can help it connect back with us
892 if (parms
.r_addr
== NULL
) {
893 /*LINTED const castaway*/
894 parms
.r_addr
= (char *) &nullstring
[0]; /* for XDRing */
897 CLNT_CONTROL(client
, CLSET_RETRY_TIMEOUT
, (char *)&rpcbrmttime
);
899 clnt_st
= CLNT_CALL(client
, (rpcproc_t
)RPCBPROC_GETADDRLIST
,
900 (xdrproc_t
) xdr_rpcb
, (char *)(void *)&parms
,
901 (xdrproc_t
) xdr_rpcb_entry_list_ptr
,
902 (char *)(void *)&relp
, *tp
);
903 if (clnt_st
== RPC_SUCCESS
) {
904 if ((address
= got_entry(relp
, nconf
)) != NULL
) {
905 xdr_free((xdrproc_t
) xdr_rpcb_entry_list_ptr
,
906 (char *)(void *)&relp
);
907 CLNT_CONTROL(client
, CLGET_SVC_ADDR
,
908 (char *)(void *)&servaddr
);
909 __rpc_fixup_addr(address
, &servaddr
);
912 /* Entry not found for this transport */
913 xdr_free((xdrproc_t
) xdr_rpcb_entry_list_ptr
,
914 (char *)(void *)&relp
);
916 * XXX: should have perhaps returned with error but
917 * since the remote machine might not always be able
918 * to send the address on all transports, we try the
919 * regular way with regular_rpcbind
921 goto regular_rpcbind
;
922 } else if ((clnt_st
== RPC_PROGVERSMISMATCH
) ||
923 (clnt_st
== RPC_PROGUNAVAIL
)) {
924 start_vers
= RPCBVERS
; /* Try version 3 now */
925 goto regular_rpcbind
; /* Try different versions */
927 rpc_createerr
.cf_stat
= RPC_PMAPFAILURE
;
928 clnt_geterr(client
, &rpc_createerr
.cf_error
);
935 /* Now the same transport is to be used to get the address */
936 if (client
&& ((nconf
->nc_semantics
== NC_TPI_COTS_ORD
) ||
937 (nconf
->nc_semantics
== NC_TPI_COTS
))) {
938 /* A CLTS type of client - destroy it */
939 CLNT_DESTROY(client
);
943 if (client
== NULL
) {
944 client
= getclnthandle(host
, nconf
, &parms
.r_addr
);
945 if (client
== NULL
) {
949 if (parms
.r_addr
== NULL
) {
950 /*LINTED const castaway*/
951 parms
.r_addr
= (char *) &nullstring
[0];
954 /* First try from start_vers and then version 3 (RPCBVERS) */
956 CLNT_CONTROL(client
, CLSET_RETRY_TIMEOUT
, (char *) &rpcbrmttime
);
957 for (vers
= start_vers
; vers
>= RPCBVERS
; vers
--) {
958 /* Set the version */
959 CLNT_CONTROL(client
, CLSET_VERS
, (char *)(void *)&vers
);
960 clnt_st
= CLNT_CALL(client
, (rpcproc_t
)RPCBPROC_GETADDR
,
961 (xdrproc_t
) xdr_rpcb
, (char *)(void *)&parms
,
962 (xdrproc_t
) xdr_wrapstring
, (char *)(void *) &ua
, *tp
);
963 if (clnt_st
== RPC_SUCCESS
) {
964 if ((ua
== NULL
) || (ua
[0] == 0)) {
965 /* address unknown */
966 rpc_createerr
.cf_stat
= RPC_PROGNOTREGISTERED
;
969 address
= uaddr2taddr(nconf
, ua
);
971 fprintf(stderr
, "\tRemote address is [%s]\n", ua
);
974 "\tCouldn't resolve remote address!\n");
976 xdr_free((xdrproc_t
)xdr_wrapstring
,
977 (char *)(void *)&ua
);
980 /* We don't know about your universal address */
981 rpc_createerr
.cf_stat
= RPC_N2AXLATEFAILURE
;
984 CLNT_CONTROL(client
, CLGET_SVC_ADDR
,
985 (char *)(void *)&servaddr
);
986 __rpc_fixup_addr(address
, &servaddr
);
988 } else if (clnt_st
== RPC_PROGVERSMISMATCH
) {
989 struct rpc_err rpcerr
;
991 clnt_geterr(client
, &rpcerr
);
992 if (rpcerr
.re_vers
.low
> RPCBVERS4
)
993 goto error
; /* a new version, can't handle */
994 } else if (clnt_st
!= RPC_PROGUNAVAIL
) {
995 /* Cant handle this error */
996 rpc_createerr
.cf_stat
= clnt_st
;
997 clnt_geterr(client
, &rpc_createerr
.cf_error
);
1004 CLNT_DESTROY(client
);
1008 if (nconf
->nc_semantics
!= NC_TPI_CLTS
) {
1009 /* This client is the connectionless one */
1011 CLNT_DESTROY(client
);
1017 } else if (client
) {
1018 CLNT_DESTROY(client
);
1020 if (parms
.r_addr
!= NULL
&& parms
.r_addr
!= nullstring
)
1027 * Find the mapped address for program, version.
1028 * Calls the rpcbind service remotely to do the lookup.
1029 * Uses the transport specified in nconf.
1030 * Returns FALSE (0) if no map exists, else returns 1.
1032 * Assuming that the address is all properly allocated
1035 rpcb_getaddr(rpcprog_t program
, rpcvers_t version
,
1036 const struct netconfig
*nconf
, struct netbuf
*address
,
1041 if ((na
= __rpcb_findaddr_timed(program
, version
,
1042 (struct netconfig
*) nconf
, (char *) host
,
1043 (CLIENT
**) NULL
, (struct timeval
*) NULL
)) == NULL
)
1046 if (na
->len
> address
->maxlen
) {
1047 /* Too long address */
1050 rpc_createerr
.cf_stat
= RPC_FAILED
;
1053 memcpy(address
->buf
, na
->buf
, (size_t)na
->len
);
1054 address
->len
= na
->len
;
1061 * Get a copy of the current maps.
1062 * Calls the rpcbind service remotely to get the maps.
1064 * It returns only a list of the services
1065 * It returns NULL on failure.
1068 rpcb_getmaps(const struct netconfig
*nconf
, const char *host
)
1070 rpcblist_ptr head
= NULL
;
1072 enum clnt_stat clnt_st
;
1075 client
= getclnthandle(host
, nconf
, NULL
);
1076 if (client
== NULL
) {
1079 clnt_st
= CLNT_CALL(client
, (rpcproc_t
)RPCBPROC_DUMP
,
1080 (xdrproc_t
) xdr_void
, NULL
, (xdrproc_t
) xdr_rpcblist_ptr
,
1081 (char *)(void *)&head
, tottimeout
);
1082 if (clnt_st
== RPC_SUCCESS
)
1085 if ((clnt_st
!= RPC_PROGVERSMISMATCH
) &&
1086 (clnt_st
!= RPC_PROGUNAVAIL
)) {
1087 rpc_createerr
.cf_stat
= RPC_RPCBFAILURE
;
1088 clnt_geterr(client
, &rpc_createerr
.cf_error
);
1092 /* fall back to earlier version */
1093 CLNT_CONTROL(client
, CLGET_VERS
, (char *)(void *)&vers
);
1094 if (vers
== RPCBVERS4
) {
1096 CLNT_CONTROL(client
, CLSET_VERS
, (char *)(void *)&vers
);
1097 if (CLNT_CALL(client
, (rpcproc_t
)RPCBPROC_DUMP
,
1098 (xdrproc_t
) xdr_void
, NULL
, (xdrproc_t
) xdr_rpcblist_ptr
,
1099 (char *)(void *)&head
, tottimeout
) == RPC_SUCCESS
)
1102 rpc_createerr
.cf_stat
= RPC_RPCBFAILURE
;
1103 clnt_geterr(client
, &rpc_createerr
.cf_error
);
1106 CLNT_DESTROY(client
);
1111 * rpcbinder remote-call-service interface.
1112 * This routine is used to call the rpcbind remote call service
1113 * which will look up a service program in the address maps, and then
1114 * remotely call that routine with the given parameters. This allows
1115 * programs to do a lookup and call in one step.
1118 rpcb_rmtcall(const struct netconfig
*nconf
, const char *host
, rpcprog_t prog
,
1119 rpcvers_t vers
, rpcproc_t proc
, xdrproc_t xdrargs
, caddr_t argsp
,
1120 xdrproc_t xdrres
, caddr_t resp
, struct timeval tout
,
1121 const struct netbuf
*addr_ptr
)
1124 enum clnt_stat stat
;
1125 struct r_rpcb_rmtcallargs a
;
1126 struct r_rpcb_rmtcallres r
;
1127 rpcvers_t rpcb_vers
;
1130 client
= getclnthandle(host
, nconf
, NULL
);
1131 if (client
== NULL
) {
1132 return (RPC_FAILED
);
1134 /*LINTED const castaway*/
1135 CLNT_CONTROL(client
, CLSET_RETRY_TIMEOUT
, (char *)(void *)&rmttimeout
);
1139 a
.args
.args_val
= argsp
;
1140 a
.xdr_args
= xdrargs
;
1142 r
.results
.results_val
= resp
;
1145 for (rpcb_vers
= RPCBVERS4
; rpcb_vers
>= RPCBVERS
; rpcb_vers
--) {
1146 CLNT_CONTROL(client
, CLSET_VERS
, (char *)(void *)&rpcb_vers
);
1147 stat
= CLNT_CALL(client
, (rpcproc_t
)RPCBPROC_CALLIT
,
1148 (xdrproc_t
) xdr_rpcb_rmtcallargs
, (char *)(void *)&a
,
1149 (xdrproc_t
) xdr_rpcb_rmtcallres
, (char *)(void *)&r
, tout
);
1150 if ((stat
== RPC_SUCCESS
) && (addr_ptr
!= NULL
)) {
1152 /*LINTED const castaway*/
1153 na
= uaddr2taddr((struct netconfig
*) nconf
, r
.addr
);
1155 stat
= RPC_N2AXLATEFAILURE
;
1156 /*LINTED const castaway*/
1157 ((struct netbuf
*) addr_ptr
)->len
= 0;
1160 if (na
->len
> addr_ptr
->maxlen
) {
1161 /* Too long address */
1162 stat
= RPC_FAILED
; /* XXX A better error no */
1165 /*LINTED const castaway*/
1166 ((struct netbuf
*) addr_ptr
)->len
= 0;
1169 memcpy(addr_ptr
->buf
, na
->buf
, (size_t)na
->len
);
1170 /*LINTED const castaway*/
1171 ((struct netbuf
*)addr_ptr
)->len
= na
->len
;
1175 } else if ((stat
!= RPC_PROGVERSMISMATCH
) &&
1176 (stat
!= RPC_PROGUNAVAIL
)) {
1181 CLNT_DESTROY(client
);
1183 xdr_free((xdrproc_t
) xdr_wrapstring
, (char *)(void *)&r
.addr
);
1188 * Gets the time on the remote host.
1189 * Returns 1 if succeeds else 0.
1192 rpcb_gettime(const char *host
, time_t *timep
)
1194 CLIENT
*client
= NULL
;
1196 struct netconfig
*nconf
;
1201 if ((host
== NULL
) || (host
[0] == 0)) {
1206 if ((handle
= __rpc_setconf("netpath")) == NULL
) {
1207 rpc_createerr
.cf_stat
= RPC_UNKNOWNPROTO
;
1210 rpc_createerr
.cf_stat
= RPC_SUCCESS
;
1211 while (client
== NULL
) {
1212 if ((nconf
= __rpc_getconf(handle
)) == NULL
) {
1213 if (rpc_createerr
.cf_stat
== RPC_SUCCESS
)
1214 rpc_createerr
.cf_stat
= RPC_UNKNOWNPROTO
;
1217 client
= getclnthandle(host
, nconf
, NULL
);
1221 __rpc_endconf(handle
);
1222 if (client
== (CLIENT
*) NULL
) {
1226 st
= CLNT_CALL(client
, (rpcproc_t
)RPCBPROC_GETTIME
,
1227 (xdrproc_t
) xdr_void
, NULL
,
1228 (xdrproc_t
) xdr_int
, (char *)(void *)timep
, tottimeout
);
1230 if ((st
== RPC_PROGVERSMISMATCH
) || (st
== RPC_PROGUNAVAIL
)) {
1231 CLNT_CONTROL(client
, CLGET_VERS
, (char *)(void *)&vers
);
1232 if (vers
== RPCBVERS4
) {
1233 /* fall back to earlier version */
1235 CLNT_CONTROL(client
, CLSET_VERS
, (char *)(void *)&vers
);
1236 st
= CLNT_CALL(client
, (rpcproc_t
)RPCBPROC_GETTIME
,
1237 (xdrproc_t
) xdr_void
, NULL
,
1238 (xdrproc_t
) xdr_int
, (char *)(void *)timep
,
1242 CLNT_DESTROY(client
);
1243 return (st
== RPC_SUCCESS
? TRUE
: FALSE
);
1247 * Converts taddr to universal address. This routine should never
1248 * really be called because local n2a libraries are always provided.
1251 rpcb_taddr2uaddr(struct netconfig
*nconf
, struct netbuf
*taddr
)
1257 /* parameter checking */
1258 if (nconf
== NULL
) {
1259 rpc_createerr
.cf_stat
= RPC_UNKNOWNPROTO
;
1262 if (taddr
== NULL
) {
1263 rpc_createerr
.cf_stat
= RPC_UNKNOWNADDR
;
1266 client
= local_rpcb();
1271 CLNT_CALL(client
, (rpcproc_t
)RPCBPROC_TADDR2UADDR
,
1272 (xdrproc_t
) xdr_netbuf
, (char *)(void *)taddr
,
1273 (xdrproc_t
) xdr_wrapstring
, (char *)(void *)&uaddr
, tottimeout
);
1274 CLNT_DESTROY(client
);
1279 * Converts universal address to netbuf. This routine should never
1280 * really be called because local n2a libraries are always provided.
1283 rpcb_uaddr2taddr(struct netconfig
*nconf
, char *uaddr
)
1286 struct netbuf
*taddr
;
1289 /* parameter checking */
1290 if (nconf
== NULL
) {
1291 rpc_createerr
.cf_stat
= RPC_UNKNOWNPROTO
;
1294 if (uaddr
== NULL
) {
1295 rpc_createerr
.cf_stat
= RPC_UNKNOWNADDR
;
1298 client
= local_rpcb();
1303 taddr
= (struct netbuf
*)calloc(1, sizeof (struct netbuf
));
1304 if (taddr
== NULL
) {
1305 CLNT_DESTROY(client
);
1308 if (CLNT_CALL(client
, (rpcproc_t
)RPCBPROC_UADDR2TADDR
,
1309 (xdrproc_t
) xdr_wrapstring
, (char *)(void *)&uaddr
,
1310 (xdrproc_t
) xdr_netbuf
, (char *)(void *)taddr
,
1311 tottimeout
) != RPC_SUCCESS
) {
1315 CLNT_DESTROY(client
);