]> git.ipfire.org Git - thirdparty/squid.git/blob - src/acl/Acl.cc
SourceFormat Enforcement
[thirdparty/squid.git] / src / acl / Acl.cc
1 /*
2 * Copyright (C) 1996-2015 The Squid Software Foundation and contributors
3 *
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
7 */
8
9 /* DEBUG: section 28 Access Control */
10
11 #include "squid.h"
12 #include "acl/Acl.h"
13 #include "acl/Checklist.h"
14 #include "acl/Gadgets.h"
15 #include "anyp/PortCfg.h"
16 #include "cache_cf.h"
17 #include "ConfigParser.h"
18 #include "Debug.h"
19 #include "dlink.h"
20 #include "fatal.h"
21 #include "globals.h"
22 #include "profiler/Profiler.h"
23 #include "SquidConfig.h"
24
25 #include <vector>
26
27 const ACLFlag ACLFlags::NoFlags[1] = {ACL_F_END};
28
29 const char *AclMatchedName = NULL;
30
31 bool ACLFlags::supported(const ACLFlag f) const
32 {
33 if (f == ACL_F_REGEX_CASE)
34 return true;
35 return (supported_.find(f) != std::string::npos);
36 }
37
38 void
39 ACLFlags::parseFlags()
40 {
41 char *nextToken;
42 while ((nextToken = ConfigParser::PeekAtToken()) != NULL && nextToken[0] == '-') {
43 (void)ConfigParser::NextToken(); //Get token from cfg line
44 //if token is the "--" break flag
45 if (strcmp(nextToken, "--") == 0)
46 break;
47
48 for (const char *flg = nextToken+1; *flg!='\0'; flg++ ) {
49 if (supported(*flg)) {
50 makeSet(*flg);
51 } else {
52 debugs(28, 0, HERE << "Flag '" << *flg << "' not supported");
53 self_destruct();
54 }
55 }
56 }
57
58 /*Regex code needs to parse -i file*/
59 if ( isSet(ACL_F_REGEX_CASE)) {
60 ConfigParser::TokenPutBack("-i");
61 makeUnSet('i');
62 }
63 }
64
65 const char *
66 ACLFlags::flagsStr() const
67 {
68 static char buf[64];
69 if (flags_ == 0)
70 return "";
71
72 char *s = buf;
73 *s++ = '-';
74 for (ACLFlag f = 'A'; f <= 'z'; f++) {
75 // ACL_F_REGEX_CASE (-i) flag handled by ACLRegexData class, ignore
76 if (isSet(f) && f != ACL_F_REGEX_CASE)
77 *s++ = f;
78 }
79 *s = '\0';
80 return buf;
81 }
82
83 void *
84 ACL::operator new (size_t)
85 {
86 fatal ("unusable ACL::new");
87 return (void *)1;
88 }
89
90 void
91 ACL::operator delete (void *)
92 {
93 fatal ("unusable ACL::delete");
94 }
95
96 ACL *
97 ACL::FindByName(const char *name)
98 {
99 ACL *a;
100 debugs(28, 9, "ACL::FindByName '" << name << "'");
101
102 for (a = Config.aclList; a; a = a->next)
103 if (!strcasecmp(a->name, name))
104 return a;
105
106 debugs(28, 9, "ACL::FindByName found no match");
107
108 return NULL;
109 }
110
111 ACL *
112 ACL::Factory (char const *type)
113 {
114 ACL *result = Prototype::Factory (type);
115
116 if (!result)
117 fatal ("Unknown acl type in ACL::Factory");
118
119 return result;
120 }
121
122 ACL::ACL() :
123 cfgline(NULL),
124 next(NULL),
125 registered(false)
126 {
127 *name = 0;
128 }
129
130 bool ACL::valid () const
131 {
132 return true;
133 }
134
135 bool
136 ACL::matches(ACLChecklist *checklist) const
137 {
138 PROF_start(ACL_matches);
139 debugs(28, 5, "checking " << name);
140
141 // XXX: AclMatchedName does not contain a matched ACL name when the acl
142 // does not match. It contains the last (usually leaf) ACL name checked
143 // (or is NULL if no ACLs were checked).
144 AclMatchedName = name;
145
146 int result = 0;
147 if (!checklist->hasRequest() && requiresRequest()) {
148 debugs(28, DBG_IMPORTANT, "WARNING: " << name << " ACL is used in " <<
149 "context without an HTTP request. Assuming mismatch.");
150 } else if (!checklist->hasReply() && requiresReply()) {
151 debugs(28, DBG_IMPORTANT, "WARNING: " << name << " ACL is used in " <<
152 "context without an HTTP response. Assuming mismatch.");
153 } else {
154 // have to cast because old match() API is missing const
155 result = const_cast<ACL*>(this)->match(checklist);
156 }
157
158 const char *extra = checklist->asyncInProgress() ? " async" : "";
159 debugs(28, 3, "checked: " << name << " = " << result << extra);
160 PROF_stop(ACL_matches);
161 return result == 1; // true for match; false for everything else
162 }
163
164 void
165 ACL::context(const char *aName, const char *aCfgLine)
166 {
167 name[0] = '\0';
168 if (aName)
169 xstrncpy(name, aName, ACL_NAME_SZ-1);
170 safe_free(cfgline);
171 if (aCfgLine)
172 cfgline = xstrdup(aCfgLine);
173 }
174
175 void
176 ACL::ParseAclLine(ConfigParser &parser, ACL ** head)
177 {
178 /* we're already using strtok() to grok the line */
179 char *t = NULL;
180 ACL *A = NULL;
181 LOCAL_ARRAY(char, aclname, ACL_NAME_SZ);
182 int new_acl = 0;
183
184 /* snarf the ACL name */
185
186 if ((t = ConfigParser::NextToken()) == NULL) {
187 debugs(28, DBG_CRITICAL, "aclParseAclLine: missing ACL name.");
188 parser.destruct();
189 return;
190 }
191
192 if (strlen(t) >= ACL_NAME_SZ) {
193 debugs(28, DBG_CRITICAL, "aclParseAclLine: aclParseAclLine: ACL name '" << t <<
194 "' too long, max " << ACL_NAME_SZ - 1 << " characters supported");
195 parser.destruct();
196 return;
197 }
198
199 xstrncpy(aclname, t, ACL_NAME_SZ);
200 /* snarf the ACL type */
201 const char *theType;
202
203 if ((theType = ConfigParser::NextToken()) == NULL) {
204 debugs(28, DBG_CRITICAL, "aclParseAclLine: missing ACL type.");
205 parser.destruct();
206 return;
207 }
208
209 // Is this ACL going to work?
210 if (strcmp(theType, "myip") == 0) {
211 AnyP::PortCfgPointer p = HttpPortList;
212 while (p != NULL) {
213 // Bug 3239: not reliable when there is interception traffic coming
214 if (p->flags.natIntercept)
215 debugs(28, DBG_CRITICAL, "WARNING: 'myip' ACL is not reliable for interception proxies. Please use 'myportname' instead.");
216 p = p->next;
217 }
218 debugs(28, DBG_IMPORTANT, "UPGRADE: ACL 'myip' type is has been renamed to 'localip' and matches the IP the client connected to.");
219 theType = "localip";
220 } else if (strcmp(theType, "myport") == 0) {
221 AnyP::PortCfgPointer p = HttpPortList;
222 while (p != NULL) {
223 // Bug 3239: not reliable when there is interception traffic coming
224 // Bug 3239: myport - not reliable (yet) when there is interception traffic coming
225 if (p->flags.natIntercept)
226 debugs(28, DBG_CRITICAL, "WARNING: 'myport' ACL is not reliable for interception proxies. Please use 'myportname' instead.");
227 p = p->next;
228 }
229 theType = "localport";
230 debugs(28, DBG_IMPORTANT, "UPGRADE: ACL 'myport' type is has been renamed to 'localport' and matches the port the client connected to.");
231 }
232
233 if (!Prototype::Registered(theType)) {
234 debugs(28, DBG_CRITICAL, "FATAL: Invalid ACL type '" << theType << "'");
235 // XXX: make this an ERROR and skip the ACL creation. We *may* die later when its use is attempted. Or may not.
236 parser.destruct();
237 return;
238 }
239
240 if ((A = FindByName(aclname)) == NULL) {
241 debugs(28, 3, "aclParseAclLine: Creating ACL '" << aclname << "'");
242 A = ACL::Factory(theType);
243 A->context(aclname, config_input_line);
244 new_acl = 1;
245 } else {
246 if (strcmp (A->typeString(),theType) ) {
247 debugs(28, DBG_CRITICAL, "aclParseAclLine: ACL '" << A->name << "' already exists with different type.");
248 parser.destruct();
249 return;
250 }
251
252 debugs(28, 3, "aclParseAclLine: Appending to '" << aclname << "'");
253 new_acl = 0;
254 }
255
256 /*
257 * Here we set AclMatchedName in case we need to use it in a
258 * warning message in aclDomainCompare().
259 */
260 AclMatchedName = A->name; /* ugly */
261
262 A->flags.parseFlags();
263
264 /*split the function here */
265 A->parse();
266
267 /*
268 * Clear AclMatchedName from our temporary hack
269 */
270 AclMatchedName = NULL; /* ugly */
271
272 if (!new_acl)
273 return;
274
275 if (A->empty()) {
276 debugs(28, DBG_CRITICAL, "Warning: empty ACL: " << A->cfgline);
277 }
278
279 if (!A->valid()) {
280 fatalf("ERROR: Invalid ACL: %s\n",
281 A->cfgline);
282 }
283
284 // add to the global list for searching explicit ACLs by name
285 assert(head && *head == Config.aclList);
286 A->next = *head;
287 *head = A;
288
289 // register for centralized cleanup
290 aclRegister(A);
291 }
292
293 bool
294 ACL::isProxyAuth() const
295 {
296 return false;
297 }
298
299 /* ACL result caching routines */
300
301 int
302 ACL::matchForCache(ACLChecklist *)
303 {
304 /* This is a fatal to ensure that cacheMatchAcl calls are _only_
305 * made for supported acl types */
306 fatal("aclCacheMatchAcl: unknown or unexpected ACL type");
307 return 0; /* NOTREACHED */
308 }
309
310 /*
311 * we lookup an acl's cached results, and if we cannot find the acl being
312 * checked we check it and cache the result. This function is a template
313 * method to support caching of multiple acl types.
314 * Note that caching of time based acl's is not
315 * wise in long lived caches (i.e. the auth_user proxy match cache)
316 * RBC
317 * TODO: does a dlink_list perform well enough? Kinkie
318 */
319 int
320 ACL::cacheMatchAcl(dlink_list * cache, ACLChecklist *checklist)
321 {
322 acl_proxy_auth_match_cache *auth_match;
323 dlink_node *link;
324 link = cache->head;
325
326 while (link) {
327 auth_match = (acl_proxy_auth_match_cache *)link->data;
328
329 if (auth_match->acl_data == this) {
330 debugs(28, 4, "ACL::cacheMatchAcl: cache hit on acl '" << name << "' (" << this << ")");
331 return auth_match->matchrv;
332 }
333
334 link = link->next;
335 }
336
337 auth_match = new acl_proxy_auth_match_cache();
338 auth_match->matchrv = matchForCache (checklist);
339 auth_match->acl_data = this;
340 dlinkAddTail(auth_match, &auth_match->link, cache);
341 debugs(28, 4, "ACL::cacheMatchAcl: miss for '" << name << "'. Adding result " << auth_match->matchrv);
342 return auth_match->matchrv;
343 }
344
345 void
346 aclCacheMatchFlush(dlink_list * cache)
347 {
348 acl_proxy_auth_match_cache *auth_match;
349 dlink_node *link, *tmplink;
350 link = cache->head;
351
352 debugs(28, 8, "aclCacheMatchFlush called for cache " << cache);
353
354 while (link) {
355 auth_match = (acl_proxy_auth_match_cache *)link->data;
356 tmplink = link;
357 link = link->next;
358 dlinkDelete(tmplink, cache);
359 delete auth_match;
360 }
361 }
362
363 bool
364 ACL::requiresReply() const
365 {
366 return false;
367 }
368
369 bool
370 ACL::requiresRequest() const
371 {
372 return false;
373 }
374
375 /*********************/
376 /* Destroy functions */
377 /*********************/
378
379 ACL::~ACL()
380 {
381 debugs(28, 3, "ACL::~ACL: '" << cfgline << "'");
382 safe_free(cfgline);
383 AclMatchedName = NULL; // in case it was pointing to our name
384 }
385
386 ACL::Prototype::Prototype() : prototype (NULL), typeString (NULL) {}
387
388 ACL::Prototype::Prototype (ACL const *aPrototype, char const *aType) : prototype (aPrototype), typeString (aType)
389 {
390 registerMe ();
391 }
392
393 std::vector<ACL::Prototype const *> * ACL::Prototype::Registry;
394 void *ACL::Prototype::Initialized;
395
396 bool
397 ACL::Prototype::Registered(char const *aType)
398 {
399 debugs(28, 7, "ACL::Prototype::Registered: invoked for type " << aType);
400
401 for (iterator i = Registry->begin(); i != Registry->end(); ++i)
402 if (!strcmp (aType, (*i)->typeString)) {
403 debugs(28, 7, "ACL::Prototype::Registered: yes");
404 return true;
405 }
406
407 debugs(28, 7, "ACL::Prototype::Registered: no");
408 return false;
409 }
410
411 void
412 ACL::Prototype::registerMe ()
413 {
414 if (!Registry || (Initialized != ((char *)Registry - 5)) ) {
415 /* TODO: extract this */
416 /* Not initialised */
417 Registry = new std::vector<ACL::Prototype const *>;
418 Initialized = (char *)Registry - 5;
419 }
420
421 if (Registered (typeString))
422 fatalf ("Attempt to register %s twice", typeString);
423
424 Registry->push_back (this);
425 }
426
427 ACL::Prototype::~Prototype()
428 {
429 // TODO: unregister me
430 }
431
432 ACL *
433 ACL::Prototype::Factory (char const *typeToClone)
434 {
435 debugs(28, 4, "ACL::Prototype::Factory: cloning an object for type '" << typeToClone << "'");
436
437 for (iterator i = Registry->begin(); i != Registry->end(); ++i)
438 if (!strcmp (typeToClone, (*i)->typeString)) {
439 ACL *A = (*i)->prototype->clone();
440 A->flags = (*i)->prototype->flags;
441 return A;
442 }
443
444 debugs(28, 4, "ACL::Prototype::Factory: cloning failed, no type '" << typeToClone << "' available");
445
446 return NULL;
447 }
448
449 void
450 ACL::Initialize()
451 {
452 ACL *a = Config.aclList;
453 debugs(53, 3, "ACL::Initialize");
454
455 while (a) {
456 a->prepareForUse();
457 a = a->next;
458 }
459 }
460