2 * Copyright (C) 1996-2015 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 "HttpHeaderTools.h"
26 #include "HttpReply.h"
27 #include "HttpRequest.h"
30 #include "mgr/Registration.h"
32 #include "SquidConfig.h"
33 #include "SquidString.h"
34 #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
&);
95 Format::Format format
;
99 Helper::ChildConfig children
;
115 * Configuration flag. May only be altered by the configuration parser.
117 * Indicates that all uses of this external_acl_type helper require authentication
118 * details to be processed. If none are available its a fail match.
123 Format::Quoting quote
; // default quoting to use, set by protocol= parameter
125 Ip::Address local_addr
;
128 CBDATA_CLASS_INIT(external_acl
);
130 external_acl::external_acl() :
132 ttl(DEFAULT_EXTERNAL_ACL_TTL
),
136 format("external_acl_type"),
138 children(DEFAULT_EXTERNAL_ACL_CHILDREN
),
141 cache_size(256*1024),
146 quote(Format::LOG_QUOTE_URL
)
148 local_addr
.setLocalhost();
151 external_acl::~external_acl()
154 wordlistDestroy(&cmdline
);
157 helperShutdown(theHelper
);
162 while (lru_list
.tail
) {
163 ExternalACLEntryPointer
e(static_cast<ExternalACLEntry
*>(lru_list
.tail
->data
));
164 external_acl_cache_delete(this, e
);
167 hashFreeMemory(cache
);
170 external_acl
*node
= next
;
172 node
->next
= NULL
; // prevent recursion
178 parse_externalAclHelper(external_acl
** list
)
180 external_acl
*a
= new external_acl
;
181 char *token
= ConfigParser::NextToken();
186 a
->name
= xstrdup(token
);
188 // Allow supported %macros inside quoted tokens
189 ConfigParser::EnableMacros();
190 token
= ConfigParser::NextToken();
194 if (strncmp(token
, "ttl=", 4) == 0) {
195 a
->ttl
= atoi(token
+ 4);
196 } else if (strncmp(token
, "negative_ttl=", 13) == 0) {
197 a
->negative_ttl
= atoi(token
+ 13);
198 } else if (strncmp(token
, "children=", 9) == 0) {
199 a
->children
.n_max
= atoi(token
+ 9);
200 debugs(0, DBG_CRITICAL
, "WARNING: external_acl_type option children=N has been deprecated in favor of children-max=N and children-startup=N");
201 } else if (strncmp(token
, "children-max=", 13) == 0) {
202 a
->children
.n_max
= atoi(token
+ 13);
203 } else if (strncmp(token
, "children-startup=", 17) == 0) {
204 a
->children
.n_startup
= atoi(token
+ 17);
205 } else if (strncmp(token
, "children-idle=", 14) == 0) {
206 a
->children
.n_idle
= atoi(token
+ 14);
207 } else if (strncmp(token
, "concurrency=", 12) == 0) {
208 a
->children
.concurrency
= atoi(token
+ 12);
209 } else if (strncmp(token
, "queue-size=", 11) == 0) {
210 a
->children
.queue_size
= atoi(token
+ 11);
211 a
->children
.defaultQueueSize
= false;
212 } else if (strncmp(token
, "cache=", 6) == 0) {
213 a
->cache_size
= atoi(token
+ 6);
214 } else if (strncmp(token
, "grace=", 6) == 0) {
215 a
->grace
= atoi(token
+ 6);
216 } else if (strcmp(token
, "protocol=2.5") == 0) {
217 a
->quote
= Format::LOG_QUOTE_SHELL
;
218 } else if (strcmp(token
, "protocol=3.0") == 0) {
219 debugs(3, DBG_PARSE_NOTE(2), "WARNING: external_acl_type option protocol=3.0 is deprecated. Remove this from your config.");
220 a
->quote
= Format::LOG_QUOTE_URL
;
221 } else if (strcmp(token
, "quote=url") == 0) {
222 debugs(3, DBG_PARSE_NOTE(2), "WARNING: external_acl_type option quote=url is deprecated. Remove this from your config.");
223 a
->quote
= Format::LOG_QUOTE_URL
;
224 } else if (strcmp(token
, "quote=shell") == 0) {
225 debugs(3, DBG_PARSE_NOTE(2), "WARNING: external_acl_type option quote=shell is deprecated. Use protocol=2.5 if still needed.");
226 a
->quote
= Format::LOG_QUOTE_SHELL
;
228 /* INET6: allow admin to configure some helpers explicitly to
229 bind to IPv4/v6 localhost port. */
230 } else if (strcmp(token
, "ipv4") == 0) {
231 if ( !a
->local_addr
.setIPv4() ) {
232 debugs(3, DBG_CRITICAL
, "WARNING: Error converting " << a
->local_addr
<< " to IPv4 in " << a
->name
);
234 } else if (strcmp(token
, "ipv6") == 0) {
236 debugs(3, DBG_CRITICAL
, "WARNING: --enable-ipv6 required for external ACL helpers to use IPv6: " << a
->name
);
237 // else nothing to do.
242 token
= ConfigParser::NextToken();
244 ConfigParser::DisableMacros();
246 /* check that child startup value is sane. */
247 if (a
->children
.n_startup
> a
->children
.n_max
)
248 a
->children
.n_startup
= a
->children
.n_max
;
250 /* check that child idle value is sane. */
251 if (a
->children
.n_idle
> a
->children
.n_max
)
252 a
->children
.n_idle
= a
->children
.n_max
;
253 if (a
->children
.n_idle
< 1)
254 a
->children
.n_idle
= 1;
256 if (a
->negative_ttl
== -1)
257 a
->negative_ttl
= a
->ttl
;
259 if (a
->children
.defaultQueueSize
)
260 a
->children
.queue_size
= 2 * a
->children
.n_max
;
262 /* Legacy external_acl_type format parser.
263 * Handles a series of %... tokens where any non-% means
264 * the start of another parameter field (ie the path to binary).
266 enum Format::Quoting quote
= Format::LOG_QUOTE_NONE
;
267 Format::Token
**fmt
= &a
->format
.format
;
268 bool data_used
= false;
270 /* stop on first non-% token found */
274 *fmt
= new Format::Token
;
275 // these tokens are whitespace delimited
276 (*fmt
)->space
= true;
278 // set the default encoding to match the protocol= config
279 // this will be overridden by explicit %macro attributes
280 (*fmt
)->quote
= a
->quote
;
282 // compatibility for old tokens incompatible with Format::Token syntax
283 #if USE_OPENSSL // dont bother if we dont have to.
284 if (strncmp(token
, "%USER_CERT_", 11) == 0) {
285 (*fmt
)->type
= Format::LFT_EXT_ACL_USER_CERT
;
286 (*fmt
)->data
.string
= xstrdup(token
+ 11);
287 (*fmt
)->data
.header
.header
= (*fmt
)->data
.string
;
288 } else if (strncmp(token
, "%USER_CA_CERT_", 14) == 0) {
289 (*fmt
)->type
= Format::LFT_EXT_ACL_USER_CA_CERT
;
290 (*fmt
)->data
.string
= xstrdup(token
+ 14);
291 (*fmt
)->data
.header
.header
= (*fmt
)->data
.string
;
292 } else if (strncmp(token
, "%CA_CERT_", 9) == 0) {
293 debugs(82, DBG_PARSE_NOTE(DBG_IMPORTANT
), "WARNING: external_acl_type %CA_CERT_* code is obsolete. Use %USER_CA_CERT_* instead");
294 (*fmt
)->type
= Format::LFT_EXT_ACL_USER_CA_CERT
;
295 (*fmt
)->data
.string
= xstrdup(token
+ 9);
296 (*fmt
)->data
.header
.header
= (*fmt
)->data
.string
;
300 // we can use the Format::Token::parse() method since it
301 // only pulls off one token. Since we already checked
302 // for '%' prefix above this is guaranteed to be a token.
303 const size_t len
= (*fmt
)->parse(token
, "e
);
304 assert(len
== strlen(token
));
307 // process special token-specific actions (only if necessary)
309 if ((*fmt
)->type
== Format::LFT_USER_LOGIN
)
310 a
->require_auth
= true;
313 if ((*fmt
)->type
== Format::LFT_EXT_ACL_DATA
)
316 fmt
= &((*fmt
)->next
);
317 token
= ConfigParser::NextToken();
320 /* There must be at least one format token */
321 if (!a
->format
.format
)
324 // format has implicit %DATA on the end if not used explicitly
326 *fmt
= new Format::Token
;
327 (*fmt
)->type
= Format::LFT_EXT_ACL_DATA
;
328 (*fmt
)->quote
= Format::LOG_QUOTE_URL
;
335 wordlistAdd(&a
->cmdline
, token
);
338 parse_wordlist(&a
->cmdline
);
341 list
= &(*list
)->next
;
347 dump_externalAclHelper(StoreEntry
* sentry
, const char *name
, const external_acl
* list
)
349 const external_acl
*node
;
350 const wordlist
*word
;
352 for (node
= list
; node
; node
= node
->next
) {
353 storeAppendPrintf(sentry
, "%s %s", name
, node
->name
);
355 if (!node
->local_addr
.isIPv6())
356 storeAppendPrintf(sentry
, " ipv4");
358 storeAppendPrintf(sentry
, " ipv6");
360 if (node
->ttl
!= DEFAULT_EXTERNAL_ACL_TTL
)
361 storeAppendPrintf(sentry
, " ttl=%d", node
->ttl
);
363 if (node
->negative_ttl
!= node
->ttl
)
364 storeAppendPrintf(sentry
, " negative_ttl=%d", node
->negative_ttl
);
367 storeAppendPrintf(sentry
, " grace=%d", node
->grace
);
369 if (node
->children
.n_max
!= DEFAULT_EXTERNAL_ACL_CHILDREN
)
370 storeAppendPrintf(sentry
, " children-max=%d", node
->children
.n_max
);
372 if (node
->children
.n_startup
!= 1)
373 storeAppendPrintf(sentry
, " children-startup=%d", node
->children
.n_startup
);
375 if (node
->children
.n_idle
!= (node
->children
.n_max
+ node
->children
.n_startup
) )
376 storeAppendPrintf(sentry
, " children-idle=%d", node
->children
.n_idle
);
378 if (node
->children
.concurrency
)
379 storeAppendPrintf(sentry
, " concurrency=%d", node
->children
.concurrency
);
382 storeAppendPrintf(sentry
, " cache=%d", node
->cache_size
);
384 if (node
->quote
== Format::LOG_QUOTE_SHELL
)
385 storeAppendPrintf(sentry
, " protocol=2.5");
387 node
->format
.dump(sentry
, NULL
, false);
389 for (word
= node
->cmdline
; word
; word
= word
->next
)
390 storeAppendPrintf(sentry
, " %s", word
->key
);
392 storeAppendPrintf(sentry
, "\n");
397 free_externalAclHelper(external_acl
** list
)
403 static external_acl
*
404 find_externalAclHelper(const char *name
)
408 for (node
= Config
.externalAclHelperList
; node
; node
= node
->next
) {
409 if (strcmp(node
->name
, name
) == 0)
417 external_acl::add(const ExternalACLEntryPointer
&anEntry
)
420 assert(anEntry
!= NULL
);
421 assert (anEntry
->def
== NULL
);
423 ExternalACLEntry
*e
= const_cast<ExternalACLEntry
*>(anEntry
.getRaw()); // XXX: make hash a std::map of Pointer.
425 dlinkAdd(e
, &e
->lru
, &lru_list
);
426 e
->lock(); //cbdataReference(e); // lock it on behalf of the hash
431 external_acl::trimCache()
433 if (cache_size
&& cache_entries
>= cache_size
) {
434 ExternalACLEntryPointer
e(static_cast<ExternalACLEntry
*>(lru_list
.tail
->data
));
435 external_acl_cache_delete(this, e
);
439 /******************************************************************
443 class external_acl_data
445 CBDATA_CLASS(external_acl_data
);
448 explicit external_acl_data(external_acl
*aDef
) : def(cbdataReference(aDef
)), name(NULL
), arguments(NULL
) {}
449 ~external_acl_data();
456 CBDATA_CLASS_INIT(external_acl_data
);
458 external_acl_data::~external_acl_data()
461 wordlistDestroy(&arguments
);
462 cbdataReferenceDone(def
);
471 char *token
= ConfigParser::strtokFile();
476 data
= new external_acl_data(find_externalAclHelper(token
));
481 // def->name is the name of the external_acl_type.
482 // this is the name of the 'acl' directive being tested
483 data
->name
= xstrdup(AclMatchedName
);
485 while ((token
= ConfigParser::strtokFile())) {
486 wordlistAdd(&data
->arguments
, token
);
491 ACLExternal::valid () const
494 if (data
->def
->require_auth
) {
495 if (authenticateSchemeCount() == 0) {
496 debugs(28, DBG_CRITICAL
, "Can't use proxy auth because no authentication schemes were compiled.");
500 if (authenticateActiveSchemeCount() == 0) {
501 debugs(28, DBG_CRITICAL
, "Can't use proxy auth because no authentication schemes are fully configured.");
511 ACLExternal::empty () const
516 ACLExternal::~ACLExternal()
523 copyResultsFromEntry(HttpRequest
*req
, const ExternalACLEntryPointer
&entry
)
527 if (entry
->user
.size())
528 req
->extacl_user
= entry
->user
;
530 if (entry
->password
.size())
531 req
->extacl_passwd
= entry
->password
;
533 if (!req
->tag
.size())
534 req
->tag
= entry
->tag
;
536 if (entry
->log
.size())
537 req
->extacl_log
= entry
->log
;
539 if (entry
->message
.size())
540 req
->extacl_message
= entry
->message
;
542 // attach the helper kv-pair to the transaction
543 UpdateRequestNotes(req
->clientConnectionManager
.get(), *req
, entry
->notes
);
548 aclMatchExternal(external_acl_data
*acl
, ACLFilledChecklist
*ch
)
550 debugs(82, 9, HERE
<< "acl=\"" << acl
->def
->name
<< "\"");
551 ExternalACLEntryPointer entry
= ch
->extacl_entry
;
553 external_acl_message
= "MISSING REQUIRED INFORMATION";
556 if (entry
->def
== acl
->def
) {
557 /* Ours, use it.. if the key matches */
558 const char *key
= makeExternalAclKey(ch
, acl
);
560 return ACCESS_DUNNO
; // insufficent data to continue
561 if (strcmp(key
, (char*)entry
->key
) != 0) {
562 debugs(82, 9, "entry key='" << (char *)entry
->key
<< "', our key='" << key
<< "' dont match. Discarded.");
563 // too bad. need a new lookup.
564 entry
= ch
->extacl_entry
= NULL
;
567 /* Not ours.. get rid of it */
568 debugs(82, 9, "entry " << entry
<< " not valid or not ours. Discarded.");
570 debugs(82, 9, "entry def=" << entry
->def
<< ", our def=" << acl
->def
);
571 const char *key
= makeExternalAclKey(ch
, acl
); // may be nil
572 debugs(82, 9, "entry key='" << (char *)entry
->key
<< "', our key='" << key
<< "'");
574 entry
= ch
->extacl_entry
= NULL
;
579 debugs(82, 9, HERE
<< "No helper entry available");
581 if (acl
->def
->require_auth
) {
582 /* Make sure the user is authenticated */
583 debugs(82, 3, HERE
<< acl
->def
->name
<< " check user authenticated.");
584 const allow_t ti
= AuthenticateAcl(ch
);
585 if (ti
!= ACCESS_ALLOWED
) {
586 debugs(82, 2, HERE
<< acl
->def
->name
<< " user not authenticated (" << ti
<< ")");
589 debugs(82, 3, HERE
<< acl
->def
->name
<< " user is authenticated.");
592 const char *key
= makeExternalAclKey(ch
, acl
);
595 /* Not sufficient data to process */
599 entry
= static_cast<ExternalACLEntry
*>(hash_lookup(acl
->def
->cache
, key
));
601 const ExternalACLEntryPointer staleEntry
= entry
;
602 if (entry
!= NULL
&& external_acl_entry_expired(acl
->def
, entry
))
605 if (entry
!= NULL
&& external_acl_grace_expired(acl
->def
, entry
)) {
606 // refresh in the background
607 ExternalACLLookup::Start(ch
, acl
, true);
608 debugs(82, 4, HERE
<< "no need to wait for the refresh of '" <<
609 key
<< "' in '" << acl
->def
->name
<< "' (ch=" << ch
<< ").");
613 debugs(82, 2, HERE
<< acl
->def
->name
<< "(\"" << key
<< "\") = lookup needed");
615 if (!acl
->def
->theHelper
->queueFull()) {
616 debugs(82, 2, HERE
<< "\"" << key
<< "\": queueing a call.");
617 if (!ch
->goAsync(ExternalACLLookup::Instance()))
618 debugs(82, 2, "\"" << key
<< "\": no async support!");
619 debugs(82, 2, HERE
<< "\"" << key
<< "\": return -1.");
620 return ACCESS_DUNNO
; // expired cached or simply absent entry
623 debugs(82, DBG_IMPORTANT
, "WARNING: external ACL '" << acl
->def
->name
<<
624 "' queue overload. Request rejected '" << key
<< "'.");
625 external_acl_message
= "SYSTEM TOO BUSY, TRY AGAIN LATER";
628 debugs(82, DBG_IMPORTANT
, "WARNING: external ACL '" << acl
->def
->name
<<
629 "' queue overload. Using stale result. '" << key
<< "'.");
631 /* Fall thru to processing below */
637 debugs(82, 4, HERE
<< "entry = { date=" <<
638 (long unsigned int) entry
->date
<<
639 ", result=" << entry
->result
<<
640 " tag=" << entry
->tag
<<
641 " log=" << entry
->log
<< " }");
643 debugs(82, 4, HERE
<< "entry user=" << entry
->user
);
646 external_acl_cache_touch(acl
->def
, entry
);
647 external_acl_message
= entry
->message
.termedBuf();
649 debugs(82, 2, HERE
<< acl
->def
->name
<< " = " << entry
->result
);
650 copyResultsFromEntry(ch
->request
, entry
);
651 return entry
->result
;
655 ACLExternal::match(ACLChecklist
*checklist
)
657 allow_t answer
= aclMatchExternal(data
, Filled(checklist
));
659 // convert to tri-state ACL match 1,0,-1
665 return 0; // non-match
668 case ACCESS_AUTH_REQUIRED
:
670 // If the answer is not allowed or denied (matches/not matches) and
671 // async authentication is not in progress, then we are done.
672 if (checklist
->keepMatching())
673 checklist
->markFinished(answer
, "aclMatchExternal exception");
679 ACLExternal::dump() const
681 external_acl_data
const *acl
= data
;
683 rv
.push_back(SBuf(acl
->def
->name
));
685 for (wordlist
*arg
= acl
->arguments
; arg
; arg
= arg
->next
) {
687 s
.Printf(" %s", arg
->key
);
694 /******************************************************************
699 external_acl_cache_touch(external_acl
* def
, const ExternalACLEntryPointer
&entry
)
701 // this must not be done when nothing is being cached.
702 if (def
->cache_size
<= 0 || (def
->ttl
<= 0 && entry
->result
== 1) || (def
->negative_ttl
<= 0 && entry
->result
!= 1))
705 dlinkDelete(&entry
->lru
, &def
->lru_list
);
706 ExternalACLEntry
*e
= const_cast<ExternalACLEntry
*>(entry
.getRaw()); // XXX: make hash a std::map of Pointer.
707 dlinkAdd(e
, &entry
->lru
, &def
->lru_list
);
711 makeExternalAclKey(ACLFilledChecklist
* ch
, external_acl_data
* acl_data
)
716 // check for special case tokens in the format
717 for (Format::Token
*t
= acl_data
->def
->format
.format
; t
; t
= t
->next
) {
719 if (t
->type
== Format::LFT_EXT_ACL_NAME
) {
721 safe_free(ch
->al
->lastAclName
);
722 ch
->al
->lastAclName
= xstrdup(acl_data
->name
);
725 if (t
->type
== Format::LFT_EXT_ACL_DATA
) {
726 // setup string for %DATA
728 for (auto arg
= acl_data
->arguments
; arg
; arg
= arg
->next
) {
732 if (acl_data
->def
->quote
== Format::LOG_QUOTE_URL
) {
733 const char *quoted
= rfc1738_escape(arg
->key
);
734 sb
.append(quoted
, strlen(quoted
));
738 strwordquote(&mb2
, arg
->key
);
739 sb
.append(mb2
.buf
, mb2
.size
);
744 ch
->al
->lastAclData
= xstrdup(sb
.c_str());
748 if (t
->type
== Format::LFT_USER_IDENT
) {
750 // if we fail to go async, we still return NULL and the caller
751 // will detect the failure in ACLExternal::match().
752 (void)ch
->goAsync(IdentLookup::Instance());
759 // assemble the full helper lookup string
760 acl_data
->def
->format
.assemble(mb
, ch
->al
, 0);
766 external_acl_entry_expired(external_acl
* def
, const ExternalACLEntryPointer
&entry
)
768 if (def
->cache_size
<= 0)
771 if (entry
->date
+ (entry
->result
== 1 ? def
->ttl
: def
->negative_ttl
) < squid_curtime
)
778 external_acl_grace_expired(external_acl
* def
, const ExternalACLEntryPointer
&entry
)
780 if (def
->cache_size
<= 0)
784 ttl
= entry
->result
== 1 ? def
->ttl
: def
->negative_ttl
;
785 ttl
= (ttl
* (100 - def
->grace
)) / 100;
787 if (entry
->date
+ ttl
<= squid_curtime
)
793 static ExternalACLEntryPointer
794 external_acl_cache_add(external_acl
* def
, const char *key
, ExternalACLEntryData
const & data
)
796 ExternalACLEntryPointer entry
;
798 // do not bother caching this result if TTL is going to expire it immediately
799 if (def
->cache_size
<= 0 || (def
->ttl
<= 0 && data
.result
== 1) || (def
->negative_ttl
<= 0 && data
.result
!= 1)) {
801 entry
= new ExternalACLEntry
;
802 entry
->key
= xstrdup(key
);
808 entry
= static_cast<ExternalACLEntry
*>(hash_lookup(def
->cache
, key
));
809 debugs(82, 2, "external_acl_cache_add: Adding '" << key
<< "' = " << data
.result
);
812 debugs(82, 3, "updating existing entry");
814 external_acl_cache_touch(def
, entry
);
818 entry
= new ExternalACLEntry
;
819 entry
->key
= xstrdup(key
);
828 external_acl_cache_delete(external_acl
* def
, const ExternalACLEntryPointer
&entry
)
830 assert(entry
!= NULL
);
831 assert(def
->cache_size
> 0 && entry
->def
== def
);
832 ExternalACLEntry
*e
= const_cast<ExternalACLEntry
*>(entry
.getRaw()); // XXX: make hash a std::map of Pointer.
833 hash_remove_link(def
->cache
, e
);
834 dlinkDelete(&e
->lru
, &def
->lru_list
);
835 e
->unlock(); // unlock on behalf of the hash
836 def
->cache_entries
-= 1;
839 /******************************************************************
840 * external_acl helpers
843 class externalAclState
845 CBDATA_CLASS(externalAclState
);
848 externalAclState(external_acl
* aDef
, const char *aKey
) :
852 def(cbdataReference(aDef
)),
862 externalAclState
*queue
;
865 CBDATA_CLASS_INIT(externalAclState
);
867 externalAclState::~externalAclState()
870 cbdataReferenceDone(callback_data
);
871 cbdataReferenceDone(def
);
875 * The helper program receives queries on stdin, one
876 * per line, and must return the result on on stdout
878 * General result syntax:
880 * OK/ERR keyword=value ...
884 * user= The users name (login)
885 * message= Message describing the reason
886 * tag= A string tag to be applied to the request that triggered the acl match.
887 * applies to both OK and ERR responses.
888 * Won't override existing request tags.
889 * log= A string to be used in access logging
891 * Other keywords may be added to the protocol later
893 * value needs to be URL-encoded or enclosed in double quotes (")
894 * with \-escaping on any whitespace, quotes, or slashes (\).
897 externalAclHandleReply(void *data
, const Helper::Reply
&reply
)
899 externalAclState
*state
= static_cast<externalAclState
*>(data
);
900 externalAclState
*next
;
901 ExternalACLEntryData entryData
;
902 entryData
.result
= ACCESS_DENIED
;
904 debugs(82, 2, HERE
<< "reply=" << reply
);
906 if (reply
.result
== Helper::Okay
)
907 entryData
.result
= ACCESS_ALLOWED
;
908 // XXX: handle other non-DENIED results better
910 // XXX: make entryData store a proper Helper::Reply object instead of copying.
912 entryData
.notes
.append(&reply
.notes
);
914 const char *label
= reply
.notes
.findFirst("tag");
915 if (label
!= NULL
&& *label
!= '\0')
916 entryData
.tag
= label
;
918 label
= reply
.notes
.findFirst("message");
919 if (label
!= NULL
&& *label
!= '\0')
920 entryData
.message
= label
;
922 label
= reply
.notes
.findFirst("log");
923 if (label
!= NULL
&& *label
!= '\0')
924 entryData
.log
= label
;
927 label
= reply
.notes
.findFirst("user");
928 if (label
!= NULL
&& *label
!= '\0')
929 entryData
.user
= label
;
931 label
= reply
.notes
.findFirst("password");
932 if (label
!= NULL
&& *label
!= '\0')
933 entryData
.password
= label
;
936 dlinkDelete(&state
->list
, &state
->def
->queue
);
938 ExternalACLEntryPointer entry
;
939 if (cbdataReferenceValid(state
->def
)) {
940 // only cache OK and ERR results.
941 if (reply
.result
== Helper::Okay
|| reply
.result
== Helper::Error
)
942 entry
= external_acl_cache_add(state
->def
, state
->key
, entryData
);
944 const ExternalACLEntryPointer oldentry
= static_cast<ExternalACLEntry
*>(hash_lookup(state
->def
->cache
, state
->key
));
946 if (oldentry
!= NULL
)
947 external_acl_cache_delete(state
->def
, oldentry
);
953 if (state
->callback
&& cbdataReferenceValidDone(state
->callback_data
, &cbdata
))
954 state
->callback(cbdata
, entry
);
966 ACLExternal::ExternalAclLookup(ACLChecklist
*checklist
, ACLExternal
* me
)
968 ExternalACLLookup::Start(checklist
, me
->data
, false);
972 ExternalACLLookup::Start(ACLChecklist
*checklist
, external_acl_data
*acl
, bool inBackground
)
974 external_acl
*def
= acl
->def
;
976 ACLFilledChecklist
*ch
= Filled(checklist
);
977 const char *key
= makeExternalAclKey(ch
, acl
);
978 assert(key
); // XXX: will fail if EXT_ACL_IDENT case needs an async lookup
980 debugs(82, 2, HERE
<< (inBackground
? "bg" : "fg") << " lookup in '" <<
981 def
->name
<< "' for '" << key
<< "'");
983 /* Check for a pending lookup to hook into */
984 // only possible if we are caching results.
985 externalAclState
*oldstate
= NULL
;
986 if (def
->cache_size
> 0) {
987 for (dlink_node
*node
= def
->queue
.head
; node
; node
= node
->next
) {
988 externalAclState
*oldstatetmp
= static_cast<externalAclState
*>(node
->data
);
990 if (strcmp(key
, oldstatetmp
->key
) == 0) {
991 oldstate
= oldstatetmp
;
997 // A background refresh has no need to piggiback on a pending request:
998 // When the pending request completes, the cache will be refreshed anyway.
999 if (oldstate
&& inBackground
) {
1000 debugs(82, 7, HERE
<< "'" << def
->name
<< "' queue is already being refreshed (ch=" << ch
<< ")");
1004 externalAclState
*state
= new externalAclState(def
, key
);
1006 if (!inBackground
) {
1007 state
->callback
= &ExternalACLLookup::LookupDone
;
1008 state
->callback_data
= cbdataReference(checklist
);
1012 /* Hook into pending lookup */
1013 state
->queue
= oldstate
->queue
;
1014 oldstate
->queue
= state
;
1016 /* No pending lookup found. Sumbit to helper */
1020 buf
.appendf("%s\n", key
);
1021 debugs(82, 4, "externalAclLookup: looking up for '" << key
<< "' in '" << def
->name
<< "'.");
1023 if (!def
->theHelper
->trySubmit(buf
.buf
, externalAclHandleReply
, state
)) {
1024 debugs(82, 7, HERE
<< "'" << def
->name
<< "' submit to helper failed");
1025 assert(inBackground
); // or the caller should have checked
1030 dlinkAdd(state
, &state
->list
, &def
->queue
);
1035 debugs(82, 4, "externalAclLookup: will wait for the result of '" << key
<<
1036 "' in '" << def
->name
<< "' (ch=" << ch
<< ").");
1040 externalAclStats(StoreEntry
* sentry
)
1042 for (external_acl
*p
= Config
.externalAclHelperList
; p
; p
= p
->next
) {
1043 storeAppendPrintf(sentry
, "External ACL Statistics: %s\n", p
->name
);
1044 storeAppendPrintf(sentry
, "Cache size: %d\n", p
->cache
->count
);
1045 assert(p
->theHelper
);
1046 p
->theHelper
->packStatsInto(sentry
);
1047 storeAppendPrintf(sentry
, "\n");
1052 externalAclRegisterWithCacheManager(void)
1054 Mgr::RegisterAction("external_acl",
1055 "External ACL stats",
1056 externalAclStats
, 0, 1);
1060 externalAclInit(void)
1062 for (external_acl
*p
= Config
.externalAclHelperList
; p
; p
= p
->next
) {
1064 p
->cache
= hash_create((HASHCMP
*) strcmp
, hashPrime(1024), hash4
);
1067 p
->theHelper
= new helper(p
->name
);
1069 p
->theHelper
->cmdline
= p
->cmdline
;
1071 p
->theHelper
->childs
.updateLimits(p
->children
);
1073 p
->theHelper
->ipc_type
= IPC_TCP_SOCKET
;
1075 p
->theHelper
->addr
= p
->local_addr
;
1077 helperOpenServers(p
->theHelper
);
1080 externalAclRegisterWithCacheManager();
1084 externalAclShutdown(void)
1088 for (p
= Config
.externalAclHelperList
; p
; p
= p
->next
) {
1089 helperShutdown(p
->theHelper
);
1093 ExternalACLLookup
ExternalACLLookup::instance_
;
1095 ExternalACLLookup::Instance()
1101 ExternalACLLookup::checkForAsync(ACLChecklist
*checklist
)const
1103 /* TODO: optimise this - we probably have a pointer to this
1104 * around somewhere */
1105 ACL
*acl
= ACL::FindByName(AclMatchedName
);
1107 ACLExternal
*me
= dynamic_cast<ACLExternal
*> (acl
);
1109 ACLExternal::ExternalAclLookup(checklist
, me
);
1112 /// Called when an async lookup returns
1114 ExternalACLLookup::LookupDone(void *data
, const ExternalACLEntryPointer
&result
)
1116 ACLFilledChecklist
*checklist
= Filled(static_cast<ACLChecklist
*>(data
));
1117 checklist
->extacl_entry
= result
;
1118 checklist
->resumeNonBlockingCheck(ExternalACLLookup::Instance());
1121 /* This registers "external" in the registry. To do dynamic definitions
1122 * of external ACL's, rather than a static prototype, have a Prototype instance
1123 * prototype in the class that defines each external acl 'class'.
1124 * Then, then the external acl instance is created, it self registers under
1126 * Be sure that clone is fully functional for that acl class though!
1128 ACL::Prototype
ACLExternal::RegistryProtoype(&ACLExternal::RegistryEntry_
, "external");
1130 ACLExternal
ACLExternal::RegistryEntry_("external");
1133 ACLExternal::clone() const
1135 return new ACLExternal(*this);
1138 ACLExternal::ACLExternal(char const *theClass
) : data(NULL
), class_(xstrdup(theClass
))
1141 ACLExternal::ACLExternal(ACLExternal
const & old
) : data(NULL
), class_(old
.class_
? xstrdup(old
.class_
) : NULL
)
1143 /* we don't have copy constructors for the data yet */
1148 ACLExternal::typeString() const
1154 ACLExternal::isProxyAuth() const
1157 return data
->def
->require_auth
;