1 /* $NetBSD: rpcb_svc_com.c,v 1.13 2007/08/27 19:51:50 dsl 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>
64 #define RPC_BUF_MAX 65536 /* can be raised if required */
66 static char emptystring
[] = "";
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 u_int32_t
forward_register(u_int32_t
, struct netbuf
*, int, char *,
101 rpcproc_t
, rpcvers_t
);
102 static struct finfo
*forward_find(u_int32_t
);
103 static int free_slot_by_xid(u_int32_t
);
104 static int free_slot_by_index(int);
105 static int netbufcmp(struct netbuf
*, struct netbuf
*);
106 static struct netbuf
*netbufdup(struct netbuf
*);
107 static void netbuffree(struct netbuf
*);
108 static int check_rmtcalls(struct pollfd
*, int);
109 static void xprt_set_caller(SVCXPRT
*, struct finfo
*);
110 static void send_svcsyserr(SVCXPRT
*, struct finfo
*);
111 static void handle_reply(int, SVCXPRT
*);
112 static void find_versions(rpcprog_t
, char *, rpcvers_t
*, rpcvers_t
*);
113 static rpcblist_ptr
find_service(rpcprog_t
, rpcvers_t
, char *);
114 static char *getowner(SVCXPRT
*, char *, size_t);
115 static int add_pmaplist(RPCB
*);
116 static int del_pmaplist(RPCB
*);
119 * Set a mapping of program, version, netid
123 rpcbproc_set_com(void *arg
, struct svc_req
*rqstp
, SVCXPRT
*transp
,
124 rpcvers_t rpcbversnum
)
126 RPCB
*regp
= (RPCB
*)arg
;
132 fprintf(stderr
, "RPCB_SET request for (%lu, %lu, %s, %s) : ",
133 (unsigned long)regp
->r_prog
, (unsigned long)regp
->r_vers
,
134 regp
->r_netid
, regp
->r_addr
);
136 ans
= map_set(regp
, getowner(transp
, owner
, sizeof owner
));
139 fprintf(stderr
, "%s\n", ans
== TRUE
? "succeeded" : "failed");
141 /* XXX: should have used some defined constant here */
142 rpcbs_set(rpcbversnum
- 2, ans
);
147 map_set(RPCB
*regp
, char *owner
)
150 rpcblist_ptr rbl
, fnd
;
154 * check to see if already used
155 * find_service returns a hit even if
156 * the versions don't match, so check for it
158 fnd
= find_service(reg
.r_prog
, reg
.r_vers
, reg
.r_netid
);
159 if (fnd
&& (fnd
->rpcb_map
.r_vers
== reg
.r_vers
)) {
160 if (!strcmp(fnd
->rpcb_map
.r_addr
, reg
.r_addr
))
162 * if these match then it is already
163 * registered so just say "OK".
170 * add to the end of the list
172 rbl
= (rpcblist_ptr
) malloc((u_int
)sizeof (RPCBLIST
));
173 if (rbl
== (rpcblist_ptr
)NULL
) {
176 a
= &(rbl
->rpcb_map
);
177 a
->r_prog
= reg
.r_prog
;
178 a
->r_vers
= reg
.r_vers
;
179 a
->r_netid
= strdup(reg
.r_netid
);
180 a
->r_addr
= strdup(reg
.r_addr
);
181 a
->r_owner
= strdup(owner
);
182 if (!a
->r_addr
|| !a
->r_netid
|| !a
->r_owner
) {
184 free((void *) a
->r_netid
);
186 free((void *) a
->r_addr
);
188 free((void *) a
->r_owner
);
192 rbl
->rpcb_next
= (rpcblist_ptr
)NULL
;
193 if (list_rbl
== NULL
) {
196 for (fnd
= list_rbl
; fnd
->rpcb_next
;
197 fnd
= fnd
->rpcb_next
)
199 fnd
->rpcb_next
= rbl
;
202 (void) add_pmaplist(regp
);
208 * Unset a mapping of program, version, netid
212 rpcbproc_unset_com(void *arg
, struct svc_req
*rqstp
, SVCXPRT
*transp
,
213 rpcvers_t rpcbversnum
)
215 RPCB
*regp
= (RPCB
*)arg
;
221 fprintf(stderr
, "RPCB_UNSET request for (%lu, %lu, %s) : ",
222 (unsigned long)regp
->r_prog
, (unsigned long)regp
->r_vers
,
225 ans
= map_unset(regp
, getowner(transp
, owner
, sizeof owner
));
228 fprintf(stderr
, "%s\n", ans
== TRUE
? "succeeded" : "failed");
230 /* XXX: should have used some defined constant here */
231 rpcbs_unset(rpcbversnum
- 2, ans
);
236 map_unset(RPCB
*regp
, const char *owner
)
239 rpcblist_ptr rbl
, prev
, tmp
;
244 for (prev
= NULL
, rbl
= list_rbl
; rbl
; /* cstyle */) {
245 if ((rbl
->rpcb_map
.r_prog
!= regp
->r_prog
) ||
246 (rbl
->rpcb_map
.r_vers
!= regp
->r_vers
) ||
247 (regp
->r_netid
[0] && strcasecmp(regp
->r_netid
,
248 rbl
->rpcb_map
.r_netid
))) {
249 /* both rbl & prev move forwards */
251 rbl
= rbl
->rpcb_next
;
255 * Check whether appropriate uid. Unset only
256 * if superuser or the owner itself.
258 if (strcmp(owner
, rpcbind_superuser
) &&
259 strcmp(rbl
->rpcb_map
.r_owner
, owner
))
261 /* found it; rbl moves forward, prev stays */
264 rbl
= rbl
->rpcb_next
;
268 prev
->rpcb_next
= rbl
;
269 free((void *) tmp
->rpcb_map
.r_addr
);
270 free((void *) tmp
->rpcb_map
.r_netid
);
271 free((void *) tmp
->rpcb_map
.r_owner
);
276 (void) del_pmaplist(regp
);
279 * We return 1 either when the entry was not there or it
280 * was able to unset it. It can come to this point only if
281 * atleast one of the conditions is true.
287 delete_prog(rpcprog_t prog
)
290 register rpcblist_ptr rbl
;
292 for (rbl
= list_rbl
; rbl
!= NULL
; rbl
= rbl
->rpcb_next
) {
293 if ((rbl
->rpcb_map
.r_prog
!= prog
))
295 if (is_bound(rbl
->rpcb_map
.r_netid
, rbl
->rpcb_map
.r_addr
))
297 reg
.r_prog
= rbl
->rpcb_map
.r_prog
;
298 reg
.r_vers
= rbl
->rpcb_map
.r_vers
;
299 reg
.r_netid
= strdup(rbl
->rpcb_map
.r_netid
);
300 (void)map_unset(®
, rpcbind_superuser
);
306 rpcbproc_getaddr_com(RPCB
*regp
, struct svc_req
*rqstp
, SVCXPRT
*transp
,
307 rpcvers_t rpcbversnum
, rpcvers_t verstype
)
313 if (uaddr
&& uaddr
[0])
314 free((void *) uaddr
);
315 fnd
= find_service(regp
->r_prog
, regp
->r_vers
, transp
->xp_netid
);
316 if (fnd
&& ((verstype
== RPCB_ALLVERS
) ||
317 (regp
->r_vers
== fnd
->rpcb_map
.r_vers
))) {
318 if (*(regp
->r_addr
) != '\0') { /* may contain a hint about */
319 saddr
= regp
->r_addr
; /* the interface that we */
321 if (!(uaddr
= mergeaddr(transp
, transp
->xp_netid
,
322 fnd
->rpcb_map
.r_addr
, saddr
))) {
323 /* Try whatever we have */
324 uaddr
= strdup(fnd
->rpcb_map
.r_addr
);
325 } else if (!uaddr
[0]) {
327 * The server died. Unset all versions of this prog.
329 delete_prog(regp
->r_prog
);
337 fprintf(stderr
, "getaddr: %s\n", uaddr
);
339 /* XXX: should have used some defined constant here */
340 rpcbs_getaddr(rpcbversnum
- 2, regp
->r_prog
, regp
->r_vers
,
341 transp
->xp_netid
, uaddr
);
342 return (void *)&uaddr
;
347 rpcbproc_gettime_com(void *arg
, struct svc_req
*rqstp
, SVCXPRT
*transp
,
348 rpcvers_t rpcbversnum
)
350 static time_t curtime
;
352 (void) time(&curtime
);
353 return (void *)&curtime
;
357 * Convert uaddr to taddr. Should be used only by
358 * local servers/clients. (kernel level stuff only)
362 rpcbproc_uaddr2taddr_com(void *arg
, struct svc_req
*rqstp
, SVCXPRT
*transp
,
363 rpcvers_t rpcbversnum
)
365 char **uaddrp
= (char **)arg
;
366 struct netconfig
*nconf
;
367 static struct netbuf nbuf
;
368 static struct netbuf
*taddr
;
371 free((void *) taddr
->buf
);
372 free((void *) taddr
);
374 if (((nconf
= rpcbind_get_conf(transp
->xp_netid
)) == NULL
) ||
375 ((taddr
= uaddr2taddr(nconf
, *uaddrp
)) == NULL
)) {
376 (void) memset((char *)&nbuf
, 0, sizeof (struct netbuf
));
377 return (void *)&nbuf
;
379 return (void *)taddr
;
383 * Convert taddr to uaddr. Should be used only by
384 * local servers/clients. (kernel level stuff only)
388 rpcbproc_taddr2uaddr_com(void *arg
, struct svc_req
*rqstp
, SVCXPRT
*transp
,
389 rpcvers_t rpcbversnum
)
391 struct netbuf
*taddr
= (struct netbuf
*)arg
;
393 struct netconfig
*nconf
;
398 if ((fd
= open("/dev/null", O_RDONLY
)) == -1) {
399 uaddr
= strerror(errno
);
402 #endif /* CHEW_FDS */
403 if (uaddr
&& !uaddr
[0])
404 free((void *) uaddr
);
405 if (((nconf
= rpcbind_get_conf(transp
->xp_netid
)) == NULL
) ||
406 ((uaddr
= taddr2uaddr(nconf
, taddr
)) == NULL
)) {
409 return (void *)&uaddr
;
414 xdr_encap_parms(XDR
*xdrs
, struct encap_parms
*epp
)
416 return (xdr_bytes(xdrs
, &(epp
->args
), (u_int
*) &(epp
->arglen
), ~0));
420 * XDR remote call arguments. It ignores the address part.
421 * written for XDR_DECODE direction only
424 xdr_rmtcall_args(XDR
*xdrs
, struct r_rmtcall_args
*cap
)
426 /* does not get the address or the arguments */
427 if (xdr_u_int32_t(xdrs
, &(cap
->rmt_prog
)) &&
428 xdr_u_int32_t(xdrs
, &(cap
->rmt_vers
)) &&
429 xdr_u_int32_t(xdrs
, &(cap
->rmt_proc
))) {
430 return (xdr_encap_parms(xdrs
, &(cap
->rmt_args
)));
436 * XDR remote call results along with the address. Ignore
437 * program number, version number and proc number.
438 * Written for XDR_ENCODE direction only.
441 xdr_rmtcall_result(XDR
*xdrs
, struct r_rmtcall_args
*cap
)
446 if (cap
->rmt_localvers
== PMAPVERS
) {
447 int h1
, h2
, h3
, h4
, p1
, p2
;
450 /* interpret the universal address for TCP/IP */
451 if (sscanf(cap
->rmt_uaddr
, "%d.%d.%d.%d.%d.%d",
452 &h1
, &h2
, &h3
, &h4
, &p1
, &p2
) != 6)
454 port
= ((p1
& 0xff) << 8) + (p2
& 0xff);
455 result
= xdr_u_long(xdrs
, &port
);
458 if ((cap
->rmt_localvers
== RPCBVERS
) ||
459 (cap
->rmt_localvers
== RPCBVERS4
)) {
460 result
= xdr_wrapstring(xdrs
, &(cap
->rmt_uaddr
));
465 return (xdr_encap_parms(xdrs
, &(cap
->rmt_args
)));
470 * only worries about the struct encap_parms part of struct r_rmtcall_args.
471 * The arglen must already be set!!
474 xdr_opaque_parms(XDR
*xdrs
, struct r_rmtcall_args
*cap
)
476 return (xdr_opaque(xdrs
, cap
->rmt_args
.args
, cap
->rmt_args
.arglen
));
479 static struct rmtcallfd_list
*rmthead
;
480 static struct rmtcallfd_list
*rmttail
;
483 create_rmtcall_fd(struct netconfig
*nconf
)
486 struct rmtcallfd_list
*rmt
;
489 if ((fd
= __rpc_nconf2fd(nconf
)) == -1) {
492 "create_rmtcall_fd: couldn't open \"%s\" (errno %d)\n",
493 nconf
->nc_device
, errno
);
496 xprt
= svc_tli_create(fd
, 0, (struct t_bind
*) 0, 0, 0);
500 "create_rmtcall_fd: svc_tli_create failed\n");
503 rmt
= (struct rmtcallfd_list
*)malloc((u_int
)
504 sizeof (struct rmtcallfd_list
));
506 syslog(LOG_ERR
, "create_rmtcall_fd: no memory!");
510 rmt
->netid
= strdup(nconf
->nc_netid
);
511 xprt
->xp_netid
= rmt
->netid
;
514 if (rmthead
== NULL
) {
521 /* XXX not threadsafe */
524 FD_SET(fd
, &svc_fdset
);
529 find_rmtcallfd_by_netid(char *netid
)
531 struct rmtcallfd_list
*rmt
;
533 for (rmt
= rmthead
; rmt
!= NULL
; rmt
= rmt
->next
) {
534 if (strcmp(netid
, rmt
->netid
) == 0) {
542 find_rmtcallxprt_by_fd(int fd
)
544 struct rmtcallfd_list
*rmt
;
546 for (rmt
= rmthead
; rmt
!= NULL
; rmt
= rmt
->next
) {
556 * Call a remote procedure service. This procedure is very quiet when things
557 * go wrong. The proc is written to support broadcast rpc. In the broadcast
558 * case, a machine should shut-up instead of complain, lest the requestor be
559 * overrun with complaints at the expense of not hearing a valid reply.
560 * When receiving a request and verifying that the service exists, we
562 * receive the request
564 * open a new TLI endpoint on the same transport on which we received
565 * the original request
567 * remember the original request's XID (which requires knowing the format
568 * of the svc_dg_data structure)
570 * forward the request, with a new XID, to the requested service,
571 * remembering the XID used to send this request (for later use in
572 * reassociating the answer with the original request), the requestor's
573 * address, the file descriptor on which the forwarded request is
574 * made and the service's address.
576 * mark the file descriptor on which we anticipate receiving a reply from
577 * the service and one to select for in our private svc_run procedure
579 * At some time in the future, a reply will be received from the service to
580 * which we forwarded the request. At that time, we detect that the socket
581 * used was for forwarding (by looking through the finfo structures to see
582 * whether the fd corresponds to one of those) and call handle_reply() to
586 * bundle the reply, along with the service's universal address
588 * create a SVCXPRT structure and use a version of svc_sendreply
589 * that allows us to specify the reply XID and destination, send the reply
590 * to the original requestor.
594 rpcbproc_callit_com(struct svc_req
*rqstp
, SVCXPRT
*transp
,
595 rpcproc_t reply_type
, rpcvers_t versnum
)
597 register rpcblist_ptr rbl
;
598 struct netconfig
*nconf
;
599 struct netbuf
*caller
;
600 struct r_rmtcall_args a
;
601 char *buf_alloc
= NULL
, *outbufp
;
602 char *outbuf_alloc
= NULL
;
603 char buf
[RPC_BUF_MAX
], outbuf
[RPC_BUF_MAX
];
604 struct netbuf
*na
= (struct netbuf
*) NULL
;
605 struct rpc_msg call_msg
;
611 char *uaddr
, *m_uaddr
, *local_uaddr
= NULL
;
613 struct __rpc_sockinfo si
;
614 struct sockaddr
*localsa
;
617 if (!__rpc_fd2sockinfo(transp
->xp_fd
, &si
)) {
618 if (reply_type
== RPCBPROC_INDIRECT
)
619 svcerr_systemerr(transp
);
622 if (si
.si_socktype
!= SOCK_DGRAM
)
623 return; /* Only datagram type accepted */
624 sendsz
= __rpc_get_t_size(si
.si_af
, si
.si_proto
, UDPMSGSIZE
);
625 if (sendsz
== 0) { /* data transfer not supported */
626 if (reply_type
== RPCBPROC_INDIRECT
)
627 svcerr_systemerr(transp
);
631 * Should be multiple of 4 for XDR.
633 sendsz
= ((sendsz
+ 3) / 4) * 4;
634 if (sendsz
> RPC_BUF_MAX
) {
636 buf_alloc
= alloca(sendsz
); /* not in IDR2? */
638 buf_alloc
= malloc(sendsz
);
640 if (buf_alloc
== NULL
) {
643 "rpcbproc_callit_com: No Memory!\n");
644 if (reply_type
== RPCBPROC_INDIRECT
)
645 svcerr_systemerr(transp
);
648 a
.rmt_args
.args
= buf_alloc
;
650 a
.rmt_args
.args
= buf
;
653 call_msg
.rm_xid
= 0; /* For error checking purposes */
654 if (!svc_getargs(transp
, (xdrproc_t
) xdr_rmtcall_args
, (char *) &a
)) {
655 if (reply_type
== RPCBPROC_INDIRECT
)
656 svcerr_decode(transp
);
659 "rpcbproc_callit_com: svc_getargs failed\n");
663 if (!check_callit(transp
, &a
, versnum
)) {
664 svcerr_weakauth(transp
);
668 caller
= svc_getrpccaller(transp
);
671 uaddr
= taddr2uaddr(rpcbind_get_conf(transp
->xp_netid
), caller
);
672 fprintf(stderr
, "%s %s req for (%lu, %lu, %lu, %s) from %s : ",
673 versnum
== PMAPVERS
? "pmap_rmtcall" :
674 versnum
== RPCBVERS
? "rpcb_rmtcall" :
675 versnum
== RPCBVERS4
? "rpcb_indirect" :
677 reply_type
== RPCBPROC_INDIRECT
? "indirect" : "callit",
678 (unsigned long)a
.rmt_prog
, (unsigned long)a
.rmt_vers
,
679 (unsigned long)a
.rmt_proc
, transp
->xp_netid
,
680 uaddr
? uaddr
: rpcbind_unknown
);
682 free((void *) uaddr
);
686 rbl
= find_service(a
.rmt_prog
, a
.rmt_vers
, transp
->xp_netid
);
688 rpcbs_rmtcall(versnum
- 2, reply_type
, a
.rmt_prog
, a
.rmt_vers
,
689 a
.rmt_proc
, transp
->xp_netid
, rbl
);
691 if (rbl
== (rpcblist_ptr
)NULL
) {
694 fprintf(stderr
, "not found\n");
696 if (reply_type
== RPCBPROC_INDIRECT
)
697 svcerr_noprog(transp
);
700 if (rbl
->rpcb_map
.r_vers
!= a
.rmt_vers
) {
701 if (reply_type
== RPCBPROC_INDIRECT
) {
702 rpcvers_t vers_low
, vers_high
;
704 find_versions(a
.rmt_prog
, transp
->xp_netid
,
705 &vers_low
, &vers_high
);
706 svcerr_progvers(transp
, vers_low
, vers_high
);
713 fprintf(stderr
, "found at uaddr %s\n", rbl
->rpcb_map
.r_addr
);
716 * Check whether this entry is valid and a server is present
717 * Mergeaddr() returns NULL if no such entry is present, and
718 * returns "" if the entry was present but the server is not
719 * present (i.e., it crashed).
721 if (reply_type
== RPCBPROC_INDIRECT
) {
722 uaddr
= mergeaddr(transp
, transp
->xp_netid
,
723 rbl
->rpcb_map
.r_addr
, NULL
);
724 if ((uaddr
== (char *) NULL
) || uaddr
[0] == '\0') {
725 svcerr_noprog(transp
);
727 free((void *) uaddr
);
732 free((void *) uaddr
);
735 nconf
= rpcbind_get_conf(transp
->xp_netid
);
736 if (nconf
== (struct netconfig
*)NULL
) {
737 if (reply_type
== RPCBPROC_INDIRECT
)
738 svcerr_systemerr(transp
);
741 "rpcbproc_callit_com: rpcbind_get_conf failed\n");
744 localsa
= local_sa(((struct sockaddr
*)caller
->buf
)->sa_family
);
745 if (localsa
== NULL
) {
748 "rpcbproc_callit_com: no local address\n");
751 tbuf
.len
= tbuf
.maxlen
= localsa
->sa_len
;
754 addrmerge(&tbuf
, rbl
->rpcb_map
.r_addr
, NULL
, nconf
->nc_netid
);
755 m_uaddr
= addrmerge(caller
, rbl
->rpcb_map
.r_addr
, NULL
,
759 fprintf(stderr
, "merged uaddr %s\n", m_uaddr
);
761 if ((fd
= find_rmtcallfd_by_netid(nconf
->nc_netid
)) == -1) {
762 if (reply_type
== RPCBPROC_INDIRECT
)
763 svcerr_systemerr(transp
);
764 free((void *) m_uaddr
);
767 xidp
= __rpcb_get_dg_xidp(transp
);
768 call_msg
.rm_xid
= forward_register(*xidp
,
769 caller
, fd
, m_uaddr
, reply_type
, versnum
);
770 if (call_msg
.rm_xid
== 0) {
772 * A duplicate request for the slow server. Let's not
773 * beat on it any more.
777 "rpcbproc_callit_com: duplicate request\n");
778 free((void *) m_uaddr
);
780 } else if (call_msg
.rm_xid
== (uint32_t)-1) {
781 /* forward_register failed. Perhaps no memory. */
784 "rpcbproc_callit_com: forward_register failed\n");
785 free((void *) m_uaddr
);
792 "rpcbproc_callit_com: original XID %x, new XID %x\n",
793 *xidp
, call_msg
.rm_xid
);
795 call_msg
.rm_direction
= CALL
;
796 call_msg
.rm_call
.cb_rpcvers
= RPC_MSG_VERSION
;
797 call_msg
.rm_call
.cb_prog
= a
.rmt_prog
;
798 call_msg
.rm_call
.cb_vers
= a
.rmt_vers
;
799 if (sendsz
> RPC_BUF_MAX
) {
801 outbuf_alloc
= alloca(sendsz
); /* not in IDR2? */
803 outbuf_alloc
= malloc(sendsz
);
805 if (outbuf_alloc
== NULL
) {
806 if (reply_type
== RPCBPROC_INDIRECT
)
807 svcerr_systemerr(transp
);
810 "rpcbproc_callit_com: No memory!\n");
813 xdrmem_create(&outxdr
, outbuf_alloc
, sendsz
, XDR_ENCODE
);
815 xdrmem_create(&outxdr
, outbuf
, sendsz
, XDR_ENCODE
);
817 if (!xdr_callhdr(&outxdr
, &call_msg
)) {
818 if (reply_type
== RPCBPROC_INDIRECT
)
819 svcerr_systemerr(transp
);
822 "rpcbproc_callit_com: xdr_callhdr failed\n");
825 if (!xdr_u_int32_t(&outxdr
, &(a
.rmt_proc
))) {
826 if (reply_type
== RPCBPROC_INDIRECT
)
827 svcerr_systemerr(transp
);
830 "rpcbproc_callit_com: xdr_u_long failed\n");
834 if (rqstp
->rq_cred
.oa_flavor
== AUTH_NULL
) {
835 auth
= authnone_create();
836 } else if (rqstp
->rq_cred
.oa_flavor
== AUTH_SYS
) {
837 struct authunix_parms
*au
;
839 au
= (struct authunix_parms
*)rqstp
->rq_clntcred
;
840 auth
= authunix_create(au
->aup_machname
,
841 au
->aup_uid
, au
->aup_gid
,
842 au
->aup_len
, au
->aup_gids
);
843 if (auth
== NULL
) /* fall back */
844 auth
= authnone_create();
846 /* we do not support any other authentication scheme */
849 "rpcbproc_callit_com: oa_flavor != AUTH_NONE and oa_flavor != AUTH_SYS\n");
850 if (reply_type
== RPCBPROC_INDIRECT
)
851 svcerr_weakauth(transp
); /* XXX too strong.. */
855 if (reply_type
== RPCBPROC_INDIRECT
)
856 svcerr_systemerr(transp
);
859 "rpcbproc_callit_com: authwhatever_create returned NULL\n");
862 if (!AUTH_MARSHALL(auth
, &outxdr
)) {
863 if (reply_type
== RPCBPROC_INDIRECT
)
864 svcerr_systemerr(transp
);
868 "rpcbproc_callit_com: AUTH_MARSHALL failed\n");
872 if (!xdr_opaque_parms(&outxdr
, &a
)) {
873 if (reply_type
== RPCBPROC_INDIRECT
)
874 svcerr_systemerr(transp
);
877 "rpcbproc_callit_com: xdr_opaque_parms failed\n");
880 outlen
= (int) XDR_GETPOS(&outxdr
);
882 outbufp
= outbuf_alloc
;
886 na
= uaddr2taddr(nconf
, local_uaddr
);
888 if (reply_type
== RPCBPROC_INDIRECT
)
889 svcerr_systemerr(transp
);
893 if (sendto(fd
, outbufp
, outlen
, 0, (struct sockaddr
*)na
->buf
, na
->len
)
897 "rpcbproc_callit_com: sendto failed: errno %d\n", errno
);
898 if (reply_type
== RPCBPROC_INDIRECT
)
899 svcerr_systemerr(transp
);
905 if (call_msg
.rm_xid
!= 0)
906 (void) free_slot_by_xid(call_msg
.rm_xid
);
911 free((void *) buf_alloc
);
913 free((void *) outbuf_alloc
);
921 * Makes an entry into the FIFO for the given request.
922 * If duplicate request, returns a 0, else returns the xid of its call.
925 forward_register(u_int32_t caller_xid
, struct netbuf
*caller_addr
,
926 int forward_fd
, char *uaddr
, rpcproc_t reply_type
,
931 time_t min_time
, time_now
;
932 static u_int32_t lastxid
;
935 min_time
= FINFO
[0].time
;
936 time_now
= time((time_t *)0);
939 lastxid
= time_now
* NFORWARD
;
942 * Check if it is an duplicate entry. Then,
943 * try to find an empty slot. If not available, then
944 * use the slot with the earliest time.
946 for (i
= 0; i
< NFORWARD
; i
++) {
947 if (FINFO
[i
].flag
& FINFO_ACTIVE
) {
948 if ((FINFO
[i
].caller_xid
== caller_xid
) &&
949 (FINFO
[i
].reply_type
== reply_type
) &&
950 (FINFO
[i
].versnum
== versnum
) &&
951 (!netbufcmp(FINFO
[i
].caller_addr
,
953 FINFO
[i
].time
= time((time_t *)0);
954 return (0); /* Duplicate entry */
956 /* Should we wait any longer */
957 if ((time_now
- FINFO
[i
].time
) > MAXTIME_OFF
)
958 (void) free_slot_by_index(i
);
962 if ((FINFO
[i
].flag
& FINFO_ACTIVE
) == 0) {
964 } else if (FINFO
[i
].time
< min_time
) {
966 min_time
= FINFO
[i
].time
;
971 /* use this empty slot */
974 (void) free_slot_by_index(j
);
976 if ((FINFO
[j
].caller_addr
= netbufdup(caller_addr
)) == NULL
) {
979 rpcb_rmtcalls
++; /* no of pending calls */
980 FINFO
[j
].flag
= FINFO_ACTIVE
;
981 FINFO
[j
].reply_type
= reply_type
;
982 FINFO
[j
].versnum
= versnum
;
983 FINFO
[j
].time
= time_now
;
984 FINFO
[j
].caller_xid
= caller_xid
;
985 FINFO
[j
].forward_fd
= forward_fd
;
987 * Though uaddr is not allocated here, it will still be freed
988 * from free_slot_*().
990 FINFO
[j
].uaddr
= uaddr
;
991 lastxid
= lastxid
+ NFORWARD
;
992 FINFO
[j
].forward_xid
= lastxid
+ j
; /* encode slot */
993 return (FINFO
[j
].forward_xid
); /* forward on this xid */
996 static struct finfo
*
997 forward_find(u_int32_t reply_xid
)
1001 i
= reply_xid
% NFORWARD
;
1004 if ((FINFO
[i
].flag
& FINFO_ACTIVE
) &&
1005 (FINFO
[i
].forward_xid
== reply_xid
)) {
1012 free_slot_by_xid(u_int32_t xid
)
1016 entry
= xid
% NFORWARD
;
1019 return (free_slot_by_index(entry
));
1023 free_slot_by_index(int idx
)
1028 if (fi
->flag
& FINFO_ACTIVE
) {
1029 netbuffree(fi
->caller_addr
);
1030 /* XXX may be too big, but can't access xprt array here */
1031 if (fi
->forward_fd
>= svc_maxfd
)
1033 free((void *) fi
->uaddr
);
1034 fi
->flag
&= ~FINFO_ACTIVE
;
1042 netbufcmp(struct netbuf
*n1
, struct netbuf
*n2
)
1044 return ((n1
->len
!= n2
->len
) || memcmp(n1
->buf
, n2
->buf
, n1
->len
));
1047 static struct netbuf
*
1048 netbufdup(struct netbuf
*ap
)
1052 np
= (struct netbuf
*) malloc(sizeof (struct netbuf
) + ap
->len
);
1054 np
->maxlen
= np
->len
= ap
->len
;
1055 np
->buf
= ((char *) np
) + sizeof (struct netbuf
);
1056 (void) memcpy(np
->buf
, ap
->buf
, ap
->len
);
1062 netbuffree(struct netbuf
*ap
)
1068 #define MASKVAL (POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND)
1069 extern bool_t
__svc_clean_idle(fd_set
*, int, bool_t
);
1075 struct pollfd pollfds
[FD_SETSIZE
];
1076 int poll_ret
, check_ret
;
1078 #ifdef SVC_RUN_DEBUG
1081 register struct pollfd
*p
;
1086 for (n
= 0; n
<= svc_maxfd
; n
++) {
1087 if (FD_ISSET(n
, &svc_fdset
)) {
1089 p
->events
= MASKVAL
;
1095 #ifdef SVC_RUN_DEBUG
1097 fprintf(stderr
, "polling for read on fd < ");
1098 for (i
= 0, p
= pollfds
; i
< nfds
; i
++, p
++)
1100 fprintf(stderr
, "%d ", p
->fd
);
1101 fprintf(stderr
, ">\n");
1104 switch (poll_ret
= poll(pollfds
, nfds
, 30 * 1000)) {
1107 * We ignore all errors, continuing with the assumption
1108 * that it was set by the signal handlers (or any
1109 * other outside event) and not caused by poll().
1112 cleanfds
= svc_fdset
;
1113 __svc_clean_idle(&cleanfds
, 30, FALSE
);
1116 #ifdef SVC_RUN_DEBUG
1118 fprintf(stderr
, "poll returned read fds < ");
1119 for (i
= 0, p
= pollfds
; i
< nfds
; i
++, p
++)
1121 fprintf(stderr
, "%d ", p
->fd
);
1122 fprintf(stderr
, ">\n");
1126 * If we found as many replies on callback fds
1127 * as the number of descriptors selectable which
1128 * poll() returned, there can be no more so we
1129 * don't call svc_getreq_poll. Otherwise, there
1130 * must be another so we must call svc_getreq_poll.
1132 if ((check_ret
= check_rmtcalls(pollfds
, nfds
)) ==
1135 svc_getreq_poll(pollfds
, poll_ret
-check_ret
);
1137 #ifdef SVC_RUN_DEBUG
1139 fprintf(stderr
, "svc_maxfd now %u\n", svc_maxfd
);
1146 check_rmtcalls(struct pollfd
*pfds
, int nfds
)
1148 int j
, ncallbacks_found
= 0, rmtcalls_pending
;
1151 if (rpcb_rmtcalls
== 0)
1154 rmtcalls_pending
= rpcb_rmtcalls
;
1155 for (j
= 0; j
< nfds
; j
++) {
1156 if ((xprt
= find_rmtcallxprt_by_fd(pfds
[j
].fd
)) != NULL
) {
1157 if (pfds
[j
].revents
) {
1159 #ifdef DEBUG_RMTCALL
1162 "my_svc_run: polled on forwarding fd %d, netid %s - calling handle_reply\n",
1163 pfds
[j
].fd
, xprt
->xp_netid
);
1165 handle_reply(pfds
[j
].fd
, xprt
);
1166 pfds
[j
].revents
= 0;
1167 if (ncallbacks_found
>= rmtcalls_pending
) {
1173 return (ncallbacks_found
);
1177 xprt_set_caller(SVCXPRT
*xprt
, struct finfo
*fi
)
1181 *(svc_getrpccaller(xprt
)) = *(fi
->caller_addr
);
1182 xidp
= __rpcb_get_dg_xidp(xprt
);
1183 *xidp
= fi
->caller_xid
;
1187 * Call svcerr_systemerr() only if RPCBVERS4
1190 send_svcsyserr(SVCXPRT
*xprt
, struct finfo
*fi
)
1192 if (fi
->reply_type
== RPCBPROC_INDIRECT
) {
1193 xprt_set_caller(xprt
, fi
);
1194 svcerr_systemerr(xprt
);
1200 handle_reply(int fd
, SVCXPRT
*xprt
)
1203 struct rpc_msg reply_msg
;
1204 struct rpc_err reply_error
;
1207 int inlen
, pos
, len
;
1208 struct r_rmtcall_args a
;
1209 struct sockaddr_storage ss
;
1211 #ifdef SVC_RUN_DEBUG
1215 buffer
= malloc(RPC_BUF_MAX
);
1220 fromlen
= sizeof ss
;
1221 inlen
= recvfrom(fd
, buffer
, RPC_BUF_MAX
, 0,
1222 (struct sockaddr
*)&ss
, &fromlen
);
1223 } while (inlen
< 0 && errno
== EINTR
);
1227 "handle_reply: recvfrom returned %d, errno %d\n", inlen
, errno
);
1231 reply_msg
.acpted_rply
.ar_verf
= _null_auth
;
1232 reply_msg
.acpted_rply
.ar_results
.where
= 0;
1233 reply_msg
.acpted_rply
.ar_results
.proc
= (xdrproc_t
) xdr_void
;
1235 xdrmem_create(&reply_xdrs
, buffer
, (u_int
)inlen
, XDR_DECODE
);
1236 if (!xdr_replymsg(&reply_xdrs
, &reply_msg
)) {
1238 (void) fprintf(stderr
,
1239 "handle_reply: xdr_replymsg failed\n");
1242 fi
= forward_find(reply_msg
.rm_xid
);
1243 #ifdef SVC_RUN_DEBUG
1245 fprintf(stderr
, "handle_reply: reply xid: %d fi addr: %p\n",
1246 reply_msg
.rm_xid
, fi
);
1252 _seterr_reply(&reply_msg
, &reply_error
);
1253 if (reply_error
.re_status
!= RPC_SUCCESS
) {
1255 (void) fprintf(stderr
, "handle_reply: %s\n",
1256 clnt_sperrno(reply_error
.re_status
));
1257 send_svcsyserr(xprt
, fi
);
1260 pos
= XDR_GETPOS(&reply_xdrs
);
1262 a
.rmt_args
.args
= &buffer
[pos
];
1263 a
.rmt_args
.arglen
= len
;
1264 a
.rmt_uaddr
= fi
->uaddr
;
1265 a
.rmt_localvers
= fi
->versnum
;
1267 xprt_set_caller(xprt
, fi
);
1268 #ifdef SVC_RUN_DEBUG
1269 uaddr
= taddr2uaddr(rpcbind_get_conf("udp"),
1270 svc_getrpccaller(xprt
));
1272 fprintf(stderr
, "handle_reply: forwarding address %s to %s\n",
1273 a
.rmt_uaddr
, uaddr
? uaddr
: rpcbind_unknown
);
1276 free((void *) uaddr
);
1278 svc_sendreply(xprt
, (xdrproc_t
) xdr_rmtcall_result
, (char *) &a
);
1283 if (reply_msg
.rm_xid
== 0) {
1284 #ifdef SVC_RUN_DEBUG
1286 fprintf(stderr
, "handle_reply: NULL xid on exit!\n");
1290 (void) free_slot_by_xid(reply_msg
.rm_xid
);
1295 find_versions(rpcprog_t prog
, char *netid
, rpcvers_t
*lowvp
, rpcvers_t
*highvp
)
1297 register rpcblist_ptr rbl
;
1299 rpcvers_t highv
= 0;
1301 for (rbl
= list_rbl
; rbl
!= NULL
; rbl
= rbl
->rpcb_next
) {
1302 if ((rbl
->rpcb_map
.r_prog
!= prog
) ||
1303 ((rbl
->rpcb_map
.r_netid
!= NULL
) &&
1304 (strcasecmp(rbl
->rpcb_map
.r_netid
, netid
) != 0)))
1307 highv
= rbl
->rpcb_map
.r_vers
;
1309 } else if (rbl
->rpcb_map
.r_vers
< lowv
) {
1310 lowv
= rbl
->rpcb_map
.r_vers
;
1311 } else if (rbl
->rpcb_map
.r_vers
> highv
) {
1312 highv
= rbl
->rpcb_map
.r_vers
;
1321 * returns the item with the given program, version number and netid.
1322 * If that version number is not found, it returns the item with that
1323 * program number, so that address is now returned to the caller. The
1324 * caller when makes a call to this program, version number, the call
1325 * will fail and it will return with PROGVERS_MISMATCH. The user can
1326 * then determine the highest and the lowest version number for this
1327 * program using clnt_geterr() and use those program version numbers.
1329 * Returns the RPCBLIST for the given prog, vers and netid
1332 find_service(rpcprog_t prog
, rpcvers_t vers
, char *netid
)
1334 register rpcblist_ptr hit
= NULL
;
1335 register rpcblist_ptr rbl
;
1337 for (rbl
= list_rbl
; rbl
!= NULL
; rbl
= rbl
->rpcb_next
) {
1338 if ((rbl
->rpcb_map
.r_prog
!= prog
) ||
1339 ((rbl
->rpcb_map
.r_netid
!= NULL
) &&
1340 (strcasecmp(rbl
->rpcb_map
.r_netid
, netid
) != 0)))
1343 if (rbl
->rpcb_map
.r_vers
== vers
)
1350 * Copies the name associated with the uid of the caller and returns
1351 * a pointer to it. Similar to getwd().
1354 getowner(SVCXPRT
*transp
, char *owner
, size_t ownersize
)
1356 struct sockcred
*sc
;
1358 sc
= __svc_getcallercreds(transp
);
1360 strlcpy(owner
, rpcbind_unknown
, ownersize
);
1361 else if (sc
->sc_uid
== 0)
1362 strlcpy(owner
, rpcbind_superuser
, ownersize
);
1364 snprintf(owner
, ownersize
, "%d", sc
->sc_uid
);
1371 * Add this to the pmap list only if it is UDP or TCP.
1374 add_pmaplist(RPCB
*arg
)
1377 struct pmaplist
*pml
;
1378 int h1
, h2
, h3
, h4
, p1
, p2
;
1380 if (strcmp(arg
->r_netid
, udptrans
) == 0) {
1382 pmap
.pm_prot
= IPPROTO_UDP
;
1383 } else if (strcmp(arg
->r_netid
, tcptrans
) == 0) {
1385 pmap
.pm_prot
= IPPROTO_TCP
;
1387 /* Not a IP protocol */
1390 /* interpret the universal address for TCP/IP */
1391 if (sscanf(arg
->r_addr
, "%d.%d.%d.%d.%d.%d",
1392 &h1
, &h2
, &h3
, &h4
, &p1
, &p2
) != 6)
1394 pmap
.pm_port
= ((p1
& 0xff) << 8) + (p2
& 0xff);
1395 pmap
.pm_prog
= arg
->r_prog
;
1396 pmap
.pm_vers
= arg
->r_vers
;
1398 * add to END of list
1400 pml
= (struct pmaplist
*) malloc((u_int
)sizeof (struct pmaplist
));
1402 (void) syslog(LOG_ERR
, "rpcbind: no memory!\n");
1405 pml
->pml_map
= pmap
;
1406 pml
->pml_next
= NULL
;
1407 if (list_pml
== NULL
) {
1410 struct pmaplist
*fnd
;
1412 /* Attach to the end of the list */
1413 for (fnd
= list_pml
; fnd
->pml_next
; fnd
= fnd
->pml_next
)
1415 fnd
->pml_next
= pml
;
1421 * Delete this from the pmap list only if it is UDP or TCP.
1424 del_pmaplist(RPCB
*arg
)
1426 struct pmaplist
*pml
;
1427 struct pmaplist
*prevpml
, *fnd
;
1430 if (strcmp(arg
->r_netid
, udptrans
) == 0) {
1433 } else if (strcmp(arg
->r_netid
, tcptrans
) == 0) {
1436 } else if (arg
->r_netid
[0] == 0) {
1437 prot
= 0; /* Remove all occurrences */
1439 /* Not a IP protocol */
1442 for (prevpml
= NULL
, pml
= list_pml
; pml
; /* cstyle */) {
1443 if ((pml
->pml_map
.pm_prog
!= arg
->r_prog
) ||
1444 (pml
->pml_map
.pm_vers
!= arg
->r_vers
) ||
1445 (prot
&& (pml
->pml_map
.pm_prot
!= prot
))) {
1446 /* both pml & prevpml move forwards */
1448 pml
= pml
->pml_next
;
1451 /* found it; pml moves forward, prevpml stays */
1453 pml
= pml
->pml_next
;
1454 if (prevpml
== NULL
)
1457 prevpml
->pml_next
= pml
;
1462 #endif /* PORTMAP */