1 /* $NetBSD: svc_simple.c,v 1.34 2024/01/23 17:24:38 christos Exp $ */
2
3 /*
4 * Copyright (c) 2010, Oracle America, Inc.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials
15 * provided with the distribution.
16 * * Neither the name of the "Oracle America, 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
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33 /*
34 * Copyright (c) 1986-1991 by Sun Microsystems Inc.
35 */
36
37 /* #pragma ident "@(#)svc_simple.c 1.18 94/04/24 SMI" */
38
39 /*
40 * svc_simple.c
41 * Simplified front end to rpc.
42 */
43
44 /*
45 * This interface creates a virtual listener for all the services
46 * started thru rpc_reg(). It listens on the same endpoint for
47 * all the services and then executes the corresponding service
48 * for the given prognum and procnum.
49 */
50
51 #include <sys/cdefs.h>
52 #if defined(LIBC_SCCS) && !defined(lint)
53 __RCSID("$NetBSD: svc_simple.c,v 1.34 2024/01/23 17:24:38 christos Exp $");
54 #endif
55
56 #include "namespace.h"
57 #include "reentrant.h"
58 #include <sys/types.h>
59 #include <rpc/rpc.h>
60 #include <rpc/nettype.h>
61 #include <assert.h>
62 #include <err.h>
63 #include <stdio.h>
64 #include <stdlib.h>
65 #include <string.h>
66
67 #include "rpc_internal.h"
68
69 #ifdef __weak_alias
70 __weak_alias(rpc_reg,_rpc_reg)
71 #endif
72
73 static void universal(struct svc_req *, SVCXPRT *);
74
75 static struct proglst {
76 char *(*p_progname)(char *);
77 rpcprog_t p_prognum;
78 rpcvers_t p_versnum;
79 rpcproc_t p_procnum;
80 SVCXPRT *p_transp;
81 char *p_netid;
82 char *p_xdrbuf;
83 int p_recvsz;
84 xdrproc_t p_inproc, p_outproc;
85 struct proglst *p_nxt;
86 } *proglst;
87
88 static const char __reg_err1[] = "can't find appropriate transport";
89 static const char __reg_err2[] = "can't get protocol info";
90 static const char __reg_err3[] = "unsupported transport size";
91 static const char __no_mem_str[] = "out of memory";
92
93 /*
94 * For simplified, easy to use kind of rpc interfaces.
95 * nettype indicates the type of transport on which the service will be
96 * listening. Used for conservation of the system resource. Only one
97 * handle is created for all the services (actually one of each netid)
98 * and same xdrbuf is used for same netid. The size of the arguments
99 * is also limited by the recvsize for that transport, even if it is
100 * a COTS transport. This may be wrong, but for cases like these, they
101 * should not use the simplified interfaces like this.
102 */
103
104 int
rpc_reg(rpcprog_t prognum,rpcvers_t versnum,rpcproc_t procnum,char * (* progname)(char *),xdrproc_t inproc,xdrproc_t outproc,char * nettype)105 rpc_reg(
106 rpcprog_t prognum, /* program number */
107 rpcvers_t versnum, /* version number */
108 rpcproc_t procnum, /* procedure number */
109 char *(*progname)(char *), /* Server routine */
110 xdrproc_t inproc, /* in XDR procedure */
111 xdrproc_t outproc, /* out XDR procedure */
112 char *nettype) /* nettype */
113 {
114 struct netconfig *nconf;
115 int done = FALSE;
116 void *handle;
117
118 if (procnum == NULLPROC) {
119 warnx("%s: can't reassign procedure number %u", __func__,
120 NULLPROC);
121 return (-1);
122 }
123
124 if (nettype == NULL)
125 nettype = __UNCONST("netpath"); /* The default behavior */
126 if ((handle = __rpc_setconf(nettype)) == NULL) {
127 warnx("%s: %s", __func__, __reg_err1);
128 return (-1);
129 }
130 /* VARIABLES PROTECTED BY proglst_lock: proglst */
131 mutex_lock(&proglst_lock);
132 while ((nconf = __rpc_getconf(handle)) != NULL) {
133 struct proglst *pl;
134 SVCXPRT *svcxprt;
135 int madenow;
136 u_int recvsz;
137 char *xdrbuf;
138 char *netid;
139
140 madenow = FALSE;
141 svcxprt = NULL;
142 recvsz = 0;
143 xdrbuf = NULL;
144 netid = NULL;
145 for (pl = proglst; pl; pl = pl->p_nxt)
146 if (strcmp(pl->p_netid, nconf->nc_netid) == 0) {
147 svcxprt = pl->p_transp;
148 xdrbuf = pl->p_xdrbuf;
149 recvsz = pl->p_recvsz;
150 netid = pl->p_netid;
151 break;
152 }
153
154 if (svcxprt == NULL) {
155 struct __rpc_sockinfo si;
156
157 svcxprt = svc_tli_create(RPC_ANYFD, nconf, NULL, 0, 0);
158 if (svcxprt == NULL)
159 continue;
160 if (!__rpc_fd2sockinfo(svcxprt->xp_fd, &si)) {
161 warnx("%s: %s", __func__, __reg_err2);
162 SVC_DESTROY(svcxprt);
163 continue;
164 }
165 recvsz = __rpc_get_t_size(si.si_af, si.si_proto, 0);
166 if (recvsz == 0) {
167 warnx("%s: %s", __func__, __reg_err3);
168 SVC_DESTROY(svcxprt);
169 continue;
170 }
171 if (((xdrbuf = mem_alloc((size_t)recvsz)) == NULL) ||
172 ((netid = strdup(nconf->nc_netid)) == NULL)) {
173 warnx("%s: %s", __func__, __no_mem_str);
174 if (xdrbuf != NULL)
175 free(xdrbuf);
176 if (netid != NULL)
177 free(netid);
178 SVC_DESTROY(svcxprt);
179 break;
180 }
181 madenow = TRUE;
182 }
183 /*
184 * Check if this (program, version, netid) had already been
185 * registered. The check may save a few RPC calls to rpcbind
186 */
187 for (pl = proglst; pl; pl = pl->p_nxt)
188 if ((pl->p_prognum == prognum) &&
189 (pl->p_versnum == versnum) &&
190 (strcmp(pl->p_netid, netid) == 0))
191 break;
192 if (pl == NULL) { /* Not yet */
193 (void) rpcb_unset(prognum, versnum, nconf);
194 } else {
195 /* so that svc_reg does not call rpcb_set() */
196 nconf = NULL;
197 }
198
199 if (!svc_reg(svcxprt, prognum, versnum, universal, nconf)) {
200 warnx("%s: couldn't register prog %u vers %u for %s",
201 __func__, (unsigned)prognum,
202 (unsigned)versnum, netid);
203 if (madenow) {
204 SVC_DESTROY(svcxprt);
205 free(xdrbuf);
206 free(netid);
207 }
208 continue;
209 }
210
211 pl = malloc(sizeof(*pl));
212 if (pl == NULL) {
213 warn("%s: %s", __func__, __no_mem_str);
214 if (madenow) {
215 SVC_DESTROY(svcxprt);
216 free(xdrbuf);
217 free(netid);
218 }
219 break;
220 }
221 pl->p_progname = progname;
222 pl->p_prognum = prognum;
223 pl->p_versnum = versnum;
224 pl->p_procnum = procnum;
225 pl->p_inproc = inproc;
226 pl->p_outproc = outproc;
227 pl->p_transp = svcxprt;
228 pl->p_xdrbuf = xdrbuf;
229 pl->p_recvsz = recvsz;
230 pl->p_netid = netid;
231 pl->p_nxt = proglst;
232 proglst = pl;
233 done = TRUE;
234 }
235 __rpc_endconf(handle);
236 mutex_unlock(&proglst_lock);
237
238 if (done == FALSE) {
239 warnx("%s: can't find suitable transport for %s",
240 __func__, nettype);
241 return (-1);
242 }
243 return (0);
244 }
245
246 /*
247 * The universal handler for the services registered using registerrpc.
248 * It handles both the connectionless and the connection oriented cases.
249 */
250
251 static void
universal(struct svc_req * rqstp,SVCXPRT * transp)252 universal(struct svc_req *rqstp, SVCXPRT *transp)
253 {
254 rpcprog_t prog;
255 rpcvers_t vers;
256 rpcproc_t proc;
257 char *outdata;
258 char *xdrbuf;
259 struct proglst *pl;
260
261 _DIAGASSERT(rqstp != NULL);
262 _DIAGASSERT(transp != NULL);
263
264 /*
265 * enforce "procnum 0 is echo" convention
266 */
267 if (rqstp->rq_proc == NULLPROC) {
268 if (svc_sendreply(transp, (xdrproc_t) xdr_void, NULL) ==
269 FALSE) {
270 warnx("%s: svc_sendreply failed", __func__);
271 }
272 return;
273 }
274 prog = rqstp->rq_prog;
275 vers = rqstp->rq_vers;
276 proc = rqstp->rq_proc;
277 mutex_lock(&proglst_lock);
278 for (pl = proglst; pl; pl = pl->p_nxt)
279 if (pl->p_prognum == prog && pl->p_procnum == proc &&
280 pl->p_versnum == vers &&
281 (strcmp(pl->p_netid, transp->xp_netid) == 0)) {
282 /* decode arguments into a CLEAN buffer */
283 xdrbuf = pl->p_xdrbuf;
284 /* Zero the arguments: reqd ! */
285 (void) memset(xdrbuf, 0, (size_t)pl->p_recvsz);
286 /*
287 * Assuming that sizeof (xdrbuf) would be enough
288 * for the arguments; if not then the program
289 * may bomb. BEWARE!
290 */
291 if (!svc_getargs(transp, pl->p_inproc, xdrbuf)) {
292 svcerr_decode(transp);
293 mutex_unlock(&proglst_lock);
294 return;
295 }
296 outdata = (*(pl->p_progname))(xdrbuf);
297 if (outdata == NULL &&
298 pl->p_outproc != (xdrproc_t) xdr_void){
299 /* there was an error */
300 mutex_unlock(&proglst_lock);
301 return;
302 }
303 if (!svc_sendreply(transp, pl->p_outproc, outdata)) {
304 warnx("%s: trouble replying to prog %u vers %u",
305 __func__, (unsigned)prog, (unsigned)vers);
306 mutex_unlock(&proglst_lock);
307 return;
308 }
309 /* free the decoded arguments */
310 (void) svc_freeargs(transp, pl->p_inproc, xdrbuf);
311 mutex_unlock(&proglst_lock);
312 return;
313 }
314 mutex_unlock(&proglst_lock);
315 /* This should never happen */
316 warnx("%s: never registered prog %u vers %u", __func__,
317 (unsigned)prog, (unsigned)vers);
318 return;
319 }
320