SCCS id got lost along the way
[unix-history] / usr / src / lib / libc / net / res_comp.c
index 700cbf5..f1b2826 100644 (file)
@@ -1,39 +1,42 @@
 /*
  * Copyright (c) 1985 Regents of the University of California.
 /*
  * Copyright (c) 1985 Regents of the University of California.
- * All rights reserved.  The Berkeley software License Agreement
- * specifies the terms and conditions for redistribution.
+ * All rights reserved.
+ *
+ * %sccs.include.redist.c%
  */
 
  */
 
-#ifndef lint
-static char sccsid[] = "@(#)res_comp.c 6.4 (Berkeley) %G%";
-#endif not lint
+#if defined(LIBC_SCCS) && !defined(lint)
+static char sccsid[] = "@(#)res_comp.c 6.22 (Berkeley) %G%";
+#endif /* LIBC_SCCS and not lint */
 
 
-#include <sys/types.h>
-#include <stdio.h>
-#include <ctype.h>
+#include <sys/param.h>
 #include <arpa/nameser.h>
 #include <arpa/nameser.h>
+#include <netinet/in.h>
+#include <resolv.h>
+#include <stdio.h>
 
 
+static dn_find();
 
 /*
  * Expand compressed domain name 'comp_dn' to full domain name.
 
 /*
  * Expand compressed domain name 'comp_dn' to full domain name.
- * Expanded names are converted to lower case.
  * 'msg' is a pointer to the begining of the message,
  * 'eomorig' points to the first location after the message,
  * 'exp_dn' is a pointer to a buffer of size 'length' for the result.
  * Return size of compressed name or -1 if there was an error.
  */
 dn_expand(msg, eomorig, comp_dn, exp_dn, length)
  * 'msg' is a pointer to the begining of the message,
  * 'eomorig' points to the first location after the message,
  * 'exp_dn' is a pointer to a buffer of size 'length' for the result.
  * Return size of compressed name or -1 if there was an error.
  */
 dn_expand(msg, eomorig, comp_dn, exp_dn, length)
-       char *msg, *eomorig, *comp_dn, *exp_dn;
+       const u_char *msg, *eomorig, *comp_dn;
+       u_char *exp_dn;
        int length;
 {
        int length;
 {
-       register char *cp, *dn;
+       register u_char *cp, *dn;
        register int n, c;
        register int n, c;
-       char *eom;
-       int len = -1;
+       u_char *eom;
+       int len = -1, checked = 0;
 
        dn = exp_dn;
 
        dn = exp_dn;
-       cp = comp_dn;
-       eom = exp_dn + length - 1;
+       cp = (u_char *)comp_dn;
+       eom = exp_dn + length;
        /*
         * fetch next label in domain name
         */
        /*
         * fetch next label in domain name
         */
@@ -50,17 +53,14 @@ dn_expand(msg, eomorig, comp_dn, exp_dn, length)
                        }
                        if (dn+n >= eom)
                                return (-1);
                        }
                        if (dn+n >= eom)
                                return (-1);
+                       checked += n + 1;
                        while (--n >= 0) {
                        while (--n >= 0) {
-                               if (isupper(c = *cp++))
-                                       *dn++ = tolower(c);
-                               else {
-                                       if (c == '.') {
-                                               if (dn+n+1 >= eom)
-                                                       return (-1);
-                                               *dn++ = '\\';
-                                       }
-                                       *dn++ = c;
+                               if ((c = *cp++) == '.') {
+                                       if (dn + n + 2 >= eom)
+                                               return (-1);
+                                       *dn++ = '\\';
                                }
                                }
+                               *dn++ = c;
                                if (cp >= eomorig)      /* out of range */
                                        return(-1);
                        }
                                if (cp >= eomorig)      /* out of range */
                                        return(-1);
                        }
@@ -69,9 +69,17 @@ dn_expand(msg, eomorig, comp_dn, exp_dn, length)
                case INDIR_MASK:
                        if (len < 0)
                                len = cp - comp_dn + 1;
                case INDIR_MASK:
                        if (len < 0)
                                len = cp - comp_dn + 1;
-                       cp = msg + (((n & 0x3f) << 8) | (*cp & 0xff));
+                       cp = (u_char *)msg + (((n & 0x3f) << 8) | (*cp & 0xff));
                        if (cp < msg || cp >= eomorig)  /* out of range */
                                return(-1);
                        if (cp < msg || cp >= eomorig)  /* out of range */
                                return(-1);
