2 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
3 * unrestricted use provided that this legend is included on all tape
4 * media and as a part of the software program in whole or part. Users
5 * may copy or modify Sun RPC without charge, but are not authorized
6 * to license or distribute it to anyone else except as part of a product or
7 * program developed by the user.
9 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
10 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
11 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
13 * Sun RPC is provided with no support and without any obligation on the
14 * part of Sun Microsystems, Inc. to assist in its use, correction,
15 * modification or enhancement.
17 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
18 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
19 * OR ANY PART THEREOF.
21 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
22 * or profits or other special, indirect and consequential damages, even if
23 * Sun has been advised of the possibility of such damages.
25 * Sun Microsystems, Inc.
27 * Mountain View, California 94043
29 * @(#)rpcb_svc_com.c 1.18 94/05/02 SMI
30 * $NetBSD: rpcb_svc_com.c,v 1.9 2002/11/08 00:16:39 fvdl Exp $
31 * $FreeBSD: src/usr.sbin/rpcbind/rpcb_svc_com.c,v 1.12 2007/11/07 10:53:39 kevlo Exp $
34 * Copyright (c) 1986 - 1991 by Sun Microsystems, Inc.
39 * The commom server procedure for the rpcbind.
42 #include <sys/types.h>
44 #include <sys/param.h>
46 #include <sys/socket.h>
48 #include <rpc/rpcb_prot.h>
49 #include <rpc/svc_dg.h>
50 #include <netconfig.h>
56 #include <netinet/in.h>
57 #include <rpc/pmap_prot.h>
64 #define RPC_BUF_MAX 65536 /* can be raised if required */
66 static char *nullstring
= "";
67 static int rpcb_rmtcalls
;
69 struct rmtcallfd_list
{
73 struct rmtcallfd_list
*next
;
77 #define MAXTIME_OFF 300 /* 5 minutes */
81 #define FINFO_ACTIVE 0x1
83 struct netbuf
*caller_addr
;
84 u_int32_t forward_xid
;
91 static struct finfo FINFO
[NFORWARD
];
94 static bool_t
xdr_encap_parms(XDR
*, struct encap_parms
*);
95 static bool_t
xdr_rmtcall_args(XDR
*, struct r_rmtcall_args
*);
96 static bool_t
xdr_rmtcall_result(XDR
*, struct r_rmtcall_args
*);
97 static bool_t
xdr_opaque_parms(XDR
*, struct r_rmtcall_args
*);
98 static int find_rmtcallfd_by_netid(char *);
99 static SVCXPRT
*find_rmtcallxprt_by_fd(int);
100 static int forward_register(u_int32_t
, struct netbuf
*, int,
101 char *, rpcproc_t
, rpcvers_t
,
103 static struct finfo
*forward_find(u_int32_t
);
104 static int free_slot_by_xid(u_int32_t
);
105 static int free_slot_by_index(int);
106 static int netbufcmp(struct netbuf
*, struct netbuf
*);
107 static struct netbuf
*netbufdup(struct netbuf
*);
108 static void netbuffree(struct netbuf
*);
109 static int check_rmtcalls(struct pollfd
*, int);
110 static void xprt_set_caller(SVCXPRT
*, struct finfo
*);
111 static void send_svcsyserr(SVCXPRT
*, struct finfo
*);
112 static void handle_reply(int, SVCXPRT
*);
113 static void find_versions(rpcprog_t
, char *, rpcvers_t
*,
115 static rpcblist_ptr
find_service(rpcprog_t
, rpcvers_t
, char *);
116 static char *getowner(SVCXPRT
*, char *, size_t);
117 static int add_pmaplist(RPCB
*);
118 static int del_pmaplist(RPCB
*);
121 * Set a mapping of program, version, netid
125 rpcbproc_set_com(void *arg
, struct svc_req
*rqstp __unused
, SVCXPRT
*transp
,
126 rpcvers_t rpcbversnum
)
128 RPCB
*regp
= (RPCB
*)arg
;
134 fprintf(stderr
, "RPCB_SET request for (%lu, %lu, %s, %s) : ",
135 (unsigned long)regp
->r_prog
, (unsigned long)regp
->r_vers
,
136 regp
->r_netid
, regp
->r_addr
);
138 ans
= map_set(regp
, getowner(transp
, owner
, sizeof owner
));
141 fprintf(stderr
, "%s\n", ans
== TRUE
? "succeeded" : "failed");
143 /* XXX: should have used some defined constant here */
144 rpcbs_set(rpcbversnum
- 2, ans
);
149 map_set(RPCB
*regp
, char *owner
)
152 rpcblist_ptr rbl
, fnd
;
156 * check to see if already used
157 * find_service returns a hit even if
158 * the versions don't match, so check for it
160 fnd
= find_service(reg
.r_prog
, reg
.r_vers
, reg
.r_netid
);
161 if (fnd
&& (fnd
->rpcb_map
.r_vers
== reg
.r_vers
)) {
162 if (!strcmp(fnd
->rpcb_map
.r_addr
, reg
.r_addr
))
164 * if these match then it is already
165 * registered so just say "OK".
172 * add to the end of the list
174 rbl
= malloc(sizeof (RPCBLIST
));
177 a
= &(rbl
->rpcb_map
);
178 a
->r_prog
= reg
.r_prog
;
179 a
->r_vers
= reg
.r_vers
;
180 a
->r_netid
= strdup(reg
.r_netid
);
181 a
->r_addr
= strdup(reg
.r_addr
);
182 a
->r_owner
= strdup(owner
);
183 if (!a
->r_addr
|| !a
->r_netid
|| !a
->r_owner
) {
193 rbl
->rpcb_next
= (rpcblist_ptr
)NULL
;
194 if (list_rbl
== NULL
) {
197 for (fnd
= list_rbl
; fnd
->rpcb_next
;
198 fnd
= fnd
->rpcb_next
)
200 fnd
->rpcb_next
= rbl
;
209 * Unset a mapping of program, version, netid
213 rpcbproc_unset_com(void *arg
, struct svc_req
*rqstp __unused
, SVCXPRT
*transp
,
214 rpcvers_t rpcbversnum
)
216 RPCB
*regp
= (RPCB
*)arg
;
222 fprintf(stderr
, "RPCB_UNSET request for (%lu, %lu, %s) : ",
223 (unsigned long)regp
->r_prog
, (unsigned long)regp
->r_vers
,
226 ans
= map_unset(regp
, getowner(transp
, owner
, sizeof owner
));
229 fprintf(stderr
, "%s\n", ans
== TRUE
? "succeeded" : "failed");
231 /* XXX: should have used some defined constant here */
232 rpcbs_unset(rpcbversnum
- 2, ans
);
237 map_unset(RPCB
*regp
, char *owner
)
240 rpcblist_ptr rbl
, prev
, tmp
;
245 for (prev
= NULL
, rbl
= list_rbl
; rbl
; /* cstyle */) {
246 if ((rbl
->rpcb_map
.r_prog
!= regp
->r_prog
) ||
247 (rbl
->rpcb_map
.r_vers
!= regp
->r_vers
) ||
248 (regp
->r_netid
[0] && strcasecmp(regp
->r_netid
,
249 rbl
->rpcb_map
.r_netid
))) {
250 /* both rbl & prev move forwards */
252 rbl
= rbl
->rpcb_next
;
256 * Check whether appropriate uid. Unset only
257 * if superuser or the owner itself.
259 if (strcmp(owner
, "superuser") &&
260 strcmp(rbl
->rpcb_map
.r_owner
, owner
))
262 /* found it; rbl moves forward, prev stays */
265 rbl
= rbl
->rpcb_next
;
269 prev
->rpcb_next
= rbl
;
270 free(tmp
->rpcb_map
.r_addr
);
271 free(tmp
->rpcb_map
.r_netid
);
272 free(tmp
->rpcb_map
.r_owner
);
280 * We return 1 either when the entry was not there or it
281 * was able to unset it. It can come to this point only if
282 * atleast one of the conditions is true.
288 delete_prog(unsigned int prog
)
293 for (rbl
= list_rbl
; rbl
!= NULL
; rbl
= rbl
->rpcb_next
) {
294 if ((rbl
->rpcb_map
.r_prog
!= prog
))
296 if (is_bound(rbl
->rpcb_map
.r_netid
, rbl
->rpcb_map
.r_addr
))
298 reg
.r_prog
= rbl
->rpcb_map
.r_prog
;
299 reg
.r_vers
= rbl
->rpcb_map
.r_vers
;
300 reg
.r_netid
= strdup(rbl
->rpcb_map
.r_netid
);
301 map_unset(®
, "superuser");
307 rpcbproc_getaddr_com(RPCB
*regp
, struct svc_req
*rqstp __unused
,
308 SVCXPRT
*transp
, rpcvers_t rpcbversnum
, rpcvers_t verstype
)
314 if (uaddr
!= NULL
&& uaddr
!= nullstring
) {
318 fnd
= find_service(regp
->r_prog
, regp
->r_vers
, transp
->xp_netid
);
319 if (fnd
&& ((verstype
== RPCB_ALLVERS
) ||
320 (regp
->r_vers
== fnd
->rpcb_map
.r_vers
))) {
321 if (*(regp
->r_addr
) != '\0') { /* may contain a hint about */
322 saddr
= regp
->r_addr
; /* the interface that we */
324 if (!(uaddr
= mergeaddr(transp
, transp
->xp_netid
,
325 fnd
->rpcb_map
.r_addr
, saddr
))) {
326 /* Try whatever we have */
327 uaddr
= strdup(fnd
->rpcb_map
.r_addr
);
328 } else if (!uaddr
[0]) {
330 * The server died. Unset all versions of this prog.
332 delete_prog(regp
->r_prog
);
340 fprintf(stderr
, "getaddr: %s\n", uaddr
);
342 /* XXX: should have used some defined constant here */
343 rpcbs_getaddr(rpcbversnum
- 2, regp
->r_prog
, regp
->r_vers
,
344 transp
->xp_netid
, uaddr
);
345 return (void *)&uaddr
;
350 rpcbproc_gettime_com(void *arg __unused
, struct svc_req
*rqstp __unused
,
351 SVCXPRT
*transp __unused
, rpcvers_t rpcbversnum __unused
)
353 static time_t curtime
;
356 return (void *)&curtime
;
360 * Convert uaddr to taddr. Should be used only by
361 * local servers/clients. (kernel level stuff only)
365 rpcbproc_uaddr2taddr_com(void *arg
, struct svc_req
*rqstp __unused
,
366 SVCXPRT
*transp
, rpcvers_t rpcbversnum __unused
)
368 char **uaddrp
= (char **)arg
;
369 struct netconfig
*nconf
;
370 static struct netbuf nbuf
;
371 static struct netbuf
*taddr
;
378 if (((nconf
= rpcbind_get_conf(transp
->xp_netid
)) == NULL
) ||
379 ((taddr
= uaddr2taddr(nconf
, *uaddrp
)) == NULL
)) {
380 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 __unused
,
393 SVCXPRT
*transp
, rpcvers_t rpcbversnum __unused
)
395 struct netbuf
*taddr
= (struct netbuf
*)arg
;
397 struct netconfig
*nconf
;
402 if ((fd
= open("/dev/null", O_RDONLY
)) == -1) {
403 uaddr
= (char *)strerror(errno
);
406 #endif /* CHEW_FDS */
407 if (uaddr
!= NULL
&& uaddr
!= nullstring
) {
411 if (((nconf
= rpcbind_get_conf(transp
->xp_netid
)) == NULL
) ||
412 ((uaddr
= taddr2uaddr(nconf
, taddr
)) == NULL
)) {
415 return (void *)&uaddr
;
420 xdr_encap_parms(XDR
*xdrs
, struct encap_parms
*epp
)
422 return (xdr_bytes(xdrs
, &(epp
->args
), (u_int
*) &(epp
->arglen
), ~0));
426 * XDR remote call arguments. It ignores the address part.
427 * written for XDR_DECODE direction only
430 xdr_rmtcall_args(XDR
*xdrs
, struct r_rmtcall_args
*cap
)
432 /* does not get the address or the arguments */
433 if (xdr_u_int32_t(xdrs
, &(cap
->rmt_prog
)) &&
434 xdr_u_int32_t(xdrs
, &(cap
->rmt_vers
)) &&
435 xdr_u_int32_t(xdrs
, &(cap
->rmt_proc
))) {
436 return (xdr_encap_parms(xdrs
, &(cap
->rmt_args
)));
442 * XDR remote call results along with the address. Ignore
443 * program number, version number and proc number.
444 * Written for XDR_ENCODE direction only.
447 xdr_rmtcall_result(XDR
*xdrs
, struct r_rmtcall_args
*cap
)
452 if (cap
->rmt_localvers
== PMAPVERS
) {
453 int h1
, h2
, h3
, h4
, p1
, p2
;
456 /* interpret the universal address for TCP/IP */
457 if (sscanf(cap
->rmt_uaddr
, "%d.%d.%d.%d.%d.%d",
458 &h1
, &h2
, &h3
, &h4
, &p1
, &p2
) != 6)
460 port
= ((p1
& 0xff) << 8) + (p2
& 0xff);
461 result
= xdr_u_long(xdrs
, &port
);
464 if ((cap
->rmt_localvers
== RPCBVERS
) ||
465 (cap
->rmt_localvers
== RPCBVERS4
)) {
466 result
= xdr_wrapstring(xdrs
, &(cap
->rmt_uaddr
));
471 return (xdr_encap_parms(xdrs
, &(cap
->rmt_args
)));
476 * only worries about the struct encap_parms part of struct r_rmtcall_args.
477 * The arglen must already be set!!
480 xdr_opaque_parms(XDR
*xdrs
, struct r_rmtcall_args
*cap
)
482 return (xdr_opaque(xdrs
, cap
->rmt_args
.args
, cap
->rmt_args
.arglen
));
485 static struct rmtcallfd_list
*rmthead
;
486 static struct rmtcallfd_list
*rmttail
;
489 create_rmtcall_fd(struct netconfig
*nconf
)
492 struct rmtcallfd_list
*rmt
;
495 if ((fd
= __rpc_nconf2fd(nconf
)) == -1) {
498 "create_rmtcall_fd: couldn't open \"%s\" (errno %d)\n",
499 nconf
->nc_device
, errno
);
502 xprt
= svc_tli_create(fd
, 0, NULL
, 0, 0);
506 "create_rmtcall_fd: svc_tli_create failed\n");
509 rmt
= malloc(sizeof (struct rmtcallfd_list
));
511 syslog(LOG_ERR
, "create_rmtcall_fd: no memory!");
515 rmt
->netid
= strdup(nconf
->nc_netid
);
516 xprt
->xp_netid
= rmt
->netid
;
519 if (rmthead
== NULL
) {
526 /* XXX not threadsafe */
529 FD_SET(fd
, &svc_fdset
);
534 find_rmtcallfd_by_netid(char *netid
)
536 struct rmtcallfd_list
*rmt
;
538 for (rmt
= rmthead
; rmt
!= NULL
; rmt
= rmt
->next
) {
539 if (strcmp(netid
, rmt
->netid
) == 0) {
547 find_rmtcallxprt_by_fd(int fd
)
549 struct rmtcallfd_list
*rmt
;
551 for (rmt
= rmthead
; rmt
!= NULL
; rmt
= rmt
->next
) {
561 * Call a remote procedure service. This procedure is very quiet when things
562 * go wrong. The proc is written to support broadcast rpc. In the broadcast
563 * case, a machine should shut-up instead of complain, lest the requestor be
564 * overrun with complaints at the expense of not hearing a valid reply.
565 * When receiving a request and verifying that the service exists, we
567 * receive the request
569 * open a new TLI endpoint on the same transport on which we received
570 * the original request
572 * remember the original request's XID (which requires knowing the format
573 * of the svc_dg_data structure)
575 * forward the request, with a new XID, to the requested service,
576 * remembering the XID used to send this request (for later use in
577 * reassociating the answer with the original request), the requestor's
578 * address, the file descriptor on which the forwarded request is
579 * made and the service's address.
581 * mark the file descriptor on which we anticipate receiving a reply from
582 * the service and one to select for in our private svc_run procedure
584 * At some time in the future, a reply will be received from the service to
585 * which we forwarded the request. At that time, we detect that the socket
586 * used was for forwarding (by looking through the finfo structures to see
587 * whether the fd corresponds to one of those) and call handle_reply() to
591 * bundle the reply, along with the service's universal address
593 * create a SVCXPRT structure and use a version of svc_sendreply
594 * that allows us to specify the reply XID and destination, send the reply
595 * to the original requestor.
599 rpcbproc_callit_com(struct svc_req
*rqstp
, SVCXPRT
*transp
,
600 rpcproc_t reply_type
, rpcvers_t versnum
)
603 struct netconfig
*nconf
;
604 struct netbuf
*caller
;
605 struct r_rmtcall_args a
;
606 char *buf_alloc
= NULL
, *outbufp
;
607 char *outbuf_alloc
= NULL
;
608 char buf
[RPC_BUF_MAX
], outbuf
[RPC_BUF_MAX
];
609 struct netbuf
*na
= NULL
;
610 struct rpc_msg call_msg
;
616 char *uaddr
, *m_uaddr
= NULL
, *local_uaddr
= NULL
;
618 struct __rpc_sockinfo si
;
619 struct sockaddr
*localsa
;
622 if (!__rpc_fd2sockinfo(transp
->xp_fd
, &si
)) {
623 if (reply_type
== RPCBPROC_INDIRECT
)
624 svcerr_systemerr(transp
);
627 if (si
.si_socktype
!= SOCK_DGRAM
)
628 return; /* Only datagram type accepted */
629 sendsz
= __rpc_get_t_size(si
.si_af
, si
.si_proto
, UDPMSGSIZE
);
630 if (sendsz
== 0) { /* data transfer not supported */
631 if (reply_type
== RPCBPROC_INDIRECT
)
632 svcerr_systemerr(transp
);
636 * Should be multiple of 4 for XDR.
638 sendsz
= ((sendsz
+ 3) / 4) * 4;
639 if (sendsz
> RPC_BUF_MAX
) {
641 buf_alloc
= alloca(sendsz
); /* not in IDR2? */
643 buf_alloc
= malloc(sendsz
);
645 if (buf_alloc
== NULL
) {
648 "rpcbproc_callit_com: No Memory!\n");
649 if (reply_type
== RPCBPROC_INDIRECT
)
650 svcerr_systemerr(transp
);
653 a
.rmt_args
.args
= buf_alloc
;
655 a
.rmt_args
.args
= buf
;
658 call_msg
.rm_xid
= 0; /* For error checking purposes */
659 if (!svc_getargs(transp
, (xdrproc_t
) xdr_rmtcall_args
, (char *) &a
)) {
660 if (reply_type
== RPCBPROC_INDIRECT
)
661 svcerr_decode(transp
);
664 "rpcbproc_callit_com: svc_getargs failed\n");
668 if (!check_callit(transp
, &a
, versnum
)) {
669 svcerr_weakauth(transp
);
673 caller
= svc_getrpccaller(transp
);
676 uaddr
= taddr2uaddr(rpcbind_get_conf(transp
->xp_netid
), caller
);
677 fprintf(stderr
, "%s %s req for (%lu, %lu, %lu, %s) from %s : ",
678 versnum
== PMAPVERS
? "pmap_rmtcall" :
679 versnum
== RPCBVERS
? "rpcb_rmtcall" :
680 versnum
== RPCBVERS4
? "rpcb_indirect" : "unknown",
681 reply_type
== RPCBPROC_INDIRECT
? "indirect" : "callit",
682 (unsigned long)a
.rmt_prog
, (unsigned long)a
.rmt_vers
,
683 (unsigned long)a
.rmt_proc
, transp
->xp_netid
,
684 uaddr
? uaddr
: "unknown");
690 rbl
= find_service(a
.rmt_prog
, a
.rmt_vers
, transp
->xp_netid
);
692 rpcbs_rmtcall(versnum
- 2, reply_type
, a
.rmt_prog
, a
.rmt_vers
,
693 a
.rmt_proc
, transp
->xp_netid
, rbl
);
695 if (rbl
== (rpcblist_ptr
)NULL
) {
698 fprintf(stderr
, "not found\n");
700 if (reply_type
== RPCBPROC_INDIRECT
)
701 svcerr_noprog(transp
);
704 if (rbl
->rpcb_map
.r_vers
!= a
.rmt_vers
) {
705 if (reply_type
== RPCBPROC_INDIRECT
) {
706 rpcvers_t vers_low
, vers_high
;
708 find_versions(a
.rmt_prog
, transp
->xp_netid
,
709 &vers_low
, &vers_high
);
710 svcerr_progvers(transp
, vers_low
, vers_high
);
717 fprintf(stderr
, "found at uaddr %s\n", rbl
->rpcb_map
.r_addr
);
720 * Check whether this entry is valid and a server is present
721 * Mergeaddr() returns NULL if no such entry is present, and
722 * returns "" if the entry was present but the server is not
723 * present (i.e., it crashed).
725 if (reply_type
== RPCBPROC_INDIRECT
) {
726 uaddr
= mergeaddr(transp
, transp
->xp_netid
,
727 rbl
->rpcb_map
.r_addr
, NULL
);
728 if (uaddr
== NULL
|| uaddr
[0] == '\0') {
729 svcerr_noprog(transp
);
736 nconf
= rpcbind_get_conf(transp
->xp_netid
);
738 if (reply_type
== RPCBPROC_INDIRECT
)
739 svcerr_systemerr(transp
);
742 "rpcbproc_callit_com: rpcbind_get_conf failed\n");
745 localsa
= local_sa(((struct sockaddr
*)caller
->buf
)->sa_family
);
746 if (localsa
== NULL
) {
749 "rpcbproc_callit_com: no local address\n");
752 tbuf
.len
= tbuf
.maxlen
= localsa
->sa_len
;
755 addrmerge(&tbuf
, rbl
->rpcb_map
.r_addr
, NULL
, nconf
->nc_netid
);
756 m_uaddr
= addrmerge(caller
, rbl
->rpcb_map
.r_addr
, NULL
,
760 fprintf(stderr
, "merged uaddr %s\n", m_uaddr
);
762 if ((fd
= find_rmtcallfd_by_netid(nconf
->nc_netid
)) == -1) {
763 if (reply_type
== RPCBPROC_INDIRECT
)
764 svcerr_systemerr(transp
);
767 xidp
= __rpcb_get_dg_xidp(transp
);
768 switch (forward_register(*xidp
, caller
, fd
, m_uaddr
, reply_type
,
769 versnum
, &call_msg
.rm_xid
)) {
771 /* Success; forward_register() will free m_uaddr for us. */
776 * A duplicate request for the slow server. Let's not
777 * beat on it any more.
781 "rpcbproc_callit_com: duplicate request\n");
784 /* forward_register failed. Perhaps no memory. */
787 "rpcbproc_callit_com: forward_register failed\n");
794 "rpcbproc_callit_com: original XID %x, new XID %x\n",
795 *xidp
, call_msg
.rm_xid
);
797 call_msg
.rm_direction
= CALL
;
798 call_msg
.rm_call
.cb_rpcvers
= RPC_MSG_VERSION
;
799 call_msg
.rm_call
.cb_prog
= a
.rmt_prog
;
800 call_msg
.rm_call
.cb_vers
= a
.rmt_vers
;
801 if (sendsz
> RPC_BUF_MAX
) {
803 outbuf_alloc
= alloca(sendsz
); /* not in IDR2? */
805 outbuf_alloc
= malloc(sendsz
);
807 if (outbuf_alloc
== NULL
) {
808 if (reply_type
== RPCBPROC_INDIRECT
)
809 svcerr_systemerr(transp
);
812 "rpcbproc_callit_com: No memory!\n");
815 xdrmem_create(&outxdr
, outbuf_alloc
, sendsz
, XDR_ENCODE
);
817 xdrmem_create(&outxdr
, outbuf
, sendsz
, XDR_ENCODE
);
819 if (!xdr_callhdr(&outxdr
, &call_msg
)) {
820 if (reply_type
== RPCBPROC_INDIRECT
)
821 svcerr_systemerr(transp
);
824 "rpcbproc_callit_com: xdr_callhdr failed\n");
827 if (!xdr_u_int32_t(&outxdr
, &(a
.rmt_proc
))) {
828 if (reply_type
== RPCBPROC_INDIRECT
)
829 svcerr_systemerr(transp
);
832 "rpcbproc_callit_com: xdr_u_long failed\n");
836 if (rqstp
->rq_cred
.oa_flavor
== AUTH_NULL
) {
837 auth
= authnone_create();
838 } else if (rqstp
->rq_cred
.oa_flavor
== AUTH_SYS
) {
839 struct authunix_parms
*au
;
841 au
= (struct authunix_parms
*)rqstp
->rq_clntcred
;
842 auth
= authunix_create(au
->aup_machname
,
843 au
->aup_uid
, au
->aup_gid
,
844 au
->aup_len
, au
->aup_gids
);
845 if (auth
== NULL
) /* fall back */
846 auth
= authnone_create();
848 /* we do not support any other authentication scheme */
851 "rpcbproc_callit_com: oa_flavor != AUTH_NONE and oa_flavor != AUTH_SYS\n");
852 if (reply_type
== RPCBPROC_INDIRECT
)
853 svcerr_weakauth(transp
); /* XXX too strong.. */
857 if (reply_type
== RPCBPROC_INDIRECT
)
858 svcerr_systemerr(transp
);
861 "rpcbproc_callit_com: authwhatever_create returned NULL\n");
864 if (!AUTH_MARSHALL(auth
, &outxdr
)) {
865 if (reply_type
== RPCBPROC_INDIRECT
)
866 svcerr_systemerr(transp
);
870 "rpcbproc_callit_com: AUTH_MARSHALL failed\n");
874 if (!xdr_opaque_parms(&outxdr
, &a
)) {
875 if (reply_type
== RPCBPROC_INDIRECT
)
876 svcerr_systemerr(transp
);
879 "rpcbproc_callit_com: xdr_opaque_parms failed\n");
882 outlen
= (int) XDR_GETPOS(&outxdr
);
884 outbufp
= outbuf_alloc
;
888 na
= uaddr2taddr(nconf
, local_uaddr
);
890 if (reply_type
== RPCBPROC_INDIRECT
)
891 svcerr_systemerr(transp
);
895 if (sendto(fd
, outbufp
, outlen
, 0, (struct sockaddr
*)na
->buf
, na
->len
)
899 "rpcbproc_callit_com: sendto failed: errno %d\n", errno
);
900 if (reply_type
== RPCBPROC_INDIRECT
)
901 svcerr_systemerr(transp
);
907 if (call_msg
.rm_xid
!= 0)
908 free_slot_by_xid(call_msg
.rm_xid
);
925 * Makes an entry into the FIFO for the given request.
926 * Returns 1 on success, 0 if this is a duplicate request, or -1 on error.
927 * *callxidp is set to the xid of the call.
930 forward_register(u_int32_t caller_xid
, struct netbuf
*caller_addr
,
931 int forward_fd
, char *uaddr
, rpcproc_t reply_type
,
932 rpcvers_t versnum
, u_int32_t
*callxidp
)
936 time_t min_time
, time_now
;
937 static u_int32_t lastxid
;
940 min_time
= FINFO
[0].time
;
941 time_now
= time(NULL
);
944 lastxid
= time_now
* NFORWARD
;
947 * Check if it is a duplicate entry. Then,
948 * try to find an empty slot. If not available, then
949 * use the slot with the earliest time.
951 for (i
= 0; i
< NFORWARD
; i
++) {
952 if (FINFO
[i
].flag
& FINFO_ACTIVE
) {
953 if ((FINFO
[i
].caller_xid
== caller_xid
) &&
954 (FINFO
[i
].reply_type
== reply_type
) &&
955 (FINFO
[i
].versnum
== versnum
) &&
956 (!netbufcmp(FINFO
[i
].caller_addr
,
958 FINFO
[i
].time
= time(NULL
);
959 return (0); /* Duplicate entry */
961 /* Should we wait any longer */
962 if ((time_now
- FINFO
[i
].time
) > MAXTIME_OFF
)
963 free_slot_by_index(i
);
967 if ((FINFO
[i
].flag
& FINFO_ACTIVE
) == 0) {
969 } else if (FINFO
[i
].time
< min_time
) {
971 min_time
= FINFO
[i
].time
;
976 /* use this empty slot */
979 free_slot_by_index(j
);
981 if ((FINFO
[j
].caller_addr
= netbufdup(caller_addr
)) == NULL
) {
984 rpcb_rmtcalls
++; /* no of pending calls */
985 FINFO
[j
].flag
= FINFO_ACTIVE
;
986 FINFO
[j
].reply_type
= reply_type
;
987 FINFO
[j
].versnum
= versnum
;
988 FINFO
[j
].time
= time_now
;
989 FINFO
[j
].caller_xid
= caller_xid
;
990 FINFO
[j
].forward_fd
= forward_fd
;
992 * Though uaddr is not allocated here, it will still be freed
993 * from free_slot_*().
995 FINFO
[j
].uaddr
= uaddr
;
996 lastxid
= lastxid
+ NFORWARD
;
997 /* Don't allow a zero xid below. */
998 if ((u_int32_t
)(lastxid
+ NFORWARD
) <= NFORWARD
)
1000 FINFO
[j
].forward_xid
= lastxid
+ j
; /* encode slot */
1001 *callxidp
= FINFO
[j
].forward_xid
; /* forward on this xid */
1005 static struct finfo
*
1006 forward_find(u_int32_t reply_xid
)
1010 i
= reply_xid
% (u_int32_t
)NFORWARD
;
1011 if ((FINFO
[i
].flag
& FINFO_ACTIVE
) &&
1012 (FINFO
[i
].forward_xid
== reply_xid
)) {
1019 free_slot_by_xid(u_int32_t xid
)
1023 entry
= xid
% (u_int32_t
)NFORWARD
;
1024 return (free_slot_by_index(entry
));
1028 free_slot_by_index(int index
)
1033 if (fi
->flag
& FINFO_ACTIVE
) {
1034 netbuffree(fi
->caller_addr
);
1035 /* XXX may be too big, but can't access xprt array here */
1036 if (fi
->forward_fd
>= svc_maxfd
)
1039 fi
->flag
&= ~FINFO_ACTIVE
;
1047 netbufcmp(struct netbuf
*n1
, struct netbuf
*n2
)
1049 return ((n1
->len
!= n2
->len
) || memcmp(n1
->buf
, n2
->buf
, n1
->len
));
1052 static struct netbuf
*
1053 netbufdup(struct netbuf
*ap
)
1057 if ((np
= malloc(sizeof(struct netbuf
))) == NULL
)
1059 if ((np
->buf
= malloc(ap
->len
)) == NULL
) {
1063 np
->maxlen
= np
->len
= ap
->len
;
1064 memcpy(np
->buf
, ap
->buf
, ap
->len
);
1069 netbuffree(struct netbuf
*ap
)
1076 #define MASKVAL (POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND)
1077 extern bool_t
__svc_clean_idle(fd_set
*, int, bool_t
);
1083 struct pollfd pollfds
[FD_SETSIZE
];
1084 int poll_ret
, check_ret
;
1086 #ifdef SVC_RUN_DEBUG
1094 for (n
= 0; n
<= svc_maxfd
; n
++) {
1095 if (FD_ISSET(n
, &svc_fdset
)) {
1097 p
->events
= MASKVAL
;
1103 #ifdef SVC_RUN_DEBUG
1105 fprintf(stderr
, "polling for read on fd < ");
1106 for (i
= 0, p
= pollfds
; i
< nfds
; i
++, p
++)
1108 fprintf(stderr
, "%d ", p
->fd
);
1109 fprintf(stderr
, ">\n");
1112 switch (poll_ret
= poll(pollfds
, nfds
, 30 * 1000)) {
1115 * We ignore all errors, continuing with the assumption
1116 * that it was set by the signal handlers (or any
1117 * other outside event) and not caused by poll().
1120 cleanfds
= svc_fdset
;
1121 __svc_clean_idle(&cleanfds
, 30, FALSE
);
1124 #ifdef SVC_RUN_DEBUG
1126 fprintf(stderr
, "poll returned read fds < ");
1127 for (i
= 0, p
= pollfds
; i
< nfds
; i
++, p
++)
1129 fprintf(stderr
, "%d ", p
->fd
);
1130 fprintf(stderr
, ">\n");
1134 * If we found as many replies on callback fds
1135 * as the number of descriptors selectable which
1136 * poll() returned, there can be no more so we
1137 * don't call svc_getreq_poll. Otherwise, there
1138 * must be another so we must call svc_getreq_poll.
1140 if ((check_ret
= check_rmtcalls(pollfds
, nfds
)) ==
1143 svc_getreq_poll(pollfds
, poll_ret
-check_ret
);
1145 #ifdef SVC_RUN_DEBUG
1147 fprintf(stderr
, "svc_maxfd now %u\n", svc_maxfd
);
1154 check_rmtcalls(struct pollfd
*pfds
, int nfds
)
1156 int j
, ncallbacks_found
= 0, rmtcalls_pending
;
1159 if (rpcb_rmtcalls
== 0)
1162 rmtcalls_pending
= rpcb_rmtcalls
;
1163 for (j
= 0; j
< nfds
; j
++) {
1164 if ((xprt
= find_rmtcallxprt_by_fd(pfds
[j
].fd
)) != NULL
) {
1165 if (pfds
[j
].revents
) {
1167 #ifdef DEBUG_RMTCALL
1170 "my_svc_run: polled on forwarding fd %d, netid %s - calling handle_reply\n",
1171 pfds
[j
].fd
, xprt
->xp_netid
);
1173 handle_reply(pfds
[j
].fd
, xprt
);
1174 pfds
[j
].revents
= 0;
1175 if (ncallbacks_found
>= rmtcalls_pending
) {
1181 return (ncallbacks_found
);
1185 xprt_set_caller(SVCXPRT
*xprt
, struct finfo
*fi
)
1189 *(svc_getrpccaller(xprt
)) = *(fi
->caller_addr
);
1190 xidp
= __rpcb_get_dg_xidp(xprt
);
1191 *xidp
= fi
->caller_xid
;
1195 * Call svcerr_systemerr() only if RPCBVERS4
1198 send_svcsyserr(SVCXPRT
*xprt
, struct finfo
*fi
)
1200 if (fi
->reply_type
== RPCBPROC_INDIRECT
) {
1201 xprt_set_caller(xprt
, fi
);
1202 svcerr_systemerr(xprt
);
1208 handle_reply(int fd
, SVCXPRT
*xprt
)
1211 struct rpc_msg reply_msg
;
1212 struct rpc_err reply_error
;
1215 int inlen
, pos
, len
;
1216 struct r_rmtcall_args a
;
1217 struct sockaddr_storage ss
;
1219 #ifdef SVC_RUN_DEBUG
1223 buffer
= malloc(RPC_BUF_MAX
);
1228 inlen
= recvfrom(fd
, buffer
, RPC_BUF_MAX
, 0,
1229 (struct sockaddr
*)&ss
, &fromlen
);
1230 } while (inlen
< 0 && errno
== EINTR
);
1234 "handle_reply: recvfrom returned %d, errno %d\n", inlen
, errno
);
1238 reply_msg
.acpted_rply
.ar_verf
= _null_auth
;
1239 reply_msg
.acpted_rply
.ar_results
.where
= 0;
1240 reply_msg
.acpted_rply
.ar_results
.proc
= (xdrproc_t
) xdr_void
;
1242 xdrmem_create(&reply_xdrs
, buffer
, (u_int
)inlen
, XDR_DECODE
);
1243 if (!xdr_replymsg(&reply_xdrs
, &reply_msg
)) {
1245 fprintf(stderr
, "handle_reply: xdr_replymsg failed\n");
1248 fi
= forward_find(reply_msg
.rm_xid
);
1249 #ifdef SVC_RUN_DEBUG
1251 fprintf(stderr
, "handle_reply: reply xid: %d fi addr: %p\n",
1252 reply_msg
.rm_xid
, fi
);
1258 _seterr_reply(&reply_msg
, &reply_error
);
1259 if (reply_error
.re_status
!= RPC_SUCCESS
) {
1261 fprintf(stderr
, "handle_reply: %s\n",
1262 clnt_sperrno(reply_error
.re_status
));
1263 send_svcsyserr(xprt
, fi
);
1266 pos
= XDR_GETPOS(&reply_xdrs
);
1268 a
.rmt_args
.args
= &buffer
[pos
];
1269 a
.rmt_args
.arglen
= len
;
1270 a
.rmt_uaddr
= fi
->uaddr
;
1271 a
.rmt_localvers
= fi
->versnum
;
1273 xprt_set_caller(xprt
, fi
);
1274 #ifdef SVC_RUN_DEBUG
1275 uaddr
= taddr2uaddr(rpcbind_get_conf("udp"),
1276 svc_getrpccaller(xprt
));
1278 fprintf(stderr
, "handle_reply: forwarding address %s to %s\n",
1279 a
.rmt_uaddr
, uaddr
? uaddr
: "unknown");
1284 svc_sendreply(xprt
, (xdrproc_t
) xdr_rmtcall_result
, (char *) &a
);
1289 if (reply_msg
.rm_xid
== 0) {
1290 #ifdef SVC_RUN_DEBUG
1292 fprintf(stderr
, "handle_reply: NULL xid on exit!\n");
1296 free_slot_by_xid(reply_msg
.rm_xid
);
1301 find_versions(rpcprog_t prog
, char *netid
, rpcvers_t
*lowvp
, rpcvers_t
*highvp
)
1304 unsigned int lowv
= 0;
1305 unsigned int highv
= 0;
1307 for (rbl
= list_rbl
; rbl
!= NULL
; rbl
= rbl
->rpcb_next
) {
1308 if ((rbl
->rpcb_map
.r_prog
!= prog
) ||
1309 ((rbl
->rpcb_map
.r_netid
!= NULL
) &&
1310 (strcasecmp(rbl
->rpcb_map
.r_netid
, netid
) != 0)))
1313 highv
= rbl
->rpcb_map
.r_vers
;
1315 } else if (rbl
->rpcb_map
.r_vers
< lowv
) {
1316 lowv
= rbl
->rpcb_map
.r_vers
;
1317 } else if (rbl
->rpcb_map
.r_vers
> highv
) {
1318 highv
= rbl
->rpcb_map
.r_vers
;
1327 * returns the item with the given program, version number and netid.
1328 * If that version number is not found, it returns the item with that
1329 * program number, so that address is now returned to the caller. The
1330 * caller when makes a call to this program, version number, the call
1331 * will fail and it will return with PROGVERS_MISMATCH. The user can
1332 * then determine the highest and the lowest version number for this
1333 * program using clnt_geterr() and use those program version numbers.
1335 * Returns the RPCBLIST for the given prog, vers and netid
1338 find_service(rpcprog_t prog
, rpcvers_t vers
, char *netid
)
1340 rpcblist_ptr hit
= NULL
;
1343 for (rbl
= list_rbl
; rbl
!= NULL
; rbl
= rbl
->rpcb_next
) {
1344 if ((rbl
->rpcb_map
.r_prog
!= prog
) ||
1345 ((rbl
->rpcb_map
.r_netid
!= NULL
) &&
1346 (strcasecmp(rbl
->rpcb_map
.r_netid
, netid
) != 0)))
1349 if (rbl
->rpcb_map
.r_vers
== vers
)
1356 * Copies the name associated with the uid of the caller and returns
1357 * a pointer to it. Similar to getwd().
1360 getowner(SVCXPRT
*transp
, char *owner
, size_t ownersize
)
1364 if (__rpc_get_local_uid(transp
, &uid
) < 0)
1365 strlcpy(owner
, "unknown", ownersize
);
1367 strlcpy(owner
, "superuser", ownersize
);
1369 snprintf(owner
, ownersize
, "%d", uid
);
1376 * Add this to the pmap list only if it is UDP or TCP.
1379 add_pmaplist(RPCB
*arg
)
1382 struct pmaplist
*pml
;
1383 int h1
, h2
, h3
, h4
, p1
, p2
;
1385 if (strcmp(arg
->r_netid
, udptrans
) == 0) {
1387 pmap
.pm_prot
= IPPROTO_UDP
;
1388 } else if (strcmp(arg
->r_netid
, tcptrans
) == 0) {
1390 pmap
.pm_prot
= IPPROTO_TCP
;
1392 /* Not an IP protocol */
1395 /* interpret the universal address for TCP/IP */
1396 if (sscanf(arg
->r_addr
, "%d.%d.%d.%d.%d.%d",
1397 &h1
, &h2
, &h3
, &h4
, &p1
, &p2
) != 6)
1399 pmap
.pm_port
= ((p1
& 0xff) << 8) + (p2
& 0xff);
1400 pmap
.pm_prog
= arg
->r_prog
;
1401 pmap
.pm_vers
= arg
->r_vers
;
1403 * add to END of list
1405 pml
= malloc(sizeof (struct pmaplist
));
1407 syslog(LOG_ERR
, "rpcbind: no memory!\n");
1410 pml
->pml_map
= pmap
;
1411 pml
->pml_next
= NULL
;
1412 if (list_pml
== NULL
) {
1415 struct pmaplist
*fnd
;
1417 /* Attach to the end of the list */
1418 for (fnd
= list_pml
; fnd
->pml_next
; fnd
= fnd
->pml_next
)
1420 fnd
->pml_next
= pml
;
1426 * Delete this from the pmap list only if it is UDP or TCP.
1429 del_pmaplist(RPCB
*arg
)
1431 struct pmaplist
*pml
;
1432 struct pmaplist
*prevpml
, *fnd
;
1435 if (strcmp(arg
->r_netid
, udptrans
) == 0) {
1438 } else if (strcmp(arg
->r_netid
, tcptrans
) == 0) {
1441 } else if (arg
->r_netid
[0] == 0) {
1442 prot
= 0; /* Remove all occurrences */
1444 /* Not an IP protocol */
1447 for (prevpml
= NULL
, pml
= list_pml
; pml
; /* cstyle */) {
1448 if ((pml
->pml_map
.pm_prog
!= arg
->r_prog
) ||
1449 (pml
->pml_map
.pm_vers
!= arg
->r_vers
) ||
1450 (prot
&& (pml
->pml_map
.pm_prot
!= prot
))) {
1451 /* both pml & prevpml move forwards */
1453 pml
= pml
->pml_next
;
1456 /* found it; pml moves forward, prevpml stays */
1458 pml
= pml
->pml_next
;
1459 if (prevpml
== NULL
)
1462 prevpml
->pml_next
= pml
;
1467 #endif /* PORTMAP */