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 bool ACL::valid () const
136 ACL::matches(ACLChecklist
*checklist
) const
138 PROF_start(ACL_matches
);
139 debugs(28, 5, "checking " << name
);
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
;
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.");
154 // have to cast because old match() API is missing const
155 result
= const_cast<ACL
*>(this)->match(checklist
);
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
165 ACL::context(const char *aName
, const char *aCfgLine
)
169 xstrncpy(name
, aName
, ACL_NAME_SZ
-1);
172 cfgline
= xstrdup(aCfgLine
);
176 ACL::ParseAclLine(ConfigParser
&parser
, ACL
** head
)
178 /* we're already using strtok() to grok the line */
181 LOCAL_ARRAY(char, aclname
, ACL_NAME_SZ
);
184 /* snarf the ACL name */
186 if ((t
= ConfigParser::NextToken()) == NULL
) {
187 debugs(28, DBG_CRITICAL
, "aclParseAclLine: missing ACL name.");
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");
199 xstrncpy(aclname
, t
, ACL_NAME_SZ
);
200 /* snarf the ACL type */
203 if ((theType
= ConfigParser::NextToken()) == NULL
) {
204 debugs(28, DBG_CRITICAL
, "aclParseAclLine: missing ACL type.");
209 // Is this ACL going to work?
210 if (strcmp(theType
, "myip") == 0) {
211 AnyP::PortCfgPointer p
= HttpPortList
;
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.");
218 debugs(28, DBG_IMPORTANT
, "UPGRADE: ACL 'myip' type is has been renamed to 'localip' and matches the IP the client connected to.");
220 } else if (strcmp(theType
, "myport") == 0) {
221 AnyP::PortCfgPointer p
= HttpPortList
;
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.");
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.");
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.
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
);
246 if (strcmp (A
->typeString(),theType
) ) {
247 debugs(28, DBG_CRITICAL
, "aclParseAclLine: ACL '" << A
->name
<< "' already exists with different type.");
252 debugs(28, 3, "aclParseAclLine: Appending to '" << aclname
<< "'");
257 * Here we set AclMatchedName in case we need to use it in a
258 * warning message in aclDomainCompare().
260 AclMatchedName
= A
->name
; /* ugly */
262 A
->flags
.parseFlags();
264 /*split the function here */
268 * Clear AclMatchedName from our temporary hack
270 AclMatchedName
= NULL
; /* ugly */
276 debugs(28, DBG_CRITICAL
, "Warning: empty ACL: " << A
->cfgline
);
280 fatalf("ERROR: Invalid ACL: %s\n",
284 // add to the global list for searching explicit ACLs by name
285 assert(head
&& *head
== Config
.aclList
);
289 // register for centralized cleanup
294 ACL::isProxyAuth() const
299 /* ACL result caching routines */
302 ACL::matchForCache(ACLChecklist
*)
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 */
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)
317 * TODO: does a dlink_list perform well enough? Kinkie
320 ACL::cacheMatchAcl(dlink_list
* cache
, ACLChecklist
*checklist
)
322 acl_proxy_auth_match_cache
*auth_match
;
327 auth_match
= (acl_proxy_auth_match_cache
*)link
->data
;
329 if (auth_match
->acl_data
== this) {
330 debugs(28, 4, "ACL::cacheMatchAcl: cache hit on acl '" << name
<< "' (" << this << ")");
331 return auth_match
->matchrv
;
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
;
346 aclCacheMatchFlush(dlink_list
* cache
)
348 acl_proxy_auth_match_cache
*auth_match
;
349 dlink_node
*link
, *tmplink
;
352 debugs(28, 8, "aclCacheMatchFlush called for cache " << cache
);
355 auth_match
= (acl_proxy_auth_match_cache
*)link
->data
;
358 dlinkDelete(tmplink
, cache
);
364 ACL::requiresReply() const
370 ACL::requiresRequest() const
375 /*********************/
376 /* Destroy functions */
377 /*********************/
381 debugs(28, 3, "freeing ACL " << name
);
383 AclMatchedName
= NULL
; // in case it was pointing to our name
386 ACL::Prototype::Prototype() : prototype (NULL
), typeString (NULL
) {}
388 ACL::Prototype::Prototype (ACL
const *aPrototype
, char const *aType
) : prototype (aPrototype
), typeString (aType
)
393 std::vector
<ACL::Prototype
const *> * ACL::Prototype::Registry
;
394 void *ACL::Prototype::Initialized
;
397 ACL::Prototype::Registered(char const *aType
)
399 debugs(28, 7, "ACL::Prototype::Registered: invoked for type " << aType
);
401 for (iterator i
= Registry
->begin(); i
!= Registry
->end(); ++i
)
402 if (!strcmp (aType
, (*i
)->typeString
)) {
403 debugs(28, 7, "ACL::Prototype::Registered: yes");
407 debugs(28, 7, "ACL::Prototype::Registered: no");
412 ACL::Prototype::registerMe ()
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;
421 if (Registered (typeString
))
422 fatalf ("Attempt to register %s twice", typeString
);
424 Registry
->push_back (this);
427 ACL::Prototype::~Prototype()
429 // TODO: unregister me
433 ACL::Prototype::Factory (char const *typeToClone
)
435 debugs(28, 4, "ACL::Prototype::Factory: cloning an object for type '" << typeToClone
<< "'");
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
;
444 debugs(28, 4, "ACL::Prototype::Factory: cloning failed, no type '" << typeToClone
<< "' available");
452 ACL
*a
= Config
.aclList
;
453 debugs(53, 3, "ACL::Initialize");