xref: /freebsd/lib/libc/net/getnetbyht.c (revision 4f52dfbb)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1983, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 /* Portions Copyright (c) 1993 Carlos Leandro and Rui Salgueiro
33  *	Dep. Matematica Universidade de Coimbra, Portugal, Europe
34  *
35  * Permission to use, copy, modify, and distribute this software for any
36  * purpose with or without fee is hereby granted, provided that the above
37  * copyright notice and this permission notice appear in all copies.
38  *
39  * from getnetent.c	1.1 (Coimbra) 93/06/02
40  */
41 
42 #if defined(LIBC_SCCS) && !defined(lint)
43 static char sccsid[] = "@(#)getnetent.c	8.1 (Berkeley) 6/4/93";
44 static char orig_rcsid[] = "From: Id: getnetent.c,v 8.4 1997/06/01 20:34:37 vixie Exp";
45 #endif /* LIBC_SCCS and not lint */
46 #include <sys/cdefs.h>
47 __FBSDID("$FreeBSD$");
48 
49 #include <sys/types.h>
50 #include <sys/socket.h>
51 #include <netinet/in.h>
52 #include <arpa/inet.h>
53 #include <arpa/nameser.h>
54 #include <errno.h>
55 #include <netdb.h>
56 #include <resolv.h>
57 #include <stdio.h>
58 #include <string.h>
59 #include <stdarg.h>
60 #include <nsswitch.h>
61 #include "netdb_private.h"
62 
63 void
64 _setnethtent(int f, struct netent_data *ned)
65 {
66 
67 	if (ned->netf == NULL)
68 		ned->netf = fopen(_PATH_NETWORKS, "re");
69 	else
70 		rewind(ned->netf);
71 	ned->stayopen |= f;
72 }
73 
74 void
75 _endnethtent(struct netent_data *ned)
76 {
77 
78 	if (ned->netf) {
79 		fclose(ned->netf);
80 		ned->netf = NULL;
81 	}
82 	ned->stayopen = 0;
83 }
84 
85 static int
86 getnetent_p(struct netent *ne, struct netent_data *ned)
87 {
88 	char *p, *bp, *ep;
89 	char *cp, **q;
90 	int len;
91 	char line[BUFSIZ + 1];
92 
93 	if (ned->netf == NULL &&
94 	    (ned->netf = fopen(_PATH_NETWORKS, "re")) == NULL)
95 		return (-1);
96 again:
97 	p = fgets(line, sizeof line, ned->netf);
98 	if (p == NULL)
99 		return (-1);
100 	if (*p == '#')
101 		goto again;
102 	cp = strpbrk(p, "#\n");
103 	if (cp != NULL)
104 		*cp = '\0';
105 	bp = ned->netbuf;
106 	ep = ned->netbuf + sizeof ned->netbuf;
107 	ne->n_name = bp;
108 	cp = strpbrk(p, " \t");
109 	if (cp == NULL)
110 		goto again;
111 	*cp++ = '\0';
112 	len = strlen(p) + 1;
113 	if (ep - bp < len) {
114 		RES_SET_H_ERRNO(__res_state(), NO_RECOVERY);
115 		return (-1);
116 	}
117 	strlcpy(bp, p, ep - bp);
118 	bp += len;
119 	while (*cp == ' ' || *cp == '\t')
120 		cp++;
121 	p = strpbrk(cp, " \t");
122 	if (p != NULL)
123 		*p++ = '\0';
124 	ne->n_net = inet_network(cp);
125 	ne->n_addrtype = AF_INET;
126 	q = ne->n_aliases = ned->net_aliases;
127 	if (p != NULL) {
128 		cp = p;
129 		while (cp && *cp) {
130 			if (*cp == ' ' || *cp == '\t') {
131 				cp++;
132 				continue;
133 			}
134 			if (q >= &ned->net_aliases[_MAXALIASES - 1])
135 				break;
136 			p = strpbrk(cp, " \t");
137 			if (p != NULL)
138 				*p++ = '\0';
139 			len = strlen(cp) + 1;
140 			if (ep - bp < len)
141 				break;
142 			strlcpy(bp, cp, ep - bp);
143 			*q++ = bp;
144 			bp += len;
145 			cp = p;
146 		}
147 	}
148 	*q = NULL;
149 	return (0);
150 }
151 
152 int
153 getnetent_r(struct netent *nptr, char *buffer, size_t buflen,
154     struct netent **result, int *h_errnop)
155 {
156 	struct netent_data *ned;
157 	struct netent ne;
158 	res_state statp;
159 
160 	statp = __res_state();
161 	if ((ned = __netent_data_init()) == NULL) {
162 		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
163 		*h_errnop = statp->res_h_errno;
164 		return (-1);
165 	}
166 	if (getnetent_p(&ne, ned) != 0)
167 		return (-1);
168 	if (__copy_netent(&ne, nptr, buffer, buflen) != 0) {
169 		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
170 		*h_errnop = statp->res_h_errno;
171 		return ((errno != 0) ? errno : -1);
172 	}
173 	*result = nptr;
174 	return (0);
175 }
176 
177 struct netent *
178 getnetent(void)
179 {
180 	struct netdata *nd;
181 	struct netent *rval;
182 	int ret_h_errno;
183 
184 	if ((nd = __netdata_init()) == NULL)
185 		return (NULL);
186 	if (getnetent_r(&nd->net, nd->data, sizeof(nd->data), &rval,
187 	    &ret_h_errno) != 0)
188 		return (NULL);
189 	return (rval);
190 }
191 
192 int
193 _ht_getnetbyname(void *rval, void *cb_data, va_list ap)
194 {
195 	const char *name;
196 	char *buffer;
197 	size_t buflen;
198 	int *errnop, *h_errnop;
199 	struct netent *nptr, ne;
200 	struct netent_data *ned;
201 	char **cp;
202 	res_state statp;
203 	int error;
204 
205 	name = va_arg(ap, const char *);
206 	nptr = va_arg(ap, struct netent *);
207 	buffer = va_arg(ap, char *);
208 	buflen = va_arg(ap, size_t);
209 	errnop = va_arg(ap, int *);
210 	h_errnop = va_arg(ap, int *);
211 
212 	statp = __res_state();
213 	if ((ned = __netent_data_init()) == NULL) {
214 		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
215 		*h_errnop = statp->res_h_errno;
216 		return (NS_UNAVAIL);
217 	}
218 
219 	_setnethtent(ned->stayopen, ned);
220 	while ((error = getnetent_p(&ne, ned)) == 0) {
221 		if (strcasecmp(ne.n_name, name) == 0)
222 			break;
223 		for (cp = ne.n_aliases; *cp != 0; cp++)
224 			if (strcasecmp(*cp, name) == 0)
225 				goto found;
226 	}
227 found:
228 	if (!ned->stayopen)
229 		_endnethtent(ned);
230 	if (error != 0) {
231 		*h_errnop = statp->res_h_errno;
232 		return (NS_NOTFOUND);
233 	}
234 	if (__copy_netent(&ne, nptr, buffer, buflen) != 0) {
235 		*errnop = errno;
236 		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
237 		*h_errnop = statp->res_h_errno;
238 		return (NS_RETURN);
239 	}
240 	*((struct netent **)rval) = nptr;
241 	return (NS_SUCCESS);
242 }
243 
244 int
245 _ht_getnetbyaddr(void *rval, void *cb_data, va_list ap)
246 {
247 	uint32_t net;
248 	int type;
249 	char *buffer;
250 	size_t buflen;
251 	int *errnop, *h_errnop;
252 	struct netent *nptr, ne;
253 	struct netent_data *ned;
254 	res_state statp;
255 	int error;
256 
257 	net = va_arg(ap, uint32_t);
258 	type = va_arg(ap, int);
259 	nptr = va_arg(ap, struct netent *);
260 	buffer = va_arg(ap, char *);
261 	buflen = va_arg(ap, size_t);
262 	errnop = va_arg(ap, int *);
263 	h_errnop = va_arg(ap, int *);
264 
265 	statp = __res_state();
266 	if ((ned = __netent_data_init()) == NULL) {
267 		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
268 		*h_errnop = statp->res_h_errno;
269 		return (NS_UNAVAIL);
270 	}
271 
272 	_setnethtent(ned->stayopen, ned);
273 	while ((error = getnetent_p(&ne, ned)) == 0)
274 		if (ne.n_addrtype == type && ne.n_net == net)
275 			break;
276 	if (!ned->stayopen)
277 		_endnethtent(ned);
278 	if (error != 0) {
279 		*h_errnop = statp->res_h_errno;
280 		return (NS_NOTFOUND);
281 	}
282 	if (__copy_netent(&ne, nptr, buffer, buflen) != 0) {
283 		*errnop = errno;
284 		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
285 		*h_errnop = statp->res_h_errno;
286 		return (NS_RETURN);
287 	}
288 	*((struct netent **)rval) = nptr;
289 	return (NS_SUCCESS);
290 }
291