2 * Copyright (c) 2009, Sun Microsystems, Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * - Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
9 * - Redistributions in binary form must reproduce the above copyright notice,
10 * this list of conditions and the following disclaimer in the documentation
11 * and/or other materials provided with the distribution.
12 * - Neither the name of Sun Microsystems, Inc. nor the names of its
13 * contributors may be used to endorse or promote products derived
14 * from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
28 * @(#)rpcb_svc_com.c 1.18 94/05/02 SMI
29 * $NetBSD: rpcb_svc_com.c,v 1.9 2002/11/08 00:16:39 fvdl Exp $
30 * $FreeBSD: src/usr.sbin/rpcbind/rpcb_svc_com.c,v 1.12 2007/11/07 10:53:39 kevlo Exp $
33 * Copyright (c) 1986 - 1991 by Sun Microsystems, Inc.
38 * The commom server procedure for the rpcbind.
41 #include <sys/types.h>
43 #include <sys/param.h>
45 #include <sys/socket.h>
47 #include <rpc/rpcb_prot.h>
48 #include <rpc/svc_dg.h>
50 #include <netconfig.h>
56 #include <netinet/in.h>
57 #include <rpc/rpc_com.h>
58 #include <rpc/pmap_prot.h>
65 #define RPC_BUF_MAX 65536 /* can be raised if required */
67 static char *nullstring
= "";
68 static int rpcb_rmtcalls
;
70 struct rmtcallfd_list
{
74 struct rmtcallfd_list
*next
;
78 #define MAXTIME_OFF 300 /* 5 minutes */
82 #define FINFO_ACTIVE 0x1
84 struct netbuf
*caller_addr
;
85 u_int32_t forward_xid
;
92 static struct finfo FINFO
[NFORWARD
];
95 static bool_t
xdr_encap_parms(XDR
*, struct encap_parms
*);
96 static bool_t
xdr_rmtcall_args(XDR
*, struct r_rmtcall_args
*);
97 static bool_t
xdr_rmtcall_result(XDR
*, struct r_rmtcall_args
*);
98 static bool_t
xdr_opaque_parms(XDR
*, struct r_rmtcall_args
*);
99 static int find_rmtcallfd_by_netid(char *);
100 static SVCXPRT
*find_rmtcallxprt_by_fd(int);
101 static int forward_register(u_int32_t
, struct netbuf
*, int,
102 char *, rpcproc_t
, rpcvers_t
,
104 static struct finfo
*forward_find(u_int32_t
);
105 static int free_slot_by_xid(u_int32_t
);
106 static int free_slot_by_index(int);
107 static int netbufcmp(struct netbuf
*, struct netbuf
*);
108 static struct netbuf
*netbufdup(struct netbuf
*);
109 static void netbuffree(struct netbuf
*);
110 static int check_rmtcalls(struct pollfd
*, int);
111 static void xprt_set_caller(SVCXPRT
*, struct finfo
*);
112 static void send_svcsyserr(SVCXPRT
*, struct finfo
*);
113 static void handle_reply(int, SVCXPRT
*);
114 static void find_versions(rpcprog_t
, char *, rpcvers_t
*,
116 static rpcblist_ptr
find_service(rpcprog_t
, rpcvers_t
, char *);
117 static char *getowner(SVCXPRT
*, char *, size_t);
118 static int add_pmaplist(RPCB
*);
119 static int del_pmaplist(RPCB
*);
122 * Set a mapping of program, version, netid
126 rpcbproc_set_com(void *arg
, struct svc_req
*rqstp __unused
, SVCXPRT
*transp
,
127 rpcvers_t rpcbversnum
)
129 RPCB
*regp
= (RPCB
*)arg
;
135 fprintf(stderr
, "RPCB_SET request for (%lu, %lu, %s, %s) : ",
136 (unsigned long)regp
->r_prog
, (unsigned long)regp
->r_vers
,
137 regp
->r_netid
, regp
->r_addr
);
139 ans
= map_set(regp
, getowner(transp
, owner
, sizeof owner
));
142 fprintf(stderr
, "%s\n", ans
== TRUE
? "succeeded" : "failed");
144 /* XXX: should have used some defined constant here */
145 rpcbs_set(rpcbversnum
- 2, ans
);
150 map_set(RPCB
*regp
, char *owner
)
153 rpcblist_ptr rbl
, fnd
;
157 * check to see if already used
158 * find_service returns a hit even if
159 * the versions don't match, so check for it
161 fnd
= find_service(reg
.r_prog
, reg
.r_vers
, reg
.r_netid
);
162 if (fnd
&& (fnd
->rpcb_map
.r_vers
== reg
.r_vers
)) {
163 if (!strcmp(fnd
->rpcb_map
.r_addr
, reg
.r_addr
))
165 * if these match then it is already
166 * registered so just say "OK".
173 * add to the end of the list
175 rbl
= malloc(sizeof (RPCBLIST
));
178 a
= &(rbl
->rpcb_map
);
179 a
->r_prog
= reg
.r_prog
;
180 a
->r_vers
= reg
.r_vers
;
181 a
->r_netid
= strdup(reg
.r_netid
);
182 a
->r_addr
= strdup(reg
.r_addr
);
183 a
->r_owner
= strdup(owner
);
184 if (!a
->r_addr
|| !a
->r_netid
|| !a
->r_owner
) {
194 rbl
->rpcb_next
= NULL
;
195 if (list_rbl
== NULL
) {
198 for (fnd
= list_rbl
; fnd
->rpcb_next
;
199 fnd
= fnd
->rpcb_next
)
201 fnd
->rpcb_next
= rbl
;
210 * Unset a mapping of program, version, netid
214 rpcbproc_unset_com(void *arg
, struct svc_req
*rqstp __unused
, SVCXPRT
*transp
,
215 rpcvers_t rpcbversnum
)
217 RPCB
*regp
= (RPCB
*)arg
;
223 fprintf(stderr
, "RPCB_UNSET request for (%lu, %lu, %s) : ",
224 (unsigned long)regp
->r_prog
, (unsigned long)regp
->r_vers
,
227 ans
= map_unset(regp
, getowner(transp
, owner
, sizeof owner
));
230 fprintf(stderr
, "%s\n", ans
== TRUE
? "succeeded" : "failed");
232 /* XXX: should have used some defined constant here */
233 rpcbs_unset(rpcbversnum
- 2, ans
);
238 map_unset(RPCB
*regp
, char *owner
)
241 rpcblist_ptr rbl
, prev
, tmp
;
246 for (prev
= NULL
, rbl
= list_rbl
; rbl
; /* cstyle */) {
247 if ((rbl
->rpcb_map
.r_prog
!= regp
->r_prog
) ||
248 (rbl
->rpcb_map
.r_vers
!= regp
->r_vers
) ||
249 (regp
->r_netid
[0] && strcasecmp(regp
->r_netid
,
250 rbl
->rpcb_map
.r_netid
))) {
251 /* both rbl & prev move forwards */
253 rbl
= rbl
->rpcb_next
;
257 * Check whether appropriate uid. Unset only
258 * if superuser or the owner itself.
260 if (strcmp(owner
, "superuser") &&
261 strcmp(rbl
->rpcb_map
.r_owner
, owner
))
263 /* found it; rbl moves forward, prev stays */
266 rbl
= rbl
->rpcb_next
;
270 prev
->rpcb_next
= rbl
;
271 free(tmp
->rpcb_map
.r_addr
);
272 free(tmp
->rpcb_map
.r_netid
);
273 free(tmp
->rpcb_map
.r_owner
);
281 * We return 1 either when the entry was not there or it
282 * was able to unset it. It can come to this point only if
283 * atleast one of the conditions is true.
289 delete_prog(unsigned int prog
)
294 for (rbl
= list_rbl
; rbl
!= NULL
; rbl
= rbl
->rpcb_next
) {
295 if ((rbl
->rpcb_map
.r_prog
!= prog
))
297 if (is_bound(rbl
->rpcb_map
.r_netid
, rbl
->rpcb_map
.r_addr
))
299 reg
.r_prog
= rbl
->rpcb_map
.r_prog
;
300 reg
.r_vers
= rbl
->rpcb_map
.r_vers
;
301 reg
.r_netid
= strdup(rbl
->rpcb_map
.r_netid
);
302 map_unset(®
, "superuser");
308 rpcbproc_getaddr_com(RPCB
*regp
, struct svc_req
*rqstp __unused
,
309 SVCXPRT
*transp
, rpcvers_t rpcbversnum
, rpcvers_t verstype
)
315 if (uaddr
!= NULL
&& uaddr
!= nullstring
) {
319 fnd
= find_service(regp
->r_prog
, regp
->r_vers
, transp
->xp_netid
);
320 if (fnd
&& ((verstype
== RPCB_ALLVERS
) ||
321 (regp
->r_vers
== fnd
->rpcb_map
.r_vers
))) {
322 if (*(regp
->r_addr
) != '\0') { /* may contain a hint about */
323 saddr
= regp
->r_addr
; /* the interface that we */
325 if (!(uaddr
= mergeaddr(transp
, transp
->xp_netid
,
326 fnd
->rpcb_map
.r_addr
, saddr
))) {
327 /* Try whatever we have */
328 uaddr
= strdup(fnd
->rpcb_map
.r_addr
);
329 } else if (!uaddr
[0]) {
331 * The server died. Unset all versions of this prog.
333 delete_prog(regp
->r_prog
);
341 fprintf(stderr
, "getaddr: %s\n", uaddr
);
343 /* XXX: should have used some defined constant here */
344 rpcbs_getaddr(rpcbversnum
- 2, regp
->r_prog
, regp
->r_vers
,
345 transp
->xp_netid
, uaddr
);
346 return (void *)&uaddr
;
351 rpcbproc_gettime_com(void *arg __unused
, struct svc_req
*rqstp __unused
,
352 SVCXPRT
*transp __unused
, rpcvers_t rpcbversnum __unused
)
354 static time_t curtime
;
357 return (void *)&curtime
;
361 * Convert uaddr to taddr. Should be used only by
362 * local servers/clients. (kernel level stuff only)
366 rpcbproc_uaddr2taddr_com(void *arg
, struct svc_req
*rqstp __unused
,
367 SVCXPRT
*transp
, rpcvers_t rpcbversnum __unused
)
369 char **uaddrp
= (char **)arg
;
370 struct netconfig
*nconf
;
371 static struct netbuf nbuf
;
372 static struct netbuf
*taddr
;
379 if (((nconf
= rpcbind_get_conf(transp
->xp_netid
)) == NULL
) ||
380 ((taddr
= uaddr2taddr(nconf
, *uaddrp
)) == NULL
)) {
381 memset((char *)&nbuf
, 0, sizeof (struct netbuf
));
382 return (void *)&nbuf
;
384 return (void *)taddr
;
388 * Convert taddr to uaddr. Should be used only by
389 * local servers/clients. (kernel level stuff only)
393 rpcbproc_taddr2uaddr_com(void *arg
, struct svc_req
*rqstp __unused
,
394 SVCXPRT
*transp
, rpcvers_t rpcbversnum __unused
)
396 struct netbuf
*taddr
= (struct netbuf
*)arg
;
398 struct netconfig
*nconf
;
403 if ((fd
= open("/dev/null", O_RDONLY
)) == -1) {
404 uaddr
= (char *)strerror(errno
);
407 #endif /* CHEW_FDS */
408 if (uaddr
!= NULL
&& uaddr
!= nullstring
) {
412 if (((nconf
= rpcbind_get_conf(transp
->xp_netid
)) == NULL
) ||
413 ((uaddr
= taddr2uaddr(nconf
, taddr
)) == NULL
)) {
416 return (void *)&uaddr
;
421 xdr_encap_parms(XDR
*xdrs
, struct encap_parms
*epp
)
423 return (xdr_bytes(xdrs
, &(epp
->args
), (u_int
*) &(epp
->arglen
),
428 * XDR remote call arguments. It ignores the address part.
429 * written for XDR_DECODE direction only
432 xdr_rmtcall_args(XDR
*xdrs
, struct r_rmtcall_args
*cap
)
434 /* does not get the address or the arguments */
435 if (xdr_rpcprog(xdrs
, &(cap
->rmt_prog
)) &&
436 xdr_rpcvers(xdrs
, &(cap
->rmt_vers
)) &&
437 xdr_rpcproc(xdrs
, &(cap
->rmt_proc
))) {
438 return (xdr_encap_parms(xdrs
, &(cap
->rmt_args
)));
444 * XDR remote call results along with the address. Ignore
445 * program number, version number and proc number.
446 * Written for XDR_ENCODE direction only.
449 xdr_rmtcall_result(XDR
*xdrs
, struct r_rmtcall_args
*cap
)
454 if (cap
->rmt_localvers
== PMAPVERS
) {
455 int h1
, h2
, h3
, h4
, p1
, p2
;
458 /* interpret the universal address for TCP/IP */
459 if (sscanf(cap
->rmt_uaddr
, "%d.%d.%d.%d.%d.%d",
460 &h1
, &h2
, &h3
, &h4
, &p1
, &p2
) != 6)
462 port
= ((p1
& 0xff) << 8) + (p2
& 0xff);
463 result
= xdr_u_long(xdrs
, &port
);
466 if ((cap
->rmt_localvers
== RPCBVERS
) ||
467 (cap
->rmt_localvers
== RPCBVERS4
)) {
468 result
= xdr_wrapstring(xdrs
, &(cap
->rmt_uaddr
));
473 return (xdr_encap_parms(xdrs
, &(cap
->rmt_args
)));
478 * only worries about the struct encap_parms part of struct r_rmtcall_args.
479 * The arglen must already be set!!
482 xdr_opaque_parms(XDR
*xdrs
, struct r_rmtcall_args
*cap
)
484 return (xdr_opaque(xdrs
, cap
->rmt_args
.args
, cap
->rmt_args
.arglen
));
487 static struct rmtcallfd_list
*rmthead
;
488 static struct rmtcallfd_list
*rmttail
;
491 create_rmtcall_fd(struct netconfig
*nconf
)
494 struct rmtcallfd_list
*rmt
;
497 if ((fd
= __rpc_nconf2fd(nconf
)) == -1) {
500 "create_rmtcall_fd: couldn't open \"%s\" (errno %d)\n",
501 nconf
->nc_device
, errno
);
504 xprt
= svc_tli_create(fd
, 0, NULL
, 0, 0);
508 "create_rmtcall_fd: svc_tli_create failed\n");
511 rmt
= malloc(sizeof (struct rmtcallfd_list
));
513 syslog(LOG_ERR
, "create_rmtcall_fd: no memory!");
517 rmt
->netid
= strdup(nconf
->nc_netid
);
518 xprt
->xp_netid
= rmt
->netid
;
521 if (rmthead
== NULL
) {
528 /* XXX not threadsafe */
531 FD_SET(fd
, &svc_fdset
);
536 find_rmtcallfd_by_netid(char *netid
)
538 struct rmtcallfd_list
*rmt
;
540 for (rmt
= rmthead
; rmt
!= NULL
; rmt
= rmt
->next
) {
541 if (strcmp(netid
, rmt
->netid
) == 0) {
549 find_rmtcallxprt_by_fd(int fd
)
551 struct rmtcallfd_list
*rmt
;
553 for (rmt
= rmthead
; rmt
!= NULL
; rmt
= rmt
->next
) {
563 * Call a remote procedure service. This procedure is very quiet when things
564 * go wrong. The proc is written to support broadcast rpc. In the broadcast
565 * case, a machine should shut-up instead of complain, lest the requestor be
566 * overrun with complaints at the expense of not hearing a valid reply.
567 * When receiving a request and verifying that the service exists, we
569 * receive the request
571 * open a new TLI endpoint on the same transport on which we received
572 * the original request
574 * remember the original request's XID (which requires knowing the format
575 * of the svc_dg_data structure)
577 * forward the request, with a new XID, to the requested service,
578 * remembering the XID used to send this request (for later use in
579 * reassociating the answer with the original request), the requestor's
580 * address, the file descriptor on which the forwarded request is
581 * made and the service's address.
583 * mark the file descriptor on which we anticipate receiving a reply from
584 * the service and one to select for in our private svc_run procedure
586 * At some time in the future, a reply will be received from the service to
587 * which we forwarded the request. At that time, we detect that the socket
588 * used was for forwarding (by looking through the finfo structures to see
589 * whether the fd corresponds to one of those) and call handle_reply() to
593 * bundle the reply, along with the service's universal address
595 * create a SVCXPRT structure and use a version of svc_sendreply
596 * that allows us to specify the reply XID and destination, send the reply
597 * to the original requestor.
601 rpcbproc_callit_com(struct svc_req
*rqstp
, SVCXPRT
*transp
,
602 rpcproc_t reply_type
, rpcvers_t versnum
)
605 struct netconfig
*nconf
;
606 struct netbuf
*caller
;
607 struct r_rmtcall_args a
;
608 char *buf_alloc
= NULL
, *outbufp
;
609 char *outbuf_alloc
= NULL
;
610 char buf
[RPC_BUF_MAX
], outbuf
[RPC_BUF_MAX
];
611 struct netbuf
*na
= NULL
;
612 struct rpc_msg call_msg
;
618 char *uaddr
, *m_uaddr
= NULL
, *local_uaddr
= NULL
;
620 struct __rpc_sockinfo si
;
621 struct sockaddr
*localsa
;
624 if (!__rpc_fd2sockinfo(transp
->xp_fd
, &si
)) {
625 if (reply_type
== RPCBPROC_INDIRECT
)
626 svcerr_systemerr(transp
);
629 if (si
.si_socktype
!= SOCK_DGRAM
)
630 return; /* Only datagram type accepted */
631 sendsz
= __rpc_get_t_size(si
.si_af
, si
.si_proto
, UDPMSGSIZE
);
632 if (sendsz
== 0) { /* data transfer not supported */
633 if (reply_type
== RPCBPROC_INDIRECT
)
634 svcerr_systemerr(transp
);
638 * Should be multiple of 4 for XDR.
640 sendsz
= ((sendsz
+ 3) / 4) * 4;
641 if (sendsz
> RPC_BUF_MAX
) {
643 buf_alloc
= alloca(sendsz
); /* not in IDR2? */
645 buf_alloc
= malloc(sendsz
);
647 if (buf_alloc
== NULL
) {
650 "rpcbproc_callit_com: No Memory!\n");
651 if (reply_type
== RPCBPROC_INDIRECT
)
652 svcerr_systemerr(transp
);
655 a
.rmt_args
.args
= buf_alloc
;
657 a
.rmt_args
.args
= buf
;
660 call_msg
.rm_xid
= 0; /* For error checking purposes */
661 if (!svc_getargs(transp
, (xdrproc_t
) xdr_rmtcall_args
, (char *) &a
)) {
662 if (reply_type
== RPCBPROC_INDIRECT
)
663 svcerr_decode(transp
);
666 "rpcbproc_callit_com: svc_getargs failed\n");
670 if (!check_callit(transp
, &a
, versnum
)) {
671 svcerr_weakauth(transp
);
675 caller
= svc_getrpccaller(transp
);
678 uaddr
= taddr2uaddr(rpcbind_get_conf(transp
->xp_netid
), caller
);
679 fprintf(stderr
, "%s %s req for (%lu, %lu, %lu, %s) from %s : ",
680 versnum
== PMAPVERS
? "pmap_rmtcall" :
681 versnum
== RPCBVERS
? "rpcb_rmtcall" :
682 versnum
== RPCBVERS4
? "rpcb_indirect" : "unknown",
683 reply_type
== RPCBPROC_INDIRECT
? "indirect" : "callit",
684 (unsigned long)a
.rmt_prog
, (unsigned long)a
.rmt_vers
,
685 (unsigned long)a
.rmt_proc
, transp
->xp_netid
,
686 uaddr
? uaddr
: "unknown");
692 rbl
= find_service(a
.rmt_prog
, a
.rmt_vers
, transp
->xp_netid
);
694 rpcbs_rmtcall(versnum
- 2, reply_type
, a
.rmt_prog
, a
.rmt_vers
,
695 a
.rmt_proc
, transp
->xp_netid
, rbl
);
700 fprintf(stderr
, "not found\n");
702 if (reply_type
== RPCBPROC_INDIRECT
)
703 svcerr_noprog(transp
);
706 if (rbl
->rpcb_map
.r_vers
!= a
.rmt_vers
) {
707 if (reply_type
== RPCBPROC_INDIRECT
) {
708 rpcvers_t vers_low
, vers_high
;
710 find_versions(a
.rmt_prog
, transp
->xp_netid
,
711 &vers_low
, &vers_high
);
712 svcerr_progvers(transp
, vers_low
, vers_high
);
719 fprintf(stderr
, "found at uaddr %s\n", rbl
->rpcb_map
.r_addr
);
722 * Check whether this entry is valid and a server is present
723 * Mergeaddr() returns NULL if no such entry is present, and
724 * returns "" if the entry was present but the server is not
725 * present (i.e., it crashed).
727 if (reply_type
== RPCBPROC_INDIRECT
) {
728 uaddr
= mergeaddr(transp
, transp
->xp_netid
,
729 rbl
->rpcb_map
.r_addr
, NULL
);
730 if (uaddr
== NULL
|| uaddr
[0] == '\0') {
731 svcerr_noprog(transp
);
738 nconf
= rpcbind_get_conf(transp
->xp_netid
);
740 if (reply_type
== RPCBPROC_INDIRECT
)
741 svcerr_systemerr(transp
);
744 "rpcbproc_callit_com: rpcbind_get_conf failed\n");
747 localsa
= local_sa(((struct sockaddr
*)caller
->buf
)->sa_family
);
748 if (localsa
== NULL
) {
751 "rpcbproc_callit_com: no local address\n");
754 tbuf
.len
= tbuf
.maxlen
= localsa
->sa_len
;
757 addrmerge(&tbuf
, rbl
->rpcb_map
.r_addr
, NULL
, nconf
->nc_netid
);
758 m_uaddr
= addrmerge(caller
, rbl
->rpcb_map
.r_addr
, NULL
,
762 fprintf(stderr
, "merged uaddr %s\n", m_uaddr
);
764 if ((fd
= find_rmtcallfd_by_netid(nconf
->nc_netid
)) == -1) {
765 if (reply_type
== RPCBPROC_INDIRECT
)
766 svcerr_systemerr(transp
);
769 xidp
= __rpcb_get_dg_xidp(transp
);
770 switch (forward_register(*xidp
, caller
, fd
, m_uaddr
, reply_type
,
771 versnum
, &call_msg
.rm_xid
)) {
773 /* Success; forward_register() will free m_uaddr for us. */
778 * A duplicate request for the slow server. Let's not
779 * beat on it any more.
783 "rpcbproc_callit_com: duplicate request\n");
786 /* forward_register failed. Perhaps no memory. */
789 "rpcbproc_callit_com: forward_register failed\n");
796 "rpcbproc_callit_com: original XID %x, new XID %x\n",
797 *xidp
, call_msg
.rm_xid
);
799 call_msg
.rm_direction
= CALL
;
800 call_msg
.rm_call
.cb_rpcvers
= RPC_MSG_VERSION
;
801 call_msg
.rm_call
.cb_prog
= a
.rmt_prog
;
802 call_msg
.rm_call
.cb_vers
= a
.rmt_vers
;
803 if (sendsz
> RPC_BUF_MAX
) {
805 outbuf_alloc
= alloca(sendsz
); /* not in IDR2? */
807 outbuf_alloc
= malloc(sendsz
);
809 if (outbuf_alloc
== NULL
) {
810 if (reply_type
== RPCBPROC_INDIRECT
)
811 svcerr_systemerr(transp
);
814 "rpcbproc_callit_com: No memory!\n");
817 xdrmem_create(&outxdr
, outbuf_alloc
, sendsz
, XDR_ENCODE
);
819 xdrmem_create(&outxdr
, outbuf
, sendsz
, XDR_ENCODE
);
821 if (!xdr_callhdr(&outxdr
, &call_msg
)) {
822 if (reply_type
== RPCBPROC_INDIRECT
)
823 svcerr_systemerr(transp
);
826 "rpcbproc_callit_com: xdr_callhdr failed\n");
829 if (!xdr_u_int32_t(&outxdr
, &(a
.rmt_proc
))) {
830 if (reply_type
== RPCBPROC_INDIRECT
)
831 svcerr_systemerr(transp
);
834 "rpcbproc_callit_com: xdr_u_long failed\n");
838 if (rqstp
->rq_cred
.oa_flavor
== AUTH_NULL
) {
839 auth
= authnone_create();
840 } else if (rqstp
->rq_cred
.oa_flavor
== AUTH_SYS
) {
841 struct authunix_parms
*au
;
843 au
= (struct authunix_parms
*)rqstp
->rq_clntcred
;
844 auth
= authunix_create(au
->aup_machname
,
845 au
->aup_uid
, au
->aup_gid
,
846 au
->aup_len
, au
->aup_gids
);
847 if (auth
== NULL
) /* fall back */
848 auth
= authnone_create();
850 /* we do not support any other authentication scheme */
853 "rpcbproc_callit_com: oa_flavor != AUTH_NONE and oa_flavor != AUTH_SYS\n");
854 if (reply_type
== RPCBPROC_INDIRECT
)
855 svcerr_weakauth(transp
); /* XXX too strong.. */
859 if (reply_type
== RPCBPROC_INDIRECT
)
860 svcerr_systemerr(transp
);
863 "rpcbproc_callit_com: authwhatever_create returned NULL\n");
866 if (!AUTH_MARSHALL(auth
, &outxdr
)) {
867 if (reply_type
== RPCBPROC_INDIRECT
)
868 svcerr_systemerr(transp
);
872 "rpcbproc_callit_com: AUTH_MARSHALL failed\n");
876 if (!xdr_opaque_parms(&outxdr
, &a
)) {
877 if (reply_type
== RPCBPROC_INDIRECT
)
878 svcerr_systemerr(transp
);
881 "rpcbproc_callit_com: xdr_opaque_parms failed\n");
884 outlen
= (int) XDR_GETPOS(&outxdr
);
886 outbufp
= outbuf_alloc
;
890 na
= uaddr2taddr(nconf
, local_uaddr
);
892 if (reply_type
== RPCBPROC_INDIRECT
)
893 svcerr_systemerr(transp
);
897 if (sendto(fd
, outbufp
, outlen
, 0, (struct sockaddr
*)na
->buf
, na
->len
)
901 "rpcbproc_callit_com: sendto failed: errno %d\n", errno
);
902 if (reply_type
== RPCBPROC_INDIRECT
)
903 svcerr_systemerr(transp
);
909 if (call_msg
.rm_xid
!= 0)
910 free_slot_by_xid(call_msg
.rm_xid
);
927 * Makes an entry into the FIFO for the given request.
928 * Returns 1 on success, 0 if this is a duplicate request, or -1 on error.
929 * *callxidp is set to the xid of the call.
932 forward_register(u_int32_t caller_xid
, struct netbuf
*caller_addr
,
933 int forward_fd
, char *uaddr
, rpcproc_t reply_type
,
934 rpcvers_t versnum
, u_int32_t
*callxidp
)
938 time_t min_time
, time_now
;
939 static u_int32_t lastxid
;
942 min_time
= FINFO
[0].time
;
943 time_now
= time(NULL
);
946 lastxid
= time_now
* NFORWARD
;
949 * Check if it is a duplicate entry. Then,
950 * try to find an empty slot. If not available, then
951 * use the slot with the earliest time.
953 for (i
= 0; i
< NFORWARD
; i
++) {
954 if (FINFO
[i
].flag
& FINFO_ACTIVE
) {
955 if ((FINFO
[i
].caller_xid
== caller_xid
) &&
956 (FINFO
[i
].reply_type
== reply_type
) &&
957 (FINFO
[i
].versnum
== versnum
) &&
958 (!netbufcmp(FINFO
[i
].caller_addr
,
960 FINFO
[i
].time
= time(NULL
);
961 return (0); /* Duplicate entry */
963 /* Should we wait any longer */
964 if ((time_now
- FINFO
[i
].time
) > MAXTIME_OFF
)
965 free_slot_by_index(i
);
969 if ((FINFO
[i
].flag
& FINFO_ACTIVE
) == 0) {
971 } else if (FINFO
[i
].time
< min_time
) {
973 min_time
= FINFO
[i
].time
;
978 /* use this empty slot */
981 free_slot_by_index(j
);
983 if ((FINFO
[j
].caller_addr
= netbufdup(caller_addr
)) == NULL
) {
986 rpcb_rmtcalls
++; /* no of pending calls */
987 FINFO
[j
].flag
= FINFO_ACTIVE
;
988 FINFO
[j
].reply_type
= reply_type
;
989 FINFO
[j
].versnum
= versnum
;
990 FINFO
[j
].time
= time_now
;
991 FINFO
[j
].caller_xid
= caller_xid
;
992 FINFO
[j
].forward_fd
= forward_fd
;
994 * Though uaddr is not allocated here, it will still be freed
995 * from free_slot_*().
997 FINFO
[j
].uaddr
= uaddr
;
998 lastxid
= lastxid
+ NFORWARD
;
999 /* Don't allow a zero xid below. */
1000 if ((u_int32_t
)(lastxid
+ NFORWARD
) <= NFORWARD
)
1002 FINFO
[j
].forward_xid
= lastxid
+ j
; /* encode slot */
1003 *callxidp
= FINFO
[j
].forward_xid
; /* forward on this xid */
1007 static struct finfo
*
1008 forward_find(u_int32_t reply_xid
)
1012 i
= reply_xid
% (u_int32_t
)NFORWARD
;
1013 if ((FINFO
[i
].flag
& FINFO_ACTIVE
) &&
1014 (FINFO
[i
].forward_xid
== reply_xid
)) {
1021 free_slot_by_xid(u_int32_t xid
)
1025 entry
= xid
% (u_int32_t
)NFORWARD
;
1026 return (free_slot_by_index(entry
));
1030 free_slot_by_index(int index
)
1035 if (fi
->flag
& FINFO_ACTIVE
) {
1036 netbuffree(fi
->caller_addr
);
1037 /* XXX may be too big, but can't access xprt array here */
1038 if (fi
->forward_fd
>= svc_maxfd
)
1041 fi
->flag
&= ~FINFO_ACTIVE
;
1049 netbufcmp(struct netbuf
*n1
, struct netbuf
*n2
)
1051 return ((n1
->len
!= n2
->len
) || memcmp(n1
->buf
, n2
->buf
, n1
->len
));
1055 netbuf_copybuf(struct netbuf
*dst
, const struct netbuf
*src
)
1058 assert(dst
->buf
== NULL
);
1060 if ((dst
->buf
= malloc(src
->len
)) == NULL
)
1063 dst
->maxlen
= dst
->len
= src
->len
;
1064 memcpy(dst
->buf
, src
->buf
, src
->len
);
1068 static struct netbuf
*
1069 netbufdup(struct netbuf
*ap
)
1073 if ((np
= calloc(1, sizeof(struct netbuf
))) == NULL
)
1075 if (netbuf_copybuf(np
, ap
) == FALSE
) {
1083 netbuffree(struct netbuf
*ap
)
1091 #define MASKVAL (POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND)
1092 extern bool_t
__svc_clean_idle(fd_set
*, int, bool_t
);
1098 struct pollfd pollfds
[FD_SETSIZE
];
1099 int poll_ret
, check_ret
;
1101 #ifdef SVC_RUN_DEBUG
1109 for (n
= 0; n
<= svc_maxfd
; n
++) {
1110 if (FD_ISSET(n
, &svc_fdset
)) {
1112 p
->events
= MASKVAL
;
1118 #ifdef SVC_RUN_DEBUG
1120 fprintf(stderr
, "polling for read on fd < ");
1121 for (i
= 0, p
= pollfds
; i
< nfds
; i
++, p
++)
1123 fprintf(stderr
, "%d ", p
->fd
);
1124 fprintf(stderr
, ">\n");
1127 switch (poll_ret
= poll(pollfds
, nfds
, 30 * 1000)) {
1130 * We ignore all errors, continuing with the assumption
1131 * that it was set by the signal handlers (or any
1132 * other outside event) and not caused by poll().
1135 cleanfds
= svc_fdset
;
1136 __svc_clean_idle(&cleanfds
, 30, FALSE
);
1139 #ifdef SVC_RUN_DEBUG
1141 fprintf(stderr
, "poll returned read fds < ");
1142 for (i
= 0, p
= pollfds
; i
< nfds
; i
++, p
++)
1144 fprintf(stderr
, "%d ", p
->fd
);
1145 fprintf(stderr
, ">\n");
1149 * If we found as many replies on callback fds
1150 * as the number of descriptors selectable which
1151 * poll() returned, there can be no more so we
1152 * don't call svc_getreq_poll. Otherwise, there
1153 * must be another so we must call svc_getreq_poll.
1155 if ((check_ret
= check_rmtcalls(pollfds
, nfds
)) ==
1158 svc_getreq_poll(pollfds
, poll_ret
-check_ret
);
1160 #ifdef SVC_RUN_DEBUG
1162 fprintf(stderr
, "svc_maxfd now %u\n", svc_maxfd
);
1169 check_rmtcalls(struct pollfd
*pfds
, int nfds
)
1171 int j
, ncallbacks_found
= 0, rmtcalls_pending
;
1174 if (rpcb_rmtcalls
== 0)
1177 rmtcalls_pending
= rpcb_rmtcalls
;
1178 for (j
= 0; j
< nfds
; j
++) {
1179 if ((xprt
= find_rmtcallxprt_by_fd(pfds
[j
].fd
)) != NULL
) {
1180 if (pfds
[j
].revents
) {
1182 #ifdef DEBUG_RMTCALL
1185 "my_svc_run: polled on forwarding fd %d, netid %s - calling handle_reply\n",
1186 pfds
[j
].fd
, xprt
->xp_netid
);
1188 handle_reply(pfds
[j
].fd
, xprt
);
1189 pfds
[j
].revents
= 0;
1190 if (ncallbacks_found
>= rmtcalls_pending
) {
1196 return (ncallbacks_found
);
1200 xprt_set_caller(SVCXPRT
*xprt
, struct finfo
*fi
)
1204 netbuf_copybuf(svc_getrpccaller(xprt
), fi
->caller_addr
);
1205 xidp
= __rpcb_get_dg_xidp(xprt
);
1206 *xidp
= fi
->caller_xid
;
1210 * Call svcerr_systemerr() only if RPCBVERS4
1213 send_svcsyserr(SVCXPRT
*xprt
, struct finfo
*fi
)
1215 if (fi
->reply_type
== RPCBPROC_INDIRECT
) {
1216 xprt_set_caller(xprt
, fi
);
1217 svcerr_systemerr(xprt
);
1223 handle_reply(int fd
, SVCXPRT
*xprt
)
1226 struct rpc_msg reply_msg
;
1227 struct rpc_err reply_error
;
1230 int inlen
, pos
, len
;
1231 struct r_rmtcall_args a
;
1232 struct sockaddr_storage ss
;
1234 #ifdef SVC_RUN_DEBUG
1238 buffer
= malloc(RPC_BUF_MAX
);
1243 inlen
= recvfrom(fd
, buffer
, RPC_BUF_MAX
, 0,
1244 (struct sockaddr
*)&ss
, &fromlen
);
1245 } while (inlen
< 0 && errno
== EINTR
);
1249 "handle_reply: recvfrom returned %d, errno %d\n", inlen
, errno
);
1253 reply_msg
.acpted_rply
.ar_verf
= _null_auth
;
1254 reply_msg
.acpted_rply
.ar_results
.where
= 0;
1255 reply_msg
.acpted_rply
.ar_results
.proc
= (xdrproc_t
) xdr_void
;
1257 xdrmem_create(&reply_xdrs
, buffer
, (u_int
)inlen
, XDR_DECODE
);
1258 if (!xdr_replymsg(&reply_xdrs
, &reply_msg
)) {
1260 fprintf(stderr
, "handle_reply: xdr_replymsg failed\n");
1263 fi
= forward_find(reply_msg
.rm_xid
);
1264 #ifdef SVC_RUN_DEBUG
1266 fprintf(stderr
, "handle_reply: reply xid: %d fi addr: %p\n",
1267 reply_msg
.rm_xid
, fi
);
1273 _seterr_reply(&reply_msg
, &reply_error
);
1274 if (reply_error
.re_status
!= RPC_SUCCESS
) {
1276 fprintf(stderr
, "handle_reply: %s\n",
1277 clnt_sperrno(reply_error
.re_status
));
1278 send_svcsyserr(xprt
, fi
);
1281 pos
= XDR_GETPOS(&reply_xdrs
);
1283 a
.rmt_args
.args
= &buffer
[pos
];
1284 a
.rmt_args
.arglen
= len
;
1285 a
.rmt_uaddr
= fi
->uaddr
;
1286 a
.rmt_localvers
= fi
->versnum
;
1288 xprt_set_caller(xprt
, fi
);
1289 #ifdef SVC_RUN_DEBUG
1290 uaddr
= taddr2uaddr(rpcbind_get_conf("udp"),
1291 svc_getrpccaller(xprt
));
1293 fprintf(stderr
, "handle_reply: forwarding address %s to %s\n",
1294 a
.rmt_uaddr
, uaddr
? uaddr
: "unknown");
1299 svc_sendreply(xprt
, (xdrproc_t
) xdr_rmtcall_result
, (char *) &a
);
1304 if (reply_msg
.rm_xid
== 0) {
1305 #ifdef SVC_RUN_DEBUG
1307 fprintf(stderr
, "handle_reply: NULL xid on exit!\n");
1311 free_slot_by_xid(reply_msg
.rm_xid
);
1316 find_versions(rpcprog_t prog
, char *netid
, rpcvers_t
*lowvp
, rpcvers_t
*highvp
)
1319 unsigned int lowv
= 0;
1320 unsigned int highv
= 0;
1322 for (rbl
= list_rbl
; rbl
!= NULL
; rbl
= rbl
->rpcb_next
) {
1323 if ((rbl
->rpcb_map
.r_prog
!= prog
) ||
1324 ((rbl
->rpcb_map
.r_netid
!= NULL
) &&
1325 (strcasecmp(rbl
->rpcb_map
.r_netid
, netid
) != 0)))
1328 highv
= rbl
->rpcb_map
.r_vers
;
1330 } else if (rbl
->rpcb_map
.r_vers
< lowv
) {
1331 lowv
= rbl
->rpcb_map
.r_vers
;
1332 } else if (rbl
->rpcb_map
.r_vers
> highv
) {
1333 highv
= rbl
->rpcb_map
.r_vers
;
1342 * returns the item with the given program, version number and netid.
1343 * If that version number is not found, it returns the item with that
1344 * program number, so that address is now returned to the caller. The
1345 * caller when makes a call to this program, version number, the call
1346 * will fail and it will return with PROGVERS_MISMATCH. The user can
1347 * then determine the highest and the lowest version number for this
1348 * program using clnt_geterr() and use those program version numbers.
1350 * Returns the RPCBLIST for the given prog, vers and netid
1353 find_service(rpcprog_t prog
, rpcvers_t vers
, char *netid
)
1355 rpcblist_ptr hit
= NULL
;
1358 for (rbl
= list_rbl
; rbl
!= NULL
; rbl
= rbl
->rpcb_next
) {
1359 if ((rbl
->rpcb_map
.r_prog
!= prog
) ||
1360 ((rbl
->rpcb_map
.r_netid
!= NULL
) &&
1361 (strcasecmp(rbl
->rpcb_map
.r_netid
, netid
) != 0)))
1364 if (rbl
->rpcb_map
.r_vers
== vers
)
1371 * Copies the name associated with the uid of the caller and returns
1372 * a pointer to it. Similar to getwd().
1375 getowner(SVCXPRT
*transp
, char *owner
, size_t ownersize
)
1379 if (__rpc_get_local_uid(transp
, &uid
) < 0)
1380 strlcpy(owner
, "unknown", ownersize
);
1382 strlcpy(owner
, "superuser", ownersize
);
1384 snprintf(owner
, ownersize
, "%d", uid
);
1391 * Add this to the pmap list only if it is UDP or TCP.
1394 add_pmaplist(RPCB
*arg
)
1397 struct pmaplist
*pml
;
1398 int h1
, h2
, h3
, h4
, p1
, p2
;
1400 if (strcmp(arg
->r_netid
, udptrans
) == 0) {
1402 pmap
.pm_prot
= IPPROTO_UDP
;
1403 } else if (strcmp(arg
->r_netid
, tcptrans
) == 0) {
1405 pmap
.pm_prot
= IPPROTO_TCP
;
1407 /* Not an IP protocol */
1410 /* interpret the universal address for TCP/IP */
1411 if (sscanf(arg
->r_addr
, "%d.%d.%d.%d.%d.%d",
1412 &h1
, &h2
, &h3
, &h4
, &p1
, &p2
) != 6)
1414 pmap
.pm_port
= ((p1
& 0xff) << 8) + (p2
& 0xff);
1415 pmap
.pm_prog
= arg
->r_prog
;
1416 pmap
.pm_vers
= arg
->r_vers
;
1418 * add to END of list
1420 pml
= malloc(sizeof (struct pmaplist
));
1422 syslog(LOG_ERR
, "rpcbind: no memory!\n");
1425 pml
->pml_map
= pmap
;
1426 pml
->pml_next
= NULL
;
1427 if (list_pml
== NULL
) {
1430 struct pmaplist
*fnd
;
1432 /* Attach to the end of the list */
1433 for (fnd
= list_pml
; fnd
->pml_next
; fnd
= fnd
->pml_next
)
1435 fnd
->pml_next
= pml
;
1441 * Delete this from the pmap list only if it is UDP or TCP.
1444 del_pmaplist(RPCB
*arg
)
1446 struct pmaplist
*pml
;
1447 struct pmaplist
*prevpml
, *fnd
;
1450 if (strcmp(arg
->r_netid
, udptrans
) == 0) {
1453 } else if (strcmp(arg
->r_netid
, tcptrans
) == 0) {
1456 } else if (arg
->r_netid
[0] == 0) {
1457 prot
= 0; /* Remove all occurrences */
1459 /* Not an IP protocol */
1462 for (prevpml
= NULL
, pml
= list_pml
; pml
; /* cstyle */) {
1463 if ((pml
->pml_map
.pm_prog
!= arg
->r_prog
) ||
1464 (pml
->pml_map
.pm_vers
!= arg
->r_vers
) ||
1465 (prot
&& (pml
->pml_map
.pm_prot
!= prot
))) {
1466 /* both pml & prevpml move forwards */
1468 pml
= pml
->pml_next
;
1471 /* found it; pml moves forward, prevpml stays */
1473 pml
= pml
->pml_next
;
1474 if (prevpml
== NULL
)
1477 prevpml
->pml_next
= pml
;
1482 #endif /* PORTMAP */