2 * Copyright (C) 1996-2023 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"
39 #include "ssl/ServerBump.h"
40 #include "ssl/support.h"
44 #include "auth/Gadgets.h"
45 #include "auth/UserRequest.h"
48 #include "ident/AclIdent.h"
51 #ifndef DEFAULT_EXTERNAL_ACL_TTL
52 #define DEFAULT_EXTERNAL_ACL_TTL 1 * 60 * 60
54 #ifndef DEFAULT_EXTERNAL_ACL_CHILDREN
55 #define DEFAULT_EXTERNAL_ACL_CHILDREN 5
58 static char *makeExternalAclKey(ACLFilledChecklist
* ch
, external_acl_data
* acl_data
);
59 static void external_acl_cache_delete(external_acl
* def
, const ExternalACLEntryPointer
&entry
);
60 static int external_acl_entry_expired(external_acl
* def
, const ExternalACLEntryPointer
&entry
);
61 static int external_acl_grace_expired(external_acl
* def
, const ExternalACLEntryPointer
&entry
);
62 static void external_acl_cache_touch(external_acl
* def
, const ExternalACLEntryPointer
&entry
);
63 static ExternalACLEntryPointer
external_acl_cache_add(external_acl
* def
, const char *key
, ExternalACLEntryData
const &data
);
65 /******************************************************************
66 * external_acl directive
71 /* XXX: These are not really cbdata, but it is an easy way
72 * to get them pooled, refcounted, accounted and freed properly...
73 * Use RefCountable MEMPROXY_CLASS instead
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
;
103 helper::Pointer theHelper
;
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
);
163 while (lru_list
.tail
) {
164 ExternalACLEntryPointer
e(static_cast<ExternalACLEntry
*>(lru_list
.tail
->data
));
165 external_acl_cache_delete(this, e
);
168 hashFreeMemory(cache
);
171 external_acl
*node
= next
;
173 node
->next
= nullptr; // prevent recursion
179 parse_externalAclHelper(external_acl
** list
)
181 char *token
= ConfigParser::NextToken();
188 external_acl
*a
= new external_acl
;
189 a
->name
= xstrdup(token
);
191 // Allow supported %macros inside quoted tokens
192 ConfigParser::EnableMacros();
193 token
= ConfigParser::NextToken();
197 if (strncmp(token
, "ttl=", 4) == 0) {
198 a
->ttl
= atoi(token
+ 4);
199 } else if (strncmp(token
, "negative_ttl=", 13) == 0) {
200 a
->negative_ttl
= atoi(token
+ 13);
201 } else if (strncmp(token
, "children=", 9) == 0) {
202 a
->children
.n_max
= atoi(token
+ 9);
203 debugs(0, DBG_CRITICAL
, "WARNING: external_acl_type option children=N has been deprecated in favor of children-max=N and children-startup=N");
204 } else if (strncmp(token
, "children-max=", 13) == 0) {
205 a
->children
.n_max
= atoi(token
+ 13);
206 } else if (strncmp(token
, "children-startup=", 17) == 0) {
207 a
->children
.n_startup
= atoi(token
+ 17);
208 } else if (strncmp(token
, "children-idle=", 14) == 0) {
209 a
->children
.n_idle
= atoi(token
+ 14);
210 } else if (strncmp(token
, "concurrency=", 12) == 0) {
211 a
->children
.concurrency
= atoi(token
+ 12);
212 } else if (strncmp(token
, "queue-size=", 11) == 0) {
213 a
->children
.queue_size
= atoi(token
+ 11);
214 a
->children
.defaultQueueSize
= false;
215 } else if (strncmp(token
, "cache=", 6) == 0) {
216 a
->cache_size
= atoi(token
+ 6);
217 } else if (strncmp(token
, "grace=", 6) == 0) {
218 a
->grace
= atoi(token
+ 6);
219 } else if (strcmp(token
, "protocol=2.5") == 0) {
220 a
->quote
= Format::LOG_QUOTE_SHELL
;
221 } else if (strcmp(token
, "protocol=3.0") == 0) {
222 debugs(3, DBG_PARSE_NOTE(2), "WARNING: external_acl_type option protocol=3.0 is deprecated. Remove this from your config.");
223 a
->quote
= Format::LOG_QUOTE_URL
;
224 } else if (strcmp(token
, "quote=url") == 0) {
225 debugs(3, DBG_PARSE_NOTE(2), "WARNING: external_acl_type option quote=url is deprecated. Remove this from your config.");
226 a
->quote
= Format::LOG_QUOTE_URL
;
227 } else if (strcmp(token
, "quote=shell") == 0) {
228 debugs(3, DBG_PARSE_NOTE(2), "WARNING: external_acl_type option quote=shell is deprecated. Use protocol=2.5 if still needed.");
229 a
->quote
= Format::LOG_QUOTE_SHELL
;
231 /* INET6: allow admin to configure some helpers explicitly to
232 bind to IPv4/v6 localhost port. */
233 } else if (strcmp(token
, "ipv4") == 0) {
234 if ( !a
->local_addr
.setIPv4() ) {
235 debugs(3, DBG_CRITICAL
, "WARNING: Error converting " << a
->local_addr
<< " to IPv4 in " << a
->name
);
237 } else if (strcmp(token
, "ipv6") == 0) {
239 debugs(3, DBG_CRITICAL
, "WARNING: --enable-ipv6 required for external ACL helpers to use IPv6: " << a
->name
);
240 // else nothing to do.
245 token
= ConfigParser::NextToken();
247 ConfigParser::DisableMacros();
249 /* check that child startup value is sane. */
250 if (a
->children
.n_startup
> a
->children
.n_max
)
251 a
->children
.n_startup
= a
->children
.n_max
;
253 /* check that child idle value is sane. */
254 if (a
->children
.n_idle
> a
->children
.n_max
)
255 a
->children
.n_idle
= a
->children
.n_max
;
256 if (a
->children
.n_idle
< 1)
257 a
->children
.n_idle
= 1;
259 if (a
->negative_ttl
== -1)
260 a
->negative_ttl
= a
->ttl
;
262 if (a
->children
.defaultQueueSize
)
263 a
->children
.queue_size
= 2 * a
->children
.n_max
;
265 /* Legacy external_acl_type format parser.
266 * Handles a series of %... tokens where any non-% means
267 * the start of another parameter field (ie the path to binary).
269 enum Format::Quoting quote
= Format::LOG_QUOTE_NONE
;
270 Format::Token
**fmt
= &a
->format
.format
;
271 bool data_used
= false;
273 /* stop on first non-% token found */
277 *fmt
= new Format::Token
;
278 // these tokens are whitespace delimited
279 (*fmt
)->space
= true;
281 // set the default encoding to match the protocol= config
282 // this will be overridden by explicit %macro attributes
283 (*fmt
)->quote
= a
->quote
;
285 // compatibility for old tokens incompatible with Format::Token syntax
286 #if USE_OPENSSL // do not bother unless we have to.
287 if (strncmp(token
, "%USER_CERT_", 11) == 0) {
288 (*fmt
)->type
= Format::LFT_EXT_ACL_USER_CERT
;
289 (*fmt
)->data
.string
= xstrdup(token
+ 11);
290 (*fmt
)->data
.header
.header
= (*fmt
)->data
.string
;
291 } else if (strncmp(token
, "%USER_CA_CERT_", 14) == 0) {
292 (*fmt
)->type
= Format::LFT_EXT_ACL_USER_CA_CERT
;
293 (*fmt
)->data
.string
= xstrdup(token
+ 14);
294 (*fmt
)->data
.header
.header
= (*fmt
)->data
.string
;
295 } else if (strncmp(token
, "%CA_CERT_", 9) == 0) {
296 debugs(82, DBG_PARSE_NOTE(DBG_IMPORTANT
), "WARNING: external_acl_type %CA_CERT_* code is obsolete. Use %USER_CA_CERT_* instead");
297 (*fmt
)->type
= Format::LFT_EXT_ACL_USER_CA_CERT
;
298 (*fmt
)->data
.string
= xstrdup(token
+ 9);
299 (*fmt
)->data
.header
.header
= (*fmt
)->data
.string
;
302 if (strncmp(token
,"%<{", 3) == 0) {
305 debugs(82, DBG_PARSE_NOTE(DBG_IMPORTANT
), "WARNING: external_acl_type format %<{...} is deprecated. Use " << tmp
);
306 const size_t parsedLen
= (*fmt
)->parse(tmp
.c_str(), "e
);
307 assert(parsedLen
== tmp
.length());
308 assert((*fmt
)->type
== Format::LFT_REPLY_HEADER
||
309 (*fmt
)->type
== Format::LFT_REPLY_HEADER_ELEM
);
311 } else if (strncmp(token
,"%>{", 3) == 0) {
314 debugs(82, DBG_PARSE_NOTE(DBG_IMPORTANT
), "WARNING: external_acl_type format %>{...} is deprecated. Use " << tmp
);
315 const size_t parsedLen
= (*fmt
)->parse(tmp
.c_str(), "e
);
316 assert(parsedLen
== tmp
.length());
317 assert((*fmt
)->type
== Format::LFT_ADAPTED_REQUEST_HEADER
||
318 (*fmt
)->type
== Format::LFT_ADAPTED_REQUEST_HEADER_ELEM
);
321 // we can use the Format::Token::parse() method since it
322 // only pulls off one token. Since we already checked
323 // for '%' prefix above this is guaranteed to be a token.
324 const size_t len
= (*fmt
)->parse(token
, "e
);
325 assert(len
== strlen(token
));
328 // process special token-specific actions (only if necessary)
330 if ((*fmt
)->type
== Format::LFT_USER_LOGIN
)
331 a
->require_auth
= true;
334 if ((*fmt
)->type
== Format::LFT_EXT_ACL_DATA
)
337 fmt
= &((*fmt
)->next
);
338 token
= ConfigParser::NextToken();
341 /* There must be at least one format token */
342 if (!a
->format
.format
) {
348 // format has implicit %DATA on the end if not used explicitly
350 *fmt
= new Format::Token
;
351 (*fmt
)->type
= Format::LFT_EXT_ACL_DATA
;
352 (*fmt
)->quote
= Format::LOG_QUOTE_NONE
;
362 wordlistAdd(&a
->cmdline
, token
);
365 parse_wordlist(&a
->cmdline
);
368 list
= &(*list
)->next
;
374 dump_externalAclHelper(StoreEntry
* sentry
, const char *name
, const external_acl
* list
)
376 const external_acl
*node
;
377 const wordlist
*word
;
379 for (node
= list
; node
; node
= node
->next
) {
380 storeAppendPrintf(sentry
, "%s %s", name
, node
->name
);
382 if (!node
->local_addr
.isIPv6())
383 storeAppendPrintf(sentry
, " ipv4");
385 storeAppendPrintf(sentry
, " ipv6");
387 if (node
->ttl
!= DEFAULT_EXTERNAL_ACL_TTL
)
388 storeAppendPrintf(sentry
, " ttl=%d", node
->ttl
);
390 if (node
->negative_ttl
!= node
->ttl
)
391 storeAppendPrintf(sentry
, " negative_ttl=%d", node
->negative_ttl
);
394 storeAppendPrintf(sentry
, " grace=%d", node
->grace
);
396 if (node
->children
.n_max
!= DEFAULT_EXTERNAL_ACL_CHILDREN
)
397 storeAppendPrintf(sentry
, " children-max=%d", node
->children
.n_max
);
399 if (node
->children
.n_startup
!= 0) // sync with helper/ChildConfig.cc default
400 storeAppendPrintf(sentry
, " children-startup=%d", node
->children
.n_startup
);
402 if (node
->children
.n_idle
!= 1) // sync with helper/ChildConfig.cc default
403 storeAppendPrintf(sentry
, " children-idle=%d", node
->children
.n_idle
);
405 if (node
->children
.concurrency
!= 0)
406 storeAppendPrintf(sentry
, " concurrency=%d", node
->children
.concurrency
);
409 storeAppendPrintf(sentry
, " cache=%d", node
->cache_size
);
411 if (node
->quote
== Format::LOG_QUOTE_SHELL
)
412 storeAppendPrintf(sentry
, " protocol=2.5");
414 node
->format
.dump(sentry
, nullptr, false);
416 for (word
= node
->cmdline
; word
; word
= word
->next
)
417 storeAppendPrintf(sentry
, " %s", word
->key
);
419 storeAppendPrintf(sentry
, "\n");
424 free_externalAclHelper(external_acl
** list
)
430 static external_acl
*
431 find_externalAclHelper(const char *name
)
435 for (node
= Config
.externalAclHelperList
; node
; node
= node
->next
) {
436 if (strcmp(node
->name
, name
) == 0)
444 external_acl::add(const ExternalACLEntryPointer
&anEntry
)
447 assert(anEntry
!= nullptr);
448 assert (anEntry
->def
== nullptr);
450 ExternalACLEntry
*e
= const_cast<ExternalACLEntry
*>(anEntry
.getRaw()); // XXX: make hash a std::map of Pointer.
452 dlinkAdd(e
, &e
->lru
, &lru_list
);
453 e
->lock(); //cbdataReference(e); // lock it on behalf of the hash
458 external_acl::trimCache()
460 if (cache_size
&& cache_entries
>= cache_size
) {
461 ExternalACLEntryPointer
e(static_cast<ExternalACLEntry
*>(lru_list
.tail
->data
));
462 external_acl_cache_delete(this, e
);
467 external_acl::maybeCacheable(const Acl::Answer
&result
) const
470 return false; // cache is disabled
472 if (result
== ACCESS_DUNNO
)
473 return false; // non-cacheable response
475 if ((result
.allowed() ? ttl
: negative_ttl
) <= 0)
476 return false; // not caching this type of response
481 /******************************************************************
485 class external_acl_data
487 CBDATA_CLASS(external_acl_data
);
490 explicit external_acl_data(external_acl
*aDef
) : def(cbdataReference(aDef
)), name(nullptr), arguments(nullptr) {}
491 ~external_acl_data();
498 CBDATA_CLASS_INIT(external_acl_data
);
500 external_acl_data::~external_acl_data()
503 wordlistDestroy(&arguments
);
504 cbdataReferenceDone(def
);
515 char *token
= ConfigParser::strtokFile();
522 data
= new external_acl_data(find_externalAclHelper(token
));
530 // def->name is the name of the external_acl_type.
531 // this is the name of the 'acl' directive being tested
532 data
->name
= xstrdup(AclMatchedName
);
534 while ((token
= ConfigParser::strtokFile())) {
535 wordlistAdd(&data
->arguments
, token
);
540 ACLExternal::valid () const
543 if (data
->def
->require_auth
) {
544 if (authenticateSchemeCount() == 0) {
545 debugs(28, DBG_CRITICAL
, "ERROR: Cannot use proxy auth because no authentication schemes were compiled.");
549 if (authenticateActiveSchemeCount() == 0) {
550 debugs(28, DBG_CRITICAL
, "ERROR: Cannot use proxy auth because no authentication schemes are fully configured.");
560 ACLExternal::empty () const
565 ACLExternal::~ACLExternal()
572 copyResultsFromEntry(HttpRequest
*req
, const ExternalACLEntryPointer
&entry
)
576 if (entry
->user
.size())
577 req
->extacl_user
= entry
->user
;
579 if (entry
->password
.size())
580 req
->extacl_passwd
= entry
->password
;
582 if (!req
->tag
.size())
583 req
->tag
= entry
->tag
;
585 if (entry
->log
.size())
586 req
->extacl_log
= entry
->log
;
588 if (entry
->message
.size())
589 req
->extacl_message
= entry
->message
;
591 // attach the helper kv-pair to the transaction
592 UpdateRequestNotes(req
->clientConnectionManager
.get(), *req
, entry
->notes
);
597 aclMatchExternal(external_acl_data
*acl
, ACLFilledChecklist
*ch
)
599 debugs(82, 9, "acl=\"" << acl
->def
->name
<< "\"");
600 ExternalACLEntryPointer entry
= ch
->extacl_entry
;
602 external_acl_message
= "MISSING REQUIRED INFORMATION";
604 if (entry
!= nullptr) {
605 if (entry
->def
== acl
->def
) {
606 /* Ours, use it.. if the key matches */
607 const char *key
= makeExternalAclKey(ch
, acl
);
609 return ACCESS_DUNNO
; // insufficient data to continue
610 if (strcmp(key
, (char*)entry
->key
) != 0) {
611 debugs(82, 9, "entry key='" << (char *)entry
->key
<< "', our key='" << key
<< "' do not match. Discarded.");
612 // too bad. need a new lookup.
613 entry
= ch
->extacl_entry
= nullptr;
616 /* Not ours.. get rid of it */
617 debugs(82, 9, "entry " << entry
<< " not valid or not ours. Discarded.");
618 if (entry
!= nullptr) {
619 debugs(82, 9, "entry def=" << entry
->def
<< ", our def=" << acl
->def
);
620 const char *key
= makeExternalAclKey(ch
, acl
); // may be nil
621 debugs(82, 9, "entry key='" << (char *)entry
->key
<< "', our key='" << key
<< "'");
623 entry
= ch
->extacl_entry
= nullptr;
628 debugs(82, 9, "No helper entry available");
630 if (acl
->def
->require_auth
) {
631 /* Make sure the user is authenticated */
632 debugs(82, 3, acl
->def
->name
<< " check user authenticated.");
633 const auto ti
= AuthenticateAcl(ch
);
635 debugs(82, 2, acl
->def
->name
<< " user not authenticated (" << ti
<< ")");
638 debugs(82, 3, acl
->def
->name
<< " user is authenticated.");
641 const char *key
= makeExternalAclKey(ch
, acl
);
644 /* Not sufficient data to process */
648 entry
= static_cast<ExternalACLEntry
*>(hash_lookup(acl
->def
->cache
, key
));
650 const ExternalACLEntryPointer staleEntry
= entry
;
651 if (entry
!= nullptr && external_acl_entry_expired(acl
->def
, entry
))
654 if (entry
!= nullptr && external_acl_grace_expired(acl
->def
, entry
)) {
655 // refresh in the background
656 ExternalACLLookup::Start(ch
, acl
, true);
657 debugs(82, 4, "no need to wait for the refresh of '" <<
658 key
<< "' in '" << acl
->def
->name
<< "' (ch=" << ch
<< ").");
662 debugs(82, 2, acl
->def
->name
<< "(\"" << key
<< "\") = lookup needed");
664 // TODO: All other helpers allow temporary overload. Should not we?
665 if (!acl
->def
->theHelper
->willOverload()) {
666 debugs(82, 2, "\"" << key
<< "\": queueing a call.");
667 if (!ch
->goAsync(ExternalACLLookup::Instance()))
668 debugs(82, 2, "\"" << key
<< "\": no async support!");
669 debugs(82, 2, "\"" << key
<< "\": return -1.");
670 return ACCESS_DUNNO
; // expired cached or simply absent entry
673 debugs(82, DBG_IMPORTANT
, "WARNING: external ACL '" << acl
->def
->name
<<
674 "' queue full. Request rejected '" << key
<< "'.");
675 external_acl_message
= "SYSTEM TOO BUSY, TRY AGAIN LATER";
678 debugs(82, DBG_IMPORTANT
, "WARNING: external ACL '" << acl
->def
->name
<<
679 "' queue full. Using stale result. '" << key
<< "'.");
681 /* Fall thru to processing below */
687 debugs(82, 4, "entry = { date=" <<
688 (long unsigned int) entry
->date
<<
689 ", result=" << entry
->result
<<
690 " tag=" << entry
->tag
<<
691 " log=" << entry
->log
<< " }");
693 debugs(82, 4, "entry user=" << entry
->user
);
696 external_acl_cache_touch(acl
->def
, entry
);
697 external_acl_message
= entry
->message
.termedBuf();
699 debugs(82, 2, acl
->def
->name
<< " = " << entry
->result
);
700 copyResultsFromEntry(ch
->request
, entry
);
701 return entry
->result
;
705 ACLExternal::match(ACLChecklist
*checklist
)
707 auto answer
= aclMatchExternal(data
, Filled(checklist
));
709 // convert to tri-state ACL match 1,0,-1
715 return 0; // non-match
718 case ACCESS_AUTH_REQUIRED
:
720 // If the answer is not allowed or denied (matches/not matches) and
721 // async authentication is not in progress, then we are done.
722 if (checklist
->keepMatching())
723 checklist
->markFinished(answer
, "aclMatchExternal exception");
729 ACLExternal::dump() const
731 external_acl_data
const *acl
= data
;
733 rv
.push_back(SBuf(acl
->def
->name
));
735 for (wordlist
*arg
= acl
->arguments
; arg
; arg
= arg
->next
) {
737 s
.Printf(" %s", arg
->key
);
744 /******************************************************************
749 external_acl_cache_touch(external_acl
* def
, const ExternalACLEntryPointer
&entry
)
751 // this must not be done when nothing is being cached.
752 if (!def
->maybeCacheable(entry
->result
))
755 dlinkDelete(&entry
->lru
, &def
->lru_list
);
756 ExternalACLEntry
*e
= const_cast<ExternalACLEntry
*>(entry
.getRaw()); // XXX: make hash a std::map of Pointer.
757 dlinkAdd(e
, &entry
->lru
, &def
->lru_list
);
761 makeExternalAclKey(ACLFilledChecklist
* ch
, external_acl_data
* acl_data
)
766 // check for special case tokens in the format
767 for (Format::Token
*t
= acl_data
->def
->format
.format
; t
; t
= t
->next
) {
769 if (t
->type
== Format::LFT_EXT_ACL_NAME
) {
771 safe_free(ch
->al
->lastAclName
);
772 ch
->al
->lastAclName
= xstrdup(acl_data
->name
);
775 if (t
->type
== Format::LFT_EXT_ACL_DATA
) {
776 // setup string for %DATA
778 for (auto arg
= acl_data
->arguments
; arg
; arg
= arg
->next
) {
782 if (acl_data
->def
->quote
== Format::LOG_QUOTE_URL
) {
783 const char *quoted
= rfc1738_escape(arg
->key
);
784 sb
.append(quoted
, strlen(quoted
));
788 strwordquote(&mb2
, arg
->key
);
789 sb
.append(mb2
.buf
, mb2
.size
);
794 ch
->al
->lastAclData
= sb
;
798 if (t
->type
== Format::LFT_USER_IDENT
) {
800 // if we fail to go async, we still return NULL and the caller
801 // will detect the failure in ACLExternal::match().
802 (void)ch
->goAsync(IdentLookup::Instance());
809 // assemble the full helper lookup string
810 acl_data
->def
->format
.assemble(mb
, ch
->al
, 0);
816 external_acl_entry_expired(external_acl
* def
, const ExternalACLEntryPointer
&entry
)
818 if (def
->cache_size
<= 0 || entry
->result
== ACCESS_DUNNO
)
821 if (entry
->date
+ (entry
->result
.allowed() ? def
->ttl
: def
->negative_ttl
) < squid_curtime
)
828 external_acl_grace_expired(external_acl
* def
, const ExternalACLEntryPointer
&entry
)
830 if (def
->cache_size
<= 0 || entry
->result
== ACCESS_DUNNO
)
834 ttl
= entry
->result
.allowed() ? def
->ttl
: def
->negative_ttl
;
835 ttl
= (ttl
* (100 - def
->grace
)) / 100;
837 if (entry
->date
+ ttl
<= squid_curtime
)
843 static ExternalACLEntryPointer
844 external_acl_cache_add(external_acl
* def
, const char *key
, ExternalACLEntryData
const & data
)
846 ExternalACLEntryPointer entry
;
848 if (!def
->maybeCacheable(data
.result
)) {
849 debugs(82,6, MYNAME
);
851 if (data
.result
== ACCESS_DUNNO
) {
852 if (const ExternalACLEntryPointer oldentry
= static_cast<ExternalACLEntry
*>(hash_lookup(def
->cache
, key
)))
853 external_acl_cache_delete(def
, oldentry
);
855 entry
= new ExternalACLEntry
;
856 entry
->key
= xstrdup(key
);
862 entry
= static_cast<ExternalACLEntry
*>(hash_lookup(def
->cache
, key
));
863 debugs(82, 2, "external_acl_cache_add: Adding '" << key
<< "' = " << data
.result
);
865 if (entry
!= nullptr) {
866 debugs(82, 3, "updating existing entry");
868 external_acl_cache_touch(def
, entry
);
872 entry
= new ExternalACLEntry
;
873 entry
->key
= xstrdup(key
);
882 external_acl_cache_delete(external_acl
* def
, const ExternalACLEntryPointer
&entry
)
884 assert(entry
!= nullptr);
885 assert(def
->cache_size
> 0 && entry
->def
== def
);
886 ExternalACLEntry
*e
= const_cast<ExternalACLEntry
*>(entry
.getRaw()); // XXX: make hash a std::map of Pointer.
887 hash_remove_link(def
->cache
, e
);
888 dlinkDelete(&e
->lru
, &def
->lru_list
);
889 e
->unlock(); // unlock on behalf of the hash
890 def
->cache_entries
-= 1;
893 /******************************************************************
894 * external_acl helpers
897 class externalAclState
899 CBDATA_CLASS(externalAclState
);
902 externalAclState(external_acl
* aDef
, const char *aKey
) :
904 callback_data(nullptr),
906 def(cbdataReference(aDef
)),
916 externalAclState
*queue
;
919 CBDATA_CLASS_INIT(externalAclState
);
921 externalAclState::~externalAclState()
924 cbdataReferenceDone(callback_data
);
925 cbdataReferenceDone(def
);
929 * The helper program receives queries on stdin, one
930 * per line, and must return the result on on stdout
932 * General result syntax:
934 * OK/ERR keyword=value ...
938 * user= The users name (login)
939 * message= Message describing the reason
940 * tag= A string tag to be applied to the request that triggered the acl match.
941 * applies to both OK and ERR responses.
942 * Won't override existing request tags.
943 * log= A string to be used in access logging
945 * Other keywords may be added to the protocol later
947 * value needs to be URL-encoded or enclosed in double quotes (")
948 * with \-escaping on any whitespace, quotes, or slashes (\).
951 externalAclHandleReply(void *data
, const Helper::Reply
&reply
)
953 externalAclState
*state
= static_cast<externalAclState
*>(data
);
954 externalAclState
*next
;
955 ExternalACLEntryData entryData
;
957 debugs(82, 2, "reply=" << reply
);
959 if (reply
.result
== Helper::Okay
)
960 entryData
.result
= ACCESS_ALLOWED
;
961 else if (reply
.result
== Helper::Error
)
962 entryData
.result
= ACCESS_DENIED
;
963 else //BrokenHelper,TimedOut or Unknown. Should not cached.
964 entryData
.result
= ACCESS_DUNNO
;
966 // XXX: make entryData store a proper Helper::Reply object instead of copying.
968 entryData
.notes
.append(&reply
.notes
);
970 const char *label
= reply
.notes
.findFirst("tag");
971 if (label
!= nullptr && *label
!= '\0')
972 entryData
.tag
= label
;
974 label
= reply
.notes
.findFirst("message");
975 if (label
!= nullptr && *label
!= '\0')
976 entryData
.message
= label
;
978 label
= reply
.notes
.findFirst("log");
979 if (label
!= nullptr && *label
!= '\0')
980 entryData
.log
= label
;
983 label
= reply
.notes
.findFirst("user");
984 if (label
!= nullptr && *label
!= '\0')
985 entryData
.user
= label
;
987 label
= reply
.notes
.findFirst("password");
988 if (label
!= nullptr && *label
!= '\0')
989 entryData
.password
= label
;
992 // XXX: This state->def access conflicts with the cbdata validity check
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
= nullptr;
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, (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
= nullptr;
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, "'" << 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, "'" << 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
= helper::Make("external_acl_type");
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());
1170 ACLExternal::ACLExternal(char const *theClass
) : data(nullptr), class_(xstrdup(theClass
))
1174 ACLExternal::typeString() const
1180 ACLExternal::isProxyAuth() const
1183 return data
->def
->require_auth
;