external_acl.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2023 The Squid Software Foundation and contributors
3  *
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.
7  */
8 
9 /* DEBUG: section 82 External ACL */
10 
11 #include "squid.h"
12 #include "acl/Acl.h"
13 #include "acl/FilledChecklist.h"
14 #include "cache_cf.h"
15 #include "client_side.h"
16 #include "client_side_request.h"
17 #include "comm/Connection.h"
18 #include "ConfigParser.h"
19 #include "ExternalACL.h"
20 #include "ExternalACLEntry.h"
21 #include "fde.h"
22 #include "format/Token.h"
23 #include "helper.h"
24 #include "helper/Reply.h"
25 #include "http/Stream.h"
26 #include "HttpHeaderTools.h"
27 #include "HttpReply.h"
28 #include "HttpRequest.h"
29 #include "ip/tools.h"
30 #include "MemBuf.h"
31 #include "mgr/Registration.h"
32 #include "rfc1738.h"
33 #include "SquidConfig.h"
34 #include "SquidString.h"
35 #include "Store.h"
36 #include "tools.h"
37 #include "wordlist.h"
38 #if USE_OPENSSL
39 #include "ssl/ServerBump.h"
40 #include "ssl/support.h"
41 #endif
42 #if USE_AUTH
43 #include "auth/Acl.h"
44 #include "auth/Gadgets.h"
45 #include "auth/UserRequest.h"
46 #endif
47 
48 #ifndef DEFAULT_EXTERNAL_ACL_TTL
49 #define DEFAULT_EXTERNAL_ACL_TTL 1 * 60 * 60
50 #endif
51 #ifndef DEFAULT_EXTERNAL_ACL_CHILDREN
52 #define DEFAULT_EXTERNAL_ACL_CHILDREN 5
53 #endif
54 
55 static void external_acl_cache_delete(external_acl * def, const ExternalACLEntryPointer &entry);
58 static void external_acl_cache_touch(external_acl * def, const ExternalACLEntryPointer &entry);
59 static ExternalACLEntryPointer external_acl_cache_add(external_acl * def, const char *key, ExternalACLEntryData const &data);
60 
61 /******************************************************************
62  * external_acl directive
63  */
64 
66 {
67  /* XXX: These are not really cbdata, but it is an easy way
68  * to get them pooled, refcounted, accounted and freed properly...
69  * Use RefCountable MEMPROXY_CLASS instead
70  */
72 
73 public:
74  external_acl();
75  ~external_acl();
76 
78 
80 
81  void trimCache();
82 
83  bool maybeCacheable(const Acl::Answer &) const;
84 
85  int ttl;
86 
88 
89  int grace;
90 
91  char *name;
92 
94 
96 
98 
100 
102 
104 
106 
108 
110 
111 #if USE_AUTH
112 
118  bool require_auth;
119 #endif
120 
121  Format::Quoting quote; // default quoting to use, set by protocol= parameter
122 
124 };
125 
127 
129  next(nullptr),
131  negative_ttl(-1),
132  grace(1),
133  name(nullptr),
134  format("external_acl_type"),
135  cmdline(nullptr),
137  theHelper(nullptr),
138  cache(nullptr),
139  cache_size(256*1024),
140  cache_entries(0),
141 #if USE_AUTH
142  require_auth(0),
143 #endif
144  quote(Format::LOG_QUOTE_URL)
145 {
147 }
148 
150 {
151  xfree(name);
153 
154  if (theHelper) {
156  theHelper = nullptr;
157  }
158 
159  while (lru_list.tail) {
161  external_acl_cache_delete(this, e);
162  }
163  if (cache)
165 
166  while (next) {
168  next = node->next;
169  node->next = nullptr; // prevent recursion
170  delete node;
171  }
172 }
173 
174 void
176 {
177  char *token = ConfigParser::NextToken();
178 
179  if (!token) {
180  self_destruct();
181  return;
182  }
183 
184  external_acl *a = new external_acl;
185  a->name = xstrdup(token);
186 
187  // Allow supported %macros inside quoted tokens
189  token = ConfigParser::NextToken();
190 
191  /* Parse options */
192  while (token) {
193  if (strncmp(token, "ttl=", 4) == 0) {
194  a->ttl = atoi(token + 4);
195  } else if (strncmp(token, "negative_ttl=", 13) == 0) {
196  a->negative_ttl = atoi(token + 13);
197  } else if (strncmp(token, "children=", 9) == 0) {
198  a->children.n_max = atoi(token + 9);
199  debugs(0, DBG_CRITICAL, "WARNING: external_acl_type option children=N has been deprecated in favor of children-max=N and children-startup=N");
200  } else if (strncmp(token, "children-max=", 13) == 0) {
201  a->children.n_max = atoi(token + 13);
202  } else if (strncmp(token, "children-startup=", 17) == 0) {
203  a->children.n_startup = atoi(token + 17);
204  } else if (strncmp(token, "children-idle=", 14) == 0) {
205  a->children.n_idle = atoi(token + 14);
206  } else if (strncmp(token, "concurrency=", 12) == 0) {
207  a->children.concurrency = atoi(token + 12);
208  } else if (strncmp(token, "queue-size=", 11) == 0) {
209  a->children.queue_size = atoi(token + 11);
210  a->children.defaultQueueSize = false;
211  } else if (strncmp(token, "cache=", 6) == 0) {
212  a->cache_size = atoi(token + 6);
213  } else if (strncmp(token, "grace=", 6) == 0) {
214  a->grace = atoi(token + 6);
215  } else if (strcmp(token, "protocol=2.5") == 0) {
217  } else if (strcmp(token, "protocol=3.0") == 0) {
218  debugs(3, DBG_PARSE_NOTE(2), "WARNING: external_acl_type option protocol=3.0 is deprecated. Remove this from your config.");
220  } else if (strcmp(token, "quote=url") == 0) {
221  debugs(3, DBG_PARSE_NOTE(2), "WARNING: external_acl_type option quote=url is deprecated. Remove this from your config.");
223  } else if (strcmp(token, "quote=shell") == 0) {
224  debugs(3, DBG_PARSE_NOTE(2), "WARNING: external_acl_type option quote=shell is deprecated. Use protocol=2.5 if still needed.");
226 
227  /* INET6: allow admin to configure some helpers explicitly to
228  bind to IPv4/v6 localhost port. */
229  } else if (strcmp(token, "ipv4") == 0) {
230  if ( !a->local_addr.setIPv4() ) {
231  debugs(3, DBG_CRITICAL, "WARNING: Error converting " << a->local_addr << " to IPv4 in " << a->name );
232  }
233  } else if (strcmp(token, "ipv6") == 0) {
234  if (!Ip::EnableIpv6)
235  debugs(3, DBG_CRITICAL, "WARNING: --enable-ipv6 required for external ACL helpers to use IPv6: " << a->name );
236  // else nothing to do.
237  } else {
238  break;
239  }
240 
241  token = ConfigParser::NextToken();
242  }
244 
245  /* check that child startup value is sane. */
246  if (a->children.n_startup > a->children.n_max)
248 
249  /* check that child idle value is sane. */
250  if (a->children.n_idle > a->children.n_max)
251  a->children.n_idle = a->children.n_max;
252  if (a->children.n_idle < 1)
253  a->children.n_idle = 1;
254 
255  if (a->negative_ttl == -1)
256  a->negative_ttl = a->ttl;
257 
258  if (a->children.defaultQueueSize)
259  a->children.queue_size = 2 * a->children.n_max;
260 
261  /* Legacy external_acl_type format parser.
262  * Handles a series of %... tokens where any non-% means
263  * the start of another parameter field (ie the path to binary).
264  */
266  Format::Token **fmt = &a->format.format;
267  bool data_used = false;
268  while (token) {
269  /* stop on first non-% token found */
270  if (*token != '%')
271  break;
272 
273  *fmt = new Format::Token;
274  // these tokens are whitespace delimited
275  (*fmt)->space = true;
276 
277  // set the default encoding to match the protocol= config
278  // this will be overridden by explicit %macro attributes
279  (*fmt)->quote = a->quote;
280 
281  // compatibility for old tokens incompatible with Format::Token syntax
282 #if USE_OPENSSL // do not bother unless we 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;
287  } else if (strncmp(token, "%USER_CA_CERT_", 14) == 0) {
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;
291  } else if (strncmp(token, "%CA_CERT_", 9) == 0) {
292  debugs(82, DBG_PARSE_NOTE(DBG_IMPORTANT), "WARNING: external_acl_type %CA_CERT_* code is obsolete. Use %USER_CA_CERT_* instead");
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
297 #endif
298  if (strncmp(token,"%<{", 3) == 0) {
299  SBuf tmp("%<h");
300  tmp.append(token+2);
301  debugs(82, DBG_PARSE_NOTE(DBG_IMPORTANT), "WARNING: external_acl_type format %<{...} is deprecated. Use " << tmp);
302  const size_t parsedLen = (*fmt)->parse(tmp.c_str(), &quote);
303  assert(parsedLen == tmp.length());
304  assert((*fmt)->type == Format::LFT_REPLY_HEADER ||
305  (*fmt)->type == Format::LFT_REPLY_HEADER_ELEM);
306 
307  } else if (strncmp(token,"%>{", 3) == 0) {
308  SBuf tmp("%>ha");
309  tmp.append(token+2);
310  debugs(82, DBG_PARSE_NOTE(DBG_IMPORTANT), "WARNING: external_acl_type format %>{...} is deprecated. Use " << tmp);
311  const size_t parsedLen = (*fmt)->parse(tmp.c_str(), &quote);
312  assert(parsedLen == tmp.length());
313  assert((*fmt)->type == Format::LFT_ADAPTED_REQUEST_HEADER ||
315 
316  } else {
317  // we can use the Format::Token::parse() method since it
318  // only pulls off one token. Since we already checked
319  // for '%' prefix above this is guaranteed to be a token.
320  const size_t len = (*fmt)->parse(token, &quote);
321  assert(len == strlen(token));
322  }
323 
324  // process special token-specific actions (only if necessary)
325 #if USE_AUTH
326  if ((*fmt)->type == Format::LFT_USER_LOGIN)
327  a->require_auth = true;
328 #endif
329 
330  if ((*fmt)->type == Format::LFT_EXT_ACL_DATA)
331  data_used = true;
332 
333  fmt = &((*fmt)->next);
334  token = ConfigParser::NextToken();
335  }
336 
337  /* There must be at least one format token */
338  if (!a->format.format) {
339  delete a;
340  self_destruct();
341  return;
342  }
343 
344  // format has implicit %DATA on the end if not used explicitly
345  if (!data_used) {
346  *fmt = new Format::Token;
347  (*fmt)->type = Format::LFT_EXT_ACL_DATA;
348  (*fmt)->quote = Format::LOG_QUOTE_NONE;
349  }
350 
351  /* helper */
352  if (!token) {
353  delete a;
354  self_destruct();
355  return;
356  }
357 
358  wordlistAdd(&a->cmdline, token);
359 
360  /* arguments */
361  parse_wordlist(&a->cmdline);
362 
363  while (*list)
364  list = &(*list)->next;
365 
366  *list = a;
367 }
368 
369 void
370 dump_externalAclHelper(StoreEntry * sentry, const char *name, const external_acl * list)
371 {
372  const external_acl *node;
373  const wordlist *word;
374 
375  for (node = list; node; node = node->next) {
376  storeAppendPrintf(sentry, "%s %s", name, node->name);
377 
378  if (!node->local_addr.isIPv6())
379  storeAppendPrintf(sentry, " ipv4");
380  else
381  storeAppendPrintf(sentry, " ipv6");
382 
383  if (node->ttl != DEFAULT_EXTERNAL_ACL_TTL)
384  storeAppendPrintf(sentry, " ttl=%d", node->ttl);
385 
386  if (node->negative_ttl != node->ttl)
387  storeAppendPrintf(sentry, " negative_ttl=%d", node->negative_ttl);
388 
389  if (node->grace)
390  storeAppendPrintf(sentry, " grace=%d", node->grace);
391 
392  if (node->children.n_max != DEFAULT_EXTERNAL_ACL_CHILDREN)
393  storeAppendPrintf(sentry, " children-max=%d", node->children.n_max);
394 
395  if (node->children.n_startup != 0) // sync with helper/ChildConfig.cc default
396  storeAppendPrintf(sentry, " children-startup=%d", node->children.n_startup);
397 
398  if (node->children.n_idle != 1) // sync with helper/ChildConfig.cc default
399  storeAppendPrintf(sentry, " children-idle=%d", node->children.n_idle);
400 
401  if (node->children.concurrency != 0)
402  storeAppendPrintf(sentry, " concurrency=%d", node->children.concurrency);
403 
404  if (node->cache)
405  storeAppendPrintf(sentry, " cache=%d", node->cache_size);
406 
407  if (node->quote == Format::LOG_QUOTE_SHELL)
408  storeAppendPrintf(sentry, " protocol=2.5");
409 
410  node->format.dump(sentry, nullptr, false);
411 
412  for (word = node->cmdline; word; word = word->next)
413  storeAppendPrintf(sentry, " %s", word->key);
414 
415  storeAppendPrintf(sentry, "\n");
416  }
417 }
418 
419 void
421 {
422  delete *list;
423  *list = nullptr;
424 }
425 
426 static external_acl *
427 find_externalAclHelper(const char *name)
428 {
430 
432  if (strcmp(node->name, name) == 0)
433  return node;
434  }
435 
436  return nullptr;
437 }
438 
439 void
441 {
442  trimCache();
443  assert(anEntry != nullptr);
444  assert (anEntry->def == nullptr);
445  anEntry->def = this;
446  ExternalACLEntry *e = const_cast<ExternalACLEntry *>(anEntry.getRaw()); // XXX: make hash a std::map of Pointer.
447  hash_join(cache, e);
448  dlinkAdd(e, &e->lru, &lru_list);
449  e->lock(); //cbdataReference(e); // lock it on behalf of the hash
450  ++cache_entries;
451 }
452 
453 void
455 {
456  if (cache_size && cache_entries >= cache_size) {
458  external_acl_cache_delete(this, e);
459  }
460 }
461 
462 bool
464 {
465  if (cache_size <= 0)
466  return false; // cache is disabled
467 
468  if (result == ACCESS_DUNNO)
469  return false; // non-cacheable response
470 
471  if ((result.allowed() ? ttl : negative_ttl) <= 0)
472  return false; // not caching this type of response
473 
474  return true;
475 }
476 
477 /******************************************************************
478  * external acl type
479  */
480 
482 {
484 
485 public:
486  explicit external_acl_data(external_acl * const aDef): def(cbdataReference(aDef)), arguments(nullptr) {}
488 
489  external_acl *def;
490  SBuf name;
492 };
493 
495 
497 {
500 }
501 
502 void
504 {
505  if (data) {
506  self_destruct();
507  return;
508  }
509 
510  char *token = ConfigParser::strtokFile();
511 
512  if (!token) {
513  self_destruct();
514  return;
515  }
516 
518 
519  if (!data->def) {
520  delete data;
521  self_destruct();
522  return;
523  }
524 
525  // def->name is the name of the external_acl_type.
526  // this is the name of the 'acl' directive being tested
527  data->name = name;
528 
529  while ((token = ConfigParser::strtokFile())) {
530  wordlistAdd(&data->arguments, token);
531  }
532 }
533 
534 bool
536 {
537 #if USE_AUTH
538  if (data->def->require_auth) {
539  if (authenticateSchemeCount() == 0) {
540  debugs(28, DBG_CRITICAL, "ERROR: Cannot use proxy auth because no authentication schemes were compiled.");
541  return false;
542  }
543 
544  if (authenticateActiveSchemeCount() == 0) {
545  debugs(28, DBG_CRITICAL, "ERROR: Cannot use proxy auth because no authentication schemes are fully configured.");
546  return false;
547  }
548  }
549 #endif
550 
551  return true;
552 }
553 
554 bool
556 {
557  return false;
558 }
559 
561 {
562  delete data;
563  xfree(class_);
564 }
565 
566 static void
568 {
569  if (req) {
570 #if USE_AUTH
571  if (entry->user.size())
572  req->extacl_user = entry->user;
573 
574  if (entry->password.size())
575  req->extacl_passwd = entry->password;
576 #endif
577  if (!req->tag.size())
578  req->tag = entry->tag;
579 
580  if (entry->log.size())
581  req->extacl_log = entry->log;
582 
583  if (entry->message.size())
584  req->extacl_message = entry->message;
585 
586  // attach the helper kv-pair to the transaction
587  UpdateRequestNotes(req->clientConnectionManager.get(), *req, entry->notes);
588  }
589 }
590 
591 // TODO: Diff reduction. Rename this helper method to match_() or similar.
594 {
595  // Despite its external_acl C++ type name, acl->def is not an ACL (i.e. not
596  // a reference-counted Acl::Node) and gets invalidated by reconfiguration.
597  // TODO: RefCount external_acl, so that we do not have to bail here.
598  if (!cbdataReferenceValid(acl->def)) {
599  debugs(82, 3, "cannot resume matching; external_acl gone");
600  return ACCESS_DUNNO;
601  }
602 
603  debugs(82, 9, "acl=\"" << acl->def->name << "\"");
605 
606  external_acl_message = "MISSING REQUIRED INFORMATION";
607 
608  if (entry != nullptr) {
609  if (entry->def == acl->def) {
610  /* Ours, use it.. if the key matches */
611  const char *key = makeExternalAclKey(ch, acl);
612  if (!key)
613  return ACCESS_DUNNO; // insufficient data to continue
614  if (strcmp(key, (char*)entry->key) != 0) {
615  debugs(82, 9, "entry key='" << (char *)entry->key << "', our key='" << key << "' do not match. Discarded.");
616  // too bad. need a new lookup.
617  entry = ch->extacl_entry = nullptr;
618  }
619  } else {
620  /* Not ours.. get rid of it */
621  debugs(82, 9, "entry " << entry << " not valid or not ours. Discarded.");
622  if (entry != nullptr) {
623  debugs(82, 9, "entry def=" << entry->def << ", our def=" << acl->def);
624  const char *key = makeExternalAclKey(ch, acl); // may be nil
625  debugs(82, 9, "entry key='" << (char *)entry->key << "', our key='" << key << "'");
626  }
627  entry = ch->extacl_entry = nullptr;
628  }
629  }
630 
631  if (!entry) {
632  debugs(82, 9, "No helper entry available");
633 #if USE_AUTH
634  if (acl->def->require_auth) {
635  /* Make sure the user is authenticated */
636  debugs(82, 3, acl->def->name << " check user authenticated.");
637  const auto ti = AuthenticateAcl(ch, *this);
638  if (!ti.allowed()) {
639  debugs(82, 2, acl->def->name << " user not authenticated (" << ti << ")");
640  return ti;
641  }
642  debugs(82, 3, acl->def->name << " user is authenticated.");
643  }
644 #endif
645  const char *key = makeExternalAclKey(ch, acl);
646 
647  if (!key) {
648  /* Not sufficient data to process */
649  return ACCESS_DUNNO;
650  }
651 
652  entry = static_cast<ExternalACLEntry *>(hash_lookup(acl->def->cache, key));
653 
654  const ExternalACLEntryPointer staleEntry = entry;
655  if (entry != nullptr && external_acl_entry_expired(acl->def, entry))
656  entry = nullptr;
657 
658  if (entry != nullptr && external_acl_grace_expired(acl->def, entry)) {
659  // refresh in the background
660  startLookup(ch, acl, true);
661  debugs(82, 4, "no need to wait for the refresh of '" <<
662  key << "' in '" << acl->def->name << "' (ch=" << ch << ").");
663  }
664 
665  if (!entry) {
666  debugs(82, 2, acl->def->name << "(\"" << key << "\") = lookup needed");
667 
668  // TODO: All other helpers allow temporary overload. Should not we?
669  if (!acl->def->theHelper->willOverload()) {
670  debugs(82, 2, "\"" << key << "\": queueing a call.");
671  if (!ch->goAsync(StartLookup, *this))
672  debugs(82, 2, "\"" << key << "\": no async support!");
673  debugs(82, 2, "\"" << key << "\": return -1.");
674  return ACCESS_DUNNO; // expired cached or simply absent entry
675  } else {
676  if (!staleEntry) {
677  debugs(82, DBG_IMPORTANT, "WARNING: external ACL '" << acl->def->name <<
678  "' queue full. Request rejected '" << key << "'.");
679  external_acl_message = "SYSTEM TOO BUSY, TRY AGAIN LATER";
680  return ACCESS_DUNNO;
681  } else {
682  debugs(82, DBG_IMPORTANT, "WARNING: external ACL '" << acl->def->name <<
683  "' queue full. Using stale result. '" << key << "'.");
684  entry = staleEntry;
685  /* Fall thru to processing below */
686  }
687  }
688  }
689  }
690 
691  debugs(82, 4, "entry = { date=" <<
692  (long unsigned int) entry->date <<
693  ", result=" << entry->result <<
694  " tag=" << entry->tag <<
695  " log=" << entry->log << " }");
696 #if USE_AUTH
697  debugs(82, 4, "entry user=" << entry->user);
698 #endif
699 
700  external_acl_cache_touch(acl->def, entry);
702 
703  debugs(82, 2, acl->def->name << " = " << entry->result);
704  copyResultsFromEntry(ch->request, entry);
705  return entry->result;
706 }
707 
708 int
710 {
711  auto answer = aclMatchExternal(data, Filled(checklist));
712 
713  // convert to tri-state ACL match 1,0,-1
714  switch (answer) {
715  case ACCESS_ALLOWED:
716  return 1; // match
717 
718  case ACCESS_DENIED:
719  return 0; // non-match
720 
721  case ACCESS_DUNNO:
723  default:
724  // If the answer is not allowed or denied (matches/not matches) and
725  // async authentication is not in progress, then we are done.
726  if (checklist->keepMatching())
727  checklist->markFinished(answer, "aclMatchExternal exception");
728  return -1; // other
729  }
730 }
731 
732 SBufList
734 {
735  external_acl_data const *acl = data;
736  SBufList rv;
737  rv.push_back(SBuf(acl->def->name));
738 
739  for (wordlist *arg = acl->arguments; arg; arg = arg->next) {
740  SBuf s;
741  s.Printf(" %s", arg->key);
742  rv.push_back(s);
743  }
744 
745  return rv;
746 }
747 
748 /******************************************************************
749  * external_acl cache
750  */
751 
752 static void
754 {
755  // this must not be done when nothing is being cached.
756  if (!def->maybeCacheable(entry->result))
757  return;
758 
759  dlinkDelete(&entry->lru, &def->lru_list);
760  ExternalACLEntry *e = const_cast<ExternalACLEntry *>(entry.getRaw()); // XXX: make hash a std::map of Pointer.
761  dlinkAdd(e, &entry->lru, &def->lru_list);
762 }
763 
764 char *
766 {
767  static MemBuf mb;
768  mb.reset();
769 
770  // check for special case tokens in the format
771  for (Format::Token *t = acl_data->def->format.format; t ; t = t->next) {
772 
773  if (t->type == Format::LFT_EXT_ACL_NAME) {
774  // setup for %ACL
775  ch->al->lastAclName = acl_data->name;
776  }
777 
778  if (t->type == Format::LFT_EXT_ACL_DATA) {
779  // setup string for %DATA
780  SBuf sb;
781  for (auto arg = acl_data->arguments; arg; arg = arg->next) {
782  if (sb.length())
783  sb.append(" ", 1);
784 
785  if (acl_data->def->quote == Format::LOG_QUOTE_URL) {
786  const char *quoted = rfc1738_escape(arg->key);
787  sb.append(quoted, strlen(quoted));
788  } else {
789  static MemBuf mb2;
790  mb2.init();
791  strwordquote(&mb2, arg->key);
792  sb.append(mb2.buf, mb2.size);
793  mb2.clean();
794  }
795  }
796 
797  ch->al->lastAclData = sb;
798  }
799  }
800 
801  // assemble the full helper lookup string
802  acl_data->def->format.assemble(mb, ch->al, 0);
803 
804  return mb.buf;
805 }
806 
807 static int
809 {
810  if (def->cache_size <= 0 || entry->result == ACCESS_DUNNO)
811  return 1;
812 
813  if (entry->date + (entry->result.allowed() ? def->ttl : def->negative_ttl) < squid_curtime)
814  return 1;
815  else
816  return 0;
817 }
818 
819 static int
821 {
822  if (def->cache_size <= 0 || entry->result == ACCESS_DUNNO)
823  return 1;
824 
825  int ttl;
826  ttl = entry->result.allowed() ? def->ttl : def->negative_ttl;
827  ttl = (ttl * (100 - def->grace)) / 100;
828 
829  if (entry->date + ttl <= squid_curtime)
830  return 1;
831  else
832  return 0;
833 }
834 
836 external_acl_cache_add(external_acl * def, const char *key, ExternalACLEntryData const & data)
837 {
839 
840  if (!def->maybeCacheable(data.result)) {
841  debugs(82,6, MYNAME);
842 
843  if (data.result == ACCESS_DUNNO) {
844  if (const ExternalACLEntryPointer oldentry = static_cast<ExternalACLEntry *>(hash_lookup(def->cache, key)))
845  external_acl_cache_delete(def, oldentry);
846  }
847  entry = new ExternalACLEntry;
848  entry->key = xstrdup(key);
849  entry->update(data);
850  entry->def = def;
851  return entry;
852  }
853 
854  entry = static_cast<ExternalACLEntry *>(hash_lookup(def->cache, key));
855  debugs(82, 2, "external_acl_cache_add: Adding '" << key << "' = " << data.result);
856 
857  if (entry != nullptr) {
858  debugs(82, 3, "updating existing entry");
859  entry->update(data);
860  external_acl_cache_touch(def, entry);
861  return entry;
862  }
863 
864  entry = new ExternalACLEntry;
865  entry->key = xstrdup(key);
866  entry->update(data);
867 
868  def->add(entry);
869 
870  return entry;
871 }
872 
873 static void
875 {
876  assert(entry != nullptr);
877  assert(def->cache_size > 0 && entry->def == def);
878  ExternalACLEntry *e = const_cast<ExternalACLEntry *>(entry.getRaw()); // XXX: make hash a std::map of Pointer.
879  hash_remove_link(def->cache, e);
880  dlinkDelete(&e->lru, &def->lru_list);
881  e->unlock(); // unlock on behalf of the hash
882  def->cache_entries -= 1;
883 }
884 
885 /******************************************************************
886  * external_acl helpers
887  */
888 
890 {
892 
893 public:
894  externalAclState(external_acl* aDef, const char *aKey) :
895  callback(nullptr),
896  callback_data(nullptr),
897  key(xstrdup(aKey)),
898  def(cbdataReference(aDef)),
899  queue(nullptr)
900  {}
902 
903  EAH *callback;
904  void *callback_data;
905  char *key;
909 };
910 
912 
914 {
915  xfree(key);
918 }
919 
920 /*
921  * The helper program receives queries on stdin, one
922  * per line, and must return the result on on stdout
923  *
924  * General result syntax:
925  *
926  * OK/ERR keyword=value ...
927  *
928  * Keywords:
929  *
930  * user= The users name (login)
931  * message= Message describing the reason
932  * tag= A string tag to be applied to the request that triggered the acl match.
933  * applies to both OK and ERR responses.
934  * Won't override existing request tags.
935  * log= A string to be used in access logging
936  *
937  * Other keywords may be added to the protocol later
938  *
939  * value needs to be URL-encoded or enclosed in double quotes (")
940  * with \-escaping on any whitespace, quotes, or slashes (\).
941  */
942 static void
943 externalAclHandleReply(void *data, const Helper::Reply &reply)
944 {
945  externalAclState *state = static_cast<externalAclState *>(data);
946  externalAclState *next;
947  ExternalACLEntryData entryData;
948 
949  debugs(82, 2, "reply=" << reply);
950 
951  if (reply.result == Helper::Okay)
952  entryData.result = ACCESS_ALLOWED;
953  else if (reply.result == Helper::Error)
954  entryData.result = ACCESS_DENIED;
955  else //BrokenHelper,TimedOut or Unknown. Should not cached.
956  entryData.result = ACCESS_DUNNO;
957 
958  // XXX: make entryData store a proper Helper::Reply object instead of copying.
959 
960  entryData.notes.append(&reply.notes);
961 
962  const char *label = reply.notes.findFirst("tag");
963  if (label != nullptr && *label != '\0')
964  entryData.tag = label;
965 
966  label = reply.notes.findFirst("message");
967  if (label != nullptr && *label != '\0')
968  entryData.message = label;
969 
970  label = reply.notes.findFirst("log");
971  if (label != nullptr && *label != '\0')
972  entryData.log = label;
973 
974 #if USE_AUTH
975  label = reply.notes.findFirst("user");
976  if (label != nullptr && *label != '\0')
977  entryData.user = label;
978 
979  label = reply.notes.findFirst("password");
980  if (label != nullptr && *label != '\0')
981  entryData.password = label;
982 #endif
983 
984  // XXX: This state->def access conflicts with the cbdata validity check
985  // below.
986  dlinkDelete(&state->list, &state->def->queue);
987 
989  if (cbdataReferenceValid(state->def))
990  entry = external_acl_cache_add(state->def, state->key, entryData);
991 
992  do {
993  void *cbdata;
994  if (state->callback && cbdataReferenceValidDone(state->callback_data, &cbdata))
995  state->callback(cbdata, entry);
996 
997  next = state->queue;
998  state->queue = nullptr;
999 
1000  delete state;
1001 
1002  state = next;
1003  } while (state);
1004 }
1005 
1008 void
1010 {
1011  const auto &me = dynamic_cast<const ACLExternal&>(acl);
1012  me.startLookup(&checklist, me.data, false);
1013 }
1014 
1015 // If possible, starts an asynchronous lookup of an external ACL.
1016 // Otherwise, asserts (or bails if background refresh is requested).
1017 void
1019 {
1020  external_acl *def = acl->def;
1021 
1022  const char *key = makeExternalAclKey(ch, acl);
1023  assert(key);
1024 
1025  debugs(82, 2, (inBackground ? "bg" : "fg") << " lookup in '" <<
1026  def->name << "' for '" << key << "'");
1027 
1028  /* Check for a pending lookup to hook into */
1029  // only possible if we are caching results.
1030  externalAclState *oldstate = nullptr;
1031  if (def->cache_size > 0) {
1032  for (dlink_node *node = def->queue.head; node; node = node->next) {
1033  externalAclState *oldstatetmp = static_cast<externalAclState *>(node->data);
1034 
1035  if (strcmp(key, oldstatetmp->key) == 0) {
1036  oldstate = oldstatetmp;
1037  break;
1038  }
1039  }
1040  }
1041 
1042  // A background refresh has no need to piggiback on a pending request:
1043  // When the pending request completes, the cache will be refreshed anyway.
1044  if (oldstate && inBackground) {
1045  debugs(82, 7, "'" << def->name << "' queue is already being refreshed (ch=" << ch << ")");
1046  return;
1047  }
1048 
1049  externalAclState *state = new externalAclState(def, key);
1050 
1051  if (!inBackground) {
1052  state->callback = &LookupDone;
1053  state->callback_data = cbdataReference(ch);
1054  }
1055 
1056  if (oldstate) {
1057  /* Hook into pending lookup */
1058  state->queue = oldstate->queue;
1059  oldstate->queue = state;
1060  } else {
1061  /* No pending lookup found. Sumbit to helper */
1062 
1063  MemBuf buf;
1064  buf.init();
1065  buf.appendf("%s\n", key);
1066  debugs(82, 4, "externalAclLookup: looking up for '" << key << "' in '" << def->name << "'.");
1067 
1068  if (!def->theHelper->trySubmit(buf.buf, externalAclHandleReply, state)) {
1069  debugs(82, 7, "'" << def->name << "' submit to helper failed");
1070  assert(inBackground); // or the caller should have checked
1071  delete state;
1072  return;
1073  }
1074 
1075  dlinkAdd(state, &state->list, &def->queue);
1076 
1077  buf.clean();
1078  }
1079 
1080  debugs(82, 4, "externalAclLookup: will wait for the result of '" << key <<
1081  "' in '" << def->name << "' (ch=" << ch << ").");
1082 }
1083 
1084 static void
1086 {
1087  for (external_acl *p = Config.externalAclHelperList; p; p = p->next) {
1088  storeAppendPrintf(sentry, "External ACL Statistics: %s\n", p->name);
1089  storeAppendPrintf(sentry, "Cache size: %d\n", p->cache->count);
1090  assert(p->theHelper);
1091  p->theHelper->packStatsInto(sentry);
1092  storeAppendPrintf(sentry, "\n");
1093  }
1094 }
1095 
1096 static void
1098 {
1099  Mgr::RegisterAction("external_acl",
1100  "External ACL stats",
1101  externalAclStats, 0, 1);
1102 }
1103 
1104 void
1106 {
1107  for (external_acl *p = Config.externalAclHelperList; p; p = p->next) {
1108  if (!p->cache)
1109  p->cache = hash_create((HASHCMP *) strcmp, hashPrime(1024), hash4);
1110 
1111  if (!p->theHelper)
1112  p->theHelper = Helper::Client::Make("external_acl_type");
1113 
1114  p->theHelper->cmdline = p->cmdline;
1115 
1116  p->theHelper->childs.updateLimits(p->children);
1117 
1118  p->theHelper->ipc_type = IPC_TCP_SOCKET;
1119 
1120  p->theHelper->addr = p->local_addr;
1121 
1122  p->theHelper->openSessions();
1123  }
1124 
1126 }
1127 
1128 void
1130 {
1131  external_acl *p;
1132 
1133  for (p = Config.externalAclHelperList; p; p = p->next) {
1135  }
1136 }
1137 
1139 void
1141 {
1142  ACLFilledChecklist *checklist = Filled(static_cast<ACLChecklist*>(data));
1143  checklist->extacl_entry = result;
1144  checklist->resumeNonBlockingCheck();
1145 }
1146 
1147 ACLExternal::ACLExternal(char const *theClass) : data(nullptr), class_(xstrdup(theClass))
1148 {}
1149 
1150 char const *
1152 {
1153  return class_;
1154 }
1155 
1156 bool
1158 {
1159 #if USE_AUTH
1160  return data->def->require_auth;
1161 #else
1162  return false;
1163 #endif
1164 }
1165 
external_acl * next
Definition: external_acl.cc:79
Format::Format format
Definition: external_acl.cc:95
void trimCache()
void EAH(void *data, const ExternalACLEntryPointer &result)
Definition: ExternalACL.h:56
Definition: parse.c:104
Cbc * get() const
a temporary valid raw Cbc pointer or NULL
Definition: CbcPointer.h:159
char * buf
Definition: MemBuf.h:134
wordlist * cmdline
Definition: external_acl.cc:97
Token * next
Definition: Token.h:73
#define IPC_TCP_SOCKET
Definition: defines.h:89
void wordlistDestroy(wordlist **list)
destroy a wordlist
Definition: wordlist.cc:16
void push_back(char)
Append a single character. The character may be NUL (\0).
Definition: SBuf.cc:208
Helper::Client::Pointer theHelper
void externalAclShutdown(void)
static void copyResultsFromEntry(const HttpRequest::Pointer &req, const ExternalACLEntryPointer &entry)
void appendf(const char *fmt,...) PRINTF_FORMAT_ARG2
Append operation with printf-style arguments.
Definition: Packable.h:61
#define DBG_CRITICAL
Definition: Stream.h:37
unsigned int queue_size
Definition: ChildConfig.h:91
NotePairs notes
list of all kv-pairs returned by the helper
@ LOG_QUOTE_URL
Definition: ByteCode.h:266
const char * findFirst(const char *noteKey) const
Definition: Notes.cc:302
void setLocalhost()
Definition: Address.cc:275
ExternalACLEntryPointer extacl_entry
#define cbdataReferenceValidDone(var, ptr)
Definition: cbdata.h:239
static void StartLookup(ACLFilledChecklist &, const Acl::Node &)
static char * strtokFile()
Definition: ConfigParser.cc:65
@ Error
Definition: ResultCode.h:19
mb_size_t size
Definition: MemBuf.h:135
external_acl_data * data
Definition: ExternalACL.h:49
bool empty() const override
struct node * next
Definition: parse.c:105
@ LFT_USER_LOGIN
Definition: ByteCode.h:152
bool valid() const override
static void externalAclHandleReply(void *data, const Helper::Reply &reply)
const char * external_acl_message
void add(const ExternalACLEntryPointer &)
static void external_acl_cache_touch(external_acl *def, const ExternalACLEntryPointer &entry)
const char * typeString() const override
static void EnableMacros()
Allow macros inside quoted strings.
Definition: ConfigParser.h:144
external_acl * def
void free_externalAclHelper(external_acl **list)
std::list< SBuf > SBufList
Definition: forward.h:22
bool keepMatching() const
Whether we should continue to match tree nodes or stop/pause.
Definition: Checklist.h:96
void storeAppendPrintf(StoreEntry *e, const char *fmt,...)
Definition: store.cc:855
void hash_remove_link(hash_table *, hash_link *)
Definition: hash.cc:220
Acl::Answer aclMatchExternal(external_acl_data *, ACLFilledChecklist *) const
#define CBDATA_CLASS(type)
Definition: cbdata.h:289
void init(mb_size_t szInit, mb_size_t szMax)
Definition: MemBuf.cc:93
Definition: SBuf.h:93
dlink_list queue
unsigned int n_idle
Definition: ChildConfig.h:66
#define DEFAULT_EXTERNAL_ACL_CHILDREN
Definition: external_acl.cc:52
#define xstrdup
Definition: cbdata.cc:37
bool goAsync(AsyncStarter, const Acl::Node &)
Definition: Checklist.cc:104
C * getRaw() const
Definition: RefCount.h:89
int cbdataReferenceValid(const void *p)
Definition: cbdata.cc:270
bool isProxyAuth() const override
AccessLogEntry::Pointer al
info for the future access.log, and external ACL
hash_link * hash_lookup(hash_table *, const void *)
Definition: hash.cc:146
const char * class_
Definition: ExternalACL.h:50
#define rfc1738_escape(x)
Definition: rfc1738.h:52
int HASHCMP(const void *, const void *)
Definition: hash.h:13
Format::Quoting quote
static ExternalACLEntryPointer external_acl_cache_add(external_acl *def, const char *key, ExternalACLEntryData const &data)
HASHHASH hash4
Definition: hash.h:46
Token * format
Definition: Format.h:60
char * makeExternalAclKey(ACLFilledChecklist *, external_acl_data *) const
@ LFT_ADAPTED_REQUEST_HEADER
Definition: ByteCode.h:97
int hashPrime(int n)
Definition: hash.cc:293
#define cbdataReference(var)
Definition: cbdata.h:348
void hashFreeMemory(hash_table *)
Definition: hash.cc:268
ByteCode_t type
Definition: Token.h:50
static Pointer Make(const char *name)
Definition: helper.cc:757
String extacl_user
Definition: HttpRequest.h:178
SBuf lastAclData
string for external_acl_type DATA format code
@ LOG_QUOTE_NONE
Definition: ByteCode.h:263
void self_destruct(void)
Definition: cache_cf.cc:276
int match(ACLChecklist *checklist) override
Matches the actual data in checklist against this Acl::Node.
wordlist * arguments
static int external_acl_grace_expired(external_acl *def, const ExternalACLEntryPointer &entry)
void startLookup(ACLFilledChecklist *, external_acl_data *, bool inBackground) const
bool space
Definition: Token.h:70
void helperShutdown(const Helper::Client::Pointer &hlp)
Definition: helper.cc:769
@ ACCESS_AUTH_REQUIRED
Definition: Acl.h:46
@ LOG_QUOTE_SHELL
Definition: ByteCode.h:267
static void DisableMacros()
Do not allow macros inside quoted strings.
Definition: ConfigParser.h:147
hash_table * cache
externalAclState * queue
static void externalAclStats(StoreEntry *sentry)
#define DBG_PARSE_NOTE(x)
Definition: Stream.h:42
static void LookupDone(void *data, const ExternalACLEntryPointer &)
Called when an async lookup returns.
Helper::ResultCode result
The helper response 'result' field.
Definition: Reply.h:59
void update(ExternalACLEntryData const &)
ACLFilledChecklist * Filled(ACLChecklist *checklist)
convenience and safety wrapper for dynamic_cast<ACLFilledChecklist*>
static external_acl * find_externalAclHelper(const char *name)
Definition: MemBuf.h:23
HttpRequest::Pointer request
external_acl * externalAclHelperList
Definition: SquidConfig.h:503
SBuf & Printf(const char *fmt,...) PRINTF_FORMAT_ARG2
Definition: SBuf.cc:214
void clean()
Definition: MemBuf.cc:110
NotePairs notes
Definition: Reply.h:62
void markFinished(const Acl::Answer &newAnswer, const char *reason)
Definition: Checklist.cc:45
@ LFT_REPLY_HEADER
Definition: ByteCode.h:131
ACLExternal(char const *)
#define assert(EX)
Definition: assert.h:17
@ Okay
Definition: ResultCode.h:18
static void externalAclRegisterWithCacheManager(void)
@ LFT_EXT_ACL_USER_CA_CERT
Definition: ByteCode.h:248
@ LFT_EXT_ACL_DATA
Definition: ByteCode.h:253
unsigned int n_max
Definition: ChildConfig.h:48
static void external_acl_cache_delete(external_acl *def, const ExternalACLEntryPointer &entry)
bool setIPv4()
Definition: Address.cc:244
external_acl * def
#define cbdataReferenceDone(var)
Definition: cbdata.h:357
const char * c_str()
Definition: SBuf.cc:516
external_acl_data(external_acl *const aDef)
#define CBDATA_CLASS_INIT(type)
Definition: cbdata.h:325
size_type length() const
Returns the number of bytes stored in SBuf.
Definition: SBuf.h:419
Helper::ChildConfig children
Definition: external_acl.cc:99
String extacl_log
Definition: HttpRequest.h:182
time_t squid_curtime
Definition: stub_libtime.cc:20
Acl::Answer AuthenticateAcl(ACLChecklist *ch, const Acl::Node &acl)
Definition: Acl.cc:28
SBuf & append(const SBuf &S)
Definition: SBuf.cc:185
#define xfree
static char * NextToken()
@ LFT_EXT_ACL_NAME
Definition: ByteCode.h:252
wordlist * next
Definition: wordlist.h:60
externalAclState(external_acl *aDef, const char *aKey)
void strwordquote(MemBuf *mb, const char *str)
Definition: tools.cc:1080
@ LFT_EXT_ACL_USER_CERT
Definition: ByteCode.h:247
unsigned int n_startup
Definition: ChildConfig.h:57
~ACLExternal() override
@ LFT_ADAPTED_REQUEST_HEADER_ELEM
Definition: ByteCode.h:98
void append(const NotePairs *src)
Append the entries of the src NotePairs list to our list.
Definition: Notes.cc:379
#define DEFAULT_EXTERNAL_ACL_TTL
Definition: external_acl.cc:49
const char * termedBuf() const
Definition: SquidString.h:92
char * key
Definition: wordlist.h:59
void parse_externalAclHelper(external_acl **list)
bool allowed() const
Definition: Acl.h:82
int authenticateSchemeCount(void)
Definition: Gadgets.cc:53
static int external_acl_entry_expired(external_acl *def, const ExternalACLEntryPointer &entry)
Definition: Node.h:25
hash_table * hash_create(HASHCMP *, int, HASHHASH *)
Definition: hash.cc:108
size_type size() const
Definition: SquidString.h:73
SBufList dump() const override
void RegisterAction(char const *action, char const *desc, OBJH *handler, Protected, Atomic, Format)
Definition: Registration.cc:54
bool maybeCacheable(const Acl::Answer &) const
int authenticateActiveSchemeCount(void)
Definition: Gadgets.cc:38
@ ACCESS_ALLOWED
Definition: Acl.h:42
void parse() override
parses node representation in squid.conf; dies on failures
@ ACCESS_DENIED
Definition: Acl.h:41
#define DBG_IMPORTANT
Definition: Stream.h:38
Acl::Answer result
#define MYNAME
Definition: Stream.h:219
void reset()
Definition: MemBuf.cc:129
SBuf lastAclName
string for external_acl_type ACL format code
void assemble(MemBuf &mb, const AccessLogEntryPointer &al, int logSequenceNumber) const
assemble the state information into a formatted line.
Definition: Format.cc:377
NotePairs notes
list of all kv-pairs returned by the helper
@ ACCESS_DUNNO
Definition: Acl.h:43
String extacl_message
Definition: HttpRequest.h:184
@ LFT_REPLY_HEADER_ELEM
Definition: ByteCode.h:132
unsigned int concurrency
Definition: ChildConfig.h:72
dlink_list lru_list
void parse_wordlist(wordlist **list)
Definition: cache_cf.cc:3134
const char * wordlistAdd(wordlist **list, const char *key)
Definition: wordlist.cc:25
int EnableIpv6
Whether IPv6 is supported and type of support.
Definition: tools.h:25
#define debugs(SECTION, LEVEL, CONTENT)
Definition: Stream.h:192
String tag
Definition: HttpRequest.h:176
String extacl_passwd
Definition: HttpRequest.h:180
void hash_join(hash_table *, hash_link *)
Definition: hash.cc:131
Ip::Address local_addr
CbcPointer< ConnStateData > clientConnectionManager
Definition: HttpRequest.h:232
void resumeNonBlockingCheck()
Definition: Checklist.cc:230
class SquidConfig Config
Definition: SquidConfig.cc:12
void UpdateRequestNotes(ConnStateData *csd, HttpRequest &request, NotePairs const &helperNotes)
Definition: HttpRequest.cc:760
void dump_externalAclHelper(StoreEntry *sentry, const char *name, const external_acl *list)
Quoting
Quoting style for a format output.
Definition: ByteCode.h:262
void externalAclInit(void)
SBuf name
Definition: Node.h:81
external_acl * def

 

Introduction

Documentation

Support

Miscellaneous