2 * Copyright (C) 1996-2015 The Squid Software Foundation and contributors
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.
9 /* DEBUG: section 28 Access Control */
13 #include "acl/Checklist.h"
14 #include "acl/Gadgets.h"
15 #include "anyp/PortCfg.h"
17 #include "ConfigParser.h"
22 #include "profiler/Profiler.h"
23 #include "SquidConfig.h"
27 const ACLFlag
ACLFlags::NoFlags
[1] = {ACL_F_END
};
29 const char *AclMatchedName
= NULL
;
31 bool ACLFlags::supported(const ACLFlag f
) const
33 if (f
== ACL_F_REGEX_CASE
)
35 return (supported_
.find(f
) != std::string::npos
);
39 ACLFlags::parseFlags()
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)
48 for (const char *flg
= nextToken
+1; *flg
!='\0'; flg
++ ) {
49 if (supported(*flg
)) {
52 debugs(28, 0, HERE
<< "Flag '" << *flg
<< "' not supported");
58 /*Regex code needs to parse -i file*/
59 if ( isSet(ACL_F_REGEX_CASE
)) {
60 ConfigParser::TokenPutBack("-i");
66 ACLFlags::flagsStr() const
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
)
84 ACL::operator new (size_t)
86 fatal ("unusable ACL::new");
91 ACL::operator delete (void *)
93 fatal ("unusable ACL::delete");
97 ACL::FindByName(const char *name
)
100 debugs(28, 9, "ACL::FindByName '" << name
<< "'");
102 for (a
= Config
.aclList
; a
; a
= a
->next
)
103 if (!strcasecmp(a
->name
, name
))
106 debugs(28, 9, "ACL::FindByName found no match");
112 ACL::Factory (char const *type
)
114 ACL
*result
= Prototype::Factory (type
);
117 fatal ("Unknown acl type in ACL::Factory");
130 ACL::ACL(const ACLFlag flgs
[]) :
139 bool ACL::valid () const
145 ACL::matches(ACLChecklist
*checklist
) const
147 PROF_start(ACL_matches
);
148 debugs(28, 5, "checking " << name
);
150 // XXX: AclMatchedName does not contain a matched ACL name when the acl
151 // does not match. It contains the last (usually leaf) ACL name checked
152 // (or is NULL if no ACLs were checked).
153 AclMatchedName
= name
;
156 if (!checklist
->hasAle() && requiresAle()) {
157 debugs(28, DBG_IMPORTANT
, "WARNING: " << name
<< " ACL is used in " <<
158 "context without an ALE state. Assuming mismatch.");
159 } else if (!checklist
->hasRequest() && requiresRequest()) {
160 debugs(28, DBG_IMPORTANT
, "WARNING: " << name
<< " ACL is used in " <<
161 "context without an HTTP request. Assuming mismatch.");
162 } else if (!checklist
->hasReply() && requiresReply()) {
163 debugs(28, DBG_IMPORTANT
, "WARNING: " << name
<< " ACL is used in " <<
164 "context without an HTTP response. Assuming mismatch.");
166 // make sure the ALE has as much data as possible
168 checklist
->syncAle();
170 // have to cast because old match() API is missing const
171 result
= const_cast<ACL
*>(this)->match(checklist
);
174 const char *extra
= checklist
->asyncInProgress() ? " async" : "";
175 debugs(28, 3, "checked: " << name
<< " = " << result
<< extra
);
176 PROF_stop(ACL_matches
);
177 return result
== 1; // true for match; false for everything else
181 ACL::context(const char *aName
, const char *aCfgLine
)
185 xstrncpy(name
, aName
, ACL_NAME_SZ
-1);
188 cfgline
= xstrdup(aCfgLine
);
192 ACL::ParseAclLine(ConfigParser
&parser
, ACL
** head
)
194 /* we're already using strtok() to grok the line */
197 LOCAL_ARRAY(char, aclname
, ACL_NAME_SZ
);
200 /* snarf the ACL name */
202 if ((t
= ConfigParser::NextToken()) == NULL
) {
203 debugs(28, DBG_CRITICAL
, "aclParseAclLine: missing ACL name.");
208 if (strlen(t
) >= ACL_NAME_SZ
) {
209 debugs(28, DBG_CRITICAL
, "aclParseAclLine: aclParseAclLine: ACL name '" << t
<<
210 "' too long, max " << ACL_NAME_SZ
- 1 << " characters supported");
215 xstrncpy(aclname
, t
, ACL_NAME_SZ
);
216 /* snarf the ACL type */
219 if ((theType
= ConfigParser::NextToken()) == NULL
) {
220 debugs(28, DBG_CRITICAL
, "aclParseAclLine: missing ACL type.");
225 // Is this ACL going to work?
226 if (strcmp(theType
, "myip") == 0) {
227 AnyP::PortCfgPointer p
= HttpPortList
;
229 // Bug 3239: not reliable when there is interception traffic coming
230 if (p
->flags
.natIntercept
)
231 debugs(28, DBG_CRITICAL
, "WARNING: 'myip' ACL is not reliable for interception proxies. Please use 'myportname' instead.");
234 debugs(28, DBG_IMPORTANT
, "UPGRADE: ACL 'myip' type is has been renamed to 'localip' and matches the IP the client connected to.");
236 } else if (strcmp(theType
, "myport") == 0) {
237 AnyP::PortCfgPointer p
= HttpPortList
;
239 // Bug 3239: not reliable when there is interception traffic coming
240 // Bug 3239: myport - not reliable (yet) when there is interception traffic coming
241 if (p
->flags
.natIntercept
)
242 debugs(28, DBG_CRITICAL
, "WARNING: 'myport' ACL is not reliable for interception proxies. Please use 'myportname' instead.");
245 theType
= "localport";
246 debugs(28, DBG_IMPORTANT
, "UPGRADE: ACL 'myport' type is has been renamed to 'localport' and matches the port the client connected to.");
249 if (!Prototype::Registered(theType
)) {
250 debugs(28, DBG_CRITICAL
, "FATAL: Invalid ACL type '" << theType
<< "'");
251 // XXX: make this an ERROR and skip the ACL creation. We *may* die later when its use is attempted. Or may not.
256 if ((A
= FindByName(aclname
)) == NULL
) {
257 debugs(28, 3, "aclParseAclLine: Creating ACL '" << aclname
<< "'");
258 A
= ACL::Factory(theType
);
259 A
->context(aclname
, config_input_line
);
262 if (strcmp (A
->typeString(),theType
) ) {
263 debugs(28, DBG_CRITICAL
, "aclParseAclLine: ACL '" << A
->name
<< "' already exists with different type.");
268 debugs(28, 3, "aclParseAclLine: Appending to '" << aclname
<< "'");
273 * Here we set AclMatchedName in case we need to use it in a
274 * warning message in aclDomainCompare().
276 AclMatchedName
= A
->name
; /* ugly */
278 A
->flags
.parseFlags();
280 /*split the function here */
284 * Clear AclMatchedName from our temporary hack
286 AclMatchedName
= NULL
; /* ugly */
292 debugs(28, DBG_CRITICAL
, "Warning: empty ACL: " << A
->cfgline
);
296 fatalf("ERROR: Invalid ACL: %s\n",
300 // add to the global list for searching explicit ACLs by name
301 assert(head
&& *head
== Config
.aclList
);
305 // register for centralized cleanup
310 ACL::isProxyAuth() const
315 /* ACL result caching routines */
318 ACL::matchForCache(ACLChecklist
*)
320 /* This is a fatal to ensure that cacheMatchAcl calls are _only_
321 * made for supported acl types */
322 fatal("aclCacheMatchAcl: unknown or unexpected ACL type");
323 return 0; /* NOTREACHED */
327 * we lookup an acl's cached results, and if we cannot find the acl being
328 * checked we check it and cache the result. This function is a template
329 * method to support caching of multiple acl types.
330 * Note that caching of time based acl's is not
331 * wise in long lived caches (i.e. the auth_user proxy match cache)
333 * TODO: does a dlink_list perform well enough? Kinkie
336 ACL::cacheMatchAcl(dlink_list
* cache
, ACLChecklist
*checklist
)
338 acl_proxy_auth_match_cache
*auth_match
;
343 auth_match
= (acl_proxy_auth_match_cache
*)link
->data
;
345 if (auth_match
->acl_data
== this) {
346 debugs(28, 4, "ACL::cacheMatchAcl: cache hit on acl '" << name
<< "' (" << this << ")");
347 return auth_match
->matchrv
;
353 auth_match
= new acl_proxy_auth_match_cache(matchForCache(checklist
), this);
354 dlinkAddTail(auth_match
, &auth_match
->link
, cache
);
355 debugs(28, 4, "ACL::cacheMatchAcl: miss for '" << name
<< "'. Adding result " << auth_match
->matchrv
);
356 return auth_match
->matchrv
;
360 aclCacheMatchFlush(dlink_list
* cache
)
362 acl_proxy_auth_match_cache
*auth_match
;
363 dlink_node
*link
, *tmplink
;
366 debugs(28, 8, "aclCacheMatchFlush called for cache " << cache
);
369 auth_match
= (acl_proxy_auth_match_cache
*)link
->data
;
372 dlinkDelete(tmplink
, cache
);
378 ACL::requiresAle() const
384 ACL::requiresReply() const
390 ACL::requiresRequest() const
395 /*********************/
396 /* Destroy functions */
397 /*********************/
401 debugs(28, 3, "freeing ACL " << name
);
403 AclMatchedName
= NULL
; // in case it was pointing to our name
406 ACL::Prototype::Prototype() : prototype (NULL
), typeString (NULL
) {}
408 ACL::Prototype::Prototype (ACL
const *aPrototype
, char const *aType
) : prototype (aPrototype
), typeString (aType
)
413 std::vector
<ACL::Prototype
const *> * ACL::Prototype::Registry
;
414 void *ACL::Prototype::Initialized
;
417 ACL::Prototype::Registered(char const *aType
)
419 debugs(28, 7, "ACL::Prototype::Registered: invoked for type " << aType
);
421 for (iterator i
= Registry
->begin(); i
!= Registry
->end(); ++i
)
422 if (!strcmp (aType
, (*i
)->typeString
)) {
423 debugs(28, 7, "ACL::Prototype::Registered: yes");
427 debugs(28, 7, "ACL::Prototype::Registered: no");
432 ACL::Prototype::registerMe ()
434 if (!Registry
|| (Initialized
!= ((char *)Registry
- 5)) ) {
435 /* TODO: extract this */
436 /* Not initialised */
437 Registry
= new std::vector
<ACL::Prototype
const *>;
438 Initialized
= (char *)Registry
- 5;
441 if (Registered (typeString
))
442 fatalf ("Attempt to register %s twice", typeString
);
444 Registry
->push_back (this);
447 ACL::Prototype::~Prototype()
449 // TODO: unregister me
453 ACL::Prototype::Factory (char const *typeToClone
)
455 debugs(28, 4, "ACL::Prototype::Factory: cloning an object for type '" << typeToClone
<< "'");
457 for (iterator i
= Registry
->begin(); i
!= Registry
->end(); ++i
)
458 if (!strcmp (typeToClone
, (*i
)->typeString
)) {
459 ACL
*A
= (*i
)->prototype
->clone();
460 A
->flags
= (*i
)->prototype
->flags
;
464 debugs(28, 4, "ACL::Prototype::Factory: cloning failed, no type '" << typeToClone
<< "' available");
472 ACL
*a
= Config
.aclList
;
473 debugs(53, 3, "ACL::Initialize");