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