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