xref: /titanic_51/usr/src/lib/libresolv2/common/irs/lcl_nw.c (revision 15d9d0b528387242011cdcc6190c9e598cfe3a07)
1 /*
2  * Copyright 1997-2002 Sun Microsystems, Inc.  All rights reserved.
3  * Use is subject to license terms.
4  */
5 
6 /*
7  * Copyright (c) 1989, 1993, 1995
8  *	The Regents of the University of California.  All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by the University of
21  *	California, Berkeley and its contributors.
22  * 4. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  */
38 
39 /*
40  * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
41  *
42  * Permission to use, copy, modify, and distribute this software for any
43  * purpose with or without fee is hereby granted, provided that the above
44  * copyright notice and this permission notice appear in all copies.
45  *
46  * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
47  * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
48  * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
49  * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
50  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
51  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
52  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
53  * SOFTWARE.
54  */
55 
56 #pragma ident	"%Z%%M%	%I%	%E% SMI"
57 
58 #if defined(LIBC_SCCS) && !defined(lint)
59 static const char rcsid[] = "$Id: lcl_nw.c,v 1.22 2001/05/29 05:49:07 marka Exp $";
60 /* from getgrent.c 8.2 (Berkeley) 3/21/94"; */
61 /* from BSDI Id: getgrent.c,v 2.8 1996/05/28 18:15:14 bostic Exp $	*/
62 #endif /* LIBC_SCCS and not lint */
63 
64 /* Imports */
65 
66 #include "port_before.h"
67 
68 #include <sys/types.h>
69 #include <sys/socket.h>
70 
71 #include <netinet/in.h>
72 #include <arpa/inet.h>
73 #include <arpa/nameser.h>
74 
75 #include <errno.h>
76 #include <fcntl.h>
77 #include <resolv.h>
78 #include <stdio.h>
79 #include <stdlib.h>
80 #include <string.h>
81 
82 #include <irs.h>
83 #include <isc/memcluster.h>
84 
85 #include "port_after.h"
86 
87 #include <isc/misc.h>
88 #include "irs_p.h"
89 #include "lcl_p.h"
90 
91 #define MAXALIASES 35
92 #define MAXADDRSIZE 4
93 
94 struct pvt {
95 	FILE *		fp;
96 	char 		line[BUFSIZ+1];
97 	struct nwent 	net;
98 	char *		aliases[MAXALIASES];
99 	char		addr[MAXADDRSIZE];
100 	struct __res_state *  res;
101 	void		(*free_res)(void *);
102 };
103 
104 /* Forward */
105 
106 static void 		nw_close(struct irs_nw *);
107 static struct nwent *	nw_byname(struct irs_nw *, const char *, int);
108 static struct nwent *	nw_byaddr(struct irs_nw *, void *, int, int);
109 static struct nwent *	nw_next(struct irs_nw *);
110 static void		nw_rewind(struct irs_nw *);
111 static void		nw_minimize(struct irs_nw *);
112 static struct __res_state * nw_res_get(struct irs_nw *this);
113 static void		nw_res_set(struct irs_nw *this,
114 				   struct __res_state *res,
115 				   void (*free_res)(void *));
116 
117 static int		init(struct irs_nw *this);
118 
119 /* Portability. */
120 
121 #ifndef SEEK_SET
122 # define SEEK_SET 0
123 #endif
124 
125 /* Public */
126 
127 struct irs_nw *
128 irs_lcl_nw(struct irs_acc *this) {
129 	struct irs_nw *nw;
130 	struct pvt *pvt;
131 
132 	UNUSED(this);
133 
134 	if (!(pvt = memget(sizeof *pvt))) {
135 		errno = ENOMEM;
136 		return (NULL);
137 	}
138 	memset(pvt, 0, sizeof *pvt);
139 	if (!(nw = memget(sizeof *nw))) {
140 		memput(pvt, sizeof *pvt);
141 		errno = ENOMEM;
142 		return (NULL);
143 	}
144 	memset(nw, 0x5e, sizeof *nw);
145 	nw->private = pvt;
146 	nw->close = nw_close;
147 	nw->byname = nw_byname;
148 	nw->byaddr = nw_byaddr;
149 	nw->next = nw_next;
150 	nw->rewind = nw_rewind;
151 	nw->minimize = nw_minimize;
152 	nw->res_get = nw_res_get;
153 	nw->res_set = nw_res_set;
154 	return (nw);
155 }
156 
157 /* Methods */
158 
159 static void
160 nw_close(struct irs_nw *this) {
161 	struct pvt *pvt = (struct pvt *)this->private;
162 
163 	nw_minimize(this);
164 	if (pvt->res && pvt->free_res)
165 		(*pvt->free_res)(pvt->res);
166 	if (pvt->fp)
167 		(void)fclose(pvt->fp);
168 	memput(pvt, sizeof *pvt);
169 	memput(this, sizeof *this);
170 }
171 
172 static struct nwent *
173 nw_byaddr(struct irs_nw *this, void *net, int length, int type) {
174 	struct nwent *p;
175 
176 	if (init(this) == -1)
177 		return(NULL);
178 
179 	nw_rewind(this);
180 	while ((p = nw_next(this)) != NULL)
181 		if (p->n_addrtype == type && p->n_length == length)
182 			if (bitncmp(p->n_addr, net, length) == 0)
183 				break;
184 	return (p);
185 }
186 
187 static struct nwent *
188 nw_byname(struct irs_nw *this, const char *name, int type) {
189 	struct nwent *p;
190 	char **ap;
191 
192 	if (init(this) == -1)
193 		return(NULL);
194 
195 	nw_rewind(this);
196 	while ((p = nw_next(this)) != NULL) {
197 		if (ns_samename(p->n_name, name) == 1 &&
198 		    p->n_addrtype == type)
199 			break;
200 		for (ap = p->n_aliases; *ap; ap++)
201 			if ((ns_samename(*ap, name) == 1) &&
202 			    (p->n_addrtype == type))
203 				goto found;
204 	}
205  found:
206 	return (p);
207 }
208 
209 static void
210 nw_rewind(struct irs_nw *this) {
211 	struct pvt *pvt = (struct pvt *)this->private;
212 
213 	if (pvt->fp) {
214 		if (fseek(pvt->fp, 0L, SEEK_SET) == 0)
215 			return;
216 		(void)fclose(pvt->fp);
217 	}
218 	if (!(pvt->fp = fopen(_PATH_NETWORKS, "r")))
219 		return;
220 	if (fcntl(fileno(pvt->fp), F_SETFD, 1) < 0) {
221 		(void)fclose(pvt->fp);
222 		pvt->fp = NULL;
223 	}
224 }
225 
226 static struct nwent *
227 nw_next(struct irs_nw *this) {
228 	struct pvt *pvt = (struct pvt *)this->private;
229 	struct nwent *ret = NULL;
230 	char *p, *cp, **q;
231 	char *bufp, *ndbuf, *dbuf = NULL;
232 	int c, bufsiz, offset = 0;
233 
234 	if (init(this) == -1)
235 		return(NULL);
236 
237 	if (pvt->fp == NULL)
238 		nw_rewind(this);
239 	if (pvt->fp == NULL) {
240 		RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL);
241 		return (NULL);
242 	}
243 	bufp = pvt->line;
244 	bufsiz = sizeof(pvt->line);
245 
246  again:
247 	p = fgets(bufp + offset, bufsiz - offset, pvt->fp);
248 	if (p == NULL)
249 		goto cleanup;
250 	if (!strchr(p, '\n') && !feof(pvt->fp)) {
251 #define GROWBUF 1024
252 		/* allocate space for longer line */
253 	  	if (dbuf == NULL) {
254 			if ((ndbuf = malloc(bufsiz + GROWBUF)) != NULL)
255 				strcpy(ndbuf, bufp);
256 		} else
257 			ndbuf = realloc(dbuf, bufsiz + GROWBUF);
258 		if (ndbuf) {
259 			dbuf = ndbuf;
260 			bufp = dbuf;
261 			bufsiz += GROWBUF;
262 			offset = strlen(dbuf);
263 		} else {
264 			/* allocation failed; skip this long line */
265 			while ((c = getc(pvt->fp)) != EOF)
266 				if (c == '\n')
267 					break;
268 			if (c != EOF)
269 				ungetc(c, pvt->fp);
270 		}
271 		goto again;
272 	}
273 
274 	p -= offset;
275 	offset = 0;
276 
277 	if (*p == '#')
278 		goto again;
279 
280 	cp = strpbrk(p, "#\n");
281 	if (cp != NULL)
282 		*cp = '\0';
283 	pvt->net.n_name = p;
284 	cp = strpbrk(p, " \t");
285 	if (cp == NULL)
286 		goto again;
287 	*cp++ = '\0';
288 	while (*cp == ' ' || *cp == '\t')
289 		cp++;
290 	p = strpbrk(cp, " \t");
291 	if (p != NULL)
292 		*p++ = '\0';
293 	pvt->net.n_length = inet_net_pton(AF_INET, cp, pvt->addr,
294 					  sizeof pvt->addr);
295 	if (pvt->net.n_length < 0)
296 		goto again;
297 	pvt->net.n_addrtype = AF_INET;
298 	pvt->net.n_addr = pvt->addr;
299 	q = pvt->net.n_aliases = pvt->aliases;
300 	if (p != NULL) {
301 		cp = p;
302 		while (cp && *cp) {
303 			if (*cp == ' ' || *cp == '\t') {
304 				cp++;
305 				continue;
306 			}
307 			if (q < &pvt->aliases[MAXALIASES - 1])
308 				*q++ = cp;
309 			cp = strpbrk(cp, " \t");
310 			if (cp != NULL)
311 				*cp++ = '\0';
312 		}
313 	}
314 	*q = NULL;
315 	ret = &pvt->net;
316 
317  cleanup:
318 	if (dbuf)
319 		free(dbuf);
320 
321 	return (ret);
322 }
323 
324 static void
325 nw_minimize(struct irs_nw *this) {
326 	struct pvt *pvt = (struct pvt *)this->private;
327 
328 	if (pvt->res)
329 		res_nclose(pvt->res);
330 	if (pvt->fp != NULL) {
331 		(void)fclose(pvt->fp);
332 		pvt->fp = NULL;
333 	}
334 }
335 
336 static struct __res_state *
337 nw_res_get(struct irs_nw *this) {
338 	struct pvt *pvt = (struct pvt *)this->private;
339 
340 	if (!pvt->res) {
341 		struct __res_state *res;
342 		res = (struct __res_state *)malloc(sizeof *res);
343 		if (!res) {
344 			errno = ENOMEM;
345 			return (NULL);
346 		}
347 		memset(res, 0, sizeof *res);
348 		nw_res_set(this, res, free);
349 	}
350 
351 	return (pvt->res);
352 }
353 
354 static void
355 nw_res_set(struct irs_nw *this, struct __res_state *res,
356 		void (*free_res)(void *)) {
357 	struct pvt *pvt = (struct pvt *)this->private;
358 
359 	if (pvt->res && pvt->free_res) {
360 		res_nclose(pvt->res);
361 		(*pvt->free_res)(pvt->res);
362 	}
363 
364 	pvt->res = res;
365 	pvt->free_res = free_res;
366 }
367 
368 static int
369 init(struct irs_nw *this) {
370 	struct pvt *pvt = (struct pvt *)this->private;
371 
372 	if (!pvt->res && !nw_res_get(this))
373 		return (-1);
374 	if (((pvt->res->options & RES_INIT) == 0) &&
375 	    res_ninit(pvt->res) == -1)
376 		return (-1);
377 	return (0);
378 }
379