1 /* $NetBSD: pmap_svc.c,v 1.2 2000/10/20 11:49:40 fvdl Exp $ */
5 * SPDX-License-Identifier: BSD-3-Clause
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) 1984 - 1991 by Sun Microsystems, Inc.
37 /* #ident "@(#)pmap_svc.c 1.14 93/07/05 SMI" */
41 static char sccsid[] = "@(#)pmap_svc.c 1.23 89/04/05 Copyr 1984 Sun Micro";
47 * The server procedure for the version 2 portmaper.
48 * All the portmapper related interface from the portmap side.
52 #include <sys/types.h>
53 #include <sys/socket.h>
56 #include <rpc/pmap_prot.h>
57 #include <rpc/rpcb_prot.h>
63 static struct pmaplist *find_service_pmap(rpcprog_t, rpcvers_t,
65 static bool_t pmapproc_change(struct svc_req *, SVCXPRT *, u_long);
66 static bool_t pmapproc_getport(struct svc_req *, SVCXPRT *);
67 static bool_t pmapproc_dump(struct svc_req *, SVCXPRT *);
70 * Called for all the version 2 inquiries.
73 pmap_service(struct svc_req *rqstp, SVCXPRT *xprt)
75 rpcbs_procinfo(RPCBVERS_2_STAT, rqstp->rq_proc);
76 switch (rqstp->rq_proc) {
83 fprintf(stderr, "PMAPPROC_NULL\n");
85 check_access(xprt, rqstp->rq_proc, NULL, PMAPVERS);
86 if ((!svc_sendreply(xprt, (xdrproc_t) xdr_void, NULL)) &&
96 * Set a program, version to port mapping
98 pmapproc_change(rqstp, xprt, rqstp->rq_proc);
103 * Remove a program, version to port mapping.
105 pmapproc_change(rqstp, xprt, rqstp->rq_proc);
108 case PMAPPROC_GETPORT:
110 * Lookup the mapping for a program, version and return its
113 pmapproc_getport(rqstp, xprt);
118 * Return the current set of mapped program, version
122 fprintf(stderr, "PMAPPROC_DUMP\n");
124 pmapproc_dump(rqstp, xprt);
127 case PMAPPROC_CALLIT:
129 * Calls a procedure on the local machine. If the requested
130 * procedure is not registered this procedure does not return
131 * error information!!
132 * This procedure is only supported on rpc/udp and calls via
133 * rpc/udp. It passes null authentication parameters.
135 rpcbproc_callit_com(rqstp, xprt, PMAPPROC_CALLIT, PMAPVERS);
145 * returns the item with the given program, version number. If that version
146 * number is not found, it returns the item with that program number, so that
147 * the port number is now returned to the caller. The caller when makes a
148 * call to this program, version number, the call will fail and it will
149 * return with PROGVERS_MISMATCH. The user can then determine the highest
150 * and the lowest version number for this program using clnt_geterr() and
151 * use those program version numbers.
153 static struct pmaplist *
154 find_service_pmap(rpcprog_t prog, rpcvers_t vers, rpcprot_t prot)
156 register struct pmaplist *hit = NULL;
157 register struct pmaplist *pml;
159 for (pml = list_pml; pml != NULL; pml = pml->pml_next) {
160 if ((pml->pml_map.pm_prog != prog) ||
161 (pml->pml_map.pm_prot != prot))
164 if (pml->pml_map.pm_vers == vers)
171 pmapproc_change(struct svc_req *rqstp __unused, SVCXPRT *xprt, unsigned long op)
176 struct sockaddr_in *who;
182 fprintf(stderr, "%s request for (%lu, %lu) : ",
183 op == PMAPPROC_SET ? "PMAP_SET" : "PMAP_UNSET",
184 reg.pm_prog, reg.pm_vers);
187 if (!svc_getargs(xprt, (xdrproc_t) xdr_pmap, (char *)®)) {
192 if (!check_access(xprt, op, ®, PMAPVERS)) {
193 svcerr_weakauth(xprt);
197 who = svc_getcaller(xprt);
200 * Can't use getpwnam here. We might end up calling ourselves
203 if (__rpc_get_local_uid(xprt, &uid) < 0)
204 rpcbreg.r_owner = "unknown";
206 rpcbreg.r_owner = "superuser";
208 /* r_owner will be strdup-ed later */
209 snprintf(uidbuf, sizeof uidbuf, "%d", uid);
210 rpcbreg.r_owner = uidbuf;
213 rpcbreg.r_prog = reg.pm_prog;
214 rpcbreg.r_vers = reg.pm_vers;
216 if (op == PMAPPROC_SET) {
219 snprintf(buf, sizeof buf, "0.0.0.0.%d.%d",
220 (int)((reg.pm_port >> 8) & 0xff),
221 (int)(reg.pm_port & 0xff));
222 rpcbreg.r_addr = buf;
223 if (reg.pm_prot == IPPROTO_UDP) {
224 rpcbreg.r_netid = udptrans;
225 } else if (reg.pm_prot == IPPROTO_TCP) {
226 rpcbreg.r_netid = tcptrans;
231 ans = map_set(&rpcbreg, rpcbreg.r_owner);
232 } else if (op == PMAPPROC_UNSET) {
235 rpcbreg.r_addr = NULL;
236 rpcbreg.r_netid = tcptrans;
237 ans1 = map_unset(&rpcbreg, rpcbreg.r_owner);
238 rpcbreg.r_netid = udptrans;
239 ans2 = map_unset(&rpcbreg, rpcbreg.r_owner);
245 if ((!svc_sendreply(xprt, (xdrproc_t) xdr_long, (caddr_t) &ans)) &&
247 fprintf(stderr, "portmap: svc_sendreply\n");
254 fprintf(stderr, "%s\n", ans == TRUE ? "succeeded" : "failed");
256 if (op == PMAPPROC_SET)
257 rpcbs_set(RPCBVERS_2_STAT, ans);
259 rpcbs_unset(RPCBVERS_2_STAT, ans);
265 pmapproc_getport(struct svc_req *rqstp __unused, SVCXPRT *xprt)
270 struct pmaplist *fnd;
275 if (!svc_getargs(xprt, (xdrproc_t) xdr_pmap, (char *)®)) {
280 if (!check_access(xprt, PMAPPROC_GETPORT, ®, PMAPVERS)) {
281 svcerr_weakauth(xprt);
287 uaddr = taddr2uaddr(rpcbind_get_conf(xprt->xp_netid),
288 svc_getrpccaller(xprt));
289 fprintf(stderr, "PMAP_GETPORT req for (%lu, %lu, %s) from %s :",
290 reg.pm_prog, reg.pm_vers,
291 reg.pm_prot == IPPROTO_UDP ? "udp" : "tcp", uaddr);
295 fnd = find_service_pmap(reg.pm_prog, reg.pm_vers, reg.pm_prot);
297 char serveuaddr[32], *ua;
298 int h1, h2, h3, h4, p1, p2;
301 if (reg.pm_prot == IPPROTO_UDP) {
305 ua = tcp_uaddr; /* To get the len */
311 if (sscanf(ua, "%d.%d.%d.%d.%d.%d", &h1, &h2, &h3,
312 &h4, &p1, &p2) == 6) {
313 p1 = (fnd->pml_map.pm_port >> 8) & 0xff;
314 p2 = (fnd->pml_map.pm_port) & 0xff;
315 snprintf(serveuaddr, sizeof serveuaddr,
316 "%d.%d.%d.%d.%d.%d", h1, h2, h3, h4, p1, p2);
317 if (is_bound(netid, serveuaddr)) {
318 port = fnd->pml_map.pm_port;
319 } else { /* this service is dead; delete it */
320 delete_prog(reg.pm_prog);
326 if ((!svc_sendreply(xprt, (xdrproc_t) xdr_long, (caddr_t)&lport)) &&
328 (void) fprintf(stderr, "portmap: svc_sendreply\n");
335 fprintf(stderr, "port = %d\n", port);
337 rpcbs_getaddr(RPCBVERS_2_STAT, reg.pm_prog, reg.pm_vers,
338 reg.pm_prot == IPPROTO_UDP ? udptrans : tcptrans,
339 port ? udptrans : "");
346 pmapproc_dump(struct svc_req *rqstp __unused, SVCXPRT *xprt)
348 if (!svc_getargs(xprt, (xdrproc_t)xdr_void, NULL)) {
353 if (!check_access(xprt, PMAPPROC_DUMP, NULL, PMAPVERS)) {
354 svcerr_weakauth(xprt);
358 if ((!svc_sendreply(xprt, (xdrproc_t) xdr_pmaplist_ptr,
359 (caddr_t)&list_pml)) && debugging) {
361 (void) fprintf(stderr, "portmap: svc_sendreply\n");