rpcb_svc_com.c revision 1.3 1 /* $NetBSD: rpcb_svc_com.c,v 1.3 2000/06/22 08:09:26 fvdl Exp $ */
2
3 /*
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.
10 *
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.
14 *
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.
18 *
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.
22 *
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.
26 *
27 * Sun Microsystems, Inc.
28 * 2550 Garcia Avenue
29 * Mountain View, California 94043
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/poll.h>
46 #include <sys/socket.h>
47 #include <rpc/rpc.h>
48 #include <rpc/rpcb_prot.h>
49 #include <netconfig.h>
50 #include <errno.h>
51 #include <syslog.h>
52 #include <unistd.h>
53 #include <stdio.h>
54 #ifdef PORTMAP
55 #include <netinet/in.h>
56 #include <rpc/pmap_prot.h>
57 #endif /* PORTMAP */
58 #include <string.h>
59 #include <stdlib.h>
60
61 #include "rpcbind.h"
62 #include "svc_dg.h"
63
64 #define RPC_BUF_MAX 65536 /* can be raised if required */
65
66 static char *nullstring = "";
67 static int rpcb_rmtcalls;
68
69 struct rmtcallfd_list {
70 int fd;
71 SVCXPRT *xprt;
72 char *netid;
73 struct rmtcallfd_list *next;
74 };
75
76 #define NFORWARD 64
77 #define MAXTIME_OFF 300 /* 5 minutes */
78
79 struct finfo {
80 int flag;
81 #define FINFO_ACTIVE 0x1
82 u_int32_t caller_xid;
83 struct netbuf *caller_addr;
84 u_int32_t forward_xid;
85 int forward_fd;
86 char *uaddr;
87 rpcproc_t reply_type;
88 rpcvers_t versnum;
89 time_t time;
90 };
91 static struct finfo FINFO[NFORWARD];
92
93
94 static bool_t xdr_encap_parms __P((XDR *, struct encap_parms *));
95 static bool_t xdr_rmtcall_args __P((XDR *, struct r_rmtcall_args *));
96 static bool_t xdr_rmtcall_result __P((XDR *, struct r_rmtcall_args *));
97 static bool_t xdr_opaque_parms __P((XDR *, struct r_rmtcall_args *));
98 static int find_rmtcallfd_by_netid __P((char *));
99 static SVCXPRT *find_rmtcallxprt_by_fd __P((int));
100 static u_int32_t forward_register __P((u_int32_t, struct netbuf *, int, char *,
101 rpcproc_t, rpcvers_t));
102 static struct finfo *forward_find __P((u_int32_t));
103 static int free_slot_by_xid __P((u_int32_t));
104 static int free_slot_by_index __P((int));
105 static int netbufcmp __P((struct netbuf *, struct netbuf *));
106 static struct netbuf *netbufdup __P((struct netbuf *));
107 static void netbuffree __P((struct netbuf *));
108 static int check_rmtcalls __P((struct pollfd *, int));
109 static void xprt_set_caller __P((SVCXPRT *, struct finfo *));
110 static void send_svcsyserr __P((SVCXPRT *, struct finfo *));
111 static void handle_reply __P((int, SVCXPRT *));
112 static void find_versions __P((rpcprog_t, char *, rpcvers_t *, rpcvers_t *));
113 static rpcblist_ptr find_service __P((rpcprog_t, rpcvers_t, char *));
114 static char *getowner __P((SVCXPRT *, char *, size_t));
115 static int add_pmaplist __P((RPCB *));
116 static int del_pmaplist __P((RPCB *));
117
118 /*
119 * Set a mapping of program, version, netid
120 */
121 /* ARGSUSED */
122 void *
123 rpcbproc_set_com(void *arg, struct svc_req *rqstp, SVCXPRT *transp,
124 rpcvers_t rpcbversnum)
125 {
126 RPCB *regp = (RPCB *)arg;
127 static bool_t ans;
128 char owner[64];
129
130 #ifdef RPCBIND_DEBUG
131 if (debugging)
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);
135 #endif
136 ans = map_set(regp, getowner(transp, owner, sizeof owner));
137 #ifdef RPCBIND_DEBUG
138 if (debugging)
139 fprintf(stderr, "%s\n", ans == TRUE ? "succeeded" : "failed");
140 #endif
141 /* XXX: should have used some defined constant here */
142 rpcbs_set(rpcbversnum - 2, ans);
143 return (void *)&ans;
144 }
145
146 bool_t
147 map_set(RPCB *regp, char *owner)
148 {
149 RPCB reg, *a;
150 rpcblist_ptr rbl, fnd;
151
152 reg = *regp;
153 /*
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
157 */
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))
161 /*
162 * if these match then it is already
163 * registered so just say "OK".
164 */
165 return (TRUE);
166 else
167 return (FALSE);
168 }
169 /*
170 * add to the end of the list
171 */
172 rbl = (rpcblist_ptr) malloc((u_int)sizeof (RPCBLIST));
173 if (rbl == (rpcblist_ptr)NULL) {
174 return (FALSE);
175 }
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) {
183 if (a->r_netid)
184 free((void *) a->r_netid);
185 if (a->r_addr)
186 free((void *) a->r_addr);
187 if (a->r_owner)
188 free((void *) a->r_owner);
189 free((void *)rbl);
190 return (FALSE);
191 }
192 rbl->rpcb_next = (rpcblist_ptr)NULL;
193 if (list_rbl == NULL) {
194 list_rbl = rbl;
195 } else {
196 for (fnd = list_rbl; fnd->rpcb_next;
197 fnd = fnd->rpcb_next)
198 ;
199 fnd->rpcb_next = rbl;
200 }
201 #ifdef PORTMAP
202 (void) add_pmaplist(regp);
203 #endif
204 return (TRUE);
205 }
206
207 /*
208 * Unset a mapping of program, version, netid
209 */
210 /* ARGSUSED */
211 void *
212 rpcbproc_unset_com(void *arg, struct svc_req *rqstp, SVCXPRT *transp,
213 rpcvers_t rpcbversnum)
214 {
215 RPCB *regp = (RPCB *)arg;
216 static bool_t ans;
217 char owner[64];
218
219 #ifdef RPCBIND_DEBUG
220 if (debugging)
221 fprintf(stderr, "RPCB_UNSET request for (%lu, %lu, %s) : ",
222 (unsigned long)regp->r_prog, (unsigned long)regp->r_vers,
223 regp->r_netid);
224 #endif
225 ans = map_unset(regp, getowner(transp, owner, sizeof owner));
226 #ifdef RPCBIND_DEBUG
227 if (debugging)
228 fprintf(stderr, "%s\n", ans == TRUE ? "succeeded" : "failed");
229 #endif
230 /* XXX: should have used some defined constant here */
231 rpcbs_unset(rpcbversnum - 2, ans);
232 return (void *)&ans;
233 }
234
235 bool_t
236 map_unset(RPCB *regp, char *owner)
237 {
238 int ans = 0;
239 rpcblist_ptr rbl, prev, tmp;
240
241 if (owner == NULL)
242 return (0);
243
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 */
250 prev = rbl;
251 rbl = rbl->rpcb_next;
252 continue;
253 }
254 /*
255 * Check whether appropriate uid. Unset only
256 * if superuser or the owner itself.
257 */
258 if (strcmp(owner, "superuser") &&
259 strcmp(rbl->rpcb_map.r_owner, owner))
260 return (0);
261 /* found it; rbl moves forward, prev stays */
262 ans = 1;
263 tmp = rbl;
264 rbl = rbl->rpcb_next;
265 if (prev == NULL)
266 list_rbl = rbl;
267 else
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);
272 free((void *) tmp);
273 }
274 #ifdef PORTMAP
275 if (ans)
276 (void) del_pmaplist(regp);
277 #endif
278 /*
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.
282 */
283 return (1);
284 }
285
286 void
287 delete_prog(int prog)
288 {
289 RPCB reg;
290 register rpcblist_ptr rbl;
291
292 for (rbl = list_rbl; rbl != NULL; rbl = rbl->rpcb_next) {
293 if ((rbl->rpcb_map.r_prog != prog))
294 continue;
295 if (is_bound(rbl->rpcb_map.r_netid, rbl->rpcb_map.r_addr))
296 continue;
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(®, "superuser");
301 free(reg.r_netid);
302 }
303 }
304
305 void *
306 rpcbproc_getaddr_com(RPCB *regp, struct svc_req *rqstp, SVCXPRT *transp,
307 rpcvers_t rpcbversnum, rpcvers_t verstype)
308 {
309 static char *uaddr;
310 char *saddr = NULL;
311 rpcblist_ptr fnd;
312
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 */
320 } /* should use */
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]) {
326 /*
327 * The server died. Unset all versions of this prog.
328 */
329 delete_prog(regp->r_prog);
330 uaddr = nullstring;
331 }
332 } else {
333 uaddr = nullstring;
334 }
335 #ifdef RPCBIND_DEBUG
336 if (debugging)
337 fprintf(stderr, "getaddr: %s\n", uaddr);
338 #endif
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;
343 }
344
345 /* ARGSUSED */
346 void *
347 rpcbproc_gettime_com(void *arg, struct svc_req *rqstp, SVCXPRT *transp,
348 rpcvers_t rpcbversnum)
349 {
350 static time_t curtime;
351
352 (void) time(&curtime);
353 return (void *)&curtime;
354 }
355
356 /*
357 * Convert uaddr to taddr. Should be used only by
358 * local servers/clients. (kernel level stuff only)
359 */
360 /* ARGSUSED */
361 void *
362 rpcbproc_uaddr2taddr_com(void *arg, struct svc_req *rqstp, SVCXPRT *transp,
363 rpcvers_t rpcbversnum)
364 {
365 char **uaddrp = (char **)arg;
366 struct netconfig *nconf;
367 static struct netbuf nbuf;
368 static struct netbuf *taddr;
369
370 if (taddr) {
371 free((void *) taddr->buf);
372 free((void *) taddr);
373 }
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;
378 }
379 return (void *)taddr;
380 }
381
382 /*
383 * Convert taddr to uaddr. Should be used only by
384 * local servers/clients. (kernel level stuff only)
385 */
386 /* ARGSUSED */
387 void *
388 rpcbproc_taddr2uaddr_com(void *arg, struct svc_req *rqstp, SVCXPRT *transp,
389 rpcvers_t rpcbversnum)
390 {
391 struct netbuf *taddr = (struct netbuf *)arg;
392 static char *uaddr;
393 struct netconfig *nconf;
394
395 #ifdef CHEW_FDS
396 int fd;
397
398 if ((fd = open("/dev/null", O_RDONLY)) == -1) {
399 uaddr = (char *)strerror(errno);
400 return (&uaddr);
401 }
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)) {
407 uaddr = nullstring;
408 }
409 return (void *)&uaddr;
410 }
411
412
413 static bool_t
414 xdr_encap_parms(XDR *xdrs, struct encap_parms *epp)
415 {
416 return (xdr_bytes(xdrs, &(epp->args), (u_int *) &(epp->arglen), ~0));
417 }
418
419 /*
420 * XDR remote call arguments. It ignores the address part.
421 * written for XDR_DECODE direction only
422 */
423 static bool_t
424 xdr_rmtcall_args(XDR *xdrs, struct r_rmtcall_args *cap)
425 {
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)));
431 }
432 return (FALSE);
433 }
434
435 /*
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.
439 */
440 static bool_t
441 xdr_rmtcall_result(XDR *xdrs, struct r_rmtcall_args *cap)
442 {
443 bool_t result;
444
445 #ifdef PORTMAP
446 if (cap->rmt_localvers == PMAPVERS) {
447 int h1, h2, h3, h4, p1, p2;
448 u_long port;
449
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)
453 return (FALSE);
454 port = ((p1 & 0xff) << 8) + (p2 & 0xff);
455 result = xdr_u_long(xdrs, &port);
456 } else
457 #endif
458 if ((cap->rmt_localvers == RPCBVERS) ||
459 (cap->rmt_localvers == RPCBVERS4)) {
460 result = xdr_wrapstring(xdrs, &(cap->rmt_uaddr));
461 } else {
462 return (FALSE);
463 }
464 if (result == TRUE)
465 return (xdr_encap_parms(xdrs, &(cap->rmt_args)));
466 return (FALSE);
467 }
468
469 /*
470 * only worries about the struct encap_parms part of struct r_rmtcall_args.
471 * The arglen must already be set!!
472 */
473 static bool_t
474 xdr_opaque_parms(XDR *xdrs, struct r_rmtcall_args *cap)
475 {
476 return (xdr_opaque(xdrs, cap->rmt_args.args, cap->rmt_args.arglen));
477 }
478
479 static struct rmtcallfd_list *rmthead;
480 static struct rmtcallfd_list *rmttail;
481
482 int
483 create_rmtcall_fd(struct netconfig *nconf)
484 {
485 int fd;
486 struct rmtcallfd_list *rmt;
487 SVCXPRT *xprt;
488
489 if ((fd = __rpc_nconf2fd(nconf)) == -1) {
490 if (debugging)
491 fprintf(stderr,
492 "create_rmtcall_fd: couldn't open \"%s\" (errno %d)\n",
493 nconf->nc_device, errno);
494 return (-1);
495 }
496 xprt = svc_tli_create(fd, 0, (struct t_bind *) 0, 0, 0);
497 if (xprt == NULL) {
498 if (debugging)
499 fprintf(stderr,
500 "create_rmtcall_fd: svc_tli_create failed\n");
501 return (-1);
502 }
503 rmt = (struct rmtcallfd_list *)malloc((u_int)
504 sizeof (struct rmtcallfd_list));
505 if (rmt == NULL) {
506 syslog(LOG_ERR, "create_rmtcall_fd: no memory!");
507 return (-1);
508 }
509 rmt->xprt = xprt;
510 rmt->netid = strdup(nconf->nc_netid);
511 xprt->xp_netid = rmt->netid;
512 rmt->fd = fd;
513 rmt->next = NULL;
514 if (rmthead == NULL) {
515 rmthead = rmt;
516 rmttail = rmt;
517 } else {
518 rmttail->next = rmt;
519 rmttail = rmt;
520 }
521 /* XXX not threadsafe */
522 if (fd > svc_maxfd)
523 svc_maxfd = fd;
524 FD_SET(fd, &svc_fdset);
525 return (fd);
526 }
527
528 static int
529 find_rmtcallfd_by_netid(char *netid)
530 {
531 struct rmtcallfd_list *rmt;
532
533 for (rmt = rmthead; rmt != NULL; rmt = rmt->next) {
534 if (strcmp(netid, rmt->netid) == 0) {
535 return (rmt->fd);
536 }
537 }
538 return (-1);
539 }
540
541 static SVCXPRT *
542 find_rmtcallxprt_by_fd(int fd)
543 {
544 struct rmtcallfd_list *rmt;
545
546 for (rmt = rmthead; rmt != NULL; rmt = rmt->next) {
547 if (fd == rmt->fd) {
548 return (rmt->xprt);
549 }
550 }
551 return (NULL);
552 }
553
554
555 /*
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
561 *
562 * receive the request
563 *
564 * open a new TLI endpoint on the same transport on which we received
565 * the original request
566 *
567 * remember the original request's XID (which requires knowing the format
568 * of the svc_dg_data structure)
569 *
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.
575 *
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
578 *
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
583 *
584 * receive the reply
585 *
586 * bundle the reply, along with the service's universal address
587 *
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.
591 */
592
593 void
594 rpcbproc_callit_com(struct svc_req *rqstp, SVCXPRT *transp,
595 rpcproc_t reply_type, rpcvers_t versnum)
596 {
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;
606 int outlen;
607 u_int sendsz;
608 XDR outxdr;
609 AUTH *auth;
610 int fd = -1;
611 char *uaddr, *m_uaddr, *local_uaddr;
612 u_int32_t *xidp;
613 struct __rpc_sockinfo si;
614 struct sockaddr *localsa;
615 struct netbuf tbuf;
616
617 if (!__rpc_fd2sockinfo(transp->xp_fd, &si)) {
618 if (reply_type == RPCBPROC_INDIRECT)
619 svcerr_systemerr(transp);
620 return;
621 }
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);
628 return;
629 }
630 /*
631 * Should be multiple of 4 for XDR.
632 */
633 sendsz = ((sendsz + 3) / 4) * 4;
634 if (sendsz > RPC_BUF_MAX) {
635 #ifdef notyet
636 buf_alloc = alloca(sendsz); /* not in IDR2? */
637 #else
638 buf_alloc = malloc(sendsz);
639 #endif /* notyet */
640 if (buf_alloc == NULL) {
641 if (debugging)
642 fprintf(stderr,
643 "rpcbproc_callit_com: No Memory!\n");
644 if (reply_type == RPCBPROC_INDIRECT)
645 svcerr_systemerr(transp);
646 return;
647 }
648 a.rmt_args.args = buf_alloc;
649 } else {
650 a.rmt_args.args = buf;
651 }
652
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);
657 if (debugging)
658 fprintf(stderr,
659 "rpcbproc_callit_com: svc_getargs failed\n");
660 goto error;
661 }
662
663 if (!check_callit(transp, &a, versnum)) {
664 svcerr_weakauth(transp);
665 goto error;
666 }
667
668 caller = svc_getrpccaller(transp);
669 #ifdef RPCBIND_DEBUG
670 if (debugging) {
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" : "unknown",
676 reply_type == RPCBPROC_INDIRECT ? "indirect" : "callit",
677 (unsigned long)a.rmt_prog, (unsigned long)a.rmt_vers,
678 (unsigned long)a.rmt_proc, transp->xp_netid,
679 uaddr ? uaddr : "unknown");
680 if (uaddr)
681 free((void *) uaddr);
682 }
683 #endif
684
685 rbl = find_service(a.rmt_prog, a.rmt_vers, transp->xp_netid);
686
687 rpcbs_rmtcall(versnum - 2, reply_type, a.rmt_prog, a.rmt_vers,
688 a.rmt_proc, transp->xp_netid, rbl);
689
690 if (rbl == (rpcblist_ptr)NULL) {
691 #ifdef RPCBIND_DEBUG
692 if (debugging)
693 fprintf(stderr, "not found\n");
694 #endif
695 if (reply_type == RPCBPROC_INDIRECT)
696 svcerr_noprog(transp);
697 goto error;
698 }
699 if (rbl->rpcb_map.r_vers != a.rmt_vers) {
700 if (reply_type == RPCBPROC_INDIRECT) {
701 rpcvers_t vers_low, vers_high;
702
703 find_versions(a.rmt_prog, transp->xp_netid,
704 &vers_low, &vers_high);
705 svcerr_progvers(transp, vers_low, vers_high);
706 }
707 goto error;
708 }
709
710 #ifdef RPCBIND_DEBUG
711 if (debugging)
712 fprintf(stderr, "found at uaddr %s\n", rbl->rpcb_map.r_addr);
713 #endif
714 /*
715 * Check whether this entry is valid and a server is present
716 * Mergeaddr() returns NULL if no such entry is present, and
717 * returns "" if the entry was present but the server is not
718 * present (i.e., it crashed).
719 */
720 if (reply_type == RPCBPROC_INDIRECT) {
721 uaddr = mergeaddr(transp, transp->xp_netid,
722 rbl->rpcb_map.r_addr, NULL);
723 if ((uaddr == (char *) NULL) || uaddr[0] == '\0') {
724 svcerr_noprog(transp);
725 if (uaddr != NULL) {
726 free((void *) uaddr);
727 }
728 goto error;
729 }
730 if (uaddr != NULL) {
731 free((void *) uaddr);
732 }
733 }
734 nconf = rpcbind_get_conf(transp->xp_netid);
735 if (nconf == (struct netconfig *)NULL) {
736 if (reply_type == RPCBPROC_INDIRECT)
737 svcerr_systemerr(transp);
738 if (debugging)
739 fprintf(stderr,
740 "rpcbproc_callit_com: rpcbind_get_conf failed\n");
741 goto error;
742 }
743 localsa = local_sa(((struct sockaddr *)caller->buf)->sa_family);
744 if (localsa == NULL) {
745 if (debugging)
746 fprintf(stderr,
747 "rpcbproc_callit_com: no local address\n");
748 goto error;
749 }
750 tbuf.len = tbuf.maxlen = localsa->sa_len;
751 tbuf.buf = localsa;
752 local_uaddr =
753 addrmerge(&tbuf, rbl->rpcb_map.r_addr, NULL, nconf->nc_netid);
754 m_uaddr = addrmerge(caller, rbl->rpcb_map.r_addr, NULL,
755 nconf->nc_netid);
756 #ifdef RPCBIND_DEBUG
757 if (debugging)
758 fprintf(stderr, "merged uaddr %s\n", m_uaddr);
759 #endif
760 if ((fd = find_rmtcallfd_by_netid(nconf->nc_netid)) == -1) {
761 if (reply_type == RPCBPROC_INDIRECT)
762 svcerr_systemerr(transp);
763 free((void *) m_uaddr);
764 goto error;
765 }
766 xidp = __rpcb_get_dg_xidp(transp);
767 call_msg.rm_xid = forward_register(*xidp,
768 caller, fd, m_uaddr, reply_type, versnum);
769 if (call_msg.rm_xid == 0) {
770 /*
771 * A duplicate request for the slow server. Let's not
772 * beat on it any more.
773 */
774 if (debugging)
775 fprintf(stderr,
776 "rpcbproc_callit_com: duplicate request\n");
777 free((void *) m_uaddr);
778 goto error;
779 } else if (call_msg.rm_xid == -1) {
780 /* forward_register failed. Perhaps no memory. */
781 if (debugging)
782 fprintf(stderr,
783 "rpcbproc_callit_com: forward_register failed\n");
784 free((void *) m_uaddr);
785 goto error;
786 }
787
788 #ifdef DEBUG_RMTCALL
789 if (debugging)
790 fprintf(stderr,
791 "rpcbproc_callit_com: original XID %x, new XID %x\n",
792 *xidp, call_msg.rm_xid);
793 #endif
794 call_msg.rm_direction = CALL;
795 call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
796 call_msg.rm_call.cb_prog = a.rmt_prog;
797 call_msg.rm_call.cb_vers = a.rmt_vers;
798 if (sendsz > RPC_BUF_MAX) {
799 #ifdef notyet
800 outbuf_alloc = alloca(sendsz); /* not in IDR2? */
801 #else
802 outbuf_alloc = malloc(sendsz);
803 #endif /* notyet */
804 if (outbuf_alloc == NULL) {
805 if (reply_type == RPCBPROC_INDIRECT)
806 svcerr_systemerr(transp);
807 if (debugging)
808 fprintf(stderr,
809 "rpcbproc_callit_com: No memory!\n");
810 goto error;
811 }
812 xdrmem_create(&outxdr, outbuf_alloc, sendsz, XDR_ENCODE);
813 } else {
814 xdrmem_create(&outxdr, outbuf, sendsz, XDR_ENCODE);
815 }
816 if (!xdr_callhdr(&outxdr, &call_msg)) {
817 if (reply_type == RPCBPROC_INDIRECT)
818 svcerr_systemerr(transp);
819 if (debugging)
820 fprintf(stderr,
821 "rpcbproc_callit_com: xdr_callhdr failed\n");
822 goto error;
823 }
824 if (!xdr_u_int32_t(&outxdr, &(a.rmt_proc))) {
825 if (reply_type == RPCBPROC_INDIRECT)
826 svcerr_systemerr(transp);
827 if (debugging)
828 fprintf(stderr,
829 "rpcbproc_callit_com: xdr_u_long failed\n");
830 goto error;
831 }
832
833 if (rqstp->rq_cred.oa_flavor == AUTH_NULL) {
834 auth = authnone_create();
835 } else if (rqstp->rq_cred.oa_flavor == AUTH_SYS) {
836 struct authunix_parms *au;
837
838 au = (struct authunix_parms *)rqstp->rq_clntcred;
839 auth = authunix_create(au->aup_machname,
840 au->aup_uid, au->aup_gid,
841 au->aup_len, au->aup_gids);
842 if (auth == NULL) /* fall back */
843 auth = authnone_create();
844 } else {
845 /* we do not support any other authentication scheme */
846 if (debugging)
847 fprintf(stderr,
848 "rpcbproc_callit_com: oa_flavor != AUTH_NONE and oa_flavor != AUTH_SYS\n");
849 if (reply_type == RPCBPROC_INDIRECT)
850 svcerr_weakauth(transp); /* XXX too strong.. */
851 goto error;
852 }
853 if (auth == NULL) {
854 if (reply_type == RPCBPROC_INDIRECT)
855 svcerr_systemerr(transp);
856 if (debugging)
857 fprintf(stderr,
858 "rpcbproc_callit_com: authwhatever_create returned NULL\n");
859 goto error;
860 }
861 if (!AUTH_MARSHALL(auth, &outxdr)) {
862 if (reply_type == RPCBPROC_INDIRECT)
863 svcerr_systemerr(transp);
864 AUTH_DESTROY(auth);
865 if (debugging)
866 fprintf(stderr,
867 "rpcbproc_callit_com: AUTH_MARSHALL failed\n");
868 goto error;
869 }
870 AUTH_DESTROY(auth);
871 if (!xdr_opaque_parms(&outxdr, &a)) {
872 if (reply_type == RPCBPROC_INDIRECT)
873 svcerr_systemerr(transp);
874 if (debugging)
875 fprintf(stderr,
876 "rpcbproc_callit_com: xdr_opaque_parms failed\n");
877 goto error;
878 }
879 outlen = (int) XDR_GETPOS(&outxdr);
880 if (outbuf_alloc)
881 outbufp = outbuf_alloc;
882 else
883 outbufp = outbuf;
884
885 na = uaddr2taddr(nconf, local_uaddr);
886 free(local_uaddr);
887 if (!na) {
888 if (reply_type == RPCBPROC_INDIRECT)
889 svcerr_systemerr(transp);
890 goto error;
891 }
892
893 if (sendto(fd, outbufp, outlen, 0, (struct sockaddr *)na->buf, na->len)
894 != outlen) {
895 if (debugging)
896 fprintf(stderr,
897 "rpcbproc_callit_com: sendto failed: errno %d\n", errno);
898 if (reply_type == RPCBPROC_INDIRECT)
899 svcerr_systemerr(transp);
900 goto error;
901 }
902 goto out;
903
904 error:
905 if (call_msg.rm_xid != 0)
906 (void) free_slot_by_xid(call_msg.rm_xid);
907 out:
908 if (buf_alloc)
909 free((void *) buf_alloc);
910 if (outbuf_alloc)
911 free((void *) outbuf_alloc);
912 if (na) {
913 free(na->buf);
914 free(na);
915 }
916 }
917
918 /*
919 * Makes an entry into the FIFO for the given request.
920 * If duplicate request, returns a 0, else returns the xid of its call.
921 */
922 static u_int32_t
923 forward_register(u_int32_t caller_xid, struct netbuf *caller_addr,
924 int forward_fd, char *uaddr, rpcproc_t reply_type,
925 rpcvers_t versnum)
926 {
927 int i;
928 int j = 0;
929 time_t min_time, time_now;
930 static u_int32_t lastxid;
931 int entry = -1;
932
933 min_time = FINFO[0].time;
934 time_now = time((time_t *)0);
935 /* initialization */
936 if (lastxid == 0)
937 lastxid = time_now * NFORWARD;
938
939 /*
940 * Check if it is an duplicate entry. Then,
941 * try to find an empty slot. If not available, then
942 * use the slot with the earliest time.
943 */
944 for (i = 0; i < NFORWARD; i++) {
945 if (FINFO[i].flag & FINFO_ACTIVE) {
946 if ((FINFO[i].caller_xid == caller_xid) &&
947 (FINFO[i].reply_type == reply_type) &&
948 (FINFO[i].versnum == versnum) &&
949 (!netbufcmp(FINFO[i].caller_addr,
950 caller_addr))) {
951 FINFO[i].time = time((time_t *)0);
952 return (0); /* Duplicate entry */
953 } else {
954 /* Should we wait any longer */
955 if ((time_now - FINFO[i].time) > MAXTIME_OFF)
956 (void) free_slot_by_index(i);
957 }
958 }
959 if (entry == -1) {
960 if ((FINFO[i].flag & FINFO_ACTIVE) == 0) {
961 entry = i;
962 } else if (FINFO[i].time < min_time) {
963 j = i;
964 min_time = FINFO[i].time;
965 }
966 }
967 }
968 if (entry != -1) {
969 /* use this empty slot */
970 j = entry;
971 } else {
972 (void) free_slot_by_index(j);
973 }
974 if ((FINFO[j].caller_addr = netbufdup(caller_addr)) == NULL) {
975 return (-1);
976 }
977 rpcb_rmtcalls++; /* no of pending calls */
978 FINFO[j].flag = FINFO_ACTIVE;
979 FINFO[j].reply_type = reply_type;
980 FINFO[j].versnum = versnum;
981 FINFO[j].time = time_now;
982 FINFO[j].caller_xid = caller_xid;
983 FINFO[j].forward_fd = forward_fd;
984 /*
985 * Though uaddr is not allocated here, it will still be freed
986 * from free_slot_*().
987 */
988 FINFO[j].uaddr = uaddr;
989 lastxid = lastxid + NFORWARD;
990 FINFO[j].forward_xid = lastxid + j; /* encode slot */
991 return (FINFO[j].forward_xid); /* forward on this xid */
992 }
993
994 static struct finfo *
995 forward_find(u_int32_t reply_xid)
996 {
997 int i;
998
999 i = reply_xid % NFORWARD;
1000 if (i < 0)
1001 i += NFORWARD;
1002 if ((FINFO[i].flag & FINFO_ACTIVE) &&
1003 (FINFO[i].forward_xid == reply_xid)) {
1004 return (&FINFO[i]);
1005 }
1006 return (NULL);
1007 }
1008
1009 static int
1010 free_slot_by_xid(u_int32_t xid)
1011 {
1012 int entry;
1013
1014 entry = xid % NFORWARD;
1015 if (entry < 0)
1016 entry += NFORWARD;
1017 return (free_slot_by_index(entry));
1018 }
1019
1020 static int
1021 free_slot_by_index(int index)
1022 {
1023 struct finfo *fi;
1024
1025 fi = &FINFO[index];
1026 if (fi->flag & FINFO_ACTIVE) {
1027 netbuffree(fi->caller_addr);
1028 /* XXX may be too big, but can't access xprt array here */
1029 if (fi->forward_fd >= svc_maxfd)
1030 svc_maxfd--;
1031 free((void *) fi->uaddr);
1032 fi->flag &= ~FINFO_ACTIVE;
1033 rpcb_rmtcalls--;
1034 return (1);
1035 }
1036 return (0);
1037 }
1038
1039 static int
1040 netbufcmp(struct netbuf *n1, struct netbuf *n2)
1041 {
1042 return ((n1->len != n2->len) || memcmp(n1->buf, n2->buf, n1->len));
1043 }
1044
1045 static struct netbuf *
1046 netbufdup(struct netbuf *ap)
1047 {
1048 struct netbuf *np;
1049
1050 np = (struct netbuf *) malloc(sizeof (struct netbuf) + ap->len);
1051 if (np) {
1052 np->maxlen = np->len = ap->len;
1053 np->buf = ((char *) np) + sizeof (struct netbuf);
1054 (void) memcpy(np->buf, ap->buf, ap->len);
1055 }
1056 return (np);
1057 }
1058
1059 static void
1060 netbuffree(struct netbuf *ap)
1061 {
1062 free((void *) ap);
1063 }
1064
1065
1066 #define MASKVAL (POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND)
1067
1068 void
1069 my_svc_run()
1070 {
1071 size_t nfds;
1072 struct pollfd pollfds[FD_SETSIZE];
1073 int poll_ret, check_ret;
1074 int n;
1075 #ifdef SVC_RUN_DEBUG
1076 int i;
1077 #endif
1078 register struct pollfd *p;
1079
1080 for (;;) {
1081 p = pollfds;
1082 for (n = 0; n <= svc_maxfd; n++) {
1083 if (FD_ISSET(n, &svc_fdset)) {
1084 p->fd = n;
1085 p->events = MASKVAL;
1086 p++;
1087 }
1088 }
1089 nfds = p - pollfds;
1090 poll_ret = 0;
1091 #ifdef SVC_RUN_DEBUG
1092 if (debugging) {
1093 fprintf(stderr, "polling for read on fd < ");
1094 for (i = 0, p = pollfds; i < nfds; i++, p++)
1095 if (p->events)
1096 fprintf(stderr, "%d ", p->fd);
1097 fprintf(stderr, ">\n");
1098 }
1099 #endif
1100 switch (poll_ret = poll(pollfds, nfds, INFTIM)) {
1101 case -1:
1102 /*
1103 * We ignore all errors, continuing with the assumption
1104 * that it was set by the signal handlers (or any
1105 * other outside event) and not caused by poll().
1106 */
1107 case 0:
1108 continue;
1109 default:
1110 #ifdef SVC_RUN_DEBUG
1111 if (debugging) {
1112 fprintf(stderr, "poll returned read fds < ");
1113 for (i = 0, p = pollfds; i < nfds; i++, p++)
1114 if (p->revents)
1115 fprintf(stderr, "%d ", p->fd);
1116 fprintf(stderr, ">\n");
1117 }
1118 #endif
1119 /*
1120 * If we found as many replies on callback fds
1121 * as the number of descriptors selectable which
1122 * poll() returned, there can be no more so we
1123 * don't call svc_getreq_poll. Otherwise, there
1124 * must be another so we must call svc_getreq_poll.
1125 */
1126 if ((check_ret = check_rmtcalls(pollfds, nfds)) ==
1127 poll_ret)
1128 continue;
1129 svc_getreq_poll(pollfds, poll_ret-check_ret);
1130 }
1131 #ifdef SVC_RUN_DEBUG
1132 if (debugging) {
1133 fprintf(stderr, "svc_maxfd now %u\n", svc_maxfd);
1134 }
1135 #endif
1136 }
1137 }
1138
1139 static int
1140 check_rmtcalls(struct pollfd *pfds, int nfds)
1141 {
1142 int j, ncallbacks_found = 0, rmtcalls_pending;
1143 SVCXPRT *xprt;
1144
1145 if (rpcb_rmtcalls == 0)
1146 return (0);
1147
1148 rmtcalls_pending = rpcb_rmtcalls;
1149 for (j = 0; j < nfds; j++) {
1150 if ((xprt = find_rmtcallxprt_by_fd(pfds[j].fd)) != NULL) {
1151 if (pfds[j].revents) {
1152 ncallbacks_found++;
1153 #ifdef DEBUG_RMTCALL
1154 if (debugging)
1155 fprintf(stderr,
1156 "my_svc_run: polled on forwarding fd %d, netid %s - calling handle_reply\n",
1157 pfds[j].fd, xprt->xp_netid);
1158 #endif
1159 handle_reply(pfds[j].fd, xprt);
1160 pfds[j].revents = 0;
1161 if (ncallbacks_found >= rmtcalls_pending) {
1162 break;
1163 }
1164 }
1165 }
1166 }
1167 return (ncallbacks_found);
1168 }
1169
1170 static void
1171 xprt_set_caller(SVCXPRT *xprt, struct finfo *fi)
1172 {
1173 u_int32_t *xidp;
1174
1175 *(svc_getrpccaller(xprt)) = *(fi->caller_addr);
1176 xidp = __rpcb_get_dg_xidp(xprt);
1177 *xidp = fi->caller_xid;
1178 }
1179
1180 /*
1181 * Call svcerr_systemerr() only if RPCBVERS4
1182 */
1183 static void
1184 send_svcsyserr(SVCXPRT *xprt, struct finfo *fi)
1185 {
1186 if (fi->reply_type == RPCBPROC_INDIRECT) {
1187 xprt_set_caller(xprt, fi);
1188 svcerr_systemerr(xprt);
1189 }
1190 return;
1191 }
1192
1193 static void
1194 handle_reply(int fd, SVCXPRT *xprt)
1195 {
1196 XDR reply_xdrs;
1197 struct rpc_msg reply_msg;
1198 struct rpc_err reply_error;
1199 char *buffer;
1200 struct finfo *fi;
1201 int inlen, pos, len, res;
1202 struct r_rmtcall_args a;
1203 struct sockaddr_storage ss;
1204 socklen_t fromlen;
1205 #ifdef SVC_RUN_DEBUG
1206 char *uaddr;
1207 #endif
1208
1209 buffer = malloc(RPC_BUF_MAX);
1210 if (buffer == NULL)
1211 goto done;
1212
1213 do {
1214 inlen = recvfrom(fd, buffer, RPC_BUF_MAX, 0,
1215 (struct sockaddr *)&ss, &fromlen);
1216 } while (inlen < 0 && errno == EINTR);
1217 if (res < 0) {
1218 if (debugging)
1219 fprintf(stderr,
1220 "handle_reply: recvfrom returned %d, errno %d\n", res, errno);
1221 goto done;
1222 }
1223
1224 reply_msg.acpted_rply.ar_verf = _null_auth;
1225 reply_msg.acpted_rply.ar_results.where = 0;
1226 reply_msg.acpted_rply.ar_results.proc = (xdrproc_t) xdr_void;
1227
1228 xdrmem_create(&reply_xdrs, buffer, (u_int)inlen, XDR_DECODE);
1229 if (!xdr_replymsg(&reply_xdrs, &reply_msg)) {
1230 if (debugging)
1231 (void) fprintf(stderr,
1232 "handle_reply: xdr_replymsg failed\n");
1233 goto done;
1234 }
1235 fi = forward_find(reply_msg.rm_xid);
1236 #ifdef SVC_RUN_DEBUG
1237 if (debugging) {
1238 fprintf(stderr, "handle_reply: reply xid: %d fi addr: %p\n",
1239 reply_msg.rm_xid, fi);
1240 }
1241 #endif
1242 if (fi == NULL) {
1243 goto done;
1244 }
1245 _seterr_reply(&reply_msg, &reply_error);
1246 if (reply_error.re_status != RPC_SUCCESS) {
1247 if (debugging)
1248 (void) fprintf(stderr, "handle_reply: %s\n",
1249 clnt_sperrno(reply_error.re_status));
1250 send_svcsyserr(xprt, fi);
1251 goto done;
1252 }
1253 pos = XDR_GETPOS(&reply_xdrs);
1254 len = inlen - pos;
1255 a.rmt_args.args = &buffer[pos];
1256 a.rmt_args.arglen = len;
1257 a.rmt_uaddr = fi->uaddr;
1258 a.rmt_localvers = fi->versnum;
1259
1260 xprt_set_caller(xprt, fi);
1261 #ifdef SVC_RUN_DEBUG
1262 uaddr = taddr2uaddr(rpcbind_get_conf("udp"),
1263 svc_getrpccaller(xprt));
1264 if (debugging) {
1265 fprintf(stderr, "handle_reply: forwarding address %s to %s\n",
1266 a.rmt_uaddr, uaddr ? uaddr : "unknown");
1267 }
1268 if (uaddr)
1269 free((void *) uaddr);
1270 if (buffer)
1271 free(buffer);
1272 #endif
1273 svc_sendreply(xprt, (xdrproc_t) xdr_rmtcall_result, (char *) &a);
1274 done:
1275 if (reply_msg.rm_xid == 0) {
1276 #ifdef SVC_RUN_DEBUG
1277 if (debugging) {
1278 fprintf(stderr, "handle_reply: NULL xid on exit!\n");
1279 }
1280 #endif
1281 } else
1282 (void) free_slot_by_xid(reply_msg.rm_xid);
1283 return;
1284 }
1285
1286 static void
1287 find_versions(rpcprog_t prog, char *netid, rpcvers_t *lowvp, rpcvers_t *highvp)
1288 {
1289 register rpcblist_ptr rbl;
1290 int lowv = 0;
1291 int highv = 0;
1292
1293 for (rbl = list_rbl; rbl != NULL; rbl = rbl->rpcb_next) {
1294 if ((rbl->rpcb_map.r_prog != prog) ||
1295 ((rbl->rpcb_map.r_netid != NULL) &&
1296 (strcasecmp(rbl->rpcb_map.r_netid, netid) != 0)))
1297 continue;
1298 if (lowv == 0) {
1299 highv = rbl->rpcb_map.r_vers;
1300 lowv = highv;
1301 } else if (rbl->rpcb_map.r_vers < lowv) {
1302 lowv = rbl->rpcb_map.r_vers;
1303 } else if (rbl->rpcb_map.r_vers > highv) {
1304 highv = rbl->rpcb_map.r_vers;
1305 }
1306 }
1307 *lowvp = lowv;
1308 *highvp = highv;
1309 return;
1310 }
1311
1312 /*
1313 * returns the item with the given program, version number and netid.
1314 * If that version number is not found, it returns the item with that
1315 * program number, so that address is now returned to the caller. The
1316 * caller when makes a call to this program, version number, the call
1317 * will fail and it will return with PROGVERS_MISMATCH. The user can
1318 * then determine the highest and the lowest version number for this
1319 * program using clnt_geterr() and use those program version numbers.
1320 *
1321 * Returns the RPCBLIST for the given prog, vers and netid
1322 */
1323 static rpcblist_ptr
1324 find_service(rpcprog_t prog, rpcvers_t vers, char *netid)
1325 {
1326 register rpcblist_ptr hit = NULL;
1327 register rpcblist_ptr rbl;
1328
1329 for (rbl = list_rbl; rbl != NULL; rbl = rbl->rpcb_next) {
1330 if ((rbl->rpcb_map.r_prog != prog) ||
1331 ((rbl->rpcb_map.r_netid != NULL) &&
1332 (strcasecmp(rbl->rpcb_map.r_netid, netid) != 0)))
1333 continue;
1334 hit = rbl;
1335 if (rbl->rpcb_map.r_vers == vers)
1336 break;
1337 }
1338 return (hit);
1339 }
1340
1341 /*
1342 * Copies the name associated with the uid of the caller and returns
1343 * a pointer to it. Similar to getwd().
1344 */
1345 static char *
1346 getowner(SVCXPRT *transp, char *owner, size_t ownersize)
1347 {
1348 struct sockcred *sc;
1349
1350 sc = __svc_getcallercreds(transp);
1351 if (sc == NULL)
1352 strlcpy(owner, "unknown", ownersize);
1353 else if (sc->sc_uid == 0)
1354 strlcpy(owner, "superuser", ownersize);
1355 else
1356 snprintf(owner, ownersize, "%d", sc->sc_uid);
1357
1358 return owner;
1359 }
1360
1361 #ifdef PORTMAP
1362 /*
1363 * Add this to the pmap list only if it is UDP or TCP.
1364 */
1365 static int
1366 add_pmaplist(RPCB *arg)
1367 {
1368 struct pmap pmap;
1369 struct pmaplist *pml;
1370 int h1, h2, h3, h4, p1, p2;
1371
1372 if (strcmp(arg->r_netid, udptrans) == 0) {
1373 /* It is UDP! */
1374 pmap.pm_prot = IPPROTO_UDP;
1375 } else if (strcmp(arg->r_netid, tcptrans) == 0) {
1376 /* It is TCP */
1377 pmap.pm_prot = IPPROTO_TCP;
1378 } else
1379 /* Not a IP protocol */
1380 return (0);
1381
1382 /* interpret the universal address for TCP/IP */
1383 if (sscanf(arg->r_addr, "%d.%d.%d.%d.%d.%d",
1384 &h1, &h2, &h3, &h4, &p1, &p2) != 6)
1385 return (0);
1386 pmap.pm_port = ((p1 & 0xff) << 8) + (p2 & 0xff);
1387 pmap.pm_prog = arg->r_prog;
1388 pmap.pm_vers = arg->r_vers;
1389 /*
1390 * add to END of list
1391 */
1392 pml = (struct pmaplist *) malloc((u_int)sizeof (struct pmaplist));
1393 if (pml == NULL) {
1394 (void) syslog(LOG_ERR, "rpcbind: no memory!\n");
1395 return (1);
1396 }
1397 pml->pml_map = pmap;
1398 pml->pml_next = NULL;
1399 if (list_pml == NULL) {
1400 list_pml = pml;
1401 } else {
1402 struct pmaplist *fnd;
1403
1404 /* Attach to the end of the list */
1405 for (fnd = list_pml; fnd->pml_next; fnd = fnd->pml_next)
1406 ;
1407 fnd->pml_next = pml;
1408 }
1409 return (0);
1410 }
1411
1412 /*
1413 * Delete this from the pmap list only if it is UDP or TCP.
1414 */
1415 static int
1416 del_pmaplist(RPCB *arg)
1417 {
1418 struct pmaplist *pml;
1419 struct pmaplist *prevpml, *fnd;
1420 long prot;
1421
1422 if (strcmp(arg->r_netid, udptrans) == 0) {
1423 /* It is UDP! */
1424 prot = IPPROTO_UDP;
1425 } else if (strcmp(arg->r_netid, tcptrans) == 0) {
1426 /* It is TCP */
1427 prot = IPPROTO_TCP;
1428 } else if (arg->r_netid[0] == NULL) {
1429 prot = 0; /* Remove all occurrences */
1430 } else {
1431 /* Not a IP protocol */
1432 return (0);
1433 }
1434 for (prevpml = NULL, pml = list_pml; pml; /* cstyle */) {
1435 if ((pml->pml_map.pm_prog != arg->r_prog) ||
1436 (pml->pml_map.pm_vers != arg->r_vers) ||
1437 (prot && (pml->pml_map.pm_prot != prot))) {
1438 /* both pml & prevpml move forwards */
1439 prevpml = pml;
1440 pml = pml->pml_next;
1441 continue;
1442 }
1443 /* found it; pml moves forward, prevpml stays */
1444 fnd = pml;
1445 pml = pml->pml_next;
1446 if (prevpml == NULL)
1447 list_pml = pml;
1448 else
1449 prevpml->pml_next = pml;
1450 free((void *) fnd);
1451 }
1452 return (0);
1453 }
1454 #endif /* PORTMAP */
1455