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