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