1 /*
2  * Copyright (c) 1996 by Internet Software Consortium.
3  *
4  * Permission to use, copy, modify, and distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
9  * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
10  * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
11  * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
12  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
13  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
14  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
15  * SOFTWARE.
16  */
17 
18 #include <sys/cdefs.h>
19 #include <sys/types.h>
20 
21 #include <sys/types.h>
22 
23 #include <netinet/in.h>
24 #include <arpa/nameser.h>
25 
26 #include <errno.h>
27 #include <resolv.h>
28 #include <string.h>
29 
30 /* These need to be in the same order as the nres.h:ns_flag enum. */
31 struct _ns_flagdata _ns_flagdata[16] = {
32 	{ 0x8000, 15 },		/* qr. */
33 	{ 0x7800, 11 },		/* opcode. */
34 	{ 0x0400, 10 },		/* aa. */
35 	{ 0x0200, 9 },		/* tc. */
36 	{ 0x0100, 8 },		/* rd. */
37 	{ 0x0080, 7 },		/* ra. */
38 	{ 0x0040, 6 },		/* z. */
39 	{ 0x0020, 5 },		/* ad. */
40 	{ 0x0010, 4 },		/* cd. */
41 	{ 0x000f, 0 },		/* rcode. */
42 	{ 0x0000, 0 },		/* expansion (1/6). */
43 	{ 0x0000, 0 },		/* expansion (2/6). */
44 	{ 0x0000, 0 },		/* expansion (3/6). */
45 	{ 0x0000, 0 },		/* expansion (4/6). */
46 	{ 0x0000, 0 },		/* expansion (5/6). */
47 	{ 0x0000, 0 },		/* expansion (6/6). */
48 };
49 
50 static int
skiprr(const u_char * ptr,const u_char * eom,ns_sect section,int count)51 skiprr(const u_char *ptr, const u_char *eom, ns_sect section, int count) {
52 	const u_char *optr = ptr;
53 
54 	for ((void)NULL; count > 0; count--) {
55 		int b, rdlength;
56 
57 		b = dn_skipname(ptr, eom);
58 		if (b < 0)
59 			goto emsgsize;
60 		ptr += b/*Name*/ + NS_INT16SZ/*Type*/ + NS_INT16SZ/*Class*/;
61 		if (section != ns_s_qd) {
62 			if (ptr + NS_INT32SZ > eom)
63 				goto emsgsize;
64 			ptr += NS_INT32SZ/*TTL*/;
65 			if (ptr + NS_INT16SZ > eom)
66 				goto emsgsize;
67 			NS_GET16(rdlength, ptr);
68 			ptr += rdlength/*RData*/;
69 		}
70 	}
71 	if (ptr > eom)
72 		goto emsgsize;
73 	return (ptr - optr);
74  emsgsize:
75 	errno = EMSGSIZE;
76 	return (-1);
77 }
78 
79 int
ns_initparse(const u_char * msg,int msglen,ns_msg * handle)80 ns_initparse(const u_char *msg, int msglen, ns_msg *handle) {
81 	const u_char *eom = msg + msglen;
82 	int i;
83 
84 	memset(handle, 0x5e, sizeof *handle);
85 	handle->_msg = msg;
86 	handle->_eom = eom;
87 	if (msg + NS_INT16SZ > eom)
88 		goto emsgsize;
89 	NS_GET16(handle->_id, msg);
90 	if (msg + NS_INT16SZ > eom)
91 		goto emsgsize;
92 	NS_GET16(handle->_flags, msg);
93 	for (i = 0; i < ns_s_max; i++) {
94 		if (msg + NS_INT16SZ > eom)
95 			goto emsgsize;
96 		NS_GET16(handle->_counts[i], msg);
97 	}
98 	for (i = 0; i < ns_s_max; i++)
99 		if (handle->_counts[i] == 0)
100 			handle->_sections[i] = NULL;
101 		else {
102 			int b = skiprr(msg, eom, (ns_sect)i,
103 				       handle->_counts[i]);
104 
105 			if (b < 0)
106 				return (-1);
107 			handle->_sections[i] = msg;
108 			msg += b;
109 		}
110 	if (msg != eom)
111 		goto emsgsize;
112 	handle->_sect = ns_s_max;
113 	handle->_rrnum = -1;
114 	handle->_ptr = NULL;
115 	return (0);
116  emsgsize:
117 	errno = EMSGSIZE;
118 	return (-1);
119 }
120 
121 int
ns_parserr(ns_msg * handle,ns_sect section,int rrnum,ns_rr * rr)122 ns_parserr(ns_msg *handle, ns_sect section, int rrnum, ns_rr *rr) {
123 	int b;
124 
125 	/* Make section right. */
126 	if (section < 0 || section >= ns_s_max)
127 		goto enodev;
128 	if ((int)section != (int)handle->_sect) {
129 		handle->_sect = section;
130 		handle->_rrnum = 0;
131 		handle->_ptr = handle->_sections[(int)section];
132 	}
133 
134 	/* Make rrnum right. */
135 	if (rrnum == -1)
136 		rrnum = handle->_rrnum;
137 	if (rrnum < 0 || rrnum >= handle->_counts[(int)section])
138 		goto enodev;
139 	if (rrnum < handle->_rrnum) {
140 		handle->_rrnum = 0;
141 		handle->_ptr = handle->_sections[(int)section];
142 	}
143 
144 	b = skiprr(handle->_msg, handle->_eom, section,
145 		   rrnum - handle->_rrnum);
146 	if (b < 0)
147 		return (-1);
148 	handle->_ptr += b;
149 	handle->_rrnum = rrnum;
150 
151 	/* Do the parse. */
152 	b = dn_expand(handle->_msg, handle->_eom,
153 		      handle->_ptr, rr->name, NS_MAXDNAME);
154 	if (b < 0)
155 		return (-1);
156 	handle->_ptr += b;
157 	if (handle->_ptr + NS_INT16SZ > handle->_eom)
158 		goto emsgsize;
159 	NS_GET16(rr->type, handle->_ptr);
160 	if (handle->_ptr + NS_INT16SZ > handle->_eom)
161 		goto emsgsize;
162 	NS_GET16(rr->rr_class, handle->_ptr);
163 	if (section == ns_s_qd) {
164 		rr->ttl = 0;
165 		rr->rdlength = 0;
166 		rr->rdata = NULL;
167 	} else {
168 		if (handle->_ptr + NS_INT32SZ > handle->_eom)
169 			goto emsgsize;
170 		NS_GET32(rr->ttl, handle->_ptr);
171 		if (handle->_ptr + NS_INT16SZ > handle->_eom)
172 			goto emsgsize;
173 		NS_GET16(rr->rdlength, handle->_ptr);
174 		if (handle->_ptr + rr->rdlength > handle->_eom)
175 			goto emsgsize;
176 		rr->rdata = handle->_ptr;
177 		handle->_ptr += rr->rdlength;
178 	}
179 	handle->_rrnum++;
180 
181 	/* All done. */
182 	return (0);
183  enodev:
184 	errno = ENODEV;
185 	return (-1);
186  emsgsize:
187 	errno = EMSGSIZE;
188 	return (-1);
189 }
190