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