xref: /freebsd/crypto/krb5/src/lib/rpc/svc_simple.c (revision 7f2fe78b9dd5f51c821d771b63d2e096f6fd49e9)
1*7f2fe78bSCy Schubert /* @(#)svc_simple.c	2.2 88/08/01 4.0 RPCSRC */
2*7f2fe78bSCy Schubert /*
3*7f2fe78bSCy Schubert  * Copyright (c) 2010, Oracle America, Inc.
4*7f2fe78bSCy Schubert  *
5*7f2fe78bSCy Schubert  * All rights reserved.
6*7f2fe78bSCy Schubert  *
7*7f2fe78bSCy Schubert  * Redistribution and use in source and binary forms, with or without
8*7f2fe78bSCy Schubert  * modification, are permitted provided that the following conditions are met:
9*7f2fe78bSCy Schubert  *
10*7f2fe78bSCy Schubert  *     * Redistributions of source code must retain the above copyright
11*7f2fe78bSCy Schubert  *       notice, this list of conditions and the following disclaimer.
12*7f2fe78bSCy Schubert  *
13*7f2fe78bSCy Schubert  *     * Redistributions in binary form must reproduce the above copyright
14*7f2fe78bSCy Schubert  *       notice, this list of conditions and the following disclaimer in
15*7f2fe78bSCy Schubert  *       the documentation and/or other materials provided with the
16*7f2fe78bSCy Schubert  *       distribution.
17*7f2fe78bSCy Schubert  *
18*7f2fe78bSCy Schubert  *     * Neither the name of the "Oracle America, Inc." nor the names of
19*7f2fe78bSCy Schubert  *       its contributors may be used to endorse or promote products
20*7f2fe78bSCy Schubert  *       derived from this software without specific prior written permission.
21*7f2fe78bSCy Schubert  *
22*7f2fe78bSCy Schubert  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
23*7f2fe78bSCy Schubert  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24*7f2fe78bSCy Schubert  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
25*7f2fe78bSCy Schubert  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26*7f2fe78bSCy Schubert  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27*7f2fe78bSCy Schubert  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
28*7f2fe78bSCy Schubert  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
29*7f2fe78bSCy Schubert  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30*7f2fe78bSCy Schubert  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31*7f2fe78bSCy Schubert  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32*7f2fe78bSCy Schubert  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33*7f2fe78bSCy Schubert  */
34*7f2fe78bSCy Schubert #if !defined(lint) && defined(SCCSIDS)
35*7f2fe78bSCy Schubert static char sccsid[] = "@(#)svc_simple.c 1.18 87/08/11 Copyr 1984 Sun Micro";
36*7f2fe78bSCy Schubert #endif
37*7f2fe78bSCy Schubert 
38*7f2fe78bSCy Schubert /*
39*7f2fe78bSCy Schubert  * svc_simple.c
40*7f2fe78bSCy Schubert  * Simplified front end to rpc.
41*7f2fe78bSCy Schubert  */
42*7f2fe78bSCy Schubert 
43*7f2fe78bSCy Schubert #include <stdio.h>
44*7f2fe78bSCy Schubert #include <string.h>
45*7f2fe78bSCy Schubert #include <gssrpc/rpc.h>
46*7f2fe78bSCy Schubert #include <gssrpc/pmap_clnt.h>
47*7f2fe78bSCy Schubert #include <sys/socket.h>
48*7f2fe78bSCy Schubert #include <netdb.h>
49*7f2fe78bSCy Schubert 
50*7f2fe78bSCy Schubert static struct proglst {
51*7f2fe78bSCy Schubert 	char *(*p_progname)();
52*7f2fe78bSCy Schubert 	int  p_prognum;
53*7f2fe78bSCy Schubert 	int  p_procnum;
54*7f2fe78bSCy Schubert 	xdrproc_t p_inproc, p_outproc;
55*7f2fe78bSCy Schubert 	struct proglst *p_nxt;
56*7f2fe78bSCy Schubert } *proglst;
57*7f2fe78bSCy Schubert static void universal(struct svc_req *, SVCXPRT *);
58*7f2fe78bSCy Schubert static SVCXPRT *transp;
59*7f2fe78bSCy Schubert 
60*7f2fe78bSCy Schubert int
registerrpc(rpcprog_t prognum,rpcvers_t versnum,rpcproc_t procnum,char * (* progname)(),xdrproc_t inproc,xdrproc_t outproc)61*7f2fe78bSCy Schubert registerrpc(
62*7f2fe78bSCy Schubert 	rpcprog_t prognum,
63*7f2fe78bSCy Schubert 	rpcvers_t versnum,
64*7f2fe78bSCy Schubert 	rpcproc_t procnum,
65*7f2fe78bSCy Schubert 	char *(*progname)(),
66*7f2fe78bSCy Schubert 	xdrproc_t inproc,
67*7f2fe78bSCy Schubert 	xdrproc_t outproc)
68*7f2fe78bSCy Schubert {
69*7f2fe78bSCy Schubert         struct proglst *pl;
70*7f2fe78bSCy Schubert 
71*7f2fe78bSCy Schubert 	if (procnum == NULLPROC) {
72*7f2fe78bSCy Schubert 		(void) fprintf(stderr,
73*7f2fe78bSCy Schubert 		    "can't reassign procedure number %d\n", NULLPROC);
74*7f2fe78bSCy Schubert 		return (-1);
75*7f2fe78bSCy Schubert 	}
76*7f2fe78bSCy Schubert 	if (transp == 0) {
77*7f2fe78bSCy Schubert 		transp = svcudp_create(RPC_ANYSOCK);
78*7f2fe78bSCy Schubert 		if (transp == NULL) {
79*7f2fe78bSCy Schubert 			(void) fprintf(stderr, "couldn't create an rpc server\n");
80*7f2fe78bSCy Schubert 			return (-1);
81*7f2fe78bSCy Schubert 		}
82*7f2fe78bSCy Schubert 	}
83*7f2fe78bSCy Schubert 	(void) pmap_unset(prognum, versnum);
84*7f2fe78bSCy Schubert 	if (!svc_register(transp, prognum, versnum, universal, IPPROTO_UDP)) {
85*7f2fe78bSCy Schubert 	    	(void) fprintf(stderr, "couldn't register prog %d vers %d\n",
86*7f2fe78bSCy Schubert 		    prognum, versnum);
87*7f2fe78bSCy Schubert 		return (-1);
88*7f2fe78bSCy Schubert 	}
89*7f2fe78bSCy Schubert 	pl = (struct proglst *)malloc(sizeof(struct proglst));
90*7f2fe78bSCy Schubert 	if (pl == NULL) {
91*7f2fe78bSCy Schubert 		(void) fprintf(stderr, "registerrpc: out of memory\n");
92*7f2fe78bSCy Schubert 		return (-1);
93*7f2fe78bSCy Schubert 	}
94*7f2fe78bSCy Schubert 	pl->p_progname = progname;
95*7f2fe78bSCy Schubert 	pl->p_prognum = prognum;
96*7f2fe78bSCy Schubert 	pl->p_procnum = procnum;
97*7f2fe78bSCy Schubert 	pl->p_inproc = inproc;
98*7f2fe78bSCy Schubert 	pl->p_outproc = outproc;
99*7f2fe78bSCy Schubert 	pl->p_nxt = proglst;
100*7f2fe78bSCy Schubert 	proglst = pl;
101*7f2fe78bSCy Schubert 	return (0);
102*7f2fe78bSCy Schubert }
103*7f2fe78bSCy Schubert 
104*7f2fe78bSCy Schubert static void
universal(struct svc_req * rqstp,SVCXPRT * s_transp)105*7f2fe78bSCy Schubert universal(
106*7f2fe78bSCy Schubert 	struct svc_req *rqstp,
107*7f2fe78bSCy Schubert 	SVCXPRT *s_transp)
108*7f2fe78bSCy Schubert {
109*7f2fe78bSCy Schubert 	int prog, proc;
110*7f2fe78bSCy Schubert 	char *outdata;
111*7f2fe78bSCy Schubert 	char xdrbuf[UDPMSGSIZE];
112*7f2fe78bSCy Schubert 	struct proglst *pl;
113*7f2fe78bSCy Schubert 
114*7f2fe78bSCy Schubert 	/*
115*7f2fe78bSCy Schubert 	 * enforce "procnum 0 is echo" convention
116*7f2fe78bSCy Schubert 	 */
117*7f2fe78bSCy Schubert 	if (rqstp->rq_proc == NULLPROC) {
118*7f2fe78bSCy Schubert 		if (svc_sendreply(s_transp, xdr_void, (char *)NULL) == FALSE) {
119*7f2fe78bSCy Schubert 			(void) fprintf(stderr, "xxx\n");
120*7f2fe78bSCy Schubert 			exit(1);
121*7f2fe78bSCy Schubert 		}
122*7f2fe78bSCy Schubert 		return;
123*7f2fe78bSCy Schubert 	}
124*7f2fe78bSCy Schubert 	prog = rqstp->rq_prog;
125*7f2fe78bSCy Schubert 	proc = rqstp->rq_proc;
126*7f2fe78bSCy Schubert 	for (pl = proglst; pl != NULL; pl = pl->p_nxt)
127*7f2fe78bSCy Schubert 		if (pl->p_prognum == prog && pl->p_procnum == proc) {
128*7f2fe78bSCy Schubert 			/* decode arguments into a CLEAN buffer */
129*7f2fe78bSCy Schubert 			memset(xdrbuf, 0, sizeof(xdrbuf)); /* required ! */
130*7f2fe78bSCy Schubert 			if (!svc_getargs(s_transp, pl->p_inproc, xdrbuf)) {
131*7f2fe78bSCy Schubert 				svcerr_decode(s_transp);
132*7f2fe78bSCy Schubert 				return;
133*7f2fe78bSCy Schubert 			}
134*7f2fe78bSCy Schubert 			outdata = (*(pl->p_progname))(xdrbuf);
135*7f2fe78bSCy Schubert 			if (outdata == NULL && pl->p_outproc != xdr_void)
136*7f2fe78bSCy Schubert 				/* there was an error */
137*7f2fe78bSCy Schubert 				return;
138*7f2fe78bSCy Schubert 			if (!svc_sendreply(s_transp, pl->p_outproc, outdata)) {
139*7f2fe78bSCy Schubert 				(void) fprintf(stderr,
140*7f2fe78bSCy Schubert 				    "trouble replying to prog %d\n",
141*7f2fe78bSCy Schubert 				    pl->p_prognum);
142*7f2fe78bSCy Schubert 				exit(1);
143*7f2fe78bSCy Schubert 			}
144*7f2fe78bSCy Schubert 			/* free the decoded arguments */
145*7f2fe78bSCy Schubert 			(void)svc_freeargs(s_transp, pl->p_inproc, xdrbuf);
146*7f2fe78bSCy Schubert 			return;
147*7f2fe78bSCy Schubert 		}
148*7f2fe78bSCy Schubert 	(void) fprintf(stderr, "never registered prog %d\n", prog);
149*7f2fe78bSCy Schubert 	exit(1);
150*7f2fe78bSCy Schubert }
151