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