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