]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/isc-dhcp/minires/ns_name.c
This commit was generated by cvs2svn to compensate for changes in r104977,
[FreeBSD/FreeBSD.git] / contrib / isc-dhcp / minires / 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 #ifndef lint
19 static const char rcsid[] = "$Id: ns_name.c,v 1.1 2000/02/02 07:28:14 mellon 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         while ((n = *cp++) != 0) {
259                 if ((n & NS_CMPRSFLGS) != 0) {
260                         /* Some kind of compression pointer. */
261                         errno = EMSGSIZE;
262                         return (-1);
263                 }
264                 *dn++ = n;
265                 if (dn + n >= eom) {
266                         errno = EMSGSIZE;
267                         return (-1);
268                 }
269                 for ((void)NULL; n > 0; n--) {
270                         c = *cp++;
271                         if (isupper(c))
272                                 *dn++ = tolower(c);
273                         else
274                                 *dn++ = c;
275                 }
276         }
277         *dn++ = '\0';
278         return (dn - dst);
279 }
280
281 /*
282  * ns_name_unpack(msg, eom, src, dst, dstsiz)
283  *      Unpack a domain name from a message, source may be compressed.
284  * return:
285  *      -1 if it fails, or consumed octets if it succeeds.
286  */
287 int
288 ns_name_unpack(const u_char *msg, const u_char *eom, const u_char *src,
289                u_char *dst, size_t dstsiz)
290 {
291         const u_char *srcp, *dstlim;
292         u_char *dstp;
293         unsigned n;
294         int len;
295         int checked;
296
297         len = -1;
298         checked = 0;
299         dstp = dst;
300         srcp = src;
301         dstlim = dst + dstsiz;
302         if (srcp < msg || srcp >= eom) {
303                 errno = EMSGSIZE;
304                 return (-1);
305         }
306         /* Fetch next label in domain name. */
307         while ((n = *srcp++) != 0) {
308                 /* Check for indirection. */
309                 switch (n & NS_CMPRSFLGS) {
310                 case 0:
311                         /* Limit checks. */
312                         if (dstp + n + 1 >= dstlim || srcp + n >= eom) {
313                                 errno = EMSGSIZE;
314                                 return (-1);
315                         }
316                         checked += n + 1;
317                         *dstp++ = n;
318                         memcpy(dstp, srcp, n);
319                         dstp += n;
320                         srcp += n;
321                         break;
322
323                 case NS_CMPRSFLGS:
324                         if (srcp >= eom) {
325                                 errno = EMSGSIZE;
326                                 return (-1);
327                         }
328                         if (len < 0)
329                                 len = srcp - src + 1;
330                         srcp = msg + (((n & 0x3f) << 8) | (*srcp & 0xff));
331                         if (srcp < msg || srcp >= eom) {  /* Out of range. */
332                                 errno = EMSGSIZE;
333                                 return (-1);
334                         }
335                         checked += 2;
336                         /*
337                          * Check for loops in the compressed name;
338                          * if we've looked at the whole message,
339                          * there must be a loop.
340                          */
341                         if (checked >= eom - msg) {
342                                 errno = EMSGSIZE;
343                                 return (-1);
344                         }
345                         break;
346
347                 default:
348                         errno = EMSGSIZE;
349                         return (-1);                    /* flag error */
350                 }
351         }
352         *dstp = '\0';
353         if (len < 0)
354                 len = srcp - src;
355         return (len);
356 }
357
358 /*
359  * ns_name_pack(src, dst, dstsiz, dnptrs, lastdnptr)
360  *      Pack domain name 'domain' into 'comp_dn'.
361  * return:
362  *      Size of the compressed name, or -1.
363  * notes:
364  *      'dnptrs' is an array of pointers to previous compressed names.
365  *      dnptrs[0] is a pointer to the beginning of the message. The array
366  *      ends with NULL.
367  *      'lastdnptr' is a pointer to the end of the array pointed to
368  *      by 'dnptrs'.
369  * Side effects:
370  *      The list of pointers in dnptrs is updated for labels inserted into
371  *      the message as we compress the name.  If 'dnptr' is NULL, we don't
372  *      try to compress names. If 'lastdnptr' is NULL, we don't update the
373  *      list.
374  */
375 int
376 ns_name_pack(const u_char *src, u_char *dst, unsigned dstsiz,
377              const u_char **dnptrs, const u_char **lastdnptr)
378 {
379         u_char *dstp;
380         const u_char **cpp, **lpp, *eob, *msg;
381         const u_char *srcp;
382         unsigned n;
383         int l;
384
385         srcp = src;
386         dstp = dst;
387         eob = dstp + dstsiz;
388         lpp = cpp = NULL;
389         if (dnptrs != NULL) {
390                 if ((msg = *dnptrs++) != NULL) {
391                         for (cpp = dnptrs; *cpp != NULL; cpp++)
392                                 (void)NULL;
393                         lpp = cpp;      /* end of list to search */
394                 }
395         } else
396                 msg = NULL;
397
398         /* make sure the domain we are about to add is legal */
399         l = 0;
400         do {
401                 n = *srcp;
402                 if ((n & NS_CMPRSFLGS) != 0) {
403                         errno = EMSGSIZE;
404                         return (-1);
405                 }
406                 l += n + 1;
407                 if (l > MAXCDNAME) {
408                         errno = EMSGSIZE;
409                         return (-1);
410                 }
411                 srcp += n + 1;
412         } while (n != 0);
413
414         /* from here on we need to reset compression pointer array on error */
415         srcp = src;
416         do {
417                 /* Look to see if we can use pointers. */
418                 n = *srcp;
419                 if (n != 0 && msg != NULL) {
420                         l = dn_find(srcp, msg, (const u_char * const *)dnptrs,
421                                     (const u_char * const *)lpp);
422                         if (l >= 0) {
423                                 if (dstp + 1 >= eob) {
424                                         goto cleanup;
425                                 }
426                                 *dstp++ = (l >> 8) | NS_CMPRSFLGS;
427                                 *dstp++ = l % 256;
428                                 return (dstp - dst);
429                         }
430                         /* Not found, save it. */
431                         if (lastdnptr != NULL && cpp < lastdnptr - 1 &&
432                             (dstp - msg) < 0x4000) {
433                                 *cpp++ = dstp;
434                                 *cpp = NULL;
435                         }
436                 }
437                 /* copy label to buffer */
438                 if (n & NS_CMPRSFLGS) {         /* Should not happen. */
439                         goto cleanup;
440                 }
441                 if (dstp + 1 + n >= eob) {
442                         goto cleanup;
443                 }
444                 memcpy(dstp, srcp, n + 1);
445                 srcp += n + 1;
446                 dstp += n + 1;
447         } while (n != 0);
448
449         if (dstp > eob) {
450 cleanup:
451                 if (msg != NULL)
452                         *lpp = NULL;
453                 errno = EMSGSIZE;
454                 return (-1);
455         } 
456         return (dstp - dst);
457 }
458
459 /*
460  * ns_name_uncompress(msg, eom, src, dst, dstsiz)
461  *      Expand compressed domain name to presentation format.
462  * return:
463  *      Number of bytes read out of `src', or -1 (with errno set).
464  * note:
465  *      Root domain returns as "." not "".
466  */
467 int
468 ns_name_uncompress(const u_char *msg, const u_char *eom, const u_char *src,
469                    char *dst, size_t dstsiz)
470 {
471         u_char tmp[NS_MAXCDNAME];
472         int n;
473         
474         if ((n = ns_name_unpack(msg, eom, src, tmp, sizeof tmp)) == -1)
475                 return (-1);
476         if (ns_name_ntop(tmp, dst, dstsiz) == -1)
477                 return (-1);
478         return (n);
479 }
480
481 /*
482  * ns_name_compress(src, dst, dstsiz, dnptrs, lastdnptr)
483  *      Compress a domain name into wire format, using compression pointers.
484  * return:
485  *      Number of bytes consumed in `dst' or -1 (with errno set).
486  * notes:
487  *      'dnptrs' is an array of pointers to previous compressed names.
488  *      dnptrs[0] is a pointer to the beginning of the message.
489  *      The list ends with NULL.  'lastdnptr' is a pointer to the end of the
490  *      array pointed to by 'dnptrs'. Side effect is to update the list of
491  *      pointers for labels inserted into the message as we compress the name.
492  *      If 'dnptr' is NULL, we don't try to compress names. If 'lastdnptr'
493  *      is NULL, we don't update the list.
494  */
495 int
496 ns_name_compress(const char *src, u_char *dst, size_t dstsiz,
497                  const u_char **dnptrs, const u_char **lastdnptr)
498 {
499         u_char tmp[NS_MAXCDNAME];
500
501         if (ns_name_pton(src, tmp, sizeof tmp) == -1)
502                 return (-1);
503         return (ns_name_pack(tmp, dst, dstsiz, dnptrs, lastdnptr));
504 }
505
506 /*
507  * ns_name_skip(ptrptr, eom)
508  *      Advance *ptrptr to skip over the compressed name it points at.
509  * return:
510  *      0 on success, -1 (with errno set) on failure.
511  */
512 int
513 ns_name_skip(const u_char **ptrptr, const u_char *eom) {
514         const u_char *cp;
515         u_int n;
516
517         cp = *ptrptr;
518         while (cp < eom && (n = *cp++) != 0) {
519                 /* Check for indirection. */
520                 switch (n & NS_CMPRSFLGS) {
521                 case 0:                 /* normal case, n == len */
522                         cp += n;
523                         continue;
524                 case NS_CMPRSFLGS:      /* indirection */
525                         cp++;
526                         break;
527                 default:                /* illegal type */
528                         errno = EMSGSIZE;
529                         return (-1);
530                 }
531                 break;
532         }
533         if (cp > eom) {
534                 errno = EMSGSIZE;
535                 return (-1);
536         }
537         *ptrptr = cp;
538         return (0);
539 }
540
541 /* Private. */
542
543 /*
544  * special(ch)
545  *      Thinking in noninternationalized USASCII (per the DNS spec),
546  *      is this characted special ("in need of quoting") ?
547  * return:
548  *      boolean.
549  */
550 static int
551 special(int ch) {
552         switch (ch) {
553         case 0x22: /* '"' */
554         case 0x2E: /* '.' */
555         case 0x3B: /* ';' */
556         case 0x5C: /* '\\' */
557         /* Special modifiers in zone files. */
558         case 0x40: /* '@' */
559         case 0x24: /* '$' */
560                 return (1);
561         default:
562                 return (0);
563         }
564 }
565
566 /*
567  * printable(ch)
568  *      Thinking in noninternationalized USASCII (per the DNS spec),
569  *      is this character visible and not a space when printed ?
570  * return:
571  *      boolean.
572  */
573 static int
574 printable(int ch) {
575         return (ch > 0x20 && ch < 0x7f);
576 }
577
578 /*
579  *      Thinking in noninternationalized USASCII (per the DNS spec),
580  *      convert this character to lower case if it's upper case.
581  */
582 static int
583 mklower(int ch) {
584         if (ch >= 0x41 && ch <= 0x5A)
585                 return (ch + 0x20);
586         return (ch);
587 }
588
589 /*
590  * dn_find(domain, msg, dnptrs, lastdnptr)
591  *      Search for the counted-label name in an array of compressed names.
592  * return:
593  *      offset from msg if found, or -1.
594  * notes:
595  *      dnptrs is the pointer to the first name on the list,
596  *      not the pointer to the start of the message.
597  */
598 static int
599 dn_find(const u_char *domain, const u_char *msg,
600         const u_char * const *dnptrs,
601         const u_char * const *lastdnptr)
602 {
603         const u_char *dn, *cp, *sp;
604         const u_char * const *cpp;
605         u_int n;
606
607         for (cpp = dnptrs; cpp < lastdnptr; cpp++) {
608                 dn = domain;
609                 sp = cp = *cpp;
610                 while ((n = *cp++) != 0) {
611                         /*
612                          * check for indirection
613                          */
614                         switch (n & NS_CMPRSFLGS) {
615                         case 0:                 /* normal case, n == len */
616                                 if (n != *dn++)
617                                         goto next;
618                                 for ((void)NULL; n > 0; n--)
619                                         if (mklower(*dn++) != mklower(*cp++))
620                                                 goto next;
621                                 /* Is next root for both ? */
622                                 if (*dn == '\0' && *cp == '\0')
623                                         return (sp - msg);
624                                 if (*dn)
625                                         continue;
626                                 goto next;
627
628                         case NS_CMPRSFLGS:      /* indirection */
629                                 cp = msg + (((n & 0x3f) << 8) | *cp);
630                                 break;
631
632                         default:        /* illegal type */
633                                 errno = EMSGSIZE;
634                                 return (-1);
635                         }
636                 }
637  next: ;
638         }
639         errno = ENOENT;
640         return (-1);
641 }