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