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