Back to index

glibc  2.9
ns_samedomain.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
00003  * Copyright (c) 1995,1999 by Internet Software Consortium.
00004  *
00005  * Permission to use, copy, modify, and distribute this software for any
00006  * purpose with or without fee is hereby granted, provided that the above
00007  * copyright notice and this permission notice appear in all copies.
00008  *
00009  * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
00010  * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
00011  * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
00012  * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
00013  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
00014  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
00015  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
00016  * SOFTWARE.
00017  */
00018 
00019 #if !defined(_LIBC) && !defined(lint)
00020 static const char rcsid[] = "$BINDId: ns_samedomain.c,v 8.9 1999/10/15 21:06:51 vixie Exp $";
00021 #endif
00022 
00023 #include <sys/types.h>
00024 #include <arpa/nameser.h>
00025 #include <errno.h>
00026 #include <string.h>
00027 
00028 /*%
00029  *     Check whether a name belongs to a domain.
00030  *
00031  * Inputs:
00032  *\li  a - the domain whose ancestory is being verified
00033  *\li  b - the potential ancestor we're checking against
00034  *
00035  * Return:
00036  *\li  boolean - is a at or below b?
00037  *
00038  * Notes:
00039  *\li  Trailing dots are first removed from name and domain.
00040  *     Always compare complete subdomains, not only whether the
00041  *     domain name is the trailing string of the given name.
00042  *
00043  *\li  "host.foobar.top" lies in "foobar.top" and in "top" and in ""
00044  *     but NOT in "bar.top"
00045  */
00046 
00047 int
00048 ns_samedomain(const char *a, const char *b) {
00049        size_t la, lb;
00050        int diff, i, escaped;
00051        const char *cp;
00052 
00053        la = strlen(a);
00054        lb = strlen(b);
00055 
00056        /* Ignore a trailing label separator (i.e. an unescaped dot) in 'a'. */
00057        if (la != 0U && a[la - 1] == '.') {
00058               escaped = 0;
00059               /* Note this loop doesn't get executed if la==1. */
00060               for (i = la - 2; i >= 0; i--)
00061                      if (a[i] == '\\') {
00062                             if (escaped)
00063                                    escaped = 0;
00064                             else
00065                                    escaped = 1;
00066                      } else
00067                             break;
00068               if (!escaped)
00069                      la--;
00070        }
00071 
00072        /* Ignore a trailing label separator (i.e. an unescaped dot) in 'b'. */
00073        if (lb != 0U && b[lb - 1] == '.') {
00074               escaped = 0;
00075               /* note this loop doesn't get executed if lb==1 */
00076               for (i = lb - 2; i >= 0; i--)
00077                      if (b[i] == '\\') {
00078                             if (escaped)
00079                                    escaped = 0;
00080                             else
00081                                    escaped = 1;
00082                      } else
00083                             break;
00084               if (!escaped)
00085                      lb--;
00086        }
00087 
00088        /* lb == 0 means 'b' is the root domain, so 'a' must be in 'b'. */
00089        if (lb == 0U)
00090               return (1);
00091 
00092        /* 'b' longer than 'a' means 'a' can't be in 'b'. */
00093        if (lb > la)
00094               return (0);
00095 
00096        /* 'a' and 'b' being equal at this point indicates sameness. */
00097        if (lb == la)
00098               return (strncasecmp(a, b, lb) == 0);
00099 
00100        /* Ok, we know la > lb. */
00101 
00102        diff = la - lb;
00103 
00104        /*
00105         * If 'a' is only 1 character longer than 'b', then it can't be
00106         * a subdomain of 'b' (because of the need for the '.' label
00107         * separator).
00108         */
00109        if (diff < 2)
00110               return (0);
00111 
00112        /*
00113         * If the character before the last 'lb' characters of 'b'
00114         * isn't '.', then it can't be a match (this lets us avoid
00115         * having "foobar.com" match "bar.com").
00116         */
00117        if (a[diff - 1] != '.')
00118               return (0);
00119 
00120        /*
00121         * We're not sure about that '.', however.  It could be escaped
00122          * and thus not a really a label separator.
00123         */
00124        escaped = 0;
00125        for (i = diff - 2; i >= 0; i--)
00126               if (a[i] == '\\') {
00127                      if (escaped)
00128                             escaped = 0;
00129                      else
00130                             escaped = 1;
00131               } else
00132                      break;
00133        if (escaped)
00134               return (0);
00135 
00136        /* Now compare aligned trailing substring. */
00137        cp = a + diff;
00138        return (strncasecmp(cp, b, lb) == 0);
00139 }
00140 libresolv_hidden_def (ns_samedomain)
00141 
00142 /*%
00143  *     is "a" a subdomain of "b"?
00144  */
00145 int
00146 ns_subdomain(const char *a, const char *b) {
00147        return (ns_samename(a, b) != 1 && ns_samedomain(a, b));
00148 }
00149 
00150 /*%
00151  *     make a canonical copy of domain name "src"
00152  *
00153  * notes:
00154  * \code
00155  *     foo -> foo.
00156  *     foo. -> foo.
00157  *     foo.. -> foo.
00158  *     foo\. -> foo\..
00159  *     foo\\. -> foo\\.
00160  * \endcode
00161  */
00162 
00163 int
00164 ns_makecanon(const char *src, char *dst, size_t dstsize) {
00165        size_t n = strlen(src);
00166 
00167        if (n + sizeof "." > dstsize) {                  /*%< Note: sizeof == 2 */
00168               __set_errno (EMSGSIZE);
00169               return (-1);
00170        }
00171        strcpy(dst, src);
00172        while (n >= 1U && dst[n - 1] == '.')             /*%< Ends in "." */
00173               if (n >= 2U && dst[n - 2] == '\\' &&      /*%< Ends in "\." */
00174                   (n < 3U || dst[n - 3] != '\\'))       /*%< But not "\\." */
00175                      break;
00176               else
00177                      dst[--n] = '\0';
00178        dst[n++] = '.';
00179        dst[n] = '\0';
00180        return (0);
00181 }
00182 libresolv_hidden_def (ns_makecanon)
00183 
00184 /*%
00185  *     determine whether domain name "a" is the same as domain name "b"
00186  *
00187  * return:
00188  *\li  -1 on error
00189  *\li  0 if names differ
00190  *\li  1 if names are the same
00191  */
00192 
00193 int
00194 ns_samename(const char *a, const char *b) {
00195        char ta[NS_MAXDNAME], tb[NS_MAXDNAME];
00196 
00197        if (ns_makecanon(a, ta, sizeof ta) < 0 ||
00198            ns_makecanon(b, tb, sizeof tb) < 0)
00199               return (-1);
00200        if (strcasecmp(ta, tb) == 0)
00201               return (1);
00202        else
00203               return (0);
00204 }
00205 libresolv_hidden_def (ns_samename)
00206 
00207