2 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (c) 1995,1999 by Internet Software Consortium.
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
15 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 static const char rcsid[] = "$Id: ns_samedomain.c,v 1.1.2.2.4.2 2004/03/16 12:34:17 marka Exp $";
22 #include "port_before.h"
24 #include <sys/types.h>
25 #include <arpa/nameser.h>
29 #include "port_after.h"
34 * Check whether a name belongs to a domain.
36 * a - the domain whose ancestory is being verified
37 * b - the potential ancestor we're checking against
39 * boolean - is a at or below b?
41 * Trailing dots are first removed from name and domain.
42 * Always compare complete subdomains, not only whether the
43 * domain name is the trailing string of the given name.
45 * "host.foobar.top" lies in "foobar.top" and in "top" and in ""
46 * but NOT in "bar.top"
50 ns_samedomain(const char *a, const char *b) {
58 /* Ignore a trailing label separator (i.e. an unescaped dot) in 'a'. */
59 if (la != 0U && a[la - 1] == '.') {
61 /* Note this loop doesn't get executed if la==1. */
62 for (i = la - 2; i >= 0; i--)
74 /* Ignore a trailing label separator (i.e. an unescaped dot) in 'b'. */
75 if (lb != 0U && b[lb - 1] == '.') {
77 /* note this loop doesn't get executed if lb==1 */
78 for (i = lb - 2; i >= 0; i--)
90 /* lb == 0 means 'b' is the root domain, so 'a' must be in 'b'. */
94 /* 'b' longer than 'a' means 'a' can't be in 'b'. */
98 /* 'a' and 'b' being equal at this point indicates sameness. */
100 return (strncasecmp(a, b, lb) == 0);
102 /* Ok, we know la > lb. */
107 * If 'a' is only 1 character longer than 'b', then it can't be
108 * a subdomain of 'b' (because of the need for the '.' label
115 * If the character before the last 'lb' characters of 'b'
116 * isn't '.', then it can't be a match (this lets us avoid
117 * having "foobar.com" match "bar.com").
119 if (a[diff - 1] != '.')
123 * We're not sure about that '.', however. It could be escaped
124 * and thus not a really a label separator.
127 for (i = diff - 2; i >= 0; i--)
138 /* Now compare aligned trailing substring. */
140 return (strncasecmp(cp, b, lb) == 0);
147 * is "a" a subdomain of "b"?
150 ns_subdomain(const char *a, const char *b) {
151 return (ns_samename(a, b) != 1 && ns_samedomain(a, b));
157 * ns_makecanon(src, dst, dstsize)
158 * make a canonical copy of domain name "src"
168 ns_makecanon(const char *src, char *dst, size_t dstsize) {
169 size_t n = strlen(src);
171 if (n + sizeof "." > dstsize) { /* Note: sizeof == 2 */
176 while (n >= 1U && dst[n - 1] == '.') /* Ends in "." */
177 if (n >= 2U && dst[n - 2] == '\\' && /* Ends in "\." */
178 (n < 3U || dst[n - 3] != '\\')) /* But not "\\." */
190 * determine whether domain name "a" is the same as domain name "b"
194 * 1 if names are the same
198 ns_samename(const char *a, const char *b) {
199 char ta[NS_MAXDNAME], tb[NS_MAXDNAME];
201 if (ns_makecanon(a, ta, sizeof ta) < 0 ||
202 ns_makecanon(b, tb, sizeof tb) < 0)
204 if (strcasecmp(ta, tb) == 0)