]>
Commit | Line | Data |
---|---|---|
d9572179 | 1 | /* |
bde978a6 | 2 | * Copyright (C) 1996-2015 The Squid Software Foundation and contributors |
d9572179 | 3 | * |
bbc27441 AJ |
4 | * Squid software is distributed under GPLv2+ license and includes |
5 | * contributions from numerous individuals and organizations. | |
6 | * Please see the COPYING and CONTRIBUTORS files for details. | |
d9572179 | 7 | */ |
8 | ||
bbc27441 AJ |
9 | /* DEBUG: section 82 External ACL */ |
10 | ||
582c2af2 | 11 | #include "squid.h" |
c0941a6a | 12 | #include "acl/Acl.h" |
582c2af2 | 13 | #include "acl/FilledChecklist.h" |
8a01b99e | 14 | #include "cache_cf.h" |
a46d2c0e | 15 | #include "client_side.h" |
4e56d7f6 | 16 | #include "client_side_request.h" |
5c336a3b | 17 | #include "comm/Connection.h" |
2eceb328 | 18 | #include "ConfigParser.h" |
582c2af2 | 19 | #include "ExternalACL.h" |
f9b6ff6e | 20 | #include "ExternalACLEntry.h" |
582c2af2 | 21 | #include "fde.h" |
4e56d7f6 | 22 | #include "format/Token.h" |
aa839030 | 23 | #include "helper.h" |
24438ec5 | 24 | #include "helper/Reply.h" |
a5bac1d2 | 25 | #include "HttpHeaderTools.h" |
582c2af2 FC |
26 | #include "HttpReply.h" |
27 | #include "HttpRequest.h" | |
28 | #include "ip/tools.h" | |
0eb49b6d | 29 | #include "MemBuf.h" |
582c2af2 | 30 | #include "mgr/Registration.h" |
1fa9b1a7 | 31 | #include "rfc1738.h" |
4d5904f7 | 32 | #include "SquidConfig.h" |
f9b6ff6e | 33 | #include "SquidString.h" |
582c2af2 FC |
34 | #include "SquidTime.h" |
35 | #include "Store.h" | |
4e540555 | 36 | #include "tools.h" |
b1bd952a | 37 | #include "URL.h" |
d295d770 | 38 | #include "wordlist.h" |
cb4f4424 | 39 | #if USE_OPENSSL |
cedca6e7 | 40 | #include "ssl/ServerBump.h" |
4db984be CT |
41 | #include "ssl/support.h" |
42 | #endif | |
582c2af2 FC |
43 | #if USE_AUTH |
44 | #include "auth/Acl.h" | |
45 | #include "auth/Gadgets.h" | |
46 | #include "auth/UserRequest.h" | |
47 | #endif | |
48 | #if USE_IDENT | |
49 | #include "ident/AclIdent.h" | |
50 | #endif | |
d9572179 | 51 | |
52 | #ifndef DEFAULT_EXTERNAL_ACL_TTL | |
53 | #define DEFAULT_EXTERNAL_ACL_TTL 1 * 60 * 60 | |
54 | #endif | |
d4f2f353 | 55 | #ifndef DEFAULT_EXTERNAL_ACL_CHILDREN |
56 | #define DEFAULT_EXTERNAL_ACL_CHILDREN 5 | |
d9572179 | 57 | #endif |
58 | ||
c0941a6a | 59 | static char *makeExternalAclKey(ACLFilledChecklist * ch, external_acl_data * acl_data); |
abdd93d0 AJ |
60 | static void external_acl_cache_delete(external_acl * def, const ExternalACLEntryPointer &entry); |
61 | static int external_acl_entry_expired(external_acl * def, const ExternalACLEntryPointer &entry); | |
62 | static int external_acl_grace_expired(external_acl * def, const ExternalACLEntryPointer &entry); | |
63 | static void external_acl_cache_touch(external_acl * def, const ExternalACLEntryPointer &entry); | |
64 | static ExternalACLEntryPointer external_acl_cache_add(external_acl * def, const char *key, ExternalACLEntryData const &data); | |
d9572179 | 65 | |
66 | /****************************************************************** | |
67 | * external_acl directive | |
68 | */ | |
62e76326 | 69 | |
1e5562e3 | 70 | class external_acl |
62e76326 | 71 | { |
f205a92e SM |
72 | /* FIXME: These are not really cbdata, but it is an easy way |
73 | * to get them pooled, refcounted, accounted and freed properly... | |
74 | */ | |
f963b531 | 75 | CBDATA_CLASS(external_acl); |
1e5562e3 | 76 | |
77 | public: | |
f963b531 AJ |
78 | external_acl(); |
79 | ~external_acl(); | |
80 | ||
d9572179 | 81 | external_acl *next; |
1e5562e3 | 82 | |
abdd93d0 | 83 | void add(const ExternalACLEntryPointer &); |
1e5562e3 | 84 | |
85 | void trimCache(); | |
86 | ||
d9572179 | 87 | int ttl; |
1e5562e3 | 88 | |
d9572179 | 89 | int negative_ttl; |
1e5562e3 | 90 | |
47b0c1fa | 91 | int grace; |
92 | ||
d9572179 | 93 | char *name; |
1e5562e3 | 94 | |
4e56d7f6 | 95 | Format::Format format; |
1e5562e3 | 96 | |
d9572179 | 97 | wordlist *cmdline; |
1e5562e3 | 98 | |
76d9b994 | 99 | Helper::ChildConfig children; |
07eca7e0 | 100 | |
e6ccf245 | 101 | helper *theHelper; |
1e5562e3 | 102 | |
d9572179 | 103 | hash_table *cache; |
1e5562e3 | 104 | |
d9572179 | 105 | dlink_list lru_list; |
1e5562e3 | 106 | |
d9572179 | 107 | int cache_size; |
1e5562e3 | 108 | |
d9572179 | 109 | int cache_entries; |
1e5562e3 | 110 | |
d9572179 | 111 | dlink_list queue; |
1e5562e3 | 112 | |
2f1431ea | 113 | #if USE_AUTH |
3265364b AJ |
114 | /** |
115 | * Configuration flag. May only be altered by the configuration parser. | |
116 | * | |
117 | * Indicates that all uses of this external_acl_type helper require authentication | |
118 | * details to be processed. If none are available its a fail match. | |
119 | */ | |
e870b1f8 | 120 | bool require_auth; |
2f1431ea | 121 | #endif |
dc1af3cf | 122 | |
26ac0430 | 123 | enum { |
dc1af3cf | 124 | QUOTE_METHOD_SHELL = 1, |
125 | QUOTE_METHOD_URL | |
2fadd50d | 126 | } quote; |
cc192b50 | 127 | |
b7ac5457 | 128 | Ip::Address local_addr; |
d9572179 | 129 | }; |
130 | ||
f963b531 AJ |
131 | CBDATA_CLASS_INIT(external_acl); |
132 | ||
133 | external_acl::external_acl() : | |
cc8c4af2 | 134 | next(NULL), |
f963b531 AJ |
135 | ttl(DEFAULT_EXTERNAL_ACL_TTL), |
136 | negative_ttl(-1), | |
137 | grace(1), | |
138 | name(NULL), | |
4e56d7f6 | 139 | format("external_acl_type"), |
f963b531 AJ |
140 | cmdline(NULL), |
141 | children(DEFAULT_EXTERNAL_ACL_CHILDREN), | |
142 | theHelper(NULL), | |
143 | cache(NULL), | |
144 | cache_size(256*1024), | |
145 | cache_entries(0), | |
146 | #if USE_AUTH | |
147 | require_auth(0), | |
148 | #endif | |
149 | quote(external_acl::QUOTE_METHOD_URL) | |
d9572179 | 150 | { |
f963b531 AJ |
151 | local_addr.setLocalhost(); |
152 | } | |
62e76326 | 153 | |
f963b531 AJ |
154 | external_acl::~external_acl() |
155 | { | |
156 | xfree(name); | |
f963b531 AJ |
157 | wordlistDestroy(&cmdline); |
158 | ||
159 | if (theHelper) { | |
160 | helperShutdown(theHelper); | |
161 | delete theHelper; | |
162 | theHelper = NULL; | |
d9572179 | 163 | } |
62e76326 | 164 | |
f963b531 AJ |
165 | while (lru_list.tail) { |
166 | ExternalACLEntryPointer e(static_cast<ExternalACLEntry *>(lru_list.tail->data)); | |
167 | external_acl_cache_delete(this, e); | |
168 | } | |
169 | if (cache) | |
170 | hashFreeMemory(cache); | |
171 | ||
172 | while (next) { | |
173 | external_acl *node = next; | |
174 | next = node->next; | |
175 | node->next = NULL; // prevent recursion | |
176 | delete node; | |
abdd93d0 | 177 | } |
d9572179 | 178 | } |
179 | ||
180 | void | |
181 | parse_externalAclHelper(external_acl ** list) | |
182 | { | |
f963b531 AJ |
183 | external_acl *a = new external_acl; |
184 | char *token = ConfigParser::NextToken(); | |
62e76326 | 185 | |
d9572179 | 186 | if (!token) |
62e76326 | 187 | self_destruct(); |
188 | ||
d9572179 | 189 | a->name = xstrdup(token); |
190 | ||
2eceb328 CT |
191 | // Allow supported %macros inside quoted tokens |
192 | ConfigParser::EnableMacros(); | |
193 | token = ConfigParser::NextToken(); | |
62e76326 | 194 | |
d9572179 | 195 | /* Parse options */ |
196 | while (token) { | |
62e76326 | 197 | if (strncmp(token, "ttl=", 4) == 0) { |
198 | a->ttl = atoi(token + 4); | |
199 | } else if (strncmp(token, "negative_ttl=", 13) == 0) { | |
200 | a->negative_ttl = atoi(token + 13); | |
07eca7e0 | 201 | } else if (strncmp(token, "children=", 9) == 0) { |
48d54e4d | 202 | a->children.n_max = atoi(token + 9); |
fa84c01d | 203 | debugs(0, DBG_CRITICAL, "WARNING: external_acl_type option children=N has been deprecated in favor of children-max=N and children-startup=N"); |
48d54e4d AJ |
204 | } else if (strncmp(token, "children-max=", 13) == 0) { |
205 | a->children.n_max = atoi(token + 13); | |
206 | } else if (strncmp(token, "children-startup=", 17) == 0) { | |
207 | a->children.n_startup = atoi(token + 17); | |
208 | } else if (strncmp(token, "children-idle=", 14) == 0) { | |
209 | a->children.n_idle = atoi(token + 14); | |
07eca7e0 | 210 | } else if (strncmp(token, "concurrency=", 12) == 0) { |
48d54e4d | 211 | a->children.concurrency = atoi(token + 12); |
6825b101 CT |
212 | } else if (strncmp(token, "queue-size=", 11) == 0) { |
213 | a->children.queue_size = atoi(token + 11); | |
214 | a->children.defaultQueueSize = false; | |
62e76326 | 215 | } else if (strncmp(token, "cache=", 6) == 0) { |
216 | a->cache_size = atoi(token + 6); | |
47b0c1fa | 217 | } else if (strncmp(token, "grace=", 6) == 0) { |
218 | a->grace = atoi(token + 6); | |
dc1af3cf | 219 | } else if (strcmp(token, "protocol=2.5") == 0) { |
220 | a->quote = external_acl::QUOTE_METHOD_SHELL; | |
221 | } else if (strcmp(token, "protocol=3.0") == 0) { | |
05e52854 AJ |
222 | debugs(3, DBG_PARSE_NOTE(2), "WARNING: external_acl_type option protocol=3.0 is deprecated. Remove this from your config."); |
223 | a->quote = external_acl::QUOTE_METHOD_URL; | |
dc1af3cf | 224 | } else if (strcmp(token, "quote=url") == 0) { |
05e52854 | 225 | debugs(3, DBG_PARSE_NOTE(2), "WARNING: external_acl_type option quote=url is deprecated. Remove this from your config."); |
dc1af3cf | 226 | a->quote = external_acl::QUOTE_METHOD_URL; |
227 | } else if (strcmp(token, "quote=shell") == 0) { | |
05e52854 | 228 | debugs(3, DBG_PARSE_NOTE(2), "WARNING: external_acl_type option quote=shell is deprecated. Use protocol=2.5 if still needed."); |
dc1af3cf | 229 | a->quote = external_acl::QUOTE_METHOD_SHELL; |
cc192b50 | 230 | |
26ac0430 AJ |
231 | /* INET6: allow admin to configure some helpers explicitly to |
232 | bind to IPv4/v6 localhost port. */ | |
cc192b50 | 233 | } else if (strcmp(token, "ipv4") == 0) { |
4dd643d5 | 234 | if ( !a->local_addr.setIPv4() ) { |
fa84c01d | 235 | debugs(3, DBG_CRITICAL, "WARNING: Error converting " << a->local_addr << " to IPv4 in " << a->name ); |
cc192b50 | 236 | } |
237 | } else if (strcmp(token, "ipv6") == 0) { | |
055421ee | 238 | if (!Ip::EnableIpv6) |
fa84c01d | 239 | debugs(3, DBG_CRITICAL, "WARNING: --enable-ipv6 required for external ACL helpers to use IPv6: " << a->name ); |
055421ee | 240 | // else nothing to do. |
62e76326 | 241 | } else { |
242 | break; | |
243 | } | |
244 | ||
2eceb328 | 245 | token = ConfigParser::NextToken(); |
d9572179 | 246 | } |
2eceb328 | 247 | ConfigParser::DisableMacros(); |
62e76326 | 248 | |
6f78f0ed | 249 | /* check that child startup value is sane. */ |
a3cecd6c AJ |
250 | if (a->children.n_startup > a->children.n_max) |
251 | a->children.n_startup = a->children.n_max; | |
48d54e4d | 252 | |
6f78f0ed | 253 | /* check that child idle value is sane. */ |
a3cecd6c AJ |
254 | if (a->children.n_idle > a->children.n_max) |
255 | a->children.n_idle = a->children.n_max; | |
256 | if (a->children.n_idle < 1) | |
257 | a->children.n_idle = 1; | |
48d54e4d | 258 | |
d9572179 | 259 | if (a->negative_ttl == -1) |
62e76326 | 260 | a->negative_ttl = a->ttl; |
d9572179 | 261 | |
e44a3ec4 | 262 | if (a->children.defaultQueueSize) |
6825b101 CT |
263 | a->children.queue_size = 2 * a->children.n_max; |
264 | ||
4e56d7f6 AJ |
265 | /* Legacy external_acl_type format parser. |
266 | * Handles a series of %... tokens where any non-% means | |
267 | * the start of another parameter field (ie the path to binary). | |
268 | */ | |
269 | enum Format::Quoting quote = Format::LOG_QUOTE_NONE; | |
270 | Format::Token **fmt = &a->format.format; | |
712fa21e | 271 | bool data_used = false; |
d9572179 | 272 | while (token) { |
4e56d7f6 | 273 | /* stop on first non-% token found */ |
62e76326 | 274 | if (*token != '%') |
275 | break; | |
276 | ||
4e56d7f6 | 277 | *fmt = new Format::Token; |
09e34608 AJ |
278 | // these tokens are whitespace delimited |
279 | (*fmt)->space = true; | |
4e56d7f6 AJ |
280 | |
281 | // compatibility for old tokens incompatible with Format::Token syntax | |
282 | #if USE_OPENSSL // dont bother if we dont have to. | |
283 | if (strncmp(token, "%USER_CERT_", 11) == 0) { | |
284 | (*fmt)->type = Format::LFT_EXT_ACL_USER_CERT; | |
285 | (*fmt)->data.string = xstrdup(token + 11); | |
286 | (*fmt)->data.header.header = (*fmt)->data.string; | |
f06585e0 | 287 | } else if (strncmp(token, "%USER_CA_CERT_", 14) == 0) { |
4e56d7f6 AJ |
288 | (*fmt)->type = Format::LFT_EXT_ACL_USER_CA_CERT; |
289 | (*fmt)->data.string = xstrdup(token + 14); | |
290 | (*fmt)->data.header.header = (*fmt)->data.string; | |
f06585e0 | 291 | } else if (strncmp(token, "%CA_CERT_", 9) == 0) { |
3650c90e | 292 | debugs(82, DBG_PARSE_NOTE(DBG_IMPORTANT), "WARNING: external_acl_type %CA_CERT_* code is obsolete. Use %USER_CA_CERT_* instead"); |
4e56d7f6 AJ |
293 | (*fmt)->type = Format::LFT_EXT_ACL_USER_CA_CERT; |
294 | (*fmt)->data.string = xstrdup(token + 9); | |
295 | (*fmt)->data.header.header = (*fmt)->data.string; | |
296 | } else | |
a7ad6e4e | 297 | #endif |
4e56d7f6 AJ |
298 | { |
299 | // we can use the Format::Token::parse() method since it | |
300 | // only pulls off one token. Since we already checked | |
301 | // for '%' prefix above this is guaranteed to be a token. | |
302 | const size_t len = (*fmt)->parse(token, "e); | |
303 | assert(len == strlen(token)); | |
304 | } | |
305 | ||
306 | // process special token-specific actions (only if necessary) | |
2f1431ea | 307 | #if USE_AUTH |
4e56d7f6 AJ |
308 | if ((*fmt)->type == Format::LFT_USER_LOGIN) |
309 | a->require_auth = true; | |
2f1431ea | 310 | #endif |
62e76326 | 311 | |
712fa21e AJ |
312 | if ((*fmt)->type == Format::LFT_EXT_ACL_DATA) |
313 | data_used = true; | |
314 | ||
4e56d7f6 | 315 | fmt = &((*fmt)->next); |
2eceb328 | 316 | token = ConfigParser::NextToken(); |
d9572179 | 317 | } |
318 | ||
319 | /* There must be at least one format token */ | |
4e56d7f6 | 320 | if (!a->format.format) |
62e76326 | 321 | self_destruct(); |
d9572179 | 322 | |
712fa21e AJ |
323 | // format has implicit %DATA on the end if not used explicitly |
324 | if (!data_used) { | |
325 | *fmt = new Format::Token; | |
326 | (*fmt)->type = Format::LFT_EXT_ACL_DATA; | |
327 | } | |
328 | ||
d9572179 | 329 | /* helper */ |
330 | if (!token) | |
62e76326 | 331 | self_destruct(); |
332 | ||
d9572179 | 333 | wordlistAdd(&a->cmdline, token); |
334 | ||
335 | /* arguments */ | |
a336d130 | 336 | parse_wordlist(&a->cmdline); |
d9572179 | 337 | |
338 | while (*list) | |
62e76326 | 339 | list = &(*list)->next; |
340 | ||
d9572179 | 341 | *list = a; |
342 | } | |
343 | ||
344 | void | |
345 | dump_externalAclHelper(StoreEntry * sentry, const char *name, const external_acl * list) | |
346 | { | |
347 | const external_acl *node; | |
d9572179 | 348 | const wordlist *word; |
62e76326 | 349 | |
d9572179 | 350 | for (node = list; node; node = node->next) { |
62e76326 | 351 | storeAppendPrintf(sentry, "%s %s", name, node->name); |
352 | ||
4dd643d5 | 353 | if (!node->local_addr.isIPv6()) |
cc192b50 | 354 | storeAppendPrintf(sentry, " ipv4"); |
355 | else | |
356 | storeAppendPrintf(sentry, " ipv6"); | |
357 | ||
62e76326 | 358 | if (node->ttl != DEFAULT_EXTERNAL_ACL_TTL) |
359 | storeAppendPrintf(sentry, " ttl=%d", node->ttl); | |
360 | ||
361 | if (node->negative_ttl != node->ttl) | |
362 | storeAppendPrintf(sentry, " negative_ttl=%d", node->negative_ttl); | |
363 | ||
47b0c1fa | 364 | if (node->grace) |
365 | storeAppendPrintf(sentry, " grace=%d", node->grace); | |
366 | ||
48d54e4d AJ |
367 | if (node->children.n_max != DEFAULT_EXTERNAL_ACL_CHILDREN) |
368 | storeAppendPrintf(sentry, " children-max=%d", node->children.n_max); | |
62e76326 | 369 | |
48d54e4d AJ |
370 | if (node->children.n_startup != 1) |
371 | storeAppendPrintf(sentry, " children-startup=%d", node->children.n_startup); | |
62e76326 | 372 | |
48d54e4d AJ |
373 | if (node->children.n_idle != (node->children.n_max + node->children.n_startup) ) |
374 | storeAppendPrintf(sentry, " children-idle=%d", node->children.n_idle); | |
375 | ||
376 | if (node->children.concurrency) | |
377 | storeAppendPrintf(sentry, " concurrency=%d", node->children.concurrency); | |
07eca7e0 | 378 | |
47b0c1fa | 379 | if (node->cache) |
380 | storeAppendPrintf(sentry, " cache=%d", node->cache_size); | |
381 | ||
05e52854 AJ |
382 | if (node->quote == external_acl::QUOTE_METHOD_SHELL) |
383 | storeAppendPrintf(sentry, " protocol=2.5"); | |
384 | ||
4e56d7f6 | 385 | node->format.dump(sentry, NULL, false); |
62e76326 | 386 | |
387 | for (word = node->cmdline; word; word = word->next) | |
388 | storeAppendPrintf(sentry, " %s", word->key); | |
389 | ||
390 | storeAppendPrintf(sentry, "\n"); | |
d9572179 | 391 | } |
392 | } | |
393 | ||
394 | void | |
395 | free_externalAclHelper(external_acl ** list) | |
396 | { | |
f963b531 AJ |
397 | delete *list; |
398 | *list = NULL; | |
d9572179 | 399 | } |
400 | ||
401 | static external_acl * | |
402 | find_externalAclHelper(const char *name) | |
403 | { | |
404 | external_acl *node; | |
405 | ||
406 | for (node = Config.externalAclHelperList; node; node = node->next) { | |
62e76326 | 407 | if (strcmp(node->name, name) == 0) |
408 | return node; | |
d9572179 | 409 | } |
62e76326 | 410 | |
d9572179 | 411 | return NULL; |
412 | } | |
413 | ||
1e5562e3 | 414 | void |
abdd93d0 | 415 | external_acl::add(const ExternalACLEntryPointer &anEntry) |
1e5562e3 | 416 | { |
417 | trimCache(); | |
abdd93d0 | 418 | assert(anEntry != NULL); |
1e5562e3 | 419 | assert (anEntry->def == NULL); |
420 | anEntry->def = this; | |
abdd93d0 AJ |
421 | ExternalACLEntry *e = const_cast<ExternalACLEntry *>(anEntry.getRaw()); // XXX: make hash a std::map of Pointer. |
422 | hash_join(cache, e); | |
423 | dlinkAdd(e, &e->lru, &lru_list); | |
424 | e->lock(); //cbdataReference(e); // lock it on behalf of the hash | |
95dc7ff4 | 425 | ++cache_entries; |
1e5562e3 | 426 | } |
427 | ||
428 | void | |
429 | external_acl::trimCache() | |
430 | { | |
abdd93d0 AJ |
431 | if (cache_size && cache_entries >= cache_size) { |
432 | ExternalACLEntryPointer e(static_cast<ExternalACLEntry *>(lru_list.tail->data)); | |
433 | external_acl_cache_delete(this, e); | |
434 | } | |
1e5562e3 | 435 | } |
436 | ||
d9572179 | 437 | /****************************************************************** |
438 | * external acl type | |
439 | */ | |
440 | ||
f963b531 AJ |
441 | class external_acl_data |
442 | { | |
443 | CBDATA_CLASS(external_acl_data); | |
444 | ||
445 | public: | |
446 | explicit external_acl_data(external_acl *aDef) : def(cbdataReference(aDef)), name(NULL), arguments(NULL) {} | |
447 | ~external_acl_data(); | |
448 | ||
d9572179 | 449 | external_acl *def; |
ec2d5242 | 450 | const char *name; |
d9572179 | 451 | wordlist *arguments; |
452 | }; | |
453 | ||
f963b531 AJ |
454 | CBDATA_CLASS_INIT(external_acl_data); |
455 | ||
456 | external_acl_data::~external_acl_data() | |
d9572179 | 457 | { |
f963b531 AJ |
458 | xfree(name); |
459 | wordlistDestroy(&arguments); | |
460 | cbdataReferenceDone(def); | |
d9572179 | 461 | } |
462 | ||
463 | void | |
b0dd28ba | 464 | ACLExternal::parse() |
d9572179 | 465 | { |
b0dd28ba | 466 | if (data) |
62e76326 | 467 | self_destruct(); |
468 | ||
16c5ad96 | 469 | char *token = ConfigParser::strtokFile(); |
62e76326 | 470 | |
d9572179 | 471 | if (!token) |
62e76326 | 472 | self_destruct(); |
473 | ||
f963b531 | 474 | data = new external_acl_data(find_externalAclHelper(token)); |
62e76326 | 475 | |
d9572179 | 476 | if (!data->def) |
62e76326 | 477 | self_destruct(); |
478 | ||
ec2d5242 HN |
479 | // def->name is the name of the external_acl_type. |
480 | // this is the name of the 'acl' directive being tested | |
481 | data->name = xstrdup(AclMatchedName); | |
482 | ||
16c5ad96 | 483 | while ((token = ConfigParser::strtokFile())) { |
62e76326 | 484 | wordlistAdd(&data->arguments, token); |
d9572179 | 485 | } |
d9572179 | 486 | } |
487 | ||
4b0f5de8 | 488 | bool |
489 | ACLExternal::valid () const | |
490 | { | |
2f1431ea | 491 | #if USE_AUTH |
4b0f5de8 | 492 | if (data->def->require_auth) { |
493 | if (authenticateSchemeCount() == 0) { | |
fa84c01d | 494 | debugs(28, DBG_CRITICAL, "Can't use proxy auth because no authentication schemes were compiled."); |
4b0f5de8 | 495 | return false; |
496 | } | |
497 | ||
498 | if (authenticateActiveSchemeCount() == 0) { | |
fa84c01d | 499 | debugs(28, DBG_CRITICAL, "Can't use proxy auth because no authentication schemes are fully configured."); |
4b0f5de8 | 500 | return false; |
501 | } | |
502 | } | |
2f1431ea | 503 | #endif |
4b0f5de8 | 504 | |
505 | return true; | |
506 | } | |
507 | ||
508 | bool | |
509 | ACLExternal::empty () const | |
510 | { | |
511 | return false; | |
512 | } | |
513 | ||
b0dd28ba | 514 | ACLExternal::~ACLExternal() |
d9572179 | 515 | { |
f963b531 AJ |
516 | delete data; |
517 | xfree(class_); | |
d9572179 | 518 | } |
519 | ||
1abe0161 | 520 | static void |
abdd93d0 | 521 | copyResultsFromEntry(HttpRequest *req, const ExternalACLEntryPointer &entry) |
1abe0161 AJ |
522 | { |
523 | if (req) { | |
524 | #if USE_AUTH | |
525 | if (entry->user.size()) | |
526 | req->extacl_user = entry->user; | |
527 | ||
528 | if (entry->password.size()) | |
529 | req->extacl_passwd = entry->password; | |
530 | #endif | |
531 | if (!req->tag.size()) | |
532 | req->tag = entry->tag; | |
533 | ||
534 | if (entry->log.size()) | |
535 | req->extacl_log = entry->log; | |
536 | ||
537 | if (entry->message.size()) | |
538 | req->extacl_message = entry->message; | |
a1df1417 NH |
539 | |
540 | // attach the helper kv-pair to the transaction | |
541 | UpdateRequestNotes(req->clientConnectionManager.get(), *req, entry->notes); | |
1abe0161 AJ |
542 | } |
543 | } | |
544 | ||
e5f825ea | 545 | static allow_t |
c0941a6a | 546 | aclMatchExternal(external_acl_data *acl, ACLFilledChecklist *ch) |
d9572179 | 547 | { |
c69ce640 | 548 | debugs(82, 9, HERE << "acl=\"" << acl->def->name << "\""); |
abdd93d0 | 549 | ExternalACLEntryPointer entry = ch->extacl_entry; |
62e76326 | 550 | |
6f58d7d7 AR |
551 | external_acl_message = "MISSING REQUIRED INFORMATION"; |
552 | ||
abdd93d0 AJ |
553 | if (entry != NULL) { |
554 | if (entry->def == acl->def) { | |
ad06254e | 555 | /* Ours, use it.. if the key matches */ |
6f58d7d7 AR |
556 | const char *key = makeExternalAclKey(ch, acl); |
557 | if (!key) | |
558 | return ACCESS_DUNNO; // insufficent data to continue | |
ad06254e | 559 | if (strcmp(key, (char*)entry->key) != 0) { |
abdd93d0 | 560 | debugs(82, 9, "entry key='" << (char *)entry->key << "', our key='" << key << "' dont match. Discarded."); |
ad06254e | 561 | // too bad. need a new lookup. |
abdd93d0 | 562 | entry = ch->extacl_entry = NULL; |
ad06254e | 563 | } |
62e76326 | 564 | } else { |
abdd93d0 AJ |
565 | /* Not ours.. get rid of it */ |
566 | debugs(82, 9, "entry " << entry << " not valid or not ours. Discarded."); | |
567 | if (entry != NULL) { | |
568 | debugs(82, 9, "entry def=" << entry->def << ", our def=" << acl->def); | |
6f58d7d7 | 569 | const char *key = makeExternalAclKey(ch, acl); // may be nil |
abdd93d0 | 570 | debugs(82, 9, "entry key='" << (char *)entry->key << "', our key='" << key << "'"); |
c69ce640 | 571 | } |
abdd93d0 | 572 | entry = ch->extacl_entry = NULL; |
62e76326 | 573 | } |
d9572179 | 574 | } |
62e76326 | 575 | |
d9572179 | 576 | if (!entry) { |
c69ce640 | 577 | debugs(82, 9, HERE << "No helper entry available"); |
2f1431ea | 578 | #if USE_AUTH |
62e76326 | 579 | if (acl->def->require_auth) { |
62e76326 | 580 | /* Make sure the user is authenticated */ |
e5f825ea | 581 | debugs(82, 3, HERE << acl->def->name << " check user authenticated."); |
e0f7153c AR |
582 | const allow_t ti = AuthenticateAcl(ch); |
583 | if (ti != ACCESS_ALLOWED) { | |
e5f825ea | 584 | debugs(82, 2, HERE << acl->def->name << " user not authenticated (" << ti << ")"); |
e0f7153c | 585 | return ti; |
62e76326 | 586 | } |
e5f825ea | 587 | debugs(82, 3, HERE << acl->def->name << " user is authenticated."); |
62e76326 | 588 | } |
2f1431ea | 589 | #endif |
6f58d7d7 | 590 | const char *key = makeExternalAclKey(ch, acl); |
41218131 | 591 | |
592 | if (!key) { | |
593 | /* Not sufficient data to process */ | |
e5f825ea | 594 | return ACCESS_DUNNO; |
41218131 | 595 | } |
596 | ||
abdd93d0 | 597 | entry = static_cast<ExternalACLEntry *>(hash_lookup(acl->def->cache, key)); |
62e76326 | 598 | |
abdd93d0 AJ |
599 | const ExternalACLEntryPointer staleEntry = entry; |
600 | if (entry != NULL && external_acl_entry_expired(acl->def, entry)) | |
e0f7153c AR |
601 | entry = NULL; |
602 | ||
abdd93d0 | 603 | if (entry != NULL && external_acl_grace_expired(acl->def, entry)) { |
e0f7153c AR |
604 | // refresh in the background |
605 | ExternalACLLookup::Start(ch, acl, true); | |
606 | debugs(82, 4, HERE << "no need to wait for the refresh of '" << | |
607 | key << "' in '" << acl->def->name << "' (ch=" << ch << ")."); | |
608 | } | |
609 | ||
610 | if (!entry) { | |
e5f825ea | 611 | debugs(82, 2, HERE << acl->def->name << "(\"" << key << "\") = lookup needed"); |
95a83c22 | 612 | |
6825b101 | 613 | if (!acl->def->theHelper->queueFull()) { |
e5f825ea | 614 | debugs(82, 2, HERE << "\"" << key << "\": queueing a call."); |
6f58d7d7 AR |
615 | if (!ch->goAsync(ExternalACLLookup::Instance())) |
616 | debugs(82, 2, "\"" << key << "\": no async support!"); | |
e5f825ea | 617 | debugs(82, 2, HERE << "\"" << key << "\": return -1."); |
e0f7153c | 618 | return ACCESS_DUNNO; // expired cached or simply absent entry |
47b0c1fa | 619 | } else { |
e0f7153c | 620 | if (!staleEntry) { |
e5f825ea | 621 | debugs(82, DBG_IMPORTANT, "WARNING: external ACL '" << acl->def->name << |
bf8fe701 | 622 | "' queue overload. Request rejected '" << key << "'."); |
4a972fa2 | 623 | external_acl_message = "SYSTEM TOO BUSY, TRY AGAIN LATER"; |
e5f825ea | 624 | return ACCESS_DUNNO; |
47b0c1fa | 625 | } else { |
e5f825ea | 626 | debugs(82, DBG_IMPORTANT, "WARNING: external ACL '" << acl->def->name << |
bf8fe701 | 627 | "' queue overload. Using stale result. '" << key << "'."); |
e0f7153c | 628 | entry = staleEntry; |
47b0c1fa | 629 | /* Fall thru to processing below */ |
630 | } | |
631 | } | |
632 | } | |
d9572179 | 633 | } |
62e76326 | 634 | |
e0f7153c AR |
635 | debugs(82, 4, HERE << "entry = { date=" << |
636 | (long unsigned int) entry->date << | |
637 | ", result=" << entry->result << | |
638 | " tag=" << entry->tag << | |
639 | " log=" << entry->log << " }"); | |
640 | #if USE_AUTH | |
641 | debugs(82, 4, HERE << "entry user=" << entry->user); | |
642 | #endif | |
643 | ||
d9572179 | 644 | external_acl_cache_touch(acl->def, entry); |
a7a42b14 | 645 | external_acl_message = entry->message.termedBuf(); |
4a972fa2 | 646 | |
e5f825ea | 647 | debugs(82, 2, HERE << acl->def->name << " = " << entry->result); |
1abe0161 | 648 | copyResultsFromEntry(ch->request, entry); |
e5f825ea | 649 | return entry->result; |
d9572179 | 650 | } |
651 | ||
b0dd28ba | 652 | int |
653 | ACLExternal::match(ACLChecklist *checklist) | |
654 | { | |
e5f825ea | 655 | allow_t answer = aclMatchExternal(data, Filled(checklist)); |
e5f825ea AJ |
656 | |
657 | // convert to tri-state ACL match 1,0,-1 | |
9b831d57 | 658 | switch (answer) { |
e5f825ea | 659 | case ACCESS_ALLOWED: |
e5f825ea AJ |
660 | return 1; // match |
661 | ||
662 | case ACCESS_DENIED: | |
e5f825ea AJ |
663 | return 0; // non-match |
664 | ||
665 | case ACCESS_DUNNO: | |
666 | case ACCESS_AUTH_REQUIRED: | |
667 | default: | |
e0f7153c | 668 | // If the answer is not allowed or denied (matches/not matches) and |
6f58d7d7 AR |
669 | // async authentication is not in progress, then we are done. |
670 | if (checklist->keepMatching()) | |
e0f7153c | 671 | checklist->markFinished(answer, "aclMatchExternal exception"); |
e5f825ea AJ |
672 | return -1; // other |
673 | } | |
b0dd28ba | 674 | } |
675 | ||
dfad5100 | 676 | SBufList |
b0dd28ba | 677 | ACLExternal::dump() const |
d9572179 | 678 | { |
b0dd28ba | 679 | external_acl_data const *acl = data; |
dfad5100 FC |
680 | SBufList rv; |
681 | rv.push_back(SBuf(acl->def->name)); | |
62e76326 | 682 | |
dfad5100 FC |
683 | for (wordlist *arg = acl->arguments; arg; arg = arg->next) { |
684 | SBuf s; | |
685 | s.Printf(" %s", arg->key); | |
686 | rv.push_back(s); | |
d9572179 | 687 | } |
62e76326 | 688 | |
dfad5100 | 689 | return rv; |
d9572179 | 690 | } |
691 | ||
692 | /****************************************************************** | |
693 | * external_acl cache | |
694 | */ | |
695 | ||
d9572179 | 696 | static void |
abdd93d0 | 697 | external_acl_cache_touch(external_acl * def, const ExternalACLEntryPointer &entry) |
d9572179 | 698 | { |
c69ce640 AJ |
699 | // this must not be done when nothing is being cached. |
700 | if (def->cache_size <= 0 || (def->ttl <= 0 && entry->result == 1) || (def->negative_ttl <= 0 && entry->result != 1)) | |
701 | return; | |
702 | ||
d9572179 | 703 | dlinkDelete(&entry->lru, &def->lru_list); |
abdd93d0 AJ |
704 | ExternalACLEntry *e = const_cast<ExternalACLEntry *>(entry.getRaw()); // XXX: make hash a std::map of Pointer. |
705 | dlinkAdd(e, &entry->lru, &def->lru_list); | |
d9572179 | 706 | } |
707 | ||
708 | static char * | |
c0941a6a | 709 | makeExternalAclKey(ACLFilledChecklist * ch, external_acl_data * acl_data) |
d9572179 | 710 | { |
032785bf | 711 | static MemBuf mb; |
09e34608 | 712 | mb.reset(); |
62e76326 | 713 | |
4e56d7f6 AJ |
714 | // check for special case tokens in the format |
715 | for (Format::Token *t = acl_data->def->format.format; t ; t = t->next) { | |
62e76326 | 716 | |
4e56d7f6 AJ |
717 | if (t->type == Format::LFT_EXT_ACL_NAME) { |
718 | // setup for %ACL | |
719 | safe_free(ch->al->_private.lastAclName); | |
720 | ch->al->_private.lastAclName = xstrdup(acl_data->name); | |
789dda8d CT |
721 | } |
722 | ||
4e56d7f6 AJ |
723 | if (t->type == Format::LFT_EXT_ACL_NAME) { |
724 | // setup string for %DATA | |
725 | SBuf sb; | |
726 | bool first = true; | |
727 | for (auto arg = acl_data->arguments; arg; arg = arg->next) { | |
ec2d5242 HN |
728 | if (!first) |
729 | sb.append(" ", 1); | |
730 | ||
731 | if (acl_data->def->quote == external_acl::QUOTE_METHOD_URL) { | |
732 | const char *quoted = rfc1738_escape(arg->key); | |
733 | sb.append(quoted, strlen(quoted)); | |
734 | } else { | |
735 | static MemBuf mb2; | |
736 | mb2.init(); | |
737 | strwordquote(&mb2, arg->key); | |
738 | sb.append(mb2.buf, mb2.size); | |
739 | mb2.clean(); | |
740 | } | |
741 | ||
4e56d7f6 | 742 | first = false; |
ec2d5242 | 743 | } |
dc1af3cf | 744 | } |
62e76326 | 745 | |
4e56d7f6 | 746 | if (t->type == Format::LFT_USER_IDENT) { |
a51ed963 | 747 | if (!*ch->rfc931) { |
4e56d7f6 AJ |
748 | // if we fail to go async, we still return NULL and the caller |
749 | // will detect the failure in ACLExternal::match(). | |
750 | (void)ch->goAsync(IdentLookup::Instance()); | |
751 | return NULL; | |
ec2d5242 | 752 | } |
ec2d5242 | 753 | } |
d9572179 | 754 | } |
62e76326 | 755 | |
712fa21e AJ |
756 | // generate %DATA token value of this acl lookup |
757 | SBuf sb; | |
758 | for (auto arg = acl_data->arguments; arg; arg = arg->next) { | |
759 | if (sb.length()) | |
760 | sb.append(" ", 1); | |
761 | ||
762 | if (acl_data->def->quote == external_acl::QUOTE_METHOD_URL) { | |
763 | const char *quoted = rfc1738_escape(arg->key); | |
764 | sb.append(quoted, strlen(quoted)); | |
765 | } else { | |
766 | static MemBuf mb2; | |
767 | mb2.init(); | |
768 | strwordquote(&mb2, arg->key); | |
769 | sb.append(mb2.buf, mb2.size); | |
770 | mb2.clean(); | |
771 | } | |
772 | } | |
773 | ch->al->_private.lastAclData = sb.c_str(); | |
774 | ||
775 | // assemble the full helper lookup string | |
4e56d7f6 AJ |
776 | acl_data->def->format.assemble(mb, ch->al, 0); |
777 | ||
d9572179 | 778 | return mb.buf; |
d9572179 | 779 | } |
780 | ||
781 | static int | |
abdd93d0 | 782 | external_acl_entry_expired(external_acl * def, const ExternalACLEntryPointer &entry) |
d9572179 | 783 | { |
c69ce640 AJ |
784 | if (def->cache_size <= 0) |
785 | return 1; | |
786 | ||
d9572179 | 787 | if (entry->date + (entry->result == 1 ? def->ttl : def->negative_ttl) < squid_curtime) |
62e76326 | 788 | return 1; |
d9572179 | 789 | else |
62e76326 | 790 | return 0; |
d9572179 | 791 | } |
62e76326 | 792 | |
47b0c1fa | 793 | static int |
abdd93d0 | 794 | external_acl_grace_expired(external_acl * def, const ExternalACLEntryPointer &entry) |
47b0c1fa | 795 | { |
c69ce640 AJ |
796 | if (def->cache_size <= 0) |
797 | return 1; | |
798 | ||
47b0c1fa | 799 | int ttl; |
800 | ttl = entry->result == 1 ? def->ttl : def->negative_ttl; | |
801 | ttl = (ttl * (100 - def->grace)) / 100; | |
802 | ||
98b70eff | 803 | if (entry->date + ttl <= squid_curtime) |
47b0c1fa | 804 | return 1; |
805 | else | |
806 | return 0; | |
807 | } | |
808 | ||
abdd93d0 | 809 | static ExternalACLEntryPointer |
1e5562e3 | 810 | external_acl_cache_add(external_acl * def, const char *key, ExternalACLEntryData const & data) |
d9572179 | 811 | { |
abdd93d0 | 812 | ExternalACLEntryPointer entry; |
c69ce640 AJ |
813 | |
814 | // do not bother caching this result if TTL is going to expire it immediately | |
815 | if (def->cache_size <= 0 || (def->ttl <= 0 && data.result == 1) || (def->negative_ttl <= 0 && data.result != 1)) { | |
816 | debugs(82,6, HERE); | |
817 | entry = new ExternalACLEntry; | |
818 | entry->key = xstrdup(key); | |
819 | entry->update(data); | |
820 | entry->def = def; | |
821 | return entry; | |
822 | } | |
823 | ||
824 | entry = static_cast<ExternalACLEntry *>(hash_lookup(def->cache, key)); | |
bf8fe701 | 825 | debugs(82, 2, "external_acl_cache_add: Adding '" << key << "' = " << data.result); |
62e76326 | 826 | |
abdd93d0 AJ |
827 | if (entry != NULL) { |
828 | debugs(82, 3, "updating existing entry"); | |
c69ce640 | 829 | entry->update(data); |
62e76326 | 830 | external_acl_cache_touch(def, entry); |
62e76326 | 831 | return entry; |
d9572179 | 832 | } |
62e76326 | 833 | |
1e5562e3 | 834 | entry = new ExternalACLEntry; |
4a8b20e8 | 835 | entry->key = xstrdup(key); |
c69ce640 | 836 | entry->update(data); |
1e5562e3 | 837 | |
6ca34f6f | 838 | def->add(entry); |
1e5562e3 | 839 | |
d9572179 | 840 | return entry; |
841 | } | |
842 | ||
843 | static void | |
abdd93d0 | 844 | external_acl_cache_delete(external_acl * def, const ExternalACLEntryPointer &entry) |
d9572179 | 845 | { |
abdd93d0 | 846 | assert(entry != NULL); |
c69ce640 | 847 | assert(def->cache_size > 0 && entry->def == def); |
abdd93d0 AJ |
848 | ExternalACLEntry *e = const_cast<ExternalACLEntry *>(entry.getRaw()); // XXX: make hash a std::map of Pointer. |
849 | hash_remove_link(def->cache, e); | |
850 | dlinkDelete(&e->lru, &def->lru_list); | |
851 | e->unlock(); // unlock on behalf of the hash | |
d9572179 | 852 | def->cache_entries -= 1; |
d9572179 | 853 | } |
854 | ||
855 | /****************************************************************** | |
856 | * external_acl helpers | |
857 | */ | |
858 | ||
f963b531 AJ |
859 | class externalAclState |
860 | { | |
861 | CBDATA_CLASS(externalAclState); | |
862 | ||
863 | public: | |
864 | externalAclState(external_acl* aDef, const char *aKey) : | |
865 | callback(NULL), | |
866 | callback_data(NULL), | |
867 | key(xstrdup(aKey)), | |
868 | def(cbdataReference(aDef)), | |
869 | queue(NULL) | |
870 | {} | |
871 | ~externalAclState(); | |
62e76326 | 872 | |
d9572179 | 873 | EAH *callback; |
874 | void *callback_data; | |
875 | char *key; | |
876 | external_acl *def; | |
877 | dlink_node list; | |
878 | externalAclState *queue; | |
879 | }; | |
880 | ||
f963b531 AJ |
881 | CBDATA_CLASS_INIT(externalAclState); |
882 | ||
883 | externalAclState::~externalAclState() | |
d9572179 | 884 | { |
f963b531 AJ |
885 | xfree(key); |
886 | cbdataReferenceDone(callback_data); | |
887 | cbdataReferenceDone(def); | |
d9572179 | 888 | } |
889 | ||
d9572179 | 890 | /* |
891 | * The helper program receives queries on stdin, one | |
07eca7e0 | 892 | * per line, and must return the result on on stdout |
d9572179 | 893 | * |
894 | * General result syntax: | |
895 | * | |
896 | * OK/ERR keyword=value ... | |
897 | * | |
898 | * Keywords: | |
899 | * | |
4a972fa2 | 900 | * user= The users name (login) |
901 | * message= Message describing the reason | |
f53969cc SM |
902 | * tag= A string tag to be applied to the request that triggered the acl match. |
903 | * applies to both OK and ERR responses. | |
904 | * Won't override existing request tags. | |
905 | * log= A string to be used in access logging | |
d9572179 | 906 | * |
907 | * Other keywords may be added to the protocol later | |
908 | * | |
05e52854 AJ |
909 | * value needs to be URL-encoded or enclosed in double quotes (") |
910 | * with \-escaping on any whitespace, quotes, or slashes (\). | |
d9572179 | 911 | */ |
d9572179 | 912 | static void |
24438ec5 | 913 | externalAclHandleReply(void *data, const Helper::Reply &reply) |
d9572179 | 914 | { |
e6ccf245 | 915 | externalAclState *state = static_cast<externalAclState *>(data); |
d9572179 | 916 | externalAclState *next; |
1e5562e3 | 917 | ExternalACLEntryData entryData; |
e5f825ea | 918 | entryData.result = ACCESS_DENIED; |
d9572179 | 919 | |
0272dd08 | 920 | debugs(82, 2, HERE << "reply=" << reply); |
d9572179 | 921 | |
2428ce02 | 922 | if (reply.result == Helper::Okay) |
0272dd08 AJ |
923 | entryData.result = ACCESS_ALLOWED; |
924 | // XXX: handle other non-DENIED results better | |
62e76326 | 925 | |
24438ec5 | 926 | // XXX: make entryData store a proper Helper::Reply object instead of copying. |
ab332e27 | 927 | |
a0592634 AJ |
928 | entryData.notes.append(&reply.notes); |
929 | ||
cf9f0261 CT |
930 | const char *label = reply.notes.findFirst("tag"); |
931 | if (label != NULL && *label != '\0') | |
932 | entryData.tag = label; | |
62e76326 | 933 | |
cf9f0261 CT |
934 | label = reply.notes.findFirst("message"); |
935 | if (label != NULL && *label != '\0') | |
936 | entryData.message = label; | |
62e76326 | 937 | |
cf9f0261 CT |
938 | label = reply.notes.findFirst("log"); |
939 | if (label != NULL && *label != '\0') | |
940 | entryData.log = label; | |
62e76326 | 941 | |
2f1431ea | 942 | #if USE_AUTH |
cf9f0261 CT |
943 | label = reply.notes.findFirst("user"); |
944 | if (label != NULL && *label != '\0') | |
945 | entryData.user = label; | |
7bbefa01 | 946 | |
cf9f0261 CT |
947 | label = reply.notes.findFirst("password"); |
948 | if (label != NULL && *label != '\0') | |
949 | entryData.password = label; | |
2f1431ea | 950 | #endif |
62e76326 | 951 | |
d9572179 | 952 | dlinkDelete(&state->list, &state->def->queue); |
62e76326 | 953 | |
abdd93d0 | 954 | ExternalACLEntryPointer entry; |
4960475f | 955 | if (cbdataReferenceValid(state->def)) { |
0272dd08 | 956 | // only cache OK and ERR results. |
2428ce02 | 957 | if (reply.result == Helper::Okay || reply.result == Helper::Error) |
1e5562e3 | 958 | entry = external_acl_cache_add(state->def, state->key, entryData); |
62e76326 | 959 | else { |
abdd93d0 | 960 | const ExternalACLEntryPointer oldentry = static_cast<ExternalACLEntry *>(hash_lookup(state->def->cache, state->key)); |
62e76326 | 961 | |
abdd93d0 | 962 | if (oldentry != NULL) |
2ae2408b | 963 | external_acl_cache_delete(state->def, oldentry); |
62e76326 | 964 | } |
466090ac | 965 | } |
d9572179 | 966 | |
967 | do { | |
62e76326 | 968 | void *cbdata; |
47b0c1fa | 969 | if (state->callback && cbdataReferenceValidDone(state->callback_data, &cbdata)) |
62e76326 | 970 | state->callback(cbdata, entry); |
971 | ||
972 | next = state->queue; | |
f963b531 | 973 | state->queue = NULL; |
d9572179 | 974 | |
f963b531 | 975 | delete state; |
d9572179 | 976 | |
62e76326 | 977 | state = next; |
d9572179 | 978 | } while (state); |
979 | } | |
980 | ||
981 | void | |
e0f7153c | 982 | ACLExternal::ExternalAclLookup(ACLChecklist *checklist, ACLExternal * me) |
d9572179 | 983 | { |
e0f7153c AR |
984 | ExternalACLLookup::Start(checklist, me->data, false); |
985 | } | |
c8e7608c | 986 | |
e0f7153c AR |
987 | void |
988 | ExternalACLLookup::Start(ACLChecklist *checklist, external_acl_data *acl, bool inBackground) | |
989 | { | |
990 | external_acl *def = acl->def; | |
c0f81932 | 991 | |
c0941a6a | 992 | ACLFilledChecklist *ch = Filled(checklist); |
c8e7608c | 993 | const char *key = makeExternalAclKey(ch, acl); |
6f58d7d7 | 994 | assert(key); // XXX: will fail if EXT_ACL_IDENT case needs an async lookup |
62e76326 | 995 | |
e0f7153c AR |
996 | debugs(82, 2, HERE << (inBackground ? "bg" : "fg") << " lookup in '" << |
997 | def->name << "' for '" << key << "'"); | |
62e76326 | 998 | |
47b0c1fa | 999 | /* Check for a pending lookup to hook into */ |
c69ce640 | 1000 | // only possible if we are caching results. |
e0f7153c | 1001 | externalAclState *oldstate = NULL; |
c69ce640 | 1002 | if (def->cache_size > 0) { |
e0f7153c | 1003 | for (dlink_node *node = def->queue.head; node; node = node->next) { |
c69ce640 | 1004 | externalAclState *oldstatetmp = static_cast<externalAclState *>(node->data); |
62e76326 | 1005 | |
c69ce640 AJ |
1006 | if (strcmp(key, oldstatetmp->key) == 0) { |
1007 | oldstate = oldstatetmp; | |
1008 | break; | |
1009 | } | |
47b0c1fa | 1010 | } |
1011 | } | |
62e76326 | 1012 | |
e0f7153c AR |
1013 | // A background refresh has no need to piggiback on a pending request: |
1014 | // When the pending request completes, the cache will be refreshed anyway. | |
1015 | if (oldstate && inBackground) { | |
1016 | debugs(82, 7, HERE << "'" << def->name << "' queue is already being refreshed (ch=" << ch << ")"); | |
47b0c1fa | 1017 | return; |
1018 | } | |
62e76326 | 1019 | |
f963b531 | 1020 | externalAclState *state = new externalAclState(def, key); |
62e76326 | 1021 | |
e0f7153c AR |
1022 | if (!inBackground) { |
1023 | state->callback = &ExternalACLLookup::LookupDone; | |
1024 | state->callback_data = cbdataReference(checklist); | |
d9572179 | 1025 | } |
62e76326 | 1026 | |
47b0c1fa | 1027 | if (oldstate) { |
1028 | /* Hook into pending lookup */ | |
1029 | state->queue = oldstate->queue; | |
1030 | oldstate->queue = state; | |
1031 | } else { | |
e0f7153c AR |
1032 | /* No pending lookup found. Sumbit to helper */ |
1033 | ||
e0f7153c | 1034 | MemBuf buf; |
2fe7eff9 | 1035 | buf.init(); |
4391cd15 | 1036 | buf.appendf("%s\n", key); |
bf8fe701 | 1037 | debugs(82, 4, "externalAclLookup: looking up for '" << key << "' in '" << def->name << "'."); |
ab321f4b | 1038 | |
6825b101 CT |
1039 | if (!def->theHelper->trySubmit(buf.buf, externalAclHandleReply, state)) { |
1040 | debugs(82, 7, HERE << "'" << def->name << "' submit to helper failed"); | |
1041 | assert(inBackground); // or the caller should have checked | |
f963b531 | 1042 | delete state; |
6825b101 CT |
1043 | return; |
1044 | } | |
62e76326 | 1045 | |
47b0c1fa | 1046 | dlinkAdd(state, &state->list, &def->queue); |
62e76326 | 1047 | |
2fe7eff9 | 1048 | buf.clean(); |
47b0c1fa | 1049 | } |
62e76326 | 1050 | |
bf8fe701 | 1051 | debugs(82, 4, "externalAclLookup: will wait for the result of '" << key << |
1052 | "' in '" << def->name << "' (ch=" << ch << ")."); | |
d9572179 | 1053 | } |
1054 | ||
1055 | static void | |
1056 | externalAclStats(StoreEntry * sentry) | |
1057 | { | |
f963b531 | 1058 | for (external_acl *p = Config.externalAclHelperList; p; p = p->next) { |
62e76326 | 1059 | storeAppendPrintf(sentry, "External ACL Statistics: %s\n", p->name); |
1060 | storeAppendPrintf(sentry, "Cache size: %d\n", p->cache->count); | |
bf3e8d5a AJ |
1061 | assert(p->theHelper); |
1062 | p->theHelper->packStatsInto(sentry); | |
62e76326 | 1063 | storeAppendPrintf(sentry, "\n"); |
d9572179 | 1064 | } |
1065 | } | |
1066 | ||
6b7d87bb FC |
1067 | static void |
1068 | externalAclRegisterWithCacheManager(void) | |
1069 | { | |
8822ebee | 1070 | Mgr::RegisterAction("external_acl", |
d9fc6862 A |
1071 | "External ACL stats", |
1072 | externalAclStats, 0, 1); | |
6b7d87bb FC |
1073 | } |
1074 | ||
d9572179 | 1075 | void |
1076 | externalAclInit(void) | |
1077 | { | |
f963b531 | 1078 | for (external_acl *p = Config.externalAclHelperList; p; p = p->next) { |
62e76326 | 1079 | if (!p->cache) |
30abd221 | 1080 | p->cache = hash_create((HASHCMP *) strcmp, hashPrime(1024), hash4); |
62e76326 | 1081 | |
1082 | if (!p->theHelper) | |
48d54e4d | 1083 | p->theHelper = new helper(p->name); |
62e76326 | 1084 | |
1085 | p->theHelper->cmdline = p->cmdline; | |
1086 | ||
1af735c7 | 1087 | p->theHelper->childs.updateLimits(p->children); |
07eca7e0 | 1088 | |
62e76326 | 1089 | p->theHelper->ipc_type = IPC_TCP_SOCKET; |
1090 | ||
cc192b50 | 1091 | p->theHelper->addr = p->local_addr; |
1092 | ||
62e76326 | 1093 | helperOpenServers(p->theHelper); |
d9572179 | 1094 | } |
62e76326 | 1095 | |
ea391f18 | 1096 | externalAclRegisterWithCacheManager(); |
d9572179 | 1097 | } |
1098 | ||
1099 | void | |
1100 | externalAclShutdown(void) | |
1101 | { | |
1102 | external_acl *p; | |
62e76326 | 1103 | |
d9572179 | 1104 | for (p = Config.externalAclHelperList; p; p = p->next) { |
62e76326 | 1105 | helperShutdown(p->theHelper); |
d9572179 | 1106 | } |
1107 | } | |
225b7b10 | 1108 | |
1109 | ExternalACLLookup ExternalACLLookup::instance_; | |
1110 | ExternalACLLookup * | |
1111 | ExternalACLLookup::Instance() | |
1112 | { | |
1113 | return &instance_; | |
1114 | } | |
1115 | ||
1116 | void | |
1117 | ExternalACLLookup::checkForAsync(ACLChecklist *checklist)const | |
1118 | { | |
b0dd28ba | 1119 | /* TODO: optimise this - we probably have a pointer to this |
1120 | * around somewhere */ | |
97427e90 | 1121 | ACL *acl = ACL::FindByName(AclMatchedName); |
ab321f4b | 1122 | assert(acl); |
b0dd28ba | 1123 | ACLExternal *me = dynamic_cast<ACLExternal *> (acl); |
1124 | assert (me); | |
e0f7153c | 1125 | ACLExternal::ExternalAclLookup(checklist, me); |
225b7b10 | 1126 | } |
1127 | ||
e0f7153c | 1128 | /// Called when an async lookup returns |
225b7b10 | 1129 | void |
abdd93d0 | 1130 | ExternalACLLookup::LookupDone(void *data, const ExternalACLEntryPointer &result) |
225b7b10 | 1131 | { |
c0941a6a | 1132 | ACLFilledChecklist *checklist = Filled(static_cast<ACLChecklist*>(data)); |
abdd93d0 | 1133 | checklist->extacl_entry = result; |
6f58d7d7 | 1134 | checklist->resumeNonBlockingCheck(ExternalACLLookup::Instance()); |
225b7b10 | 1135 | } |
b0dd28ba | 1136 | |
1137 | /* This registers "external" in the registry. To do dynamic definitions | |
1138 | * of external ACL's, rather than a static prototype, have a Prototype instance | |
1139 | * prototype in the class that defines each external acl 'class'. | |
1140 | * Then, then the external acl instance is created, it self registers under | |
1141 | * it's name. | |
1142 | * Be sure that clone is fully functional for that acl class though! | |
1143 | */ | |
1144 | ACL::Prototype ACLExternal::RegistryProtoype(&ACLExternal::RegistryEntry_, "external"); | |
1145 | ||
1146 | ACLExternal ACLExternal::RegistryEntry_("external"); | |
1147 | ||
1148 | ACL * | |
1149 | ACLExternal::clone() const | |
1150 | { | |
1151 | return new ACLExternal(*this); | |
1152 | } | |
1153 | ||
86c63190 | 1154 | ACLExternal::ACLExternal(char const *theClass) : data(NULL), class_(xstrdup(theClass)) |
b0dd28ba | 1155 | {} |
1156 | ||
86c63190 | 1157 | ACLExternal::ACLExternal(ACLExternal const & old) : data(NULL), class_(old.class_ ? xstrdup(old.class_) : NULL) |
b0dd28ba | 1158 | { |
1159 | /* we don't have copy constructors for the data yet */ | |
86c63190 | 1160 | assert(!old.data); |
b0dd28ba | 1161 | } |
1162 | ||
b0dd28ba | 1163 | char const * |
1164 | ACLExternal::typeString() const | |
1165 | { | |
1166 | return class_; | |
1167 | } | |
1168 | ||
e870b1f8 | 1169 | bool |
1170 | ACLExternal::isProxyAuth() const | |
1171 | { | |
2f1431ea | 1172 | #if USE_AUTH |
e870b1f8 | 1173 | return data->def->require_auth; |
2f1431ea AJ |
1174 | #else |
1175 | return false; | |
1176 | #endif | |
e870b1f8 | 1177 | } |
f53969cc | 1178 |