xref: /freebsd/lib/libc/net/getservent.c (revision eacee0ff7ec955b32e09515246bd97b6edcd2b0f)
1 /*
2  * Copyright (c) 1983, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #if 0
35 #if defined(LIBC_SCCS) && !defined(lint)
36 static char sccsid[] = "@(#)getservent.c	8.1 (Berkeley) 6/4/93";
37 #endif /* LIBC_SCCS and not lint */
38 #endif 0
39 
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
42 
43 #include <sys/types.h>
44 #include <sys/socket.h>
45 #include <arpa/inet.h>
46 #include <netdb.h>
47 #include <stdio.h>
48 #include <string.h>
49 #include <stdlib.h>
50 #ifdef YP
51 #include <rpc/rpc.h>
52 #include <rpcsvc/yp_prot.h>
53 #include <rpcsvc/ypclnt.h>
54 static int serv_stepping_yp = 0;
55 extern int _yp_check __P(( char ** ));
56 #endif
57 
58 
59 #define	MAXALIASES	35
60 
61 static FILE *servf = NULL;
62 static char line[BUFSIZ+1];
63 static struct servent serv;
64 static char *serv_aliases[MAXALIASES];
65 int _serv_stayopen;
66 
67 #ifdef YP
68 char *___getservbyname_yp = NULL;
69 char *___getservbyproto_yp = NULL;
70 int ___getservbyport_yp = 0;
71 static char *yp_domain = NULL;
72 
73 static int
74 _getservbyport_yp(line)
75 	char *line;
76 {
77 	char *result;
78 	int resultlen;
79 	char buf[YPMAXRECORD + 2];
80 	int rv;
81 
82 	snprintf(buf, sizeof(buf), "%d/%s", ntohs(___getservbyport_yp),
83 						___getservbyproto_yp);
84 
85 	___getservbyport_yp = 0;
86 	___getservbyproto_yp = NULL;
87 
88 	if(!yp_domain) {
89 		if(yp_get_default_domain(&yp_domain))
90 			return (0);
91 	}
92 
93 	/*
94 	 * We have to be a little flexible here. Ideally you're supposed
95 	 * to have both a services.byname and a services.byport map, but
96 	 * some systems have only services.byname. FreeBSD cheats a little
97 	 * by putting the services.byport information in the same map as
98 	 * services.byname so that either case will work. We allow for both
99 	 * possibilities here: if there is no services.byport map, we try
100 	 * services.byname instead.
101 	 */
102 	if ((rv = yp_match(yp_domain, "services.byport", buf, strlen(buf),
103 						&result, &resultlen))) {
104 		if (rv == YPERR_MAP) {
105 			if (yp_match(yp_domain, "services.byname", buf,
106 					strlen(buf), &result, &resultlen))
107 			return(0);
108 		} else
109 			return(0);
110 	}
111 
112 	/* getservent() expects lines terminated with \n -- make it happy */
113 	snprintf(line, BUFSIZ, "%.*s\n", resultlen, result);
114 
115 	free(result);
116 	return(1);
117 }
118 
119 static int
120 _getservbyname_yp(line)
121 	char *line;
122 {
123 	char *result;
124 	int resultlen;
125 	char buf[YPMAXRECORD + 2];
126 
127 	if(!yp_domain) {
128 		if(yp_get_default_domain(&yp_domain))
129 			return (0);
130 	}
131 
132 	snprintf(buf, sizeof(buf), "%s/%s", ___getservbyname_yp,
133 						___getservbyproto_yp);
134 
135 	___getservbyname_yp = 0;
136 	___getservbyproto_yp = NULL;
137 
138 	if (yp_match(yp_domain, "services.byname", buf, strlen(buf),
139 						&result, &resultlen)) {
140 		return(0);
141 	}
142 
143 	/* getservent() expects lines terminated with \n -- make it happy */
144 	snprintf(line, BUFSIZ, "%.*s\n", resultlen, result);
145 
146 	free(result);
147 	return(1);
148 }
149 
150 static int
151 _getservent_yp(line)
152 	char *line;
153 {
154 	static char *key = NULL;
155 	static int keylen;
156 	char *lastkey, *result;
157 	int resultlen;
158 	int rv;
159 
160 	if(!yp_domain) {
161 		if(yp_get_default_domain(&yp_domain))
162 			return (0);
163 	}
164 
165 	if (!serv_stepping_yp) {
166 		if (key)
167 			free(key);
168 		if ((rv = yp_first(yp_domain, "services.byname", &key, &keylen,
169 			     &result, &resultlen))) {
170 			serv_stepping_yp = 0;
171 			return(0);
172 		}
173 		serv_stepping_yp = 1;
174 	} else {
175 		lastkey = key;
176 		rv = yp_next(yp_domain, "services.byname", key, keylen, &key,
177 			     &keylen, &result, &resultlen);
178 		free(lastkey);
179 		if (rv) {
180 			serv_stepping_yp = 0;
181 			return (0);
182 		}
183 	}
184 
185 	/* getservent() expects lines terminated with \n -- make it happy */
186 	snprintf(line, BUFSIZ, "%.*s\n", resultlen, result);
187 
188 	free(result);
189 
190 	return(1);
191 }
192 #endif
193 
194 void
195 setservent(f)
196 	int f;
197 {
198 	if (servf == NULL)
199 		servf = fopen(_PATH_SERVICES, "r" );
200 	else
201 		rewind(servf);
202 	_serv_stayopen |= f;
203 }
204 
205 void
206 endservent()
207 {
208 	if (servf) {
209 		fclose(servf);
210 		servf = NULL;
211 	}
212 	_serv_stayopen = 0;
213 }
214 
215 struct servent *
216 getservent()
217 {
218 	char *p;
219 	register char *cp, **q;
220 
221 #ifdef YP
222 	if (serv_stepping_yp && _getservent_yp(line)) {
223 		p = (char *)&line;
224 		goto unpack;
225 	}
226 tryagain:
227 #endif
228 	if (servf == NULL && (servf = fopen(_PATH_SERVICES, "r" )) == NULL)
229 		return (NULL);
230 again:
231 	if ((p = fgets(line, BUFSIZ, servf)) == NULL)
232 		return (NULL);
233 #ifdef YP
234 	if (*p == '+' && _yp_check(NULL)) {
235 		if (___getservbyname_yp != NULL) {
236 			if (!_getservbyname_yp(line))
237 				goto tryagain;
238 		}
239 		else if (___getservbyport_yp != 0) {
240 			if (!_getservbyport_yp(line))
241 				goto tryagain;
242 		}
243 		else if (!_getservent_yp(line))
244 			goto tryagain;
245 	}
246 unpack:
247 #endif
248 	if (*p == '#')
249 		goto again;
250 	cp = strpbrk(p, "#\n");
251 	if (cp == NULL)
252 		goto again;
253 	*cp = '\0';
254 	serv.s_name = p;
255 	p = strpbrk(p, " \t");
256 	if (p == NULL)
257 		goto again;
258 	*p++ = '\0';
259 	while (*p == ' ' || *p == '\t')
260 		p++;
261 	cp = strpbrk(p, ",/");
262 	if (cp == NULL)
263 		goto again;
264 	*cp++ = '\0';
265 	serv.s_port = htons((u_short)atoi(p));
266 	serv.s_proto = cp;
267 	q = serv.s_aliases = serv_aliases;
268 	cp = strpbrk(cp, " \t");
269 	if (cp != NULL)
270 		*cp++ = '\0';
271 	while (cp && *cp) {
272 		if (*cp == ' ' || *cp == '\t') {
273 			cp++;
274 			continue;
275 		}
276 		if (q < &serv_aliases[MAXALIASES - 1])
277 			*q++ = cp;
278 		cp = strpbrk(cp, " \t");
279 		if (cp != NULL)
280 			*cp++ = '\0';
281 	}
282 	*q = NULL;
283 	return (&serv);
284 }
285