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