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