michael@0: /* $NetBSD: ns_samedomain.c,v 1.2 2004/05/20 20:35:05 christos Exp $ */ michael@0: michael@0: /* michael@0: * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") michael@0: * Copyright (c) 1995,1999 by Internet Software Consortium. michael@0: * michael@0: * Permission to use, copy, modify, and distribute this software for any michael@0: * purpose with or without fee is hereby granted, provided that the above michael@0: * copyright notice and this permission notice appear in all copies. michael@0: * michael@0: * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES michael@0: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF michael@0: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR michael@0: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES michael@0: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN michael@0: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT michael@0: * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. michael@0: */ michael@0: michael@0: /* michael@0: * This version of this file is derived from Android 2.3 "Gingerbread", michael@0: * which contains uncredited changes by Android/Google developers. It has michael@0: * been modified in 2011 for use in the Android build of Mozilla Firefox by michael@0: * Mozilla contributors (including Michael Edwards , michael@0: * and Steve Workman ). michael@0: * These changes are offered under the same license as the original NetBSD michael@0: * file, whose copyright and license are unchanged above. michael@0: */ michael@0: michael@0: #define ANDROID_CHANGES 1 michael@0: #define MOZILLA_NECKO_EXCLUDE_CODE 1 michael@0: michael@0: #include michael@0: #ifndef lint michael@0: #ifdef notdef michael@0: static const char rcsid[] = "Id: ns_samedomain.c,v 1.1.2.2.4.2 2004/03/16 12:34:17 marka Exp"; michael@0: #else michael@0: __RCSID("$NetBSD: ns_samedomain.c,v 1.2 2004/05/20 20:35:05 christos Exp $"); michael@0: #endif michael@0: #endif michael@0: michael@0: #include michael@0: #include "arpa_nameser.h" michael@0: #include michael@0: #include michael@0: michael@0: #ifndef MOZILLA_NECKO_EXCLUDE_CODE michael@0: #ifndef _LIBC michael@0: /* michael@0: * int michael@0: * ns_samedomain(a, b) michael@0: * Check whether a name belongs to a domain. michael@0: * Inputs: michael@0: * a - the domain whose ancestory is being verified michael@0: * b - the potential ancestor we're checking against michael@0: * Return: michael@0: * boolean - is a at or below b? michael@0: * Notes: michael@0: * Trailing dots are first removed from name and domain. michael@0: * Always compare complete subdomains, not only whether the michael@0: * domain name is the trailing string of the given name. michael@0: * michael@0: * "host.foobar.top" lies in "foobar.top" and in "top" and in "" michael@0: * but NOT in "bar.top" michael@0: */ michael@0: michael@0: int michael@0: ns_samedomain(const char *a, const char *b) { michael@0: size_t la, lb; michael@0: int diff, i, escaped; michael@0: const char *cp; michael@0: michael@0: la = strlen(a); michael@0: lb = strlen(b); michael@0: michael@0: /* Ignore a trailing label separator (i.e. an unescaped dot) in 'a'. */ michael@0: if (la != 0U && a[la - 1] == '.') { michael@0: escaped = 0; michael@0: /* Note this loop doesn't get executed if la==1. */ michael@0: for (i = la - 2; i >= 0; i--) michael@0: if (a[i] == '\\') { michael@0: if (escaped) michael@0: escaped = 0; michael@0: else michael@0: escaped = 1; michael@0: } else michael@0: break; michael@0: if (!escaped) michael@0: la--; michael@0: } michael@0: michael@0: /* Ignore a trailing label separator (i.e. an unescaped dot) in 'b'. */ michael@0: if (lb != 0U && b[lb - 1] == '.') { michael@0: escaped = 0; michael@0: /* note this loop doesn't get executed if lb==1 */ michael@0: for (i = lb - 2; i >= 0; i--) michael@0: if (b[i] == '\\') { michael@0: if (escaped) michael@0: escaped = 0; michael@0: else michael@0: escaped = 1; michael@0: } else michael@0: break; michael@0: if (!escaped) michael@0: lb--; michael@0: } michael@0: michael@0: /* lb == 0 means 'b' is the root domain, so 'a' must be in 'b'. */ michael@0: if (lb == 0U) michael@0: return (1); michael@0: michael@0: /* 'b' longer than 'a' means 'a' can't be in 'b'. */ michael@0: if (lb > la) michael@0: return (0); michael@0: michael@0: /* 'a' and 'b' being equal at this point indicates sameness. */ michael@0: if (lb == la) michael@0: return (strncasecmp(a, b, lb) == 0); michael@0: michael@0: /* Ok, we know la > lb. */ michael@0: michael@0: diff = la - lb; michael@0: michael@0: /* michael@0: * If 'a' is only 1 character longer than 'b', then it can't be michael@0: * a subdomain of 'b' (because of the need for the '.' label michael@0: * separator). michael@0: */ michael@0: if (diff < 2) michael@0: return (0); michael@0: michael@0: /* michael@0: * If the character before the last 'lb' characters of 'b' michael@0: * isn't '.', then it can't be a match (this lets us avoid michael@0: * having "foobar.com" match "bar.com"). michael@0: */ michael@0: if (a[diff - 1] != '.') michael@0: return (0); michael@0: michael@0: /* michael@0: * We're not sure about that '.', however. It could be escaped michael@0: * and thus not a really a label separator. michael@0: */ michael@0: escaped = 0; michael@0: for (i = diff - 2; i >= 0; i--) michael@0: if (a[i] == '\\') { michael@0: if (escaped) michael@0: escaped = 0; michael@0: else michael@0: escaped = 1; michael@0: } else michael@0: break; michael@0: if (escaped) michael@0: return (0); michael@0: michael@0: /* Now compare aligned trailing substring. */ michael@0: cp = a + diff; michael@0: return (strncasecmp(cp, b, lb) == 0); michael@0: } michael@0: michael@0: /* michael@0: * int michael@0: * ns_subdomain(a, b) michael@0: * is "a" a subdomain of "b"? michael@0: */ michael@0: int michael@0: ns_subdomain(const char *a, const char *b) { michael@0: return (ns_samename(a, b) != 1 && ns_samedomain(a, b)); michael@0: } michael@0: #endif michael@0: #endif michael@0: michael@0: /* michael@0: * int michael@0: * ns_makecanon(src, dst, dstsize) michael@0: * make a canonical copy of domain name "src" michael@0: * notes: michael@0: * foo -> foo. michael@0: * foo. -> foo. michael@0: * foo.. -> foo. michael@0: * foo\. -> foo\.. michael@0: * foo\\. -> foo\\. michael@0: */ michael@0: michael@0: int michael@0: ns_makecanon(const char *src, char *dst, size_t dstsize) { michael@0: size_t n = strlen(src); michael@0: michael@0: if (n + sizeof "." > dstsize) { /* Note: sizeof == 2 */ michael@0: errno = EMSGSIZE; michael@0: return (-1); michael@0: } michael@0: strcpy(dst, src); michael@0: while (n >= 1U && dst[n - 1] == '.') /* Ends in "." */ michael@0: if (n >= 2U && dst[n - 2] == '\\' && /* Ends in "\." */ michael@0: (n < 3U || dst[n - 3] != '\\')) /* But not "\\." */ michael@0: break; michael@0: else michael@0: dst[--n] = '\0'; michael@0: dst[n++] = '.'; michael@0: dst[n] = '\0'; michael@0: return (0); michael@0: } michael@0: michael@0: /* michael@0: * int michael@0: * ns_samename(a, b) michael@0: * determine whether domain name "a" is the same as domain name "b" michael@0: * return: michael@0: * -1 on error michael@0: * 0 if names differ michael@0: * 1 if names are the same michael@0: */ michael@0: michael@0: int michael@0: ns_samename(const char *a, const char *b) { michael@0: char ta[NS_MAXDNAME], tb[NS_MAXDNAME]; michael@0: michael@0: if (ns_makecanon(a, ta, sizeof ta) < 0 || michael@0: ns_makecanon(b, tb, sizeof tb) < 0) michael@0: return (-1); michael@0: if (strcasecmp(ta, tb) == 0) michael@0: return (1); michael@0: else michael@0: return (0); michael@0: }