xref: /netbsd-src/external/bsd/tcpdump/dist/missing/getservent.c (revision c41df9f6167ea7cd2f761f0a97783c8267cb8847)
1*d881c474Schristos /*
2*d881c474Schristos  * Copyright (c) 1983, 1993	The Regents of the University of California.
3*d881c474Schristos  * Copyright (c) 1993 Digital Equipment Corporation.
4*d881c474Schristos  * Copyright (c) 2012 G. Vanem <gvanem@yahoo.no>.
5*d881c474Schristos  * Copyright (c) 2017 Ali Abdulkadir <autostart.ini@gmail.com>.
6*d881c474Schristos  * All rights reserved.
7*d881c474Schristos  *
8*d881c474Schristos  * Redistribution and use in source and binary forms, with or without
9*d881c474Schristos  * modification, are permitted provided that the following conditions
10*d881c474Schristos  * are met:
11*d881c474Schristos  * 1. Redistributions of source code must retain the above copyright
12*d881c474Schristos  *    notice, this list of conditions and the following disclaimer.
13*d881c474Schristos  * 2. Redistributions in binary form must reproduce the above copyright
14*d881c474Schristos  *    notice, this list of conditions and the following disclaimer in the
15*d881c474Schristos  *    documentation and/or other materials provided with the distribution.
16*d881c474Schristos  * 3. All advertising materials mentioning features or use of this software
17*d881c474Schristos  *    must display the following acknowledgement:
18*d881c474Schristos  *	This product includes software developed by the University of
19*d881c474Schristos  *	California, Berkeley and its contributors.
20*d881c474Schristos  * 4. Neither the name of the University nor the names of its contributors
21*d881c474Schristos  *    may be used to endorse or promote products derived from this software
22*d881c474Schristos  *    without specific prior written permission.
23*d881c474Schristos  *
24*d881c474Schristos  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25*d881c474Schristos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26*d881c474Schristos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27*d881c474Schristos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28*d881c474Schristos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29*d881c474Schristos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30*d881c474Schristos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31*d881c474Schristos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32*d881c474Schristos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33*d881c474Schristos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34*d881c474Schristos  * SUCH DAMAGE.
35*d881c474Schristos  */
36*d881c474Schristos 
37*d881c474Schristos #include <config.h>
38*d881c474Schristos 
39*d881c474Schristos #include <netdissect-stdinc.h>
40*d881c474Schristos #include <getservent.h>
41*d881c474Schristos 
42*d881c474Schristos static FILE *servf = NULL;
43*d881c474Schristos static char line[BUFSIZ+1];
44*d881c474Schristos static struct servent serv;
45*d881c474Schristos static char *serv_aliases[MAXALIASES];
46*d881c474Schristos int _serv_stayopen;
47*d881c474Schristos const char *etc_path(const char *file);
48*d881c474Schristos 
49*d881c474Schristos /*
50*d881c474Schristos * Check if <file> exists in the current directory and, if so, return it.
51*d881c474Schristos * Else return either "%SYSTEMROOT%\System32\drivers\etc\<file>"
52*d881c474Schristos * or $PREFIX/etc/<file>.
53*d881c474Schristos * "<file>" is aka __PATH_SERVICES (aka "services" on Windows and
54*d881c474Schristos * "/etc/services" on other platforms that would need this).
55*d881c474Schristos */
56*d881c474Schristos const char *etc_path(const char *file)
57*d881c474Schristos {
58*d881c474Schristos     const char *env = getenv(__PATH_SYSROOT);
59*d881c474Schristos     static char path[_MAX_PATH];
60*d881c474Schristos 
61*d881c474Schristos     /* see if "<file>" exists locally or whether __PATH_SYSROOT is valid */
62*d881c474Schristos     if (fopen(file, "r") || !env)
63*d881c474Schristos         return (file);
64*d881c474Schristos     else
65*d881c474Schristos #ifdef _WIN32
66*d881c474Schristos     snprintf(path, sizeof(path), "%s%s%s", env, __PATH_ETC_INET, file);
67*d881c474Schristos #else
68*d881c474Schristos     snprintf(path, sizeof(path), "%s%s", env, file);
69*d881c474Schristos #endif
70*d881c474Schristos     return (path);
71*d881c474Schristos }
72*d881c474Schristos 
73*d881c474Schristos void
74*d881c474Schristos setservent(int f)
75*d881c474Schristos {
76*d881c474Schristos     if (servf == NULL)
77*d881c474Schristos         servf = fopen(etc_path(__PATH_SERVICES), "r");
78*d881c474Schristos     else
79*d881c474Schristos         rewind(servf);
80*d881c474Schristos     _serv_stayopen |= f;
81*d881c474Schristos }
82*d881c474Schristos 
83*d881c474Schristos void
84*d881c474Schristos endservent(void)
85*d881c474Schristos {
86*d881c474Schristos     if (servf) {
87*d881c474Schristos         fclose(servf);
88*d881c474Schristos         servf = NULL;
89*d881c474Schristos     }
90*d881c474Schristos     _serv_stayopen = 0;
91*d881c474Schristos }
92*d881c474Schristos 
93*d881c474Schristos struct servent *
94*d881c474Schristos getservent(void)
95*d881c474Schristos {
96*d881c474Schristos     char *p;
97*d881c474Schristos     char *cp, **q;
98*d881c474Schristos 
99*d881c474Schristos     if (servf == NULL && (servf = fopen(etc_path(__PATH_SERVICES), "r")) == NULL)
100*d881c474Schristos         return (NULL);
101*d881c474Schristos 
102*d881c474Schristos again:
103*d881c474Schristos     if ((p = fgets(line, BUFSIZ, servf)) == NULL)
104*d881c474Schristos         return (NULL);
105*d881c474Schristos     if (*p == '#')
106*d881c474Schristos         goto again;
107*d881c474Schristos     cp = strpbrk(p, "#\n");
108*d881c474Schristos     if (cp == NULL)
109*d881c474Schristos         goto again;
110*d881c474Schristos     *cp = '\0';
111*d881c474Schristos     serv.s_name = p;
112*d881c474Schristos     p = strpbrk(p, " \t");
113*d881c474Schristos     if (p == NULL)
114*d881c474Schristos         goto again;
115*d881c474Schristos     *p++ = '\0';
116*d881c474Schristos     while (*p == ' ' || *p == '\t')
117*d881c474Schristos         p++;
118*d881c474Schristos     cp = strpbrk(p, ",/");
119*d881c474Schristos     if (cp == NULL)
120*d881c474Schristos         goto again;
121*d881c474Schristos     *cp++ = '\0';
122*d881c474Schristos     serv.s_port = htons((u_short)atoi(p));
123*d881c474Schristos     serv.s_proto = cp;
124*d881c474Schristos     q = serv.s_aliases = serv_aliases;
125*d881c474Schristos     cp = strpbrk(cp, " \t");
126*d881c474Schristos     if (cp != NULL)
127*d881c474Schristos         *cp++ = '\0';
128*d881c474Schristos     while (cp && *cp) {
129*d881c474Schristos         if (*cp == ' ' || *cp == '\t') {
130*d881c474Schristos             cp++;
131*d881c474Schristos             continue;
132*d881c474Schristos         }
133*d881c474Schristos         if (q < &serv_aliases[MAXALIASES - 1])
134*d881c474Schristos             *q++ = cp;
135*d881c474Schristos         cp = strpbrk(cp, " \t");
136*d881c474Schristos         if (cp != NULL)
137*d881c474Schristos             *cp++ = '\0';
138*d881c474Schristos     }
139*d881c474Schristos     *q = NULL;
140*d881c474Schristos     return (&serv);
141*d881c474Schristos }
142