+                       checked += 2;
+                       /*
+                        * Check for loops in the compressed name;
+                        * if we've looked at the whole message,
+                        * there must be a loop.
+                        */
+                       if (checked >= eomorig - msg)
+                               return (-1);
                        break;
 
                default:
                        break;
 
                default:
@@ -97,16 +105,16 @@ dn_expand(msg, eomorig, comp_dn, exp_dn, length)
  * is NULL, we don't update the list.
  */
 dn_comp(exp_dn, comp_dn, length, dnptrs, lastdnptr)
  * is NULL, we don't update the list.
  */
 dn_comp(exp_dn, comp_dn, length, dnptrs, lastdnptr)
-       char *exp_dn, *comp_dn;
+       const u_char *exp_dn;
+       u_char *comp_dn, **dnptrs, **lastdnptr;
        int length;
        int length;
-       char **dnptrs, **lastdnptr;
 {
 {
-       register char *cp, *dn;
+       register u_char *cp, *dn;
        register int c, l;
        register int c, l;
-       char **cpp, **lpp, *sp, *eob;
-       char *msg;
+       u_char **cpp, **lpp, *sp, *eob;
+       u_char *msg;
 
 
-       dn = exp_dn;
+       dn = (u_char *)exp_dn;
        cp = comp_dn;
        eob = cp + length;
        if (dnptrs != NULL) {
        cp = comp_dn;
        eob = cp + length;
        if (dnptrs != NULL) {
@@ -124,7 +132,7 @@ dn_comp(exp_dn, comp_dn, length, dnptrs, lastdnptr)
                                if (cp+1 >= eob)
                                        return (-1);
                                *cp++ = (l >> 8) | INDIR_MASK;
                                if (cp+1 >= eob)
                                        return (-1);
                                *cp++ = (l >> 8) | INDIR_MASK;
-                               *cp++ = l;
+                               *cp++ = l % 256;
                                return (cp - comp_dn);
                        }
                        /* not found, save it */
                                return (cp - comp_dn);
                        }
                        /* not found, save it */
@@ -143,8 +151,11 @@ dn_comp(exp_dn, comp_dn, length, dnptrs, lastdnptr)
                                if ((c = *dn++) == '\0')
                                        break;
                        }
                                if ((c = *dn++) == '\0')
                                        break;
                        }
-                       if (cp >= eob)
+                       if (cp >= eob) {
+                               if (msg != NULL)
+                                       *lpp = NULL;
                                return (-1);
                                return (-1);
+                       }
                        *cp++ = c;
                } while ((c = *dn++) != '\0');
                /* catch trailing '.'s but not '..' */
                        *cp++ = c;
                } while ((c = *dn++) != '\0');
                /* catch trailing '.'s but not '..' */
@@ -152,12 +163,18 @@ dn_comp(exp_dn, comp_dn, length, dnptrs, lastdnptr)
                        cp--;
                        break;
                }
                        cp--;
                        break;
                }
-               if (l <= 0 || l > MAXLABEL)
+               if (l <= 0 || l > MAXLABEL) {
+                       if (msg != NULL)
+                               *lpp = NULL;
                        return (-1);
                        return (-1);
+               }
                *sp = l;
        }
                *sp = l;
        }
-       if (cp >= eob)
+       if (cp >= eob) {
+               if (msg != NULL)
+                       *lpp = NULL;
                return (-1);
                return (-1);
+       }
        *cp++ = '\0';
        return (cp - comp_dn);
 }
        *cp++ = '\0';
        return (cp - comp_dn);
 }
@@ -165,14 +182,14 @@ dn_comp(exp_dn, comp_dn, length, dnptrs, lastdnptr)
 /*
  * Skip over a compressed domain name. Return the size or -1.
  */
 /*
  * Skip over a compressed domain name. Return the size or -1.
  */
