xref: /freebsd/lib/libc/rpc/svc_simple.c (revision 2008043f386721d58158e37e0d7e50df8095942d)
1 /*	$NetBSD: svc_simple.c,v 1.20 2000/07/06 03:10:35 christos Exp $	*/
2 
3 /*-
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  * Copyright (c) 2009, Sun Microsystems, Inc.
7  * All rights reserved.
8  *
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.
19  *
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.
31  */
32 /*
33  * Copyright (c) 1986-1991 by Sun Microsystems Inc.
34  */
35 
36 /* #pragma ident	"@(#)svc_simple.c	1.18	94/04/24 SMI" */
37 /*
38  * svc_simple.c
39  * Simplified front end to rpc.
40  */
41 
42 /*
43  * This interface creates a virtual listener for all the services
44  * started through rpc_reg(). It listens on the same endpoint for
45  * all the services and then executes the corresponding service
46  * for the given prognum and procnum.
47  */
48 
49 #include "namespace.h"
50 #include "reentrant.h"
51 #include <sys/types.h>
52 #include <rpc/rpc.h>
53 #include <rpc/nettype.h>
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <string.h>
57 #include <err.h>
58 #include "un-namespace.h"
59 
60 #include "rpc_com.h"
61 #include "mt_misc.h"
62 
63 static void universal(struct svc_req *, SVCXPRT *);
64 
65 static struct proglst {
66 	char *(*p_progname)(char *);
67 	rpcprog_t p_prognum;
68 	rpcvers_t p_versnum;
69 	rpcproc_t p_procnum;
70 	SVCXPRT *p_transp;
71 	char *p_netid;
72 	char *p_xdrbuf;
73 	int p_recvsz;
74 	xdrproc_t p_inproc, p_outproc;
75 	struct proglst *p_nxt;
76 } *proglst;
77 
78 static const char rpc_reg_err[] = "%s: %s";
79 static const char rpc_reg_msg[] = "rpc_reg: ";
80 static const char __reg_err1[] = "can't find appropriate transport";
81 static const char __reg_err2[] = "can't get protocol info";
82 static const char __reg_err3[] = "unsupported transport size";
83 static const char __no_mem_str[] = "out of memory";
84 
85 /*
86  * For simplified, easy to use kind of rpc interfaces.
87  * nettype indicates the type of transport on which the service will be
88  * listening. Used for conservation of the system resource. Only one
89  * handle is created for all the services (actually one of each netid)
90  * and same xdrbuf is used for same netid. The size of the arguments
91  * is also limited by the recvsize for that transport, even if it is
92  * a COTS transport. This may be wrong, but for cases like these, they
93  * should not use the simplified interfaces like this.
94  *
95  * prognum - program number
96  * versnum - version number
97  * procnum - procedure number
98  * progname - Server routine
99  * inproc, outproc - in/out XDR procedures
100  * nettype - nettype
101  */
102 int
103 rpc_reg(rpcprog_t prognum, rpcvers_t versnum, rpcproc_t procnum,
104     char *(*progname)(char *), xdrproc_t inproc, xdrproc_t outproc,
105     char *nettype)
106 {
107 	struct netconfig *nconf;
108 	int done = FALSE;
109 	void *handle;
110 
111 
112 	if (procnum == NULLPROC) {
113 		warnx("%s can't reassign procedure number %u", rpc_reg_msg,
114 			NULLPROC);
115 		return (-1);
116 	}
117 
118 	if (nettype == NULL)
119 		nettype = "netpath";		/* The default behavior */
120 	if ((handle = __rpc_setconf(nettype)) == NULL) {
121 		warnx(rpc_reg_err, rpc_reg_msg, __reg_err1);
122 		return (-1);
123 	}
124 /* VARIABLES PROTECTED BY proglst_lock: proglst */
125 	mutex_lock(&proglst_lock);
126 	while ((nconf = __rpc_getconf(handle)) != NULL) {
127 		struct proglst *pl;
128 		SVCXPRT *svcxprt;
129 		int madenow;
130 		u_int recvsz;
131 		char *xdrbuf;
132 		char *netid;
133 
134 		madenow = FALSE;
135 		svcxprt = NULL;
136 		recvsz = 0;
137 		xdrbuf = netid = NULL;
138 		for (pl = proglst; pl; pl = pl->p_nxt) {
139 			if (strcmp(pl->p_netid, nconf->nc_netid) == 0) {
140 				svcxprt = pl->p_transp;
141 				xdrbuf = pl->p_xdrbuf;
142 				recvsz = pl->p_recvsz;
143 				netid = pl->p_netid;
144 				break;
145 			}
146 		}
147 
148 		if (svcxprt == NULL) {
149 			struct __rpc_sockinfo si;
150 
151 			svcxprt = svc_tli_create(RPC_ANYFD, nconf, NULL, 0, 0);
152 			if (svcxprt == NULL)
153 				continue;
154 			if (!__rpc_fd2sockinfo(svcxprt->xp_fd, &si)) {
155 				warnx(rpc_reg_err, rpc_reg_msg, __reg_err2);
156 				SVC_DESTROY(svcxprt);
157 				continue;
158 			}
159 			recvsz = __rpc_get_t_size(si.si_af, si.si_proto, 0);
160 			if (recvsz == 0) {
161 				warnx(rpc_reg_err, rpc_reg_msg, __reg_err3);
162 				SVC_DESTROY(svcxprt);
163 				continue;
164 			}
165 			if (((xdrbuf = malloc((unsigned)recvsz)) == NULL) ||
166 				((netid = strdup(nconf->nc_netid)) == NULL)) {
167 				warnx(rpc_reg_err, rpc_reg_msg, __no_mem_str);
168 				free(xdrbuf);
169 				free(netid);
170 				SVC_DESTROY(svcxprt);
171 				break;
172 			}
173 			madenow = TRUE;
174 		}
175 		/*
176 		 * Check if this (program, version, netid) had already been
177 		 * registered.  The check may save a few RPC calls to rpcbind
178 		 */
179 		for (pl = proglst; pl; pl = pl->p_nxt)
180 			if ((pl->p_prognum == prognum) &&
181 				(pl->p_versnum == versnum) &&
182 				(strcmp(pl->p_netid, netid) == 0))
183 				break;
184 		if (pl == NULL) { /* Not yet */
185 			(void) rpcb_unset(prognum, versnum, nconf);
186 		} else {
187 			/* so that svc_reg does not call rpcb_set() */
188 			nconf = NULL;
189 		}
190 
191 		if (!svc_reg(svcxprt, prognum, versnum, universal, nconf)) {
192 			warnx("%s couldn't register prog %u vers %u for %s",
193 				rpc_reg_msg, (unsigned)prognum,
194 				(unsigned)versnum, netid);
195 			if (madenow) {
196 				SVC_DESTROY(svcxprt);
197 				free(xdrbuf);
198 				free(netid);
199 			}
200 			continue;
201 		}
202 
203 		pl = malloc(sizeof (struct proglst));
204 		if (pl == NULL) {
205 			warnx(rpc_reg_err, rpc_reg_msg, __no_mem_str);
206 			if (madenow) {
207 				SVC_DESTROY(svcxprt);
208 				free(xdrbuf);
209 				free(netid);
210 			}
211 			break;
212 		}
213 		pl->p_progname = progname;
214 		pl->p_prognum = prognum;
215 		pl->p_versnum = versnum;
216 		pl->p_procnum = procnum;
217 		pl->p_inproc = inproc;
218 		pl->p_outproc = outproc;
219 		pl->p_transp = svcxprt;
220 		pl->p_xdrbuf = xdrbuf;
221 		pl->p_recvsz = recvsz;
222 		pl->p_netid = netid;
223 		pl->p_nxt = proglst;
224 		proglst = pl;
225 		done = TRUE;
226 	}
227 	__rpc_endconf(handle);
228 	mutex_unlock(&proglst_lock);
229 
230 	if (done == FALSE) {
231 		warnx("%s can't find suitable transport for %s",
232 			rpc_reg_msg, nettype);
233 		return (-1);
234 	}
235 	return (0);
236 }
237 
238 /*
239  * The universal handler for the services registered using registerrpc.
240  * It handles both the connectionless and the connection oriented cases.
241  */
242 
243 static void
244 universal(struct svc_req *rqstp, SVCXPRT *transp)
245 {
246 	rpcprog_t prog;
247 	rpcvers_t vers;
248 	rpcproc_t proc;
249 	char *outdata;
250 	char *xdrbuf;
251 	struct proglst *pl;
252 
253 	/*
254 	 * enforce "procnum 0 is echo" convention
255 	 */
256 	if (rqstp->rq_proc == NULLPROC) {
257 		if (svc_sendreply(transp, (xdrproc_t) xdr_void, NULL) ==
258 		    FALSE) {
259 			warnx("svc_sendreply failed");
260 		}
261 		return;
262 	}
263 	prog = rqstp->rq_prog;
264 	vers = rqstp->rq_vers;
265 	proc = rqstp->rq_proc;
266 	mutex_lock(&proglst_lock);
267 	for (pl = proglst; pl; pl = pl->p_nxt)
268 		if (pl->p_prognum == prog && pl->p_procnum == proc &&
269 			pl->p_versnum == vers &&
270 			(strcmp(pl->p_netid, transp->xp_netid) == 0)) {
271 			/* decode arguments into a CLEAN buffer */
272 			xdrbuf = pl->p_xdrbuf;
273 			/* Zero the arguments: reqd ! */
274 			(void) memset(xdrbuf, 0, (size_t)pl->p_recvsz);
275 			/*
276 			 * Assuming that sizeof (xdrbuf) would be enough
277 			 * for the arguments; if not then the program
278 			 * may bomb. BEWARE!
279 			 */
280 			if (!svc_getargs(transp, pl->p_inproc, xdrbuf)) {
281 				svcerr_decode(transp);
282 				mutex_unlock(&proglst_lock);
283 				return;
284 			}
285 			outdata = (*(pl->p_progname))(xdrbuf);
286 			if (outdata == NULL &&
287 				pl->p_outproc != (xdrproc_t) xdr_void){
288 				/* there was an error */
289 				mutex_unlock(&proglst_lock);
290 				return;
291 			}
292 			if (!svc_sendreply(transp, pl->p_outproc, outdata)) {
293 				warnx(
294 			"rpc: rpc_reg trouble replying to prog %u vers %u",
295 				(unsigned)prog, (unsigned)vers);
296 				mutex_unlock(&proglst_lock);
297 				return;
298 			}
299 			/* free the decoded arguments */
300 			(void)svc_freeargs(transp, pl->p_inproc, xdrbuf);
301 			mutex_unlock(&proglst_lock);
302 			return;
303 		}
304 	mutex_unlock(&proglst_lock);
305 	/* This should never happen */
306 	warnx("rpc: rpc_reg: never registered prog %u vers %u",
307 		(unsigned)prog, (unsigned)vers);
308 	return;
309 }
310