Home | History | Annotate | Line # | Download | only in nameser
ns_samedomain.c revision 1.2
      1  1.2  christos /*	$NetBSD: ns_samedomain.c,v 1.2 2004/05/20 20:35:05 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.2  christos #include <sys/cdefs.h>
     21  1.1  christos #ifndef lint
     22  1.2  christos #ifdef notdef
     23  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";
     24  1.2  christos #else
     25  1.2  christos __RCSID("$NetBSD: ns_samedomain.c,v 1.2 2004/05/20 20:35:05 christos Exp $");
     26  1.2  christos #endif
     27  1.1  christos #endif
     28  1.1  christos 
     29  1.1  christos #include "port_before.h"
     30  1.1  christos 
     31  1.1  christos #include <sys/types.h>
     32  1.1  christos #include <arpa/nameser.h>
     33  1.1  christos #include <errno.h>
     34  1.1  christos #include <string.h>
     35  1.1  christos 
     36  1.1  christos #include "port_after.h"
     37  1.1  christos 
     38  1.2  christos #ifndef _LIBC
     39  1.1  christos /*
     40  1.1  christos  * int
     41  1.1  christos  * ns_samedomain(a, b)
     42  1.1  christos  *	Check whether a name belongs to a domain.
     43  1.1  christos  * Inputs:
     44  1.1  christos  *	a - the domain whose ancestory is being verified
     45  1.1  christos  *	b - the potential ancestor we're checking against
     46  1.1  christos  * Return:
     47  1.1  christos  *	boolean - is a at or below b?
     48  1.1  christos  * Notes:
     49  1.1  christos  *	Trailing dots are first removed from name and domain.
     50  1.1  christos  *	Always compare complete subdomains, not only whether the
     51  1.1  christos  *	domain name is the trailing string of the given name.
     52  1.1  christos  *
     53  1.1  christos  *	"host.foobar.top" lies in "foobar.top" and in "top" and in ""
     54  1.1  christos  *	but NOT in "bar.top"
     55  1.1  christos  */
     56  1.1  christos 
     57  1.1  christos int
     58  1.1  christos ns_samedomain(const char *a, const char *b) {
     59  1.1  christos 	size_t la, lb;
     60  1.1  christos 	int diff, i, escaped;
     61  1.1  christos 	const char *cp;
     62  1.1  christos 
     63  1.1  christos 	la = strlen(a);
     64  1.1  christos 	lb = strlen(b);
     65  1.1  christos 
     66  1.1  christos 	/* Ignore a trailing label separator (i.e. an unescaped dot) in 'a'. */
     67  1.1  christos 	if (la != 0U && a[la - 1] == '.') {
     68  1.1  christos 		escaped = 0;
     69  1.1  christos 		/* Note this loop doesn't get executed if la==1. */
     70  1.1  christos 		for (i = la - 2; i >= 0; i--)
     71  1.1  christos 			if (a[i] == '\\') {
     72  1.1  christos 				if (escaped)
     73  1.1  christos 					escaped = 0;
     74  1.1  christos 				else
     75  1.1  christos 					escaped = 1;
     76  1.1  christos 			} else
     77  1.1  christos 				break;
     78  1.1  christos 		if (!escaped)
     79  1.1  christos 			la--;
     80  1.1  christos 	}
     81  1.1  christos 
     82  1.1  christos 	/* Ignore a trailing label separator (i.e. an unescaped dot) in 'b'. */
     83  1.1  christos 	if (lb != 0U && b[lb - 1] == '.') {
     84  1.1  christos 		escaped = 0;
     85  1.1  christos 		/* note this loop doesn't get executed if lb==1 */
     86  1.1  christos 		for (i = lb - 2; i >= 0; i--)
     87  1.1  christos 			if (b[i] == '\\') {
     88  1.1  christos 				if (escaped)
     89  1.1  christos 					escaped = 0;
     90  1.1  christos 				else
     91  1.1  christos 					escaped = 1;
     92  1.1  christos 			} else
     93  1.1  christos 				break;
     94  1.1  christos 		if (!escaped)
     95  1.1  christos 			lb--;
     96  1.1  christos 	}
     97  1.1  christos 
     98  1.1  christos 	/* lb == 0 means 'b' is the root domain, so 'a' must be in 'b'. */
     99  1.1  christos 	if (lb == 0U)
    100  1.1  christos 		return (1);
    101  1.1  christos 
    102  1.1  christos 	/* 'b' longer than 'a' means 'a' can't be in 'b'. */
    103  1.1  christos 	if (lb > la)
    104  1.1  christos 		return (0);
    105  1.1  christos 
    106  1.1  christos 	/* 'a' and 'b' being equal at this point indicates sameness. */
    107  1.1  christos 	if (lb == la)
    108  1.1  christos 		return (strncasecmp(a, b, lb) == 0);
    109  1.1  christos 
    110  1.1  christos 	/* Ok, we know la > lb. */
    111  1.1  christos 
    112  1.1  christos 	diff = la - lb;
    113  1.1  christos 
    114  1.1  christos 	/*
    115  1.1  christos 	 * If 'a' is only 1 character longer than 'b', then it can't be
    116  1.1  christos 	 * a subdomain of 'b' (because of the need for the '.' label
    117  1.1  christos 	 * separator).
    118  1.1  christos 	 */
    119  1.1  christos 	if (diff < 2)
    120  1.1  christos 		return (0);
    121  1.1  christos 
    122  1.1  christos 	/*
    123  1.1  christos 	 * If the character before the last 'lb' characters of 'b'
    124  1.1  christos 	 * isn't '.', then it can't be a match (this lets us avoid
    125  1.1  christos 	 * having "foobar.com" match "bar.com").
    126  1.1  christos 	 */
    127  1.1  christos 	if (a[diff - 1] != '.')
    128  1.1  christos 		return (0);
    129  1.1  christos 
    130  1.1  christos 	/*
    131  1.1  christos 	 * We're not sure about that '.', however.  It could be escaped
    132  1.1  christos          * and thus not a really a label separator.
    133  1.1  christos 	 */
    134  1.1  christos 	escaped = 0;
    135  1.1  christos 	for (i = diff - 2; i >= 0; i--)
    136  1.1  christos 		if (a[i] == '\\') {
    137  1.1  christos 			if (escaped)
    138  1.1  christos 				escaped = 0;
    139  1.1  christos 			else
    140  1.1  christos 				escaped = 1;
    141  1.1  christos 		} else
    142  1.1  christos 			break;
    143  1.1  christos 	if (escaped)
    144  1.1  christos 		return (0);
    145  1.1  christos 
    146  1.1  christos 	/* Now compare aligned trailing substring. */
    147  1.1  christos 	cp = a + diff;
    148  1.1  christos 	return (strncasecmp(cp, b, lb) == 0);
    149  1.1  christos }
    150  1.1  christos 
    151  1.1  christos /*
    152  1.1  christos  * int
    153  1.1  christos  * ns_subdomain(a, b)
    154  1.1  christos  *	is "a" a subdomain of "b"?
    155  1.1  christos  */
    156  1.1  christos int
    157  1.1  christos ns_subdomain(const char *a, const char *b) {
    158  1.1  christos 	return (ns_samename(a, b) != 1 && ns_samedomain(a, b));
    159  1.1  christos }
    160  1.2  christos #endif
    161  1.1  christos 
    162  1.1  christos /*
    163  1.1  christos  * int
    164  1.1  christos  * ns_makecanon(src, dst, dstsize)
    165  1.1  christos  *	make a canonical copy of domain name "src"
    166  1.1  christos  * notes:
    167  1.1  christos  *	foo -> foo.
    168  1.1  christos  *	foo. -> foo.
    169  1.1  christos  *	foo.. -> foo.
    170  1.1  christos  *	foo\. -> foo\..
    171  1.1  christos  *	foo\\. -> foo\\.
    172  1.1  christos  */
    173  1.1  christos 
    174  1.1  christos int
    175  1.1  christos ns_makecanon(const char *src, char *dst, size_t dstsize) {
    176  1.1  christos 	size_t n = strlen(src);
    177  1.1  christos 
    178  1.1  christos 	if (n + sizeof "." > dstsize) {			/* Note: sizeof == 2 */
    179  1.1  christos 		errno = EMSGSIZE;
    180  1.1  christos 		return (-1);
    181  1.1  christos 	}
    182  1.1  christos 	strcpy(dst, src);
    183  1.1  christos 	while (n >= 1U && dst[n - 1] == '.')		/* Ends in "." */
    184  1.1  christos 		if (n >= 2U && dst[n - 2] == '\\' &&	/* Ends in "\." */
    185  1.1  christos 		    (n < 3U || dst[n - 3] != '\\'))	/* But not "\\." */
    186  1.1  christos 			break;
    187  1.1  christos 		else
    188  1.1  christos 			dst[--n] = '\0';
    189  1.1  christos 	dst[n++] = '.';
    190  1.1  christos 	dst[n] = '\0';
    191  1.1  christos 	return (0);
    192  1.1  christos }
    193  1.1  christos 
    194  1.1  christos /*
    195  1.1  christos  * int
    196  1.1  christos  * ns_samename(a, b)
    197  1.1  christos  *	determine whether domain name "a" is the same as domain name "b"
    198  1.1  christos  * return:
    199  1.1  christos  *	-1 on error
    200  1.1  christos  *	0 if names differ
    201  1.1  christos  *	1 if names are the same
    202  1.1  christos  */
    203  1.1  christos 
    204  1.1  christos int
    205  1.1  christos ns_samename(const char *a, const char *b) {
    206  1.1  christos 	char ta[NS_MAXDNAME], tb[NS_MAXDNAME];
    207  1.1  christos 
    208  1.1  christos 	if (ns_makecanon(a, ta, sizeof ta) < 0 ||
    209  1.1  christos 	    ns_makecanon(b, tb, sizeof tb) < 0)
    210  1.1  christos 		return (-1);
    211  1.1  christos 	if (strcasecmp(ta, tb) == 0)
    212  1.1  christos 		return (1);
    213  1.1  christos 	else
    214  1.1  christos 		return (0);
    215  1.1  christos }
    216