Home | History | Annotate | Line # | Download | only in rpc
svc.c revision 1.5
      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.5 1995/01/04 02:58:45 mycroft 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 <stdlib.h>
     47 
     48 #include <sys/errno.h>
     49 #include <rpc/rpc.h>
     50 #include <rpc/pmap_clnt.h>
     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 		if (sock == svc_maxfd) {
    109 			for (svc_maxfd--; svc_maxfd>=0; svc_maxfd--)
    110 				if (xports[svc_maxfd])
    111 					break;
    112 		}
    113 	}
    114 }
    115 
    116 
    117 /* ********************** CALLOUT list related stuff ************* */
    118 
    119 /*
    120  * Add a service program to the callout list.
    121  * The dispatch routine will be called when a rpc request for this
    122  * program number comes in.
    123  */
    124 bool_t
    125 svc_register(xprt, prog, vers, dispatch, protocol)
    126 	SVCXPRT *xprt;
    127 	u_long prog;
    128 	u_long vers;
    129 	void (*dispatch)();
    130 	int protocol;
    131 {
    132 	struct svc_callout *prev;
    133 	register struct svc_callout *s;
    134 
    135 	if ((s = svc_find(prog, vers, &prev)) != NULL_SVC) {
    136 		if (s->sc_dispatch == dispatch)
    137 			goto pmap_it;  /* he is registering another xptr */
    138 		return (FALSE);
    139 	}
    140 	s = (struct svc_callout *)mem_alloc(sizeof(struct svc_callout));
    141 	if (s == (struct svc_callout *)0) {
    142 		return (FALSE);
    143 	}
    144 	s->sc_prog = prog;
    145 	s->sc_vers = vers;
    146 	s->sc_dispatch = dispatch;
    147 	s->sc_next = svc_head;
    148 	svc_head = s;
    149 pmap_it:
    150 	/* now register the information with the local binder service */
    151 	if (protocol) {
    152 		return (pmap_set(prog, vers, protocol, xprt->xp_port));
    153 	}
    154 	return (TRUE);
    155 }
    156 
    157 /*
    158  * Remove a service program from the callout list.
    159  */
    160 void
    161 svc_unregister(prog, vers)
    162 	u_long prog;
    163 	u_long vers;
    164 {
    165 	struct svc_callout *prev;
    166 	register struct svc_callout *s;
    167 
    168 	if ((s = svc_find(prog, vers, &prev)) == NULL_SVC)
    169 		return;
    170 	if (prev == NULL_SVC) {
    171 		svc_head = s->sc_next;
    172 	} else {
    173 		prev->sc_next = s->sc_next;
    174 	}
    175 	s->sc_next = NULL_SVC;
    176 	mem_free((char *) s, (u_int) sizeof(struct svc_callout));
    177 	/* now unregister the information with the local binder service */
    178 	(void)pmap_unset(prog, vers);
    179 }
    180 
    181 /*
    182  * Search the callout list for a program number, return the callout
    183  * struct.
    184  */
    185 static struct svc_callout *
    186 svc_find(prog, vers, prev)
    187 	u_long prog;
    188 	u_long vers;
    189 	struct svc_callout **prev;
    190 {
    191 	register struct svc_callout *s, *p;
    192 
    193 	p = NULL_SVC;
    194 	for (s = svc_head; s != NULL_SVC; s = s->sc_next) {
    195 		if ((s->sc_prog == prog) && (s->sc_vers == vers))
    196 			goto done;
    197 		p = s;
    198 	}
    199 done:
    200 	*prev = p;
    201 	return (s);
    202 }
    203 
    204 /* ******************* REPLY GENERATION ROUTINES  ************ */
    205 
    206 /*
    207  * Send a reply to an rpc request
    208  */
    209 bool_t
    210 svc_sendreply(xprt, xdr_results, xdr_location)
    211 	register SVCXPRT *xprt;
    212 	xdrproc_t xdr_results;
    213 	caddr_t xdr_location;
    214 {
    215 	struct rpc_msg rply;
    216 
    217 	rply.rm_direction = REPLY;
    218 	rply.rm_reply.rp_stat = MSG_ACCEPTED;
    219 	rply.acpted_rply.ar_verf = xprt->xp_verf;
    220 	rply.acpted_rply.ar_stat = SUCCESS;
    221 	rply.acpted_rply.ar_results.where = xdr_location;
    222 	rply.acpted_rply.ar_results.proc = xdr_results;
    223 	return (SVC_REPLY(xprt, &rply));
    224 }
    225 
    226 /*
    227  * No procedure error reply
    228  */
    229 void
    230 svcerr_noproc(xprt)
    231 	register SVCXPRT *xprt;
    232 {
    233 	struct rpc_msg rply;
    234 
    235 	rply.rm_direction = REPLY;
    236 	rply.rm_reply.rp_stat = MSG_ACCEPTED;
    237 	rply.acpted_rply.ar_verf = xprt->xp_verf;
    238 	rply.acpted_rply.ar_stat = PROC_UNAVAIL;
    239 	SVC_REPLY(xprt, &rply);
    240 }
    241 
    242 /*
    243  * Can't decode args error reply
    244  */
    245 void
    246 svcerr_decode(xprt)
    247 	register SVCXPRT *xprt;
    248 {
    249 	struct rpc_msg rply;
    250 
    251 	rply.rm_direction = REPLY;
    252 	rply.rm_reply.rp_stat = MSG_ACCEPTED;
    253 	rply.acpted_rply.ar_verf = xprt->xp_verf;
    254 	rply.acpted_rply.ar_stat = GARBAGE_ARGS;
    255 	SVC_REPLY(xprt, &rply);
    256 }
    257 
    258 /*
    259  * Some system error
    260  */
    261 void
    262 svcerr_systemerr(xprt)
    263 	register SVCXPRT *xprt;
    264 {
    265 	struct rpc_msg rply;
    266 
    267 	rply.rm_direction = REPLY;
    268 	rply.rm_reply.rp_stat = MSG_ACCEPTED;
    269 	rply.acpted_rply.ar_verf = xprt->xp_verf;
    270 	rply.acpted_rply.ar_stat = SYSTEM_ERR;
    271 	SVC_REPLY(xprt, &rply);
    272 }
    273 
    274 /*
    275  * Authentication error reply
    276  */
    277 void
    278 svcerr_auth(xprt, why)
    279 	SVCXPRT *xprt;
    280 	enum auth_stat why;
    281 {
    282 	struct rpc_msg rply;
    283 
    284 	rply.rm_direction = REPLY;
    285 	rply.rm_reply.rp_stat = MSG_DENIED;
    286 	rply.rjcted_rply.rj_stat = AUTH_ERROR;
    287 	rply.rjcted_rply.rj_why = why;
    288 	SVC_REPLY(xprt, &rply);
    289 }
    290 
    291 /*
    292  * Auth too weak error reply
    293  */
    294 void
    295 svcerr_weakauth(xprt)
    296 	SVCXPRT *xprt;
    297 {
    298 
    299 	svcerr_auth(xprt, AUTH_TOOWEAK);
    300 }
    301 
    302 /*
    303  * Program unavailable error reply
    304  */
    305 void
    306 svcerr_noprog(xprt)
    307 	register SVCXPRT *xprt;
    308 {
    309 	struct rpc_msg rply;
    310 
    311 	rply.rm_direction = REPLY;
    312 	rply.rm_reply.rp_stat = MSG_ACCEPTED;
    313 	rply.acpted_rply.ar_verf = xprt->xp_verf;
    314 	rply.acpted_rply.ar_stat = PROG_UNAVAIL;
    315 	SVC_REPLY(xprt, &rply);
    316 }
    317 
    318 /*
    319  * Program version mismatch error reply
    320  */
    321 void
    322 svcerr_progvers(xprt, low_vers, high_vers)
    323 	register SVCXPRT *xprt;
    324 	u_long low_vers;
    325 	u_long high_vers;
    326 {
    327 	struct rpc_msg rply;
    328 
    329 	rply.rm_direction = REPLY;
    330 	rply.rm_reply.rp_stat = MSG_ACCEPTED;
    331 	rply.acpted_rply.ar_verf = xprt->xp_verf;
    332 	rply.acpted_rply.ar_stat = PROG_MISMATCH;
    333 	rply.acpted_rply.ar_vers.low = low_vers;
    334 	rply.acpted_rply.ar_vers.high = high_vers;
    335 	SVC_REPLY(xprt, &rply);
    336 }
    337 
    338 /* ******************* SERVER INPUT STUFF ******************* */
    339 
    340 /*
    341  * Get server side input from some transport.
    342  *
    343  * Statement of authentication parameters management:
    344  * This function owns and manages all authentication parameters, specifically
    345  * the "raw" parameters (msg.rm_call.cb_cred and msg.rm_call.cb_verf) and
    346  * the "cooked" credentials (rqst->rq_clntcred).
    347  * However, this function does not know the structure of the cooked
    348  * credentials, so it make the following assumptions:
    349  *   a) the structure is contiguous (no pointers), and
    350  *   b) the cred structure size does not exceed RQCRED_SIZE bytes.
    351  * In all events, all three parameters are freed upon exit from this routine.
    352  * The storage is trivially management on the call stack in user land, but
    353  * is mallocated in kernel land.
    354  */
    355 
    356 void
    357 svc_getreq(rdfds)
    358 	int rdfds;
    359 {
    360 	fd_set readfds;
    361 
    362 	FD_ZERO(&readfds);
    363 	readfds.fds_bits[0] = rdfds;
    364 	svc_getreqset(&readfds);
    365 }
    366 
    367 void
    368 svc_getreqset(readfds)
    369 	fd_set *readfds;
    370 {
    371 	enum xprt_stat stat;
    372 	struct rpc_msg msg;
    373 	int prog_found;
    374 	u_long low_vers;
    375 	u_long high_vers;
    376 	struct svc_req r;
    377 	register SVCXPRT *xprt;
    378 	register int bit;
    379 	register u_int32_t mask, *maskp;
    380 	register int sock;
    381 	char cred_area[2*MAX_AUTH_BYTES + RQCRED_SIZE];
    382 	msg.rm_call.cb_cred.oa_base = cred_area;
    383 	msg.rm_call.cb_verf.oa_base = &(cred_area[MAX_AUTH_BYTES]);
    384 	r.rq_clntcred = &(cred_area[2*MAX_AUTH_BYTES]);
    385 
    386 
    387 	maskp = readfds->fds_bits;
    388 	for (sock = 0; sock < FD_SETSIZE; sock += NFDBITS) {
    389 	    for (mask = *maskp++; bit = ffs(mask); mask ^= (1 << (bit - 1))) {
    390 		/* sock has input waiting */
    391 		xprt = xports[sock + bit - 1];
    392 		/* now receive msgs from xprtprt (support batch calls) */
    393 		do {
    394 			if (SVC_RECV(xprt, &msg)) {
    395 
    396 				/* now find the exported program and call it */
    397 				register struct svc_callout *s;
    398 				enum auth_stat why;
    399 
    400 				r.rq_xprt = xprt;
    401 				r.rq_prog = msg.rm_call.cb_prog;
    402 				r.rq_vers = msg.rm_call.cb_vers;
    403 				r.rq_proc = msg.rm_call.cb_proc;
    404 				r.rq_cred = msg.rm_call.cb_cred;
    405 				/* first authenticate the message */
    406 				if ((why= _authenticate(&r, &msg)) != AUTH_OK) {
    407 					svcerr_auth(xprt, why);
    408 					goto call_done;
    409 				}
    410 				/* now match message with a registered service*/
    411 				prog_found = FALSE;
    412 				low_vers = 0 - 1;
    413 				high_vers = 0;
    414 				for (s = svc_head; s != NULL_SVC; s = s->sc_next) {
    415 					if (s->sc_prog == r.rq_prog) {
    416 						if (s->sc_vers == r.rq_vers) {
    417 							(*s->sc_dispatch)(&r, xprt);
    418 							goto call_done;
    419 						}  /* found correct version */
    420 						prog_found = TRUE;
    421 						if (s->sc_vers < low_vers)
    422 							low_vers = s->sc_vers;
    423 						if (s->sc_vers > high_vers)
    424 							high_vers = s->sc_vers;
    425 					}   /* found correct program */
    426 				}
    427 				/*
    428 				 * if we got here, the program or version
    429 				 * is not served ...
    430 				 */
    431 				if (prog_found)
    432 					svcerr_progvers(xprt,
    433 					low_vers, high_vers);
    434 				else
    435 					 svcerr_noprog(xprt);
    436 				/* Fall through to ... */
    437 			}
    438 		call_done:
    439 			if ((stat = SVC_STAT(xprt)) == XPRT_DIED){
    440 				SVC_DESTROY(xprt);
    441 				break;
    442 			}
    443 		} while (stat == XPRT_MOREREQS);
    444 	    }
    445 	}
    446 }
    447