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 INTERNET SOFTWARE CONSORTIUM DISCLAIMS
10 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
11 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
12 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
13 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
14 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
15 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
19 #include <sys/types.h>
20 #include <arpa/nameser.h>
25 * Check whether a name belongs to a domain.
28 *\li a - the domain whose ancestry is being verified
29 *\li b - the potential ancestor we're checking against
32 *\li boolean - is a at or below b?
35 *\li Trailing dots are first removed from name and domain.
36 * Always compare complete subdomains, not only whether the
37 * domain name is the trailing string of the given name.
39 *\li "host.foobar.top" lies in "foobar.top" and in "top" and in ""
40 * but NOT in "bar.top"
44 ns_samedomain(const char *a
, const char *b
) {
52 /* Ignore a trailing label separator (i.e. an unescaped dot) in 'a'. */
53 if (la
!= 0U && a
[la
- 1] == '.') {
55 /* Note this loop doesn't get executed if la==1. */
56 for (i
= la
- 2; i
>= 0; i
--)
68 /* Ignore a trailing label separator (i.e. an unescaped dot) in 'b'. */
69 if (lb
!= 0U && b
[lb
- 1] == '.') {
71 /* note this loop doesn't get executed if lb==1 */
72 for (i
= lb
- 2; i
>= 0; i
--)
84 /* lb == 0 means 'b' is the root domain, so 'a' must be in 'b'. */
88 /* 'b' longer than 'a' means 'a' can't be in 'b'. */
92 /* 'a' and 'b' being equal at this point indicates sameness. */
94 return (strncasecmp(a
, b
, lb
) == 0);
96 /* Ok, we know la > lb. */
101 * If 'a' is only 1 character longer than 'b', then it can't be
102 * a subdomain of 'b' (because of the need for the '.' label
109 * If the character before the last 'lb' characters of 'b'
110 * isn't '.', then it can't be a match (this lets us avoid
111 * having "foobar.com" match "bar.com").
113 if (a
[diff
- 1] != '.')
117 * We're not sure about that '.', however. It could be escaped
118 * and thus not a really a label separator.
121 for (i
= diff
- 2; i
>= 0; i
--)
132 /* Now compare aligned trailing substring. */
134 return (strncasecmp(cp
, b
, lb
) == 0);
136 libresolv_hidden_def (ns_samedomain
)
139 * is "a" a subdomain of "b"?
142 ns_subdomain (const char *a
, const char *b
)
144 return __libc_ns_samename (a
, b
) != 1 && ns_samedomain (a
, b
);