1 /* $NetBSD: svc.c,v 1.21 2000/07/06 03:10:35 christos Exp $ */
4 * SPDX-License-Identifier: BSD-3-Clause
6 * Copyright (c) 2009, Sun Microsystems, Inc.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are met:
11 * - Redistributions of source code must retain the above copyright notice,
12 * this list of conditions and the following disclaimer.
13 * - Redistributions in binary form must reproduce the above copyright notice,
14 * this list of conditions and the following disclaimer in the documentation
15 * and/or other materials provided with the distribution.
16 * - Neither the name of Sun Microsystems, Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
33 #if defined(LIBC_SCCS) && !defined(lint)
34 static char *sccsid2 = "@(#)svc.c 1.44 88/02/08 Copyr 1984 Sun Micro";
35 static char *sccsid = "@(#)svc.c 2.4 88/08/11 4.0 RPCSRC";
38 * svc.c, Server-side remote procedure call interface.
40 * There are two sets of procedures here. The xprt routines are
41 * for handling transport handles. The svc routines handle the
42 * list of service routines.
44 * Copyright (C) 1984, Sun Microsystems, Inc.
47 #include "namespace.h"
48 #include "reentrant.h"
49 #include <sys/types.h>
58 #include <rpc/pmap_clnt.h>
60 #include "un-namespace.h"
65 #define RQCRED_SIZE 400 /* this size is excessive */
67 #define SVC_VERSQUIET 0x0001 /* keep quiet about vers mismatch */
68 #define version_keepquiet(xp) (SVC_EXT(xp)->xp_flags & SVC_VERSQUIET)
70 #define max(a, b) (a > b ? a : b)
74 * Each entry represents a set of procedures (an rpc program).
75 * The dispatch routine takes request structs and runs the
76 * appropriate procedure.
78 static struct svc_callout {
79 struct svc_callout *sc_next;
83 void (*sc_dispatch)(struct svc_req *, SVCXPRT *);
86 SVCXPRT **__svc_xports;
89 static struct svc_callout *svc_find(rpcprog_t, rpcvers_t,
90 struct svc_callout **, char *);
91 static void __xprt_do_unregister (SVCXPRT *xprt, bool_t dolock);
93 /* *************** SVCXPRT related stuff **************** */
96 * Activate a transport handle.
99 xprt_register(SVCXPRT *xprt)
103 assert(xprt != NULL);
107 rwlock_wrlock(&svc_fd_lock);
108 if (__svc_xports == NULL) {
109 __svc_xports = (SVCXPRT **)
110 mem_alloc((FD_SETSIZE + 1) * sizeof(SVCXPRT *));
111 if (__svc_xports == NULL) {
112 rwlock_unlock(&svc_fd_lock);
115 memset(__svc_xports, '\0', (FD_SETSIZE + 1) * sizeof(SVCXPRT *));
117 if (sock < FD_SETSIZE) {
118 __svc_xports[sock] = xprt;
119 FD_SET(sock, &svc_fdset);
120 svc_maxfd = max(svc_maxfd, sock);
121 } else if (sock == FD_SETSIZE)
122 __svc_xports[sock] = xprt;
123 rwlock_unlock(&svc_fd_lock);
127 xprt_unregister(SVCXPRT *xprt)
129 __xprt_do_unregister(xprt, TRUE);
133 __xprt_unregister_unlocked(SVCXPRT *xprt)
135 __xprt_do_unregister(xprt, FALSE);
139 * De-activate a transport handle.
142 __xprt_do_unregister(SVCXPRT *xprt, bool_t dolock)
146 assert(xprt != NULL);
151 rwlock_wrlock(&svc_fd_lock);
152 if ((sock < FD_SETSIZE) && (__svc_xports[sock] == xprt)) {
153 __svc_xports[sock] = NULL;
154 FD_CLR(sock, &svc_fdset);
155 if (sock >= svc_maxfd) {
156 for (svc_maxfd--; svc_maxfd>=0; svc_maxfd--)
157 if (__svc_xports[svc_maxfd])
160 } else if ((sock == FD_SETSIZE) && (__svc_xports[sock] == xprt))
161 __svc_xports[sock] = NULL;
163 rwlock_unlock(&svc_fd_lock);
167 * Add a service program to the callout list.
168 * The dispatch routine will be called when a rpc request for this
169 * program number comes in.
172 svc_reg(SVCXPRT *xprt, const rpcprog_t prog, const rpcvers_t vers,
173 void (*dispatch)(struct svc_req *, SVCXPRT *),
174 const struct netconfig *nconf)
177 struct svc_callout *prev;
178 struct svc_callout *s;
179 struct netconfig *tnconf;
183 /* VARIABLES PROTECTED BY svc_lock: s, prev, svc_head */
185 if (xprt->xp_netid) {
186 netid = strdup(xprt->xp_netid);
188 } else if (nconf && nconf->nc_netid) {
189 netid = strdup(nconf->nc_netid);
191 } else if ((tnconf = __rpcgettp(xprt->xp_fd)) != NULL) {
192 netid = strdup(tnconf->nc_netid);
194 freenetconfigent(tnconf);
195 } /* must have been created with svc_raw_create */
196 if ((netid == NULL) && (flag == 1)) {
200 rwlock_wrlock(&svc_lock);
201 if ((s = svc_find(prog, vers, &prev, netid)) != NULL) {
203 if (s->sc_dispatch == dispatch)
204 goto rpcb_it; /* he is registering another xptr */
205 rwlock_unlock(&svc_lock);
208 s = mem_alloc(sizeof (struct svc_callout));
211 rwlock_unlock(&svc_lock);
217 s->sc_dispatch = dispatch;
219 s->sc_next = svc_head;
222 if ((xprt->xp_netid == NULL) && (flag == 1) && netid)
223 ((SVCXPRT *) xprt)->xp_netid = strdup(netid);
226 rwlock_unlock(&svc_lock);
227 /* now register the information with the local binder service */
229 /*LINTED const castaway*/
230 dummy = rpcb_set(prog, vers, (struct netconfig *) nconf,
231 &((SVCXPRT *) xprt)->xp_ltaddr);
238 * Remove a service program from the callout list.
241 svc_unreg(const rpcprog_t prog, const rpcvers_t vers)
243 struct svc_callout *prev;
244 struct svc_callout *s;
246 /* unregister the information anyway */
247 (void) rpcb_unset(prog, vers, NULL);
248 rwlock_wrlock(&svc_lock);
249 while ((s = svc_find(prog, vers, &prev, NULL)) != NULL) {
251 svc_head = s->sc_next;
253 prev->sc_next = s->sc_next;
257 mem_free(s->sc_netid, sizeof (s->sc_netid) + 1);
258 mem_free(s, sizeof (struct svc_callout));
260 rwlock_unlock(&svc_lock);
263 /* ********************** CALLOUT list related stuff ************* */
267 * Add a service program to the callout list.
268 * The dispatch routine will be called when a rpc request for this
269 * program number comes in.
272 svc_register(SVCXPRT *xprt, u_long prog, u_long vers,
273 void (*dispatch)(struct svc_req *, SVCXPRT *),
276 struct svc_callout *prev;
277 struct svc_callout *s;
279 assert(xprt != NULL);
280 assert(dispatch != NULL);
282 if ((s = svc_find((rpcprog_t)prog, (rpcvers_t)vers, &prev, NULL)) !=
284 if (s->sc_dispatch == dispatch)
285 goto pmap_it; /* he is registering another xptr */
288 s = mem_alloc(sizeof(struct svc_callout));
292 s->sc_prog = (rpcprog_t)prog;
293 s->sc_vers = (rpcvers_t)vers;
294 s->sc_dispatch = dispatch;
295 s->sc_next = svc_head;
298 /* now register the information with the local binder service */
300 return (pmap_set(prog, vers, protocol, xprt->xp_port));
306 * Remove a service program from the callout list.
309 svc_unregister(u_long prog, u_long vers)
311 struct svc_callout *prev;
312 struct svc_callout *s;
314 if ((s = svc_find((rpcprog_t)prog, (rpcvers_t)vers, &prev, NULL)) ==
318 svc_head = s->sc_next;
320 prev->sc_next = s->sc_next;
323 mem_free(s, sizeof(struct svc_callout));
324 /* now unregister the information with the local binder service */
325 (void)pmap_unset(prog, vers);
330 * Search the callout list for a program number, return the callout
333 static struct svc_callout *
334 svc_find(rpcprog_t prog, rpcvers_t vers, struct svc_callout **prev,
337 struct svc_callout *s, *p;
339 assert(prev != NULL);
342 for (s = svc_head; s != NULL; s = s->sc_next) {
343 if (((s->sc_prog == prog) && (s->sc_vers == vers)) &&
344 ((netid == NULL) || (s->sc_netid == NULL) ||
345 (strcmp(netid, s->sc_netid) == 0)))
353 /* ******************* REPLY GENERATION ROUTINES ************ */
356 * Send a reply to an rpc request
359 svc_sendreply(SVCXPRT *xprt, xdrproc_t xdr_results,
364 assert(xprt != NULL);
366 rply.rm_direction = REPLY;
367 rply.rm_reply.rp_stat = MSG_ACCEPTED;
368 rply.acpted_rply.ar_verf = xprt->xp_verf;
369 rply.acpted_rply.ar_stat = SUCCESS;
370 rply.acpted_rply.ar_results.where = xdr_location;
371 rply.acpted_rply.ar_results.proc = xdr_results;
372 return (SVC_REPLY(xprt, &rply));
376 * No procedure error reply
379 svcerr_noproc(SVCXPRT *xprt)
383 assert(xprt != NULL);
385 rply.rm_direction = REPLY;
386 rply.rm_reply.rp_stat = MSG_ACCEPTED;
387 rply.acpted_rply.ar_verf = xprt->xp_verf;
388 rply.acpted_rply.ar_stat = PROC_UNAVAIL;
389 SVC_REPLY(xprt, &rply);
393 * Can't decode args error reply
396 svcerr_decode(SVCXPRT *xprt)
400 assert(xprt != NULL);
402 rply.rm_direction = REPLY;
403 rply.rm_reply.rp_stat = MSG_ACCEPTED;
404 rply.acpted_rply.ar_verf = xprt->xp_verf;
405 rply.acpted_rply.ar_stat = GARBAGE_ARGS;
406 SVC_REPLY(xprt, &rply);
413 svcerr_systemerr(SVCXPRT *xprt)
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 = SYSTEM_ERR;
423 SVC_REPLY(xprt, &rply);
428 * Tell RPC package to not complain about version errors to the client. This
429 * is useful when revving broadcast protocols that sit on a fixed address.
430 * There is really one (or should be only one) example of this kind of
431 * protocol: the portmapper (or rpc binder).
434 __svc_versquiet_on(SVCXPRT *xprt)
437 SVC_EXT(xprt)->xp_flags |= SVC_VERSQUIET;
441 __svc_versquiet_off(SVCXPRT *xprt)
444 SVC_EXT(xprt)->xp_flags &= ~SVC_VERSQUIET;
448 svc_versquiet(SVCXPRT *xprt)
450 __svc_versquiet_on(xprt);
454 __svc_versquiet_get(SVCXPRT *xprt)
457 return (SVC_EXT(xprt)->xp_flags & SVC_VERSQUIET);
462 * Authentication error reply
465 svcerr_auth(SVCXPRT *xprt, enum auth_stat why)
469 assert(xprt != NULL);
471 rply.rm_direction = REPLY;
472 rply.rm_reply.rp_stat = MSG_DENIED;
473 rply.rjcted_rply.rj_stat = AUTH_ERROR;
474 rply.rjcted_rply.rj_why = why;
475 SVC_REPLY(xprt, &rply);
479 * Auth too weak error reply
482 svcerr_weakauth(SVCXPRT *xprt)
485 assert(xprt != NULL);
487 svcerr_auth(xprt, AUTH_TOOWEAK);
491 * Program unavailable error reply
494 svcerr_noprog(SVCXPRT *xprt)
498 assert(xprt != NULL);
500 rply.rm_direction = REPLY;
501 rply.rm_reply.rp_stat = MSG_ACCEPTED;
502 rply.acpted_rply.ar_verf = xprt->xp_verf;
503 rply.acpted_rply.ar_stat = PROG_UNAVAIL;
504 SVC_REPLY(xprt, &rply);
508 * Program version mismatch error reply
511 svcerr_progvers(SVCXPRT *xprt, rpcvers_t low_vers, rpcvers_t high_vers)
515 assert(xprt != NULL);
517 rply.rm_direction = REPLY;
518 rply.rm_reply.rp_stat = MSG_ACCEPTED;
519 rply.acpted_rply.ar_verf = xprt->xp_verf;
520 rply.acpted_rply.ar_stat = PROG_MISMATCH;
521 rply.acpted_rply.ar_vers.low = (u_int32_t)low_vers;
522 rply.acpted_rply.ar_vers.high = (u_int32_t)high_vers;
523 SVC_REPLY(xprt, &rply);
527 * Allocate a new server transport structure. All fields are
528 * initialized to zero and xp_p3 is initialized to point at an
529 * extension structure to hold various flags and authentication
538 xprt = mem_alloc(sizeof(SVCXPRT));
541 memset(xprt, 0, sizeof(SVCXPRT));
542 ext = mem_alloc(sizeof(SVCXPRT_EXT));
544 mem_free(xprt, sizeof(SVCXPRT));
547 memset(ext, 0, sizeof(SVCXPRT_EXT));
549 ext->xp_auth.svc_ah_ops = &svc_auth_null_ops;
555 * Free a server transport structure.
558 svc_xprt_free(SVCXPRT *xprt)
561 mem_free(xprt->xp_p3, sizeof(SVCXPRT_EXT));
562 mem_free(xprt, sizeof(SVCXPRT));
565 /* ******************* SERVER INPUT STUFF ******************* */
568 * Get server side input from some transport.
570 * Statement of authentication parameters management:
571 * This function owns and manages all authentication parameters, specifically
572 * the "raw" parameters (msg.rm_call.cb_cred and msg.rm_call.cb_verf) and
573 * the "cooked" credentials (rqst->rq_clntcred).
574 * However, this function does not know the structure of the cooked
575 * credentials, so it make the following assumptions:
576 * a) the structure is contiguous (no pointers), and
577 * b) the cred structure size does not exceed RQCRED_SIZE bytes.
578 * In all events, all three parameters are freed upon exit from this routine.
579 * The storage is trivially management on the call stack in user land, but
580 * is mallocated in kernel land.
584 svc_getreq(int rdfds)
589 readfds.fds_bits[0] = rdfds;
590 svc_getreqset(&readfds);
594 svc_getreqset(fd_set *readfds)
597 fd_mask mask, *maskp;
600 assert(readfds != NULL);
602 maskp = readfds->fds_bits;
603 for (sock = 0; sock < FD_SETSIZE; sock += NFDBITS) {
604 for (mask = *maskp++; (bit = ffsl(mask)) != 0;
605 mask ^= (1ul << (bit - 1))) {
606 /* sock has input waiting */
608 svc_getreq_common(fd);
614 svc_getreq_common(int fd)
623 char cred_area[2*MAX_AUTH_BYTES + RQCRED_SIZE];
625 msg.rm_call.cb_cred.oa_base = cred_area;
626 msg.rm_call.cb_verf.oa_base = &(cred_area[MAX_AUTH_BYTES]);
627 r.rq_clntcred = &(cred_area[2*MAX_AUTH_BYTES]);
629 rwlock_rdlock(&svc_fd_lock);
630 xprt = __svc_xports[fd];
631 rwlock_unlock(&svc_fd_lock);
633 /* But do we control sock? */
635 /* now receive msgs from xprtprt (support batch calls) */
637 if (SVC_RECV(xprt, &msg)) {
639 /* now find the exported program and call it */
640 struct svc_callout *s;
644 r.rq_prog = msg.rm_call.cb_prog;
645 r.rq_vers = msg.rm_call.cb_vers;
646 r.rq_proc = msg.rm_call.cb_proc;
647 r.rq_cred = msg.rm_call.cb_cred;
648 /* first authenticate the message */
649 if ((why = _authenticate(&r, &msg)) != AUTH_OK) {
651 * RPCSEC_GSS uses this return code
652 * for requests that form part of its
653 * context establishment protocol and
654 * should not be dispatched to the
657 if (why != RPCSEC_GSS_NODISPATCH)
658 svcerr_auth(xprt, why);
661 /* now match message with a registered service*/
663 low_vers = (rpcvers_t) -1L;
664 high_vers = (rpcvers_t) 0L;
665 for (s = svc_head; s != NULL; s = s->sc_next) {
666 if (s->sc_prog == r.rq_prog) {
667 if (s->sc_vers == r.rq_vers) {
668 (*s->sc_dispatch)(&r, xprt);
670 } /* found correct version */
672 if (s->sc_vers < low_vers)
673 low_vers = s->sc_vers;
674 if (s->sc_vers > high_vers)
675 high_vers = s->sc_vers;
676 } /* found correct program */
679 * if we got here, the program or version
683 svcerr_progvers(xprt, low_vers, high_vers);
686 /* Fall through to ... */
689 * Check if the xprt has been disconnected in a
690 * recursive call in the service dispatch routine.
693 rwlock_rdlock(&svc_fd_lock);
694 if (xprt != __svc_xports[fd]) {
695 rwlock_unlock(&svc_fd_lock);
698 rwlock_unlock(&svc_fd_lock);
700 if ((stat = SVC_STAT(xprt)) == XPRT_DIED){
704 } while (stat == XPRT_MOREREQS);
709 svc_getreq_poll(struct pollfd *pfdp, int pollretval)
714 for (i = fds_found = 0; fds_found < pollretval; i++) {
715 struct pollfd *p = &pfdp[i];
718 /* fd has input waiting */
721 * We assume that this function is only called
722 * via someone _select()ing from svc_fdset or
723 * _poll()ing from svc_pollset[]. Thus it's safe
724 * to handle the POLLNVAL event by simply turning
725 * the corresponding bit off in svc_fdset. The
726 * svc_pollset[] array is derived from svc_fdset
727 * and so will also be updated eventually.
729 * XXX Should we do an xprt_unregister() instead?
731 if (p->revents & POLLNVAL) {
732 rwlock_wrlock(&svc_fd_lock);
733 FD_CLR(p->fd, &svc_fdset);
734 rwlock_unlock(&svc_fd_lock);
736 svc_getreq_common(p->fd);
742 rpc_control(int what, void *arg)
747 case RPC_SVC_CONNMAXREC_SET:
753 case RPC_SVC_CONNMAXREC_GET:
754 *(int *)arg = __svc_maxrec;