Home | History | Annotate | Line # | Download | only in rpc
svc.c revision 1.19.2.1
      1  1.19.2.1   minoura /*	$NetBSD: svc.c,v 1.19.2.1 2000/06/23 16:17:50 minoura Exp $	*/
      2       1.7       cgd 
      3       1.1       cgd /*
      4       1.1       cgd  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
      5       1.1       cgd  * unrestricted use provided that this legend is included on all tape
      6       1.1       cgd  * media and as a part of the software program in whole or part.  Users
      7       1.1       cgd  * may copy or modify Sun RPC without charge, but are not authorized
      8       1.1       cgd  * to license or distribute it to anyone else except as part of a product or
      9       1.1       cgd  * program developed by the user.
     10       1.1       cgd  *
     11       1.1       cgd  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
     12       1.1       cgd  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
     13       1.1       cgd  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
     14       1.1       cgd  *
     15       1.1       cgd  * Sun RPC is provided with no support and without any obligation on the
     16       1.1       cgd  * part of Sun Microsystems, Inc. to assist in its use, correction,
     17       1.1       cgd  * modification or enhancement.
     18       1.1       cgd  *
     19       1.1       cgd  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
     20       1.1       cgd  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
     21       1.1       cgd  * OR ANY PART THEREOF.
     22       1.1       cgd  *
     23       1.1       cgd  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
     24       1.1       cgd  * or profits or other special, indirect and consequential damages, even if
     25       1.1       cgd  * Sun has been advised of the possibility of such damages.
     26       1.1       cgd  *
     27       1.1       cgd  * Sun Microsystems, Inc.
     28       1.1       cgd  * 2550 Garcia Avenue
     29       1.1       cgd  * Mountain View, California  94043
     30       1.1       cgd  */
     31       1.1       cgd 
     32      1.10  christos #include <sys/cdefs.h>
     33       1.1       cgd #if defined(LIBC_SCCS) && !defined(lint)
     34      1.10  christos #if 0
     35      1.10  christos static char *sccsid = "@(#)svc.c 1.44 88/02/08 Copyr 1984 Sun Micro";
     36      1.10  christos static char *sccsid = "@(#)svc.c	2.4 88/08/11 4.0 RPCSRC";
     37      1.10  christos #else
     38  1.19.2.1   minoura __RCSID("$NetBSD: svc.c,v 1.19.2.1 2000/06/23 16:17:50 minoura Exp $");
     39      1.10  christos #endif
     40       1.1       cgd #endif
     41       1.1       cgd 
     42       1.1       cgd /*
     43       1.1       cgd  * svc.c, Server-side remote procedure call interface.
     44       1.1       cgd  *
     45       1.1       cgd  * There are two sets of procedures here.  The xprt routines are
     46       1.1       cgd  * for handling transport handles.  The svc routines handle the
     47       1.1       cgd  * list of service routines.
     48       1.1       cgd  *
     49       1.1       cgd  * Copyright (C) 1984, Sun Microsystems, Inc.
     50       1.1       cgd  */
     51       1.1       cgd 
     52      1.11       jtc #include "namespace.h"
     53  1.19.2.1   minoura #include "reentrant.h"
     54  1.19.2.1   minoura #include <sys/types.h>
     55  1.19.2.1   minoura #include <sys/poll.h>
     56      1.17     lukem #include <assert.h>
     57      1.14     lukem #include <errno.h>
     58       1.4       cgd #include <stdlib.h>
     59       1.8        pk #include <string.h>
     60       1.4       cgd 
     61       1.1       cgd #include <rpc/rpc.h>
     62  1.19.2.1   minoura #ifdef PORTMAP
     63       1.1       cgd #include <rpc/pmap_clnt.h>
     64  1.19.2.1   minoura #endif
     65  1.19.2.1   minoura 
     66  1.19.2.1   minoura #include "rpc_com.h"
     67      1.11       jtc 
     68      1.11       jtc #ifdef __weak_alias
     69      1.19   mycroft __weak_alias(svc_getreq,_svc_getreq)
     70      1.19   mycroft __weak_alias(svc_getreqset,_svc_getreqset)
     71  1.19.2.1   minoura __weak_alias(svc_getreq_common,_svc_getreq_common)
     72      1.19   mycroft __weak_alias(svc_register,_svc_register)
     73  1.19.2.1   minoura __weak_alias(svc_reg,_svc_reg)
     74  1.19.2.1   minoura __weak_alias(svc_unreg,_svc_unreg)
     75      1.19   mycroft __weak_alias(svc_sendreply,_svc_sendreply)
     76      1.19   mycroft __weak_alias(svc_unregister,_svc_unregister)
     77      1.19   mycroft __weak_alias(svcerr_auth,_svcerr_auth)
     78      1.19   mycroft __weak_alias(svcerr_decode,_svcerr_decode)
     79      1.19   mycroft __weak_alias(svcerr_noproc,_svcerr_noproc)
     80      1.19   mycroft __weak_alias(svcerr_noprog,_svcerr_noprog)
     81      1.19   mycroft __weak_alias(svcerr_progvers,_svcerr_progvers)
     82      1.19   mycroft __weak_alias(svcerr_systemerr,_svcerr_systemerr)
     83      1.19   mycroft __weak_alias(svcerr_weakauth,_svcerr_weakauth)
     84      1.19   mycroft __weak_alias(xprt_register,_xprt_register)
     85      1.19   mycroft __weak_alias(xprt_unregister,_xprt_unregister)
     86      1.11       jtc #endif
     87       1.1       cgd 
     88       1.1       cgd static SVCXPRT **xports;
     89       1.1       cgd 
     90       1.1       cgd #define NULL_SVC ((struct svc_callout *)0)
     91       1.1       cgd #define	RQCRED_SIZE	400		/* this size is excessive */
     92       1.1       cgd 
     93  1.19.2.1   minoura #define SVC_VERSQUIET 0x0001		/* keep quiet about vers mismatch */
     94  1.19.2.1   minoura #define version_keepquiet(xp) ((u_long)(xp)->xp_p3 & SVC_VERSQUIET)
     95  1.19.2.1   minoura 
     96       1.2   deraadt #define max(a, b) (a > b ? a : b)
     97       1.2   deraadt 
     98       1.1       cgd /*
     99       1.1       cgd  * The services list
    100       1.1       cgd  * Each entry represents a set of procedures (an rpc program).
    101       1.1       cgd  * The dispatch routine takes request structs and runs the
    102       1.1       cgd  * apropriate procedure.
    103       1.1       cgd  */
    104       1.1       cgd static struct svc_callout {
    105       1.1       cgd 	struct svc_callout *sc_next;
    106  1.19.2.1   minoura 	rpcprog_t	    sc_prog;
    107  1.19.2.1   minoura 	rpcvers_t	    sc_vers;
    108  1.19.2.1   minoura 	char		   *sc_netid;
    109      1.10  christos 	void		    (*sc_dispatch) __P((struct svc_req *, SVCXPRT *));
    110       1.1       cgd } *svc_head;
    111       1.1       cgd 
    112  1.19.2.1   minoura #ifdef __REENT
    113  1.19.2.1   minoura extern rwlock_t svc_lock;
    114  1.19.2.1   minoura extern rwlock_t svc_fd_lock;
    115  1.19.2.1   minoura #endif
    116  1.19.2.1   minoura 
    117  1.19.2.1   minoura static struct svc_callout *svc_find __P((rpcprog_t, rpcvers_t,
    118  1.19.2.1   minoura 					 struct svc_callout **, char *));
    119       1.1       cgd 
    120       1.1       cgd /* ***************  SVCXPRT related stuff **************** */
    121       1.3   deraadt 
    122       1.1       cgd /*
    123       1.1       cgd  * Activate a transport handle.
    124       1.1       cgd  */
    125       1.1       cgd void
    126       1.1       cgd xprt_register(xprt)
    127       1.1       cgd 	SVCXPRT *xprt;
    128       1.1       cgd {
    129      1.17     lukem 	int sock;
    130      1.17     lukem 
    131      1.17     lukem 	_DIAGASSERT(xprt != NULL);
    132      1.17     lukem 
    133  1.19.2.1   minoura 	sock = xprt->xp_fd;
    134       1.1       cgd 
    135  1.19.2.1   minoura 	rwlock_wrlock(&svc_fd_lock);
    136       1.1       cgd 	if (xports == NULL) {
    137       1.1       cgd 		xports = (SVCXPRT **)
    138       1.1       cgd 			mem_alloc(FD_SETSIZE * sizeof(SVCXPRT *));
    139      1.16     lukem 		if (xports == NULL)
    140      1.16     lukem 			return;
    141       1.9       jtc 		memset(xports, '\0', FD_SETSIZE * sizeof(SVCXPRT *));
    142       1.1       cgd 	}
    143       1.2   deraadt 	if (sock < FD_SETSIZE) {
    144       1.1       cgd 		xports[sock] = xprt;
    145       1.1       cgd 		FD_SET(sock, &svc_fdset);
    146       1.2   deraadt 		svc_maxfd = max(svc_maxfd, sock);
    147       1.1       cgd 	}
    148  1.19.2.1   minoura 	rwlock_unlock(&svc_fd_lock);
    149       1.1       cgd }
    150       1.1       cgd 
    151       1.1       cgd /*
    152       1.1       cgd  * De-activate a transport handle.
    153       1.1       cgd  */
    154       1.1       cgd void
    155       1.1       cgd xprt_unregister(xprt)
    156       1.1       cgd 	SVCXPRT *xprt;
    157       1.1       cgd {
    158      1.17     lukem 	int sock;
    159      1.17     lukem 
    160      1.17     lukem 	_DIAGASSERT(xprt != NULL);
    161      1.17     lukem 
    162  1.19.2.1   minoura 	sock = xprt->xp_fd;
    163       1.1       cgd 
    164  1.19.2.1   minoura 	rwlock_wrlock(&svc_fd_lock);
    165       1.2   deraadt 	if ((sock < FD_SETSIZE) && (xports[sock] == xprt)) {
    166       1.1       cgd 		xports[sock] = (SVCXPRT *)0;
    167       1.1       cgd 		FD_CLR(sock, &svc_fdset);
    168  1.19.2.1   minoura 		if (sock >= svc_maxfd) {
    169       1.3   deraadt 			for (svc_maxfd--; svc_maxfd>=0; svc_maxfd--)
    170       1.3   deraadt 				if (xports[svc_maxfd])
    171       1.3   deraadt 					break;
    172       1.3   deraadt 		}
    173       1.1       cgd 	}
    174  1.19.2.1   minoura 	rwlock_unlock(&svc_fd_lock);
    175       1.1       cgd }
    176       1.1       cgd 
    177  1.19.2.1   minoura /*
    178  1.19.2.1   minoura  * Add a service program to the callout list.
    179  1.19.2.1   minoura  * The dispatch routine will be called when a rpc request for this
    180  1.19.2.1   minoura  * program number comes in.
    181  1.19.2.1   minoura  */
    182  1.19.2.1   minoura bool_t
    183  1.19.2.1   minoura svc_reg(xprt, prog, vers, dispatch, nconf)
    184  1.19.2.1   minoura 	SVCXPRT *xprt;
    185  1.19.2.1   minoura 	const rpcprog_t prog;
    186  1.19.2.1   minoura 	const rpcvers_t vers;
    187  1.19.2.1   minoura 	void (*dispatch) __P((struct svc_req *, SVCXPRT *));
    188  1.19.2.1   minoura 	const struct netconfig *nconf;
    189  1.19.2.1   minoura {
    190  1.19.2.1   minoura 	bool_t dummy;
    191  1.19.2.1   minoura 	struct svc_callout *prev;
    192  1.19.2.1   minoura 	register struct svc_callout *s;
    193  1.19.2.1   minoura 	struct netconfig *tnconf;
    194  1.19.2.1   minoura 	register char *netid = NULL;
    195  1.19.2.1   minoura 	int flag = 0;
    196  1.19.2.1   minoura 
    197  1.19.2.1   minoura /* VARIABLES PROTECTED BY svc_lock: s, prev, svc_head */
    198  1.19.2.1   minoura 
    199  1.19.2.1   minoura 	if (xprt->xp_netid) {
    200  1.19.2.1   minoura 		netid = strdup(xprt->xp_netid);
    201  1.19.2.1   minoura 		flag = 1;
    202  1.19.2.1   minoura 	} else if (nconf && nconf->nc_netid) {
    203  1.19.2.1   minoura 		netid = strdup(nconf->nc_netid);
    204  1.19.2.1   minoura 		flag = 1;
    205  1.19.2.1   minoura 	} else if ((tnconf = __rpcgettp(xprt->xp_fd)) != NULL) {
    206  1.19.2.1   minoura 		netid = strdup(tnconf->nc_netid);
    207  1.19.2.1   minoura 		flag = 1;
    208  1.19.2.1   minoura 		freenetconfigent(tnconf);
    209  1.19.2.1   minoura 	} /* must have been created with svc_raw_create */
    210  1.19.2.1   minoura 	if ((netid == NULL) && (flag == 1)) {
    211  1.19.2.1   minoura 		return (FALSE);
    212  1.19.2.1   minoura 	}
    213  1.19.2.1   minoura 
    214  1.19.2.1   minoura 	rwlock_wrlock(&svc_lock);
    215  1.19.2.1   minoura 	if ((s = svc_find(prog, vers, &prev, netid)) != NULL_SVC) {
    216  1.19.2.1   minoura 		if (netid)
    217  1.19.2.1   minoura 			free(netid);
    218  1.19.2.1   minoura 		if (s->sc_dispatch == dispatch)
    219  1.19.2.1   minoura 			goto rpcb_it; /* he is registering another xptr */
    220  1.19.2.1   minoura 		rwlock_unlock(&svc_lock);
    221  1.19.2.1   minoura 		return (FALSE);
    222  1.19.2.1   minoura 	}
    223  1.19.2.1   minoura 	s = (struct svc_callout *)mem_alloc(sizeof (struct svc_callout));
    224  1.19.2.1   minoura 	if (s == (struct svc_callout *)NULL) {
    225  1.19.2.1   minoura 		if (netid)
    226  1.19.2.1   minoura 			free(netid);
    227  1.19.2.1   minoura 		rwlock_unlock(&svc_lock);
    228  1.19.2.1   minoura 		return (FALSE);
    229  1.19.2.1   minoura 	}
    230  1.19.2.1   minoura 
    231  1.19.2.1   minoura 	s->sc_prog = prog;
    232  1.19.2.1   minoura 	s->sc_vers = vers;
    233  1.19.2.1   minoura 	s->sc_dispatch = dispatch;
    234  1.19.2.1   minoura 	s->sc_netid = netid;
    235  1.19.2.1   minoura 	s->sc_next = svc_head;
    236  1.19.2.1   minoura 	svc_head = s;
    237  1.19.2.1   minoura 
    238  1.19.2.1   minoura 	if ((xprt->xp_netid == NULL) && (flag == 1) && netid)
    239  1.19.2.1   minoura 		((SVCXPRT *) xprt)->xp_netid = strdup(netid);
    240  1.19.2.1   minoura 
    241  1.19.2.1   minoura rpcb_it:
    242  1.19.2.1   minoura 	rwlock_unlock(&svc_lock);
    243  1.19.2.1   minoura 	/* now register the information with the local binder service */
    244  1.19.2.1   minoura 	if (nconf) {
    245  1.19.2.1   minoura 		dummy = rpcb_set(prog, vers, (struct netconfig *) nconf,
    246  1.19.2.1   minoura 		&((SVCXPRT *) xprt)->xp_ltaddr);
    247  1.19.2.1   minoura 		return (dummy);
    248  1.19.2.1   minoura 	}
    249  1.19.2.1   minoura 	return (TRUE);
    250  1.19.2.1   minoura }
    251  1.19.2.1   minoura 
    252  1.19.2.1   minoura /*
    253  1.19.2.1   minoura  * Remove a service program from the callout list.
    254  1.19.2.1   minoura  */
    255  1.19.2.1   minoura void
    256  1.19.2.1   minoura svc_unreg(prog, vers)
    257  1.19.2.1   minoura 	const rpcprog_t prog;
    258  1.19.2.1   minoura 	const rpcvers_t vers;
    259  1.19.2.1   minoura {
    260  1.19.2.1   minoura 	struct svc_callout *prev;
    261  1.19.2.1   minoura 	register struct svc_callout *s;
    262  1.19.2.1   minoura 
    263  1.19.2.1   minoura 	/* unregister the information anyway */
    264  1.19.2.1   minoura 	(void) rpcb_unset(prog, vers, NULL);
    265  1.19.2.1   minoura 	rwlock_wrlock(&svc_lock);
    266  1.19.2.1   minoura 	while ((s = svc_find(prog, vers, &prev, NULL)) != NULL_SVC) {
    267  1.19.2.1   minoura 		if (prev == NULL_SVC) {
    268  1.19.2.1   minoura 			svc_head = s->sc_next;
    269  1.19.2.1   minoura 		} else {
    270  1.19.2.1   minoura 			prev->sc_next = s->sc_next;
    271  1.19.2.1   minoura 		}
    272  1.19.2.1   minoura 		s->sc_next = NULL_SVC;
    273  1.19.2.1   minoura 		if (s->sc_netid)
    274  1.19.2.1   minoura 			mem_free((char *)s->sc_netid,
    275  1.19.2.1   minoura 					(u_int)sizeof (s->sc_netid) + 1);
    276  1.19.2.1   minoura 		mem_free((char *)s, (u_int) sizeof (struct svc_callout));
    277  1.19.2.1   minoura 	}
    278  1.19.2.1   minoura 	rwlock_unlock(&svc_lock);
    279  1.19.2.1   minoura }
    280       1.1       cgd 
    281       1.1       cgd /* ********************** CALLOUT list related stuff ************* */
    282       1.1       cgd 
    283  1.19.2.1   minoura #ifdef PORTMAP
    284       1.1       cgd /*
    285       1.1       cgd  * Add a service program to the callout list.
    286       1.1       cgd  * The dispatch routine will be called when a rpc request for this
    287       1.1       cgd  * program number comes in.
    288       1.1       cgd  */
    289       1.1       cgd bool_t
    290       1.1       cgd svc_register(xprt, prog, vers, dispatch, protocol)
    291       1.1       cgd 	SVCXPRT *xprt;
    292      1.13     lukem 	u_long prog;
    293      1.13     lukem 	u_long vers;
    294      1.10  christos 	void (*dispatch) __P((struct svc_req *, SVCXPRT *));
    295       1.1       cgd 	int protocol;
    296       1.1       cgd {
    297       1.1       cgd 	struct svc_callout *prev;
    298      1.14     lukem 	struct svc_callout *s;
    299       1.1       cgd 
    300      1.17     lukem 	_DIAGASSERT(xprt != NULL);
    301      1.17     lukem 	_DIAGASSERT(dispatch != NULL);
    302      1.17     lukem 
    303  1.19.2.1   minoura 	if ((s = svc_find(prog, vers, &prev, NULL)) != NULL_SVC) {
    304       1.1       cgd 		if (s->sc_dispatch == dispatch)
    305       1.1       cgd 			goto pmap_it;  /* he is registering another xptr */
    306       1.1       cgd 		return (FALSE);
    307       1.1       cgd 	}
    308       1.1       cgd 	s = (struct svc_callout *)mem_alloc(sizeof(struct svc_callout));
    309       1.1       cgd 	if (s == (struct svc_callout *)0) {
    310       1.1       cgd 		return (FALSE);
    311       1.1       cgd 	}
    312       1.1       cgd 	s->sc_prog = prog;
    313       1.1       cgd 	s->sc_vers = vers;
    314       1.1       cgd 	s->sc_dispatch = dispatch;
    315       1.1       cgd 	s->sc_next = svc_head;
    316       1.1       cgd 	svc_head = s;
    317       1.1       cgd pmap_it:
    318       1.1       cgd 	/* now register the information with the local binder service */
    319       1.1       cgd 	if (protocol) {
    320       1.1       cgd 		return (pmap_set(prog, vers, protocol, xprt->xp_port));
    321       1.1       cgd 	}
    322       1.1       cgd 	return (TRUE);
    323       1.1       cgd }
    324       1.1       cgd 
    325       1.1       cgd /*
    326       1.1       cgd  * Remove a service program from the callout list.
    327       1.1       cgd  */
    328       1.1       cgd void
    329       1.1       cgd svc_unregister(prog, vers)
    330      1.13     lukem 	u_long prog;
    331      1.13     lukem 	u_long vers;
    332       1.1       cgd {
    333       1.1       cgd 	struct svc_callout *prev;
    334      1.14     lukem 	struct svc_callout *s;
    335       1.1       cgd 
    336  1.19.2.1   minoura 	if ((s = svc_find(prog, vers, &prev, NULL)) == NULL_SVC)
    337       1.1       cgd 		return;
    338       1.1       cgd 	if (prev == NULL_SVC) {
    339       1.1       cgd 		svc_head = s->sc_next;
    340       1.1       cgd 	} else {
    341       1.1       cgd 		prev->sc_next = s->sc_next;
    342       1.1       cgd 	}
    343       1.1       cgd 	s->sc_next = NULL_SVC;
    344      1.15  christos 	mem_free(s, sizeof(struct svc_callout));
    345       1.1       cgd 	/* now unregister the information with the local binder service */
    346       1.1       cgd 	(void)pmap_unset(prog, vers);
    347       1.1       cgd }
    348  1.19.2.1   minoura #endif /* PORTMAP */
    349       1.1       cgd 
    350       1.1       cgd /*
    351       1.1       cgd  * Search the callout list for a program number, return the callout
    352       1.1       cgd  * struct.
    353       1.1       cgd  */
    354       1.1       cgd static struct svc_callout *
    355  1.19.2.1   minoura svc_find(prog, vers, prev, netid)
    356  1.19.2.1   minoura 	rpcprog_t prog;
    357  1.19.2.1   minoura 	rpcvers_t vers;
    358       1.1       cgd 	struct svc_callout **prev;
    359  1.19.2.1   minoura 	char *netid;
    360       1.1       cgd {
    361      1.14     lukem 	struct svc_callout *s, *p;
    362       1.1       cgd 
    363      1.17     lukem 	_DIAGASSERT(prev != NULL);
    364      1.17     lukem 
    365       1.1       cgd 	p = NULL_SVC;
    366       1.1       cgd 	for (s = svc_head; s != NULL_SVC; s = s->sc_next) {
    367  1.19.2.1   minoura 		if (((s->sc_prog == prog) && (s->sc_vers == vers)) &&
    368  1.19.2.1   minoura 		    ((netid == NULL) || (s->sc_netid == NULL) ||
    369  1.19.2.1   minoura 		    (strcmp(netid, s->sc_netid) == 0)))
    370  1.19.2.1   minoura 			break;
    371       1.1       cgd 		p = s;
    372       1.1       cgd 	}
    373       1.1       cgd 	*prev = p;
    374       1.1       cgd 	return (s);
    375       1.1       cgd }
    376       1.1       cgd 
    377       1.1       cgd /* ******************* REPLY GENERATION ROUTINES  ************ */
    378       1.1       cgd 
    379       1.1       cgd /*
    380       1.1       cgd  * Send a reply to an rpc request
    381       1.1       cgd  */
    382       1.1       cgd bool_t
    383       1.1       cgd svc_sendreply(xprt, xdr_results, xdr_location)
    384      1.14     lukem 	SVCXPRT *xprt;
    385       1.1       cgd 	xdrproc_t xdr_results;
    386      1.13     lukem 	caddr_t xdr_location;
    387       1.1       cgd {
    388       1.1       cgd 	struct rpc_msg rply;
    389       1.1       cgd 
    390      1.17     lukem 	_DIAGASSERT(xprt != NULL);
    391      1.17     lukem 
    392       1.1       cgd 	rply.rm_direction = REPLY;
    393       1.1       cgd 	rply.rm_reply.rp_stat = MSG_ACCEPTED;
    394       1.1       cgd 	rply.acpted_rply.ar_verf = xprt->xp_verf;
    395       1.1       cgd 	rply.acpted_rply.ar_stat = SUCCESS;
    396       1.1       cgd 	rply.acpted_rply.ar_results.where = xdr_location;
    397       1.1       cgd 	rply.acpted_rply.ar_results.proc = xdr_results;
    398       1.1       cgd 	return (SVC_REPLY(xprt, &rply));
    399       1.1       cgd }
    400       1.1       cgd 
    401       1.1       cgd /*
    402       1.1       cgd  * No procedure error reply
    403       1.1       cgd  */
    404       1.1       cgd void
    405       1.1       cgd svcerr_noproc(xprt)
    406      1.14     lukem 	SVCXPRT *xprt;
    407       1.1       cgd {
    408       1.1       cgd 	struct rpc_msg rply;
    409       1.1       cgd 
    410      1.17     lukem 	_DIAGASSERT(xprt != NULL);
    411      1.17     lukem 
    412       1.1       cgd 	rply.rm_direction = REPLY;
    413       1.1       cgd 	rply.rm_reply.rp_stat = MSG_ACCEPTED;
    414       1.1       cgd 	rply.acpted_rply.ar_verf = xprt->xp_verf;
    415       1.1       cgd 	rply.acpted_rply.ar_stat = PROC_UNAVAIL;
    416       1.1       cgd 	SVC_REPLY(xprt, &rply);
    417       1.1       cgd }
    418       1.1       cgd 
    419       1.1       cgd /*
    420       1.1       cgd  * Can't decode args error reply
    421       1.1       cgd  */
    422       1.1       cgd void
    423       1.1       cgd svcerr_decode(xprt)
    424      1.14     lukem 	SVCXPRT *xprt;
    425       1.1       cgd {
    426       1.1       cgd 	struct rpc_msg rply;
    427       1.1       cgd 
    428      1.17     lukem 	_DIAGASSERT(xprt != NULL);
    429      1.17     lukem 
    430       1.1       cgd 	rply.rm_direction = REPLY;
    431       1.1       cgd 	rply.rm_reply.rp_stat = MSG_ACCEPTED;
    432       1.1       cgd 	rply.acpted_rply.ar_verf = xprt->xp_verf;
    433       1.1       cgd 	rply.acpted_rply.ar_stat = GARBAGE_ARGS;
    434       1.1       cgd 	SVC_REPLY(xprt, &rply);
    435       1.1       cgd }
    436       1.1       cgd 
    437       1.1       cgd /*
    438       1.1       cgd  * Some system error
    439       1.1       cgd  */
    440       1.1       cgd void
    441       1.1       cgd svcerr_systemerr(xprt)
    442      1.14     lukem 	SVCXPRT *xprt;
    443       1.1       cgd {
    444       1.1       cgd 	struct rpc_msg rply;
    445       1.1       cgd 
    446      1.17     lukem 	_DIAGASSERT(xprt != NULL);
    447      1.17     lukem 
    448       1.1       cgd 	rply.rm_direction = REPLY;
    449       1.1       cgd 	rply.rm_reply.rp_stat = MSG_ACCEPTED;
    450       1.1       cgd 	rply.acpted_rply.ar_verf = xprt->xp_verf;
    451       1.1       cgd 	rply.acpted_rply.ar_stat = SYSTEM_ERR;
    452       1.1       cgd 	SVC_REPLY(xprt, &rply);
    453       1.1       cgd }
    454       1.1       cgd 
    455  1.19.2.1   minoura #if 0
    456  1.19.2.1   minoura /*
    457  1.19.2.1   minoura  * Tell RPC package to not complain about version errors to the client.	 This
    458  1.19.2.1   minoura  * is useful when revving broadcast protocols that sit on a fixed address.
    459  1.19.2.1   minoura  * There is really one (or should be only one) example of this kind of
    460  1.19.2.1   minoura  * protocol: the portmapper (or rpc binder).
    461  1.19.2.1   minoura  */
    462  1.19.2.1   minoura void
    463  1.19.2.1   minoura __svc_versquiet_on(xprt)
    464  1.19.2.1   minoura 	register SVCXPRT *xprt;
    465  1.19.2.1   minoura {
    466  1.19.2.1   minoura 	u_long	tmp;
    467  1.19.2.1   minoura 
    468  1.19.2.1   minoura 	tmp = ((u_long) xprt->xp_p3) | SVC_VERSQUIET;
    469  1.19.2.1   minoura 	xprt->xp_p3 = (caddr_t) tmp;
    470  1.19.2.1   minoura }
    471  1.19.2.1   minoura 
    472  1.19.2.1   minoura void
    473  1.19.2.1   minoura __svc_versquiet_off(xprt)
    474  1.19.2.1   minoura 	register SVCXPRT *xprt;
    475  1.19.2.1   minoura {
    476  1.19.2.1   minoura 	u_long	tmp;
    477  1.19.2.1   minoura 
    478  1.19.2.1   minoura 	tmp = ((u_long) xprt->xp_p3) & ~SVC_VERSQUIET;
    479  1.19.2.1   minoura 	xprt->xp_p3 = (caddr_t) tmp;
    480  1.19.2.1   minoura }
    481  1.19.2.1   minoura 
    482  1.19.2.1   minoura void
    483  1.19.2.1   minoura svc_versquiet(xprt)
    484  1.19.2.1   minoura 	register SVCXPRT *xprt;
    485  1.19.2.1   minoura {
    486  1.19.2.1   minoura 	__svc_versquiet_on(xprt);
    487  1.19.2.1   minoura }
    488  1.19.2.1   minoura 
    489  1.19.2.1   minoura int
    490  1.19.2.1   minoura __svc_versquiet_get(xprt)
    491  1.19.2.1   minoura 	register SVCXPRT *xprt;
    492  1.19.2.1   minoura {
    493  1.19.2.1   minoura 	return ((int) xprt->xp_p3) & SVC_VERSQUIET;
    494  1.19.2.1   minoura }
    495  1.19.2.1   minoura #endif
    496  1.19.2.1   minoura 
    497       1.1       cgd /*
    498       1.1       cgd  * Authentication error reply
    499       1.1       cgd  */
    500       1.1       cgd void
    501       1.1       cgd svcerr_auth(xprt, why)
    502       1.1       cgd 	SVCXPRT *xprt;
    503       1.1       cgd 	enum auth_stat why;
    504       1.1       cgd {
    505       1.1       cgd 	struct rpc_msg rply;
    506       1.1       cgd 
    507      1.17     lukem 	_DIAGASSERT(xprt != NULL);
    508      1.17     lukem 
    509       1.1       cgd 	rply.rm_direction = REPLY;
    510       1.1       cgd 	rply.rm_reply.rp_stat = MSG_DENIED;
    511       1.1       cgd 	rply.rjcted_rply.rj_stat = AUTH_ERROR;
    512       1.1       cgd 	rply.rjcted_rply.rj_why = why;
    513       1.1       cgd 	SVC_REPLY(xprt, &rply);
    514       1.1       cgd }
    515       1.1       cgd 
    516       1.1       cgd /*
    517       1.1       cgd  * Auth too weak error reply
    518       1.1       cgd  */
    519       1.1       cgd void
    520       1.1       cgd svcerr_weakauth(xprt)
    521       1.1       cgd 	SVCXPRT *xprt;
    522       1.1       cgd {
    523       1.1       cgd 
    524      1.17     lukem 	_DIAGASSERT(xprt != NULL);
    525      1.17     lukem 
    526       1.1       cgd 	svcerr_auth(xprt, AUTH_TOOWEAK);
    527       1.1       cgd }
    528       1.1       cgd 
    529       1.1       cgd /*
    530       1.1       cgd  * Program unavailable error reply
    531       1.1       cgd  */
    532       1.1       cgd void
    533       1.1       cgd svcerr_noprog(xprt)
    534      1.14     lukem 	SVCXPRT *xprt;
    535       1.1       cgd {
    536       1.1       cgd 	struct rpc_msg rply;
    537       1.1       cgd 
    538      1.17     lukem 	_DIAGASSERT(xprt != NULL);
    539      1.17     lukem 
    540       1.1       cgd 	rply.rm_direction = REPLY;
    541       1.1       cgd 	rply.rm_reply.rp_stat = MSG_ACCEPTED;
    542       1.1       cgd 	rply.acpted_rply.ar_verf = xprt->xp_verf;
    543       1.1       cgd 	rply.acpted_rply.ar_stat = PROG_UNAVAIL;
    544       1.1       cgd 	SVC_REPLY(xprt, &rply);
    545       1.1       cgd }
    546       1.1       cgd 
    547       1.1       cgd /*
    548       1.1       cgd  * Program version mismatch error reply
    549       1.1       cgd  */
    550       1.1       cgd void
    551       1.1       cgd svcerr_progvers(xprt, low_vers, high_vers)
    552      1.14     lukem 	SVCXPRT *xprt;
    553  1.19.2.1   minoura 	rpcvers_t low_vers;
    554  1.19.2.1   minoura 	rpcvers_t high_vers;
    555       1.1       cgd {
    556       1.1       cgd 	struct rpc_msg rply;
    557       1.1       cgd 
    558      1.17     lukem 	_DIAGASSERT(xprt != NULL);
    559      1.17     lukem 
    560       1.1       cgd 	rply.rm_direction = REPLY;
    561       1.1       cgd 	rply.rm_reply.rp_stat = MSG_ACCEPTED;
    562       1.1       cgd 	rply.acpted_rply.ar_verf = xprt->xp_verf;
    563       1.1       cgd 	rply.acpted_rply.ar_stat = PROG_MISMATCH;
    564      1.15  christos 	rply.acpted_rply.ar_vers.low = (u_int32_t)low_vers;
    565      1.15  christos 	rply.acpted_rply.ar_vers.high = (u_int32_t)high_vers;
    566       1.1       cgd 	SVC_REPLY(xprt, &rply);
    567       1.1       cgd }
    568       1.1       cgd 
    569       1.1       cgd /* ******************* SERVER INPUT STUFF ******************* */
    570       1.1       cgd 
    571       1.1       cgd /*
    572       1.1       cgd  * Get server side input from some transport.
    573       1.1       cgd  *
    574       1.1       cgd  * Statement of authentication parameters management:
    575       1.1       cgd  * This function owns and manages all authentication parameters, specifically
    576       1.1       cgd  * the "raw" parameters (msg.rm_call.cb_cred and msg.rm_call.cb_verf) and
    577       1.1       cgd  * the "cooked" credentials (rqst->rq_clntcred).
    578       1.1       cgd  * However, this function does not know the structure of the cooked
    579       1.1       cgd  * credentials, so it make the following assumptions:
    580       1.1       cgd  *   a) the structure is contiguous (no pointers), and
    581       1.1       cgd  *   b) the cred structure size does not exceed RQCRED_SIZE bytes.
    582       1.1       cgd  * In all events, all three parameters are freed upon exit from this routine.
    583       1.1       cgd  * The storage is trivially management on the call stack in user land, but
    584       1.1       cgd  * is mallocated in kernel land.
    585       1.1       cgd  */
    586       1.1       cgd 
    587       1.1       cgd void
    588       1.1       cgd svc_getreq(rdfds)
    589       1.1       cgd 	int rdfds;
    590       1.1       cgd {
    591       1.1       cgd 	fd_set readfds;
    592       1.1       cgd 
    593       1.1       cgd 	FD_ZERO(&readfds);
    594       1.1       cgd 	readfds.fds_bits[0] = rdfds;
    595       1.1       cgd 	svc_getreqset(&readfds);
    596       1.1       cgd }
    597       1.1       cgd 
    598       1.1       cgd void
    599       1.1       cgd svc_getreqset(readfds)
    600       1.1       cgd 	fd_set *readfds;
    601       1.1       cgd {
    602  1.19.2.1   minoura 	int bit, fd;
    603      1.15  christos 	int32_t mask, *maskp;
    604      1.14     lukem 	int sock;
    605       1.1       cgd 
    606      1.17     lukem 	_DIAGASSERT(readfds != NULL);
    607       1.1       cgd 
    608       1.4       cgd 	maskp = readfds->fds_bits;
    609       1.2   deraadt 	for (sock = 0; sock < FD_SETSIZE; sock += NFDBITS) {
    610      1.10  christos 	    for (mask = *maskp++; (bit = ffs(mask)) != 0;
    611      1.10  christos 		mask ^= (1 << (bit - 1))) {
    612       1.1       cgd 		/* sock has input waiting */
    613  1.19.2.1   minoura 		fd = sock + bit - 1;
    614  1.19.2.1   minoura 		svc_getreq_common(fd);
    615  1.19.2.1   minoura 	    }
    616  1.19.2.1   minoura 	}
    617  1.19.2.1   minoura }
    618  1.19.2.1   minoura 
    619  1.19.2.1   minoura void
    620  1.19.2.1   minoura svc_getreq_common(fd)
    621  1.19.2.1   minoura 	int fd;
    622  1.19.2.1   minoura {
    623  1.19.2.1   minoura 	SVCXPRT *xprt;
    624  1.19.2.1   minoura 	struct svc_req r;
    625  1.19.2.1   minoura 	struct rpc_msg msg;
    626  1.19.2.1   minoura 	int prog_found;
    627  1.19.2.1   minoura 	rpcvers_t low_vers;
    628  1.19.2.1   minoura 	rpcvers_t high_vers;
    629  1.19.2.1   minoura 	enum xprt_stat stat;
    630  1.19.2.1   minoura 	char cred_area[2*MAX_AUTH_BYTES + RQCRED_SIZE];
    631  1.19.2.1   minoura 
    632  1.19.2.1   minoura 	msg.rm_call.cb_cred.oa_base = cred_area;
    633  1.19.2.1   minoura 	msg.rm_call.cb_verf.oa_base = &(cred_area[MAX_AUTH_BYTES]);
    634  1.19.2.1   minoura 	r.rq_clntcred = &(cred_area[2*MAX_AUTH_BYTES]);
    635  1.19.2.1   minoura 
    636  1.19.2.1   minoura 	rwlock_rdlock(&svc_fd_lock);
    637  1.19.2.1   minoura 	xprt = xports[fd];
    638  1.19.2.1   minoura 	rwlock_unlock(&svc_fd_lock);
    639  1.19.2.1   minoura 	if (xprt == NULL)
    640  1.19.2.1   minoura 		/* But do we control sock? */
    641  1.19.2.1   minoura 		return;
    642  1.19.2.1   minoura 	/* now receive msgs from xprtprt (support batch calls) */
    643  1.19.2.1   minoura 	do {
    644  1.19.2.1   minoura 		if (SVC_RECV(xprt, &msg)) {
    645  1.19.2.1   minoura 
    646  1.19.2.1   minoura 			/* now find the exported program and call it */
    647  1.19.2.1   minoura 			struct svc_callout *s;
    648  1.19.2.1   minoura 			enum auth_stat why;
    649  1.19.2.1   minoura 
    650  1.19.2.1   minoura 			r.rq_xprt = xprt;
    651  1.19.2.1   minoura 			r.rq_prog = msg.rm_call.cb_prog;
    652  1.19.2.1   minoura 			r.rq_vers = msg.rm_call.cb_vers;
    653  1.19.2.1   minoura 			r.rq_proc = msg.rm_call.cb_proc;
    654  1.19.2.1   minoura 			r.rq_cred = msg.rm_call.cb_cred;
    655  1.19.2.1   minoura 			/* first authenticate the message */
    656  1.19.2.1   minoura 			if ((why = _authenticate(&r, &msg)) != AUTH_OK) {
    657  1.19.2.1   minoura 				svcerr_auth(xprt, why);
    658  1.19.2.1   minoura 				goto call_done;
    659       1.1       cgd 			}
    660  1.19.2.1   minoura 			/* now match message with a registered service*/
    661  1.19.2.1   minoura 			prog_found = FALSE;
    662  1.19.2.1   minoura 			low_vers = (rpcvers_t) -1L;
    663  1.19.2.1   minoura 			high_vers = (rpcvers_t) 0L;
    664  1.19.2.1   minoura 			for (s = svc_head; s != NULL_SVC; s = s->sc_next) {
    665  1.19.2.1   minoura 				if (s->sc_prog == r.rq_prog) {
    666  1.19.2.1   minoura 					if (s->sc_vers == r.rq_vers) {
    667  1.19.2.1   minoura 						(*s->sc_dispatch)(&r, xprt);
    668  1.19.2.1   minoura 						goto call_done;
    669  1.19.2.1   minoura 					}  /* found correct version */
    670  1.19.2.1   minoura 					prog_found = TRUE;
    671  1.19.2.1   minoura 					if (s->sc_vers < low_vers)
    672  1.19.2.1   minoura 						low_vers = s->sc_vers;
    673  1.19.2.1   minoura 					if (s->sc_vers > high_vers)
    674  1.19.2.1   minoura 						high_vers = s->sc_vers;
    675  1.19.2.1   minoura 				}   /* found correct program */
    676       1.1       cgd 			}
    677  1.19.2.1   minoura 			/*
    678  1.19.2.1   minoura 			 * if we got here, the program or version
    679  1.19.2.1   minoura 			 * is not served ...
    680  1.19.2.1   minoura 			 */
    681  1.19.2.1   minoura 			if (prog_found)
    682  1.19.2.1   minoura 				svcerr_progvers(xprt, low_vers, high_vers);
    683  1.19.2.1   minoura 			else
    684  1.19.2.1   minoura 				 svcerr_noprog(xprt);
    685  1.19.2.1   minoura 			/* Fall through to ... */
    686  1.19.2.1   minoura 		}
    687  1.19.2.1   minoura 		/*
    688  1.19.2.1   minoura 		 * Check if the xprt has been disconnected in a
    689  1.19.2.1   minoura 		 * recursive call in the service dispatch routine.
    690  1.19.2.1   minoura 		 * If so, then break.
    691  1.19.2.1   minoura 		 */
    692  1.19.2.1   minoura 		rwlock_rdlock(&svc_fd_lock);
    693  1.19.2.1   minoura 		if (xprt != xports[fd]) {
    694  1.19.2.1   minoura 			rwlock_unlock(&svc_fd_lock);
    695  1.19.2.1   minoura 			break;
    696  1.19.2.1   minoura 		}
    697  1.19.2.1   minoura 		rwlock_unlock(&svc_fd_lock);
    698  1.19.2.1   minoura call_done:
    699  1.19.2.1   minoura 		if ((stat = SVC_STAT(xprt)) == XPRT_DIED){
    700  1.19.2.1   minoura 			SVC_DESTROY(xprt);
    701  1.19.2.1   minoura 			break;
    702  1.19.2.1   minoura 		}
    703  1.19.2.1   minoura 	} while (stat == XPRT_MOREREQS);
    704  1.19.2.1   minoura }
    705  1.19.2.1   minoura 
    706  1.19.2.1   minoura 
    707  1.19.2.1   minoura void
    708  1.19.2.1   minoura svc_getreq_poll(pfdp, pollretval)
    709  1.19.2.1   minoura 	struct pollfd	*pfdp;
    710  1.19.2.1   minoura 	int	pollretval;
    711  1.19.2.1   minoura {
    712  1.19.2.1   minoura 	int i;
    713  1.19.2.1   minoura 	int fds_found;
    714  1.19.2.1   minoura 
    715  1.19.2.1   minoura 	for (i = fds_found = 0; fds_found < pollretval; i++) {
    716  1.19.2.1   minoura 		register struct pollfd *p = &pfdp[i];
    717  1.19.2.1   minoura 
    718  1.19.2.1   minoura 		if (p->revents) {
    719  1.19.2.1   minoura 			/* fd has input waiting */
    720  1.19.2.1   minoura 			fds_found++;
    721  1.19.2.1   minoura 			/*
    722  1.19.2.1   minoura 			 *	We assume that this function is only called
    723  1.19.2.1   minoura 			 *	via someone select()ing from svc_fdset or
    724  1.19.2.1   minoura 			 *	poll()ing from svc_pollset[].  Thus it's safe
    725  1.19.2.1   minoura 			 *	to handle the POLLNVAL event by simply turning
    726  1.19.2.1   minoura 			 *	the corresponding bit off in svc_fdset.  The
    727  1.19.2.1   minoura 			 *	svc_pollset[] array is derived from svc_fdset
    728  1.19.2.1   minoura 			 *	and so will also be updated eventually.
    729  1.19.2.1   minoura 			 *
    730  1.19.2.1   minoura 			 *	XXX Should we do an xprt_unregister() instead?
    731  1.19.2.1   minoura 			 */
    732  1.19.2.1   minoura 			if (p->revents & POLLNVAL) {
    733  1.19.2.1   minoura 				rwlock_wrlock(&svc_fd_lock);
    734  1.19.2.1   minoura 				FD_CLR(p->fd, &svc_fdset);
    735  1.19.2.1   minoura 				rwlock_unlock(&svc_fd_lock);
    736  1.19.2.1   minoura 			} else
    737  1.19.2.1   minoura 				svc_getreq_common(p->fd);
    738  1.19.2.1   minoura 		}
    739       1.1       cgd 	}
    740       1.1       cgd }
    741