xref: /netbsd-src/lib/libc/rpc/svc_generic.c (revision 85f5e301f5b0e5c7695c7222d16769f8f55f6aeb)
1*85f5e301Schristos /*	$NetBSD: svc_generic.c,v 1.18 2024/01/23 17:24:38 christos Exp $	*/
27df0ccbaSfvdl 
37df0ccbaSfvdl /*
447c0e0c3Stron  * Copyright (c) 2010, Oracle America, Inc.
57df0ccbaSfvdl  *
647c0e0c3Stron  * Redistribution and use in source and binary forms, with or without
747c0e0c3Stron  * modification, are permitted provided that the following conditions are
847c0e0c3Stron  * met:
97df0ccbaSfvdl  *
1047c0e0c3Stron  *     * Redistributions of source code must retain the above copyright
1147c0e0c3Stron  *       notice, this list of conditions and the following disclaimer.
1247c0e0c3Stron  *     * Redistributions in binary form must reproduce the above
1347c0e0c3Stron  *       copyright notice, this list of conditions and the following
1447c0e0c3Stron  *       disclaimer in the documentation and/or other materials
1547c0e0c3Stron  *       provided with the distribution.
1647c0e0c3Stron  *     * Neither the name of the "Oracle America, Inc." nor the names of its
1747c0e0c3Stron  *       contributors may be used to endorse or promote products derived
1847c0e0c3Stron  *       from this software without specific prior written permission.
197df0ccbaSfvdl  *
2047c0e0c3Stron  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2147c0e0c3Stron  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2247c0e0c3Stron  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
2347c0e0c3Stron  *   FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
2447c0e0c3Stron  *   COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
2547c0e0c3Stron  *   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2647c0e0c3Stron  *   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
2747c0e0c3Stron  *   GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2847c0e0c3Stron  *   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
2947c0e0c3Stron  *   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
3047c0e0c3Stron  *   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
3147c0e0c3Stron  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
327df0ccbaSfvdl  */
337df0ccbaSfvdl 
347df0ccbaSfvdl /*
357df0ccbaSfvdl  * Copyright (c) 1986-1991 by Sun Microsystems Inc.
367df0ccbaSfvdl  */
377df0ccbaSfvdl 
387df0ccbaSfvdl /* #ident	"@(#)svc_generic.c	1.19	94/04/24 SMI" */
397df0ccbaSfvdl 
405c945215Sitojun #include <sys/cdefs.h>
415c945215Sitojun #if defined(LIBC_SCCS) && !defined(lint)
427df0ccbaSfvdl #if 0
437df0ccbaSfvdl static char sccsid[] = "@(#)svc_generic.c 1.21 89/02/28 Copyr 1988 Sun Micro";
445c945215Sitojun #else
45*85f5e301Schristos __RCSID("$NetBSD: svc_generic.c,v 1.18 2024/01/23 17:24:38 christos Exp $");
467df0ccbaSfvdl #endif
477df0ccbaSfvdl #endif
487df0ccbaSfvdl 
497df0ccbaSfvdl /*
507df0ccbaSfvdl  * svc_generic.c, Server side for RPC.
517df0ccbaSfvdl  *
527df0ccbaSfvdl  */
537df0ccbaSfvdl 
547df0ccbaSfvdl #include "namespace.h"
557df0ccbaSfvdl #include "reentrant.h"
567df0ccbaSfvdl #include <sys/types.h>
577df0ccbaSfvdl #include <sys/socket.h>
587df0ccbaSfvdl #include <netinet/in.h>
597df0ccbaSfvdl #include <rpc/rpc.h>
60deb154d2Schristos #include <rpc/nettype.h>
617df0ccbaSfvdl #include <stdio.h>
627df0ccbaSfvdl #include <errno.h>
63677bc7fdSdholland #include <stdlib.h>
647df0ccbaSfvdl #include <string.h>
657df0ccbaSfvdl #include <unistd.h>
667df0ccbaSfvdl #include <err.h>
677df0ccbaSfvdl 
682dec884dSchristos #include "svc_fdset.h"
6979d5b270Sfvdl #include "rpc_internal.h"
707df0ccbaSfvdl 
717df0ccbaSfvdl #ifdef __weak_alias
727df0ccbaSfvdl __weak_alias(svc_create,_svc_create)
737df0ccbaSfvdl __weak_alias(svc_tp_create,_svc_tp_create)
747df0ccbaSfvdl __weak_alias(svc_tli_create,_svc_tli_create)
757df0ccbaSfvdl #endif
767df0ccbaSfvdl 
77adb74221Smatt extern int __svc_vc_setflag(SVCXPRT *, int);
787df0ccbaSfvdl 
797df0ccbaSfvdl /*
807df0ccbaSfvdl  * The highest level interface for server creation.
817df0ccbaSfvdl  * It tries for all the nettokens in that particular class of token
827df0ccbaSfvdl  * and returns the number of handles it can create and/or find.
837df0ccbaSfvdl  *
847df0ccbaSfvdl  * It creates a link list of all the handles it could create.
857df0ccbaSfvdl  * If svc_create() is called multiple times, it uses the handle
867df0ccbaSfvdl  * created earlier instead of creating a new handle every time.
877df0ccbaSfvdl  */
887df0ccbaSfvdl int
svc_create(void (* dispatch)(struct svc_req *,SVCXPRT *),rpcprog_t prognum,rpcvers_t versnum,const char * nettype)89adb74221Smatt svc_create(
90adb74221Smatt 	void (*dispatch)(struct svc_req *, SVCXPRT *),
91adb74221Smatt 	rpcprog_t prognum,		/* Program number */
92adb74221Smatt 	rpcvers_t versnum,		/* Version number */
93adb74221Smatt 	const char *nettype)		/* Networktype token */
947df0ccbaSfvdl {
957df0ccbaSfvdl 	struct xlist {
967df0ccbaSfvdl 		SVCXPRT *xprt;		/* Server handle */
977df0ccbaSfvdl 		struct xlist *next;	/* Next item */
987df0ccbaSfvdl 	} *l;
997df0ccbaSfvdl 	static struct xlist *xprtlist;	/* A link list of all the handles */
1007df0ccbaSfvdl 	int num = 0;
1017df0ccbaSfvdl 	SVCXPRT *xprt;
1027df0ccbaSfvdl 	struct netconfig *nconf;
1037df0ccbaSfvdl 	void *handle;
1047df0ccbaSfvdl 
1057df0ccbaSfvdl /* VARIABLES PROTECTED BY xprtlist_lock: xprtlist */
1067df0ccbaSfvdl 
107deb154d2Schristos 	if ((handle = __rpc_setconf(nettype)) == NULL) {
108e24729deSchristos 		warnx("%s: unknown protocol %s", __func__, nettype);
1097df0ccbaSfvdl 		return (0);
1107df0ccbaSfvdl 	}
111deb154d2Schristos 	while ((nconf = __rpc_getconf(handle)) != NULL) {
1127df0ccbaSfvdl 		mutex_lock(&xprtlist_lock);
1137df0ccbaSfvdl 		for (l = xprtlist; l; l = l->next) {
1147df0ccbaSfvdl 			if (strcmp(l->xprt->xp_netid, nconf->nc_netid) == 0) {
1157df0ccbaSfvdl 				/* Found an old one, use it */
1167df0ccbaSfvdl 				(void) rpcb_unset(prognum, versnum, nconf);
1177df0ccbaSfvdl 				if (svc_reg(l->xprt, prognum, versnum,
1187df0ccbaSfvdl 					dispatch, nconf) == FALSE)
119e24729deSchristos 					warnx("%s: could not register prog %u "
120e24729deSchristos 					    "vers %u on %s", __func__,
121e24729deSchristos 					    (unsigned)prognum,
122e24729deSchristos 					    (unsigned)versnum, nconf->nc_netid);
1237df0ccbaSfvdl 				else
1247df0ccbaSfvdl 					num++;
1257df0ccbaSfvdl 				break;
1267df0ccbaSfvdl 			}
1277df0ccbaSfvdl 		}
128deb154d2Schristos 		if (l == NULL) {
1297df0ccbaSfvdl 			/* It was not found. Now create a new one */
1307df0ccbaSfvdl 			xprt = svc_tp_create(dispatch, prognum, versnum, nconf);
1317df0ccbaSfvdl 			if (xprt) {
132c9cdc302Schristos 				l = malloc(sizeof(*l));
133deb154d2Schristos 				if (l == NULL) {
134e24729deSchristos 					warn("%s: out of memory", __func__);
1357df0ccbaSfvdl 					mutex_unlock(&xprtlist_lock);
1367df0ccbaSfvdl 					return (0);
1377df0ccbaSfvdl 				}
1387df0ccbaSfvdl 				l->xprt = xprt;
1397df0ccbaSfvdl 				l->next = xprtlist;
1407df0ccbaSfvdl 				xprtlist = l;
1417df0ccbaSfvdl 				num++;
1427df0ccbaSfvdl 			}
1437df0ccbaSfvdl 		}
1447df0ccbaSfvdl 		mutex_unlock(&xprtlist_lock);
1457df0ccbaSfvdl 	}
1467df0ccbaSfvdl 	__rpc_endconf(handle);
1477df0ccbaSfvdl 	/*
1487df0ccbaSfvdl 	 * In case of num == 0; the error messages are generated by the
1497df0ccbaSfvdl 	 * underlying layers; and hence not needed here.
1507df0ccbaSfvdl 	 */
1517df0ccbaSfvdl 	return (num);
1527df0ccbaSfvdl }
1537df0ccbaSfvdl 
1547df0ccbaSfvdl /*
1557df0ccbaSfvdl  * The high level interface to svc_tli_create().
1567df0ccbaSfvdl  * It tries to create a server for "nconf" and registers the service
1577df0ccbaSfvdl  * with the rpcbind. It calls svc_tli_create();
1587df0ccbaSfvdl  */
1597df0ccbaSfvdl SVCXPRT *
svc_tp_create(void (* dispatch)(struct svc_req *,SVCXPRT *),rpcprog_t prognum,rpcvers_t versnum,const struct netconfig * nconf)160adb74221Smatt svc_tp_create(
161adb74221Smatt 	void (*dispatch)(struct svc_req *, SVCXPRT *),
162adb74221Smatt 	rpcprog_t prognum,		/* Program number */
163adb74221Smatt 	rpcvers_t versnum,		/* Version number */
164adb74221Smatt 	const struct netconfig *nconf) /* Netconfig structure for the network */
1657df0ccbaSfvdl {
1667df0ccbaSfvdl 	SVCXPRT *xprt;
1677df0ccbaSfvdl 
168deb154d2Schristos 	if (nconf == NULL) {
169e24729deSchristos 		warnx("%s: invalid netconfig structure for prog %u vers %u",
170e24729deSchristos 		    __func__, (unsigned)prognum, (unsigned)versnum);
171deb154d2Schristos 		return (NULL);
1727df0ccbaSfvdl 	}
173deb154d2Schristos 	xprt = svc_tli_create(RPC_ANYFD, nconf, NULL, 0, 0);
174deb154d2Schristos 	if (xprt == NULL) {
175deb154d2Schristos 		return (NULL);
1767df0ccbaSfvdl 	}
17703256c6eSchristos 	(void) rpcb_unset(prognum, versnum, __UNCONST(nconf));
1787df0ccbaSfvdl 	if (svc_reg(xprt, prognum, versnum, dispatch, nconf) == FALSE) {
179e24729deSchristos 		warnx("%s: Could not register prog %u vers %u on %s",
180e24729deSchristos 		    __func__, (unsigned)prognum, (unsigned)versnum,
1817df0ccbaSfvdl 		    nconf->nc_netid);
1827df0ccbaSfvdl 		SVC_DESTROY(xprt);
183deb154d2Schristos 		return (NULL);
1847df0ccbaSfvdl 	}
1857df0ccbaSfvdl 	return (xprt);
1867df0ccbaSfvdl }
1877df0ccbaSfvdl 
1887df0ccbaSfvdl /*
1897df0ccbaSfvdl  * If fd is RPC_ANYFD, then it opens a fd for the given transport
1907df0ccbaSfvdl  * provider (nconf cannot be NULL then). If the t_state is T_UNBND and
1917df0ccbaSfvdl  * bindaddr is NON-NULL, it performs a t_bind using the bindaddr. For
1927df0ccbaSfvdl  * NULL bindadr and Connection oriented transports, the value of qlen
1937df0ccbaSfvdl  * is set to 8.
1947df0ccbaSfvdl  *
1957df0ccbaSfvdl  * If sendsz or recvsz are zero, their default values are chosen.
1967df0ccbaSfvdl  */
1977df0ccbaSfvdl SVCXPRT *
svc_tli_create(int fd,const struct netconfig * nconf,const struct t_bind * bindaddr,u_int sendsz,u_int recvsz)198adb74221Smatt svc_tli_create(
199adb74221Smatt 	int fd,				/* Connection end point */
200adb74221Smatt 	const struct netconfig *nconf,	/* Netconfig struct for nettoken */
201adb74221Smatt 	const struct t_bind *bindaddr,	/* Local bind address */
202adb74221Smatt 	u_int sendsz,			/* Max sendsize */
203adb74221Smatt 	u_int recvsz)			/* Max recvsize */
2047df0ccbaSfvdl {
205deb154d2Schristos 	SVCXPRT *xprt = NULL;		/* service handle */
2067df0ccbaSfvdl 	bool_t madefd = FALSE;		/* whether fd opened here  */
2077df0ccbaSfvdl 	struct __rpc_sockinfo si;
2087df0ccbaSfvdl 	struct sockaddr_storage ss;
2097df0ccbaSfvdl 	socklen_t slen;
2107df0ccbaSfvdl 
2117df0ccbaSfvdl 	if (fd == RPC_ANYFD) {
212deb154d2Schristos 		if (nconf == NULL) {
213e24729deSchristos 			warnx("%s: invalid netconfig", __func__);
214deb154d2Schristos 			return (NULL);
2157df0ccbaSfvdl 		}
2167df0ccbaSfvdl 		fd = __rpc_nconf2fd(nconf);
2177df0ccbaSfvdl 		if (fd == -1) {
218e24729deSchristos 			warnx("%s: could not open connection for %s", __func__,
2197df0ccbaSfvdl 			    nconf->nc_netid);
220deb154d2Schristos 			return (NULL);
2217df0ccbaSfvdl 		}
2227df0ccbaSfvdl 		__rpc_nconf2sockinfo(nconf, &si);
2237df0ccbaSfvdl 		madefd = TRUE;
2247df0ccbaSfvdl 	} else {
2257df0ccbaSfvdl 		/*
2267df0ccbaSfvdl 		 * It is an open descriptor. Get the transport info.
2277df0ccbaSfvdl 		 */
2287df0ccbaSfvdl 		if (!__rpc_fd2sockinfo(fd, &si)) {
229e24729deSchristos 			warnx("%s: could not get transport information",
230e24729deSchristos 			    __func__);
231deb154d2Schristos 			return (NULL);
2327df0ccbaSfvdl 		}
2337df0ccbaSfvdl 	}
2347df0ccbaSfvdl 
2357df0ccbaSfvdl 	/*
2367df0ccbaSfvdl 	 * If the fd is unbound, try to bind it.
2377df0ccbaSfvdl 	 */
2387df0ccbaSfvdl 	if (madefd || !__rpc_sockisbound(fd)) {
2397df0ccbaSfvdl 		if (bindaddr == NULL) {
240737db7eeSfvdl 			if (bindresvport(fd, NULL) < 0) {
2417df0ccbaSfvdl 				memset(&ss, 0, sizeof ss);
2427df0ccbaSfvdl 				ss.ss_family = si.si_af;
2437df0ccbaSfvdl 				ss.ss_len = si.si_alen;
2447df0ccbaSfvdl 				if (bind(fd, (struct sockaddr *)(void *)&ss,
245deb154d2Schristos 				    (socklen_t)si.si_alen) < 0) {
246e24729deSchristos 					warn( "%s: could not bind to anonymous "
247e24729deSchristos 					    "port", __func__);
2487df0ccbaSfvdl 					goto freedata;
2497df0ccbaSfvdl 				}
250737db7eeSfvdl 			}
251f75f6638Schristos 			if (si.si_socktype != SOCK_DGRAM &&
252f75f6638Schristos 			    listen(fd, SOMAXCONN) == -1) {
25372143995Schristos 				warnx("%s: could not listen at anonymous port",
25472143995Schristos 				    __func__);
25572143995Schristos 				goto freedata;
25672143995Schristos 			}
2577df0ccbaSfvdl 		} else {
258deb154d2Schristos 			if (bind(fd,
2598f781c36Syamt 			    (struct sockaddr *)bindaddr->addr.buf,
260deb154d2Schristos 			    (socklen_t)si.si_alen) < 0) {
261e24729deSchristos 				warnx("%s: could not bind to requested address",
262e24729deSchristos 				    __func__);
2637df0ccbaSfvdl 				goto freedata;
2647df0ccbaSfvdl 			}
265f75f6638Schristos 			if (si.si_socktype != SOCK_DGRAM &&
266f75f6638Schristos 			    listen(fd, (int)bindaddr->qlen) == -1) {
26772143995Schristos 				warnx("%s: could not listen at requested "
26872143995Schristos 				    "address", __func__);
26972143995Schristos 				goto freedata;
2707df0ccbaSfvdl 			}
27172143995Schristos 		}
2727df0ccbaSfvdl 	}
2737df0ccbaSfvdl 	/*
2747df0ccbaSfvdl 	 * call transport specific function.
2757df0ccbaSfvdl 	 */
2767df0ccbaSfvdl 	switch (si.si_socktype) {
2777df0ccbaSfvdl 	case SOCK_STREAM:
2787df0ccbaSfvdl 		slen = sizeof ss;
279deb154d2Schristos 		if (getpeername(fd, (struct sockaddr *)(void *)&ss, &slen)
2807df0ccbaSfvdl 		    == 0) {
2817df0ccbaSfvdl 			/* accepted socket */
2827df0ccbaSfvdl 			xprt = svc_fd_create(fd, sendsz, recvsz);
2837df0ccbaSfvdl 		} else
2847df0ccbaSfvdl 			xprt = svc_vc_create(fd, sendsz, recvsz);
2857df0ccbaSfvdl 		if (!nconf || !xprt)
2867df0ccbaSfvdl 			break;
2877df0ccbaSfvdl #if 0
2887df0ccbaSfvdl 		/* XXX fvdl */
2897df0ccbaSfvdl 		if (strcmp(nconf->nc_protofmly, "inet") == 0 ||
2907df0ccbaSfvdl 		    strcmp(nconf->nc_protofmly, "inet6") == 0)
2917df0ccbaSfvdl 			(void) __svc_vc_setflag(xprt, TRUE);
2927df0ccbaSfvdl #endif
2937df0ccbaSfvdl 		break;
2947df0ccbaSfvdl 	case SOCK_DGRAM:
2957df0ccbaSfvdl 		xprt = svc_dg_create(fd, sendsz, recvsz);
2967df0ccbaSfvdl 		break;
2977df0ccbaSfvdl 	default:
298e24729deSchristos 		warnx("%s: bad service type %u", __func__, si.si_socktype);
2997df0ccbaSfvdl 		goto freedata;
3007df0ccbaSfvdl 	}
3017df0ccbaSfvdl 
302deb154d2Schristos 	if (xprt == NULL)
3037df0ccbaSfvdl 		/*
3047df0ccbaSfvdl 		 * The error messages here are spitted out by the lower layers:
3057df0ccbaSfvdl 		 * svc_vc_create(), svc_fd_create() and svc_dg_create().
3067df0ccbaSfvdl 		 */
3077df0ccbaSfvdl 		goto freedata;
3087df0ccbaSfvdl 
3097df0ccbaSfvdl 	/* Fill in type of service */
3107df0ccbaSfvdl 	xprt->xp_type = __rpc_socktype2seman(si.si_socktype);
3117df0ccbaSfvdl 
3127df0ccbaSfvdl 	if (nconf) {
3137df0ccbaSfvdl 		xprt->xp_netid = strdup(nconf->nc_netid);
3147df0ccbaSfvdl 		xprt->xp_tp = strdup(nconf->nc_device);
315c9cdc302Schristos 		if (xprt->xp_netid == NULL || xprt->xp_tp == NULL) {
316c9cdc302Schristos 			svc_destroy(xprt);
317c9cdc302Schristos 			return NULL;
318c9cdc302Schristos 		}
3197df0ccbaSfvdl 	}
3207df0ccbaSfvdl 	return (xprt);
3217df0ccbaSfvdl 
3227df0ccbaSfvdl freedata:
3237df0ccbaSfvdl 	if (madefd)
3247df0ccbaSfvdl 		(void) close(fd);
325deb154d2Schristos 	return (NULL);
3267df0ccbaSfvdl }
327