17c478bd9Sstevel@tonic-gate /*
2*9525b14bSRao Shoaib * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
37c478bd9Sstevel@tonic-gate * Copyright (c) 1995,1999 by Internet Software Consortium.
47c478bd9Sstevel@tonic-gate *
57c478bd9Sstevel@tonic-gate * Permission to use, copy, modify, and distribute this software for any
67c478bd9Sstevel@tonic-gate * purpose with or without fee is hereby granted, provided that the above
77c478bd9Sstevel@tonic-gate * copyright notice and this permission notice appear in all copies.
87c478bd9Sstevel@tonic-gate *
9*9525b14bSRao Shoaib * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
10*9525b14bSRao Shoaib * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11*9525b14bSRao Shoaib * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
12*9525b14bSRao Shoaib * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13*9525b14bSRao Shoaib * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14*9525b14bSRao Shoaib * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
15*9525b14bSRao Shoaib * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
167c478bd9Sstevel@tonic-gate */
177c478bd9Sstevel@tonic-gate
187c478bd9Sstevel@tonic-gate #include "port_before.h"
197c478bd9Sstevel@tonic-gate
207c478bd9Sstevel@tonic-gate #include <sys/types.h>
217c478bd9Sstevel@tonic-gate #include <arpa/nameser.h>
227c478bd9Sstevel@tonic-gate #include <errno.h>
237c478bd9Sstevel@tonic-gate #include <string.h>
247c478bd9Sstevel@tonic-gate
257c478bd9Sstevel@tonic-gate #include "port_after.h"
267c478bd9Sstevel@tonic-gate
27*9525b14bSRao Shoaib /*%
287c478bd9Sstevel@tonic-gate * Check whether a name belongs to a domain.
29*9525b14bSRao Shoaib *
307c478bd9Sstevel@tonic-gate * Inputs:
31*9525b14bSRao Shoaib *\li a - the domain whose ancestory is being verified
32*9525b14bSRao Shoaib *\li b - the potential ancestor we're checking against
33*9525b14bSRao Shoaib *
347c478bd9Sstevel@tonic-gate * Return:
35*9525b14bSRao Shoaib *\li boolean - is a at or below b?
36*9525b14bSRao Shoaib *
377c478bd9Sstevel@tonic-gate * Notes:
38*9525b14bSRao Shoaib *\li Trailing dots are first removed from name and domain.
397c478bd9Sstevel@tonic-gate * Always compare complete subdomains, not only whether the
407c478bd9Sstevel@tonic-gate * domain name is the trailing string of the given name.
417c478bd9Sstevel@tonic-gate *
42*9525b14bSRao Shoaib *\li "host.foobar.top" lies in "foobar.top" and in "top" and in ""
437c478bd9Sstevel@tonic-gate * but NOT in "bar.top"
447c478bd9Sstevel@tonic-gate */
457c478bd9Sstevel@tonic-gate
467c478bd9Sstevel@tonic-gate int
ns_samedomain(const char * a,const char * b)477c478bd9Sstevel@tonic-gate ns_samedomain(const char *a, const char *b) {
487c478bd9Sstevel@tonic-gate size_t la, lb;
497c478bd9Sstevel@tonic-gate int diff, i, escaped;
507c478bd9Sstevel@tonic-gate const char *cp;
517c478bd9Sstevel@tonic-gate
527c478bd9Sstevel@tonic-gate la = strlen(a);
537c478bd9Sstevel@tonic-gate lb = strlen(b);
547c478bd9Sstevel@tonic-gate
557c478bd9Sstevel@tonic-gate /* Ignore a trailing label separator (i.e. an unescaped dot) in 'a'. */
56*9525b14bSRao Shoaib if (la != 0U && a[la - 1] == '.') {
577c478bd9Sstevel@tonic-gate escaped = 0;
587c478bd9Sstevel@tonic-gate /* Note this loop doesn't get executed if la==1. */
597c478bd9Sstevel@tonic-gate for (i = la - 2; i >= 0; i--)
607c478bd9Sstevel@tonic-gate if (a[i] == '\\') {
617c478bd9Sstevel@tonic-gate if (escaped)
627c478bd9Sstevel@tonic-gate escaped = 0;
637c478bd9Sstevel@tonic-gate else
647c478bd9Sstevel@tonic-gate escaped = 1;
657c478bd9Sstevel@tonic-gate } else
667c478bd9Sstevel@tonic-gate break;
677c478bd9Sstevel@tonic-gate if (!escaped)
687c478bd9Sstevel@tonic-gate la--;
697c478bd9Sstevel@tonic-gate }
707c478bd9Sstevel@tonic-gate
717c478bd9Sstevel@tonic-gate /* Ignore a trailing label separator (i.e. an unescaped dot) in 'b'. */
72*9525b14bSRao Shoaib if (lb != 0U && b[lb - 1] == '.') {
737c478bd9Sstevel@tonic-gate escaped = 0;
747c478bd9Sstevel@tonic-gate /* note this loop doesn't get executed if lb==1 */
757c478bd9Sstevel@tonic-gate for (i = lb - 2; i >= 0; i--)
767c478bd9Sstevel@tonic-gate if (b[i] == '\\') {
777c478bd9Sstevel@tonic-gate if (escaped)
787c478bd9Sstevel@tonic-gate escaped = 0;
797c478bd9Sstevel@tonic-gate else
807c478bd9Sstevel@tonic-gate escaped = 1;
817c478bd9Sstevel@tonic-gate } else
827c478bd9Sstevel@tonic-gate break;
837c478bd9Sstevel@tonic-gate if (!escaped)
847c478bd9Sstevel@tonic-gate lb--;
857c478bd9Sstevel@tonic-gate }
867c478bd9Sstevel@tonic-gate
877c478bd9Sstevel@tonic-gate /* lb == 0 means 'b' is the root domain, so 'a' must be in 'b'. */
88*9525b14bSRao Shoaib if (lb == 0U)
897c478bd9Sstevel@tonic-gate return (1);
907c478bd9Sstevel@tonic-gate
917c478bd9Sstevel@tonic-gate /* 'b' longer than 'a' means 'a' can't be in 'b'. */
927c478bd9Sstevel@tonic-gate if (lb > la)
937c478bd9Sstevel@tonic-gate return (0);
947c478bd9Sstevel@tonic-gate
957c478bd9Sstevel@tonic-gate /* 'a' and 'b' being equal at this point indicates sameness. */
967c478bd9Sstevel@tonic-gate if (lb == la)
977c478bd9Sstevel@tonic-gate return (strncasecmp(a, b, lb) == 0);
987c478bd9Sstevel@tonic-gate
997c478bd9Sstevel@tonic-gate /* Ok, we know la > lb. */
1007c478bd9Sstevel@tonic-gate
1017c478bd9Sstevel@tonic-gate diff = la - lb;
1027c478bd9Sstevel@tonic-gate
1037c478bd9Sstevel@tonic-gate /*
1047c478bd9Sstevel@tonic-gate * If 'a' is only 1 character longer than 'b', then it can't be
1057c478bd9Sstevel@tonic-gate * a subdomain of 'b' (because of the need for the '.' label
1067c478bd9Sstevel@tonic-gate * separator).
1077c478bd9Sstevel@tonic-gate */
1087c478bd9Sstevel@tonic-gate if (diff < 2)
1097c478bd9Sstevel@tonic-gate return (0);
1107c478bd9Sstevel@tonic-gate
1117c478bd9Sstevel@tonic-gate /*
1127c478bd9Sstevel@tonic-gate * If the character before the last 'lb' characters of 'b'
1137c478bd9Sstevel@tonic-gate * isn't '.', then it can't be a match (this lets us avoid
1147c478bd9Sstevel@tonic-gate * having "foobar.com" match "bar.com").
1157c478bd9Sstevel@tonic-gate */
1167c478bd9Sstevel@tonic-gate if (a[diff - 1] != '.')
1177c478bd9Sstevel@tonic-gate return (0);
1187c478bd9Sstevel@tonic-gate
1197c478bd9Sstevel@tonic-gate /*
1207c478bd9Sstevel@tonic-gate * We're not sure about that '.', however. It could be escaped
1217c478bd9Sstevel@tonic-gate * and thus not a really a label separator.
1227c478bd9Sstevel@tonic-gate */
1237c478bd9Sstevel@tonic-gate escaped = 0;
1247c478bd9Sstevel@tonic-gate for (i = diff - 2; i >= 0; i--)
1257c478bd9Sstevel@tonic-gate if (a[i] == '\\') {
1267c478bd9Sstevel@tonic-gate if (escaped)
1277c478bd9Sstevel@tonic-gate escaped = 0;
1287c478bd9Sstevel@tonic-gate else
1297c478bd9Sstevel@tonic-gate escaped = 1;
1307c478bd9Sstevel@tonic-gate } else
1317c478bd9Sstevel@tonic-gate break;
1327c478bd9Sstevel@tonic-gate if (escaped)
1337c478bd9Sstevel@tonic-gate return (0);
1347c478bd9Sstevel@tonic-gate
1357c478bd9Sstevel@tonic-gate /* Now compare aligned trailing substring. */
1367c478bd9Sstevel@tonic-gate cp = a + diff;
1377c478bd9Sstevel@tonic-gate return (strncasecmp(cp, b, lb) == 0);
1387c478bd9Sstevel@tonic-gate }
1397c478bd9Sstevel@tonic-gate
140*9525b14bSRao Shoaib /*%
1417c478bd9Sstevel@tonic-gate * is "a" a subdomain of "b"?
1427c478bd9Sstevel@tonic-gate */
1437c478bd9Sstevel@tonic-gate int
ns_subdomain(const char * a,const char * b)1447c478bd9Sstevel@tonic-gate ns_subdomain(const char *a, const char *b) {
1457c478bd9Sstevel@tonic-gate return (ns_samename(a, b) != 1 && ns_samedomain(a, b));
1467c478bd9Sstevel@tonic-gate }
1477c478bd9Sstevel@tonic-gate
148*9525b14bSRao Shoaib /*%
1497c478bd9Sstevel@tonic-gate * make a canonical copy of domain name "src"
150*9525b14bSRao Shoaib *
1517c478bd9Sstevel@tonic-gate * notes:
152*9525b14bSRao Shoaib * \code
1537c478bd9Sstevel@tonic-gate * foo -> foo.
1547c478bd9Sstevel@tonic-gate * foo. -> foo.
1557c478bd9Sstevel@tonic-gate * foo.. -> foo.
1567c478bd9Sstevel@tonic-gate * foo\. -> foo\..
1577c478bd9Sstevel@tonic-gate * foo\\. -> foo\\.
158*9525b14bSRao Shoaib * \endcode
1597c478bd9Sstevel@tonic-gate */
1607c478bd9Sstevel@tonic-gate
1617c478bd9Sstevel@tonic-gate int
ns_makecanon(const char * src,char * dst,size_t dstsize)1627c478bd9Sstevel@tonic-gate ns_makecanon(const char *src, char *dst, size_t dstsize) {
1637c478bd9Sstevel@tonic-gate size_t n = strlen(src);
1647c478bd9Sstevel@tonic-gate
165*9525b14bSRao Shoaib if (n + sizeof "." > dstsize) { /*%< Note: sizeof == 2 */
1667c478bd9Sstevel@tonic-gate errno = EMSGSIZE;
1677c478bd9Sstevel@tonic-gate return (-1);
1687c478bd9Sstevel@tonic-gate }
1697c478bd9Sstevel@tonic-gate strcpy(dst, src);
170*9525b14bSRao Shoaib while (n >= 1U && dst[n - 1] == '.') /*%< Ends in "." */
171*9525b14bSRao Shoaib if (n >= 2U && dst[n - 2] == '\\' && /*%< Ends in "\." */
172*9525b14bSRao Shoaib (n < 3U || dst[n - 3] != '\\')) /*%< But not "\\." */
1737c478bd9Sstevel@tonic-gate break;
1747c478bd9Sstevel@tonic-gate else
1757c478bd9Sstevel@tonic-gate dst[--n] = '\0';
1767c478bd9Sstevel@tonic-gate dst[n++] = '.';
1777c478bd9Sstevel@tonic-gate dst[n] = '\0';
1787c478bd9Sstevel@tonic-gate return (0);
1797c478bd9Sstevel@tonic-gate }
1807c478bd9Sstevel@tonic-gate
181*9525b14bSRao Shoaib /*%
1827c478bd9Sstevel@tonic-gate * determine whether domain name "a" is the same as domain name "b"
183*9525b14bSRao Shoaib *
1847c478bd9Sstevel@tonic-gate * return:
185*9525b14bSRao Shoaib *\li -1 on error
186*9525b14bSRao Shoaib *\li 0 if names differ
187*9525b14bSRao Shoaib *\li 1 if names are the same
1887c478bd9Sstevel@tonic-gate */
1897c478bd9Sstevel@tonic-gate
1907c478bd9Sstevel@tonic-gate int
ns_samename(const char * a,const char * b)1917c478bd9Sstevel@tonic-gate ns_samename(const char *a, const char *b) {
1927c478bd9Sstevel@tonic-gate char ta[NS_MAXDNAME], tb[NS_MAXDNAME];
1937c478bd9Sstevel@tonic-gate
1947c478bd9Sstevel@tonic-gate if (ns_makecanon(a, ta, sizeof ta) < 0 ||
1957c478bd9Sstevel@tonic-gate ns_makecanon(b, tb, sizeof tb) < 0)
1967c478bd9Sstevel@tonic-gate return (-1);
1977c478bd9Sstevel@tonic-gate if (strcasecmp(ta, tb) == 0)
1987c478bd9Sstevel@tonic-gate return (1);
1997c478bd9Sstevel@tonic-gate else
2007c478bd9Sstevel@tonic-gate return (0);
2017c478bd9Sstevel@tonic-gate }
202*9525b14bSRao Shoaib
203*9525b14bSRao Shoaib /*! \file */
204