-dn_skip(comp_dn)
-       char *comp_dn;
+__dn_skipname(comp_dn, eom)
+       const u_char *comp_dn, *eom;
 {
 {
-       register char *cp;
+       register u_char *cp;
        register int n;
 
        register int n;
 
-       cp = comp_dn;
-       while (n = *cp++) {
+       cp = (u_char *)comp_dn;
+       while (cp < eom && (n = *cp++)) {
                /*
                 * check for indirection
                 */
                /*
                 * check for indirection
                 */
@@ -193,14 +210,17 @@ dn_skip(comp_dn)
 /*
  * Search for expanded name from a list of previously compressed names.
  * Return the offset from msg if found or -1.
 /*
  * Search for expanded name from a list of previously compressed names.
  * Return the offset from msg if found or -1.
+ * dnptrs is the pointer to the first name on the list,
+ * not the pointer to the start of the message.
  */
  */
+static
 dn_find(exp_dn, msg, dnptrs, lastdnptr)
 dn_find(exp_dn, msg, dnptrs, lastdnptr)
-       char *exp_dn, *msg;
-       char **dnptrs, **lastdnptr;
+       u_char *exp_dn, *msg;
+       u_char **dnptrs, **lastdnptr;
 {
 {
-       register char *dn, *cp, **cpp;
+       register u_char *dn, *cp, **cpp;
        register int n;
        register int n;
-       char *sp;
+       u_char *sp;
 
        for (cpp = dnptrs; cpp < lastdnptr; cpp++) {
                dn = exp_dn;
 
        for (cpp = dnptrs; cpp < lastdnptr; cpp++) {
                dn = exp_dn;
@@ -212,6 +232,8 @@ dn_find(exp_dn, msg, dnptrs, lastdnptr)
                        switch (n & INDIR_MASK) {
                        case 0:         /* normal case, n == len */
                                while (--n >= 0) {
                        switch (n & INDIR_MASK) {
                        case 0:         /* normal case, n == len */
                                while (--n >= 0) {
+                                       if (*dn == '.')
+                                               goto next;
                                        if (*dn == '\\')
                                                dn++;
                                        if (*dn++ != *cp++)
                                        if (*dn == '\\')
                                                dn++;
                                        if (*dn++ != *cp++)
@@ -227,7 +249,7 @@ dn_find(exp_dn, msg, dnptrs, lastdnptr)
                                return (-1);
 
                        case INDIR_MASK:        /* indirection */
                                return (-1);
 
                        case INDIR_MASK:        /* indirection */
-                               cp = msg + (((n & 0x3f) << 8) | (*cp & 0xff));
+                               cp = msg + (((n & 0x3f) << 8) | *cp);
                        }
                }
                if (*dn == '\0')
                        }
                }
                if (*dn == '\0')
@@ -241,11 +263,13 @@ dn_find(exp_dn, msg, dnptrs, lastdnptr)
  * Routines to insert/extract short/long's. Must account for byte
  * order and non-alignment problems. This code at least has the
  * advantage of being portable.
  * Routines to insert/extract short/long's. Must account for byte
  * order and non-alignment problems. This code at least has the
  * advantage of being portable.
+ *
+ * used by sendmail.
  */
 
 u_short
  */
 
 u_short
-getshort(msgp)
-       char *msgp;
+_getshort(msgp)
+       u_char *msgp;
 {
        register u_char *p = (u_char *) msgp;
 #ifdef vax
 {
        register u_char *p = (u_char *) msgp;
 #ifdef vax
@@ -262,8 +286,8 @@ getshort(msgp)
 }
 
 u_long
 }
 
 u_long
-getlong(msgp)
-       char *msgp;
+_getlong(msgp)
+       u_char *msgp;
 {
        register u_char *p = (u_char *) msgp;
        register u_long u;
 {
        register u_char *p = (u_char *) msgp;
        register u_long u;
@@ -274,21 +298,24 @@ getlong(msgp)
        return (u | *p);
 }
 
        return (u | *p);
 }
 
-
-putshort(s, msgp)
+void
+#ifdef __STDC__
+__putshort(register u_short s, register u_char *msgp)
+#else
+__putshort(s, msgp)
        register u_short s;
        register u_short s;
-       register char *msgp;
+       register u_char *msgp;
+#endif
 {
 {
-
        msgp[1] = s;
        msgp[0] = s >> 8;
 }
 
        msgp[1] = s;
        msgp[0] = s >> 8;
 }
 
-putlong(l, msgp)
+void
+__putlong(l, msgp)
        register u_long l;
        register u_long l;
-       register char *msgp;
+       register u_char *msgp;
 {
 {
-
        msgp[3] = l;
        msgp[2] = (l >>= 8);
        msgp[1] = (l >>= 8);
        msgp[3] = l;
        msgp[2] = (l >>= 8);
        msgp[1] = (l >>= 8);