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