blob: 12bf029023bf0aa8b4126798c0e2d93a5d05ed07 [file] [log] [blame]
Calin Juravlefcb502e2014-03-05 15:06:52 +00001/* $NetBSD: ns_name.c,v 1.9 2012/03/13 21:13:39 christos Exp $ */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002
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#include <sys/cdefs.h>
21#ifndef lint
22#ifdef notdef
Calin Juravlefcb502e2014-03-05 15:06:52 +000023static const char rcsid[] = "Id: ns_name.c,v 1.11 2009/01/23 19:59:16 each Exp";
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080024#else
Calin Juravlefcb502e2014-03-05 15:06:52 +000025__RCSID("$NetBSD: ns_name.c,v 1.9 2012/03/13 21:13:39 christos Exp $");
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080026#endif
27#endif
28
29#include <sys/types.h>
30
31#include <netinet/in.h>
Calin Juravle569fb982014-03-04 15:01:29 +000032#include <arpa/nameser.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080033
Calin Juravlefcb502e2014-03-05 15:06:52 +000034#include <assert.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080035#include <errno.h>
36#ifdef ANDROID_CHANGES
37#include "resolv_private.h"
38#else
39#include <resolv.h>
40#endif
41#include <string.h>
42#include <ctype.h>
43#include <stdlib.h>
44#include <limits.h>
45
46#ifdef SPRINTF_CHAR
Calin Juravlefcb502e2014-03-05 15:06:52 +000047# define SPRINTF(x) ((int)strlen(sprintf/**/x))
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080048#else
Calin Juravlefcb502e2014-03-05 15:06:52 +000049# define SPRINTF(x) (sprintf x)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080050#endif
51
52#define NS_TYPE_ELT 0x40 /* EDNS0 extended label type */
53#define DNS_LABELTYPE_BITSTRING 0x41
54
55/* Data. */
56
57static const char digits[] = "0123456789";
58
59static const char digitvalue[256] = {
60 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*16*/
61 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*32*/
62 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*48*/
63 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, /*64*/
64 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*80*/
65 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*96*/
66 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*112*/
67 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*128*/
68 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
69 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
70 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
71 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
72 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
73 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
74 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
75 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*256*/
76};
77
78/* Forward. */
79
80static int special(int);
81static int printable(int);
82static int dn_find(const u_char *, const u_char *,
83 const u_char * const *,
84 const u_char * const *);
85static int encode_bitsring(const char **, const char *,
86 unsigned char **, unsigned char **,
87 unsigned const char *);
88static int labellen(const u_char *);
89static int decode_bitstring(const unsigned char **,
90 char *, const char *);
91
92/* Public. */
93
94/*
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080095 * Convert an encoded domain name to printable ascii as per RFC1035.
96 * return:
97 * Number of bytes written to buffer, or -1 (with errno set)
Calin Juravlefcb502e2014-03-05 15:06:52 +000098 *
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080099 * notes:
100 * The root is returned as "."
101 * All other domains are returned in non absolute form
102 */
103int
104ns_name_ntop(const u_char *src, char *dst, size_t dstsiz)
105{
106 const u_char *cp;
107 char *dn, *eom;
108 u_char c;
109 u_int n;
110 int l;
111
112 cp = src;
113 dn = dst;
114 eom = dst + dstsiz;
115
116 while ((n = *cp++) != 0) {
117 if ((n & NS_CMPRSFLGS) == NS_CMPRSFLGS) {
118 /* Some kind of compression pointer. */
119 errno = EMSGSIZE;
120 return (-1);
121 }
122 if (dn != dst) {
123 if (dn >= eom) {
124 errno = EMSGSIZE;
125 return (-1);
126 }
127 *dn++ = '.';
128 }
129 if ((l = labellen(cp - 1)) < 0) {
130 errno = EMSGSIZE; /* XXX */
131 return(-1);
132 }
133 if (dn + l >= eom) {
134 errno = EMSGSIZE;
135 return (-1);
136 }
137 if ((n & NS_CMPRSFLGS) == NS_TYPE_ELT) {
138 int m;
139
140 if (n != DNS_LABELTYPE_BITSTRING) {
141 /* XXX: labellen should reject this case */
142 errno = EINVAL;
143 return(-1);
144 }
145 if ((m = decode_bitstring(&cp, dn, eom)) < 0)
146 {
147 errno = EMSGSIZE;
148 return(-1);
149 }
150 dn += m;
151 continue;
152 }
153 for (; l > 0; l--) {
154 c = *cp++;
155 if (special(c)) {
156 if (dn + 1 >= eom) {
157 errno = EMSGSIZE;
158 return (-1);
159 }
160 *dn++ = '\\';
161 *dn++ = (char)c;
162 } else if (!printable(c)) {
163 if (dn + 3 >= eom) {
164 errno = EMSGSIZE;
165 return (-1);
166 }
167 *dn++ = '\\';
168 *dn++ = digits[c / 100];
169 *dn++ = digits[(c % 100) / 10];
170 *dn++ = digits[c % 10];
171 } else {
172 if (dn >= eom) {
173 errno = EMSGSIZE;
174 return (-1);
175 }
176 *dn++ = (char)c;
177 }
178 }
179 }
180 if (dn == dst) {
181 if (dn >= eom) {
182 errno = EMSGSIZE;
183 return (-1);
184 }
185 *dn++ = '.';
186 }
187 if (dn >= eom) {
188 errno = EMSGSIZE;
189 return (-1);
190 }
191 *dn++ = '\0';
Calin Juravlefcb502e2014-03-05 15:06:52 +0000192 _DIAGASSERT(__type_fit(int, dn - dst));
193 return (int)(dn - dst);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800194}
195
196/*
Calin Juravlefcb502e2014-03-05 15:06:52 +0000197 * Convert a ascii string into an encoded domain name as per RFC1035.
198 *
199 * return:
200 *
201 * -1 if it fails
202 * 1 if string was fully qualified
203 * 0 is string was not fully qualified
204 *
205 * notes:
206 * Enforces label and domain length limits.
207 */
208int
209ns_name_pton(const char *src, u_char *dst, size_t dstsiz) {
210 return (ns_name_pton2(src, dst, dstsiz, NULL));
211}
212
213/*
214 * ns_name_pton2(src, dst, dstsiz, *dstlen)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800215 * Convert a ascii string into an encoded domain name as per RFC1035.
216 * return:
217 * -1 if it fails
218 * 1 if string was fully qualified
219 * 0 is string was not fully qualified
Calin Juravlefcb502e2014-03-05 15:06:52 +0000220 * side effects:
221 * fills in *dstlen (if non-NULL)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800222 * notes:
223 * Enforces label and domain length limits.
224 */
225
226int
Calin Juravlefcb502e2014-03-05 15:06:52 +0000227ns_name_pton2(const char *src, u_char *dst, size_t dstsiz, size_t *dstlen) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800228 u_char *label, *bp, *eom;
229 int c, n, escaped, e = 0;
230 char *cp;
231
232 escaped = 0;
233 bp = dst;
234 eom = dst + dstsiz;
235 label = bp++;
236
237 while ((c = *src++) != 0) {
238 if (escaped) {
239 if (c == '[') { /* start a bit string label */
240 if ((cp = strchr(src, ']')) == NULL) {
241 errno = EINVAL; /* ??? */
242 return(-1);
243 }
244 if ((e = encode_bitsring(&src, cp + 2,
245 &label, &bp, eom))
246 != 0) {
247 errno = e;
248 return(-1);
249 }
250 escaped = 0;
251 label = bp++;
252 if ((c = *src++) == 0)
253 goto done;
254 else if (c != '.') {
255 errno = EINVAL;
256 return(-1);
257 }
258 continue;
259 }
260 else if ((cp = strchr(digits, c)) != NULL) {
Calin Juravlefcb502e2014-03-05 15:06:52 +0000261 n = (int)(cp - digits) * 100;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800262 if ((c = *src++) == 0 ||
263 (cp = strchr(digits, c)) == NULL) {
264 errno = EMSGSIZE;
265 return (-1);
266 }
Calin Juravlefcb502e2014-03-05 15:06:52 +0000267 n += (int)(cp - digits) * 10;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800268 if ((c = *src++) == 0 ||
269 (cp = strchr(digits, c)) == NULL) {
270 errno = EMSGSIZE;
271 return (-1);
272 }
Calin Juravlefcb502e2014-03-05 15:06:52 +0000273 n += (int)(cp - digits);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800274 if (n > 255) {
275 errno = EMSGSIZE;
276 return (-1);
277 }
278 c = n;
279 }
280 escaped = 0;
281 } else if (c == '\\') {
282 escaped = 1;
283 continue;
284 } else if (c == '.') {
Calin Juravlefcb502e2014-03-05 15:06:52 +0000285 c = (int)(bp - label - 1);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800286 if ((c & NS_CMPRSFLGS) != 0) { /* Label too big. */
287 errno = EMSGSIZE;
288 return (-1);
289 }
290 if (label >= eom) {
291 errno = EMSGSIZE;
292 return (-1);
293 }
294 *label = c;
295 /* Fully qualified ? */
296 if (*src == '\0') {
297 if (c != 0) {
298 if (bp >= eom) {
299 errno = EMSGSIZE;
300 return (-1);
301 }
302 *bp++ = '\0';
303 }
304 if ((bp - dst) > MAXCDNAME) {
305 errno = EMSGSIZE;
306 return (-1);
307 }
Calin Juravlefcb502e2014-03-05 15:06:52 +0000308 if (dstlen != NULL)
309 *dstlen = (bp - dst);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800310 return (1);
311 }
312 if (c == 0 || *src == '.') {
313 errno = EMSGSIZE;
314 return (-1);
315 }
316 label = bp++;
317 continue;
318 }
319 if (bp >= eom) {
320 errno = EMSGSIZE;
321 return (-1);
322 }
323 *bp++ = (u_char)c;
324 }
Calin Juravlefcb502e2014-03-05 15:06:52 +0000325 c = (int)(bp - label - 1);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800326 if ((c & NS_CMPRSFLGS) != 0) { /* Label too big. */
327 errno = EMSGSIZE;
328 return (-1);
329 }
330 done:
331 if (label >= eom) {
332 errno = EMSGSIZE;
333 return (-1);
334 }
335 *label = c;
336 if (c != 0) {
337 if (bp >= eom) {
338 errno = EMSGSIZE;
339 return (-1);
340 }
341 *bp++ = 0;
342 }
343 if ((bp - dst) > MAXCDNAME) { /* src too big */
344 errno = EMSGSIZE;
345 return (-1);
346 }
Calin Juravlefcb502e2014-03-05 15:06:52 +0000347 if (dstlen != NULL)
348 *dstlen = (bp - dst);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800349 return (0);
350}
351
352/*
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800353 * Convert a network strings labels into all lowercase.
Calin Juravlefcb502e2014-03-05 15:06:52 +0000354 *
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800355 * return:
356 * Number of bytes written to buffer, or -1 (with errno set)
Calin Juravlefcb502e2014-03-05 15:06:52 +0000357 *
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800358 * notes:
359 * Enforces label and domain length limits.
360 */
361
362int
363ns_name_ntol(const u_char *src, u_char *dst, size_t dstsiz)
364{
365 const u_char *cp;
366 u_char *dn, *eom;
367 u_char c;
368 u_int n;
369 int l;
370
371 cp = src;
372 dn = dst;
373 eom = dst + dstsiz;
374
375 if (dn >= eom) {
376 errno = EMSGSIZE;
377 return (-1);
378 }
379 while ((n = *cp++) != 0) {
380 if ((n & NS_CMPRSFLGS) == NS_CMPRSFLGS) {
381 /* Some kind of compression pointer. */
382 errno = EMSGSIZE;
383 return (-1);
384 }
385 *dn++ = n;
386 if ((l = labellen(cp - 1)) < 0) {
387 errno = EMSGSIZE;
388 return (-1);
389 }
390 if (dn + l >= eom) {
391 errno = EMSGSIZE;
392 return (-1);
393 }
394 for (; l > 0; l--) {
395 c = *cp++;
Calin Juravlefcb502e2014-03-05 15:06:52 +0000396 if (isascii(c) && isupper(c))
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800397 *dn++ = tolower(c);
398 else
399 *dn++ = c;
400 }
401 }
402 *dn++ = '\0';
Calin Juravlefcb502e2014-03-05 15:06:52 +0000403 _DIAGASSERT(__type_fit(int, dn - dst));
404 return (int)(dn - dst);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800405}
406
407/*
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800408 * Unpack a domain name from a message, source may be compressed.
Calin Juravlefcb502e2014-03-05 15:06:52 +0000409 *
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800410 * return:
411 * -1 if it fails, or consumed octets if it succeeds.
412 */
413int
414ns_name_unpack(const u_char *msg, const u_char *eom, const u_char *src,
415 u_char *dst, size_t dstsiz)
416{
Calin Juravlefcb502e2014-03-05 15:06:52 +0000417 return (ns_name_unpack2(msg, eom, src, dst, dstsiz, NULL));
418}
419
420/*
421 * ns_name_unpack2(msg, eom, src, dst, dstsiz, *dstlen)
422 * Unpack a domain name from a message, source may be compressed.
423 * return:
424 * -1 if it fails, or consumed octets if it succeeds.
425 * side effect:
426 * fills in *dstlen (if non-NULL).
427 */
428int
429ns_name_unpack2(const u_char *msg, const u_char *eom, const u_char *src,
430 u_char *dst, size_t dstsiz, size_t *dstlen)
431{
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800432 const u_char *srcp, *dstlim;
433 u_char *dstp;
434 int n, len, checked, l;
435
436 len = -1;
437 checked = 0;
438 dstp = dst;
439 srcp = src;
440 dstlim = dst + dstsiz;
441 if (srcp < msg || srcp >= eom) {
442 errno = EMSGSIZE;
443 return (-1);
444 }
445 /* Fetch next label in domain name. */
446 while ((n = *srcp++) != 0) {
447 /* Check for indirection. */
448 switch (n & NS_CMPRSFLGS) {
449 case 0:
450 case NS_TYPE_ELT:
451 /* Limit checks. */
452 if ((l = labellen(srcp - 1)) < 0) {
453 errno = EMSGSIZE;
454 return(-1);
455 }
456 if (dstp + l + 1 >= dstlim || srcp + l >= eom) {
457 errno = EMSGSIZE;
458 return (-1);
459 }
460 checked += l + 1;
461 *dstp++ = n;
462 memcpy(dstp, srcp, (size_t)l);
463 dstp += l;
464 srcp += l;
465 break;
466
467 case NS_CMPRSFLGS:
468 if (srcp >= eom) {
469 errno = EMSGSIZE;
470 return (-1);
471 }
Calin Juravlefcb502e2014-03-05 15:06:52 +0000472 if (len < 0) {
473 _DIAGASSERT(__type_fit(int, srcp - src + 1));
474 len = (int)(srcp - src + 1);
475 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800476 srcp = msg + (((n & 0x3f) << 8) | (*srcp & 0xff));
477 if (srcp < msg || srcp >= eom) { /* Out of range. */
478 errno = EMSGSIZE;
479 return (-1);
480 }
481 checked += 2;
482 /*
483 * Check for loops in the compressed name;
484 * if we've looked at the whole message,
485 * there must be a loop.
486 */
487 if (checked >= eom - msg) {
488 errno = EMSGSIZE;
489 return (-1);
490 }
491 break;
492
493 default:
494 errno = EMSGSIZE;
495 return (-1); /* flag error */
496 }
497 }
Calin Juravlefcb502e2014-03-05 15:06:52 +0000498 *dstp++ = 0;
499 if (dstlen != NULL)
500 *dstlen = dstp - dst;
501 if (len < 0) {
502 _DIAGASSERT(__type_fit(int, srcp - src));
503 len = (int)(srcp - src);
504 }
505 return len;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800506}
507
508/*
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800509 * Pack domain name 'domain' into 'comp_dn'.
Calin Juravlefcb502e2014-03-05 15:06:52 +0000510 *
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800511 * return:
512 * Size of the compressed name, or -1.
Calin Juravlefcb502e2014-03-05 15:06:52 +0000513 *
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800514 * notes:
515 * 'dnptrs' is an array of pointers to previous compressed names.
516 * dnptrs[0] is a pointer to the beginning of the message. The array
517 * ends with NULL.
518 * 'lastdnptr' is a pointer to the end of the array pointed to
519 * by 'dnptrs'.
Calin Juravlefcb502e2014-03-05 15:06:52 +0000520 *
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800521 * Side effects:
522 * The list of pointers in dnptrs is updated for labels inserted into
523 * the message as we compress the name. If 'dnptr' is NULL, we don't
524 * try to compress names. If 'lastdnptr' is NULL, we don't update the
525 * list.
526 */
527int
528ns_name_pack(const u_char *src, u_char *dst, int dstsiz,
529 const u_char **dnptrs, const u_char **lastdnptr)
530{
531 u_char *dstp;
532 const u_char **cpp, **lpp, *eob, *msg;
533 const u_char *srcp;
534 int n, l, first = 1;
535
536 srcp = src;
537 dstp = dst;
538 eob = dstp + dstsiz;
539 lpp = cpp = NULL;
540 if (dnptrs != NULL) {
541 if ((msg = *dnptrs++) != NULL) {
542 for (cpp = dnptrs; *cpp != NULL; cpp++)
Calin Juravlefcb502e2014-03-05 15:06:52 +0000543 continue;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800544 lpp = cpp; /* end of list to search */
545 }
546 } else
547 msg = NULL;
548
549 /* make sure the domain we are about to add is legal */
550 l = 0;
551 do {
552 int l0;
553
554 n = *srcp;
555 if ((n & NS_CMPRSFLGS) == NS_CMPRSFLGS) {
556 errno = EMSGSIZE;
557 return (-1);
558 }
559 if ((l0 = labellen(srcp)) < 0) {
560 errno = EINVAL;
561 return(-1);
562 }
563 l += l0 + 1;
564 if (l > MAXCDNAME) {
565 errno = EMSGSIZE;
566 return (-1);
567 }
568 srcp += l0 + 1;
569 } while (n != 0);
570
571 /* from here on we need to reset compression pointer array on error */
572 srcp = src;
573 do {
574 /* Look to see if we can use pointers. */
575 n = *srcp;
576 if (n != 0 && msg != NULL) {
577 l = dn_find(srcp, msg, (const u_char * const *)dnptrs,
578 (const u_char * const *)lpp);
579 if (l >= 0) {
580 if (dstp + 1 >= eob) {
581 goto cleanup;
582 }
583 *dstp++ = ((u_int32_t)l >> 8) | NS_CMPRSFLGS;
584 *dstp++ = l % 256;
Calin Juravlefcb502e2014-03-05 15:06:52 +0000585 _DIAGASSERT(__type_fit(int, dstp - dst));
586 return (int)(dstp - dst);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800587 }
588 /* Not found, save it. */
589 if (lastdnptr != NULL && cpp < lastdnptr - 1 &&
590 (dstp - msg) < 0x4000 && first) {
591 *cpp++ = dstp;
592 *cpp = NULL;
593 first = 0;
594 }
595 }
596 /* copy label to buffer */
597 if ((n & NS_CMPRSFLGS) == NS_CMPRSFLGS) {
598 /* Should not happen. */
599 goto cleanup;
600 }
601 n = labellen(srcp);
602 if (dstp + 1 + n >= eob) {
603 goto cleanup;
604 }
605 memcpy(dstp, srcp, (size_t)(n + 1));
606 srcp += n + 1;
607 dstp += n + 1;
608 } while (n != 0);
609
610 if (dstp > eob) {
611cleanup:
612 if (msg != NULL)
613 *lpp = NULL;
614 errno = EMSGSIZE;
615 return (-1);
616 }
Calin Juravlefcb502e2014-03-05 15:06:52 +0000617 _DIAGASSERT(__type_fit(int, dstp - dst));
618 return (int)(dstp - dst);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800619}
620
621/*
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800622 * Expand compressed domain name to presentation format.
Calin Juravlefcb502e2014-03-05 15:06:52 +0000623 *
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800624 * return:
625 * Number of bytes read out of `src', or -1 (with errno set).
Calin Juravlefcb502e2014-03-05 15:06:52 +0000626 *
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800627 * note:
628 * Root domain returns as "." not "".
629 */
630int
631ns_name_uncompress(const u_char *msg, const u_char *eom, const u_char *src,
632 char *dst, size_t dstsiz)
633{
634 u_char tmp[NS_MAXCDNAME];
635 int n;
636
637 if ((n = ns_name_unpack(msg, eom, src, tmp, sizeof tmp)) == -1)
638 return (-1);
639 if (ns_name_ntop(tmp, dst, dstsiz) == -1)
640 return (-1);
641 return (n);
642}
643
644/*
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800645 * Compress a domain name into wire format, using compression pointers.
Calin Juravlefcb502e2014-03-05 15:06:52 +0000646 *
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800647 * return:
648 * Number of bytes consumed in `dst' or -1 (with errno set).
Calin Juravlefcb502e2014-03-05 15:06:52 +0000649 *
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800650 * notes:
651 * 'dnptrs' is an array of pointers to previous compressed names.
652 * dnptrs[0] is a pointer to the beginning of the message.
653 * The list ends with NULL. 'lastdnptr' is a pointer to the end of the
654 * array pointed to by 'dnptrs'. Side effect is to update the list of
655 * pointers for labels inserted into the message as we compress the name.
656 * If 'dnptr' is NULL, we don't try to compress names. If 'lastdnptr'
657 * is NULL, we don't update the list.
658 */
659int
660ns_name_compress(const char *src, u_char *dst, size_t dstsiz,
661 const u_char **dnptrs, const u_char **lastdnptr)
662{
663 u_char tmp[NS_MAXCDNAME];
664
665 if (ns_name_pton(src, tmp, sizeof tmp) == -1)
666 return (-1);
667 return (ns_name_pack(tmp, dst, (int)dstsiz, dnptrs, lastdnptr));
668}
669
670/*
671 * Reset dnptrs so that there are no active references to pointers at or
672 * after src.
673 */
674void
675ns_name_rollback(const u_char *src, const u_char **dnptrs,
676 const u_char **lastdnptr)
677{
678 while (dnptrs < lastdnptr && *dnptrs != NULL) {
679 if (*dnptrs >= src) {
680 *dnptrs = NULL;
681 break;
682 }
683 dnptrs++;
684 }
685}
686
687/*
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800688 * Advance *ptrptr to skip over the compressed name it points at.
Calin Juravlefcb502e2014-03-05 15:06:52 +0000689 *
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800690 * return:
691 * 0 on success, -1 (with errno set) on failure.
692 */
693int
694ns_name_skip(const u_char **ptrptr, const u_char *eom)
695{
696 const u_char *cp;
697 u_int n;
698 int l;
699
700 cp = *ptrptr;
701 while (cp < eom && (n = *cp++) != 0) {
702 /* Check for indirection. */
703 switch (n & NS_CMPRSFLGS) {
704 case 0: /* normal case, n == len */
705 cp += n;
706 continue;
707 case NS_TYPE_ELT: /* EDNS0 extended label */
708 if ((l = labellen(cp - 1)) < 0) {
709 errno = EMSGSIZE; /* XXX */
710 return(-1);
711 }
712 cp += l;
713 continue;
714 case NS_CMPRSFLGS: /* indirection */
715 cp++;
716 break;
717 default: /* illegal type */
718 errno = EMSGSIZE;
719 return (-1);
720 }
721 break;
722 }
723 if (cp > eom) {
724 errno = EMSGSIZE;
725 return (-1);
726 }
727 *ptrptr = cp;
728 return (0);
729}
730
Calin Juravlefcb502e2014-03-05 15:06:52 +0000731/* Find the number of octets an nname takes up, including the root label.
732 * (This is basically ns_name_skip() without compression-pointer support.)
733 * ((NOTE: can only return zero if passed-in namesiz argument is zero.))
734 */
735ssize_t
736ns_name_length(ns_nname_ct nname, size_t namesiz) {
737 ns_nname_ct orig = nname;
738 u_int n;
739
740 while (namesiz-- > 0 && (n = *nname++) != 0) {
741 if ((n & NS_CMPRSFLGS) != 0) {
742 errno = EISDIR;
743 return (-1);
744 }
745 if (n > namesiz) {
746 errno = EMSGSIZE;
747 return (-1);
748 }
749 nname += n;
750 namesiz -= n;
751 }
752 return (nname - orig);
753}
754
755/* Compare two nname's for equality. Return -1 on error (setting errno).
756 */
757int
758ns_name_eq(ns_nname_ct a, size_t as, ns_nname_ct b, size_t bs) {
759 ns_nname_ct ae = a + as, be = b + bs;
760 int ac, bc;
761
762 while (ac = *a, bc = *b, ac != 0 && bc != 0) {
763 if ((ac & NS_CMPRSFLGS) != 0 || (bc & NS_CMPRSFLGS) != 0) {
764 errno = EISDIR;
765 return (-1);
766 }
767 if (a + ac >= ae || b + bc >= be) {
768 errno = EMSGSIZE;
769 return (-1);
770 }
771 if (ac != bc || strncasecmp((const char *) ++a,
772 (const char *) ++b,
773 (size_t)ac) != 0)
774 return (0);
775 a += ac, b += bc;
776 }
777 return (ac == 0 && bc == 0);
778}
779
780/* Is domain "A" owned by (at or below) domain "B"?
781 */
782int
783ns_name_owned(ns_namemap_ct a, int an, ns_namemap_ct b, int bn) {
784 /* If A is shorter, it cannot be owned by B. */
785 if (an < bn)
786 return (0);
787
788 /* If they are unequal before the length of the shorter, A cannot... */
789 while (bn > 0) {
790 if (a->len != b->len ||
791 strncasecmp((const char *) a->base,
792 (const char *) b->base, (size_t)a->len) != 0)
793 return (0);
794 a++, an--;
795 b++, bn--;
796 }
797
798 /* A might be longer or not, but either way, B owns it. */
799 return (1);
800}
801
802/* Build an array of <base,len> tuples from an nname, top-down order.
803 * Return the number of tuples (labels) thus discovered.
804 */
805int
806ns_name_map(ns_nname_ct nname, size_t namelen, ns_namemap_t map, int mapsize) {
807 u_int n;
808 int l;
809
810 n = *nname++;
811 namelen--;
812
813 /* Root zone? */
814 if (n == 0) {
815 /* Extra data follows name? */
816 if (namelen > 0) {
817 errno = EMSGSIZE;
818 return (-1);
819 }
820 return (0);
821 }
822
823 /* Compression pointer? */
824 if ((n & NS_CMPRSFLGS) != 0) {
825 errno = EISDIR;
826 return (-1);
827 }
828
829 /* Label too long? */
830 if (n > namelen) {
831 errno = EMSGSIZE;
832 return (-1);
833 }
834
835 /* Recurse to get rest of name done first. */
836 l = ns_name_map(nname + n, namelen - n, map, mapsize);
837 if (l < 0)
838 return (-1);
839
840 /* Too many labels? */
841 if (l >= mapsize) {
842 errno = ENAMETOOLONG;
843 return (-1);
844 }
845
846 /* We're on our way back up-stack, store current map data. */
847 map[l].base = nname;
848 map[l].len = n;
849 return (l + 1);
850}
851
852/* Count the labels in a domain name. Root counts, so COM. has two. This
853 * is to make the result comparable to the result of ns_name_map().
854 */
855int
856ns_name_labels(ns_nname_ct nname, size_t namesiz) {
857 int ret = 0;
858 u_int n;
859
860 while (namesiz-- > 0 && (n = *nname++) != 0) {
861 if ((n & NS_CMPRSFLGS) != 0) {
862 errno = EISDIR;
863 return (-1);
864 }
865 if (n > namesiz) {
866 errno = EMSGSIZE;
867 return (-1);
868 }
869 nname += n;
870 namesiz -= n;
871 ret++;
872 }
873 return (ret + 1);
874}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800875/* Private. */
876
877/*
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800878 * Thinking in noninternationalized USASCII (per the DNS spec),
879 * is this characted special ("in need of quoting") ?
Calin Juravlefcb502e2014-03-05 15:06:52 +0000880 *
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800881 * return:
882 * boolean.
883 */
884static int
885special(int ch) {
886 switch (ch) {
887 case 0x22: /* '"' */
888 case 0x2E: /* '.' */
889 case 0x3B: /* ';' */
890 case 0x5C: /* '\\' */
891 case 0x28: /* '(' */
892 case 0x29: /* ')' */
893 /* Special modifiers in zone files. */
894 case 0x40: /* '@' */
895 case 0x24: /* '$' */
896 return (1);
897 default:
898 return (0);
899 }
900}
901
902/*
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800903 * Thinking in noninternationalized USASCII (per the DNS spec),
904 * is this character visible and not a space when printed ?
Calin Juravlefcb502e2014-03-05 15:06:52 +0000905 *
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800906 * return:
907 * boolean.
908 */
909static int
910printable(int ch) {
911 return (ch > 0x20 && ch < 0x7f);
912}
913
914/*
915 * Thinking in noninternationalized USASCII (per the DNS spec),
916 * convert this character to lower case if it's upper case.
917 */
918static int
919mklower(int ch) {
920 if (ch >= 0x41 && ch <= 0x5A)
921 return (ch + 0x20);
922 return (ch);
923}
924
925/*
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800926 * Search for the counted-label name in an array of compressed names.
Calin Juravlefcb502e2014-03-05 15:06:52 +0000927 *
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800928 * return:
929 * offset from msg if found, or -1.
Calin Juravlefcb502e2014-03-05 15:06:52 +0000930 *
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800931 * notes:
932 * dnptrs is the pointer to the first name on the list,
933 * not the pointer to the start of the message.
934 */
935static int
936dn_find(const u_char *domain, const u_char *msg,
937 const u_char * const *dnptrs,
938 const u_char * const *lastdnptr)
939{
940 const u_char *dn, *cp, *sp;
941 const u_char * const *cpp;
942 u_int n;
943
944 for (cpp = dnptrs; cpp < lastdnptr; cpp++) {
945 sp = *cpp;
946 /*
947 * terminate search on:
948 * root label
949 * compression pointer
950 * unusable offset
951 */
952 while (*sp != 0 && (*sp & NS_CMPRSFLGS) == 0 &&
953 (sp - msg) < 0x4000) {
954 dn = domain;
955 cp = sp;
956 while ((n = *cp++) != 0) {
957 /*
958 * check for indirection
959 */
960 switch (n & NS_CMPRSFLGS) {
961 case 0: /* normal case, n == len */
962 n = labellen(cp - 1); /* XXX */
963
964 if (n != *dn++)
965 goto next;
966
967 for (; n > 0; n--)
968 if (mklower(*dn++) !=
969 mklower(*cp++))
970 goto next;
971 /* Is next root for both ? */
Calin Juravlefcb502e2014-03-05 15:06:52 +0000972 if (*dn == '\0' && *cp == '\0') {
973 _DIAGASSERT(__type_fit(int,
974 sp - msg));
975 return (int)(sp - msg);
976 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800977 if (*dn)
978 continue;
979 goto next;
980 case NS_CMPRSFLGS: /* indirection */
981 cp = msg + (((n & 0x3f) << 8) | *cp);
982 break;
983
984 default: /* illegal type */
985 errno = EMSGSIZE;
986 return (-1);
987 }
988 }
989 next: ;
990 sp += *sp + 1;
991 }
992 }
993 errno = ENOENT;
994 return (-1);
995}
996
997static int
998decode_bitstring(const unsigned char **cpp, char *dn, const char *eom)
999{
1000 const unsigned char *cp = *cpp;
1001 char *beg = dn, tc;
1002 int b, blen, plen, i;
1003
1004 if ((blen = (*cp & 0xff)) == 0)
1005 blen = 256;
1006 plen = (blen + 3) / 4;
Calin Juravlefcb502e2014-03-05 15:06:52 +00001007 plen += (int)sizeof("\\[x/]") + (blen > 99 ? 3 : (blen > 9) ? 2 : 1);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001008 if (dn + plen >= eom)
1009 return(-1);
1010
1011 cp++;
1012 i = SPRINTF((dn, "\\[x"));
1013 if (i < 0)
1014 return (-1);
1015 dn += i;
1016 for (b = blen; b > 7; b -= 8, cp++) {
1017 i = SPRINTF((dn, "%02x", *cp & 0xff));
1018 if (i < 0)
1019 return (-1);
1020 dn += i;
1021 }
1022 if (b > 4) {
1023 tc = *cp++;
1024 i = SPRINTF((dn, "%02x", tc & (0xff << (8 - b))));
1025 if (i < 0)
1026 return (-1);
1027 dn += i;
1028 } else if (b > 0) {
1029 tc = *cp++;
1030 i = SPRINTF((dn, "%1x",
1031 (((u_int32_t)tc >> 4) & 0x0f) & (0x0f << (4 - b))));
1032 if (i < 0)
1033 return (-1);
1034 dn += i;
1035 }
1036 i = SPRINTF((dn, "/%d]", blen));
1037 if (i < 0)
1038 return (-1);
1039 dn += i;
1040
1041 *cpp = cp;
Calin Juravlefcb502e2014-03-05 15:06:52 +00001042 _DIAGASSERT(__type_fit(int, dn - beg));
1043 return (int)(dn - beg);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001044}
1045
1046static int
1047encode_bitsring(const char **bp, const char *end, unsigned char **labelp,
1048 unsigned char ** dst, unsigned const char *eom)
1049{
1050 int afterslash = 0;
1051 const char *cp = *bp;
1052 unsigned char *tp;
1053 char c;
1054 const char *beg_blen;
1055 char *end_blen = NULL;
1056 int value = 0, count = 0, tbcount = 0, blen = 0;
1057
1058 beg_blen = end_blen = NULL;
1059
1060 /* a bitstring must contain at least 2 characters */
1061 if (end - cp < 2)
1062 return(EINVAL);
1063
1064 /* XXX: currently, only hex strings are supported */
1065 if (*cp++ != 'x')
1066 return(EINVAL);
1067 if (!isxdigit((*cp) & 0xff)) /* reject '\[x/BLEN]' */
1068 return(EINVAL);
1069
1070 for (tp = *dst + 1; cp < end && tp < eom; cp++) {
1071 switch((c = *cp)) {
1072 case ']': /* end of the bitstring */
1073 if (afterslash) {
1074 if (beg_blen == NULL)
1075 return(EINVAL);
1076 blen = (int)strtol(beg_blen, &end_blen, 10);
1077 if (*end_blen != ']')
1078 return(EINVAL);
1079 }
1080 if (count)
1081 *tp++ = ((value << 4) & 0xff);
1082 cp++; /* skip ']' */
1083 goto done;
1084 case '/':
1085 afterslash = 1;
1086 break;
1087 default:
1088 if (afterslash) {
1089 if (!isdigit(c&0xff))
1090 return(EINVAL);
1091 if (beg_blen == NULL) {
1092
1093 if (c == '0') {
1094 /* blen never begings with 0 */
1095 return(EINVAL);
1096 }
1097 beg_blen = cp;
1098 }
1099 } else {
1100 if (!isxdigit(c&0xff))
1101 return(EINVAL);
1102 value <<= 4;
1103 value += digitvalue[(int)c];
1104 count += 4;
1105 tbcount += 4;
1106 if (tbcount > 256)
1107 return(EINVAL);
1108 if (count == 8) {
1109 *tp++ = value;
1110 count = 0;
1111 }
1112 }
1113 break;
1114 }
1115 }
1116 done:
1117 if (cp >= end || tp >= eom)
1118 return(EMSGSIZE);
1119
1120 /*
1121 * bit length validation:
1122 * If a <length> is present, the number of digits in the <bit-data>
1123 * MUST be just sufficient to contain the number of bits specified
1124 * by the <length>. If there are insignificant bits in a final
1125 * hexadecimal or octal digit, they MUST be zero.
1126 * RFC 2673, Section 3.2.
1127 */
1128 if (blen > 0) {
1129 int traillen;
1130
1131 if (((blen + 3) & ~3) != tbcount)
1132 return(EINVAL);
1133 traillen = tbcount - blen; /* between 0 and 3 */
1134 if (((value << (8 - traillen)) & 0xff) != 0)
1135 return(EINVAL);
1136 }
1137 else
1138 blen = tbcount;
1139 if (blen == 256)
1140 blen = 0;
1141
1142 /* encode the type and the significant bit fields */
1143 **labelp = DNS_LABELTYPE_BITSTRING;
1144 **dst = blen;
1145
1146 *bp = cp;
1147 *dst = tp;
1148
1149 return(0);
1150}
1151
1152static int
1153labellen(const u_char *lp)
1154{
1155 int bitlen;
1156 u_char l = *lp;
1157
1158 if ((l & NS_CMPRSFLGS) == NS_CMPRSFLGS) {
1159 /* should be avoided by the caller */
1160 return(-1);
1161 }
1162
1163 if ((l & NS_CMPRSFLGS) == NS_TYPE_ELT) {
1164 if (l == DNS_LABELTYPE_BITSTRING) {
1165 if ((bitlen = *(lp + 1)) == 0)
1166 bitlen = 256;
1167 return((bitlen + 7 ) / 8 + 1);
1168 }
1169 return(-1); /* unknwon ELT */
1170 }
1171 return(l);
1172}