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