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 #include "port_before.h"
19 
20 #include <sys/types.h>
21 
22 #include <netinet/in.h>
23 #include <arpa/nameser.h>
24 
25 #include <errno.h>
26 #include <resolv.h>
27 #include <string.h>
28 
29 #include "port_after.h"
30 
31 /* Forward. */
32 
33 static void	setsection(ns_msg *msg, ns_sect sect);
34 
35 /* Macros. */
36 
37 #if !defined(SOLARIS2) || defined(__COVERITY__)
38 #define RETERR(err) do { errno = (err); return (-1); } while (0)
39 #else
40 #define RETERR(err) \
41 	do { errno = (err); if (errno == errno) return (-1); } while (0)
42 #endif
43 
44 #define PARSE_FMT_PRESO 0	/* Parse using presentation-format names */
45 #define PARSE_FMT_WIRE 1	/* Parse using network-format names */
46 
47 /* Public. */
48 
49 /* These need to be in the same order as the nres.h:ns_flag enum. */
50 struct _ns_flagdata _ns_flagdata[16] = {
51 	{ 0x8000, 15 },		/*%< qr. */
52 	{ 0x7800, 11 },		/*%< opcode. */
53 	{ 0x0400, 10 },		/*%< aa. */
54 	{ 0x0200, 9 },		/*%< tc. */
55 	{ 0x0100, 8 },		/*%< rd. */
56 	{ 0x0080, 7 },		/*%< ra. */
57 	{ 0x0040, 6 },		/*%< z. */
58 	{ 0x0020, 5 },		/*%< ad. */
59 	{ 0x0010, 4 },		/*%< cd. */
60 	{ 0x000f, 0 },		/*%< rcode. */
61 	{ 0x0000, 0 },		/*%< expansion (1/6). */
62 	{ 0x0000, 0 },		/*%< expansion (2/6). */
63 	{ 0x0000, 0 },		/*%< expansion (3/6). */
64 	{ 0x0000, 0 },		/*%< expansion (4/6). */
65 	{ 0x0000, 0 },		/*%< expansion (5/6). */
66 	{ 0x0000, 0 },		/*%< expansion (6/6). */
67 };
68 
ns_msg_getflag(ns_msg handle,int flag)69 int ns_msg_getflag(ns_msg handle, int flag) {
70 	return(((handle)._flags & _ns_flagdata[flag].mask) >> _ns_flagdata[flag].shift);
71 }
72 
73 int
ns_skiprr(const u_char * ptr,const u_char * eom,ns_sect section,int count)74 ns_skiprr(const u_char *ptr, const u_char *eom, ns_sect section, int count) {
75 	const u_char *optr = ptr;
76 
77 	for ((void)NULL; count > 0; count--) {
78 		int b, rdlength;
79 
80 		b = dn_skipname(ptr, eom);
81 		if (b < 0)
82 			RETERR(EMSGSIZE);
83 		ptr += b/*Name*/ + NS_INT16SZ/*Type*/ + NS_INT16SZ/*Class*/;
84 		if (section != ns_s_qd) {
85 			if (ptr + NS_INT32SZ + NS_INT16SZ > eom)
86 				RETERR(EMSGSIZE);
87 			ptr += NS_INT32SZ/*TTL*/;
88 			NS_GET16(rdlength, ptr);
89 			ptr += rdlength/*RData*/;
90 		}
91 	}
92 	if (ptr > eom)
93 		RETERR(EMSGSIZE);
94 	return (ptr - optr);
95 }
96 
97 int
ns_initparse(const u_char * msg,int msglen,ns_msg * handle)98 ns_initparse(const u_char *msg, int msglen, ns_msg *handle) {
99 	const u_char *eom = msg + msglen;
100 	int i;
101 
102 	handle->_msg = msg;
103 	handle->_eom = eom;
104 	if (msg + NS_INT16SZ > eom)
105 		RETERR(EMSGSIZE);
106 	NS_GET16(handle->_id, msg);
107 	if (msg + NS_INT16SZ > eom)
108 		RETERR(EMSGSIZE);
109 	NS_GET16(handle->_flags, msg);
110 	for (i = 0; i < ns_s_max; i++) {
111 		if (msg + NS_INT16SZ > eom)
112 			RETERR(EMSGSIZE);
113 		NS_GET16(handle->_counts[i], msg);
114 	}
115 	for (i = 0; i < ns_s_max; i++)
116 		if (handle->_counts[i] == 0)
117 			handle->_sections[i] = NULL;
118 		else {
119 			int b = ns_skiprr(msg, eom, (ns_sect)i,
120 					  handle->_counts[i]);
121 
122 			if (b < 0)
123 				return (-1);
124 			handle->_sections[i] = msg;
125 			msg += b;
126 		}
127 	if (msg != eom)
128 		RETERR(EMSGSIZE);
129 	setsection(handle, ns_s_max);
130 	return (0);
131 }
132 
133 int
ns_parserr(ns_msg * handle,ns_sect section,int rrnum,ns_rr * rr)134 ns_parserr(ns_msg *handle, ns_sect section, int rrnum, ns_rr *rr) {
135 	int b;
136 	int tmp;
137 
138 	/* Make section right. */
139 	tmp = section;
140 	if (tmp < 0 || section >= ns_s_max)
141 		RETERR(ENODEV);
142 	if (section != handle->_sect)
143 		setsection(handle, section);
144 
145 	/* Make rrnum right. */
146 	if (rrnum == -1)
147 		rrnum = handle->_rrnum;
148 	if (rrnum < 0 || rrnum >= handle->_counts[(int)section])
149 		RETERR(ENODEV);
150 	if (rrnum < handle->_rrnum)
151 		setsection(handle, section);
152 	if (rrnum > handle->_rrnum) {
153 		b = ns_skiprr(handle->_msg_ptr, handle->_eom, section,
154 			      rrnum - handle->_rrnum);
155 
156 		if (b < 0)
157 			return (-1);
158 		handle->_msg_ptr += b;
159 		handle->_rrnum = rrnum;
160 	}
161 
162 	/* Do the parse. */
163 	b = dn_expand(handle->_msg, handle->_eom,
164 		      handle->_msg_ptr, rr->name, NS_MAXDNAME);
165 	if (b < 0)
166 		return (-1);
167 	handle->_msg_ptr += b;
168 	if (handle->_msg_ptr + NS_INT16SZ + NS_INT16SZ > handle->_eom)
169 		RETERR(EMSGSIZE);
170 	NS_GET16(rr->type, handle->_msg_ptr);
171 	NS_GET16(rr->rr_class, handle->_msg_ptr);
172 	if (section == ns_s_qd) {
173 		rr->ttl = 0;
174 		rr->rdlength = 0;
175 		rr->rdata = NULL;
176 	} else {
177 		if (handle->_msg_ptr + NS_INT32SZ + NS_INT16SZ > handle->_eom)
178 			RETERR(EMSGSIZE);
179 		NS_GET32(rr->ttl, handle->_msg_ptr);
180 		NS_GET16(rr->rdlength, handle->_msg_ptr);
181 		if (handle->_msg_ptr + rr->rdlength > handle->_eom)
182 			RETERR(EMSGSIZE);
183 		rr->rdata = handle->_msg_ptr;
184 		handle->_msg_ptr += rr->rdlength;
185 	}
186 	if (++handle->_rrnum > handle->_counts[(int)section])
187 		setsection(handle, (ns_sect)((int)section + 1));
188 
189 	/* All done. */
190 	return (0);
191 }
192 
193 /*
194  * This is identical to the above but uses network-format (uncompressed) names.
195  */
196 int
ns_parserr2(ns_msg * handle,ns_sect section,int rrnum,ns_rr2 * rr)197 ns_parserr2(ns_msg *handle, ns_sect section, int rrnum, ns_rr2 *rr) {
198 	int b;
199 	int tmp;
200 
201 	/* Make section right. */
202 	if ((tmp = section) < 0 || section >= ns_s_max)
203 		RETERR(ENODEV);
204 	if (section != handle->_sect)
205 		setsection(handle, section);
206 
207 	/* Make rrnum right. */
208 	if (rrnum == -1)
209 		rrnum = handle->_rrnum;
210 	if (rrnum < 0 || rrnum >= handle->_counts[(int)section])
211 		RETERR(ENODEV);
212 	if (rrnum < handle->_rrnum)
213 		setsection(handle, section);
214 	if (rrnum > handle->_rrnum) {
215 		b = ns_skiprr(handle->_msg_ptr, handle->_eom, section,
216 			      rrnum - handle->_rrnum);
217 
218 		if (b < 0)
219 			return (-1);
220 		handle->_msg_ptr += b;
221 		handle->_rrnum = rrnum;
222 	}
223 
224 	/* Do the parse. */
225 	b = ns_name_unpack2(handle->_msg, handle->_eom, handle->_msg_ptr,
226 			    rr->nname, NS_MAXNNAME, &rr->nnamel);
227 	if (b < 0)
228 		return (-1);
229 	handle->_msg_ptr += b;
230 	if (handle->_msg_ptr + NS_INT16SZ + NS_INT16SZ > handle->_eom)
231 		RETERR(EMSGSIZE);
232 	NS_GET16(rr->type, handle->_msg_ptr);
233 	NS_GET16(rr->rr_class, handle->_msg_ptr);
234 	if (section == ns_s_qd) {
235 		rr->ttl = 0;
236 		rr->rdlength = 0;
237 		rr->rdata = NULL;
238 	} else {
239 		if (handle->_msg_ptr + NS_INT32SZ + NS_INT16SZ > handle->_eom)
240 			RETERR(EMSGSIZE);
241 		NS_GET32(rr->ttl, handle->_msg_ptr);
242 		NS_GET16(rr->rdlength, handle->_msg_ptr);
243 		if (handle->_msg_ptr + rr->rdlength > handle->_eom)
244 			RETERR(EMSGSIZE);
245 		rr->rdata = handle->_msg_ptr;
246 		handle->_msg_ptr += rr->rdlength;
247 	}
248 	if (++handle->_rrnum > handle->_counts[(int)section])
249 		setsection(handle, (ns_sect)((int)section + 1));
250 
251 	/* All done. */
252 	return (0);
253 }
254 
255 /* Private. */
256 
257 static void
setsection(ns_msg * msg,ns_sect sect)258 setsection(ns_msg *msg, ns_sect sect) {
259 	msg->_sect = sect;
260 	if (sect == ns_s_max) {
261 		msg->_rrnum = -1;
262 		msg->_msg_ptr = NULL;
263 	} else {
264 		msg->_rrnum = 0;
265 		msg->_msg_ptr = msg->_sections[(int)sect];
266 	}
267 }
268 
269 /*! \file */
270