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