svc.c revision 50476
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";*/
33static char *rcsid = "$FreeBSD: head/lib/libc/rpc/svc.c 50476 1999-08-28 00:22:10Z peter $";
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 <string.h>
47#include <stdlib.h>
48#include <sys/errno.h>
49#include <rpc/rpc.h>
50#include <rpc/pmap_clnt.h>
51
52static SVCXPRT **xports;
53static int xportssize;
54
55#define NULL_SVC ((struct svc_callout *)0)
56#define	RQCRED_SIZE	400		/* this size is excessive */
57
58#define max(a, b) (a > b ? a : b)
59
60/*
61 * The services list
62 * Each entry represents a set of procedures (an rpc program).
63 * The dispatch routine takes request structs and runs the
64 * apropriate procedure.
65 */
66static struct svc_callout {
67	struct svc_callout *sc_next;
68	u_long		    sc_prog;
69	u_long		    sc_vers;
70	void		    (*sc_dispatch)();
71} *svc_head;
72
73static struct svc_callout *svc_find();
74
75int __svc_fdsetsize = 0;
76fd_set *__svc_fdset = NULL;
77
78/* ***************  SVCXPRT related stuff **************** */
79
80/*
81 * Activate a transport handle.
82 */
83void
84xprt_register(xprt)
85	SVCXPRT *xprt;
86{
87	register int sock = xprt->xp_sock;
88
89	if (sock + 1 > __svc_fdsetsize) {
90		int bytes = howmany(sock + 1, NFDBITS) * sizeof(fd_mask);
91		fd_set *fds;
92
93		fds = (fd_set *)malloc(bytes);
94		memset(fds, 0, bytes);
95		if (__svc_fdset) {
96			memcpy(fds, __svc_fdset, howmany(__svc_fdsetsize,
97				NFDBITS) * sizeof(fd_mask));
98			free(__svc_fdset);
99		}
100		__svc_fdset = fds;
101		__svc_fdsetsize = howmany(sock+1, NFDBITS) * NFDBITS;
102	}
103
104	if (sock < FD_SETSIZE)
105		FD_SET(sock, &svc_fdset);
106	FD_SET(sock, __svc_fdset);
107
108	if (xports == NULL || sock + 1 > xportssize) {
109		SVCXPRT **xp;
110		int size = FD_SETSIZE;
111
112		if (sock + 1 > size)
113			size = sock + 1;
114		xp = (SVCXPRT **)mem_alloc(size * sizeof(SVCXPRT *));
115		memset(xp, 0, size * sizeof(SVCXPRT *));
116		if (xports) {
117			memcpy(xp, xports, xportssize * sizeof(SVCXPRT *));
118			free(xports);
119		}
120		xportssize = size;
121		xports = xp;
122	}
123	xports[sock] = xprt;
124	svc_maxfd = max(svc_maxfd, sock);
125}
126
127/*
128 * De-activate a transport handle.
129 */
130void
131xprt_unregister(xprt)
132	SVCXPRT *xprt;
133{
134	register int sock = xprt->xp_sock;
135
136	if (xports[sock] == xprt) {
137		xports[sock] = (SVCXPRT *)0;
138		if (sock < FD_SETSIZE)
139			FD_CLR(sock, &svc_fdset);
140		FD_CLR(sock, __svc_fdset);
141		if (sock == svc_maxfd) {
142			for (svc_maxfd--; svc_maxfd >= 0; svc_maxfd--)
143				if (xports[svc_maxfd])
144					break;
145		}
146		/*
147		 * XXX could use svc_maxfd as a hint to
148		 * decrease the size of __svc_fdset
149		 */
150	}
151}
152
153
154/* ********************** CALLOUT list related stuff ************* */
155
156/*
157 * Add a service program to the callout list.
158 * The dispatch routine will be called when a rpc request for this
159 * program number comes in.
160 */
161bool_t
162svc_register(xprt, prog, vers, dispatch, protocol)
163	SVCXPRT *xprt;
164	u_long prog;
165	u_long vers;
166	void (*dispatch)();
167	int protocol;
168{
169	struct svc_callout *prev;
170	register struct svc_callout *s;
171
172	if ((s = svc_find(prog, vers, &prev)) != NULL_SVC) {
173		if (s->sc_dispatch == dispatch)
174			goto pmap_it;  /* he is registering another xptr */
175		return (FALSE);
176	}
177	s = (struct svc_callout *)mem_alloc(sizeof(struct svc_callout));
178	if (s == (struct svc_callout *)0) {
179		return (FALSE);
180	}
181	s->sc_prog = prog;
182	s->sc_vers = vers;
183	s->sc_dispatch = dispatch;
184	s->sc_next = svc_head;
185	svc_head = s;
186pmap_it:
187	/* now register the information with the local binder service */
188	if (protocol) {
189		return (pmap_set(prog, vers, protocol, xprt->xp_port));
190	}
191	return (TRUE);
192}
193
194/*
195 * Remove a service program from the callout list.
196 */
197void
198svc_unregister(prog, vers)
199	u_long prog;
200	u_long vers;
201{
202	struct svc_callout *prev;
203	register struct svc_callout *s;
204
205	if ((s = svc_find(prog, vers, &prev)) == NULL_SVC)
206		return;
207	if (prev == NULL_SVC) {
208		svc_head = s->sc_next;
209	} else {
210		prev->sc_next = s->sc_next;
211	}
212	s->sc_next = NULL_SVC;
213	mem_free((char *) s, (u_int) sizeof(struct svc_callout));
214	/* now unregister the information with the local binder service */
215	(void)pmap_unset(prog, vers);
216}
217
218/*
219 * Search the callout list for a program number, return the callout
220 * struct.
221 */
222static struct svc_callout *
223svc_find(prog, vers, prev)
224	u_long prog;
225	u_long vers;
226	struct svc_callout **prev;
227{
228	register struct svc_callout *s, *p;
229
230	p = NULL_SVC;
231	for (s = svc_head; s != NULL_SVC; s = s->sc_next) {
232		if ((s->sc_prog == prog) && (s->sc_vers == vers))
233			goto done;
234		p = s;
235	}
236done:
237	*prev = p;
238	return (s);
239}
240
241/* ******************* REPLY GENERATION ROUTINES  ************ */
242
243/*
244 * Send a reply to an rpc request
245 */
246bool_t
247svc_sendreply(xprt, xdr_results, xdr_location)
248	register SVCXPRT *xprt;
249	xdrproc_t xdr_results;
250	caddr_t xdr_location;
251{
252	struct rpc_msg rply;
253
254	rply.rm_direction = REPLY;
255	rply.rm_reply.rp_stat = MSG_ACCEPTED;
256	rply.acpted_rply.ar_verf = xprt->xp_verf;
257	rply.acpted_rply.ar_stat = SUCCESS;
258	rply.acpted_rply.ar_results.where = xdr_location;
259	rply.acpted_rply.ar_results.proc = xdr_results;
260	return (SVC_REPLY(xprt, &rply));
261}
262
263/*
264 * No procedure error reply
265 */
266void
267svcerr_noproc(xprt)
268	register SVCXPRT *xprt;
269{
270	struct rpc_msg rply;
271
272	rply.rm_direction = REPLY;
273	rply.rm_reply.rp_stat = MSG_ACCEPTED;
274	rply.acpted_rply.ar_verf = xprt->xp_verf;
275	rply.acpted_rply.ar_stat = PROC_UNAVAIL;
276	SVC_REPLY(xprt, &rply);
277}
278
279/*
280 * Can't decode args error reply
281 */
282void
283svcerr_decode(xprt)
284	register SVCXPRT *xprt;
285{
286	struct rpc_msg rply;
287
288	rply.rm_direction = REPLY;
289	rply.rm_reply.rp_stat = MSG_ACCEPTED;
290	rply.acpted_rply.ar_verf = xprt->xp_verf;
291	rply.acpted_rply.ar_stat = GARBAGE_ARGS;
292	SVC_REPLY(xprt, &rply);
293}
294
295/*
296 * Some system error
297 */
298void
299svcerr_systemerr(xprt)
300	register SVCXPRT *xprt;
301{
302	struct rpc_msg rply;
303
304	rply.rm_direction = REPLY;
305	rply.rm_reply.rp_stat = MSG_ACCEPTED;
306	rply.acpted_rply.ar_verf = xprt->xp_verf;
307	rply.acpted_rply.ar_stat = SYSTEM_ERR;
308	SVC_REPLY(xprt, &rply);
309}
310
311/*
312 * Authentication error reply
313 */
314void
315svcerr_auth(xprt, why)
316	SVCXPRT *xprt;
317	enum auth_stat why;
318{
319	struct rpc_msg rply;
320
321	rply.rm_direction = REPLY;
322	rply.rm_reply.rp_stat = MSG_DENIED;
323	rply.rjcted_rply.rj_stat = AUTH_ERROR;
324	rply.rjcted_rply.rj_why = why;
325	SVC_REPLY(xprt, &rply);
326}
327
328/*
329 * Auth too weak error reply
330 */
331void
332svcerr_weakauth(xprt)
333	SVCXPRT *xprt;
334{
335
336	svcerr_auth(xprt, AUTH_TOOWEAK);
337}
338
339/*
340 * Program unavailable error reply
341 */
342void
343svcerr_noprog(xprt)
344	register SVCXPRT *xprt;
345{
346	struct rpc_msg rply;
347
348	rply.rm_direction = REPLY;
349	rply.rm_reply.rp_stat = MSG_ACCEPTED;
350	rply.acpted_rply.ar_verf = xprt->xp_verf;
351	rply.acpted_rply.ar_stat = PROG_UNAVAIL;
352	SVC_REPLY(xprt, &rply);
353}
354
355/*
356 * Program version mismatch error reply
357 */
358void
359svcerr_progvers(xprt, low_vers, high_vers)
360	register SVCXPRT *xprt;
361	u_long low_vers;
362	u_long high_vers;
363{
364	struct rpc_msg rply;
365
366	rply.rm_direction = REPLY;
367	rply.rm_reply.rp_stat = MSG_ACCEPTED;
368	rply.acpted_rply.ar_verf = xprt->xp_verf;
369	rply.acpted_rply.ar_stat = PROG_MISMATCH;
370	rply.acpted_rply.ar_vers.low = low_vers;
371	rply.acpted_rply.ar_vers.high = high_vers;
372	SVC_REPLY(xprt, &rply);
373}
374
375/* ******************* SERVER INPUT STUFF ******************* */
376
377/*
378 * Get server side input from some transport.
379 *
380 * Statement of authentication parameters management:
381 * This function owns and manages all authentication parameters, specifically
382 * the "raw" parameters (msg.rm_call.cb_cred and msg.rm_call.cb_verf) and
383 * the "cooked" credentials (rqst->rq_clntcred).
384 * However, this function does not know the structure of the cooked
385 * credentials, so it make the following assumptions:
386 *   a) the structure is contiguous (no pointers), and
387 *   b) the cred structure size does not exceed RQCRED_SIZE bytes.
388 * In all events, all three parameters are freed upon exit from this routine.
389 * The storage is trivially management on the call stack in user land, but
390 * is mallocated in kernel land.
391 */
392
393void
394svc_getreq(rdfds)
395	int rdfds;
396{
397	fd_set readfds;
398
399	FD_ZERO(&readfds);
400	readfds.fds_bits[0] = rdfds;
401	svc_getreqset(&readfds);
402}
403
404void
405svc_getreqset(readfds)
406	fd_set *readfds;
407{
408	svc_getreqset2(readfds, FD_SETSIZE);
409}
410
411void
412svc_getreqset2(readfds, width)
413	fd_set *readfds;
414	int width;
415{
416	enum xprt_stat stat;
417	struct rpc_msg msg;
418	int prog_found;
419	u_long low_vers;
420	u_long high_vers;
421	struct svc_req r;
422	register SVCXPRT *xprt;
423	register int bit;
424	register int sock;
425	register fd_mask mask, *maskp;
426	char cred_area[2*MAX_AUTH_BYTES + RQCRED_SIZE];
427	msg.rm_call.cb_cred.oa_base = cred_area;
428	msg.rm_call.cb_verf.oa_base = &(cred_area[MAX_AUTH_BYTES]);
429	r.rq_clntcred = &(cred_area[2*MAX_AUTH_BYTES]);
430
431
432	maskp = readfds->fds_bits;
433	for (sock = 0; sock < width; sock += NFDBITS) {
434	    for (mask = *maskp++; (bit = ffs(mask)); mask ^= (1 << (bit - 1))) {
435		/* sock has input waiting */
436		xprt = xports[sock + bit - 1];
437		if (xprt == NULL)
438			/* But do we control sock? */
439			continue;
440		/* now receive msgs from xprtprt (support batch calls) */
441		do {
442			if (SVC_RECV(xprt, &msg)) {
443
444				/* now find the exported program and call it */
445				register struct svc_callout *s;
446				enum auth_stat why;
447
448				r.rq_xprt = xprt;
449				r.rq_prog = msg.rm_call.cb_prog;
450				r.rq_vers = msg.rm_call.cb_vers;
451				r.rq_proc = msg.rm_call.cb_proc;
452				r.rq_cred = msg.rm_call.cb_cred;
453				/* first authenticate the message */
454				if ((why= _authenticate(&r, &msg)) != AUTH_OK) {
455					svcerr_auth(xprt, why);
456					goto call_done;
457				}
458				/* now match message with a registered service*/
459				prog_found = FALSE;
460				low_vers = (u_long) - 1;
461				high_vers = 0;
462				for (s = svc_head; s != NULL_SVC; s = s->sc_next) {
463					if (s->sc_prog == r.rq_prog) {
464						if (s->sc_vers == r.rq_vers) {
465							(*s->sc_dispatch)(&r, xprt);
466							goto call_done;
467						}  /* found correct version */
468						prog_found = TRUE;
469						if (s->sc_vers < low_vers)
470							low_vers = s->sc_vers;
471						if (s->sc_vers > high_vers)
472							high_vers = s->sc_vers;
473					}   /* found correct program */
474				}
475				/*
476				 * if we got here, the program or version
477				 * is not served ...
478				 */
479				if (prog_found)
480					svcerr_progvers(xprt,
481					low_vers, high_vers);
482				else
483					 svcerr_noprog(xprt);
484				/* Fall through to ... */
485			}
486		call_done:
487			if ((stat = SVC_STAT(xprt)) == XPRT_DIED){
488				SVC_DESTROY(xprt);
489				break;
490			}
491		} while (stat == XPRT_MOREREQS);
492	    }
493	}
494}
495