1 /*-
2 * SPDX-License-Identifier: ISC
3 *
4 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (c) 1996,1999 by Internet Software Consortium.
6 *
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
17 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 */
19
20 #ifndef lint
21 static const char rcsid[] = "$Id: ns_parse.c,v 1.10 2009/01/23 19:59:16 each Exp $";
22 #endif
23 /* Import. */
24
25 #include "port_before.h"
26
27 #include <sys/types.h>
28
29 #include <netinet/in.h>
30 #include <arpa/nameser.h>
31
32 #include <errno.h>
33 #include <resolv.h>
34 #include <string.h>
35
36 #include "port_after.h"
37
38 /* Forward. */
39
40 static void setsection(ns_msg *msg, ns_sect sect);
41
42 /* Macros. */
43
44 #if !defined(SOLARIS2) || defined(__COVERITY__)
45 #define RETERR(err) do { errno = (err); return (-1); } while (0)
46 #else
47 #define RETERR(err) \
48 do { errno = (err); if (errno == errno) return (-1); } while (0)
49 #endif
50
51 #define PARSE_FMT_PRESO 0 /* Parse using presentation-format names */
52 #define PARSE_FMT_WIRE 1 /* Parse using network-format names */
53
54 /* Public. */
55
56 /* These need to be in the same order as the nres.h:ns_flag enum. */
57 struct _ns_flagdata _ns_flagdata[16] = {
58 { 0x8000, 15 }, /*%< qr. */
59 { 0x7800, 11 }, /*%< opcode. */
60 { 0x0400, 10 }, /*%< aa. */
61 { 0x0200, 9 }, /*%< tc. */
62 { 0x0100, 8 }, /*%< rd. */
63 { 0x0080, 7 }, /*%< ra. */
64 { 0x0040, 6 }, /*%< z. */
65 { 0x0020, 5 }, /*%< ad. */
66 { 0x0010, 4 }, /*%< cd. */
67 { 0x000f, 0 }, /*%< rcode. */
68 { 0x0000, 0 }, /*%< expansion (1/6). */
69 { 0x0000, 0 }, /*%< expansion (2/6). */
70 { 0x0000, 0 }, /*%< expansion (3/6). */
71 { 0x0000, 0 }, /*%< expansion (4/6). */
72 { 0x0000, 0 }, /*%< expansion (5/6). */
73 { 0x0000, 0 }, /*%< expansion (6/6). */
74 };
75
ns_msg_getflag(ns_msg handle,int flag)76 int ns_msg_getflag(ns_msg handle, int flag) {
77 return(((handle)._flags & _ns_flagdata[flag].mask) >> _ns_flagdata[flag].shift);
78 }
79
80 int
ns_skiprr(const u_char * ptr,const u_char * eom,ns_sect section,int count)81 ns_skiprr(const u_char *ptr, const u_char *eom, ns_sect section, int count) {
82 const u_char *optr = ptr;
83
84 for ((void)NULL; count > 0; count--) {
85 int b, rdlength;
86
87 b = dn_skipname(ptr, eom);
88 if (b < 0)
89 RETERR(EMSGSIZE);
90 ptr += b/*Name*/ + NS_INT16SZ/*Type*/ + NS_INT16SZ/*Class*/;
91 if (section != ns_s_qd) {
92 if (ptr + NS_INT32SZ + NS_INT16SZ > eom)
93 RETERR(EMSGSIZE);
94 ptr += NS_INT32SZ/*TTL*/;
95 NS_GET16(rdlength, ptr);
96 ptr += rdlength/*RData*/;
97 }
98 }
99 if (ptr > eom)
100 RETERR(EMSGSIZE);
101 return (ptr - optr);
102 }
103
104 int
ns_initparse(const u_char * msg,int msglen,ns_msg * handle)105 ns_initparse(const u_char *msg, int msglen, ns_msg *handle) {
106 const u_char *eom = msg + msglen;
107 int i;
108
109 handle->_msg = msg;
110 handle->_eom = eom;
111 if (msg + NS_INT16SZ > eom)
112 RETERR(EMSGSIZE);
113 NS_GET16(handle->_id, msg);
114 if (msg + NS_INT16SZ > eom)
115 RETERR(EMSGSIZE);
116 NS_GET16(handle->_flags, msg);
117 for (i = 0; i < ns_s_max; i++) {
118 if (msg + NS_INT16SZ > eom)
119 RETERR(EMSGSIZE);
120 NS_GET16(handle->_counts[i], msg);
121 }
122 for (i = 0; i < ns_s_max; i++)
123 if (handle->_counts[i] == 0)
124 handle->_sections[i] = NULL;
125 else {
126 int b = ns_skiprr(msg, eom, (ns_sect)i,
127 handle->_counts[i]);
128
129 if (b < 0)
130 return (-1);
131 handle->_sections[i] = msg;
132 msg += b;
133 }
134 if (msg != eom)
135 RETERR(EMSGSIZE);
136 setsection(handle, ns_s_max);
137 return (0);
138 }
139
140 int
ns_parserr(ns_msg * handle,ns_sect section,int rrnum,ns_rr * rr)141 ns_parserr(ns_msg *handle, ns_sect section, int rrnum, ns_rr *rr) {
142 int b;
143 int tmp;
144
145 /* Make section right. */
146 tmp = section;
147 if (tmp < 0 || section >= ns_s_max)
148 RETERR(ENODEV);
149 if (section != handle->_sect)
150 setsection(handle, section);
151
152 /* Make rrnum right. */
153 if (rrnum == -1)
154 rrnum = handle->_rrnum;
155 if (rrnum < 0 || rrnum >= handle->_counts[(int)section])
156 RETERR(ENODEV);
157 if (rrnum < handle->_rrnum)
158 setsection(handle, section);
159 if (rrnum > handle->_rrnum) {
160 b = ns_skiprr(handle->_msg_ptr, handle->_eom, section,
161 rrnum - handle->_rrnum);
162
163 if (b < 0)
164 return (-1);
165 handle->_msg_ptr += b;
166 handle->_rrnum = rrnum;
167 }
168
169 /* Do the parse. */
170 b = dn_expand(handle->_msg, handle->_eom,
171 handle->_msg_ptr, rr->name, NS_MAXDNAME);
172 if (b < 0)
173 return (-1);
174 handle->_msg_ptr += b;
175 if (handle->_msg_ptr + NS_INT16SZ + NS_INT16SZ > handle->_eom)
176 RETERR(EMSGSIZE);
177 NS_GET16(rr->type, handle->_msg_ptr);
178 NS_GET16(rr->rr_class, handle->_msg_ptr);
179 if (section == ns_s_qd) {
180 rr->ttl = 0;
181 rr->rdlength = 0;
182 rr->rdata = NULL;
183 } else {
184 if (handle->_msg_ptr + NS_INT32SZ + NS_INT16SZ > handle->_eom)
185 RETERR(EMSGSIZE);
186 NS_GET32(rr->ttl, handle->_msg_ptr);
187 NS_GET16(rr->rdlength, handle->_msg_ptr);
188 if (handle->_msg_ptr + rr->rdlength > handle->_eom)
189 RETERR(EMSGSIZE);
190 rr->rdata = handle->_msg_ptr;
191 handle->_msg_ptr += rr->rdlength;
192 }
193 if (++handle->_rrnum > handle->_counts[(int)section])
194 setsection(handle, (ns_sect)((int)section + 1));
195
196 /* All done. */
197 return (0);
198 }
199
200 /*
201 * This is identical to the above but uses network-format (uncompressed) names.
202 */
203 int
ns_parserr2(ns_msg * handle,ns_sect section,int rrnum,ns_rr2 * rr)204 ns_parserr2(ns_msg *handle, ns_sect section, int rrnum, ns_rr2 *rr) {
205 int b;
206 int tmp;
207
208 /* Make section right. */
209 if ((tmp = section) < 0 || section >= ns_s_max)
210 RETERR(ENODEV);
211 if (section != handle->_sect)
212 setsection(handle, section);
213
214 /* Make rrnum right. */
215 if (rrnum == -1)
216 rrnum = handle->_rrnum;
217 if (rrnum < 0 || rrnum >= handle->_counts[(int)section])
218 RETERR(ENODEV);
219 if (rrnum < handle->_rrnum)
220 setsection(handle, section);
221 if (rrnum > handle->_rrnum) {
222 b = ns_skiprr(handle->_msg_ptr, handle->_eom, section,
223 rrnum - handle->_rrnum);
224
225 if (b < 0)
226 return (-1);
227 handle->_msg_ptr += b;
228 handle->_rrnum = rrnum;
229 }
230
231 /* Do the parse. */
232 b = ns_name_unpack2(handle->_msg, handle->_eom, handle->_msg_ptr,
233 rr->nname, NS_MAXNNAME, &rr->nnamel);
234 if (b < 0)
235 return (-1);
236 handle->_msg_ptr += b;
237 if (handle->_msg_ptr + NS_INT16SZ + NS_INT16SZ > handle->_eom)
238 RETERR(EMSGSIZE);
239 NS_GET16(rr->type, handle->_msg_ptr);
240 NS_GET16(rr->rr_class, handle->_msg_ptr);
241 if (section == ns_s_qd) {
242 rr->ttl = 0;
243 rr->rdlength = 0;
244 rr->rdata = NULL;
245 } else {
246 if (handle->_msg_ptr + NS_INT32SZ + NS_INT16SZ > handle->_eom)
247 RETERR(EMSGSIZE);
248 NS_GET32(rr->ttl, handle->_msg_ptr);
249 NS_GET16(rr->rdlength, handle->_msg_ptr);
250 if (handle->_msg_ptr + rr->rdlength > handle->_eom)
251 RETERR(EMSGSIZE);
252 rr->rdata = handle->_msg_ptr;
253 handle->_msg_ptr += rr->rdlength;
254 }
255 if (++handle->_rrnum > handle->_counts[(int)section])
256 setsection(handle, (ns_sect)((int)section + 1));
257
258 /* All done. */
259 return (0);
260 }
261
262 /* Private. */
263
264 static void
setsection(ns_msg * msg,ns_sect sect)265 setsection(ns_msg *msg, ns_sect sect) {
266 msg->_sect = sect;
267 if (sect == ns_s_max) {
268 msg->_rrnum = -1;
269 msg->_msg_ptr = NULL;
270 } else {
271 msg->_rrnum = 0;
272 msg->_msg_ptr = msg->_sections[(int)sect];
273 }
274 }
275
276 /*! \file */
277