2 * Copyright (C) 1996-2014 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");
64 ACLFlags::flagsStr() const
72 for (ACLFlag f
= 'A'; f
<= 'z'; f
++) {
73 // ACL_F_REGEX_CASE (-i) flag handled by ACLRegexData class, ignore
74 if (isSet(f
) && f
!= ACL_F_REGEX_CASE
)
82 ACL::operator new (size_t byteCount
)
84 fatal ("unusable ACL::new");
89 ACL::operator delete (void *address
)
91 fatal ("unusable ACL::delete");
95 ACL::FindByName(const char *name
)
98 debugs(28, 9, "ACL::FindByName '" << name
<< "'");
100 for (a
= Config
.aclList
; a
; a
= a
->next
)
101 if (!strcasecmp(a
->name
, name
))
104 debugs(28, 9, "ACL::FindByName found no match");
110 ACL::Factory (char const *type
)
112 ACL
*result
= Prototype::Factory (type
);
115 fatal ("Unknown acl type in ACL::Factory");
128 bool ACL::valid () const
134 ACL::matches(ACLChecklist
*checklist
) const
136 PROF_start(ACL_matches
);
137 debugs(28, 5, "checking " << name
);
139 // XXX: AclMatchedName does not contain a matched ACL name when the acl
140 // does not match. It contains the last (usually leaf) ACL name checked
141 // (or is NULL if no ACLs were checked).
142 AclMatchedName
= name
;
145 if (!checklist
->hasRequest() && requiresRequest()) {
146 debugs(28, DBG_IMPORTANT
, "WARNING: " << name
<< " ACL is used in " <<
147 "context without an HTTP request. Assuming mismatch.");
148 } else if (!checklist
->hasReply() && requiresReply()) {
149 debugs(28, DBG_IMPORTANT
, "WARNING: " << name
<< " ACL is used in " <<
150 "context without an HTTP response. Assuming mismatch.");
152 // have to cast because old match() API is missing const
153 result
= const_cast<ACL
*>(this)->match(checklist
);
156 const char *extra
= checklist
->asyncInProgress() ? " async" : "";
157 debugs(28, 3, "checked: " << name
<< " = " << result
<< extra
);
158 PROF_stop(ACL_matches
);
159 return result
== 1; // true for match; false for everything else
163 ACL::context(const char *aName
, const char *aCfgLine
)
167 xstrncpy(name
, aName
, ACL_NAME_SZ
-1);
170 cfgline
= xstrdup(aCfgLine
);
174 ACL::ParseAclLine(ConfigParser
&parser
, ACL
** head
)
176 /* we're already using strtok() to grok the line */
179 LOCAL_ARRAY(char, aclname
, ACL_NAME_SZ
);
182 /* snarf the ACL name */
184 if ((t
= ConfigParser::NextToken()) == NULL
) {
185 debugs(28, DBG_CRITICAL
, "aclParseAclLine: missing ACL name.");
190 if (strlen(t
) >= ACL_NAME_SZ
) {
191 debugs(28, DBG_CRITICAL
, "aclParseAclLine: aclParseAclLine: ACL name '" << t
<<
192 "' too long, max " << ACL_NAME_SZ
- 1 << " characters supported");
197 xstrncpy(aclname
, t
, ACL_NAME_SZ
);
198 /* snarf the ACL type */
201 if ((theType
= ConfigParser::NextToken()) == NULL
) {
202 debugs(28, DBG_CRITICAL
, "aclParseAclLine: missing ACL type.");
207 // Is this ACL going to work?
208 if (strcmp(theType
, "myip") == 0) {
209 AnyP::PortCfgPointer p
= HttpPortList
;
211 // Bug 3239: not reliable when there is interception traffic coming
212 if (p
->flags
.natIntercept
)
213 debugs(28, DBG_CRITICAL
, "WARNING: 'myip' ACL is not reliable for interception proxies. Please use 'myportname' instead.");
216 debugs(28, DBG_IMPORTANT
, "UPGRADE: ACL 'myip' type is has been renamed to 'localip' and matches the IP the client connected to.");
218 } else if (strcmp(theType
, "myport") == 0) {
219 AnyP::PortCfgPointer p
= HttpPortList
;
221 // Bug 3239: not reliable when there is interception traffic coming
222 // Bug 3239: myport - not reliable (yet) when there is interception traffic coming
223 if (p
->flags
.natIntercept
)
224 debugs(28, DBG_CRITICAL
, "WARNING: 'myport' ACL is not reliable for interception proxies. Please use 'myportname' instead.");
227 theType
= "localport";
228 debugs(28, DBG_IMPORTANT
, "UPGRADE: ACL 'myport' type is has been renamed to 'localport' and matches the port the client connected to.");
231 if (!Prototype::Registered(theType
)) {
232 debugs(28, DBG_CRITICAL
, "FATAL: Invalid ACL type '" << theType
<< "'");
233 // XXX: make this an ERROR and skip the ACL creation. We *may* die later when its use is attempted. Or may not.
238 if ((A
= FindByName(aclname
)) == NULL
) {
239 debugs(28, 3, "aclParseAclLine: Creating ACL '" << aclname
<< "'");
240 A
= ACL::Factory(theType
);
241 A
->context(aclname
, config_input_line
);
244 if (strcmp (A
->typeString(),theType
) ) {
245 debugs(28, DBG_CRITICAL
, "aclParseAclLine: ACL '" << A
->name
<< "' already exists with different type.");
250 debugs(28, 3, "aclParseAclLine: Appending to '" << aclname
<< "'");
255 * Here we set AclMatchedName in case we need to use it in a
256 * warning message in aclDomainCompare().
258 AclMatchedName
= A
->name
; /* ugly */
260 A
->flags
.parseFlags();
262 /*split the function here */
266 * Clear AclMatchedName from our temporary hack
268 AclMatchedName
= NULL
; /* ugly */
274 debugs(28, DBG_CRITICAL
, "Warning: empty ACL: " << A
->cfgline
);
278 fatalf("ERROR: Invalid ACL: %s\n",
282 // add to the global list for searching explicit ACLs by name
283 assert(head
&& *head
== Config
.aclList
);
287 // register for centralized cleanup
292 ACL::isProxyAuth() const
297 /* ACL result caching routines */
300 ACL::matchForCache(ACLChecklist
*checklist
)
302 /* This is a fatal to ensure that cacheMatchAcl calls are _only_
303 * made for supported acl types */
304 fatal("aclCacheMatchAcl: unknown or unexpected ACL type");
305 return 0; /* NOTREACHED */
309 * we lookup an acl's cached results, and if we cannot find the acl being
310 * checked we check it and cache the result. This function is a template
311 * method to support caching of multiple acl types.
312 * Note that caching of time based acl's is not
313 * wise in long lived caches (i.e. the auth_user proxy match cache)
315 * TODO: does a dlink_list perform well enough? Kinkie
318 ACL::cacheMatchAcl(dlink_list
* cache
, ACLChecklist
*checklist
)
320 acl_proxy_auth_match_cache
*auth_match
;
325 auth_match
= (acl_proxy_auth_match_cache
*)link
->data
;
327 if (auth_match
->acl_data
== this) {
328 debugs(28, 4, "ACL::cacheMatchAcl: cache hit on acl '" << name
<< "' (" << this << ")");
329 return auth_match
->matchrv
;
335 auth_match
= new acl_proxy_auth_match_cache();
336 auth_match
->matchrv
= matchForCache (checklist
);
337 auth_match
->acl_data
= this;
338 dlinkAddTail(auth_match
, &auth_match
->link
, cache
);
339 debugs(28, 4, "ACL::cacheMatchAcl: miss for '" << name
<< "'. Adding result " << auth_match
->matchrv
);
340 return auth_match
->matchrv
;
344 aclCacheMatchFlush(dlink_list
* cache
)
346 acl_proxy_auth_match_cache
*auth_match
;
347 dlink_node
*link
, *tmplink
;
350 debugs(28, 8, "aclCacheMatchFlush called for cache " << cache
);
353 auth_match
= (acl_proxy_auth_match_cache
*)link
->data
;
356 dlinkDelete(tmplink
, cache
);
362 ACL::requiresReply() const
368 ACL::requiresRequest() const
373 /*********************/
374 /* Destroy functions */
375 /*********************/
379 debugs(28, 3, "ACL::~ACL: '" << cfgline
<< "'");
381 AclMatchedName
= NULL
; // in case it was pointing to our name
384 ACL::Prototype::Prototype() : prototype (NULL
), typeString (NULL
) {}
386 ACL::Prototype::Prototype (ACL
const *aPrototype
, char const *aType
) : prototype (aPrototype
), typeString (aType
)
391 std::vector
<ACL::Prototype
const *> * ACL::Prototype::Registry
;
392 void *ACL::Prototype::Initialized
;
395 ACL::Prototype::Registered(char const *aType
)
397 debugs(28, 7, "ACL::Prototype::Registered: invoked for type " << aType
);
399 for (iterator i
= Registry
->begin(); i
!= Registry
->end(); ++i
)
400 if (!strcmp (aType
, (*i
)->typeString
)) {
401 debugs(28, 7, "ACL::Prototype::Registered: yes");
405 debugs(28, 7, "ACL::Prototype::Registered: no");
410 ACL::Prototype::registerMe ()
412 if (!Registry
|| (Initialized
!= ((char *)Registry
- 5)) ) {
413 /* TODO: extract this */
414 /* Not initialised */
415 Registry
= new std::vector
<ACL::Prototype
const *>;
416 Initialized
= (char *)Registry
- 5;
419 if (Registered (typeString
))
420 fatalf ("Attempt to register %s twice", typeString
);
422 Registry
->push_back (this);
425 ACL::Prototype::~Prototype()
427 // TODO: unregister me
431 ACL::Prototype::Factory (char const *typeToClone
)
433 debugs(28, 4, "ACL::Prototype::Factory: cloning an object for type '" << typeToClone
<< "'");
435 for (iterator i
= Registry
->begin(); i
!= Registry
->end(); ++i
)
436 if (!strcmp (typeToClone
, (*i
)->typeString
)) {
437 ACL
*A
= (*i
)->prototype
->clone();
438 A
->flags
= (*i
)->prototype
->flags
;
442 debugs(28, 4, "ACL::Prototype::Factory: cloning failed, no type '" << typeToClone
<< "' available");
450 ACL
*a
= Config
.aclList
;
451 debugs(53, 3, "ACL::Initialize");