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