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