Home | History | Annotate | Line # | Download | only in rpcbind
pmap_svc.c revision 1.4
      1  1.4  itojun /*	$NetBSD: pmap_svc.c,v 1.4 2003/07/13 12:16:05 itojun Exp $	*/
      2  1.1    fvdl 
      3  1.1    fvdl /*
      4  1.1    fvdl  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
      5  1.1    fvdl  * unrestricted use provided that this legend is included on all tape
      6  1.1    fvdl  * media and as a part of the software program in whole or part.  Users
      7  1.1    fvdl  * may copy or modify Sun RPC without charge, but are not authorized
      8  1.1    fvdl  * to license or distribute it to anyone else except as part of a product or
      9  1.1    fvdl  * program developed by the user.
     10  1.1    fvdl  *
     11  1.1    fvdl  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
     12  1.1    fvdl  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
     13  1.1    fvdl  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
     14  1.1    fvdl  *
     15  1.1    fvdl  * Sun RPC is provided with no support and without any obligation on the
     16  1.1    fvdl  * part of Sun Microsystems, Inc. to assist in its use, correction,
     17  1.1    fvdl  * modification or enhancement.
     18  1.1    fvdl  *
     19  1.1    fvdl  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
     20  1.1    fvdl  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
     21  1.1    fvdl  * OR ANY PART THEREOF.
     22  1.1    fvdl  *
     23  1.1    fvdl  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
     24  1.1    fvdl  * or profits or other special, indirect and consequential damages, even if
     25  1.1    fvdl  * Sun has been advised of the possibility of such damages.
     26  1.1    fvdl  *
     27  1.1    fvdl  * Sun Microsystems, Inc.
     28  1.1    fvdl  * 2550 Garcia Avenue
     29  1.1    fvdl  * Mountain View, California  94043
     30  1.1    fvdl  */
     31  1.1    fvdl /*
     32  1.1    fvdl  * Copyright (c) 1984 - 1991 by Sun Microsystems, Inc.
     33  1.1    fvdl  */
     34  1.1    fvdl 
     35  1.1    fvdl /* #ident	"@(#)pmap_svc.c	1.14	93/07/05 SMI" */
     36  1.1    fvdl 
     37  1.1    fvdl #if 0
     38  1.1    fvdl #ifndef lint
     39  1.1    fvdl static	char sccsid[] = "@(#)pmap_svc.c 1.23 89/04/05 Copyr 1984 Sun Micro";
     40  1.1    fvdl #endif
     41  1.1    fvdl #endif
     42  1.1    fvdl 
     43  1.1    fvdl /*
     44  1.1    fvdl  * pmap_svc.c
     45  1.3    cjep  * The server procedure for the version 2 portmapper.
     46  1.1    fvdl  * All the portmapper related interface from the portmap side.
     47  1.1    fvdl  */
     48  1.1    fvdl 
     49  1.1    fvdl #ifdef PORTMAP
     50  1.1    fvdl #include <sys/types.h>
     51  1.1    fvdl #include <sys/socket.h>
     52  1.1    fvdl #include <stdio.h>
     53  1.1    fvdl #include <rpc/rpc.h>
     54  1.1    fvdl #include <rpc/pmap_prot.h>
     55  1.1    fvdl #include <rpc/rpcb_prot.h>
     56  1.1    fvdl #ifdef RPCBIND_DEBUG
     57  1.1    fvdl #include <stdlib.h>
     58  1.1    fvdl #endif
     59  1.1    fvdl #include "rpcbind.h"
     60  1.1    fvdl 
     61  1.1    fvdl static struct pmaplist *find_service_pmap __P((rpcprog_t, rpcvers_t,
     62  1.1    fvdl 					       rpcprot_t));
     63  1.1    fvdl static bool_t pmapproc_change __P((struct svc_req *, SVCXPRT *, u_long));
     64  1.1    fvdl static bool_t pmapproc_getport __P((struct svc_req *, SVCXPRT *));
     65  1.1    fvdl static bool_t pmapproc_dump __P((struct svc_req *, SVCXPRT *));
     66  1.1    fvdl 
     67  1.1    fvdl /*
     68  1.1    fvdl  * Called for all the version 2 inquiries.
     69  1.1    fvdl  */
     70  1.1    fvdl void
     71  1.1    fvdl pmap_service(struct svc_req *rqstp, SVCXPRT *xprt)
     72  1.1    fvdl {
     73  1.1    fvdl 	rpcbs_procinfo(RPCBVERS_2_STAT, rqstp->rq_proc);
     74  1.1    fvdl 	switch (rqstp->rq_proc) {
     75  1.1    fvdl 	case PMAPPROC_NULL:
     76  1.1    fvdl 		/*
     77  1.1    fvdl 		 * Null proc call
     78  1.1    fvdl 		 */
     79  1.1    fvdl #ifdef RPCBIND_DEBUG
     80  1.1    fvdl 		if (debugging)
     81  1.1    fvdl 			fprintf(stderr, "PMAPPROC_NULL\n");
     82  1.1    fvdl #endif
     83  1.1    fvdl 		check_access(xprt, rqstp->rq_proc, NULL, PMAPVERS);
     84  1.1    fvdl 		if ((!svc_sendreply(xprt, (xdrproc_t) xdr_void, NULL)) &&
     85  1.1    fvdl 			debugging) {
     86  1.1    fvdl 			if (doabort) {
     87  1.1    fvdl 				rpcbind_abort();
     88  1.1    fvdl 			}
     89  1.1    fvdl 		}
     90  1.1    fvdl 		break;
     91  1.1    fvdl 
     92  1.1    fvdl 	case PMAPPROC_SET:
     93  1.1    fvdl 		/*
     94  1.1    fvdl 		 * Set a program, version to port mapping
     95  1.1    fvdl 		 */
     96  1.1    fvdl 		pmapproc_change(rqstp, xprt, rqstp->rq_proc);
     97  1.1    fvdl 		break;
     98  1.1    fvdl 
     99  1.1    fvdl 	case PMAPPROC_UNSET:
    100  1.1    fvdl 		/*
    101  1.1    fvdl 		 * Remove a program, version to port mapping.
    102  1.1    fvdl 		 */
    103  1.1    fvdl 		pmapproc_change(rqstp, xprt, rqstp->rq_proc);
    104  1.1    fvdl 		break;
    105  1.1    fvdl 
    106  1.1    fvdl 	case PMAPPROC_GETPORT:
    107  1.1    fvdl 		/*
    108  1.1    fvdl 		 * Lookup the mapping for a program, version and return its
    109  1.1    fvdl 		 * port number.
    110  1.1    fvdl 		 */
    111  1.1    fvdl 		pmapproc_getport(rqstp, xprt);
    112  1.1    fvdl 		break;
    113  1.1    fvdl 
    114  1.1    fvdl 	case PMAPPROC_DUMP:
    115  1.1    fvdl 		/*
    116  1.1    fvdl 		 * Return the current set of mapped program, version
    117  1.1    fvdl 		 */
    118  1.1    fvdl #ifdef RPCBIND_DEBUG
    119  1.1    fvdl 		if (debugging)
    120  1.1    fvdl 			fprintf(stderr, "PMAPPROC_DUMP\n");
    121  1.1    fvdl #endif
    122  1.1    fvdl 		pmapproc_dump(rqstp, xprt);
    123  1.1    fvdl 		break;
    124  1.1    fvdl 
    125  1.1    fvdl 	case PMAPPROC_CALLIT:
    126  1.1    fvdl 		/*
    127  1.1    fvdl 		 * Calls a procedure on the local machine. If the requested
    128  1.1    fvdl 		 * procedure is not registered this procedure does not return
    129  1.1    fvdl 		 * error information!!
    130  1.1    fvdl 		 * This procedure is only supported on rpc/udp and calls via
    131  1.1    fvdl 		 * rpc/udp. It passes null authentication parameters.
    132  1.1    fvdl 		 */
    133  1.1    fvdl 		rpcbproc_callit_com(rqstp, xprt, PMAPPROC_CALLIT, PMAPVERS);
    134  1.1    fvdl 		break;
    135  1.1    fvdl 
    136  1.1    fvdl 	default:
    137  1.1    fvdl 		svcerr_noproc(xprt);
    138  1.1    fvdl 		break;
    139  1.1    fvdl 	}
    140  1.1    fvdl }
    141  1.1    fvdl 
    142  1.1    fvdl /*
    143  1.1    fvdl  * returns the item with the given program, version number. If that version
    144  1.1    fvdl  * number is not found, it returns the item with that program number, so that
    145  1.1    fvdl  * the port number is now returned to the caller. The caller when makes a
    146  1.1    fvdl  * call to this program, version number, the call will fail and it will
    147  1.1    fvdl  * return with PROGVERS_MISMATCH. The user can then determine the highest
    148  1.1    fvdl  * and the lowest version number for this program using clnt_geterr() and
    149  1.1    fvdl  * use those program version numbers.
    150  1.1    fvdl  */
    151  1.1    fvdl static struct pmaplist *
    152  1.1    fvdl find_service_pmap(rpcprog_t prog, rpcvers_t vers, rpcprot_t prot)
    153  1.1    fvdl {
    154  1.1    fvdl 	register struct pmaplist *hit = NULL;
    155  1.1    fvdl 	register struct pmaplist *pml;
    156  1.1    fvdl 
    157  1.1    fvdl 	for (pml = list_pml; pml != NULL; pml = pml->pml_next) {
    158  1.1    fvdl 		if ((pml->pml_map.pm_prog != prog) ||
    159  1.1    fvdl 			(pml->pml_map.pm_prot != prot))
    160  1.1    fvdl 			continue;
    161  1.1    fvdl 		hit = pml;
    162  1.1    fvdl 		if (pml->pml_map.pm_vers == vers)
    163  1.1    fvdl 			break;
    164  1.1    fvdl 	}
    165  1.1    fvdl 	return (hit);
    166  1.1    fvdl }
    167  1.1    fvdl 
    168  1.1    fvdl static bool_t
    169  1.1    fvdl pmapproc_change(struct svc_req *rqstp, SVCXPRT *xprt, unsigned long op)
    170  1.1    fvdl {
    171  1.1    fvdl 	struct pmap reg;
    172  1.1    fvdl 	RPCB rpcbreg;
    173  1.2    fvdl 	long ans;
    174  1.1    fvdl 	struct sockaddr_in *who;
    175  1.1    fvdl 	struct sockcred *sc;
    176  1.1    fvdl 	char uidbuf[32];
    177  1.1    fvdl 
    178  1.1    fvdl #ifdef RPCBIND_DEBUG
    179  1.1    fvdl 	if (debugging)
    180  1.1    fvdl 		fprintf(stderr, "%s request for (%lu, %lu) : ",
    181  1.1    fvdl 		    op == PMAPPROC_SET ? "PMAP_SET" : "PMAP_UNSET",
    182  1.1    fvdl 		    reg.pm_prog, reg.pm_vers);
    183  1.1    fvdl #endif
    184  1.1    fvdl 
    185  1.1    fvdl 	if (!svc_getargs(xprt, (xdrproc_t) xdr_pmap, (char *)&reg)) {
    186  1.1    fvdl 		svcerr_decode(xprt);
    187  1.1    fvdl 		return (FALSE);
    188  1.1    fvdl 	}
    189  1.1    fvdl 
    190  1.1    fvdl 	if (!check_access(xprt, op, &reg, PMAPVERS)) {
    191  1.1    fvdl 		svcerr_weakauth(xprt);
    192  1.1    fvdl 		return FALSE;
    193  1.1    fvdl 	}
    194  1.1    fvdl 
    195  1.1    fvdl 	who = svc_getcaller(xprt);
    196  1.1    fvdl 	sc = __svc_getcallercreds(xprt);
    197  1.1    fvdl 
    198  1.1    fvdl 	/*
    199  1.1    fvdl 	 * Can't use getpwnam here. We might end up calling ourselves
    200  1.1    fvdl 	 * and looping.
    201  1.1    fvdl 	 */
    202  1.1    fvdl 	if (sc == NULL)
    203  1.1    fvdl 		rpcbreg.r_owner = "unknown";
    204  1.1    fvdl 	else if (sc->sc_uid == 0)
    205  1.1    fvdl 		rpcbreg.r_owner = "superuser";
    206  1.1    fvdl 	else {
    207  1.1    fvdl 		/* r_owner will be strdup-ed later */
    208  1.1    fvdl 		snprintf(uidbuf, sizeof uidbuf, "%d", sc->sc_uid);
    209  1.1    fvdl 		rpcbreg.r_owner = uidbuf;
    210  1.1    fvdl 	}
    211  1.1    fvdl 
    212  1.1    fvdl 	rpcbreg.r_prog = reg.pm_prog;
    213  1.1    fvdl 	rpcbreg.r_vers = reg.pm_vers;
    214  1.1    fvdl 
    215  1.1    fvdl 	if (op == PMAPPROC_SET) {
    216  1.1    fvdl 		char buf[32];
    217  1.1    fvdl 
    218  1.4  itojun 		snprintf(buf, sizeof(buf), "0.0.0.0.%d.%d",
    219  1.4  itojun 		    (int)((reg.pm_port >> 8) & 0xff),
    220  1.4  itojun 		    (int)(reg.pm_port & 0xff));
    221  1.1    fvdl 		rpcbreg.r_addr = buf;
    222  1.1    fvdl 		if (reg.pm_prot == IPPROTO_UDP) {
    223  1.1    fvdl 			rpcbreg.r_netid = udptrans;
    224  1.1    fvdl 		} else if (reg.pm_prot == IPPROTO_TCP) {
    225  1.1    fvdl 			rpcbreg.r_netid = tcptrans;
    226  1.1    fvdl 		} else {
    227  1.1    fvdl 			ans = FALSE;
    228  1.1    fvdl 			goto done_change;
    229  1.1    fvdl 		}
    230  1.1    fvdl 		ans = map_set(&rpcbreg, rpcbreg.r_owner);
    231  1.1    fvdl 	} else if (op == PMAPPROC_UNSET) {
    232  1.1    fvdl 		bool_t ans1, ans2;
    233  1.1    fvdl 
    234  1.1    fvdl 		rpcbreg.r_addr = NULL;
    235  1.1    fvdl 		rpcbreg.r_netid = tcptrans;
    236  1.1    fvdl 		ans1 = map_unset(&rpcbreg, rpcbreg.r_owner);
    237  1.1    fvdl 		rpcbreg.r_netid = udptrans;
    238  1.1    fvdl 		ans2 = map_unset(&rpcbreg, rpcbreg.r_owner);
    239  1.1    fvdl 		ans = ans1 || ans2;
    240  1.1    fvdl 	} else {
    241  1.1    fvdl 		ans = FALSE;
    242  1.1    fvdl 	}
    243  1.1    fvdl done_change:
    244  1.1    fvdl 	if ((!svc_sendreply(xprt, (xdrproc_t) xdr_long, (caddr_t) &ans)) &&
    245  1.1    fvdl 	    debugging) {
    246  1.1    fvdl 		fprintf(stderr, "portmap: svc_sendreply\n");
    247  1.1    fvdl 		if (doabort) {
    248  1.1    fvdl 			rpcbind_abort();
    249  1.1    fvdl 		}
    250  1.1    fvdl 	}
    251  1.1    fvdl #ifdef RPCBIND_DEBUG
    252  1.1    fvdl 	if (debugging)
    253  1.1    fvdl 		fprintf(stderr, "%s\n", ans == TRUE ? "succeeded" : "failed");
    254  1.1    fvdl #endif
    255  1.1    fvdl 	if (op == PMAPPROC_SET)
    256  1.1    fvdl 		rpcbs_set(RPCBVERS_2_STAT, ans);
    257  1.1    fvdl 	else
    258  1.1    fvdl 		rpcbs_unset(RPCBVERS_2_STAT, ans);
    259  1.1    fvdl 	return (TRUE);
    260  1.1    fvdl }
    261  1.1    fvdl 
    262  1.1    fvdl /* ARGSUSED */
    263  1.1    fvdl static bool_t
    264  1.1    fvdl pmapproc_getport(struct svc_req *rqstp, SVCXPRT *xprt)
    265  1.1    fvdl {
    266  1.1    fvdl 	struct pmap reg;
    267  1.2    fvdl 	long lport;
    268  1.1    fvdl 	int port = 0;
    269  1.1    fvdl 	struct pmaplist *fnd;
    270  1.1    fvdl #ifdef RPCBIND_DEBUG
    271  1.1    fvdl 	char *uaddr;
    272  1.1    fvdl #endif
    273  1.1    fvdl 
    274  1.1    fvdl 	if (!svc_getargs(xprt, (xdrproc_t) xdr_pmap, (char *)&reg)) {
    275  1.1    fvdl 		svcerr_decode(xprt);
    276  1.1    fvdl 		return (FALSE);
    277  1.1    fvdl 	}
    278  1.1    fvdl 
    279  1.1    fvdl 	if (!check_access(xprt, PMAPPROC_GETPORT, &reg, PMAPVERS)) {
    280  1.1    fvdl 		svcerr_weakauth(xprt);
    281  1.1    fvdl 		return FALSE;
    282  1.1    fvdl 	}
    283  1.1    fvdl 
    284  1.1    fvdl #ifdef RPCBIND_DEBUG
    285  1.1    fvdl 	if (debugging) {
    286  1.1    fvdl 		uaddr =  taddr2uaddr(rpcbind_get_conf(xprt->xp_netid),
    287  1.1    fvdl 			    svc_getrpccaller(xprt));
    288  1.1    fvdl 		fprintf(stderr, "PMAP_GETPORT req for (%lu, %lu, %s) from %s :",
    289  1.1    fvdl 			reg.pm_prog, reg.pm_vers,
    290  1.1    fvdl 			reg.pm_prot == IPPROTO_UDP ? "udp" : "tcp", uaddr);
    291  1.1    fvdl 		free(uaddr);
    292  1.1    fvdl 	}
    293  1.1    fvdl #endif
    294  1.1    fvdl 	fnd = find_service_pmap(reg.pm_prog, reg.pm_vers, reg.pm_prot);
    295  1.1    fvdl 	if (fnd) {
    296  1.1    fvdl 		char serveuaddr[32], *ua;
    297  1.1    fvdl 		int h1, h2, h3, h4, p1, p2;
    298  1.1    fvdl 		char *netid;
    299  1.1    fvdl 
    300  1.1    fvdl 		if (reg.pm_prot == IPPROTO_UDP) {
    301  1.1    fvdl 			ua = udp_uaddr;
    302  1.1    fvdl 			netid = udptrans;
    303  1.1    fvdl 		} else {
    304  1.1    fvdl 			ua = tcp_uaddr; /* To get the len */
    305  1.1    fvdl 			netid = tcptrans;
    306  1.1    fvdl 		}
    307  1.1    fvdl 		if (ua == NULL) {
    308  1.1    fvdl 			goto sendreply;
    309  1.1    fvdl 		}
    310  1.1    fvdl 		if (sscanf(ua, "%d.%d.%d.%d.%d.%d", &h1, &h2, &h3,
    311  1.1    fvdl 				&h4, &p1, &p2) == 6) {
    312  1.1    fvdl 			p1 = (fnd->pml_map.pm_port >> 8) & 0xff;
    313  1.1    fvdl 			p2 = (fnd->pml_map.pm_port) & 0xff;
    314  1.4  itojun 			snprintf(serveuaddr, sizeof(serveuaddr),
    315  1.4  itojun 			    "%d.%d.%d.%d.%d.%d", h1, h2, h3, h4, p1, p2);
    316  1.1    fvdl 			if (is_bound(netid, serveuaddr)) {
    317  1.1    fvdl 				port = fnd->pml_map.pm_port;
    318  1.1    fvdl 			} else { /* this service is dead; delete it */
    319  1.1    fvdl 				delete_prog(reg.pm_prog);
    320  1.1    fvdl 			}
    321  1.1    fvdl 		}
    322  1.1    fvdl 	}
    323  1.1    fvdl sendreply:
    324  1.2    fvdl 	lport = port;
    325  1.2    fvdl 	if ((!svc_sendreply(xprt, (xdrproc_t) xdr_long, (caddr_t)&lport)) &&
    326  1.1    fvdl 			debugging) {
    327  1.1    fvdl 		(void) fprintf(stderr, "portmap: svc_sendreply\n");
    328  1.1    fvdl 		if (doabort) {
    329  1.1    fvdl 			rpcbind_abort();
    330  1.1    fvdl 		}
    331  1.1    fvdl 	}
    332  1.1    fvdl #ifdef RPCBIND_DEBUG
    333  1.1    fvdl 	if (debugging)
    334  1.1    fvdl 		fprintf(stderr, "port = %d\n", port);
    335  1.1    fvdl #endif
    336  1.1    fvdl 	rpcbs_getaddr(RPCBVERS_2_STAT, reg.pm_prog, reg.pm_vers,
    337  1.1    fvdl 		reg.pm_prot == IPPROTO_UDP ? udptrans : tcptrans,
    338  1.1    fvdl 		port ? udptrans : "");
    339  1.1    fvdl 
    340  1.1    fvdl 	return (TRUE);
    341  1.1    fvdl }
    342  1.1    fvdl 
    343  1.1    fvdl /* ARGSUSED */
    344  1.1    fvdl static bool_t
    345  1.1    fvdl pmapproc_dump(struct svc_req *rqstp, SVCXPRT *xprt)
    346  1.1    fvdl {
    347  1.1    fvdl 	if (!svc_getargs(xprt, (xdrproc_t)xdr_void, NULL)) {
    348  1.1    fvdl 		svcerr_decode(xprt);
    349  1.1    fvdl 		return (FALSE);
    350  1.1    fvdl 	}
    351  1.1    fvdl 
    352  1.1    fvdl 	if (!check_access(xprt, PMAPPROC_DUMP, NULL, PMAPVERS)) {
    353  1.1    fvdl 		svcerr_weakauth(xprt);
    354  1.1    fvdl 		return FALSE;
    355  1.1    fvdl 	}
    356  1.1    fvdl 
    357  1.1    fvdl 	if ((!svc_sendreply(xprt, (xdrproc_t) xdr_pmaplist_ptr,
    358  1.1    fvdl 			(caddr_t)&list_pml)) && debugging) {
    359  1.1    fvdl 		if (debugging)
    360  1.1    fvdl 			(void) fprintf(stderr, "portmap: svc_sendreply\n");
    361  1.1    fvdl 		if (doabort) {
    362  1.1    fvdl 			rpcbind_abort();
    363  1.1    fvdl 		}
    364  1.1    fvdl 	}
    365  1.1    fvdl 	return (TRUE);
    366  1.1    fvdl }
    367  1.1    fvdl 
    368  1.1    fvdl #endif /* PORTMAP */
    369