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