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