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.
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.
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.
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.
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.
25 * Sun Microsystems, Inc.
27 * Mountain View, California 94043
29 * @(#)svc.c 1.44 88/02/08 Copyr 1984 Sun Micro
30 * @(#)svc.c 2.4 88/08/11 4.0 RPCSRC
31 * $FreeBSD: src/lib/libc/rpc/svc.c,v 1.14.2.1 2001/03/05 10:50:36 obrien Exp $
32 * $DragonFly: src/lib/libcr/rpc/Attic/svc.c,v 1.3 2004/10/25 19:38:25 drhodus Exp $
36 * svc.c, Server-side remote procedure call interface.
38 * There are two sets of procedures here. The xprt routines are
39 * for handling transport handles. The svc routines handle the
40 * list of service routines.
42 * Copyright (C) 1984, Sun Microsystems, Inc.
48 #include <rpc/pmap_clnt.h>
50 static SVCXPRT **xports;
51 static int xportssize;
53 #define NULL_SVC ((struct svc_callout *)0)
54 #define RQCRED_SIZE 400 /* this size is excessive */
56 #define max(a, b) (a > b ? a : b)
60 * Each entry represents a set of procedures (an rpc program).
61 * The dispatch routine takes request structs and runs the
62 * apropriate procedure.
64 static struct svc_callout {
65 struct svc_callout *sc_next;
68 void (*sc_dispatch)();
71 static struct svc_callout *svc_find();
73 int __svc_fdsetsize = 0;
74 fd_set *__svc_fdset = NULL;
76 /* *************** SVCXPRT related stuff **************** */
79 * Activate a transport handle.
85 int sock = xprt->xp_sock;
87 if (sock + 1 > __svc_fdsetsize) {
88 int bytes = howmany(sock + 1, NFDBITS) * sizeof(fd_mask);
91 fds = (fd_set *)malloc(bytes);
92 memset(fds, 0, bytes);
94 memcpy(fds, __svc_fdset, howmany(__svc_fdsetsize,
95 NFDBITS) * sizeof(fd_mask));
99 __svc_fdsetsize = howmany(sock+1, NFDBITS) * NFDBITS;
102 if (sock < FD_SETSIZE)
103 FD_SET(sock, &svc_fdset);
104 FD_SET(sock, __svc_fdset);
106 if (xports == NULL || sock + 1 > xportssize) {
108 int size = FD_SETSIZE;
112 xp = (SVCXPRT **)mem_alloc(size * sizeof(SVCXPRT *));
113 memset(xp, 0, size * sizeof(SVCXPRT *));
115 memcpy(xp, xports, xportssize * sizeof(SVCXPRT *));
122 svc_maxfd = max(svc_maxfd, sock);
126 * De-activate a transport handle.
129 xprt_unregister(xprt)
132 int sock = xprt->xp_sock;
134 if (xports[sock] == xprt) {
135 xports[sock] = (SVCXPRT *)0;
136 if (sock < FD_SETSIZE)
137 FD_CLR(sock, &svc_fdset);
138 FD_CLR(sock, __svc_fdset);
139 if (sock == svc_maxfd) {
140 for (svc_maxfd--; svc_maxfd >= 0; svc_maxfd--)
141 if (xports[svc_maxfd])
145 * XXX could use svc_maxfd as a hint to
146 * decrease the size of __svc_fdset
152 /* ********************** CALLOUT list related stuff ************* */
155 * Add a service program to the callout list.
156 * The dispatch routine will be called when a rpc request for this
157 * program number comes in.
160 svc_register(xprt, prog, vers, dispatch, protocol)
167 struct svc_callout *prev;
168 struct svc_callout *s;
170 if ((s = svc_find(prog, vers, &prev)) != NULL_SVC) {
171 if (s->sc_dispatch == dispatch)
172 goto pmap_it; /* he is registering another xptr */
175 s = (struct svc_callout *)mem_alloc(sizeof(struct svc_callout));
176 if (s == (struct svc_callout *)0) {
181 s->sc_dispatch = dispatch;
182 s->sc_next = svc_head;
185 /* now register the information with the local binder service */
187 return (pmap_set(prog, vers, protocol, xprt->xp_port));
193 * Remove a service program from the callout list.
196 svc_unregister(prog, vers)
200 struct svc_callout *prev;
201 struct svc_callout *s;
203 if ((s = svc_find(prog, vers, &prev)) == NULL_SVC)
205 if (prev == NULL_SVC) {
206 svc_head = s->sc_next;
208 prev->sc_next = s->sc_next;
210 s->sc_next = NULL_SVC;
211 mem_free((char *) s, (u_int) sizeof(struct svc_callout));
212 /* now unregister the information with the local binder service */
213 (void)pmap_unset(prog, vers);
217 * Search the callout list for a program number, return the callout
220 static struct svc_callout *
221 svc_find(prog, vers, prev)
224 struct svc_callout **prev;
226 struct svc_callout *s, *p;
229 for (s = svc_head; s != NULL_SVC; s = s->sc_next) {
230 if ((s->sc_prog == prog) && (s->sc_vers == vers))
239 /* ******************* REPLY GENERATION ROUTINES ************ */
242 * Send a reply to an rpc request
245 svc_sendreply(xprt, xdr_results, xdr_location)
247 xdrproc_t xdr_results;
248 caddr_t xdr_location;
252 rply.rm_direction = REPLY;
253 rply.rm_reply.rp_stat = MSG_ACCEPTED;
254 rply.acpted_rply.ar_verf = xprt->xp_verf;
255 rply.acpted_rply.ar_stat = SUCCESS;
256 rply.acpted_rply.ar_results.where = xdr_location;
257 rply.acpted_rply.ar_results.proc = xdr_results;
258 return (SVC_REPLY(xprt, &rply));
262 * No procedure error reply
270 rply.rm_direction = REPLY;
271 rply.rm_reply.rp_stat = MSG_ACCEPTED;
272 rply.acpted_rply.ar_verf = xprt->xp_verf;
273 rply.acpted_rply.ar_stat = PROC_UNAVAIL;
274 SVC_REPLY(xprt, &rply);
278 * Can't decode args error reply
286 rply.rm_direction = REPLY;
287 rply.rm_reply.rp_stat = MSG_ACCEPTED;
288 rply.acpted_rply.ar_verf = xprt->xp_verf;
289 rply.acpted_rply.ar_stat = GARBAGE_ARGS;
290 SVC_REPLY(xprt, &rply);
297 svcerr_systemerr(xprt)
302 rply.rm_direction = REPLY;
303 rply.rm_reply.rp_stat = MSG_ACCEPTED;
304 rply.acpted_rply.ar_verf = xprt->xp_verf;
305 rply.acpted_rply.ar_stat = SYSTEM_ERR;
306 SVC_REPLY(xprt, &rply);
310 * Authentication error reply
313 svcerr_auth(xprt, why)
319 rply.rm_direction = REPLY;
320 rply.rm_reply.rp_stat = MSG_DENIED;
321 rply.rjcted_rply.rj_stat = AUTH_ERROR;
322 rply.rjcted_rply.rj_why = why;
323 SVC_REPLY(xprt, &rply);
327 * Auth too weak error reply
330 svcerr_weakauth(xprt)
334 svcerr_auth(xprt, AUTH_TOOWEAK);
338 * Program unavailable error reply
346 rply.rm_direction = REPLY;
347 rply.rm_reply.rp_stat = MSG_ACCEPTED;
348 rply.acpted_rply.ar_verf = xprt->xp_verf;
349 rply.acpted_rply.ar_stat = PROG_UNAVAIL;
350 SVC_REPLY(xprt, &rply);
354 * Program version mismatch error reply
357 svcerr_progvers(xprt, low_vers, high_vers)
364 rply.rm_direction = REPLY;
365 rply.rm_reply.rp_stat = MSG_ACCEPTED;
366 rply.acpted_rply.ar_verf = xprt->xp_verf;
367 rply.acpted_rply.ar_stat = PROG_MISMATCH;
368 rply.acpted_rply.ar_vers.low = low_vers;
369 rply.acpted_rply.ar_vers.high = high_vers;
370 SVC_REPLY(xprt, &rply);
373 /* ******************* SERVER INPUT STUFF ******************* */
376 * Get server side input from some transport.
378 * Statement of authentication parameters management:
379 * This function owns and manages all authentication parameters, specifically
380 * the "raw" parameters (msg.rm_call.cb_cred and msg.rm_call.cb_verf) and
381 * the "cooked" credentials (rqst->rq_clntcred).
382 * However, this function does not know the structure of the cooked
383 * credentials, so it make the following assumptions:
384 * a) the structure is contiguous (no pointers), and
385 * b) the cred structure size does not exceed RQCRED_SIZE bytes.
386 * In all events, all three parameters are freed upon exit from this routine.
387 * The storage is trivially management on the call stack in user land, but
388 * is mallocated in kernel land.
398 readfds.fds_bits[0] = rdfds;
399 svc_getreqset(&readfds);
403 svc_getreqset(readfds)
406 svc_getreqset2(readfds, FD_SETSIZE);
410 svc_getreqset2(readfds, width)
423 fd_mask mask, *maskp;
424 char cred_area[2*MAX_AUTH_BYTES + RQCRED_SIZE];
425 msg.rm_call.cb_cred.oa_base = cred_area;
426 msg.rm_call.cb_verf.oa_base = &(cred_area[MAX_AUTH_BYTES]);
427 r.rq_clntcred = &(cred_area[2*MAX_AUTH_BYTES]);
430 maskp = readfds->fds_bits;
431 for (sock = 0; sock < width; sock += NFDBITS) {
432 for (mask = *maskp++; (bit = ffs(mask)); mask ^= (1 << (bit - 1))) {
433 /* sock has input waiting */
434 xprt = xports[sock + bit - 1];
436 /* But do we control sock? */
438 /* now receive msgs from xprtprt (support batch calls) */
440 if (SVC_RECV(xprt, &msg)) {
442 /* now find the exported program and call it */
443 struct svc_callout *s;
447 r.rq_prog = msg.rm_call.cb_prog;
448 r.rq_vers = msg.rm_call.cb_vers;
449 r.rq_proc = msg.rm_call.cb_proc;
450 r.rq_cred = msg.rm_call.cb_cred;
451 /* first authenticate the message */
452 if ((why= _authenticate(&r, &msg)) != AUTH_OK) {
453 svcerr_auth(xprt, why);
456 /* now match message with a registered service*/
458 low_vers = (u_long) - 1;
460 for (s = svc_head; s != NULL_SVC; s = s->sc_next) {
461 if (s->sc_prog == r.rq_prog) {
462 if (s->sc_vers == r.rq_vers) {
463 (*s->sc_dispatch)(&r, xprt);
465 } /* found correct version */
467 if (s->sc_vers < low_vers)
468 low_vers = s->sc_vers;
469 if (s->sc_vers > high_vers)
470 high_vers = s->sc_vers;
471 } /* found correct program */
474 * if we got here, the program or version
478 svcerr_progvers(xprt,
479 low_vers, high_vers);
482 /* Fall through to ... */
485 if ((stat = SVC_STAT(xprt)) == XPRT_DIED){
489 } while (stat == XPRT_MOREREQS);