2 * Copyright (C) 1996-2017 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 82 External ACL */
13 #include "acl/FilledChecklist.h"
15 #include "client_side.h"
16 #include "client_side_request.h"
17 #include "comm/Connection.h"
18 #include "ConfigParser.h"
19 #include "ExternalACL.h"
20 #include "ExternalACLEntry.h"
22 #include "format/Token.h"
24 #include "helper/Reply.h"
25 #include "http/Stream.h"
26 #include "HttpHeaderTools.h"
27 #include "HttpReply.h"
28 #include "HttpRequest.h"
31 #include "mgr/Registration.h"
33 #include "SquidConfig.h"
34 #include "SquidString.h"
35 #include "SquidTime.h"
41 #include "ssl/ServerBump.h"
42 #include "ssl/support.h"
46 #include "auth/Gadgets.h"
47 #include "auth/UserRequest.h"
50 #include "ident/AclIdent.h"
53 #ifndef DEFAULT_EXTERNAL_ACL_TTL
54 #define DEFAULT_EXTERNAL_ACL_TTL 1 * 60 * 60
56 #ifndef DEFAULT_EXTERNAL_ACL_CHILDREN
57 #define DEFAULT_EXTERNAL_ACL_CHILDREN 5
60 static char *makeExternalAclKey(ACLFilledChecklist
* ch
, external_acl_data
* acl_data
);
61 static void external_acl_cache_delete(external_acl
* def
, const ExternalACLEntryPointer
&entry
);
62 static int external_acl_entry_expired(external_acl
* def
, const ExternalACLEntryPointer
&entry
);
63 static int external_acl_grace_expired(external_acl
* def
, const ExternalACLEntryPointer
&entry
);
64 static void external_acl_cache_touch(external_acl
* def
, const ExternalACLEntryPointer
&entry
);
65 static ExternalACLEntryPointer
external_acl_cache_add(external_acl
* def
, const char *key
, ExternalACLEntryData
const &data
);
67 /******************************************************************
68 * external_acl directive
73 /* FIXME: These are not really cbdata, but it is an easy way
74 * to get them pooled, refcounted, accounted and freed properly...
76 CBDATA_CLASS(external_acl
);
84 void add(const ExternalACLEntryPointer
&);
88 bool maybeCacheable(const allow_t
&) const;
98 Format::Format format
;
102 Helper::ChildConfig children
;
118 * Configuration flag. May only be altered by the configuration parser.
120 * Indicates that all uses of this external_acl_type helper require authentication
121 * details to be processed. If none are available its a fail match.
126 Format::Quoting quote
; // default quoting to use, set by protocol= parameter
128 Ip::Address local_addr
;
131 CBDATA_CLASS_INIT(external_acl
);
133 external_acl::external_acl() :
135 ttl(DEFAULT_EXTERNAL_ACL_TTL
),
139 format("external_acl_type"),
141 children(DEFAULT_EXTERNAL_ACL_CHILDREN
),
144 cache_size(256*1024),
149 quote(Format::LOG_QUOTE_URL
)
151 local_addr
.setLocalhost();
154 external_acl::~external_acl()
157 wordlistDestroy(&cmdline
);
160 helperShutdown(theHelper
);
165 while (lru_list
.tail
) {
166 ExternalACLEntryPointer
e(static_cast<ExternalACLEntry
*>(lru_list
.tail
->data
));
167 external_acl_cache_delete(this, e
);
170 hashFreeMemory(cache
);
173 external_acl
*node
= next
;
175 node
->next
= NULL
; // prevent recursion
181 parse_externalAclHelper(external_acl
** list
)
183 char *token
= ConfigParser::NextToken();
190 external_acl
*a
= new external_acl
;
191 a
->name
= xstrdup(token
);
193 // Allow supported %macros inside quoted tokens
194 ConfigParser::EnableMacros();
195 token
= ConfigParser::NextToken();
199 if (strncmp(token
, "ttl=", 4) == 0) {
200 a
->ttl
= atoi(token
+ 4);
201 } else if (strncmp(token
, "negative_ttl=", 13) == 0) {
202 a
->negative_ttl
= atoi(token
+ 13);
203 } else if (strncmp(token
, "children=", 9) == 0) {
204 a
->children
.n_max
= atoi(token
+ 9);
205 debugs(0, DBG_CRITICAL
, "WARNING: external_acl_type option children=N has been deprecated in favor of children-max=N and children-startup=N");
206 } else if (strncmp(token
, "children-max=", 13) == 0) {
207 a
->children
.n_max
= atoi(token
+ 13);
208 } else if (strncmp(token
, "children-startup=", 17) == 0) {
209 a
->children
.n_startup
= atoi(token
+ 17);
210 } else if (strncmp(token
, "children-idle=", 14) == 0) {
211 a
->children
.n_idle
= atoi(token
+ 14);
212 } else if (strncmp(token
, "concurrency=", 12) == 0) {
213 a
->children
.concurrency
= atoi(token
+ 12);
214 } else if (strncmp(token
, "queue-size=", 11) == 0) {
215 a
->children
.queue_size
= atoi(token
+ 11);
216 a
->children
.defaultQueueSize
= false;
217 } else if (strncmp(token
, "cache=", 6) == 0) {
218 a
->cache_size
= atoi(token
+ 6);
219 } else if (strncmp(token
, "grace=", 6) == 0) {
220 a
->grace
= atoi(token
+ 6);
221 } else if (strcmp(token
, "protocol=2.5") == 0) {
222 a
->quote
= Format::LOG_QUOTE_SHELL
;
223 } else if (strcmp(token
, "protocol=3.0") == 0) {
224 debugs(3, DBG_PARSE_NOTE(2), "WARNING: external_acl_type option protocol=3.0 is deprecated. Remove this from your config.");
225 a
->quote
= Format::LOG_QUOTE_URL
;
226 } else if (strcmp(token
, "quote=url") == 0) {
227 debugs(3, DBG_PARSE_NOTE(2), "WARNING: external_acl_type option quote=url is deprecated. Remove this from your config.");
228 a
->quote
= Format::LOG_QUOTE_URL
;
229 } else if (strcmp(token
, "quote=shell") == 0) {
230 debugs(3, DBG_PARSE_NOTE(2), "WARNING: external_acl_type option quote=shell is deprecated. Use protocol=2.5 if still needed.");
231 a
->quote
= Format::LOG_QUOTE_SHELL
;
233 /* INET6: allow admin to configure some helpers explicitly to
234 bind to IPv4/v6 localhost port. */
235 } else if (strcmp(token
, "ipv4") == 0) {
236 if ( !a
->local_addr
.setIPv4() ) {
237 debugs(3, DBG_CRITICAL
, "WARNING: Error converting " << a
->local_addr
<< " to IPv4 in " << a
->name
);
239 } else if (strcmp(token
, "ipv6") == 0) {
241 debugs(3, DBG_CRITICAL
, "WARNING: --enable-ipv6 required for external ACL helpers to use IPv6: " << a
->name
);
242 // else nothing to do.
247 token
= ConfigParser::NextToken();
249 ConfigParser::DisableMacros();
251 /* check that child startup value is sane. */
252 if (a
->children
.n_startup
> a
->children
.n_max
)
253 a
->children
.n_startup
= a
->children
.n_max
;
255 /* check that child idle value is sane. */
256 if (a
->children
.n_idle
> a
->children
.n_max
)
257 a
->children
.n_idle
= a
->children
.n_max
;
258 if (a
->children
.n_idle
< 1)
259 a
->children
.n_idle
= 1;
261 if (a
->negative_ttl
== -1)
262 a
->negative_ttl
= a
->ttl
;
264 if (a
->children
.defaultQueueSize
)
265 a
->children
.queue_size
= 2 * a
->children
.n_max
;
267 /* Legacy external_acl_type format parser.
268 * Handles a series of %... tokens where any non-% means
269 * the start of another parameter field (ie the path to binary).
271 enum Format::Quoting quote
= Format::LOG_QUOTE_NONE
;
272 Format::Token
**fmt
= &a
->format
.format
;
273 bool data_used
= false;
275 /* stop on first non-% token found */
279 *fmt
= new Format::Token
;
280 // these tokens are whitespace delimited
281 (*fmt
)->space
= true;
283 // set the default encoding to match the protocol= config
284 // this will be overridden by explicit %macro attributes
285 (*fmt
)->quote
= a
->quote
;
287 // compatibility for old tokens incompatible with Format::Token syntax
288 #if USE_OPENSSL // dont bother if we dont have to.
289 if (strncmp(token
, "%USER_CERT_", 11) == 0) {
290 (*fmt
)->type
= Format::LFT_EXT_ACL_USER_CERT
;
291 (*fmt
)->data
.string
= xstrdup(token
+ 11);
292 (*fmt
)->data
.header
.header
= (*fmt
)->data
.string
;
293 } else if (strncmp(token
, "%USER_CA_CERT_", 14) == 0) {
294 (*fmt
)->type
= Format::LFT_EXT_ACL_USER_CA_CERT
;
295 (*fmt
)->data
.string
= xstrdup(token
+ 14);
296 (*fmt
)->data
.header
.header
= (*fmt
)->data
.string
;
297 } else if (strncmp(token
, "%CA_CERT_", 9) == 0) {
298 debugs(82, DBG_PARSE_NOTE(DBG_IMPORTANT
), "WARNING: external_acl_type %CA_CERT_* code is obsolete. Use %USER_CA_CERT_* instead");
299 (*fmt
)->type
= Format::LFT_EXT_ACL_USER_CA_CERT
;
300 (*fmt
)->data
.string
= xstrdup(token
+ 9);
301 (*fmt
)->data
.header
.header
= (*fmt
)->data
.string
;
304 if (strncmp(token
,"%<{", 3) == 0) {
307 debugs(82, DBG_PARSE_NOTE(DBG_IMPORTANT
), "WARNING: external_acl_type format %<{...} is deprecated. Use " << tmp
);
308 const size_t parsedLen
= (*fmt
)->parse(tmp
.c_str(), "e
);
309 assert(parsedLen
== tmp
.length());
310 assert((*fmt
)->type
== Format::LFT_REPLY_HEADER
||
311 (*fmt
)->type
== Format::LFT_REPLY_HEADER_ELEM
);
313 } else if (strncmp(token
,"%>{", 3) == 0) {
316 debugs(82, DBG_PARSE_NOTE(DBG_IMPORTANT
), "WARNING: external_acl_type format %>{...} is deprecated. Use " << tmp
);
317 const size_t parsedLen
= (*fmt
)->parse(tmp
.c_str(), "e
);
318 assert(parsedLen
== tmp
.length());
319 assert((*fmt
)->type
== Format::LFT_ADAPTED_REQUEST_HEADER
||
320 (*fmt
)->type
== Format::LFT_ADAPTED_REQUEST_HEADER_ELEM
);
323 // we can use the Format::Token::parse() method since it
324 // only pulls off one token. Since we already checked
325 // for '%' prefix above this is guaranteed to be a token.
326 const size_t len
= (*fmt
)->parse(token
, "e
);
327 assert(len
== strlen(token
));
330 // process special token-specific actions (only if necessary)
332 if ((*fmt
)->type
== Format::LFT_USER_LOGIN
)
333 a
->require_auth
= true;
336 if ((*fmt
)->type
== Format::LFT_EXT_ACL_DATA
)
339 fmt
= &((*fmt
)->next
);
340 token
= ConfigParser::NextToken();
343 /* There must be at least one format token */
344 if (!a
->format
.format
) {
350 // format has implicit %DATA on the end if not used explicitly
352 *fmt
= new Format::Token
;
353 (*fmt
)->type
= Format::LFT_EXT_ACL_DATA
;
354 (*fmt
)->quote
= Format::LOG_QUOTE_NONE
;
364 wordlistAdd(&a
->cmdline
, token
);
367 parse_wordlist(&a
->cmdline
);
370 list
= &(*list
)->next
;
376 dump_externalAclHelper(StoreEntry
* sentry
, const char *name
, const external_acl
* list
)
378 const external_acl
*node
;
379 const wordlist
*word
;
381 for (node
= list
; node
; node
= node
->next
) {
382 storeAppendPrintf(sentry
, "%s %s", name
, node
->name
);
384 if (!node
->local_addr
.isIPv6())
385 storeAppendPrintf(sentry
, " ipv4");
387 storeAppendPrintf(sentry
, " ipv6");
389 if (node
->ttl
!= DEFAULT_EXTERNAL_ACL_TTL
)
390 storeAppendPrintf(sentry
, " ttl=%d", node
->ttl
);
392 if (node
->negative_ttl
!= node
->ttl
)
393 storeAppendPrintf(sentry
, " negative_ttl=%d", node
->negative_ttl
);
396 storeAppendPrintf(sentry
, " grace=%d", node
->grace
);
398 if (node
->children
.n_max
!= DEFAULT_EXTERNAL_ACL_CHILDREN
)
399 storeAppendPrintf(sentry
, " children-max=%d", node
->children
.n_max
);
401 if (node
->children
.n_startup
!= 0) // sync with helper/ChildConfig.cc default
402 storeAppendPrintf(sentry
, " children-startup=%d", node
->children
.n_startup
);
404 if (node
->children
.n_idle
!= 1) // sync with helper/ChildConfig.cc default
405 storeAppendPrintf(sentry
, " children-idle=%d", node
->children
.n_idle
);
407 if (node
->children
.concurrency
!= 0)
408 storeAppendPrintf(sentry
, " concurrency=%d", node
->children
.concurrency
);
411 storeAppendPrintf(sentry
, " cache=%d", node
->cache_size
);
413 if (node
->quote
== Format::LOG_QUOTE_SHELL
)
414 storeAppendPrintf(sentry
, " protocol=2.5");
416 node
->format
.dump(sentry
, NULL
, false);
418 for (word
= node
->cmdline
; word
; word
= word
->next
)
419 storeAppendPrintf(sentry
, " %s", word
->key
);
421 storeAppendPrintf(sentry
, "\n");
426 free_externalAclHelper(external_acl
** list
)
432 static external_acl
*
433 find_externalAclHelper(const char *name
)
437 for (node
= Config
.externalAclHelperList
; node
; node
= node
->next
) {
438 if (strcmp(node
->name
, name
) == 0)
446 external_acl::add(const ExternalACLEntryPointer
&anEntry
)
449 assert(anEntry
!= NULL
);
450 assert (anEntry
->def
== NULL
);
452 ExternalACLEntry
*e
= const_cast<ExternalACLEntry
*>(anEntry
.getRaw()); // XXX: make hash a std::map of Pointer.
454 dlinkAdd(e
, &e
->lru
, &lru_list
);
455 e
->lock(); //cbdataReference(e); // lock it on behalf of the hash
460 external_acl::trimCache()
462 if (cache_size
&& cache_entries
>= cache_size
) {
463 ExternalACLEntryPointer
e(static_cast<ExternalACLEntry
*>(lru_list
.tail
->data
));
464 external_acl_cache_delete(this, e
);
469 external_acl::maybeCacheable(const allow_t
&result
) const
472 return false; // cache is disabled
474 if (result
== ACCESS_DUNNO
)
475 return false; // non-cacheable response
477 if ((result
.allowed() ? ttl
: negative_ttl
) <= 0)
478 return false; // not caching this type of response
483 /******************************************************************
487 class external_acl_data
489 CBDATA_CLASS(external_acl_data
);
492 explicit external_acl_data(external_acl
*aDef
) : def(cbdataReference(aDef
)), name(NULL
), arguments(NULL
) {}
493 ~external_acl_data();
500 CBDATA_CLASS_INIT(external_acl_data
);
502 external_acl_data::~external_acl_data()
505 wordlistDestroy(&arguments
);
506 cbdataReferenceDone(def
);
517 char *token
= ConfigParser::strtokFile();
524 data
= new external_acl_data(find_externalAclHelper(token
));
532 // def->name is the name of the external_acl_type.
533 // this is the name of the 'acl' directive being tested
534 data
->name
= xstrdup(AclMatchedName
);
536 while ((token
= ConfigParser::strtokFile())) {
537 wordlistAdd(&data
->arguments
, token
);
542 ACLExternal::valid () const
545 if (data
->def
->require_auth
) {
546 if (authenticateSchemeCount() == 0) {
547 debugs(28, DBG_CRITICAL
, "Can't use proxy auth because no authentication schemes were compiled.");
551 if (authenticateActiveSchemeCount() == 0) {
552 debugs(28, DBG_CRITICAL
, "Can't use proxy auth because no authentication schemes are fully configured.");
562 ACLExternal::empty () const
567 ACLExternal::~ACLExternal()
574 copyResultsFromEntry(HttpRequest
*req
, const ExternalACLEntryPointer
&entry
)
578 if (entry
->user
.size())
579 req
->extacl_user
= entry
->user
;
581 if (entry
->password
.size())
582 req
->extacl_passwd
= entry
->password
;
584 if (!req
->tag
.size())
585 req
->tag
= entry
->tag
;
587 if (entry
->log
.size())
588 req
->extacl_log
= entry
->log
;
590 if (entry
->message
.size())
591 req
->extacl_message
= entry
->message
;
593 // attach the helper kv-pair to the transaction
594 UpdateRequestNotes(req
->clientConnectionManager
.get(), *req
, entry
->notes
);
599 aclMatchExternal(external_acl_data
*acl
, ACLFilledChecklist
*ch
)
601 debugs(82, 9, HERE
<< "acl=\"" << acl
->def
->name
<< "\"");
602 ExternalACLEntryPointer entry
= ch
->extacl_entry
;
604 external_acl_message
= "MISSING REQUIRED INFORMATION";
607 if (entry
->def
== acl
->def
) {
608 /* Ours, use it.. if the key matches */
609 const char *key
= makeExternalAclKey(ch
, acl
);
611 return ACCESS_DUNNO
; // insufficent data to continue
612 if (strcmp(key
, (char*)entry
->key
) != 0) {
613 debugs(82, 9, "entry key='" << (char *)entry
->key
<< "', our key='" << key
<< "' dont match. Discarded.");
614 // too bad. need a new lookup.
615 entry
= ch
->extacl_entry
= NULL
;
618 /* Not ours.. get rid of it */
619 debugs(82, 9, "entry " << entry
<< " not valid or not ours. Discarded.");
621 debugs(82, 9, "entry def=" << entry
->def
<< ", our def=" << acl
->def
);
622 const char *key
= makeExternalAclKey(ch
, acl
); // may be nil
623 debugs(82, 9, "entry key='" << (char *)entry
->key
<< "', our key='" << key
<< "'");
625 entry
= ch
->extacl_entry
= NULL
;
630 debugs(82, 9, HERE
<< "No helper entry available");
632 if (acl
->def
->require_auth
) {
633 /* Make sure the user is authenticated */
634 debugs(82, 3, HERE
<< acl
->def
->name
<< " check user authenticated.");
635 const allow_t ti
= AuthenticateAcl(ch
);
637 debugs(82, 2, HERE
<< acl
->def
->name
<< " user not authenticated (" << ti
<< ")");
640 debugs(82, 3, HERE
<< acl
->def
->name
<< " user is authenticated.");
643 const char *key
= makeExternalAclKey(ch
, acl
);
646 /* Not sufficient data to process */
650 entry
= static_cast<ExternalACLEntry
*>(hash_lookup(acl
->def
->cache
, key
));
652 const ExternalACLEntryPointer staleEntry
= entry
;
653 if (entry
!= NULL
&& external_acl_entry_expired(acl
->def
, entry
))
656 if (entry
!= NULL
&& external_acl_grace_expired(acl
->def
, entry
)) {
657 // refresh in the background
658 ExternalACLLookup::Start(ch
, acl
, true);
659 debugs(82, 4, HERE
<< "no need to wait for the refresh of '" <<
660 key
<< "' in '" << acl
->def
->name
<< "' (ch=" << ch
<< ").");
664 debugs(82, 2, HERE
<< acl
->def
->name
<< "(\"" << key
<< "\") = lookup needed");
666 // TODO: All other helpers allow temporary overload. Should not we?
667 if (!acl
->def
->theHelper
->willOverload()) {
668 debugs(82, 2, HERE
<< "\"" << key
<< "\": queueing a call.");
669 if (!ch
->goAsync(ExternalACLLookup::Instance()))
670 debugs(82, 2, "\"" << key
<< "\": no async support!");
671 debugs(82, 2, HERE
<< "\"" << key
<< "\": return -1.");
672 return ACCESS_DUNNO
; // expired cached or simply absent entry
675 debugs(82, DBG_IMPORTANT
, "WARNING: external ACL '" << acl
->def
->name
<<
676 "' queue full. Request rejected '" << key
<< "'.");
677 external_acl_message
= "SYSTEM TOO BUSY, TRY AGAIN LATER";
680 debugs(82, DBG_IMPORTANT
, "WARNING: external ACL '" << acl
->def
->name
<<
681 "' queue full. Using stale result. '" << key
<< "'.");
683 /* Fall thru to processing below */
689 debugs(82, 4, HERE
<< "entry = { date=" <<
690 (long unsigned int) entry
->date
<<
691 ", result=" << entry
->result
<<
692 " tag=" << entry
->tag
<<
693 " log=" << entry
->log
<< " }");
695 debugs(82, 4, HERE
<< "entry user=" << entry
->user
);
698 external_acl_cache_touch(acl
->def
, entry
);
699 external_acl_message
= entry
->message
.termedBuf();
701 debugs(82, 2, HERE
<< acl
->def
->name
<< " = " << entry
->result
);
702 copyResultsFromEntry(ch
->request
, entry
);
703 return entry
->result
;
707 ACLExternal::match(ACLChecklist
*checklist
)
709 allow_t answer
= aclMatchExternal(data
, Filled(checklist
));
711 // convert to tri-state ACL match 1,0,-1
717 return 0; // non-match
720 case ACCESS_AUTH_REQUIRED
:
722 // If the answer is not allowed or denied (matches/not matches) and
723 // async authentication is not in progress, then we are done.
724 if (checklist
->keepMatching())
725 checklist
->markFinished(answer
, "aclMatchExternal exception");
731 ACLExternal::dump() const
733 external_acl_data
const *acl
= data
;
735 rv
.push_back(SBuf(acl
->def
->name
));
737 for (wordlist
*arg
= acl
->arguments
; arg
; arg
= arg
->next
) {
739 s
.Printf(" %s", arg
->key
);
746 /******************************************************************
751 external_acl_cache_touch(external_acl
* def
, const ExternalACLEntryPointer
&entry
)
753 // this must not be done when nothing is being cached.
754 if (!def
->maybeCacheable(entry
->result
))
757 dlinkDelete(&entry
->lru
, &def
->lru_list
);
758 ExternalACLEntry
*e
= const_cast<ExternalACLEntry
*>(entry
.getRaw()); // XXX: make hash a std::map of Pointer.
759 dlinkAdd(e
, &entry
->lru
, &def
->lru_list
);
763 makeExternalAclKey(ACLFilledChecklist
* ch
, external_acl_data
* acl_data
)
768 // check for special case tokens in the format
769 for (Format::Token
*t
= acl_data
->def
->format
.format
; t
; t
= t
->next
) {
771 if (t
->type
== Format::LFT_EXT_ACL_NAME
) {
773 safe_free(ch
->al
->lastAclName
);
774 ch
->al
->lastAclName
= xstrdup(acl_data
->name
);
777 if (t
->type
== Format::LFT_EXT_ACL_DATA
) {
778 // setup string for %DATA
780 for (auto arg
= acl_data
->arguments
; arg
; arg
= arg
->next
) {
784 if (acl_data
->def
->quote
== Format::LOG_QUOTE_URL
) {
785 const char *quoted
= rfc1738_escape(arg
->key
);
786 sb
.append(quoted
, strlen(quoted
));
790 strwordquote(&mb2
, arg
->key
);
791 sb
.append(mb2
.buf
, mb2
.size
);
796 ch
->al
->lastAclData
= sb
;
800 if (t
->type
== Format::LFT_USER_IDENT
) {
802 // if we fail to go async, we still return NULL and the caller
803 // will detect the failure in ACLExternal::match().
804 (void)ch
->goAsync(IdentLookup::Instance());
811 // assemble the full helper lookup string
812 acl_data
->def
->format
.assemble(mb
, ch
->al
, 0);
818 external_acl_entry_expired(external_acl
* def
, const ExternalACLEntryPointer
&entry
)
820 if (def
->cache_size
<= 0 || entry
->result
== ACCESS_DUNNO
)
823 if (entry
->date
+ (entry
->result
.allowed() ? def
->ttl
: def
->negative_ttl
) < squid_curtime
)
830 external_acl_grace_expired(external_acl
* def
, const ExternalACLEntryPointer
&entry
)
832 if (def
->cache_size
<= 0 || entry
->result
== ACCESS_DUNNO
)
836 ttl
= entry
->result
.allowed() ? def
->ttl
: def
->negative_ttl
;
837 ttl
= (ttl
* (100 - def
->grace
)) / 100;
839 if (entry
->date
+ ttl
<= squid_curtime
)
845 static ExternalACLEntryPointer
846 external_acl_cache_add(external_acl
* def
, const char *key
, ExternalACLEntryData
const & data
)
848 ExternalACLEntryPointer entry
;
850 if (!def
->maybeCacheable(data
.result
)) {
853 if (data
.result
== ACCESS_DUNNO
) {
854 if (const ExternalACLEntryPointer oldentry
= static_cast<ExternalACLEntry
*>(hash_lookup(def
->cache
, key
)))
855 external_acl_cache_delete(def
, oldentry
);
857 entry
= new ExternalACLEntry
;
858 entry
->key
= xstrdup(key
);
864 entry
= static_cast<ExternalACLEntry
*>(hash_lookup(def
->cache
, key
));
865 debugs(82, 2, "external_acl_cache_add: Adding '" << key
<< "' = " << data
.result
);
868 debugs(82, 3, "updating existing entry");
870 external_acl_cache_touch(def
, entry
);
874 entry
= new ExternalACLEntry
;
875 entry
->key
= xstrdup(key
);
884 external_acl_cache_delete(external_acl
* def
, const ExternalACLEntryPointer
&entry
)
886 assert(entry
!= NULL
);
887 assert(def
->cache_size
> 0 && entry
->def
== def
);
888 ExternalACLEntry
*e
= const_cast<ExternalACLEntry
*>(entry
.getRaw()); // XXX: make hash a std::map of Pointer.
889 hash_remove_link(def
->cache
, e
);
890 dlinkDelete(&e
->lru
, &def
->lru_list
);
891 e
->unlock(); // unlock on behalf of the hash
892 def
->cache_entries
-= 1;
895 /******************************************************************
896 * external_acl helpers
899 class externalAclState
901 CBDATA_CLASS(externalAclState
);
904 externalAclState(external_acl
* aDef
, const char *aKey
) :
908 def(cbdataReference(aDef
)),
918 externalAclState
*queue
;
921 CBDATA_CLASS_INIT(externalAclState
);
923 externalAclState::~externalAclState()
926 cbdataReferenceDone(callback_data
);
927 cbdataReferenceDone(def
);
931 * The helper program receives queries on stdin, one
932 * per line, and must return the result on on stdout
934 * General result syntax:
936 * OK/ERR keyword=value ...
940 * user= The users name (login)
941 * message= Message describing the reason
942 * tag= A string tag to be applied to the request that triggered the acl match.
943 * applies to both OK and ERR responses.
944 * Won't override existing request tags.
945 * log= A string to be used in access logging
947 * Other keywords may be added to the protocol later
949 * value needs to be URL-encoded or enclosed in double quotes (")
950 * with \-escaping on any whitespace, quotes, or slashes (\).
953 externalAclHandleReply(void *data
, const Helper::Reply
&reply
)
955 externalAclState
*state
= static_cast<externalAclState
*>(data
);
956 externalAclState
*next
;
957 ExternalACLEntryData entryData
;
959 debugs(82, 2, HERE
<< "reply=" << reply
);
961 if (reply
.result
== Helper::Okay
)
962 entryData
.result
= ACCESS_ALLOWED
;
963 else if (reply
.result
== Helper::Error
)
964 entryData
.result
= ACCESS_DENIED
;
965 else //BrokenHelper,TimedOut or Unknown. Should not cached.
966 entryData
.result
= ACCESS_DUNNO
;
968 // XXX: make entryData store a proper Helper::Reply object instead of copying.
970 entryData
.notes
.append(&reply
.notes
);
972 const char *label
= reply
.notes
.findFirst("tag");
973 if (label
!= NULL
&& *label
!= '\0')
974 entryData
.tag
= label
;
976 label
= reply
.notes
.findFirst("message");
977 if (label
!= NULL
&& *label
!= '\0')
978 entryData
.message
= label
;
980 label
= reply
.notes
.findFirst("log");
981 if (label
!= NULL
&& *label
!= '\0')
982 entryData
.log
= label
;
985 label
= reply
.notes
.findFirst("user");
986 if (label
!= NULL
&& *label
!= '\0')
987 entryData
.user
= label
;
989 label
= reply
.notes
.findFirst("password");
990 if (label
!= NULL
&& *label
!= '\0')
991 entryData
.password
= label
;
994 dlinkDelete(&state
->list
, &state
->def
->queue
);
996 ExternalACLEntryPointer entry
;
997 if (cbdataReferenceValid(state
->def
))
998 entry
= external_acl_cache_add(state
->def
, state
->key
, entryData
);
1002 if (state
->callback
&& cbdataReferenceValidDone(state
->callback_data
, &cbdata
))
1003 state
->callback(cbdata
, entry
);
1005 next
= state
->queue
;
1006 state
->queue
= NULL
;
1015 ACLExternal::ExternalAclLookup(ACLChecklist
*checklist
, ACLExternal
* me
)
1017 ExternalACLLookup::Start(checklist
, me
->data
, false);
1021 ExternalACLLookup::Start(ACLChecklist
*checklist
, external_acl_data
*acl
, bool inBackground
)
1023 external_acl
*def
= acl
->def
;
1025 ACLFilledChecklist
*ch
= Filled(checklist
);
1026 const char *key
= makeExternalAclKey(ch
, acl
);
1027 assert(key
); // XXX: will fail if EXT_ACL_IDENT case needs an async lookup
1029 debugs(82, 2, HERE
<< (inBackground
? "bg" : "fg") << " lookup in '" <<
1030 def
->name
<< "' for '" << key
<< "'");
1032 /* Check for a pending lookup to hook into */
1033 // only possible if we are caching results.
1034 externalAclState
*oldstate
= NULL
;
1035 if (def
->cache_size
> 0) {
1036 for (dlink_node
*node
= def
->queue
.head
; node
; node
= node
->next
) {
1037 externalAclState
*oldstatetmp
= static_cast<externalAclState
*>(node
->data
);
1039 if (strcmp(key
, oldstatetmp
->key
) == 0) {
1040 oldstate
= oldstatetmp
;
1046 // A background refresh has no need to piggiback on a pending request:
1047 // When the pending request completes, the cache will be refreshed anyway.
1048 if (oldstate
&& inBackground
) {
1049 debugs(82, 7, HERE
<< "'" << def
->name
<< "' queue is already being refreshed (ch=" << ch
<< ")");
1053 externalAclState
*state
= new externalAclState(def
, key
);
1055 if (!inBackground
) {
1056 state
->callback
= &ExternalACLLookup::LookupDone
;
1057 state
->callback_data
= cbdataReference(checklist
);
1061 /* Hook into pending lookup */
1062 state
->queue
= oldstate
->queue
;
1063 oldstate
->queue
= state
;
1065 /* No pending lookup found. Sumbit to helper */
1069 buf
.appendf("%s\n", key
);
1070 debugs(82, 4, "externalAclLookup: looking up for '" << key
<< "' in '" << def
->name
<< "'.");
1072 if (!def
->theHelper
->trySubmit(buf
.buf
, externalAclHandleReply
, state
)) {
1073 debugs(82, 7, HERE
<< "'" << def
->name
<< "' submit to helper failed");
1074 assert(inBackground
); // or the caller should have checked
1079 dlinkAdd(state
, &state
->list
, &def
->queue
);
1084 debugs(82, 4, "externalAclLookup: will wait for the result of '" << key
<<
1085 "' in '" << def
->name
<< "' (ch=" << ch
<< ").");
1089 externalAclStats(StoreEntry
* sentry
)
1091 for (external_acl
*p
= Config
.externalAclHelperList
; p
; p
= p
->next
) {
1092 storeAppendPrintf(sentry
, "External ACL Statistics: %s\n", p
->name
);
1093 storeAppendPrintf(sentry
, "Cache size: %d\n", p
->cache
->count
);
1094 assert(p
->theHelper
);
1095 p
->theHelper
->packStatsInto(sentry
);
1096 storeAppendPrintf(sentry
, "\n");
1101 externalAclRegisterWithCacheManager(void)
1103 Mgr::RegisterAction("external_acl",
1104 "External ACL stats",
1105 externalAclStats
, 0, 1);
1109 externalAclInit(void)
1111 for (external_acl
*p
= Config
.externalAclHelperList
; p
; p
= p
->next
) {
1113 p
->cache
= hash_create((HASHCMP
*) strcmp
, hashPrime(1024), hash4
);
1116 p
->theHelper
= new helper(p
->name
);
1118 p
->theHelper
->cmdline
= p
->cmdline
;
1120 p
->theHelper
->childs
.updateLimits(p
->children
);
1122 p
->theHelper
->ipc_type
= IPC_TCP_SOCKET
;
1124 p
->theHelper
->addr
= p
->local_addr
;
1126 helperOpenServers(p
->theHelper
);
1129 externalAclRegisterWithCacheManager();
1133 externalAclShutdown(void)
1137 for (p
= Config
.externalAclHelperList
; p
; p
= p
->next
) {
1138 helperShutdown(p
->theHelper
);
1142 ExternalACLLookup
ExternalACLLookup::instance_
;
1144 ExternalACLLookup::Instance()
1150 ExternalACLLookup::checkForAsync(ACLChecklist
*checklist
)const
1152 /* TODO: optimise this - we probably have a pointer to this
1153 * around somewhere */
1154 ACL
*acl
= ACL::FindByName(AclMatchedName
);
1156 ACLExternal
*me
= dynamic_cast<ACLExternal
*> (acl
);
1158 ACLExternal::ExternalAclLookup(checklist
, me
);
1161 /// Called when an async lookup returns
1163 ExternalACLLookup::LookupDone(void *data
, const ExternalACLEntryPointer
&result
)
1165 ACLFilledChecklist
*checklist
= Filled(static_cast<ACLChecklist
*>(data
));
1166 checklist
->extacl_entry
= result
;
1167 checklist
->resumeNonBlockingCheck(ExternalACLLookup::Instance());
1171 ACLExternal::clone() const
1173 return new ACLExternal(*this);
1176 ACLExternal::ACLExternal(char const *theClass
) : data(NULL
), class_(xstrdup(theClass
))
1179 ACLExternal::ACLExternal(ACLExternal
const & old
) : data(NULL
), class_(old
.class_
? xstrdup(old
.class_
) : NULL
)
1181 /* we don't have copy constructors for the data yet */
1186 ACLExternal::typeString() const
1192 ACLExternal::isProxyAuth() const
1195 return data
->def
->require_auth
;