1 /* $NetBSD: svc_generic.c,v 1.18 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 /* 35 * Copyright (c) 1986-1991 by Sun Microsystems Inc. 36 */ 37 38 /* #ident "@(#)svc_generic.c 1.19 94/04/24 SMI" */ 39 40 #include <sys/cdefs.h> 41 #if defined(LIBC_SCCS) && !defined(lint) 42 #if 0 43 static char sccsid[] = "@(#)svc_generic.c 1.21 89/02/28 Copyr 1988 Sun Micro"; 44 #else 45 __RCSID("$NetBSD: svc_generic.c,v 1.18 2024/01/23 17:24:38 christos Exp $"); 46 #endif 47 #endif 48 49 /* 50 * svc_generic.c, Server side for RPC. 51 * 52 */ 53 54 #include "namespace.h" 55 #include "reentrant.h" 56 #include <sys/types.h> 57 #include <sys/socket.h> 58 #include <netinet/in.h> 59 #include <rpc/rpc.h> 60 #include <rpc/nettype.h> 61 #include <stdio.h> 62 #include <errno.h> 63 #include <stdlib.h> 64 #include <string.h> 65 #include <unistd.h> 66 #include <err.h> 67 68 #include "svc_fdset.h" 69 #include "rpc_internal.h" 70 71 #ifdef __weak_alias 72 __weak_alias(svc_create,_svc_create) 73 __weak_alias(svc_tp_create,_svc_tp_create) 74 __weak_alias(svc_tli_create,_svc_tli_create) 75 #endif 76 77 extern int __svc_vc_setflag(SVCXPRT *, int); 78 79 /* 80 * The highest level interface for server creation. 81 * It tries for all the nettokens in that particular class of token 82 * and returns the number of handles it can create and/or find. 83 * 84 * It creates a link list of all the handles it could create. 85 * If svc_create() is called multiple times, it uses the handle 86 * created earlier instead of creating a new handle every time. 87 */ 88 int 89 svc_create( 90 void (*dispatch)(struct svc_req *, SVCXPRT *), 91 rpcprog_t prognum, /* Program number */ 92 rpcvers_t versnum, /* Version number */ 93 const char *nettype) /* Networktype token */ 94 { 95 struct xlist { 96 SVCXPRT *xprt; /* Server handle */ 97 struct xlist *next; /* Next item */ 98 } *l; 99 static struct xlist *xprtlist; /* A link list of all the handles */ 100 int num = 0; 101 SVCXPRT *xprt; 102 struct netconfig *nconf; 103 void *handle; 104 105 /* VARIABLES PROTECTED BY xprtlist_lock: xprtlist */ 106 107 if ((handle = __rpc_setconf(nettype)) == NULL) { 108 warnx("%s: unknown protocol %s", __func__, nettype); 109 return (0); 110 } 111 while ((nconf = __rpc_getconf(handle)) != NULL) { 112 mutex_lock(&xprtlist_lock); 113 for (l = xprtlist; l; l = l->next) { 114 if (strcmp(l->xprt->xp_netid, nconf->nc_netid) == 0) { 115 /* Found an old one, use it */ 116 (void) rpcb_unset(prognum, versnum, nconf); 117 if (svc_reg(l->xprt, prognum, versnum, 118 dispatch, nconf) == FALSE) 119 warnx("%s: could not register prog %u " 120 "vers %u on %s", __func__, 121 (unsigned)prognum, 122 (unsigned)versnum, nconf->nc_netid); 123 else 124 num++; 125 break; 126 } 127 } 128 if (l == NULL) { 129 /* It was not found. Now create a new one */ 130 xprt = svc_tp_create(dispatch, prognum, versnum, nconf); 131 if (xprt) { 132 l = malloc(sizeof(*l)); 133 if (l == NULL) { 134 warn("%s: out of memory", __func__); 135 mutex_unlock(&xprtlist_lock); 136 return (0); 137 } 138 l->xprt = xprt; 139 l->next = xprtlist; 140 xprtlist = l; 141 num++; 142 } 143 } 144 mutex_unlock(&xprtlist_lock); 145 } 146 __rpc_endconf(handle); 147 /* 148 * In case of num == 0; the error messages are generated by the 149 * underlying layers; and hence not needed here. 150 */ 151 return (num); 152 } 153 154 /* 155 * The high level interface to svc_tli_create(). 156 * It tries to create a server for "nconf" and registers the service 157 * with the rpcbind. It calls svc_tli_create(); 158 */ 159 SVCXPRT * 160 svc_tp_create( 161 void (*dispatch)(struct svc_req *, SVCXPRT *), 162 rpcprog_t prognum, /* Program number */ 163 rpcvers_t versnum, /* Version number */ 164 const struct netconfig *nconf) /* Netconfig structure for the network */ 165 { 166 SVCXPRT *xprt; 167 168 if (nconf == NULL) { 169 warnx("%s: invalid netconfig structure for prog %u vers %u", 170 __func__, (unsigned)prognum, (unsigned)versnum); 171 return (NULL); 172 } 173 xprt = svc_tli_create(RPC_ANYFD, nconf, NULL, 0, 0); 174 if (xprt == NULL) { 175 return (NULL); 176 } 177 (void) rpcb_unset(prognum, versnum, __UNCONST(nconf)); 178 if (svc_reg(xprt, prognum, versnum, dispatch, nconf) == FALSE) { 179 warnx("%s: Could not register prog %u vers %u on %s", 180 __func__, (unsigned)prognum, (unsigned)versnum, 181 nconf->nc_netid); 182 SVC_DESTROY(xprt); 183 return (NULL); 184 } 185 return (xprt); 186 } 187 188 /* 189 * If fd is RPC_ANYFD, then it opens a fd for the given transport 190 * provider (nconf cannot be NULL then). If the t_state is T_UNBND and 191 * bindaddr is NON-NULL, it performs a t_bind using the bindaddr. For 192 * NULL bindadr and Connection oriented transports, the value of qlen 193 * is set to 8. 194 * 195 * If sendsz or recvsz are zero, their default values are chosen. 196 */ 197 SVCXPRT * 198 svc_tli_create( 199 int fd, /* Connection end point */ 200 const struct netconfig *nconf, /* Netconfig struct for nettoken */ 201 const struct t_bind *bindaddr, /* Local bind address */ 202 u_int sendsz, /* Max sendsize */ 203 u_int recvsz) /* Max recvsize */ 204 { 205 SVCXPRT *xprt = NULL; /* service handle */ 206 bool_t madefd = FALSE; /* whether fd opened here */ 207 struct __rpc_sockinfo si; 208 struct sockaddr_storage ss; 209 socklen_t slen; 210 211 if (fd == RPC_ANYFD) { 212 if (nconf == NULL) { 213 warnx("%s: invalid netconfig", __func__); 214 return (NULL); 215 } 216 fd = __rpc_nconf2fd(nconf); 217 if (fd == -1) { 218 warnx("%s: could not open connection for %s", __func__, 219 nconf->nc_netid); 220 return (NULL); 221 } 222 __rpc_nconf2sockinfo(nconf, &si); 223 madefd = TRUE; 224 } else { 225 /* 226 * It is an open descriptor. Get the transport info. 227 */ 228 if (!__rpc_fd2sockinfo(fd, &si)) { 229 warnx("%s: could not get transport information", 230 __func__); 231 return (NULL); 232 } 233 } 234 235 /* 236 * If the fd is unbound, try to bind it. 237 */ 238 if (madefd || !__rpc_sockisbound(fd)) { 239 if (bindaddr == NULL) { 240 if (bindresvport(fd, NULL) < 0) { 241 memset(&ss, 0, sizeof ss); 242 ss.ss_family = si.si_af; 243 ss.ss_len = si.si_alen; 244 if (bind(fd, (struct sockaddr *)(void *)&ss, 245 (socklen_t)si.si_alen) < 0) { 246 warn( "%s: could not bind to anonymous " 247 "port", __func__); 248 goto freedata; 249 } 250 } 251 if (si.si_socktype != SOCK_DGRAM && 252 listen(fd, SOMAXCONN) == -1) { 253 warnx("%s: could not listen at anonymous port", 254 __func__); 255 goto freedata; 256 } 257 } else { 258 if (bind(fd, 259 (struct sockaddr *)bindaddr->addr.buf, 260 (socklen_t)si.si_alen) < 0) { 261 warnx("%s: could not bind to requested address", 262 __func__); 263 goto freedata; 264 } 265 if (si.si_socktype != SOCK_DGRAM && 266 listen(fd, (int)bindaddr->qlen) == -1) { 267 warnx("%s: could not listen at requested " 268 "address", __func__); 269 goto freedata; 270 } 271 } 272 } 273 /* 274 * call transport specific function. 275 */ 276 switch (si.si_socktype) { 277 case SOCK_STREAM: 278 slen = sizeof ss; 279 if (getpeername(fd, (struct sockaddr *)(void *)&ss, &slen) 280 == 0) { 281 /* accepted socket */ 282 xprt = svc_fd_create(fd, sendsz, recvsz); 283 } else 284 xprt = svc_vc_create(fd, sendsz, recvsz); 285 if (!nconf || !xprt) 286 break; 287 #if 0 288 /* XXX fvdl */ 289 if (strcmp(nconf->nc_protofmly, "inet") == 0 || 290 strcmp(nconf->nc_protofmly, "inet6") == 0) 291 (void) __svc_vc_setflag(xprt, TRUE); 292 #endif 293 break; 294 case SOCK_DGRAM: 295 xprt = svc_dg_create(fd, sendsz, recvsz); 296 break; 297 default: 298 warnx("%s: bad service type %u", __func__, si.si_socktype); 299 goto freedata; 300 } 301 302 if (xprt == NULL) 303 /* 304 * The error messages here are spitted out by the lower layers: 305 * svc_vc_create(), svc_fd_create() and svc_dg_create(). 306 */ 307 goto freedata; 308 309 /* Fill in type of service */ 310 xprt->xp_type = __rpc_socktype2seman(si.si_socktype); 311 312 if (nconf) { 313 xprt->xp_netid = strdup(nconf->nc_netid); 314 xprt->xp_tp = strdup(nconf->nc_device); 315 if (xprt->xp_netid == NULL || xprt->xp_tp == NULL) { 316 svc_destroy(xprt); 317 return NULL; 318 } 319 } 320 return (xprt); 321 322 freedata: 323 if (madefd) 324 (void) close(fd); 325 return (NULL); 326 } 327