]> git.ipfire.org Git - thirdparty/squid.git/blame - src/external_acl.cc
Portability fix: u_int*_t types are deprecated, replaced with uint*_t
[thirdparty/squid.git] / src / external_acl.cc
CommitLineData
d9572179 1
2/*
262a0e14 3 * $Id$
d9572179 4 *
5 * DEBUG: section 82 External ACL
6 * AUTHOR: Henrik Nordstrom, MARA Systems AB
7 *
8 * SQUID Web Proxy Cache http://www.squid-cache.org/
9 * ----------------------------------------------------------
10 *
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.
17 *
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.
26 *
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.
26ac0430 31 *
d9572179 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.
26ac0430 36 *
d9572179 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.
40 *
41 */
42
43#include "squid.h"
62ee09ca 44#include "CacheManager.h"
225b7b10 45#include "ExternalACL.h"
1e5562e3 46#include "ExternalACLEntry.h"
2d2b0bb7 47#include "auth/UserRequest.h"
985c86bc 48#include "SquidTime.h"
e6ccf245 49#include "Store.h"
ecb04c8c 50#include "fde.h"
c0941a6a
AR
51#include "acl/FilledChecklist.h"
52#include "acl/Acl.h"
3841dd46 53#if USE_IDENT
4daaf3cb 54#include "ident/AclIdent.h"
3841dd46 55#endif
055421ee 56#include "ip/tools.h"
a46d2c0e 57#include "client_side.h"
a2ac85d9 58#include "HttpRequest.h"
7b0ca1e8 59#include "HttpReply.h"
c0941a6a
AR
60#include "auth/Acl.h"
61#include "auth/Gadgets.h"
aa839030 62#include "helper.h"
0eb49b6d 63#include "MemBuf.h"
1fa9b1a7 64#include "rfc1738.h"
985c86bc 65#include "URLScheme.h"
d295d770 66#include "wordlist.h"
d9572179 67
68#ifndef DEFAULT_EXTERNAL_ACL_TTL
69#define DEFAULT_EXTERNAL_ACL_TTL 1 * 60 * 60
70#endif
d4f2f353 71#ifndef DEFAULT_EXTERNAL_ACL_CHILDREN
72#define DEFAULT_EXTERNAL_ACL_CHILDREN 5
d9572179 73#endif
74
75typedef struct _external_acl_format external_acl_format;
62e76326 76
c0941a6a 77static char *makeExternalAclKey(ACLFilledChecklist * ch, external_acl_data * acl_data);
d9572179 78static void external_acl_cache_delete(external_acl * def, external_acl_entry * entry);
79static int external_acl_entry_expired(external_acl * def, external_acl_entry * entry);
47b0c1fa 80static int external_acl_grace_expired(external_acl * def, external_acl_entry * entry);
d9572179 81static void external_acl_cache_touch(external_acl * def, external_acl_entry * entry);
1e5562e3 82static external_acl_entry *external_acl_cache_add(external_acl * def, const char *key, ExternalACLEntryData const &data);
d9572179 83
84/******************************************************************
85 * external_acl directive
86 */
62e76326 87
1e5562e3 88class external_acl
62e76326 89{
1e5562e3 90
91public:
d9572179 92 external_acl *next;
1e5562e3 93
94 void add
26ac0430 95 (ExternalACLEntry *);
1e5562e3 96
97 void trimCache();
98
d9572179 99 int ttl;
1e5562e3 100
d9572179 101 int negative_ttl;
1e5562e3 102
47b0c1fa 103 int grace;
104
d9572179 105 char *name;
1e5562e3 106
d9572179 107 external_acl_format *format;
1e5562e3 108
d9572179 109 wordlist *cmdline;
1e5562e3 110
48d54e4d 111 HelperChildConfig children;
07eca7e0 112
e6ccf245 113 helper *theHelper;
1e5562e3 114
d9572179 115 hash_table *cache;
1e5562e3 116
d9572179 117 dlink_list lru_list;
1e5562e3 118
d9572179 119 int cache_size;
1e5562e3 120
d9572179 121 int cache_entries;
1e5562e3 122
d9572179 123 dlink_list queue;
1e5562e3 124
3265364b
AJ
125 /**
126 * Configuration flag. May only be altered by the configuration parser.
127 *
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.
130 */
e870b1f8 131 bool require_auth;
dc1af3cf 132
26ac0430 133 enum {
dc1af3cf 134 QUOTE_METHOD_SHELL = 1,
135 QUOTE_METHOD_URL
2fadd50d 136 } quote;
cc192b50 137
b7ac5457 138 Ip::Address local_addr;
d9572179 139};
140
26ac0430 141struct _external_acl_format {
4ecc6631 142 enum format_type {
62e76326 143 EXT_ACL_UNKNOWN,
144 EXT_ACL_LOGIN,
7a16973f 145#if USE_IDENT
62e76326 146 EXT_ACL_IDENT,
7a16973f 147#endif
62e76326 148 EXT_ACL_SRC,
47b0c1fa 149 EXT_ACL_SRCPORT,
a98c2da5
AJ
150#if USE_SQUID_EUI
151 EXT_ACL_SRCEUI48,
152 EXT_ACL_SRCEUI64,
153#endif
47b0c1fa 154 EXT_ACL_MYADDR,
155 EXT_ACL_MYPORT,
3cf6e9c5 156 EXT_ACL_URI,
62e76326 157 EXT_ACL_DST,
158 EXT_ACL_PROTO,
159 EXT_ACL_PORT,
160 EXT_ACL_PATH,
161 EXT_ACL_METHOD,
7b0ca1e8
AJ
162
163 EXT_ACL_HEADER_REQUEST,
164 EXT_ACL_HEADER_REQUEST_MEMBER,
165 EXT_ACL_HEADER_REQUEST_ID,
166 EXT_ACL_HEADER_REQUEST_ID_MEMBER,
167
168 EXT_ACL_HEADER_REPLY,
169 EXT_ACL_HEADER_REPLY_MEMBER,
170 EXT_ACL_HEADER_REPLY_ID,
171 EXT_ACL_HEADER_REPLY_ID_MEMBER,
172
a7ad6e4e 173#if USE_SSL
62e76326 174 EXT_ACL_USER_CERT,
175 EXT_ACL_CA_CERT,
4ac9968f 176 EXT_ACL_USER_CERT_RAW,
3d61c476 177 EXT_ACL_USER_CERTCHAIN_RAW,
a7ad6e4e 178#endif
abb929f0 179 EXT_ACL_EXT_USER,
62e76326 180 EXT_ACL_END
d9572179 181 } type;
182 external_acl_format *next;
183 char *header;
184 char *member;
185 char separator;
186 http_hdr_type header_id;
187};
188
189/* FIXME: These are not really cbdata, but it is an easy way
190 * to get them pooled, refcounted, accounted and freed properly...
191 */
192CBDATA_TYPE(external_acl);
193CBDATA_TYPE(external_acl_format);
194
195static void
196free_external_acl_format(void *data)
197{
e6ccf245 198 external_acl_format *p = static_cast<external_acl_format *>(data);
d9572179 199 safe_free(p->header);
200}
201
202static void
203free_external_acl(void *data)
204{
e6ccf245 205 external_acl *p = static_cast<external_acl *>(data);
d9572179 206 safe_free(p->name);
62e76326 207
d9572179 208 while (p->format) {
62e76326 209 external_acl_format *f = p->format;
210 p->format = f->next;
211 cbdataFree(f);
d9572179 212 }
62e76326 213
d9572179 214 wordlistDestroy(&p->cmdline);
62e76326 215
e6ccf245 216 if (p->theHelper) {
62e76326 217 helperShutdown(p->theHelper);
48d54e4d 218 delete p->theHelper;
62e76326 219 p->theHelper = NULL;
d9572179 220 }
62e76326 221
d9572179 222 while (p->lru_list.tail)
62e76326 223 external_acl_cache_delete(p, static_cast<external_acl_entry *>(p->lru_list.tail->data));
d9572179 224 if (p->cache)
62e76326 225 hashFreeMemory(p->cache);
d9572179 226}
227
7b0ca1e8
AJ
228/**
229 * Parse the External ACL format %<{.*} and %>{.*} token(s) to pass a specific
230 * request or reply header to external helper.
231 *
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.
235 */
236void
4ecc6631 237parse_header_token(external_acl_format *format, char *header, const _external_acl_format::format_type type)
7b0ca1e8
AJ
238{
239 /* header format */
240 char *member, *end;
241
242 /** Cut away the closing brace */
243 end = strchr(header, '}');
244 if (end && strlen(end) == 1)
245 *end = '\0';
246 else
247 self_destruct();
248
249 member = strchr(header, ':');
250
251 if (member) {
252 /* Split in header and member */
253 *member++ = '\0';
254
255 if (!xisalnum(*member))
256 format->separator = *member++;
257 else
258 format->separator = ',';
259
260 format->member = xstrdup(member);
261
26ac0430 262 if (type == _external_acl_format::EXT_ACL_HEADER_REQUEST)
7b0ca1e8
AJ
263 format->type = _external_acl_format::EXT_ACL_HEADER_REQUEST_MEMBER;
264 else
265 format->type = _external_acl_format::EXT_ACL_HEADER_REQUEST_MEMBER;
266 } else {
267 format->type = type;
268 }
269
270 format->header = xstrdup(header);
271 format->header_id = httpHeaderIdByNameDef(header, strlen(header));
272
273 if (format->header_id != -1) {
274 if (member) {
26ac0430 275 if (type == _external_acl_format::EXT_ACL_HEADER_REQUEST)
7b0ca1e8
AJ
276 format->type = _external_acl_format::EXT_ACL_HEADER_REQUEST_ID_MEMBER;
277 else
278 format->type = _external_acl_format::EXT_ACL_HEADER_REPLY_ID_MEMBER;
279 } else {
26ac0430 280 if (type == _external_acl_format::EXT_ACL_HEADER_REQUEST)
7b0ca1e8
AJ
281 format->type = _external_acl_format::EXT_ACL_HEADER_REQUEST_ID;
282 else
283 format->type = _external_acl_format::EXT_ACL_HEADER_REPLY_ID;
284 }
285 }
286}
287
d9572179 288void
289parse_externalAclHelper(external_acl ** list)
290{
291 external_acl *a;
292 char *token;
293 external_acl_format **p;
294
295 CBDATA_INIT_TYPE_FREECB(external_acl, free_external_acl);
296 CBDATA_INIT_TYPE_FREECB(external_acl_format, free_external_acl_format);
297
298 a = cbdataAlloc(external_acl);
299
cc192b50 300 /* set defaults */
d9572179 301 a->ttl = DEFAULT_EXTERNAL_ACL_TTL;
302 a->negative_ttl = -1;
84f795a5 303 a->cache_size = 256*1024;
48d54e4d
AJ
304 a->children.n_max = DEFAULT_EXTERNAL_ACL_CHILDREN;
305 a->children.n_startup = a->children.n_max;
404cfda1 306 a->children.n_idle = 1;
cc192b50 307 a->local_addr.SetLocalhost();
308 a->quote = external_acl::QUOTE_METHOD_URL;
309
d9572179 310 token = strtok(NULL, w_space);
62e76326 311
d9572179 312 if (!token)
62e76326 313 self_destruct();
314
d9572179 315 a->name = xstrdup(token);
316
317 token = strtok(NULL, w_space);
62e76326 318
d9572179 319 /* Parse options */
320 while (token) {
62e76326 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);
07eca7e0 325 } else if (strncmp(token, "children=", 9) == 0) {
48d54e4d
AJ
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);
07eca7e0 334 } else if (strncmp(token, "concurrency=", 12) == 0) {
48d54e4d 335 a->children.concurrency = atoi(token + 12);
62e76326 336 } else if (strncmp(token, "cache=", 6) == 0) {
337 a->cache_size = atoi(token + 6);
47b0c1fa 338 } else if (strncmp(token, "grace=", 6) == 0) {
339 a->grace = atoi(token + 6);
dc1af3cf 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;
cc192b50 348
26ac0430
AJ
349 /* INET6: allow admin to configure some helpers explicitly to
350 bind to IPv4/v6 localhost port. */
cc192b50 351 } else if (strcmp(token, "ipv4") == 0) {
26ac0430 352 if ( !a->local_addr.SetIPv4() ) {
cc192b50 353 debugs(3, 0, "WARNING: Error converting " << a->local_addr << " to IPv4 in " << a->name );
354 }
355 } else if (strcmp(token, "ipv6") == 0) {
055421ee
AJ
356 if (!Ip::EnableIpv6)
357 debugs(3, 0, "WARNING: --enable-ipv6 required for external ACL helpers to use IPv6: " << a->name );
358 // else nothing to do.
62e76326 359 } else {
360 break;
361 }
362
363 token = strtok(NULL, w_space);
d9572179 364 }
62e76326 365
6f78f0ed 366 /* check that child startup value is sane. */
a3cecd6c
AJ
367 if (a->children.n_startup > a->children.n_max)
368 a->children.n_startup = a->children.n_max;
48d54e4d 369
6f78f0ed 370 /* check that child idle value is sane. */
a3cecd6c
AJ
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;
48d54e4d 375
d9572179 376 if (a->negative_ttl == -1)
62e76326 377 a->negative_ttl = a->ttl;
d9572179 378
379 /* Parse format */
380 p = &a->format;
62e76326 381
d9572179 382 while (token) {
62e76326 383 external_acl_format *format;
384
385 /* stop on first non-format token found */
386
387 if (*token != '%')
388 break;
389
390 format = cbdataAlloc(external_acl_format);
391
392 if (strncmp(token, "%{", 2) == 0) {
7b0ca1e8
AJ
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);
4ecc6631 395 parse_header_token(format, (token+2), _external_acl_format::EXT_ACL_HEADER_REQUEST);
c68c9682 396 } else if (strncmp(token, "%>{", 3) == 0) {
4ecc6631 397 parse_header_token(format, (token+3), _external_acl_format::EXT_ACL_HEADER_REQUEST);
c68c9682 398 } else if (strncmp(token, "%<{", 3) == 0) {
4ecc6631 399 parse_header_token(format, (token+3), _external_acl_format::EXT_ACL_HEADER_REPLY);
62e76326 400 } else if (strcmp(token, "%LOGIN") == 0) {
401 format->type = _external_acl_format::EXT_ACL_LOGIN;
e870b1f8 402 a->require_auth = true;
62e76326 403 }
404
7a16973f 405#if USE_IDENT
62e76326 406 else if (strcmp(token, "%IDENT") == 0)
407 format->type = _external_acl_format::EXT_ACL_IDENT;
408
7a16973f 409#endif
62e76326 410
411 else if (strcmp(token, "%SRC") == 0)
412 format->type = _external_acl_format::EXT_ACL_SRC;
47b0c1fa 413 else if (strcmp(token, "%SRCPORT") == 0)
414 format->type = _external_acl_format::EXT_ACL_SRCPORT;
a98c2da5
AJ
415#if USE_SQUID_EUI
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;
420#endif
47b0c1fa 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;
3cf6e9c5 425 else if (strcmp(token, "%URI") == 0)
426 format->type = _external_acl_format::EXT_ACL_URI;
62e76326 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;
437
a7ad6e4e 438#if USE_SSL
4ac9968f 439 else if (strcmp(token, "%USER_CERT") == 0)
3de409f0 440 format->type = _external_acl_format::EXT_ACL_USER_CERT_RAW;
3d61c476 441 else if (strcmp(token, "%USER_CERTCHAIN") == 0)
3de409f0 442 format->type = _external_acl_format::EXT_ACL_USER_CERTCHAIN_RAW;
c68c9682 443 else if (strncmp(token, "%USER_CERT_", 11) == 0) {
62e76326 444 format->type = _external_acl_format::EXT_ACL_USER_CERT;
445 format->header = xstrdup(token + 11);
c68c9682 446 } else if (strncmp(token, "%CA_CERT_", 11) == 0) {
62e76326 447 format->type = _external_acl_format::EXT_ACL_USER_CERT;
448 format->header = xstrdup(token + 11);
449 }
a7ad6e4e 450#endif
abb929f0 451 else if (strcmp(token, "%EXT_USER") == 0)
452 format->type = _external_acl_format::EXT_ACL_EXT_USER;
62e76326 453 else {
a98c2da5 454 debugs(0,0, "ERROR: Unknown Format token " << token);
62e76326 455 self_destruct();
456 }
457
458 *p = format;
459 p = &format->next;
460 token = strtok(NULL, w_space);
d9572179 461 }
462
463 /* There must be at least one format token */
464 if (!a->format)
62e76326 465 self_destruct();
d9572179 466
467 /* helper */
468 if (!token)
62e76326 469 self_destruct();
470
d9572179 471 wordlistAdd(&a->cmdline, token);
472
473 /* arguments */
a336d130 474 parse_wordlist(&a->cmdline);
d9572179 475
476 while (*list)
62e76326 477 list = &(*list)->next;
478
d9572179 479 *list = a;
480}
481
482void
483dump_externalAclHelper(StoreEntry * sentry, const char *name, const external_acl * list)
484{
485 const external_acl *node;
486 const external_acl_format *format;
487 const wordlist *word;
62e76326 488
d9572179 489 for (node = list; node; node = node->next) {
62e76326 490 storeAppendPrintf(sentry, "%s %s", name, node->name);
491
cc192b50 492 if (!node->local_addr.IsIPv6())
493 storeAppendPrintf(sentry, " ipv4");
494 else
495 storeAppendPrintf(sentry, " ipv6");
496
62e76326 497 if (node->ttl != DEFAULT_EXTERNAL_ACL_TTL)
498 storeAppendPrintf(sentry, " ttl=%d", node->ttl);
499
500 if (node->negative_ttl != node->ttl)
501 storeAppendPrintf(sentry, " negative_ttl=%d", node->negative_ttl);
502
47b0c1fa 503 if (node->grace)
504 storeAppendPrintf(sentry, " grace=%d", node->grace);
505
48d54e4d
AJ
506 if (node->children.n_max != DEFAULT_EXTERNAL_ACL_CHILDREN)
507 storeAppendPrintf(sentry, " children-max=%d", node->children.n_max);
62e76326 508
48d54e4d
AJ
509 if (node->children.n_startup != 1)
510 storeAppendPrintf(sentry, " children-startup=%d", node->children.n_startup);
62e76326 511
48d54e4d
AJ
512 if (node->children.n_idle != (node->children.n_max + node->children.n_startup) )
513 storeAppendPrintf(sentry, " children-idle=%d", node->children.n_idle);
514
515 if (node->children.concurrency)
516 storeAppendPrintf(sentry, " concurrency=%d", node->children.concurrency);
07eca7e0 517
47b0c1fa 518 if (node->cache)
519 storeAppendPrintf(sentry, " cache=%d", node->cache_size);
520
62e76326 521 for (format = node->format; format; format = format->next) {
522 switch (format->type) {
523
4ecc6631 524 case _external_acl_format::EXT_ACL_HEADER_REQUEST:
4ecc6631 525 case _external_acl_format::EXT_ACL_HEADER_REQUEST_ID:
2170db3e 526 storeAppendPrintf(sentry, " %%>{%s}", format->header);
62e76326 527 break;
528
4ecc6631 529 case _external_acl_format::EXT_ACL_HEADER_REQUEST_MEMBER:
4ecc6631 530 case _external_acl_format::EXT_ACL_HEADER_REQUEST_ID_MEMBER:
2170db3e
AJ
531 storeAppendPrintf(sentry, " %%>{%s:%s}", format->header, format->member);
532 break;
533
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);
537 break;
538
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);
62e76326 542 break;
d9572179 543#define DUMP_EXT_ACL_TYPE(a) \
dc1af3cf 544 case _external_acl_format::EXT_ACL_##a: \
545 storeAppendPrintf(sentry, " %%%s", #a); \
546 break
62e76326 547
548 DUMP_EXT_ACL_TYPE(LOGIN);
8f45b34c 549#if USE_IDENT
62e76326 550
551 DUMP_EXT_ACL_TYPE(IDENT);
8f45b34c 552#endif
62e76326 553
554 DUMP_EXT_ACL_TYPE(SRC);
47b0c1fa 555 DUMP_EXT_ACL_TYPE(SRCPORT);
a98c2da5
AJ
556#if USE_SQUID_EUI
557 DUMP_EXT_ACL_TYPE(SRCEUI48);
558 DUMP_EXT_ACL_TYPE(SRCEUI64);
559#endif
560
47b0c1fa 561 DUMP_EXT_ACL_TYPE(MYADDR);
562 DUMP_EXT_ACL_TYPE(MYPORT);
3cf6e9c5 563 DUMP_EXT_ACL_TYPE(URI);
62e76326 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);
1f183752 569#if USE_SSL
62e76326 570
4ac9968f 571 case _external_acl_format::EXT_ACL_USER_CERT_RAW:
572 storeAppendPrintf(sentry, " %%USER_CERT");
573 break;
574
3d61c476 575 case _external_acl_format::EXT_ACL_USER_CERTCHAIN_RAW:
576 storeAppendPrintf(sentry, " %%USER_CERTCHAIN");
577 break;
578
62e76326 579 case _external_acl_format::EXT_ACL_USER_CERT:
580 storeAppendPrintf(sentry, " %%USER_CERT_%s", format->header);
581 break;
582
583 case _external_acl_format::EXT_ACL_CA_CERT:
584 storeAppendPrintf(sentry, " %%USER_CERT_%s", format->header);
585 break;
1f183752 586#endif
62e76326 587
abb929f0 588 DUMP_EXT_ACL_TYPE(EXT_USER);
589
4ecc6631 590 default:
62e76326 591 fatal("unknown external_acl format error");
592 break;
593 }
594 }
595
596 for (word = node->cmdline; word; word = word->next)
597 storeAppendPrintf(sentry, " %s", word->key);
598
599 storeAppendPrintf(sentry, "\n");
d9572179 600 }
601}
602
603void
604free_externalAclHelper(external_acl ** list)
605{
606 while (*list) {
62e76326 607 external_acl *node = *list;
608 *list = node->next;
609 node->next = NULL;
610 cbdataFree(node);
d9572179 611 }
612}
613
614static external_acl *
615find_externalAclHelper(const char *name)
616{
617 external_acl *node;
618
619 for (node = Config.externalAclHelperList; node; node = node->next) {
62e76326 620 if (strcmp(node->name, name) == 0)
621 return node;
d9572179 622 }
62e76326 623
d9572179 624 return NULL;
625}
626
1e5562e3 627void
628
629external_acl::add
26ac0430 630(ExternalACLEntry *anEntry)
1e5562e3 631{
632 trimCache();
633 assert (anEntry->def == NULL);
634 anEntry->def = this;
635 hash_join(cache, anEntry);
636 dlinkAdd(anEntry, &anEntry->lru, &lru_list);
637 cache_entries++;
638}
639
640void
641external_acl::trimCache()
642{
643 if (cache_size && cache_entries >= cache_size)
644 external_acl_cache_delete(this, static_cast<external_acl_entry *>(lru_list.tail->data));
645}
646
d9572179 647
648/******************************************************************
649 * external acl type
650 */
651
26ac0430 652struct _external_acl_data {
d9572179 653 external_acl *def;
654 wordlist *arguments;
655};
656
657CBDATA_TYPE(external_acl_data);
658static void
659free_external_acl_data(void *data)
660{
e6ccf245 661 external_acl_data *p = static_cast<external_acl_data *>(data);
d9572179 662 wordlistDestroy(&p->arguments);
663 cbdataReferenceDone(p->def);
664}
665
666void
b0dd28ba 667ACLExternal::parse()
d9572179 668{
d9572179 669 char *token;
62e76326 670
b0dd28ba 671 if (data)
62e76326 672 self_destruct();
673
d9572179 674 CBDATA_INIT_TYPE_FREECB(external_acl_data, free_external_acl_data);
62e76326 675
d9572179 676 data = cbdataAlloc(external_acl_data);
62e76326 677
d9572179 678 token = strtok(NULL, w_space);
62e76326 679
d9572179 680 if (!token)
62e76326 681 self_destruct();
682
d9572179 683 data->def = cbdataReference(find_externalAclHelper(token));
62e76326 684
d9572179 685 if (!data->def)
62e76326 686 self_destruct();
687
4d091667 688 while ((token = strtokFile())) {
62e76326 689 wordlistAdd(&data->arguments, token);
d9572179 690 }
d9572179 691}
692
4b0f5de8 693bool
694ACLExternal::valid () const
695{
696 if (data->def->require_auth) {
697 if (authenticateSchemeCount() == 0) {
bf8fe701 698 debugs(28, 0, "Can't use proxy auth because no authentication schemes were compiled.");
4b0f5de8 699 return false;
700 }
701
702 if (authenticateActiveSchemeCount() == 0) {
bf8fe701 703 debugs(28, 0, "Can't use proxy auth because no authentication schemes are fully configured.");
4b0f5de8 704 return false;
705 }
706 }
707
708 return true;
709}
710
711bool
712ACLExternal::empty () const
713{
714 return false;
715}
716
b0dd28ba 717ACLExternal::~ACLExternal()
d9572179 718{
b0dd28ba 719 cbdataFree(data);
720 safe_free (class_);
d9572179 721}
722
b0dd28ba 723static int
c0941a6a 724aclMatchExternal(external_acl_data *acl, ACLFilledChecklist *ch)
d9572179 725{
726 int result;
727 external_acl_entry *entry;
d9572179 728 const char *key = "";
bf8fe701 729 debugs(82, 9, "aclMatchExternal: acl=\"" << acl->def->name << "\"");
d9572179 730 entry = ch->extacl_entry;
62e76326 731
d9572179 732 if (entry) {
62e76326 733 if (cbdataReferenceValid(entry) && entry->def == acl->def &&
734 strcmp((char *)entry->key, key) == 0) {
735 /* Ours, use it.. */
736 } else {
737 /* Not valid, or not ours.. get rid of it */
738 cbdataReferenceDone(ch->extacl_entry);
739 entry = NULL;
740 }
d9572179 741 }
62e76326 742
4a972fa2 743 external_acl_message = "MISSING REQUIRED INFORMATION";
744
d9572179 745 if (!entry) {
62e76326 746 if (acl->def->require_auth) {
747 int ti;
748 /* Make sure the user is authenticated */
3265364b 749 debugs(82, 3, "aclMatchExternal: " << acl->def->name << " check user authenticated.");
62e76326 750
c0941a6a 751 if ((ti = AuthenticateAcl(ch)) != 1) {
bf8fe701 752 debugs(82, 2, "aclMatchExternal: " << acl->def->name << " user not authenticated (" << ti << ")");
62e76326 753 return ti;
754 }
3265364b 755 debugs(82, 3, "aclMatchExternal: " << acl->def->name << " user is authenticated.");
62e76326 756 }
757
758 key = makeExternalAclKey(ch, acl);
41218131 759
760 if (!key) {
761 /* Not sufficient data to process */
762 return -1;
763 }
764
62e76326 765 entry = static_cast<external_acl_entry *>(hash_lookup(acl->def->cache, key));
766
47b0c1fa 767 if (!entry || external_acl_grace_expired(acl->def, entry)) {
bf8fe701 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));
95a83c22 771
48d54e4d 772 if (acl->def->theHelper->stats.queue_size <= (int)acl->def->theHelper->childs.n_active) {
bf8fe701 773 debugs(82, 2, "aclMatchExternal: \"" << key << "\": queueing a call.");
47b0c1fa 774 ch->changeState (ExternalACLLookup::Instance());
ab321f4b 775
776 if (entry == NULL) {
bf8fe701 777 debugs(82, 2, "aclMatchExternal: \"" << key << "\": return -1.");
ab321f4b 778 return -1;
779 }
47b0c1fa 780 } else {
781 if (!entry) {
bf8fe701 782 debugs(82, 1, "aclMatchExternal: '" << acl->def->name <<
783 "' queue overload. Request rejected '" << key << "'.");
4a972fa2 784 external_acl_message = "SYSTEM TOO BUSY, TRY AGAIN LATER";
47b0c1fa 785 return -1;
786 } else {
bf8fe701 787 debugs(82, 1, "aclMatchExternal: '" << acl->def->name <<
788 "' queue overload. Using stale result. '" << key << "'.");
47b0c1fa 789 /* Fall thru to processing below */
790 }
791 }
792 }
d9572179 793 }
62e76326 794
d9572179 795 external_acl_cache_touch(acl->def, entry);
796 result = entry->result;
a7a42b14 797 external_acl_message = entry->message.termedBuf();
4a972fa2 798
bf8fe701 799 debugs(82, 2, "aclMatchExternal: " << acl->def->name << " = " << result);
62e76326 800
abb929f0 801 if (ch->request) {
802 if (entry->user.size())
803 ch->request->extacl_user = entry->user;
cbd2dc91 804
abb929f0 805 if (entry->password.size())
806 ch->request->extacl_passwd = entry->password;
62e76326 807
abb929f0 808 if (!ch->request->tag.size())
809 ch->request->tag = entry->tag;
4a972fa2 810
811 if (entry->log.size())
812 ch->request->extacl_log = entry->log;
8c93a598
HN
813
814 if (entry->message.size())
815 ch->request->extacl_message = entry->message;
abb929f0 816 }
1e5562e3 817
d9572179 818 return result;
819}
820
b0dd28ba 821int
822ACLExternal::match(ACLChecklist *checklist)
823{
c0941a6a 824 return aclMatchExternal (data, Filled(checklist));
b0dd28ba 825}
826
d9572179 827wordlist *
b0dd28ba 828ACLExternal::dump() const
d9572179 829{
b0dd28ba 830 external_acl_data const *acl = data;
d9572179 831 wordlist *result = NULL;
832 wordlist *arg;
833 MemBuf mb;
2fe7eff9 834 mb.init();
835 mb.Printf("%s", acl->def->name);
62e76326 836
d9572179 837 for (arg = acl->arguments; arg; arg = arg->next) {
2fe7eff9 838 mb.Printf(" %s", arg->key);
d9572179 839 }
62e76326 840
d9572179 841 wordlistAdd(&result, mb.buf);
2fe7eff9 842 mb.clean();
d9572179 843 return result;
844}
845
846/******************************************************************
847 * external_acl cache
848 */
849
d9572179 850static void
851external_acl_cache_touch(external_acl * def, external_acl_entry * entry)
852{
853 dlinkDelete(&entry->lru, &def->lru_list);
854 dlinkAdd(entry, &entry->lru, &def->lru_list);
855}
856
857static char *
c0941a6a 858makeExternalAclKey(ACLFilledChecklist * ch, external_acl_data * acl_data)
d9572179 859{
032785bf 860 static MemBuf mb;
d9572179 861 char buf[256];
862 int first = 1;
863 wordlist *arg;
864 external_acl_format *format;
190154cf 865 HttpRequest *request = ch->request;
7b0ca1e8 866 HttpReply *reply = ch->reply;
2fe7eff9 867 mb.reset();
62e76326 868
d9572179 869 for (format = acl_data->def->format; format; format = format->next) {
62e76326 870 const char *str = NULL;
30abd221 871 String sb;
62e76326 872
873 switch (format->type) {
874
875 case _external_acl_format::EXT_ACL_LOGIN:
a33a428a 876 assert (ch->auth_user_request != NULL);
f5691f9c 877 str = ch->auth_user_request->username();
62e76326 878 break;
7a16973f 879#if USE_IDENT
62e76326 880
881 case _external_acl_format::EXT_ACL_IDENT:
882 str = ch->rfc931;
883
41218131 884 if (!str || !*str) {
62e76326 885 ch->changeState(IdentLookup::Instance());
886 return NULL;
887 }
888
889 break;
7a16973f 890#endif
62e76326 891
892 case _external_acl_format::EXT_ACL_SRC:
cc192b50 893 str = ch->src_addr.NtoA(buf,sizeof(buf));
62e76326 894 break;
895
47b0c1fa 896 case _external_acl_format::EXT_ACL_SRCPORT:
cc192b50 897 snprintf(buf, sizeof(buf), "%d", request->client_addr.GetPort());
47b0c1fa 898 str = buf;
899 break;
900
a98c2da5
AJ
901#if USE_SQUID_EUI
902 case _external_acl_format::EXT_ACL_SRCEUI48:
903 if (request->client_eui48.encode(buf, sizeof(buf)))
904 str = buf;
905 break;
906
907 case _external_acl_format::EXT_ACL_SRCEUI64:
908 if (request->client_eui64.encode(buf, sizeof(buf)))
909 str = buf;
910 break;
911#endif
912
47b0c1fa 913 case _external_acl_format::EXT_ACL_MYADDR:
cc192b50 914 str = request->my_addr.NtoA(buf, sizeof(buf));
47b0c1fa 915 break;
916
917 case _external_acl_format::EXT_ACL_MYPORT:
cc192b50 918 snprintf(buf, sizeof(buf), "%d", request->my_addr.GetPort());
47b0c1fa 919 str = buf;
920 break;
921
3cf6e9c5 922 case _external_acl_format::EXT_ACL_URI:
923 str = urlCanonical(request);
924 break;
925
62e76326 926 case _external_acl_format::EXT_ACL_DST:
cc192b50 927 str = request->GetHost();
62e76326 928 break;
929
930 case _external_acl_format::EXT_ACL_PROTO:
931 str = ProtocolStr[request->protocol];
932 break;
933
934 case _external_acl_format::EXT_ACL_PORT:
935 snprintf(buf, sizeof(buf), "%d", request->port);
936 str = buf;
937 break;
938
939 case _external_acl_format::EXT_ACL_PATH:
a313a9ba 940 str = request->urlpath.termedBuf();
62e76326 941 break;
942
943 case _external_acl_format::EXT_ACL_METHOD:
60745f24 944 str = RequestMethodStr(request->method);
62e76326 945 break;
946
7b0ca1e8 947 case _external_acl_format::EXT_ACL_HEADER_REQUEST:
a9925b40 948 sb = request->header.getByName(format->header);
a313a9ba 949 str = sb.termedBuf();
62e76326 950 break;
951
7b0ca1e8 952 case _external_acl_format::EXT_ACL_HEADER_REQUEST_ID:
a9925b40 953 sb = request->header.getStrOrList(format->header_id);
a313a9ba 954 str = sb.termedBuf();
62e76326 955 break;
956
7b0ca1e8 957 case _external_acl_format::EXT_ACL_HEADER_REQUEST_MEMBER:
a9925b40 958 sb = request->header.getByNameListMember(format->header, format->member, format->separator);
a313a9ba 959 str = sb.termedBuf();
62e76326 960 break;
961
7b0ca1e8 962 case _external_acl_format::EXT_ACL_HEADER_REQUEST_ID_MEMBER:
a9925b40 963 sb = request->header.getListMember(format->header_id, format->member, format->separator);
a313a9ba 964 str = sb.termedBuf();
62e76326 965 break;
7b0ca1e8
AJ
966
967 case _external_acl_format::EXT_ACL_HEADER_REPLY:
26ac0430 968 if (reply) {
7b0ca1e8 969 sb = reply->header.getByName(format->header);
a313a9ba 970 str = sb.termedBuf();
7b0ca1e8
AJ
971 }
972 break;
973
974 case _external_acl_format::EXT_ACL_HEADER_REPLY_ID:
26ac0430 975 if (reply) {
7b0ca1e8 976 sb = reply->header.getStrOrList(format->header_id);
a313a9ba 977 str = sb.termedBuf();
7b0ca1e8
AJ
978 }
979 break;
980
981 case _external_acl_format::EXT_ACL_HEADER_REPLY_MEMBER:
26ac0430 982 if (reply) {
7b0ca1e8 983 sb = reply->header.getByNameListMember(format->header, format->member, format->separator);
a313a9ba 984 str = sb.termedBuf();
7b0ca1e8
AJ
985 }
986 break;
987
988 case _external_acl_format::EXT_ACL_HEADER_REPLY_ID_MEMBER:
26ac0430 989 if (reply) {
7b0ca1e8 990 sb = reply->header.getListMember(format->header_id, format->member, format->separator);
a313a9ba 991 str = sb.termedBuf();
7b0ca1e8
AJ
992 }
993 break;
a7ad6e4e 994#if USE_SSL
62e76326 995
4ac9968f 996 case _external_acl_format::EXT_ACL_USER_CERT_RAW:
997
85a4b153 998 if (ch->conn() != NULL) {
4ac9968f 999 SSL *ssl = fd_table[ch->conn()->fd].ssl;
1000
1001 if (ssl)
1002 str = sslGetUserCertificatePEM(ssl);
1003 }
1004
1005 break;
1006
3d61c476 1007 case _external_acl_format::EXT_ACL_USER_CERTCHAIN_RAW:
1008
85a4b153 1009 if (ch->conn() != NULL) {
3d61c476 1010 SSL *ssl = fd_table[ch->conn()->fd].ssl;
1011
1012 if (ssl)
1013 str = sslGetUserCertificateChainPEM(ssl);
1014 }
1015
1016 break;
1017
62e76326 1018 case _external_acl_format::EXT_ACL_USER_CERT:
1019
85a4b153 1020 if (ch->conn() != NULL) {
62e76326 1021 SSL *ssl = fd_table[ch->conn()->fd].ssl;
1022
1023 if (ssl)
1024 str = sslGetUserAttribute(ssl, format->header);
1025 }
1026
1027 break;
1028
1029 case _external_acl_format::EXT_ACL_CA_CERT:
1030
85a4b153 1031 if (ch->conn() != NULL) {
62e76326 1032 SSL *ssl = fd_table[ch->conn()->fd].ssl;
1033
1034 if (ssl)
1035 str = sslGetCAAttribute(ssl, format->header);
1036 }
1037
1038 break;
a7ad6e4e 1039#endif
62e76326 1040
abb929f0 1041 case _external_acl_format::EXT_ACL_EXT_USER:
a313a9ba 1042 str = request->extacl_user.termedBuf();
abb929f0 1043 break;
1044
62e76326 1045 case _external_acl_format::EXT_ACL_UNKNOWN:
1046
1047 case _external_acl_format::EXT_ACL_END:
1048 fatal("unknown external_acl format error");
1049 break;
1050 }
1051
1052 if (str)
1053 if (!*str)
1054 str = NULL;
1055
1056 if (!str)
1057 str = "-";
1058
1059 if (!first)
2fe7eff9 1060 mb.append(" ", 1);
62e76326 1061
dc1af3cf 1062 if (acl_data->def->quote == external_acl::QUOTE_METHOD_URL) {
1063 const char *quoted = rfc1738_escape(str);
2fe7eff9 1064 mb.append(quoted, strlen(quoted));
dc1af3cf 1065 } else {
1066 strwordquote(&mb, str);
1067 }
62e76326 1068
30abd221 1069 sb.clean();
62e76326 1070
1071 first = 0;
d9572179 1072 }
62e76326 1073
d9572179 1074 for (arg = acl_data->arguments; arg; arg = arg->next) {
62e76326 1075 if (!first)
2fe7eff9 1076 mb.append(" ", 1);
62e76326 1077
dc1af3cf 1078 if (acl_data->def->quote == external_acl::QUOTE_METHOD_URL) {
1079 const char *quoted = rfc1738_escape(arg->key);
2fe7eff9 1080 mb.append(quoted, strlen(quoted));
dc1af3cf 1081 } else {
1082 strwordquote(&mb, arg->key);
1083 }
62e76326 1084
1085 first = 0;
d9572179 1086 }
62e76326 1087
d9572179 1088 return mb.buf;
d9572179 1089}
1090
1091static int
1092external_acl_entry_expired(external_acl * def, external_acl_entry * entry)
1093{
1094 if (entry->date + (entry->result == 1 ? def->ttl : def->negative_ttl) < squid_curtime)
62e76326 1095 return 1;
d9572179 1096 else
62e76326 1097 return 0;
d9572179 1098}
62e76326 1099
47b0c1fa 1100static int
1101external_acl_grace_expired(external_acl * def, external_acl_entry * entry)
1102{
1103 int ttl;
1104 ttl = entry->result == 1 ? def->ttl : def->negative_ttl;
1105 ttl = (ttl * (100 - def->grace)) / 100;
1106
1107 if (entry->date + ttl < squid_curtime)
1108 return 1;
1109 else
1110 return 0;
1111}
1112
d9572179 1113static external_acl_entry *
1e5562e3 1114external_acl_cache_add(external_acl * def, const char *key, ExternalACLEntryData const & data)
d9572179 1115{
1e5562e3 1116 ExternalACLEntry *entry = static_cast<ExternalACLEntry *>(hash_lookup(def->cache, key));
bf8fe701 1117 debugs(82, 2, "external_acl_cache_add: Adding '" << key << "' = " << data.result);
62e76326 1118
d9572179 1119 if (entry) {
bf8fe701 1120 debugs(82, 3, "ExternalACLEntry::update: updating existing entry");
1e5562e3 1121 entry->update (data);
62e76326 1122 external_acl_cache_touch(def, entry);
1123
1124 return entry;
d9572179 1125 }
62e76326 1126
1e5562e3 1127 entry = new ExternalACLEntry;
4a8b20e8 1128 entry->key = xstrdup(key);
1e5562e3 1129 entry->update (data);
1130
1131 def->add
1132 (entry);
1133
d9572179 1134 return entry;
1135}
1136
1137static void
1138external_acl_cache_delete(external_acl * def, external_acl_entry * entry)
1139{
1e5562e3 1140 assert (entry->def == def);
4a8b20e8 1141 hash_remove_link(def->cache, entry);
d9572179 1142 dlinkDelete(&entry->lru, &def->lru_list);
1143 def->cache_entries -= 1;
00d77d6b 1144 delete entry;
d9572179 1145}
1146
1147/******************************************************************
1148 * external_acl helpers
1149 */
1150
1151typedef struct _externalAclState externalAclState;
62e76326 1152
26ac0430 1153struct _externalAclState {
d9572179 1154 EAH *callback;
1155 void *callback_data;
1156 char *key;
1157 external_acl *def;
1158 dlink_node list;
1159 externalAclState *queue;
1160};
1161
1162CBDATA_TYPE(externalAclState);
1163static void
1164free_externalAclState(void *data)
1165{
e6ccf245 1166 externalAclState *state = static_cast<externalAclState *>(data);
d9572179 1167 safe_free(state->key);
1168 cbdataReferenceDone(state->callback_data);
1169 cbdataReferenceDone(state->def);
1170}
1171
d9572179 1172/*
1173 * The helper program receives queries on stdin, one
07eca7e0 1174 * per line, and must return the result on on stdout
d9572179 1175 *
1176 * General result syntax:
1177 *
1178 * OK/ERR keyword=value ...
1179 *
1180 * Keywords:
1181 *
4a972fa2 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
d9572179 1188 *
1189 * Other keywords may be added to the protocol later
1190 *
26ac0430
AJ
1191 * value needs to be enclosed in quotes if it may contain whitespace, or
1192 * the whitespace escaped using \ (\ escaping obviously also applies to
d9572179 1193 * any " characters)
1194 */
1195
1196static void
1197externalAclHandleReply(void *data, char *reply)
1198{
e6ccf245 1199 externalAclState *state = static_cast<externalAclState *>(data);
d9572179 1200 externalAclState *next;
d9572179 1201 char *status;
1202 char *token;
1203 char *value;
1204 char *t;
1e5562e3 1205 ExternalACLEntryData entryData;
1206 entryData.result = 0;
466090ac 1207 external_acl_entry *entry = NULL;
d9572179 1208
bf8fe701 1209 debugs(82, 2, "externalAclHandleReply: reply=\"" << reply << "\"");
d9572179 1210
4960475f 1211 if (reply) {
62e76326 1212 status = strwordtok(reply, &t);
1213
1214 if (status && strcmp(status, "OK") == 0)
1e5562e3 1215 entryData.result = 1;
62e76326 1216
1217 while ((token = strwordtok(NULL, &t))) {
1218 value = strchr(token, '=');
1219
1220 if (value) {
1221 *value++ = '\0'; /* terminate the token, and move up to the value */
1222
dc1af3cf 1223 if (state->def->quote == external_acl::QUOTE_METHOD_URL)
1224 rfc1738_unescape(value);
1225
62e76326 1226 if (strcmp(token, "user") == 0)
1e5562e3 1227 entryData.user = value;
d7bd27db 1228 else if (strcmp(token, "message") == 0)
1229 entryData.message = value;
62e76326 1230 else if (strcmp(token, "error") == 0)
d7bd27db 1231 entryData.message = value;
abb929f0 1232 else if (strcmp(token, "tag") == 0)
1233 entryData.tag = value;
4a972fa2 1234 else if (strcmp(token, "log") == 0)
1235 entryData.log = value;
abb929f0 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;
62e76326 1242 }
1243 }
d9572179 1244 }
62e76326 1245
d9572179 1246 dlinkDelete(&state->list, &state->def->queue);
62e76326 1247
4960475f 1248 if (cbdataReferenceValid(state->def)) {
62e76326 1249 if (reply)
1e5562e3 1250 entry = external_acl_cache_add(state->def, state->key, entryData);
62e76326 1251 else {
2ae2408b 1252 external_acl_entry *oldentry = (external_acl_entry *)hash_lookup(state->def->cache, state->key);
62e76326 1253
2ae2408b 1254 if (oldentry)
1255 external_acl_cache_delete(state->def, oldentry);
62e76326 1256 }
466090ac 1257 }
d9572179 1258
1259 do {
62e76326 1260 void *cbdata;
1261 cbdataReferenceDone(state->def);
1262
47b0c1fa 1263 if (state->callback && cbdataReferenceValidDone(state->callback_data, &cbdata))
62e76326 1264 state->callback(cbdata, entry);
1265
1266 next = state->queue;
d9572179 1267
62e76326 1268 cbdataFree(state);
d9572179 1269
62e76326 1270 state = next;
d9572179 1271 } while (state);
1272}
1273
1274void
c0941a6a 1275ACLExternal::ExternalAclLookup(ACLChecklist *checklist, ACLExternal * me, EAH * callback, void *callback_data)
d9572179 1276{
1277 MemBuf buf;
e870b1f8 1278 external_acl_data *acl = me->data;
d9572179 1279 external_acl *def = acl->def;
d9572179 1280 externalAclState *state;
47b0c1fa 1281 dlink_node *node;
1282 externalAclState *oldstate = NULL;
1283 bool graceful = 0;
c8e7608c 1284
c0941a6a 1285 ACLFilledChecklist *ch = Filled(checklist);
c8e7608c 1286 if (acl->def->require_auth) {
1287 int ti;
1288 /* Make sure the user is authenticated */
3265364b 1289 debugs(82, 3, "aclMatchExternal: " << acl->def->name << " check user authenticated.");
c8e7608c 1290
c0941a6a 1291 if ((ti = AuthenticateAcl(ch)) != 1) {
bf8fe701 1292 debugs(82, 1, "externalAclLookup: " << acl->def->name <<
1293 " user authentication failure (" << ti << ", ch=" << ch << ")");
c8e7608c 1294 callback(callback_data, NULL);
1295 return;
1296 }
3265364b 1297 debugs(82, 3, "aclMatchExternal: " << acl->def->name << " user is authenticated.");
c8e7608c 1298 }
1299
1300 const char *key = makeExternalAclKey(ch, acl);
62e76326 1301
d9572179 1302 if (!key) {
bf8fe701 1303 debugs(82, 1, "externalAclLookup: lookup in '" << def->name <<
1304 "', prerequisit failure (ch=" << ch << ")");
62e76326 1305 callback(callback_data, NULL);
1306 return;
d9572179 1307 }
62e76326 1308
bf8fe701 1309 debugs(82, 2, "externalAclLookup: lookup in '" << def->name << "' for '" << key << "'");
1310
c8e7608c 1311 external_acl_entry *entry = static_cast<external_acl_entry *>(hash_lookup(def->cache, key));
1312
47b0c1fa 1313 if (entry && external_acl_entry_expired(def, entry))
1314 entry = NULL;
62e76326 1315
47b0c1fa 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);
62e76326 1319
47b0c1fa 1320 if (strcmp(key, oldstatetmp->key) == 0) {
1321 oldstate = oldstatetmp;
1322 break;
1323 }
1324 }
62e76326 1325
47b0c1fa 1326 if (entry && external_acl_grace_expired(def, entry)) {
1327 if (oldstate) {
bf8fe701 1328 debugs(82, 4, "externalAclLookup: in grace period, but already pending lookup ('" << key << "', ch=" << ch << ")");
62e76326 1329 callback(callback_data, entry);
62e76326 1330 return;
47b0c1fa 1331 } else {
ab321f4b 1332 graceful = 1; // grace expired, (neg)ttl did not, and we must start a new lookup.
62e76326 1333 }
d9572179 1334 }
62e76326 1335
ab321f4b 1336 // The entry is in the cache, grace_ttl did not expired.
47b0c1fa 1337 if (!graceful && entry && !external_acl_grace_expired(def, entry)) {
1338 /* Should not really happen, but why not.. */
1339 callback(callback_data, entry);
bf8fe701 1340 debugs(82, 4, "externalAclLookup: no lookup pending for '" << key << "', and grace not expired");
1341 debugs(82, 4, "externalAclLookup: (what tha' hell?)");
47b0c1fa 1342 return;
1343 }
62e76326 1344
47b0c1fa 1345 /* No pending lookup found. Sumbit to helper */
1346 state = cbdataAlloc(externalAclState);
62e76326 1347
47b0c1fa 1348 state->def = cbdataReference(def);
62e76326 1349
47b0c1fa 1350 state->key = xstrdup(key);
62e76326 1351
47b0c1fa 1352 if (!graceful) {
1353 state->callback = callback;
1354 state->callback_data = cbdataReference(callback_data);
d9572179 1355 }
62e76326 1356
47b0c1fa 1357 if (oldstate) {
1358 /* Hook into pending lookup */
1359 state->queue = oldstate->queue;
1360 oldstate->queue = state;
1361 } else {
1362 /* Check for queue overload */
1363
48d54e4d 1364 if (def->theHelper->stats.queue_size >= (int)def->theHelper->childs.n_running) {
bf8fe701 1365 debugs(82, 1, "externalAclLookup: '" << def->name << "' queue overload (ch=" << ch << ")");
47b0c1fa 1366 cbdataFree(state);
1367 callback(callback_data, entry);
1368 return;
1369 }
1370
1371 /* Send it off to the helper */
2fe7eff9 1372 buf.init();
62e76326 1373
2fe7eff9 1374 buf.Printf("%s\n", key);
62e76326 1375
bf8fe701 1376 debugs(82, 4, "externalAclLookup: looking up for '" << key << "' in '" << def->name << "'.");
ab321f4b 1377
47b0c1fa 1378 helperSubmit(def->theHelper, buf.buf, externalAclHandleReply, state);
62e76326 1379
47b0c1fa 1380 dlinkAdd(state, &state->list, &def->queue);
62e76326 1381
2fe7eff9 1382 buf.clean();
47b0c1fa 1383 }
62e76326 1384
47b0c1fa 1385 if (graceful) {
1386 /* No need to wait during grace period */
bf8fe701 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);
ab321f4b 1390
1391 if (entry != NULL) {
bf8fe701 1392 debugs(82, 4, "externalAclLookup: entry = { date=" <<
1393 (long unsigned int) entry->date << ", result=" <<
a313a9ba
FC
1394 entry->result << ", user=" << entry->user << " tag=" <<
1395 entry->tag << " log=" << entry->log << " }");
bf8fe701 1396
ab321f4b 1397 }
1398
47b0c1fa 1399 callback(callback_data, entry);
1400 return;
1401 }
ab321f4b 1402
bf8fe701 1403 debugs(82, 4, "externalAclLookup: will wait for the result of '" << key <<
1404 "' in '" << def->name << "' (ch=" << ch << ").");
d9572179 1405}
1406
1407static void
1408externalAclStats(StoreEntry * sentry)
1409{
1410 external_acl *p;
1411
1412 for (p = Config.externalAclHelperList; p; p = p->next) {
62e76326 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");
d9572179 1417 }
1418}
1419
6b7d87bb
FC
1420static void
1421externalAclRegisterWithCacheManager(void)
1422{
1423 CacheManager::GetInstance()->
26ac0430
AJ
1424 registerAction("external_acl",
1425 "External ACL stats",
1426 externalAclStats, 0, 1);
6b7d87bb
FC
1427}
1428
d9572179 1429void
1430externalAclInit(void)
1431{
1432 static int firstTimeInit = 1;
1433 external_acl *p;
1434
1435 for (p = Config.externalAclHelperList; p; p = p->next) {
62e76326 1436 if (!p->cache)
30abd221 1437 p->cache = hash_create((HASHCMP *) strcmp, hashPrime(1024), hash4);
62e76326 1438
1439 if (!p->theHelper)
48d54e4d 1440 p->theHelper = new helper(p->name);
62e76326 1441
1442 p->theHelper->cmdline = p->cmdline;
1443
48d54e4d 1444 p->theHelper->childs = p->children;
07eca7e0 1445
62e76326 1446 p->theHelper->ipc_type = IPC_TCP_SOCKET;
1447
cc192b50 1448 p->theHelper->addr = p->local_addr;
1449
62e76326 1450 helperOpenServers(p->theHelper);
d9572179 1451 }
62e76326 1452
d9572179 1453 if (firstTimeInit) {
62e76326 1454 firstTimeInit = 0;
62e76326 1455 CBDATA_INIT_TYPE_FREECB(externalAclState, free_externalAclState);
d9572179 1456 }
ea391f18
FC
1457
1458 externalAclRegisterWithCacheManager();
d9572179 1459}
1460
1461void
1462externalAclShutdown(void)
1463{
1464 external_acl *p;
62e76326 1465
d9572179 1466 for (p = Config.externalAclHelperList; p; p = p->next) {
62e76326 1467 helperShutdown(p->theHelper);
d9572179 1468 }
1469}
225b7b10 1470
1471ExternalACLLookup ExternalACLLookup::instance_;
1472ExternalACLLookup *
1473ExternalACLLookup::Instance()
1474{
1475 return &instance_;
1476}
1477
1478void
1479ExternalACLLookup::checkForAsync(ACLChecklist *checklist)const
1480{
b0dd28ba 1481 /* TODO: optimise this - we probably have a pointer to this
1482 * around somewhere */
97427e90 1483 ACL *acl = ACL::FindByName(AclMatchedName);
ab321f4b 1484 assert(acl);
b0dd28ba 1485 ACLExternal *me = dynamic_cast<ACLExternal *> (acl);
1486 assert (me);
225b7b10 1487 checklist->asyncInProgress(true);
b0dd28ba 1488 ACLExternal::ExternalAclLookup(checklist, me, LookupDone, checklist);
225b7b10 1489}
1490
1491void
1492ExternalACLLookup::LookupDone(void *data, void *result)
1493{
c0941a6a 1494 ACLFilledChecklist *checklist = Filled(static_cast<ACLChecklist*>(data));
225b7b10 1495 checklist->extacl_entry = cbdataReference((external_acl_entry *)result);
1496 checklist->asyncInProgress(false);
c059ed04 1497 checklist->changeState (ACLChecklist::NullState::Instance());
225b7b10 1498 checklist->check();
1499}
b0dd28ba 1500
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
1505 * it's name.
1506 * Be sure that clone is fully functional for that acl class though!
1507 */
1508ACL::Prototype ACLExternal::RegistryProtoype(&ACLExternal::RegistryEntry_, "external");
1509
1510ACLExternal ACLExternal::RegistryEntry_("external");
1511
1512ACL *
1513ACLExternal::clone() const
1514{
1515 return new ACLExternal(*this);
1516}
1517
1518ACLExternal::ACLExternal (char const *theClass) : data (NULL), class_ (xstrdup (theClass))
1519{}
1520
1521ACLExternal::ACLExternal (ACLExternal const & old) : data (NULL), class_ (old.class_ ? xstrdup (old.class_) : NULL)
1522{
1523 /* we don't have copy constructors for the data yet */
1524 assert (!old.data);
1525}
1526
b0dd28ba 1527char const *
1528ACLExternal::typeString() const
1529{
1530 return class_;
1531}
1532
e870b1f8 1533bool
1534ACLExternal::isProxyAuth() const
1535{
1536 return data->def->require_auth;
1537}