Home | History | Annotate | Download | only in nameser
      1 /*	$NetBSD: ns_samedomain.c,v 1.8 2012/11/22 20:22:31 christos Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
      5  * Copyright (c) 1995,1999 by Internet Software Consortium.
      6  *
      7  * Permission to use, copy, modify, and distribute this software for any
      8  * purpose with or without fee is hereby granted, provided that the above
      9  * copyright notice and this permission notice appear in all copies.
     10  *
     11  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
     12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     13  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
     14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
     17  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     18  */
     19 
     20 #include <sys/cdefs.h>
     21 #ifndef lint
     22 #ifdef notdef
     23 static const char rcsid[] = "Id: ns_samedomain.c,v 1.6 2005/04/27 04:56:40 sra Exp";
     24 #else
     25 __RCSID("$NetBSD: ns_samedomain.c,v 1.8 2012/11/22 20:22:31 christos Exp $");
     26 #endif
     27 #endif
     28 
     29 #include <sys/types.h>
     30 #include <arpa/nameser.h>
     31 #include <errno.h>
     32 #include <string.h>
     33 
     34 #ifdef _LIBRESOLV
     35 /*
     36  *	Check whether a name belongs to a domain.
     37  *
     38  * Inputs:
     39  *	a - the domain whose ancestory is being verified
     40  *	b - the potential ancestor we're checking against
     41  *
     42  * Return:
     43  *	boolean - is a at or below b?
     44  *
     45  * Notes:
     46  *	Trailing dots are first removed from name and domain.
     47  *	Always compare complete subdomains, not only whether the
     48  *	domain name is the trailing string of the given name.
     49  *
     50  *	"host.foobar.top" lies in "foobar.top" and in "top" and in ""
     51  *	but NOT in "bar.top"
     52  */
     53 
     54 int
     55 ns_samedomain(const char *a, const char *b) {
     56 	size_t la, lb, i;
     57 	int diff, escaped;
     58 	const char *cp;
     59 
     60 	la = strlen(a);
     61 	lb = strlen(b);
     62 
     63 	/* Ignore a trailing label separator (i.e. an unescaped dot) in 'a'. */
     64 	if (la != 0U && a[la - 1] == '.') {
     65 		escaped = 0;
     66 		/* Note this loop doesn't get executed if la==1. */
     67 		for (i = la - 1; i > 0; i--)
     68 			if (a[i - 1] == '\\') {
     69 				if (escaped)
     70 					escaped = 0;
     71 				else
     72 					escaped = 1;
     73 			} else
     74 				break;
     75 		if (!escaped)
     76 			la--;
     77 	}
     78 
     79 	/* Ignore a trailing label separator (i.e. an unescaped dot) in 'b'. */
     80 	if (lb != 0U && b[lb - 1] == '.') {
     81 		escaped = 0;
     82 		/* note this loop doesn't get executed if lb==1 */
     83 		for (i = lb - 1; i > 0; i--)
     84 			if (b[i - 1] == '\\') {
     85 				if (escaped)
     86 					escaped = 0;
     87 				else
     88 					escaped = 1;
     89 			} else
     90 				break;
     91 		if (!escaped)
     92 			lb--;
     93 	}
     94 
     95 	/* lb == 0 means 'b' is the root domain, so 'a' must be in 'b'. */
     96 	if (lb == 0U)
     97 		return (1);
     98 
     99 	/* 'b' longer than 'a' means 'a' can't be in 'b'. */
    100 	if (lb > la)
    101 		return (0);
    102 
    103 	/* 'a' and 'b' being equal at this point indicates sameness. */
    104 	if (lb == la)
    105 		return (strncasecmp(a, b, lb) == 0);
    106 
    107 	/* Ok, we know la > lb. */
    108 
    109 	diff = (int)(la - lb);
    110 
    111 	/*
    112 	 * If 'a' is only 1 character longer than 'b', then it can't be
    113 	 * a subdomain of 'b' (because of the need for the '.' label
    114 	 * separator).
    115 	 */
    116 	if (diff < 2)
    117 		return (0);
    118 
    119 	/*
    120 	 * If the character before the last 'lb' characters of 'b'
    121 	 * isn't '.', then it can't be a match (this lets us avoid
    122 	 * having "foobar.com" match "bar.com").
    123 	 */
    124 	if (a[diff - 1] != '.')
    125 		return (0);
    126 
    127 	/*
    128 	 * We're not sure about that '.', however.  It could be escaped
    129          * and thus not a really a label separator.
    130 	 */
    131 	escaped = 0;
    132 	for (i = diff - 1; i > 0; i--)
    133 		if (a[i - 1] == '\\') {
    134 			if (escaped)
    135 				escaped = 0;
    136 			else
    137 				escaped = 1;
    138 		} else
    139 			break;
    140 	if (escaped)
    141 		return (0);
    142 
    143 	/* Now compare aligned trailing substring. */
    144 	cp = a + diff;
    145 	return (strncasecmp(cp, b, lb) == 0);
    146 }
    147 
    148 /*
    149  *	is "a" a subdomain of "b"?
    150  */
    151 int
    152 ns_subdomain(const char *a, const char *b) {
    153 	return (ns_samename(a, b) != 1 && ns_samedomain(a, b));
    154 }
    155 #endif
    156 
    157 #ifdef _LIBC
    158 /*
    159  *	make a canonical copy of domain name "src"
    160  *
    161  * notes:
    162  *	foo -> foo.
    163  *	foo. -> foo.
    164  *	foo.. -> foo.
    165  *	foo\. -> foo\..
    166  *	foo\\. -> foo\\.
    167  */
    168 
    169 int
    170 ns_makecanon(const char *src, char *dst, size_t dstsize) {
    171 	size_t n = strlen(src);
    172 
    173 	if (n + sizeof "." > dstsize) {			/* Note: sizeof == 2 */
    174 		errno = EMSGSIZE;
    175 		return (-1);
    176 	}
    177 	strcpy(dst, src);
    178 	while (n >= 1U && dst[n - 1] == '.')		/* Ends in "." */
    179 		if (n >= 2U && dst[n - 2] == '\\' &&	/* Ends in "\." */
    180 		    (n < 3U || dst[n - 3] != '\\'))	/* But not "\\." */
    181 			break;
    182 		else
    183 			dst[--n] = '\0';
    184 	dst[n++] = '.';
    185 	dst[n] = '\0';
    186 	return (0);
    187 }
    188 
    189 /*
    190  *	determine whether domain name "a" is the same as domain name "b"
    191  *
    192  * return:
    193  *	-1 on error
    194  *	0 if names differ
    195  *	1 if names are the same
    196  */
    197 
    198 int
    199 ns_samename(const char *a, const char *b) {
    200 	char ta[NS_MAXDNAME], tb[NS_MAXDNAME];
    201 
    202 	if (ns_makecanon(a, ta, sizeof ta) < 0 ||
    203 	    ns_makecanon(b, tb, sizeof tb) < 0)
    204 		return (-1);
    205 	if (strcasecmp(ta, tb) == 0)
    206 		return (1);
    207 	else
    208 		return (0);
    209 }
    210 #endif
    211