xref: /onnv-gate/usr/src/lib/libresolv2/common/nameser/ns_samedomain.c (revision 11038:74b12212b8a2)
10Sstevel@tonic-gate /*
2*11038SRao.Shoaib@Sun.COM  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
30Sstevel@tonic-gate  * Copyright (c) 1995,1999 by Internet Software Consortium.
40Sstevel@tonic-gate  *
50Sstevel@tonic-gate  * Permission to use, copy, modify, and distribute this software for any
60Sstevel@tonic-gate  * purpose with or without fee is hereby granted, provided that the above
70Sstevel@tonic-gate  * copyright notice and this permission notice appear in all copies.
80Sstevel@tonic-gate  *
9*11038SRao.Shoaib@Sun.COM  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
10*11038SRao.Shoaib@Sun.COM  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11*11038SRao.Shoaib@Sun.COM  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
12*11038SRao.Shoaib@Sun.COM  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13*11038SRao.Shoaib@Sun.COM  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14*11038SRao.Shoaib@Sun.COM  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
15*11038SRao.Shoaib@Sun.COM  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
160Sstevel@tonic-gate  */
170Sstevel@tonic-gate 
180Sstevel@tonic-gate #ifndef lint
19*11038SRao.Shoaib@Sun.COM static const char rcsid[] = "$Id: ns_samedomain.c,v 1.6 2005/04/27 04:56:40 sra Exp $";
200Sstevel@tonic-gate #endif
210Sstevel@tonic-gate 
220Sstevel@tonic-gate #include "port_before.h"
230Sstevel@tonic-gate 
240Sstevel@tonic-gate #include <sys/types.h>
250Sstevel@tonic-gate #include <arpa/nameser.h>
260Sstevel@tonic-gate #include <errno.h>
270Sstevel@tonic-gate #include <string.h>
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include "port_after.h"
300Sstevel@tonic-gate 
31*11038SRao.Shoaib@Sun.COM /*%
320Sstevel@tonic-gate  *	Check whether a name belongs to a domain.
33*11038SRao.Shoaib@Sun.COM  *
340Sstevel@tonic-gate  * Inputs:
35*11038SRao.Shoaib@Sun.COM  *\li	a - the domain whose ancestory is being verified
36*11038SRao.Shoaib@Sun.COM  *\li	b - the potential ancestor we're checking against
37*11038SRao.Shoaib@Sun.COM  *
380Sstevel@tonic-gate  * Return:
39*11038SRao.Shoaib@Sun.COM  *\li	boolean - is a at or below b?
40*11038SRao.Shoaib@Sun.COM  *
410Sstevel@tonic-gate  * Notes:
42*11038SRao.Shoaib@Sun.COM  *\li	Trailing dots are first removed from name and domain.
430Sstevel@tonic-gate  *	Always compare complete subdomains, not only whether the
440Sstevel@tonic-gate  *	domain name is the trailing string of the given name.
450Sstevel@tonic-gate  *
46*11038SRao.Shoaib@Sun.COM  *\li	"host.foobar.top" lies in "foobar.top" and in "top" and in ""
470Sstevel@tonic-gate  *	but NOT in "bar.top"
480Sstevel@tonic-gate  */
490Sstevel@tonic-gate 
500Sstevel@tonic-gate int
ns_samedomain(const char * a,const char * b)510Sstevel@tonic-gate ns_samedomain(const char *a, const char *b) {
520Sstevel@tonic-gate 	size_t la, lb;
530Sstevel@tonic-gate 	int diff, i, escaped;
540Sstevel@tonic-gate 	const char *cp;
550Sstevel@tonic-gate 
560Sstevel@tonic-gate 	la = strlen(a);
570Sstevel@tonic-gate 	lb = strlen(b);
580Sstevel@tonic-gate 
590Sstevel@tonic-gate 	/* Ignore a trailing label separator (i.e. an unescaped dot) in 'a'. */
60*11038SRao.Shoaib@Sun.COM 	if (la != 0U && a[la - 1] == '.') {
610Sstevel@tonic-gate 		escaped = 0;
620Sstevel@tonic-gate 		/* Note this loop doesn't get executed if la==1. */
630Sstevel@tonic-gate 		for (i = la - 2; i >= 0; i--)
640Sstevel@tonic-gate 			if (a[i] == '\\') {
650Sstevel@tonic-gate 				if (escaped)
660Sstevel@tonic-gate 					escaped = 0;
670Sstevel@tonic-gate 				else
680Sstevel@tonic-gate 					escaped = 1;
690Sstevel@tonic-gate 			} else
700Sstevel@tonic-gate 				break;
710Sstevel@tonic-gate 		if (!escaped)
720Sstevel@tonic-gate 			la--;
730Sstevel@tonic-gate 	}
740Sstevel@tonic-gate 
750Sstevel@tonic-gate 	/* Ignore a trailing label separator (i.e. an unescaped dot) in 'b'. */
76*11038SRao.Shoaib@Sun.COM 	if (lb != 0U && b[lb - 1] == '.') {
770Sstevel@tonic-gate 		escaped = 0;
780Sstevel@tonic-gate 		/* note this loop doesn't get executed if lb==1 */
790Sstevel@tonic-gate 		for (i = lb - 2; i >= 0; i--)
800Sstevel@tonic-gate 			if (b[i] == '\\') {
810Sstevel@tonic-gate 				if (escaped)
820Sstevel@tonic-gate 					escaped = 0;
830Sstevel@tonic-gate 				else
840Sstevel@tonic-gate 					escaped = 1;
850Sstevel@tonic-gate 			} else
860Sstevel@tonic-gate 				break;
870Sstevel@tonic-gate 		if (!escaped)
880Sstevel@tonic-gate 			lb--;
890Sstevel@tonic-gate 	}
900Sstevel@tonic-gate 
910Sstevel@tonic-gate 	/* lb == 0 means 'b' is the root domain, so 'a' must be in 'b'. */
92*11038SRao.Shoaib@Sun.COM 	if (lb == 0U)
930Sstevel@tonic-gate 		return (1);
940Sstevel@tonic-gate 
950Sstevel@tonic-gate 	/* 'b' longer than 'a' means 'a' can't be in 'b'. */
960Sstevel@tonic-gate 	if (lb > la)
970Sstevel@tonic-gate 		return (0);
980Sstevel@tonic-gate 
990Sstevel@tonic-gate 	/* 'a' and 'b' being equal at this point indicates sameness. */
1000Sstevel@tonic-gate 	if (lb == la)
1010Sstevel@tonic-gate 		return (strncasecmp(a, b, lb) == 0);
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate 	/* Ok, we know la > lb. */
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate 	diff = la - lb;
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate 	/*
1080Sstevel@tonic-gate 	 * If 'a' is only 1 character longer than 'b', then it can't be
1090Sstevel@tonic-gate 	 * a subdomain of 'b' (because of the need for the '.' label
1100Sstevel@tonic-gate 	 * separator).
1110Sstevel@tonic-gate 	 */
1120Sstevel@tonic-gate 	if (diff < 2)
1130Sstevel@tonic-gate 		return (0);
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate 	/*
1160Sstevel@tonic-gate 	 * If the character before the last 'lb' characters of 'b'
1170Sstevel@tonic-gate 	 * isn't '.', then it can't be a match (this lets us avoid
1180Sstevel@tonic-gate 	 * having "foobar.com" match "bar.com").
1190Sstevel@tonic-gate 	 */
1200Sstevel@tonic-gate 	if (a[diff - 1] != '.')
1210Sstevel@tonic-gate 		return (0);
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate 	/*
1240Sstevel@tonic-gate 	 * We're not sure about that '.', however.  It could be escaped
1250Sstevel@tonic-gate          * and thus not a really a label separator.
1260Sstevel@tonic-gate 	 */
1270Sstevel@tonic-gate 	escaped = 0;
1280Sstevel@tonic-gate 	for (i = diff - 2; i >= 0; i--)
1290Sstevel@tonic-gate 		if (a[i] == '\\') {
1300Sstevel@tonic-gate 			if (escaped)
1310Sstevel@tonic-gate 				escaped = 0;
1320Sstevel@tonic-gate 			else
1330Sstevel@tonic-gate 				escaped = 1;
1340Sstevel@tonic-gate 		} else
1350Sstevel@tonic-gate 			break;
1360Sstevel@tonic-gate 	if (escaped)
1370Sstevel@tonic-gate 		return (0);
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate 	/* Now compare aligned trailing substring. */
1400Sstevel@tonic-gate 	cp = a + diff;
1410Sstevel@tonic-gate 	return (strncasecmp(cp, b, lb) == 0);
1420Sstevel@tonic-gate }
1430Sstevel@tonic-gate 
144*11038SRao.Shoaib@Sun.COM /*%
1450Sstevel@tonic-gate  *	is "a" a subdomain of "b"?
1460Sstevel@tonic-gate  */
1470Sstevel@tonic-gate int
ns_subdomain(const char * a,const char * b)1480Sstevel@tonic-gate ns_subdomain(const char *a, const char *b) {
1490Sstevel@tonic-gate 	return (ns_samename(a, b) != 1 && ns_samedomain(a, b));
1500Sstevel@tonic-gate }
1510Sstevel@tonic-gate 
152*11038SRao.Shoaib@Sun.COM /*%
1530Sstevel@tonic-gate  *	make a canonical copy of domain name "src"
154*11038SRao.Shoaib@Sun.COM  *
1550Sstevel@tonic-gate  * notes:
156*11038SRao.Shoaib@Sun.COM  * \code
1570Sstevel@tonic-gate  *	foo -> foo.
1580Sstevel@tonic-gate  *	foo. -> foo.
1590Sstevel@tonic-gate  *	foo.. -> foo.
1600Sstevel@tonic-gate  *	foo\. -> foo\..
1610Sstevel@tonic-gate  *	foo\\. -> foo\\.
162*11038SRao.Shoaib@Sun.COM  * \endcode
1630Sstevel@tonic-gate  */
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate int
ns_makecanon(const char * src,char * dst,size_t dstsize)1660Sstevel@tonic-gate ns_makecanon(const char *src, char *dst, size_t dstsize) {
1670Sstevel@tonic-gate 	size_t n = strlen(src);
1680Sstevel@tonic-gate 
169*11038SRao.Shoaib@Sun.COM 	if (n + sizeof "." > dstsize) {			/*%< Note: sizeof == 2 */
1700Sstevel@tonic-gate 		errno = EMSGSIZE;
1710Sstevel@tonic-gate 		return (-1);
1720Sstevel@tonic-gate 	}
1730Sstevel@tonic-gate 	strcpy(dst, src);
174*11038SRao.Shoaib@Sun.COM 	while (n >= 1U && dst[n - 1] == '.')		/*%< Ends in "." */
175*11038SRao.Shoaib@Sun.COM 		if (n >= 2U && dst[n - 2] == '\\' &&	/*%< Ends in "\." */
176*11038SRao.Shoaib@Sun.COM 		    (n < 3U || dst[n - 3] != '\\'))	/*%< But not "\\." */
1770Sstevel@tonic-gate 			break;
1780Sstevel@tonic-gate 		else
1790Sstevel@tonic-gate 			dst[--n] = '\0';
1800Sstevel@tonic-gate 	dst[n++] = '.';
1810Sstevel@tonic-gate 	dst[n] = '\0';
1820Sstevel@tonic-gate 	return (0);
1830Sstevel@tonic-gate }
1840Sstevel@tonic-gate 
185*11038SRao.Shoaib@Sun.COM /*%
1860Sstevel@tonic-gate  *	determine whether domain name "a" is the same as domain name "b"
187*11038SRao.Shoaib@Sun.COM  *
1880Sstevel@tonic-gate  * return:
189*11038SRao.Shoaib@Sun.COM  *\li	-1 on error
190*11038SRao.Shoaib@Sun.COM  *\li	0 if names differ
191*11038SRao.Shoaib@Sun.COM  *\li	1 if names are the same
1920Sstevel@tonic-gate  */
1930Sstevel@tonic-gate 
1940Sstevel@tonic-gate int
ns_samename(const char * a,const char * b)1950Sstevel@tonic-gate ns_samename(const char *a, const char *b) {
1960Sstevel@tonic-gate 	char ta[NS_MAXDNAME], tb[NS_MAXDNAME];
1970Sstevel@tonic-gate 
1980Sstevel@tonic-gate 	if (ns_makecanon(a, ta, sizeof ta) < 0 ||
1990Sstevel@tonic-gate 	    ns_makecanon(b, tb, sizeof tb) < 0)
2000Sstevel@tonic-gate 		return (-1);
2010Sstevel@tonic-gate 	if (strcasecmp(ta, tb) == 0)
2020Sstevel@tonic-gate 		return (1);
2030Sstevel@tonic-gate 	else
2040Sstevel@tonic-gate 		return (0);
2050Sstevel@tonic-gate }
206*11038SRao.Shoaib@Sun.COM 
207*11038SRao.Shoaib@Sun.COM /*! \file */
208