xref: /netbsd/lib/libc/rpc/svc_simple.c (revision 15c1b43e)
1*15c1b43eStron /*	$NetBSD: svc_simple.c,v 1.33 2013/03/11 20:19:29 tron Exp $	*/
29e15c989Scgd 
363d7b677Scgd /*
4*15c1b43eStron  * Copyright (c) 2010, Oracle America, Inc.
563d7b677Scgd  *
6*15c1b43eStron  * Redistribution and use in source and binary forms, with or without
7*15c1b43eStron  * modification, are permitted provided that the following conditions are
8*15c1b43eStron  * met:
963d7b677Scgd  *
10*15c1b43eStron  *     * Redistributions of source code must retain the above copyright
11*15c1b43eStron  *       notice, this list of conditions and the following disclaimer.
12*15c1b43eStron  *     * Redistributions in binary form must reproduce the above
13*15c1b43eStron  *       copyright notice, this list of conditions and the following
14*15c1b43eStron  *       disclaimer in the documentation and/or other materials
15*15c1b43eStron  *       provided with the distribution.
16*15c1b43eStron  *     * Neither the name of the "Oracle America, Inc." nor the names of its
17*15c1b43eStron  *       contributors may be used to endorse or promote products derived
18*15c1b43eStron  *       from this software without specific prior written permission.
1963d7b677Scgd  *
20*15c1b43eStron  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21*15c1b43eStron  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22*15c1b43eStron  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23*15c1b43eStron  *   FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24*15c1b43eStron  *   COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25*15c1b43eStron  *   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26*15c1b43eStron  *   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27*15c1b43eStron  *   GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28*15c1b43eStron  *   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29*15c1b43eStron  *   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30*15c1b43eStron  *   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31*15c1b43eStron  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3263d7b677Scgd  */
337df0ccbaSfvdl /*
347df0ccbaSfvdl  * Copyright (c) 1986-1991 by Sun Microsystems Inc.
357df0ccbaSfvdl  */
3663d7b677Scgd 
377df0ccbaSfvdl /* #pragma ident	"@(#)svc_simple.c	1.18	94/04/24 SMI" */
3863d7b677Scgd 
3963d7b677Scgd /*
4063d7b677Scgd  * svc_simple.c
4163d7b677Scgd  * Simplified front end to rpc.
427df0ccbaSfvdl  */
437df0ccbaSfvdl 
447df0ccbaSfvdl /*
457df0ccbaSfvdl  * This interface creates a virtual listener for all the services
467df0ccbaSfvdl  * started thru rpc_reg(). It listens on the same endpoint for
477df0ccbaSfvdl  * all the services and then executes the corresponding service
487df0ccbaSfvdl  * for the given prognum and procnum.
4963d7b677Scgd  */
5063d7b677Scgd 
515c945215Sitojun #include <sys/cdefs.h>
525c945215Sitojun #if defined(LIBC_SCCS) && !defined(lint)
53*15c1b43eStron __RCSID("$NetBSD: svc_simple.c,v 1.33 2013/03/11 20:19:29 tron Exp $");
545c945215Sitojun #endif
555c945215Sitojun 
5643fa6fe3Sjtc #include "namespace.h"
577df0ccbaSfvdl #include "reentrant.h"
5846e6c5e8Slukem #include <sys/types.h>
597df0ccbaSfvdl #include <rpc/rpc.h>
60deb154d2Schristos #include <rpc/nettype.h>
610e8cfd8fSlukem #include <assert.h>
620e8cfd8fSlukem #include <err.h>
6363d7b677Scgd #include <stdio.h>
642e2a3a25Scgd #include <stdlib.h>
653c565bbeSjtc #include <string.h>
6646e6c5e8Slukem 
6779d5b270Sfvdl #include "rpc_internal.h"
6863d7b677Scgd 
6943fa6fe3Sjtc #ifdef __weak_alias
707df0ccbaSfvdl __weak_alias(rpc_reg,_rpc_reg)
7143fa6fe3Sjtc #endif
7243fa6fe3Sjtc 
73c30c4c40Smatt static void universal(struct svc_req *, SVCXPRT *);
747df0ccbaSfvdl 
7563d7b677Scgd static struct proglst {
76c30c4c40Smatt 	char *(*p_progname)(char *);
777df0ccbaSfvdl 	rpcprog_t p_prognum;
787df0ccbaSfvdl 	rpcvers_t p_versnum;
797df0ccbaSfvdl 	rpcproc_t p_procnum;
807df0ccbaSfvdl 	SVCXPRT *p_transp;
817df0ccbaSfvdl 	char *p_netid;
827df0ccbaSfvdl 	char *p_xdrbuf;
837df0ccbaSfvdl 	int p_recvsz;
8463d7b677Scgd 	xdrproc_t p_inproc, p_outproc;
8563d7b677Scgd 	struct proglst *p_nxt;
8663d7b677Scgd } *proglst;
87c63c52b2Schristos 
887df0ccbaSfvdl static const char __reg_err1[] = "can't find appropriate transport";
897df0ccbaSfvdl static const char __reg_err2[] = "can't get protocol info";
907df0ccbaSfvdl static const char __reg_err3[] = "unsupported transport size";
917df0ccbaSfvdl static const char __no_mem_str[] = "out of memory";
9263d7b677Scgd 
937df0ccbaSfvdl /*
947df0ccbaSfvdl  * For simplified, easy to use kind of rpc interfaces.
957df0ccbaSfvdl  * nettype indicates the type of transport on which the service will be
967df0ccbaSfvdl  * listening. Used for conservation of the system resource. Only one
977df0ccbaSfvdl  * handle is created for all the services (actually one of each netid)
987df0ccbaSfvdl  * and same xdrbuf is used for same netid. The size of the arguments
997df0ccbaSfvdl  * is also limited by the recvsize for that transport, even if it is
1007df0ccbaSfvdl  * a COTS transport. This may be wrong, but for cases like these, they
1017df0ccbaSfvdl  * should not use the simplified interfaces like this.
1027df0ccbaSfvdl  */
103c63c52b2Schristos 
10418ec2ba0Sjtc int
rpc_reg(rpcprog_t prognum,rpcvers_t versnum,rpcproc_t procnum,char * (* progname)(char *),xdrproc_t inproc,xdrproc_t outproc,char * nettype)105c30c4c40Smatt rpc_reg(
106c30c4c40Smatt 	rpcprog_t prognum,		/* program number */
107c30c4c40Smatt 	rpcvers_t versnum,		/* version number */
108c30c4c40Smatt 	rpcproc_t procnum,		/* procedure number */
109c30c4c40Smatt 	char *(*progname)(char *),	/* Server routine */
110c30c4c40Smatt 	xdrproc_t inproc,		/* in XDR procedure */
111c30c4c40Smatt 	xdrproc_t outproc,		/* out XDR procedure */
112c30c4c40Smatt 	char *nettype)			/* nettype */
11363d7b677Scgd {
1147df0ccbaSfvdl 	struct netconfig *nconf;
1157df0ccbaSfvdl 	int done = FALSE;
1167df0ccbaSfvdl 	void *handle;
1173fdac2b8Sthorpej #ifdef _REENTRANT
1187df0ccbaSfvdl 	extern mutex_t proglst_lock;
1197df0ccbaSfvdl #endif
1207df0ccbaSfvdl 
12163d7b677Scgd 	if (procnum == NULLPROC) {
122cd136b95Schristos 		warnx("%s: can't reassign procedure number %u", __func__,
1237df0ccbaSfvdl 		    NULLPROC);
12463d7b677Scgd 		return (-1);
12563d7b677Scgd 	}
1267df0ccbaSfvdl 
1277df0ccbaSfvdl 	if (nettype == NULL)
12803256c6eSchristos 		nettype = __UNCONST("netpath");	/* The default behavior */
1297df0ccbaSfvdl 	if ((handle = __rpc_setconf(nettype)) == NULL) {
130cd136b95Schristos 		warnx("%s: %s", __func__, __reg_err1);
13163d7b677Scgd 		return (-1);
13263d7b677Scgd 	}
1337df0ccbaSfvdl /* VARIABLES PROTECTED BY proglst_lock: proglst */
1347df0ccbaSfvdl 	mutex_lock(&proglst_lock);
135deb154d2Schristos 	while ((nconf = __rpc_getconf(handle)) != NULL) {
1367df0ccbaSfvdl 		struct proglst *pl;
1377df0ccbaSfvdl 		SVCXPRT *svcxprt;
1387df0ccbaSfvdl 		int madenow;
1397df0ccbaSfvdl 		u_int recvsz;
1407df0ccbaSfvdl 		char *xdrbuf;
1417df0ccbaSfvdl 		char *netid;
1427df0ccbaSfvdl 
1437df0ccbaSfvdl 		madenow = FALSE;
144deb154d2Schristos 		svcxprt = NULL;
14546af2990Slukem 		recvsz = 0;
14646af2990Slukem 		xdrbuf = NULL;
14746af2990Slukem 		netid = NULL;
1487df0ccbaSfvdl 		for (pl = proglst; pl; pl = pl->p_nxt)
1497df0ccbaSfvdl 			if (strcmp(pl->p_netid, nconf->nc_netid) == 0) {
1507df0ccbaSfvdl 				svcxprt = pl->p_transp;
1517df0ccbaSfvdl 				xdrbuf = pl->p_xdrbuf;
1527df0ccbaSfvdl 				recvsz = pl->p_recvsz;
1537df0ccbaSfvdl 				netid = pl->p_netid;
1547df0ccbaSfvdl 				break;
15563d7b677Scgd 			}
1567df0ccbaSfvdl 
157deb154d2Schristos 		if (svcxprt == NULL) {
1587df0ccbaSfvdl 			struct __rpc_sockinfo si;
1597df0ccbaSfvdl 
160deb154d2Schristos 			svcxprt = svc_tli_create(RPC_ANYFD, nconf, NULL, 0, 0);
161deb154d2Schristos 			if (svcxprt == NULL)
1627df0ccbaSfvdl 				continue;
1637df0ccbaSfvdl 			if (!__rpc_fd2sockinfo(svcxprt->xp_fd, &si)) {
164cd136b95Schristos 				warnx("%s: %s", __func__, __reg_err2);
1657df0ccbaSfvdl 				SVC_DESTROY(svcxprt);
1667df0ccbaSfvdl 				continue;
16763d7b677Scgd 			}
1687df0ccbaSfvdl 			recvsz = __rpc_get_t_size(si.si_af, si.si_proto, 0);
1697df0ccbaSfvdl 			if (recvsz == 0) {
170cd136b95Schristos 				warnx("%s: %s", __func__, __reg_err3);
1717df0ccbaSfvdl 				SVC_DESTROY(svcxprt);
1727df0ccbaSfvdl 				continue;
1737df0ccbaSfvdl 			}
174cd136b95Schristos 			if (((xdrbuf = mem_alloc((size_t)recvsz)) == NULL) ||
1757df0ccbaSfvdl 				((netid = strdup(nconf->nc_netid)) == NULL)) {
176cd136b95Schristos 				warnx("%s: %s", __func__, __no_mem_str);
177a742aacdSchristos 				if (xdrbuf != NULL)
178a742aacdSchristos 					free(xdrbuf);
179a742aacdSchristos 				if (netid != NULL)
180a742aacdSchristos 					free(netid);
1817df0ccbaSfvdl 				SVC_DESTROY(svcxprt);
1827df0ccbaSfvdl 				break;
1837df0ccbaSfvdl 			}
1847df0ccbaSfvdl 			madenow = TRUE;
1857df0ccbaSfvdl 		}
1867df0ccbaSfvdl 		/*
1877df0ccbaSfvdl 		 * Check if this (program, version, netid) had already been
1887df0ccbaSfvdl 		 * registered.  The check may save a few RPC calls to rpcbind
1897df0ccbaSfvdl 		 */
1907df0ccbaSfvdl 		for (pl = proglst; pl; pl = pl->p_nxt)
1917df0ccbaSfvdl 			if ((pl->p_prognum == prognum) &&
1927df0ccbaSfvdl 				(pl->p_versnum == versnum) &&
1937df0ccbaSfvdl 				(strcmp(pl->p_netid, netid) == 0))
1947df0ccbaSfvdl 				break;
1957df0ccbaSfvdl 		if (pl == NULL) { /* Not yet */
1967df0ccbaSfvdl 			(void) rpcb_unset(prognum, versnum, nconf);
1977df0ccbaSfvdl 		} else {
1987df0ccbaSfvdl 			/* so that svc_reg does not call rpcb_set() */
1997df0ccbaSfvdl 			nconf = NULL;
2007df0ccbaSfvdl 		}
2017df0ccbaSfvdl 
2027df0ccbaSfvdl 		if (!svc_reg(svcxprt, prognum, versnum, universal, nconf)) {
203cd136b95Schristos 			warnx("%s: couldn't register prog %u vers %u for %s",
204cd136b95Schristos 			    __func__, (unsigned)prognum,
2057df0ccbaSfvdl 			    (unsigned)versnum, netid);
2067df0ccbaSfvdl 			if (madenow) {
2077df0ccbaSfvdl 				SVC_DESTROY(svcxprt);
2087df0ccbaSfvdl 				free(xdrbuf);
2097df0ccbaSfvdl 				free(netid);
2107df0ccbaSfvdl 			}
2117df0ccbaSfvdl 			continue;
2127df0ccbaSfvdl 		}
2137df0ccbaSfvdl 
214c9cdc302Schristos 		pl = malloc(sizeof(*pl));
215deb154d2Schristos 		if (pl == NULL) {
216cd136b95Schristos 			warn("%s: %s", __func__, __no_mem_str);
2177df0ccbaSfvdl 			if (madenow) {
2187df0ccbaSfvdl 				SVC_DESTROY(svcxprt);
2197df0ccbaSfvdl 				free(xdrbuf);
2207df0ccbaSfvdl 				free(netid);
2217df0ccbaSfvdl 			}
2227df0ccbaSfvdl 			break;
22363d7b677Scgd 		}
22463d7b677Scgd 		pl->p_progname = progname;
22563d7b677Scgd 		pl->p_prognum = prognum;
2267df0ccbaSfvdl 		pl->p_versnum = versnum;
22763d7b677Scgd 		pl->p_procnum = procnum;
22863d7b677Scgd 		pl->p_inproc = inproc;
22963d7b677Scgd 		pl->p_outproc = outproc;
2307df0ccbaSfvdl 		pl->p_transp = svcxprt;
2317df0ccbaSfvdl 		pl->p_xdrbuf = xdrbuf;
2327df0ccbaSfvdl 		pl->p_recvsz = recvsz;
2337df0ccbaSfvdl 		pl->p_netid = netid;
23463d7b677Scgd 		pl->p_nxt = proglst;
23563d7b677Scgd 		proglst = pl;
2367df0ccbaSfvdl 		done = TRUE;
2377df0ccbaSfvdl 	}
2387df0ccbaSfvdl 	__rpc_endconf(handle);
2397df0ccbaSfvdl 	mutex_unlock(&proglst_lock);
2407df0ccbaSfvdl 
2417df0ccbaSfvdl 	if (done == FALSE) {
242cd136b95Schristos 		warnx("%s: can't find suitable transport for %s",
243cd136b95Schristos 		    __func__, nettype);
2447df0ccbaSfvdl 		return (-1);
2457df0ccbaSfvdl 	}
24663d7b677Scgd 	return (0);
24763d7b677Scgd }
24863d7b677Scgd 
2497df0ccbaSfvdl /*
2507df0ccbaSfvdl  * The universal handler for the services registered using registerrpc.
2517df0ccbaSfvdl  * It handles both the connectionless and the connection oriented cases.
2527df0ccbaSfvdl  */
2537df0ccbaSfvdl 
25463d7b677Scgd static void
universal(struct svc_req * rqstp,SVCXPRT * transp)255c30c4c40Smatt universal(struct svc_req *rqstp, SVCXPRT *transp)
25663d7b677Scgd {
2577df0ccbaSfvdl 	rpcprog_t prog;
2587df0ccbaSfvdl 	rpcvers_t vers;
2597df0ccbaSfvdl 	rpcproc_t proc;
26063d7b677Scgd 	char *outdata;
2617df0ccbaSfvdl 	char *xdrbuf;
2627df0ccbaSfvdl 	struct proglst *pl;
2633fdac2b8Sthorpej #ifdef _REENTRANT
2647df0ccbaSfvdl 	extern mutex_t proglst_lock;
2657df0ccbaSfvdl #endif
266b48252f3Slukem 
2670e8cfd8fSlukem 	_DIAGASSERT(rqstp != NULL);
2680e8cfd8fSlukem 	_DIAGASSERT(transp != NULL);
2690e8cfd8fSlukem 
27063d7b677Scgd 	/*
27163d7b677Scgd 	 * enforce "procnum 0 is echo" convention
27263d7b677Scgd 	 */
27363d7b677Scgd 	if (rqstp->rq_proc == NULLPROC) {
274deb154d2Schristos 		if (svc_sendreply(transp, (xdrproc_t) xdr_void, NULL) ==
275deb154d2Schristos 		    FALSE) {
276cd136b95Schristos 			warnx("%s: svc_sendreply failed", __func__);
2777df0ccbaSfvdl 		}
27863d7b677Scgd 		return;
27963d7b677Scgd 	}
28063d7b677Scgd 	prog = rqstp->rq_prog;
2817df0ccbaSfvdl 	vers = rqstp->rq_vers;
28263d7b677Scgd 	proc = rqstp->rq_proc;
2837df0ccbaSfvdl 	mutex_lock(&proglst_lock);
2847df0ccbaSfvdl 	for (pl = proglst; pl; pl = pl->p_nxt)
2857df0ccbaSfvdl 		if (pl->p_prognum == prog && pl->p_procnum == proc &&
2867df0ccbaSfvdl 			pl->p_versnum == vers &&
2877df0ccbaSfvdl 			(strcmp(pl->p_netid, transp->xp_netid) == 0)) {
28863d7b677Scgd 			/* decode arguments into a CLEAN buffer */
2897df0ccbaSfvdl 			xdrbuf = pl->p_xdrbuf;
2907df0ccbaSfvdl 			/* Zero the arguments: reqd ! */
291036ef4f3Schristos 			(void) memset(xdrbuf, 0, (size_t)pl->p_recvsz);
2927df0ccbaSfvdl 			/*
2937df0ccbaSfvdl 			 * Assuming that sizeof (xdrbuf) would be enough
2947df0ccbaSfvdl 			 * for the arguments; if not then the program
2957df0ccbaSfvdl 			 * may bomb. BEWARE!
2967df0ccbaSfvdl 			 */
2977df0ccbaSfvdl 			if (!svc_getargs(transp, pl->p_inproc, xdrbuf)) {
29863d7b677Scgd 				svcerr_decode(transp);
2997df0ccbaSfvdl 				mutex_unlock(&proglst_lock);
30063d7b677Scgd 				return;
30163d7b677Scgd 			}
3027df0ccbaSfvdl 			outdata = (*(pl->p_progname))(xdrbuf);
3032e2a3a25Scgd 			if (outdata == NULL &&
3047df0ccbaSfvdl 				pl->p_outproc != (xdrproc_t) xdr_void){
30563d7b677Scgd 				/* there was an error */
3067df0ccbaSfvdl 				mutex_unlock(&proglst_lock);
30763d7b677Scgd 				return;
30863d7b677Scgd 			}
3097df0ccbaSfvdl 			if (!svc_sendreply(transp, pl->p_outproc, outdata)) {
310cd136b95Schristos 				warnx("%s: trouble replying to prog %u vers %u",
311cd136b95Schristos 				    __func__, (unsigned)prog, (unsigned)vers);
3127df0ccbaSfvdl 				mutex_unlock(&proglst_lock);
3137df0ccbaSfvdl 				return;
3147df0ccbaSfvdl 			}
3157df0ccbaSfvdl 			/* free the decoded arguments */
3167df0ccbaSfvdl 			(void) svc_freeargs(transp, pl->p_inproc, xdrbuf);
3177df0ccbaSfvdl 			mutex_unlock(&proglst_lock);
3187df0ccbaSfvdl 			return;
3197df0ccbaSfvdl 		}
3207df0ccbaSfvdl 	mutex_unlock(&proglst_lock);
3217df0ccbaSfvdl 	/* This should never happen */
322cd136b95Schristos 	warnx("%s: never registered prog %u vers %u", __func__,
3237df0ccbaSfvdl 	    (unsigned)prog, (unsigned)vers);
3247df0ccbaSfvdl 	return;
32563d7b677Scgd }
326