xref: /titanic_41/usr/src/lib/libresolv2/common/irs/dns_nw.c (revision 0c79d02b29618f74322989ec8ceafaa5486ac1db)
1 /*
2  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (c) 1996-1999 by Internet Software Consortium.
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
15  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 
18 #if defined(LIBC_SCCS) && !defined(lint)
19 static const char rcsid[] = "$Id: dns_nw.c,v 1.12 2005/04/27 04:56:22 sra Exp $";
20 #endif /* LIBC_SCCS and not lint */
21 
22 /* Imports. */
23 
24 #include "port_before.h"
25 
26 #include <sys/param.h>
27 #include <sys/socket.h>
28 
29 #include <netinet/in.h>
30 #include <arpa/inet.h>
31 #include <arpa/nameser.h>
32 
33 #include <ctype.h>
34 #include <errno.h>
35 #include <netdb.h>
36 #include <resolv.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 
41 #include <isc/memcluster.h>
42 #include <irs.h>
43 
44 #include "port_after.h"
45 
46 #include "irs_p.h"
47 #include "dns_p.h"
48 
49 #ifdef SPRINTF_CHAR
50 # define SPRINTF(x) strlen(sprintf/**/x)
51 #else
52 # define SPRINTF(x) sprintf x
53 #endif
54 
55 /* Definitions. */
56 
57 #define	MAXALIASES	35
58 
59 #define	MAXPACKET	(64*1024)
60 
61 struct pvt {
62 	struct nwent	net;
63 	char *		ali[MAXALIASES];
64 	char		buf[BUFSIZ+1];
65 	struct __res_state * res;
66 	void		(*free_res)(void *);
67 };
68 
69 typedef union {
70 	long	al;
71 	char	ac;
72 } align;
73 
74 enum by_what { by_addr, by_name };
75 
76 /* Forwards. */
77 
78 static void		nw_close(struct irs_nw *);
79 static struct nwent *	nw_byname(struct irs_nw *, const char *, int);
80 static struct nwent *	nw_byaddr(struct irs_nw *, void *, int, int);
81 static struct nwent *	nw_next(struct irs_nw *);
82 static void		nw_rewind(struct irs_nw *);
83 static void		nw_minimize(struct irs_nw *);
84 static struct __res_state * nw_res_get(struct irs_nw *this);
85 static void		nw_res_set(struct irs_nw *this,
86 				   struct __res_state *res,
87 				   void (*free_res)(void *));
88 
89 static struct nwent *	get1101byaddr(struct irs_nw *, u_char *, int);
90 static struct nwent *	get1101byname(struct irs_nw *, const char *);
91 static struct nwent *	get1101answer(struct irs_nw *,
92 				      u_char *ansbuf, int anslen,
93 				      enum by_what by_what,
94 				      int af, const char *name,
95 				      const u_char *addr, int addrlen);
96 static struct nwent *	get1101mask(struct irs_nw *this, struct nwent *);
97 static int		make1101inaddr(const u_char *, int, char *, int);
98 static void		normalize_name(char *name);
99 static int		init(struct irs_nw *this);
100 
101 /* Exports. */
102 
103 struct irs_nw *
104 irs_dns_nw(struct irs_acc *this) {
105 	struct irs_nw *nw;
106 	struct pvt *pvt;
107 
108 	UNUSED(this);
109 
110 	if (!(pvt = memget(sizeof *pvt))) {
111 		errno = ENOMEM;
112 		return (NULL);
113 	}
114 	memset(pvt, 0, sizeof *pvt);
115 	if (!(nw = memget(sizeof *nw))) {
116 		memput(pvt, sizeof *pvt);
117 		errno = ENOMEM;
118 		return (NULL);
119 	}
120 	memset(nw, 0x5e, sizeof *nw);
121 	nw->private = pvt;
122 	nw->close = nw_close;
123 	nw->byname = nw_byname;
124 	nw->byaddr = nw_byaddr;
125 	nw->next = nw_next;
126 	nw->rewind = nw_rewind;
127 	nw->minimize = nw_minimize;
128 	nw->res_get = nw_res_get;
129 	nw->res_set = nw_res_set;
130 	return (nw);
131 }
132 
133 /* Methods. */
134 
135 static void
136 nw_close(struct irs_nw *this) {
137 	struct pvt *pvt = (struct pvt *)this->private;
138 
139 	nw_minimize(this);
140 
141 	if (pvt->res && pvt->free_res)
142 		(*pvt->free_res)(pvt->res);
143 
144 	memput(pvt, sizeof *pvt);
145 	memput(this, sizeof *this);
146 }
147 
148 static struct nwent *
149 nw_byname(struct irs_nw *this, const char *name, int af) {
150 	struct pvt *pvt = (struct pvt *)this->private;
151 
152 	if (init(this) == -1)
153 		return (NULL);
154 
155 	switch (af) {
156 	case AF_INET:
157 		return (get1101byname(this, name));
158 	default:
159 		(void)NULL;
160 	}
161 	RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL);
162 	errno = EAFNOSUPPORT;
163 	return (NULL);
164 }
165 
166 static struct nwent *
167 nw_byaddr(struct irs_nw *this, void *net, int len, int af) {
168 	struct pvt *pvt = (struct pvt *)this->private;
169 
170 	if (init(this) == -1)
171 		return (NULL);
172 
173 	switch (af) {
174 	case AF_INET:
175 		return (get1101byaddr(this, net, len));
176 	default:
177 		(void)NULL;
178 	}
179 	RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL);
180 	errno = EAFNOSUPPORT;
181 	return (NULL);
182 }
183 
184 static struct nwent *
185 nw_next(struct irs_nw *this) {
186 
187 	UNUSED(this);
188 
189 	return (NULL);
190 }
191 
192 static void
193 nw_rewind(struct irs_nw *this) {
194 	UNUSED(this);
195 	/* NOOP */
196 }
197 
198 static void
199 nw_minimize(struct irs_nw *this) {
200 	struct pvt *pvt = (struct pvt *)this->private;
201 
202 	if (pvt->res)
203 		res_nclose(pvt->res);
204 }
205 
206 static struct __res_state *
207 nw_res_get(struct irs_nw *this) {
208 	struct pvt *pvt = (struct pvt *)this->private;
209 
210 	if (!pvt->res) {
211 		struct __res_state *res;
212 		res = (struct __res_state *)malloc(sizeof *res);
213 		if (!res) {
214 			errno = ENOMEM;
215 			return (NULL);
216 		}
217 		memset(res, 0, sizeof *res);
218 		nw_res_set(this, res, free);
219 	}
220 
221 	return (pvt->res);
222 }
223 
224 static void
225 nw_res_set(struct irs_nw *this, struct __res_state *res,
226 		void (*free_res)(void *)) {
227 	struct pvt *pvt = (struct pvt *)this->private;
228 
229 	if (pvt->res && pvt->free_res) {
230 		res_nclose(pvt->res);
231 		(*pvt->free_res)(pvt->res);
232 	}
233 
234 	pvt->res = res;
235 	pvt->free_res = free_res;
236 }
237 
238 /* Private. */
239 
240 static struct nwent *
241 get1101byname(struct irs_nw *this, const char *name) {
242 	struct pvt *pvt = (struct pvt *)this->private;
243 	u_char *ansbuf;
244 	int anslen;
245 	struct nwent *result;
246 
247 	ansbuf = memget(MAXPACKET);
248 	if (ansbuf == NULL) {
249 		errno = ENOMEM;
250 		RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL);
251 		return (NULL);
252 	}
253 	anslen = res_nsearch(pvt->res, name, C_IN, T_PTR, ansbuf, MAXPACKET);
254 	if (anslen < 0) {
255 		memput(ansbuf, MAXPACKET);
256 		return (NULL);
257 	}
258 	result = get1101mask(this, get1101answer(this, ansbuf, anslen, by_name,
259 						 AF_INET, name, NULL, 0));
260 	memput(ansbuf, MAXPACKET);
261 	return (result);
262 }
263 
264 static struct nwent *
265 get1101byaddr(struct irs_nw *this, u_char *net, int len) {
266 	struct pvt *pvt = (struct pvt *)this->private;
267 	char qbuf[sizeof "255.255.255.255.in-addr.arpa"];
268 	struct nwent *result;
269 	u_char *ansbuf;
270 	int anslen;
271 
272 	if (len < 1 || len > 32) {
273 		errno = EINVAL;
274 		RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL);
275 		return (NULL);
276 	}
277 	if (make1101inaddr(net, len, qbuf, sizeof qbuf) < 0)
278 		return (NULL);
279 	ansbuf = memget(MAXPACKET);
280 	if (ansbuf == NULL) {
281 		errno = ENOMEM;
282 		RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL);
283 		return (NULL);
284 	}
285 	anslen = res_nquery(pvt->res, qbuf, C_IN, T_PTR, ansbuf, MAXPACKET);
286 	if (anslen < 0) {
287 		memput(ansbuf, MAXPACKET);
288 		return (NULL);
289 	}
290 	result = get1101mask(this, get1101answer(this, ansbuf, anslen, by_addr,
291 						 AF_INET, NULL, net, len));
292 	memput(ansbuf, MAXPACKET);
293 	return (result);
294 }
295 
296 static struct nwent *
297 get1101answer(struct irs_nw *this,
298 	      u_char *ansbuf, int anslen, enum by_what by_what,
299 	      int af, const char *name, const u_char *addr, int addrlen)
300 {
301 	struct pvt *pvt = (struct pvt *)this->private;
302 	int type, class, ancount, qdcount, haveanswer;
303 	char *bp, *ep, **ap;
304 	u_char *cp, *eom;
305 	HEADER *hp;
306 
307 	/* Initialize, and parse header. */
308 	eom = ansbuf + anslen;
309 	if (ansbuf + HFIXEDSZ > eom) {
310 		RES_SET_H_ERRNO(pvt->res, NO_RECOVERY);
311 		return (NULL);
312 	}
313 	hp = (HEADER *)ansbuf;
314 	cp = ansbuf + HFIXEDSZ;
315 	qdcount = ntohs(hp->qdcount);
316 	while (qdcount-- > 0) {
317 		int n = dn_skipname(cp, eom);
318 		cp += n + QFIXEDSZ;
319 		if (n < 0 || cp > eom) {
320 			RES_SET_H_ERRNO(pvt->res, NO_RECOVERY);
321 			return (NULL);
322 		}
323 	}
324 	ancount = ntohs(hp->ancount);
325 	if (!ancount) {
326 		if (hp->aa)
327 			RES_SET_H_ERRNO(pvt->res, HOST_NOT_FOUND);
328 		else
329 			RES_SET_H_ERRNO(pvt->res, TRY_AGAIN);
330 		return (NULL);
331 	}
332 
333 	/* Prepare a return structure. */
334 	bp = pvt->buf;
335 	ep = pvt->buf + sizeof(pvt->buf);
336 	pvt->net.n_name = NULL;
337 	pvt->net.n_aliases = pvt->ali;
338 	pvt->net.n_addrtype = af;
339 	pvt->net.n_addr = NULL;
340 	pvt->net.n_length = addrlen;
341 
342 	/* Save input key if given. */
343 	switch (by_what) {
344 	case by_name:
345 		if (name != NULL) {
346 			int n = strlen(name) + 1;
347 
348 			if (n > (ep - bp)) {
349 				RES_SET_H_ERRNO(pvt->res, NO_RECOVERY);
350 				return (NULL);
351 			}
352 			pvt->net.n_name = strcpy(bp, name);	/* (checked) */
353 			bp += n;
354 		}
355 		break;
356 	case by_addr:
357 		if (addr != NULL && addrlen != 0) {
358 			int n = addrlen / 8 + ((addrlen % 8) != 0);
359 
360 			if (INADDRSZ > (ep - bp)) {
361 				RES_SET_H_ERRNO(pvt->res, NO_RECOVERY);
362 				return (NULL);
363 			}
364 			memset(bp, 0, INADDRSZ);
365 			memcpy(bp, addr, n);
366 			pvt->net.n_addr = bp;
367 			bp += INADDRSZ;
368 		}
369 		break;
370 	default:
371 		abort();
372 	}
373 
374 	/* Parse the answer, collect aliases. */
375 	ap = pvt->ali;
376 	haveanswer = 0;
377 	while (--ancount >= 0 && cp < eom) {
378 		int n = dn_expand(ansbuf, eom, cp, bp, ep - bp);
379 
380 		cp += n;		/*%< Owner */
381 		if (n < 0 || !maybe_dnok(pvt->res, bp) ||
382 		    cp + 3 * INT16SZ + INT32SZ > eom) {
383 			RES_SET_H_ERRNO(pvt->res, NO_RECOVERY);
384 			return (NULL);
385 		}
386 		GETSHORT(type, cp);	/*%< Type */
387 		GETSHORT(class, cp);	/*%< Class */
388 		cp += INT32SZ;		/*%< TTL */
389 		GETSHORT(n, cp);	/*%< RDLENGTH */
390 		if (class == C_IN && type == T_PTR) {
391 			int nn;
392 
393 			nn = dn_expand(ansbuf, eom, cp, bp, ep - bp);
394 			if (nn < 0 || !maybe_hnok(pvt->res, bp) || nn != n) {
395 				RES_SET_H_ERRNO(pvt->res, NO_RECOVERY);
396 				return (NULL);
397 			}
398 			normalize_name(bp);
399 			switch (by_what) {
400 			case by_addr: {
401 				if (pvt->net.n_name == NULL)
402 					pvt->net.n_name = bp;
403 				else if (ns_samename(pvt->net.n_name, bp) == 1)
404 					break;
405 				else
406 					*ap++ = bp;
407 				nn = strlen(bp) + 1;
408 				bp += nn;
409 				haveanswer++;
410 				break;
411 			    }
412 			case by_name: {
413 				u_int b1, b2, b3, b4;
414 
415 				if (pvt->net.n_addr != NULL ||
416 				    sscanf(bp, "%u.%u.%u.%u.in-addr.arpa",
417 					   &b1, &b2, &b3, &b4) != 4)
418 					break;
419 				if ((ep - bp) < INADDRSZ) {
420 					RES_SET_H_ERRNO(pvt->res, NO_RECOVERY);
421 					return (NULL);
422 				}
423 				pvt->net.n_addr = bp;
424 				*bp++ = b4;
425 				*bp++ = b3;
426 				*bp++ = b2;
427 				*bp++ = b1;
428 				pvt->net.n_length = INADDRSZ * 8;
429 				haveanswer++;
430 			    }
431 			}
432 		}
433 		cp += n;		/*%< RDATA */
434 	}
435 	if (!haveanswer) {
436 		RES_SET_H_ERRNO(pvt->res, TRY_AGAIN);
437 		return (NULL);
438 	}
439 	*ap = NULL;
440 
441 	return (&pvt->net);
442 }
443 
444 static struct nwent *
445 get1101mask(struct irs_nw *this, struct nwent *nwent) {
446 	struct pvt *pvt = (struct pvt *)this->private;
447 	char qbuf[sizeof "255.255.255.255.in-addr.arpa"], owner[MAXDNAME];
448 	int anslen, type, class, ancount, qdcount;
449 	u_char *ansbuf, *cp, *eom;
450 	HEADER *hp;
451 
452 	if (!nwent)
453 		return (NULL);
454 	if (make1101inaddr(nwent->n_addr, nwent->n_length, qbuf, sizeof qbuf)
455 	    < 0) {
456 		/* "First, do no harm." */
457 		return (nwent);
458 	}
459 
460 	ansbuf = memget(MAXPACKET);
461 	if (ansbuf == NULL) {
462 		errno = ENOMEM;
463 		RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL);
464 		return (NULL);
465 	}
466 	/* Query for the A RR that would hold this network's mask. */
467 	anslen = res_nquery(pvt->res, qbuf, C_IN, T_A, ansbuf, MAXPACKET);
468 	if (anslen < HFIXEDSZ) {
469 		memput(ansbuf, MAXPACKET);
470 		return (nwent);
471 	}
472 
473 	/* Initialize, and parse header. */
474 	hp = (HEADER *)ansbuf;
475 	cp = ansbuf + HFIXEDSZ;
476 	eom = ansbuf + anslen;
477 	qdcount = ntohs(hp->qdcount);
478 	while (qdcount-- > 0) {
479 		int n = dn_skipname(cp, eom);
480 		cp += n + QFIXEDSZ;
481 		if (n < 0 || cp > eom) {
482 			memput(ansbuf, MAXPACKET);
483 			return (nwent);
484 		}
485 	}
486 	ancount = ntohs(hp->ancount);
487 
488 	/* Parse the answer, collect aliases. */
489 	while (--ancount >= 0 && cp < eom) {
490 		int n = dn_expand(ansbuf, eom, cp, owner, sizeof owner);
491 
492 		if (n < 0 || !maybe_dnok(pvt->res, owner))
493 			break;
494 		cp += n;		/*%< Owner */
495 		if (cp + 3 * INT16SZ + INT32SZ > eom)
496 			break;
497 		GETSHORT(type, cp);	/*%< Type */
498 		GETSHORT(class, cp);	/*%< Class */
499 		cp += INT32SZ;		/*%< TTL */
500 		GETSHORT(n, cp);	/*%< RDLENGTH */
501 		if (cp + n > eom)
502 			break;
503 		if (n == INADDRSZ && class == C_IN && type == T_A &&
504 		    ns_samename(qbuf, owner) == 1) {
505 			/* This A RR indicates the actual netmask. */
506 			int nn, mm;
507 
508 			nwent->n_length = 0;
509 			for (nn = 0; nn < INADDRSZ; nn++)
510 				for (mm = 7; mm >= 0; mm--)
511 					if (cp[nn] & (1 << mm))
512 						nwent->n_length++;
513 					else
514 						break;
515 		}
516 		cp += n;		/*%< RDATA */
517 	}
518 	memput(ansbuf, MAXPACKET);
519 	return (nwent);
520 }
521 
522 static int
523 make1101inaddr(const u_char *net, int bits, char *name, int size) {
524 	int n, m;
525 	char *ep;
526 
527 	ep = name + size;
528 
529 	/* Zero fill any whole bytes left out of the prefix. */
530 	for (n = (32 - bits) / 8; n > 0; n--) {
531 		if (ep - name < (int)(sizeof "0."))
532 			goto emsgsize;
533 		m = SPRINTF((name, "0."));
534 		name += m;
535 	}
536 
537 	/* Format the partial byte, if any, within the prefix. */
538 	if ((n = bits % 8) != 0) {
539 		if (ep - name < (int)(sizeof "255."))
540 			goto emsgsize;
541 		m = SPRINTF((name, "%u.",
542 			     net[bits / 8] & ~((1 << (8 - n)) - 1)));
543 		name += m;
544 	}
545 
546 	/* Format the whole bytes within the prefix. */
547 	for (n = bits / 8; n > 0; n--) {
548 		if (ep - name < (int)(sizeof "255."))
549 			goto emsgsize;
550 		m = SPRINTF((name, "%u.", net[n - 1]));
551 		name += m;
552 	}
553 
554 	/* Add the static text. */
555 	if (ep - name < (int)(sizeof "in-addr.arpa"))
556 		goto emsgsize;
557 	(void) SPRINTF((name, "in-addr.arpa"));
558 	return (0);
559 
560  emsgsize:
561 	errno = EMSGSIZE;
562 	return (-1);
563 }
564 
565 static void
566 normalize_name(char *name) {
567 	char *t;
568 
569 	/* Make lower case. */
570 	for (t = name; *t; t++)
571 		if (isascii((unsigned char)*t) && isupper((unsigned char)*t))
572 			*t = tolower((*t)&0xff);
573 
574 	/* Remove trailing dots. */
575 	while (t > name && t[-1] == '.')
576 		*--t = '\0';
577 }
578 
579 static int
580 init(struct irs_nw *this) {
581 	struct pvt *pvt = (struct pvt *)this->private;
582 
583 	if (!pvt->res && !nw_res_get(this))
584 		return (-1);
585 	if (((pvt->res->options & RES_INIT) == 0U) &&
586 	    res_ninit(pvt->res) == -1)
587 		return (-1);
588 	return (0);
589 }
590 
591 /*! \file */
592