other-licenses/android/ns_samedomain.c

Wed, 31 Dec 2014 06:55:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:55:50 +0100
changeset 2
7e26c7da4463
permissions
-rw-r--r--

Added tag UPSTREAM_283F7C6 for changeset ca08bd8f51b2

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

mercurial