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 #ifndef lint 19*9525b14bSRao Shoaib static const char rcsid[] = "$Id: ns_samedomain.c,v 1.6 2005/04/27 04:56:40 sra Exp $"; 207c478bd9Sstevel@tonic-gate #endif 217c478bd9Sstevel@tonic-gate 227c478bd9Sstevel@tonic-gate #include "port_before.h" 237c478bd9Sstevel@tonic-gate 247c478bd9Sstevel@tonic-gate #include <sys/types.h> 257c478bd9Sstevel@tonic-gate #include <arpa/nameser.h> 267c478bd9Sstevel@tonic-gate #include <errno.h> 277c478bd9Sstevel@tonic-gate #include <string.h> 287c478bd9Sstevel@tonic-gate 297c478bd9Sstevel@tonic-gate #include "port_after.h" 307c478bd9Sstevel@tonic-gate 31*9525b14bSRao Shoaib /*% 327c478bd9Sstevel@tonic-gate * Check whether a name belongs to a domain. 33*9525b14bSRao Shoaib * 347c478bd9Sstevel@tonic-gate * Inputs: 35*9525b14bSRao Shoaib *\li a - the domain whose ancestory is being verified 36*9525b14bSRao Shoaib *\li b - the potential ancestor we're checking against 37*9525b14bSRao Shoaib * 387c478bd9Sstevel@tonic-gate * Return: 39*9525b14bSRao Shoaib *\li boolean - is a at or below b? 40*9525b14bSRao Shoaib * 417c478bd9Sstevel@tonic-gate * Notes: 42*9525b14bSRao Shoaib *\li Trailing dots are first removed from name and domain. 437c478bd9Sstevel@tonic-gate * Always compare complete subdomains, not only whether the 447c478bd9Sstevel@tonic-gate * domain name is the trailing string of the given name. 457c478bd9Sstevel@tonic-gate * 46*9525b14bSRao Shoaib *\li "host.foobar.top" lies in "foobar.top" and in "top" and in "" 477c478bd9Sstevel@tonic-gate * but NOT in "bar.top" 487c478bd9Sstevel@tonic-gate */ 497c478bd9Sstevel@tonic-gate 507c478bd9Sstevel@tonic-gate int 517c478bd9Sstevel@tonic-gate ns_samedomain(const char *a, const char *b) { 527c478bd9Sstevel@tonic-gate size_t la, lb; 537c478bd9Sstevel@tonic-gate int diff, i, escaped; 547c478bd9Sstevel@tonic-gate const char *cp; 557c478bd9Sstevel@tonic-gate 567c478bd9Sstevel@tonic-gate la = strlen(a); 577c478bd9Sstevel@tonic-gate lb = strlen(b); 587c478bd9Sstevel@tonic-gate 597c478bd9Sstevel@tonic-gate /* Ignore a trailing label separator (i.e. an unescaped dot) in 'a'. */ 60*9525b14bSRao Shoaib if (la != 0U && a[la - 1] == '.') { 617c478bd9Sstevel@tonic-gate escaped = 0; 627c478bd9Sstevel@tonic-gate /* Note this loop doesn't get executed if la==1. */ 637c478bd9Sstevel@tonic-gate for (i = la - 2; i >= 0; i--) 647c478bd9Sstevel@tonic-gate if (a[i] == '\\') { 657c478bd9Sstevel@tonic-gate if (escaped) 667c478bd9Sstevel@tonic-gate escaped = 0; 677c478bd9Sstevel@tonic-gate else 687c478bd9Sstevel@tonic-gate escaped = 1; 697c478bd9Sstevel@tonic-gate } else 707c478bd9Sstevel@tonic-gate break; 717c478bd9Sstevel@tonic-gate if (!escaped) 727c478bd9Sstevel@tonic-gate la--; 737c478bd9Sstevel@tonic-gate } 747c478bd9Sstevel@tonic-gate 757c478bd9Sstevel@tonic-gate /* Ignore a trailing label separator (i.e. an unescaped dot) in 'b'. */ 76*9525b14bSRao Shoaib if (lb != 0U && b[lb - 1] == '.') { 777c478bd9Sstevel@tonic-gate escaped = 0; 787c478bd9Sstevel@tonic-gate /* note this loop doesn't get executed if lb==1 */ 797c478bd9Sstevel@tonic-gate for (i = lb - 2; i >= 0; i--) 807c478bd9Sstevel@tonic-gate if (b[i] == '\\') { 817c478bd9Sstevel@tonic-gate if (escaped) 827c478bd9Sstevel@tonic-gate escaped = 0; 837c478bd9Sstevel@tonic-gate else 847c478bd9Sstevel@tonic-gate escaped = 1; 857c478bd9Sstevel@tonic-gate } else 867c478bd9Sstevel@tonic-gate break; 877c478bd9Sstevel@tonic-gate if (!escaped) 887c478bd9Sstevel@tonic-gate lb--; 897c478bd9Sstevel@tonic-gate } 907c478bd9Sstevel@tonic-gate 917c478bd9Sstevel@tonic-gate /* lb == 0 means 'b' is the root domain, so 'a' must be in 'b'. */ 92*9525b14bSRao Shoaib if (lb == 0U) 937c478bd9Sstevel@tonic-gate return (1); 947c478bd9Sstevel@tonic-gate 957c478bd9Sstevel@tonic-gate /* 'b' longer than 'a' means 'a' can't be in 'b'. */ 967c478bd9Sstevel@tonic-gate if (lb > la) 977c478bd9Sstevel@tonic-gate return (0); 987c478bd9Sstevel@tonic-gate 997c478bd9Sstevel@tonic-gate /* 'a' and 'b' being equal at this point indicates sameness. */ 1007c478bd9Sstevel@tonic-gate if (lb == la) 1017c478bd9Sstevel@tonic-gate return (strncasecmp(a, b, lb) == 0); 1027c478bd9Sstevel@tonic-gate 1037c478bd9Sstevel@tonic-gate /* Ok, we know la > lb. */ 1047c478bd9Sstevel@tonic-gate 1057c478bd9Sstevel@tonic-gate diff = la - lb; 1067c478bd9Sstevel@tonic-gate 1077c478bd9Sstevel@tonic-gate /* 1087c478bd9Sstevel@tonic-gate * If 'a' is only 1 character longer than 'b', then it can't be 1097c478bd9Sstevel@tonic-gate * a subdomain of 'b' (because of the need for the '.' label 1107c478bd9Sstevel@tonic-gate * separator). 1117c478bd9Sstevel@tonic-gate */ 1127c478bd9Sstevel@tonic-gate if (diff < 2) 1137c478bd9Sstevel@tonic-gate return (0); 1147c478bd9Sstevel@tonic-gate 1157c478bd9Sstevel@tonic-gate /* 1167c478bd9Sstevel@tonic-gate * If the character before the last 'lb' characters of 'b' 1177c478bd9Sstevel@tonic-gate * isn't '.', then it can't be a match (this lets us avoid 1187c478bd9Sstevel@tonic-gate * having "foobar.com" match "bar.com"). 1197c478bd9Sstevel@tonic-gate */ 1207c478bd9Sstevel@tonic-gate if (a[diff - 1] != '.') 1217c478bd9Sstevel@tonic-gate return (0); 1227c478bd9Sstevel@tonic-gate 1237c478bd9Sstevel@tonic-gate /* 1247c478bd9Sstevel@tonic-gate * We're not sure about that '.', however. It could be escaped 1257c478bd9Sstevel@tonic-gate * and thus not a really a label separator. 1267c478bd9Sstevel@tonic-gate */ 1277c478bd9Sstevel@tonic-gate escaped = 0; 1287c478bd9Sstevel@tonic-gate for (i = diff - 2; i >= 0; i--) 1297c478bd9Sstevel@tonic-gate if (a[i] == '\\') { 1307c478bd9Sstevel@tonic-gate if (escaped) 1317c478bd9Sstevel@tonic-gate escaped = 0; 1327c478bd9Sstevel@tonic-gate else 1337c478bd9Sstevel@tonic-gate escaped = 1; 1347c478bd9Sstevel@tonic-gate } else 1357c478bd9Sstevel@tonic-gate break; 1367c478bd9Sstevel@tonic-gate if (escaped) 1377c478bd9Sstevel@tonic-gate return (0); 1387c478bd9Sstevel@tonic-gate 1397c478bd9Sstevel@tonic-gate /* Now compare aligned trailing substring. */ 1407c478bd9Sstevel@tonic-gate cp = a + diff; 1417c478bd9Sstevel@tonic-gate return (strncasecmp(cp, b, lb) == 0); 1427c478bd9Sstevel@tonic-gate } 1437c478bd9Sstevel@tonic-gate 144*9525b14bSRao Shoaib /*% 1457c478bd9Sstevel@tonic-gate * is "a" a subdomain of "b"? 1467c478bd9Sstevel@tonic-gate */ 1477c478bd9Sstevel@tonic-gate int 1487c478bd9Sstevel@tonic-gate ns_subdomain(const char *a, const char *b) { 1497c478bd9Sstevel@tonic-gate return (ns_samename(a, b) != 1 && ns_samedomain(a, b)); 1507c478bd9Sstevel@tonic-gate } 1517c478bd9Sstevel@tonic-gate 152*9525b14bSRao Shoaib /*% 1537c478bd9Sstevel@tonic-gate * make a canonical copy of domain name "src" 154*9525b14bSRao Shoaib * 1557c478bd9Sstevel@tonic-gate * notes: 156*9525b14bSRao Shoaib * \code 1577c478bd9Sstevel@tonic-gate * foo -> foo. 1587c478bd9Sstevel@tonic-gate * foo. -> foo. 1597c478bd9Sstevel@tonic-gate * foo.. -> foo. 1607c478bd9Sstevel@tonic-gate * foo\. -> foo\.. 1617c478bd9Sstevel@tonic-gate * foo\\. -> foo\\. 162*9525b14bSRao Shoaib * \endcode 1637c478bd9Sstevel@tonic-gate */ 1647c478bd9Sstevel@tonic-gate 1657c478bd9Sstevel@tonic-gate int 1667c478bd9Sstevel@tonic-gate ns_makecanon(const char *src, char *dst, size_t dstsize) { 1677c478bd9Sstevel@tonic-gate size_t n = strlen(src); 1687c478bd9Sstevel@tonic-gate 169*9525b14bSRao Shoaib if (n + sizeof "." > dstsize) { /*%< Note: sizeof == 2 */ 1707c478bd9Sstevel@tonic-gate errno = EMSGSIZE; 1717c478bd9Sstevel@tonic-gate return (-1); 1727c478bd9Sstevel@tonic-gate } 1737c478bd9Sstevel@tonic-gate strcpy(dst, src); 174*9525b14bSRao Shoaib while (n >= 1U && dst[n - 1] == '.') /*%< Ends in "." */ 175*9525b14bSRao Shoaib if (n >= 2U && dst[n - 2] == '\\' && /*%< Ends in "\." */ 176*9525b14bSRao Shoaib (n < 3U || dst[n - 3] != '\\')) /*%< But not "\\." */ 1777c478bd9Sstevel@tonic-gate break; 1787c478bd9Sstevel@tonic-gate else 1797c478bd9Sstevel@tonic-gate dst[--n] = '\0'; 1807c478bd9Sstevel@tonic-gate dst[n++] = '.'; 1817c478bd9Sstevel@tonic-gate dst[n] = '\0'; 1827c478bd9Sstevel@tonic-gate return (0); 1837c478bd9Sstevel@tonic-gate } 1847c478bd9Sstevel@tonic-gate 185*9525b14bSRao Shoaib /*% 1867c478bd9Sstevel@tonic-gate * determine whether domain name "a" is the same as domain name "b" 187*9525b14bSRao Shoaib * 1887c478bd9Sstevel@tonic-gate * return: 189*9525b14bSRao Shoaib *\li -1 on error 190*9525b14bSRao Shoaib *\li 0 if names differ 191*9525b14bSRao Shoaib *\li 1 if names are the same 1927c478bd9Sstevel@tonic-gate */ 1937c478bd9Sstevel@tonic-gate 1947c478bd9Sstevel@tonic-gate int 1957c478bd9Sstevel@tonic-gate ns_samename(const char *a, const char *b) { 1967c478bd9Sstevel@tonic-gate char ta[NS_MAXDNAME], tb[NS_MAXDNAME]; 1977c478bd9Sstevel@tonic-gate 1987c478bd9Sstevel@tonic-gate if (ns_makecanon(a, ta, sizeof ta) < 0 || 1997c478bd9Sstevel@tonic-gate ns_makecanon(b, tb, sizeof tb) < 0) 2007c478bd9Sstevel@tonic-gate return (-1); 2017c478bd9Sstevel@tonic-gate if (strcasecmp(ta, tb) == 0) 2027c478bd9Sstevel@tonic-gate return (1); 2037c478bd9Sstevel@tonic-gate else 2047c478bd9Sstevel@tonic-gate return (0); 2057c478bd9Sstevel@tonic-gate } 206*9525b14bSRao Shoaib 207*9525b14bSRao Shoaib /*! \file */ 208