1 /* $NetBSD: rpcb_svc_com.c,v 1.2 2011/09/16 16:13:18 plunky Exp $ */
4 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
5 * unrestricted use provided that this legend is included on all tape
6 * media and as a part of the software program in whole or part. Users
7 * may copy or modify Sun RPC without charge, but are not authorized
8 * to license or distribute it to anyone else except as part of a product or
9 * program developed by the user.
11 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
12 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
13 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
15 * Sun RPC is provided with no support and without any obligation on the
16 * part of Sun Microsystems, Inc. to assist in its use, correction,
17 * modification or enhancement.
19 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
20 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
21 * OR ANY PART THEREOF.
23 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
24 * or profits or other special, indirect and consequential damages, even if
25 * Sun has been advised of the possibility of such damages.
27 * Sun Microsystems, Inc.
29 * Mountain View, California 94043
32 * Copyright (c) 1986 - 1991 by Sun Microsystems, Inc.
35 /* #ident "@(#)rpcb_svc_com.c 1.18 94/05/02 SMI" */
39 * The commom server procedure for the rpcbind.
42 #include <sys/types.h>
44 #include <sys/param.h>
45 #include <sys/socket.h>
47 #include <rpc/rpcb_prot.h>
48 #include <netconfig.h>
55 #include <netinet/in.h>
56 #include <rpc/pmap_prot.h>
61 #include <rump/rump.h>
62 #include <rump/rump_syscalls.h>
66 #include "svc_fdset.h"
68 #define RPC_BUF_MAX 65536 /* can be raised if required */
70 static char emptystring
[] = "";
71 static int rpcb_rmtcalls
;
73 struct rmtcallfd_list
{
77 struct rmtcallfd_list
*next
;
81 #define MAXTIME_OFF 300 /* 5 minutes */
85 #define FINFO_ACTIVE 0x1
87 struct netbuf
*caller_addr
;
88 u_int32_t forward_xid
;
95 static struct finfo FINFO
[NFORWARD
];
98 static bool_t
xdr_encap_parms(XDR
*, struct encap_parms
*);
99 static bool_t
xdr_rmtcall_args(XDR
*, struct r_rmtcall_args
*);
100 static bool_t
xdr_rmtcall_result(XDR
*, struct r_rmtcall_args
*);
101 static bool_t
xdr_opaque_parms(XDR
*, struct r_rmtcall_args
*);
102 static int find_rmtcallfd_by_netid(char *);
103 static SVCXPRT
*find_rmtcallxprt_by_fd(int);
104 static u_int32_t
forward_register(u_int32_t
, struct netbuf
*, int, char *,
105 rpcproc_t
, rpcvers_t
);
106 static struct finfo
*forward_find(u_int32_t
);
107 static int free_slot_by_xid(u_int32_t
);
108 static int free_slot_by_index(int);
109 static int netbufcmp(struct netbuf
*, struct netbuf
*);
110 static struct netbuf
*netbufdup(struct netbuf
*);
111 static void netbuffree(struct netbuf
*);
112 static int check_rmtcalls(struct pollfd
*, int);
113 static void xprt_set_caller(SVCXPRT
*, struct finfo
*);
114 static void send_svcsyserr(SVCXPRT
*, struct finfo
*);
115 static void handle_reply(int, SVCXPRT
*);
116 static void find_versions(rpcprog_t
, char *, rpcvers_t
*, rpcvers_t
*);
117 static rpcblist_ptr
find_service(rpcprog_t
, rpcvers_t
, char *);
118 static char *getowner(SVCXPRT
*, char *, size_t);
119 static int add_pmaplist(RPCB
*);
120 static int del_pmaplist(RPCB
*);
123 * Set a mapping of program, version, netid
127 rpcbproc_set_com(void *arg
, struct svc_req
*rqstp
, SVCXPRT
*transp
,
128 rpcvers_t rpcbversnum
)
130 RPCB
*regp
= (RPCB
*)arg
;
136 fprintf(stderr
, "RPCB_SET request for (%lu, %lu, %s, %s) : ",
137 (unsigned long)regp
->r_prog
, (unsigned long)regp
->r_vers
,
138 regp
->r_netid
, regp
->r_addr
);
140 ans
= map_set(regp
, getowner(transp
, owner
, sizeof owner
));
143 fprintf(stderr
, "%s\n", ans
== TRUE
? "succeeded" : "failed");
145 /* XXX: should have used some defined constant here */
146 rpcbs_set(rpcbversnum
- 2, ans
);
151 map_set(RPCB
*regp
, char *owner
)
154 rpcblist_ptr rbl
, fnd
;
158 * check to see if already used
159 * find_service returns a hit even if
160 * the versions don't match, so check for it
162 fnd
= find_service(reg
.r_prog
, reg
.r_vers
, reg
.r_netid
);
163 if (fnd
&& (fnd
->rpcb_map
.r_vers
== reg
.r_vers
)) {
164 if (!strcmp(fnd
->rpcb_map
.r_addr
, reg
.r_addr
))
166 * if these match then it is already
167 * registered so just say "OK".
174 * add to the end of the list
176 rbl
= (rpcblist_ptr
) malloc((u_int
)sizeof (RPCBLIST
));
180 a
= &(rbl
->rpcb_map
);
181 a
->r_prog
= reg
.r_prog
;
182 a
->r_vers
= reg
.r_vers
;
183 a
->r_netid
= strdup(reg
.r_netid
);
184 a
->r_addr
= strdup(reg
.r_addr
);
185 a
->r_owner
= strdup(owner
);
186 if (!a
->r_addr
|| !a
->r_netid
|| !a
->r_owner
) {
188 free((void *) a
->r_netid
);
190 free((void *) a
->r_addr
);
192 free((void *) a
->r_owner
);
196 rbl
->rpcb_next
= NULL
;
197 if (list_rbl
== NULL
) {
200 for (fnd
= list_rbl
; fnd
->rpcb_next
;
201 fnd
= fnd
->rpcb_next
)
203 fnd
->rpcb_next
= rbl
;
206 (void) add_pmaplist(regp
);
212 * Unset a mapping of program, version, netid
216 rpcbproc_unset_com(void *arg
, struct svc_req
*rqstp
, SVCXPRT
*transp
,
217 rpcvers_t rpcbversnum
)
219 RPCB
*regp
= (RPCB
*)arg
;
225 fprintf(stderr
, "RPCB_UNSET request for (%lu, %lu, %s) : ",
226 (unsigned long)regp
->r_prog
, (unsigned long)regp
->r_vers
,
229 ans
= map_unset(regp
, getowner(transp
, owner
, sizeof owner
));
232 fprintf(stderr
, "%s\n", ans
== TRUE
? "succeeded" : "failed");
234 /* XXX: should have used some defined constant here */
235 rpcbs_unset(rpcbversnum
- 2, ans
);
240 map_unset(RPCB
*regp
, const char *owner
)
243 rpcblist_ptr rbl
, prev
, tmp
;
248 for (prev
= NULL
, rbl
= list_rbl
; rbl
; /* cstyle */) {
249 if ((rbl
->rpcb_map
.r_prog
!= regp
->r_prog
) ||
250 (rbl
->rpcb_map
.r_vers
!= regp
->r_vers
) ||
251 (regp
->r_netid
[0] && strcasecmp(regp
->r_netid
,
252 rbl
->rpcb_map
.r_netid
))) {
253 /* both rbl & prev move forwards */
255 rbl
= rbl
->rpcb_next
;
259 * Check whether appropriate uid. Unset only
260 * if superuser or the owner itself.
262 if (strcmp(owner
, rpcbind_superuser
) &&
263 strcmp(rbl
->rpcb_map
.r_owner
, owner
))
265 /* found it; rbl moves forward, prev stays */
268 rbl
= rbl
->rpcb_next
;
272 prev
->rpcb_next
= rbl
;
273 free((void *) tmp
->rpcb_map
.r_addr
);
274 free((void *) tmp
->rpcb_map
.r_netid
);
275 free((void *) tmp
->rpcb_map
.r_owner
);
280 (void) del_pmaplist(regp
);
283 * We return 1 either when the entry was not there or it
284 * was able to unset it. It can come to this point only if
285 * atleast one of the conditions is true.
291 delete_prog(int prog
)
294 register rpcblist_ptr rbl
;
296 for (rbl
= list_rbl
; rbl
!= NULL
; rbl
= rbl
->rpcb_next
) {
297 if ((rbl
->rpcb_map
.r_prog
!= prog
))
299 if (is_bound(rbl
->rpcb_map
.r_netid
, rbl
->rpcb_map
.r_addr
))
301 reg
.r_prog
= rbl
->rpcb_map
.r_prog
;
302 reg
.r_vers
= rbl
->rpcb_map
.r_vers
;
303 reg
.r_netid
= strdup(rbl
->rpcb_map
.r_netid
);
304 (void)map_unset(®
, rpcbind_superuser
);
310 rpcbproc_getaddr_com(RPCB
*regp
, struct svc_req
*rqstp
, SVCXPRT
*transp
,
311 rpcvers_t rpcbversnum
, rpcvers_t verstype
)
317 if (uaddr
&& uaddr
[0])
318 free((void *) uaddr
);
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
, struct svc_req
*rqstp
, SVCXPRT
*transp
,
352 rpcvers_t rpcbversnum
)
354 static time_t curtime
;
356 (void) time(&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
, SVCXPRT
*transp
,
367 rpcvers_t rpcbversnum
)
369 char **uaddrp
= (char **)arg
;
370 struct netconfig
*nconf
;
371 static struct netbuf nbuf
;
372 static struct netbuf
*taddr
;
375 free((void *) taddr
->buf
);
376 free((void *) taddr
);
378 if (((nconf
= rpcbind_get_conf(transp
->xp_netid
)) == NULL
) ||
379 ((taddr
= uaddr2taddr(nconf
, *uaddrp
)) == NULL
)) {
380 (void) memset((char *)&nbuf
, 0, sizeof (struct netbuf
));
381 return (void *)&nbuf
;
383 return (void *)taddr
;
387 * Convert taddr to uaddr. Should be used only by
388 * local servers/clients. (kernel level stuff only)
392 rpcbproc_taddr2uaddr_com(void *arg
, struct svc_req
*rqstp
, SVCXPRT
*transp
,
393 rpcvers_t rpcbversnum
)
395 struct netbuf
*taddr
= (struct netbuf
*)arg
;
397 struct netconfig
*nconf
;
399 if (uaddr
&& !uaddr
[0])
400 free((void *) uaddr
);
401 if (((nconf
= rpcbind_get_conf(transp
->xp_netid
)) == NULL
) ||
402 ((uaddr
= taddr2uaddr(nconf
, taddr
)) == NULL
)) {
405 return (void *)&uaddr
;
410 xdr_encap_parms(XDR
*xdrs
, struct encap_parms
*epp
)
412 return (xdr_bytes(xdrs
, &(epp
->args
), (u_int
*) &(epp
->arglen
), ~0));
416 * XDR remote call arguments. It ignores the address part.
417 * written for XDR_DECODE direction only
420 xdr_rmtcall_args(XDR
*xdrs
, struct r_rmtcall_args
*cap
)
422 /* does not get the address or the arguments */
423 if (xdr_u_int32_t(xdrs
, &(cap
->rmt_prog
)) &&
424 xdr_u_int32_t(xdrs
, &(cap
->rmt_vers
)) &&
425 xdr_u_int32_t(xdrs
, &(cap
->rmt_proc
))) {
426 return (xdr_encap_parms(xdrs
, &(cap
->rmt_args
)));
432 * XDR remote call results along with the address. Ignore
433 * program number, version number and proc number.
434 * Written for XDR_ENCODE direction only.
437 xdr_rmtcall_result(XDR
*xdrs
, struct r_rmtcall_args
*cap
)
442 if (cap
->rmt_localvers
== PMAPVERS
) {
443 int h1
, h2
, h3
, h4
, p1
, p2
;
446 /* interpret the universal address for TCP/IP */
447 if (sscanf(cap
->rmt_uaddr
, "%d.%d.%d.%d.%d.%d",
448 &h1
, &h2
, &h3
, &h4
, &p1
, &p2
) != 6)
450 port
= ((p1
& 0xff) << 8) + (p2
& 0xff);
451 result
= xdr_u_long(xdrs
, &port
);
454 if ((cap
->rmt_localvers
== RPCBVERS
) ||
455 (cap
->rmt_localvers
== RPCBVERS4
)) {
456 result
= xdr_wrapstring(xdrs
, &(cap
->rmt_uaddr
));
461 return (xdr_encap_parms(xdrs
, &(cap
->rmt_args
)));
466 * only worries about the struct encap_parms part of struct r_rmtcall_args.
467 * The arglen must already be set!!
470 xdr_opaque_parms(XDR
*xdrs
, struct r_rmtcall_args
*cap
)
472 return (xdr_opaque(xdrs
, cap
->rmt_args
.args
, cap
->rmt_args
.arglen
));
475 static struct rmtcallfd_list
*rmthead
;
476 static struct rmtcallfd_list
*rmttail
;
479 create_rmtcall_fd(struct netconfig
*nconf
)
482 struct rmtcallfd_list
*rmt
;
485 if ((fd
= __rpc_nconf2fd(nconf
)) == -1) {
488 "create_rmtcall_fd: couldn't open \"%s\" (errno %d)\n",
489 nconf
->nc_device
, errno
);
492 xprt
= svc_tli_create(fd
, 0, (struct t_bind
*) 0, 0, 0);
496 "create_rmtcall_fd: svc_tli_create failed\n");
499 rmt
= (struct rmtcallfd_list
*)malloc((u_int
)
500 sizeof (struct rmtcallfd_list
));
502 syslog(LOG_ERR
, "create_rmtcall_fd: no memory!");
506 rmt
->netid
= strdup(nconf
->nc_netid
);
507 xprt
->xp_netid
= rmt
->netid
;
510 if (rmthead
== NULL
) {
517 /* XXX not threadsafe */
518 if (fd
> *get_fdsetmax())
519 *get_fdsetmax() = fd
;
520 FD_SET(fd
, get_fdset());
525 find_rmtcallfd_by_netid(char *netid
)
527 struct rmtcallfd_list
*rmt
;
529 for (rmt
= rmthead
; rmt
!= NULL
; rmt
= rmt
->next
) {
530 if (strcmp(netid
, rmt
->netid
) == 0) {
538 find_rmtcallxprt_by_fd(int fd
)
540 struct rmtcallfd_list
*rmt
;
542 for (rmt
= rmthead
; rmt
!= NULL
; rmt
= rmt
->next
) {
552 * Call a remote procedure service. This procedure is very quiet when things
553 * go wrong. The proc is written to support broadcast rpc. In the broadcast
554 * case, a machine should shut-up instead of complain, lest the requestor be
555 * overrun with complaints at the expense of not hearing a valid reply.
556 * When receiving a request and verifying that the service exists, we
558 * receive the request
560 * open a new TLI endpoint on the same transport on which we received
561 * the original request
563 * remember the original request's XID (which requires knowing the format
564 * of the svc_dg_data structure)
566 * forward the request, with a new XID, to the requested service,
567 * remembering the XID used to send this request (for later use in
568 * reassociating the answer with the original request), the requestor's
569 * address, the file descriptor on which the forwarded request is
570 * made and the service's address.
572 * mark the file descriptor on which we anticipate receiving a reply from
573 * the service and one to select for in our private svc_run procedure
575 * At some time in the future, a reply will be received from the service to
576 * which we forwarded the request. At that time, we detect that the socket
577 * used was for forwarding (by looking through the finfo structures to see
578 * whether the fd corresponds to one of those) and call handle_reply() to
582 * bundle the reply, along with the service's universal address
584 * create a SVCXPRT structure and use a version of svc_sendreply
585 * that allows us to specify the reply XID and destination, send the reply
586 * to the original requestor.
590 rpcbproc_callit_com(struct svc_req
*rqstp
, SVCXPRT
*transp
,
591 rpcproc_t reply_type
, rpcvers_t versnum
)
593 register rpcblist_ptr rbl
;
594 struct netconfig
*nconf
;
595 struct netbuf
*caller
;
596 struct r_rmtcall_args a
;
597 char *buf_alloc
= NULL
, *outbufp
;
598 char *outbuf_alloc
= NULL
;
599 char buf
[RPC_BUF_MAX
], outbuf
[RPC_BUF_MAX
];
600 struct netbuf
*na
= NULL
;
601 struct rpc_msg call_msg
;
607 char *uaddr
, *m_uaddr
, *local_uaddr
= NULL
;
609 struct __rpc_sockinfo si
;
610 struct sockaddr
*localsa
;
613 if (!__rpc_fd2sockinfo(transp
->xp_fd
, &si
)) {
614 if (reply_type
== RPCBPROC_INDIRECT
)
615 svcerr_systemerr(transp
);
618 if (si
.si_socktype
!= SOCK_DGRAM
)
619 return; /* Only datagram type accepted */
620 sendsz
= __rpc_get_t_size(si
.si_af
, si
.si_proto
, UDPMSGSIZE
);
621 if (sendsz
== 0) { /* data transfer not supported */
622 if (reply_type
== RPCBPROC_INDIRECT
)
623 svcerr_systemerr(transp
);
627 * Should be multiple of 4 for XDR.
629 sendsz
= ((sendsz
+ 3) / 4) * 4;
630 if (sendsz
> RPC_BUF_MAX
) {
632 buf_alloc
= alloca(sendsz
); /* not in IDR2? */
634 buf_alloc
= malloc(sendsz
);
636 if (buf_alloc
== NULL
) {
639 "rpcbproc_callit_com: No Memory!\n");
640 if (reply_type
== RPCBPROC_INDIRECT
)
641 svcerr_systemerr(transp
);
644 a
.rmt_args
.args
= buf_alloc
;
646 a
.rmt_args
.args
= buf
;
649 call_msg
.rm_xid
= 0; /* For error checking purposes */
650 if (!svc_getargs(transp
, (xdrproc_t
) xdr_rmtcall_args
, (char *) &a
)) {
651 if (reply_type
== RPCBPROC_INDIRECT
)
652 svcerr_decode(transp
);
655 "rpcbproc_callit_com: svc_getargs failed\n");
659 if (!check_callit(transp
, &a
, versnum
)) {
660 svcerr_weakauth(transp
);
664 caller
= svc_getrpccaller(transp
);
667 uaddr
= taddr2uaddr(rpcbind_get_conf(transp
->xp_netid
), caller
);
668 fprintf(stderr
, "%s %s req for (%lu, %lu, %lu, %s) from %s : ",
669 versnum
== PMAPVERS
? "pmap_rmtcall" :
670 versnum
== RPCBVERS
? "rpcb_rmtcall" :
671 versnum
== RPCBVERS4
? "rpcb_indirect" :
673 reply_type
== RPCBPROC_INDIRECT
? "indirect" : "callit",
674 (unsigned long)a
.rmt_prog
, (unsigned long)a
.rmt_vers
,
675 (unsigned long)a
.rmt_proc
, transp
->xp_netid
,
676 uaddr
? uaddr
: rpcbind_unknown
);
678 free((void *) uaddr
);
682 rbl
= find_service(a
.rmt_prog
, a
.rmt_vers
, transp
->xp_netid
);
684 rpcbs_rmtcall(versnum
- 2, reply_type
, a
.rmt_prog
, a
.rmt_vers
,
685 a
.rmt_proc
, transp
->xp_netid
, rbl
);
690 fprintf(stderr
, "not found\n");
692 if (reply_type
== RPCBPROC_INDIRECT
)
693 svcerr_noprog(transp
);
696 if (rbl
->rpcb_map
.r_vers
!= a
.rmt_vers
) {
697 if (reply_type
== RPCBPROC_INDIRECT
) {
698 rpcvers_t vers_low
, vers_high
;
700 find_versions(a
.rmt_prog
, transp
->xp_netid
,
701 &vers_low
, &vers_high
);
702 svcerr_progvers(transp
, vers_low
, vers_high
);
709 fprintf(stderr
, "found at uaddr %s\n", rbl
->rpcb_map
.r_addr
);
712 * Check whether this entry is valid and a server is present
713 * Mergeaddr() returns NULL if no such entry is present, and
714 * returns "" if the entry was present but the server is not
715 * present (i.e., it crashed).
717 if (reply_type
== RPCBPROC_INDIRECT
) {
718 uaddr
= mergeaddr(transp
, transp
->xp_netid
,
719 rbl
->rpcb_map
.r_addr
, NULL
);
720 if (uaddr
== NULL
|| uaddr
[0] == '\0') {
721 svcerr_noprog(transp
);
723 free((void *) uaddr
);
728 free((void *) uaddr
);
731 nconf
= rpcbind_get_conf(transp
->xp_netid
);
733 if (reply_type
== RPCBPROC_INDIRECT
)
734 svcerr_systemerr(transp
);
737 "rpcbproc_callit_com: rpcbind_get_conf failed\n");
740 localsa
= local_sa(((struct sockaddr
*)caller
->buf
)->sa_family
);
741 if (localsa
== NULL
) {
744 "rpcbproc_callit_com: no local address\n");
747 tbuf
.len
= tbuf
.maxlen
= localsa
->sa_len
;
750 addrmerge(&tbuf
, rbl
->rpcb_map
.r_addr
, NULL
, nconf
->nc_netid
);
751 m_uaddr
= addrmerge(caller
, rbl
->rpcb_map
.r_addr
, NULL
,
755 fprintf(stderr
, "merged uaddr %s\n", m_uaddr
);
757 if ((fd
= find_rmtcallfd_by_netid(nconf
->nc_netid
)) == -1) {
758 if (reply_type
== RPCBPROC_INDIRECT
)
759 svcerr_systemerr(transp
);
760 free((void *) m_uaddr
);
763 xidp
= __rpcb_get_dg_xidp(transp
);
764 call_msg
.rm_xid
= forward_register(*xidp
,
765 caller
, fd
, m_uaddr
, reply_type
, versnum
);
766 if (call_msg
.rm_xid
== 0) {
768 * A duplicate request for the slow server. Let's not
769 * beat on it any more.
773 "rpcbproc_callit_com: duplicate request\n");
774 free((void *) m_uaddr
);
776 } else if (call_msg
.rm_xid
== -1) {
777 /* forward_register failed. Perhaps no memory. */
780 "rpcbproc_callit_com: forward_register failed\n");
781 free((void *) m_uaddr
);
788 "rpcbproc_callit_com: original XID %x, new XID %x\n",
789 *xidp
, call_msg
.rm_xid
);
791 call_msg
.rm_direction
= CALL
;
792 call_msg
.rm_call
.cb_rpcvers
= RPC_MSG_VERSION
;
793 call_msg
.rm_call
.cb_prog
= a
.rmt_prog
;
794 call_msg
.rm_call
.cb_vers
= a
.rmt_vers
;
795 if (sendsz
> RPC_BUF_MAX
) {
797 outbuf_alloc
= alloca(sendsz
); /* not in IDR2? */
799 outbuf_alloc
= malloc(sendsz
);
801 if (outbuf_alloc
== NULL
) {
802 if (reply_type
== RPCBPROC_INDIRECT
)
803 svcerr_systemerr(transp
);
806 "rpcbproc_callit_com: No memory!\n");
809 xdrmem_create(&outxdr
, outbuf_alloc
, sendsz
, XDR_ENCODE
);
811 xdrmem_create(&outxdr
, outbuf
, sendsz
, XDR_ENCODE
);
813 if (!xdr_callhdr(&outxdr
, &call_msg
)) {
814 if (reply_type
== RPCBPROC_INDIRECT
)
815 svcerr_systemerr(transp
);
818 "rpcbproc_callit_com: xdr_callhdr failed\n");
821 if (!xdr_u_int32_t(&outxdr
, &(a
.rmt_proc
))) {
822 if (reply_type
== RPCBPROC_INDIRECT
)
823 svcerr_systemerr(transp
);
826 "rpcbproc_callit_com: xdr_u_long failed\n");
830 if (rqstp
->rq_cred
.oa_flavor
== AUTH_NULL
) {
831 auth
= authnone_create();
832 } else if (rqstp
->rq_cred
.oa_flavor
== AUTH_SYS
) {
833 struct authunix_parms
*au
;
835 au
= (struct authunix_parms
*)rqstp
->rq_clntcred
;
836 auth
= authunix_create(au
->aup_machname
,
837 au
->aup_uid
, au
->aup_gid
,
838 au
->aup_len
, au
->aup_gids
);
839 if (auth
== NULL
) /* fall back */
840 auth
= authnone_create();
842 /* we do not support any other authentication scheme */
845 "rpcbproc_callit_com: oa_flavor != AUTH_NONE and oa_flavor != AUTH_SYS\n");
846 if (reply_type
== RPCBPROC_INDIRECT
)
847 svcerr_weakauth(transp
); /* XXX too strong.. */
851 if (reply_type
== RPCBPROC_INDIRECT
)
852 svcerr_systemerr(transp
);
855 "rpcbproc_callit_com: authwhatever_create returned NULL\n");
858 if (!AUTH_MARSHALL(auth
, &outxdr
)) {
859 if (reply_type
== RPCBPROC_INDIRECT
)
860 svcerr_systemerr(transp
);
864 "rpcbproc_callit_com: AUTH_MARSHALL failed\n");
868 if (!xdr_opaque_parms(&outxdr
, &a
)) {
869 if (reply_type
== RPCBPROC_INDIRECT
)
870 svcerr_systemerr(transp
);
873 "rpcbproc_callit_com: xdr_opaque_parms failed\n");
876 outlen
= (int) XDR_GETPOS(&outxdr
);
878 outbufp
= outbuf_alloc
;
882 na
= uaddr2taddr(nconf
, local_uaddr
);
884 if (reply_type
== RPCBPROC_INDIRECT
)
885 svcerr_systemerr(transp
);
889 if (sendto(fd
, outbufp
, outlen
, 0, (struct sockaddr
*)na
->buf
, na
->len
)
893 "rpcbproc_callit_com: sendto failed: errno %d\n", errno
);
894 if (reply_type
== RPCBPROC_INDIRECT
)
895 svcerr_systemerr(transp
);
901 if (call_msg
.rm_xid
!= 0)
902 (void) free_slot_by_xid(call_msg
.rm_xid
);
907 free((void *) buf_alloc
);
909 free((void *) outbuf_alloc
);
917 * Makes an entry into the FIFO for the given request.
918 * If duplicate request, returns a 0, else returns the xid of its call.
921 forward_register(u_int32_t caller_xid
, struct netbuf
*caller_addr
,
922 int forward_fd
, char *uaddr
, rpcproc_t reply_type
,
927 time_t min_time
, time_now
;
928 static u_int32_t lastxid
;
931 min_time
= FINFO
[0].time
;
932 time_now
= time((time_t *)0);
935 lastxid
= time_now
* NFORWARD
;
938 * Check if it is an duplicate entry. Then,
939 * try to find an empty slot. If not available, then
940 * use the slot with the earliest time.
942 for (i
= 0; i
< NFORWARD
; i
++) {
943 if (FINFO
[i
].flag
& FINFO_ACTIVE
) {
944 if ((FINFO
[i
].caller_xid
== caller_xid
) &&
945 (FINFO
[i
].reply_type
== reply_type
) &&
946 (FINFO
[i
].versnum
== versnum
) &&
947 (!netbufcmp(FINFO
[i
].caller_addr
,
949 FINFO
[i
].time
= time((time_t *)0);
950 return (0); /* Duplicate entry */
952 /* Should we wait any longer */
953 if ((time_now
- FINFO
[i
].time
) > MAXTIME_OFF
)
954 (void) free_slot_by_index(i
);
958 if ((FINFO
[i
].flag
& FINFO_ACTIVE
) == 0) {
960 } else if (FINFO
[i
].time
< min_time
) {
962 min_time
= FINFO
[i
].time
;
967 /* use this empty slot */
970 (void) free_slot_by_index(j
);
972 if ((FINFO
[j
].caller_addr
= netbufdup(caller_addr
)) == NULL
) {
975 rpcb_rmtcalls
++; /* no of pending calls */
976 FINFO
[j
].flag
= FINFO_ACTIVE
;
977 FINFO
[j
].reply_type
= reply_type
;
978 FINFO
[j
].versnum
= versnum
;
979 FINFO
[j
].time
= time_now
;
980 FINFO
[j
].caller_xid
= caller_xid
;
981 FINFO
[j
].forward_fd
= forward_fd
;
983 * Though uaddr is not allocated here, it will still be freed
984 * from free_slot_*().
986 FINFO
[j
].uaddr
= uaddr
;
987 lastxid
= lastxid
+ NFORWARD
;
988 FINFO
[j
].forward_xid
= lastxid
+ j
; /* encode slot */
989 return (FINFO
[j
].forward_xid
); /* forward on this xid */
992 static struct finfo
*
993 forward_find(u_int32_t reply_xid
)
997 i
= reply_xid
% NFORWARD
;
1000 if ((FINFO
[i
].flag
& FINFO_ACTIVE
) &&
1001 (FINFO
[i
].forward_xid
== reply_xid
)) {
1008 free_slot_by_xid(u_int32_t xid
)
1012 entry
= xid
% NFORWARD
;
1015 return (free_slot_by_index(entry
));
1019 free_slot_by_index(int idx
)
1024 if (fi
->flag
& FINFO_ACTIVE
) {
1025 netbuffree(fi
->caller_addr
);
1026 /* XXX may be too big, but can't access xprt array here */
1027 if (fi
->forward_fd
>= *get_fdsetmax())
1028 (*get_fdsetmax())--;
1029 free((void *) fi
->uaddr
);
1030 fi
->flag
&= ~FINFO_ACTIVE
;
1038 netbufcmp(struct netbuf
*n1
, struct netbuf
*n2
)
1040 return ((n1
->len
!= n2
->len
) || memcmp(n1
->buf
, n2
->buf
, n1
->len
));
1043 static struct netbuf
*
1044 netbufdup(struct netbuf
*ap
)
1048 np
= (struct netbuf
*) malloc(sizeof (struct netbuf
) + ap
->len
);
1050 np
->maxlen
= np
->len
= ap
->len
;
1051 np
->buf
= ((char *) np
) + sizeof (struct netbuf
);
1052 (void) memcpy(np
->buf
, ap
->buf
, ap
->len
);
1058 netbuffree(struct netbuf
*ap
)
1064 #define MASKVAL (POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND)
1065 extern bool_t
__svc_clean_idle(fd_set
*, int, bool_t
);
1071 struct pollfd pollfds
[FD_SETSIZE
];
1072 int poll_ret
, check_ret
;
1074 #ifdef SVC_RUN_DEBUG
1077 register struct pollfd
*p
;
1082 for (n
= 0; n
<= *get_fdsetmax(); n
++) {
1083 if (FD_ISSET(n
, get_fdset())) {
1085 p
->events
= MASKVAL
;
1091 #ifdef SVC_RUN_DEBUG
1093 fprintf(stderr
, "polling for read on fd < ");
1094 for (i
= 0, p
= pollfds
; i
< nfds
; i
++, p
++)
1096 fprintf(stderr
, "%d ", p
->fd
);
1097 fprintf(stderr
, ">\n");
1100 poll_ret
= rump_sys_poll(pollfds
, nfds
, 30 * 1000);
1101 //printf("rpcbind poll got %d\n", poll_ret);
1105 * We ignore all errors, continuing with the assumption
1106 * that it was set by the signal handlers (or any
1107 * other outside event) and not caused by poll().
1110 cleanfds
= *get_fdset();
1111 __svc_clean_idle(&cleanfds
, 30, FALSE
);
1114 #ifdef SVC_RUN_DEBUG
1116 fprintf(stderr
, "poll returned read fds < ");
1117 for (i
= 0, p
= pollfds
; i
< nfds
; i
++, p
++)
1119 fprintf(stderr
, "%d (0x%x) ", p
->fd
, p
->revents
);
1120 fprintf(stderr
, ">\n");
1124 * If we found as many replies on callback fds
1125 * as the number of descriptors selectable which
1126 * poll() returned, there can be no more so we
1127 * don't call svc_getreq_poll. Otherwise, there
1128 * must be another so we must call svc_getreq_poll.
1130 if ((check_ret
= check_rmtcalls(pollfds
, nfds
)) ==
1133 svc_getreq_poll(pollfds
, poll_ret
-check_ret
);
1135 #ifdef SVC_RUN_DEBUG
1137 fprintf(stderr
, "svc_maxfd now %u\n", *get_fdsetmax());
1144 check_rmtcalls(struct pollfd
*pfds
, int nfds
)
1146 int j
, ncallbacks_found
= 0, rmtcalls_pending
;
1149 if (rpcb_rmtcalls
== 0)
1152 rmtcalls_pending
= rpcb_rmtcalls
;
1153 for (j
= 0; j
< nfds
; j
++) {
1154 if ((xprt
= find_rmtcallxprt_by_fd(pfds
[j
].fd
)) != NULL
) {
1155 if (pfds
[j
].revents
) {
1157 #ifdef DEBUG_RMTCALL
1160 "my_svc_run: polled on forwarding fd %d, netid %s - calling handle_reply\n",
1161 pfds
[j
].fd
, xprt
->xp_netid
);
1163 handle_reply(pfds
[j
].fd
, xprt
);
1164 pfds
[j
].revents
= 0;
1165 if (ncallbacks_found
>= rmtcalls_pending
) {
1171 return (ncallbacks_found
);
1175 xprt_set_caller(SVCXPRT
*xprt
, struct finfo
*fi
)
1179 *(svc_getrpccaller(xprt
)) = *(fi
->caller_addr
);
1180 xidp
= __rpcb_get_dg_xidp(xprt
);
1181 *xidp
= fi
->caller_xid
;
1185 * Call svcerr_systemerr() only if RPCBVERS4
1188 send_svcsyserr(SVCXPRT
*xprt
, struct finfo
*fi
)
1190 if (fi
->reply_type
== RPCBPROC_INDIRECT
) {
1191 xprt_set_caller(xprt
, fi
);
1192 svcerr_systemerr(xprt
);
1198 handle_reply(int fd
, SVCXPRT
*xprt
)
1201 struct rpc_msg reply_msg
;
1202 struct rpc_err reply_error
;
1205 int inlen
, pos
, len
;
1206 struct r_rmtcall_args a
;
1207 struct sockaddr_storage ss
;
1209 #ifdef SVC_RUN_DEBUG
1213 buffer
= malloc(RPC_BUF_MAX
);
1218 fromlen
= sizeof ss
;
1219 inlen
= recvfrom(fd
, buffer
, RPC_BUF_MAX
, 0,
1220 (struct sockaddr
*)&ss
, &fromlen
);
1221 } while (inlen
< 0 && errno
== EINTR
);
1225 "handle_reply: recvfrom returned %d, errno %d\n", inlen
, errno
);
1229 reply_msg
.acpted_rply
.ar_verf
= _null_auth
;
1230 reply_msg
.acpted_rply
.ar_results
.where
= 0;
1231 reply_msg
.acpted_rply
.ar_results
.proc
= (xdrproc_t
) xdr_void
;
1233 xdrmem_create(&reply_xdrs
, buffer
, (u_int
)inlen
, XDR_DECODE
);
1234 if (!xdr_replymsg(&reply_xdrs
, &reply_msg
)) {
1236 (void) fprintf(stderr
,
1237 "handle_reply: xdr_replymsg failed\n");
1240 fi
= forward_find(reply_msg
.rm_xid
);
1241 #ifdef SVC_RUN_DEBUG
1243 fprintf(stderr
, "handle_reply: reply xid: %d fi addr: %p\n",
1244 reply_msg
.rm_xid
, fi
);
1250 _seterr_reply(&reply_msg
, &reply_error
);
1251 if (reply_error
.re_status
!= RPC_SUCCESS
) {
1253 (void) fprintf(stderr
, "handle_reply: %s\n",
1254 clnt_sperrno(reply_error
.re_status
));
1255 send_svcsyserr(xprt
, fi
);
1258 pos
= XDR_GETPOS(&reply_xdrs
);
1260 a
.rmt_args
.args
= &buffer
[pos
];
1261 a
.rmt_args
.arglen
= len
;
1262 a
.rmt_uaddr
= fi
->uaddr
;
1263 a
.rmt_localvers
= fi
->versnum
;
1265 xprt_set_caller(xprt
, fi
);
1266 #ifdef SVC_RUN_DEBUG
1267 uaddr
= taddr2uaddr(rpcbind_get_conf("udp"),
1268 svc_getrpccaller(xprt
));
1270 fprintf(stderr
, "handle_reply: forwarding address %s to %s\n",
1271 a
.rmt_uaddr
, uaddr
? uaddr
: rpcbind_unknown
);
1274 free((void *) uaddr
);
1276 svc_sendreply(xprt
, (xdrproc_t
) xdr_rmtcall_result
, (char *) &a
);
1281 if (reply_msg
.rm_xid
== 0) {
1282 #ifdef SVC_RUN_DEBUG
1284 fprintf(stderr
, "handle_reply: NULL xid on exit!\n");
1288 (void) free_slot_by_xid(reply_msg
.rm_xid
);
1293 find_versions(rpcprog_t prog
, char *netid
, rpcvers_t
*lowvp
, rpcvers_t
*highvp
)
1295 register rpcblist_ptr rbl
;
1299 for (rbl
= list_rbl
; rbl
!= NULL
; rbl
= rbl
->rpcb_next
) {
1300 if ((rbl
->rpcb_map
.r_prog
!= prog
) ||
1301 ((rbl
->rpcb_map
.r_netid
!= NULL
) &&
1302 (strcasecmp(rbl
->rpcb_map
.r_netid
, netid
) != 0)))
1305 highv
= rbl
->rpcb_map
.r_vers
;
1307 } else if (rbl
->rpcb_map
.r_vers
< lowv
) {
1308 lowv
= rbl
->rpcb_map
.r_vers
;
1309 } else if (rbl
->rpcb_map
.r_vers
> highv
) {
1310 highv
= rbl
->rpcb_map
.r_vers
;
1319 * returns the item with the given program, version number and netid.
1320 * If that version number is not found, it returns the item with that
1321 * program number, so that address is now returned to the caller. The
1322 * caller when makes a call to this program, version number, the call
1323 * will fail and it will return with PROGVERS_MISMATCH. The user can
1324 * then determine the highest and the lowest version number for this
1325 * program using clnt_geterr() and use those program version numbers.
1327 * Returns the RPCBLIST for the given prog, vers and netid
1330 find_service(rpcprog_t prog
, rpcvers_t vers
, char *netid
)
1332 register rpcblist_ptr hit
= NULL
;
1333 register rpcblist_ptr rbl
;
1335 for (rbl
= list_rbl
; rbl
!= NULL
; rbl
= rbl
->rpcb_next
) {
1336 if ((rbl
->rpcb_map
.r_prog
!= prog
) ||
1337 ((rbl
->rpcb_map
.r_netid
!= NULL
) &&
1338 (strcasecmp(rbl
->rpcb_map
.r_netid
, netid
) != 0)))
1341 if (rbl
->rpcb_map
.r_vers
== vers
)
1348 * Copies the name associated with the uid of the caller and returns
1349 * a pointer to it. Similar to getwd().
1352 getowner(SVCXPRT
*transp
, char *owner
, size_t ownersize
)
1354 struct sockcred
*sc
;
1356 sc
= __svc_getcallercreds(transp
);
1358 strlcpy(owner
, rpcbind_unknown
, ownersize
);
1359 else if (sc
->sc_uid
== 0)
1360 strlcpy(owner
, rpcbind_superuser
, ownersize
);
1362 snprintf(owner
, ownersize
, "%d", sc
->sc_uid
);
1369 * Add this to the pmap list only if it is UDP or TCP.
1372 add_pmaplist(RPCB
*arg
)
1375 struct pmaplist
*pml
;
1376 int h1
, h2
, h3
, h4
, p1
, p2
;
1378 if (strcmp(arg
->r_netid
, udptrans
) == 0) {
1380 pmap
.pm_prot
= IPPROTO_UDP
;
1381 } else if (strcmp(arg
->r_netid
, tcptrans
) == 0) {
1383 pmap
.pm_prot
= IPPROTO_TCP
;
1385 /* Not a IP protocol */
1388 /* interpret the universal address for TCP/IP */
1389 if (sscanf(arg
->r_addr
, "%d.%d.%d.%d.%d.%d",
1390 &h1
, &h2
, &h3
, &h4
, &p1
, &p2
) != 6)
1392 pmap
.pm_port
= ((p1
& 0xff) << 8) + (p2
& 0xff);
1393 pmap
.pm_prog
= arg
->r_prog
;
1394 pmap
.pm_vers
= arg
->r_vers
;
1396 * add to END of list
1398 pml
= (struct pmaplist
*) malloc((u_int
)sizeof (struct pmaplist
));
1400 (void) syslog(LOG_ERR
, "rpcbind: no memory!\n");
1403 pml
->pml_map
= pmap
;
1404 pml
->pml_next
= NULL
;
1405 if (list_pml
== NULL
) {
1408 struct pmaplist
*fnd
;
1410 /* Attach to the end of the list */
1411 for (fnd
= list_pml
; fnd
->pml_next
; fnd
= fnd
->pml_next
)
1413 fnd
->pml_next
= pml
;
1419 * Delete this from the pmap list only if it is UDP or TCP.
1422 del_pmaplist(RPCB
*arg
)
1424 struct pmaplist
*pml
;
1425 struct pmaplist
*prevpml
, *fnd
;
1428 if (strcmp(arg
->r_netid
, udptrans
) == 0) {
1431 } else if (strcmp(arg
->r_netid
, tcptrans
) == 0) {
1434 } else if (arg
->r_netid
[0] == 0) {
1435 prot
= 0; /* Remove all occurrences */
1437 /* Not a IP protocol */
1440 for (prevpml
= NULL
, pml
= list_pml
; pml
; /* cstyle */) {
1441 if ((pml
->pml_map
.pm_prog
!= arg
->r_prog
) ||
1442 (pml
->pml_map
.pm_vers
!= arg
->r_vers
) ||
1443 (prot
&& (pml
->pml_map
.pm_prot
!= prot
))) {
1444 /* both pml & prevpml move forwards */
1446 pml
= pml
->pml_next
;
1449 /* found it; pml moves forward, prevpml stays */
1451 pml
= pml
->pml_next
;
1452 if (prevpml
== NULL
)
1455 prevpml
->pml_next
= pml
;
1460 #endif /* PORTMAP */