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