rpcb_svc_com.c revision 1.20.4.1 1 /* $NetBSD: rpcb_svc_com.c,v 1.20.4.1 2019/06/10 22:10:36 christos Exp $ */
2 /* $FreeBSD: head/usr.sbin/rpcbind/rpcb_svc_com.c 301770 2016-06-09 22:25:00Z pfg $ */
3
4 /*-
5 * Copyright (c) 2009, Sun Microsystems, Inc.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
10 * - Redistributions of source code must retain the above copyright notice,
11 * this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright notice,
13 * this list of conditions and the following disclaimer in the documentation
14 * and/or other materials provided with the distribution.
15 * - Neither the name of Sun Microsystems, Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived
17 * from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31 /*
32 * Copyright (c) 1986 - 1991 by Sun Microsystems, Inc.
33 */
34
35 /* #ident "@(#)rpcb_svc_com.c 1.18 94/05/02 SMI" */
36
37 /*
38 * rpcb_svc_com.c
39 * The commom server procedure for the rpcbind.
40 */
41
42 #include <sys/types.h>
43 #include <sys/stat.h>
44 #include <sys/param.h>
45 #include <sys/socket.h>
46 #include <rpc/rpc.h>
47 #include <rpc/rpcb_prot.h>
48 #include <rpc/rpc_com.h>
49 #include <assert.h>
50 #include <netconfig.h>
51 #include <errno.h>
52 #include <syslog.h>
53 #include <unistd.h>
54 #include <stdio.h>
55 #include <poll.h>
56 #ifdef PORTMAP
57 #include <netinet/in.h>
58 #include <rpc/pmap_prot.h>
59 #endif /* PORTMAP */
60 #include <string.h>
61 #include <stdlib.h>
62
63 #ifdef RPCBIND_RUMP
64 #include <rump/rump.h>
65 #include <rump/rump_syscalls.h>
66 #endif
67
68 #include "svc_dg.h"
69 #include "rpcbind.h"
70 #ifdef RPCBIND_RUMP
71 #include "svc_fdset.h"
72 #endif
73
74 #define RPC_BUF_MAX 65536 /* can be raised if required */
75
76 static char nullstring[] = "";
77 static int rpcb_rmtcalls;
78
79 struct rmtcallfd_list {
80 int fd;
81 SVCXPRT *xprt;
82 char *netid;
83 struct rmtcallfd_list *next;
84 };
85
86 #define NFORWARD 64
87 #define MAXTIME_OFF 300 /* 5 minutes */
88
89 struct finfo {
90 int flag;
91 #define FINFO_ACTIVE 0x1
92 u_int32_t caller_xid;
93 struct netbuf *caller_addr;
94 u_int32_t forward_xid;
95 int forward_fd;
96 char *uaddr;
97 rpcproc_t reply_type;
98 rpcvers_t versnum;
99 time_t time;
100 };
101 static struct finfo FINFO[NFORWARD];
102
103
104 static bool_t xdr_encap_parms(XDR *, struct encap_parms *);
105 static bool_t xdr_rmtcall_args(XDR *, struct r_rmtcall_args *);
106 static bool_t xdr_rmtcall_result(XDR *, struct r_rmtcall_args *);
107 static bool_t xdr_opaque_parms(XDR *, struct r_rmtcall_args *);
108 static int find_rmtcallfd_by_netid(char *);
109 static SVCXPRT *find_rmtcallxprt_by_fd(int);
110 static int forward_register(u_int32_t, struct netbuf *, int, char *,
111 rpcproc_t, rpcvers_t, u_int32_t *);
112 static struct finfo *forward_find(u_int32_t);
113 static int free_slot_by_xid(u_int32_t);
114 static int free_slot_by_index(int);
115 static int netbufcmp(struct netbuf *, struct netbuf *);
116 static struct netbuf *netbufdup(struct netbuf *);
117 static void netbuffree(struct netbuf *);
118 static int check_rmtcalls(struct pollfd *, int);
119 static void xprt_set_caller(SVCXPRT *, struct finfo *);
120 static void send_svcsyserr(SVCXPRT *, struct finfo *);
121 static void handle_reply(int, SVCXPRT *);
122 static void find_versions(rpcprog_t, char *, rpcvers_t *, rpcvers_t *);
123 static rpcblist_ptr find_service(rpcprog_t, rpcvers_t, char *);
124 static char *getowner(SVCXPRT *, char *, size_t);
125 static int add_pmaplist(RPCB *);
126 static int del_pmaplist(RPCB *);
127
128 /*
129 * Set a mapping of program, version, netid
130 */
131 /* ARGSUSED */
132 void *
133 rpcbproc_set_com(void *arg, struct svc_req *rqstp __unused, SVCXPRT *transp,
134 rpcvers_t rpcbversnum)
135 {
136 RPCB *regp = arg;
137 static bool_t ans;
138 char owner[64];
139
140 #ifdef RPCBIND_DEBUG
141 if (debugging)
142 fprintf(stderr, "RPCB_SET request for (%lu, %lu, %s, %s) : ",
143 (unsigned long)regp->r_prog, (unsigned long)regp->r_vers,
144 regp->r_netid, regp->r_addr);
145 #endif
146 ans = map_set(regp, getowner(transp, owner, sizeof owner));
147 #ifdef RPCBIND_DEBUG
148 if (debugging)
149 fprintf(stderr, "%s\n", ans == TRUE ? "succeeded" : "failed");
150 #endif
151 /* XXX: should have used some defined constant here */
152 rpcbs_set(rpcbversnum - 2, ans);
153 return (void *)&ans;
154 }
155
156 bool_t
157 map_set(RPCB *regp, char *owner)
158 {
159 RPCB reg, *a;
160 rpcblist_ptr rbl, fnd;
161
162 reg = *regp;
163 /*
164 * check to see if already used
165 * find_service returns a hit even if
166 * the versions don't match, so check for it
167 */
168 fnd = find_service(reg.r_prog, reg.r_vers, reg.r_netid);
169 if (fnd && (fnd->rpcb_map.r_vers == reg.r_vers)) {
170 if (!strcmp(fnd->rpcb_map.r_addr, reg.r_addr))
171 /*
172 * if these match then it is already
173 * registered so just say "OK".
174 */
175 return (TRUE);
176 else
177 return (FALSE);
178 }
179 /*
180 * add to the end of the list
181 */
182 rbl = malloc(sizeof(*rbl));
183 if (rbl == NULL)
184 return (FALSE);
185 a = &(rbl->rpcb_map);
186 a->r_prog = reg.r_prog;
187 a->r_vers = reg.r_vers;
188 a->r_netid = strdup(reg.r_netid);
189 a->r_addr = strdup(reg.r_addr);
190 a->r_owner = strdup(owner);
191 if (!a->r_addr || !a->r_netid || !a->r_owner) {
192 if (a->r_netid)
193 free(a->r_netid);
194 if (a->r_addr)
195 free(a->r_addr);
196 if (a->r_owner)
197 free(a->r_owner);
198 free(rbl);
199 return (FALSE);
200 }
201 rbl->rpcb_next = NULL;
202 if (list_rbl == NULL) {
203 list_rbl = rbl;
204 } else {
205 for (fnd = list_rbl; fnd->rpcb_next;
206 fnd = fnd->rpcb_next)
207 ;
208 fnd->rpcb_next = rbl;
209 }
210 #ifdef PORTMAP
211 (void) add_pmaplist(regp);
212 #endif
213 return (TRUE);
214 }
215
216 /*
217 * Unset a mapping of program, version, netid
218 */
219 /* ARGSUSED */
220 void *
221 rpcbproc_unset_com(void *arg, struct svc_req *rqstp __unused, SVCXPRT *transp,
222 rpcvers_t rpcbversnum)
223 {
224 RPCB *regp = arg;
225 static bool_t ans;
226 char owner[64];
227
228 #ifdef RPCBIND_DEBUG
229 if (debugging)
230 fprintf(stderr, "RPCB_UNSET request for (%lu, %lu, %s) : ",
231 (unsigned long)regp->r_prog, (unsigned long)regp->r_vers,
232 regp->r_netid);
233 #endif
234 ans = map_unset(regp, getowner(transp, owner, sizeof owner));
235 #ifdef RPCBIND_DEBUG
236 if (debugging)
237 fprintf(stderr, "%s\n", ans == TRUE ? "succeeded" : "failed");
238 #endif
239 /* XXX: should have used some defined constant here */
240 rpcbs_unset(rpcbversnum - 2, ans);
241 return (void *)&ans;
242 }
243
244 bool_t
245 map_unset(RPCB *regp, const char *owner)
246 {
247 int ans = 0;
248 rpcblist_ptr rbl, prev, tmp;
249
250 if (owner == NULL)
251 return (0);
252
253 for (prev = NULL, rbl = list_rbl; rbl; /* cstyle */) {
254 if ((rbl->rpcb_map.r_prog != regp->r_prog) ||
255 (rbl->rpcb_map.r_vers != regp->r_vers) ||
256 (regp->r_netid[0] && strcasecmp(regp->r_netid,
257 rbl->rpcb_map.r_netid))) {
258 /* both rbl & prev move forwards */
259 prev = rbl;
260 rbl = rbl->rpcb_next;
261 continue;
262 }
263 /*
264 * Check whether appropriate uid. Unset only
265 * if superuser or the owner itself.
266 */
267 if (strcmp(owner, rpcbind_superuser) &&
268 strcmp(rbl->rpcb_map.r_owner, owner))
269 return (0);
270 /* found it; rbl moves forward, prev stays */
271 ans = 1;
272 tmp = rbl;
273 rbl = rbl->rpcb_next;
274 if (prev == NULL)
275 list_rbl = rbl;
276 else
277 prev->rpcb_next = rbl;
278 free(tmp->rpcb_map.r_addr);
279 free(tmp->rpcb_map.r_netid);
280 free(tmp->rpcb_map.r_owner);
281 free(tmp);
282 }
283 #ifdef PORTMAP
284 if (ans)
285 (void) del_pmaplist(regp);
286 #endif
287 /*
288 * We return 1 either when the entry was not there or it
289 * was able to unset it. It can come to this point only if
290 * atleast one of the conditions is true.
291 */
292 return (1);
293 }
294
295 void
296 delete_prog(rpcprog_t prog)
297 {
298 RPCB reg;
299 rpcblist_ptr rbl;
300
301 for (rbl = list_rbl; rbl != NULL; rbl = rbl->rpcb_next) {
302 if ((rbl->rpcb_map.r_prog != prog))
303 continue;
304 if (is_bound(rbl->rpcb_map.r_netid, rbl->rpcb_map.r_addr))
305 continue;
306 reg.r_prog = rbl->rpcb_map.r_prog;
307 reg.r_vers = rbl->rpcb_map.r_vers;
308 reg.r_netid = strdup(rbl->rpcb_map.r_netid);
309 if (reg.r_netid == NULL)
310 syslog(LOG_ERR, "%s: Cannot allocate memory", __func__);
311 else {
312 (void)map_unset(®, rpcbind_superuser);
313 free(reg.r_netid);
314 }
315 }
316 }
317
318 void *
319 rpcbproc_getaddr_com(RPCB *regp, struct svc_req *rqstp __unused,
320 SVCXPRT *transp, rpcvers_t rpcbversnum, rpcvers_t verstype)
321 {
322 static char *uaddr;
323 char *saddr = NULL;
324 rpcblist_ptr fnd;
325
326 if (uaddr != NULL && uaddr != nullstring) {
327 free(uaddr);
328 uaddr = NULL;
329 }
330 fnd = find_service(regp->r_prog, regp->r_vers, transp->xp_netid);
331 if (fnd && ((verstype == RPCB_ALLVERS) ||
332 (regp->r_vers == fnd->rpcb_map.r_vers))) {
333 if (*(regp->r_addr) != '\0') { /* may contain a hint about */
334 saddr = regp->r_addr; /* the interface that we */
335 } /* should use */
336 if (!(uaddr = mergeaddr(transp, transp->xp_netid,
337 fnd->rpcb_map.r_addr, saddr))) {
338 /* Try whatever we have */
339 uaddr = strdup(fnd->rpcb_map.r_addr);
340 } else if (!uaddr[0]) {
341 /*
342 * The server died. Unset all versions of this prog.
343 */
344 delete_prog(regp->r_prog);
345 uaddr = nullstring;
346 }
347 } else {
348 uaddr = nullstring;
349 }
350 #ifdef RPCBIND_DEBUG
351 if (debugging)
352 fprintf(stderr, "getaddr: %s\n", uaddr);
353 #endif
354 /* XXX: should have used some defined constant here */
355 rpcbs_getaddr(rpcbversnum - 2, regp->r_prog, regp->r_vers,
356 transp->xp_netid, uaddr);
357 return (void *)&uaddr;
358 }
359
360 /* ARGSUSED */
361 void *
362 rpcbproc_gettime_com(void *arg __unused, struct svc_req *rqstp __unused,
363 SVCXPRT *transp __unused, rpcvers_t rpcbversnum __unused)
364 {
365 static time_t curtime;
366
367 (void) time(&curtime);
368 return &curtime;
369 }
370
371 /*
372 * Convert uaddr to taddr. Should be used only by
373 * local servers/clients. (kernel level stuff only)
374 */
375 /* ARGSUSED */
376 void *
377 rpcbproc_uaddr2taddr_com(void *arg, struct svc_req *rqstp __unused,
378 SVCXPRT *transp, rpcvers_t rpcbversnum __unused)
379 {
380 char **uaddrp = arg;
381 struct netconfig *nconf;
382 static struct netbuf nbuf;
383 static struct netbuf *taddr;
384
385 if (taddr) {
386 free(taddr->buf);
387 free(taddr);
388 taddr = NULL;
389 }
390 if (((nconf = rpcbind_get_conf(transp->xp_netid)) == NULL) ||
391 ((taddr = uaddr2taddr(nconf, *uaddrp)) == NULL)) {
392 (void) memset(&nbuf, 0, sizeof (struct netbuf));
393 return &nbuf;
394 }
395 return taddr;
396 }
397
398 /*
399 * Convert taddr to uaddr. Should be used only by
400 * local servers/clients. (kernel level stuff only)
401 */
402 /* ARGSUSED */
403 void *
404 rpcbproc_taddr2uaddr_com(void *arg, struct svc_req *rqstp __unused,
405 SVCXPRT *transp, rpcvers_t rpcbversnum __unused)
406 {
407 struct netbuf *taddr = arg;
408 static char *uaddr;
409 struct netconfig *nconf;
410
411 #ifdef CHEW_FDS
412 int fd;
413
414 if ((fd = open("/dev/null", O_RDONLY)) == -1) {
415 uaddr = strerror(errno);
416 return (&uaddr);
417 }
418 #endif /* CHEW_FDS */
419 if (uaddr != NULL && uaddr != nullstring) {
420 free(uaddr);
421 uaddr = NULL;
422 }
423 if (((nconf = rpcbind_get_conf(transp->xp_netid)) == NULL) ||
424 ((uaddr = taddr2uaddr(nconf, taddr)) == NULL)) {
425 uaddr = nullstring;
426 }
427 return (void *)&uaddr;
428 }
429
430
431 static bool_t
432 xdr_encap_parms(XDR *xdrs, struct encap_parms *epp)
433 {
434 return (xdr_bytes(xdrs, &(epp->args), (u_int *) &(epp->arglen),
435 RPC_MAXDATASIZE));
436 }
437
438 /*
439 * XDR remote call arguments. It ignores the address part.
440 * written for XDR_DECODE direction only
441 */
442 static bool_t
443 xdr_rmtcall_args(XDR *xdrs, struct r_rmtcall_args *cap)
444 {
445 /* does not get the address or the arguments */
446 if (xdr_rpcprog(xdrs, &(cap->rmt_prog)) &&
447 xdr_rpcvers(xdrs, &(cap->rmt_vers)) &&
448 xdr_rpcproc(xdrs, &(cap->rmt_proc))) {
449 return (xdr_encap_parms(xdrs, &(cap->rmt_args)));
450 }
451 return (FALSE);
452 }
453
454 /*
455 * XDR remote call results along with the address. Ignore
456 * program number, version number and proc number.
457 * Written for XDR_ENCODE direction only.
458 */
459 static bool_t
460 xdr_rmtcall_result(XDR *xdrs, struct r_rmtcall_args *cap)
461 {
462 bool_t result;
463
464 #ifdef PORTMAP
465 if (cap->rmt_localvers == PMAPVERS) {
466 int h1, h2, h3, h4, p1, p2;
467 u_long port;
468
469 /* interpret the universal address for TCP/IP */
470 if (sscanf(cap->rmt_uaddr, "%d.%d.%d.%d.%d.%d",
471 &h1, &h2, &h3, &h4, &p1, &p2) != 6)
472 return (FALSE);
473 port = ((p1 & 0xff) << 8) + (p2 & 0xff);
474 result = xdr_u_long(xdrs, &port);
475 } else
476 #endif
477 if ((cap->rmt_localvers == RPCBVERS) ||
478 (cap->rmt_localvers == RPCBVERS4)) {
479 result = xdr_wrapstring(xdrs, &(cap->rmt_uaddr));
480 } else {
481 return (FALSE);
482 }
483 if (result == TRUE)
484 return (xdr_encap_parms(xdrs, &(cap->rmt_args)));
485 return (FALSE);
486 }
487
488 /*
489 * only worries about the struct encap_parms part of struct r_rmtcall_args.
490 * The arglen must already be set!!
491 */
492 static bool_t
493 xdr_opaque_parms(XDR *xdrs, struct r_rmtcall_args *cap)
494 {
495 return (xdr_opaque(xdrs, cap->rmt_args.args, cap->rmt_args.arglen));
496 }
497
498 static struct rmtcallfd_list *rmthead;
499 static struct rmtcallfd_list *rmttail;
500
501 int
502 create_rmtcall_fd(struct netconfig *nconf)
503 {
504 int fd;
505 struct rmtcallfd_list *rmt;
506 SVCXPRT *xprt;
507
508 if ((fd = __rpc_nconf2fd(nconf)) == -1) {
509 if (debugging)
510 fprintf(stderr,
511 "create_rmtcall_fd: couldn't open \"%s\" (errno %d)\n",
512 nconf->nc_device, errno);
513 return (-1);
514 }
515 xprt = svc_tli_create(fd, 0, NULL, 0, 0);
516 if (xprt == NULL) {
517 if (debugging)
518 fprintf(stderr,
519 "%s: svc_tli_create failed\n", __func__);
520 return (-1);
521 }
522 rmt = malloc(sizeof(*rmt));
523 if (rmt == NULL) {
524 syslog(LOG_ERR, "%s: Cannot allocate memory", __func__);
525 return (-1);
526 }
527 rmt->xprt = xprt;
528 rmt->netid = strdup(nconf->nc_netid);
529 xprt->xp_netid = rmt->netid;
530 rmt->fd = fd;
531 rmt->next = NULL;
532 if (rmthead == NULL) {
533 rmthead = rmt;
534 rmttail = rmt;
535 } else {
536 rmttail->next = rmt;
537 rmttail = rmt;
538 }
539 svc_fdset_set(fd);
540 return (fd);
541 }
542
543 static int
544 find_rmtcallfd_by_netid(char *netid)
545 {
546 struct rmtcallfd_list *rmt;
547
548 for (rmt = rmthead; rmt != NULL; rmt = rmt->next) {
549 if (strcmp(netid, rmt->netid) == 0) {
550 return (rmt->fd);
551 }
552 }
553 return (-1);
554 }
555
556 static SVCXPRT *
557 find_rmtcallxprt_by_fd(int fd)
558 {
559 struct rmtcallfd_list *rmt;
560
561 for (rmt = rmthead; rmt != NULL; rmt = rmt->next) {
562 if (fd == rmt->fd) {
563 return (rmt->xprt);
564 }
565 }
566 return (NULL);
567 }
568
569
570 /*
571 * Call a remote procedure service. This procedure is very quiet when things
572 * go wrong. The proc is written to support broadcast rpc. In the broadcast
573 * case, a machine should shut-up instead of complain, lest the requestor be
574 * overrun with complaints at the expense of not hearing a valid reply.
575 * When receiving a request and verifying that the service exists, we
576 *
577 * receive the request
578 *
579 * open a new TLI endpoint on the same transport on which we received
580 * the original request
581 *
582 * remember the original request's XID (which requires knowing the format
583 * of the svc_dg_data structure)
584 *
585 * forward the request, with a new XID, to the requested service,
586 * remembering the XID used to send this request (for later use in
587 * reassociating the answer with the original request), the requestor's
588 * address, the file descriptor on which the forwarded request is
589 * made and the service's address.
590 *
591 * mark the file descriptor on which we anticipate receiving a reply from
592 * the service and one to select for in our private svc_run procedure
593 *
594 * At some time in the future, a reply will be received from the service to
595 * which we forwarded the request. At that time, we detect that the socket
596 * used was for forwarding (by looking through the finfo structures to see
597 * whether the fd corresponds to one of those) and call handle_reply() to
598 *
599 * receive the reply
600 *
601 * bundle the reply, along with the service's universal address
602 *
603 * create a SVCXPRT structure and use a version of svc_sendreply
604 * that allows us to specify the reply XID and destination, send the reply
605 * to the original requestor.
606 */
607
608 void
609 rpcbproc_callit_com(struct svc_req *rqstp, SVCXPRT *transp,
610 rpcproc_t reply_type, rpcvers_t versnum)
611 {
612 rpcblist_ptr rbl;
613 struct netconfig *nconf;
614 struct netbuf *caller;
615 struct r_rmtcall_args a;
616 char *buf_alloc = NULL, *outbufp;
617 char *outbuf_alloc = NULL;
618 char buf[RPC_BUF_MAX], outbuf[RPC_BUF_MAX];
619 struct netbuf *na = NULL;
620 struct rpc_msg call_msg;
621 int outlen;
622 u_int sendsz;
623 XDR outxdr;
624 AUTH *auth;
625 int fd = -1;
626 char *uaddr, *m_uaddr = NULL, *local_uaddr = NULL;
627 u_int32_t *xidp;
628 struct __rpc_sockinfo si;
629 struct sockaddr *localsa;
630 struct netbuf tbuf;
631
632 if (!__rpc_fd2sockinfo(transp->xp_fd, &si)) {
633 if (reply_type == RPCBPROC_INDIRECT)
634 svcerr_systemerr(transp);
635 return;
636 }
637 if (si.si_socktype != SOCK_DGRAM)
638 return; /* Only datagram type accepted */
639 sendsz = __rpc_get_t_size(si.si_af, si.si_proto, UDPMSGSIZE);
640 if (sendsz == 0) { /* data transfer not supported */
641 if (reply_type == RPCBPROC_INDIRECT)
642 svcerr_systemerr(transp);
643 return;
644 }
645 /*
646 * Should be multiple of 4 for XDR.
647 */
648 sendsz = roundup(sendsz, 4);
649 if (sendsz > RPC_BUF_MAX) {
650 #ifdef notyet
651 buf_alloc = alloca(sendsz); /* not in IDR2? */
652 #else
653 buf_alloc = malloc(sendsz);
654 #endif /* notyet */
655 if (buf_alloc == NULL) {
656 if (debugging)
657 fprintf(stderr,
658 "rpcbproc_callit_com: No Memory!\n");
659 if (reply_type == RPCBPROC_INDIRECT)
660 svcerr_systemerr(transp);
661 return;
662 }
663 a.rmt_args.args = buf_alloc;
664 } else {
665 a.rmt_args.args = buf;
666 }
667
668 call_msg.rm_xid = 0; /* For error checking purposes */
669 if (!svc_getargs(transp, (xdrproc_t) xdr_rmtcall_args, (char *) &a)) {
670 if (reply_type == RPCBPROC_INDIRECT)
671 svcerr_decode(transp);
672 if (debugging)
673 fprintf(stderr,
674 "rpcbproc_callit_com: svc_getargs failed\n");
675 goto error;
676 }
677
678 if (!check_callit(transp, &a, versnum)) {
679 svcerr_weakauth(transp);
680 goto error;
681 }
682
683 caller = svc_getrpccaller(transp);
684 #ifdef RPCBIND_DEBUG
685 if (debugging) {
686 uaddr = taddr2uaddr(rpcbind_get_conf(transp->xp_netid), caller);
687 fprintf(stderr, "%s %s req for (%lu, %lu, %lu, %s) from %s : ",
688 versnum == PMAPVERS ? "pmap_rmtcall" :
689 versnum == RPCBVERS ? "rpcb_rmtcall" :
690 versnum == RPCBVERS4 ? "rpcb_indirect" :
691 rpcbind_unknown,
692 reply_type == RPCBPROC_INDIRECT ? "indirect" : "callit",
693 (unsigned long)a.rmt_prog, (unsigned long)a.rmt_vers,
694 (unsigned long)a.rmt_proc, transp->xp_netid,
695 uaddr ? uaddr : rpcbind_unknown);
696 if (uaddr)
697 free(uaddr);
698 }
699 #endif
700
701 rbl = find_service(a.rmt_prog, a.rmt_vers, transp->xp_netid);
702
703 rpcbs_rmtcall(versnum - 2, reply_type, a.rmt_prog, a.rmt_vers,
704 a.rmt_proc, transp->xp_netid, rbl);
705
706 if (rbl == NULL) {
707 #ifdef RPCBIND_DEBUG
708 if (debugging)
709 fprintf(stderr, "not found\n");
710 #endif
711 if (reply_type == RPCBPROC_INDIRECT)
712 svcerr_noprog(transp);
713 goto error;
714 }
715 if (rbl->rpcb_map.r_vers != a.rmt_vers) {
716 if (reply_type == RPCBPROC_INDIRECT) {
717 rpcvers_t vers_low, vers_high;
718
719 find_versions(a.rmt_prog, transp->xp_netid,
720 &vers_low, &vers_high);
721 svcerr_progvers(transp, vers_low, vers_high);
722 }
723 goto error;
724 }
725
726 #ifdef RPCBIND_DEBUG
727 if (debugging)
728 fprintf(stderr, "found at uaddr %s\n", rbl->rpcb_map.r_addr);
729 #endif
730 /*
731 * Check whether this entry is valid and a server is present
732 * Mergeaddr() returns NULL if no such entry is present, and
733 * returns "" if the entry was present but the server is not
734 * present (i.e., it crashed).
735 */
736 if (reply_type == RPCBPROC_INDIRECT) {
737 uaddr = mergeaddr(transp, transp->xp_netid,
738 rbl->rpcb_map.r_addr, NULL);
739 if (uaddr == NULL || uaddr[0] == '\0') {
740 svcerr_noprog(transp);
741 free(uaddr);
742 goto error;
743 }
744 free(uaddr);
745 }
746 nconf = rpcbind_get_conf(transp->xp_netid);
747 if (nconf == NULL) {
748 if (reply_type == RPCBPROC_INDIRECT)
749 svcerr_systemerr(transp);
750 if (debugging)
751 fprintf(stderr,
752 "rpcbproc_callit_com: rpcbind_get_conf failed\n");
753 goto error;
754 }
755 localsa = local_sa(((struct sockaddr *)caller->buf)->sa_family);
756 if (localsa == NULL) {
757 if (debugging)
758 fprintf(stderr,
759 "rpcbproc_callit_com: no local address\n");
760 goto error;
761 }
762 tbuf.len = tbuf.maxlen = localsa->sa_len;
763 tbuf.buf = localsa;
764 local_uaddr =
765 addrmerge(&tbuf, rbl->rpcb_map.r_addr, NULL, nconf->nc_netid);
766 m_uaddr = addrmerge(caller, rbl->rpcb_map.r_addr, NULL,
767 nconf->nc_netid);
768 #ifdef RPCBIND_DEBUG
769 if (debugging)
770 fprintf(stderr, "merged uaddr %s\n", m_uaddr);
771 #endif
772 if ((fd = find_rmtcallfd_by_netid(nconf->nc_netid)) == -1) {
773 if (reply_type == RPCBPROC_INDIRECT)
774 svcerr_systemerr(transp);
775 goto error;
776 }
777 xidp = __rpcb_get_dg_xidp(transp);
778 switch (forward_register(*xidp, caller, fd, m_uaddr, reply_type,
779 versnum, &call_msg.rm_xid)) {
780 case 1:
781 /* Success; forward_register() will free m_uaddr for us. */
782 m_uaddr = NULL;
783 break;
784 case 0:
785 /*
786 * A duplicate request for the slow server. Let's not
787 * beat on it any more.
788 */
789 if (debugging)
790 fprintf(stderr,
791 "rpcbproc_callit_com: duplicate request\n");
792 goto error;
793 case -1:
794 /* forward_register failed. Perhaps no memory. */
795 if (debugging)
796 fprintf(stderr,
797 "rpcbproc_callit_com: forward_register failed\n");
798 goto error;
799 }
800
801 #ifdef DEBUG_RMTCALL
802 if (debugging)
803 fprintf(stderr,
804 "rpcbproc_callit_com: original XID %x, new XID %x\n",
805 *xidp, call_msg.rm_xid);
806 #endif
807 call_msg.rm_direction = CALL;
808 call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
809 call_msg.rm_call.cb_prog = a.rmt_prog;
810 call_msg.rm_call.cb_vers = a.rmt_vers;
811 if (sendsz > RPC_BUF_MAX) {
812 #ifdef notyet
813 outbuf_alloc = alloca(sendsz); /* not in IDR2? */
814 #else
815 outbuf_alloc = malloc(sendsz);
816 #endif /* notyet */
817 if (outbuf_alloc == NULL) {
818 if (reply_type == RPCBPROC_INDIRECT)
819 svcerr_systemerr(transp);
820 syslog(LOG_ERR, "%s: Cannot allocate memory", __func__);
821 goto error;
822 }
823 xdrmem_create(&outxdr, outbuf_alloc, sendsz, XDR_ENCODE);
824 } else {
825 xdrmem_create(&outxdr, outbuf, sendsz, XDR_ENCODE);
826 }
827 if (!xdr_callhdr(&outxdr, &call_msg)) {
828 if (reply_type == RPCBPROC_INDIRECT)
829 svcerr_systemerr(transp);
830 if (debugging)
831 fprintf(stderr,
832 "rpcbproc_callit_com: xdr_callhdr failed\n");
833 goto error;
834 }
835 if (!xdr_u_int32_t(&outxdr, &(a.rmt_proc))) {
836 if (reply_type == RPCBPROC_INDIRECT)
837 svcerr_systemerr(transp);
838 if (debugging)
839 fprintf(stderr,
840 "rpcbproc_callit_com: xdr_u_long failed\n");
841 goto error;
842 }
843
844 if (rqstp->rq_cred.oa_flavor == AUTH_NULL) {
845 auth = authnone_create();
846 } else if (rqstp->rq_cred.oa_flavor == AUTH_SYS) {
847 struct authunix_parms *au;
848
849 au = (struct authunix_parms *)rqstp->rq_clntcred;
850 auth = authunix_create(au->aup_machname,
851 au->aup_uid, au->aup_gid,
852 au->aup_len, au->aup_gids);
853 if (auth == NULL) /* fall back */
854 auth = authnone_create();
855 } else {
856 /* we do not support any other authentication scheme */
857 if (debugging)
858 fprintf(stderr,
859 "rpcbproc_callit_com: oa_flavor != AUTH_NONE and oa_flavor != AUTH_SYS\n");
860 if (reply_type == RPCBPROC_INDIRECT)
861 svcerr_weakauth(transp); /* XXX too strong.. */
862 goto error;
863 }
864 if (auth == NULL) {
865 if (reply_type == RPCBPROC_INDIRECT)
866 svcerr_systemerr(transp);
867 if (debugging)
868 fprintf(stderr,
869 "rpcbproc_callit_com: authwhatever_create returned NULL\n");
870 goto error;
871 }
872 if (!AUTH_MARSHALL(auth, &outxdr)) {
873 if (reply_type == RPCBPROC_INDIRECT)
874 svcerr_systemerr(transp);
875 AUTH_DESTROY(auth);
876 if (debugging)
877 fprintf(stderr,
878 "rpcbproc_callit_com: AUTH_MARSHALL failed\n");
879 goto error;
880 }
881 AUTH_DESTROY(auth);
882 if (!xdr_opaque_parms(&outxdr, &a)) {
883 if (reply_type == RPCBPROC_INDIRECT)
884 svcerr_systemerr(transp);
885 if (debugging)
886 fprintf(stderr,
887 "rpcbproc_callit_com: xdr_opaque_parms failed\n");
888 goto error;
889 }
890 outlen = (int) XDR_GETPOS(&outxdr);
891 if (outbuf_alloc)
892 outbufp = outbuf_alloc;
893 else
894 outbufp = outbuf;
895
896 na = uaddr2taddr(nconf, local_uaddr);
897 if (!na) {
898 if (reply_type == RPCBPROC_INDIRECT)
899 svcerr_systemerr(transp);
900 goto error;
901 }
902
903 if (sendto(fd, outbufp, outlen, 0, (struct sockaddr *)na->buf, na->len)
904 != outlen) {
905 if (debugging)
906 fprintf(stderr,
907 "rpcbproc_callit_com: sendto failed: errno %d\n", errno);
908 if (reply_type == RPCBPROC_INDIRECT)
909 svcerr_systemerr(transp);
910 goto error;
911 }
912 goto out;
913
914 error:
915 if (call_msg.rm_xid != 0)
916 (void) free_slot_by_xid(call_msg.rm_xid);
917 out:
918 if (local_uaddr)
919 free(local_uaddr);
920 if (buf_alloc)
921 free(buf_alloc);
922 if (outbuf_alloc)
923 free(outbuf_alloc);
924 if (na) {
925 free(na->buf);
926 free(na);
927 }
928 if (m_uaddr != NULL)
929 free(m_uaddr);
930 }
931
932 /*
933 * Makes an entry into the FIFO for the given request.
934 * Returns 1 on success, 0 if this is a duplicate request, or -1 on error.
935 * *callxidp is set to the xid of the call.
936 */
937 static int
938 forward_register(u_int32_t caller_xid, struct netbuf *caller_addr,
939 int forward_fd, char *uaddr, rpcproc_t reply_type,
940 rpcvers_t versnum, u_int32_t *callxidp)
941 {
942 int i;
943 int j = 0;
944 time_t min_time, time_now;
945 static u_int32_t lastxid;
946 int entry = -1;
947
948 min_time = FINFO[0].time;
949 time_now = time((time_t *)0);
950 /* initialization */
951 if (lastxid == 0)
952 lastxid = time_now * NFORWARD;
953
954 /*
955 * Check if it is a duplicate entry. Then,
956 * try to find an empty slot. If not available, then
957 * use the slot with the earliest time.
958 */
959 for (i = 0; i < NFORWARD; i++) {
960 if (FINFO[i].flag & FINFO_ACTIVE) {
961 if ((FINFO[i].caller_xid == caller_xid) &&
962 (FINFO[i].reply_type == reply_type) &&
963 (FINFO[i].versnum == versnum) &&
964 (!netbufcmp(FINFO[i].caller_addr,
965 caller_addr))) {
966 FINFO[i].time = time((time_t *)0);
967 return (0); /* Duplicate entry */
968 } else {
969 /* Should we wait any longer */
970 if ((time_now - FINFO[i].time) > MAXTIME_OFF)
971 (void) free_slot_by_index(i);
972 }
973 }
974 if (entry == -1) {
975 if ((FINFO[i].flag & FINFO_ACTIVE) == 0) {
976 entry = i;
977 } else if (FINFO[i].time < min_time) {
978 j = i;
979 min_time = FINFO[i].time;
980 }
981 }
982 }
983 if (entry != -1) {
984 /* use this empty slot */
985 j = entry;
986 } else {
987 (void) free_slot_by_index(j);
988 }
989 if ((FINFO[j].caller_addr = netbufdup(caller_addr)) == NULL) {
990 return (-1);
991 }
992 rpcb_rmtcalls++; /* no of pending calls */
993 FINFO[j].flag = FINFO_ACTIVE;
994 FINFO[j].reply_type = reply_type;
995 FINFO[j].versnum = versnum;
996 FINFO[j].time = time_now;
997 FINFO[j].caller_xid = caller_xid;
998 FINFO[j].forward_fd = forward_fd;
999 /*
1000 * Though uaddr is not allocated here, it will still be freed
1001 * from free_slot_*().
1002 */
1003 FINFO[j].uaddr = uaddr;
1004 lastxid = lastxid + NFORWARD;
1005 /* Don't allow a zero xid below. */
1006 if ((u_int32_t)(lastxid + NFORWARD) <= NFORWARD)
1007 lastxid = NFORWARD;
1008 FINFO[j].forward_xid = lastxid + j; /* encode slot */
1009 *callxidp = FINFO[j].forward_xid; /* forward on this xid */
1010 return (1);
1011 }
1012
1013 static struct finfo *
1014 forward_find(u_int32_t reply_xid)
1015 {
1016 int i;
1017
1018 i = reply_xid % NFORWARD;
1019 if (i < 0)
1020 i += NFORWARD;
1021 if ((FINFO[i].flag & FINFO_ACTIVE) &&
1022 (FINFO[i].forward_xid == reply_xid)) {
1023 return (&FINFO[i]);
1024 }
1025 return (NULL);
1026 }
1027
1028 static int
1029 free_slot_by_xid(u_int32_t xid)
1030 {
1031 int entry;
1032
1033 entry = xid % NFORWARD;
1034 if (entry < 0)
1035 entry += NFORWARD;
1036 return (free_slot_by_index(entry));
1037 }
1038
1039 static int
1040 free_slot_by_index(int idx)
1041 {
1042 struct finfo *fi;
1043
1044 fi = &FINFO[idx];
1045 if (fi->flag & FINFO_ACTIVE) {
1046 netbuffree(fi->caller_addr);
1047 /* XXX may be too big, but can't access xprt array here */
1048 if (fi->forward_fd >= *svc_fdset_getmax())
1049 (*svc_fdset_getmax())--;
1050 free(fi->uaddr);
1051 fi->flag &= ~FINFO_ACTIVE;
1052 rpcb_rmtcalls--;
1053 return (1);
1054 }
1055 return (0);
1056 }
1057
1058 static int
1059 netbufcmp(struct netbuf *n1, struct netbuf *n2)
1060 {
1061 return ((n1->len != n2->len) || memcmp(n1->buf, n2->buf, n1->len));
1062 }
1063
1064 static bool_t
1065 netbuf_copybuf(struct netbuf *dst, const struct netbuf *src)
1066 {
1067 assert(src->len <= src->maxlen);
1068
1069 if (dst->maxlen < src->len || dst->buf == NULL) {
1070 if (dst->buf != NULL)
1071 free(dst->buf);
1072 if ((dst->buf = calloc(1, src->maxlen)) == NULL)
1073 return (FALSE);
1074 dst->maxlen = src->maxlen;
1075 }
1076
1077 dst->len = src->len;
1078 memcpy(dst->buf, src->buf, src->len);
1079
1080 return (TRUE);
1081 }
1082
1083 static struct netbuf *
1084 netbufdup(struct netbuf *ap)
1085 {
1086 struct netbuf *np;
1087
1088 if ((np = calloc(1, sizeof(struct netbuf))) == NULL)
1089 return (NULL);
1090 if (netbuf_copybuf(np, ap) == FALSE) {
1091 free(np);
1092 return (NULL);
1093 }
1094 return (np);
1095 }
1096
1097 static void
1098 netbuffree(struct netbuf *ap)
1099 {
1100 free(ap->buf);
1101 ap->buf = NULL;
1102 free(ap);
1103 }
1104
1105
1106 #define MASKVAL (POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND)
1107 extern bool_t __svc_clean_idle(fd_set *, int, bool_t);
1108
1109 void
1110 my_svc_run(void)
1111 {
1112 size_t nfds;
1113 struct pollfd *pollfds;
1114 int npollfds;
1115 int poll_ret, check_ret;
1116 int n, *m;
1117 #ifdef SVC_RUN_DEBUG
1118 int i;
1119 #endif
1120 struct pollfd *p;
1121
1122 pollfds = NULL;
1123 npollfds = 0;
1124
1125 for (;;) {
1126 if (svc_fdset_getsize(0) != npollfds) {
1127 npollfds = svc_fdset_getsize(0);
1128 pollfds = realloc(pollfds, npollfds * sizeof(*pollfds));
1129 }
1130 p = pollfds;
1131 if (p == NULL) {
1132 out:
1133 syslog(LOG_ERR, "Cannot allocate pollfds");
1134 sleep(1);
1135 continue;
1136 }
1137 if ((m = svc_fdset_getmax()) == NULL)
1138 goto out;
1139 for (n = 0; n <= *m; n++) {
1140 if (svc_fdset_isset(n)) {
1141 p->fd = n;
1142 p->events = MASKVAL;
1143 p++;
1144 }
1145 }
1146 nfds = p - pollfds;
1147 poll_ret = 0;
1148 #ifdef SVC_RUN_DEBUG
1149 if (debugging) {
1150 fprintf(stderr, "polling for read on fd < ");
1151 for (i = 0, p = pollfds; i < nfds; i++, p++)
1152 if (p->events)
1153 fprintf(stderr, "%d ", p->fd);
1154 fprintf(stderr, ">\n");
1155 }
1156 #endif
1157 #ifdef RPCBIND_RUMP
1158 poll_ret = rump_sys_poll(pollfds, nfds, 30 * 1000);
1159 #else
1160 poll_ret = poll(pollfds, nfds, 30 * 1000);
1161 #endif
1162 switch (poll_ret) {
1163 case -1:
1164 /*
1165 * We ignore all errors, continuing with the assumption
1166 * that it was set by the signal handlers (or any
1167 * other outside event) and not caused by poll().
1168 */
1169 #ifdef SVC_RUN_DEBUG
1170 if (debugging) {
1171 fprintf(stderr, "poll returned %d (%s)\n",
1172 poll_ret, strerror(errno));
1173 }
1174 #endif
1175 case 0:
1176 __svc_clean_idle(NULL, 30, FALSE);
1177 continue;
1178 default:
1179 #ifdef SVC_RUN_DEBUG
1180 if (debugging) {
1181 fprintf(stderr, "poll returned read fds < ");
1182 for (i = 0, p = pollfds; i < nfds; i++, p++)
1183 if (p->revents)
1184 fprintf(stderr, "%d (%#x)",
1185 p->fd, p->revents);
1186 fprintf(stderr, ">\n");
1187 }
1188 #endif
1189 /*
1190 * If we found as many replies on callback fds
1191 * as the number of descriptors selectable which
1192 * poll() returned, there can be no more so we
1193 * don't call svc_getreq_poll. Otherwise, there
1194 * must be another so we must call svc_getreq_poll.
1195 */
1196 if ((check_ret = check_rmtcalls(pollfds, nfds)) ==
1197 poll_ret)
1198 continue;
1199 svc_getreq_poll(pollfds, poll_ret-check_ret);
1200 }
1201 #ifdef SVC_RUN_DEBUG
1202 if (debugging) {
1203 fprintf(stderr, "svc_maxfd now %u\n",
1204 *svc_fdset_getmax());
1205 }
1206 #endif
1207 }
1208 }
1209
1210 static int
1211 check_rmtcalls(struct pollfd *pfds, int nfds)
1212 {
1213 int j, ncallbacks_found = 0, rmtcalls_pending;
1214 SVCXPRT *xprt;
1215
1216 if (rpcb_rmtcalls == 0)
1217 return (0);
1218
1219 rmtcalls_pending = rpcb_rmtcalls;
1220 for (j = 0; j < nfds; j++) {
1221 if ((xprt = find_rmtcallxprt_by_fd(pfds[j].fd)) != NULL) {
1222 if (pfds[j].revents) {
1223 ncallbacks_found++;
1224 #ifdef DEBUG_RMTCALL
1225 if (debugging)
1226 fprintf(stderr,
1227 "my_svc_run: polled on forwarding fd %d, netid %s - calling handle_reply\n",
1228 pfds[j].fd, xprt->xp_netid);
1229 #endif
1230 handle_reply(pfds[j].fd, xprt);
1231 pfds[j].revents = 0;
1232 if (ncallbacks_found >= rmtcalls_pending) {
1233 break;
1234 }
1235 }
1236 }
1237 }
1238 return (ncallbacks_found);
1239 }
1240
1241 static void
1242 xprt_set_caller(SVCXPRT *xprt, struct finfo *fi)
1243 {
1244 u_int32_t *xidp;
1245
1246 netbuf_copybuf(svc_getrpccaller(xprt), fi->caller_addr);
1247 xidp = __rpcb_get_dg_xidp(xprt);
1248 *xidp = fi->caller_xid;
1249 }
1250
1251 /*
1252 * Call svcerr_systemerr() only if RPCBVERS4
1253 */
1254 static void
1255 send_svcsyserr(SVCXPRT *xprt, struct finfo *fi)
1256 {
1257 if (fi->reply_type == RPCBPROC_INDIRECT) {
1258 xprt_set_caller(xprt, fi);
1259 svcerr_systemerr(xprt);
1260 }
1261 return;
1262 }
1263
1264 static void
1265 handle_reply(int fd, SVCXPRT *xprt)
1266 {
1267 XDR reply_xdrs;
1268 struct rpc_msg reply_msg;
1269 struct rpc_err reply_error;
1270 char *buffer;
1271 struct finfo *fi;
1272 int inlen, pos, len;
1273 struct r_rmtcall_args a;
1274 struct sockaddr_storage ss;
1275 socklen_t fromlen;
1276 #ifdef SVC_RUN_DEBUG
1277 char *uaddr;
1278 #endif
1279
1280 buffer = malloc(RPC_BUF_MAX);
1281 if (buffer == NULL)
1282 goto done;
1283
1284 do {
1285 fromlen = sizeof(ss);
1286 inlen = recvfrom(fd, buffer, RPC_BUF_MAX, 0,
1287 (struct sockaddr *)&ss, &fromlen);
1288 } while (inlen < 0 && errno == EINTR);
1289 if (inlen < 0) {
1290 if (debugging)
1291 fprintf(stderr,
1292 "handle_reply: recvfrom returned %d, errno %d\n", inlen, errno);
1293 goto done;
1294 }
1295
1296 reply_msg.acpted_rply.ar_verf = _null_auth;
1297 reply_msg.acpted_rply.ar_results.where = 0;
1298 reply_msg.acpted_rply.ar_results.proc = (xdrproc_t) xdr_void;
1299
1300 xdrmem_create(&reply_xdrs, buffer, (u_int)inlen, XDR_DECODE);
1301 if (!xdr_replymsg(&reply_xdrs, &reply_msg)) {
1302 if (debugging)
1303 (void) fprintf(stderr,
1304 "handle_reply: xdr_replymsg failed\n");
1305 goto done;
1306 }
1307 fi = forward_find(reply_msg.rm_xid);
1308 #ifdef SVC_RUN_DEBUG
1309 if (debugging) {
1310 fprintf(stderr, "handle_reply: reply xid: %d fi addr: %p\n",
1311 reply_msg.rm_xid, fi);
1312 }
1313 #endif
1314 if (fi == NULL) {
1315 goto done;
1316 }
1317 _seterr_reply(&reply_msg, &reply_error);
1318 if (reply_error.re_status != RPC_SUCCESS) {
1319 if (debugging)
1320 (void) fprintf(stderr, "handle_reply: %s\n",
1321 clnt_sperrno(reply_error.re_status));
1322 send_svcsyserr(xprt, fi);
1323 goto done;
1324 }
1325 pos = XDR_GETPOS(&reply_xdrs);
1326 len = inlen - pos;
1327 a.rmt_args.args = &buffer[pos];
1328 a.rmt_args.arglen = len;
1329 a.rmt_uaddr = fi->uaddr;
1330 a.rmt_localvers = fi->versnum;
1331
1332 xprt_set_caller(xprt, fi);
1333 #ifdef SVC_RUN_DEBUG
1334 uaddr = taddr2uaddr(rpcbind_get_conf("udp"),
1335 svc_getrpccaller(xprt));
1336 if (debugging) {
1337 fprintf(stderr, "handle_reply: forwarding address %s to %s\n",
1338 a.rmt_uaddr, uaddr ? uaddr : rpcbind_unknown);
1339 }
1340 if (uaddr)
1341 free(uaddr);
1342 #endif
1343 svc_sendreply(xprt, (xdrproc_t) xdr_rmtcall_result, (char *) &a);
1344 done:
1345 if (buffer)
1346 free(buffer);
1347
1348 if (reply_msg.rm_xid == 0) {
1349 #ifdef SVC_RUN_DEBUG
1350 if (debugging) {
1351 fprintf(stderr, "handle_reply: NULL xid on exit!\n");
1352 }
1353 #endif
1354 } else
1355 (void) free_slot_by_xid(reply_msg.rm_xid);
1356 return;
1357 }
1358
1359 static void
1360 find_versions(rpcprog_t prog, char *netid, rpcvers_t *lowvp, rpcvers_t *highvp)
1361 {
1362 rpcblist_ptr rbl;
1363 rpcvers_t lowv = 0;
1364 rpcvers_t highv = 0;
1365
1366 for (rbl = list_rbl; rbl != NULL; rbl = rbl->rpcb_next) {
1367 if ((rbl->rpcb_map.r_prog != prog) ||
1368 ((rbl->rpcb_map.r_netid != NULL) &&
1369 (strcasecmp(rbl->rpcb_map.r_netid, netid) != 0)))
1370 continue;
1371 if (lowv == 0) {
1372 highv = rbl->rpcb_map.r_vers;
1373 lowv = highv;
1374 } else if (rbl->rpcb_map.r_vers < lowv) {
1375 lowv = rbl->rpcb_map.r_vers;
1376 } else if (rbl->rpcb_map.r_vers > highv) {
1377 highv = rbl->rpcb_map.r_vers;
1378 }
1379 }
1380 *lowvp = lowv;
1381 *highvp = highv;
1382 return;
1383 }
1384
1385 /*
1386 * returns the item with the given program, version number and netid.
1387 * If that version number is not found, it returns the item with that
1388 * program number, so that address is now returned to the caller. The
1389 * caller when makes a call to this program, version number, the call
1390 * will fail and it will return with PROGVERS_MISMATCH. The user can
1391 * then determine the highest and the lowest version number for this
1392 * program using clnt_geterr() and use those program version numbers.
1393 *
1394 * Returns the RPCBLIST for the given prog, vers and netid
1395 */
1396 static rpcblist_ptr
1397 find_service(rpcprog_t prog, rpcvers_t vers, char *netid)
1398 {
1399 rpcblist_ptr hit = NULL;
1400 rpcblist_ptr rbl;
1401
1402 for (rbl = list_rbl; rbl != NULL; rbl = rbl->rpcb_next) {
1403 if ((rbl->rpcb_map.r_prog != prog) ||
1404 ((rbl->rpcb_map.r_netid != NULL) &&
1405 (strcasecmp(rbl->rpcb_map.r_netid, netid) != 0)))
1406 continue;
1407 hit = rbl;
1408 if (rbl->rpcb_map.r_vers == vers)
1409 break;
1410 }
1411 return (hit);
1412 }
1413
1414 /*
1415 * Copies the name associated with the uid of the caller and returns
1416 * a pointer to it. Similar to getwd().
1417 */
1418 static char *
1419 getowner(SVCXPRT *transp, char *owner, size_t ownersize)
1420 {
1421 struct sockcred *sc;
1422
1423 sc = __svc_getcallercreds(transp);
1424 if (sc == NULL)
1425 strlcpy(owner, rpcbind_unknown, ownersize);
1426 else if (sc->sc_uid == 0)
1427 strlcpy(owner, rpcbind_superuser, ownersize);
1428 else
1429 snprintf(owner, ownersize, "%d", sc->sc_uid);
1430
1431 return owner;
1432 }
1433
1434 #ifdef PORTMAP
1435 /*
1436 * Add this to the pmap list only if it is UDP or TCP.
1437 */
1438 static int
1439 add_pmaplist(RPCB *arg)
1440 {
1441 struct pmap pmap;
1442 struct pmaplist *pml;
1443 int h1, h2, h3, h4, p1, p2;
1444
1445 if (strcmp(arg->r_netid, udptrans) == 0) {
1446 /* It is UDP! */
1447 pmap.pm_prot = IPPROTO_UDP;
1448 } else if (strcmp(arg->r_netid, tcptrans) == 0) {
1449 /* It is TCP */
1450 pmap.pm_prot = IPPROTO_TCP;
1451 } else
1452 /* Not an IP protocol */
1453 return (0);
1454
1455 /* interpret the universal address for TCP/IP */
1456 if (sscanf(arg->r_addr, "%d.%d.%d.%d.%d.%d",
1457 &h1, &h2, &h3, &h4, &p1, &p2) != 6)
1458 return (0);
1459 pmap.pm_port = ((p1 & 0xff) << 8) + (p2 & 0xff);
1460 pmap.pm_prog = arg->r_prog;
1461 pmap.pm_vers = arg->r_vers;
1462 /*
1463 * add to END of list
1464 */
1465 pml = malloc(sizeof(*pml));
1466 if (pml == NULL) {
1467 syslog(LOG_ERR, "%s: Cannot allocate memory", __func__);
1468 return (1);
1469 }
1470 pml->pml_map = pmap;
1471 pml->pml_next = NULL;
1472 if (list_pml == NULL) {
1473 list_pml = pml;
1474 } else {
1475 struct pmaplist *fnd;
1476
1477 /* Attach to the end of the list */
1478 for (fnd = list_pml; fnd->pml_next; fnd = fnd->pml_next)
1479 continue;
1480 fnd->pml_next = pml;
1481 }
1482 return (0);
1483 }
1484
1485 /*
1486 * Delete this from the pmap list only if it is UDP or TCP.
1487 */
1488 static int
1489 del_pmaplist(RPCB *arg)
1490 {
1491 struct pmaplist *pml;
1492 struct pmaplist *prevpml, *fnd;
1493 unsigned long prot;
1494
1495 if (strcmp(arg->r_netid, udptrans) == 0) {
1496 /* It is UDP! */
1497 prot = IPPROTO_UDP;
1498 } else if (strcmp(arg->r_netid, tcptrans) == 0) {
1499 /* It is TCP */
1500 prot = IPPROTO_TCP;
1501 } else if (arg->r_netid[0] == 0) {
1502 prot = 0; /* Remove all occurrences */
1503 } else {
1504 /* Not an IP protocol */
1505 return (0);
1506 }
1507 for (prevpml = NULL, pml = list_pml; pml; /* cstyle */) {
1508 if ((pml->pml_map.pm_prog != arg->r_prog) ||
1509 (pml->pml_map.pm_vers != arg->r_vers) ||
1510 (prot && (pml->pml_map.pm_prot != prot))) {
1511 /* both pml & prevpml move forwards */
1512 prevpml = pml;
1513 pml = pml->pml_next;
1514 continue;
1515 }
1516 /* found it; pml moves forward, prevpml stays */
1517 fnd = pml;
1518 pml = pml->pml_next;
1519 if (prevpml == NULL)
1520 list_pml = pml;
1521 else
1522 prevpml->pml_next = pml;
1523 free(fnd);
1524 }
1525 return (0);
1526 }
1527 #endif /* PORTMAP */
1528