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 "auth/UserRequest.h"
48 #include "SquidTime.h"
51 #include "acl/FilledChecklist.h"
54 #include "ident/AclIdent.h"
57 #include "client_side.h"
58 #include "HttpRequest.h"
59 #include "HttpReply.h"
61 #include "auth/Gadgets.h"
65 #include "URLScheme.h"
68 #ifndef DEFAULT_EXTERNAL_ACL_TTL
69 #define DEFAULT_EXTERNAL_ACL_TTL 1 * 60 * 60
71 #ifndef DEFAULT_EXTERNAL_ACL_CHILDREN
72 #define DEFAULT_EXTERNAL_ACL_CHILDREN 5
75 typedef struct _external_acl_format external_acl_format
;
77 static char *makeExternalAclKey(ACLFilledChecklist
* ch
, external_acl_data
* acl_data
);
78 static void external_acl_cache_delete(external_acl
* def
, external_acl_entry
* entry
);
79 static int external_acl_entry_expired(external_acl
* def
, external_acl_entry
* entry
);
80 static int external_acl_grace_expired(external_acl
* def
, external_acl_entry
* entry
);
81 static void external_acl_cache_touch(external_acl
* def
, external_acl_entry
* entry
);
82 static external_acl_entry
*external_acl_cache_add(external_acl
* def
, const char *key
, ExternalACLEntryData
const &data
);
84 /******************************************************************
85 * external_acl directive
107 external_acl_format
*format
;
111 HelperChildConfig children
;
126 * Configuration flag. May only be altered by the configuration parser.
128 * Indicates that all uses of this external_acl_type helper require authentication
129 * details to be processed. If none are available its a fail match.
134 QUOTE_METHOD_SHELL
= 1,
138 Ip::Address local_addr
;
141 struct _external_acl_format
{
163 EXT_ACL_HEADER_REQUEST
,
164 EXT_ACL_HEADER_REQUEST_MEMBER
,
165 EXT_ACL_HEADER_REQUEST_ID
,
166 EXT_ACL_HEADER_REQUEST_ID_MEMBER
,
168 EXT_ACL_HEADER_REPLY
,
169 EXT_ACL_HEADER_REPLY_MEMBER
,
170 EXT_ACL_HEADER_REPLY_ID
,
171 EXT_ACL_HEADER_REPLY_ID_MEMBER
,
176 EXT_ACL_USER_CERT_RAW
,
177 EXT_ACL_USER_CERTCHAIN_RAW
,
182 external_acl_format
*next
;
186 http_hdr_type header_id
;
189 /* FIXME: These are not really cbdata, but it is an easy way
190 * to get them pooled, refcounted, accounted and freed properly...
192 CBDATA_TYPE(external_acl
);
193 CBDATA_TYPE(external_acl_format
);
196 free_external_acl_format(void *data
)
198 external_acl_format
*p
= static_cast<external_acl_format
*>(data
);
199 safe_free(p
->header
);
203 free_external_acl(void *data
)
205 external_acl
*p
= static_cast<external_acl
*>(data
);
209 external_acl_format
*f
= p
->format
;
214 wordlistDestroy(&p
->cmdline
);
217 helperShutdown(p
->theHelper
);
222 while (p
->lru_list
.tail
)
223 external_acl_cache_delete(p
, static_cast<external_acl_entry
*>(p
->lru_list
.tail
->data
));
225 hashFreeMemory(p
->cache
);
229 * Parse the External ACL format %<{.*} and %>{.*} token(s) to pass a specific
230 * request or reply header to external helper.
232 \param header - the token being parsed (without the identifying prefix)
233 \param type - format enum identifier for this element, pulled from identifying prefix
234 \param format - structure to contain all the info about this format element.
237 parse_header_token(external_acl_format
*format
, char *header
, const _external_acl_format::format_type type
)
242 /** Cut away the closing brace */
243 end
= strchr(header
, '}');
244 if (end
&& strlen(end
) == 1)
249 member
= strchr(header
, ':');
252 /* Split in header and member */
255 if (!xisalnum(*member
))
256 format
->separator
= *member
++;
258 format
->separator
= ',';
260 format
->member
= xstrdup(member
);
262 if (type
== _external_acl_format::EXT_ACL_HEADER_REQUEST
)
263 format
->type
= _external_acl_format::EXT_ACL_HEADER_REQUEST_MEMBER
;
265 format
->type
= _external_acl_format::EXT_ACL_HEADER_REQUEST_MEMBER
;
270 format
->header
= xstrdup(header
);
271 format
->header_id
= httpHeaderIdByNameDef(header
, strlen(header
));
273 if (format
->header_id
!= -1) {
275 if (type
== _external_acl_format::EXT_ACL_HEADER_REQUEST
)
276 format
->type
= _external_acl_format::EXT_ACL_HEADER_REQUEST_ID_MEMBER
;
278 format
->type
= _external_acl_format::EXT_ACL_HEADER_REPLY_ID_MEMBER
;
280 if (type
== _external_acl_format::EXT_ACL_HEADER_REQUEST
)
281 format
->type
= _external_acl_format::EXT_ACL_HEADER_REQUEST_ID
;
283 format
->type
= _external_acl_format::EXT_ACL_HEADER_REPLY_ID
;
289 parse_externalAclHelper(external_acl
** list
)
293 external_acl_format
**p
;
295 CBDATA_INIT_TYPE_FREECB(external_acl
, free_external_acl
);
296 CBDATA_INIT_TYPE_FREECB(external_acl_format
, free_external_acl_format
);
298 a
= cbdataAlloc(external_acl
);
301 a
->ttl
= DEFAULT_EXTERNAL_ACL_TTL
;
302 a
->negative_ttl
= -1;
303 a
->cache_size
= 256*1024;
304 a
->children
.n_max
= DEFAULT_EXTERNAL_ACL_CHILDREN
;
305 a
->children
.n_startup
= a
->children
.n_max
;
306 a
->children
.n_idle
= 1;
307 a
->local_addr
.SetLocalhost();
308 a
->quote
= external_acl::QUOTE_METHOD_URL
;
310 token
= strtok(NULL
, w_space
);
315 a
->name
= xstrdup(token
);
317 token
= strtok(NULL
, w_space
);
321 if (strncmp(token
, "ttl=", 4) == 0) {
322 a
->ttl
= atoi(token
+ 4);
323 } else if (strncmp(token
, "negative_ttl=", 13) == 0) {
324 a
->negative_ttl
= atoi(token
+ 13);
325 } else if (strncmp(token
, "children=", 9) == 0) {
326 a
->children
.n_max
= atoi(token
+ 9);
327 debugs(0, 0, "WARNING: external_acl_type option children=N has been deprecated in favor of children-max=N and children-startup=N");
328 } else if (strncmp(token
, "children-max=", 13) == 0) {
329 a
->children
.n_max
= atoi(token
+ 13);
330 } else if (strncmp(token
, "children-startup=", 17) == 0) {
331 a
->children
.n_startup
= atoi(token
+ 17);
332 } else if (strncmp(token
, "children-idle=", 14) == 0) {
333 a
->children
.n_idle
= atoi(token
+ 14);
334 } else if (strncmp(token
, "concurrency=", 12) == 0) {
335 a
->children
.concurrency
= atoi(token
+ 12);
336 } else if (strncmp(token
, "cache=", 6) == 0) {
337 a
->cache_size
= atoi(token
+ 6);
338 } else if (strncmp(token
, "grace=", 6) == 0) {
339 a
->grace
= atoi(token
+ 6);
340 } else if (strcmp(token
, "protocol=2.5") == 0) {
341 a
->quote
= external_acl::QUOTE_METHOD_SHELL
;
342 } else if (strcmp(token
, "protocol=3.0") == 0) {
343 a
->quote
= external_acl::QUOTE_METHOD_URL
;
344 } else if (strcmp(token
, "quote=url") == 0) {
345 a
->quote
= external_acl::QUOTE_METHOD_URL
;
346 } else if (strcmp(token
, "quote=shell") == 0) {
347 a
->quote
= external_acl::QUOTE_METHOD_SHELL
;
349 /* INET6: allow admin to configure some helpers explicitly to
350 bind to IPv4/v6 localhost port. */
351 } else if (strcmp(token
, "ipv4") == 0) {
352 if ( !a
->local_addr
.SetIPv4() ) {
353 debugs(3, 0, "WARNING: Error converting " << a
->local_addr
<< " to IPv4 in " << a
->name
);
355 } else if (strcmp(token
, "ipv6") == 0) {
357 debugs(3, 0, "WARNING: --enable-ipv6 required for external ACL helpers to use IPv6: " << a
->name
);
358 // else nothing to do.
363 token
= strtok(NULL
, w_space
);
366 /* check that child startup value is sane. */
367 if (a
->children
.n_startup
> a
->children
.n_max
)
368 a
->children
.n_startup
= a
->children
.n_max
;
370 /* check that child idle value is sane. */
371 if (a
->children
.n_idle
> a
->children
.n_max
)
372 a
->children
.n_idle
= a
->children
.n_max
;
373 if (a
->children
.n_idle
< 1)
374 a
->children
.n_idle
= 1;
376 if (a
->negative_ttl
== -1)
377 a
->negative_ttl
= a
->ttl
;
383 external_acl_format
*format
;
385 /* stop on first non-format token found */
390 format
= cbdataAlloc(external_acl_format
);
392 if (strncmp(token
, "%{", 2) == 0) {
393 // deprecated. but assume the old configs all referred to request headers.
394 debugs(82, DBG_IMPORTANT
, "WARNING: external_acl_type format %{...} is being replaced by %>{...} for : " << token
);
395 parse_header_token(format
, (token
+2), _external_acl_format::EXT_ACL_HEADER_REQUEST
);
396 } else if (strncmp(token
, "%>{", 3) == 0) {
397 parse_header_token(format
, (token
+3), _external_acl_format::EXT_ACL_HEADER_REQUEST
);
398 } else if (strncmp(token
, "%<{", 3) == 0) {
399 parse_header_token(format
, (token
+3), _external_acl_format::EXT_ACL_HEADER_REPLY
);
400 } else if (strcmp(token
, "%LOGIN") == 0) {
401 format
->type
= _external_acl_format::EXT_ACL_LOGIN
;
402 a
->require_auth
= true;
406 else if (strcmp(token
, "%IDENT") == 0)
407 format
->type
= _external_acl_format::EXT_ACL_IDENT
;
411 else if (strcmp(token
, "%SRC") == 0)
412 format
->type
= _external_acl_format::EXT_ACL_SRC
;
413 else if (strcmp(token
, "%SRCPORT") == 0)
414 format
->type
= _external_acl_format::EXT_ACL_SRCPORT
;
416 else if (strcmp(token
, "%SRCEUI48") == 0)
417 format
->type
= _external_acl_format::EXT_ACL_SRCEUI48
;
418 else if (strcmp(token
, "%SRCEUI64") == 0)
419 format
->type
= _external_acl_format::EXT_ACL_SRCEUI64
;
421 else if (strcmp(token
, "%MYADDR") == 0)
422 format
->type
= _external_acl_format::EXT_ACL_MYADDR
;
423 else if (strcmp(token
, "%MYPORT") == 0)
424 format
->type
= _external_acl_format::EXT_ACL_MYPORT
;
425 else if (strcmp(token
, "%URI") == 0)
426 format
->type
= _external_acl_format::EXT_ACL_URI
;
427 else if (strcmp(token
, "%DST") == 0)
428 format
->type
= _external_acl_format::EXT_ACL_DST
;
429 else if (strcmp(token
, "%PROTO") == 0)
430 format
->type
= _external_acl_format::EXT_ACL_PROTO
;
431 else if (strcmp(token
, "%PORT") == 0)
432 format
->type
= _external_acl_format::EXT_ACL_PORT
;
433 else if (strcmp(token
, "%PATH") == 0)
434 format
->type
= _external_acl_format::EXT_ACL_PATH
;
435 else if (strcmp(token
, "%METHOD") == 0)
436 format
->type
= _external_acl_format::EXT_ACL_METHOD
;
439 else if (strcmp(token
, "%USER_CERT") == 0)
440 format
->type
= _external_acl_format::EXT_ACL_USER_CERT_RAW
;
441 else if (strcmp(token
, "%USER_CERTCHAIN") == 0)
442 format
->type
= _external_acl_format::EXT_ACL_USER_CERTCHAIN_RAW
;
443 else if (strncmp(token
, "%USER_CERT_", 11) == 0) {
444 format
->type
= _external_acl_format::EXT_ACL_USER_CERT
;
445 format
->header
= xstrdup(token
+ 11);
446 } else if (strncmp(token
, "%CA_CERT_", 11) == 0) {
447 format
->type
= _external_acl_format::EXT_ACL_USER_CERT
;
448 format
->header
= xstrdup(token
+ 11);
451 else if (strcmp(token
, "%EXT_USER") == 0)
452 format
->type
= _external_acl_format::EXT_ACL_EXT_USER
;
454 debugs(0,0, "ERROR: Unknown Format token " << token
);
460 token
= strtok(NULL
, w_space
);
463 /* There must be at least one format token */
471 wordlistAdd(&a
->cmdline
, token
);
474 parse_wordlist(&a
->cmdline
);
477 list
= &(*list
)->next
;
483 dump_externalAclHelper(StoreEntry
* sentry
, const char *name
, const external_acl
* list
)
485 const external_acl
*node
;
486 const external_acl_format
*format
;
487 const wordlist
*word
;
489 for (node
= list
; node
; node
= node
->next
) {
490 storeAppendPrintf(sentry
, "%s %s", name
, node
->name
);
492 if (!node
->local_addr
.IsIPv6())
493 storeAppendPrintf(sentry
, " ipv4");
495 storeAppendPrintf(sentry
, " ipv6");
497 if (node
->ttl
!= DEFAULT_EXTERNAL_ACL_TTL
)
498 storeAppendPrintf(sentry
, " ttl=%d", node
->ttl
);
500 if (node
->negative_ttl
!= node
->ttl
)
501 storeAppendPrintf(sentry
, " negative_ttl=%d", node
->negative_ttl
);
504 storeAppendPrintf(sentry
, " grace=%d", node
->grace
);
506 if (node
->children
.n_max
!= DEFAULT_EXTERNAL_ACL_CHILDREN
)
507 storeAppendPrintf(sentry
, " children-max=%d", node
->children
.n_max
);
509 if (node
->children
.n_startup
!= 1)
510 storeAppendPrintf(sentry
, " children-startup=%d", node
->children
.n_startup
);
512 if (node
->children
.n_idle
!= (node
->children
.n_max
+ node
->children
.n_startup
) )
513 storeAppendPrintf(sentry
, " children-idle=%d", node
->children
.n_idle
);
515 if (node
->children
.concurrency
)
516 storeAppendPrintf(sentry
, " concurrency=%d", node
->children
.concurrency
);
519 storeAppendPrintf(sentry
, " cache=%d", node
->cache_size
);
521 for (format
= node
->format
; format
; format
= format
->next
) {
522 switch (format
->type
) {
524 case _external_acl_format::EXT_ACL_HEADER_REQUEST
:
525 case _external_acl_format::EXT_ACL_HEADER_REQUEST_ID
:
526 storeAppendPrintf(sentry
, " %%>{%s}", format
->header
);
529 case _external_acl_format::EXT_ACL_HEADER_REQUEST_MEMBER
:
530 case _external_acl_format::EXT_ACL_HEADER_REQUEST_ID_MEMBER
:
531 storeAppendPrintf(sentry
, " %%>{%s:%s}", format
->header
, format
->member
);
534 case _external_acl_format::EXT_ACL_HEADER_REPLY
:
535 case _external_acl_format::EXT_ACL_HEADER_REPLY_ID
:
536 storeAppendPrintf(sentry
, " %%<{%s}", format
->header
);
539 case _external_acl_format::EXT_ACL_HEADER_REPLY_MEMBER
:
540 case _external_acl_format::EXT_ACL_HEADER_REPLY_ID_MEMBER
:
541 storeAppendPrintf(sentry
, " %%<{%s:%s}", format
->header
, format
->member
);
543 #define DUMP_EXT_ACL_TYPE(a) \
544 case _external_acl_format::EXT_ACL_##a: \
545 storeAppendPrintf(sentry, " %%%s", #a); \
548 DUMP_EXT_ACL_TYPE(LOGIN
);
551 DUMP_EXT_ACL_TYPE(IDENT
);
554 DUMP_EXT_ACL_TYPE(SRC
);
555 DUMP_EXT_ACL_TYPE(SRCPORT
);
557 DUMP_EXT_ACL_TYPE(SRCEUI48
);
558 DUMP_EXT_ACL_TYPE(SRCEUI64
);
561 DUMP_EXT_ACL_TYPE(MYADDR
);
562 DUMP_EXT_ACL_TYPE(MYPORT
);
563 DUMP_EXT_ACL_TYPE(URI
);
564 DUMP_EXT_ACL_TYPE(DST
);
565 DUMP_EXT_ACL_TYPE(PROTO
);
566 DUMP_EXT_ACL_TYPE(PORT
);
567 DUMP_EXT_ACL_TYPE(PATH
);
568 DUMP_EXT_ACL_TYPE(METHOD
);
571 case _external_acl_format::EXT_ACL_USER_CERT_RAW
:
572 storeAppendPrintf(sentry
, " %%USER_CERT");
575 case _external_acl_format::EXT_ACL_USER_CERTCHAIN_RAW
:
576 storeAppendPrintf(sentry
, " %%USER_CERTCHAIN");
579 case _external_acl_format::EXT_ACL_USER_CERT
:
580 storeAppendPrintf(sentry
, " %%USER_CERT_%s", format
->header
);
583 case _external_acl_format::EXT_ACL_CA_CERT
:
584 storeAppendPrintf(sentry
, " %%USER_CERT_%s", format
->header
);
588 DUMP_EXT_ACL_TYPE(EXT_USER
);
591 fatal("unknown external_acl format error");
596 for (word
= node
->cmdline
; word
; word
= word
->next
)
597 storeAppendPrintf(sentry
, " %s", word
->key
);
599 storeAppendPrintf(sentry
, "\n");
604 free_externalAclHelper(external_acl
** list
)
607 external_acl
*node
= *list
;
614 static external_acl
*
615 find_externalAclHelper(const char *name
)
619 for (node
= Config
.externalAclHelperList
; node
; node
= node
->next
) {
620 if (strcmp(node
->name
, name
) == 0)
630 (ExternalACLEntry
*anEntry
)
633 assert (anEntry
->def
== NULL
);
635 hash_join(cache
, anEntry
);
636 dlinkAdd(anEntry
, &anEntry
->lru
, &lru_list
);
641 external_acl::trimCache()
643 if (cache_size
&& cache_entries
>= cache_size
)
644 external_acl_cache_delete(this, static_cast<external_acl_entry
*>(lru_list
.tail
->data
));
648 /******************************************************************
652 struct _external_acl_data
{
657 CBDATA_TYPE(external_acl_data
);
659 free_external_acl_data(void *data
)
661 external_acl_data
*p
= static_cast<external_acl_data
*>(data
);
662 wordlistDestroy(&p
->arguments
);
663 cbdataReferenceDone(p
->def
);
674 CBDATA_INIT_TYPE_FREECB(external_acl_data
, free_external_acl_data
);
676 data
= cbdataAlloc(external_acl_data
);
678 token
= strtok(NULL
, w_space
);
683 data
->def
= cbdataReference(find_externalAclHelper(token
));
688 while ((token
= strtokFile())) {
689 wordlistAdd(&data
->arguments
, token
);
694 ACLExternal::valid () const
696 if (data
->def
->require_auth
) {
697 if (authenticateSchemeCount() == 0) {
698 debugs(28, 0, "Can't use proxy auth because no authentication schemes were compiled.");
702 if (authenticateActiveSchemeCount() == 0) {
703 debugs(28, 0, "Can't use proxy auth because no authentication schemes are fully configured.");
712 ACLExternal::empty () const
717 ACLExternal::~ACLExternal()
724 aclMatchExternal(external_acl_data
*acl
, ACLFilledChecklist
*ch
)
727 external_acl_entry
*entry
;
728 const char *key
= "";
729 debugs(82, 9, "aclMatchExternal: acl=\"" << acl
->def
->name
<< "\"");
730 entry
= ch
->extacl_entry
;
733 if (cbdataReferenceValid(entry
) && entry
->def
== acl
->def
&&
734 strcmp((char *)entry
->key
, key
) == 0) {
737 /* Not valid, or not ours.. get rid of it */
738 cbdataReferenceDone(ch
->extacl_entry
);
743 external_acl_message
= "MISSING REQUIRED INFORMATION";
746 if (acl
->def
->require_auth
) {
748 /* Make sure the user is authenticated */
749 debugs(82, 3, "aclMatchExternal: " << acl
->def
->name
<< " check user authenticated.");
751 if ((ti
= AuthenticateAcl(ch
)) != 1) {
752 debugs(82, 2, "aclMatchExternal: " << acl
->def
->name
<< " user not authenticated (" << ti
<< ")");
755 debugs(82, 3, "aclMatchExternal: " << acl
->def
->name
<< " user is authenticated.");
758 key
= makeExternalAclKey(ch
, acl
);
761 /* Not sufficient data to process */
765 entry
= static_cast<external_acl_entry
*>(hash_lookup(acl
->def
->cache
, key
));
767 if (!entry
|| external_acl_grace_expired(acl
->def
, entry
)) {
768 debugs(82, 2, "aclMatchExternal: " << acl
->def
->name
<< "(\"" << key
<< "\") = lookup needed");
769 debugs(82, 2, "aclMatchExternal: \"" << key
<< "\": entry=@" <<
770 entry
<< ", age=" << (entry
? (long int) squid_curtime
- entry
->date
: 0));
772 if (acl
->def
->theHelper
->stats
.queue_size
<= (int)acl
->def
->theHelper
->childs
.n_active
) {
773 debugs(82, 2, "aclMatchExternal: \"" << key
<< "\": queueing a call.");
774 ch
->changeState (ExternalACLLookup::Instance());
777 debugs(82, 2, "aclMatchExternal: \"" << key
<< "\": return -1.");
782 debugs(82, 1, "aclMatchExternal: '" << acl
->def
->name
<<
783 "' queue overload. Request rejected '" << key
<< "'.");
784 external_acl_message
= "SYSTEM TOO BUSY, TRY AGAIN LATER";
787 debugs(82, 1, "aclMatchExternal: '" << acl
->def
->name
<<
788 "' queue overload. Using stale result. '" << key
<< "'.");
789 /* Fall thru to processing below */
795 external_acl_cache_touch(acl
->def
, entry
);
796 result
= entry
->result
;
797 external_acl_message
= entry
->message
.termedBuf();
799 debugs(82, 2, "aclMatchExternal: " << acl
->def
->name
<< " = " << result
);
802 if (entry
->user
.size())
803 ch
->request
->extacl_user
= entry
->user
;
805 if (entry
->password
.size())
806 ch
->request
->extacl_passwd
= entry
->password
;
808 if (!ch
->request
->tag
.size())
809 ch
->request
->tag
= entry
->tag
;
811 if (entry
->log
.size())
812 ch
->request
->extacl_log
= entry
->log
;
814 if (entry
->message
.size())
815 ch
->request
->extacl_message
= entry
->message
;
822 ACLExternal::match(ACLChecklist
*checklist
)
824 return aclMatchExternal (data
, Filled(checklist
));
828 ACLExternal::dump() const
830 external_acl_data
const *acl
= data
;
831 wordlist
*result
= NULL
;
835 mb
.Printf("%s", acl
->def
->name
);
837 for (arg
= acl
->arguments
; arg
; arg
= arg
->next
) {
838 mb
.Printf(" %s", arg
->key
);
841 wordlistAdd(&result
, mb
.buf
);
846 /******************************************************************
851 external_acl_cache_touch(external_acl
* def
, external_acl_entry
* entry
)
853 dlinkDelete(&entry
->lru
, &def
->lru_list
);
854 dlinkAdd(entry
, &entry
->lru
, &def
->lru_list
);
858 makeExternalAclKey(ACLFilledChecklist
* ch
, external_acl_data
* acl_data
)
864 external_acl_format
*format
;
865 HttpRequest
*request
= ch
->request
;
866 HttpReply
*reply
= ch
->reply
;
869 for (format
= acl_data
->def
->format
; format
; format
= format
->next
) {
870 const char *str
= NULL
;
873 switch (format
->type
) {
875 case _external_acl_format::EXT_ACL_LOGIN
:
876 assert (ch
->auth_user_request
!= NULL
);
877 str
= ch
->auth_user_request
->username();
881 case _external_acl_format::EXT_ACL_IDENT
:
885 ch
->changeState(IdentLookup::Instance());
892 case _external_acl_format::EXT_ACL_SRC
:
893 str
= ch
->src_addr
.NtoA(buf
,sizeof(buf
));
896 case _external_acl_format::EXT_ACL_SRCPORT
:
897 snprintf(buf
, sizeof(buf
), "%d", request
->client_addr
.GetPort());
902 case _external_acl_format::EXT_ACL_SRCEUI48
:
903 if (request
->client_eui48
.encode(buf
, sizeof(buf
)))
907 case _external_acl_format::EXT_ACL_SRCEUI64
:
908 if (request
->client_eui64
.encode(buf
, sizeof(buf
)))
913 case _external_acl_format::EXT_ACL_MYADDR
:
914 str
= request
->my_addr
.NtoA(buf
, sizeof(buf
));
917 case _external_acl_format::EXT_ACL_MYPORT
:
918 snprintf(buf
, sizeof(buf
), "%d", request
->my_addr
.GetPort());
922 case _external_acl_format::EXT_ACL_URI
:
923 str
= urlCanonical(request
);
926 case _external_acl_format::EXT_ACL_DST
:
927 str
= request
->GetHost();
930 case _external_acl_format::EXT_ACL_PROTO
:
931 str
= ProtocolStr
[request
->protocol
];
934 case _external_acl_format::EXT_ACL_PORT
:
935 snprintf(buf
, sizeof(buf
), "%d", request
->port
);
939 case _external_acl_format::EXT_ACL_PATH
:
940 str
= request
->urlpath
.termedBuf();
943 case _external_acl_format::EXT_ACL_METHOD
:
944 str
= RequestMethodStr(request
->method
);
947 case _external_acl_format::EXT_ACL_HEADER_REQUEST
:
948 sb
= request
->header
.getByName(format
->header
);
949 str
= sb
.termedBuf();
952 case _external_acl_format::EXT_ACL_HEADER_REQUEST_ID
:
953 sb
= request
->header
.getStrOrList(format
->header_id
);
954 str
= sb
.termedBuf();
957 case _external_acl_format::EXT_ACL_HEADER_REQUEST_MEMBER
:
958 sb
= request
->header
.getByNameListMember(format
->header
, format
->member
, format
->separator
);
959 str
= sb
.termedBuf();
962 case _external_acl_format::EXT_ACL_HEADER_REQUEST_ID_MEMBER
:
963 sb
= request
->header
.getListMember(format
->header_id
, format
->member
, format
->separator
);
964 str
= sb
.termedBuf();
967 case _external_acl_format::EXT_ACL_HEADER_REPLY
:
969 sb
= reply
->header
.getByName(format
->header
);
970 str
= sb
.termedBuf();
974 case _external_acl_format::EXT_ACL_HEADER_REPLY_ID
:
976 sb
= reply
->header
.getStrOrList(format
->header_id
);
977 str
= sb
.termedBuf();
981 case _external_acl_format::EXT_ACL_HEADER_REPLY_MEMBER
:
983 sb
= reply
->header
.getByNameListMember(format
->header
, format
->member
, format
->separator
);
984 str
= sb
.termedBuf();
988 case _external_acl_format::EXT_ACL_HEADER_REPLY_ID_MEMBER
:
990 sb
= reply
->header
.getListMember(format
->header_id
, format
->member
, format
->separator
);
991 str
= sb
.termedBuf();
996 case _external_acl_format::EXT_ACL_USER_CERT_RAW
:
998 if (ch
->conn() != NULL
) {
999 SSL
*ssl
= fd_table
[ch
->conn()->fd
].ssl
;
1002 str
= sslGetUserCertificatePEM(ssl
);
1007 case _external_acl_format::EXT_ACL_USER_CERTCHAIN_RAW
:
1009 if (ch
->conn() != NULL
) {
1010 SSL
*ssl
= fd_table
[ch
->conn()->fd
].ssl
;
1013 str
= sslGetUserCertificateChainPEM(ssl
);
1018 case _external_acl_format::EXT_ACL_USER_CERT
:
1020 if (ch
->conn() != NULL
) {
1021 SSL
*ssl
= fd_table
[ch
->conn()->fd
].ssl
;
1024 str
= sslGetUserAttribute(ssl
, format
->header
);
1029 case _external_acl_format::EXT_ACL_CA_CERT
:
1031 if (ch
->conn() != NULL
) {
1032 SSL
*ssl
= fd_table
[ch
->conn()->fd
].ssl
;
1035 str
= sslGetCAAttribute(ssl
, format
->header
);
1041 case _external_acl_format::EXT_ACL_EXT_USER
:
1042 str
= request
->extacl_user
.termedBuf();
1045 case _external_acl_format::EXT_ACL_UNKNOWN
:
1047 case _external_acl_format::EXT_ACL_END
:
1048 fatal("unknown external_acl format error");
1062 if (acl_data
->def
->quote
== external_acl::QUOTE_METHOD_URL
) {
1063 const char *quoted
= rfc1738_escape(str
);
1064 mb
.append(quoted
, strlen(quoted
));
1066 strwordquote(&mb
, str
);
1074 for (arg
= acl_data
->arguments
; arg
; arg
= arg
->next
) {
1078 if (acl_data
->def
->quote
== external_acl::QUOTE_METHOD_URL
) {
1079 const char *quoted
= rfc1738_escape(arg
->key
);
1080 mb
.append(quoted
, strlen(quoted
));
1082 strwordquote(&mb
, arg
->key
);
1092 external_acl_entry_expired(external_acl
* def
, external_acl_entry
* entry
)
1094 if (entry
->date
+ (entry
->result
== 1 ? def
->ttl
: def
->negative_ttl
) < squid_curtime
)
1101 external_acl_grace_expired(external_acl
* def
, external_acl_entry
* entry
)
1104 ttl
= entry
->result
== 1 ? def
->ttl
: def
->negative_ttl
;
1105 ttl
= (ttl
* (100 - def
->grace
)) / 100;
1107 if (entry
->date
+ ttl
< squid_curtime
)
1113 static external_acl_entry
*
1114 external_acl_cache_add(external_acl
* def
, const char *key
, ExternalACLEntryData
const & data
)
1116 ExternalACLEntry
*entry
= static_cast<ExternalACLEntry
*>(hash_lookup(def
->cache
, key
));
1117 debugs(82, 2, "external_acl_cache_add: Adding '" << key
<< "' = " << data
.result
);
1120 debugs(82, 3, "ExternalACLEntry::update: updating existing entry");
1121 entry
->update (data
);
1122 external_acl_cache_touch(def
, entry
);
1127 entry
= new ExternalACLEntry
;
1128 entry
->key
= xstrdup(key
);
1129 entry
->update (data
);
1138 external_acl_cache_delete(external_acl
* def
, external_acl_entry
* entry
)
1140 assert (entry
->def
== def
);
1141 hash_remove_link(def
->cache
, entry
);
1142 dlinkDelete(&entry
->lru
, &def
->lru_list
);
1143 def
->cache_entries
-= 1;
1147 /******************************************************************
1148 * external_acl helpers
1151 typedef struct _externalAclState externalAclState
;
1153 struct _externalAclState
{
1155 void *callback_data
;
1159 externalAclState
*queue
;
1162 CBDATA_TYPE(externalAclState
);
1164 free_externalAclState(void *data
)
1166 externalAclState
*state
= static_cast<externalAclState
*>(data
);
1167 safe_free(state
->key
);
1168 cbdataReferenceDone(state
->callback_data
);
1169 cbdataReferenceDone(state
->def
);
1173 * The helper program receives queries on stdin, one
1174 * per line, and must return the result on on stdout
1176 * General result syntax:
1178 * OK/ERR keyword=value ...
1182 * user= The users name (login)
1183 * message= Message describing the reason
1184 * tag= A string tag to be applied to the request that triggered the acl match.
1185 * applies to both OK and ERR responses.
1186 * Won't override existing request tags.
1187 * log= A string to be used in access logging
1189 * Other keywords may be added to the protocol later
1191 * value needs to be enclosed in quotes if it may contain whitespace, or
1192 * the whitespace escaped using \ (\ escaping obviously also applies to
1197 externalAclHandleReply(void *data
, char *reply
)
1199 externalAclState
*state
= static_cast<externalAclState
*>(data
);
1200 externalAclState
*next
;
1205 ExternalACLEntryData entryData
;
1206 entryData
.result
= 0;
1207 external_acl_entry
*entry
= NULL
;
1209 debugs(82, 2, "externalAclHandleReply: reply=\"" << reply
<< "\"");
1212 status
= strwordtok(reply
, &t
);
1214 if (status
&& strcmp(status
, "OK") == 0)
1215 entryData
.result
= 1;
1217 while ((token
= strwordtok(NULL
, &t
))) {
1218 value
= strchr(token
, '=');
1221 *value
++ = '\0'; /* terminate the token, and move up to the value */
1223 if (state
->def
->quote
== external_acl::QUOTE_METHOD_URL
)
1224 rfc1738_unescape(value
);
1226 if (strcmp(token
, "user") == 0)
1227 entryData
.user
= value
;
1228 else if (strcmp(token
, "message") == 0)
1229 entryData
.message
= value
;
1230 else if (strcmp(token
, "error") == 0)
1231 entryData
.message
= value
;
1232 else if (strcmp(token
, "tag") == 0)
1233 entryData
.tag
= value
;
1234 else if (strcmp(token
, "log") == 0)
1235 entryData
.log
= value
;
1236 else if (strcmp(token
, "password") == 0)
1237 entryData
.password
= value
;
1238 else if (strcmp(token
, "passwd") == 0)
1239 entryData
.password
= value
;
1240 else if (strcmp(token
, "login") == 0)
1241 entryData
.user
= value
;
1246 dlinkDelete(&state
->list
, &state
->def
->queue
);
1248 if (cbdataReferenceValid(state
->def
)) {
1250 entry
= external_acl_cache_add(state
->def
, state
->key
, entryData
);
1252 external_acl_entry
*oldentry
= (external_acl_entry
*)hash_lookup(state
->def
->cache
, state
->key
);
1255 external_acl_cache_delete(state
->def
, oldentry
);
1261 cbdataReferenceDone(state
->def
);
1263 if (state
->callback
&& cbdataReferenceValidDone(state
->callback_data
, &cbdata
))
1264 state
->callback(cbdata
, entry
);
1266 next
= state
->queue
;
1275 ACLExternal::ExternalAclLookup(ACLChecklist
*checklist
, ACLExternal
* me
, EAH
* callback
, void *callback_data
)
1278 external_acl_data
*acl
= me
->data
;
1279 external_acl
*def
= acl
->def
;
1280 externalAclState
*state
;
1282 externalAclState
*oldstate
= NULL
;
1285 ACLFilledChecklist
*ch
= Filled(checklist
);
1286 if (acl
->def
->require_auth
) {
1288 /* Make sure the user is authenticated */
1289 debugs(82, 3, "aclMatchExternal: " << acl
->def
->name
<< " check user authenticated.");
1291 if ((ti
= AuthenticateAcl(ch
)) != 1) {
1292 debugs(82, 1, "externalAclLookup: " << acl
->def
->name
<<
1293 " user authentication failure (" << ti
<< ", ch=" << ch
<< ")");
1294 callback(callback_data
, NULL
);
1297 debugs(82, 3, "aclMatchExternal: " << acl
->def
->name
<< " user is authenticated.");
1300 const char *key
= makeExternalAclKey(ch
, acl
);
1303 debugs(82, 1, "externalAclLookup: lookup in '" << def
->name
<<
1304 "', prerequisit failure (ch=" << ch
<< ")");
1305 callback(callback_data
, NULL
);
1309 debugs(82, 2, "externalAclLookup: lookup in '" << def
->name
<< "' for '" << key
<< "'");
1311 external_acl_entry
*entry
= static_cast<external_acl_entry
*>(hash_lookup(def
->cache
, key
));
1313 if (entry
&& external_acl_entry_expired(def
, entry
))
1316 /* Check for a pending lookup to hook into */
1317 for (node
= def
->queue
.head
; node
; node
= node
->next
) {
1318 externalAclState
*oldstatetmp
= static_cast<externalAclState
*>(node
->data
);
1320 if (strcmp(key
, oldstatetmp
->key
) == 0) {
1321 oldstate
= oldstatetmp
;
1326 if (entry
&& external_acl_grace_expired(def
, entry
)) {
1328 debugs(82, 4, "externalAclLookup: in grace period, but already pending lookup ('" << key
<< "', ch=" << ch
<< ")");
1329 callback(callback_data
, entry
);
1332 graceful
= 1; // grace expired, (neg)ttl did not, and we must start a new lookup.
1336 // The entry is in the cache, grace_ttl did not expired.
1337 if (!graceful
&& entry
&& !external_acl_grace_expired(def
, entry
)) {
1338 /* Should not really happen, but why not.. */
1339 callback(callback_data
, entry
);
1340 debugs(82, 4, "externalAclLookup: no lookup pending for '" << key
<< "', and grace not expired");
1341 debugs(82, 4, "externalAclLookup: (what tha' hell?)");
1345 /* No pending lookup found. Sumbit to helper */
1346 state
= cbdataAlloc(externalAclState
);
1348 state
->def
= cbdataReference(def
);
1350 state
->key
= xstrdup(key
);
1353 state
->callback
= callback
;
1354 state
->callback_data
= cbdataReference(callback_data
);
1358 /* Hook into pending lookup */
1359 state
->queue
= oldstate
->queue
;
1360 oldstate
->queue
= state
;
1362 /* Check for queue overload */
1364 if (def
->theHelper
->stats
.queue_size
>= (int)def
->theHelper
->childs
.n_running
) {
1365 debugs(82, 1, "externalAclLookup: '" << def
->name
<< "' queue overload (ch=" << ch
<< ")");
1367 callback(callback_data
, entry
);
1371 /* Send it off to the helper */
1374 buf
.Printf("%s\n", key
);
1376 debugs(82, 4, "externalAclLookup: looking up for '" << key
<< "' in '" << def
->name
<< "'.");
1378 helperSubmit(def
->theHelper
, buf
.buf
, externalAclHandleReply
, state
);
1380 dlinkAdd(state
, &state
->list
, &def
->queue
);
1386 /* No need to wait during grace period */
1387 debugs(82, 4, "externalAclLookup: no need to wait for the result of '" <<
1388 key
<< "' in '" << def
->name
<< "' (ch=" << ch
<< ").");
1389 debugs(82, 4, "externalAclLookup: using cached entry " << entry
);
1391 if (entry
!= NULL
) {
1392 debugs(82, 4, "externalAclLookup: entry = { date=" <<
1393 (long unsigned int) entry
->date
<< ", result=" <<
1394 entry
->result
<< ", user=" << entry
->user
<< " tag=" <<
1395 entry
->tag
<< " log=" << entry
->log
<< " }");
1399 callback(callback_data
, entry
);
1403 debugs(82, 4, "externalAclLookup: will wait for the result of '" << key
<<
1404 "' in '" << def
->name
<< "' (ch=" << ch
<< ").");
1408 externalAclStats(StoreEntry
* sentry
)
1412 for (p
= Config
.externalAclHelperList
; p
; p
= p
->next
) {
1413 storeAppendPrintf(sentry
, "External ACL Statistics: %s\n", p
->name
);
1414 storeAppendPrintf(sentry
, "Cache size: %d\n", p
->cache
->count
);
1415 helperStats(sentry
, p
->theHelper
);
1416 storeAppendPrintf(sentry
, "\n");
1421 externalAclRegisterWithCacheManager(void)
1423 CacheManager::GetInstance()->
1424 registerAction("external_acl",
1425 "External ACL stats",
1426 externalAclStats
, 0, 1);
1430 externalAclInit(void)
1432 static int firstTimeInit
= 1;
1435 for (p
= Config
.externalAclHelperList
; p
; p
= p
->next
) {
1437 p
->cache
= hash_create((HASHCMP
*) strcmp
, hashPrime(1024), hash4
);
1440 p
->theHelper
= new helper(p
->name
);
1442 p
->theHelper
->cmdline
= p
->cmdline
;
1444 p
->theHelper
->childs
= p
->children
;
1446 p
->theHelper
->ipc_type
= IPC_TCP_SOCKET
;
1448 p
->theHelper
->addr
= p
->local_addr
;
1450 helperOpenServers(p
->theHelper
);
1453 if (firstTimeInit
) {
1455 CBDATA_INIT_TYPE_FREECB(externalAclState
, free_externalAclState
);
1458 externalAclRegisterWithCacheManager();
1462 externalAclShutdown(void)
1466 for (p
= Config
.externalAclHelperList
; p
; p
= p
->next
) {
1467 helperShutdown(p
->theHelper
);
1471 ExternalACLLookup
ExternalACLLookup::instance_
;
1473 ExternalACLLookup::Instance()
1479 ExternalACLLookup::checkForAsync(ACLChecklist
*checklist
)const
1481 /* TODO: optimise this - we probably have a pointer to this
1482 * around somewhere */
1483 ACL
*acl
= ACL::FindByName(AclMatchedName
);
1485 ACLExternal
*me
= dynamic_cast<ACLExternal
*> (acl
);
1487 checklist
->asyncInProgress(true);
1488 ACLExternal::ExternalAclLookup(checklist
, me
, LookupDone
, checklist
);
1492 ExternalACLLookup::LookupDone(void *data
, void *result
)
1494 ACLFilledChecklist
*checklist
= Filled(static_cast<ACLChecklist
*>(data
));
1495 checklist
->extacl_entry
= cbdataReference((external_acl_entry
*)result
);
1496 checklist
->asyncInProgress(false);
1497 checklist
->changeState (ACLChecklist::NullState::Instance());
1501 /* This registers "external" in the registry. To do dynamic definitions
1502 * of external ACL's, rather than a static prototype, have a Prototype instance
1503 * prototype in the class that defines each external acl 'class'.
1504 * Then, then the external acl instance is created, it self registers under
1506 * Be sure that clone is fully functional for that acl class though!
1508 ACL::Prototype
ACLExternal::RegistryProtoype(&ACLExternal::RegistryEntry_
, "external");
1510 ACLExternal
ACLExternal::RegistryEntry_("external");
1513 ACLExternal::clone() const
1515 return new ACLExternal(*this);
1518 ACLExternal::ACLExternal (char const *theClass
) : data (NULL
), class_ (xstrdup (theClass
))
1521 ACLExternal::ACLExternal (ACLExternal
const & old
) : data (NULL
), class_ (old
.class_
? xstrdup (old
.class_
) : NULL
)
1523 /* we don't have copy constructors for the data yet */
1528 ACLExternal::typeString() const
1534 ACLExternal::isProxyAuth() const
1536 return data
->def
->require_auth
;