5 * DEBUG: section 82 External ACL
6 * AUTHOR: Henrik Nordstrom, MARA Systems AB
8 * SQUID Web Proxy Cache http://www.squid-cache.org/
9 * ----------------------------------------------------------
11 * The contents of this file is Copyright (C) 2002 by MARA Systems AB,
12 * Sweden, unless otherwise is indicated in the specific function. The
13 * author gives his full permission to include this file into the Squid
14 * software product under the terms of the GNU General Public License as
15 * published by the Free Software Foundation; either version 2 of the
16 * License, or (at your option) any later version.
18 * Squid is the result of efforts by numerous individuals from
19 * the Internet community; see the CONTRIBUTORS file for full
20 * details. Many organizations have provided support for Squid's
21 * development; see the SPONSORS file for full details. Squid is
22 * Copyrighted (C) 2001 by the Regents of the University of
23 * California; see the COPYRIGHT file for full details. Squid
24 * incorporates software developed and/or copyrighted by other
25 * sources; see the CREDITS file for full details.
27 * This program is free software; you can redistribute it and/or modify
28 * it under the terms of the GNU General Public License as published by
29 * the Free Software Foundation; either version 2 of the License, or
30 * (at your option) any later version.
32 * This program is distributed in the hope that it will be useful,
33 * but WITHOUT ANY WARRANTY; without even the implied warranty of
34 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
35 * GNU General Public License for more details.
37 * You should have received a copy of the GNU General Public License
38 * along with this program; if not, write to the Free Software
39 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
44 #include "CacheManager.h"
45 #include "ExternalACL.h"
46 #include "ExternalACLEntry.h"
47 #include "AuthUserRequest.h"
48 #include "SquidTime.h"
51 #include "ACLChecklist.h"
56 #include "client_side.h"
57 #include "HttpRequest.h"
58 #include "HttpReply.h"
59 #include "authenticate.h"
62 #include "URLScheme.h"
65 #ifndef DEFAULT_EXTERNAL_ACL_TTL
66 #define DEFAULT_EXTERNAL_ACL_TTL 1 * 60 * 60
68 #ifndef DEFAULT_EXTERNAL_ACL_CHILDREN
69 #define DEFAULT_EXTERNAL_ACL_CHILDREN 5
72 typedef struct _external_acl_format external_acl_format
;
74 static char *makeExternalAclKey(ACLChecklist
* ch
, external_acl_data
* acl_data
);
75 static void external_acl_cache_delete(external_acl
* def
, external_acl_entry
* entry
);
76 static int external_acl_entry_expired(external_acl
* def
, external_acl_entry
* entry
);
77 static int external_acl_grace_expired(external_acl
* def
, external_acl_entry
* entry
);
78 static void external_acl_cache_touch(external_acl
* def
, external_acl_entry
* entry
);
79 static external_acl_entry
*external_acl_cache_add(external_acl
* def
, const char *key
, ExternalACLEntryData
const &data
);
81 /******************************************************************
82 * external_acl directive
104 external_acl_format
*format
;
127 QUOTE_METHOD_SHELL
= 1,
131 IpAddress local_addr
;
134 struct _external_acl_format
{
152 EXT_ACL_HEADER_REQUEST
,
153 EXT_ACL_HEADER_REQUEST_MEMBER
,
154 EXT_ACL_HEADER_REQUEST_ID
,
155 EXT_ACL_HEADER_REQUEST_ID_MEMBER
,
157 EXT_ACL_HEADER_REPLY
,
158 EXT_ACL_HEADER_REPLY_MEMBER
,
159 EXT_ACL_HEADER_REPLY_ID
,
160 EXT_ACL_HEADER_REPLY_ID_MEMBER
,
165 EXT_ACL_USER_CERT_RAW
,
166 EXT_ACL_USER_CERTCHAIN_RAW
,
171 external_acl_format
*next
;
175 http_hdr_type header_id
;
178 /* FIXME: These are not really cbdata, but it is an easy way
179 * to get them pooled, refcounted, accounted and freed properly...
181 CBDATA_TYPE(external_acl
);
182 CBDATA_TYPE(external_acl_format
);
185 free_external_acl_format(void *data
)
187 external_acl_format
*p
= static_cast<external_acl_format
*>(data
);
188 safe_free(p
->header
);
192 free_external_acl(void *data
)
194 external_acl
*p
= static_cast<external_acl
*>(data
);
198 external_acl_format
*f
= p
->format
;
203 wordlistDestroy(&p
->cmdline
);
206 helperShutdown(p
->theHelper
);
207 helperFree(p
->theHelper
);
211 while (p
->lru_list
.tail
)
212 external_acl_cache_delete(p
, static_cast<external_acl_entry
*>(p
->lru_list
.tail
->data
));
214 hashFreeMemory(p
->cache
);
218 * Parse the External ACL format %<{.*} and %>{.*} token(s) to pass a specific
219 * request or reply header to external helper.
221 \param header - the token being parsed (without the identifying prefix)
222 \param type - format enum identifier for this element, pulled from identifying prefix
223 \param format - structure to contain all the info about this format element.
226 parse_header_token(external_acl_format
*format
, char *header
, const _external_acl_format::format_type type
)
231 /** Cut away the closing brace */
232 end
= strchr(header
, '}');
233 if (end
&& strlen(end
) == 1)
238 member
= strchr(header
, ':');
241 /* Split in header and member */
244 if (!xisalnum(*member
))
245 format
->separator
= *member
++;
247 format
->separator
= ',';
249 format
->member
= xstrdup(member
);
251 if (type
== _external_acl_format::EXT_ACL_HEADER_REQUEST
)
252 format
->type
= _external_acl_format::EXT_ACL_HEADER_REQUEST_MEMBER
;
254 format
->type
= _external_acl_format::EXT_ACL_HEADER_REQUEST_MEMBER
;
259 format
->header
= xstrdup(header
);
260 format
->header_id
= httpHeaderIdByNameDef(header
, strlen(header
));
262 if (format
->header_id
!= -1) {
264 if (type
== _external_acl_format::EXT_ACL_HEADER_REQUEST
)
265 format
->type
= _external_acl_format::EXT_ACL_HEADER_REQUEST_ID_MEMBER
;
267 format
->type
= _external_acl_format::EXT_ACL_HEADER_REPLY_ID_MEMBER
;
269 if (type
== _external_acl_format::EXT_ACL_HEADER_REQUEST
)
270 format
->type
= _external_acl_format::EXT_ACL_HEADER_REQUEST_ID
;
272 format
->type
= _external_acl_format::EXT_ACL_HEADER_REPLY_ID
;
278 parse_externalAclHelper(external_acl
** list
)
282 external_acl_format
**p
;
284 CBDATA_INIT_TYPE_FREECB(external_acl
, free_external_acl
);
285 CBDATA_INIT_TYPE_FREECB(external_acl_format
, free_external_acl_format
);
287 a
= cbdataAlloc(external_acl
);
290 a
->ttl
= DEFAULT_EXTERNAL_ACL_TTL
;
291 a
->negative_ttl
= -1;
292 a
->children
= DEFAULT_EXTERNAL_ACL_CHILDREN
;
293 a
->local_addr
.SetLocalhost();
294 a
->quote
= external_acl::QUOTE_METHOD_URL
;
297 token
= strtok(NULL
, w_space
);
302 a
->name
= xstrdup(token
);
304 token
= strtok(NULL
, w_space
);
308 if (strncmp(token
, "ttl=", 4) == 0) {
309 a
->ttl
= atoi(token
+ 4);
310 } else if (strncmp(token
, "negative_ttl=", 13) == 0) {
311 a
->negative_ttl
= atoi(token
+ 13);
312 } else if (strncmp(token
, "children=", 9) == 0) {
313 a
->children
= atoi(token
+ 9);
314 } else if (strncmp(token
, "concurrency=", 12) == 0) {
315 a
->concurrency
= atoi(token
+ 12);
316 } else if (strncmp(token
, "cache=", 6) == 0) {
317 a
->cache_size
= atoi(token
+ 6);
318 } else if (strncmp(token
, "grace=", 6) == 0) {
319 a
->grace
= atoi(token
+ 6);
320 } else if (strcmp(token
, "protocol=2.5") == 0) {
321 a
->quote
= external_acl::QUOTE_METHOD_SHELL
;
322 } else if (strcmp(token
, "protocol=3.0") == 0) {
323 a
->quote
= external_acl::QUOTE_METHOD_URL
;
324 } else if (strcmp(token
, "quote=url") == 0) {
325 a
->quote
= external_acl::QUOTE_METHOD_URL
;
326 } else if (strcmp(token
, "quote=shell") == 0) {
327 a
->quote
= external_acl::QUOTE_METHOD_SHELL
;
329 /* INET6: allow admin to configure some helpers explicitly to
330 bind to IPv4/v6 localhost port. */
331 } else if (strcmp(token
, "ipv4") == 0) {
332 if ( !a
->local_addr
.SetIPv4() ) {
333 debugs(3, 0, "WARNING: Error converting " << a
->local_addr
<< " to IPv4 in " << a
->name
);
335 } else if (strcmp(token
, "ipv6") == 0) {
337 debugs(3, 0, "WARNING: --enable-ipv6 required for external ACL helpers to use IPv6: " << a
->name
);
345 token
= strtok(NULL
, w_space
);
348 if (a
->negative_ttl
== -1)
349 a
->negative_ttl
= a
->ttl
;
355 external_acl_format
*format
;
357 /* stop on first non-format token found */
362 format
= cbdataAlloc(external_acl_format
);
364 if (strncmp(token
, "%{", 2) == 0) {
365 // deprecated. but assume the old configs all referred to request headers.
366 debugs(82, DBG_IMPORTANT
, "WARNING: external_acl_type format %{...} is being replaced by %>{...} for : " << token
);
367 parse_header_token(format
, (token
+2), _external_acl_format::EXT_ACL_HEADER_REQUEST
);
368 } else if (strncmp(token
, "%>{", 3) == 0) {
369 parse_header_token(format
, (token
+3), _external_acl_format::EXT_ACL_HEADER_REQUEST
);
370 } else if (strncmp(token
, "%<{", 3) == 0) {
371 parse_header_token(format
, (token
+3), _external_acl_format::EXT_ACL_HEADER_REPLY
);
372 } else if (strcmp(token
, "%LOGIN") == 0) {
373 format
->type
= _external_acl_format::EXT_ACL_LOGIN
;
374 a
->require_auth
= true;
378 else if (strcmp(token
, "%IDENT") == 0)
379 format
->type
= _external_acl_format::EXT_ACL_IDENT
;
383 else if (strcmp(token
, "%SRC") == 0)
384 format
->type
= _external_acl_format::EXT_ACL_SRC
;
385 else if (strcmp(token
, "%SRCPORT") == 0)
386 format
->type
= _external_acl_format::EXT_ACL_SRCPORT
;
387 else if (strcmp(token
, "%MYADDR") == 0)
388 format
->type
= _external_acl_format::EXT_ACL_MYADDR
;
389 else if (strcmp(token
, "%MYPORT") == 0)
390 format
->type
= _external_acl_format::EXT_ACL_MYPORT
;
391 else if (strcmp(token
, "%URI") == 0)
392 format
->type
= _external_acl_format::EXT_ACL_URI
;
393 else if (strcmp(token
, "%DST") == 0)
394 format
->type
= _external_acl_format::EXT_ACL_DST
;
395 else if (strcmp(token
, "%PROTO") == 0)
396 format
->type
= _external_acl_format::EXT_ACL_PROTO
;
397 else if (strcmp(token
, "%PORT") == 0)
398 format
->type
= _external_acl_format::EXT_ACL_PORT
;
399 else if (strcmp(token
, "%PATH") == 0)
400 format
->type
= _external_acl_format::EXT_ACL_PATH
;
401 else if (strcmp(token
, "%METHOD") == 0)
402 format
->type
= _external_acl_format::EXT_ACL_METHOD
;
406 else if (strcmp(token
, "%USER_CERT") == 0)
407 format
->type
= _external_acl_format::EXT_ACL_USER_CERT_RAW
;
408 else if (strcmp(token
, "%USER_CERTCHAIN") == 0)
409 format
->type
= _external_acl_format::EXT_ACL_USER_CERTCHAIN_RAW
;
410 else if (strncmp(token
, "%USER_CERT_", 11) == 0) {
411 format
->type
= _external_acl_format::EXT_ACL_USER_CERT
;
412 format
->header
= xstrdup(token
+ 11);
413 } else if (strncmp(token
, "%CA_CERT_", 11) == 0) {
414 format
->type
= _external_acl_format::EXT_ACL_USER_CERT
;
415 format
->header
= xstrdup(token
+ 11);
419 else if (strcmp(token
, "%EXT_USER") == 0)
420 format
->type
= _external_acl_format::EXT_ACL_EXT_USER
;
427 token
= strtok(NULL
, w_space
);
430 /* There must be at least one format token */
438 wordlistAdd(&a
->cmdline
, token
);
441 parse_wordlist(&a
->cmdline
);
444 list
= &(*list
)->next
;
450 dump_externalAclHelper(StoreEntry
* sentry
, const char *name
, const external_acl
* list
)
452 const external_acl
*node
;
453 const external_acl_format
*format
;
454 const wordlist
*word
;
456 for (node
= list
; node
; node
= node
->next
) {
457 storeAppendPrintf(sentry
, "%s %s", name
, node
->name
);
459 if (!node
->local_addr
.IsIPv6())
460 storeAppendPrintf(sentry
, " ipv4");
462 storeAppendPrintf(sentry
, " ipv6");
464 if (node
->ttl
!= DEFAULT_EXTERNAL_ACL_TTL
)
465 storeAppendPrintf(sentry
, " ttl=%d", node
->ttl
);
467 if (node
->negative_ttl
!= node
->ttl
)
468 storeAppendPrintf(sentry
, " negative_ttl=%d", node
->negative_ttl
);
471 storeAppendPrintf(sentry
, " grace=%d", node
->grace
);
473 if (node
->children
!= DEFAULT_EXTERNAL_ACL_CHILDREN
)
474 storeAppendPrintf(sentry
, " children=%d", node
->children
);
476 if (node
->concurrency
)
477 storeAppendPrintf(sentry
, " concurrency=%d", node
->concurrency
);
480 storeAppendPrintf(sentry
, " cache=%d", node
->cache_size
);
482 for (format
= node
->format
; format
; format
= format
->next
) {
483 switch (format
->type
) {
485 case _external_acl_format::EXT_ACL_HEADER_REQUEST
:
486 case _external_acl_format::EXT_ACL_HEADER_REQUEST_ID
:
487 storeAppendPrintf(sentry
, " %%>{%s}", format
->header
);
490 case _external_acl_format::EXT_ACL_HEADER_REQUEST_MEMBER
:
491 case _external_acl_format::EXT_ACL_HEADER_REQUEST_ID_MEMBER
:
492 storeAppendPrintf(sentry
, " %%>{%s:%s}", format
->header
, format
->member
);
495 case _external_acl_format::EXT_ACL_HEADER_REPLY
:
496 case _external_acl_format::EXT_ACL_HEADER_REPLY_ID
:
497 storeAppendPrintf(sentry
, " %%<{%s}", format
->header
);
500 case _external_acl_format::EXT_ACL_HEADER_REPLY_MEMBER
:
501 case _external_acl_format::EXT_ACL_HEADER_REPLY_ID_MEMBER
:
502 storeAppendPrintf(sentry
, " %%<{%s:%s}", format
->header
, format
->member
);
504 #define DUMP_EXT_ACL_TYPE(a) \
505 case _external_acl_format::EXT_ACL_##a: \
506 storeAppendPrintf(sentry, " %%%s", #a); \
509 DUMP_EXT_ACL_TYPE(LOGIN
);
512 DUMP_EXT_ACL_TYPE(IDENT
);
515 DUMP_EXT_ACL_TYPE(SRC
);
516 DUMP_EXT_ACL_TYPE(SRCPORT
);
517 DUMP_EXT_ACL_TYPE(MYADDR
);
518 DUMP_EXT_ACL_TYPE(MYPORT
);
519 DUMP_EXT_ACL_TYPE(URI
);
520 DUMP_EXT_ACL_TYPE(DST
);
521 DUMP_EXT_ACL_TYPE(PROTO
);
522 DUMP_EXT_ACL_TYPE(PORT
);
523 DUMP_EXT_ACL_TYPE(PATH
);
524 DUMP_EXT_ACL_TYPE(METHOD
);
527 case _external_acl_format::EXT_ACL_USER_CERT_RAW
:
528 storeAppendPrintf(sentry
, " %%USER_CERT");
531 case _external_acl_format::EXT_ACL_USER_CERTCHAIN_RAW
:
532 storeAppendPrintf(sentry
, " %%USER_CERTCHAIN");
535 case _external_acl_format::EXT_ACL_USER_CERT
:
536 storeAppendPrintf(sentry
, " %%USER_CERT_%s", format
->header
);
539 case _external_acl_format::EXT_ACL_CA_CERT
:
540 storeAppendPrintf(sentry
, " %%USER_CERT_%s", format
->header
);
544 DUMP_EXT_ACL_TYPE(EXT_USER
);
547 fatal("unknown external_acl format error");
552 for (word
= node
->cmdline
; word
; word
= word
->next
)
553 storeAppendPrintf(sentry
, " %s", word
->key
);
555 storeAppendPrintf(sentry
, "\n");
560 free_externalAclHelper(external_acl
** list
)
563 external_acl
*node
= *list
;
570 static external_acl
*
571 find_externalAclHelper(const char *name
)
575 for (node
= Config
.externalAclHelperList
; node
; node
= node
->next
) {
576 if (strcmp(node
->name
, name
) == 0)
586 (ExternalACLEntry
*anEntry
)
589 assert (anEntry
->def
== NULL
);
591 hash_join(cache
, anEntry
);
592 dlinkAdd(anEntry
, &anEntry
->lru
, &lru_list
);
597 external_acl::trimCache()
599 if (cache_size
&& cache_entries
>= cache_size
)
600 external_acl_cache_delete(this, static_cast<external_acl_entry
*>(lru_list
.tail
->data
));
604 /******************************************************************
608 struct _external_acl_data
{
613 CBDATA_TYPE(external_acl_data
);
615 free_external_acl_data(void *data
)
617 external_acl_data
*p
= static_cast<external_acl_data
*>(data
);
618 wordlistDestroy(&p
->arguments
);
619 cbdataReferenceDone(p
->def
);
630 CBDATA_INIT_TYPE_FREECB(external_acl_data
, free_external_acl_data
);
632 data
= cbdataAlloc(external_acl_data
);
634 token
= strtok(NULL
, w_space
);
639 data
->def
= cbdataReference(find_externalAclHelper(token
));
644 while ((token
= strtokFile())) {
645 wordlistAdd(&data
->arguments
, token
);
650 ACLExternal::valid () const
652 if (data
->def
->require_auth
) {
653 if (authenticateSchemeCount() == 0) {
654 debugs(28, 0, "Can't use proxy auth because no authentication schemes were compiled.");
658 if (authenticateActiveSchemeCount() == 0) {
659 debugs(28, 0, "Can't use proxy auth because no authentication schemes are fully configured.");
668 ACLExternal::empty () const
673 ACLExternal::~ACLExternal()
680 aclMatchExternal(external_acl_data
*acl
, ACLChecklist
* ch
);
682 aclMatchExternal(external_acl_data
*acl
, ACLChecklist
* ch
)
685 external_acl_entry
*entry
;
686 const char *key
= "";
687 debugs(82, 9, "aclMatchExternal: acl=\"" << acl
->def
->name
<< "\"");
688 entry
= ch
->extacl_entry
;
691 if (cbdataReferenceValid(entry
) && entry
->def
== acl
->def
&&
692 strcmp((char *)entry
->key
, key
) == 0) {
695 /* Not valid, or not ours.. get rid of it */
696 cbdataReferenceDone(ch
->extacl_entry
);
701 external_acl_message
= "MISSING REQUIRED INFORMATION";
704 if (acl
->def
->require_auth
) {
706 /* Make sure the user is authenticated */
708 if ((ti
= ch
->authenticated()) != 1) {
709 debugs(82, 2, "aclMatchExternal: " << acl
->def
->name
<< " user not authenticated (" << ti
<< ")");
714 key
= makeExternalAclKey(ch
, acl
);
716 if (acl
->def
->require_auth
)
717 AUTHUSERREQUESTUNLOCK(ch
->auth_user_request
, "ACLChecklist via aclMatchExternal");
720 /* Not sufficient data to process */
724 entry
= static_cast<external_acl_entry
*>(hash_lookup(acl
->def
->cache
, key
));
726 if (!entry
|| external_acl_grace_expired(acl
->def
, entry
)) {
727 debugs(82, 2, "aclMatchExternal: " << acl
->def
->name
<< "(\"" << key
<< "\") = lookup needed");
728 debugs(82, 2, "aclMatchExternal: \"" << key
<< "\": entry=@" <<
729 entry
<< ", age=" << (entry
? (long int) squid_curtime
- entry
->date
: 0));
731 if (acl
->def
->theHelper
->stats
.queue_size
<= acl
->def
->theHelper
->n_running
) {
732 debugs(82, 2, "aclMatchExternal: \"" << key
<< "\": queueing a call.");
733 ch
->changeState (ExternalACLLookup::Instance());
736 debugs(82, 2, "aclMatchExternal: \"" << key
<< "\": return -1.");
741 debugs(82, 1, "aclMatchExternal: '" << acl
->def
->name
<<
742 "' queue overload. Request rejected '" << key
<< "'.");
743 external_acl_message
= "SYSTEM TOO BUSY, TRY AGAIN LATER";
746 debugs(82, 1, "aclMatchExternal: '" << acl
->def
->name
<<
747 "' queue overload. Using stale result. '" << key
<< "'.");
748 /* Fall thru to processing below */
754 external_acl_cache_touch(acl
->def
, entry
);
755 result
= entry
->result
;
756 external_acl_message
= entry
->message
.unsafeBuf();
758 debugs(82, 2, "aclMatchExternal: " << acl
->def
->name
<< " = " << result
);
761 if (entry
->user
.size())
762 ch
->request
->extacl_user
= entry
->user
;
764 if (entry
->password
.size())
765 ch
->request
->extacl_passwd
= entry
->password
;
767 if (!ch
->request
->tag
.size())
768 ch
->request
->tag
= entry
->tag
;
770 if (entry
->log
.size())
771 ch
->request
->extacl_log
= entry
->log
;
778 ACLExternal::match(ACLChecklist
*checklist
)
780 return aclMatchExternal (data
, checklist
);
784 ACLExternal::dump() const
786 external_acl_data
const *acl
= data
;
787 wordlist
*result
= NULL
;
791 mb
.Printf("%s", acl
->def
->name
);
793 for (arg
= acl
->arguments
; arg
; arg
= arg
->next
) {
794 mb
.Printf(" %s", arg
->key
);
797 wordlistAdd(&result
, mb
.buf
);
802 /******************************************************************
807 external_acl_cache_touch(external_acl
* def
, external_acl_entry
* entry
)
809 dlinkDelete(&entry
->lru
, &def
->lru_list
);
810 dlinkAdd(entry
, &entry
->lru
, &def
->lru_list
);
814 makeExternalAclKey(ACLChecklist
* ch
, external_acl_data
* acl_data
)
820 external_acl_format
*format
;
821 HttpRequest
*request
= ch
->request
;
822 HttpReply
*reply
= ch
->reply
;
825 for (format
= acl_data
->def
->format
; format
; format
= format
->next
) {
826 const char *str
= NULL
;
829 switch (format
->type
) {
831 case _external_acl_format::EXT_ACL_LOGIN
:
832 assert (ch
->auth_user_request
);
833 str
= ch
->auth_user_request
->username();
837 case _external_acl_format::EXT_ACL_IDENT
:
841 ch
->changeState(IdentLookup::Instance());
848 case _external_acl_format::EXT_ACL_SRC
:
849 str
= ch
->src_addr
.NtoA(buf
,sizeof(buf
));
852 case _external_acl_format::EXT_ACL_SRCPORT
:
853 snprintf(buf
, sizeof(buf
), "%d", request
->client_addr
.GetPort());
857 case _external_acl_format::EXT_ACL_MYADDR
:
858 str
= request
->my_addr
.NtoA(buf
, sizeof(buf
));
861 case _external_acl_format::EXT_ACL_MYPORT
:
862 snprintf(buf
, sizeof(buf
), "%d", request
->my_addr
.GetPort());
866 case _external_acl_format::EXT_ACL_URI
:
867 str
= urlCanonical(request
);
870 case _external_acl_format::EXT_ACL_DST
:
871 str
= request
->GetHost();
874 case _external_acl_format::EXT_ACL_PROTO
:
875 str
= ProtocolStr
[request
->protocol
];
878 case _external_acl_format::EXT_ACL_PORT
:
879 snprintf(buf
, sizeof(buf
), "%d", request
->port
);
883 case _external_acl_format::EXT_ACL_PATH
:
884 str
= request
->urlpath
.termedBuf();
887 case _external_acl_format::EXT_ACL_METHOD
:
888 str
= RequestMethodStr(request
->method
);
891 case _external_acl_format::EXT_ACL_HEADER_REQUEST
:
892 sb
= request
->header
.getByName(format
->header
);
893 str
= sb
.termedBuf();
896 case _external_acl_format::EXT_ACL_HEADER_REQUEST_ID
:
897 sb
= request
->header
.getStrOrList(format
->header_id
);
898 str
= sb
.termedBuf();
901 case _external_acl_format::EXT_ACL_HEADER_REQUEST_MEMBER
:
902 sb
= request
->header
.getByNameListMember(format
->header
, format
->member
, format
->separator
);
903 str
= sb
.termedBuf();
906 case _external_acl_format::EXT_ACL_HEADER_REQUEST_ID_MEMBER
:
907 sb
= request
->header
.getListMember(format
->header_id
, format
->member
, format
->separator
);
908 str
= sb
.termedBuf();
911 case _external_acl_format::EXT_ACL_HEADER_REPLY
:
913 sb
= reply
->header
.getByName(format
->header
);
914 str
= sb
.termedBuf();
918 case _external_acl_format::EXT_ACL_HEADER_REPLY_ID
:
920 sb
= reply
->header
.getStrOrList(format
->header_id
);
921 str
= sb
.termedBuf();
925 case _external_acl_format::EXT_ACL_HEADER_REPLY_MEMBER
:
927 sb
= reply
->header
.getByNameListMember(format
->header
, format
->member
, format
->separator
);
928 str
= sb
.termedBuf();
932 case _external_acl_format::EXT_ACL_HEADER_REPLY_ID_MEMBER
:
934 sb
= reply
->header
.getListMember(format
->header_id
, format
->member
, format
->separator
);
935 str
= sb
.termedBuf();
940 case _external_acl_format::EXT_ACL_USER_CERT_RAW
:
942 if (ch
->conn() != NULL
) {
943 SSL
*ssl
= fd_table
[ch
->conn()->fd
].ssl
;
946 str
= sslGetUserCertificatePEM(ssl
);
951 case _external_acl_format::EXT_ACL_USER_CERTCHAIN_RAW
:
953 if (ch
->conn() != NULL
) {
954 SSL
*ssl
= fd_table
[ch
->conn()->fd
].ssl
;
957 str
= sslGetUserCertificateChainPEM(ssl
);
962 case _external_acl_format::EXT_ACL_USER_CERT
:
964 if (ch
->conn() != NULL
) {
965 SSL
*ssl
= fd_table
[ch
->conn()->fd
].ssl
;
968 str
= sslGetUserAttribute(ssl
, format
->header
);
973 case _external_acl_format::EXT_ACL_CA_CERT
:
975 if (ch
->conn() != NULL
) {
976 SSL
*ssl
= fd_table
[ch
->conn()->fd
].ssl
;
979 str
= sslGetCAAttribute(ssl
, format
->header
);
985 case _external_acl_format::EXT_ACL_EXT_USER
:
986 str
= request
->extacl_user
.termedBuf();
989 case _external_acl_format::EXT_ACL_UNKNOWN
:
991 case _external_acl_format::EXT_ACL_END
:
992 fatal("unknown external_acl format error");
1006 if (acl_data
->def
->quote
== external_acl::QUOTE_METHOD_URL
) {
1007 const char *quoted
= rfc1738_escape(str
);
1008 mb
.append(quoted
, strlen(quoted
));
1010 strwordquote(&mb
, str
);
1018 for (arg
= acl_data
->arguments
; arg
; arg
= arg
->next
) {
1022 if (acl_data
->def
->quote
== external_acl::QUOTE_METHOD_URL
) {
1023 const char *quoted
= rfc1738_escape(arg
->key
);
1024 mb
.append(quoted
, strlen(quoted
));
1026 strwordquote(&mb
, arg
->key
);
1036 external_acl_entry_expired(external_acl
* def
, external_acl_entry
* entry
)
1038 if (entry
->date
+ (entry
->result
== 1 ? def
->ttl
: def
->negative_ttl
) < squid_curtime
)
1045 external_acl_grace_expired(external_acl
* def
, external_acl_entry
* entry
)
1048 ttl
= entry
->result
== 1 ? def
->ttl
: def
->negative_ttl
;
1049 ttl
= (ttl
* (100 - def
->grace
)) / 100;
1051 if (entry
->date
+ ttl
< squid_curtime
)
1057 static external_acl_entry
*
1058 external_acl_cache_add(external_acl
* def
, const char *key
, ExternalACLEntryData
const & data
)
1060 ExternalACLEntry
*entry
= static_cast<ExternalACLEntry
*>(hash_lookup(def
->cache
, key
));
1061 debugs(82, 2, "external_acl_cache_add: Adding '" << key
<< "' = " << data
.result
);
1064 debugs(82, 3, "ExternalACLEntry::update: updating existing entry");
1065 entry
->update (data
);
1066 external_acl_cache_touch(def
, entry
);
1071 entry
= new ExternalACLEntry
;
1072 entry
->key
= xstrdup(key
);
1073 entry
->update (data
);
1082 external_acl_cache_delete(external_acl
* def
, external_acl_entry
* entry
)
1084 assert (entry
->def
== def
);
1085 hash_remove_link(def
->cache
, entry
);
1086 dlinkDelete(&entry
->lru
, &def
->lru_list
);
1087 def
->cache_entries
-= 1;
1091 /******************************************************************
1092 * external_acl helpers
1095 typedef struct _externalAclState externalAclState
;
1097 struct _externalAclState
{
1099 void *callback_data
;
1103 externalAclState
*queue
;
1106 CBDATA_TYPE(externalAclState
);
1108 free_externalAclState(void *data
)
1110 externalAclState
*state
= static_cast<externalAclState
*>(data
);
1111 safe_free(state
->key
);
1112 cbdataReferenceDone(state
->callback_data
);
1113 cbdataReferenceDone(state
->def
);
1117 * The helper program receives queries on stdin, one
1118 * per line, and must return the result on on stdout
1120 * General result syntax:
1122 * OK/ERR keyword=value ...
1126 * user= The users name (login)
1127 * message= Message describing the reason
1128 * tag= A string tag to be applied to the request that triggered the acl match.
1129 * applies to both OK and ERR responses.
1130 * Won't override existing request tags.
1131 * log= A string to be used in access logging
1133 * Other keywords may be added to the protocol later
1135 * value needs to be enclosed in quotes if it may contain whitespace, or
1136 * the whitespace escaped using \ (\ escaping obviously also applies to
1141 externalAclHandleReply(void *data
, char *reply
)
1143 externalAclState
*state
= static_cast<externalAclState
*>(data
);
1144 externalAclState
*next
;
1149 ExternalACLEntryData entryData
;
1150 entryData
.result
= 0;
1151 external_acl_entry
*entry
= NULL
;
1153 debugs(82, 2, "externalAclHandleReply: reply=\"" << reply
<< "\"");
1156 status
= strwordtok(reply
, &t
);
1158 if (status
&& strcmp(status
, "OK") == 0)
1159 entryData
.result
= 1;
1161 while ((token
= strwordtok(NULL
, &t
))) {
1162 value
= strchr(token
, '=');
1165 *value
++ = '\0'; /* terminate the token, and move up to the value */
1167 if (state
->def
->quote
== external_acl::QUOTE_METHOD_URL
)
1168 rfc1738_unescape(value
);
1170 if (strcmp(token
, "user") == 0)
1171 entryData
.user
= value
;
1172 else if (strcmp(token
, "message") == 0)
1173 entryData
.message
= value
;
1174 else if (strcmp(token
, "error") == 0)
1175 entryData
.message
= value
;
1176 else if (strcmp(token
, "tag") == 0)
1177 entryData
.tag
= value
;
1178 else if (strcmp(token
, "log") == 0)
1179 entryData
.log
= value
;
1180 else if (strcmp(token
, "password") == 0)
1181 entryData
.password
= value
;
1182 else if (strcmp(token
, "passwd") == 0)
1183 entryData
.password
= value
;
1184 else if (strcmp(token
, "login") == 0)
1185 entryData
.user
= value
;
1190 dlinkDelete(&state
->list
, &state
->def
->queue
);
1192 if (cbdataReferenceValid(state
->def
)) {
1194 entry
= external_acl_cache_add(state
->def
, state
->key
, entryData
);
1196 external_acl_entry
*oldentry
= (external_acl_entry
*)hash_lookup(state
->def
->cache
, state
->key
);
1199 external_acl_cache_delete(state
->def
, oldentry
);
1205 cbdataReferenceDone(state
->def
);
1207 if (state
->callback
&& cbdataReferenceValidDone(state
->callback_data
, &cbdata
))
1208 state
->callback(cbdata
, entry
);
1210 next
= state
->queue
;
1219 ACLExternal::ExternalAclLookup(ACLChecklist
* ch
, ACLExternal
* me
, EAH
* callback
, void *callback_data
)
1222 external_acl_data
*acl
= me
->data
;
1223 external_acl
*def
= acl
->def
;
1224 externalAclState
*state
;
1226 externalAclState
*oldstate
= NULL
;
1229 if (acl
->def
->require_auth
) {
1231 /* Make sure the user is authenticated */
1233 if ((ti
= ch
->authenticated()) != 1) {
1234 debugs(82, 1, "externalAclLookup: " << acl
->def
->name
<<
1235 " user authentication failure (" << ti
<< ", ch=" << ch
<< ")");
1236 callback(callback_data
, NULL
);
1241 const char *key
= makeExternalAclKey(ch
, acl
);
1244 debugs(82, 1, "externalAclLookup: lookup in '" << def
->name
<<
1245 "', prerequisit failure (ch=" << ch
<< ")");
1246 callback(callback_data
, NULL
);
1250 debugs(82, 2, "externalAclLookup: lookup in '" << def
->name
<< "' for '" << key
<< "'");
1252 external_acl_entry
*entry
= static_cast<external_acl_entry
*>(hash_lookup(def
->cache
, key
));
1254 if (entry
&& external_acl_entry_expired(def
, entry
))
1257 /* Check for a pending lookup to hook into */
1258 for (node
= def
->queue
.head
; node
; node
= node
->next
) {
1259 externalAclState
*oldstatetmp
= static_cast<externalAclState
*>(node
->data
);
1261 if (strcmp(key
, oldstatetmp
->key
) == 0) {
1262 oldstate
= oldstatetmp
;
1267 if (entry
&& external_acl_grace_expired(def
, entry
)) {
1269 debugs(82, 4, "externalAclLookup: in grace period, but already pending lookup ('" << key
<< "', ch=" << ch
<< ")");
1270 callback(callback_data
, entry
);
1273 graceful
= 1; // grace expired, (neg)ttl did not, and we must start a new lookup.
1277 // The entry is in the cache, grace_ttl did not expired.
1278 if (!graceful
&& entry
&& !external_acl_grace_expired(def
, entry
)) {
1279 /* Should not really happen, but why not.. */
1280 callback(callback_data
, entry
);
1281 debugs(82, 4, "externalAclLookup: no lookup pending for '" << key
<< "', and grace not expired");
1282 debugs(82, 4, "externalAclLookup: (what tha' hell?)");
1286 /* No pending lookup found. Sumbit to helper */
1287 state
= cbdataAlloc(externalAclState
);
1289 state
->def
= cbdataReference(def
);
1291 state
->key
= xstrdup(key
);
1294 state
->callback
= callback
;
1295 state
->callback_data
= cbdataReference(callback_data
);
1299 /* Hook into pending lookup */
1300 state
->queue
= oldstate
->queue
;
1301 oldstate
->queue
= state
;
1303 /* Check for queue overload */
1305 if (def
->theHelper
->stats
.queue_size
>= def
->theHelper
->n_running
) {
1306 debugs(82, 1, "externalAclLookup: '" << def
->name
<< "' queue overload (ch=" << ch
<< ")");
1308 callback(callback_data
, entry
);
1312 /* Send it off to the helper */
1315 buf
.Printf("%s\n", key
);
1317 debugs(82, 4, "externalAclLookup: looking up for '" << key
<< "' in '" << def
->name
<< "'.");
1319 helperSubmit(def
->theHelper
, buf
.buf
, externalAclHandleReply
, state
);
1321 dlinkAdd(state
, &state
->list
, &def
->queue
);
1327 /* No need to wait during grace period */
1328 debugs(82, 4, "externalAclLookup: no need to wait for the result of '" <<
1329 key
<< "' in '" << def
->name
<< "' (ch=" << ch
<< ").");
1330 debugs(82, 4, "externalAclLookup: using cached entry " << entry
);
1332 if (entry
!= NULL
) {
1333 debugs(82, 4, "externalAclLookup: entry = { date=" <<
1334 (long unsigned int) entry
->date
<< ", result=" <<
1335 entry
->result
<< ", user=" << entry
->user
<< " tag=" <<
1336 entry
->tag
<< " log=" << entry
->log
<< " }");
1340 callback(callback_data
, entry
);
1344 debugs(82, 4, "externalAclLookup: will wait for the result of '" << key
<<
1345 "' in '" << def
->name
<< "' (ch=" << ch
<< ").");
1349 externalAclStats(StoreEntry
* sentry
)
1353 for (p
= Config
.externalAclHelperList
; p
; p
= p
->next
) {
1354 storeAppendPrintf(sentry
, "External ACL Statistics: %s\n", p
->name
);
1355 storeAppendPrintf(sentry
, "Cache size: %d\n", p
->cache
->count
);
1356 helperStats(sentry
, p
->theHelper
);
1357 storeAppendPrintf(sentry
, "\n");
1362 externalAclRegisterWithCacheManager(void)
1364 CacheManager::GetInstance()->
1365 registerAction("external_acl",
1366 "External ACL stats",
1367 externalAclStats
, 0, 1);
1371 externalAclInit(void)
1373 static int firstTimeInit
= 1;
1376 for (p
= Config
.externalAclHelperList
; p
; p
= p
->next
) {
1378 p
->cache
= hash_create((HASHCMP
*) strcmp
, hashPrime(1024), hash4
);
1381 p
->theHelper
= helperCreate(p
->name
);
1383 p
->theHelper
->cmdline
= p
->cmdline
;
1385 p
->theHelper
->n_to_start
= p
->children
;
1387 p
->theHelper
->concurrency
= p
->concurrency
;
1389 p
->theHelper
->ipc_type
= IPC_TCP_SOCKET
;
1391 p
->theHelper
->addr
= p
->local_addr
;
1393 helperOpenServers(p
->theHelper
);
1396 if (firstTimeInit
) {
1398 CBDATA_INIT_TYPE_FREECB(externalAclState
, free_externalAclState
);
1401 externalAclRegisterWithCacheManager();
1405 externalAclShutdown(void)
1409 for (p
= Config
.externalAclHelperList
; p
; p
= p
->next
) {
1410 helperShutdown(p
->theHelper
);
1414 ExternalACLLookup
ExternalACLLookup::instance_
;
1416 ExternalACLLookup::Instance()
1422 ExternalACLLookup::checkForAsync(ACLChecklist
*checklist
)const
1424 /* TODO: optimise this - we probably have a pointer to this
1425 * around somewhere */
1426 ACL
*acl
= ACL::FindByName(AclMatchedName
);
1428 ACLExternal
*me
= dynamic_cast<ACLExternal
*> (acl
);
1430 checklist
->asyncInProgress(true);
1431 ACLExternal::ExternalAclLookup(checklist
, me
, LookupDone
, checklist
);
1435 ExternalACLLookup::LookupDone(void *data
, void *result
)
1437 ACLChecklist
*checklist
= (ACLChecklist
*)data
;
1438 checklist
->extacl_entry
= cbdataReference((external_acl_entry
*)result
);
1439 checklist
->asyncInProgress(false);
1440 checklist
->changeState (ACLChecklist::NullState::Instance());
1444 /* This registers "external" in the registry. To do dynamic definitions
1445 * of external ACL's, rather than a static prototype, have a Prototype instance
1446 * prototype in the class that defines each external acl 'class'.
1447 * Then, then the external acl instance is created, it self registers under
1449 * Be sure that clone is fully functional for that acl class though!
1451 ACL::Prototype
ACLExternal::RegistryProtoype(&ACLExternal::RegistryEntry_
, "external");
1453 ACLExternal
ACLExternal::RegistryEntry_("external");
1456 ACLExternal::clone() const
1458 return new ACLExternal(*this);
1461 ACLExternal::ACLExternal (char const *theClass
) : data (NULL
), class_ (xstrdup (theClass
))
1464 ACLExternal::ACLExternal (ACLExternal
const & old
) : data (NULL
), class_ (old
.class_
? xstrdup (old
.class_
) : NULL
)
1466 /* we don't have copy constructors for the data yet */
1471 ACLExternal::typeString() const
1477 ACLExternal::isProxyAuth() const
1479 return data
->def
->require_auth
;