1 /* $NetBSD: pmap_svc.c,v 1.2 2000/10/20 11:49:40 fvdl Exp $ */
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.
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.
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.
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.
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.
28 * Sun Microsystems, Inc.
30 * Mountain View, California 94043
33 * Copyright (c) 1984 - 1991 by Sun Microsystems, Inc.
36 /* #ident "@(#)pmap_svc.c 1.14 93/07/05 SMI" */
40 static char sccsid[] = "@(#)pmap_svc.c 1.23 89/04/05 Copyr 1984 Sun Micro";
46 * The server procedure for the version 2 portmaper.
47 * All the portmapper related interface from the portmap side.
51 #include <sys/types.h>
52 #include <sys/socket.h>
55 #include <rpc/pmap_prot.h>
56 #include <rpc/rpcb_prot.h>
62 static struct pmaplist *find_service_pmap __P((rpcprog_t, rpcvers_t,
64 static bool_t pmapproc_change __P((struct svc_req *, SVCXPRT *, u_long));
65 static bool_t pmapproc_getport __P((struct svc_req *, SVCXPRT *));
66 static bool_t pmapproc_dump __P((struct svc_req *, SVCXPRT *));
69 * Called for all the version 2 inquiries.
72 pmap_service(struct svc_req *rqstp, SVCXPRT *xprt)
74 rpcbs_procinfo(RPCBVERS_2_STAT, rqstp->rq_proc);
75 switch (rqstp->rq_proc) {
82 fprintf(stderr, "PMAPPROC_NULL\n");
84 check_access(xprt, rqstp->rq_proc, NULL, PMAPVERS);
85 if ((!svc_sendreply(xprt, (xdrproc_t) xdr_void, NULL)) &&
95 * Set a program, version to port mapping
97 pmapproc_change(rqstp, xprt, rqstp->rq_proc);
102 * Remove a program, version to port mapping.
104 pmapproc_change(rqstp, xprt, rqstp->rq_proc);
107 case PMAPPROC_GETPORT:
109 * Lookup the mapping for a program, version and return its
112 pmapproc_getport(rqstp, xprt);
117 * Return the current set of mapped program, version
121 fprintf(stderr, "PMAPPROC_DUMP\n");
123 pmapproc_dump(rqstp, xprt);
126 case PMAPPROC_CALLIT:
128 * Calls a procedure on the local machine. If the requested
129 * procedure is not registered this procedure does not return
130 * error information!!
131 * This procedure is only supported on rpc/udp and calls via
132 * rpc/udp. It passes null authentication parameters.
134 rpcbproc_callit_com(rqstp, xprt, PMAPPROC_CALLIT, PMAPVERS);
144 * returns the item with the given program, version number. If that version
145 * number is not found, it returns the item with that program number, so that
146 * the port number is now returned to the caller. The caller when makes a
147 * call to this program, version number, the call will fail and it will
148 * return with PROGVERS_MISMATCH. The user can then determine the highest
149 * and the lowest version number for this program using clnt_geterr() and
150 * use those program version numbers.
152 static struct pmaplist *
153 find_service_pmap(rpcprog_t prog, rpcvers_t vers, rpcprot_t prot)
155 register struct pmaplist *hit = NULL;
156 register struct pmaplist *pml;
158 for (pml = list_pml; pml != NULL; pml = pml->pml_next) {
159 if ((pml->pml_map.pm_prog != prog) ||
160 (pml->pml_map.pm_prot != prot))
163 if (pml->pml_map.pm_vers == vers)
170 pmapproc_change(struct svc_req *rqstp __unused, SVCXPRT *xprt, unsigned long op)
175 struct sockaddr_in *who;
181 fprintf(stderr, "%s request for (%lu, %lu) : ",
182 op == PMAPPROC_SET ? "PMAP_SET" : "PMAP_UNSET",
183 reg.pm_prog, reg.pm_vers);
186 if (!svc_getargs(xprt, (xdrproc_t) xdr_pmap, (char *)®)) {
191 if (!check_access(xprt, op, ®, PMAPVERS)) {
192 svcerr_weakauth(xprt);
196 who = svc_getcaller(xprt);
199 * Can't use getpwnam here. We might end up calling ourselves
202 if (__rpc_get_local_uid(xprt, &uid) < 0)
203 rpcbreg.r_owner = "unknown";
205 rpcbreg.r_owner = "superuser";
207 /* r_owner will be strdup-ed later */
208 snprintf(uidbuf, sizeof uidbuf, "%d", uid);
209 rpcbreg.r_owner = uidbuf;
212 rpcbreg.r_prog = reg.pm_prog;
213 rpcbreg.r_vers = reg.pm_vers;
215 if (op == PMAPPROC_SET) {
218 snprintf(buf, sizeof buf, "0.0.0.0.%d.%d",
219 (int)((reg.pm_port >> 8) & 0xff),
220 (int)(reg.pm_port & 0xff));
221 rpcbreg.r_addr = buf;
222 if (reg.pm_prot == IPPROTO_UDP) {
223 rpcbreg.r_netid = udptrans;
224 } else if (reg.pm_prot == IPPROTO_TCP) {
225 rpcbreg.r_netid = tcptrans;
230 ans = map_set(&rpcbreg, rpcbreg.r_owner);
231 } else if (op == PMAPPROC_UNSET) {
234 rpcbreg.r_addr = NULL;
235 rpcbreg.r_netid = tcptrans;
236 ans1 = map_unset(&rpcbreg, rpcbreg.r_owner);
237 rpcbreg.r_netid = udptrans;
238 ans2 = map_unset(&rpcbreg, rpcbreg.r_owner);
244 if ((!svc_sendreply(xprt, (xdrproc_t) xdr_long, (caddr_t) &ans)) &&
246 fprintf(stderr, "portmap: svc_sendreply\n");
253 fprintf(stderr, "%s\n", ans == TRUE ? "succeeded" : "failed");
255 if (op == PMAPPROC_SET)
256 rpcbs_set(RPCBVERS_2_STAT, ans);
258 rpcbs_unset(RPCBVERS_2_STAT, ans);
264 pmapproc_getport(struct svc_req *rqstp __unused, SVCXPRT *xprt)
269 struct pmaplist *fnd;
274 if (!svc_getargs(xprt, (xdrproc_t) xdr_pmap, (char *)®)) {
279 if (!check_access(xprt, PMAPPROC_GETPORT, ®, PMAPVERS)) {
280 svcerr_weakauth(xprt);
286 uaddr = taddr2uaddr(rpcbind_get_conf(xprt->xp_netid),
287 svc_getrpccaller(xprt));
288 fprintf(stderr, "PMAP_GETPORT req for (%lu, %lu, %s) from %s :",
289 reg.pm_prog, reg.pm_vers,
290 reg.pm_prot == IPPROTO_UDP ? "udp" : "tcp", uaddr);
294 fnd = find_service_pmap(reg.pm_prog, reg.pm_vers, reg.pm_prot);
296 char serveuaddr[32], *ua;
297 int h1, h2, h3, h4, p1, p2;
300 if (reg.pm_prot == IPPROTO_UDP) {
304 ua = tcp_uaddr; /* To get the len */
310 if (sscanf(ua, "%d.%d.%d.%d.%d.%d", &h1, &h2, &h3,
311 &h4, &p1, &p2) == 6) {
312 p1 = (fnd->pml_map.pm_port >> 8) & 0xff;
313 p2 = (fnd->pml_map.pm_port) & 0xff;
314 snprintf(serveuaddr, sizeof serveuaddr,
315 "%d.%d.%d.%d.%d.%d", h1, h2, h3, h4, p1, p2);
316 if (is_bound(netid, serveuaddr)) {
317 port = fnd->pml_map.pm_port;
318 } else { /* this service is dead; delete it */
319 delete_prog(reg.pm_prog);
325 if ((!svc_sendreply(xprt, (xdrproc_t) xdr_long, (caddr_t)&lport)) &&
327 (void) fprintf(stderr, "portmap: svc_sendreply\n");
334 fprintf(stderr, "port = %d\n", port);
336 rpcbs_getaddr(RPCBVERS_2_STAT, reg.pm_prog, reg.pm_vers,
337 reg.pm_prot == IPPROTO_UDP ? udptrans : tcptrans,
338 port ? udptrans : "");
345 pmapproc_dump(struct svc_req *rqstp __unused, SVCXPRT *xprt)
347 if (!svc_getargs(xprt, (xdrproc_t)xdr_void, NULL)) {
352 if (!check_access(xprt, PMAPPROC_DUMP, NULL, PMAPVERS)) {
353 svcerr_weakauth(xprt);
357 if ((!svc_sendreply(xprt, (xdrproc_t) xdr_pmaplist_ptr,
358 (caddr_t)&list_pml)) && debugging) {
360 (void) fprintf(stderr, "portmap: svc_sendreply\n");