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 #if !defined(_LIBC) && !defined(lint)
20 static const char rcsid
[] = "$BINDId: ns_samedomain.c,v 8.9 1999/10/15 21:06:51 vixie Exp $";
23 #include <sys/types.h>
24 #include <arpa/nameser.h>
29 * Check whether a name belongs to a domain.
32 *\li a - the domain whose ancestory is being verified
33 *\li b - the potential ancestor we're checking against
36 *\li boolean - is a at or below b?
39 *\li Trailing dots are first removed from name and domain.
40 * Always compare complete subdomains, not only whether the
41 * domain name is the trailing string of the given name.
43 *\li "host.foobar.top" lies in "foobar.top" and in "top" and in ""
44 * but NOT in "bar.top"
48 ns_samedomain(const char *a
, const char *b
) {
56 /* Ignore a trailing label separator (i.e. an unescaped dot) in 'a'. */
57 if (la
!= 0U && a
[la
- 1] == '.') {
59 /* Note this loop doesn't get executed if la==1. */
60 for (i
= la
- 2; i
>= 0; i
--)
72 /* Ignore a trailing label separator (i.e. an unescaped dot) in 'b'. */
73 if (lb
!= 0U && b
[lb
- 1] == '.') {
75 /* note this loop doesn't get executed if lb==1 */
76 for (i
= lb
- 2; i
>= 0; i
--)
88 /* lb == 0 means 'b' is the root domain, so 'a' must be in 'b'. */
92 /* 'b' longer than 'a' means 'a' can't be in 'b'. */
96 /* 'a' and 'b' being equal at this point indicates sameness. */
98 return (strncasecmp(a
, b
, lb
) == 0);
100 /* Ok, we know la > lb. */
105 * If 'a' is only 1 character longer than 'b', then it can't be
106 * a subdomain of 'b' (because of the need for the '.' label
113 * If the character before the last 'lb' characters of 'b'
114 * isn't '.', then it can't be a match (this lets us avoid
115 * having "foobar.com" match "bar.com").
117 if (a
[diff
- 1] != '.')
121 * We're not sure about that '.', however. It could be escaped
122 * and thus not a really a label separator.
125 for (i
= diff
- 2; i
>= 0; i
--)
136 /* Now compare aligned trailing substring. */
138 return (strncasecmp(cp
, b
, lb
) == 0);
140 libresolv_hidden_def (ns_samedomain
)
143 * is "a" a subdomain of "b"?
146 ns_subdomain(const char *a
, const char *b
) {
147 return (ns_samename(a
, b
) != 1 && ns_samedomain(a
, b
));
151 * make a canonical copy of domain name "src"
164 ns_makecanon(const char *src
, char *dst
, size_t dstsize
) {
165 size_t n
= strlen(src
);
167 if (n
+ sizeof "." > dstsize
) { /*%< Note: sizeof == 2 */
168 __set_errno (EMSGSIZE
);
172 while (n
>= 1U && dst
[n
- 1] == '.') /*%< Ends in "." */
173 if (n
>= 2U && dst
[n
- 2] == '\\' && /*%< Ends in "\." */
174 (n
< 3U || dst
[n
- 3] != '\\')) /*%< But not "\\." */
182 libresolv_hidden_def (ns_makecanon
)
185 * determine whether domain name "a" is the same as domain name "b"
189 *\li 0 if names differ
190 *\li 1 if names are the same
194 ns_samename(const char *a
, const char *b
) {
195 char ta
[NS_MAXDNAME
], tb
[NS_MAXDNAME
];
197 if (ns_makecanon(a
, ta
, sizeof ta
) < 0 ||
198 ns_makecanon(b
, tb
, sizeof tb
) < 0)
200 if (strcasecmp(ta
, tb
) == 0)
205 libresolv_hidden_def (ns_samename
)