1/*	$NetBSD$	*/
2
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
21static const char rcsid[] = "Id: ns_samedomain.c,v 1.6 2005/04/27 04:56:40 sra Exp ";
22#endif
23
24#include "port_before.h"
25
26#include <sys/types.h>
27#include <arpa/nameser.h>
28#include <errno.h>
29#include <string.h>
30
31#include "port_after.h"
32
33/*%
34 *	Check whether a name belongs to a domain.
35 *
36 * Inputs:
37 *\li	a - the domain whose ancestory is being verified
38 *\li	b - the potential ancestor we're checking against
39 *
40 * Return:
41 *\li	boolean - is a at or below b?
42 *
43 * Notes:
44 *\li	Trailing dots are first removed from name and domain.
45 *	Always compare complete subdomains, not only whether the
46 *	domain name is the trailing string of the given name.
47 *
48 *\li	"host.foobar.top" lies in "foobar.top" and in "top" and in ""
49 *	but NOT in "bar.top"
50 */
51
52int
53ns_samedomain(const char *a, const char *b) {
54	size_t la, lb;
55	int diff, i, escaped;
56	const char *cp;
57
58	la = strlen(a);
59	lb = strlen(b);
60
61	/* Ignore a trailing label separator (i.e. an unescaped dot) in 'a'. */
62	if (la != 0U && a[la - 1] == '.') {
63		escaped = 0;
64		/* Note this loop doesn't get executed if la==1. */
65		for (i = la - 2; i >= 0; i--)
66			if (a[i] == '\\') {
67				if (escaped)
68					escaped = 0;
69				else
70					escaped = 1;
71			} else
72				break;
73		if (!escaped)
74			la--;
75	}
76
77	/* Ignore a trailing label separator (i.e. an unescaped dot) in 'b'. */
78	if (lb != 0U && b[lb - 1] == '.') {
79		escaped = 0;
80		/* note this loop doesn't get executed if lb==1 */
81		for (i = lb - 2; i >= 0; i--)
82			if (b[i] == '\\') {
83				if (escaped)
84					escaped = 0;
85				else
86					escaped = 1;
87			} else
88				break;
89		if (!escaped)
90			lb--;
91	}
92
93	/* lb == 0 means 'b' is the root domain, so 'a' must be in 'b'. */
94	if (lb == 0U)
95		return (1);
96
97	/* 'b' longer than 'a' means 'a' can't be in 'b'. */
98	if (lb > la)
99		return (0);
100
101	/* 'a' and 'b' being equal at this point indicates sameness. */
102	if (lb == la)
103		return (strncasecmp(a, b, lb) == 0);
104
105	/* Ok, we know la > lb. */
106
107	diff = la - lb;
108
109	/*
110	 * If 'a' is only 1 character longer than 'b', then it can't be
111	 * a subdomain of 'b' (because of the need for the '.' label
112	 * separator).
113	 */
114	if (diff < 2)
115		return (0);
116
117	/*
118	 * If the character before the last 'lb' characters of 'b'
119	 * isn't '.', then it can't be a match (this lets us avoid
120	 * having "foobar.com" match "bar.com").
121	 */
122	if (a[diff - 1] != '.')
123		return (0);
124
125	/*
126	 * We're not sure about that '.', however.  It could be escaped
127         * and thus not a really a label separator.
128	 */
129	escaped = 0;
130	for (i = diff - 2; i >= 0; i--)
131		if (a[i] == '\\') {
132			if (escaped)
133				escaped = 0;
134			else
135				escaped = 1;
136		} else
137			break;
138	if (escaped)
139		return (0);
140
141	/* Now compare aligned trailing substring. */
142	cp = a + diff;
143	return (strncasecmp(cp, b, lb) == 0);
144}
145
146/*%
147 *	is "a" a subdomain of "b"?
148 */
149int
150ns_subdomain(const char *a, const char *b) {
151	return (ns_samename(a, b) != 1 && ns_samedomain(a, b));
152}
153
154/*%
155 *	make a canonical copy of domain name "src"
156 *
157 * notes:
158 * \code
159 *	foo -> foo.
160 *	foo. -> foo.
161 *	foo.. -> foo.
162 *	foo\. -> foo\..
163 *	foo\\. -> foo\\.
164 * \endcode
165 */
166
167int
168ns_makecanon(const char *src, char *dst, size_t dstsize) {
169	size_t n = strlen(src);
170
171	if (n + sizeof "." > dstsize) {			/*%< Note: sizeof == 2 */
172		errno = EMSGSIZE;
173		return (-1);
174	}
175	strcpy(dst, src);
176	while (n >= 1U && dst[n - 1] == '.')		/*%< Ends in "." */
177		if (n >= 2U && dst[n - 2] == '\\' &&	/*%< Ends in "\." */
178		    (n < 3U || dst[n - 3] != '\\'))	/*%< But not "\\." */
179			break;
180		else
181			dst[--n] = '\0';
182	dst[n++] = '.';
183	dst[n] = '\0';
184	return (0);
185}
186
187/*%
188 *	determine whether domain name "a" is the same as domain name "b"
189 *
190 * return:
191 *\li	-1 on error
192 *\li	0 if names differ
193 *\li	1 if names are the same
194 */
195
196int
197ns_samename(const char *a, const char *b) {
198	char ta[NS_MAXDNAME], tb[NS_MAXDNAME];
199
200	if (ns_makecanon(a, ta, sizeof ta) < 0 ||
201	    ns_makecanon(b, tb, sizeof tb) < 0)
202		return (-1);
203	if (strcasecmp(ta, tb) == 0)
204		return (1);
205	else
206		return (0);
207}
208
209/*! \file */
210