Tue, 06 Jan 2015 21:39:09 +0100
Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.
1 /* $NetBSD: ns_samedomain.c,v 1.2 2004/05/20 20:35:05 christos Exp $ */
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 */
20 /*
21 * This version of this file is derived from Android 2.3 "Gingerbread",
22 * which contains uncredited changes by Android/Google developers. It has
23 * been modified in 2011 for use in the Android build of Mozilla Firefox by
24 * Mozilla contributors (including Michael Edwards <m.k.edwards@gmail.com>,
25 * and Steve Workman <sjhworkman@gmail.com>).
26 * These changes are offered under the same license as the original NetBSD
27 * file, whose copyright and license are unchanged above.
28 */
30 #define ANDROID_CHANGES 1
31 #define MOZILLA_NECKO_EXCLUDE_CODE 1
33 #include <sys/cdefs.h>
34 #ifndef lint
35 #ifdef notdef
36 static const char rcsid[] = "Id: ns_samedomain.c,v 1.1.2.2.4.2 2004/03/16 12:34:17 marka Exp";
37 #else
38 __RCSID("$NetBSD: ns_samedomain.c,v 1.2 2004/05/20 20:35:05 christos Exp $");
39 #endif
40 #endif
42 #include <sys/types.h>
43 #include "arpa_nameser.h"
44 #include <errno.h>
45 #include <string.h>
47 #ifndef MOZILLA_NECKO_EXCLUDE_CODE
48 #ifndef _LIBC
49 /*
50 * int
51 * ns_samedomain(a, b)
52 * Check whether a name belongs to a domain.
53 * Inputs:
54 * a - the domain whose ancestory is being verified
55 * b - the potential ancestor we're checking against
56 * Return:
57 * boolean - is a at or below b?
58 * Notes:
59 * Trailing dots are first removed from name and domain.
60 * Always compare complete subdomains, not only whether the
61 * domain name is the trailing string of the given name.
62 *
63 * "host.foobar.top" lies in "foobar.top" and in "top" and in ""
64 * but NOT in "bar.top"
65 */
67 int
68 ns_samedomain(const char *a, const char *b) {
69 size_t la, lb;
70 int diff, i, escaped;
71 const char *cp;
73 la = strlen(a);
74 lb = strlen(b);
76 /* Ignore a trailing label separator (i.e. an unescaped dot) in 'a'. */
77 if (la != 0U && a[la - 1] == '.') {
78 escaped = 0;
79 /* Note this loop doesn't get executed if la==1. */
80 for (i = la - 2; i >= 0; i--)
81 if (a[i] == '\\') {
82 if (escaped)
83 escaped = 0;
84 else
85 escaped = 1;
86 } else
87 break;
88 if (!escaped)
89 la--;
90 }
92 /* Ignore a trailing label separator (i.e. an unescaped dot) in 'b'. */
93 if (lb != 0U && b[lb - 1] == '.') {
94 escaped = 0;
95 /* note this loop doesn't get executed if lb==1 */
96 for (i = lb - 2; i >= 0; i--)
97 if (b[i] == '\\') {
98 if (escaped)
99 escaped = 0;
100 else
101 escaped = 1;
102 } else
103 break;
104 if (!escaped)
105 lb--;
106 }
108 /* lb == 0 means 'b' is the root domain, so 'a' must be in 'b'. */
109 if (lb == 0U)
110 return (1);
112 /* 'b' longer than 'a' means 'a' can't be in 'b'. */
113 if (lb > la)
114 return (0);
116 /* 'a' and 'b' being equal at this point indicates sameness. */
117 if (lb == la)
118 return (strncasecmp(a, b, lb) == 0);
120 /* Ok, we know la > lb. */
122 diff = la - lb;
124 /*
125 * If 'a' is only 1 character longer than 'b', then it can't be
126 * a subdomain of 'b' (because of the need for the '.' label
127 * separator).
128 */
129 if (diff < 2)
130 return (0);
132 /*
133 * If the character before the last 'lb' characters of 'b'
134 * isn't '.', then it can't be a match (this lets us avoid
135 * having "foobar.com" match "bar.com").
136 */
137 if (a[diff - 1] != '.')
138 return (0);
140 /*
141 * We're not sure about that '.', however. It could be escaped
142 * and thus not a really a label separator.
143 */
144 escaped = 0;
145 for (i = diff - 2; i >= 0; i--)
146 if (a[i] == '\\') {
147 if (escaped)
148 escaped = 0;
149 else
150 escaped = 1;
151 } else
152 break;
153 if (escaped)
154 return (0);
156 /* Now compare aligned trailing substring. */
157 cp = a + diff;
158 return (strncasecmp(cp, b, lb) == 0);
159 }
161 /*
162 * int
163 * ns_subdomain(a, b)
164 * is "a" a subdomain of "b"?
165 */
166 int
167 ns_subdomain(const char *a, const char *b) {
168 return (ns_samename(a, b) != 1 && ns_samedomain(a, b));
169 }
170 #endif
171 #endif
173 /*
174 * int
175 * ns_makecanon(src, dst, dstsize)
176 * make a canonical copy of domain name "src"
177 * notes:
178 * foo -> foo.
179 * foo. -> foo.
180 * foo.. -> foo.
181 * foo\. -> foo\..
182 * foo\\. -> foo\\.
183 */
185 int
186 ns_makecanon(const char *src, char *dst, size_t dstsize) {
187 size_t n = strlen(src);
189 if (n + sizeof "." > dstsize) { /* Note: sizeof == 2 */
190 errno = EMSGSIZE;
191 return (-1);
192 }
193 strcpy(dst, src);
194 while (n >= 1U && dst[n - 1] == '.') /* Ends in "." */
195 if (n >= 2U && dst[n - 2] == '\\' && /* Ends in "\." */
196 (n < 3U || dst[n - 3] != '\\')) /* But not "\\." */
197 break;
198 else
199 dst[--n] = '\0';
200 dst[n++] = '.';
201 dst[n] = '\0';
202 return (0);
203 }
205 /*
206 * int
207 * ns_samename(a, b)
208 * determine whether domain name "a" is the same as domain name "b"
209 * return:
210 * -1 on error
211 * 0 if names differ
212 * 1 if names are the same
213 */
215 int
216 ns_samename(const char *a, const char *b) {
217 char ta[NS_MAXDNAME], tb[NS_MAXDNAME];
219 if (ns_makecanon(a, ta, sizeof ta) < 0 ||
220 ns_makecanon(b, tb, sizeof tb) < 0)
221 return (-1);
222 if (strcasecmp(ta, tb) == 0)
223 return (1);
224 else
225 return (0);
226 }