xref: /freebsd/lib/libc/nameser/ns_samedomain.c (revision 1ca63a8219b88b752b064d19bd3428c61dbcf1f9)
1 /*-
2  * SPDX-License-Identifier: ISC
3  *
4  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
5  * Copyright (c) 1995,1999 by Internet Software Consortium.
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
17  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #ifndef lint
21 static const char rcsid[] = "$Id: ns_samedomain.c,v 1.6 2005/04/27 04:56:40 sra Exp $";
22 #endif
23 #include "port_before.h"
24 
25 #include <sys/types.h>
26 #include <arpa/nameser.h>
27 #include <errno.h>
28 #include <string.h>
29 
30 #include "port_after.h"
31 
32 /*%
33  *	Check whether a name belongs to a domain.
34  *
35  * Inputs:
36  *\li	a - the domain whose ancestry is being verified
37  *\li	b - the potential ancestor we're checking against
38  *
39  * Return:
40  *\li	boolean - is a at or below b?
41  *
42  * Notes:
43  *\li	Trailing dots are first removed from name and domain.
44  *	Always compare complete subdomains, not only whether the
45  *	domain name is the trailing string of the given name.
46  *
47  *\li	"host.foobar.top" lies in "foobar.top" and in "top" and in ""
48  *	but NOT in "bar.top"
49  */
50 
51 int
52 ns_samedomain(const char *a, const char *b) {
53 	size_t la, lb;
54 	int diff, i, escaped;
55 	const char *cp;
56 
57 	la = strlen(a);
58 	lb = strlen(b);
59 
60 	/* Ignore a trailing label separator (i.e. an unescaped dot) in 'a'. */
61 	if (la != 0U && a[la - 1] == '.') {
62 		escaped = 0;
63 		/* Note this loop doesn't get executed if la==1. */
64 		for (i = la - 2; i >= 0; i--)
65 			if (a[i] == '\\') {
66 				if (escaped)
67 					escaped = 0;
68 				else
69 					escaped = 1;
70 			} else
71 				break;
72 		if (!escaped)
73 			la--;
74 	}
75 
76 	/* Ignore a trailing label separator (i.e. an unescaped dot) in 'b'. */
77 	if (lb != 0U && b[lb - 1] == '.') {
78 		escaped = 0;
79 		/* note this loop doesn't get executed if lb==1 */
80 		for (i = lb - 2; i >= 0; i--)
81 			if (b[i] == '\\') {
82 				if (escaped)
83 					escaped = 0;
84 				else
85 					escaped = 1;
86 			} else
87 				break;
88 		if (!escaped)
89 			lb--;
90 	}
91 
92 	/* lb == 0 means 'b' is the root domain, so 'a' must be in 'b'. */
93 	if (lb == 0U)
94 		return (1);
95 
96 	/* 'b' longer than 'a' means 'a' can't be in 'b'. */
97 	if (lb > la)
98 		return (0);
99 
100 	/* 'a' and 'b' being equal at this point indicates sameness. */
101 	if (lb == la)
102 		return (strncasecmp(a, b, lb) == 0);
103 
104 	/* Ok, we know la > lb. */
105 
106 	diff = la - lb;
107 
108 	/*
109 	 * If 'a' is only 1 character longer than 'b', then it can't be
110 	 * a subdomain of 'b' (because of the need for the '.' label
111 	 * separator).
112 	 */
113 	if (diff < 2)
114 		return (0);
115 
116 	/*
117 	 * If the character before the last 'lb' characters of 'b'
118 	 * isn't '.', then it can't be a match (this lets us avoid
119 	 * having "foobar.com" match "bar.com").
120 	 */
121 	if (a[diff - 1] != '.')
122 		return (0);
123 
124 	/*
125 	 * We're not sure about that '.', however.  It could be escaped
126          * and thus not a really a label separator.
127 	 */
128 	escaped = 0;
129 	for (i = diff - 2; i >= 0; i--)
130 		if (a[i] == '\\') {
131 			if (escaped)
132 				escaped = 0;
133 			else
134 				escaped = 1;
135 		} else
136 			break;
137 	if (escaped)
138 		return (0);
139 
140 	/* Now compare aligned trailing substring. */
141 	cp = a + diff;
142 	return (strncasecmp(cp, b, lb) == 0);
143 }
144 
145 #ifndef _LIBC
146 /*%
147  *	is "a" a subdomain of "b"?
148  */
149 int
150 ns_subdomain(const char *a, const char *b) {
151 	return (ns_samename(a, b) != 1 && ns_samedomain(a, b));
152 }
153 #endif
154 
155 /*%
156  *	make a canonical copy of domain name "src"
157  *
158  * notes:
159  * \code
160  *	foo -> foo.
161  *	foo. -> foo.
162  *	foo.. -> foo.
163  *	foo\. -> foo\..
164  *	foo\\. -> foo\\.
165  * \endcode
166  */
167 
168 int
169 ns_makecanon(const char *src, char *dst, size_t dstsize) {
170 	size_t n = strlen(src);
171 
172 	if (n + sizeof "." > dstsize) {			/*%< Note: sizeof == 2 */
173 		errno = EMSGSIZE;
174 		return (-1);
175 	}
176 	strcpy(dst, src);
177 	while (n >= 1U && dst[n - 1] == '.')		/*%< Ends in "." */
178 		if (n >= 2U && dst[n - 2] == '\\' &&	/*%< Ends in "\." */
179 		    (n < 3U || dst[n - 3] != '\\'))	/*%< But not "\\." */
180 			break;
181 		else
182 			dst[--n] = '\0';
183 	dst[n++] = '.';
184 	dst[n] = '\0';
185 	return (0);
186 }
187 
188 /*%
189  *	determine whether domain name "a" is the same as domain name "b"
190  *
191  * return:
192  *\li	-1 on error
193  *\li	0 if names differ
194  *\li	1 if names are the same
195  */
196 
197 int
198 ns_samename(const char *a, const char *b) {
199 	char ta[NS_MAXDNAME], tb[NS_MAXDNAME];
200 
201 	if (ns_makecanon(a, ta, sizeof ta) < 0 ||
202 	    ns_makecanon(b, tb, sizeof tb) < 0)
203 		return (-1);
204 	if (strcasecmp(ta, tb) == 0)
205 		return (1);
206 	else
207 		return (0);
208 }
209 
210 /*! \file */
211