3 * Copyright (c) 2009, Sun Microsystems, Inc.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 * - Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * - Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * - Neither the name of Sun Microsystems, Inc. nor the names of its
14 * contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
31 * svc.c, Server-side remote procedure call interface.
33 * There are two sets of procedures here. The xprt routines are
34 * for handling transport handles. The svc routines handle the
35 * list of service routines.
37 * Copyright (C) 1984, Sun Microsystems, Inc.
41 #include <reentrant.h>
42 #include <sys/types.h>
51 #include <rpc/pmap_clnt.h>
56 #define RQCRED_SIZE 400 /* this size is excessive */
58 #define SVC_VERSQUIET 0x0001 /* keep quiet about vers mismatch */
59 #define version_keepquiet(xp) ((u_long)(xp)->xp_p3 & SVC_VERSQUIET)
61 #define max(a, b) (a > b ? a : b)
65 * Each entry represents a set of procedures (an rpc program).
66 * The dispatch routine takes request structs and runs the
67 * apropriate procedure.
69 static struct svc_callout
71 struct svc_callout *sc_next;
75 void (*sc_dispatch) (struct svc_req *, SVCXPRT *);
78 extern rwlock_t svc_lock;
79 extern rwlock_t svc_fd_lock;
81 static struct svc_callout *svc_find (rpcprog_t, rpcvers_t,
82 struct svc_callout **, char *);
83 static void __xprt_do_unregister (SVCXPRT * xprt, bool_t dolock);
85 /* *************** SVCXPRT related stuff **************** */
88 * Activate a transport handle.
96 assert (xprt != NULL);
100 rwlock_wrlock (&svc_fd_lock);
101 if (__svc_xports == NULL)
103 __svc_xports = (SVCXPRT **) mem_alloc (FD_SETSIZE * sizeof (SVCXPRT *));
104 if (__svc_xports == NULL)
106 memset (__svc_xports, '\0', FD_SETSIZE * sizeof (SVCXPRT *));
108 if (sock < FD_SETSIZE)
110 __svc_xports[sock] = xprt;
111 FD_SET (sock, &svc_fdset);
112 svc_maxfd = max (svc_maxfd, sock);
114 rwlock_unlock (&svc_fd_lock);
118 xprt_unregister (SVCXPRT * xprt)
120 __xprt_do_unregister (xprt, TRUE);
124 __xprt_unregister_unlocked (SVCXPRT * xprt)
126 __xprt_do_unregister (xprt, FALSE);
130 * De-activate a transport handle.
133 __xprt_do_unregister (xprt, dolock)
139 assert (xprt != NULL);
144 rwlock_wrlock (&svc_fd_lock);
145 if ((sock < FD_SETSIZE) && (__svc_xports[sock] == xprt))
147 __svc_xports[sock] = NULL;
148 FD_CLR (sock, &svc_fdset);
149 if (sock >= svc_maxfd)
151 for (svc_maxfd--; svc_maxfd >= 0; svc_maxfd--)
152 if (__svc_xports[svc_maxfd])
157 rwlock_unlock (&svc_fd_lock);
161 * Add a service program to the callout list.
162 * The dispatch routine will be called when a rpc request for this
163 * program number comes in.
166 svc_reg (xprt, prog, vers, dispatch, nconf)
168 const rpcprog_t prog;
169 const rpcvers_t vers;
170 void (*dispatch) (struct svc_req *, SVCXPRT *);
171 const struct netconfig *nconf;
174 struct svc_callout *prev;
175 struct svc_callout *s;
176 struct netconfig *tnconf;
180 /* VARIABLES PROTECTED BY svc_lock: s, prev, svc_head */
183 netid = strdup (xprt->xp_netid);
186 else if (nconf && nconf->nc_netid)
188 netid = strdup (nconf->nc_netid);
191 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))
202 rwlock_wrlock (&svc_lock);
203 if ((s = svc_find (prog, vers, &prev, netid)) != NULL)
207 if (s->sc_dispatch == dispatch)
208 goto rpcb_it; /* he is registering another xptr */
209 rwlock_unlock (&svc_lock);
212 s = mem_alloc (sizeof (struct svc_callout));
217 rwlock_unlock (&svc_lock);
223 s->sc_dispatch = dispatch;
225 s->sc_next = svc_head;
228 if ((xprt->xp_netid == NULL) && (flag == 1) && netid)
229 ((SVCXPRT *) xprt)->xp_netid = strdup (netid);
232 rwlock_unlock (&svc_lock);
233 /* now register the information with the local binder service */
236 /*LINTED const castaway */
237 dummy = rpcb_set (prog, vers, (struct netconfig *) nconf,
238 &((SVCXPRT *) xprt)->xp_ltaddr);
245 * Remove a service program from the callout list.
248 svc_unreg (prog, vers)
249 const rpcprog_t prog;
250 const rpcvers_t vers;
252 struct svc_callout *prev;
253 struct svc_callout *s;
255 /* unregister the information anyway */
256 (void) rpcb_unset (prog, vers, NULL);
257 rwlock_wrlock (&svc_lock);
258 while ((s = svc_find (prog, vers, &prev, NULL)) != NULL)
262 svc_head = s->sc_next;
266 prev->sc_next = s->sc_next;
270 mem_free (s->sc_netid, sizeof (s->sc_netid) + 1);
271 mem_free (s, sizeof (struct svc_callout));
273 rwlock_unlock (&svc_lock);
276 /* ********************** CALLOUT list related stuff ************* */
280 * Add a service program to the callout list.
281 * The dispatch routine will be called when a rpc request for this
282 * program number comes in.
285 svc_register (xprt, prog, vers, dispatch, protocol)
289 void (*dispatch) (struct svc_req *, SVCXPRT *);
292 struct svc_callout *prev;
293 struct svc_callout *s;
295 assert (xprt != NULL);
296 assert (dispatch != NULL);
298 if ((s = svc_find ((rpcprog_t) prog, (rpcvers_t) vers, &prev, NULL)) !=
301 if (s->sc_dispatch == dispatch)
302 goto pmap_it; /* he is registering another xptr */
305 s = mem_alloc (sizeof (struct svc_callout));
310 s->sc_prog = (rpcprog_t) prog;
311 s->sc_vers = (rpcvers_t) vers;
312 s->sc_dispatch = dispatch;
313 s->sc_next = svc_head;
316 /* now register the information with the local binder service */
319 return (pmap_set (prog, vers, protocol, xprt->xp_port));
325 * Remove a service program from the callout list.
328 svc_unregister (prog, vers)
332 struct svc_callout *prev;
333 struct svc_callout *s;
335 if ((s = svc_find ((rpcprog_t) prog, (rpcvers_t) vers, &prev, NULL)) ==
340 svc_head = s->sc_next;
344 prev->sc_next = s->sc_next;
347 mem_free (s, sizeof (struct svc_callout));
348 /* now unregister the information with the local binder service */
349 (void) pmap_unset (prog, vers);
354 * Search the callout list for a program number, return the callout
357 static struct svc_callout *
358 svc_find (prog, vers, prev, netid)
361 struct svc_callout **prev;
364 struct svc_callout *s, *p;
366 assert (prev != NULL);
369 for (s = svc_head; s != NULL; s = s->sc_next)
371 if (((s->sc_prog == prog) && (s->sc_vers == vers)) &&
372 ((netid == NULL) || (s->sc_netid == NULL) ||
373 (strcmp (netid, s->sc_netid) == 0)))
381 /* ******************* REPLY GENERATION ROUTINES ************ */
384 * Send a reply to an rpc request
387 svc_sendreply (xprt, xdr_results, xdr_location)
389 xdrproc_t xdr_results;
394 assert (xprt != NULL);
396 rply.rm_direction = REPLY;
397 rply.rm_reply.rp_stat = MSG_ACCEPTED;
398 rply.acpted_rply.ar_verf = xprt->xp_verf;
399 rply.acpted_rply.ar_stat = SUCCESS;
400 rply.acpted_rply.ar_results.where = xdr_location;
401 rply.acpted_rply.ar_results.proc = xdr_results;
402 return (SVC_REPLY (xprt, &rply));
406 * No procedure error reply
414 assert (xprt != NULL);
416 rply.rm_direction = REPLY;
417 rply.rm_reply.rp_stat = MSG_ACCEPTED;
418 rply.acpted_rply.ar_verf = xprt->xp_verf;
419 rply.acpted_rply.ar_stat = PROC_UNAVAIL;
420 SVC_REPLY (xprt, &rply);
424 * Can't decode args error reply
432 assert (xprt != NULL);
434 rply.rm_direction = REPLY;
435 rply.rm_reply.rp_stat = MSG_ACCEPTED;
436 rply.acpted_rply.ar_verf = xprt->xp_verf;
437 rply.acpted_rply.ar_stat = GARBAGE_ARGS;
438 SVC_REPLY (xprt, &rply);
445 svcerr_systemerr (xprt)
450 assert (xprt != NULL);
452 rply.rm_direction = REPLY;
453 rply.rm_reply.rp_stat = MSG_ACCEPTED;
454 rply.acpted_rply.ar_verf = xprt->xp_verf;
455 rply.acpted_rply.ar_stat = SYSTEM_ERR;
456 SVC_REPLY (xprt, &rply);
461 * Tell RPC package to not complain about version errors to the client. This
462 * is useful when revving broadcast protocols that sit on a fixed address.
463 * There is really one (or should be only one) example of this kind of
464 * protocol: the portmapper (or rpc binder).
467 __svc_versquiet_on (xprt)
472 tmp = ((u_long) xprt->xp_p3) | SVC_VERSQUIET;
477 __svc_versquiet_off (xprt)
482 tmp = ((u_long) xprt->xp_p3) & ~SVC_VERSQUIET;
490 __svc_versquiet_on (xprt);
494 __svc_versquiet_get (xprt)
497 return ((int) xprt->xp_p3) & SVC_VERSQUIET;
502 * Authentication error reply
505 svcerr_auth (xprt, why)
511 assert (xprt != NULL);
513 rply.rm_direction = REPLY;
514 rply.rm_reply.rp_stat = MSG_DENIED;
515 rply.rjcted_rply.rj_stat = AUTH_ERROR;
516 rply.rjcted_rply.rj_why = why;
517 SVC_REPLY (xprt, &rply);
521 * Auth too weak error reply
524 svcerr_weakauth (xprt)
528 assert (xprt != NULL);
530 svcerr_auth (xprt, AUTH_TOOWEAK);
534 * Program unavailable error reply
542 assert (xprt != NULL);
544 rply.rm_direction = REPLY;
545 rply.rm_reply.rp_stat = MSG_ACCEPTED;
546 rply.acpted_rply.ar_verf = xprt->xp_verf;
547 rply.acpted_rply.ar_stat = PROG_UNAVAIL;
548 SVC_REPLY (xprt, &rply);
552 * Program version mismatch error reply
555 svcerr_progvers (xprt, low_vers, high_vers)
562 assert (xprt != NULL);
564 rply.rm_direction = REPLY;
565 rply.rm_reply.rp_stat = MSG_ACCEPTED;
566 rply.acpted_rply.ar_verf = xprt->xp_verf;
567 rply.acpted_rply.ar_stat = PROG_MISMATCH;
568 rply.acpted_rply.ar_vers.low = (u_int32_t) low_vers;
569 rply.acpted_rply.ar_vers.high = (u_int32_t) high_vers;
570 SVC_REPLY (xprt, &rply);
573 /* ******************* SERVER INPUT STUFF ******************* */
576 * Get server side input from some transport.
578 * Statement of authentication parameters management:
579 * This function owns and manages all authentication parameters, specifically
580 * the "raw" parameters (msg.rm_call.cb_cred and msg.rm_call.cb_verf) and
581 * the "cooked" credentials (rqst->rq_clntcred).
582 * However, this function does not know the structure of the cooked
583 * credentials, so it make the following assumptions:
584 * a) the structure is contiguous (no pointers), and
585 * b) the cred structure size does not exceed RQCRED_SIZE bytes.
586 * In all events, all three parameters are freed upon exit from this routine.
587 * The storage is trivially management on the call stack in user land, but
588 * is mallocated in kernel land.
598 readfds.fds_bits[0] = rdfds;
599 svc_getreqset (&readfds);
603 svc_getreqset (readfds)
607 fd_mask mask, *maskp;
610 assert (readfds != NULL);
612 maskp = readfds->fds_bits;
613 for (sock = 0; sock < FD_SETSIZE; sock += NFDBITS)
615 for (mask = *maskp++; (bit = ffsl(mask)) != 0; mask ^= (1L << (bit - 1)))
617 /* sock has input waiting */
619 svc_getreq_common (fd);
625 svc_getreq_common (fd)
635 char cred_area[2 * MAX_AUTH_BYTES + RQCRED_SIZE];
637 msg.rm_call.cb_cred.oa_base = cred_area;
638 msg.rm_call.cb_verf.oa_base = &(cred_area[MAX_AUTH_BYTES]);
639 r.rq_clntcred = &(cred_area[2 * MAX_AUTH_BYTES]);
641 rwlock_rdlock (&svc_fd_lock);
642 xprt = __svc_xports[fd];
643 rwlock_unlock (&svc_fd_lock);
645 /* But do we control sock? */
647 /* now receive msgs from xprtprt (support batch calls) */
650 if (SVC_RECV (xprt, &msg))
654 /* now find the exported program and call it */
655 struct svc_callout *s;
659 r.rq_prog = msg.rm_call.cb_prog;
660 r.rq_vers = msg.rm_call.cb_vers;
661 r.rq_proc = msg.rm_call.cb_proc;
662 r.rq_cred = msg.rm_call.cb_cred;
663 /* first authenticate the message */
664 why = _gss_authenticate(&r, &msg, &no_dispatch);
667 svcerr_auth (xprt, why);
672 /* now match message with a registered service */
674 low_vers = (rpcvers_t) - 1L;
675 high_vers = (rpcvers_t) 0L;
676 for (s = svc_head; s != NULL; s = s->sc_next)
678 if (s->sc_prog == r.rq_prog)
680 if (s->sc_vers == r.rq_vers)
682 (*s->sc_dispatch) (&r, xprt);
684 } /* found correct version */
686 if (s->sc_vers < low_vers)
687 low_vers = s->sc_vers;
688 if (s->sc_vers > high_vers)
689 high_vers = s->sc_vers;
690 } /* found correct program */
693 * if we got here, the program or version
697 svcerr_progvers (xprt, low_vers, high_vers);
699 svcerr_noprog (xprt);
700 /* Fall through to ... */
703 * Check if the xprt has been disconnected in a
704 * recursive call in the service dispatch routine.
707 rwlock_rdlock (&svc_fd_lock);
709 if (xprt != __svc_xports[fd])
711 rwlock_unlock (&svc_fd_lock);
714 rwlock_unlock (&svc_fd_lock);
716 if ((stat = SVC_STAT (xprt)) == XPRT_DIED)
721 else if ((xprt->xp_auth != NULL) &&
722 (xprt->xp_auth->svc_ah_private == NULL))
724 xprt->xp_auth = NULL;
727 while (stat == XPRT_MOREREQS);
732 svc_getreq_poll (pfdp, pollretval)
739 for (i = fds_found = 0; fds_found < pollretval; i++)
741 struct pollfd *p = &pfdp[i];
745 /* fd has input waiting */
748 * We assume that this function is only called
749 * via someone _select()ing from svc_fdset or
750 * _poll()ing from svc_pollset[]. Thus it's safe
751 * to handle the POLLNVAL event by simply turning
752 * the corresponding bit off in svc_fdset. The
753 * svc_pollset[] array is derived from svc_fdset
754 * and so will also be updated eventually.
756 * XXX Should we do an xprt_unregister() instead?
758 if (p->revents & POLLNVAL)
760 rwlock_wrlock (&svc_fd_lock);
761 FD_CLR (p->fd, &svc_fdset);
762 rwlock_unlock (&svc_fd_lock);
765 svc_getreq_common (p->fd);
771 rpc_control (int what, void *arg)
777 case RPC_SVC_CONNMAXREC_SET:
783 case RPC_SVC_CONNMAXREC_GET:
784 *(int *) arg = __svc_maxrec;