1 /*-
2  * Copyright (c) 1985, 1988, 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. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  * -
33  * Portions Copyright (c) 1993 by Digital Equipment Corporation.
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, and that
38  * the name of Digital Equipment Corporation not be used in advertising or
39  * publicity pertaining to distribution of the document or software without
40  * specific, written prior permission.
41  *
42  * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
43  * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
44  * OF MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT
45  * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
46  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
47  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
48  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
49  * SOFTWARE.
50  * -
51  * --Copyright--
52  */
53 /* Portions Copyright (c) 1993 Carlos Leandro and Rui Salgueiro
54  *	Dep. Matematica Universidade de Coimbra, Portugal, Europe
55  *
56  * Permission to use, copy, modify, and distribute this software for any
57  * purpose with or without fee is hereby granted, provided that the above
58  * copyright notice and this permission notice appear in all copies.
59  */
60 
61 #if defined(LIBC_SCCS) && !defined(lint)
62 static char sccsid[] = "@(#)gethostnamadr.c	8.1 (Berkeley) 6/4/93";
63 #endif /* LIBC_SCCS and not lint */
64 #include <sys/cdefs.h>
65 #include <sys/types.h>
66 #include <sys/types.h>
67 
68 #include <sys/param.h>
69 #include <sys/socket.h>
70 #include <netinet/in.h>
71 #include <arpa/inet.h>
72 #include <arpa/nameser.h>
73 
74 #include <stdio.h>
75 #include <netdb.h>
76 #include <resolv.h>
77 #include <ctype.h>
78 #include <string.h>
79 #include <unistd.h>
80 #include <syslog.h>
81 #include <stdarg.h>
82 #include <nsswitch.h>
83 
84 #include "res_config.h"
85 
86 extern int h_errno;
87 
88 #define BYADDR 0
89 #define BYNAME 1
90 #define	MAXALIASES	35
91 
92 #if PACKETSZ > 1024
93 #define	MAXPACKET	PACKETSZ
94 #else
95 #define	MAXPACKET	1024
96 #endif
97 
98 typedef union {
99 	HEADER	hdr;
100 	u_char	buf[MAXPACKET];
101 } querybuf;
102 
103 typedef union {
104 	long	al;
105 	char	ac;
106 } align;
107 
108 static struct netent *
getnetanswer(answer,anslen,net_i)109 getnetanswer(answer, anslen, net_i)
110 	querybuf *answer;
111 	int anslen;
112 	int net_i;
113 {
114 
115 	HEADER *hp;
116 	u_char *cp;
117 	int n;
118 	u_char *eom;
119 	int type, class, buflen, ancount, qdcount, haveanswer, i, nchar;
120 	char aux1[MAXHOSTNAMELEN], aux2[MAXHOSTNAMELEN], ans[MAXHOSTNAMELEN];
121 	char *in, *st, *pauxt, *bp, **ap;
122 	char *paux1 = &aux1[0], *paux2 = &aux2[0], flag = 0;
123 static	struct netent net_entry;
124 static	char *net_aliases[MAXALIASES], netbuf[PACKETSZ];
125 
126 	/*
127 	 * find first satisfactory answer
128 	 *
129 	 *      answer --> +------------+  ( MESSAGE )
130 	 *		   |   Header   |
131 	 *		   +------------+
132 	 *		   |  Question  | the question for the name server
133 	 *		   +------------+
134 	 *		   |   Answer   | RRs answering the question
135 	 *		   +------------+
136 	 *		   | Authority  | RRs pointing toward an authority
137 	 *		   | Additional | RRs holding additional information
138 	 *		   +------------+
139 	 */
140 	eom = answer->buf + anslen;
141 	hp = &answer->hdr;
142 	ancount = ntohs(hp->ancount); /* #/records in the answer section */
143 	qdcount = ntohs(hp->qdcount); /* #/entries in the question section */
144 	bp = netbuf;
145 	buflen = sizeof(netbuf);
146 	cp = answer->buf + HFIXEDSZ;
147 	if (!qdcount) {
148 		if (hp->aa)
149 			h_errno = HOST_NOT_FOUND;
150 		else
151 			h_errno = TRY_AGAIN;
152 		return (NULL);
153 	}
154 	while (qdcount-- > 0)
155 		cp += __dn_skipname(cp, eom) + QFIXEDSZ;
156 	ap = net_aliases;
157 	*ap = NULL;
158 	net_entry.n_aliases = net_aliases;
159 	haveanswer = 0;
160 	while (--ancount >= 0 && cp < eom) {
161 		n = dn_expand(answer->buf, eom, cp, bp, buflen);
162 		if ((n < 0) || !res_dnok(bp))
163 			break;
164 		cp += n;
165 		ans[0] = '\0';
166 		(void)strncpy(&ans[0], bp, sizeof(ans) - 1);
167 		ans[sizeof(ans) - 1] = '\0';
168 		GETSHORT(type, cp);
169 		GETSHORT(class, cp);
170 		cp += INT32SZ;		/* TTL */
171 		GETSHORT(n, cp);
172 		if (class == C_IN && type == T_PTR) {
173 			n = dn_expand(answer->buf, eom, cp, bp, buflen);
174 			if ((n < 0) || !res_hnok(bp)) {
175 				cp += n;
176 				return (NULL);
177 			}
178 			cp += n;
179 			*ap++ = bp;
180 			bp += strlen(bp) + 1;
181 			net_entry.n_addrtype =
182 				(class == C_IN) ? AF_INET : AF_UNSPEC;
183 			haveanswer++;
184 		}
185 	}
186 	if (haveanswer) {
187 		*ap = NULL;
188 		switch (net_i) {
189 		case BYADDR:
190 			net_entry.n_name = *net_entry.n_aliases;
191 			net_entry.n_net = 0L;
192 			break;
193 		case BYNAME:
194 			in = *net_entry.n_aliases;
195 			net_entry.n_name = &ans[0];
196 			aux2[0] = '\0';
197 			for (i = 0; i < 4; i++) {
198 				for (st = in, nchar = 0;
199 				     *st != '.';
200 				     st++, nchar++)
201 					;
202 				if (nchar != 1 || *in != '0' || flag) {
203 					flag = 1;
204 					(void)strncpy(paux1,
205 						      (i==0) ? in : in-1,
206 						      (i==0) ?nchar : nchar+1);
207 					paux1[(i==0) ? nchar : nchar+1] = '\0';
208 					pauxt = paux2;
209 					paux2 = strcat(paux1, paux2);
210 					paux1 = pauxt;
211 				}
212 				in = ++st;
213 			}
214 			net_entry.n_net = inet_network(paux2);
215 			break;
216 		}
217 		net_entry.n_aliases++;
218 		return (&net_entry);
219 	}
220 	h_errno = TRY_AGAIN;
221 	return (NULL);
222 }
223 
224 int
_dns_getnetbyaddr(void * rval,void * cb_data,va_list ap)225 _dns_getnetbyaddr(void *rval, void *cb_data, va_list ap)
226 {
227 	unsigned long net;
228 	int net_type;
229 	unsigned int netbr[4];
230 	int nn, anslen;
231 	querybuf buf;
232 	char qbuf[MAXDNAME];
233 	unsigned long net2;
234 	struct netent *net_entry;
235 
236 	net = va_arg(ap, unsigned long);
237 	net_type = va_arg(ap, int);
238 
239 	*(struct netent **)rval = NULL;
240 
241 	if (net_type != AF_INET)
242 		return NS_UNAVAIL;
243 
244 	for (nn = 4, net2 = net; net2; net2 >>= 8)
245 		netbr[--nn] = net2 & 0xff;
246 	switch (nn) {
247 	case 3: 	/* Class A */
248 		sprintf(qbuf, "0.0.0.%u.in-addr.arpa", netbr[3]);
249 		break;
250 	case 2: 	/* Class B */
251 		sprintf(qbuf, "0.0.%u.%u.in-addr.arpa", netbr[3], netbr[2]);
252 		break;
253 	case 1: 	/* Class C */
254 		sprintf(qbuf, "0.%u.%u.%u.in-addr.arpa", netbr[3], netbr[2],
255 		    netbr[1]);
256 		break;
257 	case 0: 	/* Class D - E */
258 		sprintf(qbuf, "%u.%u.%u.%u.in-addr.arpa", netbr[3], netbr[2],
259 		    netbr[1], netbr[0]);
260 		break;
261 	}
262 	anslen = res_query(qbuf, C_IN, T_PTR, (u_char *)&buf, sizeof(buf));
263 	if (anslen < 0) {
264 #ifdef DEBUG
265 		if (_res.options & RES_DEBUG)
266 			printf("res_query failed\n");
267 #endif
268 		return NS_UNAVAIL;
269 	}
270 	net_entry = getnetanswer(&buf, anslen, BYADDR);
271 	if (net_entry) {
272 		unsigned u_net = net;	/* maybe net should be unsigned ? */
273 
274 		/* Strip trailing zeros */
275 		while ((u_net & 0xff) == 0 && u_net != 0)
276 			u_net >>= 8;
277 		net_entry->n_net = u_net;
278 		*(struct netent **)rval = net_entry;
279 		return NS_SUCCESS;
280 	}
281 	return NS_NOTFOUND;
282 }
283 
284 int
_dns_getnetbyname(void * rval,void * cb_data,va_list ap)285 _dns_getnetbyname(void *rval, void *cb_data, va_list ap)
286 {
287 	const char *net;
288 	int anslen;
289 	querybuf buf;
290 	char qbuf[MAXDNAME];
291 
292 	net = va_arg(ap, const char *);
293 
294 	*(struct netent**)rval = NULL;
295 
296 	if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
297 		h_errno = NETDB_INTERNAL;
298 		return NS_UNAVAIL;
299 	}
300 	strncpy(qbuf, net, sizeof(qbuf) - 1);
301 	qbuf[sizeof(qbuf) - 1] = '\0';
302 	anslen = res_search(qbuf, C_IN, T_PTR, (u_char *)&buf, sizeof(buf));
303 	if (anslen < 0) {
304 #ifdef DEBUG
305 		if (_res.options & RES_DEBUG)
306 			printf("res_query failed\n");
307 #endif
308 		return NS_UNAVAIL;
309 	}
310 	*(struct netent**)rval = getnetanswer(&buf, anslen, BYNAME);
311 	return (*(struct netent**)rval != NULL) ? NS_SUCCESS : NS_NOTFOUND;
312 }
313 
314 void
_setnetdnsent(stayopen)315 _setnetdnsent(stayopen)
316 	int stayopen;
317 {
318 	if (stayopen)
319 		_res.options |= RES_STAYOPEN | RES_USEVC;
320 }
321 
322 void
_endnetdnsent()323 _endnetdnsent()
324 {
325 	_res.options &= ~(RES_STAYOPEN | RES_USEVC);
326 	res_close();
327 }
328