xref: /netbsd-src/lib/libc/net/getnetnamadr.c (revision d20841bb642898112fe68f0ad3f7b26dddf56f07)
1 /*	$NetBSD: getnetnamadr.c,v 1.28 2004/01/29 01:39:15 lukem Exp $	*/
2 
3 /* Copyright (c) 1993 Carlos Leandro and Rui Salgueiro
4  *	Dep. Matematica Universidade de Coimbra, Portugal, Europe
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  */
10 /*
11  * Copyright (c) 1983, 1993
12  *	The Regents of the University of California.  All rights reserved.
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions
16  * are met:
17  * 1. Redistributions of source code must retain the above copyright
18  *    notice, this list of conditions and the following disclaimer.
19  * 2. Redistributions in binary form must reproduce the above copyright
20  *    notice, this list of conditions and the following disclaimer in the
21  *    documentation and/or other materials provided with the distribution.
22  * 3. 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 #include <sys/cdefs.h>
40 #if defined(LIBC_SCCS) && !defined(lint)
41 #if 0
42 static char sccsid[] = "@(#)getnetbyaddr.c	8.1 (Berkeley) 6/4/93";
43 static char sccsid_[] = "from getnetnamadr.c	1.4 (Coimbra) 93/06/03";
44 static char rcsid[] = "Id: getnetnamadr.c,v 8.8 1997/06/01 20:34:37 vixie Exp ";
45 #else
46 __RCSID("$NetBSD: getnetnamadr.c,v 1.28 2004/01/29 01:39:15 lukem Exp $");
47 #endif
48 #endif /* LIBC_SCCS and not lint */
49 
50 #include "namespace.h"
51 #include <sys/types.h>
52 #include <sys/param.h>
53 #include <sys/socket.h>
54 #include <netinet/in.h>
55 #include <arpa/inet.h>
56 #include <arpa/nameser.h>
57 
58 #include <assert.h>
59 #include <ctype.h>
60 #include <errno.h>
61 #include <netdb.h>
62 #include <nsswitch.h>
63 #include <resolv.h>
64 #include <stdarg.h>
65 #include <stdio.h>
66 #include <stdlib.h>
67 #include <string.h>
68 
69 #ifdef YP
70 #include <rpc/rpc.h>
71 #include <rpcsvc/yp_prot.h>
72 #include <rpcsvc/ypclnt.h>
73 #endif
74 
75 #ifdef __weak_alias
76 __weak_alias(getnetbyaddr,_getnetbyaddr)
77 __weak_alias(getnetbyname,_getnetbyname)
78 #endif
79 
80 extern int _net_stayopen;
81 
82 #define BYADDR 0
83 #define BYNAME 1
84 #define	MAXALIASES	35
85 
86 #define	MAXPACKET	(64*1024)
87 
88 typedef union {
89 	HEADER	hdr;
90 	u_char	buf[MAXPACKET];
91 } querybuf;
92 
93 typedef union {
94 	long	al;
95 	char	ac;
96 } align;
97 
98 #ifdef YP
99 static char *__ypdomain;
100 static char *__ypcurrent;
101 static int   __ypcurrentlen;
102 #endif
103 
104 static	struct netent net_entry;
105 static	char *net_aliases[MAXALIASES];
106 
107 static struct netent *getnetanswer __P((querybuf *, int, int));
108 int	_files_getnetbyaddr __P((void *, void *, va_list));
109 int	_files_getnetbyname __P((void *, void *, va_list));
110 int	_dns_getnetbyaddr __P((void *, void *, va_list));
111 int	_dns_getnetbyname __P((void *, void *, va_list));
112 #ifdef YP
113 int	_yp_getnetbyaddr __P((void *, void *, va_list));
114 int	_yp_getnetbyname __P((void *, void *, va_list));
115 struct netent *_ypnetent __P((char *));
116 #endif
117 
118 static struct netent *
119 getnetanswer(answer, anslen, net_i)
120 	querybuf *answer;
121 	int anslen;
122 	int net_i;
123 {
124 	HEADER *hp;
125 	u_char *cp;
126 	int n;
127 	u_char *eom;
128 	int type, class, ancount, qdcount, haveanswer, i, nchar;
129 	char aux1[MAXDNAME], aux2[MAXDNAME], ans[MAXDNAME];
130 	char *in, *st, *pauxt, *bp, **ap;
131 	char *paux1 = &aux1[0], *paux2 = &aux2[0], *ep;
132 	static	char netbuf[PACKETSZ];
133 
134 	_DIAGASSERT(answer != NULL);
135 
136 	/*
137 	 * find first satisfactory answer
138 	 *
139 	 *      answer --> +------------+  ( MESSAGE )
140 	 *		   |   Header   |
141 	 *		   +------------+
142 	 *		   |  Question  | the question for the name server
143 	 *		   +------------+
144 	 *		   |   Answer   | RRs answering the question
145 	 *		   +------------+
146 	 *		   | Authority  | RRs pointing toward an authority
147 	 *		   | Additional | RRs holding additional information
148 	 *		   +------------+
149 	 */
150 	eom = answer->buf + anslen;
151 	hp = &answer->hdr;
152 	ancount = ntohs(hp->ancount); /* #/records in the answer section */
153 	qdcount = ntohs(hp->qdcount); /* #/entries in the question section */
154 	bp = netbuf;
155 	ep = netbuf + sizeof(netbuf);
156 	cp = answer->buf + HFIXEDSZ;
157 	if (!qdcount) {
158 		if (hp->aa)
159 			h_errno = HOST_NOT_FOUND;
160 		else
161 			h_errno = TRY_AGAIN;
162 		return (NULL);
163 	}
164 	while (qdcount-- > 0) {
165 		n = __dn_skipname(cp, eom);
166 		if (n < 0 || (cp + n + QFIXEDSZ) > eom) {
167 			h_errno = NO_RECOVERY;
168 			return(NULL);
169 		}
170 		cp += n + QFIXEDSZ;
171 	}
172 	ap = net_aliases;
173 	*ap = NULL;
174 	net_entry.n_aliases = net_aliases;
175 	haveanswer = 0;
176 	while (--ancount >= 0 && cp < eom) {
177 		n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
178 		if ((n < 0) || !res_dnok(bp))
179 			break;
180 		cp += n;
181 		ans[0] = '\0';
182 		(void)strlcpy(ans, bp, sizeof(ans));
183 		GETSHORT(type, cp);
184 		GETSHORT(class, cp);
185 		cp += INT32SZ;		/* TTL */
186 		GETSHORT(n, cp);
187 		if (class == C_IN && type == T_PTR) {
188 			n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
189 			if ((n < 0) || !res_hnok(bp)) {
190 				cp += n;
191 				return (NULL);
192 			}
193 			cp += n;
194 			*ap++ = bp;
195 			bp += strlen(bp) + 1;
196 			net_entry.n_addrtype =
197 				(class == C_IN) ? AF_INET : AF_UNSPEC;
198 			haveanswer++;
199 		}
200 	}
201 	if (haveanswer) {
202 		*ap = NULL;
203 		switch (net_i) {
204 		case BYADDR:
205 			net_entry.n_name = *net_entry.n_aliases;
206 			net_entry.n_net = 0L;
207 			break;
208 		case BYNAME:
209 			ap = net_entry.n_aliases;
210 		next_alias:
211 			in = *ap++;
212 			if (in == NULL) {
213 				h_errno = HOST_NOT_FOUND;
214 				return (NULL);
215 			}
216 			net_entry.n_name = ans;
217 			aux2[0] = '\0';
218 			for (i = 0; i < 4; i++) {
219 				for (st = in, nchar = 0;
220 				     isdigit((unsigned char)*st);
221 				     st++, nchar++)
222 					;
223 				if (*st != '.' || nchar == 0 || nchar > 3)
224 					goto next_alias;
225 				if (i != 0)
226 					nchar++;
227 				(void)strlcpy(paux1, in, (size_t)nchar);
228 				paux1[nchar] = '\0';
229 				pauxt = paux2;
230 				paux2 = strcat(paux1, paux2);
231 				paux1 = pauxt;
232 				in = ++st;
233 			}
234 			net_entry.n_net = inet_network(paux2);
235 			break;
236 		}
237 		if (strcasecmp(in, "IN-ADDR.ARPA") != 0)
238 			goto next_alias;
239 		net_entry.n_aliases++;
240 		return (&net_entry);
241 	}
242 	h_errno = TRY_AGAIN;
243 	return (NULL);
244 }
245 
246 /*ARGSUSED*/
247 int
248 _files_getnetbyaddr(rv, cb_data, ap)
249 	void	*rv;
250 	void	*cb_data;
251 	va_list	 ap;
252 {
253 	struct netent *p;
254 	unsigned long net;
255 	int type;
256 
257 	_DIAGASSERT(rv != NULL);
258 
259 	net = va_arg(ap, unsigned long);
260 	type = va_arg(ap, int);
261 
262 	setnetent(_net_stayopen);
263 	while ((p = getnetent()) != NULL)
264 		if (p->n_addrtype == type && p->n_net == net)
265 			break;
266 	if (!_net_stayopen)
267 		endnetent();
268 	*((struct netent **)rv) = p;
269 	if (p==NULL) {
270 		h_errno = HOST_NOT_FOUND;
271 		return NS_NOTFOUND;
272 	}
273 	return NS_SUCCESS;
274 }
275 
276 /*ARGSUSED*/
277 int
278 _dns_getnetbyaddr(rv, cb_data, ap)
279 	void    *rv;
280 	void    *cb_data;
281 	va_list  ap;
282 {
283 	unsigned int netbr[4];
284 	int nn, anslen;
285 	querybuf *buf;
286 	char qbuf[MAXDNAME];
287 	unsigned long net2;
288 	struct netent *np;
289 	unsigned long net;
290 	int type;
291 
292 	_DIAGASSERT(rv != NULL);
293 
294 	net = va_arg(ap, unsigned long);
295 	type = va_arg(ap, int);
296 
297 	if (type != AF_INET)
298 		return NS_UNAVAIL;
299 
300 	for (nn = 4, net2 = net; net2; net2 >>= 8)
301 		netbr[--nn] = (unsigned int)(net2 & 0xff);
302 	switch (nn) {
303 	default:
304 		return NS_UNAVAIL;
305 	case 3: 	/* Class A */
306 		snprintf(qbuf, sizeof(qbuf), "0.0.0.%u.in-addr.arpa", netbr[3]);
307 		break;
308 	case 2: 	/* Class B */
309 		snprintf(qbuf, sizeof(qbuf), "0.0.%u.%u.in-addr.arpa",
310 		    netbr[3], netbr[2]);
311 		break;
312 	case 1: 	/* Class C */
313 		snprintf(qbuf, sizeof(qbuf), "0.%u.%u.%u.in-addr.arpa",
314 		    netbr[3], netbr[2], netbr[1]);
315 		break;
316 	case 0: 	/* Class D - E */
317 		snprintf(qbuf, sizeof(qbuf), "%u.%u.%u.%u.in-addr.arpa",
318 		    netbr[3], netbr[2], netbr[1], netbr[0]);
319 		break;
320 	}
321 	buf = malloc(sizeof(*buf));
322 	if (buf == NULL) {
323 		h_errno = NETDB_INTERNAL;
324 		return NS_NOTFOUND;
325 	}
326 	anslen = res_query(qbuf, C_IN, T_PTR, buf->buf, sizeof(buf->buf));
327 	if (anslen < 0) {
328 		free(buf);
329 #ifdef DEBUG
330 		if (_res.options & RES_DEBUG)
331 			printf("res_query failed\n");
332 #endif
333 		return NS_NOTFOUND;
334 	}
335 	np = getnetanswer(buf, anslen, BYADDR);
336 	free(buf);
337 	if (np) {
338 		/* maybe net should be unsigned? */
339 		unsigned long u_net = net;
340 
341 		/* Strip trailing zeros */
342 		while ((u_net & 0xff) == 0 && u_net != 0)
343 			u_net >>= 8;
344 		np->n_net = u_net;
345 	}
346 	*((struct netent **)rv) = np;
347 	if (np == NULL) {
348 		h_errno = HOST_NOT_FOUND;
349 		return NS_NOTFOUND;
350 	}
351 	return NS_SUCCESS;
352 }
353 
354 
355 struct netent *
356 getnetbyaddr(net, net_type)
357 	u_long net;
358 	int net_type;
359 {
360 	struct netent *np;
361 	static const ns_dtab dtab[] = {
362 		NS_FILES_CB(_files_getnetbyaddr, NULL)
363 		{ NSSRC_DNS, _dns_getnetbyaddr, NULL },	/* force -DHESIOD */
364 		NS_NIS_CB(_yp_getnetbyaddr, NULL)
365 		{ 0 }
366 	};
367 
368 	if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
369 		h_errno = NETDB_INTERNAL;
370 		return (NULL);
371 	}
372 
373 	np = NULL;
374 	h_errno = NETDB_INTERNAL;
375 	if (nsdispatch(&np, dtab, NSDB_NETWORKS, "getnetbyaddr", __nsdefaultsrc,
376 	    net, net_type) != NS_SUCCESS)
377 		return (NULL);
378 	h_errno = NETDB_SUCCESS;
379 	return (np);
380 }
381 
382 /*ARGSUSED*/
383 int
384 _files_getnetbyname(rv, cb_data, ap)
385 	void	*rv;
386 	void	*cb_data;
387 	va_list	 ap;
388 {
389 	struct netent *p;
390 	char **cp;
391 	const char *name;
392 
393 	_DIAGASSERT(rv != NULL);
394 
395 	name = va_arg(ap, const char *);
396 	setnetent(_net_stayopen);
397 	while ((p = getnetent()) != NULL) {
398 		if (strcasecmp(p->n_name, name) == 0)
399 			break;
400 		for (cp = p->n_aliases; *cp != 0; cp++)
401 			if (strcasecmp(*cp, name) == 0)
402 				goto found;
403 	}
404 found:
405 	if (!_net_stayopen)
406 		endnetent();
407 	*((struct netent **)rv) = p;
408 	if (p==NULL) {
409 		h_errno = HOST_NOT_FOUND;
410 		return NS_NOTFOUND;
411 	}
412 	return NS_SUCCESS;
413 }
414 
415 /*ARGSUSED*/
416 int
417 _dns_getnetbyname(rv, cb_data, ap)
418 	void    *rv;
419 	void    *cb_data;
420 	va_list  ap;
421 {
422 	int anslen;
423 	querybuf *buf;
424 	char qbuf[MAXDNAME];
425 	struct netent *np;
426 	const char *net;
427 
428 	_DIAGASSERT(rv != NULL);
429 
430 	net = va_arg(ap, const char *);
431 	strlcpy(&qbuf[0], net, sizeof(qbuf));
432 	buf = malloc(sizeof(*buf));
433 	if (buf == NULL) {
434 		h_errno = NETDB_INTERNAL;
435 		return NS_NOTFOUND;
436 	}
437 	anslen = res_search(qbuf, C_IN, T_PTR, buf->buf, sizeof(buf->buf));
438 	if (anslen < 0) {
439 		free(buf);
440 #ifdef DEBUG
441 		if (_res.options & RES_DEBUG)
442 			printf("res_search failed\n");
443 #endif
444 		return NS_NOTFOUND;
445 	}
446 	np = getnetanswer(buf, anslen, BYNAME);
447 	free(buf);
448 	*((struct netent **)rv) = np;
449 	if (np == NULL) {
450 		h_errno = HOST_NOT_FOUND;
451 		return NS_NOTFOUND;
452 	}
453 	return NS_SUCCESS;
454 }
455 
456 struct netent *
457 getnetbyname(net)
458 	const char *net;
459 {
460 	struct netent *np;
461 	static const ns_dtab dtab[] = {
462 		NS_FILES_CB(_files_getnetbyname, NULL)
463 		{ NSSRC_DNS, _dns_getnetbyname, NULL },	/* force -DHESIOD */
464 		NS_NIS_CB(_yp_getnetbyname, NULL)
465 		{ 0 }
466 	};
467 
468 	_DIAGASSERT(net != NULL);
469 
470 	if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
471 		h_errno = NETDB_INTERNAL;
472 		return (NULL);
473 	}
474 
475 	np = NULL;
476 	h_errno = NETDB_INTERNAL;
477 	if (nsdispatch(&np, dtab, NSDB_NETWORKS, "getnetbyname", __nsdefaultsrc,
478 	    net) != NS_SUCCESS)
479 		return (NULL);
480 	h_errno = NETDB_SUCCESS;
481 	return (np);
482 }
483 
484 #ifdef YP
485 /*ARGSUSED*/
486 int
487 _yp_getnetbyaddr(rv, cb_data, ap)
488 	void    *rv;
489 	void    *cb_data;
490 	va_list  ap;
491 {
492 	struct netent *np;
493 	char qbuf[MAXDNAME];
494 	unsigned int netbr[4];
495 	unsigned long net, net2;
496 	int type, r;
497 
498 	_DIAGASSERT(rv != NULL);
499 
500 	net = va_arg(ap, unsigned long);
501 	type = va_arg(ap, int);
502 
503 	if (type != AF_INET)
504 		return NS_UNAVAIL;
505 
506 	if (!__ypdomain) {
507 		if (_yp_check(&__ypdomain) == 0)
508 			return NS_UNAVAIL;
509 	}
510 	np = NULL;
511 	if (__ypcurrent)
512 		free(__ypcurrent);
513 	__ypcurrent = NULL;
514 	for (r = 4, net2 = net; net2; net2 >>= 8)
515 		netbr[--r] = (unsigned int)(net2 & 0xff);
516 	switch (r) {
517 	default:
518 		return NS_UNAVAIL;
519 	case 3: 	/* Class A */
520 		snprintf(qbuf, sizeof(qbuf), "%u", netbr[3]);
521 		break;
522 	case 2: 	/* Class B */
523 		snprintf(qbuf, sizeof(qbuf), "%u.%u", netbr[2], netbr[3]);
524 		break;
525 	case 1: 	/* Class C */
526 		snprintf(qbuf, sizeof(qbuf), "%u.%u.%u", netbr[1], netbr[2],
527 		    netbr[3]);
528 		break;
529 	case 0: 	/* Class D - E */
530 		snprintf(qbuf, sizeof(qbuf), "%u.%u.%u.%u", netbr[0], netbr[1],
531 		    netbr[2], netbr[3]);
532 		break;
533 	}
534 	r = yp_match(__ypdomain, "networks.byaddr", qbuf, (int)strlen(qbuf),
535 	    &__ypcurrent, &__ypcurrentlen);
536 	if (r == 0)
537 		np = _ypnetent(__ypcurrent);
538 
539 	*((struct netent **)rv) = np;
540 	if (np == NULL) {
541 		h_errno = HOST_NOT_FOUND;
542 		return NS_NOTFOUND;
543 	}
544 	return NS_SUCCESS;
545 
546 }
547 
548 int
549 /*ARGSUSED*/
550 _yp_getnetbyname(rv, cb_data, ap)
551 	void    *rv;
552 	void    *cb_data;
553 	va_list  ap;
554 {
555 	struct netent *np;
556 	const char *name;
557 	int r;
558 
559 	_DIAGASSERT(rv != NULL);
560 
561 	name = va_arg(ap, const char *);
562 
563 	if (!__ypdomain) {
564 		if (_yp_check(&__ypdomain) == 0)
565 			return NS_UNAVAIL;
566 	}
567 	np = NULL;
568 	if (__ypcurrent)
569 		free(__ypcurrent);
570 	__ypcurrent = NULL;
571 	r = yp_match(__ypdomain, "networks.byname", name, (int)strlen(name),
572 	    &__ypcurrent, &__ypcurrentlen);
573 	if (r == 0)
574 		np = _ypnetent(__ypcurrent);
575 
576 	*((struct netent **)rv) = np;
577 	if (np == NULL) {
578 		h_errno = HOST_NOT_FOUND;
579 		return NS_NOTFOUND;
580 	}
581 	return NS_SUCCESS;
582 }
583 
584 struct netent *
585 _ypnetent(line)
586 	char *line;
587 {
588 	char *cp, *p, **q;
589 
590 	_DIAGASSERT(line != NULL);
591 
592 	net_entry.n_name = line;
593 	cp = strpbrk(line, " \t");
594 	if (cp == NULL)
595 		return (NULL);
596 	*cp++ = '\0';
597 	while (*cp == ' ' || *cp == '\t')
598 		cp++;
599 	p = strpbrk(cp, " \t");
600 	if (p != NULL)
601 		*p++ = '\0';
602 	net_entry.n_net = inet_network(cp);
603 	net_entry.n_addrtype = AF_INET;
604 	q = net_entry.n_aliases = net_aliases;
605 	if (p != NULL)  {
606 		cp = p;
607 		while (cp && *cp) {
608 			if (*cp == ' ' || *cp == '\t') {
609 				cp++;
610 				continue;
611 			}
612 			if (q < &net_aliases[MAXALIASES - 1])
613 				*q++ = cp;
614 			cp = strpbrk(cp, " \t");
615 			if (cp != NULL)
616 				*cp++ = '\0';
617 		}
618 	}
619 	*q = NULL;
620 
621 	return (&net_entry);
622 }
623 #endif
624