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