Path: blob/main/crypto/krb5/src/lib/rpc/getrpcent.c
39537 views
/* lib/rpc/getrpcent.c */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*/3334#include <stdio.h>35#include <sys/types.h>36#include <netdb.h>37#include <gssrpc/rpc.h>38#include <string.h>39#include <sys/socket.h>4041SETRPCENT_TYPE setrpcent (int);42ENDRPCENT_TYPE endrpcent (void);4344/*45* Internet version.46*/47struct rpcdata {48FILE *rpcf;49char *current;50int currentlen;51int stayopen;52#define MAXALIASES 3553char *rpc_aliases[MAXALIASES];54struct rpcent rpc;55char line[BUFSIZ+1];56char *domain;57} *rpcdata;58static struct rpcdata *get_rpcdata(void);5960static struct rpcent *interpret(void);61struct hostent *gethostent(void);6263static char RPCDB[] = "/etc/rpc";6465static struct rpcdata *66get_rpcdata(void)67{68struct rpcdata *d = rpcdata;6970if (d == 0) {71d = (struct rpcdata *)calloc(1, sizeof (struct rpcdata));72rpcdata = d;73}74return (d);75}7677struct rpcent *78getrpcbynumber(int number)79{80struct rpcdata *d = get_rpcdata();81struct rpcent *p;82int reason;83char adrstr[16], *val = NULL;84int vallen;8586if (d == 0)87return (0);88setrpcent(0);89while (p = getrpcent()) {90if (p->r_number == number)91break;92}93endrpcent();94return (p);95}9697struct rpcent *98getrpcbyname(const char *name)99{100struct rpcent *rpc;101char **rp;102103setrpcent(0);104while(rpc = getrpcent()) {105if (strcmp(rpc->r_name, name) == 0)106return (rpc);107for (rp = rpc->r_aliases; *rp != NULL; rp++) {108if (strcmp(*rp, name) == 0)109return (rpc);110}111}112endrpcent();113return (NULL);114}115116SETRPCENT_TYPE setrpcent(int f)117{118struct rpcdata *d = _rpcdata();119120if (d == 0)121return;122if (d->rpcf == NULL) {123d->rpcf = fopen(RPCDB, "r");124if (d->rpcf)125set_cloexec_file(d->rpcf);126} else127rewind(d->rpcf);128if (d->current)129free(d->current);130d->current = NULL;131d->stayopen |= f;132}133134ENDRPCENT_TYPE endrpcent(void)135{136struct rpcdata *d = _rpcdata();137138if (d == 0)139return;140if (d->current && !d->stayopen) {141free(d->current);142d->current = NULL;143}144if (d->rpcf && !d->stayopen) {145fclose(d->rpcf);146d->rpcf = NULL;147}148}149150struct rpcent *151getrpcent(void)152{153struct rpcent *hp;154int reason;155char *key = NULL, *val = NULL;156int keylen, vallen;157struct rpcdata *d = _rpcdata();158159if (d == 0)160return(NULL);161if (d->rpcf == NULL) {162if ((d->rpcf = fopen(RPCDB, "r")) == NULL)163return (NULL);164set_cloexec_file(d->rpcf);165}166if (fgets(d->line, BUFSIZ, d->rpcf) == NULL)167return (NULL);168return interpret(d->line, strlen(d->line));169}170171static struct rpcent *172interpret(char *val, int len)173{174struct rpcdata *d = _rpcdata();175char *p;176char *cp, **q;177178if (d == 0)179return;180strncpy(d->line, val, len);181p = d->line;182d->line[len] = '\n';183if (*p == '#')184return (getrpcent());185cp = strchr(p, '#');186if (cp == NULL)187{188cp = strchr(p, '\n');189if (cp == NULL)190return (getrpcent());191}192*cp = '\0';193cp = strchr(p, ' ');194if (cp == NULL)195{196cp = strchr(p, '\t');197if (cp == NULL)198return (getrpcent());199}200*cp++ = '\0';201/* THIS STUFF IS INTERNET SPECIFIC */202d->rpc.r_name = d->line;203while (*cp == ' ' || *cp == '\t')204cp++;205d->rpc.r_number = atoi(cp);206q = d->rpc.r_aliases = d->rpc_aliases;207cp = strchr(p, ' ');208if (cp != NULL)209*cp++ = '\0';210else211{212cp = strchr(p, '\t');213if (cp != NULL)214*cp++ = '\0';215}216while (cp && *cp) {217if (*cp == ' ' || *cp == '\t') {218cp++;219continue;220}221if (q < &(d->rpc_aliases[MAXALIASES - 1]))222*q++ = cp;223cp = strchr(p, ' ');224if (cp != NULL)225*cp++ = '\0';226else227{228cp = strchr(p, '\t');229if (cp != NULL)230*cp++ = '\0';231}232}233*q = NULL;234return (&d->rpc);235}236237238