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