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