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