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
);
370 if (strncmp(token
, "%>{", 3) == 0) {
371 parse_header_token(format
, (token
+3), _external_acl_format::EXT_ACL_HEADER_REQUEST
);
373 if (strncmp(token
, "%<{", 3) == 0) {
374 parse_header_token(format
, (token
+3), _external_acl_format::EXT_ACL_HEADER_REPLY
);
376 } else if (strcmp(token
, "%LOGIN") == 0) {
377 format
->type
= _external_acl_format::EXT_ACL_LOGIN
;
378 a
->require_auth
= true;
382 else if (strcmp(token
, "%IDENT") == 0)
383 format
->type
= _external_acl_format::EXT_ACL_IDENT
;
387 else if (strcmp(token
, "%SRC") == 0)
388 format
->type
= _external_acl_format::EXT_ACL_SRC
;
389 else if (strcmp(token
, "%SRCPORT") == 0)
390 format
->type
= _external_acl_format::EXT_ACL_SRCPORT
;
391 else if (strcmp(token
, "%MYADDR") == 0)
392 format
->type
= _external_acl_format::EXT_ACL_MYADDR
;
393 else if (strcmp(token
, "%MYPORT") == 0)
394 format
->type
= _external_acl_format::EXT_ACL_MYPORT
;
395 else if (strcmp(token
, "%URI") == 0)
396 format
->type
= _external_acl_format::EXT_ACL_URI
;
397 else if (strcmp(token
, "%DST") == 0)
398 format
->type
= _external_acl_format::EXT_ACL_DST
;
399 else if (strcmp(token
, "%PROTO") == 0)
400 format
->type
= _external_acl_format::EXT_ACL_PROTO
;
401 else if (strcmp(token
, "%PORT") == 0)
402 format
->type
= _external_acl_format::EXT_ACL_PORT
;
403 else if (strcmp(token
, "%PATH") == 0)
404 format
->type
= _external_acl_format::EXT_ACL_PATH
;
405 else if (strcmp(token
, "%METHOD") == 0)
406 format
->type
= _external_acl_format::EXT_ACL_METHOD
;
410 else if (strcmp(token
, "%USER_CERT") == 0)
411 format
->type
= _external_acl_format::EXT_ACL_USER_CERT_RAW
;
412 else if (strcmp(token
, "%USER_CERTCHAIN") == 0)
413 format
->type
= _external_acl_format::EXT_ACL_USER_CERTCHAIN_RAW
;
414 else if (strncmp(token
, "%USER_CERT_", 11)) {
415 format
->type
= _external_acl_format::EXT_ACL_USER_CERT
;
416 format
->header
= xstrdup(token
+ 11);
417 } else if (strncmp(token
, "%CA_CERT_", 11)) {
418 format
->type
= _external_acl_format::EXT_ACL_USER_CERT
;
419 format
->header
= xstrdup(token
+ 11);
423 else if (strcmp(token
, "%EXT_USER") == 0)
424 format
->type
= _external_acl_format::EXT_ACL_EXT_USER
;
431 token
= strtok(NULL
, w_space
);
434 /* There must be at least one format token */
442 wordlistAdd(&a
->cmdline
, token
);
445 parse_wordlist(&a
->cmdline
);
448 list
= &(*list
)->next
;
454 dump_externalAclHelper(StoreEntry
* sentry
, const char *name
, const external_acl
* list
)
456 const external_acl
*node
;
457 const external_acl_format
*format
;
458 const wordlist
*word
;
460 for (node
= list
; node
; node
= node
->next
) {
461 storeAppendPrintf(sentry
, "%s %s", name
, node
->name
);
463 if (!node
->local_addr
.IsIPv6())
464 storeAppendPrintf(sentry
, " ipv4");
466 storeAppendPrintf(sentry
, " ipv6");
468 if (node
->ttl
!= DEFAULT_EXTERNAL_ACL_TTL
)
469 storeAppendPrintf(sentry
, " ttl=%d", node
->ttl
);
471 if (node
->negative_ttl
!= node
->ttl
)
472 storeAppendPrintf(sentry
, " negative_ttl=%d", node
->negative_ttl
);
475 storeAppendPrintf(sentry
, " grace=%d", node
->grace
);
477 if (node
->children
!= DEFAULT_EXTERNAL_ACL_CHILDREN
)
478 storeAppendPrintf(sentry
, " children=%d", node
->children
);
480 if (node
->concurrency
)
481 storeAppendPrintf(sentry
, " concurrency=%d", node
->concurrency
);
484 storeAppendPrintf(sentry
, " cache=%d", node
->cache_size
);
486 for (format
= node
->format
; format
; format
= format
->next
) {
487 switch (format
->type
) {
489 case _external_acl_format::EXT_ACL_HEADER_REQUEST
:
490 case _external_acl_format::EXT_ACL_HEADER_REQUEST_ID
:
491 storeAppendPrintf(sentry
, " %%>{%s}", format
->header
);
494 case _external_acl_format::EXT_ACL_HEADER_REQUEST_MEMBER
:
495 case _external_acl_format::EXT_ACL_HEADER_REQUEST_ID_MEMBER
:
496 storeAppendPrintf(sentry
, " %%>{%s:%s}", format
->header
, format
->member
);
499 case _external_acl_format::EXT_ACL_HEADER_REPLY
:
500 case _external_acl_format::EXT_ACL_HEADER_REPLY_ID
:
501 storeAppendPrintf(sentry
, " %%<{%s}", format
->header
);
504 case _external_acl_format::EXT_ACL_HEADER_REPLY_MEMBER
:
505 case _external_acl_format::EXT_ACL_HEADER_REPLY_ID_MEMBER
:
506 storeAppendPrintf(sentry
, " %%<{%s:%s}", format
->header
, format
->member
);
508 #define DUMP_EXT_ACL_TYPE(a) \
509 case _external_acl_format::EXT_ACL_##a: \
510 storeAppendPrintf(sentry, " %%%s", #a); \
513 DUMP_EXT_ACL_TYPE(LOGIN
);
516 DUMP_EXT_ACL_TYPE(IDENT
);
519 DUMP_EXT_ACL_TYPE(SRC
);
520 DUMP_EXT_ACL_TYPE(SRCPORT
);
521 DUMP_EXT_ACL_TYPE(MYADDR
);
522 DUMP_EXT_ACL_TYPE(MYPORT
);
523 DUMP_EXT_ACL_TYPE(URI
);
524 DUMP_EXT_ACL_TYPE(DST
);
525 DUMP_EXT_ACL_TYPE(PROTO
);
526 DUMP_EXT_ACL_TYPE(PORT
);
527 DUMP_EXT_ACL_TYPE(PATH
);
528 DUMP_EXT_ACL_TYPE(METHOD
);
531 case _external_acl_format::EXT_ACL_USER_CERT_RAW
:
532 storeAppendPrintf(sentry
, " %%USER_CERT");
535 case _external_acl_format::EXT_ACL_USER_CERTCHAIN_RAW
:
536 storeAppendPrintf(sentry
, " %%USER_CERTCHAIN");
539 case _external_acl_format::EXT_ACL_USER_CERT
:
540 storeAppendPrintf(sentry
, " %%USER_CERT_%s", format
->header
);
543 case _external_acl_format::EXT_ACL_CA_CERT
:
544 storeAppendPrintf(sentry
, " %%USER_CERT_%s", format
->header
);
548 DUMP_EXT_ACL_TYPE(EXT_USER
);
551 fatal("unknown external_acl format error");
556 for (word
= node
->cmdline
; word
; word
= word
->next
)
557 storeAppendPrintf(sentry
, " %s", word
->key
);
559 storeAppendPrintf(sentry
, "\n");
564 free_externalAclHelper(external_acl
** list
)
567 external_acl
*node
= *list
;
574 static external_acl
*
575 find_externalAclHelper(const char *name
)
579 for (node
= Config
.externalAclHelperList
; node
; node
= node
->next
) {
580 if (strcmp(node
->name
, name
) == 0)
590 (ExternalACLEntry
*anEntry
)
593 assert (anEntry
->def
== NULL
);
595 hash_join(cache
, anEntry
);
596 dlinkAdd(anEntry
, &anEntry
->lru
, &lru_list
);
601 external_acl::trimCache()
603 if (cache_size
&& cache_entries
>= cache_size
)
604 external_acl_cache_delete(this, static_cast<external_acl_entry
*>(lru_list
.tail
->data
));
608 /******************************************************************
612 struct _external_acl_data
{
617 CBDATA_TYPE(external_acl_data
);
619 free_external_acl_data(void *data
)
621 external_acl_data
*p
= static_cast<external_acl_data
*>(data
);
622 wordlistDestroy(&p
->arguments
);
623 cbdataReferenceDone(p
->def
);
634 CBDATA_INIT_TYPE_FREECB(external_acl_data
, free_external_acl_data
);
636 data
= cbdataAlloc(external_acl_data
);
638 token
= strtok(NULL
, w_space
);
643 data
->def
= cbdataReference(find_externalAclHelper(token
));
648 while ((token
= strtokFile())) {
649 wordlistAdd(&data
->arguments
, token
);
654 ACLExternal::valid () const
656 if (data
->def
->require_auth
) {
657 if (authenticateSchemeCount() == 0) {
658 debugs(28, 0, "Can't use proxy auth because no authentication schemes were compiled.");
662 if (authenticateActiveSchemeCount() == 0) {
663 debugs(28, 0, "Can't use proxy auth because no authentication schemes are fully configured.");
672 ACLExternal::empty () const
677 ACLExternal::~ACLExternal()
684 aclMatchExternal(external_acl_data
*acl
, ACLChecklist
* ch
);
686 aclMatchExternal(external_acl_data
*acl
, ACLChecklist
* ch
)
689 external_acl_entry
*entry
;
690 const char *key
= "";
691 debugs(82, 9, "aclMatchExternal: acl=\"" << acl
->def
->name
<< "\"");
692 entry
= ch
->extacl_entry
;
695 if (cbdataReferenceValid(entry
) && entry
->def
== acl
->def
&&
696 strcmp((char *)entry
->key
, key
) == 0) {
699 /* Not valid, or not ours.. get rid of it */
700 cbdataReferenceDone(ch
->extacl_entry
);
705 external_acl_message
= "MISSING REQUIRED INFORMATION";
708 if (acl
->def
->require_auth
) {
710 /* Make sure the user is authenticated */
712 if ((ti
= ch
->authenticated()) != 1) {
713 debugs(82, 2, "aclMatchExternal: " << acl
->def
->name
<< " user not authenticated (" << ti
<< ")");
718 key
= makeExternalAclKey(ch
, acl
);
720 if (acl
->def
->require_auth
)
721 AUTHUSERREQUESTUNLOCK(ch
->auth_user_request
, "ACLChecklist via aclMatchExternal");
724 /* Not sufficient data to process */
728 entry
= static_cast<external_acl_entry
*>(hash_lookup(acl
->def
->cache
, key
));
730 if (!entry
|| external_acl_grace_expired(acl
->def
, entry
)) {
731 debugs(82, 2, "aclMatchExternal: " << acl
->def
->name
<< "(\"" << key
<< "\") = lookup needed");
732 debugs(82, 2, "aclMatchExternal: \"" << key
<< "\": entry=@" <<
733 entry
<< ", age=" << (entry
? (long int) squid_curtime
- entry
->date
: 0));
735 if (acl
->def
->theHelper
->stats
.queue_size
<= acl
->def
->theHelper
->n_running
) {
736 debugs(82, 2, "aclMatchExternal: \"" << key
<< "\": queueing a call.");
737 ch
->changeState (ExternalACLLookup::Instance());
740 debugs(82, 2, "aclMatchExternal: \"" << key
<< "\": return -1.");
745 debugs(82, 1, "aclMatchExternal: '" << acl
->def
->name
<<
746 "' queue overload. Request rejected '" << key
<< "'.");
747 external_acl_message
= "SYSTEM TOO BUSY, TRY AGAIN LATER";
750 debugs(82, 1, "aclMatchExternal: '" << acl
->def
->name
<<
751 "' queue overload. Using stale result. '" << key
<< "'.");
752 /* Fall thru to processing below */
758 external_acl_cache_touch(acl
->def
, entry
);
759 result
= entry
->result
;
760 external_acl_message
= entry
->message
.unsafeBuf();
762 debugs(82, 2, "aclMatchExternal: " << acl
->def
->name
<< " = " << result
);
765 if (entry
->user
.size())
766 ch
->request
->extacl_user
= entry
->user
;
768 if (entry
->password
.size())
769 ch
->request
->extacl_passwd
= entry
->password
;
771 if (!ch
->request
->tag
.size())
772 ch
->request
->tag
= entry
->tag
;
774 if (entry
->log
.size())
775 ch
->request
->extacl_log
= entry
->log
;
782 ACLExternal::match(ACLChecklist
*checklist
)
784 return aclMatchExternal (data
, checklist
);
788 ACLExternal::dump() const
790 external_acl_data
const *acl
= data
;
791 wordlist
*result
= NULL
;
795 mb
.Printf("%s", acl
->def
->name
);
797 for (arg
= acl
->arguments
; arg
; arg
= arg
->next
) {
798 mb
.Printf(" %s", arg
->key
);
801 wordlistAdd(&result
, mb
.buf
);
806 /******************************************************************
811 external_acl_cache_touch(external_acl
* def
, external_acl_entry
* entry
)
813 dlinkDelete(&entry
->lru
, &def
->lru_list
);
814 dlinkAdd(entry
, &entry
->lru
, &def
->lru_list
);
818 makeExternalAclKey(ACLChecklist
* ch
, external_acl_data
* acl_data
)
824 external_acl_format
*format
;
825 HttpRequest
*request
= ch
->request
;
826 HttpReply
*reply
= ch
->reply
;
829 for (format
= acl_data
->def
->format
; format
; format
= format
->next
) {
830 const char *str
= NULL
;
833 switch (format
->type
) {
835 case _external_acl_format::EXT_ACL_LOGIN
:
836 assert (ch
->auth_user_request
);
837 str
= ch
->auth_user_request
->username();
841 case _external_acl_format::EXT_ACL_IDENT
:
845 ch
->changeState(IdentLookup::Instance());
852 case _external_acl_format::EXT_ACL_SRC
:
853 str
= ch
->src_addr
.NtoA(buf
,sizeof(buf
));
856 case _external_acl_format::EXT_ACL_SRCPORT
:
857 snprintf(buf
, sizeof(buf
), "%d", request
->client_addr
.GetPort());
861 case _external_acl_format::EXT_ACL_MYADDR
:
862 str
= request
->my_addr
.NtoA(buf
, sizeof(buf
));
865 case _external_acl_format::EXT_ACL_MYPORT
:
866 snprintf(buf
, sizeof(buf
), "%d", request
->my_addr
.GetPort());
870 case _external_acl_format::EXT_ACL_URI
:
871 str
= urlCanonical(request
);
874 case _external_acl_format::EXT_ACL_DST
:
875 str
= request
->GetHost();
878 case _external_acl_format::EXT_ACL_PROTO
:
879 str
= ProtocolStr
[request
->protocol
];
882 case _external_acl_format::EXT_ACL_PORT
:
883 snprintf(buf
, sizeof(buf
), "%d", request
->port
);
887 case _external_acl_format::EXT_ACL_PATH
:
888 str
= request
->urlpath
.termedBuf();
891 case _external_acl_format::EXT_ACL_METHOD
:
892 str
= RequestMethodStr(request
->method
);
895 case _external_acl_format::EXT_ACL_HEADER_REQUEST
:
896 sb
= request
->header
.getByName(format
->header
);
897 str
= sb
.termedBuf();
900 case _external_acl_format::EXT_ACL_HEADER_REQUEST_ID
:
901 sb
= request
->header
.getStrOrList(format
->header_id
);
902 str
= sb
.termedBuf();
905 case _external_acl_format::EXT_ACL_HEADER_REQUEST_MEMBER
:
906 sb
= request
->header
.getByNameListMember(format
->header
, format
->member
, format
->separator
);
907 str
= sb
.termedBuf();
910 case _external_acl_format::EXT_ACL_HEADER_REQUEST_ID_MEMBER
:
911 sb
= request
->header
.getListMember(format
->header_id
, format
->member
, format
->separator
);
912 str
= sb
.termedBuf();
915 case _external_acl_format::EXT_ACL_HEADER_REPLY
:
917 sb
= reply
->header
.getByName(format
->header
);
918 str
= sb
.termedBuf();
922 case _external_acl_format::EXT_ACL_HEADER_REPLY_ID
:
924 sb
= reply
->header
.getStrOrList(format
->header_id
);
925 str
= sb
.termedBuf();
929 case _external_acl_format::EXT_ACL_HEADER_REPLY_MEMBER
:
931 sb
= reply
->header
.getByNameListMember(format
->header
, format
->member
, format
->separator
);
932 str
= sb
.termedBuf();
936 case _external_acl_format::EXT_ACL_HEADER_REPLY_ID_MEMBER
:
938 sb
= reply
->header
.getListMember(format
->header_id
, format
->member
, format
->separator
);
939 str
= sb
.termedBuf();
944 case _external_acl_format::EXT_ACL_USER_CERT_RAW
:
946 if (ch
->conn() != NULL
) {
947 SSL
*ssl
= fd_table
[ch
->conn()->fd
].ssl
;
950 str
= sslGetUserCertificatePEM(ssl
);
955 case _external_acl_format::EXT_ACL_USER_CERTCHAIN_RAW
:
957 if (ch
->conn() != NULL
) {
958 SSL
*ssl
= fd_table
[ch
->conn()->fd
].ssl
;
961 str
= sslGetUserCertificateChainPEM(ssl
);
966 case _external_acl_format::EXT_ACL_USER_CERT
:
968 if (ch
->conn() != NULL
) {
969 SSL
*ssl
= fd_table
[ch
->conn()->fd
].ssl
;
972 str
= sslGetUserAttribute(ssl
, format
->header
);
977 case _external_acl_format::EXT_ACL_CA_CERT
:
979 if (ch
->conn() != NULL
) {
980 SSL
*ssl
= fd_table
[ch
->conn()->fd
].ssl
;
983 str
= sslGetCAAttribute(ssl
, format
->header
);
989 case _external_acl_format::EXT_ACL_EXT_USER
:
990 str
= request
->extacl_user
.termedBuf();
993 case _external_acl_format::EXT_ACL_UNKNOWN
:
995 case _external_acl_format::EXT_ACL_END
:
996 fatal("unknown external_acl format error");
1010 if (acl_data
->def
->quote
== external_acl::QUOTE_METHOD_URL
) {
1011 const char *quoted
= rfc1738_escape(str
);
1012 mb
.append(quoted
, strlen(quoted
));
1014 strwordquote(&mb
, str
);
1022 for (arg
= acl_data
->arguments
; arg
; arg
= arg
->next
) {
1026 if (acl_data
->def
->quote
== external_acl::QUOTE_METHOD_URL
) {
1027 const char *quoted
= rfc1738_escape(arg
->key
);
1028 mb
.append(quoted
, strlen(quoted
));
1030 strwordquote(&mb
, arg
->key
);
1040 external_acl_entry_expired(external_acl
* def
, external_acl_entry
* entry
)
1042 if (entry
->date
+ (entry
->result
== 1 ? def
->ttl
: def
->negative_ttl
) < squid_curtime
)
1049 external_acl_grace_expired(external_acl
* def
, external_acl_entry
* entry
)
1052 ttl
= entry
->result
== 1 ? def
->ttl
: def
->negative_ttl
;
1053 ttl
= (ttl
* (100 - def
->grace
)) / 100;
1055 if (entry
->date
+ ttl
< squid_curtime
)
1061 static external_acl_entry
*
1062 external_acl_cache_add(external_acl
* def
, const char *key
, ExternalACLEntryData
const & data
)
1064 ExternalACLEntry
*entry
= static_cast<ExternalACLEntry
*>(hash_lookup(def
->cache
, key
));
1065 debugs(82, 2, "external_acl_cache_add: Adding '" << key
<< "' = " << data
.result
);
1068 debugs(82, 3, "ExternalACLEntry::update: updating existing entry");
1069 entry
->update (data
);
1070 external_acl_cache_touch(def
, entry
);
1075 entry
= new ExternalACLEntry
;
1076 entry
->key
= xstrdup(key
);
1077 entry
->update (data
);
1086 external_acl_cache_delete(external_acl
* def
, external_acl_entry
* entry
)
1088 assert (entry
->def
== def
);
1089 hash_remove_link(def
->cache
, entry
);
1090 dlinkDelete(&entry
->lru
, &def
->lru_list
);
1091 def
->cache_entries
-= 1;
1095 /******************************************************************
1096 * external_acl helpers
1099 typedef struct _externalAclState externalAclState
;
1101 struct _externalAclState
{
1103 void *callback_data
;
1107 externalAclState
*queue
;
1110 CBDATA_TYPE(externalAclState
);
1112 free_externalAclState(void *data
)
1114 externalAclState
*state
= static_cast<externalAclState
*>(data
);
1115 safe_free(state
->key
);
1116 cbdataReferenceDone(state
->callback_data
);
1117 cbdataReferenceDone(state
->def
);
1121 * The helper program receives queries on stdin, one
1122 * per line, and must return the result on on stdout
1124 * General result syntax:
1126 * OK/ERR keyword=value ...
1130 * user= The users name (login)
1131 * message= Message describing the reason
1132 * tag= A string tag to be applied to the request that triggered the acl match.
1133 * applies to both OK and ERR responses.
1134 * Won't override existing request tags.
1135 * log= A string to be used in access logging
1137 * Other keywords may be added to the protocol later
1139 * value needs to be enclosed in quotes if it may contain whitespace, or
1140 * the whitespace escaped using \ (\ escaping obviously also applies to
1145 externalAclHandleReply(void *data
, char *reply
)
1147 externalAclState
*state
= static_cast<externalAclState
*>(data
);
1148 externalAclState
*next
;
1153 ExternalACLEntryData entryData
;
1154 entryData
.result
= 0;
1155 external_acl_entry
*entry
= NULL
;
1157 debugs(82, 2, "externalAclHandleReply: reply=\"" << reply
<< "\"");
1160 status
= strwordtok(reply
, &t
);
1162 if (status
&& strcmp(status
, "OK") == 0)
1163 entryData
.result
= 1;
1165 while ((token
= strwordtok(NULL
, &t
))) {
1166 value
= strchr(token
, '=');
1169 *value
++ = '\0'; /* terminate the token, and move up to the value */
1171 if (state
->def
->quote
== external_acl::QUOTE_METHOD_URL
)
1172 rfc1738_unescape(value
);
1174 if (strcmp(token
, "user") == 0)
1175 entryData
.user
= value
;
1176 else if (strcmp(token
, "message") == 0)
1177 entryData
.message
= value
;
1178 else if (strcmp(token
, "error") == 0)
1179 entryData
.message
= value
;
1180 else if (strcmp(token
, "tag") == 0)
1181 entryData
.tag
= value
;
1182 else if (strcmp(token
, "log") == 0)
1183 entryData
.log
= value
;
1184 else if (strcmp(token
, "password") == 0)
1185 entryData
.password
= value
;
1186 else if (strcmp(token
, "passwd") == 0)
1187 entryData
.password
= value
;
1188 else if (strcmp(token
, "login") == 0)
1189 entryData
.user
= value
;
1194 dlinkDelete(&state
->list
, &state
->def
->queue
);
1196 if (cbdataReferenceValid(state
->def
)) {
1198 entry
= external_acl_cache_add(state
->def
, state
->key
, entryData
);
1200 external_acl_entry
*oldentry
= (external_acl_entry
*)hash_lookup(state
->def
->cache
, state
->key
);
1203 external_acl_cache_delete(state
->def
, oldentry
);
1209 cbdataReferenceDone(state
->def
);
1211 if (state
->callback
&& cbdataReferenceValidDone(state
->callback_data
, &cbdata
))
1212 state
->callback(cbdata
, entry
);
1214 next
= state
->queue
;
1223 ACLExternal::ExternalAclLookup(ACLChecklist
* ch
, ACLExternal
* me
, EAH
* callback
, void *callback_data
)
1226 external_acl_data
*acl
= me
->data
;
1227 external_acl
*def
= acl
->def
;
1228 externalAclState
*state
;
1230 externalAclState
*oldstate
= NULL
;
1233 if (acl
->def
->require_auth
) {
1235 /* Make sure the user is authenticated */
1237 if ((ti
= ch
->authenticated()) != 1) {
1238 debugs(82, 1, "externalAclLookup: " << acl
->def
->name
<<
1239 " user authentication failure (" << ti
<< ", ch=" << ch
<< ")");
1240 callback(callback_data
, NULL
);
1245 const char *key
= makeExternalAclKey(ch
, acl
);
1248 debugs(82, 1, "externalAclLookup: lookup in '" << def
->name
<<
1249 "', prerequisit failure (ch=" << ch
<< ")");
1250 callback(callback_data
, NULL
);
1254 debugs(82, 2, "externalAclLookup: lookup in '" << def
->name
<< "' for '" << key
<< "'");
1256 external_acl_entry
*entry
= static_cast<external_acl_entry
*>(hash_lookup(def
->cache
, key
));
1258 if (entry
&& external_acl_entry_expired(def
, entry
))
1261 /* Check for a pending lookup to hook into */
1262 for (node
= def
->queue
.head
; node
; node
= node
->next
) {
1263 externalAclState
*oldstatetmp
= static_cast<externalAclState
*>(node
->data
);
1265 if (strcmp(key
, oldstatetmp
->key
) == 0) {
1266 oldstate
= oldstatetmp
;
1271 if (entry
&& external_acl_grace_expired(def
, entry
)) {
1273 debugs(82, 4, "externalAclLookup: in grace period, but already pending lookup ('" << key
<< "', ch=" << ch
<< ")");
1274 callback(callback_data
, entry
);
1277 graceful
= 1; // grace expired, (neg)ttl did not, and we must start a new lookup.
1281 // The entry is in the cache, grace_ttl did not expired.
1282 if (!graceful
&& entry
&& !external_acl_grace_expired(def
, entry
)) {
1283 /* Should not really happen, but why not.. */
1284 callback(callback_data
, entry
);
1285 debugs(82, 4, "externalAclLookup: no lookup pending for '" << key
<< "', and grace not expired");
1286 debugs(82, 4, "externalAclLookup: (what tha' hell?)");
1290 /* No pending lookup found. Sumbit to helper */
1291 state
= cbdataAlloc(externalAclState
);
1293 state
->def
= cbdataReference(def
);
1295 state
->key
= xstrdup(key
);
1298 state
->callback
= callback
;
1299 state
->callback_data
= cbdataReference(callback_data
);
1303 /* Hook into pending lookup */
1304 state
->queue
= oldstate
->queue
;
1305 oldstate
->queue
= state
;
1307 /* Check for queue overload */
1309 if (def
->theHelper
->stats
.queue_size
>= def
->theHelper
->n_running
) {
1310 debugs(82, 1, "externalAclLookup: '" << def
->name
<< "' queue overload (ch=" << ch
<< ")");
1312 callback(callback_data
, entry
);
1316 /* Send it off to the helper */
1319 buf
.Printf("%s\n", key
);
1321 debugs(82, 4, "externalAclLookup: looking up for '" << key
<< "' in '" << def
->name
<< "'.");
1323 helperSubmit(def
->theHelper
, buf
.buf
, externalAclHandleReply
, state
);
1325 dlinkAdd(state
, &state
->list
, &def
->queue
);
1331 /* No need to wait during grace period */
1332 debugs(82, 4, "externalAclLookup: no need to wait for the result of '" <<
1333 key
<< "' in '" << def
->name
<< "' (ch=" << ch
<< ").");
1334 debugs(82, 4, "externalAclLookup: using cached entry " << entry
);
1336 if (entry
!= NULL
) {
1337 debugs(82, 4, "externalAclLookup: entry = { date=" <<
1338 (long unsigned int) entry
->date
<< ", result=" <<
1339 entry
->result
<< ", user=" << entry
->user
<< " tag=" <<
1340 entry
->tag
<< " log=" << entry
->log
<< " }");
1344 callback(callback_data
, entry
);
1348 debugs(82, 4, "externalAclLookup: will wait for the result of '" << key
<<
1349 "' in '" << def
->name
<< "' (ch=" << ch
<< ").");
1353 externalAclStats(StoreEntry
* sentry
)
1357 for (p
= Config
.externalAclHelperList
; p
; p
= p
->next
) {
1358 storeAppendPrintf(sentry
, "External ACL Statistics: %s\n", p
->name
);
1359 storeAppendPrintf(sentry
, "Cache size: %d\n", p
->cache
->count
);
1360 helperStats(sentry
, p
->theHelper
);
1361 storeAppendPrintf(sentry
, "\n");
1366 externalAclRegisterWithCacheManager(void)
1368 CacheManager::GetInstance()->
1369 registerAction("external_acl",
1370 "External ACL stats",
1371 externalAclStats
, 0, 1);
1375 externalAclInit(void)
1377 static int firstTimeInit
= 1;
1380 for (p
= Config
.externalAclHelperList
; p
; p
= p
->next
) {
1382 p
->cache
= hash_create((HASHCMP
*) strcmp
, hashPrime(1024), hash4
);
1385 p
->theHelper
= helperCreate(p
->name
);
1387 p
->theHelper
->cmdline
= p
->cmdline
;
1389 p
->theHelper
->n_to_start
= p
->children
;
1391 p
->theHelper
->concurrency
= p
->concurrency
;
1393 p
->theHelper
->ipc_type
= IPC_TCP_SOCKET
;
1395 p
->theHelper
->addr
= p
->local_addr
;
1397 helperOpenServers(p
->theHelper
);
1400 if (firstTimeInit
) {
1402 CBDATA_INIT_TYPE_FREECB(externalAclState
, free_externalAclState
);
1405 externalAclRegisterWithCacheManager();
1409 externalAclShutdown(void)
1413 for (p
= Config
.externalAclHelperList
; p
; p
= p
->next
) {
1414 helperShutdown(p
->theHelper
);
1418 ExternalACLLookup
ExternalACLLookup::instance_
;
1420 ExternalACLLookup::Instance()
1426 ExternalACLLookup::checkForAsync(ACLChecklist
*checklist
)const
1428 /* TODO: optimise this - we probably have a pointer to this
1429 * around somewhere */
1430 ACL
*acl
= ACL::FindByName(AclMatchedName
);
1432 ACLExternal
*me
= dynamic_cast<ACLExternal
*> (acl
);
1434 checklist
->asyncInProgress(true);
1435 ACLExternal::ExternalAclLookup(checklist
, me
, LookupDone
, checklist
);
1439 ExternalACLLookup::LookupDone(void *data
, void *result
)
1441 ACLChecklist
*checklist
= (ACLChecklist
*)data
;
1442 checklist
->extacl_entry
= cbdataReference((external_acl_entry
*)result
);
1443 checklist
->asyncInProgress(false);
1444 checklist
->changeState (ACLChecklist::NullState::Instance());
1448 /* This registers "external" in the registry. To do dynamic definitions
1449 * of external ACL's, rather than a static prototype, have a Prototype instance
1450 * prototype in the class that defines each external acl 'class'.
1451 * Then, then the external acl instance is created, it self registers under
1453 * Be sure that clone is fully functional for that acl class though!
1455 ACL::Prototype
ACLExternal::RegistryProtoype(&ACLExternal::RegistryEntry_
, "external");
1457 ACLExternal
ACLExternal::RegistryEntry_("external");
1460 ACLExternal::clone() const
1462 return new ACLExternal(*this);
1465 ACLExternal::ACLExternal (char const *theClass
) : data (NULL
), class_ (xstrdup (theClass
))
1468 ACLExternal::ACLExternal (ACLExternal
const & old
) : data (NULL
), class_ (old
.class_
? xstrdup (old
.class_
) : NULL
)
1470 /* we don't have copy constructors for the data yet */
1475 ACLExternal::typeString() const
1481 ACLExternal::isProxyAuth() const
1483 return data
->def
->require_auth
;