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