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