]> git.ipfire.org Git - thirdparty/glibc.git/blob - resolv/ns_name.c
Update.
[thirdparty/glibc.git] / resolv / ns_name.c
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
33 /* Data. */
34
35 static const char digits[] = "0123456789";
36
37 /* Forward. */
38
39 static int special(int);
40 static int printable(int);
41 static int dn_find(const u_char *, const u_char *,
42 const u_char * const *,
43 const u_char * const *);
44
45 /* Public. */
46
47 /*
48 * ns_name_ntop(src, dst, dstsiz)
49 * Convert an encoded domain name to printable ascii as per RFC1035.
50 * return:
51 * Number of bytes written to buffer, or -1 (with errno set)
52 * notes:
53 * The root is returned as "."
54 * All other domains are returned in non absolute form
55 */
56 int
57 ns_name_ntop(const u_char *src, char *dst, size_t dstsiz) {
58 const u_char *cp;
59 char *dn, *eom;
60 u_char c;
61 u_int n;
62
63 cp = src;
64 dn = dst;
65 eom = dst + dstsiz;
66
67 while ((n = *cp++) != 0) {
68 if ((n & NS_CMPRSFLGS) != 0 && n != 0x41) {
69 /* Some kind of compression pointer. */
70 __set_errno (EMSGSIZE);
71 return (-1);
72 }
73 if (dn != dst) {
74 if (dn >= eom) {
75 __set_errno (EMSGSIZE);
76 return (-1);
77 }
78 *dn++ = '.';
79 }
80
81 if (n == 0x41) {
82 n = *cp++ / 8;
83 if (dn + n * 2 + 4 >= eom) {
84 __set_errno (EMSGSIZE);
85 return (-1);
86 }
87 *dn++ = '\\';
88 *dn++ = '[';
89 *dn++ = 'x';
90
91 while (n-- > 0) {
92 c = *cp++;
93 unsigned u = c >> 4;
94 *dn++ = u > 9 ? 'a' + u - 10 : '0' + u;
95 u = c & 0xf;
96 *dn++ = u > 9 ? 'a' + u - 10 : '0' + u;
97 }
98
99 *dn++ = ']';
100 continue;
101 }
102
103 if (dn + n >= eom) {
104 __set_errno (EMSGSIZE);
105 return (-1);
106 }
107 for ((void)NULL; n > 0; n--) {
108 c = *cp++;
109 if (special(c)) {
110 if (dn + 1 >= eom) {
111 __set_errno (EMSGSIZE);
112 return (-1);
113 }
114 *dn++ = '\\';
115 *dn++ = (char)c;
116 } else if (!printable(c)) {
117 if (dn + 3 >= eom) {
118 __set_errno (EMSGSIZE);
119 return (-1);
120 }
121 *dn++ = '\\';
122 *dn++ = digits[c / 100];
123 *dn++ = digits[(c % 100) / 10];
124 *dn++ = digits[c % 10];
125 } else {
126 if (dn >= eom) {
127 __set_errno (EMSGSIZE);
128 return (-1);
129 }
130 *dn++ = (char)c;
131 }
132 }
133 }
134 if (dn == dst) {
135 if (dn >= eom) {
136 __set_errno (EMSGSIZE);
137 return (-1);
138 }
139 *dn++ = '.';
140 }
141 if (dn >= eom) {
142 __set_errno (EMSGSIZE);
143 return (-1);
144 }
145 *dn++ = '\0';
146 return (dn - dst);
147 }
148
149 /*
150 * ns_name_pton(src, dst, dstsiz)
151 * Convert a ascii string into an encoded domain name as per RFC1035.
152 * return:
153 * -1 if it fails
154 * 1 if string was fully qualified
155 * 0 is string was not fully qualified
156 * notes:
157 * Enforces label and domain length limits.
158 */
159
160 int
161 ns_name_pton(const char *src, u_char *dst, size_t dstsiz) {
162 u_char *label, *bp, *eom;
163 int c, n, escaped;
164 char *cp;
165
166 escaped = 0;
167 bp = dst;
168 eom = dst + dstsiz;
169 label = bp++;
170
171 while ((c = *src++) != 0) {
172 if (escaped) {
173 if ((cp = strchr(digits, c)) != NULL) {
174 n = (cp - digits) * 100;
175 if ((c = *src++) == 0 ||
176 (cp = strchr(digits, c)) == NULL) {
177 __set_errno (EMSGSIZE);
178 return (-1);
179 }
180 n += (cp - digits) * 10;
181 if ((c = *src++) == 0 ||
182 (cp = strchr(digits, c)) == NULL) {
183 __set_errno (EMSGSIZE);
184 return (-1);
185 }
186 n += (cp - digits);
187 if (n > 255) {
188 __set_errno (EMSGSIZE);
189 return (-1);
190 }
191 c = n;
192 } else if (c == '[' && label == bp - 1 && *src == 'x') {
193 /* Theoretically we would have to handle \[o
194 as well but we do not since we do not need
195 it internally. */
196 *label = 0x41;
197 label = bp++;
198 /* Another simplification: always assume
199 128 bit number. */
200 if (bp + 16 >= eom) {
201 __set_errno (EMSGSIZE);
202 return (-1);
203 }
204 ++src;
205 while (isxdigit (*src)) {
206 n = *src > '9' ? *src - 'a' + 10 : *src - '0';
207 ++src;
208 if (! isxdigit(*src)) {
209 __set_errno (EMSGSIZE);
210 return (-1);
211 }
212 n <<= 4;
213 n += *src > '9' ? *src - 'a' + 10 : *src - '0';
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
427 /*
428 * ns_name_pack(src, dst, dstsiz, dnptrs, lastdnptr)
429 * Pack domain name 'domain' into 'comp_dn'.
430 * return:
431 * Size of the compressed name, or -1.
432 * notes:
433 * 'dnptrs' is an array of pointers to previous compressed names.
434 * dnptrs[0] is a pointer to the beginning of the message. The array
435 * ends with NULL.
436 * 'lastdnptr' is a pointer to the end of the array pointed to
437 * by 'dnptrs'.
438 * Side effects:
439 * The list of pointers in dnptrs is updated for labels inserted into
440 * the message as we compress the name. If 'dnptr' is NULL, we don't
441 * try to compress names. If 'lastdnptr' is NULL, we don't update the
442 * list.
443 */
444 int
445 ns_name_pack(const u_char *src, u_char *dst, int dstsiz,
446 const u_char **dnptrs, const u_char **lastdnptr)
447 {
448 u_char *dstp;
449 const u_char **cpp, **lpp, *eob, *msg;
450 const u_char *srcp;
451 int n, l, first = 1;
452
453 srcp = src;
454 dstp = dst;
455 eob = dstp + dstsiz;
456 lpp = cpp = NULL;
457 if (dnptrs != NULL) {
458 if ((msg = *dnptrs++) != NULL) {
459 for (cpp = dnptrs; *cpp != NULL; cpp++)
460 (void)NULL;
461 lpp = cpp; /* end of list to search */
462 }
463 } else
464 msg = NULL;
465
466 /* make sure the domain we are about to add is legal */
467 l = 0;
468 do {
469 n = *srcp;
470 if ((n & NS_CMPRSFLGS) != 0 && n != 0x41) {
471 __set_errno (EMSGSIZE);
472 return (-1);
473 }
474 if (n == 0x41)
475 n = *++srcp / 8;
476 l += n + 1;
477 if (l > MAXCDNAME) {
478 __set_errno (EMSGSIZE);
479 return (-1);
480 }
481 srcp += n + 1;
482 } while (n != 0);
483
484 /* from here on we need to reset compression pointer array on error */
485 srcp = src;
486 do {
487 /* Look to see if we can use pointers. */
488 n = *srcp;
489 if (n != 0 && n != 0x41 && msg != NULL) {
490 l = dn_find(srcp, msg, (const u_char * const *)dnptrs,
491 (const u_char * const *)lpp);
492 if (l >= 0) {
493 if (dstp + 1 >= eob) {
494 goto cleanup;
495 }
496 *dstp++ = (l >> 8) | NS_CMPRSFLGS;
497 *dstp++ = l % 256;
498 return (dstp - dst);
499 }
500 /* Not found, save it. */
501 if (lastdnptr != NULL && cpp < lastdnptr - 1 &&
502 (dstp - msg) < 0x4000 && first) {
503 *cpp++ = dstp;
504 *cpp = NULL;
505 first = 0;
506 }
507 }
508 /* copy label to buffer */
509 if ((n & NS_CMPRSFLGS) != 0 && n != 0x41) { /* Should not happen. */
510 goto cleanup;
511 }
512 if (n == 0x41) {
513 n = *++srcp / 8;
514 if (dstp + 1 >= eob)
515 goto cleanup;
516 *dstp++ = 0x41;
517 }
518 if (dstp + 1 + n >= eob) {
519 goto cleanup;
520 }
521 memcpy(dstp, srcp, n + 1);
522 srcp += n + 1;
523 dstp += n + 1;
524 } while (n != 0);
525
526 if (dstp > eob) {
527 cleanup:
528 if (msg != NULL)
529 *lpp = NULL;
530 __set_errno (EMSGSIZE);
531 return (-1);
532 }
533 return (dstp - dst);
534 }
535
536 /*
537 * ns_name_uncompress(msg, eom, src, dst, dstsiz)
538 * Expand compressed domain name to presentation format.
539 * return:
540 * Number of bytes read out of `src', or -1 (with errno set).
541 * note:
542 * Root domain returns as "." not "".
543 */
544 int
545 ns_name_uncompress(const u_char *msg, const u_char *eom, const u_char *src,
546 char *dst, size_t dstsiz)
547 {
548 u_char tmp[NS_MAXCDNAME];
549 int n;
550
551 if ((n = ns_name_unpack(msg, eom, src, tmp, sizeof tmp)) == -1)
552 return (-1);
553 if (ns_name_ntop(tmp, dst, dstsiz) == -1)
554 return (-1);
555 return (n);
556 }
557
558 /*
559 * ns_name_compress(src, dst, dstsiz, dnptrs, lastdnptr)
560 * Compress a domain name into wire format, using compression pointers.
561 * return:
562 * Number of bytes consumed in `dst' or -1 (with errno set).
563 * notes:
564 * 'dnptrs' is an array of pointers to previous compressed names.
565 * dnptrs[0] is a pointer to the beginning of the message.
566 * The list ends with NULL. 'lastdnptr' is a pointer to the end of the
567 * array pointed to by 'dnptrs'. Side effect is to update the list of
568 * pointers for labels inserted into the message as we compress the name.
569 * If 'dnptr' is NULL, we don't try to compress names. If 'lastdnptr'
570 * is NULL, we don't update the list.
571 */
572 int
573 ns_name_compress(const char *src, u_char *dst, size_t dstsiz,
574 const u_char **dnptrs, const u_char **lastdnptr)
575 {
576 u_char tmp[NS_MAXCDNAME];
577
578 if (ns_name_pton(src, tmp, sizeof tmp) == -1)
579 return (-1);
580 return (ns_name_pack(tmp, dst, dstsiz, dnptrs, lastdnptr));
581 }
582
583 /*
584 * Reset dnptrs so that there are no active references to pointers at or
585 * after src.
586 */
587 void
588 ns_name_rollback(const u_char *src, const u_char **dnptrs,
589 const u_char **lastdnptr)
590 {
591 while (dnptrs < lastdnptr && *dnptrs != NULL) {
592 if (*dnptrs >= src) {
593 *dnptrs = NULL;
594 break;
595 }
596 dnptrs++;
597 }
598 }
599
600 /*
601 * ns_name_skip(ptrptr, eom)
602 * Advance *ptrptr to skip over the compressed name it points at.
603 * return:
604 * 0 on success, -1 (with errno set) on failure.
605 */
606 int
607 ns_name_skip(const u_char **ptrptr, const u_char *eom) {
608 const u_char *cp;
609 u_int n;
610
611 cp = *ptrptr;
612 while (cp < eom && (n = *cp++) != 0) {
613 /* Check for indirection. */
614 switch (n & NS_CMPRSFLGS) {
615 case 0: /* normal case, n == len */
616 cp += n;
617 continue;
618 case NS_CMPRSFLGS: /* indirection */
619 cp++;
620 break;
621 default: /* illegal type */
622 __set_errno (EMSGSIZE);
623 return (-1);
624 }
625 break;
626 }
627 if (cp > eom) {
628 __set_errno (EMSGSIZE);
629 return (-1);
630 }
631 *ptrptr = cp;
632 return (0);
633 }
634
635 /* Private. */
636
637 /*
638 * special(ch)
639 * Thinking in noninternationalized USASCII (per the DNS spec),
640 * is this characted special ("in need of quoting") ?
641 * return:
642 * boolean.
643 */
644 static int
645 special(int ch) {
646 switch (ch) {
647 case 0x22: /* '"' */
648 case 0x2E: /* '.' */
649 case 0x3B: /* ';' */
650 case 0x5C: /* '\\' */
651 /* Special modifiers in zone files. */
652 case 0x40: /* '@' */
653 case 0x24: /* '$' */
654 return (1);
655 default:
656 return (0);
657 }
658 }
659
660 /*
661 * printable(ch)
662 * Thinking in noninternationalized USASCII (per the DNS spec),
663 * is this character visible and not a space when printed ?
664 * return:
665 * boolean.
666 */
667 static int
668 printable(int ch) {
669 return (ch > 0x20 && ch < 0x7f);
670 }
671
672 /*
673 * Thinking in noninternationalized USASCII (per the DNS spec),
674 * convert this character to lower case if it's upper case.
675 */
676 static int
677 mklower(int ch) {
678 if (ch >= 0x41 && ch <= 0x5A)
679 return (ch + 0x20);
680 return (ch);
681 }
682
683 /*
684 * dn_find(domain, msg, dnptrs, lastdnptr)
685 * Search for the counted-label name in an array of compressed names.
686 * return:
687 * offset from msg if found, or -1.
688 * notes:
689 * dnptrs is the pointer to the first name on the list,
690 * not the pointer to the start of the message.
691 */
692 static int
693 dn_find(const u_char *domain, const u_char *msg,
694 const u_char * const *dnptrs,
695 const u_char * const *lastdnptr)
696 {
697 const u_char *dn, *cp, *sp;
698 const u_char * const *cpp;
699 u_int n;
700
701 for (cpp = dnptrs; cpp < lastdnptr; cpp++) {
702 sp = *cpp;
703 /*
704 * terminate search on:
705 * root label
706 * compression pointer
707 * unusable offset
708 */
709 while (*sp != 0 && (*sp & NS_CMPRSFLGS) == 0 &&
710 (sp - msg) < 0x4000) {
711 dn = domain;
712 cp = sp;
713 while ((n = *cp++) != 0) {
714 /*
715 * check for indirection
716 */
717 switch (n & NS_CMPRSFLGS) {
718 case 0: /* normal case, n == len */
719 if (n != *dn++)
720 goto next;
721 for ((void)NULL; n > 0; n--)
722 if (mklower(*dn++) !=
723 mklower(*cp++))
724 goto next;
725 /* Is next root for both ? */
726 if (*dn == '\0' && *cp == '\0')
727 return (sp - msg);
728 if (*dn)
729 continue;
730 goto next;
731
732 case NS_CMPRSFLGS: /* indirection */
733 cp = msg + (((n & 0x3f) << 8) | *cp);
734 break;
735
736 default: /* illegal type */
737 __set_errno (EMSGSIZE);
738 return (-1);
739 }
740 }
741 next:
742 sp += *sp + 1;
743 }
744 }
745 __set_errno (ENOENT);
746 return (-1);
747 }