4 * DEBUG: section 28 Access Control
5 * AUTHOR: Duane Wessels
7 * SQUID Web Proxy Cache http://www.squid-cache.org/
8 * ----------------------------------------------------------
10 * Squid is the result of efforts by numerous individuals from
11 * the Internet community; see the CONTRIBUTORS file for full
12 * details. Many organizations have provided support for Squid's
13 * development; see the SPONSORS file for full details. Squid is
14 * Copyrighted (C) 2001 by the Regents of the University of
15 * California; see the COPYRIGHT file for full details. Squid
16 * incorporates software developed and/or copyrighted by other
17 * sources; see the CREDITS file for full details.
19 * This program is free software; you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation; either version 2 of the License, or
22 * (at your option) any later version.
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
29 * You should have received a copy of the GNU General Public License
30 * along with this program; if not, write to the Free Software
31 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
36 #include "acl/Checklist.h"
37 #include "ConfigParser.h"
40 #include "anyp/PortCfg.h"
42 const char *AclMatchedName
= NULL
;
45 ACL::operator new (size_t byteCount
)
47 fatal ("unusable ACL::new");
52 ACL::operator delete (void *address
)
54 fatal ("unusable ACL::delete");
58 ACL::FindByName(const char *name
)
61 debugs(28, 9, "ACL::FindByName '" << name
<< "'");
63 for (a
= Config
.aclList
; a
; a
= a
->next
)
64 if (!strcasecmp(a
->name
, name
))
67 debugs(28, 9, "ACL::FindByName found no match");
73 ACL::Factory (char const *type
)
75 ACL
*result
= Prototype::Factory (type
);
78 fatal ("Unknown acl type in ACL::Factory");
83 ACL::ACL () :cfgline(NULL
) {}
85 bool ACL::valid () const
91 ACL::ParseAclLine(ConfigParser
&parser
, ACL
** head
)
93 /* we're already using strtok() to grok the line */
96 LOCAL_ARRAY(char, aclname
, ACL_NAME_SZ
);
99 /* snarf the ACL name */
101 if ((t
= strtok(NULL
, w_space
)) == NULL
) {
102 debugs(28, 0, "aclParseAclLine: missing ACL name.");
107 if (strlen(t
) >= ACL_NAME_SZ
) {
108 debugs(28, 0, "aclParseAclLine: aclParseAclLine: ACL name '" << t
<<
109 "' too long, max " << ACL_NAME_SZ
- 1 << " characters supported");
114 xstrncpy(aclname
, t
, ACL_NAME_SZ
);
115 /* snarf the ACL type */
118 if ((theType
= strtok(NULL
, w_space
)) == NULL
) {
119 debugs(28, 0, "aclParseAclLine: missing ACL type.");
124 // Is this ACL going to work?
125 if (strcmp(theType
, "myip") == 0) {
126 AnyP::PortCfg
*p
= Config
.Sockaddr
.http
;
128 // Bug 3239: not reliable when there is interception traffic coming
130 debugs(28, DBG_CRITICAL
, "WARNING: 'myip' ACL is not reliable for interception proxies. Please use 'myportname' instead.");
133 debugs(28, DBG_IMPORTANT
, "UPGRADE: ACL 'myip' type is has been renamed to 'localip' and matches the IP the client connected to.");
135 } else if (strcmp(theType
, "myport") == 0) {
136 AnyP::PortCfg
*p
= Config
.Sockaddr
.http
;
138 // Bug 3239: not reliable when there is interception traffic coming
139 // Bug 3239: myport - not reliable (yet) when there is interception traffic coming
141 debugs(28, DBG_CRITICAL
, "WARNING: 'myport' ACL is not reliable for interception proxies. Please use 'myportname' instead.");
144 theType
= "localport";
145 debugs(28, DBG_IMPORTANT
, "UPGRADE: ACL 'myport' type is has been renamed to 'localport' and matches the port the client connected to.");
148 if (!Prototype::Registered(theType
)) {
149 debugs(28, DBG_CRITICAL
, "FATAL: Invalid ACL type '" << theType
<< "'");
150 // XXX: make this an ERROR and skip the ACL creation. We *may* die later when its use is attempted. Or may not.
155 if ((A
= FindByName(aclname
)) == NULL
) {
156 debugs(28, 3, "aclParseAclLine: Creating ACL '" << aclname
<< "'");
157 A
= ACL::Factory(theType
);
158 xstrncpy(A
->name
, aclname
, ACL_NAME_SZ
);
159 A
->cfgline
= xstrdup(config_input_line
);
162 if (strcmp (A
->typeString(),theType
) ) {
163 debugs(28, 0, "aclParseAclLine: ACL '" << A
->name
<< "' already exists with different type.");
168 debugs(28, 3, "aclParseAclLine: Appending to '" << aclname
<< "'");
173 * Here we set AclMatchedName in case we need to use it in a
174 * warning message in aclDomainCompare().
176 AclMatchedName
= A
->name
; /* ugly */
178 /*split the function here */
182 * Clear AclMatchedName from our temporary hack
184 AclMatchedName
= NULL
; /* ugly */
190 debugs(28, 0, "Warning: empty ACL: " << A
->cfgline
);
194 fatalf("ERROR: Invalid ACL: %s\n",
200 head
= &(*head
)->next
;
206 ACL::isProxyAuth() const
212 ACLList::ACLList() : op (1), _acl (NULL
), next (NULL
)
216 ACLList::negated(bool isNegated
)
224 /* ACL result caching routines */
227 ACL::matchForCache(ACLChecklist
*checklist
)
229 /* This is a fatal to ensure that cacheMatchAcl calls are _only_
230 * made for supported acl types */
231 fatal("aclCacheMatchAcl: unknown or unexpected ACL type");
232 return 0; /* NOTREACHED */
236 * we lookup an acl's cached results, and if we cannot find the acl being
237 * checked we check it and cache the result. This function is a template
238 * method to support caching of multiple acl types.
239 * Note that caching of time based acl's is not
240 * wise in long lived caches (i.e. the auth_user proxy match cache)
242 * TODO: does a dlink_list perform well enough? Kinkie
245 ACL::cacheMatchAcl(dlink_list
* cache
, ACLChecklist
*checklist
)
247 acl_proxy_auth_match_cache
*auth_match
;
252 auth_match
= (acl_proxy_auth_match_cache
*)link
->data
;
254 if (auth_match
->acl_data
== this) {
255 debugs(28, 4, "ACL::cacheMatchAcl: cache hit on acl '" << name
<< "' (" << this << ")");
256 return auth_match
->matchrv
;
262 auth_match
= new acl_proxy_auth_match_cache();
263 auth_match
->matchrv
= matchForCache (checklist
);
264 auth_match
->acl_data
= this;
265 dlinkAddTail(auth_match
, &auth_match
->link
, cache
);
266 debugs(28, 4, "ACL::cacheMatchAcl: miss for '" << name
<< "'. Adding result " << auth_match
->matchrv
);
267 return auth_match
->matchrv
;
271 aclCacheMatchFlush(dlink_list
* cache
)
273 acl_proxy_auth_match_cache
*auth_match
;
274 dlink_node
*link
, *tmplink
;
277 debugs(28, 8, "aclCacheMatchFlush called for cache " << cache
);
280 auth_match
= (acl_proxy_auth_match_cache
*)link
->data
;
283 dlinkDelete(tmplink
, cache
);
289 ACL::requiresReply() const
295 ACL::requiresRequest() const
301 ACL::checklistMatches(ACLChecklist
*checklist
)
305 if (!checklist
->hasRequest() && requiresRequest()) {
306 debugs(28, 1, "ACL::checklistMatches WARNING: '" << name
<< "' ACL is used but there is no HTTP request -- not matching.");
310 if (!checklist
->hasReply() && requiresReply()) {
311 debugs(28, 1, "ACL::checklistMatches WARNING: '" << name
<< "' ACL is used but there is no HTTP reply -- not matching.");
315 debugs(28, 3, "ACL::checklistMatches: checking '" << name
<< "'");
316 rv
= match(checklist
);
317 debugs(28, 3, "ACL::ChecklistMatches: result for '" << name
<< "' is " << rv
);
322 ACLList::matches (ACLChecklist
*checklist
) const
325 // XXX: AclMatchedName does not contain a matched ACL name when the acl
326 // does not match (or contains stale name if no ACLs are checked). In
327 // either case, we get misleading debugging and possibly incorrect error
328 // messages. Unfortunately, deny_info's "when none http_access
329 // lines match" exception essentially requires this mess.
330 // TODO: Rework by using an acl-free deny_info for the no-match cases?
331 AclMatchedName
= _acl
->name
;
332 debugs(28, 3, "ACLList::matches: checking " << (op
? null_string
: "!") << _acl
->name
);
334 if (_acl
->checklistMatches(checklist
) != op
) {
335 debugs(28, 4, "ACLList::matches: result is false");
339 debugs(28, 4, "ACLList::matches: result is true");
344 /*********************/
345 /* Destroy functions */
346 /*********************/
350 debugs(28, 3, "ACL::~ACL: '" << cfgline
<< "'");
354 /* to be split into separate files in the future */
356 CBDATA_CLASS_INIT(acl_access
);
359 acl_access::operator new (size_t)
361 CBDATA_INIT_TYPE(acl_access
);
362 acl_access
*result
= cbdataAlloc(acl_access
);
367 acl_access::operator delete (void *address
)
369 acl_access
*t
= static_cast<acl_access
*>(address
);
373 ACL::Prototype::Prototype() : prototype (NULL
), typeString (NULL
) {}
375 ACL::Prototype::Prototype (ACL
const *aPrototype
, char const *aType
) : prototype (aPrototype
), typeString (aType
)
380 Vector
<ACL::Prototype
const *> * ACL::Prototype::Registry
;
381 void *ACL::Prototype::Initialized
;
384 ACL::Prototype::Registered(char const *aType
)
386 debugs(28, 7, "ACL::Prototype::Registered: invoked for type " << aType
);
388 for (iterator i
= Registry
->begin(); i
!= Registry
->end(); ++i
)
389 if (!strcmp (aType
, (*i
)->typeString
)) {
390 debugs(28, 7, "ACL::Prototype::Registered: yes");
394 debugs(28, 7, "ACL::Prototype::Registered: no");
399 ACL::Prototype::registerMe ()
401 if (!Registry
|| (Initialized
!= ((char *)Registry
- 5)) ) {
402 /* TODO: extract this */
403 /* Not initialised */
404 Registry
= new Vector
<ACL::Prototype
const *>;
405 Initialized
= (char *)Registry
- 5;
408 if (Registered (typeString
))
409 fatalf ("Attempt to register %s twice", typeString
);
411 Registry
->push_back (this);
414 ACL::Prototype::~Prototype()
416 // TODO: unregister me
420 ACL::Prototype::Factory (char const *typeToClone
)
422 debugs(28, 4, "ACL::Prototype::Factory: cloning an object for type '" << typeToClone
<< "'");
424 for (iterator i
= Registry
->begin(); i
!= Registry
->end(); ++i
)
425 if (!strcmp (typeToClone
, (*i
)->typeString
))
426 return (*i
)->prototype
->clone();
428 debugs(28, 4, "ACL::Prototype::Factory: cloning failed, no type '" << typeToClone
<< "' available");
436 ACL
*a
= Config
.aclList
;
437 debugs(53, 3, "ACL::Initialize");