1 /* $NetBSD: svc.c,v 1.21 2000/07/06 03:10:35 christos Exp $ */
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.
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.
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.
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.
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.
27 * Sun Microsystems, Inc.
29 * Mountain View, California 94043
32 #if defined(LIBC_SCCS) && !defined(lint)
33 static char *sccsid2 = "@(#)svc.c 1.44 88/02/08 Copyr 1984 Sun Micro";
34 static char *sccsid = "@(#)svc.c 2.4 88/08/11 4.0 RPCSRC";
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
40 * svc.c, Server-side remote procedure call interface.
42 * There are two sets of procedures here. The xprt routines are
43 * for handling transport handles. The svc routines handle the
44 * list of service routines.
46 * Copyright (C) 1984, Sun Microsystems, Inc.
49 #include "namespace.h"
50 #include "reentrant.h"
51 #include <sys/types.h>
60 #include <rpc/pmap_clnt.h>
62 #include "un-namespace.h"
67 #define RQCRED_SIZE 400 /* this size is excessive */
69 #define SVC_VERSQUIET 0x0001 /* keep quiet about vers mismatch */
70 #define version_keepquiet(xp) (SVC_EXT(xp)->xp_flags & SVC_VERSQUIET)
72 #define max(a, b) (a > b ? a : b)
76 * Each entry represents a set of procedures (an rpc program).
77 * The dispatch routine takes request structs and runs the
78 * apropriate procedure.
80 static struct svc_callout {
81 struct svc_callout *sc_next;
85 void (*sc_dispatch)(struct svc_req *, SVCXPRT *);
88 static struct svc_callout *svc_find(rpcprog_t, rpcvers_t,
89 struct svc_callout **, char *);
90 static void __xprt_do_unregister (SVCXPRT *xprt, bool_t dolock);
92 /* *************** SVCXPRT related stuff **************** */
95 * Activate a transport handle.
103 assert(xprt != NULL);
107 rwlock_wrlock(&svc_fd_lock);
108 if (__svc_xports == NULL) {
109 __svc_xports = (SVCXPRT **)
110 mem_alloc(FD_SETSIZE * sizeof(SVCXPRT *));
111 if (__svc_xports == NULL)
113 memset(__svc_xports, '\0', FD_SETSIZE * sizeof(SVCXPRT *));
115 if (sock < FD_SETSIZE) {
116 __svc_xports[sock] = xprt;
117 FD_SET(sock, &svc_fdset);
118 svc_maxfd = max(svc_maxfd, sock);
120 rwlock_unlock(&svc_fd_lock);
124 xprt_unregister(SVCXPRT *xprt)
126 __xprt_do_unregister(xprt, TRUE);
130 __xprt_unregister_unlocked(SVCXPRT *xprt)
132 __xprt_do_unregister(xprt, FALSE);
136 * De-activate a transport handle.
139 __xprt_do_unregister(xprt, dolock)
145 assert(xprt != NULL);
150 rwlock_wrlock(&svc_fd_lock);
151 if ((sock < FD_SETSIZE) && (__svc_xports[sock] == xprt)) {
152 __svc_xports[sock] = NULL;
153 FD_CLR(sock, &svc_fdset);
154 if (sock >= svc_maxfd) {
155 for (svc_maxfd--; svc_maxfd>=0; svc_maxfd--)
156 if (__svc_xports[svc_maxfd])
161 rwlock_unlock(&svc_fd_lock);
165 * Add a service program to the callout list.
166 * The dispatch routine will be called when a rpc request for this
167 * program number comes in.
170 svc_reg(xprt, prog, vers, dispatch, nconf)
172 const rpcprog_t prog;
173 const rpcvers_t vers;
174 void (*dispatch)(struct svc_req *, SVCXPRT *);
175 const struct netconfig *nconf;
178 struct svc_callout *prev;
179 struct svc_callout *s;
180 struct netconfig *tnconf;
184 /* VARIABLES PROTECTED BY svc_lock: s, prev, svc_head */
186 if (xprt->xp_netid) {
187 netid = strdup(xprt->xp_netid);
189 } else if (nconf && nconf->nc_netid) {
190 netid = strdup(nconf->nc_netid);
192 } else if ((tnconf = __rpcgettp(xprt->xp_fd)) != NULL) {
193 netid = strdup(tnconf->nc_netid);
195 freenetconfigent(tnconf);
196 } /* must have been created with svc_raw_create */
197 if ((netid == NULL) && (flag == 1)) {
201 rwlock_wrlock(&svc_lock);
202 if ((s = svc_find(prog, vers, &prev, netid)) != NULL) {
205 if (s->sc_dispatch == dispatch)
206 goto rpcb_it; /* he is registering another xptr */
207 rwlock_unlock(&svc_lock);
210 s = mem_alloc(sizeof (struct svc_callout));
214 rwlock_unlock(&svc_lock);
220 s->sc_dispatch = dispatch;
222 s->sc_next = svc_head;
225 if ((xprt->xp_netid == NULL) && (flag == 1) && netid)
226 ((SVCXPRT *) xprt)->xp_netid = strdup(netid);
229 rwlock_unlock(&svc_lock);
230 /* now register the information with the local binder service */
232 /*LINTED const castaway*/
233 dummy = rpcb_set(prog, vers, (struct netconfig *) nconf,
234 &((SVCXPRT *) xprt)->xp_ltaddr);
241 * Remove a service program from the callout list.
244 svc_unreg(prog, vers)
245 const rpcprog_t prog;
246 const rpcvers_t vers;
248 struct svc_callout *prev;
249 struct svc_callout *s;
251 /* unregister the information anyway */
252 (void) rpcb_unset(prog, vers, NULL);
253 rwlock_wrlock(&svc_lock);
254 while ((s = svc_find(prog, vers, &prev, NULL)) != NULL) {
256 svc_head = s->sc_next;
258 prev->sc_next = s->sc_next;
262 mem_free(s->sc_netid, sizeof (s->sc_netid) + 1);
263 mem_free(s, sizeof (struct svc_callout));
265 rwlock_unlock(&svc_lock);
268 /* ********************** CALLOUT list related stuff ************* */
272 * Add a service program to the callout list.
273 * The dispatch routine will be called when a rpc request for this
274 * program number comes in.
277 svc_register(xprt, prog, vers, dispatch, protocol)
281 void (*dispatch)(struct svc_req *, SVCXPRT *);
284 struct svc_callout *prev;
285 struct svc_callout *s;
287 assert(xprt != NULL);
288 assert(dispatch != NULL);
290 if ((s = svc_find((rpcprog_t)prog, (rpcvers_t)vers, &prev, NULL)) !=
292 if (s->sc_dispatch == dispatch)
293 goto pmap_it; /* he is registering another xptr */
296 s = mem_alloc(sizeof(struct svc_callout));
300 s->sc_prog = (rpcprog_t)prog;
301 s->sc_vers = (rpcvers_t)vers;
302 s->sc_dispatch = dispatch;
303 s->sc_next = svc_head;
306 /* now register the information with the local binder service */
308 return (pmap_set(prog, vers, protocol, xprt->xp_port));
314 * Remove a service program from the callout list.
317 svc_unregister(prog, vers)
321 struct svc_callout *prev;
322 struct svc_callout *s;
324 if ((s = svc_find((rpcprog_t)prog, (rpcvers_t)vers, &prev, NULL)) ==
328 svc_head = s->sc_next;
330 prev->sc_next = s->sc_next;
333 mem_free(s, sizeof(struct svc_callout));
334 /* now unregister the information with the local binder service */
335 (void)pmap_unset(prog, vers);
340 * Search the callout list for a program number, return the callout
343 static struct svc_callout *
344 svc_find(prog, vers, prev, netid)
347 struct svc_callout **prev;
350 struct svc_callout *s, *p;
352 assert(prev != NULL);
355 for (s = svc_head; s != NULL; s = s->sc_next) {
356 if (((s->sc_prog == prog) && (s->sc_vers == vers)) &&
357 ((netid == NULL) || (s->sc_netid == NULL) ||
358 (strcmp(netid, s->sc_netid) == 0)))
366 /* ******************* REPLY GENERATION ROUTINES ************ */
369 * Send a reply to an rpc request
372 svc_sendreply(xprt, xdr_results, xdr_location)
374 xdrproc_t xdr_results;
379 assert(xprt != NULL);
381 rply.rm_direction = REPLY;
382 rply.rm_reply.rp_stat = MSG_ACCEPTED;
383 rply.acpted_rply.ar_verf = xprt->xp_verf;
384 rply.acpted_rply.ar_stat = SUCCESS;
385 rply.acpted_rply.ar_results.where = xdr_location;
386 rply.acpted_rply.ar_results.proc = xdr_results;
387 return (SVC_REPLY(xprt, &rply));
391 * No procedure error reply
399 assert(xprt != NULL);
401 rply.rm_direction = REPLY;
402 rply.rm_reply.rp_stat = MSG_ACCEPTED;
403 rply.acpted_rply.ar_verf = xprt->xp_verf;
404 rply.acpted_rply.ar_stat = PROC_UNAVAIL;
405 SVC_REPLY(xprt, &rply);
409 * Can't decode args error reply
417 assert(xprt != NULL);
419 rply.rm_direction = REPLY;
420 rply.rm_reply.rp_stat = MSG_ACCEPTED;
421 rply.acpted_rply.ar_verf = xprt->xp_verf;
422 rply.acpted_rply.ar_stat = GARBAGE_ARGS;
423 SVC_REPLY(xprt, &rply);
430 svcerr_systemerr(xprt)
435 assert(xprt != NULL);
437 rply.rm_direction = REPLY;
438 rply.rm_reply.rp_stat = MSG_ACCEPTED;
439 rply.acpted_rply.ar_verf = xprt->xp_verf;
440 rply.acpted_rply.ar_stat = SYSTEM_ERR;
441 SVC_REPLY(xprt, &rply);
446 * Tell RPC package to not complain about version errors to the client. This
447 * is useful when revving broadcast protocols that sit on a fixed address.
448 * There is really one (or should be only one) example of this kind of
449 * protocol: the portmapper (or rpc binder).
452 __svc_versquiet_on(xprt)
456 SVC_EXT(xprt)->xp_flags |= SVC_VERSQUIET;
460 __svc_versquiet_off(xprt)
464 SVC_EXT(xprt)->xp_flags &= ~SVC_VERSQUIET;
471 __svc_versquiet_on(xprt);
475 __svc_versquiet_get(xprt)
479 return (SVC_EXT(xprt)->xp_flags & SVC_VERSQUIET);
484 * Authentication error reply
487 svcerr_auth(xprt, why)
493 assert(xprt != NULL);
495 rply.rm_direction = REPLY;
496 rply.rm_reply.rp_stat = MSG_DENIED;
497 rply.rjcted_rply.rj_stat = AUTH_ERROR;
498 rply.rjcted_rply.rj_why = why;
499 SVC_REPLY(xprt, &rply);
503 * Auth too weak error reply
506 svcerr_weakauth(xprt)
510 assert(xprt != NULL);
512 svcerr_auth(xprt, AUTH_TOOWEAK);
516 * Program unavailable error reply
524 assert(xprt != NULL);
526 rply.rm_direction = REPLY;
527 rply.rm_reply.rp_stat = MSG_ACCEPTED;
528 rply.acpted_rply.ar_verf = xprt->xp_verf;
529 rply.acpted_rply.ar_stat = PROG_UNAVAIL;
530 SVC_REPLY(xprt, &rply);
534 * Program version mismatch error reply
537 svcerr_progvers(xprt, low_vers, high_vers)
544 assert(xprt != NULL);
546 rply.rm_direction = REPLY;
547 rply.rm_reply.rp_stat = MSG_ACCEPTED;
548 rply.acpted_rply.ar_verf = xprt->xp_verf;
549 rply.acpted_rply.ar_stat = PROG_MISMATCH;
550 rply.acpted_rply.ar_vers.low = (u_int32_t)low_vers;
551 rply.acpted_rply.ar_vers.high = (u_int32_t)high_vers;
552 SVC_REPLY(xprt, &rply);
556 * Allocate a new server transport structure. All fields are
557 * initialized to zero and xp_p3 is initialized to point at an
558 * extension structure to hold various flags and authentication
567 xprt = mem_alloc(sizeof(SVCXPRT));
568 memset(xprt, 0, sizeof(SVCXPRT));
569 ext = mem_alloc(sizeof(SVCXPRT_EXT));
570 memset(ext, 0, sizeof(SVCXPRT_EXT));
572 ext->xp_auth.svc_ah_ops = &svc_auth_null_ops;
578 * Free a server transport structure.
585 mem_free(xprt->xp_p3, sizeof(SVCXPRT_EXT));
586 mem_free(xprt, sizeof(SVCXPRT));
589 /* ******************* SERVER INPUT STUFF ******************* */
592 * Get server side input from some transport.
594 * Statement of authentication parameters management:
595 * This function owns and manages all authentication parameters, specifically
596 * the "raw" parameters (msg.rm_call.cb_cred and msg.rm_call.cb_verf) and
597 * the "cooked" credentials (rqst->rq_clntcred).
598 * However, this function does not know the structure of the cooked
599 * credentials, so it make the following assumptions:
600 * a) the structure is contiguous (no pointers), and
601 * b) the cred structure size does not exceed RQCRED_SIZE bytes.
602 * In all events, all three parameters are freed upon exit from this routine.
603 * The storage is trivially management on the call stack in user land, but
604 * is mallocated in kernel land.
614 readfds.fds_bits[0] = rdfds;
615 svc_getreqset(&readfds);
619 svc_getreqset(readfds)
623 fd_mask mask, *maskp;
626 assert(readfds != NULL);
628 maskp = readfds->fds_bits;
629 for (sock = 0; sock < FD_SETSIZE; sock += NFDBITS) {
630 for (mask = *maskp++; (bit = ffsl(mask)) != 0;
631 mask ^= (1ul << (bit - 1))) {
632 /* sock has input waiting */
634 svc_getreq_common(fd);
640 svc_getreq_common(fd)
650 char cred_area[2*MAX_AUTH_BYTES + RQCRED_SIZE];
652 msg.rm_call.cb_cred.oa_base = cred_area;
653 msg.rm_call.cb_verf.oa_base = &(cred_area[MAX_AUTH_BYTES]);
654 r.rq_clntcred = &(cred_area[2*MAX_AUTH_BYTES]);
656 rwlock_rdlock(&svc_fd_lock);
657 xprt = __svc_xports[fd];
658 rwlock_unlock(&svc_fd_lock);
660 /* But do we control sock? */
662 /* now receive msgs from xprtprt (support batch calls) */
664 if (SVC_RECV(xprt, &msg)) {
666 /* now find the exported program and call it */
667 struct svc_callout *s;
671 r.rq_prog = msg.rm_call.cb_prog;
672 r.rq_vers = msg.rm_call.cb_vers;
673 r.rq_proc = msg.rm_call.cb_proc;
674 r.rq_cred = msg.rm_call.cb_cred;
675 /* first authenticate the message */
676 if ((why = _authenticate(&r, &msg)) != AUTH_OK) {
678 * RPCSEC_GSS uses this return code
679 * for requests that form part of its
680 * context establishment protocol and
681 * should not be dispatched to the
684 if (why != RPCSEC_GSS_NODISPATCH)
685 svcerr_auth(xprt, why);
688 /* now match message with a registered service*/
690 low_vers = (rpcvers_t) -1L;
691 high_vers = (rpcvers_t) 0L;
692 for (s = svc_head; s != NULL; s = s->sc_next) {
693 if (s->sc_prog == r.rq_prog) {
694 if (s->sc_vers == r.rq_vers) {
695 (*s->sc_dispatch)(&r, xprt);
697 } /* found correct version */
699 if (s->sc_vers < low_vers)
700 low_vers = s->sc_vers;
701 if (s->sc_vers > high_vers)
702 high_vers = s->sc_vers;
703 } /* found correct program */
706 * if we got here, the program or version
710 svcerr_progvers(xprt, low_vers, high_vers);
713 /* Fall through to ... */
716 * Check if the xprt has been disconnected in a
717 * recursive call in the service dispatch routine.
720 rwlock_rdlock(&svc_fd_lock);
721 if (xprt != __svc_xports[fd]) {
722 rwlock_unlock(&svc_fd_lock);
725 rwlock_unlock(&svc_fd_lock);
727 if ((stat = SVC_STAT(xprt)) == XPRT_DIED){
731 } while (stat == XPRT_MOREREQS);
736 svc_getreq_poll(pfdp, pollretval)
743 for (i = fds_found = 0; fds_found < pollretval; i++) {
744 struct pollfd *p = &pfdp[i];
747 /* fd has input waiting */
750 * We assume that this function is only called
751 * via someone _select()ing from svc_fdset or
752 * _poll()ing from svc_pollset[]. Thus it's safe
753 * to handle the POLLNVAL event by simply turning
754 * the corresponding bit off in svc_fdset. The
755 * svc_pollset[] array is derived from svc_fdset
756 * and so will also be updated eventually.
758 * XXX Should we do an xprt_unregister() instead?
760 if (p->revents & POLLNVAL) {
761 rwlock_wrlock(&svc_fd_lock);
762 FD_CLR(p->fd, &svc_fdset);
763 rwlock_unlock(&svc_fd_lock);
765 svc_getreq_common(p->fd);
771 rpc_control(int what, void *arg)
776 case RPC_SVC_CONNMAXREC_SET:
782 case RPC_SVC_CONNMAXREC_GET:
783 *(int *)arg = __svc_maxrec;