2 * DEBUG: section 28 Access Control
4 * SQUID Web Proxy Cache http://www.squid-cache.org/
5 * ----------------------------------------------------------
7 * Squid is the result of efforts by numerous individuals from
8 * the Internet community; see the CONTRIBUTORS file for full
9 * details. Many organizations have provided support for Squid's
10 * development; see the SPONSORS file for full details. Squid is
11 * Copyrighted (C) 2001 by the Regents of the University of
12 * California; see the COPYRIGHT file for full details. Squid
13 * incorporates software developed and/or copyrighted by other
14 * sources; see the CREDITS file for full details.
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
33 #include "acl/Checklist.h"
34 #include "anyp/PortCfg.h"
36 #include "ConfigParser.h"
40 #include "profiler/Profiler.h"
41 #include "SquidConfig.h"
43 const ACLFlag
ACLFlags::NoFlags
[1] = {ACL_F_END
};
45 const char *AclMatchedName
= NULL
;
47 bool ACLFlags::supported(const ACLFlag f
) const
49 if (f
== ACL_F_REGEX_CASE
)
51 return (supported_
.find(f
) != std::string::npos
);
55 ACLFlags::parseFlags()
58 while ((nextToken
= ConfigParser::strtokFile()) != NULL
&& nextToken
[0] == '-') {
60 //if token is the "--" break flag
61 if (strcmp(nextToken
, "--") == 0)
64 for (const char *flg
= nextToken
+1; *flg
!='\0'; flg
++ ) {
65 if (supported(*flg
)) {
68 debugs(28, 0, HERE
<< "Flag '" << *flg
<< "' not supported");
74 /*Regex code needs to parse -i file*/
75 if ( isSet(ACL_F_REGEX_CASE
))
76 ConfigParser::strtokFilePutBack("-i");
78 if (nextToken
!= NULL
&& strcmp(nextToken
, "--") != 0 )
79 ConfigParser::strtokFileUndo();
83 ACLFlags::flagsStr() const
91 for (ACLFlag f
= 'A'; f
<= 'z'; f
++) {
92 // ACL_F_REGEX_CASE (-i) flag handled by ACLRegexData class, ignore
93 if (isSet(f
) && f
!= ACL_F_REGEX_CASE
)
101 ACL::operator new (size_t byteCount
)
103 fatal ("unusable ACL::new");
108 ACL::operator delete (void *address
)
110 fatal ("unusable ACL::delete");
114 ACL::FindByName(const char *name
)
117 debugs(28, 9, "ACL::FindByName '" << name
<< "'");
119 for (a
= Config
.aclList
; a
; a
= a
->next
)
120 if (!strcasecmp(a
->name
, name
))
123 debugs(28, 9, "ACL::FindByName found no match");
129 ACL::Factory (char const *type
)
131 ACL
*result
= Prototype::Factory (type
);
134 fatal ("Unknown acl type in ACL::Factory");
147 bool ACL::valid () const
153 ACL::matches(ACLChecklist
*checklist
) const
155 PROF_start(ACL_matches
);
156 debugs(28, 5, "checking " << name
);
158 // XXX: AclMatchedName does not contain a matched ACL name when the acl
159 // does not match. It contains the last (usually leaf) ACL name checked
160 // (or is NULL if no ACLs were checked).
161 AclMatchedName
= name
;
164 if (!checklist
->hasRequest() && requiresRequest()) {
165 debugs(28, DBG_IMPORTANT
, "WARNING: " << name
<< " ACL is used in " <<
166 "context without an HTTP request. Assuming mismatch.");
167 } else if (!checklist
->hasReply() && requiresReply()) {
168 debugs(28, DBG_IMPORTANT
, "WARNING: " << name
<< " ACL is used in " <<
169 "context without an HTTP response. Assuming mismatch.");
171 // have to cast because old match() API is missing const
172 result
= const_cast<ACL
*>(this)->match(checklist
);
175 const char *extra
= checklist
->asyncInProgress() ? " async" : "";
176 debugs(28, 3, "checked: " << name
<< " = " << result
<< extra
);
177 PROF_stop(ACL_matches
);
178 return result
== 1; // true for match; false for everything else
182 ACL::context(const char *aName
, const char *aCfgLine
)
186 xstrncpy(name
, aName
, ACL_NAME_SZ
-1);
189 cfgline
= xstrdup(aCfgLine
);
193 ACL::ParseAclLine(ConfigParser
&parser
, ACL
** head
)
195 /* we're already using strtok() to grok the line */
198 LOCAL_ARRAY(char, aclname
, ACL_NAME_SZ
);
201 /* snarf the ACL name */
203 if ((t
= strtok(NULL
, w_space
)) == NULL
) {
204 debugs(28, DBG_CRITICAL
, "aclParseAclLine: missing ACL name.");
209 if (strlen(t
) >= ACL_NAME_SZ
) {
210 debugs(28, DBG_CRITICAL
, "aclParseAclLine: aclParseAclLine: ACL name '" << t
<<
211 "' too long, max " << ACL_NAME_SZ
- 1 << " characters supported");
216 xstrncpy(aclname
, t
, ACL_NAME_SZ
);
217 /* snarf the ACL type */
220 if ((theType
= strtok(NULL
, w_space
)) == NULL
) {
221 debugs(28, DBG_CRITICAL
, "aclParseAclLine: missing ACL type.");
226 // Is this ACL going to work?
227 if (strcmp(theType
, "myip") == 0) {
228 AnyP::PortCfg
*p
= Config
.Sockaddr
.http
;
230 // Bug 3239: not reliable when there is interception traffic coming
231 if (p
->flags
.natIntercept
)
232 debugs(28, DBG_CRITICAL
, "WARNING: 'myip' ACL is not reliable for interception proxies. Please use 'myportname' instead.");
235 debugs(28, DBG_IMPORTANT
, "UPGRADE: ACL 'myip' type is has been renamed to 'localip' and matches the IP the client connected to.");
237 } else if (strcmp(theType
, "myport") == 0) {
238 AnyP::PortCfg
*p
= Config
.Sockaddr
.http
;
240 // Bug 3239: not reliable when there is interception traffic coming
241 // Bug 3239: myport - not reliable (yet) when there is interception traffic coming
242 if (p
->flags
.natIntercept
)
243 debugs(28, DBG_CRITICAL
, "WARNING: 'myport' ACL is not reliable for interception proxies. Please use 'myportname' instead.");
246 theType
= "localport";
247 debugs(28, DBG_IMPORTANT
, "UPGRADE: ACL 'myport' type is has been renamed to 'localport' and matches the port the client connected to.");
250 if (!Prototype::Registered(theType
)) {
251 debugs(28, DBG_CRITICAL
, "FATAL: Invalid ACL type '" << theType
<< "'");
252 // XXX: make this an ERROR and skip the ACL creation. We *may* die later when its use is attempted. Or may not.
257 if ((A
= FindByName(aclname
)) == NULL
) {
258 debugs(28, 3, "aclParseAclLine: Creating ACL '" << aclname
<< "'");
259 A
= ACL::Factory(theType
);
260 A
->context(aclname
, config_input_line
);
263 if (strcmp (A
->typeString(),theType
) ) {
264 debugs(28, DBG_CRITICAL
, "aclParseAclLine: ACL '" << A
->name
<< "' already exists with different type.");
269 debugs(28, 3, "aclParseAclLine: Appending to '" << aclname
<< "'");
274 * Here we set AclMatchedName in case we need to use it in a
275 * warning message in aclDomainCompare().
277 AclMatchedName
= A
->name
; /* ugly */
279 A
->flags
.parseFlags();
281 /*split the function here */
285 * Clear AclMatchedName from our temporary hack
287 AclMatchedName
= NULL
; /* ugly */
293 debugs(28, DBG_CRITICAL
, "Warning: empty ACL: " << A
->cfgline
);
297 fatalf("ERROR: Invalid ACL: %s\n",
302 assert(head
&& *head
== Config
.aclList
);
303 A
->registered
= true;
306 head
= &(*head
)->next
;
312 ACL::isProxyAuth() const
317 /* ACL result caching routines */
320 ACL::matchForCache(ACLChecklist
*checklist
)
322 /* This is a fatal to ensure that cacheMatchAcl calls are _only_
323 * made for supported acl types */
324 fatal("aclCacheMatchAcl: unknown or unexpected ACL type");
325 return 0; /* NOTREACHED */
329 * we lookup an acl's cached results, and if we cannot find the acl being
330 * checked we check it and cache the result. This function is a template
331 * method to support caching of multiple acl types.
332 * Note that caching of time based acl's is not
333 * wise in long lived caches (i.e. the auth_user proxy match cache)
335 * TODO: does a dlink_list perform well enough? Kinkie
338 ACL::cacheMatchAcl(dlink_list
* cache
, ACLChecklist
*checklist
)
340 acl_proxy_auth_match_cache
*auth_match
;
345 auth_match
= (acl_proxy_auth_match_cache
*)link
->data
;
347 if (auth_match
->acl_data
== this) {
348 debugs(28, 4, "ACL::cacheMatchAcl: cache hit on acl '" << name
<< "' (" << this << ")");
349 return auth_match
->matchrv
;
355 auth_match
= new acl_proxy_auth_match_cache();
356 auth_match
->matchrv
= matchForCache (checklist
);
357 auth_match
->acl_data
= this;
358 dlinkAddTail(auth_match
, &auth_match
->link
, cache
);
359 debugs(28, 4, "ACL::cacheMatchAcl: miss for '" << name
<< "'. Adding result " << auth_match
->matchrv
);
360 return auth_match
->matchrv
;
364 aclCacheMatchFlush(dlink_list
* cache
)
366 acl_proxy_auth_match_cache
*auth_match
;
367 dlink_node
*link
, *tmplink
;
370 debugs(28, 8, "aclCacheMatchFlush called for cache " << cache
);
373 auth_match
= (acl_proxy_auth_match_cache
*)link
->data
;
376 dlinkDelete(tmplink
, cache
);
382 ACL::requiresReply() const
388 ACL::requiresRequest() const
393 /*********************/
394 /* Destroy functions */
395 /*********************/
399 debugs(28, 3, "ACL::~ACL: '" << cfgline
<< "'");
401 AclMatchedName
= NULL
; // in case it was pointing to our name
405 ACL::Prototype::Prototype() : prototype (NULL
), typeString (NULL
) {}
407 ACL::Prototype::Prototype (ACL
const *aPrototype
, char const *aType
) : prototype (aPrototype
), typeString (aType
)
412 Vector
<ACL::Prototype
const *> * ACL::Prototype::Registry
;
413 void *ACL::Prototype::Initialized
;
416 ACL::Prototype::Registered(char const *aType
)
418 debugs(28, 7, "ACL::Prototype::Registered: invoked for type " << aType
);
420 for (iterator i
= Registry
->begin(); i
!= Registry
->end(); ++i
)
421 if (!strcmp (aType
, (*i
)->typeString
)) {
422 debugs(28, 7, "ACL::Prototype::Registered: yes");
426 debugs(28, 7, "ACL::Prototype::Registered: no");
431 ACL::Prototype::registerMe ()
433 if (!Registry
|| (Initialized
!= ((char *)Registry
- 5)) ) {
434 /* TODO: extract this */
435 /* Not initialised */
436 Registry
= new Vector
<ACL::Prototype
const *>;
437 Initialized
= (char *)Registry
- 5;
440 if (Registered (typeString
))
441 fatalf ("Attempt to register %s twice", typeString
);
443 Registry
->push_back (this);
446 ACL::Prototype::~Prototype()
448 // TODO: unregister me
452 ACL::Prototype::Factory (char const *typeToClone
)
454 debugs(28, 4, "ACL::Prototype::Factory: cloning an object for type '" << typeToClone
<< "'");
456 for (iterator i
= Registry
->begin(); i
!= Registry
->end(); ++i
)
457 if (!strcmp (typeToClone
, (*i
)->typeString
)) {
458 ACL
*A
= (*i
)->prototype
->clone();
459 A
->flags
= (*i
)->prototype
->flags
;
463 debugs(28, 4, "ACL::Prototype::Factory: cloning failed, no type '" << typeToClone
<< "' available");
471 ACL
*a
= Config
.aclList
;
472 debugs(53, 3, "ACL::Initialize");