Path: blob/main/crypto/krb5/src/lib/rpc/svc_simple.c
39536 views
/* @(#)svc_simple.c 2.2 88/08/01 4.0 RPCSRC */1/*2* Copyright (c) 2010, Oracle America, Inc.3*4* All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions are met:8*9* * Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11*12* * Redistributions in binary form must reproduce the above copyright13* notice, this list of conditions and the following disclaimer in14* the documentation and/or other materials provided with the15* distribution.16*17* * Neither the name of the "Oracle America, Inc." nor the names of18* its contributors may be used to endorse or promote products19* derived from this software without specific prior written permission.20*21* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS22* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED23* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A24* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT25* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,26* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED27* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR28* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF29* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING30* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS31* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.32*/33#if !defined(lint) && defined(SCCSIDS)34static char sccsid[] = "@(#)svc_simple.c 1.18 87/08/11 Copyr 1984 Sun Micro";35#endif3637/*38* svc_simple.c39* Simplified front end to rpc.40*/4142#include <stdio.h>43#include <string.h>44#include <gssrpc/rpc.h>45#include <gssrpc/pmap_clnt.h>46#include <sys/socket.h>47#include <netdb.h>4849static struct proglst {50char *(*p_progname)(void *);51int p_prognum;52int p_procnum;53xdrproc_t p_inproc, p_outproc;54struct proglst *p_nxt;55} *proglst;56static void universal(struct svc_req *, SVCXPRT *);57static SVCXPRT *transp;5859int60registerrpc(61rpcprog_t prognum,62rpcvers_t versnum,63rpcproc_t procnum,64char *(*progname)(void *),65xdrproc_t inproc,66xdrproc_t outproc)67{68struct proglst *pl;6970if (procnum == NULLPROC) {71(void) fprintf(stderr,72"can't reassign procedure number %d\n", NULLPROC);73return (-1);74}75if (transp == 0) {76transp = svcudp_create(RPC_ANYSOCK);77if (transp == NULL) {78(void) fprintf(stderr, "couldn't create an rpc server\n");79return (-1);80}81}82(void) pmap_unset(prognum, versnum);83if (!svc_register(transp, prognum, versnum, universal, IPPROTO_UDP)) {84(void) fprintf(stderr, "couldn't register prog %d vers %d\n",85prognum, versnum);86return (-1);87}88pl = (struct proglst *)malloc(sizeof(struct proglst));89if (pl == NULL) {90(void) fprintf(stderr, "registerrpc: out of memory\n");91return (-1);92}93pl->p_progname = progname;94pl->p_prognum = prognum;95pl->p_procnum = procnum;96pl->p_inproc = inproc;97pl->p_outproc = outproc;98pl->p_nxt = proglst;99proglst = pl;100return (0);101}102103static void104universal(105struct svc_req *rqstp,106SVCXPRT *s_transp)107{108int prog, proc;109char *outdata;110char xdrbuf[UDPMSGSIZE];111struct proglst *pl;112113/*114* enforce "procnum 0 is echo" convention115*/116if (rqstp->rq_proc == NULLPROC) {117if (svc_sendreply(s_transp, xdr_void, (char *)NULL) == FALSE) {118(void) fprintf(stderr, "xxx\n");119exit(1);120}121return;122}123prog = rqstp->rq_prog;124proc = rqstp->rq_proc;125for (pl = proglst; pl != NULL; pl = pl->p_nxt)126if (pl->p_prognum == prog && pl->p_procnum == proc) {127/* decode arguments into a CLEAN buffer */128memset(xdrbuf, 0, sizeof(xdrbuf)); /* required ! */129if (!svc_getargs(s_transp, pl->p_inproc, xdrbuf)) {130svcerr_decode(s_transp);131return;132}133outdata = (*(pl->p_progname))(xdrbuf);134if (outdata == NULL && pl->p_outproc != xdr_void)135/* there was an error */136return;137if (!svc_sendreply(s_transp, pl->p_outproc, outdata)) {138(void) fprintf(stderr,139"trouble replying to prog %d\n",140pl->p_prognum);141exit(1);142}143/* free the decoded arguments */144(void)svc_freeargs(s_transp, pl->p_inproc, xdrbuf);145return;146}147(void) fprintf(stderr, "never registered prog %d\n", prog);148exit(1);149}150151152