1*84d9c625SLionel Sambuc /* $NetBSD: getrpcent.c,v 1.23 2013/03/11 20:19:29 tron Exp $ */
22fe8fb19SBen Gras
32fe8fb19SBen Gras /*
4*84d9c625SLionel Sambuc * Copyright (c) 2010, Oracle America, Inc.
52fe8fb19SBen Gras *
6*84d9c625SLionel Sambuc * Redistribution and use in source and binary forms, with or without
7*84d9c625SLionel Sambuc * modification, are permitted provided that the following conditions are
8*84d9c625SLionel Sambuc * met:
92fe8fb19SBen Gras *
10*84d9c625SLionel Sambuc * * Redistributions of source code must retain the above copyright
11*84d9c625SLionel Sambuc * notice, this list of conditions and the following disclaimer.
12*84d9c625SLionel Sambuc * * Redistributions in binary form must reproduce the above
13*84d9c625SLionel Sambuc * copyright notice, this list of conditions and the following
14*84d9c625SLionel Sambuc * disclaimer in the documentation and/or other materials
15*84d9c625SLionel Sambuc * provided with the distribution.
16*84d9c625SLionel Sambuc * * Neither the name of the "Oracle America, Inc." nor the names of its
17*84d9c625SLionel Sambuc * contributors may be used to endorse or promote products derived
18*84d9c625SLionel Sambuc * from this software without specific prior written permission.
192fe8fb19SBen Gras *
20*84d9c625SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21*84d9c625SLionel Sambuc * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22*84d9c625SLionel Sambuc * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23*84d9c625SLionel Sambuc * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24*84d9c625SLionel Sambuc * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25*84d9c625SLionel Sambuc * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26*84d9c625SLionel Sambuc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27*84d9c625SLionel Sambuc * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28*84d9c625SLionel Sambuc * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29*84d9c625SLionel Sambuc * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30*84d9c625SLionel Sambuc * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31*84d9c625SLionel Sambuc * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
322fe8fb19SBen Gras */
332fe8fb19SBen Gras
342fe8fb19SBen Gras #include <sys/cdefs.h>
352fe8fb19SBen Gras #if defined(LIBC_SCCS) && !defined(lint)
362fe8fb19SBen Gras #if 0
372fe8fb19SBen Gras static char *sccsid = "@(#)getrpcent.c 1.14 91/03/11 Copyr 1984 Sun Micro";
382fe8fb19SBen Gras #else
39*84d9c625SLionel Sambuc __RCSID("$NetBSD: getrpcent.c,v 1.23 2013/03/11 20:19:29 tron Exp $");
402fe8fb19SBen Gras #endif
412fe8fb19SBen Gras #endif
422fe8fb19SBen Gras
432fe8fb19SBen Gras /*
442fe8fb19SBen Gras * Copyright (c) 1984 by Sun Microsystems, Inc.
452fe8fb19SBen Gras */
462fe8fb19SBen Gras
472fe8fb19SBen Gras #include "namespace.h"
482fe8fb19SBen Gras
492fe8fb19SBen Gras #include <sys/types.h>
502fe8fb19SBen Gras
512fe8fb19SBen Gras #include <netinet/in.h>
522fe8fb19SBen Gras #include <arpa/inet.h>
532fe8fb19SBen Gras
542fe8fb19SBen Gras #include <assert.h>
552fe8fb19SBen Gras #include <netdb.h>
562fe8fb19SBen Gras #include <stdio.h>
572fe8fb19SBen Gras #include <stdlib.h>
582fe8fb19SBen Gras #include <string.h>
592fe8fb19SBen Gras
602fe8fb19SBen Gras #include <rpc/rpc.h>
612fe8fb19SBen Gras
622fe8fb19SBen Gras #ifdef __weak_alias
632fe8fb19SBen Gras __weak_alias(endrpcent,_endrpcent)
642fe8fb19SBen Gras __weak_alias(getrpcbyname,_getrpcbyname)
652fe8fb19SBen Gras __weak_alias(getrpcbynumber,_getrpcbynumber)
662fe8fb19SBen Gras __weak_alias(getrpcent,_getrpcent)
672fe8fb19SBen Gras __weak_alias(setrpcent,_setrpcent)
682fe8fb19SBen Gras #endif
692fe8fb19SBen Gras
702fe8fb19SBen Gras /*
712fe8fb19SBen Gras * Internet version.
722fe8fb19SBen Gras */
732fe8fb19SBen Gras static struct rpcdata {
742fe8fb19SBen Gras FILE *rpcf;
752fe8fb19SBen Gras int stayopen;
762fe8fb19SBen Gras #define MAXALIASES 35
772fe8fb19SBen Gras char *rpc_aliases[MAXALIASES];
782fe8fb19SBen Gras struct rpcent rpc;
792fe8fb19SBen Gras char line[BUFSIZ+1];
802fe8fb19SBen Gras } *rpcdata;
812fe8fb19SBen Gras
822fe8fb19SBen Gras static struct rpcent *interpret(char *val, size_t len);
832fe8fb19SBen Gras
842fe8fb19SBen Gras #define RPCDB "/etc/rpc"
852fe8fb19SBen Gras
862fe8fb19SBen Gras static struct rpcdata *_rpcdata(void);
872fe8fb19SBen Gras
882fe8fb19SBen Gras static struct rpcdata *
_rpcdata(void)892fe8fb19SBen Gras _rpcdata(void)
902fe8fb19SBen Gras {
912fe8fb19SBen Gras struct rpcdata *d = rpcdata;
922fe8fb19SBen Gras
932fe8fb19SBen Gras if (d == 0) {
942fe8fb19SBen Gras d = (struct rpcdata *)calloc(1, sizeof (struct rpcdata));
952fe8fb19SBen Gras rpcdata = d;
962fe8fb19SBen Gras }
972fe8fb19SBen Gras return (d);
982fe8fb19SBen Gras }
992fe8fb19SBen Gras
1002fe8fb19SBen Gras struct rpcent *
getrpcbynumber(int number)1012fe8fb19SBen Gras getrpcbynumber(int number)
1022fe8fb19SBen Gras {
1032fe8fb19SBen Gras struct rpcent *rpc;
1042fe8fb19SBen Gras
1052fe8fb19SBen Gras setrpcent(0);
1062fe8fb19SBen Gras while ((rpc = getrpcent()) != NULL) {
1072fe8fb19SBen Gras if (rpc->r_number == number)
1082fe8fb19SBen Gras break;
1092fe8fb19SBen Gras }
1102fe8fb19SBen Gras endrpcent();
1112fe8fb19SBen Gras return (rpc);
1122fe8fb19SBen Gras }
1132fe8fb19SBen Gras
1142fe8fb19SBen Gras struct rpcent *
getrpcbyname(const char * name)1152fe8fb19SBen Gras getrpcbyname(const char *name)
1162fe8fb19SBen Gras {
1172fe8fb19SBen Gras struct rpcent *rpc;
1182fe8fb19SBen Gras char **rp;
1192fe8fb19SBen Gras
1202fe8fb19SBen Gras _DIAGASSERT(name != NULL);
1212fe8fb19SBen Gras
1222fe8fb19SBen Gras setrpcent(0);
1232fe8fb19SBen Gras while ((rpc = getrpcent()) != NULL) {
1242fe8fb19SBen Gras if (strcmp(rpc->r_name, name) == 0)
1252fe8fb19SBen Gras break;
1262fe8fb19SBen Gras for (rp = rpc->r_aliases; *rp != NULL; rp++) {
1272fe8fb19SBen Gras if (strcmp(*rp, name) == 0)
1282fe8fb19SBen Gras goto found;
1292fe8fb19SBen Gras }
1302fe8fb19SBen Gras }
1312fe8fb19SBen Gras found:
1322fe8fb19SBen Gras endrpcent();
1332fe8fb19SBen Gras return (rpc);
1342fe8fb19SBen Gras }
1352fe8fb19SBen Gras
1362fe8fb19SBen Gras void
setrpcent(int f)1372fe8fb19SBen Gras setrpcent(int f)
1382fe8fb19SBen Gras {
1392fe8fb19SBen Gras struct rpcdata *d = _rpcdata();
1402fe8fb19SBen Gras
1412fe8fb19SBen Gras if (d == 0)
1422fe8fb19SBen Gras return;
1432fe8fb19SBen Gras if (d->rpcf == NULL)
144f14fb602SLionel Sambuc d->rpcf = fopen(RPCDB, "re");
1452fe8fb19SBen Gras else
1462fe8fb19SBen Gras rewind(d->rpcf);
1472fe8fb19SBen Gras d->stayopen |= f;
1482fe8fb19SBen Gras }
1492fe8fb19SBen Gras
1502fe8fb19SBen Gras void
endrpcent(void)1512fe8fb19SBen Gras endrpcent(void)
1522fe8fb19SBen Gras {
1532fe8fb19SBen Gras struct rpcdata *d = _rpcdata();
1542fe8fb19SBen Gras
1552fe8fb19SBen Gras if (d == 0)
1562fe8fb19SBen Gras return;
1572fe8fb19SBen Gras if (d->rpcf && !d->stayopen) {
1582fe8fb19SBen Gras fclose(d->rpcf);
1592fe8fb19SBen Gras d->rpcf = NULL;
1602fe8fb19SBen Gras }
1612fe8fb19SBen Gras }
1622fe8fb19SBen Gras
1632fe8fb19SBen Gras struct rpcent *
getrpcent(void)1642fe8fb19SBen Gras getrpcent(void)
1652fe8fb19SBen Gras {
1662fe8fb19SBen Gras struct rpcdata *d = _rpcdata();
1672fe8fb19SBen Gras
1682fe8fb19SBen Gras if (d == 0)
1692fe8fb19SBen Gras return(NULL);
170f14fb602SLionel Sambuc if (d->rpcf == NULL && (d->rpcf = fopen(RPCDB, "re")) == NULL)
1712fe8fb19SBen Gras return (NULL);
1722fe8fb19SBen Gras if (fgets(d->line, BUFSIZ, d->rpcf) == NULL)
1732fe8fb19SBen Gras return (NULL);
1742fe8fb19SBen Gras return (interpret(d->line, strlen(d->line)));
1752fe8fb19SBen Gras }
1762fe8fb19SBen Gras
1772fe8fb19SBen Gras static struct rpcent *
interpret(char * val,size_t len)1782fe8fb19SBen Gras interpret(char *val, size_t len)
1792fe8fb19SBen Gras {
1802fe8fb19SBen Gras struct rpcdata *d = _rpcdata();
1812fe8fb19SBen Gras char *p;
1822fe8fb19SBen Gras char *cp, **q;
1832fe8fb19SBen Gras
1842fe8fb19SBen Gras _DIAGASSERT(val != NULL);
1852fe8fb19SBen Gras
1862fe8fb19SBen Gras if (d == 0)
1872fe8fb19SBen Gras return (0);
1882fe8fb19SBen Gras (void) strncpy(d->line, val, len);
1892fe8fb19SBen Gras p = d->line;
1902fe8fb19SBen Gras d->line[len] = '\n';
1912fe8fb19SBen Gras if (*p == '#')
1922fe8fb19SBen Gras return (getrpcent());
1932fe8fb19SBen Gras cp = strpbrk(p, "#\n");
1942fe8fb19SBen Gras if (cp == NULL)
1952fe8fb19SBen Gras return (getrpcent());
1962fe8fb19SBen Gras *cp = '\0';
1972fe8fb19SBen Gras cp = strpbrk(p, " \t");
1982fe8fb19SBen Gras if (cp == NULL)
1992fe8fb19SBen Gras return (getrpcent());
2002fe8fb19SBen Gras *cp++ = '\0';
2012fe8fb19SBen Gras /* THIS STUFF IS INTERNET SPECIFIC */
2022fe8fb19SBen Gras d->rpc.r_name = d->line;
2032fe8fb19SBen Gras while (*cp == ' ' || *cp == '\t')
2042fe8fb19SBen Gras cp++;
2052fe8fb19SBen Gras d->rpc.r_number = atoi(cp);
2062fe8fb19SBen Gras q = d->rpc.r_aliases = d->rpc_aliases;
2072fe8fb19SBen Gras cp = strpbrk(cp, " \t");
2082fe8fb19SBen Gras if (cp != NULL)
2092fe8fb19SBen Gras *cp++ = '\0';
2102fe8fb19SBen Gras while (cp && *cp) {
2112fe8fb19SBen Gras if (*cp == ' ' || *cp == '\t') {
2122fe8fb19SBen Gras cp++;
2132fe8fb19SBen Gras continue;
2142fe8fb19SBen Gras }
2152fe8fb19SBen Gras if (q < &(d->rpc_aliases[MAXALIASES - 1]))
2162fe8fb19SBen Gras *q++ = cp;
2172fe8fb19SBen Gras cp = strpbrk(cp, " \t");
2182fe8fb19SBen Gras if (cp != NULL)
2192fe8fb19SBen Gras *cp++ = '\0';
2202fe8fb19SBen Gras }
2212fe8fb19SBen Gras *q = NULL;
2222fe8fb19SBen Gras return (&d->rpc);
2232fe8fb19SBen Gras }
224