]> git.ipfire.org Git - thirdparty/squid.git/blame - src/cache_cf.cc
Use PackableStream for dump_acl (#1573)
[thirdparty/squid.git] / src / cache_cf.cc
CommitLineData
30a4f2a8 1/*
b8ae064d 2 * Copyright (C) 1996-2023 The Squid Software Foundation and contributors
e25c139f 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.
30a4f2a8 7 */
cf5fd929 8
bbc27441
AJ
9/* DEBUG: section 03 Configuration File Parsing */
10
582c2af2 11#include "squid.h"
82b7abe3 12#include "acl/Acl.h"
7f0b3324 13#include "acl/AclDenyInfoList.h"
1328cfb7 14#include "acl/AclSizeLimit.h"
289848ca 15#include "acl/Address.h"
82b7abe3
AJ
16#include "acl/Gadgets.h"
17#include "acl/MethodData.h"
6f58d7d7 18#include "acl/Tree.h"
582c2af2 19#include "anyp/PortCfg.h"
1ca54a54 20#include "anyp/UriScheme.h"
5c112575
AJ
21#include "auth/Config.h"
22#include "auth/Scheme.h"
89736861 23#include "AuthReg.h"
39d7714a 24#include "base/PackableStream.h"
582c2af2 25#include "base/RunnersRegistry.h"
602d9612 26#include "cache_cf.h"
a011edee 27#include "CachePeer.h"
2e24d0bf 28#include "CachePeers.h"
2e6535ab 29#include "ConfigOption.h"
582c2af2
FC
30#include "ConfigParser.h"
31#include "CpuAffinityMap.h"
675b8408 32#include "debug/Messages.h"
582c2af2
FC
33#include "DiskIO/DiskIOModule.h"
34#include "eui/Config.h"
f0c32849 35#include "ExternalACL.h"
582c2af2 36#include "format/Format.h"
a8c7a110 37#include "fqdncache.h"
ecb19f1a 38#include "ftp/Elements.h"
582c2af2 39#include "globals.h"
a5bac1d2 40#include "HttpHeaderTools.h"
1c2b4465 41#include "HttpUpgradeProtocolAccess.h"
7a9d36e3 42#include "icmp/IcmpConfig.h"
582c2af2
FC
43#include "ident/Config.h"
44#include "ip/Intercept.h"
244da4ad 45#include "ip/NfMarkConfig.h"
582c2af2
FC
46#include "ip/QosConfig.h"
47#include "ip/tools.h"
48#include "ipc/Kids.h"
49#include "log/Config.h"
8f645002 50#include "log/CustomLog.h"
582c2af2 51#include "MemBuf.h"
b27668ec 52#include "MessageDelayPools.h"
602d9612 53#include "mgr/ActionPasswordList.h"
582c2af2 54#include "mgr/Registration.h"
602d9612 55#include "neighbors.h"
5844d003 56#include "NeighborTypeDomainList.h"
582c2af2 57#include "Parsing.h"
e8dca475 58#include "pconn.h"
e35609f5 59#include "PeerDigest.h"
e8dca475 60#include "PeerPoolMgr.h"
faf2558a 61#include "redirect.h"
8d9a8184 62#include "RefreshPattern.h"
582c2af2 63#include "rfc1738.h"
58d8264f 64#include "sbuf/List.h"
244da4ad 65#include "sbuf/Stream.h"
4d5904f7 66#include "SquidConfig.h"
f9b6ff6e 67#include "SquidString.h"
d9c7489e 68#include "ssl/ProxyCerts.h"
582c2af2 69#include "Store.h"
2745fea5 70#include "store/Disks.h"
4e540555 71#include "tools.h"
ed6e9fb9 72#include "util.h"
602d9612 73#include "wordlist.h"
b007d04d
FC
74/* wccp2 has its own conditional definitions */
75#include "wccp2.h"
82b7abe3
AJ
76#if USE_ADAPTATION
77#include "adaptation/Config.h"
78#endif
79#if ICAP_CLIENT
80#include "adaptation/icap/Config.h"
81#endif
82#if USE_ECAP
83#include "adaptation/ecap/Config.h"
84#endif
cb4f4424 85#if USE_OPENSSL
95d2589c 86#include "ssl/Config.h"
602d9612 87#include "ssl/support.h"
95d2589c 88#endif
82b7abe3
AJ
89#if USE_SQUID_ESI
90#include "esi/Parser.h"
91#endif
82b7abe3
AJ
92#if SQUID_SNMP
93#include "snmp.h"
94#endif
582c2af2 95
52303a3d 96#if HAVE_GLOB_H
592a09dc 97#include <glob.h>
52303a3d 98#endif
8da861a5 99#include <chrono>
425de4c8 100#include <limits>
f4698e0b 101#include <list>
582c2af2
FC
102#if HAVE_PWD_H
103#include <pwd.h>
104#endif
105#if HAVE_GRP_H
106#include <grp.h>
107#endif
7d17a6a6
EB
108#if HAVE_SYS_SOCKET_H
109#include <sys/socket.h>
110#endif
582c2af2
FC
111#if HAVE_SYS_STAT_H
112#include <sys/stat.h>
113#endif
f4698e0b 114
cb4f4424 115#if USE_OPENSSL
95d2589c
CT
116#include "ssl/gadgets.h"
117#endif
118
62c7f90e 119#if USE_ADAPTATION
62c7f90e 120static void parse_adaptation_service_set_type();
a22e6cd3 121static void parse_adaptation_service_chain_type();
62c7f90e 122static void parse_adaptation_access_type();
62c7f90e
AR
123#endif
124
3a69ddf3 125#if ICAP_CLIENT
26cc52cb
AR
126static void parse_icap_service_type(Adaptation::Icap::Config *);
127static void dump_icap_service_type(StoreEntry *, const char *, const Adaptation::Icap::Config &);
128static void free_icap_service_type(Adaptation::Icap::Config *);
c939dc70
AR
129static void parse_icap_class_type();
130static void parse_icap_access_type();
8277060a
CT
131
132static void parse_icap_service_failure_limit(Adaptation::Icap::Config *);
133static void dump_icap_service_failure_limit(StoreEntry *, const char *, const Adaptation::Icap::Config &);
134static void free_icap_service_failure_limit(Adaptation::Icap::Config *);
3a69ddf3 135#endif
136
21a26d31 137#if USE_ECAP
574b508c
AR
138static void parse_ecap_service_type(Adaptation::Ecap::Config *);
139static void dump_ecap_service_type(StoreEntry *, const char *, const Adaptation::Ecap::Config &);
140static void free_ecap_service_type(Adaptation::Ecap::Config *);
21a26d31
AR
141#endif
142
6a9136ad
FC
143static peer_t parseNeighborType(const char *s);
144
8da861a5
EB
145static const char *const T_NANOSECOND_STR = "nanosecond";
146static const char *const T_MICROSECOND_STR = "microsecond";
fd0f51c4 147static const char *const T_MILLISECOND_STR = "millisecond";
8813e606 148static const char *const T_SECOND_STR = "second";
149static const char *const T_MINUTE_STR = "minute";
150static const char *const T_HOUR_STR = "hour";
151static const char *const T_DAY_STR = "day";
152static const char *const T_WEEK_STR = "week";
153static const char *const T_FORTNIGHT_STR = "fortnight";
154static const char *const T_MONTH_STR = "month";
155static const char *const T_YEAR_STR = "year";
156static const char *const T_DECADE_STR = "decade";
aa0a0c7c 157
9906e724 158static const char *const B_BYTES_STR = "bytes";
159static const char *const B_KBYTES_STR = "KB";
160static const char *const B_MBYTES_STR = "MB";
161static const char *const B_GBYTES_STR = "GB";
162
8da861a5
EB
163// std::chrono::years requires C++20. Do our own rough calculation for now.
164static const double HoursPerYear = 24*365.2522;
165
c59baaa8
EB
166static void parse_cache_log_message(DebugMessages **messages);
167static void dump_cache_log_message(StoreEntry *entry, const char *name, const DebugMessages *messages);
168static void free_cache_log_message(DebugMessages **messages);
169
87ddff6e
FC
170static void parse_access_log(CustomLog ** customlog_definitions);
171static int check_null_access_log(CustomLog *customlog_definitions);
172static void dump_access_log(StoreEntry * entry, const char *name, CustomLog * definitions);
173static void free_access_log(CustomLog ** definitions);
7684c4b1 174
f5b8bbc4 175static void configDoConfigure(void);
8d9a8184 176static void parse_refreshpattern(RefreshPattern **);
f45dd259 177static void parse_u_short(unsigned short * var);
f5b8bbc4 178static void parse_string(char **);
f5b8bbc4 179static void default_all(void);
180static void defaults_if_none(void);
10d914f6 181static void defaults_postscriptum(void);
f5b8bbc4 182static int parse_line(char *);
76f44481 183static void parse_obsolete(const char *);
f5b8bbc4 184static void parseBytesLine(size_t * bptr, const char *units);
e210930b 185static void parseBytesLineSigned(ssize_t * bptr, const char *units);
f5b8bbc4 186static size_t parseBytesUnits(const char *unit);
f5b8bbc4 187static void free_all(void);
94439e4e 188void requirePathnameExists(const char *name, const char *path);
ed7f5615 189static OBJH dump_config;
626096be 190#if USE_HTTP_VIOLATIONS
3b07476b
CT
191static void free_HeaderManglers(HeaderManglers **pm);
192static void dump_http_header_access(StoreEntry * entry, const char *name, const HeaderManglers *manglers);
193static void parse_http_header_access(HeaderManglers **manglers);
194#define free_http_header_access free_HeaderManglers
195static void dump_http_header_replace(StoreEntry * entry, const char *name, const HeaderManglers *manglers);
196static void parse_http_header_replace(HeaderManglers **manglers);
197#define free_http_header_replace free_HeaderManglers
9e8b2f1c 198#endif
f4698e0b
CT
199static void dump_HeaderWithAclList(StoreEntry * entry, const char *name, HeaderWithAclList *headers);
200static void parse_HeaderWithAclList(HeaderWithAclList **header);
201static void free_HeaderWithAclList(HeaderWithAclList **header);
d7f4a0b7
CT
202static void parse_note(Notes *);
203static void dump_note(StoreEntry *, const char *, Notes &);
204static void free_note(Notes *);
7f0b3324
FC
205static void parse_denyinfo(AclDenyInfoList ** var);
206static void dump_denyinfo(StoreEntry * entry, const char *name, AclDenyInfoList * var);
207static void free_denyinfo(AclDenyInfoList ** var);
e1a88700 208
0b0cfcf2 209#if USE_WCCPv2
b7ac5457
AJ
210static void parse_IpAddress_list(Ip::Address_list **);
211static void dump_IpAddress_list(StoreEntry *, const char *, const Ip::Address_list *);
212static void free_IpAddress_list(Ip::Address_list **);
0b0cfcf2 213#if CURRENTLY_UNUSED
b7ac5457 214static int check_null_IpAddress_list(const Ip::Address_list *);
3f38a55e 215#endif /* CURRENTLY_UNUSED */
e1a88700 216#endif /* USE_WCCPv2 */
217
fa720bfb 218static void parsePortCfg(AnyP::PortCfgPointer *, const char *protocol);
65d448bc 219#define parse_PortCfg(l) parsePortCfg((l), token)
fa720bfb
AJ
220static void dump_PortCfg(StoreEntry *, const char *, const AnyP::PortCfgPointer &);
221#define free_PortCfg(h) *(h)=NULL
e1a88700 222
cb4f4424 223#if USE_OPENSSL
aebe6888
CT
224static void parse_sslproxy_cert_sign(sslproxy_cert_sign **cert_sign);
225static void dump_sslproxy_cert_sign(StoreEntry *entry, const char *name, sslproxy_cert_sign *cert_sign);
226static void free_sslproxy_cert_sign(sslproxy_cert_sign **cert_sign);
fb2178bb
CT
227static void parse_sslproxy_cert_adapt(sslproxy_cert_adapt **cert_adapt);
228static void dump_sslproxy_cert_adapt(StoreEntry *entry, const char *name, sslproxy_cert_adapt *cert_adapt);
229static void free_sslproxy_cert_adapt(sslproxy_cert_adapt **cert_adapt);
caf3666d
AR
230static void parse_sslproxy_ssl_bump(acl_access **ssl_bump);
231static void dump_sslproxy_ssl_bump(StoreEntry *entry, const char *name, acl_access *ssl_bump);
232static void free_sslproxy_ssl_bump(acl_access **ssl_bump);
cb4f4424 233#endif /* USE_OPENSSL */
e1a88700 234
ddf5aa2b
CT
235static void parse_ftp_epsv(acl_access **ftp_epsv);
236static void dump_ftp_epsv(StoreEntry *entry, const char *name, acl_access *ftp_epsv);
237static void free_ftp_epsv(acl_access **ftp_epsv);
238
ef1955a5 239static void parse_b_size_t(size_t * var);
47f6e231 240static void parse_b_int64_t(int64_t * var);
270b86af 241
96c2bb61
AR
242static void parse_CpuAffinityMap(CpuAffinityMap **const cpuAffinityMap);
243static void dump_CpuAffinityMap(StoreEntry *const entry, const char *const name, const CpuAffinityMap *const cpuAffinityMap);
244static void free_CpuAffinityMap(CpuAffinityMap **const cpuAffinityMap);
245
ced8def3
AJ
246static void parse_UrlHelperTimeout(SquidConfig::UrlHelperTimeout *);
247static void dump_UrlHelperTimeout(StoreEntry *, const char *, SquidConfig::UrlHelperTimeout &);
248static void free_UrlHelperTimeout(SquidConfig::UrlHelperTimeout *);
32fd6d8a 249
cfcb6b30 250static int parseOneConfigFile(const char *file_name, unsigned int depth);
251
bde7a8ce
CT
252static void parse_configuration_includes_quoted_values(bool *recognizeQuotedValues);
253static void dump_configuration_includes_quoted_values(StoreEntry *const entry, const char *const name, bool recognizeQuotedValues);
254static void free_configuration_includes_quoted_values(bool *recognizeQuotedValues);
3248e962
CT
255static void parse_on_unsupported_protocol(acl_access **access);
256static void dump_on_unsupported_protocol(StoreEntry *entry, const char *name, acl_access *access);
257static void free_on_unsupported_protocol(acl_access **access);
329c128c 258static void ParseAclWithAction(acl_access **access, const Acl::Answer &action, const char *desc, ACL *acl = nullptr);
1c2b4465
CT
259static void parse_http_upgrade_request_protocols(HttpUpgradeProtocolAccess **protoGuards);
260static void dump_http_upgrade_request_protocols(StoreEntry *entry, const char *name, HttpUpgradeProtocolAccess *protoGuards);
261static void free_http_upgrade_request_protocols(HttpUpgradeProtocolAccess **protoGuards);
bde7a8ce 262
a385afc3 263/*
264 * LegacyParser is a parser for legacy code that uses the global
265 * approach. This is static so that it is only exposed to cache_cf.
266 * Other modules needing access to a ConfigParser should have it
267 * provided to them in their parserFOO methods.
268 */
269static ConfigParser LegacyParser = ConfigParser();
a9f20260 270
aa3b39af
AJ
271const char *cfg_directive = nullptr;
272const char *cfg_filename = nullptr;
273int config_lineno = 0;
274char config_input_line[BUFSIZ] = {};
275
0e4e0e7d 276void
0673c0ba 277self_destruct(void)
090089c4 278{
a9f20260 279 LegacyParser.destruct();
6b8e7481 280}
281
76b9daa5 282static void
283SetConfigFilename(char const *file_name, bool is_pipe)
284{
76b9daa5 285 if (is_pipe)
286 cfg_filename = file_name + 1;
328a3f05
AJ
287 else
288 cfg_filename = file_name;
76b9daa5 289}
290
130bff33 291static const char*
292skip_ws(const char* s)
293{
294 while (xisspace(*s))
295 ++s;
296
297 return s;
298}
299
cfcb6b30 300static int
301parseManyConfigFiles(char* files, int depth)
302{
303 int error_count = 0;
aee3523a 304 char* saveptr = nullptr;
52303a3d 305#if HAVE_GLOB
592a09dc 306 char *path;
307 glob_t globbuf;
308 int i;
309 memset(&globbuf, 0, sizeof(globbuf));
aee3523a
AR
310 for (path = strwordtok(files, &saveptr); path; path = strwordtok(nullptr, &saveptr)) {
311 if (glob(path, globbuf.gl_pathc ? GLOB_APPEND : 0, nullptr, &globbuf) != 0) {
b69e9ffa
AJ
312 int xerrno = errno;
313 fatalf("Unable to find configuration file: %s: %s", path, xstrerr(xerrno));
26ac0430
AJ
314 }
315 }
a38ec4b1 316 for (i = 0; i < (int)globbuf.gl_pathc; ++i) {
26ac0430 317 error_count += parseOneConfigFile(globbuf.gl_pathv[i], depth);
cfcb6b30 318 }
592a09dc 319 globfree(&globbuf);
52303a3d 320#else
321 char* file = strwordtok(files, &saveptr);
322 while (file != NULL) {
26ac0430 323 error_count += parseOneConfigFile(file, depth);
a1b1756c 324 file = strwordtok(nullptr, &saveptr);
52303a3d 325 }
326#endif /* HAVE_GLOB */
cfcb6b30 327 return error_count;
328}
329
d4a3e179
AR
330static void
331ReplaceSubstr(char*& str, int& len, unsigned substrIdx, unsigned substrLen, const char* newSubstr)
332{
aee3523a
AR
333 assert(str != nullptr);
334 assert(newSubstr != nullptr);
d4a3e179
AR
335
336 unsigned newSubstrLen = strlen(newSubstr);
337 if (newSubstrLen > substrLen)
338 str = (char*)realloc(str, len - substrLen + newSubstrLen + 1);
339
340 // move tail part including zero
341 memmove(str + substrIdx + newSubstrLen, str + substrIdx + substrLen, len - substrIdx - substrLen + 1);
342 // copy new substring in place
343 memcpy(str + substrIdx, newSubstr, newSubstrLen);
344
345 len = strlen(str);
346}
347
348static void
349SubstituteMacro(char*& line, int& len, const char* macroName, const char* substStr)
350{
aee3523a
AR
351 assert(line != nullptr);
352 assert(macroName != nullptr);
353 assert(substStr != nullptr);
d4a3e179
AR
354 unsigned macroNameLen = strlen(macroName);
355 while (const char* macroPos = strstr(line, macroName)) // we would replace all occurrences
356 ReplaceSubstr(line, len, macroPos - line, macroNameLen, substStr);
357}
358
359static void
360ProcessMacros(char*& line, int& len)
361{
d5f21615 362 SubstituteMacro(line, len, "${service_name}", service_name.c_str());
28bca1f7 363 SubstituteMacro(line, len, "${process_name}", TheKidName.c_str());
d4a3e179
AR
364 SubstituteMacro(line, len, "${process_number}", xitoa(KidIdentifier));
365}
366
a7ea9b13
AR
367static void
368trim_trailing_ws(char* str)
369{
aee3523a 370 assert(str != nullptr);
a7ea9b13
AR
371 unsigned i = strlen(str);
372 while ((i > 0) && xisspace(str[i - 1]))
373 --i;
374 str[i] = '\0';
375}
376
377static const char*
378FindStatement(const char* line, const char* statement)
379{
aee3523a
AR
380 assert(line != nullptr);
381 assert(statement != nullptr);
a7ea9b13
AR
382
383 const char* str = skip_ws(line);
384 unsigned len = strlen(statement);
385 if (strncmp(str, statement, len) == 0) {
386 str += len;
387 if (*str == '\0')
388 return str;
389 else if (xisspace(*str))
390 return skip_ws(str);
391 }
392
aee3523a 393 return nullptr;
a7ea9b13
AR
394}
395
396static bool
397StrToInt(const char* str, long& number)
398{
aee3523a 399 assert(str != nullptr);
a7ea9b13
AR
400
401 char* end;
402 number = strtol(str, &end, 0);
403
404 return (end != str) && (*end == '\0'); // returns true if string contains nothing except number
405}
406
407static bool
408EvalBoolExpr(const char* expr)
409{
aee3523a 410 assert(expr != nullptr);
a7ea9b13
AR
411 if (strcmp(expr, "true") == 0) {
412 return true;
413 } else if (strcmp(expr, "false") == 0) {
414 return false;
415 } else if (const char* equation = strchr(expr, '=')) {
416 const char* rvalue = skip_ws(equation + 1);
417 char* lvalue = (char*)xmalloc(equation - expr + 1);
418 xstrncpy(lvalue, expr, equation - expr + 1);
419 trim_trailing_ws(lvalue);
420
421 long number1;
422 if (!StrToInt(lvalue, number1))
423 fatalf("String is not a integer number: '%s'\n", lvalue);
424 long number2;
425 if (!StrToInt(rvalue, number2))
426 fatalf("String is not a integer number: '%s'\n", rvalue);
427
428 xfree(lvalue);
429 return number1 == number2;
430 }
431 fatalf("Unable to evaluate expression '%s'\n", expr);
432 return false; // this place cannot be reached
433}
434
cfcb6b30 435static int
436parseOneConfigFile(const char *file_name, unsigned int depth)
2546fcb3 437{
aee3523a 438 FILE *fp = nullptr;
cfcb6b30 439 const char *orig_cfg_filename = cfg_filename;
440 const int orig_config_lineno = config_lineno;
aee3523a
AR
441 char *token = nullptr;
442 char *tmp_line = nullptr;
81c161d0 443 int tmp_line_len = 0;
e13ee7ad 444 int err_count = 0;
1741cbad 445 int is_pipe = 0;
cfcb6b30 446
9259c796 447 debugs(3, Important(68), "Processing Configuration File: " << file_name << " (depth " << depth << ")");
cfcb6b30 448 if (depth > 16) {
449 fatalf("WARNING: can't include %s: includes are nested too deeply (>16)!\n", file_name);
450 return 1;
451 }
62e76326 452
1741cbad 453 if (file_name[0] == '!' || file_name[0] == '|') {
62e76326 454 fp = popen(file_name + 1, "r");
455 is_pipe = 1;
1741cbad 456 } else {
62e76326 457 fp = fopen(file_name, "r");
1741cbad 458 }
62e76326 459
b69e9ffa
AJ
460 if (!fp) {
461 int xerrno = errno;
462 fatalf("Unable to open configuration file: %s: %s", file_name, xstrerr(xerrno));
463 }
62e76326 464
be266cb2 465#if _SQUID_WINDOWS_
c4aefe96 466 setmode(fileno(fp), O_TEXT);
467#endif
62e76326 468
76b9daa5 469 SetConfigFilename(file_name, bool(is_pipe));
62e76326 470
270b86af 471 memset(config_input_line, '\0', BUFSIZ);
62e76326 472
270b86af 473 config_lineno = 0;
62e76326 474
c8ea3cc0 475 std::vector<bool> if_states;
270b86af 476 while (fgets(config_input_line, BUFSIZ, fp)) {
a38ec4b1 477 ++config_lineno;
62e76326 478
479 if ((token = strchr(config_input_line, '\n')))
480 *token = '\0';
481
d866e2ad 482 if ((token = strchr(config_input_line, '\r')))
483 *token = '\0';
484
f068973f
AJ
485 // strip any prefix whitespace off the line.
486 const char *p = skip_ws(config_input_line);
487 if (config_input_line != p)
488 memmove(config_input_line, p, strlen(p)+1);
489
62e76326 490 if (strncmp(config_input_line, "#line ", 6) == 0) {
491 static char new_file_name[1024];
492 static char *file;
493 static char new_lineno;
494 token = config_input_line + 6;
495 new_lineno = strtol(token, &file, 0) - 1;
496
497 if (file == token)
f53969cc 498 continue; /* Not a valid #line directive, may be a comment */
62e76326 499
e4755e29 500 while (*file && xisspace((unsigned char) *file))
a38ec4b1 501 ++file;
62e76326 502
503 if (*file) {
504 if (*file != '"')
f53969cc 505 continue; /* Not a valid #line directive, may be a comment */
62e76326 506
507 xstrncpy(new_file_name, file + 1, sizeof(new_file_name));
508
509 if ((token = strchr(new_file_name, '"')))
510 *token = '\0';
511
328a3f05 512 SetConfigFilename(new_file_name, false);
62e76326 513 }
514
515 config_lineno = new_lineno;
516 }
517
518 if (config_input_line[0] == '#')
519 continue;
520
521 if (config_input_line[0] == '\0')
522 continue;
523
130bff33 524 const char* append = tmp_line_len ? skip_ws(config_input_line) : config_input_line;
525
526 size_t append_len = strlen(append);
62e76326 527
130bff33 528 tmp_line = (char*)xrealloc(tmp_line, tmp_line_len + append_len + 1);
81c161d0 529
130bff33 530 strcpy(tmp_line + tmp_line_len, append);
81c161d0 531
130bff33 532 tmp_line_len += append_len;
81c161d0 533
534 if (tmp_line[tmp_line_len-1] == '\\') {
bf8fe701 535 debugs(3, 5, "parseConfigFile: tmp_line='" << tmp_line << "'");
81c161d0 536 tmp_line[--tmp_line_len] = '\0';
537 continue;
538 }
539
a7ea9b13 540 trim_trailing_ws(tmp_line);
d4a3e179 541 ProcessMacros(tmp_line, tmp_line_len);
dad047ab 542 debugs(3, (opt_parse_cfg_only?1:5), "Processing: " << tmp_line);
62e76326 543
a7ea9b13
AR
544 if (const char* expr = FindStatement(tmp_line, "if")) {
545 if_states.push_back(EvalBoolExpr(expr)); // store last if-statement meaning
546 } else if (FindStatement(tmp_line, "endif")) {
547 if (!if_states.empty())
548 if_states.pop_back(); // remove last if-statement meaning
549 else
550 fatalf("'endif' without 'if'\n");
551 } else if (FindStatement(tmp_line, "else")) {
552 if (!if_states.empty())
553 if_states.back() = !if_states.back();
554 else
555 fatalf("'else' without 'if'\n");
556 } else if (if_states.empty() || if_states.back()) { // test last if-statement meaning if present
557 /* Handle includes here */
558 if (tmp_line_len >= 9 && strncmp(tmp_line, "include", 7) == 0 && xisspace(tmp_line[7])) {
559 err_count += parseManyConfigFiles(tmp_line + 8, depth + 1);
8da861a5
EB
560 } else {
561 try {
562 if (!parse_line(tmp_line)) {
563 debugs(3, DBG_CRITICAL, ConfigParser::CurrentLocation() << ": unrecognized: '" << tmp_line << "'");
564 ++err_count;
565 }
566 } catch (...) {
567 // fatal for now
d816f28d 568 debugs(3, DBG_CRITICAL, "ERROR: configuration failure: " << CurrentException);
8da861a5
EB
569 self_destruct();
570 }
a7ea9b13 571 }
26ac0430 572 }
62e76326 573
574 safe_free(tmp_line);
81c161d0 575 tmp_line_len = 0;
576
270b86af 577 }
a7ea9b13
AR
578 if (!if_states.empty())
579 fatalf("if-statement without 'endif'\n");
62e76326 580
1741cbad 581 if (is_pipe) {
62e76326 582 int ret = pclose(fp);
583
584 if (ret != 0)
585 fatalf("parseConfigFile: '%s' failed with exit code %d\n", file_name, ret);
1741cbad 586 } else {
62e76326 587 fclose(fp);
1741cbad 588 }
62e76326 589
328a3f05 590 SetConfigFilename(orig_cfg_filename, false);
cfcb6b30 591 config_lineno = orig_config_lineno;
592
5c6dae44 593 xfree(tmp_line);
cfcb6b30 594 return err_count;
595}
596
4eac3407 597static
cfcb6b30 598int
4eac3407 599parseConfigFileOrThrow(const char *file_name)
cfcb6b30 600{
601 int err_count = 0;
602
bf95c10a 603 debugs(5, 4, MYNAME);
5817ee13 604
cfcb6b30 605 configFreeMemory();
606
5491c11e 607 ACLMethodData::ThePurgeCount = 0;
cfcb6b30 608 default_all();
609
610 err_count = parseOneConfigFile(file_name, 0);
611
f0b19334 612 defaults_if_none();
f9ad0106 613
10d914f6
CT
614 defaults_postscriptum();
615
9c46ca97 616 /*
617 * We must call configDoConfigure() before leave_suid() because
618 * configDoConfigure() is where we turn username strings into
619 * uid values.
620 */
621 configDoConfigure();
622
478c4f2f 623 if (opt_send_signal == -1) {
8822ebee 624 Mgr::RegisterAction("config",
d9fc6862
A
625 "Current Squid Configuration",
626 dump_config,
627 1, 1);
478c4f2f 628 }
629
e13ee7ad 630 return err_count;
f0b19334 631}
270b86af 632
4eac3407
CT
633// TODO: Refactor main.cc to centrally handle (and report) all exceptions.
634int
635parseConfigFile(const char *file_name)
636{
637 try {
638 return parseConfigFileOrThrow(file_name);
639 }
640 catch (const std::exception &ex) {
641 debugs(3, DBG_CRITICAL, "FATAL: bad configuration: " << ex.what());
642 self_destruct();
643 return 1; // not reached
644 }
645}
646
2e6535ab
AR
647/*
648 * The templated functions below are essentially ConfigParser methods. They are
649 * not implemented as such because our generated code calling them is the only
650 * code that can instantiate implementations for each T -- we cannot place these
651 * definitions into ConfigParser.cc unless cf_parser.cci is moved there.
652 */
653
654// TODO: When adding Ts incompatible with this trivial API and implementation,
655// replace both with a ConfigParser-maintained table of seen directives.
656/// whether we have seen (and, hence, configured) the given directive
657template <typename T>
658static bool
659SawDirective(const T &raw)
660{
661 return bool(raw);
662}
663
664/// Sets the given raw SquidConfig data member.
665/// Extracts and interprets parser's configuration tokens.
666template <typename T>
667static void
668ParseDirective(T &raw, ConfigParser &parser)
669{
670 if (SawDirective(raw))
671 parser.rejectDuplicateDirective();
672
673 // TODO: parser.openDirective(directiveName);
674 Must(!raw);
675 raw = Configuration::Component<T>::Parse(parser);
676 Must(raw);
677 parser.closeDirective();
678}
679
680/// reports raw SquidConfig data member configuration using squid.conf syntax
681/// \param name the name of the configuration directive being dumped
682template <typename T>
683static void
684DumpDirective(const T &raw, StoreEntry *entry, const char *name)
685{
686 if (!SawDirective(raw))
687 return; // not configured
688
689 entry->append(name, strlen(name));
690 SBufStream os;
691 Configuration::Component<T>::Print(os, raw);
692 const auto buf = os.buf();
693 if (buf.length()) {
694 entry->append(" ", 1);
695 entry->append(buf.rawContent(), buf.length());
696 }
697 entry->append("\n", 1);
698}
699
700/// frees any resources associated with the given raw SquidConfig data member
701template <typename T>
702static void
703FreeDirective(T &raw)
704{
705 Configuration::Component<T>::Free(raw);
706
707 // While the implementation may change, there is no way to avoid zeroing.
708 // Even migration to a proper SquidConfig class would not help: While
709 // ordinary destructors do not need to zero data members, a SquidConfig
710 // destructor would have to zero to protect any SquidConfig::x destruction
711 // code from accidentally dereferencing an already destroyed Config.y.
712 static_assert(std::is_trivial<T>::value, "SquidConfig member is trivial");
713 memset(&raw, 0, sizeof(raw));
714}
715
f0b19334 716static void
717configDoConfigure(void)
718{
871cbc7d 719 Config2.clear();
7021844c 720 /* init memory as early as possible */
721 memConfigure();
270b86af 722 /* Sanity checks */
62e76326 723
47df1aa7
AJ
724 if (Debug::rotateNumber < 0) {
725 Debug::rotateNumber = Config.Log.rotateNumber;
726 }
727
5aecb102 728#if SIZEOF_OFF_T <= 4
729 if (Config.Store.maxObjectSize > 0x7FFF0000) {
fa84c01d 730 debugs(3, DBG_CRITICAL, "WARNING: This Squid binary can not handle files larger than 2GB. Limiting maximum_object_size to just below 2GB");
26ac0430 731 Config.Store.maxObjectSize = 0x7FFF0000;
5aecb102 732 }
733#endif
62e76326 734
d3caee79 735 if (Config.onoff.httpd_suppress_version_string)
736 visible_appname_string = (char *)appname_string;
737 else
7dbca7a4 738 visible_appname_string = (char const *)APP_FULLNAME;
d3caee79 739
270b86af 740 if (Config.Program.redirect) {
48d54e4d
AJ
741 if (Config.redirectChildren.n_max < 1) {
742 Config.redirectChildren.n_max = 0;
62e76326 743 wordlistDestroy(&Config.Program.redirect);
744 }
fea2e6e0 745 }
62e76326 746
a8a0b1c2
EC
747 if (Config.Program.store_id) {
748 if (Config.storeIdChildren.n_max < 1) {
749 Config.storeIdChildren.n_max = 0;
750 wordlistDestroy(&Config.Program.store_id);
751 }
752 }
753
f1dc9b30 754 if (Config.appendDomain)
62e76326 755 if (*Config.appendDomain != '.')
756 fatal("append_domain must begin with a '.'");
757
aee3523a 758 if (Config.errHtmlText == nullptr)
62e76326 759 Config.errHtmlText = xstrdup(null_string);
760
1b7fae06
AJ
761#if !HAVE_SETRLIMIT || !defined(RLIMIT_NOFILE)
762 if (Config.max_filedescriptors > 0) {
3a9087af 763 debugs(0, DBG_IMPORTANT, "WARNING: max_filedescriptors disabled. Operating System setrlimit(RLIMIT_NOFILE) is missing.");
1b7fae06 764 }
91a04222 765#elif USE_SELECT
1b7fae06 766 if (Config.max_filedescriptors > FD_SETSIZE) {
3a9087af 767 debugs(0, DBG_IMPORTANT, "WARNING: max_filedescriptors limited to " << FD_SETSIZE << " by select() algorithm.");
1b7fae06
AJ
768 }
769#endif
770
270b86af 771 storeConfigure();
62e76326 772
52f772de 773 snprintf(ThisCache, sizeof(ThisCache), "%s (%s)",
62e76326 774 uniqueHostname(),
d3caee79 775 visible_appname_string);
fbdba7c4 776
38a6c74e 777 /*
778 * the extra space is for loop detection in client_side.c -- we search
779 * for substrings in the Via header.
780 */
971c012b 781 snprintf(ThisCache2, sizeof(ThisCache2), " %s (%s)",
62e76326 782 uniqueHostname(),
d3caee79 783 visible_appname_string);
62e76326 784
b2b40d8c
AJ
785 /* Use visible_hostname as default surrogate_id */
786 if (!Config.Accel.surrogate_id) {
787 const char *t = getMyHostname();
788 Config.Accel.surrogate_id = xstrdup( (t?t:"unset-id") );
789 }
790
270b86af 791 if (!Config.udpMaxHitObjsz || Config.udpMaxHitObjsz > SQUID_UDP_SO_SNDBUF)
62e76326 792 Config.udpMaxHitObjsz = SQUID_UDP_SO_SNDBUF;
793
270b86af 794 if (Config.appendDomain)
62e76326 795 Config.appendDomainLen = strlen(Config.appendDomain);
270b86af 796 else
62e76326 797 Config.appendDomainLen = 0;
798
31ef19cd
AJ
799 if (Config.connect_retries > 10) {
800 debugs(0,DBG_CRITICAL, "WARNING: connect_retries cannot be larger than 10. Resetting to 10.");
801 Config.connect_retries = 10;
5210854d 802 }
62e76326 803
f0b19334 804 requirePathnameExists("MIME Config Table", Config.mimeTablePathname);
a3d0a19d 805#if USE_UNLINKD
62e76326 806
f0b19334 807 requirePathnameExists("unlinkd_program", Config.Program.unlinkd);
a3d0a19d 808#endif
8566b13e
AJ
809 bool logDaemonUsed = false;
810 for (const auto *log = Config.Log.accesslogs; !logDaemonUsed && log; log = log->next)
811 logDaemonUsed = log->usesDaemon();
812#if ICAP_CLIENT
813 for (const auto *log = Config.Log.icaplogs; !logDaemonUsed && log; log = log->next)
814 logDaemonUsed = log->usesDaemon();
815#endif
816 if (logDaemonUsed)
817 requirePathnameExists("logfile_daemon", Log::TheConfig.logfile_daemon);
818
f0b19334 819 if (Config.Program.redirect)
62e76326 820 requirePathnameExists("redirect_program", Config.Program.redirect->key);
821
a8a0b1c2
EC
822 if (Config.Program.store_id)
823 requirePathnameExists("store_id_program", Config.Program.store_id->key);
824
f0b19334 825 requirePathnameExists("Icon Directory", Config.icons.directory);
62e76326 826
26ac0430 827 if (Config.errorDirectory)
43000484 828 requirePathnameExists("Error Directory", Config.errorDirectory);
62e76326 829
626096be 830#if USE_HTTP_VIOLATIONS
62e76326 831
9f60cfdf 832 {
8d9a8184 833 const RefreshPattern *R;
62e76326 834
26ac0430 835 for (R = Config.Refresh; R; R = R->next) {
62e76326 836 if (!R->flags.override_expire)
837 continue;
838
e0236918 839 debugs(22, DBG_IMPORTANT, "WARNING: use of 'override-expire' in 'refresh_pattern' violates HTTP");
62e76326 840
841 break;
842 }
843
26ac0430 844 for (R = Config.Refresh; R; R = R->next) {
62e76326 845 if (!R->flags.override_lastmod)
846 continue;
847
e0236918 848 debugs(22, DBG_IMPORTANT, "WARNING: use of 'override-lastmod' in 'refresh_pattern' violates HTTP");
62e76326 849
850 break;
851 }
852
26ac0430 853 for (R = Config.Refresh; R; R = R->next) {
38f9c547 854 if (!R->flags.reload_into_ims)
855 continue;
856
e0236918 857 debugs(22, DBG_IMPORTANT, "WARNING: use of 'reload-into-ims' in 'refresh_pattern' violates HTTP");
38f9c547 858
859 break;
860 }
861
26ac0430 862 for (R = Config.Refresh; R; R = R->next) {
38f9c547 863 if (!R->flags.ignore_reload)
864 continue;
865
e0236918 866 debugs(22, DBG_IMPORTANT, "WARNING: use of 'ignore-reload' in 'refresh_pattern' violates HTTP");
38f9c547 867
868 break;
869 }
870
26ac0430 871 for (R = Config.Refresh; R; R = R->next) {
38f9c547 872 if (!R->flags.ignore_no_store)
873 continue;
874
e0236918 875 debugs(22, DBG_IMPORTANT, "WARNING: use of 'ignore-no-store' in 'refresh_pattern' violates HTTP");
38f9c547 876
877 break;
878 }
879
26ac0430 880 for (R = Config.Refresh; R; R = R->next) {
38f9c547 881 if (!R->flags.ignore_private)
882 continue;
883
e0236918 884 debugs(22, DBG_IMPORTANT, "WARNING: use of 'ignore-private' in 'refresh_pattern' violates HTTP");
38f9c547 885
886 break;
887 }
9f60cfdf 888 }
c2f5c744 889#endif
626096be 890#if !USE_HTTP_VIOLATIONS
c2f5c744 891 Config.onoff.via = 1;
892#else
62e76326 893
c2f5c744 894 if (!Config.onoff.via)
e0236918 895 debugs(22, DBG_IMPORTANT, "WARNING: HTTP requires the use of Via");
62e76326 896
9f60cfdf 897#endif
62e76326 898
5491c11e
AR
899 // we enable runtime PURGE checks if there is at least one PURGE method ACL
900 // TODO: replace with a dedicated "purge" ACL option?
3d9019c7 901 Config2.onoff.enable_purge = (ACLMethodData::ThePurgeCount > 0);
62e76326 902
8749fa47 903 if (geteuid() == 0) {
aee3523a 904 if (nullptr != Config.effectiveUser) {
62e76326 905
906 struct passwd *pwd = getpwnam(Config.effectiveUser);
907
aee3523a 908 if (nullptr == pwd) {
62e76326 909 /*
910 * Andres Kroonmaa <andre@online.ee>:
911 * Some getpwnam() implementations (Solaris?) require
912 * an available FD < 256 for opening a FILE* to the
913 * passwd file.
914 * DW:
915 * This should be safe at startup, but might still fail
916 * during reconfigure.
917 */
918 fatalf("getpwnam failed to find userid for effective user '%s'",
919 Config.effectiveUser);
5ad8d199 920 return;
921 }
62e76326 922
923 Config2.effectiveUserID = pwd->pw_uid;
924
925 Config2.effectiveGroupID = pwd->pw_gid;
08ac5ea7 926
cff61cb8 927#if HAVE_PUTENV
cff61cb8 928 if (pwd->pw_dir && *pwd->pw_dir) {
96b9d962
AR
929 // putenv() leaks by design; avoid leaks when nothing changes
930 static SBuf lastDir;
b1b27939 931 if (lastDir.isEmpty() || lastDir.cmp(pwd->pw_dir) != 0) {
96b9d962
AR
932 lastDir = pwd->pw_dir;
933 int len = strlen(pwd->pw_dir) + 6;
934 char *env_str = (char *)xcalloc(len, 1);
935 snprintf(env_str, len, "HOME=%s", pwd->pw_dir);
936 putenv(env_str);
937 }
cff61cb8 938 }
cff61cb8 939#endif
62e76326 940 }
8749fa47 941 } else {
62e76326 942 Config2.effectiveUserID = geteuid();
943 Config2.effectiveGroupID = getegid();
d20b1cd0 944 }
62e76326 945
aee3523a 946 if (nullptr != Config.effectiveGroup) {
62e76326 947
948 struct group *grp = getgrnam(Config.effectiveGroup);
949
aee3523a 950 if (nullptr == grp) {
62e76326 951 fatalf("getgrnam failed to find groupid for effective group '%s'",
952 Config.effectiveGroup);
5ad8d199 953 return;
954 }
62e76326 955
956 Config2.effectiveGroupID = grp->gr_gid;
d20b1cd0 957 }
62e76326 958
866be11c
CT
959#if USE_OPENSSL
960 if (Config.ssl_client.foreignIntermediateCertsPath)
961 Ssl::loadSquidUntrusted(Config.ssl_client.foreignIntermediateCertsPath);
962#endif
963
a465e144 964 if (Security::ProxyOutgoingConfig.encryptTransport) {
9259c796 965 debugs(3, 2, "initializing https:// proxy context");
a465e144 966 Config.ssl_client.sslContext = Security::ProxyOutgoingConfig.createClientContext(false);
91667ff4 967 if (!Config.ssl_client.sslContext) {
79ff8023
AJ
968#if USE_OPENSSL
969 fatal("ERROR: Could not initialize https:// proxy context");
970#else
971 debugs(3, DBG_IMPORTANT, "ERROR: proxying https:// currently still requires --with-openssl");
972#endif
91667ff4 973 }
866be11c 974#if USE_OPENSSL
900daee3 975 Ssl::useSquidUntrusted(Config.ssl_client.sslContext.get());
866be11c 976#endif
a465e144 977 }
f9ad0106 978
2e24d0bf 979 for (const auto &p: CurrentCachePeers()) {
1f1f29e8
AJ
980
981 // default value for ssldomain= is the peer host/IP
982 if (p->secure.sslDomain.isEmpty())
983 p->secure.sslDomain = p->host;
984
985 if (p->secure.encryptTransport) {
9259c796 986 debugs(3, 2, "initializing TLS context for cache_peer " << *p);
a465e144 987 p->sslContext = p->secure.createClientContext(true);
91667ff4 988 if (!p->sslContext) {
a555a85b 989 debugs(3, DBG_CRITICAL, "ERROR: Could not initialize TLS context for cache_peer " << *p);
91667ff4 990 self_destruct();
ba724607 991 return;
91667ff4 992 }
f9ad0106 993 }
04401ab0 994 }
f9ad0106 995
aee3523a 996 for (AnyP::PortCfgPointer s = HttpPortList; s != nullptr; s = s->next) {
339e4d7a 997 if (!s->secure.encryptTransport)
04401ab0 998 continue;
9259c796 999 debugs(3, 2, "initializing " << AnyP::UriScheme(s->transport.protocol) << "_port " << s->s << " TLS contexts");
51e09c08 1000 s->secure.initServerContexts(*s);
04401ab0 1001 }
1f771fed
AJ
1002
1003 // prevent infinite fetch loops in the request parser
2f8abb64 1004 // due to buffer full but not enough data received to finish parse
1f771fed 1005 if (Config.maxRequestBufferSize <= Config.maxRequestHeaderSize) {
d0bbf50e 1006 fatalf("Client request buffer of %u bytes cannot hold a request with %u bytes of headers." \
1f771fed 1007 " Change client_request_buffer_max or request_header_max_size limits.",
d0bbf50e 1008 (uint32_t)Config.maxRequestBufferSize, (uint32_t)Config.maxRequestHeaderSize);
1f771fed 1009 }
a0e23afd 1010
801593a9
AR
1011 // Warn about the dangers of exceeding String limits when manipulating HTTP
1012 // headers. Technically, we do not concatenate _requests_, so we could relax
1013 // their check, but we keep the two checks the same for simplicity sake.
1014 const auto safeRawHeaderValueSizeMax = (String::SizeMaxXXX()+1)/3;
1015 // TODO: static_assert(safeRawHeaderValueSizeMax >= 64*1024); // no WARNINGs for default settings
1016 if (Config.maxRequestHeaderSize > safeRawHeaderValueSizeMax)
1017 debugs(3, DBG_CRITICAL, "WARNING: Increasing request_header_max_size beyond " << safeRawHeaderValueSizeMax <<
1018 " bytes makes Squid more vulnerable to denial-of-service attacks; configured value: " << Config.maxRequestHeaderSize << " bytes");
1019 if (Config.maxReplyHeaderSize > safeRawHeaderValueSizeMax)
1020 debugs(3, DBG_CRITICAL, "WARNING: Increasing reply_header_max_size beyond " << safeRawHeaderValueSizeMax <<
1021 " bytes makes Squid more vulnerable to denial-of-service attacks; configured value: " << Config.maxReplyHeaderSize << " bytes");
1022
079a8480
AJ
1023 /*
1024 * Disable client side request pipelining if client_persistent_connections OFF.
1025 * Waste of resources queueing any pipelined requests when the first will close the connection.
1026 */
1027 if (Config.pipeline_max_prefetch > 0 && !Config.onoff.client_pconns) {
1028 debugs(3, DBG_PARSE_NOTE(DBG_IMPORTANT), "WARNING: pipeline_prefetch " << Config.pipeline_max_prefetch <<
cba78478 1029 " requires client_persistent_connections ON. Forced pipeline_prefetch 0.");
079a8480
AJ
1030 Config.pipeline_max_prefetch = 0;
1031 }
1032
2f1431ea 1033#if USE_AUTH
a0e23afd
AJ
1034 /*
1035 * disable client side request pipelining. There is a race with
1036 * Negotiate and NTLM when the client sends a second request on an
1037 * connection before the authenticate challenge is sent. With
1038 * pipelining OFF, the client may fail to authenticate, but squid's
1039 * state will be preserved.
1040 */
079a8480 1041 if (Config.pipeline_max_prefetch > 0) {
dc79fed8
AJ
1042 Auth::SchemeConfig *nego = Auth::SchemeConfig::Find("Negotiate");
1043 Auth::SchemeConfig *ntlm = Auth::SchemeConfig::Find("NTLM");
a0e23afd 1044 if ((nego && nego->active()) || (ntlm && ntlm->active())) {
079a8480
AJ
1045 debugs(3, DBG_PARSE_NOTE(DBG_IMPORTANT), "WARNING: pipeline_prefetch breaks NTLM and Negotiate authentication. Forced pipeline_prefetch 0.");
1046 Config.pipeline_max_prefetch = 0;
a0e23afd
AJ
1047 }
1048 }
5bfc3dbd 1049
d6e94bda 1050 for (auto &authSchemes : Auth::TheConfig.schemeLists) {
5bfc3dbd
EB
1051 authSchemes.expand();
1052 if (authSchemes.authConfigs.empty()) {
1053 debugs(3, DBG_CRITICAL, "auth_schemes: at least one scheme name is required; got: " << authSchemes.rawSchemes);
1054 self_destruct();
1055 }
1056 }
2f1431ea 1057#endif
090089c4 1058}
1059
76f44481
AJ
1060/** Parse a line containing an obsolete directive.
1061 * To upgrade it where possible instead of just "Bungled config" for
1062 * directives which cannot be marked as simply aliases of the some name.
1063 * For example if the parameter order and content has changed.
1064 * Or if the directive has been completely removed.
1065 */
1066void
1067parse_obsolete(const char *name)
1068{
1069 // Directives which have been radically changed rather than removed
1070 if (!strcmp(name, "url_rewrite_concurrency")) {
254d8ef1
AJ
1071 int cval;
1072 parse_int(&cval);
76f44481 1073 debugs(3, DBG_CRITICAL, "WARNING: url_rewrite_concurrency upgrade overriding url_rewrite_children settings.");
254d8ef1 1074 Config.redirectChildren.concurrency = cval;
76f44481 1075 }
8ebad780 1076
ba724607 1077 if (!strcmp(name, "log_access")) {
8ebad780 1078 self_destruct();
ba724607
AJ
1079 return;
1080 }
8ebad780 1081
ba724607 1082 if (!strcmp(name, "log_icap")) {
8ebad780 1083 self_destruct();
ba724607
AJ
1084 return;
1085 }
96598f93
AJ
1086
1087 if (!strcmp(name, "ignore_ims_on_miss")) {
1088 // the replacement directive cache_revalidate_on_miss has opposite meanings for ON/OFF value
1089 // than the 2.7 directive. We need to parse and invert the configured value.
1090 int temp = 0;
1091 parse_onoff(&temp);
1092 Config.onoff.cache_miss_revalidate = !temp;
1093 }
7e62a74f
AJ
1094
1095 if (!strncmp(name, "sslproxy_", 9)) {
1096 // the replacement directive tls_outgoing_options uses options instead of whole-line input
1097 SBuf tmp;
1098 if (!strcmp(name, "sslproxy_cafile"))
1099 tmp.append("cafile=");
1100 else if (!strcmp(name, "sslproxy_capath"))
1101 tmp.append("capath=");
1102 else if (!strcmp(name, "sslproxy_cipher"))
1103 tmp.append("cipher=");
1104 else if (!strcmp(name, "sslproxy_client_certificate"))
1105 tmp.append("cert=");
1106 else if (!strcmp(name, "sslproxy_client_key"))
1107 tmp.append("key=");
1108 else if (!strcmp(name, "sslproxy_flags"))
1109 tmp.append("flags=");
1110 else if (!strcmp(name, "sslproxy_options"))
1111 tmp.append("options=");
1112 else if (!strcmp(name, "sslproxy_version"))
1113 tmp.append("version=");
1114 else {
1115 debugs(3, DBG_CRITICAL, "ERROR: unknown directive: " << name);
1116 self_destruct();
ba724607 1117 return;
7e62a74f
AJ
1118 }
1119
1120 // add the value as unquoted-string because the old values did not support whitespace
1121 const char *token = ConfigParser::NextQuotedOrToEol();
1122 tmp.append(token, strlen(token));
1123 Security::ProxyOutgoingConfig.parse(tmp.c_str());
1124 }
76f44481
AJ
1125}
1126
8da861a5
EB
1127template <class MinimalUnit>
1128static const char *
1129TimeUnitToString()
1130{
1131 const auto minUnit = MinimalUnit(1);
1132 if(minUnit == std::chrono::nanoseconds(1))
1133 return T_NANOSECOND_STR;
1134 else if (minUnit == std::chrono::microseconds(1))
1135 return T_MICROSECOND_STR;
1136 else if (minUnit == std::chrono::milliseconds(1))
1137 return T_MILLISECOND_STR;
1138 else {
1139 assert(minUnit >= std::chrono::seconds(1));
1140 return T_SECOND_STR;
ba724607 1141 }
8da861a5 1142}
62e76326 1143
8da861a5
EB
1144/// Assigns 'ns' the number of nanoseconds corresponding to 'unitName'.
1145/// \param MinimalUnit is a chrono duration type specifying the minimal
1146/// allowed time unit.
1147/// \returns true if unitName is correct and its time unit is not less
1148/// than MinimalUnit.
1149template <class MinimalUnit>
1150static bool
1151parseTimeUnit(const char *unitName, std::chrono::nanoseconds &ns)
1152{
1153 if (!unitName)
1154 throw TexcHere("missing time unit");
1155
1156 if (!strncasecmp(unitName, T_NANOSECOND_STR, strlen(T_NANOSECOND_STR)))
1157 ns = std::chrono::nanoseconds(1);
1158 else if (!strncasecmp(unitName, T_MICROSECOND_STR, strlen(T_MICROSECOND_STR)))
1159 ns = std::chrono::microseconds(1);
1160 else if (!strncasecmp(unitName, T_MILLISECOND_STR, strlen(T_MILLISECOND_STR)))
1161 ns = std::chrono::milliseconds(1);
1162 else if (!strncasecmp(unitName, T_SECOND_STR, strlen(T_SECOND_STR)))
1163 ns = std::chrono::seconds(1);
1164 else if (!strncasecmp(unitName, T_MINUTE_STR, strlen(T_MINUTE_STR)))
1165 ns = std::chrono::minutes(1);
1166 else if (!strncasecmp(unitName, T_HOUR_STR, strlen(T_HOUR_STR)))
1167 ns = std::chrono::hours(1);
1168 else if (!strncasecmp(unitName, T_DAY_STR, strlen(T_DAY_STR)))
1169 ns = std::chrono::hours(24);
1170 else if (!strncasecmp(unitName, T_WEEK_STR, strlen(T_WEEK_STR)))
1171 ns = std::chrono::hours(24 * 7);
1172 else if (!strncasecmp(unitName, T_FORTNIGHT_STR, strlen(T_FORTNIGHT_STR)))
1173 ns = std::chrono::hours(24 * 14);
1174 else if (!strncasecmp(unitName, T_MONTH_STR, strlen(T_MONTH_STR)))
1175 ns = std::chrono::hours(24 * 30);
1176 else if (!strncasecmp(unitName, T_YEAR_STR, strlen(T_YEAR_STR)))
1177 ns = std::chrono::hours(static_cast<std::chrono::hours::rep>(HoursPerYear));
1178 else if (!strncasecmp(unitName, T_DECADE_STR, strlen(T_DECADE_STR)))
1179 ns = std::chrono::hours(static_cast<std::chrono::hours::rep>(HoursPerYear * 10));
1180 else
1181 return false;
62e76326 1182
8da861a5
EB
1183 if (ns < MinimalUnit(1)) {
1184 throw TexcHere(ToSBuf("time unit '", unitName, "' is too small to be used in this context, the minimal unit is ",
1185 TimeUnitToString<MinimalUnit>()));
1186 }
62e76326 1187
8da861a5
EB
1188 return true;
1189}
62e76326 1190
8da861a5 1191static std::chrono::nanoseconds
f1e130c5 1192ToNanoSeconds(const double value, const std::chrono::nanoseconds &unit)
8da861a5 1193{
f1e130c5 1194 if (value < 0.0)
8da861a5 1195 throw TexcHere("time must have a positive value");
65ae31c6 1196
f1e130c5
FC
1197 if (value > (static_cast<double>(std::chrono::nanoseconds::max().count()) / unit.count())) {
1198 const auto maxYears = std::chrono::duration_cast<std::chrono::hours>(std::chrono::nanoseconds::max()).count()/HoursPerYear;
8da861a5
EB
1199 throw TexcHere(ToSBuf("time values cannot exceed ", maxYears, " years"));
1200 }
62e76326 1201
f1e130c5 1202 return std::chrono::duration_cast<std::chrono::nanoseconds>(unit * value);
8da861a5 1203}
54a063a2 1204
8da861a5
EB
1205template <class TimeUnit>
1206static TimeUnit
1207FromNanoseconds(const std::chrono::nanoseconds &ns, const double parsedValue)
1208{
1209 const auto result = std::chrono::duration_cast<TimeUnit>(ns);
1210 if (!result.count()) {
1211 throw TexcHere(ToSBuf("time value '", parsedValue,
1212 "' is too small to be used in this context, the minimal value is 1 ",
1213 TimeUnitToString<TimeUnit>()));
54a063a2 1214 }
d1838de9 1215 return result;
090089c4 1216}
1217
8da861a5
EB
1218/// Parses a time specification from the config file and
1219/// returns the time as a chrono duration object of 'TimeUnit' type.
1220template <class TimeUnit>
1221static TimeUnit
1222parseTimeLine()
270b86af 1223{
8da861a5
EB
1224 const auto valueToken = ConfigParser::NextToken();
1225 if (!valueToken)
1226 throw TexcHere("cannot read a time value");
62e76326 1227
8da861a5 1228 const auto parsedValue = xatof(valueToken);
62e76326 1229
8da861a5
EB
1230 if (parsedValue == 0)
1231 return TimeUnit::zero();
62e76326 1232
8da861a5 1233 std::chrono::nanoseconds parsedUnitDuration;
62e76326 1234
8da861a5 1235 const auto token = ConfigParser::PeekAtToken();
62e76326 1236
8da861a5
EB
1237 if (!parseTimeUnit<TimeUnit>(token, parsedUnitDuration))
1238 throw TexcHere(ToSBuf("unknown time unit '", token, "'"));
62e76326 1239
8da861a5 1240 (void)ConfigParser::NextToken();
62e76326 1241
f1e130c5 1242 const auto nanoseconds = ToNanoSeconds(parsedValue, parsedUnitDuration);
62e76326 1243
8da861a5
EB
1244 // validate precisions (time-units-small only)
1245 if (TimeUnit(1) <= std::chrono::microseconds(1)) {
1246 if (0 < nanoseconds.count() && nanoseconds.count() < 3) {
1247 debugs(3, DBG_PARSE_NOTE(DBG_IMPORTANT), ConfigParser::CurrentLocation() << ": WARNING: " <<
1248 "Squid time measurement precision is likely to be far worse than " <<
1249 "the nanosecond-level precision implied by the configured value: " << parsedValue << ' ' << token);
1250 }
1251 }
62e76326 1252
8da861a5 1253 return FromNanoseconds<TimeUnit>(nanoseconds, parsedValue);
270b86af 1254}
1255
47f6e231 1256static void
1257parseBytesLine64(int64_t * bptr, const char *units)
1258{
1259 char *token;
1260 double d;
1261 int64_t m;
1262 int64_t u;
1263
9eeb8e4b 1264 if ((u = parseBytesUnits(units)) == 0) {
47f6e231 1265 self_destruct();
9eeb8e4b 1266 return;
1267 }
47f6e231 1268
aee3523a 1269 if ((token = ConfigParser::NextToken()) == nullptr) {
47f6e231 1270 self_destruct();
9eeb8e4b 1271 return;
1272 }
47f6e231 1273
1274 if (strcmp(token, "none") == 0 || strcmp(token, "-1") == 0) {
bc41416d 1275 *bptr = -1;
47f6e231 1276 return;
1277 }
1278
1279 d = xatof(token);
1280
f53969cc 1281 m = u; /* default to 'units' if none specified */
47f6e231 1282
1283 if (0.0 == d)
1284 (void) 0;
aee3523a 1285 else if ((token = ConfigParser::NextToken()) == nullptr)
fa84c01d 1286 debugs(3, DBG_CRITICAL, "WARNING: No units on '" <<
26ac0430
AJ
1287 config_input_line << "', assuming " <<
1288 d << " " << units );
9eeb8e4b 1289 else if ((m = parseBytesUnits(token)) == 0) {
47f6e231 1290 self_destruct();
9eeb8e4b 1291 return;
1292 }
47f6e231 1293
1294 *bptr = static_cast<int64_t>(m * d / u);
1295
54a063a2
TX
1296 if (static_cast<double>(*bptr) * 2 != (m * d / u) * 2) {
1297 debugs(3, DBG_CRITICAL, "ERROR: Invalid value '" <<
1298 d << " " << token << ": integer overflow (int64_t).");
47f6e231 1299 self_destruct();
54a063a2 1300 }
47f6e231 1301}
1302
9906e724 1303static void
9e975e4e 1304parseBytesLine(size_t * bptr, const char *units)
9906e724 1305{
1306 char *token;
1307 double d;
47f6e231 1308 int m;
1309 int u;
62e76326 1310
9eeb8e4b 1311 if ((u = parseBytesUnits(units)) == 0) {
62e76326 1312 self_destruct();
9eeb8e4b 1313 return;
1314 }
62e76326 1315
aee3523a 1316 if ((token = ConfigParser::NextToken()) == nullptr) {
62e76326 1317 self_destruct();
9eeb8e4b 1318 return;
1319 }
62e76326 1320
f8ecd7d7 1321 if (strcmp(token, "none") == 0 || strcmp(token, "-1") == 0) {
b2d729e6 1322 *bptr = static_cast<size_t>(-1);
ef1955a5 1323 return;
1324 }
1325
5c20d6fa 1326 d = xatof(token);
62e76326 1327
f53969cc 1328 m = u; /* default to 'units' if none specified */
62e76326 1329
343f47a3 1330 if (0.0 == d)
62e76326 1331 (void) 0;
aee3523a 1332 else if ((token = ConfigParser::NextToken()) == nullptr)
fa84c01d 1333 debugs(3, DBG_CRITICAL, "WARNING: No units on '" <<
26ac0430
AJ
1334 config_input_line << "', assuming " <<
1335 d << " " << units );
9eeb8e4b 1336 else if ((m = parseBytesUnits(token)) == 0) {
62e76326 1337 self_destruct();
9eeb8e4b 1338 return;
1339 }
62e76326 1340
e6ccf245 1341 *bptr = static_cast<size_t>(m * d / u);
347ae7c4 1342
54a063a2
TX
1343 if (static_cast<double>(*bptr) * 2 != (m * d / u) * 2) {
1344 debugs(3, DBG_CRITICAL, "ERROR: Invalid value '" <<
1345 d << " " << token << ": integer overflow (size_t).");
347ae7c4 1346 self_destruct();
54a063a2 1347 }
9906e724 1348}
1349
e210930b
AJ
1350static void
1351parseBytesLineSigned(ssize_t * bptr, const char *units)
1352{
1353 char *token;
1354 double d;
1355 int m;
1356 int u;
1357
1358 if ((u = parseBytesUnits(units)) == 0) {
1359 self_destruct();
1360 return;
1361 }
1362
aee3523a 1363 if ((token = ConfigParser::NextToken()) == nullptr) {
e210930b
AJ
1364 self_destruct();
1365 return;
1366 }
1367
1368 if (strcmp(token, "none") == 0 || token[0] == '-' /* -N */) {
1369 *bptr = -1;
1370 return;
1371 }
1372
1373 d = xatof(token);
1374
f53969cc 1375 m = u; /* default to 'units' if none specified */
e210930b
AJ
1376
1377 if (0.0 == d)
1378 (void) 0;
aee3523a 1379 else if ((token = ConfigParser::NextToken()) == nullptr)
fa84c01d 1380 debugs(3, DBG_CRITICAL, "WARNING: No units on '" <<
e210930b
AJ
1381 config_input_line << "', assuming " <<
1382 d << " " << units );
1383 else if ((m = parseBytesUnits(token)) == 0) {
1384 self_destruct();
1385 return;
1386 }
1387
54a063a2 1388 *bptr = static_cast<ssize_t>(m * d / u);
e210930b 1389
54a063a2
TX
1390 if (static_cast<double>(*bptr) * 2 != (m * d / u) * 2) {
1391 debugs(3, DBG_CRITICAL, "ERROR: Invalid value '" <<
1392 d << " " << token << ": integer overflow (ssize_t).");
e210930b 1393 self_destruct();
54a063a2 1394 }
e210930b 1395}
e210930b 1396
95d2589c
CT
1397/**
1398 * Parse bytes from a string.
1399 * Similar to the parseBytesLine function but parses the string value instead of
1400 * the current token value.
1401 */
cf487124
AJ
1402void
1403parseBytesOptionValue(size_t * bptr, const char *units, char const * value)
95d2589c
CT
1404{
1405 int u;
1406 if ((u = parseBytesUnits(units)) == 0) {
1407 self_destruct();
1408 return;
1409 }
1410
1411 // Find number from string beginning.
1412 char const * number_begin = value;
1413 char const * number_end = value;
1414
1415 while ((*number_end >= '0' && *number_end <= '9')) {
a38ec4b1 1416 ++number_end;
95d2589c
CT
1417 }
1418
1419 String number;
2fe0439c 1420 number.assign(number_begin, number_end - number_begin);
95d2589c
CT
1421
1422 int d = xatoi(number.termedBuf());
1423 int m;
1424 if ((m = parseBytesUnits(number_end)) == 0) {
1425 self_destruct();
1426 return;
1427 }
1428
1429 *bptr = static_cast<size_t>(m * d / u);
54a063a2 1430 if (static_cast<double>(*bptr) * 2 != (m * d / u) * 2)
95d2589c
CT
1431 self_destruct();
1432}
95d2589c 1433
9906e724 1434static size_t
1435parseBytesUnits(const char *unit)
1436{
1437 if (!strncasecmp(unit, B_BYTES_STR, strlen(B_BYTES_STR)))
62e76326 1438 return 1;
1439
9906e724 1440 if (!strncasecmp(unit, B_KBYTES_STR, strlen(B_KBYTES_STR)))
62e76326 1441 return 1 << 10;
1442
9906e724 1443 if (!strncasecmp(unit, B_MBYTES_STR, strlen(B_MBYTES_STR)))
62e76326 1444 return 1 << 20;
1445
9906e724 1446 if (!strncasecmp(unit, B_GBYTES_STR, strlen(B_GBYTES_STR)))
62e76326 1447 return 1 << 30;
1448
fa37412f 1449 debugs(3, DBG_CRITICAL, "WARNING: Unknown bytes unit '" << unit << "'");
62e76326 1450
9906e724 1451 return 0;
1452}
1453
5a1098fb
AJ
1454static void
1455parse_SBufList(SBufList * list)
1456{
1457 while (char *token = ConfigParser::NextQuotedToken())
1458 list->push_back(SBuf(token));
1459}
1460
1461// just dump a list, no directive name
8203a132 1462static void
dfad5100 1463dump_SBufList(StoreEntry * entry, const SBufList &words)
090089c4 1464{
5a1098fb
AJ
1465 for (const auto &i : words) {
1466 entry->append(i.rawContent(), i.length());
dfad5100
FC
1467 entry->append(" ",1);
1468 }
86f84bd1 1469 entry->append("\n",1);
6f58d7d7 1470}
62e76326 1471
5a1098fb
AJ
1472// dump a SBufList type directive with name
1473static void
1474dump_SBufList(StoreEntry * entry, const char *name, SBufList &list)
1475{
1476 if (!list.empty()) {
1477 entry->append(name, strlen(name));
1478 entry->append(" ", 1);
1479 dump_SBufList(entry, list);
1480 }
1481}
1482
1483static void
1484free_SBufList(SBufList *list)
1485{
1486 if (list)
1487 list->clear();
1488}
1489
6f58d7d7
AR
1490static void
1491dump_acl(StoreEntry * entry, const char *name, ACL * ae)
1492{
36dcfef6 1493 PackableStream os(*entry);
aee3523a 1494 while (ae != nullptr) {
bf8fe701 1495 debugs(3, 3, "dump_acl: " << name << " " << ae->name);
36dcfef6 1496 ae->dumpWhole(name, os);
62e76326 1497 ae = ae->next;
56b63fa1 1498 }
090089c4 1499}
1500
8203a132 1501static void
97427e90 1502parse_acl(ACL ** ae)
090089c4 1503{
a9f20260 1504 ACL::ParseAclLine(LegacyParser, ae);
f1dc9b30 1505}
1506
1507static void
97427e90 1508free_acl(ACL ** ae)
f1dc9b30 1509{
9ef28b60 1510 aclDestroyAcls(ae);
090089c4 1511}
1512
425de4c8 1513void
76cd39d7 1514dump_acl_list(StoreEntry * entry, ACLList * head)
30a4f2a8 1515{
e227da8d
AR
1516 // XXX: Should dump ACL names like "foo !bar" but dumps parsing context like
1517 // "(clientside_tos 0x11 line)".
dfad5100 1518 dump_SBufList(entry, head->dump());
d6827718 1519}
1520
b67e2c8c 1521void
d6827718 1522dump_acl_access(StoreEntry * entry, const char *name, acl_access * head)
1523{
dfad5100 1524 if (head)
ba6fffba 1525 dump_SBufList(entry, head->treeDump(name, &Acl::AllowOrDeny));
30a4f2a8 1526}
090089c4 1527
8203a132 1528static void
16300b58 1529parse_acl_access(acl_access ** head)
090089c4 1530{
6f58d7d7
AR
1531 aclParseAccessLine(cfg_directive, LegacyParser, head);
1532}
1533
0153d498 1534static void
16300b58 1535free_acl_access(acl_access ** head)
0153d498 1536{
a47b9029 1537 aclDestroyAccessList(head);
0153d498 1538}
1539
8203a132 1540static void
b7ac5457 1541dump_address(StoreEntry * entry, const char *name, Ip::Address &addr)
270b86af 1542{
cc192b50 1543 char buf[MAX_IPSTRLEN];
4dd643d5 1544 storeAppendPrintf(entry, "%s %s\n", name, addr.toStr(buf,MAX_IPSTRLEN) );
270b86af 1545}
1546
1547static void
b7ac5457 1548parse_address(Ip::Address *addr)
090089c4 1549{
2eceb328 1550 char *token = ConfigParser::NextToken();
270b86af 1551
9eeb8e4b 1552 if (!token) {
62e76326 1553 self_destruct();
9eeb8e4b 1554 return;
1555 }
62e76326 1556
9f2665ee 1557 if (!strcmp(token,"any_addr"))
4dd643d5 1558 addr->setAnyAddr();
9f2665ee 1559 else if ( (!strcmp(token,"no_addr")) || (!strcmp(token,"full_mask")) )
4dd643d5 1560 addr->setNoAddr();
9f2665ee 1561 else if ( (*addr = token) ) // try parse numeric/IPA
26ac0430 1562 (void) 0;
61beade2 1563 else if (addr->GetHostByName(token)) // do not use ipcache
36a83790
AJ
1564 (void) 0;
1565 else { // not an IP and not a hostname
1566 debugs(3, DBG_CRITICAL, "FATAL: invalid IP address or domain name '" << token << "'");
1567 self_destruct();
1568 }
090089c4 1569}
1570
0153d498 1571static void
b7ac5457 1572free_address(Ip::Address *addr)
0153d498 1573{
4dd643d5 1574 addr->setEmpty();
0153d498 1575}
1576
d6827718 1577static void
289848ca 1578dump_acl_address(StoreEntry * entry, const char *name, Acl::Address * head)
d6827718 1579{
cc192b50 1580 char buf[MAX_IPSTRLEN];
62e76326 1581
289848ca 1582 for (Acl::Address *l = head; l; l = l->next) {
4dd643d5
AJ
1583 if (!l->addr.isAnyAddr())
1584 storeAppendPrintf(entry, "%s %s", name, l->addr.toStr(buf,MAX_IPSTRLEN));
62e76326 1585 else
1586 storeAppendPrintf(entry, "%s autoselect", name);
1587
1588 dump_acl_list(entry, l->aclList);
1589
1590 storeAppendPrintf(entry, "\n");
d6827718 1591 }
1592}
1593
1594static void
289848ca 1595parse_acl_address(Acl::Address ** head)
d6827718 1596{
289848ca 1597 Acl::Address *l = new Acl::Address;
d6827718 1598 parse_address(&l->addr);
6f58d7d7 1599 aclParseAclList(LegacyParser, &l->aclList, l->addr);
62e76326 1600
289848ca 1601 Acl::Address **tail = head;
d6827718 1602 while (*tail)
62e76326 1603 tail = &(*tail)->next;
1604
d6827718 1605 *tail = l;
1606}
1607
1608static void
289848ca 1609free_acl_address(Acl::Address ** head)
d6827718 1610{
289848ca 1611 delete *head;
aee3523a 1612 *head = nullptr;
d6827718 1613}
1614
d6827718 1615static void
1616dump_acl_tos(StoreEntry * entry, const char *name, acl_tos * head)
1617{
1618 acl_tos *l;
62e76326 1619
d6827718 1620 for (l = head; l; l = l->next) {
62e76326 1621 if (l->tos > 0)
1622 storeAppendPrintf(entry, "%s 0x%02X", name, l->tos);
1623 else
1624 storeAppendPrintf(entry, "%s none", name);
1625
1626 dump_acl_list(entry, l->aclList);
1627
1628 storeAppendPrintf(entry, "\n");
d6827718 1629 }
1630}
1631
d6827718 1632static void
1633parse_acl_tos(acl_tos ** head)
1634{
425de4c8 1635 unsigned int tos; /* Initially uint for strtoui. Casted to tos_t before return */
2eceb328 1636 char *token = ConfigParser::NextToken();
62e76326 1637
9eeb8e4b 1638 if (!token) {
62e76326 1639 self_destruct();
9eeb8e4b 1640 return;
1641 }
62e76326 1642
aee3523a 1643 if (!xstrtoui(token, nullptr, &tos, 0, std::numeric_limits<tos_t>::max())) {
62e76326 1644 self_destruct();
9eeb8e4b 1645 return;
1646 }
62e76326 1647
c6f168c1
CT
1648 const unsigned int chTos = tos & 0xFC;
1649 if (chTos != tos) {
1650 debugs(3, DBG_PARSE_NOTE(DBG_IMPORTANT), "WARNING: Tos value '" << tos << "' adjusted to '" << chTos << "'");
1651 tos = chTos;
1652 }
1653
60019fea 1654 acl_tos *l = new acl_tos;
62e76326 1655
425de4c8 1656 l->tos = (tos_t)tos;
62e76326 1657
6f58d7d7 1658 aclParseAclList(LegacyParser, &l->aclList, token);
62e76326 1659
60019fea 1660 acl_tos **tail = head; /* sane name below */
d6827718 1661 while (*tail)
62e76326 1662 tail = &(*tail)->next;
1663
d6827718 1664 *tail = l;
1665}
1666
1667static void
1668free_acl_tos(acl_tos ** head)
1669{
60019fea 1670 delete *head;
aee3523a 1671 *head = nullptr;
d6827718 1672}
1673
11e8cfe3 1674#if SO_MARK && USE_LIBCAP
f4f6c2e0 1675
425de4c8
AJ
1676static void
1677dump_acl_nfmark(StoreEntry * entry, const char *name, acl_nfmark * head)
1678{
60019fea 1679 for (acl_nfmark *l = head; l; l = l->next) {
244da4ad 1680 storeAppendPrintf(entry, "%s %s", name, ToSBuf(l->markConfig).c_str());
425de4c8
AJ
1681
1682 dump_acl_list(entry, l->aclList);
1683
1684 storeAppendPrintf(entry, "\n");
1685 }
1686}
1687
425de4c8
AJ
1688static void
1689parse_acl_nfmark(acl_nfmark ** head)
1690{
244da4ad
AG
1691 SBuf token(ConfigParser::NextToken());
1692 const auto mc = Ip::NfMarkConfig::Parse(token);
425de4c8 1693
244da4ad
AG
1694 // Packet marking directives should not allow to use masks.
1695 const auto pkt_dirs = {"mark_client_packet", "clientside_mark", "tcp_outgoing_mark"};
1696 if (mc.hasMask() && std::find(pkt_dirs.begin(), pkt_dirs.end(), cfg_directive) != pkt_dirs.end())
1697 throw TexcHere(ToSBuf("'", cfg_directive, "' does not support masked marks"));
425de4c8 1698
60019fea 1699 acl_nfmark *l = new acl_nfmark;
244da4ad 1700 l->markConfig = mc;
425de4c8 1701
244da4ad 1702 aclParseAclList(LegacyParser, &l->aclList, token.c_str());
425de4c8 1703
60019fea 1704 acl_nfmark **tail = head; /* sane name below */
425de4c8
AJ
1705 while (*tail)
1706 tail = &(*tail)->next;
1707
1708 *tail = l;
1709}
1710
1711static void
1712free_acl_nfmark(acl_nfmark ** head)
1713{
60019fea 1714 delete *head;
aee3523a 1715 *head = nullptr;
425de4c8 1716}
f4f6c2e0 1717#endif /* SO_MARK */
425de4c8 1718
ef1955a5 1719static void
1328cfb7 1720dump_acl_b_size_t(StoreEntry * entry, const char *name, AclSizeLimit * head)
ef1955a5 1721{
d21b3722 1722 for (AclSizeLimit *l = head; l; l = l->next) {
ed013b6c 1723 if (l->size != -1)
ef1955a5 1724 storeAppendPrintf(entry, "%s %d %s\n", name, (int) l->size, B_BYTES_STR);
1725 else
1726 storeAppendPrintf(entry, "%s none", name);
1727
1728 dump_acl_list(entry, l->aclList);
1729
1730 storeAppendPrintf(entry, "\n");
1731 }
1732}
1733
ef1955a5 1734static void
1328cfb7 1735parse_acl_b_size_t(AclSizeLimit ** head)
ef1955a5 1736{
d21b3722 1737 AclSizeLimit *l = new AclSizeLimit;
ef1955a5 1738
47f6e231 1739 parse_b_int64_t(&l->size);
ef1955a5 1740
6f58d7d7 1741 aclParseAclList(LegacyParser, &l->aclList, l->size);
ef1955a5 1742
d21b3722 1743 AclSizeLimit **tail = head; /* sane name below */
ef1955a5 1744 while (*tail)
1745 tail = &(*tail)->next;
1746
1747 *tail = l;
1748}
1749
1750static void
1328cfb7 1751free_acl_b_size_t(AclSizeLimit ** head)
ef1955a5 1752{
d21b3722 1753 delete *head;
aee3523a 1754 *head = nullptr;
ef1955a5 1755}
1756
9a0a18de 1757#if USE_DELAY_POOLS
59715b38 1758
b67e2c8c 1759#include "DelayConfig.h"
602d9612 1760#include "DelayPools.h"
59715b38 1761/* do nothing - free_delay_pool_count is the magic free function.
ae870270 1762 * this is why delay_pool_count isn't just marked TYPE: u_short
59715b38 1763 */
1764#define free_delay_pool_class(X)
1765#define free_delay_pool_access(X)
1766#define free_delay_pool_rates(X)
1767#define dump_delay_pool_class(X, Y, Z)
1768#define dump_delay_pool_access(X, Y, Z)
1769#define dump_delay_pool_rates(X, Y, Z)
1770
1771static void
b67e2c8c 1772free_delay_pool_count(DelayConfig * cfg)
59715b38 1773{
b67e2c8c 1774 cfg->freePoolCount();
59715b38 1775}
1776
1777static void
b67e2c8c 1778dump_delay_pool_count(StoreEntry * entry, const char *name, DelayConfig &cfg)
59715b38 1779{
b67e2c8c 1780 cfg.dumpPoolCount (entry, name);
59715b38 1781}
1782
1783static void
b67e2c8c 1784parse_delay_pool_count(DelayConfig * cfg)
59715b38 1785{
b67e2c8c 1786 cfg->parsePoolCount();
59715b38 1787}
1788
1789static void
b67e2c8c 1790parse_delay_pool_class(DelayConfig * cfg)
59715b38 1791{
b67e2c8c 1792 cfg->parsePoolClass();
59715b38 1793}
1794
1795static void
b67e2c8c 1796parse_delay_pool_rates(DelayConfig * cfg)
59715b38 1797{
b67e2c8c 1798 cfg->parsePoolRates();
59715b38 1799}
1800
1801static void
b67e2c8c 1802parse_delay_pool_access(DelayConfig * cfg)
59715b38 1803{
a9f20260 1804 cfg->parsePoolAccess(LegacyParser);
59715b38 1805}
62e76326 1806
59715b38 1807#endif
1808
9a0a18de 1809#if USE_DELAY_POOLS
b4cd430a
CT
1810#include "ClientDelayConfig.h"
1811/* do nothing - free_client_delay_pool_count is the magic free function.
ae870270 1812 * this is why client_delay_pool_count isn't just marked TYPE: u_short
b4cd430a
CT
1813 */
1814
1815#define free_client_delay_pool_access(X)
1816#define free_client_delay_pool_rates(X)
1817#define dump_client_delay_pool_access(X, Y, Z)
1818#define dump_client_delay_pool_rates(X, Y, Z)
1819
1820static void
1821free_client_delay_pool_count(ClientDelayConfig * cfg)
1822{
b27668ec 1823 cfg->freePools();
b4cd430a
CT
1824}
1825
1826static void
1827dump_client_delay_pool_count(StoreEntry * entry, const char *name, ClientDelayConfig &cfg)
1828{
1829 cfg.dumpPoolCount (entry, name);
1830}
1831
1832static void
1833parse_client_delay_pool_count(ClientDelayConfig * cfg)
1834{
1835 cfg->parsePoolCount();
1836}
1837
1838static void
1839parse_client_delay_pool_rates(ClientDelayConfig * cfg)
1840{
1841 cfg->parsePoolRates();
1842}
1843
1844static void
1845parse_client_delay_pool_access(ClientDelayConfig * cfg)
1846{
1847 cfg->parsePoolAccess(LegacyParser);
1848}
1849#endif
1850
626096be 1851#if USE_HTTP_VIOLATIONS
97474590 1852static void
3b07476b 1853dump_http_header_access(StoreEntry * entry, const char *name, const HeaderManglers *manglers)
e3dd531e 1854{
3b07476b
CT
1855 if (manglers)
1856 manglers->dumpAccess(entry, name);
97474590 1857}
e3dd531e 1858
97474590 1859static void
3b07476b 1860parse_http_header_access(HeaderManglers **pm)
97474590 1861{
aee3523a 1862 char *t = nullptr;
62e76326 1863
aee3523a 1864 if ((t = ConfigParser::NextToken()) == nullptr) {
fa84c01d 1865 debugs(3, DBG_CRITICAL, "" << cfg_filename << " line " << config_lineno << ": " << config_input_line);
d816f28d 1866 debugs(3, DBG_CRITICAL, "ERROR: parse_http_header_access: missing header name.");
62e76326 1867 return;
97474590 1868 }
62e76326 1869
3b07476b
CT
1870 if (!*pm)
1871 *pm = new HeaderManglers;
1872 HeaderManglers *manglers = *pm;
001d55dc 1873 headerMangler *mangler = manglers->track(t);
3b07476b 1874 assert(mangler);
6f58d7d7
AR
1875
1876 std::string directive = "http_header_access ";
1877 directive += t;
2450353f 1878 aclParseAccessLine(directive.c_str(), LegacyParser, &mangler->access_list);
6bccf575 1879}
1880
1881static void
3b07476b 1882free_HeaderManglers(HeaderManglers **pm)
6bccf575 1883{
3b07476b
CT
1884 // we delete the entire http_header_* mangler configuration at once
1885 if (const HeaderManglers *manglers = *pm) {
1886 delete manglers;
aee3523a 1887 *pm = nullptr;
6bccf575 1888 }
1889}
1890
1891static void
3b07476b 1892dump_http_header_replace(StoreEntry * entry, const char *name, const HeaderManglers *manglers)
6bccf575 1893{
3b07476b
CT
1894 if (manglers)
1895 manglers->dumpReplacement(entry, name);
97474590 1896}
e3dd531e 1897
97474590 1898static void
3b07476b 1899parse_http_header_replace(HeaderManglers **pm)
e3dd531e 1900{
aee3523a 1901 char *t = nullptr;
62e76326 1902
aee3523a 1903 if ((t = ConfigParser::NextToken()) == nullptr) {
fa84c01d 1904 debugs(3, DBG_CRITICAL, "" << cfg_filename << " line " << config_lineno << ": " << config_input_line);
d816f28d 1905 debugs(3, DBG_CRITICAL, "ERROR: parse_http_header_replace: missing header name.");
62e76326 1906 return;
6bccf575 1907 }
62e76326 1908
bde7a8ce 1909 const char *value = ConfigParser::NextQuotedOrToEol();
62e76326 1910
3b07476b
CT
1911 if (!*pm)
1912 *pm = new HeaderManglers;
1913 HeaderManglers *manglers = *pm;
1914 manglers->setReplacement(t, value);
97474590 1915}
62e76326 1916
9e8b2f1c 1917#endif
97474590 1918
e90100aa 1919static void
2745fea5 1920dump_cachedir(StoreEntry * entry, const char *name, const Store::DiskConfig &swap)
e90100aa 1921{
5d84beb5 1922 Store::Disks::Dump(swap, *entry, name);
f53b06f9 1923}
1924
53ad48e6 1925static int
1926check_null_string(char *s)
1927{
aee3523a 1928 return s == nullptr;
53ad48e6 1929}
1930
2f1431ea 1931#if USE_AUTH
94439e4e 1932static void
9f3d2b2e 1933parse_authparam(Auth::ConfigVector * config)
94439e4e 1934{
ba724607
AJ
1935 char *type_str = ConfigParser::NextToken();
1936 if (!type_str) {
62e76326 1937 self_destruct();
ba724607
AJ
1938 return;
1939 }
94439e4e 1940
ba724607
AJ
1941 char *param_str = ConfigParser::NextToken();
1942 if (!param_str) {
62e76326 1943 self_destruct();
ba724607
AJ
1944 return;
1945 }
94439e4e 1946
5817ee13 1947 /* find a configuration for the scheme in the currently parsed configs... */
dc79fed8 1948 Auth::SchemeConfig *schemeCfg = Auth::SchemeConfig::Find(type_str);
62e76326 1949
aee3523a 1950 if (schemeCfg == nullptr) {
5817ee13 1951 /* Create a configuration based on the scheme info */
c6cf8dee 1952 Auth::Scheme::Pointer theScheme = Auth::Scheme::Find(type_str);
f5691f9c 1953
aee3523a 1954 if (theScheme == nullptr) {
d816f28d 1955 debugs(3, DBG_CRITICAL, "ERROR: Failure while parsing Config File: Unknown authentication scheme '" << type_str << "'.");
5817ee13 1956 self_destruct();
ba724607 1957 return;
62e76326 1958 }
94439e4e 1959
f5691f9c 1960 config->push_back(theScheme->createConfig());
dc79fed8 1961 schemeCfg = Auth::SchemeConfig::Find(type_str);
aee3523a 1962 if (schemeCfg == nullptr) {
5817ee13
AJ
1963 debugs(3, DBG_CRITICAL, "Parsing Config File: Corruption configuring authentication scheme '" << type_str << "'.");
1964 self_destruct();
ba724607 1965 return;
5817ee13 1966 }
94439e4e 1967 }
62e76326 1968
5817ee13 1969 schemeCfg->parse(schemeCfg, config->size(), param_str);
94439e4e 1970}
1971
1972static void
9f3d2b2e 1973free_authparam(Auth::ConfigVector * cfg)
94439e4e 1974{
5817ee13 1975 /* Wipe the Auth globals and Detach/Destruct component config + state. */
c33a88ca 1976 cfg->clear();
62e76326 1977
5817ee13 1978 /* on reconfigure initialize new auth schemes for the new config. */
ec5858ff 1979 if (reconfiguring) {
ccd8a22a 1980 Auth::Init();
94439e4e 1981 }
94439e4e 1982}
1983
1984static void
9f3d2b2e 1985dump_authparam(StoreEntry * entry, const char *name, Auth::ConfigVector cfg)
94439e4e 1986{
dc79fed8
AJ
1987 for (auto *scheme : cfg)
1988 scheme->dump(entry, name, scheme);
94439e4e 1989}
5bfc3dbd
EB
1990
1991static void
1992parse_AuthSchemes(acl_access **authSchemes)
1993{
1994 const char *tok = ConfigParser::NextQuotedToken();
1995 if (!tok) {
1996 debugs(29, DBG_CRITICAL, "FATAL: auth_schemes missing the parameter");
1997 self_destruct();
1998 return;
1999 }
d6e94bda 2000 Auth::TheConfig.schemeLists.emplace_back(tok, ConfigParser::LastTokenWasQuoted());
329c128c 2001 const auto action = Acl::Answer(ACCESS_ALLOWED, Auth::TheConfig.schemeLists.size() - 1);
5bfc3dbd
EB
2002 ParseAclWithAction(authSchemes, action, "auth_schemes");
2003}
2004
2005static void
2006free_AuthSchemes(acl_access **authSchemes)
2007{
d6e94bda 2008 Auth::TheConfig.schemeLists.clear();
5bfc3dbd
EB
2009 free_acl_access(authSchemes);
2010}
2011
2012static void
2013dump_AuthSchemes(StoreEntry *entry, const char *name, acl_access *authSchemes)
2014{
2015 if (authSchemes)
329c128c 2016 dump_SBufList(entry, authSchemes->treeDump(name, [](const Acl::Answer &action) {
d6e94bda 2017 return Auth::TheConfig.schemeLists.at(action.kind).rawSchemes;
74ac279f 2018 }));
5bfc3dbd
EB
2019}
2020
2f1431ea 2021#endif /* USE_AUTH */
94439e4e 2022
ba6fffba 2023static void
329c128c 2024ParseAclWithAction(acl_access **access, const Acl::Answer &action, const char *desc, ACL *acl)
ba6fffba
EB
2025{
2026 assert(access);
2027 SBuf name;
2028 if (!*access) {
2029 *access = new Acl::Tree;
2030 name.Printf("(%s rules)", desc);
2031 (*access)->context(name.c_str(), config_input_line);
2032 }
2033 Acl::AndNode *rule = new Acl::AndNode;
2034 name.Printf("(%s rule)", desc);
2035 rule->context(name.c_str(), config_input_line);
e227da8d
AR
2036 if (acl)
2037 rule->add(acl);
2038 else
2039 rule->lineParse();
ba6fffba
EB
2040 (*access)->add(rule, action);
2041}
2042
0e4e0e7d 2043static void
2745fea5 2044parse_cachedir(Store::DiskConfig *swap)
0e4e0e7d 2045{
5d84beb5
EB
2046 assert(swap);
2047 Store::Disks::Parse(*swap);
752c3b27 2048}
2049
2d72d4fd 2050static const char *
505e35db 2051peer_type_str(const peer_t type)
2052{
1f140227 2053 const char * result;
2054
0cdcddb9 2055 switch (type) {
62e76326 2056
505e35db 2057 case PEER_PARENT:
1f140227 2058 result = "parent";
62e76326 2059 break;
2060
505e35db 2061 case PEER_SIBLING:
1f140227 2062 result = "sibling";
62e76326 2063 break;
2064
505e35db 2065 case PEER_MULTICAST:
1f140227 2066 result = "multicast";
62e76326 2067 break;
2068
505e35db 2069 default:
1f140227 2070 result = "unknown";
62e76326 2071 break;
505e35db 2072 }
1f140227 2073
2074 return result;
505e35db 2075}
2076
f1dc9b30 2077static void
2e24d0bf 2078dump_peer(StoreEntry * entry, const char *name, const CachePeers *peers)
98ffb7e4 2079{
2e24d0bf
EB
2080 if (!peers)
2081 return;
2082
5844d003 2083 NeighborTypeDomainList *t;
505e35db 2084 LOCAL_ARRAY(char, xname, 128);
62e76326 2085
2e24d0bf
EB
2086 for (const auto &peer: *peers) {
2087 const auto p = peer.get();
4bc48d15 2088 storeAppendPrintf(entry, "%s %s %s %d %d name=%s",
62e76326 2089 name,
2090 p->host,
2091 neighborTypeStr(p),
2092 p->http_port,
4bc48d15
AJ
2093 p->icp.port,
2094 p->name);
62e76326 2095 dump_peer_options(entry, p);
2096
62e76326 2097 if (p->access) {
2098 snprintf(xname, 128, "cache_peer_access %s", p->name);
2099 dump_acl_access(entry, xname, p->access);
2100 }
2101
2102 for (t = p->typelist; t; t = t->next) {
2103 storeAppendPrintf(entry, "neighbor_type_domain %s %s %s\n",
2104 p->host,
2105 peer_type_str(t->type),
2106 t->domain);
2107 }
d41de3c1 2108 }
98ffb7e4 2109}
2110
86ae97bb
AJ
2111/**
2112 * utility function to prevent getservbyname() being called with a numeric value
2113 * on Windows at least it returns garage results.
2114 */
2115static bool
2116isUnsignedNumeric(const char *str, size_t len)
2117{
2118 if (len < 1) return false;
2119
5e263176 2120 for (; len >0 && *str; ++str, --len) {
86ae97bb
AJ
2121 if (! isdigit(*str))
2122 return false;
2123 }
2124 return true;
2125}
2126
609fac72 2127/**
f53969cc 2128 \param proto 'tcp' or 'udp' for protocol
609fac72 2129 \returns Port the named service is supposed to be listening on.
2130 */
f45dd259 2131static unsigned short
609fac72 2132GetService(const char *proto)
2133{
aee3523a 2134 struct servent *port = nullptr;
609fac72 2135 /** Parses a port number or service name from the squid.conf */
2eceb328 2136 char *token = ConfigParser::NextToken();
aee3523a 2137 if (token == nullptr) {
26ac0430
AJ
2138 self_destruct();
2139 return 0; /* NEVER REACHED */
609fac72 2140 }
2141 /** Returns either the service port number from /etc/services */
e1381638 2142 if ( !isUnsignedNumeric(token, strlen(token)) )
86ae97bb 2143 port = getservbyname(token, proto);
aee3523a 2144 if (port != nullptr) {
f45dd259 2145 return ntohs((unsigned short)port->s_port);
609fac72 2146 }
2147 /** Or a numeric translation of the config text. */
2148 return xatos(token);
2149}
2150
2151/**
2152 \returns Port the named TCP service is supposed to be listening on.
2153 \copydoc GetService(const char *proto)
2154 */
f45dd259 2155inline unsigned short
609fac72 2156GetTcpService(void)
2157{
2158 return GetService("tcp");
2159}
2160
2161/**
2162 \returns Port the named UDP service is supposed to be listening on.
2163 \copydoc GetService(const char *proto)
2164 */
f45dd259 2165inline unsigned short
609fac72 2166GetUdpService(void)
2167{
2168 return GetService("udp");
2169}
2170
8203a132 2171static void
2e24d0bf 2172parse_peer(CachePeers **peers)
7813c6d5 2173{
ba724607
AJ
2174 char *host_str = ConfigParser::NextToken();
2175 if (!host_str) {
62e76326 2176 self_destruct();
ba724607
AJ
2177 return;
2178 }
62e76326 2179
ba724607
AJ
2180 char *token = ConfigParser::NextToken();
2181 if (!token) {
62e76326 2182 self_destruct();
ba724607
AJ
2183 return;
2184 }
62e76326 2185
a555a85b
AR
2186 const auto p = new CachePeer(host_str);
2187
40a1495e 2188 p->type = parseNeighborType(token);
62e76326 2189
0d5a2006 2190 if (p->type == PEER_MULTICAST) {
ccdf4138
FC
2191 p->options.no_digest = true;
2192 p->options.no_netdb_exchange = true;
0d5a2006 2193 }
2194
609fac72 2195 p->http_port = GetTcpService();
62e76326 2196
ba724607
AJ
2197 if (!p->http_port) {
2198 delete p;
0e656b69 2199 self_destruct();
ba724607
AJ
2200 return;
2201 }
62e76326 2202
609fac72 2203 p->icp.port = GetUdpService();
62e76326 2204
2eceb328 2205 while ((token = ConfigParser::NextToken())) {
a37d6070 2206 if (!strcmp(token, "proxy-only")) {
ccdf4138 2207 p->options.proxy_only = true;
a37d6070 2208 } else if (!strcmp(token, "no-query")) {
ccdf4138 2209 p->options.no_query = true;
a37d6070 2210 } else if (!strcmp(token, "background-ping")) {
ccdf4138 2211 p->options.background_ping = true;
a37d6070 2212 } else if (!strcmp(token, "no-digest")) {
ccdf4138 2213 p->options.no_digest = true;
a37d6070 2214 } else if (!strcmp(token, "no-tproxy")) {
ccdf4138 2215 p->options.no_tproxy = true;
a37d6070 2216 } else if (!strcmp(token, "multicast-responder")) {
ccdf4138 2217 p->options.mcast_responder = true;
8a368316 2218#if PEER_MULTICAST_SIBLINGS
a37d6070 2219 } else if (!strcmp(token, "multicast-siblings")) {
ccdf4138 2220 p->options.mcast_siblings = true;
8a368316 2221#endif
a37d6070 2222 } else if (!strncmp(token, "weight=", 7)) {
62e76326 2223 p->weight = xatoi(token + 7);
a37d6070 2224 } else if (!strncmp(token, "basetime=", 9)) {
62e76326 2225 p->basetime = xatoi(token + 9);
a37d6070 2226 } else if (!strcmp(token, "closest-only")) {
ccdf4138 2227 p->options.closest_only = true;
a37d6070 2228 } else if (!strncmp(token, "ttl=", 4)) {
62e76326 2229 p->mcast.ttl = xatoi(token + 4);
2230
2231 if (p->mcast.ttl < 0)
2232 p->mcast.ttl = 0;
2233
2234 if (p->mcast.ttl > 128)
2235 p->mcast.ttl = 128;
a37d6070 2236 } else if (!strcmp(token, "default")) {
ccdf4138 2237 p->options.default_parent = true;
a37d6070 2238 } else if (!strcmp(token, "round-robin")) {
ccdf4138 2239 p->options.roundrobin = true;
a37d6070 2240 } else if (!strcmp(token, "weighted-round-robin")) {
ccdf4138 2241 p->options.weighted_roundrobin = true;
dc9d133b 2242#if USE_HTCP
a37d6070 2243 } else if (!strcmp(token, "htcp")) {
ccdf4138 2244 p->options.htcp = true;
a37d6070 2245 } else if (!strncmp(token, "htcp=", 5) || !strncmp(token, "htcp-", 5)) {
18191440 2246 /* Note: The htcp- form is deprecated, replaced by htcp= */
ccdf4138 2247 p->options.htcp = true;
18191440
AJ
2248 char *tmp = xstrdup(token+5);
2249 char *mode, *nextmode;
2250 for (mode = nextmode = tmp; mode; mode = nextmode) {
2251 nextmode = strchr(mode, ',');
a38ec4b1
FC
2252 if (nextmode) {
2253 *nextmode = '\0';
2254 ++nextmode;
2255 }
a37d6070 2256 if (!strcmp(mode, "no-clr")) {
18191440
AJ
2257 if (p->options.htcp_only_clr)
2258 fatalf("parse_peer: can't set htcp-no-clr and htcp-only-clr simultaneously");
ccdf4138 2259 p->options.htcp_no_clr = true;
a37d6070 2260 } else if (!strcmp(mode, "no-purge-clr")) {
ccdf4138 2261 p->options.htcp_no_purge_clr = true;
a37d6070 2262 } else if (!strcmp(mode, "only-clr")) {
18191440
AJ
2263 if (p->options.htcp_no_clr)
2264 fatalf("parse_peer: can't set htcp no-clr and only-clr simultaneously");
ccdf4138 2265 p->options.htcp_only_clr = true;
a37d6070 2266 } else if (!strcmp(mode, "forward-clr")) {
ccdf4138 2267 p->options.htcp_forward_clr = true;
a37d6070 2268 } else if (!strcmp(mode, "oldsquid")) {
ccdf4138 2269 p->options.htcp_oldsquid = true;
18191440
AJ
2270 } else {
2271 fatalf("invalid HTCP mode '%s'", mode);
2272 }
2273 }
2274 safe_free(tmp);
dc9d133b 2275#endif
a37d6070 2276 } else if (!strcmp(token, "no-netdb-exchange")) {
ccdf4138 2277 p->options.no_netdb_exchange = true;
62e76326 2278
a37d6070 2279 } else if (!strcmp(token, "carp")) {
62e76326 2280 if (p->type != PEER_PARENT)
a555a85b 2281 throw TextException(ToSBuf("non-parent carp cache_peer ", *p), Here());
62e76326 2282
ccdf4138 2283 p->options.carp = true;
a37d6070 2284 } else if (!strncmp(token, "carp-key=", 9)) {
ccdf4138 2285 if (p->options.carp != true)
a555a85b 2286 throw TextException(ToSBuf("carp-key specified on non-carp cache_peer ", *p), Here());
ccdf4138 2287 p->options.carp_key.set = true;
de03b596
FC
2288 char *nextkey=token+strlen("carp-key="), *key=nextkey;
2289 for (; key; key = nextkey) {
96f6f33b
A
2290 nextkey=strchr(key,',');
2291 if (nextkey) ++nextkey; // skip the comma, any
a37d6070 2292 if (0==strncmp(key,"scheme",6)) {
ccdf4138 2293 p->options.carp_key.scheme = true;
a37d6070 2294 } else if (0==strncmp(key,"host",4)) {
ccdf4138 2295 p->options.carp_key.host = true;
a37d6070 2296 } else if (0==strncmp(key,"port",4)) {
ccdf4138 2297 p->options.carp_key.port = true;
a37d6070 2298 } else if (0==strncmp(key,"path",4)) {
ccdf4138 2299 p->options.carp_key.path = true;
a37d6070 2300 } else if (0==strncmp(key,"params",6)) {
ccdf4138 2301 p->options.carp_key.params = true;
96f6f33b
A
2302 } else {
2303 fatalf("invalid carp-key '%s'",key);
2304 }
de03b596 2305 }
a37d6070 2306 } else if (!strcmp(token, "userhash")) {
2f1431ea 2307#if USE_AUTH
f7e1d9ce 2308 if (p->type != PEER_PARENT)
a555a85b 2309 throw TextException(ToSBuf("non-parent userhash cache_peer ", *p), Here());
f7e1d9ce 2310
ccdf4138 2311 p->options.userhash = true;
2f1431ea 2312#else
a555a85b 2313 throw TextException(ToSBuf("missing authentication support; required for userhash cache_peer ", *p), Here());
2f1431ea 2314#endif
a37d6070 2315 } else if (!strcmp(token, "sourcehash")) {
f7e1d9ce 2316 if (p->type != PEER_PARENT)
a555a85b 2317 throw TextException(ToSBuf("non-parent sourcehash cache_peer ", *p), Here());
f7e1d9ce 2318
ccdf4138 2319 p->options.sourcehash = true;
f7e1d9ce 2320
a37d6070 2321 } else if (!strcmp(token, "no-delay")) {
9a0a18de 2322#if USE_DELAY_POOLS
ccdf4138 2323 p->options.no_delay = true;
9a0a18de
AJ
2324#else
2325 debugs(0, DBG_CRITICAL, "WARNING: cache_peer option 'no-delay' requires --enable-delay-pools");
afd88fbe 2326#endif
a37d6070 2327 } else if (!strncmp(token, "login=", 6)) {
62e76326 2328 p->login = xstrdup(token + 6);
2329 rfc1738_unescape(p->login);
9825b398
AJ
2330 } else if (!strcmp(token, "auth-no-keytab")) {
2331 p->options.auth_no_keytab = 1;
a37d6070 2332 } else if (!strncmp(token, "connect-timeout=", 16)) {
0ce8e93b 2333 p->connect_timeout_raw = xatoi(token + 16);
a37d6070 2334 } else if (!strncmp(token, "connect-fail-limit=", 19)) {
ff9970cc 2335 p->connect_fail_limit = xatoi(token + 19);
7e3ce7b9 2336#if USE_CACHE_DIGESTS
a37d6070 2337 } else if (!strncmp(token, "digest-url=", 11)) {
62e76326 2338 p->digest_url = xstrdup(token + 11);
7e3ce7b9 2339#endif
62e76326 2340
a37d6070 2341 } else if (!strcmp(token, "allow-miss")) {
ccdf4138 2342 p->options.allow_miss = true;
a37d6070 2343 } else if (!strncmp(token, "max-conn=", 9)) {
62e76326 2344 p->max_conn = xatoi(token + 9);
e8dca475
CT
2345 } else if (!strncmp(token, "standby=", 8)) {
2346 p->standby.limit = xatoi(token + 8);
a37d6070 2347 } else if (!strcmp(token, "originserver")) {
ccdf4138 2348 p->options.originserver = true;
a37d6070 2349 } else if (!strncmp(token, "name=", 5)) {
a555a85b 2350 p->rename(token + 5);
a37d6070 2351 } else if (!strncmp(token, "forceddomain=", 13)) {
62e76326 2352 safe_free(p->domain);
62e76326 2353 if (token[13])
2354 p->domain = xstrdup(token + 13);
2355
cdfb670c
AJ
2356 } else if (strncmp(token, "ssl", 3) == 0) {
2357#if !USE_OPENSSL
2358 debugs(0, DBG_CRITICAL, "WARNING: cache_peer option '" << token << "' requires --with-openssl");
2359#else
0b0e0864 2360 p->secure.parse(token+3);
cdfb670c 2361#endif
1cc44095 2362 } else if (strncmp(token, "tls-", 4) == 0) {
1cc44095 2363 p->secure.parse(token+4);
0ff7e52d
AJ
2364 } else if (strncmp(token, "tls", 3) == 0) {
2365 p->secure.parse(token+3);
62e76326 2366 } else if (strcmp(token, "front-end-https") == 0) {
2367 p->front_end_https = 1;
2368 } else if (strcmp(token, "front-end-https=on") == 0) {
2369 p->front_end_https = 1;
2370 } else if (strcmp(token, "front-end-https=auto") == 0) {
2371 p->front_end_https = 2;
26ac0430 2372 } else if (strcmp(token, "connection-auth=off") == 0) {
d67acb4e
AJ
2373 p->connection_auth = 0;
2374 } else if (strcmp(token, "connection-auth") == 0) {
2375 p->connection_auth = 1;
2376 } else if (strcmp(token, "connection-auth=on") == 0) {
2377 p->connection_auth = 1;
2378 } else if (strcmp(token, "connection-auth=auto") == 0) {
2379 p->connection_auth = 2;
9d22bac3
AJ
2380 } else if (token[0] == '#') {
2381 // start of a text comment. stop reading this line.
2382 break;
62e76326 2383 } else {
9d22bac3 2384 debugs(3, DBG_PARSE_NOTE(DBG_IMPORTANT), "ERROR: Ignoring unknown cache_peer option '" << token << "'");
62e76326 2385 }
270b86af 2386 }
62e76326 2387
a555a85b
AR
2388 if (findCachePeerByName(p->name))
2389 throw TextException(ToSBuf("cache_peer ", *p, " specified twice"), Here());
62e76326 2390
e8dca475 2391 if (p->max_conn > 0 && p->max_conn < p->standby.limit)
a555a85b
AR
2392 throw TextException(ToSBuf("cache_peer ", *p, " max-conn=", p->max_conn,
2393 " is lower than its standby=", p->standby.limit), Here());
e8dca475 2394
40a1495e 2395 if (p->weight < 1)
62e76326 2396 p->weight = 1;
2397
ff9970cc 2398 if (p->connect_fail_limit < 1)
a4d889e0 2399 p->connect_fail_limit = 10;
ff9970cc 2400
e13ee7ad 2401#if USE_CACHE_DIGESTS
b56b37cf 2402 if (!p->options.no_digest)
0f783794 2403 p->digest = new PeerDigest(p);
e13ee7ad 2404#endif
cc192b50 2405
151644b7
AJ
2406 if (p->secure.encryptTransport)
2407 p->secure.parseOptions();
2408
2e24d0bf
EB
2409 if (!*peers)
2410 *peers = new CachePeers;
cc192b50 2411
2e24d0bf 2412 (*peers)->add(p);
62e76326 2413
2e24d0bf 2414 p->index = (*peers)->size();
62e76326 2415
32a47e3e 2416 peerClearRRStart();
0153d498 2417}
2418
2419static void
2e24d0bf 2420free_peer(CachePeers ** const peers)
0153d498 2421{
2e24d0bf
EB
2422 delete *peers;
2423 *peers = nullptr;
270b86af 2424}
2425
2426static void
613924ee 2427dump_cachemgrpasswd(StoreEntry * entry, const char *name, Mgr::ActionPasswordList * list)
270b86af 2428{
a5f27c62 2429 while (list) {
62e76326 2430 if (strcmp(list->passwd, "none") && strcmp(list->passwd, "disable"))
2431 storeAppendPrintf(entry, "%s XXXXXXXXXX", name);
2432 else
2433 storeAppendPrintf(entry, "%s %s", name, list->passwd);
2434
a5f27c62
AJ
2435 for (auto w : list->actions)
2436 entry->appendf(" " SQUIDSBUFPH, SQUIDSBUFPRINT(w));
62e76326 2437
2438 storeAppendPrintf(entry, "\n");
2439 list = list->next;
d41de3c1 2440 }
270b86af 2441}
2442
2443static void
613924ee 2444parse_cachemgrpasswd(Mgr::ActionPasswordList ** head)
270b86af 2445{
a5f27c62 2446 char *passwd = nullptr;
270b86af 2447 parse_string(&passwd);
a5f27c62
AJ
2448
2449 Mgr::ActionPasswordList *p = new Mgr::ActionPasswordList;
22f3fd98 2450 p->passwd = passwd;
62e76326 2451
a5f27c62
AJ
2452 while (char *token = ConfigParser::NextQuotedToken())
2453 p->actions.push_back(SBuf(token));
2454
2455 Mgr::ActionPasswordList **P;
26aa7e31 2456 for (P = head; *P; P = &(*P)->next) {
62e76326 2457 /*
2458 * See if any of the actions from this line already have a
2459 * password from previous lines. The password checking
2460 * routines in cache_manager.c take the the password from
613924ee 2461 * the first Mgr::ActionPasswordList that contains the
62e76326 2462 * requested action. Thus, we should warn users who might
2463 * think they can have two passwords for the same action.
2464 */
a5f27c62
AJ
2465 for (const auto &w : (*P)->actions) {
2466 for (const auto &u : p->actions) {
2467 if (w != u)
62e76326 2468 continue;
2469
a5f27c62 2470 debugs(0, DBG_PARSE_NOTE(1), "ERROR: action '" << u << "' (line " << config_lineno << ") already has a password");
62e76326 2471 }
2472 }
26aa7e31 2473 }
62e76326 2474
22f3fd98 2475 *P = p;
270b86af 2476}
2477
2478static void
613924ee 2479free_cachemgrpasswd(Mgr::ActionPasswordList ** head)
270b86af 2480{
a5f27c62
AJ
2481 delete *head;
2482 *head = nullptr;
270b86af 2483}
2484
8203a132 2485static void
7f0b3324 2486dump_denyinfo(StoreEntry * entry, const char *name, AclDenyInfoList * var)
270b86af 2487{
aee3523a 2488 while (var != nullptr) {
62e76326 2489 storeAppendPrintf(entry, "%s %s", name, var->err_page_name);
2490
1205859f
AJ
2491 for (const auto &aclName: var->acl_list)
2492 storeAppendPrintf(entry, " " SQUIDSBUFPH, SQUIDSBUFPRINT(aclName));
62e76326 2493
2494 storeAppendPrintf(entry, "\n");
2495
2496 var = var->next;
d41de3c1 2497 }
270b86af 2498}
2499
2500static void
7f0b3324 2501parse_denyinfo(AclDenyInfoList ** var)
6e40f263 2502{
f1dc9b30 2503 aclParseDenyInfoLine(var);
6e40f263 2504}
403279e0 2505
1273d501 2506void
7f0b3324 2507free_denyinfo(AclDenyInfoList ** list)
3c5557f9 2508{
2e00d185
AJ
2509 delete *list;
2510 *list = nullptr;
270b86af 2511}
2512
2513static void
505e35db 2514parse_peer_access(void)
270b86af 2515{
a555a85b 2516 auto &p = LegacyParser.cachePeer("cache_peer_access peer-name");
6f58d7d7 2517 std::string directive = "peer_access ";
a555a85b
AR
2518 directive += p.name;
2519 aclParseAccessLine(directive.c_str(), LegacyParser, &p.access);
270b86af 2520}
2521
270b86af 2522static void
2523parse_hostdomaintype(void)
2524{
a555a85b 2525 auto &p = LegacyParser.cachePeer("neighbor_type_domain peer-name");
62e76326 2526
ba724607
AJ
2527 char *type = ConfigParser::NextToken();
2528 if (!type) {
62e76326 2529 self_destruct();
ba724607
AJ
2530 return;
2531 }
62e76326 2532
ba724607 2533 char *domain = nullptr;
2eceb328 2534 while ((domain = ConfigParser::NextToken())) {
ba724607 2535 auto *l = static_cast<NeighborTypeDomainList *>(xcalloc(1, sizeof(NeighborTypeDomainList)));
62e76326 2536 l->type = parseNeighborType(type);
2537 l->domain = xstrdup(domain);
2538
ba724607 2539 NeighborTypeDomainList **L = nullptr;
a555a85b 2540 for (L = &p.typelist; *L; L = &((*L)->next));
62e76326 2541 *L = l;
f1dc9b30 2542 }
270b86af 2543}
2544
270b86af 2545static void
a7d59104 2546dump_int(StoreEntry * entry, const char *name, int var)
270b86af 2547{
f53b06f9 2548 storeAppendPrintf(entry, "%s %d\n", name, var);
270b86af 2549}
c1c29eb6 2550
94439e4e 2551void
270b86af 2552parse_int(int *var)
2553{
270b86af 2554 int i;
0e4e0e7d 2555 i = GetInteger();
270b86af 2556 *var = i;
2557}
090089c4 2558
0153d498 2559static void
2560free_int(int *var)
2561{
a47b9029 2562 *var = 0;
0153d498 2563}
2564
8f7dbf74
DD
2565static void
2566dump_int64_t(StoreEntry * entry, const char *name, int64_t var)
2567{
2568 storeAppendPrintf(entry, "%s %" PRId64 "\n", name, var);
2569}
2570
8b082ed9 2571static void
8f7dbf74
DD
2572parse_int64_t(int64_t *var)
2573{
2574 int64_t i;
2575 i = GetInteger64();
2576 *var = i;
2577}
2578
2579static void
2580free_int64_t(int64_t *var)
2581{
2582 *var = 0;
2583}
2584
270b86af 2585static void
a7d59104 2586dump_onoff(StoreEntry * entry, const char *name, int var)
270b86af 2587{
f53b06f9 2588 storeAppendPrintf(entry, "%s %s\n", name, var ? "on" : "off");
270b86af 2589}
090089c4 2590
d205783b 2591void
270b86af 2592parse_onoff(int *var)
2593{
2eceb328 2594 char *token = ConfigParser::NextToken();
ba724607 2595 if (!token) {
62e76326 2596 self_destruct();
ba724607
AJ
2597 return;
2598 }
62e76326 2599
a37d6070 2600 if (!strcmp(token, "on")) {
62e76326 2601 *var = 1;
a37d6070 2602 } else if (!strcmp(token, "enable")) {
54a063a2
TX
2603 debugs(0, DBG_PARSE_NOTE(DBG_IMPORTANT), "WARNING: 'enable' is deprecated. Please update to use 'on'.");
2604 *var = 1;
a37d6070 2605 } else if (!strcmp(token, "off")) {
62e76326 2606 *var = 0;
a37d6070 2607 } else if (!strcmp(token, "disable")) {
54a063a2
TX
2608 debugs(0, DBG_PARSE_NOTE(DBG_IMPORTANT), "WARNING: 'disable' is deprecated. Please update to use 'off'.");
2609 *var = 0;
2610 } else {
2611 debugs(0, DBG_PARSE_NOTE(DBG_IMPORTANT), "ERROR: Invalid option: Boolean options can only be 'on' or 'off'.");
2612 self_destruct();
2613 }
270b86af 2614}
e90100aa 2615
0153d498 2616#define free_onoff free_int
52d3f198 2617
2618static void
2619dump_tristate(StoreEntry * entry, const char *name, int var)
2620{
2621 const char *state;
2622
2623 if (var > 0)
2624 state = "on";
2625 else if (var < 0)
2626 state = "warn";
2627 else
2628 state = "off";
2629
2630 storeAppendPrintf(entry, "%s %s\n", name, state);
2631}
2632
2633static void
2634parse_tristate(int *var)
2635{
2eceb328 2636 char *token = ConfigParser::NextToken();
ba724607 2637 if (!token) {
52d3f198 2638 self_destruct();
ba724607
AJ
2639 return;
2640 }
52d3f198 2641
a37d6070 2642 if (!strcmp(token, "on")) {
54a063a2 2643 *var = 1;
a37d6070 2644 } else if (!strcmp(token, "enable")) {
54a063a2 2645 debugs(0, DBG_PARSE_NOTE(DBG_IMPORTANT), "WARNING: 'enable' is deprecated. Please update to use value 'on'.");
52d3f198 2646 *var = 1;
a37d6070 2647 } else if (!strcmp(token, "warn")) {
52d3f198 2648 *var = -1;
a37d6070 2649 } else if (!strcmp(token, "off")) {
52d3f198 2650 *var = 0;
a37d6070 2651 } else if (!strcmp(token, "disable")) {
54a063a2
TX
2652 debugs(0, DBG_PARSE_NOTE(DBG_IMPORTANT), "WARNING: 'disable' is deprecated. Please update to use value 'off'.");
2653 *var = 0;
2654 } else {
2655 debugs(0, DBG_PARSE_NOTE(DBG_IMPORTANT), "ERROR: Invalid option: Tristate options can only be 'on', 'off', or 'warn'.");
2656 self_destruct();
2657 }
52d3f198 2658}
2659
2660#define free_tristate free_int
30a4f2a8 2661
8b082ed9 2662static void
079a8480
AJ
2663parse_pipelinePrefetch(int *var)
2664{
bde7a8ce 2665 char *token = ConfigParser::PeekAtToken();
ba724607 2666 if (!token) {
079a8480 2667 self_destruct();
ba724607
AJ
2668 return;
2669 }
079a8480
AJ
2670
2671 if (!strcmp(token, "on")) {
2672 debugs(0, DBG_PARSE_NOTE(DBG_IMPORTANT), "WARNING: 'pipeline_prefetch on' is deprecated. Please update to use 1 (or a higher number).");
2673 *var = 1;
bde7a8ce
CT
2674 //pop the token
2675 (void)ConfigParser::NextToken();
079a8480
AJ
2676 } else if (!strcmp(token, "off")) {
2677 debugs(0, DBG_PARSE_NOTE(2), "WARNING: 'pipeline_prefetch off' is deprecated. Please update to use '0'.");
2678 *var = 0;
bde7a8ce
CT
2679 //pop the token
2680 (void)ConfigParser::NextToken();
2681 } else
079a8480 2682 parse_int(var);
079a8480
AJ
2683}
2684
2685#define free_pipelinePrefetch free_int
2686#define dump_pipelinePrefetch dump_int
2687
270b86af 2688static void
8d9a8184 2689dump_refreshpattern(StoreEntry * entry, const char *name, RefreshPattern * head)
270b86af 2690{
aee3523a 2691 while (head != nullptr) {
0fa036e3
AR
2692 PackableStream os(*entry);
2693 os << name << ' ';
2694 head->printHead(os);
4c3ef9b2 2695
570d3f75
AJ
2696 if (head->max_stale >= 0)
2697 storeAppendPrintf(entry, " max-stale=%d", head->max_stale);
2698
4c3ef9b2 2699 if (head->flags.refresh_ims)
2700 storeAppendPrintf(entry, " refresh-ims");
2701
3d8b6ba4
AJ
2702 if (head->flags.store_stale)
2703 storeAppendPrintf(entry, " store-stale");
2704
626096be 2705#if USE_HTTP_VIOLATIONS
62e76326 2706
2707 if (head->flags.override_expire)
2708 storeAppendPrintf(entry, " override-expire");
2709
2710 if (head->flags.override_lastmod)
2711 storeAppendPrintf(entry, " override-lastmod");
2712
2713 if (head->flags.reload_into_ims)
2714 storeAppendPrintf(entry, " reload-into-ims");
2715
2716 if (head->flags.ignore_reload)
2717 storeAppendPrintf(entry, " ignore-reload");
2718
38f9c547 2719 if (head->flags.ignore_no_store)
2720 storeAppendPrintf(entry, " ignore-no-store");
2721
2722 if (head->flags.ignore_private)
2723 storeAppendPrintf(entry, " ignore-private");
9f60cfdf 2724#endif
62e76326 2725
2726 storeAppendPrintf(entry, "\n");
2727
2728 head = head->next;
d41de3c1 2729 }
270b86af 2730}
090089c4 2731
270b86af 2732static void
8d9a8184 2733parse_refreshpattern(RefreshPattern ** head)
270b86af 2734{
f1dc9b30 2735 char *token;
f1dc9b30 2736 time_t min = 0;
c3f6d204 2737 double pct = 0.0;
f1dc9b30 2738 time_t max = 0;
4c3ef9b2 2739 int refresh_ims = 0;
3d8b6ba4 2740 int store_stale = 0;
570d3f75 2741 int max_stale = -1;
3d8b6ba4 2742
626096be 2743#if USE_HTTP_VIOLATIONS
62e76326 2744
1dfa1d81 2745 int override_expire = 0;
2746 int override_lastmod = 0;
cbe3a719 2747 int reload_into_ims = 0;
2748 int ignore_reload = 0;
38f9c547 2749 int ignore_no_store = 0;
2750 int ignore_private = 0;
9f60cfdf 2751#endif
62e76326 2752
c2afddd8
AJ
2753 int i;
2754 RefreshPattern *t;
c2afddd8 2755
0fa036e3 2756 auto regex = LegacyParser.regex("refresh_pattern regex");
62e76326 2757
c2afddd8 2758 i = GetInteger(); /* token: min */
62e76326 2759
a632ba71
AJ
2760 /* catch negative and insanely huge values close to 32-bit wrap */
2761 if (i < 0) {
2762 debugs(3, DBG_IMPORTANT, "WARNING: refresh_pattern minimum age negative. Cropped back to zero.");
2763 i = 0;
2764 }
2765 if (i > 60*24*365) {
2766 debugs(3, DBG_IMPORTANT, "WARNING: refresh_pattern minimum age too high. Cropped back to 1 year.");
2767 i = 60*24*365;
2768 }
2769
f53969cc 2770 min = (time_t) (i * 60); /* convert minutes to seconds */
62e76326 2771
1454480a 2772 pct = GetPercentage(false); /* token: pct . with no limit on size */
62e76326 2773
f53969cc 2774 i = GetInteger(); /* token: max */
62e76326 2775
a632ba71
AJ
2776 /* catch negative and insanely huge values close to 32-bit wrap */
2777 if (i < 0) {
2778 debugs(3, DBG_IMPORTANT, "WARNING: refresh_pattern maximum age negative. Cropped back to zero.");
2779 i = 0;
2780 }
2781 if (i > 60*24*365) {
2782 debugs(3, DBG_IMPORTANT, "WARNING: refresh_pattern maximum age too high. Cropped back to 1 year.");
2783 i = 60*24*365;
2784 }
2785
f53969cc 2786 max = (time_t) (i * 60); /* convert minutes to seconds */
62e76326 2787
1dfa1d81 2788 /* Options */
aee3523a 2789 while ((token = ConfigParser::NextToken()) != nullptr) {
4c3ef9b2 2790 if (!strcmp(token, "refresh-ims")) {
2791 refresh_ims = 1;
3d8b6ba4
AJ
2792 } else if (!strcmp(token, "store-stale")) {
2793 store_stale = 1;
570d3f75 2794 } else if (!strncmp(token, "max-stale=", 10)) {
54a063a2 2795 max_stale = xatoi(token + 10);
064679ea 2796
626096be 2797#if USE_HTTP_VIOLATIONS
62e76326 2798
4c3ef9b2 2799 } else if (!strcmp(token, "override-expire"))
62e76326 2800 override_expire = 1;
2801 else if (!strcmp(token, "override-lastmod"))
2802 override_lastmod = 1;
38f9c547 2803 else if (!strcmp(token, "ignore-no-store"))
2804 ignore_no_store = 1;
2805 else if (!strcmp(token, "ignore-private"))
2806 ignore_private = 1;
62e76326 2807 else if (!strcmp(token, "reload-into-ims")) {
2808 reload_into_ims = 1;
2809 refresh_nocache_hack = 1;
2810 /* tell client_side.c that this is used */
2811 } else if (!strcmp(token, "ignore-reload")) {
2812 ignore_reload = 1;
2813 refresh_nocache_hack = 1;
2814 /* tell client_side.c that this is used */
9f60cfdf 2815#endif
62e76326 2816
064679ea
AJ
2817 } else if (!strcmp(token, "ignore-no-cache") ||
2818 !strcmp(token, "ignore-must-revalidate") ||
2819 !strcmp(token, "ignore-auth")
7766dad3 2820 ) {
064679ea 2821 debugs(22, DBG_PARSE_NOTE(2), "UPGRADE: refresh_pattern option '" << token << "' is obsolete. Remove it.");
4c3ef9b2 2822 } else
0fa036e3 2823 debugs(22, DBG_CRITICAL, "ERROR: Unknown refresh_pattern option: " << token);
f1dc9b30 2824 }
62e76326 2825
c3f6d204 2826 pct = pct < 0.0 ? 0.0 : pct;
f1dc9b30 2827 max = max < 0 ? 0 : max;
0fa036e3 2828 t = new RefreshPattern(std::move(regex));
f1dc9b30 2829 t->min = min;
c3f6d204 2830 t->pct = pct;
f1dc9b30 2831 t->max = max;
62e76326 2832
4c3ef9b2 2833 if (refresh_ims)
be4d35dc 2834 t->flags.refresh_ims = true;
4c3ef9b2 2835
3d8b6ba4 2836 if (store_stale)
be4d35dc 2837 t->flags.store_stale = true;
3d8b6ba4 2838
570d3f75
AJ
2839 t->max_stale = max_stale;
2840
626096be 2841#if USE_HTTP_VIOLATIONS
62e76326 2842
1dfa1d81 2843 if (override_expire)
be4d35dc 2844 t->flags.override_expire = true;
62e76326 2845
1dfa1d81 2846 if (override_lastmod)
be4d35dc 2847 t->flags.override_lastmod = true;
62e76326 2848
cbe3a719 2849 if (reload_into_ims)
be4d35dc 2850 t->flags.reload_into_ims = true;
62e76326 2851
cbe3a719 2852 if (ignore_reload)
be4d35dc 2853 t->flags.ignore_reload = true;
62e76326 2854
38f9c547 2855 if (ignore_no_store)
be4d35dc 2856 t->flags.ignore_no_store = true;
38f9c547 2857
2858 if (ignore_private)
be4d35dc 2859 t->flags.ignore_private = true;
9f60cfdf 2860#endif
62e76326 2861
aee3523a 2862 t->next = nullptr;
62e76326 2863
f1dc9b30 2864 while (*head)
62e76326 2865 head = &(*head)->next;
2866
f1dc9b30 2867 *head = t;
270b86af 2868}
090089c4 2869
270b86af 2870static void
8d9a8184 2871free_refreshpattern(RefreshPattern ** head)
270b86af 2872{
95b8eae2
AJ
2873 delete *head;
2874 *head = nullptr;
c2066637 2875
626096be 2876#if USE_HTTP_VIOLATIONS
c2066637 2877 refresh_nocache_hack = 0;
8970d351 2878
2879#endif
270b86af 2880}
12b9e9b1 2881
270b86af 2882static void
a7d59104 2883dump_string(StoreEntry * entry, const char *name, char *var)
270b86af 2884{
aee3523a 2885 if (var != nullptr)
62e76326 2886 storeAppendPrintf(entry, "%s %s\n", name, var);
270b86af 2887}
98ffb7e4 2888
270b86af 2889static void
0153d498 2890parse_string(char **var)
270b86af 2891{
270b86af 2892 safe_free(*var);
62e76326 2893
ba724607
AJ
2894 char *token = ConfigParser::NextToken();
2895 if (!token) {
62e76326 2896 self_destruct();
ba724607
AJ
2897 return;
2898 }
62e76326 2899
270b86af 2900 *var = xstrdup(token);
2901}
b15e6857 2902
0153d498 2903static void
2904free_string(char **var)
2905{
027acbaf 2906 safe_free(*var);
0153d498 2907}
caebbe00 2908
94439e4e 2909void
f1dc9b30 2910parse_eol(char *volatile *var)
270b86af 2911{
65657c1a
AJ
2912 if (!var) {
2913 self_destruct();
2914 return;
2915 }
2916
2eceb328 2917 unsigned char *token = (unsigned char *) ConfigParser::NextQuotedOrToEol();
270b86af 2918 safe_free(*var);
62e76326 2919
9eeb8e4b 2920 if (!token) {
62e76326 2921 self_destruct();
9eeb8e4b 2922 return;
2923 }
62e76326 2924
e4755e29 2925 while (*token && xisspace(*token))
a38ec4b1 2926 ++token;
62e76326 2927
9eeb8e4b 2928 if (!*token) {
62e76326 2929 self_destruct();
9eeb8e4b 2930 return;
2931 }
62e76326 2932
852751f7 2933 *var = xstrdup((char *) token);
270b86af 2934}
090089c4 2935
52d3f198 2936#define dump_eol dump_string
2937#define free_eol free_string
2938
b11724bb
CT
2939static void
2940parse_TokenOrQuotedString(char **var)
2941{
b11724bb
CT
2942 safe_free(*var);
2943
ba724607 2944 char *token = ConfigParser::NextQuotedToken();
ec1e53fa 2945 if (!token) {
b11724bb 2946 self_destruct();
ba724607
AJ
2947 return;
2948 }
b11724bb
CT
2949
2950 *var = xstrdup(token);
2951}
2952
2953#define dump_TokenOrQuotedString dump_string
2954#define free_TokenOrQuotedString free_string
2955
270b86af 2956static void
a7d59104 2957dump_time_t(StoreEntry * entry, const char *name, time_t var)
090089c4 2958{
608c50d2
FC
2959 PackableStream os(*entry);
2960 os << name << ' ' << var << " seconds\n";
090089c4 2961}
2962
94439e4e 2963void
a47b9029 2964parse_time_t(time_t * var)
0ffd22bc 2965{
8da861a5
EB
2966 const auto maxTime = std::numeric_limits<time_t>::max();
2967 const auto seconds = parseTimeLine<std::chrono::seconds>();
2968 if (maxTime < seconds.count())
2969 throw TexcHere(ToSBuf("directive supports time values up to ", maxTime, " but is given ", seconds.count(), " seconds"));
2970 *var = static_cast<time_t>(seconds.count());
0ffd22bc 2971}
2972
270b86af 2973static void
a47b9029 2974free_time_t(time_t * var)
270b86af 2975{
a47b9029 2976 *var = 0;
270b86af 2977}
9906e724 2978
fd0f51c4
CT
2979static void
2980dump_time_msec(StoreEntry * entry, const char *name, time_msec_t var)
2981{
608c50d2 2982 PackableStream os(*entry);
fd0f51c4 2983 if (var % 1000)
608c50d2 2984 os << name << ' ' << var << " milliseconds\n";
fd0f51c4 2985 else
608c50d2 2986 os << name << ' ' << (var/1000) << " seconds\n";
fd0f51c4
CT
2987}
2988
8b082ed9
FC
2989static void
2990parse_time_msec(time_msec_t *var)
fd0f51c4 2991{
8da861a5 2992 *var = parseTimeLine<std::chrono::milliseconds>().count();
fd0f51c4
CT
2993}
2994
2995static void
2996free_time_msec(time_msec_t * var)
2997{
2998 *var = 0;
2999}
3000
8da861a5
EB
3001static void
3002dump_time_nanoseconds(StoreEntry *entry, const char *name, const std::chrono::nanoseconds &var)
3003{
eba422fc
FC
3004 // std::chrono::nanoseconds::rep is unknown a priori so we cast to (and print) the largest supported integer
3005 storeAppendPrintf(entry, "%s %jd nanoseconds\n", name, static_cast<intmax_t>(var.count()));
8da861a5
EB
3006}
3007
3008static void
3009parse_time_nanoseconds(std::chrono::nanoseconds *var)
3010{
3011 *var = parseTimeLine<std::chrono::nanoseconds>();
3012}
3013
3014static void
3015free_time_nanoseconds(std::chrono::nanoseconds *var)
3016{
3017 *var = std::chrono::nanoseconds::zero();
3018}
8da861a5 3019
1b635117 3020static void
a7d59104 3021dump_b_size_t(StoreEntry * entry, const char *name, size_t var)
9906e724 3022{
f53b06f9 3023 storeAppendPrintf(entry, "%s %d %s\n", name, (int) var, B_BYTES_STR);
9906e724 3024}
3025
e210930b
AJ
3026static void
3027dump_b_ssize_t(StoreEntry * entry, const char *name, ssize_t var)
3028{
3029 storeAppendPrintf(entry, "%s %d %s\n", name, (int) var, B_BYTES_STR);
3030}
e210930b 3031
47f6e231 3032static void
3033dump_b_int64_t(StoreEntry * entry, const char *name, int64_t var)
3034{
c91ca3ce 3035 storeAppendPrintf(entry, "%s %" PRId64 " %s\n", name, var, B_BYTES_STR);
47f6e231 3036}
3037
3038static void
3039dump_kb_int64_t(StoreEntry * entry, const char *name, int64_t var)
3040{
c91ca3ce 3041 storeAppendPrintf(entry, "%s %" PRId64 " %s\n", name, var, B_KBYTES_STR);
47f6e231 3042}
3043
1b635117 3044static void
3045parse_b_size_t(size_t * var)
9906e724 3046{
3047 parseBytesLine(var, B_BYTES_STR);
3048}
3049
e210930b
AJ
3050static void
3051parse_b_ssize_t(ssize_t * var)
3052{
3053 parseBytesLineSigned(var, B_BYTES_STR);
3054}
e210930b 3055
47f6e231 3056static void
3057parse_b_int64_t(int64_t * var)
3058{
3059 parseBytesLine64(var, B_BYTES_STR);
3060}
3061
3062static void
3063parse_kb_int64_t(int64_t * var)
3064{
3065 parseBytesLine64(var, B_KBYTES_STR);
3066}
3067
9906e724 3068static void
a47b9029 3069free_size_t(size_t * var)
9906e724 3070{
a47b9029 3071 *var = 0;
9906e724 3072}
3073
e210930b
AJ
3074static void
3075free_ssize_t(ssize_t * var)
3076{
3077 *var = 0;
3078}
e210930b 3079
47f6e231 3080static void
3081free_b_int64_t(int64_t * var)
3082{
3083 *var = 0;
3084}
3085
1b635117 3086#define free_b_size_t free_size_t
e210930b 3087#define free_b_ssize_t free_ssize_t
9906e724 3088#define free_kb_size_t free_size_t
3089#define free_mb_size_t free_size_t
3090#define free_gb_size_t free_size_t
47f6e231 3091#define free_kb_int64_t free_b_int64_t
090089c4 3092
8203a132 3093static void
f45dd259 3094dump_u_short(StoreEntry * entry, const char *name, unsigned short var)
090089c4 3095{
f53b06f9 3096 storeAppendPrintf(entry, "%s %d\n", name, var);
270b86af 3097}
090089c4 3098
0153d498 3099static void
f45dd259 3100free_u_short(unsigned short * u)
0153d498 3101{
3102 *u = 0;
3103}
3104
270b86af 3105static void
f45dd259 3106parse_u_short(unsigned short * var)
b67e2c8c 3107{
3108 ConfigParser::ParseUShort(var);
3109}
3110
3111void
f45dd259 3112ConfigParser::ParseUShort(unsigned short *var)
270b86af 3113{
0e656b69 3114 *var = GetShort();
090089c4 3115}
3116
3a69ddf3 3117void
3118ConfigParser::ParseBool(bool *var)
3119{
3120 int i = GetInteger();
3121
3122 if (0 == i)
3123 *var = false;
3124 else if (1 == i)
3125 *var = true;
3126 else
3127 self_destruct();
3128}
3129
270b86af 3130static void
a7d59104 3131dump_wordlist(StoreEntry * entry, const char *name, wordlist * list)
270b86af 3132{
aee3523a 3133 while (list != nullptr) {
62e76326 3134 storeAppendPrintf(entry, "%s %s\n", name, list->key);
3135 list = list->next;
429fdbec 3136 }
429fdbec 3137}
3138
3a69ddf3 3139void
3140ConfigParser::ParseWordList(wordlist ** list)
3141{
3142 parse_wordlist(list);
3143}
3144
94439e4e 3145void
270b86af 3146parse_wordlist(wordlist ** list)
429fdbec 3147{
270b86af 3148 char *token;
bde7a8ce 3149 while ((token = ConfigParser::NextQuotedToken()))
62e76326 3150 wordlistAdd(list, token);
429fdbec 3151}
270b86af 3152
63e9d884 3153static int
c6d5b87b 3154check_null_acl_access(acl_access * a)
63e9d884 3155{
aee3523a 3156 return a == nullptr;
63e9d884 3157}
3158
0153d498 3159#define free_wordlist wordlistDestroy
270b86af 3160
d548ee64 3161#define free_uri_whitespace free_int
3162
3163static void
3164parse_uri_whitespace(int *var)
3165{
2eceb328 3166 char *token = ConfigParser::NextToken();
ba724607 3167 if (!token) {
62e76326 3168 self_destruct();
ba724607
AJ
3169 return;
3170 }
62e76326 3171
a37d6070 3172 if (!strcmp(token, "strip"))
62e76326 3173 *var = URI_WHITESPACE_STRIP;
a37d6070 3174 else if (!strcmp(token, "deny"))
62e76326 3175 *var = URI_WHITESPACE_DENY;
a37d6070 3176 else if (!strcmp(token, "allow"))
62e76326 3177 *var = URI_WHITESPACE_ALLOW;
a37d6070 3178 else if (!strcmp(token, "encode"))
62e76326 3179 *var = URI_WHITESPACE_ENCODE;
a37d6070 3180 else if (!strcmp(token, "chop"))
62e76326 3181 *var = URI_WHITESPACE_CHOP;
54a063a2
TX
3182 else {
3183 debugs(0, DBG_PARSE_NOTE(2), "ERROR: Invalid option '" << token << "': 'uri_whitespace' accepts 'strip', 'deny', 'allow', 'encode', and 'chop'.");
62e76326 3184 self_destruct();
54a063a2 3185 }
d548ee64 3186}
3187
d548ee64 3188static void
3189dump_uri_whitespace(StoreEntry * entry, const char *name, int var)
3190{
c193c972 3191 const char *s;
62e76326 3192
d548ee64 3193 if (var == URI_WHITESPACE_ALLOW)
62e76326 3194 s = "allow";
d548ee64 3195 else if (var == URI_WHITESPACE_ENCODE)
62e76326 3196 s = "encode";
d548ee64 3197 else if (var == URI_WHITESPACE_CHOP)
62e76326 3198 s = "chop";
7e3ce7b9 3199 else if (var == URI_WHITESPACE_DENY)
62e76326 3200 s = "deny";
7e3ce7b9 3201 else
62e76326 3202 s = "strip";
3203
d548ee64 3204 storeAppendPrintf(entry, "%s %s\n", name, s);
3205}
3206
6a566b9c 3207static void
c1dd71ae 3208free_removalpolicy(RemovalPolicySettings ** settings)
6a566b9c 3209{
3210 if (!*settings)
62e76326 3211 return;
3212
6a566b9c 3213 free_string(&(*settings)->type);
62e76326 3214
6a566b9c 3215 free_wordlist(&(*settings)->args);
62e76326 3216
c8f4eac4 3217 delete *settings;
62e76326 3218
aee3523a 3219 *settings = nullptr;
6a566b9c 3220}
3221
3222static void
c1dd71ae 3223parse_removalpolicy(RemovalPolicySettings ** settings)
6a566b9c 3224{
3225 if (*settings)
62e76326 3226 free_removalpolicy(settings);
3227
c8f4eac4 3228 *settings = new RemovalPolicySettings;
62e76326 3229
6a566b9c 3230 parse_string(&(*settings)->type);
62e76326 3231
6a566b9c 3232 parse_wordlist(&(*settings)->args);
3233}
3234
3235static void
c1dd71ae 3236dump_removalpolicy(StoreEntry * entry, const char *name, RemovalPolicySettings * settings)
6a566b9c 3237{
3238 wordlist *args;
3239 storeAppendPrintf(entry, "%s %s", name, settings->type);
3240 args = settings->args;
62e76326 3241
6a566b9c 3242 while (args) {
62e76326 3243 storeAppendPrintf(entry, " %s", args->key);
3244 args = args->next;
6a566b9c 3245 }
62e76326 3246
be58afb5 3247 storeAppendPrintf(entry, "\n");
6a566b9c 3248}
c1dd71ae 3249
de0cb3d8
AR
3250inline void
3251free_YesNoNone(YesNoNone *)
ced8def3 3252{}
57af1e3f
AR
3253
3254static void
3255parse_YesNoNone(YesNoNone *option)
3256{
3257 int value = 0;
3258 parse_onoff(&value);
3259 option->configure(value > 0);
3260}
3261
3262static void
3263dump_YesNoNone(StoreEntry * entry, const char *name, YesNoNone &option)
3264{
3265 if (option.configured())
3266 dump_onoff(entry, name, option ? 1 : 0);
3267}
3268
ea21d497 3269static void
ced8def3
AJ
3270free_memcachemode(SquidConfig *)
3271{}
ea21d497
HN
3272
3273static void
ced8def3 3274parse_memcachemode(SquidConfig *)
ea21d497 3275{
2eceb328 3276 char *token = ConfigParser::NextToken();
ba724607 3277 if (!token) {
e1381638 3278 self_destruct();
ba724607
AJ
3279 return;
3280 }
ea21d497 3281
10aeba1d 3282 if (strcmp(token, "always") == 0) {
e1381638
AJ
3283 Config.onoff.memory_cache_first = 1;
3284 Config.onoff.memory_cache_disk = 1;
10aeba1d 3285 } else if (strcmp(token, "disk") == 0) {
e1381638
AJ
3286 Config.onoff.memory_cache_first = 0;
3287 Config.onoff.memory_cache_disk = 1;
ea21d497 3288 } else if (strncmp(token, "net", 3) == 0) {
e1381638
AJ
3289 Config.onoff.memory_cache_first = 1;
3290 Config.onoff.memory_cache_disk = 0;
10aeba1d 3291 } else if (strcmp(token, "never") == 0) {
e1381638
AJ
3292 Config.onoff.memory_cache_first = 0;
3293 Config.onoff.memory_cache_disk = 0;
54a063a2
TX
3294 } else {
3295 debugs(0, DBG_PARSE_NOTE(2), "ERROR: Invalid option '" << token << "': 'memory_cache_mode' accepts 'always', 'disk', 'network', and 'never'.");
e1381638 3296 self_destruct();
54a063a2 3297 }
ea21d497
HN
3298}
3299
3300static void
ced8def3 3301dump_memcachemode(StoreEntry * entry, const char *name, SquidConfig &)
ea21d497
HN
3302{
3303 storeAppendPrintf(entry, "%s ", name);
3304 if (Config.onoff.memory_cache_first && Config.onoff.memory_cache_disk)
e1381638 3305 storeAppendPrintf(entry, "always");
ea21d497 3306 else if (!Config.onoff.memory_cache_first && Config.onoff.memory_cache_disk)
e1381638 3307 storeAppendPrintf(entry, "disk");
ea21d497 3308 else if (Config.onoff.memory_cache_first && !Config.onoff.memory_cache_disk)
e1381638 3309 storeAppendPrintf(entry, "network");
ea21d497 3310 else if (!Config.onoff.memory_cache_first && !Config.onoff.memory_cache_disk)
e1381638 3311 storeAppendPrintf(entry, "none");
ea21d497
HN
3312 storeAppendPrintf(entry, "\n");
3313}
3314
cca8ba0d 3315#include "cf_parser.cci"
f1dc9b30 3316
3317peer_t
3318parseNeighborType(const char *s)
3319{
a37d6070 3320 if (!strcmp(s, "parent"))
62e76326 3321 return PEER_PARENT;
3322
a37d6070 3323 if (!strcmp(s, "neighbor"))
62e76326 3324 return PEER_SIBLING;
3325
a37d6070 3326 if (!strcmp(s, "neighbour"))
62e76326 3327 return PEER_SIBLING;
3328
a37d6070 3329 if (!strcmp(s, "sibling"))
62e76326 3330 return PEER_SIBLING;
3331
a37d6070 3332 if (!strcmp(s, "multicast"))
62e76326 3333 return PEER_MULTICAST;
3334
fa84c01d 3335 debugs(15, DBG_CRITICAL, "WARNING: Unknown neighbor type: " << s);
62e76326 3336
f1dc9b30 3337 return PEER_SIBLING;
3338}
f150dd4b 3339
0b0cfcf2 3340#if USE_WCCPv2
52f772de 3341static void
b7ac5457 3342parse_IpAddress_list(Ip::Address_list ** head)
52f772de 3343{
3344 char *token;
b7ac5457
AJ
3345 Ip::Address_list *s;
3346 Ip::Address ipa;
62e76326 3347
2eceb328 3348 while ((token = ConfigParser::NextToken())) {
82b7abe3
AJ
3349 if (GetHostWithPort(token, &ipa)) {
3350
3351 while (*head)
3352 head = &(*head)->next;
3353
b7ac5457 3354 s = static_cast<Ip::Address_list *>(xcalloc(1, sizeof(*s)));
82b7abe3
AJ
3355 s->s = ipa;
3356
3357 *head = s;
ba724607 3358 } else {
82b7abe3 3359 self_destruct();
ba724607
AJ
3360 return;
3361 }
7e3ce7b9 3362 }
3363}
3364
3365static void
b7ac5457 3366dump_IpAddress_list(StoreEntry * e, const char *n, const Ip::Address_list * s)
7e3ce7b9 3367{
cc192b50 3368 char ntoabuf[MAX_IPSTRLEN];
3369
7e3ce7b9 3370 while (s) {
cc192b50 3371 storeAppendPrintf(e, "%s %s\n",
62e76326 3372 n,
4dd643d5 3373 s->s.toStr(ntoabuf,MAX_IPSTRLEN));
62e76326 3374 s = s->next;
7e3ce7b9 3375 }
3376}
3377
3378static void
b7ac5457 3379free_IpAddress_list(Ip::Address_list ** head)
7e3ce7b9 3380{
26ac0430 3381 if (*head) delete *head;
aee3523a 3382 *head = nullptr;
7e3ce7b9 3383}
3384
0b0cfcf2 3385#if CURRENTLY_UNUSED
3386/* This code was previously used by http_port. Left as it really should
3387 * be used by icp_port and htcp_port
3388 */
7e3ce7b9 3389static int
b7ac5457 3390check_null_IpAddress_list(const Ip::Address_list * s)
7e3ce7b9 3391{
3392 return NULL == s;
3393}
62e76326 3394
3f38a55e 3395#endif /* CURRENTLY_UNUSED */
0b0cfcf2 3396#endif /* USE_WCCPv2 */
7e3ce7b9 3397
d193a436 3398static void
fa720bfb 3399parsePortSpecification(const AnyP::PortCfgPointer &s, char *token)
d193a436 3400{
aee3523a 3401 char *host = nullptr;
3f38a55e 3402 unsigned short port = 0;
aee3523a
AR
3403 char *t = nullptr;
3404 char *junk = nullptr;
62e76326 3405
5529ca8a 3406 s->disable_pmtu_discovery = DISABLE_PMTU_OFF;
13adaf1f 3407 s->name = xstrdup(token);
d67acb4e 3408 s->connection_auth_disabled = false;
5529ca8a 3409
d31d59d8 3410 const SBuf &portType = AnyP::UriScheme(s->transport.protocol).image();
eb6ac808 3411
cc192b50 3412 if (*token == '[') {
3413 /* [ipv6]:port */
26ac0430
AJ
3414 host = token + 1;
3415 t = strchr(host, ']');
3416 if (!t) {
eb6ac808 3417 debugs(3, DBG_CRITICAL, "FATAL: " << portType << "_port: missing ']' on IPv6 address: " << token);
26ac0430 3418 self_destruct();
ba724607 3419 return;
cc192b50 3420 }
a38ec4b1
FC
3421 *t = '\0';
3422 ++t;
26ac0430 3423 if (*t != ':') {
eb6ac808 3424 debugs(3, DBG_CRITICAL, "FATAL: " << portType << "_port: missing Port in: " << token);
26ac0430 3425 self_destruct();
ba724607 3426 return;
cc192b50 3427 }
055421ee 3428 if (!Ip::EnableIpv6) {
eb6ac808 3429 debugs(3, DBG_CRITICAL, "FATAL: " << portType << "_port: IPv6 is not available.");
26ac0430 3430 self_destruct();
ba724607 3431 return;
26ac0430 3432 }
055421ee
AJ
3433 port = xatos(t + 1);
3434 } else if ((t = strchr(token, ':'))) {
3435 /* host:port */
3436 /* ipv4:port */
3437 host = token;
3438 *t = '\0';
3439 port = xatos(t + 1);
3440
54a063a2
TX
3441 } else if (strtol(token, &junk, 10) && !*junk) {
3442 port = xatos(token);
eb6ac808 3443 debugs(3, 3, portType << "_port: found Listen on Port: " << port);
055421ee 3444 } else {
eb6ac808 3445 debugs(3, DBG_CRITICAL, "FATAL: " << portType << "_port: missing Port: " << token);
055421ee 3446 self_destruct();
ba724607 3447 return;
055421ee 3448 }
62e76326 3449
aee3523a 3450 if (port == 0 && host != nullptr) {
eb6ac808 3451 debugs(3, DBG_CRITICAL, "FATAL: " << portType << "_port: Port cannot be 0: " << token);
0e656b69 3452 self_destruct();
ba724607 3453 return;
cc192b50 3454 }
0e656b69 3455
aee3523a 3456 if (nullptr == host) {
4dd643d5
AJ
3457 s->s.setAnyAddr();
3458 s->s.port(port);
6df7ad56 3459 if (!Ip::EnableIpv6)
4dd643d5 3460 s->s.setIPv4();
eb6ac808 3461 debugs(3, 3, portType << "_port: found Listen on wildcard address: *:" << s->s.port());
c49d44e1 3462 } else if ( (s->s = host) ) { /* check/parse numeric IPA */
4dd643d5 3463 s->s.port(port);
6df7ad56 3464 if (!Ip::EnableIpv6)
4dd643d5 3465 s->s.setIPv4();
eb6ac808 3466 debugs(3, 3, portType << "_port: Listen on Host/IP: " << host << " --> " << s->s);
26ac0430 3467 } else if ( s->s.GetHostByName(host) ) { /* check/parse for FQDN */
61beade2 3468 /* do not use ipcache */
62e76326 3469 s->defaultsite = xstrdup(host);
4dd643d5 3470 s->s.port(port);
6df7ad56 3471 if (!Ip::EnableIpv6)
4dd643d5 3472 s->s.setIPv4();
eb6ac808 3473 debugs(3, 3, portType << "_port: found Listen as Host " << s->defaultsite << " on IP: " << s->s);
26ac0430 3474 } else {
eb6ac808 3475 debugs(3, DBG_CRITICAL, "FATAL: " << portType << "_port: failed to resolve Host/IP: " << host);
62e76326 3476 self_destruct();
cc192b50 3477 }
3f38a55e 3478}
3479
ecb19f1a
AR
3480/// parses the protocol= option of the *_port directive, returning parsed value
3481/// unsupported option values result in a fatal error message
3cc0f4e7 3482/// upper case values required; caller may convert for backward compatibility
ecb19f1a 3483static AnyP::ProtocolVersion
3cc0f4e7 3484parsePortProtocol(const SBuf &value)
ecb19f1a
AR
3485{
3486 // HTTP/1.0 not supported because we are version 1.1 which contains a superset of 1.0
3487 // and RFC 2616 requires us to upgrade 1.0 to 1.1
3cc0f4e7 3488 if (value.cmp("HTTP") == 0 || value.cmp("HTTP/1.1") == 0)
41cb7fe5 3489 return Http::ProtocolVersion(1,1);
ecb19f1a 3490
3cc0f4e7 3491 if (value.cmp("HTTPS") == 0 || value.cmp("HTTPS/1.1") == 0)
ecb19f1a
AR
3492 return AnyP::ProtocolVersion(AnyP::PROTO_HTTPS, 1,1);
3493
3cc0f4e7 3494 if (value.cmp("FTP") == 0)
aea65fec 3495 return Ftp::ProtocolVersion();
ecb19f1a 3496
3cc0f4e7 3497 fatalf("%s directive does not support protocol=" SQUIDSBUFPH "\n", cfg_directive, SQUIDSBUFPRINT(value));
ecb19f1a
AR
3498 return AnyP::ProtocolVersion(); // not reached
3499}
3500
3f38a55e 3501static void
fa720bfb 3502parse_port_option(AnyP::PortCfgPointer &s, char *token)
3f38a55e 3503{
c7b1dd5d
AJ
3504 /* modes first */
3505
3506 if (strcmp(token, "accel") == 0) {
151ba0d4 3507 if (s->flags.isIntercepted()) {
3cc0f4e7 3508 debugs(3, DBG_CRITICAL, "FATAL: " << cfg_directive << ": Accelerator mode requires its own port. It cannot be shared with other modes.");
c7b1dd5d 3509 self_destruct();
ba724607 3510 return;
c7b1dd5d 3511 }
6a25a046 3512 s->flags.accelSurrogate = true;
86ab7a90 3513 s->vhost = true;
c7b1dd5d 3514 } else if (strcmp(token, "transparent") == 0 || strcmp(token, "intercept") == 0) {
151ba0d4 3515 if (s->flags.accelSurrogate || s->flags.tproxyIntercept) {
3cc0f4e7 3516 debugs(3, DBG_CRITICAL, "FATAL: " << cfg_directive << ": Intercept mode requires its own interception port. It cannot be shared with other modes.");
c7b1dd5d 3517 self_destruct();
ba724607 3518 return;
c7b1dd5d 3519 }
6a25a046 3520 s->flags.natIntercept = true;
c7b1dd5d
AJ
3521 Ip::Interceptor.StartInterception();
3522 /* Log information regarding the port modes under interception. */
3523 debugs(3, DBG_IMPORTANT, "Starting Authentication on port " << s->s);
3524 debugs(3, DBG_IMPORTANT, "Disabling Authentication on port " << s->s << " (interception enabled)");
c7b1dd5d 3525 } else if (strcmp(token, "tproxy") == 0) {
151ba0d4 3526 if (s->flags.natIntercept || s->flags.accelSurrogate) {
3cc0f4e7 3527 debugs(3,DBG_CRITICAL, "FATAL: " << cfg_directive << ": TPROXY option requires its own interception port. It cannot be shared with other modes.");
c7b1dd5d 3528 self_destruct();
ba724607 3529 return;
c7b1dd5d 3530 }
6a25a046 3531 s->flags.tproxyIntercept = true;
c7b1dd5d
AJ
3532 Ip::Interceptor.StartTransparency();
3533 /* Log information regarding the port modes under transparency. */
0d901ef4 3534 debugs(3, DBG_IMPORTANT, "Disabling Authentication on port " << s->s << " (TPROXY enabled)");
c7b1dd5d 3535
151ba0d4 3536 if (s->flags.proxySurrogate) {
d3d92daa 3537 debugs(3, DBG_IMPORTANT, "Disabling TPROXY Spoofing on port " << s->s << " (require-proxy-header enabled)");
151ba0d4
AJ
3538 }
3539
c7b1dd5d 3540 if (!Ip::Interceptor.ProbeForTproxy(s->s)) {
3cc0f4e7 3541 debugs(3, DBG_CRITICAL, "FATAL: " << cfg_directive << ": TPROXY support in the system does not work.");
c7b1dd5d 3542 self_destruct();
ba724607 3543 return;
c7b1dd5d
AJ
3544 }
3545
d3d92daa 3546 } else if (strcmp(token, "require-proxy-header") == 0) {
00d0ce87 3547 s->flags.proxySurrogate = true;
13edf4cc
AJ
3548 if (s->flags.tproxyIntercept) {
3549 // receiving is still permitted, so we do not unset the TPROXY flag
3550 // spoofing access control override takes care of the spoof disable later
3551 debugs(3, DBG_IMPORTANT, "Disabling TPROXY Spoofing on port " << s->s << " (require-proxy-header enabled)");
3552 }
00d0ce87 3553
c7b1dd5d 3554 } else if (strncmp(token, "defaultsite=", 12) == 0) {
6a25a046 3555 if (!s->flags.accelSurrogate) {
3cc0f4e7 3556 debugs(3, DBG_CRITICAL, "FATAL: " << cfg_directive << ": defaultsite option requires Acceleration mode flag.");
c7b1dd5d 3557 self_destruct();
ba724607 3558 return;
c7b1dd5d 3559 }
62e76326 3560 safe_free(s->defaultsite);
3561 s->defaultsite = xstrdup(token + 12);
3f38a55e 3562 } else if (strcmp(token, "vhost") == 0) {
6a25a046 3563 if (!s->flags.accelSurrogate) {
3cc0f4e7 3564 debugs(3, DBG_CRITICAL, "WARNING: " << cfg_directive << ": vhost option is deprecated. Use 'accel' mode flag instead.");
cf673853 3565 }
6a25a046 3566 s->flags.accelSurrogate = true;
86ab7a90 3567 s->vhost = true;
cf673853 3568 } else if (strcmp(token, "no-vhost") == 0) {
6a25a046 3569 if (!s->flags.accelSurrogate) {
3cc0f4e7 3570 debugs(3, DBG_IMPORTANT, "ERROR: " << cfg_directive << ": no-vhost option requires Acceleration mode flag.");
c7b1dd5d 3571 }
86ab7a90 3572 s->vhost = false;
3f38a55e 3573 } else if (strcmp(token, "vport") == 0) {
6a25a046 3574 if (!s->flags.accelSurrogate) {
3cc0f4e7 3575 debugs(3, DBG_CRITICAL, "FATAL: " << cfg_directive << ": vport option requires Acceleration mode flag.");
c7b1dd5d 3576 self_destruct();
ba724607 3577 return;
c7b1dd5d 3578 }
62e76326 3579 s->vport = -1;
3f38a55e 3580 } else if (strncmp(token, "vport=", 6) == 0) {
6a25a046 3581 if (!s->flags.accelSurrogate) {
3cc0f4e7 3582 debugs(3, DBG_CRITICAL, "FATAL: " << cfg_directive << ": vport option requires Acceleration mode flag.");
c7b1dd5d 3583 self_destruct();
ba724607 3584 return;
c7b1dd5d 3585 }
0e656b69 3586 s->vport = xatos(token + 6);
3f38a55e 3587 } else if (strncmp(token, "protocol=", 9) == 0) {
6a25a046 3588 if (!s->flags.accelSurrogate) {
3cc0f4e7 3589 debugs(3, DBG_CRITICAL, "FATAL: " << cfg_directive << ": protocol option requires Acceleration mode flag.");
c7b1dd5d 3590 self_destruct();
ba724607 3591 return;
c7b1dd5d 3592 }
3cc0f4e7 3593 s->transport = parsePortProtocol(ToUpper(SBuf(token + 9)));
7f7bdd96 3594 } else if (strcmp(token, "allow-direct") == 0) {
6a25a046 3595 if (!s->flags.accelSurrogate) {
3cc0f4e7 3596 debugs(3, DBG_CRITICAL, "FATAL: " << cfg_directive << ": allow-direct option requires Acceleration mode flag.");
c7b1dd5d 3597 self_destruct();
ba724607 3598 return;
c7b1dd5d 3599 }
86ab7a90 3600 s->allow_direct = true;
90fa5816 3601 } else if (strcmp(token, "act-as-origin") == 0) {
6a25a046 3602 if (!s->flags.accelSurrogate) {
3cc0f4e7 3603 debugs(3, DBG_IMPORTANT, "ERROR: " << cfg_directive << ": act-as-origin option requires Acceleration mode flag.");
90fa5816 3604 } else
86ab7a90 3605 s->actAsOrigin = true;
432bc83c 3606 } else if (strcmp(token, "ignore-cc") == 0) {
626096be 3607#if !USE_HTTP_VIOLATIONS
6a25a046 3608 if (!s->flags.accelSurrogate) {
3cc0f4e7 3609 debugs(3, DBG_CRITICAL, "FATAL: " << cfg_directive << ": ignore-cc option requires Acceleration mode flag.");
838daf3f 3610 self_destruct();
ba724607 3611 return;
838daf3f 3612 }
432bc83c 3613#endif
86ab7a90 3614 s->ignore_cc = true;
c7b1dd5d
AJ
3615 } else if (strncmp(token, "name=", 5) == 0) {
3616 safe_free(s->name);
3617 s->name = xstrdup(token + 5);
d67acb4e
AJ
3618 } else if (strcmp(token, "no-connection-auth") == 0) {
3619 s->connection_auth_disabled = true;
3620 } else if (strcmp(token, "connection-auth=off") == 0) {
26ac0430 3621 s->connection_auth_disabled = true;
d67acb4e 3622 } else if (strcmp(token, "connection-auth") == 0) {
26ac0430 3623 s->connection_auth_disabled = false;
d67acb4e 3624 } else if (strcmp(token, "connection-auth=on") == 0) {
26ac0430 3625 s->connection_auth_disabled = false;
5529ca8a 3626 } else if (strncmp(token, "disable-pmtu-discovery=", 23) == 0) {
a37d6070 3627 if (!strcmp(token + 23, "off"))
5529ca8a 3628 s->disable_pmtu_discovery = DISABLE_PMTU_OFF;
a37d6070 3629 else if (!strcmp(token + 23, "transparent"))
5529ca8a 3630 s->disable_pmtu_discovery = DISABLE_PMTU_TRANSPARENT;
a37d6070 3631 else if (!strcmp(token + 23, "always"))
5529ca8a 3632 s->disable_pmtu_discovery = DISABLE_PMTU_ALWAYS;
ba724607 3633 else {
5529ca8a 3634 self_destruct();
ba724607
AJ
3635 return;
3636 }
cc192b50 3637 } else if (strcmp(token, "ipv4") == 0) {
4dd643d5 3638 if ( !s->s.setIPv4() ) {
3cc0f4e7 3639 debugs(3, DBG_CRITICAL, "FATAL: " << cfg_directive << ": IPv6 addresses cannot be used as IPv4-Only. " << s->s );
cc192b50 3640 self_destruct();
ba724607 3641 return;
cc192b50 3642 }
26ac0430 3643 } else if (strcmp(token, "tcpkeepalive") == 0) {
86ab7a90 3644 s->tcp_keepalive.enabled = true;
b2130d58 3645 } else if (strncmp(token, "tcpkeepalive=", 13) == 0) {
26ac0430 3646 char *t = token + 13;
86ab7a90 3647 s->tcp_keepalive.enabled = true;
e398d16e 3648 s->tcp_keepalive.idle = xatoui(t,',');
26ac0430
AJ
3649 t = strchr(t, ',');
3650 if (t) {
a38ec4b1 3651 ++t;
e398d16e 3652 s->tcp_keepalive.interval = xatoui(t,',');
26ac0430
AJ
3653 t = strchr(t, ',');
3654 }
3655 if (t) {
a38ec4b1 3656 ++t;
54a063a2 3657 s->tcp_keepalive.timeout = xatoui(t);
26ac0430 3658 }
cb4f4424 3659#if USE_OPENSSL
a37d6070 3660 } else if (strcmp(token, "sslBump") == 0) {
9a622f3e 3661 debugs(3, DBG_PARSE_NOTE(1), "WARNING: '" << token << "' is deprecated " <<
3cc0f4e7 3662 "in " << cfg_directive << ". Use 'ssl-bump' instead.");
c3d24490 3663 s->flags.tunnelSslBumping = true;
3a0c8eb5 3664 } else if (strcmp(token, "ssl-bump") == 0) {
c3d24490 3665 s->flags.tunnelSslBumping = true;
154dc884 3666 } else if (strncmp(token, "cert=", 5) == 0) {
9a622f3e 3667 s->secure.parse(token);
154dc884 3668 } else if (strncmp(token, "key=", 4) == 0) {
9a622f3e 3669 s->secure.parse(token);
154dc884 3670 } else if (strncmp(token, "version=", 8) == 0) {
d816f28d 3671 debugs(3, DBG_PARSE_NOTE(1), "WARNING: UPGRADE: '" << token << "' is deprecated " <<
1cc44095 3672 "in " << cfg_directive << ". Use 'options=' instead.");
9a622f3e 3673 s->secure.parse(token);
154dc884 3674 } else if (strncmp(token, "options=", 8) == 0) {
9a622f3e 3675 s->secure.parse(token);
154dc884 3676 } else if (strncmp(token, "cipher=", 7) == 0) {
9a622f3e 3677 s->secure.parse(token);
154dc884 3678 } else if (strncmp(token, "clientca=", 9) == 0) {
621f4299 3679 s->secure.parse(token);
154dc884 3680 } else if (strncmp(token, "cafile=", 7) == 0) {
d816f28d 3681 debugs(3, DBG_PARSE_NOTE(1), "WARNING: UPGRADE: '" << token << "' is deprecated " <<
86a84cc0 3682 "in " << cfg_directive << ". Use 'tls-cafile=' instead.");
9a622f3e 3683 s->secure.parse(token);
154dc884 3684 } else if (strncmp(token, "capath=", 7) == 0) {
9a622f3e 3685 s->secure.parse(token);
154dc884 3686 } else if (strncmp(token, "crlfile=", 8) == 0) {
9a622f3e 3687 s->secure.parse(token);
154dc884 3688 } else if (strncmp(token, "dhparams=", 9) == 0) {
54fbe371
PM
3689 debugs(3, DBG_PARSE_NOTE(DBG_IMPORTANT), "WARNING: '" << token << "' is deprecated " <<
3690 "in " << cfg_directive << ". Use 'tls-dh=' instead.");
474f076e 3691 s->secure.parse(token);
154dc884 3692 } else if (strncmp(token, "sslflags=", 9) == 0) {
8b253b83 3693 // NP: deprecation warnings output by secure.parse() when relevant
9a622f3e 3694 s->secure.parse(token+3);
154dc884 3695 } else if (strncmp(token, "sslcontext=", 11) == 0) {
cf487124
AJ
3696 // NP: deprecation warnings output by secure.parse() when relevant
3697 s->secure.parse(token+3);
3698 } else if (strncmp(token, "generate-host-certificates", 26) == 0) {
3699 s->secure.parse(token);
154dc884 3700#endif
cf487124
AJ
3701 } else if (strncmp(token, "dynamic_cert_mem_cache_size=", 28) == 0) {
3702 s->secure.parse(token);
9a622f3e
AJ
3703 } else if (strncmp(token, "tls-", 4) == 0) {
3704 s->secure.parse(token+4);
3cc0f4e7 3705 } else if (strcmp(token, "ftp-track-dirs") == 0) {
27c841f6 3706 s->ftp_track_dirs = true;
7d17a6a6
EB
3707 } else if (strcmp(token, "worker-queues") == 0) {
3708#if !defined(SO_REUSEADDR)
3709#error missing system #include that #defines SO_* constants
3710#endif
3711#if !defined(SO_REUSEPORT)
3712 throw TexcHere(ToSBuf(cfg_directive, ' ', token, " option requires building Squid where SO_REUSEPORT is supported by the TCP stack"));
3713#endif
3714 s->workerQueues = true;
3f38a55e 3715 } else {
3cc0f4e7 3716 debugs(3, DBG_CRITICAL, "FATAL: Unknown " << cfg_directive << " option '" << token << "'.");
62e76326 3717 self_destruct();
3f38a55e 3718 }
3719}
3720
3f38a55e 3721void
3722add_http_port(char *portspec)
3723{
fa720bfb 3724 AnyP::PortCfgPointer s = new AnyP::PortCfg();
3cc0f4e7 3725 s->transport = parsePortProtocol(SBuf("HTTP"));
859741ed 3726 parsePortSpecification(s, portspec);
65d448bc 3727 // we may need to merge better if the above returns a list with clones
aee3523a 3728 assert(s->next == nullptr);
fa720bfb
AJ
3729 s->next = HttpPortList;
3730 HttpPortList = s;
3f38a55e 3731}
3732
3733static void
fa720bfb 3734parsePortCfg(AnyP::PortCfgPointer *head, const char *optionName)
d3388e1f 3735{
3cc0f4e7 3736 SBuf protoName;
d3388e1f 3737 if (strcmp(optionName, "http_port") == 0 ||
b5944cff 3738 strcmp(optionName, "ascii_port") == 0)
3cc0f4e7 3739 protoName = "HTTP";
d3388e1f 3740 else if (strcmp(optionName, "https_port") == 0)
3cc0f4e7 3741 protoName = "HTTPS";
434a79b0 3742 else if (strcmp(optionName, "ftp_port") == 0)
3cc0f4e7
AR
3743 protoName = "FTP";
3744 if (protoName.isEmpty()) {
d3388e1f
AR
3745 self_destruct();
3746 return;
3747 }
3748
2eceb328 3749 char *token = ConfigParser::NextToken();
62e76326 3750
9eeb8e4b 3751 if (!token) {
62e76326 3752 self_destruct();
9eeb8e4b 3753 return;
3754 }
62e76326 3755
fa720bfb 3756 AnyP::PortCfgPointer s = new AnyP::PortCfg();
3cc0f4e7 3757 s->transport = parsePortProtocol(protoName); // default; protocol=... overwrites
859741ed 3758 parsePortSpecification(s, token);
62e76326 3759
3f38a55e 3760 /* parse options ... */
2eceb328 3761 while ((token = ConfigParser::NextToken())) {
65d448bc 3762 parse_port_option(s, token);
3f38a55e 3763 }
62e76326 3764
621f4299 3765 s->secure.syncCaFiles();
86a84cc0 3766
6507233b 3767 if (s->transport.protocol == AnyP::PROTO_HTTPS) {
49d8cc6f 3768 s->secure.encryptTransport = true;
151ba0d4 3769#if USE_OPENSSL
38450a50 3770 /* ssl-bump on https_port configuration requires either tproxy or intercept, and vice versa */
c3d24490 3771 const bool hijacked = s->flags.isIntercepted();
6a25a046 3772 if (s->flags.tunnelSslBumping && !hijacked) {
38450a50 3773 debugs(3, DBG_CRITICAL, "FATAL: ssl-bump on https_port requires tproxy/intercept which is missing.");
03f00a11 3774 self_destruct();
ba724607 3775 return;
03f00a11 3776 }
6a25a046 3777 if (hijacked && !s->flags.tunnelSslBumping) {
38450a50 3778 debugs(3, DBG_CRITICAL, "FATAL: tproxy/intercept on https_port requires ssl-bump which is missing.");
03f00a11 3779 self_destruct();
ba724607 3780 return;
03f00a11 3781 }
03f00a11 3782#endif
a07a354f 3783 if (s->flags.proxySurrogate) {
d3d92daa 3784 debugs(3,DBG_CRITICAL, "FATAL: https_port: require-proxy-header option is not supported on HTTPS ports.");
151ba0d4 3785 self_destruct();
ba724607 3786 return;
151ba0d4 3787 }
3cc0f4e7 3788 } else if (protoName.cmp("FTP") == 0) {
434a79b0
DK
3789 /* ftp_port does not support ssl-bump */
3790 if (s->flags.tunnelSslBumping) {
3791 debugs(3, DBG_CRITICAL, "FATAL: ssl-bump is not supported for ftp_port.");
3792 self_destruct();
ba724607 3793 return;
434a79b0 3794 }
a07a354f
AJ
3795 if (s->flags.proxySurrogate) {
3796 // Passive FTP data channel does not work without deep protocol inspection in the frontend.
3797 debugs(3,DBG_CRITICAL, "FATAL: require-proxy-header option is not supported on ftp_port.");
3798 self_destruct();
ba724607 3799 return;
a07a354f 3800 }
151ba0d4 3801 }
03f00a11 3802
79110c18
AJ
3803 if (s->secure.encryptTransport) {
3804 if (s->secure.certs.empty()) {
3805 debugs(3, DBG_CRITICAL, "FATAL: " << AnyP::UriScheme(s->transport.protocol) << "_port requires a cert= parameter");
3806 self_destruct();
3807 return;
3808 }
151644b7 3809 s->secure.parseOptions();
79110c18
AJ
3810 }
3811
3812 // *_port line should now be fully valid so we can clone it if necessary
4dd643d5 3813 if (Ip::EnableIpv6&IPV6_SPECIAL_SPLITSTACK && s->s.isAnyAddr()) {
7801cce9 3814 s->next = s->ipV4clone();
7e07ced1 3815 }
7e07ced1 3816
aee3523a 3817 while (*head != nullptr)
fa720bfb 3818 head = &((*head)->next);
62e76326 3819
fa720bfb 3820 *head = s;
3f38a55e 3821}
3822
3823static void
fa720bfb 3824dump_generic_port(StoreEntry * e, const char *n, const AnyP::PortCfgPointer &s)
3f38a55e 3825{
cc192b50 3826 char buf[MAX_IPSTRLEN];
3827
3828 storeAppendPrintf(e, "%s %s",
62e76326 3829 n,
4dd643d5 3830 s->s.toUrl(buf,MAX_IPSTRLEN));
62e76326 3831
df1b20e4 3832 // MODES and specific sub-options.
6a25a046 3833 if (s->flags.natIntercept)
2ad20b4f 3834 storeAppendPrintf(e, " intercept");
62e76326 3835
6a25a046 3836 else if (s->flags.tproxyIntercept)
c7b1dd5d
AJ
3837 storeAppendPrintf(e, " tproxy");
3838
00d0ce87 3839 else if (s->flags.proxySurrogate)
d3d92daa 3840 storeAppendPrintf(e, " require-proxy-header");
00d0ce87 3841
6a25a046 3842 else if (s->flags.accelSurrogate) {
c7b1dd5d
AJ
3843 storeAppendPrintf(e, " accel");
3844
df1b20e4
AJ
3845 if (s->vhost)
3846 storeAppendPrintf(e, " vhost");
62e76326 3847
df1b20e4
AJ
3848 if (s->vport < 0)
3849 storeAppendPrintf(e, " vport");
3850 else if (s->vport > 0)
3851 storeAppendPrintf(e, " vport=%d", s->vport);
5529ca8a 3852
df1b20e4
AJ
3853 if (s->defaultsite)
3854 storeAppendPrintf(e, " defaultsite=%s", s->defaultsite);
3855
eb6ac808
AJ
3856 // TODO: compare against prefix of 'n' instead of assuming http_port
3857 if (s->transport.protocol != AnyP::PROTO_HTTP)
d31d59d8 3858 storeAppendPrintf(e, " protocol=%s", AnyP::ProtocolType_str[s->transport.protocol]);
df1b20e4
AJ
3859
3860 if (s->allow_direct)
3861 storeAppendPrintf(e, " allow-direct");
3862
3863 if (s->ignore_cc)
3864 storeAppendPrintf(e, " ignore-cc");
3865
3866 }
3867
3868 // Generic independent options
3869
3870 if (s->name)
3871 storeAppendPrintf(e, " name=%s", s->name);
3872
33e701f3 3873#if USE_HTTP_VIOLATIONS
6a25a046 3874 if (!s->flags.accelSurrogate && s->ignore_cc)
df1b20e4
AJ
3875 storeAppendPrintf(e, " ignore-cc");
3876#endif
c7b1dd5d 3877
d67acb4e
AJ
3878 if (s->connection_auth_disabled)
3879 storeAppendPrintf(e, " connection-auth=off");
3880 else
3881 storeAppendPrintf(e, " connection-auth=on");
3882
5529ca8a 3883 if (s->disable_pmtu_discovery != DISABLE_PMTU_OFF) {
3884 const char *pmtu;
3885
3886 if (s->disable_pmtu_discovery == DISABLE_PMTU_ALWAYS)
3887 pmtu = "always";
3888 else
3889 pmtu = "transparent";
3890
3891 storeAppendPrintf(e, " disable-pmtu-discovery=%s", pmtu);
3892 }
b2130d58 3893
4dd643d5 3894 if (s->s.isAnyAddr() && !s->s.isIPv6())
df1b20e4
AJ
3895 storeAppendPrintf(e, " ipv4");
3896
b2130d58 3897 if (s->tcp_keepalive.enabled) {
26ac0430 3898 if (s->tcp_keepalive.idle || s->tcp_keepalive.interval || s->tcp_keepalive.timeout) {
68924b6d 3899 storeAppendPrintf(e, " tcpkeepalive=%d,%d,%d", s->tcp_keepalive.idle, s->tcp_keepalive.interval, s->tcp_keepalive.timeout);
26ac0430 3900 } else {
68924b6d 3901 storeAppendPrintf(e, " tcpkeepalive");
26ac0430 3902 }
b2130d58 3903 }
154dc884 3904
cb4f4424 3905#if USE_OPENSSL
6a25a046 3906 if (s->flags.tunnelSslBumping)
df1b20e4 3907 storeAppendPrintf(e, " ssl-bump");
9a622f3e 3908#endif
c7b1dd5d 3909
90153ed6
EB
3910 PackableStream os(*e);
3911 s->secure.dumpCfg(os, "tls-");
3f38a55e 3912}
62e76326 3913
3f38a55e 3914static void
fa720bfb 3915dump_PortCfg(StoreEntry * e, const char *n, const AnyP::PortCfgPointer &s)
3f38a55e 3916{
aee3523a 3917 for (AnyP::PortCfgPointer p = s; p != nullptr; p = p->next) {
fa720bfb 3918 dump_generic_port(e, n, p);
62e76326 3919 storeAppendPrintf(e, "\n");
3f38a55e 3920 }
3921}
3922
f150dd4b 3923void
3924configFreeMemory(void)
3925{
23ff6968 3926 free_all();
a8c7a110 3927 Dns::ResolveClientAddressesAsap = false;
900daee3 3928 Config.ssl_client.sslContext.reset();
cb4f4424 3929#if USE_OPENSSL
866be11c 3930 Ssl::unloadSquidUntrusted();
29bf4910 3931#endif
f150dd4b 3932}
f0b19334 3933
94439e4e 3934void
f0b19334 3935requirePathnameExists(const char *name, const char *path)
3936{
62e76326 3937
f0b19334 3938 struct stat sb;
08577e16 3939 char pathbuf[BUFSIZ];
aee3523a 3940 assert(path != nullptr);
62e76326 3941
0b796acd 3942 if (Config.chroot_dir && (geteuid() == 0)) {
62e76326 3943 snprintf(pathbuf, BUFSIZ, "%s/%s", Config.chroot_dir, path);
3944 path = pathbuf;
08577e16 3945 }
62e76326 3946
a572d8be 3947 if (stat(path, &sb) < 0) {
b69e9ffa
AJ
3948 int xerrno = errno;
3949 debugs(0, DBG_CRITICAL, (opt_parse_cfg_only?"FATAL: ":"ERROR: ") << name << " " << path << ": " << xstrerr(xerrno));
a32a190b
AJ
3950 // keep going to find more issues if we are only checking the config file with "-k parse"
3951 if (opt_parse_cfg_only)
3952 return;
3953 // this is fatal if it is found during startup or reconfigure
a572d8be 3954 if (opt_send_signal == -1 || opt_send_signal == SIGHUP)
b69e9ffa 3955 fatalf("%s %s: %s", name, path, xstrerr(xerrno));
a572d8be 3956 }
f0b19334 3957}
d860a1aa 3958
450e0c10 3959#include "AccessLogEntry.h"
7684c4b1 3960
fb0c2f17
NH
3961/**
3962 * We support several access_log configuration styles:
3963 *
3964 * #1: Deprecated ancient style without an explicit logging module:
3965 * access_log /var/log/access.log
3966 *
3967 * #2: The "none" logging module (i.e., no logging [of matching transactions]):
3968 * access_log none [acl ...]
3969 *
3970 * #3: Configurable logging module without named options:
3971 * Logformat or the first ACL name, whichever comes first, may not contain '='.
3972 * If no explicit logformat name is given, the first ACL name, if any,
3973 * should not be an existing logformat name or it will be treated as such.
3974 * access_log module:place [logformat_name] [acl ...]
3975 *
3976 * #4: Configurable logging module with name=value options such as logformat=x:
3977 * The first ACL name may not contain '='.
39d7714a
AR
3978 * Without any optional parts, directives using this style are indistinguishable
3979 * from directives using style #1 until we start requiring the "module:" prefix.
fb0c2f17
NH
3980 * access_log module:place [option ...] [acl ...]
3981 *
3982 */
7684c4b1 3983static void
87ddff6e 3984parse_access_log(CustomLog ** logs)
7684c4b1 3985{
2eceb328 3986 const char *filename = ConfigParser::NextToken();
fb0c2f17 3987 if (!filename) {
7684c4b1 3988 self_destruct();
9eeb8e4b 3989 return;
3990 }
7684c4b1 3991
39d7714a 3992 const auto cl = new CustomLog();
1847642b
AJ
3993
3994 cl->filename = xstrdup(filename);
1847642b 3995
7684c4b1 3996 if (strcmp(filename, "none") == 0) {
20efa1c2 3997 cl->type = Log::Format::CLF_NONE;
6f58d7d7 3998 aclParseAclList(LegacyParser, &cl->aclList, filename);
6375a714
AJ
3999 while (*logs)
4000 logs = &(*logs)->next;
4001 *logs = cl;
4002 return;
7684c4b1 4003 }
4004
bde7a8ce 4005 const char *token = ConfigParser::PeekAtToken();
39d7714a
AR
4006 if (token && !strchr(token, '=')) { // style #3
4007 // TODO: Deprecate this style to avoid this dangerous guessing.
4008 if (Log::TheConfig.knownFormat(token)) {
4009 cl->setLogformat(token);
4010 (void)ConfigParser::NextToken(); // consume the token used above
4011 } else {
4012 // assume there is no explicit logformat name and use the default
4013 cl->setLogformat("squid");
4014 }
4015 } else { // style #1 or style #4
4016 // TODO: Drop deprecated style #1 support. We already warn about it, and
4017 // its exceptional treatment makes detecting "module" typos impractical!
4018 cl->parseOptions(LegacyParser, "squid");
fb0c2f17 4019 }
39d7714a 4020 assert(cl->type); // setLogformat() was called
7684c4b1 4021
0a003ce5 4022 aclParseAclList(LegacyParser, &cl->aclList, cl->filename);
fb0c2f17
NH
4023
4024 while (*logs)
4025 logs = &(*logs)->next;
4026
4027 *logs = cl;
4028}
4029
d64bef4c 4030static int
87ddff6e 4031check_null_access_log(CustomLog *customlog_definitions)
d64bef4c 4032{
aee3523a 4033 return customlog_definitions == nullptr;
d64bef4c 4034}
4035
7684c4b1 4036static void
87ddff6e 4037dump_access_log(StoreEntry * entry, const char *name, CustomLog * logs)
7684c4b1 4038{
39d7714a
AR
4039 assert(entry);
4040 for (auto log = logs; log; log = log->next) {
4041 {
4042 PackableStream os(*entry);
4043 os << name; // directive name
4044 os << ' ' << log->filename; // including "none"
4045 log->dumpOptions(os);
7684c4b1 4046 }
4047
4048 if (log->aclList)
4049 dump_acl_list(entry, log->aclList);
4050
4051 storeAppendPrintf(entry, "\n");
4052 }
4053}
4054
7684c4b1 4055static void
87ddff6e 4056free_access_log(CustomLog ** definitions)
7684c4b1 4057{
4058 while (*definitions) {
87ddff6e 4059 CustomLog *log = *definitions;
7684c4b1 4060 *definitions = log->next;
39d7714a 4061 delete log;
7684c4b1 4062 }
4063}
3a69ddf3 4064
ab7e1298 4065#if HAVE_CPU_AFFINITY /* until somebody else needs this general code */
96c2bb61
AR
4066/// parses list of integers form name=N1,N2,N3,...
4067static bool
c8ea3cc0 4068parseNamedIntList(const char *data, const String &name, std::vector<int> &list)
96c2bb61
AR
4069{
4070 if (data && (strncmp(data, name.rawBuf(), name.size()) == 0)) {
4071 data += name.size();
4072 if (*data == '=') {
4073 while (true) {
4074 ++data;
4075 int value = 0;
4076 if (!StringToInt(data, value, &data, 10))
4077 break;
4078 list.push_back(value);
4079 if (*data == '\0' || *data != ',')
4080 break;
4081 }
4082 }
4083 }
cff99cec 4084 return data && *data == '\0';
96c2bb61 4085}
ab7e1298 4086#endif
96c2bb61
AR
4087
4088static void
212af65c
A
4089parse_CpuAffinityMap(CpuAffinityMap **const cpuAffinityMap)
4090{
96c2bb61 4091#if !HAVE_CPU_AFFINITY
2081cefa 4092 (void)cpuAffinityMap;
96c2bb61
AR
4093 debugs(3, DBG_CRITICAL, "FATAL: Squid built with no CPU affinity " <<
4094 "support, do not set 'cpu_affinity_map'");
4095 self_destruct();
96c2bb61 4096
ba724607 4097#else /* HAVE_CPU_AFFINITY */
96c2bb61
AR
4098 if (!*cpuAffinityMap)
4099 *cpuAffinityMap = new CpuAffinityMap;
4100
2eceb328
CT
4101 const char *const pToken = ConfigParser::NextToken();
4102 const char *const cToken = ConfigParser::NextToken();
c8ea3cc0 4103 std::vector<int> processes, cores;
96c2bb61
AR
4104 if (!parseNamedIntList(pToken, "process_numbers", processes)) {
4105 debugs(3, DBG_CRITICAL, "FATAL: bad 'process_numbers' parameter " <<
4106 "in 'cpu_affinity_map'");
4107 self_destruct();
4108 } else if (!parseNamedIntList(cToken, "cores", cores)) {
4109 debugs(3, DBG_CRITICAL, "FATAL: bad 'cores' parameter in " <<
4110 "'cpu_affinity_map'");
4111 self_destruct();
4112 } else if (!(*cpuAffinityMap)->add(processes, cores)) {
4113 debugs(3, DBG_CRITICAL, "FATAL: bad 'cpu_affinity_map'; " <<
4114 "process_numbers and cores lists differ in length or " <<
4115 "contain numbers <= 0");
4116 self_destruct();
4117 }
ba724607 4118#endif
96c2bb61
AR
4119}
4120
4121static void
212af65c
A
4122dump_CpuAffinityMap(StoreEntry *const entry, const char *const name, const CpuAffinityMap *const cpuAffinityMap)
4123{
96c2bb61
AR
4124 if (cpuAffinityMap) {
4125 storeAppendPrintf(entry, "%s process_numbers=", name);
4126 for (size_t i = 0; i < cpuAffinityMap->processes().size(); ++i) {
4127 storeAppendPrintf(entry, "%s%i", (i ? "," : ""),
4128 cpuAffinityMap->processes()[i]);
4129 }
4130 storeAppendPrintf(entry, " cores=");
eaed60cc 4131 for (size_t i = 0; i < cpuAffinityMap->cores().size(); ++i) {
96c2bb61
AR
4132 storeAppendPrintf(entry, "%s%i", (i ? "," : ""),
4133 cpuAffinityMap->cores()[i]);
4134 }
4135 storeAppendPrintf(entry, "\n");
4136 }
4137}
4138
4139static void
212af65c
A
4140free_CpuAffinityMap(CpuAffinityMap **const cpuAffinityMap)
4141{
96c2bb61 4142 delete *cpuAffinityMap;
aee3523a 4143 *cpuAffinityMap = nullptr;
96c2bb61
AR
4144}
4145
62c7f90e
AR
4146#if USE_ADAPTATION
4147
4148static void
4149parse_adaptation_service_set_type()
4150{
4151 Adaptation::Config::ParseServiceSet();
4152}
4153
a22e6cd3
AR
4154static void
4155parse_adaptation_service_chain_type()
4156{
4157 Adaptation::Config::ParseServiceChain();
4158}
4159
62c7f90e
AR
4160static void
4161parse_adaptation_access_type()
4162{
4163 Adaptation::Config::ParseAccess(LegacyParser);
4164}
62c7f90e
AR
4165#endif /* USE_ADAPTATION */
4166
3a69ddf3 4167#if ICAP_CLIENT
4168
4169static void
26cc52cb 4170parse_icap_service_type(Adaptation::Icap::Config * cfg)
3a69ddf3 4171{
462e63d3 4172 cfg->parseService();
3a69ddf3 4173}
4174
4175static void
26cc52cb 4176free_icap_service_type(Adaptation::Icap::Config * cfg)
3a69ddf3 4177{
462e63d3 4178 cfg->freeService();
3a69ddf3 4179}
4180
4181static void
26cc52cb 4182dump_icap_service_type(StoreEntry * entry, const char *name, const Adaptation::Icap::Config &cfg)
3a69ddf3 4183{
462e63d3 4184 cfg.dumpService(entry, name);
3a69ddf3 4185}
4186
4187static void
c939dc70 4188parse_icap_class_type()
3a69ddf3 4189{
61beade2 4190 debugs(93, DBG_CRITICAL, "WARNING: 'icap_class' is deprecated. " <<
26ac0430 4191 "Use 'adaptation_service_set' instead");
62c7f90e 4192 Adaptation::Config::ParseServiceSet();
3a69ddf3 4193}
4194
3a69ddf3 4195static void
c939dc70 4196parse_icap_access_type()
3a69ddf3 4197{
61beade2 4198 debugs(93, DBG_CRITICAL, "WARNING: 'icap_access' is deprecated. " <<
26ac0430 4199 "Use 'adaptation_access' instead");
21a26d31 4200 Adaptation::Config::ParseAccess(LegacyParser);
3a69ddf3 4201}
4202
3a69ddf3 4203#endif
21a26d31 4204
21a26d31
AR
4205#if USE_ECAP
4206
4207static void
574b508c 4208parse_ecap_service_type(Adaptation::Ecap::Config * cfg)
21a26d31
AR
4209{
4210 cfg->parseService();
4211}
4212
4213static void
574b508c 4214free_ecap_service_type(Adaptation::Ecap::Config * cfg)
21a26d31
AR
4215{
4216 cfg->freeService();
4217}
4218
4219static void
574b508c 4220dump_ecap_service_type(StoreEntry * entry, const char *name, const Adaptation::Ecap::Config &cfg)
21a26d31
AR
4221{
4222 cfg.dumpService(entry, name);
4223}
4224
4225#endif /* USE_ECAP */
8277060a
CT
4226
4227#if ICAP_CLIENT
4228static void parse_icap_service_failure_limit(Adaptation::Icap::Config *cfg)
4229{
4230 char *token;
8277060a
CT
4231 cfg->service_failure_limit = GetInteger();
4232
aee3523a 4233 if ((token = ConfigParser::NextToken()) == nullptr)
8277060a
CT
4234 return;
4235
4236 if (strcmp(token,"in") != 0) {
fa84c01d 4237 debugs(3, DBG_CRITICAL, "expecting 'in' on'" << config_input_line << "'");
8277060a 4238 self_destruct();
ba724607 4239 return;
8277060a
CT
4240 }
4241
8da861a5 4242 parse_time_t(&cfg->oldest_service_failure);
8277060a
CT
4243}
4244
4245static void dump_icap_service_failure_limit(StoreEntry *entry, const char *name, const Adaptation::Icap::Config &cfg)
4246{
4247 storeAppendPrintf(entry, "%s %d", name, cfg.service_failure_limit);
4248 if (cfg.oldest_service_failure > 0) {
4249 storeAppendPrintf(entry, " in %d seconds", (int)cfg.oldest_service_failure);
4250 }
4251 storeAppendPrintf(entry, "\n");
4252}
4253
4254static void free_icap_service_failure_limit(Adaptation::Icap::Config *cfg)
4255{
4256 cfg->oldest_service_failure = 0;
4257 cfg->service_failure_limit = 0;
4258}
aebe6888 4259#endif
8277060a 4260
cb4f4424 4261#if USE_OPENSSL
fb2178bb
CT
4262static void parse_sslproxy_cert_adapt(sslproxy_cert_adapt **cert_adapt)
4263{
0a4c4478
AJ
4264 auto *al = ConfigParser::NextToken();
4265 if (!al) {
fb2178bb
CT
4266 self_destruct();
4267 return;
4268 }
87f237a9 4269
fb2178bb
CT
4270 const char *param;
4271 if ( char *s = strchr(al, '{')) {
4272 *s = '\0'; // terminate the al string
a38ec4b1 4273 ++s;
fb2178bb
CT
4274 param = s;
4275 s = strchr(s, '}');
4276 if (!s) {
4277 self_destruct();
4278 return;
4279 }
4280 *s = '\0';
87f237a9 4281 } else
aee3523a 4282 param = nullptr;
fb2178bb 4283
0a4c4478 4284 std::unique_ptr<sslproxy_cert_adapt> ca(new sslproxy_cert_adapt);
fb2178bb
CT
4285 if (strcmp(al, Ssl::CertAdaptAlgorithmStr[Ssl::algSetValidAfter]) == 0) {
4286 ca->alg = Ssl::algSetValidAfter;
b8889258 4287 ca->param = xstrdup("on");
87f237a9 4288 } else if (strcmp(al, Ssl::CertAdaptAlgorithmStr[Ssl::algSetValidBefore]) == 0) {
fb2178bb 4289 ca->alg = Ssl::algSetValidBefore;
b8889258 4290 ca->param = xstrdup("on");
87f237a9 4291 } else if (strcmp(al, Ssl::CertAdaptAlgorithmStr[Ssl::algSetCommonName]) == 0) {
fb2178bb
CT
4292 ca->alg = Ssl::algSetCommonName;
4293 if (param) {
5367d845
CT
4294 if (strlen(param) > 64) {
4295 debugs(3, DBG_CRITICAL, "FATAL: sslproxy_cert_adapt: setCommonName{" <<param << "} : using common name longer than 64 bytes is not supported");
4296 self_destruct();
4297 return;
4298 }
b8889258 4299 ca->param = xstrdup(param);
fb2178bb
CT
4300 }
4301 } else {
4302 debugs(3, DBG_CRITICAL, "FATAL: sslproxy_cert_adapt: unknown cert adaptation algorithm: " << al);
4303 self_destruct();
4304 return;
4305 }
4306
6f58d7d7 4307 aclParseAclList(LegacyParser, &ca->aclList, al);
fb2178bb 4308
87f237a9 4309 while (*cert_adapt)
fb2178bb
CT
4310 cert_adapt = &(*cert_adapt)->next;
4311
0a4c4478 4312 *cert_adapt = ca.release();
fb2178bb
CT
4313}
4314
4315static void dump_sslproxy_cert_adapt(StoreEntry *entry, const char *name, sslproxy_cert_adapt *cert_adapt)
4316{
0a4c4478 4317 for (const auto *ca = cert_adapt; ca; ca = ca->next) {
fb2178bb
CT
4318 storeAppendPrintf(entry, "%s ", name);
4319 storeAppendPrintf(entry, "%s{%s} ", Ssl::sslCertAdaptAlgoritm(ca->alg), ca->param);
4320 if (ca->aclList)
4321 dump_acl_list(entry, ca->aclList);
4322 storeAppendPrintf(entry, "\n");
4323 }
4324}
4325
4326static void free_sslproxy_cert_adapt(sslproxy_cert_adapt **cert_adapt)
4327{
0a4c4478
AJ
4328 delete *cert_adapt;
4329 *cert_adapt = nullptr;
fb2178bb 4330}
aebe6888
CT
4331
4332static void parse_sslproxy_cert_sign(sslproxy_cert_sign **cert_sign)
4333{
0a4c4478
AJ
4334 const auto al = ConfigParser::NextToken();
4335 if (!al) {
aebe6888
CT
4336 self_destruct();
4337 return;
4338 }
4339
0a4c4478 4340 std::unique_ptr<sslproxy_cert_sign> cs(new sslproxy_cert_sign);
aebe6888
CT
4341 if (strcmp(al, Ssl::CertSignAlgorithmStr[Ssl::algSignTrusted]) == 0)
4342 cs->alg = Ssl::algSignTrusted;
4343 else if (strcmp(al, Ssl::CertSignAlgorithmStr[Ssl::algSignUntrusted]) == 0)
4344 cs->alg = Ssl::algSignUntrusted;
4345 else if (strcmp(al, Ssl::CertSignAlgorithmStr[Ssl::algSignSelf]) == 0)
4346 cs->alg = Ssl::algSignSelf;
4347 else {
4348 debugs(3, DBG_CRITICAL, "FATAL: sslproxy_cert_sign: unknown cert signing algorithm: " << al);
4349 self_destruct();
4350 return;
4351 }
4352
6f58d7d7 4353 aclParseAclList(LegacyParser, &cs->aclList, al);
aebe6888 4354
87f237a9 4355 while (*cert_sign)
aebe6888
CT
4356 cert_sign = &(*cert_sign)->next;
4357
0a4c4478 4358 *cert_sign = cs.release();
aebe6888
CT
4359}
4360
4361static void dump_sslproxy_cert_sign(StoreEntry *entry, const char *name, sslproxy_cert_sign *cert_sign)
4362{
0a4c4478 4363 for (const auto *cs = cert_sign; cs; cs = cs->next) {
aebe6888
CT
4364 storeAppendPrintf(entry, "%s ", name);
4365 storeAppendPrintf(entry, "%s ", Ssl::certSignAlgorithm(cs->alg));
4366 if (cs->aclList)
4367 dump_acl_list(entry, cs->aclList);
4368 storeAppendPrintf(entry, "\n");
4369 }
4370}
4371
4372static void free_sslproxy_cert_sign(sslproxy_cert_sign **cert_sign)
4373{
0a4c4478
AJ
4374 delete *cert_sign;
4375 *cert_sign = nullptr;
aebe6888 4376}
fb2178bb 4377
38450a50
CT
4378class sslBumpCfgRr: public ::RegisteredRunner
4379{
4380public:
4381 static Ssl::BumpMode lastDeprecatedRule;
4382 /* RegisteredRunner API */
337b9aa4 4383 void finalizeConfig() override;
38450a50
CT
4384};
4385
4386Ssl::BumpMode sslBumpCfgRr::lastDeprecatedRule = Ssl::bumpEnd;
4387
230d4410 4388DefineRunnerRegistrator(sslBumpCfgRr);
38450a50 4389
21b7990f
AR
4390void
4391sslBumpCfgRr::finalizeConfig()
38450a50
CT
4392{
4393 if (lastDeprecatedRule != Ssl::bumpEnd) {
4394 assert( lastDeprecatedRule == Ssl::bumpClientFirst || lastDeprecatedRule == Ssl::bumpNone);
4395 static char buf[1024];
4396 if (lastDeprecatedRule == Ssl::bumpClientFirst) {
4397 strcpy(buf, "ssl_bump deny all");
4398 debugs(3, DBG_CRITICAL, "WARNING: auto-converting deprecated implicit "
4399 "\"ssl_bump deny all\" to \"ssl_bump none all\". New ssl_bump configurations "
4400 "must not use implicit rules. Update your ssl_bump rules.");
4401 } else {
4402 strcpy(buf, "ssl_bump allow all");
4403 debugs(3, DBG_CRITICAL, "SECURITY NOTICE: auto-converting deprecated implicit "
4404 "\"ssl_bump allow all\" to \"ssl_bump client-first all\" which is usually "
4405 "inferior to the newer server-first bumping mode. New ssl_bump"
4406 " configurations must not use implicit rules. Update your ssl_bump rules.");
4407 }
4408 parse_line(buf);
4409 }
4410}
4411
caf3666d
AR
4412static void parse_sslproxy_ssl_bump(acl_access **ssl_bump)
4413{
38450a50 4414 typedef const char *BumpCfgStyle;
aee3523a 4415 BumpCfgStyle bcsNone = nullptr;
38450a50
CT
4416 BumpCfgStyle bcsNew = "new client/server-first/none";
4417 BumpCfgStyle bcsOld = "deprecated allow/deny";
4418 static BumpCfgStyle bumpCfgStyleLast = bcsNone;
4419 BumpCfgStyle bumpCfgStyleNow = bcsNone;
caf3666d 4420 char *bm;
aee3523a 4421 if ((bm = ConfigParser::NextToken()) == nullptr) {
caf3666d
AR
4422 self_destruct();
4423 return;
4424 }
4425
2f8abb64 4426 // if this is the first rule processed
aee3523a 4427 if (*ssl_bump == nullptr) {
38450a50
CT
4428 bumpCfgStyleLast = bcsNone;
4429 sslBumpCfgRr::lastDeprecatedRule = Ssl::bumpEnd;
4430 }
4431
329c128c 4432 auto action = Acl::Answer(ACCESS_ALLOWED);
caf3666d 4433
38450a50 4434 if (strcmp(bm, Ssl::BumpModeStr[Ssl::bumpClientFirst]) == 0) {
6f58d7d7 4435 action.kind = Ssl::bumpClientFirst;
38450a50
CT
4436 bumpCfgStyleNow = bcsNew;
4437 } else if (strcmp(bm, Ssl::BumpModeStr[Ssl::bumpServerFirst]) == 0) {
6f58d7d7 4438 action.kind = Ssl::bumpServerFirst;
38450a50 4439 bumpCfgStyleNow = bcsNew;
5d65362c
CT
4440 } else if (strcmp(bm, Ssl::BumpModeStr[Ssl::bumpPeek]) == 0) {
4441 action.kind = Ssl::bumpPeek;
4442 bumpCfgStyleNow = bcsNew;
4443 } else if (strcmp(bm, Ssl::BumpModeStr[Ssl::bumpStare]) == 0) {
4444 action.kind = Ssl::bumpStare;
4445 bumpCfgStyleNow = bcsNew;
4446 } else if (strcmp(bm, Ssl::BumpModeStr[Ssl::bumpSplice]) == 0) {
4447 action.kind = Ssl::bumpSplice;
4448 bumpCfgStyleNow = bcsNew;
4449 } else if (strcmp(bm, Ssl::BumpModeStr[Ssl::bumpBump]) == 0) {
4450 action.kind = Ssl::bumpBump;
4451 bumpCfgStyleNow = bcsNew;
4452 } else if (strcmp(bm, Ssl::BumpModeStr[Ssl::bumpTerminate]) == 0) {
4453 action.kind = Ssl::bumpTerminate;
4454 bumpCfgStyleNow = bcsNew;
38450a50 4455 } else if (strcmp(bm, Ssl::BumpModeStr[Ssl::bumpNone]) == 0) {
6f58d7d7 4456 action.kind = Ssl::bumpNone;
38450a50
CT
4457 bumpCfgStyleNow = bcsNew;
4458 } else if (strcmp(bm, "allow") == 0) {
4459 debugs(3, DBG_CRITICAL, "SECURITY NOTICE: auto-converting deprecated "
4460 "\"ssl_bump allow <acl>\" to \"ssl_bump client-first <acl>\" which "
4461 "is usually inferior to the newer server-first "
4462 "bumping mode. Update your ssl_bump rules.");
6f58d7d7 4463 action.kind = Ssl::bumpClientFirst;
38450a50
CT
4464 bumpCfgStyleNow = bcsOld;
4465 sslBumpCfgRr::lastDeprecatedRule = Ssl::bumpClientFirst;
4466 } else if (strcmp(bm, "deny") == 0) {
4467 debugs(3, DBG_CRITICAL, "WARNING: auto-converting deprecated "
4468 "\"ssl_bump deny <acl>\" to \"ssl_bump none <acl>\". Update "
4469 "your ssl_bump rules.");
6f58d7d7 4470 action.kind = Ssl::bumpNone;
38450a50
CT
4471 bumpCfgStyleNow = bcsOld;
4472 sslBumpCfgRr::lastDeprecatedRule = Ssl::bumpNone;
caf3666d
AR
4473 } else {
4474 debugs(3, DBG_CRITICAL, "FATAL: unknown ssl_bump mode: " << bm);
4475 self_destruct();
4476 return;
4477 }
4478
38450a50 4479 if (bumpCfgStyleLast != bcsNone && bumpCfgStyleNow != bumpCfgStyleLast) {
87f237a9 4480 debugs(3, DBG_CRITICAL, "FATAL: do not mix " << bumpCfgStyleNow << " actions with " <<
38450a50
CT
4481 bumpCfgStyleLast << " actions. Update your ssl_bump rules.");
4482 self_destruct();
4483 return;
4484 }
4485
4486 bumpCfgStyleLast = bumpCfgStyleNow;
4487
8984b8cd 4488 // empty rule OK
ba6fffba 4489 ParseAclWithAction(ssl_bump, action, "ssl_bump");
caf3666d
AR
4490}
4491
4492static void dump_sslproxy_ssl_bump(StoreEntry *entry, const char *name, acl_access *ssl_bump)
4493{
2cb8d372 4494 if (ssl_bump)
329c128c 4495 dump_SBufList(entry, ssl_bump->treeDump(name, [](const Acl::Answer &action) {
f42a85ac
SM
4496 return Ssl::BumpModeStr.at(action.kind);
4497 }));
caf3666d
AR
4498}
4499
4500static void free_sslproxy_ssl_bump(acl_access **ssl_bump)
4501{
4502 free_acl_access(ssl_bump);
4503}
8277060a
CT
4504
4505#endif
f4698e0b
CT
4506
4507static void dump_HeaderWithAclList(StoreEntry * entry, const char *name, HeaderWithAclList *headers)
4508{
4509 if (!headers)
4510 return;
4511
4512 for (HeaderWithAclList::iterator hwa = headers->begin(); hwa != headers->end(); ++hwa) {
ced8def3 4513 storeAppendPrintf(entry, "%s %s %s", name, hwa->fieldName.c_str(), hwa->fieldValue.c_str());
f4698e0b
CT
4514 if (hwa->aclList)
4515 dump_acl_list(entry, hwa->aclList);
4516 storeAppendPrintf(entry, "\n");
4517 }
4518}
4519
4520static void parse_HeaderWithAclList(HeaderWithAclList **headers)
4521{
4522 char *fn;
4523 if (!*headers) {
4524 *headers = new HeaderWithAclList;
4525 }
aee3523a 4526 if ((fn = ConfigParser::NextToken()) == nullptr) {
f4698e0b
CT
4527 self_destruct();
4528 return;
4529 }
4530 HeaderWithAcl hwa;
4531 hwa.fieldName = fn;
81ab22b6 4532 hwa.fieldId = Http::HeaderLookupTable.lookup(hwa.fieldName).id;
789217a2
FC
4533 if (hwa.fieldId == Http::HdrType::BAD_HDR)
4534 hwa.fieldId = Http::HdrType::OTHER;
f4698e0b 4535
2eceb328
CT
4536 Format::Format *nlf = new ::Format::Format("hdrWithAcl");
4537 ConfigParser::EnableMacros();
bde7a8ce 4538 String buf = ConfigParser::NextQuotedToken();
2eceb328 4539 ConfigParser::DisableMacros();
f4698e0b 4540 hwa.fieldValue = buf.termedBuf();
2eceb328 4541 hwa.quoted = ConfigParser::LastTokenWasQuoted();
f4698e0b 4542 if (hwa.quoted) {
f4698e0b
CT
4543 if (!nlf->parse(hwa.fieldValue.c_str())) {
4544 self_destruct();
4545 return;
4546 }
4547 hwa.valueFormat = nlf;
2eceb328
CT
4548 } else
4549 delete nlf;
6f58d7d7 4550 aclParseAclList(LegacyParser, &hwa.aclList, (hwa.fieldName + ':' + hwa.fieldValue).c_str());
f4698e0b
CT
4551 (*headers)->push_back(hwa);
4552}
4553
4554static void free_HeaderWithAclList(HeaderWithAclList **header)
4555{
4556 if (!(*header))
4557 return;
4558
4559 for (HeaderWithAclList::iterator hwa = (*header)->begin(); hwa != (*header)->end(); ++hwa) {
4560 if (hwa->aclList)
4561 aclDestroyAclList(&hwa->aclList);
4562
4563 if (hwa->valueFormat) {
4564 delete hwa->valueFormat;
aee3523a 4565 hwa->valueFormat = nullptr;
f4698e0b
CT
4566 }
4567 }
4568 delete *header;
aee3523a 4569 *header = nullptr;
f4698e0b 4570}
d7f4a0b7
CT
4571
4572static void parse_note(Notes *notes)
4573{
4574 assert(notes);
4575 notes->parse(LegacyParser);
4576}
4577
4578static void dump_note(StoreEntry *entry, const char *name, Notes &notes)
4579{
4580 notes.dump(entry, name);
4581}
4582
4583static void free_note(Notes *notes)
4584{
4585 notes->clean();
4586}
bde7a8ce 4587
c59baaa8
EB
4588static DebugMessageId ParseDebugMessageId(const char *value, const char eov)
4589{
4590 const auto id = xatoui(value, eov);
4591 if (!(0 < id && id < DebugMessageIdUpperBound))
4592 throw TextException(ToSBuf("unknown cache_log_message ID: ", value), Here());
4593 return static_cast<DebugMessageId>(id);
4594}
4595
4596static void parse_cache_log_message(DebugMessages **debugMessages)
4597{
4598 DebugMessage msg;
4599 DebugMessageId minId = 0;
4600 DebugMessageId maxId = 0;
4601
4602 char *key = nullptr;
4603 char *value = nullptr;
4604 while (ConfigParser::NextKvPair(key, value)) {
4605 if (strcmp(key, "id") == 0) {
4606 if (minId > 0)
4607 break;
4608 minId = maxId = ParseDebugMessageId(value, '\0');
4609 } else if (strcmp(key, "ids") == 0) {
4610 if (minId > 0)
4611 break;
4612 const auto dash = strchr(value, '-');
4613 if (!dash)
4614 throw TextException(ToSBuf("malformed cache_log_message ID range: ", key, '=', value), Here());
4615 minId = ParseDebugMessageId(value, '-');
4616 maxId = ParseDebugMessageId(dash+1, '\0');
4617 if (minId > maxId)
4618 throw TextException(ToSBuf("invalid cache_log_message ID range: ", key, '=', value), Here());
4619 } else if (strcmp(key, "level") == 0) {
4620 if (msg.levelled())
4621 break;
4622 const auto level = xatoi(value);
4623 if (level < 0)
4624 throw TextException(ToSBuf("negative cache_log_message level: ", value), Here());
4625 msg.level = level;
4626 } else if (strcmp(key, "limit") == 0) {
4627 if (msg.limited())
4628 break;
4629 msg.limit = xatoull(value, 10);
4630 } else {
4631 throw TextException(ToSBuf("unsupported cache_log_message option: ", key), Here());
4632 }
4633 key = value = nullptr;
4634 }
4635
4636 if (key && value)
4637 throw TextException(ToSBuf("repeated or conflicting cache_log_message option: ", key, '=', value), Here());
4638
4639 if (!minId)
4640 throw TextException("cache_log_message is missing a required id=... or ids=... option", Here());
4641
4642 if (!(msg.levelled() || msg.limited()))
4643 throw TextException("cache_log_message is missing a required level=... or limit=... option", Here());
4644
4645 assert(debugMessages);
4646 if (!*debugMessages)
4647 *debugMessages = new DebugMessages();
4648
4649 for (auto id = minId; id <= maxId; ++id) {
4650 msg.id = id;
4651 (*debugMessages)->messages.at(id) = msg;
4652 }
4653}
4654
4655static void dump_cache_log_message(StoreEntry *entry, const char *name, const DebugMessages *debugMessages)
4656{
4657 if (!debugMessages)
4658 return;
4659
4660 SBufStream out;
4661 for (const auto &msg: debugMessages->messages) {
4662 if (!msg.configured())
4663 continue;
4664 out << name << " id=" << msg.id;
4665 if (msg.levelled())
4666 out << " level=" << msg.level;
4667 if (msg.limited())
4668 out << " limit=" << msg.limit;
4669 out << "\n";
4670 }
4671 const auto buf = out.buf();
4672 entry->append(buf.rawContent(), buf.length()); // may be empty
4673}
4674
4675static void free_cache_log_message(DebugMessages **debugMessages)
4676{
4677 // clear old messages to avoid cumulative effect across (re)configurations
4678 assert(debugMessages);
4679 delete *debugMessages;
4680 *debugMessages = nullptr;
4681}
4682
ddf5aa2b
CT
4683static bool FtpEspvDeprecated = false;
4684static void parse_ftp_epsv(acl_access **ftp_epsv)
4685{
329c128c 4686 Acl::Answer ftpEpsvDeprecatedAction;
ddf5aa2b
CT
4687 bool ftpEpsvIsDeprecatedRule = false;
4688
4689 char *t = ConfigParser::PeekAtToken();
e2849af8 4690 if (!t) {
ddf5aa2b
CT
4691 self_destruct();
4692 return;
4693 }
4694
4695 if (!strcmp(t, "off")) {
4696 (void)ConfigParser::NextToken();
4697 ftpEpsvIsDeprecatedRule = true;
329c128c 4698 ftpEpsvDeprecatedAction = Acl::Answer(ACCESS_DENIED);
ddf5aa2b
CT
4699 } else if (!strcmp(t, "on")) {
4700 (void)ConfigParser::NextToken();
4701 ftpEpsvIsDeprecatedRule = true;
329c128c 4702 ftpEpsvDeprecatedAction = Acl::Answer(ACCESS_ALLOWED);
ddf5aa2b
CT
4703 }
4704
4705 // Check for mixing "ftp_epsv on|off" and "ftp_epsv allow|deny .." rules:
4706 // 1) if this line is "ftp_epsv allow|deny ..." and already exist rules of "ftp_epsv on|off"
4707 // 2) if this line is "ftp_epsv on|off" and already exist rules of "ftp_epsv allow|deny ..."
4708 // then abort
4709 if ((!ftpEpsvIsDeprecatedRule && FtpEspvDeprecated) ||
aee3523a 4710 (ftpEpsvIsDeprecatedRule && !FtpEspvDeprecated && *ftp_epsv != nullptr)) {
ddf5aa2b
CT
4711 debugs(3, DBG_CRITICAL, "FATAL: do not mix \"ftp_epsv on|off\" cfg lines with \"ftp_epsv allow|deny ...\" cfg lines. Update your ftp_epsv rules.");
4712 self_destruct();
1847642b 4713 return;
ddf5aa2b
CT
4714 }
4715
4716 if (ftpEpsvIsDeprecatedRule) {
4717 // overwrite previous ftp_epsv lines
4718 delete *ftp_epsv;
ba6fffba
EB
4719 *ftp_epsv = nullptr;
4720
329c128c 4721 if (ftpEpsvDeprecatedAction == Acl::Answer(ACCESS_DENIED)) {
ba6fffba
EB
4722 if (ACL *a = ACL::FindByName("all"))
4723 ParseAclWithAction(ftp_epsv, ftpEpsvDeprecatedAction, "ftp_epsv", a);
4724 else {
ddf5aa2b
CT
4725 self_destruct();
4726 return;
4727 }
ba6fffba 4728 }
ddf5aa2b
CT
4729 FtpEspvDeprecated = true;
4730 } else {
4731 aclParseAccessLine(cfg_directive, LegacyParser, ftp_epsv);
4732 }
4733}
4734
4735static void dump_ftp_epsv(StoreEntry *entry, const char *name, acl_access *ftp_epsv)
4736{
dfad5100 4737 if (ftp_epsv)
ba6fffba 4738 dump_SBufList(entry, ftp_epsv->treeDump(name, Acl::AllowOrDeny));
ddf5aa2b
CT
4739}
4740
4741static void free_ftp_epsv(acl_access **ftp_epsv)
4742{
4743 free_acl_access(ftp_epsv);
4744 FtpEspvDeprecated = false;
4745}
4746
8da861a5
EB
4747/// Like parseTimeLine() but does not require the timeunit to be specified.
4748/// If missed, the default 'second' timeunit is assumed.
4749static std::chrono::seconds
4750ParseUrlRewriteTimeout()
4751{
4752 const auto timeValueToken = ConfigParser::NextToken();
4753 if (!timeValueToken)
4754 throw TexcHere("cannot read a time value");
4755
4756 using Seconds = std::chrono::seconds;
4757
4758 const auto parsedTimeValue = xatof(timeValueToken);
4759
4760 if (parsedTimeValue == 0)
4761 return std::chrono::seconds::zero();
4762
4763 std::chrono::nanoseconds parsedUnitDuration;
4764
4765 const auto unitToken = ConfigParser::PeekAtToken();
4766 if (parseTimeUnit<Seconds>(unitToken, parsedUnitDuration))
4767 (void)ConfigParser::NextToken();
4768 else {
4769 const auto defaultParsed = parseTimeUnit<Seconds>(T_SECOND_STR, parsedUnitDuration);
4770 assert(defaultParsed);
4771 debugs(3, DBG_PARSE_NOTE(DBG_IMPORTANT), ConfigParser::CurrentLocation() <<
b5316557 4772 ": WARNING: missing time unit, using deprecated default '" << T_SECOND_STR << "'");
8da861a5
EB
4773 }
4774
f1e130c5 4775 const auto nanoseconds = ToNanoSeconds(parsedTimeValue, parsedUnitDuration);
8da861a5
EB
4776
4777 return FromNanoseconds<Seconds>(nanoseconds, parsedTimeValue);
4778}
4779
bde7a8ce 4780static void
ced8def3 4781parse_UrlHelperTimeout(SquidConfig::UrlHelperTimeout *config)
32fd6d8a 4782{
8da861a5
EB
4783 // TODO: do not allow optional timeunit (as the documentation prescribes)
4784 // and use parseTimeLine() instead.
4785 Config.Timeout.urlRewrite = ParseUrlRewriteTimeout().count();
32fd6d8a
CT
4786
4787 char *key, *value;
4788 while(ConfigParser::NextKvPair(key, value)) {
4789 if (strcasecmp(key, "on_timeout") == 0) {
4790 if (strcasecmp(value, "bypass") == 0)
ced8def3 4791 config->action = toutActBypass;
32fd6d8a 4792 else if (strcasecmp(value, "fail") == 0)
ced8def3 4793 config->action = toutActFail;
32fd6d8a 4794 else if (strcasecmp(value, "retry") == 0)
ced8def3 4795 config->action = toutActRetry;
32fd6d8a 4796 else if (strcasecmp(value, "use_configured_response") == 0) {
ced8def3 4797 config->action = toutActUseConfiguredResponse;
32fd6d8a 4798 } else {
61beade2 4799 debugs(3, DBG_CRITICAL, "FATAL: unsupported \"on_timeout\" action: " << value);
32fd6d8a 4800 self_destruct();
ba724607 4801 return;
32fd6d8a
CT
4802 }
4803 } else if (strcasecmp(key, "response") == 0) {
ced8def3 4804 config->response = xstrdup(value);
32fd6d8a 4805 } else {
61beade2 4806 debugs(3, DBG_CRITICAL, "FATAL: unsupported option " << key);
32fd6d8a 4807 self_destruct();
ba724607 4808 return;
32fd6d8a
CT
4809 }
4810 }
4811
ced8def3 4812 if (config->action == toutActUseConfiguredResponse && !config->response) {
32fd6d8a
CT
4813 debugs(3, DBG_CRITICAL, "FATAL: Expected 'response=' option after 'on_timeout=use_configured_response' option");
4814 self_destruct();
4815 }
4816
ced8def3 4817 if (config->action != toutActUseConfiguredResponse && config->response) {
61beade2 4818 debugs(3, DBG_CRITICAL, "FATAL: 'response=' option is valid only when used with the 'on_timeout=use_configured_response' option");
32fd6d8a
CT
4819 self_destruct();
4820 }
4821}
4822
4823static void
ced8def3 4824dump_UrlHelperTimeout(StoreEntry *entry, const char *name, SquidConfig::UrlHelperTimeout &config)
32fd6d8a
CT
4825{
4826 const char *onTimedOutActions[] = {"bypass", "fail", "retry", "use_configured_response"};
ced8def3 4827 assert(config.action >= 0 && config.action <= toutActUseConfiguredResponse);
32fd6d8a
CT
4828
4829 dump_time_t(entry, name, Config.Timeout.urlRewrite);
ced8def3 4830 storeAppendPrintf(entry, " on_timeout=%s", onTimedOutActions[config.action]);
32fd6d8a 4831
ced8def3
AJ
4832 if (config.response)
4833 storeAppendPrintf(entry, " response=\"%s\"", config.response);
32fd6d8a
CT
4834
4835 storeAppendPrintf(entry, "\n");
4836}
4837
4838static void
ced8def3 4839free_UrlHelperTimeout(SquidConfig::UrlHelperTimeout *config)
32fd6d8a
CT
4840{
4841 Config.Timeout.urlRewrite = 0;
ced8def3
AJ
4842 config->action = 0;
4843 safe_free(config->response);
32fd6d8a
CT
4844}
4845
bde7a8ce 4846static void
ced8def3 4847parse_configuration_includes_quoted_values(bool *)
bde7a8ce
CT
4848{
4849 int val = 0;
4850 parse_onoff(&val);
4851
4852 // If quoted values is set to on then enable new strict mode parsing
4853 if (val) {
4854 ConfigParser::RecognizeQuotedValues = true;
4855 ConfigParser::StrictMode = true;
4856 } else {
4857 ConfigParser::RecognizeQuotedValues = false;
4858 ConfigParser::StrictMode = false;
4859 }
4860}
4861
4862static void
ced8def3 4863dump_configuration_includes_quoted_values(StoreEntry *const entry, const char *const name, bool)
bde7a8ce
CT
4864{
4865 int val = ConfigParser::RecognizeQuotedValues ? 1 : 0;
4866 dump_onoff(entry, name, val);
4867}
4868
4869static void
ced8def3 4870free_configuration_includes_quoted_values(bool *)
bde7a8ce
CT
4871{
4872 ConfigParser::RecognizeQuotedValues = false;
4873 ConfigParser::StrictMode = false;
4874}
f53969cc 4875
3248e962
CT
4876static void
4877parse_on_unsupported_protocol(acl_access **access)
4878{
44352c16 4879 char *tm;
aee3523a 4880 if ((tm = ConfigParser::NextToken()) == nullptr) {
3248e962
CT
4881 self_destruct();
4882 return;
4883 }
4884
329c128c 4885 auto action = Acl::Answer(ACCESS_ALLOWED);
3248e962
CT
4886 if (strcmp(tm, "tunnel") == 0)
4887 action.kind = 1;
4888 else if (strcmp(tm, "respond") == 0)
4889 action.kind = 2;
4890 else {
4891 debugs(3, DBG_CRITICAL, "FATAL: unknown on_unsupported_protocol mode: " << tm);
4892 self_destruct();
4893 return;
4894 }
4895
3248e962 4896 // empty rule OK
ba6fffba 4897 ParseAclWithAction(access, action, "on_unsupported_protocol");
3248e962
CT
4898}
4899
4900static void
4901dump_on_unsupported_protocol(StoreEntry *entry, const char *name, acl_access *access)
4902{
ba6fffba 4903 static const std::vector<const char *> onErrorTunnelMode = {
3248e962
CT
4904 "none",
4905 "tunnel",
ba6fffba 4906 "respond"
3248e962
CT
4907 };
4908 if (access) {
329c128c 4909 SBufList lines = access->treeDump(name, [](const Acl::Answer &action) {
f42a85ac 4910 return onErrorTunnelMode.at(action.kind);
ba6fffba 4911 });
3248e962
CT
4912 dump_SBufList(entry, lines);
4913 }
4914}
4915
4916static void
4917free_on_unsupported_protocol(acl_access **access)
4918{
4919 free_acl_access(access);
4920}
44352c16 4921
1c2b4465
CT
4922static void
4923parse_http_upgrade_request_protocols(HttpUpgradeProtocolAccess **protoGuardsPtr)
4924{
4925 assert(protoGuardsPtr);
4926 auto &protoGuards = *protoGuardsPtr;
4927 if (!protoGuards)
4928 protoGuards = new HttpUpgradeProtocolAccess();
4929 protoGuards->configureGuard(LegacyParser);
4930}
4931
4932static void
4933dump_http_upgrade_request_protocols(StoreEntry *entry, const char *rawName, HttpUpgradeProtocolAccess *protoGuards)
4934{
182d433a
AR
4935 if (!protoGuards)
4936 return;
4937
1c2b4465
CT
4938 const SBuf name(rawName);
4939 protoGuards->forEach([entry,&name](const SBuf &proto, const acl_access *acls) {
4940 SBufList line;
4941 line.push_back(name);
4942 line.push_back(proto);
4943 const auto acld = acls->treeDump("", &Acl::AllowOrDeny);
4944 line.insert(line.end(), acld.begin(), acld.end());
4945 dump_SBufList(entry, line);
4946 });
4947}
4948
4949static void
4950free_http_upgrade_request_protocols(HttpUpgradeProtocolAccess **protoGuardsPtr)
4951{
4952 assert(protoGuardsPtr);
4953 auto &protoGuards = *protoGuardsPtr;
4954 delete protoGuards;
4955 protoGuards = nullptr;
4956}
4957