1/*
2 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
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 *
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.
16 */
17
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>
27#include <stdlib.h>
28#include <limits.h>
29
30# define SPRINTF(x) ((size_t)sprintf x)
31
32/* Data. */
33
34static const char digits[] = "0123456789";
35
36/* Forward. */
37
38static int special(int);
39static int printable(int);
40static int dn_find(const u_char *, const u_char *,
41 const u_char * const *,
42 const u_char * const *);
43static int labellen(const u_char *);
44
45/* Public. */
46
47/*%
48 * Convert an encoded domain name to printable ascii as per RFC1035.
49
50 * return:
51 *\li Number of bytes written to buffer, or -1 (with errno set)
52 *
53 * notes:
54 *\li The root is returned as "."
55 *\li All other domains are returned in non absolute form
56 */
57int
58ns_name_ntop(const u_char *src, char *dst, size_t dstsiz)
59{
60 const u_char *cp;
61 char *dn, *eom;
62 u_char c;
63 u_int n;
64 int l;
65
66 cp = src;
67 dn = dst;
68 eom = dst + dstsiz;
69
70 while ((n = *cp++) != 0) {
71 if ((n & NS_CMPRSFLGS) == NS_CMPRSFLGS) {
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 }
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 }
91 for ((void)NULL; l > 0; l--) {
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}
132libresolv_hidden_def (ns_name_ntop)
133strong_alias (ns_name_ntop, __ns_name_ntop)
134
135/*%
136 * Convert an ascii string into an encoded domain name as per RFC1035.
137 *
138 * return:
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 *
144 * notes:
145 *\li Enforces label and domain length limits.
146 */
147
148int
149ns_name_pton(const char *src, u_char *dst, size_t dstsiz)
150{
151 u_char *label, *bp, *eom;
152 int c, n, escaped;
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) {
162 if ((cp = strchr(digits, c)) != NULL) {
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);
188 if ((c & NS_CMPRSFLGS) != 0) { /*%< Label too big. */
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 }
225 if (escaped) {
226 /* Trailing backslash. */
227 __set_errno (EMSGSIZE);
228 return -1;
229 }
230 c = (bp - label - 1);
231 if ((c & NS_CMPRSFLGS) != 0) { /*%< Label too big. */
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 }
247 if ((bp - dst) > MAXCDNAME) { /*%< src too big */
248 __set_errno (EMSGSIZE);
249 return (-1);
250 }
251 return (0);
252}
253libresolv_hidden_def (ns_name_pton)
254
255/*%
256 * Convert a network strings labels into all lowercase.
257 *
258 * return:
259 *\li Number of bytes written to buffer, or -1 (with errno set)
260 *
261 * notes:
262 *\li Enforces label and domain length limits.
263 */
264
265int
266ns_name_ntol(const u_char *src, u_char *dst, size_t dstsiz)
267{
268 const u_char *cp;
269 u_char *dn, *eom;
270 u_char c;
271 u_int n;
272 int l;
273
274 cp = src;
275 dn = dst;
276 eom = dst + dstsiz;
277
278 if (dn >= eom) {
279 __set_errno (EMSGSIZE);
280 return (-1);
281 }
282 while ((n = *cp++) != 0) {
283 if ((n & NS_CMPRSFLGS) == NS_CMPRSFLGS) {
284 /* Some kind of compression pointer. */
285 __set_errno (EMSGSIZE);
286 return (-1);
287 }
288 *dn++ = n;
289 if ((l = labellen(cp - 1)) < 0) {
290 __set_errno (EMSGSIZE);
291 return (-1);
292 }
293 if (dn + l >= eom) {
294 __set_errno (EMSGSIZE);
295 return (-1);
296 }
297 for ((void)NULL; l > 0; l--) {
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
309/*%
310 * Unpack a domain name from a message, source may be compressed.
311 *
312 * return:
313 *\li -1 if it fails, or consumed octets if it succeeds.
314 */
315int
316ns_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;
321 int n, len, checked, l;
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. */
338 if ((l = labellen(srcp - 1)) < 0) {
339 __set_errno (EMSGSIZE);
340 return(-1);
341 }
342 if (dstp + l + 1 >= dstlim || srcp + l >= eom) {
343 __set_errno (EMSGSIZE);
344 return (-1);
345 }
346 checked += l + 1;
347 *dstp++ = n;
348 memcpy(dstp, srcp, l);
349 dstp += l;
350 srcp += l;
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));
361 if (srcp < msg || srcp >= eom) { /*%< Out of range. */
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);
379 return (-1); /*%< flag error */
380 }
381 }
382 *dstp = '\0';
383 if (len < 0)
384 len = srcp - src;
385 return (len);
386}
387libresolv_hidden_def (ns_name_unpack)
388strong_alias (ns_name_unpack, __ns_name_unpack)
389
390/*%
391 * Pack domain name 'domain' into 'comp_dn'.
392 *
393 * return:
394 *\li Size of the compressed name, or -1.
395 *
396 * notes:
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
399 * ends with NULL.
400 *\li 'lastdnptr' is a pointer to the end of the array pointed to
401 * by 'dnptrs'.
402 *
403 * Side effects:
404 *\li The list of pointers in dnptrs is updated for labels inserted into
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 */
409int
410ns_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;
416 int n, l, first = 1;
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;
426 lpp = cpp; /*%< end of list to search */
427 }
428 } else
429 msg = NULL;
430
431 /* make sure the domain we are about to add is legal */
432 l = 0;
433 do {
434 int l0;
435
436 n = *srcp;
437 if ((n & NS_CMPRSFLGS) == NS_CMPRSFLGS) {
438 __set_errno (EMSGSIZE);
439 return (-1);
440 }
441 if ((l0 = labellen(srcp)) < 0) {
442 __set_errno (EINVAL);
443 return(-1);
444 }
445 l += l0 + 1;
446 if (l > MAXCDNAME) {
447 __set_errno (EMSGSIZE);
448 return (-1);
449 }
450 srcp += l0 + 1;
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;
458 if (n != 0 && msg != NULL) {
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 &&
471 (dstp - msg) < 0x4000 && first) {
472 *cpp++ = dstp;
473 *cpp = NULL;
474 first = 0;
475 }
476 }
477 /* copy label to buffer */
478 if ((n & NS_CMPRSFLGS) == NS_CMPRSFLGS) {
479 /* Should not happen. */
480 goto cleanup;
481 }
482 n = labellen(srcp);
483 if (n + 1 > eob - dstp) {
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) {
492cleanup:
493 if (msg != NULL)
494 *lpp = NULL;
495 __set_errno (EMSGSIZE);
496 return (-1);
497 }
498 return (dstp - dst);
499}
500libresolv_hidden_def (ns_name_pack)
501
502/*%
503 * Expand compressed domain name to presentation format.
504 *
505 * return:
506 *\li Number of bytes read out of `src', or -1 (with errno set).
507 *
508 * note:
509 *\li Root domain returns as "." not "".
510 */
511int
512ns_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;
517
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}
524libresolv_hidden_def (ns_name_uncompress)
525
526/*%
527 * Compress a domain name into wire format, using compression pointers.
528 *
529 * return:
530 *\li Number of bytes consumed in `dst' or -1 (with errno set).
531 *
532 * notes:
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
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.
538 *\li If 'dnptr' is NULL, we don't try to compress names. If 'lastdnptr'
539 * is NULL, we don't update the list.
540 */
541int
542ns_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}
551libresolv_hidden_def (ns_name_compress)
552
553/*%
554 * Reset dnptrs so that there are no active references to pointers at or
555 * after src.
556 */
557void
558ns_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
570/*%
571 * Advance *ptrptr to skip over the compressed name it points at.
572 *
573 * return:
574 *\li 0 on success, -1 (with errno set) on failure.
575 */
576int
577ns_name_skip(const u_char **ptrptr, const u_char *eom)
578{
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) {
586 case 0: /*%< normal case, n == len */
587 cp += n;
588 continue;
589 case NS_CMPRSFLGS: /*%< indirection */
590 cp++;
591 break;
592 default: /*%< illegal type */
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}
605libresolv_hidden_def (ns_name_skip)
606
607/* Private. */
608
609/*%
610 * Thinking in noninternationalized USASCII (per the DNS spec),
611 * is this character special ("in need of quoting") ?
612 *
613 * return:
614 *\li boolean.
615 */
616static int
617special(int ch) {
618 switch (ch) {
619 case 0x22: /*%< '"' */
620 case 0x2E: /*%< '.' */
621 case 0x3B: /*%< ';' */
622 case 0x5C: /*%< '\\' */
623 case 0x28: /*%< '(' */
624 case 0x29: /*%< ')' */
625 /* Special modifiers in zone files. */
626 case 0x40: /*%< '@' */
627 case 0x24: /*%< '$' */
628 return (1);
629 default:
630 return (0);
631 }
632}
633
634/*%
635 * Thinking in noninternationalized USASCII (per the DNS spec),
636 * is this character visible and not a space when printed ?
637 *
638 * return:
639 *\li boolean.
640 */
641static int
642printable(int ch) {
643 return (ch > 0x20 && ch < 0x7f);
644}
645
646/*%
647 * Thinking in noninternationalized USASCII (per the DNS spec),
648 * convert this character to lower case if it's upper case.
649 */
650static int
651mklower(int ch) {
652 if (ch >= 0x41 && ch <= 0x5A)
653 return (ch + 0x20);
654 return (ch);
655}
656
657/*%
658 * Search for the counted-label name in an array of compressed names.
659 *
660 * return:
661 *\li offset from msg if found, or -1.
662 *
663 * notes:
664 *\li dnptrs is the pointer to the first name on the list,
665 *\li not the pointer to the start of the message.
666 */
667static int
668dn_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++) {
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) {
693 case 0: /*%< normal case, n == len */
694 n = labellen(cp - 1); /*%< XXX */
695 if (n != *dn++)
696 goto next;
697
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;
708 case NS_CMPRSFLGS: /*%< indirection */
709 cp = msg + (((n & 0x3f) << 8) | *cp);
710 break;
711
712 default: /*%< illegal type */
713 __set_errno (EMSGSIZE);
714 return (-1);
715 }
716 }
717 next: ;
718 sp += *sp + 1;
719 }
720 }
721 __set_errno (ENOENT);
722 return (-1);
723}
724
725/* Return the length of the encoded label starting at LP, or -1 for
726 compression references and extended label types. */
727static int
728labellen (const unsigned char *lp)
729{
730 if (*lp <= 63)
731 return *lp;
732 return -1;
733}
734
735/*! \file */
736