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
30 #if defined(LIBC_SCCS) && !defined(lint)
31 static char *rcsid = "$OpenBSD: svc.c,v 1.11 1998/02/25 12:21:18 deraadt Exp $";
32 #endif /* LIBC_SCCS and not lint */
35 * svc.c, Server-side remote procedure call interface.
37 * There are two sets of procedures here. The xprt routines are
38 * for handling transport handles. The svc routines handle the
39 * list of service routines.
41 * Copyright (C) 1984, Sun Microsystems, Inc.
47 #include <sys/param.h>
48 #include <sys/errno.h>
50 #include <rpc/pmap_clnt.h>
52 static SVCXPRT **xports;
53 static int xportssize;
55 #define NULL_SVC ((struct svc_callout *)0)
56 #define RQCRED_SIZE 1024 /* this size is excessive */
58 #define max(a, b) (a > b ? a : b)
62 #define fds_bits __fds_bits /* XXX - Linux sux */
68 * Each entry represents a set of procedures (an rpc program).
69 * The dispatch routine takes request structs and runs the
70 * apropriate procedure.
72 static struct svc_callout {
73 struct svc_callout *sc_next;
76 void (*sc_dispatch)();
79 static struct svc_callout *svc_find();
84 /* *************** SVCXPRT related stuff **************** */
87 * Activate a transport handle.
93 register int sock = xprt->xp_sock;
95 if (sock+1 > __svc_fdsetsize) {
96 int bytes = howmany(sock+1, NFDBITS) * sizeof(fd_mask);
99 fds = (fd_set *)malloc(bytes);
100 memset(fds, 0, bytes);
102 memcpy(fds, __svc_fdset, howmany(__svc_fdsetsize,
103 NFDBITS) * sizeof(fd_mask));
107 __svc_fdsetsize = sock+1;
110 if (sock < FD_SETSIZE)
111 FD_SET(sock, &svc_fdset);
112 FD_SET(sock, __svc_fdset);
114 if (xports == NULL || sock+1 > xportssize) {
116 int size = FD_SETSIZE;
120 xp = (SVCXPRT **)mem_alloc(size * sizeof(SVCXPRT *));
121 memset(xp, 0, size * sizeof(SVCXPRT *));
123 memcpy(xp, xports, xportssize * sizeof(SVCXPRT *));
130 svc_maxfd = max(svc_maxfd, sock);
134 * De-activate a transport handle.
137 xprt_unregister(xprt)
140 register int sock = xprt->xp_sock;
142 if (xports[sock] == xprt) {
143 xports[sock] = (SVCXPRT *)0;
144 if (sock < FD_SETSIZE)
145 FD_CLR(sock, &svc_fdset);
146 FD_CLR(sock, __svc_fdset);
147 if (sock == svc_maxfd) {
148 for (svc_maxfd--; svc_maxfd>=0; svc_maxfd--)
149 if (xports[svc_maxfd])
153 * XXX could use svc_maxfd as a hint to
154 * decrease the size of __svc_fdset
160 /* ********************** CALLOUT list related stuff ************* */
163 * Add a service program to the callout list.
164 * The dispatch routine will be called when a rpc request for this
165 * program number comes in.
168 svc_register(xprt, prog, vers, dispatch, protocol)
175 struct svc_callout *prev;
176 register struct svc_callout *s;
178 if ((s = svc_find(prog, vers, &prev)) != NULL_SVC) {
179 if (s->sc_dispatch == dispatch)
180 goto pmap_it; /* he is registering another xptr */
183 s = (struct svc_callout *)mem_alloc(sizeof(struct svc_callout));
184 if (s == (struct svc_callout *)0) {
189 s->sc_dispatch = dispatch;
190 s->sc_next = svc_head;
193 /* now register the information with the local binder service */
195 return (pmap_set(prog, vers, protocol, xprt->xp_port));
201 * Remove a service program from the callout list.
204 svc_unregister(prog, vers)
208 struct svc_callout *prev;
209 register struct svc_callout *s;
211 if ((s = svc_find(prog, vers, &prev)) == NULL_SVC)
213 if (prev == NULL_SVC) {
214 svc_head = s->sc_next;
216 prev->sc_next = s->sc_next;
218 s->sc_next = NULL_SVC;
219 mem_free((char *) s, (u_int) sizeof(struct svc_callout));
220 /* now unregister the information with the local binder service */
221 (void)pmap_unset(prog, vers);
225 * Search the callout list for a program number, return the callout
228 static struct svc_callout *
229 svc_find(prog, vers, prev)
232 struct svc_callout **prev;
234 register struct svc_callout *s, *p;
237 for (s = svc_head; s != NULL_SVC; s = s->sc_next) {
238 if ((s->sc_prog == prog) && (s->sc_vers == vers))
247 /* ******************* REPLY GENERATION ROUTINES ************ */
250 * Send a reply to an rpc request
253 svc_sendreply(xprt, xdr_results, xdr_location)
254 register SVCXPRT *xprt;
255 xdrproc_t xdr_results;
256 caddr_t xdr_location;
260 rply.rm_direction = REPLY;
261 rply.rm_reply.rp_stat = MSG_ACCEPTED;
262 rply.acpted_rply.ar_verf = xprt->xp_verf;
263 rply.acpted_rply.ar_stat = SUCCESS;
264 rply.acpted_rply.ar_results.where = xdr_location;
265 rply.acpted_rply.ar_results.proc = xdr_results;
266 return (SVC_REPLY(xprt, &rply));
270 * No procedure error reply
274 register SVCXPRT *xprt;
278 rply.rm_direction = REPLY;
279 rply.rm_reply.rp_stat = MSG_ACCEPTED;
280 rply.acpted_rply.ar_verf = xprt->xp_verf;
281 rply.acpted_rply.ar_stat = PROC_UNAVAIL;
282 SVC_REPLY(xprt, &rply);
286 * Can't decode args error reply
290 register SVCXPRT *xprt;
294 rply.rm_direction = REPLY;
295 rply.rm_reply.rp_stat = MSG_ACCEPTED;
296 rply.acpted_rply.ar_verf = xprt->xp_verf;
297 rply.acpted_rply.ar_stat = GARBAGE_ARGS;
298 SVC_REPLY(xprt, &rply);
305 svcerr_systemerr(xprt)
306 register SVCXPRT *xprt;
310 rply.rm_direction = REPLY;
311 rply.rm_reply.rp_stat = MSG_ACCEPTED;
312 rply.acpted_rply.ar_verf = xprt->xp_verf;
313 rply.acpted_rply.ar_stat = SYSTEM_ERR;
314 SVC_REPLY(xprt, &rply);
318 * Authentication error reply
321 svcerr_auth(xprt, why)
327 rply.rm_direction = REPLY;
328 rply.rm_reply.rp_stat = MSG_DENIED;
329 rply.rjcted_rply.rj_stat = AUTH_ERROR;
330 rply.rjcted_rply.rj_why = why;
331 SVC_REPLY(xprt, &rply);
335 * Auth too weak error reply
338 svcerr_weakauth(xprt)
342 svcerr_auth(xprt, AUTH_TOOWEAK);
346 * Program unavailable error reply
350 register SVCXPRT *xprt;
354 rply.rm_direction = REPLY;
355 rply.rm_reply.rp_stat = MSG_ACCEPTED;
356 rply.acpted_rply.ar_verf = xprt->xp_verf;
357 rply.acpted_rply.ar_stat = PROG_UNAVAIL;
358 SVC_REPLY(xprt, &rply);
362 * Program version mismatch error reply
365 svcerr_progvers(xprt, low_vers, high_vers)
366 register SVCXPRT *xprt;
372 rply.rm_direction = REPLY;
373 rply.rm_reply.rp_stat = MSG_ACCEPTED;
374 rply.acpted_rply.ar_verf = xprt->xp_verf;
375 rply.acpted_rply.ar_stat = PROG_MISMATCH;
376 rply.acpted_rply.ar_vers.low = low_vers;
377 rply.acpted_rply.ar_vers.high = high_vers;
378 SVC_REPLY(xprt, &rply);
381 /* ******************* SERVER INPUT STUFF ******************* */
384 * Get server side input from some transport.
386 * Statement of authentication parameters management:
387 * This function owns and manages all authentication parameters, specifically
388 * the "raw" parameters (msg.rm_call.cb_cred and msg.rm_call.cb_verf) and
389 * the "cooked" credentials (rqst->rq_clntcred).
390 * However, this function does not know the structure of the cooked
391 * credentials, so it make the following assumptions:
392 * a) the structure is contiguous (no pointers), and
393 * b) the cred structure size does not exceed RQCRED_SIZE bytes.
394 * In all events, all three parameters are freed upon exit from this routine.
395 * The storage is trivially management on the call stack in user land, but
396 * is mallocated in kernel land.
406 readfds.fds_bits[0] = rdfds;
407 svc_getreqset(&readfds);
410 void svc_getreqset2 __P((fd_set *, int));
413 svc_getreqset(readfds)
416 svc_getreqset2(readfds, FD_SETSIZE);
420 svc_getreqset2(readfds, width)
430 register SVCXPRT *xprt;
432 register fd_mask mask, *maskp;
435 char cred_area[2*MAX_AUTH_BYTES + RQCRED_SIZE];
436 msg.rm_call.cb_cred.oa_base = cred_area;
437 msg.rm_call.cb_verf.oa_base = &(cred_area[MAX_AUTH_BYTES]);
438 r.rq_clntcred = &(cred_area[2*MAX_AUTH_BYTES]);
440 maskp = readfds->fds_bits;
441 for (sock = 0; sock < width; sock += NFDBITS) {
442 for (mask = *maskp++; (bit = ffs(mask)); mask ^= (1 << (bit - 1))) {
443 /* sock has input waiting */
444 xprt = xports[sock + bit - 1];
446 /* But do we control sock? */
448 /* now receive msgs from xprtprt (support batch calls) */
450 if (SVC_RECV(xprt, &msg)) {
452 /* now find the exported program and call it */
453 register struct svc_callout *s;
457 r.rq_prog = msg.rm_call.cb_prog;
458 r.rq_vers = msg.rm_call.cb_vers;
459 r.rq_proc = msg.rm_call.cb_proc;
460 r.rq_cred = msg.rm_call.cb_cred;
464 /* first authenticate the message */
465 if ((why = _authenticate(&r, &msg, &no_dispatch)) != AUTH_OK) {
466 svcerr_auth(xprt, why);
469 else if (no_dispatch) {
472 /* now match message with a registered service*/
474 low_vers = (u_long) -1;
476 for (s = svc_head; s != NULL_SVC; s = s->sc_next) {
477 if (s->sc_prog == r.rq_prog) {
478 if (s->sc_vers == r.rq_vers) {
479 (*s->sc_dispatch)(&r, xprt);
481 } /* found correct version */
483 if (s->sc_vers < low_vers)
484 low_vers = s->sc_vers;
485 if (s->sc_vers > high_vers)
486 high_vers = s->sc_vers;
487 } /* found correct program */
490 * if we got here, the program or version
494 svcerr_progvers(xprt,
495 low_vers, high_vers);
498 /* Fall through to ... */
501 if ((stat = SVC_STAT(xprt)) == XPRT_DIED) {
505 } while (stat == XPRT_MOREREQS);