xref: /netbsd-src/lib/libc/net/getservent_r.c (revision 3249d3dc15e1f55b0ed4cc6808e5e4243d55e4f8)
1*3249d3dcSchristos /*	$NetBSD: getservent_r.c,v 1.13 2022/03/12 17:31:39 christos Exp $	*/
28059eed1Schristos 
38059eed1Schristos /*
48059eed1Schristos  * Copyright (c) 1983, 1993
58059eed1Schristos  *	The Regents of the University of California.  All rights reserved.
68059eed1Schristos  *
78059eed1Schristos  * Redistribution and use in source and binary forms, with or without
88059eed1Schristos  * modification, are permitted provided that the following conditions
98059eed1Schristos  * are met:
108059eed1Schristos  * 1. Redistributions of source code must retain the above copyright
118059eed1Schristos  *    notice, this list of conditions and the following disclaimer.
128059eed1Schristos  * 2. Redistributions in binary form must reproduce the above copyright
138059eed1Schristos  *    notice, this list of conditions and the following disclaimer in the
148059eed1Schristos  *    documentation and/or other materials provided with the distribution.
158059eed1Schristos  * 3. Neither the name of the University nor the names of its contributors
168059eed1Schristos  *    may be used to endorse or promote products derived from this software
178059eed1Schristos  *    without specific prior written permission.
188059eed1Schristos  *
198059eed1Schristos  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
208059eed1Schristos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
218059eed1Schristos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
228059eed1Schristos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
238059eed1Schristos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
248059eed1Schristos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
258059eed1Schristos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
268059eed1Schristos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
278059eed1Schristos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
288059eed1Schristos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
298059eed1Schristos  * SUCH DAMAGE.
308059eed1Schristos  */
318059eed1Schristos 
328059eed1Schristos #include <sys/cdefs.h>
338059eed1Schristos #if defined(LIBC_SCCS) && !defined(lint)
348059eed1Schristos #if 0
358059eed1Schristos static char sccsid[] = "@(#)getservent.c	8.1 (Berkeley) 6/4/93";
368059eed1Schristos #else
37*3249d3dcSchristos __RCSID("$NetBSD: getservent_r.c,v 1.13 2022/03/12 17:31:39 christos Exp $");
388059eed1Schristos #endif
398059eed1Schristos #endif /* LIBC_SCCS and not lint */
408059eed1Schristos 
418059eed1Schristos #include "namespace.h"
42b9cf7d31Sjoerg #include <cdbr.h>
438059eed1Schristos #include <errno.h>
44754fca0eSchristos #include <fcntl.h>
45b9cf7d31Sjoerg #include <netdb.h>
46b9cf7d31Sjoerg #include <stdio.h>
47b9cf7d31Sjoerg #include <stdlib.h>
48b9cf7d31Sjoerg #include <string.h>
498059eed1Schristos 
504e3cae58Skleink #include "servent.h"
514e3cae58Skleink 
528059eed1Schristos #ifdef __weak_alias
__weak_alias(endservent_r,_endservent_r)538059eed1Schristos __weak_alias(endservent_r,_endservent_r)
548059eed1Schristos __weak_alias(getservent_r,_getservent_r)
558059eed1Schristos __weak_alias(setservent_r,_setservent_r)
568059eed1Schristos #endif
578059eed1Schristos 
58754fca0eSchristos int
59754fca0eSchristos _servent_open(struct servent_data *sd)
608059eed1Schristos {
61b9cf7d31Sjoerg 	if (sd->flags & (_SV_CDB | _SV_PLAINFILE)) {
62fd58d463Schristos 		sd->flags |= _SV_FIRST;
63b9cf7d31Sjoerg 		return 0;
64754fca0eSchristos 	}
65b9cf7d31Sjoerg 
66b9cf7d31Sjoerg 	free(sd->line);
67b9cf7d31Sjoerg 	sd->line = NULL;
68b9cf7d31Sjoerg 	free(sd->cdb_buf);
69b9cf7d31Sjoerg 	sd->cdb_buf = NULL;
70b9cf7d31Sjoerg 	sd->cdb_buf_len = 0;
71b9cf7d31Sjoerg 	free(sd->aliases);
72b9cf7d31Sjoerg 	sd->aliases = NULL;
73b9cf7d31Sjoerg 	sd->maxaliases = 0;
74b9cf7d31Sjoerg 	sd->flags |= _SV_FIRST;
75b9cf7d31Sjoerg 
76b9cf7d31Sjoerg 	sd->cdb = cdbr_open(_PATH_SERVICES_CDB, CDBR_DEFAULT);
77b9cf7d31Sjoerg 	if (sd->cdb != NULL) {
78b9cf7d31Sjoerg 		sd->flags |= _SV_CDB;
79b9cf7d31Sjoerg 		return 0;
80b9cf7d31Sjoerg 	}
81b9cf7d31Sjoerg 
829292cfb2Schristos 	sd->plainfile = fopen(_PATH_SERVICES, "re");
83b9cf7d31Sjoerg 	if (sd->plainfile != NULL) {
84b9cf7d31Sjoerg 		sd->flags |= _SV_PLAINFILE;
85b9cf7d31Sjoerg 		return 0;
86b9cf7d31Sjoerg 	}
87b9cf7d31Sjoerg 	return -1;
888059eed1Schristos }
898059eed1Schristos 
908059eed1Schristos void
_servent_close(struct servent_data * sd)91754fca0eSchristos _servent_close(struct servent_data *sd)
928059eed1Schristos {
93b9cf7d31Sjoerg 	if (sd->flags & _SV_CDB) {
94b9cf7d31Sjoerg 		cdbr_close(sd->cdb);
95b9cf7d31Sjoerg 		sd->cdb = NULL;
96b9cf7d31Sjoerg 		sd->flags &= ~_SV_CDB;
97b9cf7d31Sjoerg 	}
98b9cf7d31Sjoerg 
99b9cf7d31Sjoerg 	if (sd->flags & _SV_PLAINFILE) {
100b9cf7d31Sjoerg 		(void)fclose(sd->plainfile);
101b9cf7d31Sjoerg 		sd->plainfile = NULL;
102b9cf7d31Sjoerg 		sd->flags &= ~_SV_PLAINFILE;
1038059eed1Schristos 	}
104754fca0eSchristos 	sd->flags &= ~_SV_STAYOPEN;
1058059eed1Schristos }
106754fca0eSchristos 
107754fca0eSchristos 
108754fca0eSchristos int
_servent_getline(struct servent_data * sd)109754fca0eSchristos _servent_getline(struct servent_data *sd)
110754fca0eSchristos {
1115f01dfbdSchristos 
112b9cf7d31Sjoerg 	if (sd->flags & _SV_CDB)
1135f01dfbdSchristos 		return -1;
1145f01dfbdSchristos 
115b9cf7d31Sjoerg 	if ((sd->flags & _SV_PLAINFILE) == 0)
116b9cf7d31Sjoerg 		return -1;
117754fca0eSchristos 
118b9cf7d31Sjoerg 	free(sd->line);
119b9cf7d31Sjoerg 	sd->line = NULL;
120b9cf7d31Sjoerg 
121b9cf7d31Sjoerg 	if (sd->flags & _SV_FIRST) {
122b9cf7d31Sjoerg 		(void)rewind((FILE *)sd->plainfile);
123754fca0eSchristos 		sd->flags &= ~_SV_FIRST;
124b9cf7d31Sjoerg 	}
125b9cf7d31Sjoerg 	sd->line = fparseln(sd->plainfile, NULL, NULL, NULL,
126b9cf7d31Sjoerg 	    FPARSELN_UNESCALL);
127754fca0eSchristos 	return sd->line == NULL ? -1 : 0;
1288059eed1Schristos }
1298059eed1Schristos 
1308059eed1Schristos struct servent *
_servent_parseline(struct servent_data * sd,struct servent * sp)131754fca0eSchristos _servent_parseline(struct servent_data *sd, struct servent *sp)
1328059eed1Schristos {
1338059eed1Schristos 	size_t i = 0;
1348059eed1Schristos 	int oerrno;
135754fca0eSchristos 	char *p, *cp, **q;
1368059eed1Schristos 
137d3516aeeSchristos 	if (sd->line == NULL)
1388059eed1Schristos 		return NULL;
139754fca0eSchristos 
140d3516aeeSchristos 	sp->s_name = p = sd->line;
1418059eed1Schristos 	p = strpbrk(p, " \t");
1428059eed1Schristos 	if (p == NULL)
143754fca0eSchristos 		return NULL;
1448059eed1Schristos 	*p++ = '\0';
1458059eed1Schristos 	while (*p == ' ' || *p == '\t')
1468059eed1Schristos 		p++;
1478059eed1Schristos 	cp = strpbrk(p, ",/");
1488059eed1Schristos 	if (cp == NULL)
149754fca0eSchristos 		return NULL;
1508059eed1Schristos 	*cp++ = '\0';
1518059eed1Schristos 	sp->s_port = htons((u_short)atoi(p));
1528059eed1Schristos 	sp->s_proto = cp;
1538059eed1Schristos 	if (sd->aliases == NULL) {
154e79fe4efSchristos 		sd->maxaliases = 10;
155b9cf7d31Sjoerg 		sd->aliases = calloc(sd->maxaliases, sizeof(*sd->aliases));
1568059eed1Schristos 		if (sd->aliases == NULL) {
1578059eed1Schristos 			oerrno = errno;
1588059eed1Schristos 			endservent_r(sd);
1598059eed1Schristos 			errno = oerrno;
1608059eed1Schristos 			return NULL;
1618059eed1Schristos 		}
1628059eed1Schristos 	}
163b9cf7d31Sjoerg 	sp->s_aliases = sd->aliases;
1648059eed1Schristos 	cp = strpbrk(cp, " \t");
1658059eed1Schristos 	if (cp != NULL)
1668059eed1Schristos 		*cp++ = '\0';
1678059eed1Schristos 	while (cp && *cp) {
1688059eed1Schristos 		if (*cp == ' ' || *cp == '\t') {
1698059eed1Schristos 			cp++;
1708059eed1Schristos 			continue;
1718059eed1Schristos 		}
1728059eed1Schristos 		if (i == sd->maxaliases - 2) {
1738059eed1Schristos 			sd->maxaliases *= 2;
174b9cf7d31Sjoerg 			q = realloc(sd->aliases, sd->maxaliases * sizeof(*q));
1758059eed1Schristos 			if (q == NULL) {
1768059eed1Schristos 				oerrno = errno;
1778059eed1Schristos 				endservent_r(sd);
1788059eed1Schristos 				errno = oerrno;
1798059eed1Schristos 				return NULL;
1808059eed1Schristos 			}
181d3516aeeSchristos 			sp->s_aliases = sd->aliases = q;
1828059eed1Schristos 		}
183b9cf7d31Sjoerg 		sp->s_aliases[i++] = cp;
1848059eed1Schristos 		cp = strpbrk(cp, " \t");
1858059eed1Schristos 		if (cp != NULL)
1868059eed1Schristos 			*cp++ = '\0';
1878059eed1Schristos 	}
188b9cf7d31Sjoerg 	sp->s_aliases[i] = NULL;
1898059eed1Schristos 	return sp;
1908059eed1Schristos }
191754fca0eSchristos 
192754fca0eSchristos void
setservent_r(int f,struct servent_data * sd)193754fca0eSchristos setservent_r(int f, struct servent_data *sd)
194754fca0eSchristos {
195754fca0eSchristos 	(void)_servent_open(sd);
196754fca0eSchristos 	sd->flags |= f ? _SV_STAYOPEN : 0;
197754fca0eSchristos }
198754fca0eSchristos 
199754fca0eSchristos void
endservent_r(struct servent_data * sd)200754fca0eSchristos endservent_r(struct servent_data *sd)
201754fca0eSchristos {
202754fca0eSchristos 	_servent_close(sd);
203754fca0eSchristos 	free(sd->aliases);
204754fca0eSchristos 	sd->aliases = NULL;
205754fca0eSchristos 	sd->maxaliases = 0;
206754fca0eSchristos 	free(sd->line);
207754fca0eSchristos 	sd->line = NULL;
208b9cf7d31Sjoerg 	free(sd->cdb_buf);
209b9cf7d31Sjoerg 	sd->cdb_buf = NULL;
210b9cf7d31Sjoerg 	sd->cdb_buf_len = 0;
211754fca0eSchristos }
212754fca0eSchristos 
213754fca0eSchristos struct servent *
getservent_r(struct servent * sp,struct servent_data * sd)214754fca0eSchristos getservent_r(struct servent *sp, struct servent_data *sd)
215754fca0eSchristos {
216b9cf7d31Sjoerg 
217b9cf7d31Sjoerg 	if ((sd->flags & (_SV_CDB | _SV_PLAINFILE)) == 0 &&
218b9cf7d31Sjoerg 	    _servent_open(sd) == -1)
219754fca0eSchristos 		return NULL;
220754fca0eSchristos 
221b9cf7d31Sjoerg 	if (sd->flags & _SV_CDB) {
222b9cf7d31Sjoerg 		const void *data;
223b9cf7d31Sjoerg 		size_t len;
224b9cf7d31Sjoerg 
225b9cf7d31Sjoerg 		if (sd->flags & _SV_FIRST) {
226b9cf7d31Sjoerg 			sd->cdb_index = 0;
227b9cf7d31Sjoerg 			sd->flags &= ~_SV_FIRST;
228b9cf7d31Sjoerg 		}
229b9cf7d31Sjoerg 
230b9cf7d31Sjoerg 		if (cdbr_get(sd->cdb, sd->cdb_index, &data, &len))
231b9cf7d31Sjoerg 			return NULL;
232b9cf7d31Sjoerg 		++sd->cdb_index;
233b9cf7d31Sjoerg 		return _servent_parsedb(sd, sp, data, len);
234b9cf7d31Sjoerg 	}
235b9cf7d31Sjoerg 	if (sd->flags & _SV_PLAINFILE) {
236754fca0eSchristos 		for (;;) {
237754fca0eSchristos 			if (_servent_getline(sd) == -1)
238754fca0eSchristos 				return NULL;
239754fca0eSchristos 			if (_servent_parseline(sd, sp) == NULL)
240754fca0eSchristos 				continue;
241754fca0eSchristos 			return sp;
242754fca0eSchristos 		}
2438059eed1Schristos 	}
244b9cf7d31Sjoerg 	return NULL;
245b9cf7d31Sjoerg }
246b9cf7d31Sjoerg 
247b9cf7d31Sjoerg struct servent *
_servent_parsedb(struct servent_data * sd,struct servent * sp,const uint8_t * data,size_t len)248b9cf7d31Sjoerg _servent_parsedb(struct servent_data *sd, struct servent *sp,
249b9cf7d31Sjoerg     const uint8_t *data, size_t len)
250b9cf7d31Sjoerg {
251b9cf7d31Sjoerg 	char **q;
252b9cf7d31Sjoerg 	size_t i;
253b9cf7d31Sjoerg 	int oerrno;
254b9cf7d31Sjoerg 
255b9cf7d31Sjoerg 	if ((sd->flags & _SV_STAYOPEN) == 0) {
256b9cf7d31Sjoerg 		if (len > sd->cdb_buf_len) {
257b9cf7d31Sjoerg 			void *tmp = realloc(sd->cdb_buf, len);
258b9cf7d31Sjoerg 			if (tmp == NULL)
259b9cf7d31Sjoerg 				goto fail;
260b9cf7d31Sjoerg 			sd->cdb_buf = tmp;
261b9cf7d31Sjoerg 			sd->cdb_buf_len = len;
262b9cf7d31Sjoerg 		}
263b9cf7d31Sjoerg 		memcpy(sd->cdb_buf, data, len);
264b9cf7d31Sjoerg 		data = sd->cdb_buf;
265b9cf7d31Sjoerg 	}
266b9cf7d31Sjoerg 
267b9cf7d31Sjoerg 	if (len < 2)
268b9cf7d31Sjoerg 		goto fail;
269b9cf7d31Sjoerg 	sp->s_port = htobe16(be16dec(data));
270b9cf7d31Sjoerg 	data += 2;
271b9cf7d31Sjoerg 	len -= 2;
272b9cf7d31Sjoerg 
273b9cf7d31Sjoerg 	if (len == 0 || len < (size_t)data[0] + 2)
274b9cf7d31Sjoerg 		goto fail;
275b9cf7d31Sjoerg 	sp->s_proto = __UNCONST(data + 1);
276b9cf7d31Sjoerg 
277b9cf7d31Sjoerg 	if (sp->s_proto[data[0]] != '\0')
278b9cf7d31Sjoerg 		goto fail;
279b9cf7d31Sjoerg 
280b9cf7d31Sjoerg 	len -= 2 + data[0];
281b9cf7d31Sjoerg 	data += 2 + data[0];
282b9cf7d31Sjoerg 
283b9cf7d31Sjoerg 	if (len == 0)
284b9cf7d31Sjoerg 		goto fail;
285b9cf7d31Sjoerg 	if (len < (size_t)data[0] + 2)
286b9cf7d31Sjoerg 		goto fail;
287b9cf7d31Sjoerg 
288b9cf7d31Sjoerg 	sp->s_name = __UNCONST(data + 1);
289b9cf7d31Sjoerg 	len -= 2 + data[0];
290b9cf7d31Sjoerg 	data += 2 + data[0];
291b9cf7d31Sjoerg 
292b9cf7d31Sjoerg 	if (sd->aliases == NULL) {
293b9cf7d31Sjoerg 		sd->maxaliases = 10;
29482fb0af1Snia 		sd->aliases = NULL;
295*3249d3dcSchristos 		errno = reallocarr(&sd->aliases,
296*3249d3dcSchristos 		    sd->maxaliases, sizeof(*sd->aliases));
297*3249d3dcSchristos 		if (errno)
298b9cf7d31Sjoerg 			goto fail;
299b9cf7d31Sjoerg 	}
300b9cf7d31Sjoerg 	sp->s_aliases = sd->aliases;
301b9cf7d31Sjoerg 	i = 0;
302b9cf7d31Sjoerg 	while (len) {
303b9cf7d31Sjoerg 		if (len < (size_t)data[0] + 2)
304b9cf7d31Sjoerg 			goto fail;
305b9cf7d31Sjoerg 		if (i == sd->maxaliases - 2) {
306b9cf7d31Sjoerg 			sd->maxaliases *= 2;
30782fb0af1Snia 			q = sd->aliases;
308*3249d3dcSchristos 			errno = reallocarr(&q, sd->maxaliases, sizeof(*q));
309*3249d3dcSchristos 			if (errno)
310b9cf7d31Sjoerg 				goto fail;
311b9cf7d31Sjoerg 			sp->s_aliases = sd->aliases = q;
312b9cf7d31Sjoerg 		}
313b9cf7d31Sjoerg 		sp->s_aliases[i++] = __UNCONST(data + 1);
314b9cf7d31Sjoerg 		len -= 2 + data[0];
315b9cf7d31Sjoerg 		data += 2 + data[0];
316b9cf7d31Sjoerg 	}
317b9cf7d31Sjoerg 	sp->s_aliases[i] = NULL;
318b9cf7d31Sjoerg 	return sp;
319b9cf7d31Sjoerg 
320b9cf7d31Sjoerg fail:
321b9cf7d31Sjoerg 	oerrno = errno;
322b9cf7d31Sjoerg 	endservent_r(sd);
323b9cf7d31Sjoerg 	errno = oerrno;
324b9cf7d31Sjoerg 	return NULL;
325b9cf7d31Sjoerg }
326b9cf7d31Sjoerg 
327