2 * Copyright (C) 1996-2020 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"
40 #include "ssl/ServerBump.h"
41 #include "ssl/support.h"
45 #include "auth/Gadgets.h"
46 #include "auth/UserRequest.h"
49 #include "ident/AclIdent.h"
52 #ifndef DEFAULT_EXTERNAL_ACL_TTL
53 #define DEFAULT_EXTERNAL_ACL_TTL 1 * 60 * 60
55 #ifndef DEFAULT_EXTERNAL_ACL_CHILDREN
56 #define DEFAULT_EXTERNAL_ACL_CHILDREN 5
59 static char *makeExternalAclKey(ACLFilledChecklist
* ch
, external_acl_data
* acl_data
);
60 static void external_acl_cache_delete(external_acl
* def
, const ExternalACLEntryPointer
&entry
);
61 static int external_acl_entry_expired(external_acl
* def
, const ExternalACLEntryPointer
&entry
);
62 static int external_acl_grace_expired(external_acl
* def
, const ExternalACLEntryPointer
&entry
);
63 static void external_acl_cache_touch(external_acl
* def
, const ExternalACLEntryPointer
&entry
);
64 static ExternalACLEntryPointer
external_acl_cache_add(external_acl
* def
, const char *key
, ExternalACLEntryData
const &data
);
66 /******************************************************************
67 * external_acl directive
72 /* FIXME: These are not really cbdata, but it is an easy way
73 * to get them pooled, refcounted, accounted and freed properly...
75 CBDATA_CLASS(external_acl
);
83 void add(const ExternalACLEntryPointer
&);
87 bool maybeCacheable(const Acl::Answer
&) const;
97 Format::Format format
;
101 Helper::ChildConfig children
;
117 * Configuration flag. May only be altered by the configuration parser.
119 * Indicates that all uses of this external_acl_type helper require authentication
120 * details to be processed. If none are available its a fail match.
125 Format::Quoting quote
; // default quoting to use, set by protocol= parameter
127 Ip::Address local_addr
;
130 CBDATA_CLASS_INIT(external_acl
);
132 external_acl::external_acl() :
134 ttl(DEFAULT_EXTERNAL_ACL_TTL
),
138 format("external_acl_type"),
140 children(DEFAULT_EXTERNAL_ACL_CHILDREN
),
143 cache_size(256*1024),
148 quote(Format::LOG_QUOTE_URL
)
150 local_addr
.setLocalhost();
153 external_acl::~external_acl()
156 wordlistDestroy(&cmdline
);
159 helperShutdown(theHelper
);
164 while (lru_list
.tail
) {
165 ExternalACLEntryPointer
e(static_cast<ExternalACLEntry
*>(lru_list
.tail
->data
));
166 external_acl_cache_delete(this, e
);
169 hashFreeMemory(cache
);
172 external_acl
*node
= next
;
174 node
->next
= NULL
; // prevent recursion
180 parse_externalAclHelper(external_acl
** list
)
182 char *token
= ConfigParser::NextToken();
189 external_acl
*a
= new external_acl
;
190 a
->name
= xstrdup(token
);
192 // Allow supported %macros inside quoted tokens
193 ConfigParser::EnableMacros();
194 token
= ConfigParser::NextToken();
198 if (strncmp(token
, "ttl=", 4) == 0) {
199 a
->ttl
= atoi(token
+ 4);
200 } else if (strncmp(token
, "negative_ttl=", 13) == 0) {
201 a
->negative_ttl
= atoi(token
+ 13);
202 } else if (strncmp(token
, "children=", 9) == 0) {
203 a
->children
.n_max
= atoi(token
+ 9);
204 debugs(0, DBG_CRITICAL
, "WARNING: external_acl_type option children=N has been deprecated in favor of children-max=N and children-startup=N");
205 } else if (strncmp(token
, "children-max=", 13) == 0) {
206 a
->children
.n_max
= atoi(token
+ 13);
207 } else if (strncmp(token
, "children-startup=", 17) == 0) {
208 a
->children
.n_startup
= atoi(token
+ 17);
209 } else if (strncmp(token
, "children-idle=", 14) == 0) {
210 a
->children
.n_idle
= atoi(token
+ 14);
211 } else if (strncmp(token
, "concurrency=", 12) == 0) {
212 a
->children
.concurrency
= atoi(token
+ 12);
213 } else if (strncmp(token
, "queue-size=", 11) == 0) {
214 a
->children
.queue_size
= atoi(token
+ 11);
215 a
->children
.defaultQueueSize
= false;
216 } else if (strncmp(token
, "cache=", 6) == 0) {
217 a
->cache_size
= atoi(token
+ 6);
218 } else if (strncmp(token
, "grace=", 6) == 0) {
219 a
->grace
= atoi(token
+ 6);
220 } else if (strcmp(token
, "protocol=2.5") == 0) {
221 a
->quote
= Format::LOG_QUOTE_SHELL
;
222 } else if (strcmp(token
, "protocol=3.0") == 0) {
223 debugs(3, DBG_PARSE_NOTE(2), "WARNING: external_acl_type option protocol=3.0 is deprecated. Remove this from your config.");
224 a
->quote
= Format::LOG_QUOTE_URL
;
225 } else if (strcmp(token
, "quote=url") == 0) {
226 debugs(3, DBG_PARSE_NOTE(2), "WARNING: external_acl_type option quote=url is deprecated. Remove this from your config.");
227 a
->quote
= Format::LOG_QUOTE_URL
;
228 } else if (strcmp(token
, "quote=shell") == 0) {
229 debugs(3, DBG_PARSE_NOTE(2), "WARNING: external_acl_type option quote=shell is deprecated. Use protocol=2.5 if still needed.");
230 a
->quote
= Format::LOG_QUOTE_SHELL
;
232 /* INET6: allow admin to configure some helpers explicitly to
233 bind to IPv4/v6 localhost port. */
234 } else if (strcmp(token
, "ipv4") == 0) {
235 if ( !a
->local_addr
.setIPv4() ) {
236 debugs(3, DBG_CRITICAL
, "WARNING: Error converting " << a
->local_addr
<< " to IPv4 in " << a
->name
);
238 } else if (strcmp(token
, "ipv6") == 0) {
240 debugs(3, DBG_CRITICAL
, "WARNING: --enable-ipv6 required for external ACL helpers to use IPv6: " << a
->name
);
241 // else nothing to do.
246 token
= ConfigParser::NextToken();
248 ConfigParser::DisableMacros();
250 /* check that child startup value is sane. */
251 if (a
->children
.n_startup
> a
->children
.n_max
)
252 a
->children
.n_startup
= a
->children
.n_max
;
254 /* check that child idle value is sane. */
255 if (a
->children
.n_idle
> a
->children
.n_max
)
256 a
->children
.n_idle
= a
->children
.n_max
;
257 if (a
->children
.n_idle
< 1)
258 a
->children
.n_idle
= 1;
260 if (a
->negative_ttl
== -1)
261 a
->negative_ttl
= a
->ttl
;
263 if (a
->children
.defaultQueueSize
)
264 a
->children
.queue_size
= 2 * a
->children
.n_max
;
266 /* Legacy external_acl_type format parser.
267 * Handles a series of %... tokens where any non-% means
268 * the start of another parameter field (ie the path to binary).
270 enum Format::Quoting quote
= Format::LOG_QUOTE_NONE
;
271 Format::Token
**fmt
= &a
->format
.format
;
272 bool data_used
= false;
274 /* stop on first non-% token found */
278 *fmt
= new Format::Token
;
279 // these tokens are whitespace delimited
280 (*fmt
)->space
= true;
282 // set the default encoding to match the protocol= config
283 // this will be overridden by explicit %macro attributes
284 (*fmt
)->quote
= a
->quote
;
286 // compatibility for old tokens incompatible with Format::Token syntax
287 #if USE_OPENSSL // do not bother unless we have to.
288 if (strncmp(token
, "%USER_CERT_", 11) == 0) {
289 (*fmt
)->type
= Format::LFT_EXT_ACL_USER_CERT
;
290 (*fmt
)->data
.string
= xstrdup(token
+ 11);
291 (*fmt
)->data
.header
.header
= (*fmt
)->data
.string
;
292 } else if (strncmp(token
, "%USER_CA_CERT_", 14) == 0) {
293 (*fmt
)->type
= Format::LFT_EXT_ACL_USER_CA_CERT
;
294 (*fmt
)->data
.string
= xstrdup(token
+ 14);
295 (*fmt
)->data
.header
.header
= (*fmt
)->data
.string
;
296 } else if (strncmp(token
, "%CA_CERT_", 9) == 0) {
297 debugs(82, DBG_PARSE_NOTE(DBG_IMPORTANT
), "WARNING: external_acl_type %CA_CERT_* code is obsolete. Use %USER_CA_CERT_* instead");
298 (*fmt
)->type
= Format::LFT_EXT_ACL_USER_CA_CERT
;
299 (*fmt
)->data
.string
= xstrdup(token
+ 9);
300 (*fmt
)->data
.header
.header
= (*fmt
)->data
.string
;
303 if (strncmp(token
,"%<{", 3) == 0) {
306 debugs(82, DBG_PARSE_NOTE(DBG_IMPORTANT
), "WARNING: external_acl_type format %<{...} is deprecated. Use " << tmp
);
307 const size_t parsedLen
= (*fmt
)->parse(tmp
.c_str(), "e
);
308 assert(parsedLen
== tmp
.length());
309 assert((*fmt
)->type
== Format::LFT_REPLY_HEADER
||
310 (*fmt
)->type
== Format::LFT_REPLY_HEADER_ELEM
);
312 } else if (strncmp(token
,"%>{", 3) == 0) {
315 debugs(82, DBG_PARSE_NOTE(DBG_IMPORTANT
), "WARNING: external_acl_type format %>{...} is deprecated. Use " << tmp
);
316 const size_t parsedLen
= (*fmt
)->parse(tmp
.c_str(), "e
);
317 assert(parsedLen
== tmp
.length());
318 assert((*fmt
)->type
== Format::LFT_ADAPTED_REQUEST_HEADER
||
319 (*fmt
)->type
== Format::LFT_ADAPTED_REQUEST_HEADER_ELEM
);
322 // we can use the Format::Token::parse() method since it
323 // only pulls off one token. Since we already checked
324 // for '%' prefix above this is guaranteed to be a token.
325 const size_t len
= (*fmt
)->parse(token
, "e
);
326 assert(len
== strlen(token
));
329 // process special token-specific actions (only if necessary)
331 if ((*fmt
)->type
== Format::LFT_USER_LOGIN
)
332 a
->require_auth
= true;
335 if ((*fmt
)->type
== Format::LFT_EXT_ACL_DATA
)
338 fmt
= &((*fmt
)->next
);
339 token
= ConfigParser::NextToken();
342 /* There must be at least one format token */
343 if (!a
->format
.format
) {
349 // format has implicit %DATA on the end if not used explicitly
351 *fmt
= new Format::Token
;
352 (*fmt
)->type
= Format::LFT_EXT_ACL_DATA
;
353 (*fmt
)->quote
= Format::LOG_QUOTE_NONE
;
363 wordlistAdd(&a
->cmdline
, token
);
366 parse_wordlist(&a
->cmdline
);
369 list
= &(*list
)->next
;
375 dump_externalAclHelper(StoreEntry
* sentry
, const char *name
, const external_acl
* list
)
377 const external_acl
*node
;
378 const wordlist
*word
;
380 for (node
= list
; node
; node
= node
->next
) {
381 storeAppendPrintf(sentry
, "%s %s", name
, node
->name
);
383 if (!node
->local_addr
.isIPv6())
384 storeAppendPrintf(sentry
, " ipv4");
386 storeAppendPrintf(sentry
, " ipv6");
388 if (node
->ttl
!= DEFAULT_EXTERNAL_ACL_TTL
)
389 storeAppendPrintf(sentry
, " ttl=%d", node
->ttl
);
391 if (node
->negative_ttl
!= node
->ttl
)
392 storeAppendPrintf(sentry
, " negative_ttl=%d", node
->negative_ttl
);
395 storeAppendPrintf(sentry
, " grace=%d", node
->grace
);
397 if (node
->children
.n_max
!= DEFAULT_EXTERNAL_ACL_CHILDREN
)
398 storeAppendPrintf(sentry
, " children-max=%d", node
->children
.n_max
);
400 if (node
->children
.n_startup
!= 0) // sync with helper/ChildConfig.cc default
401 storeAppendPrintf(sentry
, " children-startup=%d", node
->children
.n_startup
);
403 if (node
->children
.n_idle
!= 1) // sync with helper/ChildConfig.cc default
404 storeAppendPrintf(sentry
, " children-idle=%d", node
->children
.n_idle
);
406 if (node
->children
.concurrency
!= 0)
407 storeAppendPrintf(sentry
, " concurrency=%d", node
->children
.concurrency
);
410 storeAppendPrintf(sentry
, " cache=%d", node
->cache_size
);
412 if (node
->quote
== Format::LOG_QUOTE_SHELL
)
413 storeAppendPrintf(sentry
, " protocol=2.5");
415 node
->format
.dump(sentry
, NULL
, false);
417 for (word
= node
->cmdline
; word
; word
= word
->next
)
418 storeAppendPrintf(sentry
, " %s", word
->key
);
420 storeAppendPrintf(sentry
, "\n");
425 free_externalAclHelper(external_acl
** list
)
431 static external_acl
*
432 find_externalAclHelper(const char *name
)
436 for (node
= Config
.externalAclHelperList
; node
; node
= node
->next
) {
437 if (strcmp(node
->name
, name
) == 0)
445 external_acl::add(const ExternalACLEntryPointer
&anEntry
)
448 assert(anEntry
!= NULL
);
449 assert (anEntry
->def
== NULL
);
451 ExternalACLEntry
*e
= const_cast<ExternalACLEntry
*>(anEntry
.getRaw()); // XXX: make hash a std::map of Pointer.
453 dlinkAdd(e
, &e
->lru
, &lru_list
);
454 e
->lock(); //cbdataReference(e); // lock it on behalf of the hash
459 external_acl::trimCache()
461 if (cache_size
&& cache_entries
>= cache_size
) {
462 ExternalACLEntryPointer
e(static_cast<ExternalACLEntry
*>(lru_list
.tail
->data
));
463 external_acl_cache_delete(this, e
);
468 external_acl::maybeCacheable(const Acl::Answer
&result
) const
471 return false; // cache is disabled
473 if (result
== ACCESS_DUNNO
)
474 return false; // non-cacheable response
476 if ((result
.allowed() ? ttl
: negative_ttl
) <= 0)
477 return false; // not caching this type of response
482 /******************************************************************
486 class external_acl_data
488 CBDATA_CLASS(external_acl_data
);
491 explicit external_acl_data(external_acl
*aDef
) : def(cbdataReference(aDef
)), name(NULL
), arguments(NULL
) {}
492 ~external_acl_data();
499 CBDATA_CLASS_INIT(external_acl_data
);
501 external_acl_data::~external_acl_data()
504 wordlistDestroy(&arguments
);
505 cbdataReferenceDone(def
);
516 char *token
= ConfigParser::strtokFile();
523 data
= new external_acl_data(find_externalAclHelper(token
));
531 // def->name is the name of the external_acl_type.
532 // this is the name of the 'acl' directive being tested
533 data
->name
= xstrdup(AclMatchedName
);
535 while ((token
= ConfigParser::strtokFile())) {
536 wordlistAdd(&data
->arguments
, token
);
541 ACLExternal::valid () const
544 if (data
->def
->require_auth
) {
545 if (authenticateSchemeCount() == 0) {
546 debugs(28, DBG_CRITICAL
, "Can't use proxy auth because no authentication schemes were compiled.");
550 if (authenticateActiveSchemeCount() == 0) {
551 debugs(28, DBG_CRITICAL
, "Can't use proxy auth because no authentication schemes are fully configured.");
561 ACLExternal::empty () const
566 ACLExternal::~ACLExternal()
573 copyResultsFromEntry(HttpRequest
*req
, const ExternalACLEntryPointer
&entry
)
577 if (entry
->user
.size())
578 req
->extacl_user
= entry
->user
;
580 if (entry
->password
.size())
581 req
->extacl_passwd
= entry
->password
;
583 if (!req
->tag
.size())
584 req
->tag
= entry
->tag
;
586 if (entry
->log
.size())
587 req
->extacl_log
= entry
->log
;
589 if (entry
->message
.size())
590 req
->extacl_message
= entry
->message
;
592 // attach the helper kv-pair to the transaction
593 UpdateRequestNotes(req
->clientConnectionManager
.get(), *req
, entry
->notes
);
598 aclMatchExternal(external_acl_data
*acl
, ACLFilledChecklist
*ch
)
600 debugs(82, 9, HERE
<< "acl=\"" << acl
->def
->name
<< "\"");
601 ExternalACLEntryPointer entry
= ch
->extacl_entry
;
603 external_acl_message
= "MISSING REQUIRED INFORMATION";
606 if (entry
->def
== acl
->def
) {
607 /* Ours, use it.. if the key matches */
608 const char *key
= makeExternalAclKey(ch
, acl
);
610 return ACCESS_DUNNO
; // insufficient data to continue
611 if (strcmp(key
, (char*)entry
->key
) != 0) {
612 debugs(82, 9, "entry key='" << (char *)entry
->key
<< "', our key='" << key
<< "' do not match. Discarded.");
613 // too bad. need a new lookup.
614 entry
= ch
->extacl_entry
= NULL
;
617 /* Not ours.. get rid of it */
618 debugs(82, 9, "entry " << entry
<< " not valid or not ours. Discarded.");
620 debugs(82, 9, "entry def=" << entry
->def
<< ", our def=" << acl
->def
);
621 const char *key
= makeExternalAclKey(ch
, acl
); // may be nil
622 debugs(82, 9, "entry key='" << (char *)entry
->key
<< "', our key='" << key
<< "'");
624 entry
= ch
->extacl_entry
= NULL
;
629 debugs(82, 9, HERE
<< "No helper entry available");
631 if (acl
->def
->require_auth
) {
632 /* Make sure the user is authenticated */
633 debugs(82, 3, HERE
<< acl
->def
->name
<< " check user authenticated.");
634 const auto ti
= AuthenticateAcl(ch
);
636 debugs(82, 2, HERE
<< acl
->def
->name
<< " user not authenticated (" << ti
<< ")");
639 debugs(82, 3, HERE
<< acl
->def
->name
<< " user is authenticated.");
642 const char *key
= makeExternalAclKey(ch
, acl
);
645 /* Not sufficient data to process */
649 entry
= static_cast<ExternalACLEntry
*>(hash_lookup(acl
->def
->cache
, key
));
651 const ExternalACLEntryPointer staleEntry
= entry
;
652 if (entry
!= NULL
&& external_acl_entry_expired(acl
->def
, entry
))
655 if (entry
!= NULL
&& external_acl_grace_expired(acl
->def
, entry
)) {
656 // refresh in the background
657 ExternalACLLookup::Start(ch
, acl
, true);
658 debugs(82, 4, HERE
<< "no need to wait for the refresh of '" <<
659 key
<< "' in '" << acl
->def
->name
<< "' (ch=" << ch
<< ").");
663 debugs(82, 2, HERE
<< acl
->def
->name
<< "(\"" << key
<< "\") = lookup needed");
665 // TODO: All other helpers allow temporary overload. Should not we?
666 if (!acl
->def
->theHelper
->willOverload()) {
667 debugs(82, 2, HERE
<< "\"" << key
<< "\": queueing a call.");
668 if (!ch
->goAsync(ExternalACLLookup::Instance()))
669 debugs(82, 2, "\"" << key
<< "\": no async support!");
670 debugs(82, 2, HERE
<< "\"" << key
<< "\": return -1.");
671 return ACCESS_DUNNO
; // expired cached or simply absent entry
674 debugs(82, DBG_IMPORTANT
, "WARNING: external ACL '" << acl
->def
->name
<<
675 "' queue full. Request rejected '" << key
<< "'.");
676 external_acl_message
= "SYSTEM TOO BUSY, TRY AGAIN LATER";
679 debugs(82, DBG_IMPORTANT
, "WARNING: external ACL '" << acl
->def
->name
<<
680 "' queue full. Using stale result. '" << key
<< "'.");
682 /* Fall thru to processing below */
688 debugs(82, 4, HERE
<< "entry = { date=" <<
689 (long unsigned int) entry
->date
<<
690 ", result=" << entry
->result
<<
691 " tag=" << entry
->tag
<<
692 " log=" << entry
->log
<< " }");
694 debugs(82, 4, HERE
<< "entry user=" << entry
->user
);
697 external_acl_cache_touch(acl
->def
, entry
);
698 external_acl_message
= entry
->message
.termedBuf();
700 debugs(82, 2, HERE
<< acl
->def
->name
<< " = " << entry
->result
);
701 copyResultsFromEntry(ch
->request
, entry
);
702 return entry
->result
;
706 ACLExternal::match(ACLChecklist
*checklist
)
708 auto answer
= aclMatchExternal(data
, Filled(checklist
));
710 // convert to tri-state ACL match 1,0,-1
716 return 0; // non-match
719 case ACCESS_AUTH_REQUIRED
:
721 // If the answer is not allowed or denied (matches/not matches) and
722 // async authentication is not in progress, then we are done.
723 if (checklist
->keepMatching())
724 checklist
->markFinished(answer
, "aclMatchExternal exception");
730 ACLExternal::dump() const
732 external_acl_data
const *acl
= data
;
734 rv
.push_back(SBuf(acl
->def
->name
));
736 for (wordlist
*arg
= acl
->arguments
; arg
; arg
= arg
->next
) {
738 s
.Printf(" %s", arg
->key
);
745 /******************************************************************
750 external_acl_cache_touch(external_acl
* def
, const ExternalACLEntryPointer
&entry
)
752 // this must not be done when nothing is being cached.
753 if (!def
->maybeCacheable(entry
->result
))
756 dlinkDelete(&entry
->lru
, &def
->lru_list
);
757 ExternalACLEntry
*e
= const_cast<ExternalACLEntry
*>(entry
.getRaw()); // XXX: make hash a std::map of Pointer.
758 dlinkAdd(e
, &entry
->lru
, &def
->lru_list
);
762 makeExternalAclKey(ACLFilledChecklist
* ch
, external_acl_data
* acl_data
)
767 // check for special case tokens in the format
768 for (Format::Token
*t
= acl_data
->def
->format
.format
; t
; t
= t
->next
) {
770 if (t
->type
== Format::LFT_EXT_ACL_NAME
) {
772 safe_free(ch
->al
->lastAclName
);
773 ch
->al
->lastAclName
= xstrdup(acl_data
->name
);
776 if (t
->type
== Format::LFT_EXT_ACL_DATA
) {
777 // setup string for %DATA
779 for (auto arg
= acl_data
->arguments
; arg
; arg
= arg
->next
) {
783 if (acl_data
->def
->quote
== Format::LOG_QUOTE_URL
) {
784 const char *quoted
= rfc1738_escape(arg
->key
);
785 sb
.append(quoted
, strlen(quoted
));
789 strwordquote(&mb2
, arg
->key
);
790 sb
.append(mb2
.buf
, mb2
.size
);
795 ch
->al
->lastAclData
= sb
;
799 if (t
->type
== Format::LFT_USER_IDENT
) {
801 // if we fail to go async, we still return NULL and the caller
802 // will detect the failure in ACLExternal::match().
803 (void)ch
->goAsync(IdentLookup::Instance());
810 // assemble the full helper lookup string
811 acl_data
->def
->format
.assemble(mb
, ch
->al
, 0);
817 external_acl_entry_expired(external_acl
* def
, const ExternalACLEntryPointer
&entry
)
819 if (def
->cache_size
<= 0 || entry
->result
== ACCESS_DUNNO
)
822 if (entry
->date
+ (entry
->result
.allowed() ? def
->ttl
: def
->negative_ttl
) < squid_curtime
)
829 external_acl_grace_expired(external_acl
* def
, const ExternalACLEntryPointer
&entry
)
831 if (def
->cache_size
<= 0 || entry
->result
== ACCESS_DUNNO
)
835 ttl
= entry
->result
.allowed() ? def
->ttl
: def
->negative_ttl
;
836 ttl
= (ttl
* (100 - def
->grace
)) / 100;
838 if (entry
->date
+ ttl
<= squid_curtime
)
844 static ExternalACLEntryPointer
845 external_acl_cache_add(external_acl
* def
, const char *key
, ExternalACLEntryData
const & data
)
847 ExternalACLEntryPointer entry
;
849 if (!def
->maybeCacheable(data
.result
)) {
852 if (data
.result
== ACCESS_DUNNO
) {
853 if (const ExternalACLEntryPointer oldentry
= static_cast<ExternalACLEntry
*>(hash_lookup(def
->cache
, key
)))
854 external_acl_cache_delete(def
, oldentry
);
856 entry
= new ExternalACLEntry
;
857 entry
->key
= xstrdup(key
);
863 entry
= static_cast<ExternalACLEntry
*>(hash_lookup(def
->cache
, key
));
864 debugs(82, 2, "external_acl_cache_add: Adding '" << key
<< "' = " << data
.result
);
867 debugs(82, 3, "updating existing entry");
869 external_acl_cache_touch(def
, entry
);
873 entry
= new ExternalACLEntry
;
874 entry
->key
= xstrdup(key
);
883 external_acl_cache_delete(external_acl
* def
, const ExternalACLEntryPointer
&entry
)
885 assert(entry
!= NULL
);
886 assert(def
->cache_size
> 0 && entry
->def
== def
);
887 ExternalACLEntry
*e
= const_cast<ExternalACLEntry
*>(entry
.getRaw()); // XXX: make hash a std::map of Pointer.
888 hash_remove_link(def
->cache
, e
);
889 dlinkDelete(&e
->lru
, &def
->lru_list
);
890 e
->unlock(); // unlock on behalf of the hash
891 def
->cache_entries
-= 1;
894 /******************************************************************
895 * external_acl helpers
898 class externalAclState
900 CBDATA_CLASS(externalAclState
);
903 externalAclState(external_acl
* aDef
, const char *aKey
) :
907 def(cbdataReference(aDef
)),
917 externalAclState
*queue
;
920 CBDATA_CLASS_INIT(externalAclState
);
922 externalAclState::~externalAclState()
925 cbdataReferenceDone(callback_data
);
926 cbdataReferenceDone(def
);
930 * The helper program receives queries on stdin, one
931 * per line, and must return the result on on stdout
933 * General result syntax:
935 * OK/ERR keyword=value ...
939 * user= The users name (login)
940 * message= Message describing the reason
941 * tag= A string tag to be applied to the request that triggered the acl match.
942 * applies to both OK and ERR responses.
943 * Won't override existing request tags.
944 * log= A string to be used in access logging
946 * Other keywords may be added to the protocol later
948 * value needs to be URL-encoded or enclosed in double quotes (")
949 * with \-escaping on any whitespace, quotes, or slashes (\).
952 externalAclHandleReply(void *data
, const Helper::Reply
&reply
)
954 externalAclState
*state
= static_cast<externalAclState
*>(data
);
955 externalAclState
*next
;
956 ExternalACLEntryData entryData
;
958 debugs(82, 2, HERE
<< "reply=" << reply
);
960 if (reply
.result
== Helper::Okay
)
961 entryData
.result
= ACCESS_ALLOWED
;
962 else if (reply
.result
== Helper::Error
)
963 entryData
.result
= ACCESS_DENIED
;
964 else //BrokenHelper,TimedOut or Unknown. Should not cached.
965 entryData
.result
= ACCESS_DUNNO
;
967 // XXX: make entryData store a proper Helper::Reply object instead of copying.
969 entryData
.notes
.append(&reply
.notes
);
971 const char *label
= reply
.notes
.findFirst("tag");
972 if (label
!= NULL
&& *label
!= '\0')
973 entryData
.tag
= label
;
975 label
= reply
.notes
.findFirst("message");
976 if (label
!= NULL
&& *label
!= '\0')
977 entryData
.message
= label
;
979 label
= reply
.notes
.findFirst("log");
980 if (label
!= NULL
&& *label
!= '\0')
981 entryData
.log
= label
;
984 label
= reply
.notes
.findFirst("user");
985 if (label
!= NULL
&& *label
!= '\0')
986 entryData
.user
= label
;
988 label
= reply
.notes
.findFirst("password");
989 if (label
!= NULL
&& *label
!= '\0')
990 entryData
.password
= label
;
993 // XXX: This state->def access conflicts with the cbdata validity check
995 dlinkDelete(&state
->list
, &state
->def
->queue
);
997 ExternalACLEntryPointer entry
;
998 if (cbdataReferenceValid(state
->def
))
999 entry
= external_acl_cache_add(state
->def
, state
->key
, entryData
);
1003 if (state
->callback
&& cbdataReferenceValidDone(state
->callback_data
, &cbdata
))
1004 state
->callback(cbdata
, entry
);
1006 next
= state
->queue
;
1007 state
->queue
= NULL
;
1016 ACLExternal::ExternalAclLookup(ACLChecklist
*checklist
, ACLExternal
* me
)
1018 ExternalACLLookup::Start(checklist
, me
->data
, false);
1022 ExternalACLLookup::Start(ACLChecklist
*checklist
, external_acl_data
*acl
, bool inBackground
)
1024 external_acl
*def
= acl
->def
;
1026 ACLFilledChecklist
*ch
= Filled(checklist
);
1027 const char *key
= makeExternalAclKey(ch
, acl
);
1028 assert(key
); // XXX: will fail if EXT_ACL_IDENT case needs an async lookup
1030 debugs(82, 2, HERE
<< (inBackground
? "bg" : "fg") << " lookup in '" <<
1031 def
->name
<< "' for '" << key
<< "'");
1033 /* Check for a pending lookup to hook into */
1034 // only possible if we are caching results.
1035 externalAclState
*oldstate
= NULL
;
1036 if (def
->cache_size
> 0) {
1037 for (dlink_node
*node
= def
->queue
.head
; node
; node
= node
->next
) {
1038 externalAclState
*oldstatetmp
= static_cast<externalAclState
*>(node
->data
);
1040 if (strcmp(key
, oldstatetmp
->key
) == 0) {
1041 oldstate
= oldstatetmp
;
1047 // A background refresh has no need to piggiback on a pending request:
1048 // When the pending request completes, the cache will be refreshed anyway.
1049 if (oldstate
&& inBackground
) {
1050 debugs(82, 7, HERE
<< "'" << def
->name
<< "' queue is already being refreshed (ch=" << ch
<< ")");
1054 externalAclState
*state
= new externalAclState(def
, key
);
1056 if (!inBackground
) {
1057 state
->callback
= &ExternalACLLookup::LookupDone
;
1058 state
->callback_data
= cbdataReference(checklist
);
1062 /* Hook into pending lookup */
1063 state
->queue
= oldstate
->queue
;
1064 oldstate
->queue
= state
;
1066 /* No pending lookup found. Sumbit to helper */
1070 buf
.appendf("%s\n", key
);
1071 debugs(82, 4, "externalAclLookup: looking up for '" << key
<< "' in '" << def
->name
<< "'.");
1073 if (!def
->theHelper
->trySubmit(buf
.buf
, externalAclHandleReply
, state
)) {
1074 debugs(82, 7, HERE
<< "'" << def
->name
<< "' submit to helper failed");
1075 assert(inBackground
); // or the caller should have checked
1080 dlinkAdd(state
, &state
->list
, &def
->queue
);
1085 debugs(82, 4, "externalAclLookup: will wait for the result of '" << key
<<
1086 "' in '" << def
->name
<< "' (ch=" << ch
<< ").");
1090 externalAclStats(StoreEntry
* sentry
)
1092 for (external_acl
*p
= Config
.externalAclHelperList
; p
; p
= p
->next
) {
1093 storeAppendPrintf(sentry
, "External ACL Statistics: %s\n", p
->name
);
1094 storeAppendPrintf(sentry
, "Cache size: %d\n", p
->cache
->count
);
1095 assert(p
->theHelper
);
1096 p
->theHelper
->packStatsInto(sentry
);
1097 storeAppendPrintf(sentry
, "\n");
1102 externalAclRegisterWithCacheManager(void)
1104 Mgr::RegisterAction("external_acl",
1105 "External ACL stats",
1106 externalAclStats
, 0, 1);
1110 externalAclInit(void)
1112 for (external_acl
*p
= Config
.externalAclHelperList
; p
; p
= p
->next
) {
1114 p
->cache
= hash_create((HASHCMP
*) strcmp
, hashPrime(1024), hash4
);
1117 p
->theHelper
= new helper(p
->name
);
1119 p
->theHelper
->cmdline
= p
->cmdline
;
1121 p
->theHelper
->childs
.updateLimits(p
->children
);
1123 p
->theHelper
->ipc_type
= IPC_TCP_SOCKET
;
1125 p
->theHelper
->addr
= p
->local_addr
;
1127 helperOpenServers(p
->theHelper
);
1130 externalAclRegisterWithCacheManager();
1134 externalAclShutdown(void)
1138 for (p
= Config
.externalAclHelperList
; p
; p
= p
->next
) {
1139 helperShutdown(p
->theHelper
);
1143 ExternalACLLookup
ExternalACLLookup::instance_
;
1145 ExternalACLLookup::Instance()
1151 ExternalACLLookup::checkForAsync(ACLChecklist
*checklist
)const
1153 /* TODO: optimise this - we probably have a pointer to this
1154 * around somewhere */
1155 ACL
*acl
= ACL::FindByName(AclMatchedName
);
1157 ACLExternal
*me
= dynamic_cast<ACLExternal
*> (acl
);
1159 ACLExternal::ExternalAclLookup(checklist
, me
);
1162 /// Called when an async lookup returns
1164 ExternalACLLookup::LookupDone(void *data
, const ExternalACLEntryPointer
&result
)
1166 ACLFilledChecklist
*checklist
= Filled(static_cast<ACLChecklist
*>(data
));
1167 checklist
->extacl_entry
= result
;
1168 checklist
->resumeNonBlockingCheck(ExternalACLLookup::Instance());
1172 ACLExternal::clone() const
1174 return new ACLExternal(*this);
1177 ACLExternal::ACLExternal(char const *theClass
) : data(NULL
), class_(xstrdup(theClass
))
1180 ACLExternal::ACLExternal(ACLExternal
const & old
) : data(NULL
), class_(old
.class_
? xstrdup(old
.class_
) : NULL
)
1182 /* we don't have copy constructors for the data yet */
1187 ACLExternal::typeString() const
1193 ACLExternal::isProxyAuth() const
1196 return data
->def
->require_auth
;