Deleted Added
full compact
ns_samedomain.c (158787) ns_samedomain.c (170244)
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
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
19static const char rcsid[] = "$Id: ns_samedomain.c,v 1.1.2.2.4.2 2004/03/16 12:34:17 marka Exp $";
19static const char rcsid[] = "$Id: ns_samedomain.c,v 1.5.18.1 2005/04/27 05:01:09 sra Exp $";
20#endif
21#include <sys/cdefs.h>
20#endif
21#include <sys/cdefs.h>
22__FBSDID("$FreeBSD: head/lib/libc/nameser/ns_samedomain.c 158787 2006-05-21 11:19:36Z ume $");
22__FBSDID("$FreeBSD: head/lib/libc/nameser/ns_samedomain.c 170244 2007-06-03 17:20:27Z ume $");
23
24#include "port_before.h"
25
26#include <sys/types.h>
27#include <arpa/nameser.h>
28#include <errno.h>
29#include <string.h>
30
31#include "port_after.h"
32
23
24#include "port_before.h"
25
26#include <sys/types.h>
27#include <arpa/nameser.h>
28#include <errno.h>
29#include <string.h>
30
31#include "port_after.h"
32
33/*
34 * int
35 * ns_samedomain(a, b)
33/*%
36 * Check whether a name belongs to a domain.
34 * Check whether a name belongs to a domain.
35 *
37 * Inputs:
36 * Inputs:
38 * a - the domain whose ancestory is being verified
39 * b - the potential ancestor we're checking against
37 *\li a - the domain whose ancestory is being verified
38 *\li b - the potential ancestor we're checking against
39 *
40 * Return:
40 * Return:
41 * boolean - is a at or below b?
41 *\li boolean - is a at or below b?
42 *
42 * Notes:
43 * Notes:
43 * Trailing dots are first removed from name and domain.
44 *\li Trailing dots are first removed from name and domain.
44 * Always compare complete subdomains, not only whether the
45 * domain name is the trailing string of the given name.
46 *
45 * Always compare complete subdomains, not only whether the
46 * domain name is the trailing string of the given name.
47 *
47 * "host.foobar.top" lies in "foobar.top" and in "top" and in ""
48 *\li "host.foobar.top" lies in "foobar.top" and in "top" and in ""
48 * but NOT in "bar.top"
49 */
50
51int
52ns_samedomain(const char *a, const char *b) {
53 size_t la, lb;
54 int diff, i, escaped;
55 const char *cp;

--- 82 unchanged lines hidden (view full) ---

138 return (0);
139
140 /* Now compare aligned trailing substring. */
141 cp = a + diff;
142 return (strncasecmp(cp, b, lb) == 0);
143}
144
145#ifndef _LIBC
49 * but NOT in "bar.top"
50 */
51
52int
53ns_samedomain(const char *a, const char *b) {
54 size_t la, lb;
55 int diff, i, escaped;
56 const char *cp;

--- 82 unchanged lines hidden (view full) ---

139 return (0);
140
141 /* Now compare aligned trailing substring. */
142 cp = a + diff;
143 return (strncasecmp(cp, b, lb) == 0);
144}
145
146#ifndef _LIBC
146/*
147 * int
148 * ns_subdomain(a, b)
147/*%
149 * is "a" a subdomain of "b"?
150 */
151int
152ns_subdomain(const char *a, const char *b) {
153 return (ns_samename(a, b) != 1 && ns_samedomain(a, b));
154}
155#endif
156
148 * is "a" a subdomain of "b"?
149 */
150int
151ns_subdomain(const char *a, const char *b) {
152 return (ns_samename(a, b) != 1 && ns_samedomain(a, b));
153}
154#endif
155
157/*
158 * int
159 * ns_makecanon(src, dst, dstsize)
156/*%
160 * make a canonical copy of domain name "src"
157 * make a canonical copy of domain name "src"
158 *
161 * notes:
159 * notes:
160 * \code
162 * foo -> foo.
163 * foo. -> foo.
164 * foo.. -> foo.
165 * foo\. -> foo\..
166 * foo\\. -> foo\\.
161 * foo -> foo.
162 * foo. -> foo.
163 * foo.. -> foo.
164 * foo\. -> foo\..
165 * foo\\. -> foo\\.
166 * \endcode
167 */
168
169int
170ns_makecanon(const char *src, char *dst, size_t dstsize) {
171 size_t n = strlen(src);
172
167 */
168
169int
170ns_makecanon(const char *src, char *dst, size_t dstsize) {
171 size_t n = strlen(src);
172
173 if (n + sizeof "." > dstsize) { /* Note: sizeof == 2 */
173 if (n + sizeof "." > dstsize) { /*%< Note: sizeof == 2 */
174 errno = EMSGSIZE;
175 return (-1);
176 }
177 strcpy(dst, src);
174 errno = EMSGSIZE;
175 return (-1);
176 }
177 strcpy(dst, src);
178 while (n >= 1U && dst[n - 1] == '.') /* Ends in "." */
179 if (n >= 2U && dst[n - 2] == '\\' && /* Ends in "\." */
180 (n < 3U || dst[n - 3] != '\\')) /* But not "\\." */
178 while (n >= 1U && dst[n - 1] == '.') /*%< Ends in "." */
179 if (n >= 2U && dst[n - 2] == '\\' && /*%< Ends in "\." */
180 (n < 3U || dst[n - 3] != '\\')) /*%< But not "\\." */
181 break;
182 else
183 dst[--n] = '\0';
184 dst[n++] = '.';
185 dst[n] = '\0';
186 return (0);
187}
188
181 break;
182 else
183 dst[--n] = '\0';
184 dst[n++] = '.';
185 dst[n] = '\0';
186 return (0);
187}
188
189/*
190 * int
191 * ns_samename(a, b)
189/*%
192 * determine whether domain name "a" is the same as domain name "b"
190 * determine whether domain name "a" is the same as domain name "b"
191 *
193 * return:
192 * return:
194 * -1 on error
195 * 0 if names differ
196 * 1 if names are the same
193 *\li -1 on error
194 *\li 0 if names differ
195 *\li 1 if names are the same
197 */
198
199int
200ns_samename(const char *a, const char *b) {
201 char ta[NS_MAXDNAME], tb[NS_MAXDNAME];
202
203 if (ns_makecanon(a, ta, sizeof ta) < 0 ||
204 ns_makecanon(b, tb, sizeof tb) < 0)
205 return (-1);
206 if (strcasecmp(ta, tb) == 0)
207 return (1);
208 else
209 return (0);
210}
196 */
197
198int
199ns_samename(const char *a, const char *b) {
200 char ta[NS_MAXDNAME], tb[NS_MAXDNAME];
201
202 if (ns_makecanon(a, ta, sizeof ta) < 0 ||
203 ns_makecanon(b, tb, sizeof tb) < 0)
204 return (-1);
205 if (strcasecmp(ta, tb) == 0)
206 return (1);
207 else
208 return (0);
209}
210
211/*! \file */