]>
Commit | Line | Data |
---|---|---|
b43b13ac | 1 | /* |
cd5743fd | 2 | * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") |
b43b13ac UD |
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 | * | |
cd5743fd UD |
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. | |
b43b13ac UD |
16 | */ |
17 | ||
b43b13ac UD |
18 | #include <sys/types.h> |
19 | ||
20 | #include <netinet/in.h> | |
21 | #include <arpa/nameser.h> | |
22 | ||
23 | #include <errno.h> | |
24 | #include <resolv.h> | |
25 | #include <string.h> | |
26 | #include <ctype.h> | |
cd5743fd UD |
27 | #include <stdlib.h> |
28 | #include <limits.h> | |
29 | ||
30 | # define SPRINTF(x) ((size_t)sprintf x) | |
31 | ||
b43b13ac UD |
32 | /* Data. */ |
33 | ||
34 | static const char digits[] = "0123456789"; | |
35 | ||
36 | /* Forward. */ | |
37 | ||
38 | static int special(int); | |
39 | static int printable(int); | |
40 | static int dn_find(const u_char *, const u_char *, | |
41 | const u_char * const *, | |
42 | const u_char * const *); | |
cd5743fd | 43 | static int labellen(const u_char *); |
b43b13ac UD |
44 | |
45 | /* Public. */ | |
46 | ||
cd5743fd | 47 | /*% |
b43b13ac | 48 | * Convert an encoded domain name to printable ascii as per RFC1035. |
cd5743fd | 49 | |
b43b13ac | 50 | * return: |
cd5743fd UD |
51 | *\li Number of bytes written to buffer, or -1 (with errno set) |
52 | * | |
b43b13ac | 53 | * notes: |
cd5743fd UD |
54 | *\li The root is returned as "." |
55 | *\li All other domains are returned in non absolute form | |
b43b13ac UD |
56 | */ |
57 | int | |
cd5743fd UD |
58 | ns_name_ntop(const u_char *src, char *dst, size_t dstsiz) |
59 | { | |
b43b13ac UD |
60 | const u_char *cp; |
61 | char *dn, *eom; | |
62 | u_char c; | |
63 | u_int n; | |
cd5743fd | 64 | int l; |
b43b13ac UD |
65 | |
66 | cp = src; | |
67 | dn = dst; | |
68 | eom = dst + dstsiz; | |
69 | ||
70 | while ((n = *cp++) != 0) { | |
cd5743fd | 71 | if ((n & NS_CMPRSFLGS) == NS_CMPRSFLGS) { |
b43b13ac UD |
72 | /* Some kind of compression pointer. */ |
73 | __set_errno (EMSGSIZE); | |
74 | return (-1); | |
75 | } | |
76 | if (dn != dst) { | |
77 | if (dn >= eom) { | |
78 | __set_errno (EMSGSIZE); | |
79 | return (-1); | |
80 | } | |
81 | *dn++ = '.'; | |
82 | } | |
cd5743fd UD |
83 | if ((l = labellen(cp - 1)) < 0) { |
84 | __set_errno (EMSGSIZE); | |
85 | return(-1); | |
86 | } | |
87 | if (dn + l >= eom) { | |
88 | __set_errno (EMSGSIZE); | |
89 | return (-1); | |
90 | } | |
cd5743fd | 91 | for ((void)NULL; l > 0; l--) { |
b43b13ac UD |
92 | c = *cp++; |
93 | if (special(c)) { | |
94 | if (dn + 1 >= eom) { | |
95 | __set_errno (EMSGSIZE); | |
96 | return (-1); | |
97 | } | |
98 | *dn++ = '\\'; | |
99 | *dn++ = (char)c; | |
100 | } else if (!printable(c)) { | |
101 | if (dn + 3 >= eom) { | |
102 | __set_errno (EMSGSIZE); | |
103 | return (-1); | |
104 | } | |
105 | *dn++ = '\\'; | |
106 | *dn++ = digits[c / 100]; | |
107 | *dn++ = digits[(c % 100) / 10]; | |
108 | *dn++ = digits[c % 10]; | |
109 | } else { | |
110 | if (dn >= eom) { | |
111 | __set_errno (EMSGSIZE); | |
112 | return (-1); | |
113 | } | |
114 | *dn++ = (char)c; | |
115 | } | |
116 | } | |
117 | } | |
118 | if (dn == dst) { | |
119 | if (dn >= eom) { | |
120 | __set_errno (EMSGSIZE); | |
121 | return (-1); | |
122 | } | |
123 | *dn++ = '.'; | |
124 | } | |
125 | if (dn >= eom) { | |
126 | __set_errno (EMSGSIZE); | |
127 | return (-1); | |
128 | } | |
129 | *dn++ = '\0'; | |
130 | return (dn - dst); | |
131 | } | |
6f9d8e68 | 132 | libresolv_hidden_def (ns_name_ntop) |
cd5743fd | 133 | strong_alias (ns_name_ntop, __ns_name_ntop) |
b43b13ac | 134 | |
cd5743fd | 135 | /*% |
c0c3f78a | 136 | * Convert an ascii string into an encoded domain name as per RFC1035. |
cd5743fd | 137 | * |
b43b13ac | 138 | * return: |
cd5743fd UD |
139 | * |
140 | *\li -1 if it fails | |
141 | *\li 1 if string was fully qualified | |
142 | *\li 0 is string was not fully qualified | |
143 | * | |
b43b13ac | 144 | * notes: |
cd5743fd | 145 | *\li Enforces label and domain length limits. |
b43b13ac UD |
146 | */ |
147 | ||
148 | int | |
cd5743fd UD |
149 | ns_name_pton(const char *src, u_char *dst, size_t dstsiz) |
150 | { | |
b43b13ac | 151 | u_char *label, *bp, *eom; |
5140d036 | 152 | int c, n, escaped; |
b43b13ac UD |
153 | char *cp; |
154 | ||
155 | escaped = 0; | |
156 | bp = dst; | |
157 | eom = dst + dstsiz; | |
158 | label = bp++; | |
159 | ||
160 | while ((c = *src++) != 0) { | |
161 | if (escaped) { | |
5140d036 | 162 | if ((cp = strchr(digits, c)) != NULL) { |
b43b13ac UD |
163 | n = (cp - digits) * 100; |
164 | if ((c = *src++) == 0 || | |
165 | (cp = strchr(digits, c)) == NULL) { | |
166 | __set_errno (EMSGSIZE); | |
167 | return (-1); | |
168 | } | |
169 | n += (cp - digits) * 10; | |
170 | if ((c = *src++) == 0 || | |
171 | (cp = strchr(digits, c)) == NULL) { | |
172 | __set_errno (EMSGSIZE); | |
173 | return (-1); | |
174 | } | |
175 | n += (cp - digits); | |
176 | if (n > 255) { | |
177 | __set_errno (EMSGSIZE); | |
178 | return (-1); | |
179 | } | |
180 | c = n; | |
181 | } | |
182 | escaped = 0; | |
183 | } else if (c == '\\') { | |
184 | escaped = 1; | |
185 | continue; | |
186 | } else if (c == '.') { | |
187 | c = (bp - label - 1); | |
cd5743fd | 188 | if ((c & NS_CMPRSFLGS) != 0) { /*%< Label too big. */ |
b43b13ac UD |
189 | __set_errno (EMSGSIZE); |
190 | return (-1); | |
191 | } | |
192 | if (label >= eom) { | |
193 | __set_errno (EMSGSIZE); | |
194 | return (-1); | |
195 | } | |
196 | *label = c; | |
197 | /* Fully qualified ? */ | |
198 | if (*src == '\0') { | |
199 | if (c != 0) { | |
200 | if (bp >= eom) { | |
201 | __set_errno (EMSGSIZE); | |
202 | return (-1); | |
203 | } | |
204 | *bp++ = '\0'; | |
205 | } | |
206 | if ((bp - dst) > MAXCDNAME) { | |
207 | __set_errno (EMSGSIZE); | |
208 | return (-1); | |
209 | } | |
210 | return (1); | |
211 | } | |
212 | if (c == 0 || *src == '.') { | |
213 | __set_errno (EMSGSIZE); | |
214 | return (-1); | |
215 | } | |
216 | label = bp++; | |
217 | continue; | |
218 | } | |
219 | if (bp >= eom) { | |
220 | __set_errno (EMSGSIZE); | |
221 | return (-1); | |
222 | } | |
223 | *bp++ = (u_char)c; | |
224 | } | |
9e0ad304 FW |
225 | if (escaped) { |
226 | /* Trailing backslash. */ | |
227 | __set_errno (EMSGSIZE); | |
228 | return -1; | |
229 | } | |
b43b13ac | 230 | c = (bp - label - 1); |
cd5743fd | 231 | if ((c & NS_CMPRSFLGS) != 0) { /*%< Label too big. */ |
b43b13ac UD |
232 | __set_errno (EMSGSIZE); |
233 | return (-1); | |
234 | } | |
235 | if (label >= eom) { | |
236 | __set_errno (EMSGSIZE); | |
237 | return (-1); | |
238 | } | |
239 | *label = c; | |
240 | if (c != 0) { | |
241 | if (bp >= eom) { | |
242 | __set_errno (EMSGSIZE); | |
243 | return (-1); | |
244 | } | |
245 | *bp++ = 0; | |
246 | } | |
cd5743fd | 247 | if ((bp - dst) > MAXCDNAME) { /*%< src too big */ |
b43b13ac UD |
248 | __set_errno (EMSGSIZE); |
249 | return (-1); | |
250 | } | |
251 | return (0); | |
252 | } | |
cd5743fd | 253 | libresolv_hidden_def (ns_name_pton) |
b43b13ac | 254 | |
cd5743fd | 255 | /*% |
b43b13ac | 256 | * Convert a network strings labels into all lowercase. |
cd5743fd | 257 | * |
b43b13ac | 258 | * return: |
cd5743fd UD |
259 | *\li Number of bytes written to buffer, or -1 (with errno set) |
260 | * | |
b43b13ac | 261 | * notes: |
cd5743fd | 262 | *\li Enforces label and domain length limits. |
b43b13ac UD |
263 | */ |
264 | ||
265 | int | |
cd5743fd UD |
266 | ns_name_ntol(const u_char *src, u_char *dst, size_t dstsiz) |
267 | { | |
b43b13ac UD |
268 | const u_char *cp; |
269 | u_char *dn, *eom; | |
270 | u_char c; | |
271 | u_int n; | |
cd5743fd | 272 | int l; |
b43b13ac UD |
273 | |
274 | cp = src; | |
275 | dn = dst; | |
276 | eom = dst + dstsiz; | |
277 | ||
cd5743fd UD |
278 | if (dn >= eom) { |
279 | __set_errno (EMSGSIZE); | |
280 | return (-1); | |
281 | } | |
b43b13ac | 282 | while ((n = *cp++) != 0) { |
cd5743fd | 283 | if ((n & NS_CMPRSFLGS) == NS_CMPRSFLGS) { |
b43b13ac UD |
284 | /* Some kind of compression pointer. */ |
285 | __set_errno (EMSGSIZE); | |
286 | return (-1); | |
287 | } | |
288 | *dn++ = n; | |
cd5743fd | 289 | if ((l = labellen(cp - 1)) < 0) { |
b43b13ac UD |
290 | __set_errno (EMSGSIZE); |
291 | return (-1); | |
292 | } | |
cd5743fd UD |
293 | if (dn + l >= eom) { |
294 | __set_errno (EMSGSIZE); | |
295 | return (-1); | |
296 | } | |
297 | for ((void)NULL; l > 0; l--) { | |
b43b13ac UD |
298 | c = *cp++; |
299 | if (isupper(c)) | |
300 | *dn++ = tolower(c); | |
301 | else | |
302 | *dn++ = c; | |
303 | } | |
304 | } | |
305 | *dn++ = '\0'; | |
306 | return (dn - dst); | |
307 | } | |
308 | ||
cd5743fd | 309 | /*% |
b43b13ac | 310 | * Unpack a domain name from a message, source may be compressed. |
cd5743fd | 311 | * |
b43b13ac | 312 | * return: |
cd5743fd | 313 | *\li -1 if it fails, or consumed octets if it succeeds. |
b43b13ac UD |
314 | */ |
315 | int | |
316 | ns_name_unpack(const u_char *msg, const u_char *eom, const u_char *src, | |
317 | u_char *dst, size_t dstsiz) | |
318 | { | |
319 | const u_char *srcp, *dstlim; | |
320 | u_char *dstp; | |
cd5743fd | 321 | int n, len, checked, l; |
b43b13ac UD |
322 | |
323 | len = -1; | |
324 | checked = 0; | |
325 | dstp = dst; | |
326 | srcp = src; | |
327 | dstlim = dst + dstsiz; | |
328 | if (srcp < msg || srcp >= eom) { | |
329 | __set_errno (EMSGSIZE); | |
330 | return (-1); | |
331 | } | |
332 | /* Fetch next label in domain name. */ | |
333 | while ((n = *srcp++) != 0) { | |
334 | /* Check for indirection. */ | |
335 | switch (n & NS_CMPRSFLGS) { | |
336 | case 0: | |
337 | /* Limit checks. */ | |
cd5743fd UD |
338 | if ((l = labellen(srcp - 1)) < 0) { |
339 | __set_errno (EMSGSIZE); | |
340 | return(-1); | |
341 | } | |
342 | if (dstp + l + 1 >= dstlim || srcp + l >= eom) { | |
b43b13ac UD |
343 | __set_errno (EMSGSIZE); |
344 | return (-1); | |
345 | } | |
cd5743fd UD |
346 | checked += l + 1; |
347 | *dstp++ = n; | |
348 | memcpy(dstp, srcp, l); | |
349 | dstp += l; | |
350 | srcp += l; | |
b43b13ac UD |
351 | break; |
352 | ||
353 | case NS_CMPRSFLGS: | |
354 | if (srcp >= eom) { | |
355 | __set_errno (EMSGSIZE); | |
356 | return (-1); | |
357 | } | |
358 | if (len < 0) | |
359 | len = srcp - src + 1; | |
360 | srcp = msg + (((n & 0x3f) << 8) | (*srcp & 0xff)); | |
cd5743fd | 361 | if (srcp < msg || srcp >= eom) { /*%< Out of range. */ |
b43b13ac UD |
362 | __set_errno (EMSGSIZE); |
363 | return (-1); | |
364 | } | |
365 | checked += 2; | |
366 | /* | |
367 | * Check for loops in the compressed name; | |
368 | * if we've looked at the whole message, | |
369 | * there must be a loop. | |
370 | */ | |
371 | if (checked >= eom - msg) { | |
372 | __set_errno (EMSGSIZE); | |
373 | return (-1); | |
374 | } | |
375 | break; | |
376 | ||
377 | default: | |
378 | __set_errno (EMSGSIZE); | |
cd5743fd | 379 | return (-1); /*%< flag error */ |
b43b13ac UD |
380 | } |
381 | } | |
382 | *dstp = '\0'; | |
383 | if (len < 0) | |
384 | len = srcp - src; | |
385 | return (len); | |
386 | } | |
6f9d8e68 | 387 | libresolv_hidden_def (ns_name_unpack) |
cd5743fd | 388 | strong_alias (ns_name_unpack, __ns_name_unpack) |
b43b13ac | 389 | |
cd5743fd | 390 | /*% |
b43b13ac | 391 | * Pack domain name 'domain' into 'comp_dn'. |
cd5743fd | 392 | * |
b43b13ac | 393 | * return: |
cd5743fd UD |
394 | *\li Size of the compressed name, or -1. |
395 | * | |
b43b13ac | 396 | * notes: |
cd5743fd UD |
397 | *\li 'dnptrs' is an array of pointers to previous compressed names. |
398 | *\li dnptrs[0] is a pointer to the beginning of the message. The array | |
b43b13ac | 399 | * ends with NULL. |
cd5743fd | 400 | *\li 'lastdnptr' is a pointer to the end of the array pointed to |
b43b13ac | 401 | * by 'dnptrs'. |
cd5743fd | 402 | * |
b43b13ac | 403 | * Side effects: |
cd5743fd | 404 | *\li The list of pointers in dnptrs is updated for labels inserted into |
b43b13ac UD |
405 | * the message as we compress the name. If 'dnptr' is NULL, we don't |
406 | * try to compress names. If 'lastdnptr' is NULL, we don't update the | |
407 | * list. | |
408 | */ | |
409 | int | |
410 | ns_name_pack(const u_char *src, u_char *dst, int dstsiz, | |
411 | const u_char **dnptrs, const u_char **lastdnptr) | |
412 | { | |
413 | u_char *dstp; | |
414 | const u_char **cpp, **lpp, *eob, *msg; | |
415 | const u_char *srcp; | |
e685e07d | 416 | int n, l, first = 1; |
b43b13ac UD |
417 | |
418 | srcp = src; | |
419 | dstp = dst; | |
420 | eob = dstp + dstsiz; | |
421 | lpp = cpp = NULL; | |
422 | if (dnptrs != NULL) { | |
423 | if ((msg = *dnptrs++) != NULL) { | |
424 | for (cpp = dnptrs; *cpp != NULL; cpp++) | |
425 | (void)NULL; | |
cd5743fd | 426 | lpp = cpp; /*%< end of list to search */ |
b43b13ac UD |
427 | } |
428 | } else | |
429 | msg = NULL; | |
430 | ||
431 | /* make sure the domain we are about to add is legal */ | |
432 | l = 0; | |
433 | do { | |
cd5743fd UD |
434 | int l0; |
435 | ||
b43b13ac | 436 | n = *srcp; |
cd5743fd | 437 | if ((n & NS_CMPRSFLGS) == NS_CMPRSFLGS) { |
b43b13ac UD |
438 | __set_errno (EMSGSIZE); |
439 | return (-1); | |
440 | } | |
cd5743fd UD |
441 | if ((l0 = labellen(srcp)) < 0) { |
442 | __set_errno (EINVAL); | |
443 | return(-1); | |
444 | } | |
445 | l += l0 + 1; | |
b43b13ac UD |
446 | if (l > MAXCDNAME) { |
447 | __set_errno (EMSGSIZE); | |
448 | return (-1); | |
449 | } | |
cd5743fd | 450 | srcp += l0 + 1; |
b43b13ac UD |
451 | } while (n != 0); |
452 | ||
453 | /* from here on we need to reset compression pointer array on error */ | |
454 | srcp = src; | |
455 | do { | |
456 | /* Look to see if we can use pointers. */ | |
457 | n = *srcp; | |
cd5743fd | 458 | if (n != 0 && msg != NULL) { |
b43b13ac UD |
459 | l = dn_find(srcp, msg, (const u_char * const *)dnptrs, |
460 | (const u_char * const *)lpp); | |
461 | if (l >= 0) { | |
462 | if (dstp + 1 >= eob) { | |
463 | goto cleanup; | |
464 | } | |
465 | *dstp++ = (l >> 8) | NS_CMPRSFLGS; | |
466 | *dstp++ = l % 256; | |
467 | return (dstp - dst); | |
468 | } | |
469 | /* Not found, save it. */ | |
470 | if (lastdnptr != NULL && cpp < lastdnptr - 1 && | |
e685e07d | 471 | (dstp - msg) < 0x4000 && first) { |
b43b13ac UD |
472 | *cpp++ = dstp; |
473 | *cpp = NULL; | |
e685e07d | 474 | first = 0; |
b43b13ac UD |
475 | } |
476 | } | |
477 | /* copy label to buffer */ | |
cd5743fd UD |
478 | if ((n & NS_CMPRSFLGS) == NS_CMPRSFLGS) { |
479 | /* Should not happen. */ | |
b43b13ac UD |
480 | goto cleanup; |
481 | } | |
cd5743fd | 482 | n = labellen(srcp); |
c803cb9b | 483 | if (n + 1 > eob - dstp) { |
b43b13ac UD |
484 | goto cleanup; |
485 | } | |
486 | memcpy(dstp, srcp, n + 1); | |
487 | srcp += n + 1; | |
488 | dstp += n + 1; | |
489 | } while (n != 0); | |
490 | ||
491 | if (dstp > eob) { | |
492 | cleanup: | |
493 | if (msg != NULL) | |
494 | *lpp = NULL; | |
495 | __set_errno (EMSGSIZE); | |
496 | return (-1); | |
5ae19a50 | 497 | } |
b43b13ac UD |
498 | return (dstp - dst); |
499 | } | |
cd5743fd | 500 | libresolv_hidden_def (ns_name_pack) |
b43b13ac | 501 | |
cd5743fd | 502 | /*% |
b43b13ac | 503 | * Expand compressed domain name to presentation format. |
cd5743fd | 504 | * |
b43b13ac | 505 | * return: |
cd5743fd UD |
506 | *\li Number of bytes read out of `src', or -1 (with errno set). |
507 | * | |
b43b13ac | 508 | * note: |
cd5743fd | 509 | *\li Root domain returns as "." not "". |
b43b13ac UD |
510 | */ |
511 | int | |
512 | ns_name_uncompress(const u_char *msg, const u_char *eom, const u_char *src, | |
513 | char *dst, size_t dstsiz) | |
514 | { | |
515 | u_char tmp[NS_MAXCDNAME]; | |
516 | int n; | |
5ae19a50 | 517 | |
b43b13ac UD |
518 | if ((n = ns_name_unpack(msg, eom, src, tmp, sizeof tmp)) == -1) |
519 | return (-1); | |
520 | if (ns_name_ntop(tmp, dst, dstsiz) == -1) | |
521 | return (-1); | |
522 | return (n); | |
523 | } | |
cd5743fd | 524 | libresolv_hidden_def (ns_name_uncompress) |
b43b13ac | 525 | |
cd5743fd | 526 | /*% |
b43b13ac | 527 | * Compress a domain name into wire format, using compression pointers. |
cd5743fd | 528 | * |
b43b13ac | 529 | * return: |
cd5743fd UD |
530 | *\li Number of bytes consumed in `dst' or -1 (with errno set). |
531 | * | |
b43b13ac | 532 | * notes: |
cd5743fd UD |
533 | *\li 'dnptrs' is an array of pointers to previous compressed names. |
534 | *\li dnptrs[0] is a pointer to the beginning of the message. | |
535 | *\li The list ends with NULL. 'lastdnptr' is a pointer to the end of the | |
b43b13ac UD |
536 | * array pointed to by 'dnptrs'. Side effect is to update the list of |
537 | * pointers for labels inserted into the message as we compress the name. | |
cd5743fd | 538 | *\li If 'dnptr' is NULL, we don't try to compress names. If 'lastdnptr' |
b43b13ac UD |
539 | * is NULL, we don't update the list. |
540 | */ | |
541 | int | |
542 | ns_name_compress(const char *src, u_char *dst, size_t dstsiz, | |
543 | const u_char **dnptrs, const u_char **lastdnptr) | |
544 | { | |
545 | u_char tmp[NS_MAXCDNAME]; | |
546 | ||
547 | if (ns_name_pton(src, tmp, sizeof tmp) == -1) | |
548 | return (-1); | |
549 | return (ns_name_pack(tmp, dst, dstsiz, dnptrs, lastdnptr)); | |
550 | } | |
cd5743fd | 551 | libresolv_hidden_def (ns_name_compress) |
b43b13ac | 552 | |
cd5743fd | 553 | /*% |
e685e07d UD |
554 | * Reset dnptrs so that there are no active references to pointers at or |
555 | * after src. | |
556 | */ | |
557 | void | |
558 | ns_name_rollback(const u_char *src, const u_char **dnptrs, | |
559 | const u_char **lastdnptr) | |
560 | { | |
561 | while (dnptrs < lastdnptr && *dnptrs != NULL) { | |
562 | if (*dnptrs >= src) { | |
563 | *dnptrs = NULL; | |
564 | break; | |
565 | } | |
566 | dnptrs++; | |
567 | } | |
568 | } | |
569 | ||
cd5743fd | 570 | /*% |
b43b13ac | 571 | * Advance *ptrptr to skip over the compressed name it points at. |
cd5743fd | 572 | * |
b43b13ac | 573 | * return: |
cd5743fd | 574 | *\li 0 on success, -1 (with errno set) on failure. |
b43b13ac UD |
575 | */ |
576 | int | |
cd5743fd UD |
577 | ns_name_skip(const u_char **ptrptr, const u_char *eom) |
578 | { | |
b43b13ac UD |
579 | const u_char *cp; |
580 | u_int n; | |
581 | ||
582 | cp = *ptrptr; | |
583 | while (cp < eom && (n = *cp++) != 0) { | |
584 | /* Check for indirection. */ | |
585 | switch (n & NS_CMPRSFLGS) { | |
cd5743fd | 586 | case 0: /*%< normal case, n == len */ |
b43b13ac UD |
587 | cp += n; |
588 | continue; | |
cd5743fd | 589 | case NS_CMPRSFLGS: /*%< indirection */ |
b43b13ac UD |
590 | cp++; |
591 | break; | |
cd5743fd | 592 | default: /*%< illegal type */ |
b43b13ac UD |
593 | __set_errno (EMSGSIZE); |
594 | return (-1); | |
595 | } | |
596 | break; | |
597 | } | |
598 | if (cp > eom) { | |
599 | __set_errno (EMSGSIZE); | |
600 | return (-1); | |
601 | } | |
602 | *ptrptr = cp; | |
603 | return (0); | |
604 | } | |
cd5743fd | 605 | libresolv_hidden_def (ns_name_skip) |
b43b13ac UD |
606 | |
607 | /* Private. */ | |
608 | ||
cd5743fd | 609 | /*% |
b43b13ac | 610 | * Thinking in noninternationalized USASCII (per the DNS spec), |
382466e0 | 611 | * is this character special ("in need of quoting") ? |
cd5743fd | 612 | * |
b43b13ac | 613 | * return: |
cd5743fd | 614 | *\li boolean. |
b43b13ac UD |
615 | */ |
616 | static int | |
617 | special(int ch) { | |
618 | switch (ch) { | |
cd5743fd UD |
619 | case 0x22: /*%< '"' */ |
620 | case 0x2E: /*%< '.' */ | |
621 | case 0x3B: /*%< ';' */ | |
622 | case 0x5C: /*%< '\\' */ | |
623 | case 0x28: /*%< '(' */ | |
624 | case 0x29: /*%< ')' */ | |
b43b13ac | 625 | /* Special modifiers in zone files. */ |
cd5743fd UD |
626 | case 0x40: /*%< '@' */ |
627 | case 0x24: /*%< '$' */ | |
b43b13ac UD |
628 | return (1); |
629 | default: | |
630 | return (0); | |
631 | } | |
632 | } | |
633 | ||
cd5743fd | 634 | /*% |
b43b13ac UD |
635 | * Thinking in noninternationalized USASCII (per the DNS spec), |
636 | * is this character visible and not a space when printed ? | |
cd5743fd | 637 | * |
b43b13ac | 638 | * return: |
cd5743fd | 639 | *\li boolean. |
b43b13ac UD |
640 | */ |
641 | static int | |
642 | printable(int ch) { | |
643 | return (ch > 0x20 && ch < 0x7f); | |
644 | } | |
645 | ||
cd5743fd | 646 | /*% |
b43b13ac UD |
647 | * Thinking in noninternationalized USASCII (per the DNS spec), |
648 | * convert this character to lower case if it's upper case. | |
649 | */ | |
650 | static int | |
651 | mklower(int ch) { | |
652 | if (ch >= 0x41 && ch <= 0x5A) | |
653 | return (ch + 0x20); | |
654 | return (ch); | |
655 | } | |
656 | ||
cd5743fd | 657 | /*% |
b43b13ac | 658 | * Search for the counted-label name in an array of compressed names. |
cd5743fd | 659 | * |
b43b13ac | 660 | * return: |
cd5743fd UD |
661 | *\li offset from msg if found, or -1. |
662 | * | |
b43b13ac | 663 | * notes: |
cd5743fd UD |
664 | *\li dnptrs is the pointer to the first name on the list, |
665 | *\li not the pointer to the start of the message. | |
b43b13ac UD |
666 | */ |
667 | static int | |
668 | dn_find(const u_char *domain, const u_char *msg, | |
669 | const u_char * const *dnptrs, | |
670 | const u_char * const *lastdnptr) | |
671 | { | |
672 | const u_char *dn, *cp, *sp; | |
673 | const u_char * const *cpp; | |
674 | u_int n; | |
675 | ||
676 | for (cpp = dnptrs; cpp < lastdnptr; cpp++) { | |
e685e07d UD |
677 | sp = *cpp; |
678 | /* | |
679 | * terminate search on: | |
680 | * root label | |
681 | * compression pointer | |
682 | * unusable offset | |
683 | */ | |
684 | while (*sp != 0 && (*sp & NS_CMPRSFLGS) == 0 && | |
685 | (sp - msg) < 0x4000) { | |
686 | dn = domain; | |
687 | cp = sp; | |
688 | while ((n = *cp++) != 0) { | |
689 | /* | |
690 | * check for indirection | |
691 | */ | |
692 | switch (n & NS_CMPRSFLGS) { | |
cd5743fd UD |
693 | case 0: /*%< normal case, n == len */ |
694 | n = labellen(cp - 1); /*%< XXX */ | |
e685e07d | 695 | if (n != *dn++) |
b43b13ac | 696 | goto next; |
cd5743fd | 697 | |
e685e07d UD |
698 | for ((void)NULL; n > 0; n--) |
699 | if (mklower(*dn++) != | |
700 | mklower(*cp++)) | |
701 | goto next; | |
702 | /* Is next root for both ? */ | |
703 | if (*dn == '\0' && *cp == '\0') | |
704 | return (sp - msg); | |
705 | if (*dn) | |
706 | continue; | |
707 | goto next; | |
cd5743fd | 708 | case NS_CMPRSFLGS: /*%< indirection */ |
e685e07d UD |
709 | cp = msg + (((n & 0x3f) << 8) | *cp); |
710 | break; | |
711 | ||
cd5743fd | 712 | default: /*%< illegal type */ |
e685e07d UD |
713 | __set_errno (EMSGSIZE); |
714 | return (-1); | |
715 | } | |
b43b13ac | 716 | } |
cd5743fd | 717 | next: ; |
e685e07d | 718 | sp += *sp + 1; |
b43b13ac | 719 | } |
b43b13ac UD |
720 | } |
721 | __set_errno (ENOENT); | |
722 | return (-1); | |
723 | } | |
cd5743fd | 724 | |
5140d036 FW |
725 | /* Return the length of the encoded label starting at LP, or -1 for |
726 | compression references and extended label types. */ | |
cd5743fd | 727 | static int |
5140d036 | 728 | labellen (const unsigned char *lp) |
cd5743fd | 729 | { |
5140d036 FW |
730 | if (*lp <= 63) |
731 | return *lp; | |
732 | return -1; | |
cd5743fd UD |
733 | } |
734 | ||
735 | /*! \file */ |