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