Home | History | Annotate | Line # | Download | only in rpc
svc.c revision 1.2
      1 /*
      2  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
      3  * unrestricted use provided that this legend is included on all tape
      4  * media and as a part of the software program in whole or part.  Users
      5  * may copy or modify Sun RPC without charge, but are not authorized
      6  * to license or distribute it to anyone else except as part of a product or
      7  * program developed by the user.
      8  *
      9  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
     10  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
     11  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
     12  *
     13  * Sun RPC is provided with no support and without any obligation on the
     14  * part of Sun Microsystems, Inc. to assist in its use, correction,
     15  * modification or enhancement.
     16  *
     17  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
     18  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
     19  * OR ANY PART THEREOF.
     20  *
     21  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
     22  * or profits or other special, indirect and consequential damages, even if
     23  * Sun has been advised of the possibility of such damages.
     24  *
     25  * Sun Microsystems, Inc.
     26  * 2550 Garcia Avenue
     27  * Mountain View, California  94043
     28  */
     29 
     30 #if defined(LIBC_SCCS) && !defined(lint)
     31 /*static char *sccsid = "from: @(#)svc.c 1.44 88/02/08 Copyr 1984 Sun Micro";*/
     32 /*static char *sccsid = "from: @(#)svc.c	2.4 88/08/11 4.0 RPCSRC";*/
     33 static char *rcsid = "$Id: svc.c,v 1.2 1994/08/20 00:55:32 deraadt Exp $";
     34 #endif
     35 
     36 /*
     37  * svc.c, Server-side remote procedure call interface.
     38  *
     39  * There are two sets of procedures here.  The xprt routines are
     40  * for handling transport handles.  The svc routines handle the
     41  * list of service routines.
     42  *
     43  * Copyright (C) 1984, Sun Microsystems, Inc.
     44  */
     45 
     46 #include <sys/errno.h>
     47 #include <rpc/rpc.h>
     48 #include <rpc/pmap_clnt.h>
     49 
     50 extern int errno;
     51 
     52 static SVCXPRT **xports;
     53 
     54 #define NULL_SVC ((struct svc_callout *)0)
     55 #define	RQCRED_SIZE	400		/* this size is excessive */
     56 
     57 #define max(a, b) (a > b ? a : b)
     58 
     59 /*
     60  * The services list
     61  * Each entry represents a set of procedures (an rpc program).
     62  * The dispatch routine takes request structs and runs the
     63  * apropriate procedure.
     64  */
     65 static struct svc_callout {
     66 	struct svc_callout *sc_next;
     67 	u_long		    sc_prog;
     68 	u_long		    sc_vers;
     69 	void		    (*sc_dispatch)();
     70 } *svc_head;
     71 
     72 static struct svc_callout *svc_find();
     73 
     74 /* ***************  SVCXPRT related stuff **************** */
     75 
     76 /*
     77  * Activate a transport handle.
     78  */
     79 void
     80 xprt_register(xprt)
     81 	SVCXPRT *xprt;
     82 {
     83 	register int sock = xprt->xp_sock;
     84 
     85 	if (xports == NULL) {
     86 		xports = (SVCXPRT **)
     87 			mem_alloc(FD_SETSIZE * sizeof(SVCXPRT *));
     88 	}
     89 	if (sock < FD_SETSIZE) {
     90 		xports[sock] = xprt;
     91 		FD_SET(sock, &svc_fdset);
     92 		svc_maxfd = max(svc_maxfd, sock);
     93 	}
     94 }
     95 
     96 /*
     97  * De-activate a transport handle.
     98  */
     99 void
    100 xprt_unregister(xprt)
    101 	SVCXPRT *xprt;
    102 {
    103 	register int sock = xprt->xp_sock;
    104 
    105 	if ((sock < FD_SETSIZE) && (xports[sock] == xprt)) {
    106 		xports[sock] = (SVCXPRT *)0;
    107 		FD_CLR(sock, &svc_fdset);
    108 		for (svc_maxfd--; svc_maxfd; svc_maxfd--)
    109 			if (xports[svc_maxfd])
    110 				break;
    111 	}
    112 }
    113 
    114 
    115 /* ********************** CALLOUT list related stuff ************* */
    116 
    117 /*
    118  * Add a service program to the callout list.
    119  * The dispatch routine will be called when a rpc request for this
    120  * program number comes in.
    121  */
    122 bool_t
    123 svc_register(xprt, prog, vers, dispatch, protocol)
    124 	SVCXPRT *xprt;
    125 	u_long prog;
    126 	u_long vers;
    127 	void (*dispatch)();
    128 	int protocol;
    129 {
    130 	struct svc_callout *prev;
    131 	register struct svc_callout *s;
    132 
    133 	if ((s = svc_find(prog, vers, &prev)) != NULL_SVC) {
    134 		if (s->sc_dispatch == dispatch)
    135 			goto pmap_it;  /* he is registering another xptr */
    136 		return (FALSE);
    137 	}
    138 	s = (struct svc_callout *)mem_alloc(sizeof(struct svc_callout));
    139 	if (s == (struct svc_callout *)0) {
    140 		return (FALSE);
    141 	}
    142 	s->sc_prog = prog;
    143 	s->sc_vers = vers;
    144 	s->sc_dispatch = dispatch;
    145 	s->sc_next = svc_head;
    146 	svc_head = s;
    147 pmap_it:
    148 	/* now register the information with the local binder service */
    149 	if (protocol) {
    150 		return (pmap_set(prog, vers, protocol, xprt->xp_port));
    151 	}
    152 	return (TRUE);
    153 }
    154 
    155 /*
    156  * Remove a service program from the callout list.
    157  */
    158 void
    159 svc_unregister(prog, vers)
    160 	u_long prog;
    161 	u_long vers;
    162 {
    163 	struct svc_callout *prev;
    164 	register struct svc_callout *s;
    165 
    166 	if ((s = svc_find(prog, vers, &prev)) == NULL_SVC)
    167 		return;
    168 	if (prev == NULL_SVC) {
    169 		svc_head = s->sc_next;
    170 	} else {
    171 		prev->sc_next = s->sc_next;
    172 	}
    173 	s->sc_next = NULL_SVC;
    174 	mem_free((char *) s, (u_int) sizeof(struct svc_callout));
    175 	/* now unregister the information with the local binder service */
    176 	(void)pmap_unset(prog, vers);
    177 }
    178 
    179 /*
    180  * Search the callout list for a program number, return the callout
    181  * struct.
    182  */
    183 static struct svc_callout *
    184 svc_find(prog, vers, prev)
    185 	u_long prog;
    186 	u_long vers;
    187 	struct svc_callout **prev;
    188 {
    189 	register struct svc_callout *s, *p;
    190 
    191 	p = NULL_SVC;
    192 	for (s = svc_head; s != NULL_SVC; s = s->sc_next) {
    193 		if ((s->sc_prog == prog) && (s->sc_vers == vers))
    194 			goto done;
    195 		p = s;
    196 	}
    197 done:
    198 	*prev = p;
    199 	return (s);
    200 }
    201 
    202 /* ******************* REPLY GENERATION ROUTINES  ************ */
    203 
    204 /*
    205  * Send a reply to an rpc request
    206  */
    207 bool_t
    208 svc_sendreply(xprt, xdr_results, xdr_location)
    209 	register SVCXPRT *xprt;
    210 	xdrproc_t xdr_results;
    211 	caddr_t xdr_location;
    212 {
    213 	struct rpc_msg rply;
    214 
    215 	rply.rm_direction = REPLY;
    216 	rply.rm_reply.rp_stat = MSG_ACCEPTED;
    217 	rply.acpted_rply.ar_verf = xprt->xp_verf;
    218 	rply.acpted_rply.ar_stat = SUCCESS;
    219 	rply.acpted_rply.ar_results.where = xdr_location;
    220 	rply.acpted_rply.ar_results.proc = xdr_results;
    221 	return (SVC_REPLY(xprt, &rply));
    222 }
    223 
    224 /*
    225  * No procedure error reply
    226  */
    227 void
    228 svcerr_noproc(xprt)
    229 	register SVCXPRT *xprt;
    230 {
    231 	struct rpc_msg rply;
    232 
    233 	rply.rm_direction = REPLY;
    234 	rply.rm_reply.rp_stat = MSG_ACCEPTED;
    235 	rply.acpted_rply.ar_verf = xprt->xp_verf;
    236 	rply.acpted_rply.ar_stat = PROC_UNAVAIL;
    237 	SVC_REPLY(xprt, &rply);
    238 }
    239 
    240 /*
    241  * Can't decode args error reply
    242  */
    243 void
    244 svcerr_decode(xprt)
    245 	register SVCXPRT *xprt;
    246 {
    247 	struct rpc_msg rply;
    248 
    249 	rply.rm_direction = REPLY;
    250 	rply.rm_reply.rp_stat = MSG_ACCEPTED;
    251 	rply.acpted_rply.ar_verf = xprt->xp_verf;
    252 	rply.acpted_rply.ar_stat = GARBAGE_ARGS;
    253 	SVC_REPLY(xprt, &rply);
    254 }
    255 
    256 /*
    257  * Some system error
    258  */
    259 void
    260 svcerr_systemerr(xprt)
    261 	register SVCXPRT *xprt;
    262 {
    263 	struct rpc_msg rply;
    264 
    265 	rply.rm_direction = REPLY;
    266 	rply.rm_reply.rp_stat = MSG_ACCEPTED;
    267 	rply.acpted_rply.ar_verf = xprt->xp_verf;
    268 	rply.acpted_rply.ar_stat = SYSTEM_ERR;
    269 	SVC_REPLY(xprt, &rply);
    270 }
    271 
    272 /*
    273  * Authentication error reply
    274  */
    275 void
    276 svcerr_auth(xprt, why)
    277 	SVCXPRT *xprt;
    278 	enum auth_stat why;
    279 {
    280 	struct rpc_msg rply;
    281 
    282 	rply.rm_direction = REPLY;
    283 	rply.rm_reply.rp_stat = MSG_DENIED;
    284 	rply.rjcted_rply.rj_stat = AUTH_ERROR;
    285 	rply.rjcted_rply.rj_why = why;
    286 	SVC_REPLY(xprt, &rply);
    287 }
    288 
    289 /*
    290  * Auth too weak error reply
    291  */
    292 void
    293 svcerr_weakauth(xprt)
    294 	SVCXPRT *xprt;
    295 {
    296 
    297 	svcerr_auth(xprt, AUTH_TOOWEAK);
    298 }
    299 
    300 /*
    301  * Program unavailable error reply
    302  */
    303 void
    304 svcerr_noprog(xprt)
    305 	register SVCXPRT *xprt;
    306 {
    307 	struct rpc_msg rply;
    308 
    309 	rply.rm_direction = REPLY;
    310 	rply.rm_reply.rp_stat = MSG_ACCEPTED;
    311 	rply.acpted_rply.ar_verf = xprt->xp_verf;
    312 	rply.acpted_rply.ar_stat = PROG_UNAVAIL;
    313 	SVC_REPLY(xprt, &rply);
    314 }
    315 
    316 /*
    317  * Program version mismatch error reply
    318  */
    319 void
    320 svcerr_progvers(xprt, low_vers, high_vers)
    321 	register SVCXPRT *xprt;
    322 	u_long low_vers;
    323 	u_long high_vers;
    324 {
    325 	struct rpc_msg rply;
    326 
    327 	rply.rm_direction = REPLY;
    328 	rply.rm_reply.rp_stat = MSG_ACCEPTED;
    329 	rply.acpted_rply.ar_verf = xprt->xp_verf;
    330 	rply.acpted_rply.ar_stat = PROG_MISMATCH;
    331 	rply.acpted_rply.ar_vers.low = low_vers;
    332 	rply.acpted_rply.ar_vers.high = high_vers;
    333 	SVC_REPLY(xprt, &rply);
    334 }
    335 
    336 /* ******************* SERVER INPUT STUFF ******************* */
    337 
    338 /*
    339  * Get server side input from some transport.
    340  *
    341  * Statement of authentication parameters management:
    342  * This function owns and manages all authentication parameters, specifically
    343  * the "raw" parameters (msg.rm_call.cb_cred and msg.rm_call.cb_verf) and
    344  * the "cooked" credentials (rqst->rq_clntcred).
    345  * However, this function does not know the structure of the cooked
    346  * credentials, so it make the following assumptions:
    347  *   a) the structure is contiguous (no pointers), and
    348  *   b) the cred structure size does not exceed RQCRED_SIZE bytes.
    349  * In all events, all three parameters are freed upon exit from this routine.
    350  * The storage is trivially management on the call stack in user land, but
    351  * is mallocated in kernel land.
    352  */
    353 
    354 void
    355 svc_getreq(rdfds)
    356 	int rdfds;
    357 {
    358 	fd_set readfds;
    359 
    360 	FD_ZERO(&readfds);
    361 	readfds.fds_bits[0] = rdfds;
    362 	svc_getreqset(&readfds);
    363 }
    364 
    365 void
    366 svc_getreqset(readfds)
    367 	fd_set *readfds;
    368 {
    369 	enum xprt_stat stat;
    370 	struct rpc_msg msg;
    371 	int prog_found;
    372 	u_long low_vers;
    373 	u_long high_vers;
    374 	struct svc_req r;
    375 	register SVCXPRT *xprt;
    376 	register u_long mask;
    377 	register int bit;
    378 	register u_long *maskp;
    379 	register int sock;
    380 	char cred_area[2*MAX_AUTH_BYTES + RQCRED_SIZE];
    381 	msg.rm_call.cb_cred.oa_base = cred_area;
    382 	msg.rm_call.cb_verf.oa_base = &(cred_area[MAX_AUTH_BYTES]);
    383 	r.rq_clntcred = &(cred_area[2*MAX_AUTH_BYTES]);
    384 
    385 
    386 	maskp = (u_long *)readfds->fds_bits;
    387 	for (sock = 0; sock < FD_SETSIZE; sock += NFDBITS) {
    388 	    for (mask = *maskp++; bit = ffs(mask); mask ^= (1 << (bit - 1))) {
    389 		/* sock has input waiting */
    390 		xprt = xports[sock + bit - 1];
    391 		/* now receive msgs from xprtprt (support batch calls) */
    392 		do {
    393 			if (SVC_RECV(xprt, &msg)) {
    394 
    395 				/* now find the exported program and call it */
    396 				register struct svc_callout *s;
    397 				enum auth_stat why;
    398 
    399 				r.rq_xprt = xprt;
    400 				r.rq_prog = msg.rm_call.cb_prog;
    401 				r.rq_vers = msg.rm_call.cb_vers;
    402 				r.rq_proc = msg.rm_call.cb_proc;
    403 				r.rq_cred = msg.rm_call.cb_cred;
    404 				/* first authenticate the message */
    405 				if ((why= _authenticate(&r, &msg)) != AUTH_OK) {
    406 					svcerr_auth(xprt, why);
    407 					goto call_done;
    408 				}
    409 				/* now match message with a registered service*/
    410 				prog_found = FALSE;
    411 				low_vers = 0 - 1;
    412 				high_vers = 0;
    413 				for (s = svc_head; s != NULL_SVC; s = s->sc_next) {
    414 					if (s->sc_prog == r.rq_prog) {
    415 						if (s->sc_vers == r.rq_vers) {
    416 							(*s->sc_dispatch)(&r, xprt);
    417 							goto call_done;
    418 						}  /* found correct version */
    419 						prog_found = TRUE;
    420 						if (s->sc_vers < low_vers)
    421 							low_vers = s->sc_vers;
    422 						if (s->sc_vers > high_vers)
    423 							high_vers = s->sc_vers;
    424 					}   /* found correct program */
    425 				}
    426 				/*
    427 				 * if we got here, the program or version
    428 				 * is not served ...
    429 				 */
    430 				if (prog_found)
    431 					svcerr_progvers(xprt,
    432 					low_vers, high_vers);
    433 				else
    434 					 svcerr_noprog(xprt);
    435 				/* Fall through to ... */
    436 			}
    437 		call_done:
    438 			if ((stat = SVC_STAT(xprt)) == XPRT_DIED){
    439 				SVC_DESTROY(xprt);
    440 				break;
    441 			}
    442 		} while (stat == XPRT_MOREREQS);
    443 	    }
    444 	}
    445 }
    446