]> git.ipfire.org Git - thirdparty/squid.git/blob - src/acl/DomainData.cc
Removed inclusion of protos.h from most clients.
[thirdparty/squid.git] / src / acl / DomainData.cc
1 /*
2 * $Id$
3 *
4 * DEBUG: section 28 Access Control
5 * AUTHOR: Duane Wessels
6 *
7 * SQUID Web Proxy Cache http://www.squid-cache.org/
8 * ----------------------------------------------------------
9 *
10 * Squid is the result of efforts by numerous individuals from
11 * the Internet community; see the CONTRIBUTORS file for full
12 * details. Many organizations have provided support for Squid's
13 * development; see the SPONSORS file for full details. Squid is
14 * Copyrighted (C) 2001 by the Regents of the University of
15 * California; see the COPYRIGHT file for full details. Squid
16 * incorporates software developed and/or copyrighted by other
17 * sources; see the CREDITS file for full details.
18 *
19 * This program is free software; you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation; either version 2 of the License, or
22 * (at your option) any later version.
23 *
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
28 *
29 * You should have received a copy of the GNU General Public License
30 * along with this program; if not, write to the Free Software
31 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
32 *
33 *
34 * Copyright (c) 2003, Robert Collins <robertc@squid-cache.org>
35 */
36
37 #include "squid.h"
38 #include "acl/DomainData.h"
39 #include "acl/Checklist.h"
40 #include "cache_cf.h"
41 #include "Debug.h"
42 #include "wordlist.h"
43 #include "URL.h"
44
45 template<class T>
46 inline void
47 xRefFree(T &thing)
48 {
49 xfree (thing);
50 }
51
52 ACLDomainData::~ACLDomainData()
53 {
54 if (domains)
55 domains->destroy(xRefFree);
56 }
57
58 template<class T>
59 inline int
60 splaystrcasecmp (T&l, T&r)
61 {
62 return strcasecmp ((char *)l,(char *)r);
63 }
64
65 template<class T>
66 inline int
67 splaystrcmp (T&l, T&r)
68 {
69 return strcmp ((char *)l,(char *)r);
70 }
71
72 /* general compare functions, these are used for tree search algorithms
73 * so they return <0, 0 or >0 */
74
75 /* compare a host and a domain */
76
77 static int
78 aclHostDomainCompare( char *const &a, char * const &b)
79 {
80 const char *h = static_cast<const char *>(a);
81 const char *d = static_cast<const char *>(b);
82 return matchDomainName(h, d);
83 }
84
85 /* compare two domains */
86
87 template<class T>
88 int
89 aclDomainCompare(T const &a, T const &b)
90 {
91 char * const d1 = static_cast<char *>(b);
92 char * const d2 = static_cast<char *>(a);
93 int ret;
94 ret = aclHostDomainCompare(d1, d2);
95
96 if (ret != 0) {
97 char *const d3 = d2;
98 char *const d4 = d1;
99 ret = aclHostDomainCompare(d3, d4);
100 if (ret == 0) {
101 // When a.example.com comes after .example.com in an ACL
102 // sub-domain is ignored. That is okay. Just important
103 debugs(28, DBG_IMPORTANT, "WARNING: '" << d3 << "' is a subdomain of '" << d4 << "'");
104 debugs(28, DBG_IMPORTANT, "WARNING: because of this '" << d3 << "' is ignored to keep splay tree searching predictable");
105 debugs(28, DBG_IMPORTANT, "WARNING: You should remove '" << (*d3=='.'?d4:d3) << "' from the ACL named '" << AclMatchedName << "'");
106 }
107 } else if (ret == 0) {
108 // When a.example.com comes before .example.com in an ACL
109 // discarding the wildcard is critically bad.
110 debugs(28, DBG_CRITICAL, "ERROR: '" << d1 << "' is a subdomain of '" << d2 << "'");
111 debugs(28, DBG_CRITICAL, "ERROR: because of this '" << d2 << "' is ignored to keep splay tree searching predictable");
112 debugs(28, DBG_CRITICAL, "ERROR: You should remove '" << (*d1=='.'?d2:d1) << "' from the ACL named '" << AclMatchedName << "'");
113 self_destruct();
114 }
115
116 return ret;
117 }
118
119 bool
120 ACLDomainData::match(char const *host)
121 {
122 if (host == NULL)
123 return 0;
124
125 debugs(28, 3, "aclMatchDomainList: checking '" << host << "'");
126
127 domains = domains->splay((char *)host, aclHostDomainCompare);
128
129 debugs(28, 3, "aclMatchDomainList: '" << host << "' " << (splayLastResult ? "NOT found" : "found"));
130
131 return !splayLastResult;
132 }
133
134 static void
135 aclDumpDomainListWalkee(char * const & node_data, void *outlist)
136 {
137 /* outlist is really a wordlist ** */
138 wordlistAdd((wordlist **)outlist, (char const *)node_data);
139 }
140
141 wordlist *
142 ACLDomainData::dump()
143 {
144 wordlist *wl = NULL;
145 /* damn this is VERY inefficient for long ACL lists... filling
146 * a wordlist this way costs Sum(1,N) iterations. For instance
147 * a 1000-elements list will be filled in 499500 iterations.
148 */
149 domains->walk(aclDumpDomainListWalkee, &wl);
150 return wl;
151 }
152
153 void
154 ACLDomainData::parse()
155 {
156 char *t = NULL;
157
158 while ((t = strtokFile())) {
159 Tolower(t);
160 domains = domains->insert(xstrdup(t), aclDomainCompare);
161 }
162 }
163
164 bool
165 ACLDomainData::empty() const
166 {
167 return domains->empty();
168 }
169
170 ACLData<char const *> *
171 ACLDomainData::clone() const
172 {
173 /* Splay trees don't clone yet. */
174 assert (!domains);
175 return new ACLDomainData;
176 }