1 /* $NetBSD: rpcb_svc_com.c,v 1.9 2002/11/08 00:16:39 fvdl Exp $ */
5 * SPDX-License-Identifier: BSD-3-Clause
7 * Copyright (c) 2009, Sun Microsystems, Inc.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions are met:
12 * - Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
14 * - Redistributions in binary form must reproduce the above copyright notice,
15 * this list of conditions and the following disclaimer in the documentation
16 * and/or other materials provided with the distribution.
17 * - Neither the name of Sun Microsystems, Inc. nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
25 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
34 * Copyright (c) 1986 - 1991 by Sun Microsystems, Inc.
37 /* #ident "@(#)rpcb_svc_com.c 1.18 94/05/02 SMI" */
41 * The commom server procedure for the rpcbind.
44 #include <sys/types.h>
46 #include <sys/param.h>
48 #include <sys/socket.h>
50 #include <rpc/rpcb_prot.h>
51 #include <rpc/svc_dg.h>
53 #include <netconfig.h>
61 #include <netinet/in.h>
62 #include <rpc/rpc_com.h>
63 #include <rpc/pmap_prot.h>
68 #define RPC_BUF_MAX 65536 /* can be raised if required */
70 static char *nullstring = "";
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 int forward_register(u_int32_t, struct netbuf *, int, char *,
105 rpcproc_t, rpcvers_t, u_int32_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 __unused, 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 = malloc(sizeof (RPCBLIST));
179 a = &(rbl->rpcb_map);
180 a->r_prog = reg.r_prog;
181 a->r_vers = reg.r_vers;
182 a->r_netid = strdup(reg.r_netid);
183 a->r_addr = strdup(reg.r_addr);
184 a->r_owner = strdup(owner);
185 if (!a->r_addr || !a->r_netid || !a->r_owner) {
195 rbl->rpcb_next = (rpcblist_ptr)NULL;
196 if (list_rbl == NULL) {
199 for (fnd = list_rbl; fnd->rpcb_next;
200 fnd = fnd->rpcb_next)
202 fnd->rpcb_next = rbl;
205 (void) add_pmaplist(regp);
211 * Unset a mapping of program, version, netid
215 rpcbproc_unset_com(void *arg, struct svc_req *rqstp __unused, SVCXPRT *transp,
216 rpcvers_t rpcbversnum)
218 RPCB *regp = (RPCB *)arg;
224 fprintf(stderr, "RPCB_UNSET request for (%lu, %lu, %s) : ",
225 (unsigned long)regp->r_prog, (unsigned long)regp->r_vers,
228 ans = map_unset(regp, getowner(transp, owner, sizeof owner));
231 fprintf(stderr, "%s\n", ans == TRUE ? "succeeded" : "failed");
233 /* XXX: should have used some defined constant here */
234 rpcbs_unset(rpcbversnum - 2, ans);
239 map_unset(RPCB *regp, char *owner)
242 rpcblist_ptr rbl, prev, tmp;
247 for (prev = NULL, rbl = list_rbl; rbl; /* cstyle */) {
248 if ((rbl->rpcb_map.r_prog != regp->r_prog) ||
249 (rbl->rpcb_map.r_vers != regp->r_vers) ||
250 (regp->r_netid[0] && strcasecmp(regp->r_netid,
251 rbl->rpcb_map.r_netid))) {
252 /* both rbl & prev move forwards */
254 rbl = rbl->rpcb_next;
258 * Check whether appropriate uid. Unset only
259 * if superuser or the owner itself.
261 if (strcmp(owner, "superuser") &&
262 strcmp(rbl->rpcb_map.r_owner, owner))
264 /* found it; rbl moves forward, prev stays */
267 rbl = rbl->rpcb_next;
271 prev->rpcb_next = rbl;
272 free(tmp->rpcb_map.r_addr);
273 free(tmp->rpcb_map.r_netid);
274 free(tmp->rpcb_map.r_owner);
279 (void) del_pmaplist(regp);
282 * We return 1 either when the entry was not there or it
283 * was able to unset it. It can come to this point only if
284 * atleast one of the conditions is true.
290 delete_prog(unsigned int prog)
293 register rpcblist_ptr rbl;
295 for (rbl = list_rbl; rbl != NULL; rbl = rbl->rpcb_next) {
296 if ((rbl->rpcb_map.r_prog != prog))
298 if (is_bound(rbl->rpcb_map.r_netid, rbl->rpcb_map.r_addr))
300 reg.r_prog = rbl->rpcb_map.r_prog;
301 reg.r_vers = rbl->rpcb_map.r_vers;
302 reg.r_netid = strdup(rbl->rpcb_map.r_netid);
303 (void) map_unset(®, "superuser");
309 rpcbproc_getaddr_com(RPCB *regp, struct svc_req *rqstp __unused,
310 SVCXPRT *transp, rpcvers_t rpcbversnum, rpcvers_t verstype)
316 if (uaddr != NULL && uaddr != nullstring) {
320 fnd = find_service(regp->r_prog, regp->r_vers, transp->xp_netid);
321 if (fnd && ((verstype == RPCB_ALLVERS) ||
322 (regp->r_vers == fnd->rpcb_map.r_vers))) {
323 if (*(regp->r_addr) != '\0') { /* may contain a hint about */
324 saddr = regp->r_addr; /* the interface that we */
326 if (!(uaddr = mergeaddr(transp, transp->xp_netid,
327 fnd->rpcb_map.r_addr, saddr))) {
328 /* Try whatever we have */
329 uaddr = strdup(fnd->rpcb_map.r_addr);
330 } else if (!uaddr[0]) {
332 * The server died. Unset all versions of this prog.
334 delete_prog(regp->r_prog);
342 fprintf(stderr, "getaddr: %s\n", uaddr);
344 /* XXX: should have used some defined constant here */
345 rpcbs_getaddr(rpcbversnum - 2, regp->r_prog, regp->r_vers,
346 transp->xp_netid, uaddr);
347 return (void *)&uaddr;
352 rpcbproc_gettime_com(void *arg __unused, struct svc_req *rqstp __unused,
353 SVCXPRT *transp __unused, rpcvers_t rpcbversnum __unused)
355 static time_t curtime;
357 (void) time(&curtime);
358 return (void *)&curtime;
362 * Convert uaddr to taddr. Should be used only by
363 * local servers/clients. (kernel level stuff only)
367 rpcbproc_uaddr2taddr_com(void *arg, struct svc_req *rqstp __unused,
368 SVCXPRT *transp, rpcvers_t rpcbversnum __unused)
370 char **uaddrp = (char **)arg;
371 struct netconfig *nconf;
372 static struct netbuf nbuf;
373 static struct netbuf *taddr;
380 if (((nconf = rpcbind_get_conf(transp->xp_netid)) == NULL) ||
381 ((taddr = uaddr2taddr(nconf, *uaddrp)) == NULL)) {
382 (void) memset((char *)&nbuf, 0, sizeof (struct netbuf));
383 return (void *)&nbuf;
385 return (void *)taddr;
389 * Convert taddr to uaddr. Should be used only by
390 * local servers/clients. (kernel level stuff only)
394 rpcbproc_taddr2uaddr_com(void *arg, struct svc_req *rqstp __unused,
395 SVCXPRT *transp, rpcvers_t rpcbversnum __unused)
397 struct netbuf *taddr = (struct netbuf *)arg;
399 struct netconfig *nconf;
404 if ((fd = open("/dev/null", O_RDONLY)) == -1) {
405 uaddr = (char *)strerror(errno);
408 #endif /* CHEW_FDS */
409 if (uaddr != NULL && uaddr != nullstring) {
413 if (((nconf = rpcbind_get_conf(transp->xp_netid)) == NULL) ||
414 ((uaddr = taddr2uaddr(nconf, taddr)) == NULL)) {
417 return (void *)&uaddr;
422 xdr_encap_parms(XDR *xdrs, struct encap_parms *epp)
424 return (xdr_bytes(xdrs, &(epp->args), (u_int *) &(epp->arglen),
429 * XDR remote call arguments. It ignores the address part.
430 * written for XDR_DECODE direction only
433 xdr_rmtcall_args(XDR *xdrs, struct r_rmtcall_args *cap)
435 /* does not get the address or the arguments */
436 if (xdr_rpcprog(xdrs, &(cap->rmt_prog)) &&
437 xdr_rpcvers(xdrs, &(cap->rmt_vers)) &&
438 xdr_rpcproc(xdrs, &(cap->rmt_proc))) {
439 return (xdr_encap_parms(xdrs, &(cap->rmt_args)));
445 * XDR remote call results along with the address. Ignore
446 * program number, version number and proc number.
447 * Written for XDR_ENCODE direction only.
450 xdr_rmtcall_result(XDR *xdrs, struct r_rmtcall_args *cap)
455 if (cap->rmt_localvers == PMAPVERS) {
456 int h1, h2, h3, h4, p1, p2;
459 /* interpret the universal address for TCP/IP */
460 if (sscanf(cap->rmt_uaddr, "%d.%d.%d.%d.%d.%d",
461 &h1, &h2, &h3, &h4, &p1, &p2) != 6)
463 port = ((p1 & 0xff) << 8) + (p2 & 0xff);
464 result = xdr_u_long(xdrs, &port);
467 if ((cap->rmt_localvers == RPCBVERS) ||
468 (cap->rmt_localvers == RPCBVERS4)) {
469 result = xdr_wrapstring(xdrs, &(cap->rmt_uaddr));
474 return (xdr_encap_parms(xdrs, &(cap->rmt_args)));
479 * only worries about the struct encap_parms part of struct r_rmtcall_args.
480 * The arglen must already be set!!
483 xdr_opaque_parms(XDR *xdrs, struct r_rmtcall_args *cap)
485 return (xdr_opaque(xdrs, cap->rmt_args.args, cap->rmt_args.arglen));
488 static struct rmtcallfd_list *rmthead;
489 static struct rmtcallfd_list *rmttail;
492 create_rmtcall_fd(struct netconfig *nconf)
495 struct rmtcallfd_list *rmt;
498 if ((fd = __rpc_nconf2fd(nconf)) == -1) {
501 "create_rmtcall_fd: couldn't open \"%s\" (errno %d)\n",
502 nconf->nc_device, errno);
505 xprt = svc_tli_create(fd, 0, (struct t_bind *) 0, 0, 0);
509 "create_rmtcall_fd: svc_tli_create failed\n");
512 rmt = malloc(sizeof (struct rmtcallfd_list));
514 syslog(LOG_ERR, "create_rmtcall_fd: no memory!");
518 rmt->netid = strdup(nconf->nc_netid);
519 xprt->xp_netid = rmt->netid;
522 if (rmthead == NULL) {
529 /* XXX not threadsafe */
532 FD_SET(fd, &svc_fdset);
537 find_rmtcallfd_by_netid(char *netid)
539 struct rmtcallfd_list *rmt;
541 for (rmt = rmthead; rmt != NULL; rmt = rmt->next) {
542 if (strcmp(netid, rmt->netid) == 0) {
550 find_rmtcallxprt_by_fd(int fd)
552 struct rmtcallfd_list *rmt;
554 for (rmt = rmthead; rmt != NULL; rmt = rmt->next) {
564 * Call a remote procedure service. This procedure is very quiet when things
565 * go wrong. The proc is written to support broadcast rpc. In the broadcast
566 * case, a machine should shut-up instead of complain, lest the requestor be
567 * overrun with complaints at the expense of not hearing a valid reply.
568 * When receiving a request and verifying that the service exists, we
570 * receive the request
572 * open a new TLI endpoint on the same transport on which we received
573 * the original request
575 * remember the original request's XID (which requires knowing the format
576 * of the svc_dg_data structure)
578 * forward the request, with a new XID, to the requested service,
579 * remembering the XID used to send this request (for later use in
580 * reassociating the answer with the original request), the requestor's
581 * address, the file descriptor on which the forwarded request is
582 * made and the service's address.
584 * mark the file descriptor on which we anticipate receiving a reply from
585 * the service and one to select for in our private svc_run procedure
587 * At some time in the future, a reply will be received from the service to
588 * which we forwarded the request. At that time, we detect that the socket
589 * used was for forwarding (by looking through the finfo structures to see
590 * whether the fd corresponds to one of those) and call handle_reply() to
594 * bundle the reply, along with the service's universal address
596 * create a SVCXPRT structure and use a version of svc_sendreply
597 * that allows us to specify the reply XID and destination, send the reply
598 * to the original requestor.
602 rpcbproc_callit_com(struct svc_req *rqstp, SVCXPRT *transp,
603 rpcproc_t reply_type, rpcvers_t versnum)
605 register rpcblist_ptr rbl;
606 struct netconfig *nconf;
607 struct netbuf *caller;
608 struct r_rmtcall_args a;
609 char *buf_alloc = NULL, *outbufp;
610 char *outbuf_alloc = NULL;
611 char buf[RPC_BUF_MAX], outbuf[RPC_BUF_MAX];
612 struct netbuf *na = (struct netbuf *) NULL;
613 struct rpc_msg call_msg;
619 char *uaddr, *m_uaddr = NULL, *local_uaddr = NULL;
621 struct __rpc_sockinfo si;
622 struct sockaddr *localsa;
625 if (!__rpc_fd2sockinfo(transp->xp_fd, &si)) {
626 if (reply_type == RPCBPROC_INDIRECT)
627 svcerr_systemerr(transp);
630 if (si.si_socktype != SOCK_DGRAM)
631 return; /* Only datagram type accepted */
632 sendsz = __rpc_get_t_size(si.si_af, si.si_proto, UDPMSGSIZE);
633 if (sendsz == 0) { /* data transfer not supported */
634 if (reply_type == RPCBPROC_INDIRECT)
635 svcerr_systemerr(transp);
639 * Should be multiple of 4 for XDR.
641 sendsz = roundup(sendsz, 4);
642 if (sendsz > RPC_BUF_MAX) {
644 buf_alloc = alloca(sendsz); /* not in IDR2? */
646 buf_alloc = malloc(sendsz);
648 if (buf_alloc == NULL) {
651 "rpcbproc_callit_com: No Memory!\n");
652 if (reply_type == RPCBPROC_INDIRECT)
653 svcerr_systemerr(transp);
656 a.rmt_args.args = buf_alloc;
658 a.rmt_args.args = buf;
661 call_msg.rm_xid = 0; /* For error checking purposes */
662 if (!svc_getargs(transp, (xdrproc_t) xdr_rmtcall_args, (char *) &a)) {
663 if (reply_type == RPCBPROC_INDIRECT)
664 svcerr_decode(transp);
667 "rpcbproc_callit_com: svc_getargs failed\n");
671 if (!check_callit(transp, &a, versnum)) {
672 svcerr_weakauth(transp);
676 caller = svc_getrpccaller(transp);
679 uaddr = taddr2uaddr(rpcbind_get_conf(transp->xp_netid), caller);
680 fprintf(stderr, "%s %s req for (%lu, %lu, %lu, %s) from %s : ",
681 versnum == PMAPVERS ? "pmap_rmtcall" :
682 versnum == RPCBVERS ? "rpcb_rmtcall" :
683 versnum == RPCBVERS4 ? "rpcb_indirect" : "unknown",
684 reply_type == RPCBPROC_INDIRECT ? "indirect" : "callit",
685 (unsigned long)a.rmt_prog, (unsigned long)a.rmt_vers,
686 (unsigned long)a.rmt_proc, transp->xp_netid,
687 uaddr ? uaddr : "unknown");
693 rbl = find_service(a.rmt_prog, a.rmt_vers, transp->xp_netid);
695 rpcbs_rmtcall(versnum - 2, reply_type, a.rmt_prog, a.rmt_vers,
696 a.rmt_proc, transp->xp_netid, rbl);
698 if (rbl == (rpcblist_ptr)NULL) {
701 fprintf(stderr, "not found\n");
703 if (reply_type == RPCBPROC_INDIRECT)
704 svcerr_noprog(transp);
707 if (rbl->rpcb_map.r_vers != a.rmt_vers) {
708 if (reply_type == RPCBPROC_INDIRECT) {
709 rpcvers_t vers_low, vers_high;
711 find_versions(a.rmt_prog, transp->xp_netid,
712 &vers_low, &vers_high);
713 svcerr_progvers(transp, vers_low, vers_high);
720 fprintf(stderr, "found at uaddr %s\n", rbl->rpcb_map.r_addr);
723 * Check whether this entry is valid and a server is present
724 * Mergeaddr() returns NULL if no such entry is present, and
725 * returns "" if the entry was present but the server is not
726 * present (i.e., it crashed).
728 if (reply_type == RPCBPROC_INDIRECT) {
729 uaddr = mergeaddr(transp, transp->xp_netid,
730 rbl->rpcb_map.r_addr, NULL);
731 if (uaddr == NULL || uaddr[0] == '\0') {
732 svcerr_noprog(transp);
739 nconf = rpcbind_get_conf(transp->xp_netid);
740 if (nconf == (struct netconfig *)NULL) {
741 if (reply_type == RPCBPROC_INDIRECT)
742 svcerr_systemerr(transp);
745 "rpcbproc_callit_com: rpcbind_get_conf failed\n");
748 localsa = local_sa(((struct sockaddr *)caller->buf)->sa_family);
749 if (localsa == NULL) {
752 "rpcbproc_callit_com: no local address\n");
755 tbuf.len = tbuf.maxlen = localsa->sa_len;
758 addrmerge(&tbuf, rbl->rpcb_map.r_addr, NULL, nconf->nc_netid);
759 m_uaddr = addrmerge(caller, rbl->rpcb_map.r_addr, NULL,
763 fprintf(stderr, "merged uaddr %s\n", m_uaddr);
765 if ((fd = find_rmtcallfd_by_netid(nconf->nc_netid)) == -1) {
766 if (reply_type == RPCBPROC_INDIRECT)
767 svcerr_systemerr(transp);
770 xidp = __rpcb_get_dg_xidp(transp);
771 switch (forward_register(*xidp, caller, fd, m_uaddr, reply_type,
772 versnum, &call_msg.rm_xid)) {
774 /* Success; forward_register() will free m_uaddr for us. */
779 * A duplicate request for the slow server. Let's not
780 * beat on it any more.
784 "rpcbproc_callit_com: duplicate request\n");
787 /* forward_register failed. Perhaps no memory. */
790 "rpcbproc_callit_com: forward_register failed\n");
797 "rpcbproc_callit_com: original XID %x, new XID %x\n",
798 *xidp, call_msg.rm_xid);
800 call_msg.rm_direction = CALL;
801 call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
802 call_msg.rm_call.cb_prog = a.rmt_prog;
803 call_msg.rm_call.cb_vers = a.rmt_vers;
804 if (sendsz > RPC_BUF_MAX) {
806 outbuf_alloc = alloca(sendsz); /* not in IDR2? */
808 outbuf_alloc = malloc(sendsz);
810 if (outbuf_alloc == NULL) {
811 if (reply_type == RPCBPROC_INDIRECT)
812 svcerr_systemerr(transp);
815 "rpcbproc_callit_com: No memory!\n");
818 xdrmem_create(&outxdr, outbuf_alloc, sendsz, XDR_ENCODE);
820 xdrmem_create(&outxdr, outbuf, sendsz, XDR_ENCODE);
822 if (!xdr_callhdr(&outxdr, &call_msg)) {
823 if (reply_type == RPCBPROC_INDIRECT)
824 svcerr_systemerr(transp);
827 "rpcbproc_callit_com: xdr_callhdr failed\n");
830 if (!xdr_u_int32_t(&outxdr, &(a.rmt_proc))) {
831 if (reply_type == RPCBPROC_INDIRECT)
832 svcerr_systemerr(transp);
835 "rpcbproc_callit_com: xdr_u_long failed\n");
839 if (rqstp->rq_cred.oa_flavor == AUTH_NULL) {
840 auth = authnone_create();
841 } else if (rqstp->rq_cred.oa_flavor == AUTH_SYS) {
842 struct authunix_parms *au;
844 au = (struct authunix_parms *)rqstp->rq_clntcred;
845 auth = authunix_create(au->aup_machname,
846 au->aup_uid, au->aup_gid,
847 au->aup_len, au->aup_gids);
848 if (auth == NULL) /* fall back */
849 auth = authnone_create();
851 /* we do not support any other authentication scheme */
854 "rpcbproc_callit_com: oa_flavor != AUTH_NONE and oa_flavor != AUTH_SYS\n");
855 if (reply_type == RPCBPROC_INDIRECT)
856 svcerr_weakauth(transp); /* XXX too strong.. */
860 if (reply_type == RPCBPROC_INDIRECT)
861 svcerr_systemerr(transp);
864 "rpcbproc_callit_com: authwhatever_create returned NULL\n");
867 if (!AUTH_MARSHALL(auth, &outxdr)) {
868 if (reply_type == RPCBPROC_INDIRECT)
869 svcerr_systemerr(transp);
873 "rpcbproc_callit_com: AUTH_MARSHALL failed\n");
877 if (!xdr_opaque_parms(&outxdr, &a)) {
878 if (reply_type == RPCBPROC_INDIRECT)
879 svcerr_systemerr(transp);
882 "rpcbproc_callit_com: xdr_opaque_parms failed\n");
885 outlen = (int) XDR_GETPOS(&outxdr);
887 outbufp = outbuf_alloc;
891 na = uaddr2taddr(nconf, local_uaddr);
893 if (reply_type == RPCBPROC_INDIRECT)
894 svcerr_systemerr(transp);
898 if (sendto(fd, outbufp, outlen, 0, (struct sockaddr *)na->buf, na->len)
902 "rpcbproc_callit_com: sendto failed: errno %d\n", errno);
903 if (reply_type == RPCBPROC_INDIRECT)
904 svcerr_systemerr(transp);
910 if (call_msg.rm_xid != 0)
911 (void) free_slot_by_xid(call_msg.rm_xid);
928 * Makes an entry into the FIFO for the given request.
929 * Returns 1 on success, 0 if this is a duplicate request, or -1 on error.
930 * *callxidp is set to the xid of the call.
933 forward_register(u_int32_t caller_xid, struct netbuf *caller_addr,
934 int forward_fd, char *uaddr, rpcproc_t reply_type,
935 rpcvers_t versnum, u_int32_t *callxidp)
939 time_t min_time, time_now;
940 static u_int32_t lastxid;
943 min_time = FINFO[0].time;
944 time_now = time((time_t *)0);
947 lastxid = time_now * NFORWARD;
950 * Check if it is a duplicate entry. Then,
951 * try to find an empty slot. If not available, then
952 * use the slot with the earliest time.
954 for (i = 0; i < NFORWARD; i++) {
955 if (FINFO[i].flag & FINFO_ACTIVE) {
956 if ((FINFO[i].caller_xid == caller_xid) &&
957 (FINFO[i].reply_type == reply_type) &&
958 (FINFO[i].versnum == versnum) &&
959 (!netbufcmp(FINFO[i].caller_addr,
961 FINFO[i].time = time((time_t *)0);
962 return (0); /* Duplicate entry */
964 /* Should we wait any longer */
965 if ((time_now - FINFO[i].time) > MAXTIME_OFF)
966 (void) free_slot_by_index(i);
970 if ((FINFO[i].flag & FINFO_ACTIVE) == 0) {
972 } else if (FINFO[i].time < min_time) {
974 min_time = FINFO[i].time;
979 /* use this empty slot */
982 (void) free_slot_by_index(j);
984 if ((FINFO[j].caller_addr = netbufdup(caller_addr)) == NULL) {
987 rpcb_rmtcalls++; /* no of pending calls */
988 FINFO[j].flag = FINFO_ACTIVE;
989 FINFO[j].reply_type = reply_type;
990 FINFO[j].versnum = versnum;
991 FINFO[j].time = time_now;
992 FINFO[j].caller_xid = caller_xid;
993 FINFO[j].forward_fd = forward_fd;
995 * Though uaddr is not allocated here, it will still be freed
996 * from free_slot_*().
998 FINFO[j].uaddr = uaddr;
999 lastxid = lastxid + NFORWARD;
1000 /* Don't allow a zero xid below. */
1001 if ((u_int32_t)(lastxid + NFORWARD) <= NFORWARD)
1003 FINFO[j].forward_xid = lastxid + j; /* encode slot */
1004 *callxidp = FINFO[j].forward_xid; /* forward on this xid */
1008 static struct finfo *
1009 forward_find(u_int32_t reply_xid)
1013 i = reply_xid % (u_int32_t)NFORWARD;
1014 if ((FINFO[i].flag & FINFO_ACTIVE) &&
1015 (FINFO[i].forward_xid == reply_xid)) {
1022 free_slot_by_xid(u_int32_t xid)
1026 entry = xid % (u_int32_t)NFORWARD;
1027 return (free_slot_by_index(entry));
1031 free_slot_by_index(int index)
1036 if (fi->flag & FINFO_ACTIVE) {
1037 netbuffree(fi->caller_addr);
1038 /* XXX may be too big, but can't access xprt array here */
1039 if (fi->forward_fd >= svc_maxfd)
1042 fi->flag &= ~FINFO_ACTIVE;
1050 netbufcmp(struct netbuf *n1, struct netbuf *n2)
1052 return ((n1->len != n2->len) || memcmp(n1->buf, n2->buf, n1->len));
1056 netbuf_copybuf(struct netbuf *dst, const struct netbuf *src)
1058 assert(src->len <= src->maxlen);
1060 if (dst->maxlen < src->len || dst->buf == NULL) {
1061 if (dst->buf != NULL)
1063 if ((dst->buf = calloc(1, src->maxlen)) == NULL)
1065 dst->maxlen = src->maxlen;
1068 dst->len = src->len;
1069 memcpy(dst->buf, src->buf, src->len);
1074 static struct netbuf *
1075 netbufdup(struct netbuf *ap)
1079 if ((np = calloc(1, sizeof(struct netbuf))) == NULL)
1081 if (netbuf_copybuf(np, ap) == FALSE) {
1089 netbuffree(struct netbuf *ap)
1097 #define MASKVAL (POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND)
1098 extern bool_t __svc_clean_idle(fd_set *, int, bool_t);
1104 struct pollfd pollfds[FD_SETSIZE + 1];
1105 int poll_ret, check_ret;
1107 #ifdef SVC_RUN_DEBUG
1110 register struct pollfd *p;
1115 p->fd = terminate_rfd;
1116 p->events = MASKVAL;
1118 for (n = 0; n <= svc_maxfd; n++) {
1119 if (FD_ISSET(n, &svc_fdset)) {
1121 p->events = MASKVAL;
1127 #ifdef SVC_RUN_DEBUG
1129 fprintf(stderr, "polling for read on fd < ");
1130 for (i = 0, p = pollfds; i < nfds; i++, p++)
1132 fprintf(stderr, "%d ", p->fd);
1133 fprintf(stderr, ">\n");
1136 poll_ret = poll(pollfds, nfds, 30 * 1000);
1138 if (doterminate != 0) {
1139 close(rpcbindlockfd);
1142 "rpcbind terminating on signal %d. Restart with \"rpcbind -w\"",
1144 write_warmstart(); /* Dump yourself */
1152 * We ignore all errors, continuing with the assumption
1153 * that it was set by the signal handlers (or any
1154 * other outside event) and not caused by poll().
1157 cleanfds = svc_fdset;
1158 __svc_clean_idle(&cleanfds, 30, FALSE);
1161 #ifdef SVC_RUN_DEBUG
1163 fprintf(stderr, "poll returned read fds < ");
1164 for (i = 0, p = pollfds; i < nfds; i++, p++)
1166 fprintf(stderr, "%d ", p->fd);
1167 fprintf(stderr, ">\n");
1171 * If we found as many replies on callback fds
1172 * as the number of descriptors selectable which
1173 * poll() returned, there can be no more so we
1174 * don't call svc_getreq_poll. Otherwise, there
1175 * must be another so we must call svc_getreq_poll.
1177 if ((check_ret = check_rmtcalls(pollfds, nfds)) ==
1180 svc_getreq_poll(pollfds, poll_ret-check_ret);
1182 #ifdef SVC_RUN_DEBUG
1184 fprintf(stderr, "svc_maxfd now %u\n", svc_maxfd);
1191 check_rmtcalls(struct pollfd *pfds, int nfds)
1193 int j, ncallbacks_found = 0, rmtcalls_pending;
1196 if (rpcb_rmtcalls == 0)
1199 rmtcalls_pending = rpcb_rmtcalls;
1200 for (j = 0; j < nfds; j++) {
1201 if ((xprt = find_rmtcallxprt_by_fd(pfds[j].fd)) != NULL) {
1202 if (pfds[j].revents) {
1204 #ifdef DEBUG_RMTCALL
1207 "my_svc_run: polled on forwarding fd %d, netid %s - calling handle_reply\n",
1208 pfds[j].fd, xprt->xp_netid);
1210 handle_reply(pfds[j].fd, xprt);
1211 pfds[j].revents = 0;
1212 if (ncallbacks_found >= rmtcalls_pending) {
1218 return (ncallbacks_found);
1222 xprt_set_caller(SVCXPRT *xprt, struct finfo *fi)
1226 netbuf_copybuf(svc_getrpccaller(xprt), fi->caller_addr);
1227 xidp = __rpcb_get_dg_xidp(xprt);
1228 *xidp = fi->caller_xid;
1232 * Call svcerr_systemerr() only if RPCBVERS4
1235 send_svcsyserr(SVCXPRT *xprt, struct finfo *fi)
1237 if (fi->reply_type == RPCBPROC_INDIRECT) {
1238 xprt_set_caller(xprt, fi);
1239 svcerr_systemerr(xprt);
1245 handle_reply(int fd, SVCXPRT *xprt)
1248 struct rpc_msg reply_msg;
1249 struct rpc_err reply_error;
1252 int inlen, pos, len;
1253 struct r_rmtcall_args a;
1254 struct sockaddr_storage ss;
1256 #ifdef SVC_RUN_DEBUG
1260 buffer = malloc(RPC_BUF_MAX);
1265 fromlen = sizeof(ss);
1266 inlen = recvfrom(fd, buffer, RPC_BUF_MAX, 0,
1267 (struct sockaddr *)&ss, &fromlen);
1268 } while (inlen < 0 && errno == EINTR);
1272 "handle_reply: recvfrom returned %d, errno %d\n", inlen, errno);
1276 reply_msg.acpted_rply.ar_verf = _null_auth;
1277 reply_msg.acpted_rply.ar_results.where = 0;
1278 reply_msg.acpted_rply.ar_results.proc = (xdrproc_t) xdr_void;
1280 xdrmem_create(&reply_xdrs, buffer, (u_int)inlen, XDR_DECODE);
1281 if (!xdr_replymsg(&reply_xdrs, &reply_msg)) {
1283 (void) fprintf(stderr,
1284 "handle_reply: xdr_replymsg failed\n");
1287 fi = forward_find(reply_msg.rm_xid);
1288 #ifdef SVC_RUN_DEBUG
1290 fprintf(stderr, "handle_reply: reply xid: %d fi addr: %p\n",
1291 reply_msg.rm_xid, fi);
1297 _seterr_reply(&reply_msg, &reply_error);
1298 if (reply_error.re_status != RPC_SUCCESS) {
1300 (void) fprintf(stderr, "handle_reply: %s\n",
1301 clnt_sperrno(reply_error.re_status));
1302 send_svcsyserr(xprt, fi);
1305 pos = XDR_GETPOS(&reply_xdrs);
1307 a.rmt_args.args = &buffer[pos];
1308 a.rmt_args.arglen = len;
1309 a.rmt_uaddr = fi->uaddr;
1310 a.rmt_localvers = fi->versnum;
1312 xprt_set_caller(xprt, fi);
1313 #ifdef SVC_RUN_DEBUG
1314 uaddr = taddr2uaddr(rpcbind_get_conf("udp"),
1315 svc_getrpccaller(xprt));
1317 fprintf(stderr, "handle_reply: forwarding address %s to %s\n",
1318 a.rmt_uaddr, uaddr ? uaddr : "unknown");
1323 svc_sendreply(xprt, (xdrproc_t) xdr_rmtcall_result, (char *) &a);
1328 if (reply_msg.rm_xid == 0) {
1329 #ifdef SVC_RUN_DEBUG
1331 fprintf(stderr, "handle_reply: NULL xid on exit!\n");
1335 (void) free_slot_by_xid(reply_msg.rm_xid);
1340 find_versions(rpcprog_t prog, char *netid, rpcvers_t *lowvp, rpcvers_t *highvp)
1342 register rpcblist_ptr rbl;
1343 unsigned int lowv = 0;
1344 unsigned int highv = 0;
1346 for (rbl = list_rbl; rbl != NULL; rbl = rbl->rpcb_next) {
1347 if ((rbl->rpcb_map.r_prog != prog) ||
1348 ((rbl->rpcb_map.r_netid != NULL) &&
1349 (strcasecmp(rbl->rpcb_map.r_netid, netid) != 0)))
1352 highv = rbl->rpcb_map.r_vers;
1354 } else if (rbl->rpcb_map.r_vers < lowv) {
1355 lowv = rbl->rpcb_map.r_vers;
1356 } else if (rbl->rpcb_map.r_vers > highv) {
1357 highv = rbl->rpcb_map.r_vers;
1366 * returns the item with the given program, version number and netid.
1367 * If that version number is not found, it returns the item with that
1368 * program number, so that address is now returned to the caller. The
1369 * caller when makes a call to this program, version number, the call
1370 * will fail and it will return with PROGVERS_MISMATCH. The user can
1371 * then determine the highest and the lowest version number for this
1372 * program using clnt_geterr() and use those program version numbers.
1374 * Returns the RPCBLIST for the given prog, vers and netid
1377 find_service(rpcprog_t prog, rpcvers_t vers, char *netid)
1379 register rpcblist_ptr hit = NULL;
1380 register rpcblist_ptr rbl;
1382 for (rbl = list_rbl; rbl != NULL; rbl = rbl->rpcb_next) {
1383 if ((rbl->rpcb_map.r_prog != prog) ||
1384 ((rbl->rpcb_map.r_netid != NULL) &&
1385 (strcasecmp(rbl->rpcb_map.r_netid, netid) != 0)))
1388 if (rbl->rpcb_map.r_vers == vers)
1395 * Copies the name associated with the uid of the caller and returns
1396 * a pointer to it. Similar to getwd().
1399 getowner(SVCXPRT *transp, char *owner, size_t ownersize)
1403 if (__rpc_get_local_uid(transp, &uid) < 0)
1404 strlcpy(owner, "unknown", ownersize);
1406 strlcpy(owner, "superuser", ownersize);
1408 snprintf(owner, ownersize, "%d", uid);
1415 * Add this to the pmap list only if it is UDP or TCP.
1418 add_pmaplist(RPCB *arg)
1421 struct pmaplist *pml;
1422 int h1, h2, h3, h4, p1, p2;
1424 if (strcmp(arg->r_netid, udptrans) == 0) {
1426 pmap.pm_prot = IPPROTO_UDP;
1427 } else if (strcmp(arg->r_netid, tcptrans) == 0) {
1429 pmap.pm_prot = IPPROTO_TCP;
1431 /* Not an IP protocol */
1434 /* interpret the universal address for TCP/IP */
1435 if (sscanf(arg->r_addr, "%d.%d.%d.%d.%d.%d",
1436 &h1, &h2, &h3, &h4, &p1, &p2) != 6)
1438 pmap.pm_port = ((p1 & 0xff) << 8) + (p2 & 0xff);
1439 pmap.pm_prog = arg->r_prog;
1440 pmap.pm_vers = arg->r_vers;
1442 * add to END of list
1444 pml = malloc(sizeof (struct pmaplist));
1446 (void) syslog(LOG_ERR, "rpcbind: no memory!\n");
1449 pml->pml_map = pmap;
1450 pml->pml_next = NULL;
1451 if (list_pml == NULL) {
1454 struct pmaplist *fnd;
1456 /* Attach to the end of the list */
1457 for (fnd = list_pml; fnd->pml_next; fnd = fnd->pml_next)
1459 fnd->pml_next = pml;
1465 * Delete this from the pmap list only if it is UDP or TCP.
1468 del_pmaplist(RPCB *arg)
1470 struct pmaplist *pml;
1471 struct pmaplist *prevpml, *fnd;
1474 if (strcmp(arg->r_netid, udptrans) == 0) {
1477 } else if (strcmp(arg->r_netid, tcptrans) == 0) {
1480 } else if (arg->r_netid[0] == 0) {
1481 prot = 0; /* Remove all occurrences */
1483 /* Not an IP protocol */
1486 for (prevpml = NULL, pml = list_pml; pml; /* cstyle */) {
1487 if ((pml->pml_map.pm_prog != arg->r_prog) ||
1488 (pml->pml_map.pm_vers != arg->r_vers) ||
1489 (prot && (pml->pml_map.pm_prot != prot))) {
1490 /* both pml & prevpml move forwards */
1492 pml = pml->pml_next;
1495 /* found it; pml moves forward, prevpml stays */
1497 pml = pml->pml_next;
1498 if (prevpml == NULL)
1501 prevpml->pml_next = pml;
1506 #endif /* PORTMAP */