xref: /openbsd-src/lib/libc/net/getservent.c (revision 91f110e064cd7c194e59e019b83bb7496c1c84d4)
1 /*	$OpenBSD: getservent.c,v 1.12 2007/09/02 15:19:17 deraadt Exp $ */
2 /*
3  * Copyright (c) 1983, 1993
4  *	The Regents of the University of California.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. Neither the name of the University nor the names of its contributors
15  *    may be used to endorse or promote products derived from this software
16  *    without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 
31 #include <sys/types.h>
32 #include <sys/socket.h>
33 
34 #include <errno.h>
35 #include <limits.h>
36 #include <netdb.h>
37 #include <stdio.h>
38 #include <string.h>
39 #include <stdlib.h>
40 
41 void
42 setservent_r(int f, struct servent_data *sd)
43 {
44 	if (sd->fp == NULL)
45 		sd->fp = fopen(_PATH_SERVICES, "r" );
46 	else
47 		rewind(sd->fp);
48 	sd->stayopen |= f;
49 }
50 
51 void
52 endservent_r(struct servent_data *sd)
53 {
54 	if (sd->fp) {
55 		fclose(sd->fp);
56 		sd->fp = NULL;
57 	}
58 	free(sd->aliases);
59 	sd->aliases = NULL;
60 	sd->maxaliases = 0;
61 	free(sd->line);
62 	sd->line = NULL;
63 	sd->stayopen = 0;
64 }
65 
66 int
67 getservent_r(struct servent *se, struct servent_data *sd)
68 {
69 	char *p, *cp, **q, *endp;
70 	size_t len;
71 	long l;
72 	int serrno;
73 
74 	if (sd->fp == NULL && (sd->fp = fopen(_PATH_SERVICES, "r" )) == NULL)
75 		return (-1);
76 again:
77 	if ((p = fgetln(sd->fp, &len)) == NULL)
78 		return (-1);
79 	if (len == 0 || *p == '#' || *p == '\n')
80 		goto again;
81 	if (p[len-1] == '\n')
82 		len--;
83 	if ((cp = memchr(p, '#', len)) != NULL)
84 		len = cp - p;
85 	cp = realloc(sd->line, len + 1);
86 	if (cp == NULL)
87 		return (-1);
88 	sd->line = se->s_name = memcpy(cp, p, len);
89 	cp[len] = '\0';
90 	p = strpbrk(cp, " \t");
91 	if (p == NULL)
92 		goto again;
93 	*p++ = '\0';
94 	while (*p == ' ' || *p == '\t')
95 		p++;
96 	cp = strpbrk(p, ",/");
97 	if (cp == NULL)
98 		goto again;
99 	*cp++ = '\0';
100 	l = strtol(p, &endp, 10);
101 	if (endp == p || *endp != '\0' || l < 0 || l > USHRT_MAX)
102 		goto again;
103 	se->s_port = htons((in_port_t)l);
104 	se->s_proto = cp;
105 	if (sd->aliases == NULL) {
106 		sd->maxaliases = 10;
107 		sd->aliases = calloc(sd->maxaliases, sizeof(char *));
108 		if (sd->aliases == NULL) {
109 			serrno = errno;
110 			endservent_r(sd);
111 			errno = serrno;
112 			return (-1);
113 		}
114 	}
115 	q = se->s_aliases = sd->aliases;
116 	cp = strpbrk(cp, " \t");
117 	if (cp != NULL)
118 		*cp++ = '\0';
119 	while (cp && *cp) {
120 		if (*cp == ' ' || *cp == '\t') {
121 			cp++;
122 			continue;
123 		}
124 		if (q == &se->s_aliases[sd->maxaliases - 1]) {
125 			p = realloc(se->s_aliases,
126 			    2 * sd->maxaliases * sizeof(char *));
127 			if (p == NULL) {
128 				serrno = errno;
129 				endservent_r(sd);
130 				errno = serrno;
131 				return (-1);
132 			}
133 			sd->maxaliases *= 2;
134 			q = (char **)p + (q - se->s_aliases);
135 			se->s_aliases = sd->aliases = (char **)p;
136 		}
137 		*q++ = cp;
138 		cp = strpbrk(cp, " \t");
139 		if (cp != NULL)
140 			*cp++ = '\0';
141 	}
142 	*q = NULL;
143 	return (0);
144 }
145 
146 struct servent_data _servent_data;	/* shared with getservby{name,port}.c */
147 
148 void
149 setservent(int f)
150 {
151 	setservent_r(f, &_servent_data);
152 }
153 
154 void
155 endservent(void)
156 {
157 	endservent_r(&_servent_data);
158 }
159 
160 struct servent *
161 getservent(void)
162 {
163 	static struct servent serv;
164 
165 	if (getservent_r(&serv, &_servent_data) != 0)
166 		return (NULL);
167 	return (&serv);
168 }
169