1*f525b84eSchristos /* $NetBSD: pmap_svc.c,v 1.10 2021/03/07 00:23:06 christos Exp $ */
234b7ffd9Schristos /* $FreeBSD: head/usr.sbin/rpcbind/pmap_svc.c 258564 2013-11-25 16:44:02Z hrs $ */
3d687de29Sfvdl
434b7ffd9Schristos /*-
534b7ffd9Schristos * Copyright (c) 2009, Sun Microsystems, Inc.
634b7ffd9Schristos * All rights reserved.
7d687de29Sfvdl *
834b7ffd9Schristos * Redistribution and use in source and binary forms, with or without
934b7ffd9Schristos * modification, are permitted provided that the following conditions are met:
1034b7ffd9Schristos * - Redistributions of source code must retain the above copyright notice,
1134b7ffd9Schristos * this list of conditions and the following disclaimer.
1234b7ffd9Schristos * - Redistributions in binary form must reproduce the above copyright notice,
1334b7ffd9Schristos * this list of conditions and the following disclaimer in the documentation
1434b7ffd9Schristos * and/or other materials provided with the distribution.
1534b7ffd9Schristos * - Neither the name of Sun Microsystems, Inc. nor the names of its
1634b7ffd9Schristos * contributors may be used to endorse or promote products derived
1734b7ffd9Schristos * from this software without specific prior written permission.
18d687de29Sfvdl *
1934b7ffd9Schristos * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
2034b7ffd9Schristos * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2134b7ffd9Schristos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2234b7ffd9Schristos * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
2334b7ffd9Schristos * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2434b7ffd9Schristos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2534b7ffd9Schristos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2634b7ffd9Schristos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2734b7ffd9Schristos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2834b7ffd9Schristos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2934b7ffd9Schristos * POSSIBILITY OF SUCH DAMAGE.
30d687de29Sfvdl */
31d687de29Sfvdl /*
32d687de29Sfvdl * Copyright (c) 1984 - 1991 by Sun Microsystems, Inc.
33d687de29Sfvdl */
34d687de29Sfvdl
35d687de29Sfvdl /* #ident "@(#)pmap_svc.c 1.14 93/07/05 SMI" */
36d687de29Sfvdl
37d687de29Sfvdl #if 0
38d687de29Sfvdl #ifndef lint
39d687de29Sfvdl static char sccsid[] = "@(#)pmap_svc.c 1.23 89/04/05 Copyr 1984 Sun Micro";
40d687de29Sfvdl #endif
41d687de29Sfvdl #endif
42d687de29Sfvdl
43d687de29Sfvdl /*
44d687de29Sfvdl * pmap_svc.c
45a8f6b300Scjep * The server procedure for the version 2 portmapper.
46d687de29Sfvdl * All the portmapper related interface from the portmap side.
47d687de29Sfvdl */
48d687de29Sfvdl
49d687de29Sfvdl #ifdef PORTMAP
50d687de29Sfvdl #include <sys/types.h>
51d687de29Sfvdl #include <sys/socket.h>
52d687de29Sfvdl #include <stdio.h>
53d687de29Sfvdl #include <rpc/rpc.h>
54d687de29Sfvdl #include <rpc/pmap_prot.h>
55d687de29Sfvdl #include <rpc/rpcb_prot.h>
56d687de29Sfvdl #ifdef RPCBIND_DEBUG
57d687de29Sfvdl #include <stdlib.h>
58d687de29Sfvdl #endif
59d687de29Sfvdl #include "rpcbind.h"
60d687de29Sfvdl
61de327a01Schristos static struct pmaplist *find_service_pmap(rpcprog_t, rpcvers_t, rpcprot_t);
62de327a01Schristos static bool_t pmapproc_change(struct svc_req *, SVCXPRT *, u_long);
63de327a01Schristos static bool_t pmapproc_getport(struct svc_req *, SVCXPRT *);
64de327a01Schristos static bool_t pmapproc_dump(struct svc_req *, SVCXPRT *);
65d687de29Sfvdl
66d687de29Sfvdl /*
67d687de29Sfvdl * Called for all the version 2 inquiries.
68d687de29Sfvdl */
69d687de29Sfvdl void
pmap_service(struct svc_req * rqstp,SVCXPRT * xprt)70d687de29Sfvdl pmap_service(struct svc_req *rqstp, SVCXPRT *xprt)
71d687de29Sfvdl {
72d687de29Sfvdl rpcbs_procinfo(RPCBVERS_2_STAT, rqstp->rq_proc);
73d687de29Sfvdl switch (rqstp->rq_proc) {
74d687de29Sfvdl case PMAPPROC_NULL:
75d687de29Sfvdl /*
76d687de29Sfvdl * Null proc call
77d687de29Sfvdl */
78d687de29Sfvdl #ifdef RPCBIND_DEBUG
79d687de29Sfvdl if (debugging)
80d687de29Sfvdl fprintf(stderr, "PMAPPROC_NULL\n");
81d687de29Sfvdl #endif
82d687de29Sfvdl check_access(xprt, rqstp->rq_proc, NULL, PMAPVERS);
83d687de29Sfvdl if ((!svc_sendreply(xprt, (xdrproc_t) xdr_void, NULL)) &&
84d687de29Sfvdl debugging) {
85d687de29Sfvdl if (doabort) {
86d687de29Sfvdl rpcbind_abort();
87d687de29Sfvdl }
88d687de29Sfvdl }
89d687de29Sfvdl break;
90d687de29Sfvdl
91d687de29Sfvdl case PMAPPROC_SET:
92d687de29Sfvdl /*
93d687de29Sfvdl * Set a program, version to port mapping
94d687de29Sfvdl */
95d687de29Sfvdl pmapproc_change(rqstp, xprt, rqstp->rq_proc);
96d687de29Sfvdl break;
97d687de29Sfvdl
98d687de29Sfvdl case PMAPPROC_UNSET:
99d687de29Sfvdl /*
100d687de29Sfvdl * Remove a program, version to port mapping.
101d687de29Sfvdl */
102d687de29Sfvdl pmapproc_change(rqstp, xprt, rqstp->rq_proc);
103d687de29Sfvdl break;
104d687de29Sfvdl
105d687de29Sfvdl case PMAPPROC_GETPORT:
106d687de29Sfvdl /*
107d687de29Sfvdl * Lookup the mapping for a program, version and return its
108d687de29Sfvdl * port number.
109d687de29Sfvdl */
110d687de29Sfvdl pmapproc_getport(rqstp, xprt);
111d687de29Sfvdl break;
112d687de29Sfvdl
113d687de29Sfvdl case PMAPPROC_DUMP:
114d687de29Sfvdl /*
115d687de29Sfvdl * Return the current set of mapped program, version
116d687de29Sfvdl */
117d687de29Sfvdl #ifdef RPCBIND_DEBUG
118d687de29Sfvdl if (debugging)
119d687de29Sfvdl fprintf(stderr, "PMAPPROC_DUMP\n");
120d687de29Sfvdl #endif
121d687de29Sfvdl pmapproc_dump(rqstp, xprt);
122d687de29Sfvdl break;
123d687de29Sfvdl
124d687de29Sfvdl case PMAPPROC_CALLIT:
125d687de29Sfvdl /*
126d687de29Sfvdl * Calls a procedure on the local machine. If the requested
127d687de29Sfvdl * procedure is not registered this procedure does not return
128d687de29Sfvdl * error information!!
129d687de29Sfvdl * This procedure is only supported on rpc/udp and calls via
130d687de29Sfvdl * rpc/udp. It passes null authentication parameters.
131d687de29Sfvdl */
132d687de29Sfvdl rpcbproc_callit_com(rqstp, xprt, PMAPPROC_CALLIT, PMAPVERS);
133d687de29Sfvdl break;
134d687de29Sfvdl
135d687de29Sfvdl default:
136d687de29Sfvdl svcerr_noproc(xprt);
137d687de29Sfvdl break;
138d687de29Sfvdl }
139d687de29Sfvdl }
140d687de29Sfvdl
141d687de29Sfvdl /*
142d687de29Sfvdl * returns the item with the given program, version number. If that version
143d687de29Sfvdl * number is not found, it returns the item with that program number, so that
144d687de29Sfvdl * the port number is now returned to the caller. The caller when makes a
145d687de29Sfvdl * call to this program, version number, the call will fail and it will
146d687de29Sfvdl * return with PROGVERS_MISMATCH. The user can then determine the highest
147d687de29Sfvdl * and the lowest version number for this program using clnt_geterr() and
148d687de29Sfvdl * use those program version numbers.
149d687de29Sfvdl */
150d687de29Sfvdl static struct pmaplist *
find_service_pmap(rpcprog_t prog,rpcvers_t vers,rpcprot_t prot)151d687de29Sfvdl find_service_pmap(rpcprog_t prog, rpcvers_t vers, rpcprot_t prot)
152d687de29Sfvdl {
153d687de29Sfvdl register struct pmaplist *hit = NULL;
154d687de29Sfvdl register struct pmaplist *pml;
155d687de29Sfvdl
156d687de29Sfvdl for (pml = list_pml; pml != NULL; pml = pml->pml_next) {
157d687de29Sfvdl if ((pml->pml_map.pm_prog != prog) ||
158d687de29Sfvdl (pml->pml_map.pm_prot != prot))
159d687de29Sfvdl continue;
160d687de29Sfvdl hit = pml;
161d687de29Sfvdl if (pml->pml_map.pm_vers == vers)
162d687de29Sfvdl break;
163d687de29Sfvdl }
164d687de29Sfvdl return (hit);
165d687de29Sfvdl }
166d687de29Sfvdl
167d687de29Sfvdl static bool_t
pmapproc_change(struct svc_req * rqstp __unused,SVCXPRT * xprt,unsigned long op)16834b7ffd9Schristos pmapproc_change(struct svc_req *rqstp __unused, SVCXPRT *xprt, unsigned long op)
169d687de29Sfvdl {
170d687de29Sfvdl struct pmap reg;
171d687de29Sfvdl RPCB rpcbreg;
172b2475f7eSfvdl long ans;
173d687de29Sfvdl struct sockcred *sc;
174d687de29Sfvdl char uidbuf[32];
175d687de29Sfvdl
176*f525b84eSchristos if (!svc_getargs(xprt, (xdrproc_t) xdr_pmap, (char *)®)) {
177*f525b84eSchristos svcerr_decode(xprt);
178*f525b84eSchristos return (FALSE);
179*f525b84eSchristos }
180*f525b84eSchristos
181d687de29Sfvdl #ifdef RPCBIND_DEBUG
182d687de29Sfvdl if (debugging)
183d687de29Sfvdl fprintf(stderr, "%s request for (%lu, %lu) : ",
184d687de29Sfvdl op == PMAPPROC_SET ? "PMAP_SET" : "PMAP_UNSET",
185d687de29Sfvdl reg.pm_prog, reg.pm_vers);
186d687de29Sfvdl #endif
187d687de29Sfvdl
188d687de29Sfvdl if (!check_access(xprt, op, ®, PMAPVERS)) {
189d687de29Sfvdl svcerr_weakauth(xprt);
190d687de29Sfvdl return FALSE;
191d687de29Sfvdl }
192d687de29Sfvdl
1930a77b69aSchristos (void)svc_getcaller(xprt);
194d687de29Sfvdl sc = __svc_getcallercreds(xprt);
195d687de29Sfvdl
196d687de29Sfvdl /*
197d687de29Sfvdl * Can't use getpwnam here. We might end up calling ourselves
198d687de29Sfvdl * and looping.
199d687de29Sfvdl */
200d687de29Sfvdl if (sc == NULL)
201724513abSchristos rpcbreg.r_owner = __UNCONST(rpcbind_unknown);
202d687de29Sfvdl else if (sc->sc_uid == 0)
203724513abSchristos rpcbreg.r_owner = __UNCONST(rpcbind_superuser);
204d687de29Sfvdl else {
205d687de29Sfvdl /* r_owner will be strdup-ed later */
206d687de29Sfvdl snprintf(uidbuf, sizeof uidbuf, "%d", sc->sc_uid);
207d687de29Sfvdl rpcbreg.r_owner = uidbuf;
208d687de29Sfvdl }
209d687de29Sfvdl
210d687de29Sfvdl rpcbreg.r_prog = reg.pm_prog;
211d687de29Sfvdl rpcbreg.r_vers = reg.pm_vers;
212d687de29Sfvdl
213d687de29Sfvdl if (op == PMAPPROC_SET) {
214d687de29Sfvdl char buf[32];
215d687de29Sfvdl
216d9f2774cSitojun snprintf(buf, sizeof(buf), "0.0.0.0.%d.%d",
217d9f2774cSitojun (int)((reg.pm_port >> 8) & 0xff),
218d687de29Sfvdl (int)(reg.pm_port & 0xff));
219d687de29Sfvdl rpcbreg.r_addr = buf;
220d687de29Sfvdl if (reg.pm_prot == IPPROTO_UDP) {
221de327a01Schristos rpcbreg.r_netid = __UNCONST(udptrans);
222d687de29Sfvdl } else if (reg.pm_prot == IPPROTO_TCP) {
223de327a01Schristos rpcbreg.r_netid = __UNCONST(tcptrans);
224d687de29Sfvdl } else {
225d687de29Sfvdl ans = FALSE;
226d687de29Sfvdl goto done_change;
227d687de29Sfvdl }
228d687de29Sfvdl ans = map_set(&rpcbreg, rpcbreg.r_owner);
229d687de29Sfvdl } else if (op == PMAPPROC_UNSET) {
230d687de29Sfvdl bool_t ans1, ans2;
231d687de29Sfvdl
232d687de29Sfvdl rpcbreg.r_addr = NULL;
233de327a01Schristos rpcbreg.r_netid = __UNCONST(tcptrans);
234d687de29Sfvdl ans1 = map_unset(&rpcbreg, rpcbreg.r_owner);
235de327a01Schristos rpcbreg.r_netid = __UNCONST(udptrans);
236d687de29Sfvdl ans2 = map_unset(&rpcbreg, rpcbreg.r_owner);
237d687de29Sfvdl ans = ans1 || ans2;
238d687de29Sfvdl } else {
239d687de29Sfvdl ans = FALSE;
240d687de29Sfvdl }
241d687de29Sfvdl done_change:
242d687de29Sfvdl if ((!svc_sendreply(xprt, (xdrproc_t) xdr_long, (caddr_t) &ans)) &&
243d687de29Sfvdl debugging) {
244d687de29Sfvdl fprintf(stderr, "portmap: svc_sendreply\n");
245d687de29Sfvdl if (doabort) {
246d687de29Sfvdl rpcbind_abort();
247d687de29Sfvdl }
248d687de29Sfvdl }
249d687de29Sfvdl #ifdef RPCBIND_DEBUG
250d687de29Sfvdl if (debugging)
251d687de29Sfvdl fprintf(stderr, "%s\n", ans == TRUE ? "succeeded" : "failed");
252d687de29Sfvdl #endif
253d687de29Sfvdl if (op == PMAPPROC_SET)
254d687de29Sfvdl rpcbs_set(RPCBVERS_2_STAT, ans);
255d687de29Sfvdl else
256d687de29Sfvdl rpcbs_unset(RPCBVERS_2_STAT, ans);
257d687de29Sfvdl return (TRUE);
258d687de29Sfvdl }
259d687de29Sfvdl
260d687de29Sfvdl /* ARGSUSED */
261d687de29Sfvdl static bool_t
pmapproc_getport(struct svc_req * rqstp __unused,SVCXPRT * xprt)26234b7ffd9Schristos pmapproc_getport(struct svc_req *rqstp __unused, SVCXPRT *xprt)
263d687de29Sfvdl {
264d687de29Sfvdl struct pmap reg;
265b2475f7eSfvdl long lport;
266d687de29Sfvdl int port = 0;
267d687de29Sfvdl struct pmaplist *fnd;
268d687de29Sfvdl #ifdef RPCBIND_DEBUG
269d687de29Sfvdl char *uaddr;
270d687de29Sfvdl #endif
271d687de29Sfvdl
272d687de29Sfvdl if (!svc_getargs(xprt, (xdrproc_t) xdr_pmap, (char *)®)) {
273d687de29Sfvdl svcerr_decode(xprt);
274d687de29Sfvdl return (FALSE);
275d687de29Sfvdl }
276d687de29Sfvdl
277d687de29Sfvdl if (!check_access(xprt, PMAPPROC_GETPORT, ®, PMAPVERS)) {
278d687de29Sfvdl svcerr_weakauth(xprt);
279d687de29Sfvdl return FALSE;
280d687de29Sfvdl }
281d687de29Sfvdl
282d687de29Sfvdl #ifdef RPCBIND_DEBUG
283d687de29Sfvdl if (debugging) {
284d687de29Sfvdl uaddr = taddr2uaddr(rpcbind_get_conf(xprt->xp_netid),
285d687de29Sfvdl svc_getrpccaller(xprt));
286d687de29Sfvdl fprintf(stderr, "PMAP_GETPORT req for (%lu, %lu, %s) from %s :",
287d687de29Sfvdl reg.pm_prog, reg.pm_vers,
288d687de29Sfvdl reg.pm_prot == IPPROTO_UDP ? "udp" : "tcp", uaddr);
289d687de29Sfvdl free(uaddr);
290d687de29Sfvdl }
291d687de29Sfvdl #endif
292d687de29Sfvdl fnd = find_service_pmap(reg.pm_prog, reg.pm_vers, reg.pm_prot);
293d687de29Sfvdl if (fnd) {
294de327a01Schristos char serveuaddr[32];
295d687de29Sfvdl int h1, h2, h3, h4, p1, p2;
296de327a01Schristos const char *netid, *ua;
297d687de29Sfvdl
298d687de29Sfvdl if (reg.pm_prot == IPPROTO_UDP) {
299d687de29Sfvdl ua = udp_uaddr;
300d687de29Sfvdl netid = udptrans;
301d687de29Sfvdl } else {
302d687de29Sfvdl ua = tcp_uaddr; /* To get the len */
303d687de29Sfvdl netid = tcptrans;
304d687de29Sfvdl }
305d687de29Sfvdl if (ua == NULL) {
306d687de29Sfvdl goto sendreply;
307d687de29Sfvdl }
308d687de29Sfvdl if (sscanf(ua, "%d.%d.%d.%d.%d.%d", &h1, &h2, &h3,
309d687de29Sfvdl &h4, &p1, &p2) == 6) {
310d687de29Sfvdl p1 = (fnd->pml_map.pm_port >> 8) & 0xff;
311d687de29Sfvdl p2 = (fnd->pml_map.pm_port) & 0xff;
312d9f2774cSitojun snprintf(serveuaddr, sizeof(serveuaddr),
313d9f2774cSitojun "%d.%d.%d.%d.%d.%d", h1, h2, h3, h4, p1, p2);
314d687de29Sfvdl if (is_bound(netid, serveuaddr)) {
315d687de29Sfvdl port = fnd->pml_map.pm_port;
316d687de29Sfvdl } else { /* this service is dead; delete it */
317d687de29Sfvdl delete_prog(reg.pm_prog);
318d687de29Sfvdl }
319d687de29Sfvdl }
320d687de29Sfvdl }
321d687de29Sfvdl sendreply:
322b2475f7eSfvdl lport = port;
323b2475f7eSfvdl if ((!svc_sendreply(xprt, (xdrproc_t) xdr_long, (caddr_t)&lport)) &&
324d687de29Sfvdl debugging) {
325d687de29Sfvdl (void) fprintf(stderr, "portmap: svc_sendreply\n");
326d687de29Sfvdl if (doabort) {
327d687de29Sfvdl rpcbind_abort();
328d687de29Sfvdl }
329d687de29Sfvdl }
330d687de29Sfvdl #ifdef RPCBIND_DEBUG
331d687de29Sfvdl if (debugging)
332d687de29Sfvdl fprintf(stderr, "port = %d\n", port);
333d687de29Sfvdl #endif
334d687de29Sfvdl rpcbs_getaddr(RPCBVERS_2_STAT, reg.pm_prog, reg.pm_vers,
335d687de29Sfvdl reg.pm_prot == IPPROTO_UDP ? udptrans : tcptrans,
336d687de29Sfvdl port ? udptrans : "");
337d687de29Sfvdl
338d687de29Sfvdl return (TRUE);
339d687de29Sfvdl }
340d687de29Sfvdl
341d687de29Sfvdl /* ARGSUSED */
342d687de29Sfvdl static bool_t
pmapproc_dump(struct svc_req * rqstp __unused,SVCXPRT * xprt)34334b7ffd9Schristos pmapproc_dump(struct svc_req *rqstp __unused, SVCXPRT *xprt)
344d687de29Sfvdl {
345d687de29Sfvdl if (!svc_getargs(xprt, (xdrproc_t)xdr_void, NULL)) {
346d687de29Sfvdl svcerr_decode(xprt);
347d687de29Sfvdl return (FALSE);
348d687de29Sfvdl }
349d687de29Sfvdl
350d687de29Sfvdl if (!check_access(xprt, PMAPPROC_DUMP, NULL, PMAPVERS)) {
351d687de29Sfvdl svcerr_weakauth(xprt);
352d687de29Sfvdl return FALSE;
353d687de29Sfvdl }
354d687de29Sfvdl
355d687de29Sfvdl if ((!svc_sendreply(xprt, (xdrproc_t) xdr_pmaplist_ptr,
356d687de29Sfvdl (caddr_t)&list_pml)) && debugging) {
357d687de29Sfvdl if (debugging)
358d687de29Sfvdl (void) fprintf(stderr, "portmap: svc_sendreply\n");
359d687de29Sfvdl if (doabort) {
360d687de29Sfvdl rpcbind_abort();
361d687de29Sfvdl }
362d687de29Sfvdl }
363d687de29Sfvdl return (TRUE);
364d687de29Sfvdl }
365d687de29Sfvdl
366d687de29Sfvdl #endif /* PORTMAP */
367