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 "mgr/Registration.h"
45 #include "ExternalACL.h"
46 #include "ExternalACLEntry.h"
49 #include "auth/Gadgets.h"
50 #include "auth/UserRequest.h"
52 #include "SquidTime.h"
55 #include "acl/FilledChecklist.h"
58 #include "ident/AclIdent.h"
61 #include "client_side.h"
62 #include "comm/Connection.h"
63 #include "HttpRequest.h"
64 #include "HttpReply.h"
68 #include "URLScheme.h"
71 #include "ssl/support.h"
74 #ifndef DEFAULT_EXTERNAL_ACL_TTL
75 #define DEFAULT_EXTERNAL_ACL_TTL 1 * 60 * 60
77 #ifndef DEFAULT_EXTERNAL_ACL_CHILDREN
78 #define DEFAULT_EXTERNAL_ACL_CHILDREN 5
81 typedef struct _external_acl_format external_acl_format
;
83 static char *makeExternalAclKey(ACLFilledChecklist
* ch
, external_acl_data
* acl_data
);
84 static void external_acl_cache_delete(external_acl
* def
, external_acl_entry
* entry
);
85 static int external_acl_entry_expired(external_acl
* def
, external_acl_entry
* entry
);
86 static int external_acl_grace_expired(external_acl
* def
, external_acl_entry
* entry
);
87 static void external_acl_cache_touch(external_acl
* def
, external_acl_entry
* entry
);
88 static external_acl_entry
*external_acl_cache_add(external_acl
* def
, const char *key
, ExternalACLEntryData
const &data
);
90 /******************************************************************
91 * external_acl directive
100 void add(ExternalACLEntry
*);
112 external_acl_format
*format
;
116 HelperChildConfig children
;
132 * Configuration flag. May only be altered by the configuration parser.
134 * Indicates that all uses of this external_acl_type helper require authentication
135 * details to be processed. If none are available its a fail match.
141 QUOTE_METHOD_SHELL
= 1,
145 Ip::Address local_addr
;
148 struct _external_acl_format
{
172 EXT_ACL_HEADER_REQUEST
,
173 EXT_ACL_HEADER_REQUEST_MEMBER
,
174 EXT_ACL_HEADER_REQUEST_ID
,
175 EXT_ACL_HEADER_REQUEST_ID_MEMBER
,
177 EXT_ACL_HEADER_REPLY
,
178 EXT_ACL_HEADER_REPLY_MEMBER
,
179 EXT_ACL_HEADER_REPLY_ID
,
180 EXT_ACL_HEADER_REPLY_ID_MEMBER
,
185 EXT_ACL_USER_CERT_RAW
,
186 EXT_ACL_USER_CERTCHAIN_RAW
,
195 external_acl_format
*next
;
199 http_hdr_type header_id
;
202 /* FIXME: These are not really cbdata, but it is an easy way
203 * to get them pooled, refcounted, accounted and freed properly...
205 CBDATA_TYPE(external_acl
);
206 CBDATA_TYPE(external_acl_format
);
209 free_external_acl_format(void *data
)
211 external_acl_format
*p
= static_cast<external_acl_format
*>(data
);
212 safe_free(p
->header
);
216 free_external_acl(void *data
)
218 external_acl
*p
= static_cast<external_acl
*>(data
);
222 external_acl_format
*f
= p
->format
;
227 wordlistDestroy(&p
->cmdline
);
230 helperShutdown(p
->theHelper
);
235 while (p
->lru_list
.tail
)
236 external_acl_cache_delete(p
, static_cast<external_acl_entry
*>(p
->lru_list
.tail
->data
));
238 hashFreeMemory(p
->cache
);
242 * Parse the External ACL format %<{.*} and %>{.*} token(s) to pass a specific
243 * request or reply header to external helper.
245 \param header - the token being parsed (without the identifying prefix)
246 \param type - format enum identifier for this element, pulled from identifying prefix
247 \param format - structure to contain all the info about this format element.
250 parse_header_token(external_acl_format
*format
, char *header
, const _external_acl_format::format_type type
)
255 /** Cut away the closing brace */
256 end
= strchr(header
, '}');
257 if (end
&& strlen(end
) == 1)
262 member
= strchr(header
, ':');
265 /* Split in header and member */
268 if (!xisalnum(*member
))
269 format
->separator
= *member
++;
271 format
->separator
= ',';
273 format
->member
= xstrdup(member
);
275 if (type
== _external_acl_format::EXT_ACL_HEADER_REQUEST
)
276 format
->type
= _external_acl_format::EXT_ACL_HEADER_REQUEST_MEMBER
;
278 format
->type
= _external_acl_format::EXT_ACL_HEADER_REQUEST_MEMBER
;
283 format
->header
= xstrdup(header
);
284 format
->header_id
= httpHeaderIdByNameDef(header
, strlen(header
));
286 if (format
->header_id
!= -1) {
288 if (type
== _external_acl_format::EXT_ACL_HEADER_REQUEST
)
289 format
->type
= _external_acl_format::EXT_ACL_HEADER_REQUEST_ID_MEMBER
;
291 format
->type
= _external_acl_format::EXT_ACL_HEADER_REPLY_ID_MEMBER
;
293 if (type
== _external_acl_format::EXT_ACL_HEADER_REQUEST
)
294 format
->type
= _external_acl_format::EXT_ACL_HEADER_REQUEST_ID
;
296 format
->type
= _external_acl_format::EXT_ACL_HEADER_REPLY_ID
;
302 parse_externalAclHelper(external_acl
** list
)
306 external_acl_format
**p
;
308 CBDATA_INIT_TYPE_FREECB(external_acl
, free_external_acl
);
309 CBDATA_INIT_TYPE_FREECB(external_acl_format
, free_external_acl_format
);
311 a
= cbdataAlloc(external_acl
);
314 a
->ttl
= DEFAULT_EXTERNAL_ACL_TTL
;
315 a
->negative_ttl
= -1;
316 a
->cache_size
= 256*1024;
317 a
->children
.n_max
= DEFAULT_EXTERNAL_ACL_CHILDREN
;
318 a
->children
.n_startup
= a
->children
.n_max
;
319 a
->children
.n_idle
= 1;
320 a
->local_addr
.SetLocalhost();
321 a
->quote
= external_acl::QUOTE_METHOD_URL
;
323 token
= strtok(NULL
, w_space
);
328 a
->name
= xstrdup(token
);
330 token
= strtok(NULL
, w_space
);
334 if (strncmp(token
, "ttl=", 4) == 0) {
335 a
->ttl
= atoi(token
+ 4);
336 } else if (strncmp(token
, "negative_ttl=", 13) == 0) {
337 a
->negative_ttl
= atoi(token
+ 13);
338 } else if (strncmp(token
, "children=", 9) == 0) {
339 a
->children
.n_max
= atoi(token
+ 9);
340 debugs(0, 0, "WARNING: external_acl_type option children=N has been deprecated in favor of children-max=N and children-startup=N");
341 } else if (strncmp(token
, "children-max=", 13) == 0) {
342 a
->children
.n_max
= atoi(token
+ 13);
343 } else if (strncmp(token
, "children-startup=", 17) == 0) {
344 a
->children
.n_startup
= atoi(token
+ 17);
345 } else if (strncmp(token
, "children-idle=", 14) == 0) {
346 a
->children
.n_idle
= atoi(token
+ 14);
347 } else if (strncmp(token
, "concurrency=", 12) == 0) {
348 a
->children
.concurrency
= atoi(token
+ 12);
349 } else if (strncmp(token
, "cache=", 6) == 0) {
350 a
->cache_size
= atoi(token
+ 6);
351 } else if (strncmp(token
, "grace=", 6) == 0) {
352 a
->grace
= atoi(token
+ 6);
353 } else if (strcmp(token
, "protocol=2.5") == 0) {
354 a
->quote
= external_acl::QUOTE_METHOD_SHELL
;
355 } else if (strcmp(token
, "protocol=3.0") == 0) {
356 a
->quote
= external_acl::QUOTE_METHOD_URL
;
357 } else if (strcmp(token
, "quote=url") == 0) {
358 a
->quote
= external_acl::QUOTE_METHOD_URL
;
359 } else if (strcmp(token
, "quote=shell") == 0) {
360 a
->quote
= external_acl::QUOTE_METHOD_SHELL
;
362 /* INET6: allow admin to configure some helpers explicitly to
363 bind to IPv4/v6 localhost port. */
364 } else if (strcmp(token
, "ipv4") == 0) {
365 if ( !a
->local_addr
.SetIPv4() ) {
366 debugs(3, 0, "WARNING: Error converting " << a
->local_addr
<< " to IPv4 in " << a
->name
);
368 } else if (strcmp(token
, "ipv6") == 0) {
370 debugs(3, 0, "WARNING: --enable-ipv6 required for external ACL helpers to use IPv6: " << a
->name
);
371 // else nothing to do.
376 token
= strtok(NULL
, w_space
);
379 /* check that child startup value is sane. */
380 if (a
->children
.n_startup
> a
->children
.n_max
)
381 a
->children
.n_startup
= a
->children
.n_max
;
383 /* check that child idle value is sane. */
384 if (a
->children
.n_idle
> a
->children
.n_max
)
385 a
->children
.n_idle
= a
->children
.n_max
;
386 if (a
->children
.n_idle
< 1)
387 a
->children
.n_idle
= 1;
389 if (a
->negative_ttl
== -1)
390 a
->negative_ttl
= a
->ttl
;
396 external_acl_format
*format
;
398 /* stop on first non-format token found */
403 format
= cbdataAlloc(external_acl_format
);
405 if (strncmp(token
, "%{", 2) == 0) {
406 // deprecated. but assume the old configs all referred to request headers.
407 debugs(82, DBG_IMPORTANT
, "WARNING: external_acl_type format %{...} is being replaced by %>{...} for : " << token
);
408 parse_header_token(format
, (token
+2), _external_acl_format::EXT_ACL_HEADER_REQUEST
);
409 } else if (strncmp(token
, "%>{", 3) == 0) {
410 parse_header_token(format
, (token
+3), _external_acl_format::EXT_ACL_HEADER_REQUEST
);
411 } else if (strncmp(token
, "%<{", 3) == 0) {
412 parse_header_token(format
, (token
+3), _external_acl_format::EXT_ACL_HEADER_REPLY
);
414 } else if (strcmp(token
, "%LOGIN") == 0) {
415 format
->type
= _external_acl_format::EXT_ACL_LOGIN
;
416 a
->require_auth
= true;
421 else if (strcmp(token
, "%IDENT") == 0)
422 format
->type
= _external_acl_format::EXT_ACL_IDENT
;
426 else if (strcmp(token
, "%SRC") == 0)
427 format
->type
= _external_acl_format::EXT_ACL_SRC
;
428 else if (strcmp(token
, "%SRCPORT") == 0)
429 format
->type
= _external_acl_format::EXT_ACL_SRCPORT
;
431 else if (strcmp(token
, "%SRCEUI48") == 0)
432 format
->type
= _external_acl_format::EXT_ACL_SRCEUI48
;
433 else if (strcmp(token
, "%SRCEUI64") == 0)
434 format
->type
= _external_acl_format::EXT_ACL_SRCEUI64
;
436 else if (strcmp(token
, "%MYADDR") == 0)
437 format
->type
= _external_acl_format::EXT_ACL_MYADDR
;
438 else if (strcmp(token
, "%MYPORT") == 0)
439 format
->type
= _external_acl_format::EXT_ACL_MYPORT
;
440 else if (strcmp(token
, "%URI") == 0)
441 format
->type
= _external_acl_format::EXT_ACL_URI
;
442 else if (strcmp(token
, "%DST") == 0)
443 format
->type
= _external_acl_format::EXT_ACL_DST
;
444 else if (strcmp(token
, "%PROTO") == 0)
445 format
->type
= _external_acl_format::EXT_ACL_PROTO
;
446 else if (strcmp(token
, "%PORT") == 0)
447 format
->type
= _external_acl_format::EXT_ACL_PORT
;
448 else if (strcmp(token
, "%PATH") == 0)
449 format
->type
= _external_acl_format::EXT_ACL_PATH
;
450 else if (strcmp(token
, "%METHOD") == 0)
451 format
->type
= _external_acl_format::EXT_ACL_METHOD
;
454 else if (strcmp(token
, "%USER_CERT") == 0)
455 format
->type
= _external_acl_format::EXT_ACL_USER_CERT_RAW
;
456 else if (strcmp(token
, "%USER_CERTCHAIN") == 0)
457 format
->type
= _external_acl_format::EXT_ACL_USER_CERTCHAIN_RAW
;
458 else if (strncmp(token
, "%USER_CERT_", 11) == 0) {
459 format
->type
= _external_acl_format::EXT_ACL_USER_CERT
;
460 format
->header
= xstrdup(token
+ 11);
461 } else if (strncmp(token
, "%CA_CERT_", 11) == 0) {
462 format
->type
= _external_acl_format::EXT_ACL_USER_CERT
;
463 format
->header
= xstrdup(token
+ 11);
467 else if (strcmp(token
, "%EXT_USER") == 0)
468 format
->type
= _external_acl_format::EXT_ACL_EXT_USER
;
470 else if (strcmp(token
, "%EXT_LOG") == 0)
471 format
->type
= _external_acl_format::EXT_ACL_EXT_LOG
;
472 else if (strcmp(token
, "%TAG") == 0)
473 format
->type
= _external_acl_format::EXT_ACL_TAG
;
475 debugs(0,0, "ERROR: Unknown Format token " << token
);
481 token
= strtok(NULL
, w_space
);
484 /* There must be at least one format token */
492 wordlistAdd(&a
->cmdline
, token
);
495 parse_wordlist(&a
->cmdline
);
498 list
= &(*list
)->next
;
504 dump_externalAclHelper(StoreEntry
* sentry
, const char *name
, const external_acl
* list
)
506 const external_acl
*node
;
507 const external_acl_format
*format
;
508 const wordlist
*word
;
510 for (node
= list
; node
; node
= node
->next
) {
511 storeAppendPrintf(sentry
, "%s %s", name
, node
->name
);
513 if (!node
->local_addr
.IsIPv6())
514 storeAppendPrintf(sentry
, " ipv4");
516 storeAppendPrintf(sentry
, " ipv6");
518 if (node
->ttl
!= DEFAULT_EXTERNAL_ACL_TTL
)
519 storeAppendPrintf(sentry
, " ttl=%d", node
->ttl
);
521 if (node
->negative_ttl
!= node
->ttl
)
522 storeAppendPrintf(sentry
, " negative_ttl=%d", node
->negative_ttl
);
525 storeAppendPrintf(sentry
, " grace=%d", node
->grace
);
527 if (node
->children
.n_max
!= DEFAULT_EXTERNAL_ACL_CHILDREN
)
528 storeAppendPrintf(sentry
, " children-max=%d", node
->children
.n_max
);
530 if (node
->children
.n_startup
!= 1)
531 storeAppendPrintf(sentry
, " children-startup=%d", node
->children
.n_startup
);
533 if (node
->children
.n_idle
!= (node
->children
.n_max
+ node
->children
.n_startup
) )
534 storeAppendPrintf(sentry
, " children-idle=%d", node
->children
.n_idle
);
536 if (node
->children
.concurrency
)
537 storeAppendPrintf(sentry
, " concurrency=%d", node
->children
.concurrency
);
540 storeAppendPrintf(sentry
, " cache=%d", node
->cache_size
);
542 for (format
= node
->format
; format
; format
= format
->next
) {
543 switch (format
->type
) {
545 case _external_acl_format::EXT_ACL_HEADER_REQUEST
:
546 case _external_acl_format::EXT_ACL_HEADER_REQUEST_ID
:
547 storeAppendPrintf(sentry
, " %%>{%s}", format
->header
);
550 case _external_acl_format::EXT_ACL_HEADER_REQUEST_MEMBER
:
551 case _external_acl_format::EXT_ACL_HEADER_REQUEST_ID_MEMBER
:
552 storeAppendPrintf(sentry
, " %%>{%s:%s}", format
->header
, format
->member
);
555 case _external_acl_format::EXT_ACL_HEADER_REPLY
:
556 case _external_acl_format::EXT_ACL_HEADER_REPLY_ID
:
557 storeAppendPrintf(sentry
, " %%<{%s}", format
->header
);
560 case _external_acl_format::EXT_ACL_HEADER_REPLY_MEMBER
:
561 case _external_acl_format::EXT_ACL_HEADER_REPLY_ID_MEMBER
:
562 storeAppendPrintf(sentry
, " %%<{%s:%s}", format
->header
, format
->member
);
564 #define DUMP_EXT_ACL_TYPE(a) \
565 case _external_acl_format::EXT_ACL_##a: \
566 storeAppendPrintf(sentry, " %%%s", #a); \
569 DUMP_EXT_ACL_TYPE(LOGIN
);
573 DUMP_EXT_ACL_TYPE(IDENT
);
576 DUMP_EXT_ACL_TYPE(SRC
);
577 DUMP_EXT_ACL_TYPE(SRCPORT
);
579 DUMP_EXT_ACL_TYPE(SRCEUI48
);
580 DUMP_EXT_ACL_TYPE(SRCEUI64
);
583 DUMP_EXT_ACL_TYPE(MYADDR
);
584 DUMP_EXT_ACL_TYPE(MYPORT
);
585 DUMP_EXT_ACL_TYPE(URI
);
586 DUMP_EXT_ACL_TYPE(DST
);
587 DUMP_EXT_ACL_TYPE(PROTO
);
588 DUMP_EXT_ACL_TYPE(PORT
);
589 DUMP_EXT_ACL_TYPE(PATH
);
590 DUMP_EXT_ACL_TYPE(METHOD
);
593 case _external_acl_format::EXT_ACL_USER_CERT_RAW
:
594 storeAppendPrintf(sentry
, " %%USER_CERT");
597 case _external_acl_format::EXT_ACL_USER_CERTCHAIN_RAW
:
598 storeAppendPrintf(sentry
, " %%USER_CERTCHAIN");
601 case _external_acl_format::EXT_ACL_USER_CERT
:
602 storeAppendPrintf(sentry
, " %%USER_CERT_%s", format
->header
);
605 case _external_acl_format::EXT_ACL_CA_CERT
:
606 storeAppendPrintf(sentry
, " %%USER_CERT_%s", format
->header
);
610 DUMP_EXT_ACL_TYPE(EXT_USER
);
612 DUMP_EXT_ACL_TYPE(EXT_LOG
);
613 DUMP_EXT_ACL_TYPE(TAG
);
615 fatal("unknown external_acl format error");
620 for (word
= node
->cmdline
; word
; word
= word
->next
)
621 storeAppendPrintf(sentry
, " %s", word
->key
);
623 storeAppendPrintf(sentry
, "\n");
628 free_externalAclHelper(external_acl
** list
)
631 external_acl
*node
= *list
;
638 static external_acl
*
639 find_externalAclHelper(const char *name
)
643 for (node
= Config
.externalAclHelperList
; node
; node
= node
->next
) {
644 if (strcmp(node
->name
, name
) == 0)
652 external_acl::add(ExternalACLEntry
*anEntry
)
655 assert (anEntry
->def
== NULL
);
657 hash_join(cache
, anEntry
);
658 dlinkAdd(anEntry
, &anEntry
->lru
, &lru_list
);
663 external_acl::trimCache()
665 if (cache_size
&& cache_entries
>= cache_size
)
666 external_acl_cache_delete(this, static_cast<external_acl_entry
*>(lru_list
.tail
->data
));
670 /******************************************************************
674 struct _external_acl_data
{
679 CBDATA_TYPE(external_acl_data
);
681 free_external_acl_data(void *data
)
683 external_acl_data
*p
= static_cast<external_acl_data
*>(data
);
684 wordlistDestroy(&p
->arguments
);
685 cbdataReferenceDone(p
->def
);
696 CBDATA_INIT_TYPE_FREECB(external_acl_data
, free_external_acl_data
);
698 data
= cbdataAlloc(external_acl_data
);
700 token
= strtok(NULL
, w_space
);
705 data
->def
= cbdataReference(find_externalAclHelper(token
));
710 while ((token
= strtokFile())) {
711 wordlistAdd(&data
->arguments
, token
);
716 ACLExternal::valid () const
719 if (data
->def
->require_auth
) {
720 if (authenticateSchemeCount() == 0) {
721 debugs(28, 0, "Can't use proxy auth because no authentication schemes were compiled.");
725 if (authenticateActiveSchemeCount() == 0) {
726 debugs(28, 0, "Can't use proxy auth because no authentication schemes are fully configured.");
736 ACLExternal::empty () const
741 ACLExternal::~ACLExternal()
748 aclMatchExternal(external_acl_data
*acl
, ACLFilledChecklist
*ch
)
751 external_acl_entry
*entry
;
752 const char *key
= "";
753 debugs(82, 9, HERE
<< "acl=\"" << acl
->def
->name
<< "\"");
754 entry
= ch
->extacl_entry
;
757 if (cbdataReferenceValid(entry
) && entry
->def
== acl
->def
) {
760 /* Not valid, or not ours.. get rid of it */
761 debugs(82, 9, HERE
<< "entry " << entry
<< " not valid or not ours. Discarded.");
763 debugs(82, 9, HERE
<< "entry def=" << entry
->def
<< ", our def=" << acl
->def
);
764 key
= makeExternalAclKey(ch
, acl
);
765 debugs(82, 9, HERE
<< "entry key='" << (char *)entry
->key
<< "', our key='" << key
<< "'");
767 cbdataReferenceDone(ch
->extacl_entry
);
772 external_acl_message
= "MISSING REQUIRED INFORMATION";
775 debugs(82, 9, HERE
<< "No helper entry available");
777 if (acl
->def
->require_auth
) {
779 /* Make sure the user is authenticated */
780 debugs(82, 3, "aclMatchExternal: " << acl
->def
->name
<< " check user authenticated.");
781 if ((ti
= AuthenticateAcl(ch
)) != 1) {
782 debugs(82, 2, "aclMatchExternal: " << acl
->def
->name
<< " user not authenticated (" << ti
<< ")");
785 debugs(82, 3, "aclMatchExternal: " << acl
->def
->name
<< " user is authenticated.");
788 key
= makeExternalAclKey(ch
, acl
);
791 /* Not sufficient data to process */
795 entry
= static_cast<external_acl_entry
*>(hash_lookup(acl
->def
->cache
, key
));
797 if (!entry
|| external_acl_grace_expired(acl
->def
, entry
)) {
798 debugs(82, 2, "aclMatchExternal: " << acl
->def
->name
<< "(\"" << key
<< "\") = lookup needed");
799 debugs(82, 2, "aclMatchExternal: \"" << key
<< "\": entry=@" <<
800 entry
<< ", age=" << (entry
? (long int) squid_curtime
- entry
->date
: 0));
802 if (acl
->def
->theHelper
->stats
.queue_size
<= (int)acl
->def
->theHelper
->childs
.n_active
) {
803 debugs(82, 2, "aclMatchExternal: \"" << key
<< "\": queueing a call.");
804 ch
->changeState(ExternalACLLookup::Instance());
805 debugs(82, 2, "aclMatchExternal: \"" << key
<< "\": return -1.");
806 return -1; // to get here we have to have an expired cache entry. MUST not use.
809 debugs(82, 1, "aclMatchExternal: '" << acl
->def
->name
<<
810 "' queue overload. Request rejected '" << key
<< "'.");
811 external_acl_message
= "SYSTEM TOO BUSY, TRY AGAIN LATER";
814 debugs(82, 1, "aclMatchExternal: '" << acl
->def
->name
<<
815 "' queue overload. Using stale result. '" << key
<< "'.");
816 /* Fall thru to processing below */
822 external_acl_cache_touch(acl
->def
, entry
);
823 result
= entry
->result
;
824 external_acl_message
= entry
->message
.termedBuf();
826 debugs(82, 2, "aclMatchExternal: " << acl
->def
->name
<< " = " << result
);
830 if (entry
->user
.size())
831 ch
->request
->extacl_user
= entry
->user
;
833 if (entry
->password
.size())
834 ch
->request
->extacl_passwd
= entry
->password
;
836 if (!ch
->request
->tag
.size())
837 ch
->request
->tag
= entry
->tag
;
839 if (entry
->log
.size())
840 ch
->request
->extacl_log
= entry
->log
;
842 if (entry
->message
.size())
843 ch
->request
->extacl_message
= entry
->message
;
850 ACLExternal::match(ACLChecklist
*checklist
)
852 return aclMatchExternal (data
, Filled(checklist
));
856 ACLExternal::dump() const
858 external_acl_data
const *acl
= data
;
859 wordlist
*result
= NULL
;
863 mb
.Printf("%s", acl
->def
->name
);
865 for (arg
= acl
->arguments
; arg
; arg
= arg
->next
) {
866 mb
.Printf(" %s", arg
->key
);
869 wordlistAdd(&result
, mb
.buf
);
874 /******************************************************************
879 external_acl_cache_touch(external_acl
* def
, external_acl_entry
* entry
)
881 // this must not be done when nothing is being cached.
882 if (def
->cache_size
<= 0 || (def
->ttl
<= 0 && entry
->result
== 1) || (def
->negative_ttl
<= 0 && entry
->result
!= 1))
885 dlinkDelete(&entry
->lru
, &def
->lru_list
);
886 dlinkAdd(entry
, &entry
->lru
, &def
->lru_list
);
890 makeExternalAclKey(ACLFilledChecklist
* ch
, external_acl_data
* acl_data
)
896 external_acl_format
*format
;
897 HttpRequest
*request
= ch
->request
;
898 HttpReply
*reply
= ch
->reply
;
901 for (format
= acl_data
->def
->format
; format
; format
= format
->next
) {
902 const char *str
= NULL
;
905 switch (format
->type
) {
907 case _external_acl_format::EXT_ACL_LOGIN
:
908 assert (ch
->auth_user_request
!= NULL
);
909 str
= ch
->auth_user_request
->username();
913 case _external_acl_format::EXT_ACL_IDENT
:
917 ch
->changeState(IdentLookup::Instance());
924 case _external_acl_format::EXT_ACL_SRC
:
925 str
= ch
->src_addr
.NtoA(buf
,sizeof(buf
));
928 case _external_acl_format::EXT_ACL_SRCPORT
:
929 snprintf(buf
, sizeof(buf
), "%d", request
->client_addr
.GetPort());
934 case _external_acl_format::EXT_ACL_SRCEUI48
:
935 if (request
->clientConnectionManager
.valid() && request
->clientConnectionManager
->clientConnection
!= NULL
&&
936 request
->clientConnectionManager
->clientConnection
->remoteEui48
.encode(buf
, sizeof(buf
)))
940 case _external_acl_format::EXT_ACL_SRCEUI64
:
941 if (request
->clientConnectionManager
.valid() && request
->clientConnectionManager
->clientConnection
!= NULL
&&
942 request
->clientConnectionManager
->clientConnection
->remoteEui64
.encode(buf
, sizeof(buf
)))
947 case _external_acl_format::EXT_ACL_MYADDR
:
948 str
= request
->my_addr
.NtoA(buf
, sizeof(buf
));
951 case _external_acl_format::EXT_ACL_MYPORT
:
952 snprintf(buf
, sizeof(buf
), "%d", request
->my_addr
.GetPort());
956 case _external_acl_format::EXT_ACL_URI
:
957 str
= urlCanonical(request
);
960 case _external_acl_format::EXT_ACL_DST
:
961 str
= request
->GetHost();
964 case _external_acl_format::EXT_ACL_PROTO
:
965 str
= AnyP::ProtocolType_str
[request
->protocol
];
968 case _external_acl_format::EXT_ACL_PORT
:
969 snprintf(buf
, sizeof(buf
), "%d", request
->port
);
973 case _external_acl_format::EXT_ACL_PATH
:
974 str
= request
->urlpath
.termedBuf();
977 case _external_acl_format::EXT_ACL_METHOD
:
978 str
= RequestMethodStr(request
->method
);
981 case _external_acl_format::EXT_ACL_HEADER_REQUEST
:
982 sb
= request
->header
.getByName(format
->header
);
983 str
= sb
.termedBuf();
986 case _external_acl_format::EXT_ACL_HEADER_REQUEST_ID
:
987 sb
= request
->header
.getStrOrList(format
->header_id
);
988 str
= sb
.termedBuf();
991 case _external_acl_format::EXT_ACL_HEADER_REQUEST_MEMBER
:
992 sb
= request
->header
.getByNameListMember(format
->header
, format
->member
, format
->separator
);
993 str
= sb
.termedBuf();
996 case _external_acl_format::EXT_ACL_HEADER_REQUEST_ID_MEMBER
:
997 sb
= request
->header
.getListMember(format
->header_id
, format
->member
, format
->separator
);
998 str
= sb
.termedBuf();
1001 case _external_acl_format::EXT_ACL_HEADER_REPLY
:
1003 sb
= reply
->header
.getByName(format
->header
);
1004 str
= sb
.termedBuf();
1008 case _external_acl_format::EXT_ACL_HEADER_REPLY_ID
:
1010 sb
= reply
->header
.getStrOrList(format
->header_id
);
1011 str
= sb
.termedBuf();
1015 case _external_acl_format::EXT_ACL_HEADER_REPLY_MEMBER
:
1017 sb
= reply
->header
.getByNameListMember(format
->header
, format
->member
, format
->separator
);
1018 str
= sb
.termedBuf();
1022 case _external_acl_format::EXT_ACL_HEADER_REPLY_ID_MEMBER
:
1024 sb
= reply
->header
.getListMember(format
->header_id
, format
->member
, format
->separator
);
1025 str
= sb
.termedBuf();
1030 case _external_acl_format::EXT_ACL_USER_CERT_RAW
:
1032 if (ch
->conn() != NULL
&& Comm::IsConnOpen(ch
->conn()->clientConnection
)) {
1033 SSL
*ssl
= fd_table
[ch
->conn()->clientConnection
->fd
].ssl
;
1036 str
= sslGetUserCertificatePEM(ssl
);
1041 case _external_acl_format::EXT_ACL_USER_CERTCHAIN_RAW
:
1043 if (ch
->conn() != NULL
&& Comm::IsConnOpen(ch
->conn()->clientConnection
)) {
1044 SSL
*ssl
= fd_table
[ch
->conn()->clientConnection
->fd
].ssl
;
1047 str
= sslGetUserCertificateChainPEM(ssl
);
1052 case _external_acl_format::EXT_ACL_USER_CERT
:
1054 if (ch
->conn() != NULL
&& Comm::IsConnOpen(ch
->conn()->clientConnection
)) {
1055 SSL
*ssl
= fd_table
[ch
->conn()->clientConnection
->fd
].ssl
;
1058 str
= sslGetUserAttribute(ssl
, format
->header
);
1063 case _external_acl_format::EXT_ACL_CA_CERT
:
1065 if (ch
->conn() != NULL
&& Comm::IsConnOpen(ch
->conn()->clientConnection
)) {
1066 SSL
*ssl
= fd_table
[ch
->conn()->clientConnection
->fd
].ssl
;
1069 str
= sslGetCAAttribute(ssl
, format
->header
);
1075 case _external_acl_format::EXT_ACL_EXT_USER
:
1076 str
= request
->extacl_user
.termedBuf();
1079 case _external_acl_format::EXT_ACL_EXT_LOG
:
1080 str
= request
->extacl_log
.termedBuf();
1082 case _external_acl_format::EXT_ACL_TAG
:
1083 str
= request
->tag
.termedBuf();
1085 case _external_acl_format::EXT_ACL_UNKNOWN
:
1087 case _external_acl_format::EXT_ACL_END
:
1088 fatal("unknown external_acl format error");
1102 if (acl_data
->def
->quote
== external_acl::QUOTE_METHOD_URL
) {
1103 const char *quoted
= rfc1738_escape(str
);
1104 mb
.append(quoted
, strlen(quoted
));
1106 strwordquote(&mb
, str
);
1114 for (arg
= acl_data
->arguments
; arg
; arg
= arg
->next
) {
1118 if (acl_data
->def
->quote
== external_acl::QUOTE_METHOD_URL
) {
1119 const char *quoted
= rfc1738_escape(arg
->key
);
1120 mb
.append(quoted
, strlen(quoted
));
1122 strwordquote(&mb
, arg
->key
);
1132 external_acl_entry_expired(external_acl
* def
, external_acl_entry
* entry
)
1134 if (def
->cache_size
<= 0)
1137 if (entry
->date
+ (entry
->result
== 1 ? def
->ttl
: def
->negative_ttl
) < squid_curtime
)
1144 external_acl_grace_expired(external_acl
* def
, external_acl_entry
* entry
)
1146 if (def
->cache_size
<= 0)
1150 ttl
= entry
->result
== 1 ? def
->ttl
: def
->negative_ttl
;
1151 ttl
= (ttl
* (100 - def
->grace
)) / 100;
1153 if (entry
->date
+ ttl
<= squid_curtime
)
1159 static external_acl_entry
*
1160 external_acl_cache_add(external_acl
* def
, const char *key
, ExternalACLEntryData
const & data
)
1162 ExternalACLEntry
*entry
;
1164 // do not bother caching this result if TTL is going to expire it immediately
1165 if (def
->cache_size
<= 0 || (def
->ttl
<= 0 && data
.result
== 1) || (def
->negative_ttl
<= 0 && data
.result
!= 1)) {
1167 entry
= new ExternalACLEntry
;
1168 entry
->key
= xstrdup(key
);
1169 entry
->update(data
);
1174 entry
= static_cast<ExternalACLEntry
*>(hash_lookup(def
->cache
, key
));
1175 debugs(82, 2, "external_acl_cache_add: Adding '" << key
<< "' = " << data
.result
);
1178 debugs(82, 3, "ExternalACLEntry::update: updating existing entry");
1179 entry
->update(data
);
1180 external_acl_cache_touch(def
, entry
);
1185 entry
= new ExternalACLEntry
;
1186 entry
->key
= xstrdup(key
);
1187 entry
->update(data
);
1195 external_acl_cache_delete(external_acl
* def
, external_acl_entry
* entry
)
1197 assert(def
->cache_size
> 0 && entry
->def
== def
);
1198 hash_remove_link(def
->cache
, entry
);
1199 dlinkDelete(&entry
->lru
, &def
->lru_list
);
1200 def
->cache_entries
-= 1;
1204 /******************************************************************
1205 * external_acl helpers
1208 typedef struct _externalAclState externalAclState
;
1210 struct _externalAclState
{
1212 void *callback_data
;
1216 externalAclState
*queue
;
1219 CBDATA_TYPE(externalAclState
);
1221 free_externalAclState(void *data
)
1223 externalAclState
*state
= static_cast<externalAclState
*>(data
);
1224 safe_free(state
->key
);
1225 cbdataReferenceDone(state
->callback_data
);
1226 cbdataReferenceDone(state
->def
);
1230 * The helper program receives queries on stdin, one
1231 * per line, and must return the result on on stdout
1233 * General result syntax:
1235 * OK/ERR keyword=value ...
1239 * user= The users name (login)
1240 * message= Message describing the reason
1241 * tag= A string tag to be applied to the request that triggered the acl match.
1242 * applies to both OK and ERR responses.
1243 * Won't override existing request tags.
1244 * log= A string to be used in access logging
1246 * Other keywords may be added to the protocol later
1248 * value needs to be enclosed in quotes if it may contain whitespace, or
1249 * the whitespace escaped using \ (\ escaping obviously also applies to
1254 externalAclHandleReply(void *data
, char *reply
)
1256 externalAclState
*state
= static_cast<externalAclState
*>(data
);
1257 externalAclState
*next
;
1262 ExternalACLEntryData entryData
;
1263 entryData
.result
= 0;
1264 external_acl_entry
*entry
= NULL
;
1266 debugs(82, 2, "externalAclHandleReply: reply=\"" << reply
<< "\"");
1269 status
= strwordtok(reply
, &t
);
1271 if (status
&& strcmp(status
, "OK") == 0)
1272 entryData
.result
= 1;
1274 while ((token
= strwordtok(NULL
, &t
))) {
1275 value
= strchr(token
, '=');
1278 *value
++ = '\0'; /* terminate the token, and move up to the value */
1280 if (state
->def
->quote
== external_acl::QUOTE_METHOD_URL
)
1281 rfc1738_unescape(value
);
1283 if (strcmp(token
, "message") == 0)
1284 entryData
.message
= value
;
1285 else if (strcmp(token
, "error") == 0)
1286 entryData
.message
= value
;
1287 else if (strcmp(token
, "tag") == 0)
1288 entryData
.tag
= value
;
1289 else if (strcmp(token
, "log") == 0)
1290 entryData
.log
= value
;
1292 else if (strcmp(token
, "user") == 0)
1293 entryData
.user
= value
;
1294 else if (strcmp(token
, "password") == 0)
1295 entryData
.password
= value
;
1296 else if (strcmp(token
, "passwd") == 0)
1297 entryData
.password
= value
;
1298 else if (strcmp(token
, "login") == 0)
1299 entryData
.user
= value
;
1305 dlinkDelete(&state
->list
, &state
->def
->queue
);
1307 if (cbdataReferenceValid(state
->def
)) {
1309 entry
= external_acl_cache_add(state
->def
, state
->key
, entryData
);
1311 external_acl_entry
*oldentry
= (external_acl_entry
*)hash_lookup(state
->def
->cache
, state
->key
);
1314 external_acl_cache_delete(state
->def
, oldentry
);
1320 cbdataReferenceDone(state
->def
);
1322 if (state
->callback
&& cbdataReferenceValidDone(state
->callback_data
, &cbdata
))
1323 state
->callback(cbdata
, entry
);
1325 next
= state
->queue
;
1334 ACLExternal::ExternalAclLookup(ACLChecklist
*checklist
, ACLExternal
* me
, EAH
* callback
, void *callback_data
)
1337 external_acl_data
*acl
= me
->data
;
1338 external_acl
*def
= acl
->def
;
1339 externalAclState
*state
;
1341 externalAclState
*oldstate
= NULL
;
1344 ACLFilledChecklist
*ch
= Filled(checklist
);
1346 if (acl
->def
->require_auth
) {
1348 /* Make sure the user is authenticated */
1349 debugs(82, 3, "aclMatchExternal: " << acl
->def
->name
<< " check user authenticated.");
1351 if ((ti
= AuthenticateAcl(ch
)) != 1) {
1352 debugs(82, 1, "externalAclLookup: " << acl
->def
->name
<<
1353 " user authentication failure (" << ti
<< ", ch=" << ch
<< ")");
1354 callback(callback_data
, NULL
);
1357 debugs(82, 3, "aclMatchExternal: " << acl
->def
->name
<< " user is authenticated.");
1361 const char *key
= makeExternalAclKey(ch
, acl
);
1364 debugs(82, 1, "externalAclLookup: lookup in '" << def
->name
<<
1365 "', prerequisit failure (ch=" << ch
<< ")");
1366 callback(callback_data
, NULL
);
1370 debugs(82, 2, "externalAclLookup: lookup in '" << def
->name
<< "' for '" << key
<< "'");
1372 external_acl_entry
*entry
= static_cast<external_acl_entry
*>(hash_lookup(def
->cache
, key
));
1374 if (entry
&& external_acl_entry_expired(def
, entry
))
1377 /* Check for a pending lookup to hook into */
1378 // only possible if we are caching results.
1379 if (def
->cache_size
> 0) {
1380 for (node
= def
->queue
.head
; node
; node
= node
->next
) {
1381 externalAclState
*oldstatetmp
= static_cast<externalAclState
*>(node
->data
);
1383 if (strcmp(key
, oldstatetmp
->key
) == 0) {
1384 oldstate
= oldstatetmp
;
1390 if (entry
&& external_acl_grace_expired(def
, entry
)) {
1392 debugs(82, 4, "externalAclLookup: in grace period, but already pending lookup ('" << key
<< "', ch=" << ch
<< ")");
1393 callback(callback_data
, entry
);
1396 graceful
= 1; // grace expired, (neg)ttl did not, and we must start a new lookup.
1400 // The entry is in the cache, grace_ttl did not expired.
1401 if (!graceful
&& entry
&& !external_acl_grace_expired(def
, entry
)) {
1402 /* Should not really happen, but why not.. */
1403 callback(callback_data
, entry
);
1404 debugs(82, 4, "externalAclLookup: no lookup pending for '" << key
<< "', and grace not expired");
1405 debugs(82, 4, "externalAclLookup: (what tha' hell?)");
1409 /* No pending lookup found. Sumbit to helper */
1410 state
= cbdataAlloc(externalAclState
);
1412 state
->def
= cbdataReference(def
);
1414 state
->key
= xstrdup(key
);
1417 state
->callback
= callback
;
1418 state
->callback_data
= cbdataReference(callback_data
);
1422 /* Hook into pending lookup */
1423 state
->queue
= oldstate
->queue
;
1424 oldstate
->queue
= state
;
1426 /* Check for queue overload */
1428 if (def
->theHelper
->stats
.queue_size
>= (int)def
->theHelper
->childs
.n_running
) {
1429 debugs(82, 1, "externalAclLookup: '" << def
->name
<< "' queue overload (ch=" << ch
<< ")");
1431 callback(callback_data
, entry
);
1435 /* Send it off to the helper */
1438 buf
.Printf("%s\n", key
);
1440 debugs(82, 4, "externalAclLookup: looking up for '" << key
<< "' in '" << def
->name
<< "'.");
1442 helperSubmit(def
->theHelper
, buf
.buf
, externalAclHandleReply
, state
);
1444 dlinkAdd(state
, &state
->list
, &def
->queue
);
1450 /* No need to wait during grace period */
1451 debugs(82, 4, "externalAclLookup: no need to wait for the result of '" <<
1452 key
<< "' in '" << def
->name
<< "' (ch=" << ch
<< ").");
1453 debugs(82, 4, "externalAclLookup: using cached entry " << entry
);
1455 if (entry
!= NULL
) {
1456 debugs(82, 4, "externalAclLookup: entry = { date=" <<
1457 (long unsigned int) entry
->date
<<
1458 ", result=" << entry
->result
<<
1459 " tag=" << entry
->tag
<<
1460 " log=" << entry
->log
<< " }");
1462 debugs(82, 4, "externalAclLookup: user=" << entry
->user
);
1467 callback(callback_data
, entry
);
1471 debugs(82, 4, "externalAclLookup: will wait for the result of '" << key
<<
1472 "' in '" << def
->name
<< "' (ch=" << ch
<< ").");
1476 externalAclStats(StoreEntry
* sentry
)
1480 for (p
= Config
.externalAclHelperList
; p
; p
= p
->next
) {
1481 storeAppendPrintf(sentry
, "External ACL Statistics: %s\n", p
->name
);
1482 storeAppendPrintf(sentry
, "Cache size: %d\n", p
->cache
->count
);
1483 helperStats(sentry
, p
->theHelper
);
1484 storeAppendPrintf(sentry
, "\n");
1489 externalAclRegisterWithCacheManager(void)
1491 Mgr::RegisterAction("external_acl",
1492 "External ACL stats",
1493 externalAclStats
, 0, 1);
1497 externalAclInit(void)
1499 static int firstTimeInit
= 1;
1502 for (p
= Config
.externalAclHelperList
; p
; p
= p
->next
) {
1504 p
->cache
= hash_create((HASHCMP
*) strcmp
, hashPrime(1024), hash4
);
1507 p
->theHelper
= new helper(p
->name
);
1509 p
->theHelper
->cmdline
= p
->cmdline
;
1511 p
->theHelper
->childs
= p
->children
;
1513 p
->theHelper
->ipc_type
= IPC_TCP_SOCKET
;
1515 p
->theHelper
->addr
= p
->local_addr
;
1517 helperOpenServers(p
->theHelper
);
1520 if (firstTimeInit
) {
1522 CBDATA_INIT_TYPE_FREECB(externalAclState
, free_externalAclState
);
1525 externalAclRegisterWithCacheManager();
1529 externalAclShutdown(void)
1533 for (p
= Config
.externalAclHelperList
; p
; p
= p
->next
) {
1534 helperShutdown(p
->theHelper
);
1538 ExternalACLLookup
ExternalACLLookup::instance_
;
1540 ExternalACLLookup::Instance()
1546 ExternalACLLookup::checkForAsync(ACLChecklist
*checklist
)const
1548 /* TODO: optimise this - we probably have a pointer to this
1549 * around somewhere */
1550 ACL
*acl
= ACL::FindByName(AclMatchedName
);
1552 ACLExternal
*me
= dynamic_cast<ACLExternal
*> (acl
);
1554 checklist
->asyncInProgress(true);
1555 ACLExternal::ExternalAclLookup(checklist
, me
, LookupDone
, checklist
);
1559 ExternalACLLookup::LookupDone(void *data
, void *result
)
1561 ACLFilledChecklist
*checklist
= Filled(static_cast<ACLChecklist
*>(data
));
1562 checklist
->extacl_entry
= cbdataReference((external_acl_entry
*)result
);
1563 checklist
->asyncInProgress(false);
1564 checklist
->changeState (ACLChecklist::NullState::Instance());
1565 checklist
->matchNonBlocking();
1568 /* This registers "external" in the registry. To do dynamic definitions
1569 * of external ACL's, rather than a static prototype, have a Prototype instance
1570 * prototype in the class that defines each external acl 'class'.
1571 * Then, then the external acl instance is created, it self registers under
1573 * Be sure that clone is fully functional for that acl class though!
1575 ACL::Prototype
ACLExternal::RegistryProtoype(&ACLExternal::RegistryEntry_
, "external");
1577 ACLExternal
ACLExternal::RegistryEntry_("external");
1580 ACLExternal::clone() const
1582 return new ACLExternal(*this);
1585 ACLExternal::ACLExternal (char const *theClass
) : data (NULL
), class_ (xstrdup (theClass
))
1588 ACLExternal::ACLExternal (ACLExternal
const & old
) : data (NULL
), class_ (old
.class_
? xstrdup (old
.class_
) : NULL
)
1590 /* we don't have copy constructors for the data yet */
1595 ACLExternal::typeString() const
1601 ACLExternal::isProxyAuth() const
1604 return data
->def
->require_auth
;