2 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (c) 1995-2003 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.
17 * Internet Systems Consortium, Inc.
19 * Redwood City, CA 94063
25 static const char rcsid[] = "$Id: ns_samedomain.c,v 1.3.2.4 2004/06/10 17:59:41 dhankins Exp $";
28 #include <sys/types.h>
32 #include <netinet/in.h>
33 #include <sys/socket.h>
35 #include "minires/minires.h"
36 #include "arpa/nameser.h"
41 * Check whether a name belongs to a domain.
43 * a - the domain whose ancestory is being verified
44 * b - the potential ancestor we're checking against
46 * boolean - is a at or below b?
48 * Trailing dots are first removed from name and domain.
49 * Always compare complete subdomains, not only whether the
50 * domain name is the trailing string of the given name.
52 * "host.foobar.top" lies in "foobar.top" and in "top" and in ""
53 * but NOT in "bar.top"
57 ns_samedomain(const char *a, const char *b) {
65 /* Ignore a trailing label separator (i.e. an unescaped dot) in 'a'. */
66 if (la != 0 && a[la - 1] == '.') {
68 /* Note this loop doesn't get executed if la==1. */
69 for (i = la - 2; i >= 0; i--)
81 /* Ignore a trailing label separator (i.e. an unescaped dot) in 'b'. */
82 if (lb != 0 && b[lb - 1] == '.') {
84 /* note this loop doesn't get executed if lb==1 */
85 for (i = lb - 2; i >= 0; i--)
97 /* lb == 0 means 'b' is the root domain, so 'a' must be in 'b'. */
101 /* 'b' longer than 'a' means 'a' can't be in 'b'. */
105 /* 'a' and 'b' being equal at this point indicates sameness. */
107 return (strncasecmp(a, b, lb) == 0);
109 /* Ok, we know la > lb. */
114 * If 'a' is only 1 character longer than 'b', then it can't be
115 * a subdomain of 'b' (because of the need for the '.' label
122 * If the character before the last 'lb' characters of 'b'
123 * isn't '.', then it can't be a match (this lets us avoid
124 * having "foobar.com" match "bar.com").
126 if (a[diff - 1] != '.')
130 * We're not sure about that '.', however. It could be escaped
131 * and thus not a really a label separator.
134 for (i = diff - 2; i >= 0; i--)
145 /* Now compare aligned trailing substring. */
147 return (strncasecmp(cp, b, lb) == 0);
153 * is "a" a subdomain of "b"?
156 ns_subdomain(const char *a, const char *b) {
157 return (ns_samename(a, b) != 1 && ns_samedomain(a, b));
162 * ns_makecanon(src, dst, dstsize)
163 * make a canonical copy of domain name "src"
173 ns_makecanon(const char *src, char *dst, size_t dstsize) {
174 size_t n = strlen(src);
176 if (n + sizeof "." > dstsize) {
177 return ISC_R_NOSPACE;
180 while (n > 0 && dst[n - 1] == '.') /* Ends in "." */
181 if (n > 1 && dst[n - 2] == '\\' && /* Ends in "\." */
182 (n < 2 || dst[n - 3] != '\\')) /* But not "\\." */
188 return ISC_R_SUCCESS;
194 * determine whether domain name "a" is the same as domain name "b"
198 * 1 if names are the same
202 ns_samename(const char *a, const char *b) {
203 char ta[NS_MAXDNAME], tb[NS_MAXDNAME];
206 status = ns_makecanon(a, ta, sizeof ta);
207 if (status != ISC_R_SUCCESS)
209 status = ns_makecanon(b, tb, sizeof tb);
210 if (status != ISC_R_SUCCESS)
212 if (strcasecmp(ta, tb) == 0)