2 * $NetBSD: rpcb_svc_4.c,v 1.1 2000/06/02 23:15:41 fvdl Exp $
7 * Copyright (c) 2009, Sun Microsystems, Inc.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions are met:
12 * - Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
14 * - Redistributions in binary form must reproduce the above copyright notice,
15 * this list of conditions and the following disclaimer in the documentation
16 * and/or other materials provided with the distribution.
17 * - Neither the name of Sun Microsystems, Inc. nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
25 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
34 * Copyright (c) 1986 - 1991 by Sun Microsystems, Inc.
37 /* #ident "@(#)rpcb_svc_4.c 1.8 93/07/05 SMI" */
41 * The server procedure for the version 4 rpcbind.
45 #include <sys/types.h>
50 #include <netconfig.h>
56 static void *rpcbproc_getaddr_4_local(void *, struct svc_req *, SVCXPRT *,
58 static void *rpcbproc_getversaddr_4_local(void *, struct svc_req *, SVCXPRT *, rpcvers_t);
59 static void *rpcbproc_getaddrlist_4_local
60 (void *, struct svc_req *, SVCXPRT *, rpcvers_t);
61 static void free_rpcb_entry_list(rpcb_entry_list_ptr *);
62 static void *rpcbproc_dump_4_local(void *, struct svc_req *, SVCXPRT *, rpcvers_t);
65 * Called by svc_getreqset. There is a separate server handle for
66 * every transport that it waits on.
69 rpcb_service_4(struct svc_req *rqstp, SVCXPRT *transp)
72 rpcb rpcbproc_set_4_arg;
73 rpcb rpcbproc_unset_4_arg;
74 rpcb rpcbproc_getaddr_4_local_arg;
75 char *rpcbproc_uaddr2taddr_4_arg;
76 struct netbuf rpcbproc_taddr2uaddr_4_arg;
79 xdrproc_t xdr_argument, xdr_result;
80 void *(*local)(void *, struct svc_req *, SVCXPRT *, rpcvers_t);
82 rpcbs_procinfo(RPCBVERS_4_STAT, rqstp->rq_proc);
84 switch (rqstp->rq_proc) {
91 fprintf(stderr, "RPCBPROC_NULL\n");
93 check_access(transp, rqstp->rq_proc, NULL, RPCBVERS4);
94 (void) svc_sendreply(transp, (xdrproc_t) xdr_void,
100 * Check to see whether the message came from
101 * loopback transports (for security reasons)
103 xdr_argument = (xdrproc_t)xdr_rpcb;
104 xdr_result = (xdrproc_t)xdr_bool;
105 local = rpcbproc_set_com;
110 * Check to see whether the message came from
111 * loopback transports (for security reasons)
113 xdr_argument = (xdrproc_t)xdr_rpcb;
114 xdr_result = (xdrproc_t)xdr_bool;
115 local = rpcbproc_unset_com;
118 case RPCBPROC_GETADDR:
119 xdr_argument = (xdrproc_t)xdr_rpcb;
120 xdr_result = (xdrproc_t)xdr_wrapstring;
121 local = rpcbproc_getaddr_4_local;
124 case RPCBPROC_GETVERSADDR:
127 fprintf(stderr, "RPCBPROC_GETVERSADDR\n");
129 xdr_argument = (xdrproc_t)xdr_rpcb;
130 xdr_result = (xdrproc_t)xdr_wrapstring;
131 local = rpcbproc_getversaddr_4_local;
137 fprintf(stderr, "RPCBPROC_DUMP\n");
139 xdr_argument = (xdrproc_t)xdr_void;
140 xdr_result = (xdrproc_t)xdr_rpcblist_ptr;
141 local = rpcbproc_dump_4_local;
144 case RPCBPROC_INDIRECT:
147 fprintf(stderr, "RPCBPROC_INDIRECT\n");
149 rpcbproc_callit_com(rqstp, transp, rqstp->rq_proc, RPCBVERS4);
152 /* case RPCBPROC_CALLIT: */
156 fprintf(stderr, "RPCBPROC_BCAST\n");
158 rpcbproc_callit_com(rqstp, transp, rqstp->rq_proc, RPCBVERS4);
161 case RPCBPROC_GETTIME:
164 fprintf(stderr, "RPCBPROC_GETTIME\n");
166 xdr_argument = (xdrproc_t)xdr_void;
167 xdr_result = (xdrproc_t)xdr_u_long;
168 local = rpcbproc_gettime_com;
171 case RPCBPROC_UADDR2TADDR:
174 fprintf(stderr, "RPCBPROC_UADDR2TADDR\n");
176 xdr_argument = (xdrproc_t)xdr_wrapstring;
177 xdr_result = (xdrproc_t)xdr_netbuf;
178 local = rpcbproc_uaddr2taddr_com;
181 case RPCBPROC_TADDR2UADDR:
184 fprintf(stderr, "RPCBPROC_TADDR2UADDR\n");
186 xdr_argument = (xdrproc_t)xdr_netbuf;
187 xdr_result = (xdrproc_t)xdr_wrapstring;
188 local = rpcbproc_taddr2uaddr_com;
191 case RPCBPROC_GETADDRLIST:
194 fprintf(stderr, "RPCBPROC_GETADDRLIST\n");
196 xdr_argument = (xdrproc_t)xdr_rpcb;
197 xdr_result = (xdrproc_t)xdr_rpcb_entry_list_ptr;
198 local = rpcbproc_getaddrlist_4_local;
201 case RPCBPROC_GETSTAT:
204 fprintf(stderr, "RPCBPROC_GETSTAT\n");
206 xdr_argument = (xdrproc_t)xdr_void;
207 xdr_result = (xdrproc_t)xdr_rpcb_stat_byvers;
208 local = rpcbproc_getstat;
212 svcerr_noproc(transp);
215 memset((char *)&argument, 0, sizeof (argument));
216 if (!svc_getargs(transp, (xdrproc_t) xdr_argument,
217 (char *)&argument)) {
218 svcerr_decode(transp);
220 (void) fprintf(stderr, "rpcbind: could not decode\n");
223 if (!check_access(transp, rqstp->rq_proc, &argument, RPCBVERS4)) {
224 svcerr_weakauth(transp);
227 result = (*local)(&argument, rqstp, transp, RPCBVERS4);
228 if (result != NULL && !svc_sendreply(transp, (xdrproc_t) xdr_result,
230 svcerr_systemerr(transp);
232 (void) fprintf(stderr, "rpcbind: svc_sendreply\n");
239 if (!svc_freeargs(transp, (xdrproc_t) xdr_argument,
240 (char *)&argument)) {
242 (void) fprintf(stderr, "unable to free arguments\n");
252 * Lookup the mapping for a program, version and return its
253 * address. Assuming that the caller wants the address of the
254 * server running on the transport on which the request came.
255 * Even if a service with a different version number is available,
256 * it will return that address. The client should check with an
257 * clnt_call to verify whether the service is the one that is desired.
258 * We also try to resolve the universal address in terms of
259 * address of the caller.
263 rpcbproc_getaddr_4_local(void *arg, struct svc_req *rqstp, SVCXPRT *transp,
264 rpcvers_t rpcbversnum __unused)
266 RPCB *regp = (RPCB *)arg;
271 uaddr = taddr2uaddr(rpcbind_get_conf(transp->xp_netid),
272 svc_getrpccaller(transp));
273 fprintf(stderr, "RPCB_GETADDR req for (%lu, %lu, %s) from %s: ",
274 (unsigned long)regp->r_prog, (unsigned long)regp->r_vers,
275 regp->r_netid, uaddr);
279 return (rpcbproc_getaddr_com(regp, rqstp, transp, RPCBVERS4,
284 * Lookup the mapping for a program, version and return its
285 * address. Assuming that the caller wants the address of the
286 * server running on the transport on which the request came.
288 * We also try to resolve the universal address in terms of
289 * address of the caller.
293 rpcbproc_getversaddr_4_local(void *arg, struct svc_req *rqstp, SVCXPRT *transp,
294 rpcvers_t versnum __unused)
296 RPCB *regp = (RPCB *)arg;
301 uaddr = taddr2uaddr(rpcbind_get_conf(transp->xp_netid),
302 svc_getrpccaller(transp));
303 fprintf(stderr, "RPCB_GETVERSADDR rqst for (%lu, %lu, %s)"
305 (unsigned long)regp->r_prog, (unsigned long)regp->r_vers,
306 regp->r_netid, uaddr);
310 return (rpcbproc_getaddr_com(regp, rqstp, transp, RPCBVERS4,
315 * Lookup the mapping for a program, version and return the
316 * addresses for all transports in the current transport family.
317 * We return a merged address.
321 rpcbproc_getaddrlist_4_local(void *arg, struct svc_req *rqstp __unused,
322 SVCXPRT *transp, rpcvers_t versnum __unused)
324 RPCB *regp = (RPCB *)arg;
325 static rpcb_entry_list_ptr rlist;
326 register rpcblist_ptr rbl;
327 rpcb_entry_list_ptr rp, tail;
331 struct netconfig *nconf;
332 struct netconfig *reg_nconf;
333 char *saddr, *maddr = NULL;
335 free_rpcb_entry_list(&rlist);
339 reg_nconf = rpcbind_get_conf(transp->xp_netid);
340 if (reg_nconf == NULL)
342 if (*(regp->r_addr) != '\0') {
343 saddr = regp->r_addr;
349 fprintf(stderr, "r_addr: %s r_netid: %s nc_protofmly: %s\n",
350 regp->r_addr, regp->r_netid, reg_nconf->nc_protofmly);
353 for (rbl = list_rbl; rbl != NULL; rbl = rbl->rpcb_next) {
354 if ((rbl->rpcb_map.r_prog == prog) &&
355 (rbl->rpcb_map.r_vers == vers)) {
356 nconf = rpcbind_get_conf(rbl->rpcb_map.r_netid);
359 if (strcmp(nconf->nc_protofmly, reg_nconf->nc_protofmly)
361 continue; /* not same proto family */
365 fprintf(stderr, "\tmerge with: %s\n",
366 rbl->rpcb_map.r_addr);
368 if ((maddr = mergeaddr(transp, rbl->rpcb_map.r_netid,
369 rbl->rpcb_map.r_addr, saddr)) == NULL) {
372 fprintf(stderr, " FAILED\n");
375 } else if (!maddr[0]) {
378 fprintf(stderr, " SUCCEEDED, but port died - maddr: nullstring\n");
380 /* The server died. Unset this combination */
381 delete_prog(regp->r_prog);
386 fprintf(stderr, " SUCCEEDED maddr: %s\n", maddr);
391 rp = malloc(sizeof (rpcb_entry_list));
394 a = &rp->rpcb_entry_map;
396 a->r_nc_netid = nconf->nc_netid;
397 a->r_nc_semantics = nconf->nc_semantics;
398 a->r_nc_protofmly = nconf->nc_protofmly;
399 a->r_nc_proto = nconf->nc_proto;
400 rp->rpcb_entry_next = NULL;
405 tail->rpcb_entry_next = rp;
413 for (rp = rlist; rp; rp = rp->rpcb_entry_next) {
414 fprintf(stderr, "\t%s %s\n", rp->rpcb_entry_map.r_maddr,
415 rp->rpcb_entry_map.r_nc_proto);
420 * XXX: getaddrlist info is also being stuffed into getaddr.
421 * Perhaps wrong, but better than it not getting counted at all.
423 rpcbs_getaddr(RPCBVERS4 - 2, prog, vers, transp->xp_netid, maddr);
424 return (void *)&rlist;
426 fail: free_rpcb_entry_list(&rlist);
431 * Free only the allocated structure, rest is all a pointer to some
432 * other data somewhere else.
435 free_rpcb_entry_list(rpcb_entry_list_ptr *rlistp)
437 register rpcb_entry_list_ptr rbl, tmp;
439 for (rbl = *rlistp; rbl != NULL; ) {
441 rbl = rbl->rpcb_entry_next;
442 free((char *)tmp->rpcb_entry_map.r_maddr);
450 rpcbproc_dump_4_local(void *arg __unused, struct svc_req *req __unused,
451 SVCXPRT *xprt __unused, rpcvers_t versnum __unused)
453 return ((void *)&list_rbl);