]> git.ipfire.org Git - thirdparty/squid.git/blame - src/cache_cf.cc
4.0.8
[thirdparty/squid.git] / src / cache_cf.cc
CommitLineData
30a4f2a8 1/*
ef57eb7b 2 * Copyright (C) 1996-2016 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"
58d8264f 55#include "sbuf/List.h"
4d5904f7 56#include "SquidConfig.h"
f9b6ff6e 57#include "SquidString.h"
d9c7489e 58#include "ssl/ProxyCerts.h"
582c2af2 59#include "Store.h"
2745fea5
AR
60#include "store/Disk.h"
61#include "store/Disks.h"
582c2af2 62#include "StoreFileSystem.h"
4e540555 63#include "tools.h"
ed6e9fb9 64#include "util.h"
602d9612 65#include "wordlist.h"
b007d04d
FC
66/* wccp2 has its own conditional definitions */
67#include "wccp2.h"
82b7abe3
AJ
68#if USE_ADAPTATION
69#include "adaptation/Config.h"
70#endif
71#if ICAP_CLIENT
72#include "adaptation/icap/Config.h"
73#endif
74#if USE_ECAP
75#include "adaptation/ecap/Config.h"
76#endif
cb4f4424 77#if USE_OPENSSL
95d2589c 78#include "ssl/Config.h"
602d9612 79#include "ssl/support.h"
95d2589c 80#endif
2f1431ea 81#if USE_AUTH
2d2b0bb7
AR
82#include "auth/Config.h"
83#include "auth/Scheme.h"
2f1431ea 84#endif
82b7abe3
AJ
85#if USE_SQUID_ESI
86#include "esi/Parser.h"
87#endif
82b7abe3
AJ
88#if SQUID_SNMP
89#include "snmp.h"
90#endif
582c2af2 91
52303a3d 92#if HAVE_GLOB_H
592a09dc 93#include <glob.h>
52303a3d 94#endif
425de4c8 95#include <limits>
f4698e0b 96#include <list>
582c2af2
FC
97#if HAVE_PWD_H
98#include <pwd.h>
99#endif
100#if HAVE_GRP_H
101#include <grp.h>
102#endif
103#if HAVE_SYS_STAT_H
104#include <sys/stat.h>
105#endif
f4698e0b 106
cb4f4424 107#if USE_OPENSSL
95d2589c
CT
108#include "ssl/gadgets.h"
109#endif
110
62c7f90e 111#if USE_ADAPTATION
62c7f90e 112static void parse_adaptation_service_set_type();
a22e6cd3 113static void parse_adaptation_service_chain_type();
62c7f90e 114static void parse_adaptation_access_type();
62c7f90e
AR
115#endif
116
3a69ddf3 117#if ICAP_CLIENT
26cc52cb
AR
118static void parse_icap_service_type(Adaptation::Icap::Config *);
119static void dump_icap_service_type(StoreEntry *, const char *, const Adaptation::Icap::Config &);
120static void free_icap_service_type(Adaptation::Icap::Config *);
c939dc70
AR
121static void parse_icap_class_type();
122static void parse_icap_access_type();
8277060a
CT
123
124static void parse_icap_service_failure_limit(Adaptation::Icap::Config *);
125static void dump_icap_service_failure_limit(StoreEntry *, const char *, const Adaptation::Icap::Config &);
126static void free_icap_service_failure_limit(Adaptation::Icap::Config *);
3a69ddf3 127#endif
128
21a26d31 129#if USE_ECAP
574b508c
AR
130static void parse_ecap_service_type(Adaptation::Ecap::Config *);
131static void dump_ecap_service_type(StoreEntry *, const char *, const Adaptation::Ecap::Config &);
132static void free_ecap_service_type(Adaptation::Ecap::Config *);
21a26d31
AR
133#endif
134
6a9136ad
FC
135static peer_t parseNeighborType(const char *s);
136
fd0f51c4 137static const char *const T_MILLISECOND_STR = "millisecond";
8813e606 138static const char *const T_SECOND_STR = "second";
139static const char *const T_MINUTE_STR = "minute";
140static const char *const T_HOUR_STR = "hour";
141static const char *const T_DAY_STR = "day";
142static const char *const T_WEEK_STR = "week";
143static const char *const T_FORTNIGHT_STR = "fortnight";
144static const char *const T_MONTH_STR = "month";
145static const char *const T_YEAR_STR = "year";
146static const char *const T_DECADE_STR = "decade";
aa0a0c7c 147
9906e724 148static const char *const B_BYTES_STR = "bytes";
149static const char *const B_KBYTES_STR = "KB";
150static const char *const B_MBYTES_STR = "MB";
151static const char *const B_GBYTES_STR = "GB";
152
4db43fab 153static const char *const list_sep = ", \t\n\r";
b0e7d2d5 154
87ddff6e
FC
155static void parse_access_log(CustomLog ** customlog_definitions);
156static int check_null_access_log(CustomLog *customlog_definitions);
157static void dump_access_log(StoreEntry * entry, const char *name, CustomLog * definitions);
158static void free_access_log(CustomLog ** definitions);
fb0c2f17 159static bool setLogformat(CustomLog *cl, const char *name, const bool dieWhenMissing);
7684c4b1 160
f5b8bbc4 161static void configDoConfigure(void);
8d9a8184 162static void parse_refreshpattern(RefreshPattern **);
9b741834 163static uint64_t parseTimeUnits(const char *unit, bool allowMsec);
32fd6d8a 164static void parseTimeLine(time_msec_t * tptr, const char *units, bool allowMsec, bool expectMoreArguments);
f45dd259 165static void parse_u_short(unsigned short * var);
f5b8bbc4 166static void parse_string(char **);
f5b8bbc4 167static void default_all(void);
168static void defaults_if_none(void);
10d914f6 169static void defaults_postscriptum(void);
f5b8bbc4 170static int parse_line(char *);
76f44481 171static void parse_obsolete(const char *);
f5b8bbc4 172static void parseBytesLine(size_t * bptr, const char *units);
cb4f4424 173#if USE_OPENSSL
95d2589c
CT
174static void parseBytesOptionValue(size_t * bptr, const char *units, char const * value);
175#endif
e210930b 176static void parseBytesLineSigned(ssize_t * bptr, const char *units);
f5b8bbc4 177static size_t parseBytesUnits(const char *unit);
f5b8bbc4 178static void free_all(void);
94439e4e 179void requirePathnameExists(const char *name, const char *path);
ed7f5615 180static OBJH dump_config;
626096be 181#if USE_HTTP_VIOLATIONS
3b07476b
CT
182static void free_HeaderManglers(HeaderManglers **pm);
183static void dump_http_header_access(StoreEntry * entry, const char *name, const HeaderManglers *manglers);
184static void parse_http_header_access(HeaderManglers **manglers);
185#define free_http_header_access free_HeaderManglers
186static void dump_http_header_replace(StoreEntry * entry, const char *name, const HeaderManglers *manglers);
187static void parse_http_header_replace(HeaderManglers **manglers);
188#define free_http_header_replace free_HeaderManglers
9e8b2f1c 189#endif
f4698e0b
CT
190static void dump_HeaderWithAclList(StoreEntry * entry, const char *name, HeaderWithAclList *headers);
191static void parse_HeaderWithAclList(HeaderWithAclList **header);
192static void free_HeaderWithAclList(HeaderWithAclList **header);
d7f4a0b7
CT
193static void parse_note(Notes *);
194static void dump_note(StoreEntry *, const char *, Notes &);
195static void free_note(Notes *);
7f0b3324
FC
196static void parse_denyinfo(AclDenyInfoList ** var);
197static void dump_denyinfo(StoreEntry * entry, const char *name, AclDenyInfoList * var);
198static void free_denyinfo(AclDenyInfoList ** var);
e1a88700 199
0b0cfcf2 200#if USE_WCCPv2
b7ac5457
AJ
201static void parse_IpAddress_list(Ip::Address_list **);
202static void dump_IpAddress_list(StoreEntry *, const char *, const Ip::Address_list *);
203static void free_IpAddress_list(Ip::Address_list **);
0b0cfcf2 204#if CURRENTLY_UNUSED
b7ac5457 205static int check_null_IpAddress_list(const Ip::Address_list *);
3f38a55e 206#endif /* CURRENTLY_UNUSED */
e1a88700 207#endif /* USE_WCCPv2 */
208
fa720bfb 209static void parsePortCfg(AnyP::PortCfgPointer *, const char *protocol);
65d448bc 210#define parse_PortCfg(l) parsePortCfg((l), token)
fa720bfb
AJ
211static void dump_PortCfg(StoreEntry *, const char *, const AnyP::PortCfgPointer &);
212#define free_PortCfg(h) *(h)=NULL
e1a88700 213
cb4f4424 214#if USE_OPENSSL
aebe6888
CT
215static void parse_sslproxy_cert_sign(sslproxy_cert_sign **cert_sign);
216static void dump_sslproxy_cert_sign(StoreEntry *entry, const char *name, sslproxy_cert_sign *cert_sign);
217static void free_sslproxy_cert_sign(sslproxy_cert_sign **cert_sign);
fb2178bb
CT
218static void parse_sslproxy_cert_adapt(sslproxy_cert_adapt **cert_adapt);
219static void dump_sslproxy_cert_adapt(StoreEntry *entry, const char *name, sslproxy_cert_adapt *cert_adapt);
220static void free_sslproxy_cert_adapt(sslproxy_cert_adapt **cert_adapt);
caf3666d
AR
221static void parse_sslproxy_ssl_bump(acl_access **ssl_bump);
222static void dump_sslproxy_ssl_bump(StoreEntry *entry, const char *name, acl_access *ssl_bump);
223static void free_sslproxy_ssl_bump(acl_access **ssl_bump);
cb4f4424 224#endif /* USE_OPENSSL */
e1a88700 225
ddf5aa2b
CT
226static void parse_ftp_epsv(acl_access **ftp_epsv);
227static void dump_ftp_epsv(StoreEntry *entry, const char *name, acl_access *ftp_epsv);
228static void free_ftp_epsv(acl_access **ftp_epsv);
229
ef1955a5 230static void parse_b_size_t(size_t * var);
47f6e231 231static void parse_b_int64_t(int64_t * var);
270b86af 232
c8ea3cc0 233static bool parseNamedIntList(const char *data, const String &name, std::vector<int> &list);
96c2bb61
AR
234
235static void parse_CpuAffinityMap(CpuAffinityMap **const cpuAffinityMap);
236static void dump_CpuAffinityMap(StoreEntry *const entry, const char *const name, const CpuAffinityMap *const cpuAffinityMap);
237static void free_CpuAffinityMap(CpuAffinityMap **const cpuAffinityMap);
238
ced8def3
AJ
239static void parse_UrlHelperTimeout(SquidConfig::UrlHelperTimeout *);
240static void dump_UrlHelperTimeout(StoreEntry *, const char *, SquidConfig::UrlHelperTimeout &);
241static void free_UrlHelperTimeout(SquidConfig::UrlHelperTimeout *);
32fd6d8a 242
cfcb6b30 243static int parseOneConfigFile(const char *file_name, unsigned int depth);
244
bde7a8ce
CT
245static void parse_configuration_includes_quoted_values(bool *recognizeQuotedValues);
246static void dump_configuration_includes_quoted_values(StoreEntry *const entry, const char *const name, bool recognizeQuotedValues);
247static void free_configuration_includes_quoted_values(bool *recognizeQuotedValues);
3248e962
CT
248static void parse_on_unsupported_protocol(acl_access **access);
249static void dump_on_unsupported_protocol(StoreEntry *entry, const char *name, acl_access *access);
250static void free_on_unsupported_protocol(acl_access **access);
bde7a8ce 251
a385afc3 252/*
253 * LegacyParser is a parser for legacy code that uses the global
254 * approach. This is static so that it is only exposed to cache_cf.
255 * Other modules needing access to a ConfigParser should have it
256 * provided to them in their parserFOO methods.
257 */
258static ConfigParser LegacyParser = ConfigParser();
a9f20260 259
0e4e0e7d 260void
0673c0ba 261self_destruct(void)
090089c4 262{
a9f20260 263 LegacyParser.destruct();
6b8e7481 264}
265
76b9daa5 266static void
267SetConfigFilename(char const *file_name, bool is_pipe)
268{
76b9daa5 269 if (is_pipe)
270 cfg_filename = file_name + 1;
328a3f05
AJ
271 else
272 cfg_filename = file_name;
76b9daa5 273}
274
130bff33 275static const char*
276skip_ws(const char* s)
277{
278 while (xisspace(*s))
279 ++s;
280
281 return s;
282}
283
cfcb6b30 284static int
285parseManyConfigFiles(char* files, int depth)
286{
287 int error_count = 0;
d7ac36b9 288 char* saveptr = NULL;
52303a3d 289#if HAVE_GLOB
592a09dc 290 char *path;
291 glob_t globbuf;
292 int i;
293 memset(&globbuf, 0, sizeof(globbuf));
294 for (path = strwordtok(files, &saveptr); path; path = strwordtok(NULL, &saveptr)) {
26ac0430
AJ
295 if (glob(path, globbuf.gl_pathc ? GLOB_APPEND : 0, NULL, &globbuf) != 0) {
296 fatalf("Unable to find configuration file: %s: %s",
297 path, xstrerror());
298 }
299 }
a38ec4b1 300 for (i = 0; i < (int)globbuf.gl_pathc; ++i) {
26ac0430 301 error_count += parseOneConfigFile(globbuf.gl_pathv[i], depth);
cfcb6b30 302 }
592a09dc 303 globfree(&globbuf);
52303a3d 304#else
305 char* file = strwordtok(files, &saveptr);
306 while (file != NULL) {
26ac0430
AJ
307 error_count += parseOneConfigFile(file, depth);
308 file = strwordtok(NULL, &saveptr);
52303a3d 309 }
310#endif /* HAVE_GLOB */
cfcb6b30 311 return error_count;
312}
313
d4a3e179
AR
314static void
315ReplaceSubstr(char*& str, int& len, unsigned substrIdx, unsigned substrLen, const char* newSubstr)
316{
317 assert(str != NULL);
318 assert(newSubstr != NULL);
319
320 unsigned newSubstrLen = strlen(newSubstr);
321 if (newSubstrLen > substrLen)
322 str = (char*)realloc(str, len - substrLen + newSubstrLen + 1);
323
324 // move tail part including zero
325 memmove(str + substrIdx + newSubstrLen, str + substrIdx + substrLen, len - substrIdx - substrLen + 1);
326 // copy new substring in place
327 memcpy(str + substrIdx, newSubstr, newSubstrLen);
328
329 len = strlen(str);
330}
331
332static void
333SubstituteMacro(char*& line, int& len, const char* macroName, const char* substStr)
334{
335 assert(line != NULL);
336 assert(macroName != NULL);
337 assert(substStr != NULL);
338 unsigned macroNameLen = strlen(macroName);
339 while (const char* macroPos = strstr(line, macroName)) // we would replace all occurrences
340 ReplaceSubstr(line, len, macroPos - line, macroNameLen, substStr);
341}
342
343static void
344ProcessMacros(char*& line, int& len)
345{
d5f21615 346 SubstituteMacro(line, len, "${service_name}", service_name.c_str());
9de6c973 347 SubstituteMacro(line, len, "${process_name}", TheKidName);
d4a3e179
AR
348 SubstituteMacro(line, len, "${process_number}", xitoa(KidIdentifier));
349}
350
a7ea9b13
AR
351static void
352trim_trailing_ws(char* str)
353{
354 assert(str != NULL);
355 unsigned i = strlen(str);
356 while ((i > 0) && xisspace(str[i - 1]))
357 --i;
358 str[i] = '\0';
359}
360
361static const char*
362FindStatement(const char* line, const char* statement)
363{
364 assert(line != NULL);
365 assert(statement != NULL);
366
367 const char* str = skip_ws(line);
368 unsigned len = strlen(statement);
369 if (strncmp(str, statement, len) == 0) {
370 str += len;
371 if (*str == '\0')
372 return str;
373 else if (xisspace(*str))
374 return skip_ws(str);
375 }
376
377 return NULL;
378}
379
380static bool
381StrToInt(const char* str, long& number)
382{
383 assert(str != NULL);
384
385 char* end;
386 number = strtol(str, &end, 0);
387
388 return (end != str) && (*end == '\0'); // returns true if string contains nothing except number
389}
390
391static bool
392EvalBoolExpr(const char* expr)
393{
394 assert(expr != NULL);
395 if (strcmp(expr, "true") == 0) {
396 return true;
397 } else if (strcmp(expr, "false") == 0) {
398 return false;
399 } else if (const char* equation = strchr(expr, '=')) {
400 const char* rvalue = skip_ws(equation + 1);
401 char* lvalue = (char*)xmalloc(equation - expr + 1);
402 xstrncpy(lvalue, expr, equation - expr + 1);
403 trim_trailing_ws(lvalue);
404
405 long number1;
406 if (!StrToInt(lvalue, number1))
407 fatalf("String is not a integer number: '%s'\n", lvalue);
408 long number2;
409 if (!StrToInt(rvalue, number2))
410 fatalf("String is not a integer number: '%s'\n", rvalue);
411
412 xfree(lvalue);
413 return number1 == number2;
414 }
415 fatalf("Unable to evaluate expression '%s'\n", expr);
416 return false; // this place cannot be reached
417}
418
cfcb6b30 419static int
420parseOneConfigFile(const char *file_name, unsigned int depth)
2546fcb3 421{
270b86af 422 FILE *fp = NULL;
cfcb6b30 423 const char *orig_cfg_filename = cfg_filename;
424 const int orig_config_lineno = config_lineno;
270b86af 425 char *token = NULL;
81c161d0 426 char *tmp_line = NULL;
427 int tmp_line_len = 0;
e13ee7ad 428 int err_count = 0;
1741cbad 429 int is_pipe = 0;
cfcb6b30 430
e0236918 431 debugs(3, DBG_IMPORTANT, "Processing Configuration File: " << file_name << " (depth " << depth << ")");
cfcb6b30 432 if (depth > 16) {
433 fatalf("WARNING: can't include %s: includes are nested too deeply (>16)!\n", file_name);
434 return 1;
435 }
62e76326 436
1741cbad 437 if (file_name[0] == '!' || file_name[0] == '|') {
62e76326 438 fp = popen(file_name + 1, "r");
439 is_pipe = 1;
1741cbad 440 } else {
62e76326 441 fp = fopen(file_name, "r");
1741cbad 442 }
62e76326 443
1741cbad 444 if (fp == NULL)
cfcb6b30 445 fatalf("Unable to open configuration file: %s: %s", file_name, xstrerror());
62e76326 446
be266cb2 447#if _SQUID_WINDOWS_
c4aefe96 448 setmode(fileno(fp), O_TEXT);
449#endif
62e76326 450
76b9daa5 451 SetConfigFilename(file_name, bool(is_pipe));
62e76326 452
270b86af 453 memset(config_input_line, '\0', BUFSIZ);
62e76326 454
270b86af 455 config_lineno = 0;
62e76326 456
c8ea3cc0 457 std::vector<bool> if_states;
270b86af 458 while (fgets(config_input_line, BUFSIZ, fp)) {
a38ec4b1 459 ++config_lineno;
62e76326 460
461 if ((token = strchr(config_input_line, '\n')))
462 *token = '\0';
463
d866e2ad 464 if ((token = strchr(config_input_line, '\r')))
465 *token = '\0';
466
f068973f
AJ
467 // strip any prefix whitespace off the line.
468 const char *p = skip_ws(config_input_line);
469 if (config_input_line != p)
470 memmove(config_input_line, p, strlen(p)+1);
471
62e76326 472 if (strncmp(config_input_line, "#line ", 6) == 0) {
473 static char new_file_name[1024];
474 static char *file;
475 static char new_lineno;
476 token = config_input_line + 6;
477 new_lineno = strtol(token, &file, 0) - 1;
478
479 if (file == token)
f53969cc 480 continue; /* Not a valid #line directive, may be a comment */
62e76326 481
e4755e29 482 while (*file && xisspace((unsigned char) *file))
a38ec4b1 483 ++file;
62e76326 484
485 if (*file) {
486 if (*file != '"')
f53969cc 487 continue; /* Not a valid #line directive, may be a comment */
62e76326 488
489 xstrncpy(new_file_name, file + 1, sizeof(new_file_name));
490
491 if ((token = strchr(new_file_name, '"')))
492 *token = '\0';
493
328a3f05 494 SetConfigFilename(new_file_name, false);
62e76326 495 }
496
497 config_lineno = new_lineno;
498 }
499
500 if (config_input_line[0] == '#')
501 continue;
502
503 if (config_input_line[0] == '\0')
504 continue;
505
130bff33 506 const char* append = tmp_line_len ? skip_ws(config_input_line) : config_input_line;
507
508 size_t append_len = strlen(append);
62e76326 509
130bff33 510 tmp_line = (char*)xrealloc(tmp_line, tmp_line_len + append_len + 1);
81c161d0 511
130bff33 512 strcpy(tmp_line + tmp_line_len, append);
81c161d0 513
130bff33 514 tmp_line_len += append_len;
81c161d0 515
516 if (tmp_line[tmp_line_len-1] == '\\') {
bf8fe701 517 debugs(3, 5, "parseConfigFile: tmp_line='" << tmp_line << "'");
81c161d0 518 tmp_line[--tmp_line_len] = '\0';
519 continue;
520 }
521
a7ea9b13 522 trim_trailing_ws(tmp_line);
d4a3e179 523 ProcessMacros(tmp_line, tmp_line_len);
dad047ab 524 debugs(3, (opt_parse_cfg_only?1:5), "Processing: " << tmp_line);
62e76326 525
a7ea9b13
AR
526 if (const char* expr = FindStatement(tmp_line, "if")) {
527 if_states.push_back(EvalBoolExpr(expr)); // store last if-statement meaning
528 } else if (FindStatement(tmp_line, "endif")) {
529 if (!if_states.empty())
530 if_states.pop_back(); // remove last if-statement meaning
531 else
532 fatalf("'endif' without 'if'\n");
533 } else if (FindStatement(tmp_line, "else")) {
534 if (!if_states.empty())
535 if_states.back() = !if_states.back();
536 else
537 fatalf("'else' without 'if'\n");
538 } else if (if_states.empty() || if_states.back()) { // test last if-statement meaning if present
539 /* Handle includes here */
540 if (tmp_line_len >= 9 && strncmp(tmp_line, "include", 7) == 0 && xisspace(tmp_line[7])) {
541 err_count += parseManyConfigFiles(tmp_line + 8, depth + 1);
542 } else if (!parse_line(tmp_line)) {
fa84c01d 543 debugs(3, DBG_CRITICAL, HERE << cfg_filename << ":" << config_lineno << " unrecognized: '" << tmp_line << "'");
a38ec4b1 544 ++err_count;
a7ea9b13 545 }
26ac0430 546 }
62e76326 547
548 safe_free(tmp_line);
81c161d0 549 tmp_line_len = 0;
550
270b86af 551 }
a7ea9b13
AR
552 if (!if_states.empty())
553 fatalf("if-statement without 'endif'\n");
62e76326 554
1741cbad 555 if (is_pipe) {
62e76326 556 int ret = pclose(fp);
557
558 if (ret != 0)
559 fatalf("parseConfigFile: '%s' failed with exit code %d\n", file_name, ret);
1741cbad 560 } else {
62e76326 561 fclose(fp);
1741cbad 562 }
62e76326 563
328a3f05 564 SetConfigFilename(orig_cfg_filename, false);
cfcb6b30 565 config_lineno = orig_config_lineno;
566
5c6dae44 567 xfree(tmp_line);
cfcb6b30 568 return err_count;
569}
570
571int
dd9b1081 572parseConfigFile(const char *file_name)
cfcb6b30 573{
574 int err_count = 0;
575
5817ee13
AJ
576 debugs(5, 4, HERE);
577
cfcb6b30 578 configFreeMemory();
579
5491c11e 580 ACLMethodData::ThePurgeCount = 0;
cfcb6b30 581 default_all();
582
583 err_count = parseOneConfigFile(file_name, 0);
584
f0b19334 585 defaults_if_none();
f9ad0106 586
10d914f6
CT
587 defaults_postscriptum();
588
9c46ca97 589 /*
590 * We must call configDoConfigure() before leave_suid() because
591 * configDoConfigure() is where we turn username strings into
592 * uid values.
593 */
594 configDoConfigure();
595
f9ad0106 596 if (!Config.chroot_dir) {
597 leave_suid();
c642c141 598 setUmask(Config.umask);
62493678 599 _db_init(Debug::cache_log, Debug::debugOptions);
f9ad0106 600 enter_suid();
601 }
602
478c4f2f 603 if (opt_send_signal == -1) {
8822ebee 604 Mgr::RegisterAction("config",
d9fc6862
A
605 "Current Squid Configuration",
606 dump_config,
607 1, 1);
478c4f2f 608 }
609
e13ee7ad 610 return err_count;
f0b19334 611}
270b86af 612
f0b19334 613static void
614configDoConfigure(void)
615{
f0b19334 616 memset(&Config2, '\0', sizeof(SquidConfig2));
7021844c 617 /* init memory as early as possible */
618 memConfigure();
270b86af 619 /* Sanity checks */
62e76326 620
b69575d6 621 Config.cacheSwap.n_strands = 0; // no diskers by default
7895fa18
AJ
622 if (Config.cacheSwap.swapDirs == NULL) {
623 /* Memory-only cache probably in effect. */
624 /* turn off the cache rebuild delays... */
625 StoreController::store_dirs_rebuilding = 0;
3b581957
DK
626 } else if (InDaemonMode()) { // no diskers in non-daemon mode
627 for (int i = 0; i < Config.cacheSwap.n_configured; ++i) {
628 const RefCount<SwapDir> sd = Config.cacheSwap.swapDirs[i];
629 if (sd->needsDiskStrand())
630 sd->disker = Config.workers + (++Config.cacheSwap.n_strands);
631 }
7895fa18
AJ
632 }
633
47df1aa7
AJ
634 if (Debug::rotateNumber < 0) {
635 Debug::rotateNumber = Config.Log.rotateNumber;
636 }
637
5aecb102 638#if SIZEOF_OFF_T <= 4
639 if (Config.Store.maxObjectSize > 0x7FFF0000) {
fa84c01d 640 debugs(3, DBG_CRITICAL, "WARNING: This Squid binary can not handle files larger than 2GB. Limiting maximum_object_size to just below 2GB");
26ac0430 641 Config.Store.maxObjectSize = 0x7FFF0000;
5aecb102 642 }
643#endif
62e76326 644
84f42bac 645 if (Config.Announce.period > 0) {
62e76326 646 Config.onoff.announce = 1;
9376d64c 647 } else {
f53969cc 648 Config.Announce.period = 86400 * 365; /* one year */
62e76326 649 Config.onoff.announce = 0;
270b86af 650 }
62e76326 651
d3caee79 652 if (Config.onoff.httpd_suppress_version_string)
653 visible_appname_string = (char *)appname_string;
654 else
7dbca7a4 655 visible_appname_string = (char const *)APP_FULLNAME;
d3caee79 656
270b86af 657 if (Config.Program.redirect) {
48d54e4d
AJ
658 if (Config.redirectChildren.n_max < 1) {
659 Config.redirectChildren.n_max = 0;
62e76326 660 wordlistDestroy(&Config.Program.redirect);
661 }
fea2e6e0 662 }
62e76326 663
a8a0b1c2
EC
664 if (Config.Program.store_id) {
665 if (Config.storeIdChildren.n_max < 1) {
666 Config.storeIdChildren.n_max = 0;
667 wordlistDestroy(&Config.Program.store_id);
668 }
669 }
670
f1dc9b30 671 if (Config.appendDomain)
62e76326 672 if (*Config.appendDomain != '.')
673 fatal("append_domain must begin with a '.'");
674
270b86af 675 if (Config.errHtmlText == NULL)
62e76326 676 Config.errHtmlText = xstrdup(null_string);
677
1b7fae06
AJ
678#if !HAVE_SETRLIMIT || !defined(RLIMIT_NOFILE)
679 if (Config.max_filedescriptors > 0) {
3a9087af 680 debugs(0, DBG_IMPORTANT, "WARNING: max_filedescriptors disabled. Operating System setrlimit(RLIMIT_NOFILE) is missing.");
1b7fae06
AJ
681 }
682#elif USE_SELECT || USE_SELECT_WIN32
683 if (Config.max_filedescriptors > FD_SETSIZE) {
3a9087af 684 debugs(0, DBG_IMPORTANT, "WARNING: max_filedescriptors limited to " << FD_SETSIZE << " by select() algorithm.");
1b7fae06
AJ
685 }
686#endif
687
270b86af 688 storeConfigure();
62e76326 689
52f772de 690 snprintf(ThisCache, sizeof(ThisCache), "%s (%s)",
62e76326 691 uniqueHostname(),
d3caee79 692 visible_appname_string);
fbdba7c4 693
38a6c74e 694 /*
695 * the extra space is for loop detection in client_side.c -- we search
696 * for substrings in the Via header.
697 */
52f772de 698 snprintf(ThisCache2, sizeof(ThisCache), " %s (%s)",
62e76326 699 uniqueHostname(),
d3caee79 700 visible_appname_string);
62e76326 701
b2b40d8c
AJ
702 /* Use visible_hostname as default surrogate_id */
703 if (!Config.Accel.surrogate_id) {
704 const char *t = getMyHostname();
705 Config.Accel.surrogate_id = xstrdup( (t?t:"unset-id") );
706 }
707
270b86af 708 if (!Config.udpMaxHitObjsz || Config.udpMaxHitObjsz > SQUID_UDP_SO_SNDBUF)
62e76326 709 Config.udpMaxHitObjsz = SQUID_UDP_SO_SNDBUF;
710
270b86af 711 if (Config.appendDomain)
62e76326 712 Config.appendDomainLen = strlen(Config.appendDomain);
270b86af 713 else
62e76326 714 Config.appendDomainLen = 0;
715
31ef19cd
AJ
716 if (Config.connect_retries > 10) {
717 debugs(0,DBG_CRITICAL, "WARNING: connect_retries cannot be larger than 10. Resetting to 10.");
718 Config.connect_retries = 10;
5210854d 719 }
62e76326 720
f0b19334 721 requirePathnameExists("MIME Config Table", Config.mimeTablePathname);
a3d0a19d 722#if USE_UNLINKD
62e76326 723
f0b19334 724 requirePathnameExists("unlinkd_program", Config.Program.unlinkd);
a3d0a19d 725#endif
82b7abe3 726 requirePathnameExists("logfile_daemon", Log::TheConfig.logfile_daemon);
f0b19334 727 if (Config.Program.redirect)
62e76326 728 requirePathnameExists("redirect_program", Config.Program.redirect->key);
729
a8a0b1c2
EC
730 if (Config.Program.store_id)
731 requirePathnameExists("store_id_program", Config.Program.store_id->key);
732
f0b19334 733 requirePathnameExists("Icon Directory", Config.icons.directory);
62e76326 734
26ac0430 735 if (Config.errorDirectory)
43000484 736 requirePathnameExists("Error Directory", Config.errorDirectory);
62e76326 737
626096be 738#if USE_HTTP_VIOLATIONS
62e76326 739
9f60cfdf 740 {
8d9a8184 741 const RefreshPattern *R;
62e76326 742
26ac0430 743 for (R = Config.Refresh; R; R = R->next) {
62e76326 744 if (!R->flags.override_expire)
745 continue;
746
e0236918 747 debugs(22, DBG_IMPORTANT, "WARNING: use of 'override-expire' in 'refresh_pattern' violates HTTP");
62e76326 748
749 break;
750 }
751
26ac0430 752 for (R = Config.Refresh; R; R = R->next) {
62e76326 753 if (!R->flags.override_lastmod)
754 continue;
755
e0236918 756 debugs(22, DBG_IMPORTANT, "WARNING: use of 'override-lastmod' in 'refresh_pattern' violates HTTP");
62e76326 757
758 break;
759 }
760
26ac0430 761 for (R = Config.Refresh; R; R = R->next) {
38f9c547 762 if (!R->flags.reload_into_ims)
763 continue;
764
e0236918 765 debugs(22, DBG_IMPORTANT, "WARNING: use of 'reload-into-ims' in 'refresh_pattern' violates HTTP");
38f9c547 766
767 break;
768 }
769
26ac0430 770 for (R = Config.Refresh; R; R = R->next) {
38f9c547 771 if (!R->flags.ignore_reload)
772 continue;
773
e0236918 774 debugs(22, DBG_IMPORTANT, "WARNING: use of 'ignore-reload' in 'refresh_pattern' violates HTTP");
38f9c547 775
776 break;
777 }
778
26ac0430 779 for (R = Config.Refresh; R; R = R->next) {
38f9c547 780 if (!R->flags.ignore_no_store)
781 continue;
782
e0236918 783 debugs(22, DBG_IMPORTANT, "WARNING: use of 'ignore-no-store' in 'refresh_pattern' violates HTTP");
38f9c547 784
785 break;
786 }
787
26ac0430 788 for (R = Config.Refresh; R; R = R->next) {
38f9c547 789 if (!R->flags.ignore_private)
790 continue;
791
e0236918 792 debugs(22, DBG_IMPORTANT, "WARNING: use of 'ignore-private' in 'refresh_pattern' violates HTTP");
38f9c547 793
794 break;
795 }
9f60cfdf 796 }
c2f5c744 797#endif
626096be 798#if !USE_HTTP_VIOLATIONS
c2f5c744 799 Config.onoff.via = 1;
800#else
62e76326 801
c2f5c744 802 if (!Config.onoff.via)
e0236918 803 debugs(22, DBG_IMPORTANT, "WARNING: HTTP requires the use of Via");
62e76326 804
9f60cfdf 805#endif
62e76326 806
5491c11e
AR
807 // we enable runtime PURGE checks if there is at least one PURGE method ACL
808 // TODO: replace with a dedicated "purge" ACL option?
3d9019c7 809 Config2.onoff.enable_purge = (ACLMethodData::ThePurgeCount > 0);
62e76326 810
8749fa47 811 if (geteuid() == 0) {
62e76326 812 if (NULL != Config.effectiveUser) {
813
814 struct passwd *pwd = getpwnam(Config.effectiveUser);
815
5ad8d199 816 if (NULL == pwd) {
62e76326 817 /*
818 * Andres Kroonmaa <andre@online.ee>:
819 * Some getpwnam() implementations (Solaris?) require
820 * an available FD < 256 for opening a FILE* to the
821 * passwd file.
822 * DW:
823 * This should be safe at startup, but might still fail
824 * during reconfigure.
825 */
826 fatalf("getpwnam failed to find userid for effective user '%s'",
827 Config.effectiveUser);
5ad8d199 828 return;
829 }
62e76326 830
831 Config2.effectiveUserID = pwd->pw_uid;
832
833 Config2.effectiveGroupID = pwd->pw_gid;
08ac5ea7 834
cff61cb8 835#if HAVE_PUTENV
cff61cb8 836 if (pwd->pw_dir && *pwd->pw_dir) {
96b9d962
AR
837 // putenv() leaks by design; avoid leaks when nothing changes
838 static SBuf lastDir;
839 if (lastDir.isEmpty() || !lastDir.cmp(pwd->pw_dir)) {
840 lastDir = pwd->pw_dir;
841 int len = strlen(pwd->pw_dir) + 6;
842 char *env_str = (char *)xcalloc(len, 1);
843 snprintf(env_str, len, "HOME=%s", pwd->pw_dir);
844 putenv(env_str);
845 }
cff61cb8 846 }
cff61cb8 847#endif
62e76326 848 }
8749fa47 849 } else {
62e76326 850 Config2.effectiveUserID = geteuid();
851 Config2.effectiveGroupID = getegid();
d20b1cd0 852 }
62e76326 853
d20b1cd0 854 if (NULL != Config.effectiveGroup) {
62e76326 855
856 struct group *grp = getgrnam(Config.effectiveGroup);
857
5ad8d199 858 if (NULL == grp) {
62e76326 859 fatalf("getgrnam failed to find groupid for effective group '%s'",
860 Config.effectiveGroup);
5ad8d199 861 return;
862 }
62e76326 863
864 Config2.effectiveGroupID = grp->gr_gid;
d20b1cd0 865 }
62e76326 866
866be11c
CT
867#if USE_OPENSSL
868 if (Config.ssl_client.foreignIntermediateCertsPath)
869 Ssl::loadSquidUntrusted(Config.ssl_client.foreignIntermediateCertsPath);
870#endif
871
a465e144
AJ
872 if (Security::ProxyOutgoingConfig.encryptTransport) {
873 debugs(3, DBG_IMPORTANT, "Initializing https:// proxy context");
874 Config.ssl_client.sslContext = Security::ProxyOutgoingConfig.createClientContext(false);
91667ff4 875 if (!Config.ssl_client.sslContext) {
79ff8023
AJ
876#if USE_OPENSSL
877 fatal("ERROR: Could not initialize https:// proxy context");
878#else
879 debugs(3, DBG_IMPORTANT, "ERROR: proxying https:// currently still requires --with-openssl");
880#endif
91667ff4 881 }
866be11c
CT
882#if USE_OPENSSL
883 Ssl::useSquidUntrusted(Config.ssl_client.sslContext);
884#endif
a465e144 885 }
f9ad0106 886
a3c6762c 887 for (CachePeer *p = Config.peers; p != NULL; p = p->next) {
1f1f29e8
AJ
888
889 // default value for ssldomain= is the peer host/IP
890 if (p->secure.sslDomain.isEmpty())
891 p->secure.sslDomain = p->host;
892
893 if (p->secure.encryptTransport) {
91667ff4 894 debugs(3, DBG_IMPORTANT, "Initializing cache_peer " << p->name << " TLS context");
a465e144 895 p->sslContext = p->secure.createClientContext(true);
91667ff4
AJ
896 if (!p->sslContext) {
897 debugs(3, DBG_CRITICAL, "ERROR: Could not initialize cache_peer " << p->name << " TLS context");
898 self_destruct();
899 }
f9ad0106 900 }
04401ab0 901 }
f9ad0106 902
a465e144 903#if USE_OPENSSL
fa720bfb 904 for (AnyP::PortCfgPointer s = HttpPortList; s != NULL; s = s->next) {
339e4d7a 905 if (!s->secure.encryptTransport)
04401ab0 906 continue;
339e4d7a 907 debugs(3, DBG_IMPORTANT, "Initializing " << AnyP::UriScheme(s->transport.protocol) << "_port " << s->s << " TLS context");
5ae65581 908 s->configureSslServerContext();
04401ab0 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
2745fea5 1737dump_cachedir(StoreEntry * entry, const char *name, const Store::DiskConfig &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
2745fea5 1826parse_cachedir(Store::DiskConfig *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);
9825b398
AJ
2168 } else if (!strcmp(token, "auth-no-keytab")) {
2169 p->options.auth_no_keytab = 1;
a37d6070 2170 } else if (!strncmp(token, "connect-timeout=", 16)) {
62e76326 2171 p->connect_timeout = xatoi(token + 16);
a37d6070 2172 } else if (!strncmp(token, "connect-fail-limit=", 19)) {
ff9970cc 2173 p->connect_fail_limit = xatoi(token + 19);
7e3ce7b9 2174#if USE_CACHE_DIGESTS
a37d6070 2175 } else if (!strncmp(token, "digest-url=", 11)) {
62e76326 2176 p->digest_url = xstrdup(token + 11);
7e3ce7b9 2177#endif
62e76326 2178
a37d6070 2179 } else if (!strcmp(token, "allow-miss")) {
ccdf4138 2180 p->options.allow_miss = true;
a37d6070 2181 } else if (!strncmp(token, "max-conn=", 9)) {
62e76326 2182 p->max_conn = xatoi(token + 9);
e8dca475
CT
2183 } else if (!strncmp(token, "standby=", 8)) {
2184 p->standby.limit = xatoi(token + 8);
a37d6070 2185 } else if (!strcmp(token, "originserver")) {
ccdf4138 2186 p->options.originserver = true;
a37d6070 2187 } else if (!strncmp(token, "name=", 5)) {
62e76326 2188 safe_free(p->name);
2189
2190 if (token[5])
2191 p->name = xstrdup(token + 5);
a37d6070 2192 } else if (!strncmp(token, "forceddomain=", 13)) {
62e76326 2193 safe_free(p->domain);
62e76326 2194 if (token[13])
2195 p->domain = xstrdup(token + 13);
2196
cdfb670c
AJ
2197 } else if (strncmp(token, "ssl", 3) == 0) {
2198#if !USE_OPENSSL
2199 debugs(0, DBG_CRITICAL, "WARNING: cache_peer option '" << token << "' requires --with-openssl");
2200#else
0b0e0864 2201 p->secure.parse(token+3);
cdfb670c 2202#endif
1cc44095 2203 } else if (strncmp(token, "tls-", 4) == 0) {
1cc44095 2204 p->secure.parse(token+4);
62e76326 2205 } else if (strcmp(token, "front-end-https") == 0) {
2206 p->front_end_https = 1;
2207 } else if (strcmp(token, "front-end-https=on") == 0) {
2208 p->front_end_https = 1;
2209 } else if (strcmp(token, "front-end-https=auto") == 0) {
2210 p->front_end_https = 2;
26ac0430 2211 } else if (strcmp(token, "connection-auth=off") == 0) {
d67acb4e
AJ
2212 p->connection_auth = 0;
2213 } else if (strcmp(token, "connection-auth") == 0) {
2214 p->connection_auth = 1;
2215 } else if (strcmp(token, "connection-auth=on") == 0) {
2216 p->connection_auth = 1;
2217 } else if (strcmp(token, "connection-auth=auto") == 0) {
2218 p->connection_auth = 2;
9d22bac3
AJ
2219 } else if (token[0] == '#') {
2220 // start of a text comment. stop reading this line.
2221 break;
62e76326 2222 } else {
9d22bac3 2223 debugs(3, DBG_PARSE_NOTE(DBG_IMPORTANT), "ERROR: Ignoring unknown cache_peer option '" << token << "'");
62e76326 2224 }
270b86af 2225 }
62e76326 2226
be753325 2227 if (peerFindByName(p->name))
62e76326 2228 fatalf("ERROR: cache_peer %s specified twice\n", p->name);
2229
e8dca475
CT
2230 if (p->max_conn > 0 && p->max_conn < p->standby.limit)
2231 fatalf("ERROR: cache_peer %s max-conn=%d is lower than its standby=%d\n", p->host, p->max_conn, p->standby.limit);
2232
40a1495e 2233 if (p->weight < 1)
62e76326 2234 p->weight = 1;
2235
ff9970cc 2236 if (p->connect_fail_limit < 1)
a4d889e0 2237 p->connect_fail_limit = 10;
ff9970cc 2238
e13ee7ad 2239#if USE_CACHE_DIGESTS
62e76326 2240
e13ee7ad 2241 if (!p->options.no_digest) {
62e76326 2242 /* XXX This looks odd.. who has the original pointer
2243 * then?
2244 */
2245 PeerDigest *pd = peerDigestCreate(p);
2246 p->digest = cbdataReference(pd);
8a6218c6 2247 }
62e76326 2248
e13ee7ad 2249#endif
cc192b50 2250
2251 p->index = ++Config.npeers;
2252
0153d498 2253 while (*head != NULL)
62e76326 2254 head = &(*head)->next;
2255
0153d498 2256 *head = p;
62e76326 2257
32a47e3e 2258 peerClearRRStart();
0153d498 2259}
2260
2261static void
a3c6762c 2262free_peer(CachePeer ** P)
0153d498 2263{
719815a0
AJ
2264 delete *P;
2265 *P = NULL;
987c67d1 2266 Config.npeers = 0;
270b86af 2267}
2268
2269static void
613924ee 2270dump_cachemgrpasswd(StoreEntry * entry, const char *name, Mgr::ActionPasswordList * list)
270b86af 2271{
d41de3c1 2272 wordlist *w;
62e76326 2273
d41de3c1 2274 while (list != NULL) {
62e76326 2275 if (strcmp(list->passwd, "none") && strcmp(list->passwd, "disable"))
2276 storeAppendPrintf(entry, "%s XXXXXXXXXX", name);
2277 else
2278 storeAppendPrintf(entry, "%s %s", name, list->passwd);
2279
2280 for (w = list->actions; w != NULL; w = w->next) {
2281 storeAppendPrintf(entry, " %s", w->key);
2282 }
2283
2284 storeAppendPrintf(entry, "\n");
2285 list = list->next;
d41de3c1 2286 }
270b86af 2287}
2288
2289static void
613924ee 2290parse_cachemgrpasswd(Mgr::ActionPasswordList ** head)
270b86af 2291{
2292 char *passwd = NULL;
2293 wordlist *actions = NULL;
613924ee
FC
2294 Mgr::ActionPasswordList *p;
2295 Mgr::ActionPasswordList **P;
270b86af 2296 parse_string(&passwd);
2297 parse_wordlist(&actions);
613924ee 2298 p = new Mgr::ActionPasswordList;
22f3fd98 2299 p->passwd = passwd;
2300 p->actions = actions;
62e76326 2301
26aa7e31 2302 for (P = head; *P; P = &(*P)->next) {
62e76326 2303 /*
2304 * See if any of the actions from this line already have a
2305 * password from previous lines. The password checking
2306 * routines in cache_manager.c take the the password from
613924ee 2307 * the first Mgr::ActionPasswordList that contains the
62e76326 2308 * requested action. Thus, we should warn users who might
2309 * think they can have two passwords for the same action.
2310 */
2311 wordlist *w;
2312 wordlist *u;
2313
2314 for (w = (*P)->actions; w; w = w->next) {
2315 for (u = actions; u; u = u->next) {
2316 if (strcmp(w->key, u->key))
2317 continue;
2318
fa84c01d 2319 debugs(0, DBG_CRITICAL, "WARNING: action '" << u->key << "' (line " << config_lineno << ") already has a password");
62e76326 2320 }
2321 }
26aa7e31 2322 }
62e76326 2323
22f3fd98 2324 *P = p;
270b86af 2325}
2326
2327static void
613924ee 2328free_cachemgrpasswd(Mgr::ActionPasswordList ** head)
270b86af 2329{
613924ee 2330 Mgr::ActionPasswordList *p;
62e76326 2331
79d39a72 2332 while ((p = *head) != NULL) {
62e76326 2333 *head = p->next;
2334 xfree(p->passwd);
2335 wordlistDestroy(&p->actions);
2336 xfree(p);
a47b9029 2337 }
270b86af 2338}
2339
8203a132 2340static void
7f0b3324 2341dump_denyinfo(StoreEntry * entry, const char *name, AclDenyInfoList * var)
270b86af 2342{
6be70545 2343 AclNameList *a;
62e76326 2344
d41de3c1 2345 while (var != NULL) {
62e76326 2346 storeAppendPrintf(entry, "%s %s", name, var->err_page_name);
2347
2348 for (a = var->acl_list; a != NULL; a = a->next)
2349 storeAppendPrintf(entry, " %s", a->name);
2350
2351 storeAppendPrintf(entry, "\n");
2352
2353 var = var->next;
d41de3c1 2354 }
270b86af 2355}
2356
2357static void
7f0b3324 2358parse_denyinfo(AclDenyInfoList ** var)
6e40f263 2359{
f1dc9b30 2360 aclParseDenyInfoLine(var);
6e40f263 2361}
403279e0 2362
1273d501 2363void
7f0b3324 2364free_denyinfo(AclDenyInfoList ** list)
3c5557f9 2365{
7f0b3324
FC
2366 AclDenyInfoList *a = NULL;
2367 AclDenyInfoList *a_next = NULL;
6be70545
FC
2368 AclNameList *l = NULL;
2369 AclNameList *l_next = NULL;
62e76326 2370
1273d501 2371 for (a = *list; a; a = a_next) {
62e76326 2372 for (l = a->acl_list; l; l = l_next) {
2373 l_next = l->next;
2374 memFree(l, MEM_ACL_NAME_LIST);
2375 l = NULL;
2376 }
2377
2378 a_next = a->next;
2379 memFree(a, MEM_ACL_DENY_INFO_LIST);
2380 a = NULL;
1273d501 2381 }
62e76326 2382
1273d501 2383 *list = NULL;
270b86af 2384}
2385
2386static void
505e35db 2387parse_peer_access(void)
270b86af 2388{
2389 char *host = NULL;
a3c6762c 2390 CachePeer *p;
62e76326 2391
2eceb328 2392 if (!(host = ConfigParser::NextToken()))
62e76326 2393 self_destruct();
2394
0cdcddb9 2395 if ((p = peerFindByName(host)) == NULL) {
fa84c01d 2396 debugs(15, DBG_CRITICAL, "" << cfg_filename << ", line " << config_lineno << ": No cache_peer '" << host << "'");
62e76326 2397 return;
0cdcddb9 2398 }
62e76326 2399
6f58d7d7
AR
2400 std::string directive = "peer_access ";
2401 directive += host;
2402 aclParseAccessLine(directive.c_str(), LegacyParser, &p->access);
270b86af 2403}
2404
270b86af 2405static void
2406parse_hostdomaintype(void)
2407{
2408 char *host = NULL;
2409 char *type = NULL;
2410 char *domain = NULL;
62e76326 2411
2eceb328 2412 if (!(host = ConfigParser::NextToken()))
62e76326 2413 self_destruct();
2414
2eceb328 2415 if (!(type = ConfigParser::NextToken()))
62e76326 2416 self_destruct();
2417
2eceb328 2418 while ((domain = ConfigParser::NextToken())) {
5844d003
FC
2419 NeighborTypeDomainList *l = NULL;
2420 NeighborTypeDomainList **L = NULL;
a3c6762c 2421 CachePeer *p;
62e76326 2422
2423 if ((p = peerFindByName(host)) == NULL) {
fa84c01d 2424 debugs(15, DBG_CRITICAL, "" << cfg_filename << ", line " << config_lineno << ": No cache_peer '" << host << "'");
62e76326 2425 return;
2426 }
2427
5844d003 2428 l = static_cast<NeighborTypeDomainList *>(xcalloc(1, sizeof(NeighborTypeDomainList)));
62e76326 2429 l->type = parseNeighborType(type);
2430 l->domain = xstrdup(domain);
2431
3d0ac046 2432 for (L = &(p->typelist); *L; L = &((*L)->next));
62e76326 2433 *L = l;
f1dc9b30 2434 }
270b86af 2435}
2436
270b86af 2437static void
a7d59104 2438dump_int(StoreEntry * entry, const char *name, int var)
270b86af 2439{
f53b06f9 2440 storeAppendPrintf(entry, "%s %d\n", name, var);
270b86af 2441}
c1c29eb6 2442
94439e4e 2443void
270b86af 2444parse_int(int *var)
2445{
270b86af 2446 int i;
0e4e0e7d 2447 i = GetInteger();
270b86af 2448 *var = i;
2449}
090089c4 2450
0153d498 2451static void
2452free_int(int *var)
2453{
a47b9029 2454 *var = 0;
0153d498 2455}
2456
8f7dbf74
DD
2457static void
2458dump_int64_t(StoreEntry * entry, const char *name, int64_t var)
2459{
2460 storeAppendPrintf(entry, "%s %" PRId64 "\n", name, var);
2461}
2462
2463void
2464parse_int64_t(int64_t *var)
2465{
2466 int64_t i;
2467 i = GetInteger64();
2468 *var = i;
2469}
2470
2471static void
2472free_int64_t(int64_t *var)
2473{
2474 *var = 0;
2475}
2476
270b86af 2477static void
a7d59104 2478dump_onoff(StoreEntry * entry, const char *name, int var)
270b86af 2479{
f53b06f9 2480 storeAppendPrintf(entry, "%s %s\n", name, var ? "on" : "off");
270b86af 2481}
090089c4 2482
d205783b 2483void
270b86af 2484parse_onoff(int *var)
2485{
2eceb328 2486 char *token = ConfigParser::NextToken();
090089c4 2487
270b86af 2488 if (token == NULL)
62e76326 2489 self_destruct();
2490
a37d6070 2491 if (!strcmp(token, "on")) {
62e76326 2492 *var = 1;
a37d6070 2493 } else if (!strcmp(token, "enable")) {
54a063a2
TX
2494 debugs(0, DBG_PARSE_NOTE(DBG_IMPORTANT), "WARNING: 'enable' is deprecated. Please update to use 'on'.");
2495 *var = 1;
a37d6070 2496 } else if (!strcmp(token, "off")) {
62e76326 2497 *var = 0;
a37d6070 2498 } else if (!strcmp(token, "disable")) {
54a063a2
TX
2499 debugs(0, DBG_PARSE_NOTE(DBG_IMPORTANT), "WARNING: 'disable' is deprecated. Please update to use 'off'.");
2500 *var = 0;
2501 } else {
2502 debugs(0, DBG_PARSE_NOTE(DBG_IMPORTANT), "ERROR: Invalid option: Boolean options can only be 'on' or 'off'.");
2503 self_destruct();
2504 }
270b86af 2505}
e90100aa 2506
0153d498 2507#define free_onoff free_int
52d3f198 2508
2509static void
2510dump_tristate(StoreEntry * entry, const char *name, int var)
2511{
2512 const char *state;
2513
2514 if (var > 0)
2515 state = "on";
2516 else if (var < 0)
2517 state = "warn";
2518 else
2519 state = "off";
2520
2521 storeAppendPrintf(entry, "%s %s\n", name, state);
2522}
2523
2524static void
2525parse_tristate(int *var)
2526{
2eceb328 2527 char *token = ConfigParser::NextToken();
52d3f198 2528
2529 if (token == NULL)
2530 self_destruct();
2531
a37d6070 2532 if (!strcmp(token, "on")) {
54a063a2 2533 *var = 1;
a37d6070 2534 } else if (!strcmp(token, "enable")) {
54a063a2 2535 debugs(0, DBG_PARSE_NOTE(DBG_IMPORTANT), "WARNING: 'enable' is deprecated. Please update to use value 'on'.");
52d3f198 2536 *var = 1;
a37d6070 2537 } else if (!strcmp(token, "warn")) {
52d3f198 2538 *var = -1;
a37d6070 2539 } else if (!strcmp(token, "off")) {
52d3f198 2540 *var = 0;
a37d6070 2541 } else if (!strcmp(token, "disable")) {
54a063a2
TX
2542 debugs(0, DBG_PARSE_NOTE(DBG_IMPORTANT), "WARNING: 'disable' is deprecated. Please update to use value 'off'.");
2543 *var = 0;
2544 } else {
2545 debugs(0, DBG_PARSE_NOTE(DBG_IMPORTANT), "ERROR: Invalid option: Tristate options can only be 'on', 'off', or 'warn'.");
2546 self_destruct();
2547 }
52d3f198 2548}
2549
2550#define free_tristate free_int
30a4f2a8 2551
079a8480
AJ
2552void
2553parse_pipelinePrefetch(int *var)
2554{
bde7a8ce 2555 char *token = ConfigParser::PeekAtToken();
079a8480
AJ
2556
2557 if (token == NULL)
2558 self_destruct();
2559
2560 if (!strcmp(token, "on")) {
2561 debugs(0, DBG_PARSE_NOTE(DBG_IMPORTANT), "WARNING: 'pipeline_prefetch on' is deprecated. Please update to use 1 (or a higher number).");
2562 *var = 1;
bde7a8ce
CT
2563 //pop the token
2564 (void)ConfigParser::NextToken();
079a8480
AJ
2565 } else if (!strcmp(token, "off")) {
2566 debugs(0, DBG_PARSE_NOTE(2), "WARNING: 'pipeline_prefetch off' is deprecated. Please update to use '0'.");
2567 *var = 0;
bde7a8ce
CT
2568 //pop the token
2569 (void)ConfigParser::NextToken();
2570 } else
079a8480 2571 parse_int(var);
079a8480
AJ
2572}
2573
2574#define free_pipelinePrefetch free_int
2575#define dump_pipelinePrefetch dump_int
2576
270b86af 2577static void
8d9a8184 2578dump_refreshpattern(StoreEntry * entry, const char *name, RefreshPattern * head)
270b86af 2579{
d41de3c1 2580 while (head != NULL) {
0e1d7629 2581 storeAppendPrintf(entry, "%s%s %s %d %d%% %d",
62e76326 2582 name,
95b8eae2
AJ
2583 head->pattern.flags&REG_ICASE ? " -i" : null_string,
2584 head->pattern.c_str(),
62e76326 2585 (int) head->min / 60,
2586 (int) (100.0 * head->pct + 0.5),
2587 (int) head->max / 60);
4c3ef9b2 2588
570d3f75
AJ
2589 if (head->max_stale >= 0)
2590 storeAppendPrintf(entry, " max-stale=%d", head->max_stale);
2591
4c3ef9b2 2592 if (head->flags.refresh_ims)
2593 storeAppendPrintf(entry, " refresh-ims");
2594
3d8b6ba4
AJ
2595 if (head->flags.store_stale)
2596 storeAppendPrintf(entry, " store-stale");
2597
626096be 2598#if USE_HTTP_VIOLATIONS
62e76326 2599
2600 if (head->flags.override_expire)
2601 storeAppendPrintf(entry, " override-expire");
2602
2603 if (head->flags.override_lastmod)
2604 storeAppendPrintf(entry, " override-lastmod");
2605
2606 if (head->flags.reload_into_ims)
2607 storeAppendPrintf(entry, " reload-into-ims");
2608
2609 if (head->flags.ignore_reload)
2610 storeAppendPrintf(entry, " ignore-reload");
2611
38f9c547 2612 if (head->flags.ignore_no_store)
2613 storeAppendPrintf(entry, " ignore-no-store");
2614
2615 if (head->flags.ignore_private)
2616 storeAppendPrintf(entry, " ignore-private");
9f60cfdf 2617#endif
62e76326 2618
2619 storeAppendPrintf(entry, "\n");
2620
2621 head = head->next;
d41de3c1 2622 }
270b86af 2623}
090089c4 2624
270b86af 2625static void
8d9a8184 2626parse_refreshpattern(RefreshPattern ** head)
270b86af 2627{
f1dc9b30 2628 char *token;
2629 char *pattern;
2630 time_t min = 0;
c3f6d204 2631 double pct = 0.0;
f1dc9b30 2632 time_t max = 0;
4c3ef9b2 2633 int refresh_ims = 0;
3d8b6ba4 2634 int store_stale = 0;
570d3f75 2635 int max_stale = -1;
3d8b6ba4 2636
626096be 2637#if USE_HTTP_VIOLATIONS
62e76326 2638
1dfa1d81 2639 int override_expire = 0;
2640 int override_lastmod = 0;
cbe3a719 2641 int reload_into_ims = 0;
2642 int ignore_reload = 0;
38f9c547 2643 int ignore_no_store = 0;
2644 int ignore_private = 0;
9f60cfdf 2645#endif
62e76326 2646
f1dc9b30 2647 int i;
8d9a8184 2648 RefreshPattern *t;
f1dc9b30 2649 regex_t comp;
2650 int errcode;
2651 int flags = REG_EXTENDED | REG_NOSUB;
62e76326 2652
bde7a8ce 2653 if ((token = ConfigParser::RegexPattern()) != NULL) {
2f3c75c3
AJ
2654
2655 if (strcmp(token, "-i") == 0) {
2656 flags |= REG_ICASE;
bde7a8ce 2657 token = ConfigParser::RegexPattern();
2f3c75c3
AJ
2658 } else if (strcmp(token, "+i") == 0) {
2659 flags &= ~REG_ICASE;
bde7a8ce 2660 token = ConfigParser::RegexPattern();
2f3c75c3 2661 }
62e76326 2662
f1dc9b30 2663 }
62e76326 2664
9eeb8e4b 2665 if (token == NULL) {
2f3c75c3 2666 debugs(3, DBG_CRITICAL, "FATAL: refresh_pattern missing the regex pattern parameter");
62e76326 2667 self_destruct();
9eeb8e4b 2668 return;
2669 }
62e76326 2670
f1dc9b30 2671 pattern = xstrdup(token);
62e76326 2672
f53969cc 2673 i = GetInteger(); /* token: min */
62e76326 2674
a632ba71
AJ
2675 /* catch negative and insanely huge values close to 32-bit wrap */
2676 if (i < 0) {
2677 debugs(3, DBG_IMPORTANT, "WARNING: refresh_pattern minimum age negative. Cropped back to zero.");
2678 i = 0;
2679 }
2680 if (i > 60*24*365) {
2681 debugs(3, DBG_IMPORTANT, "WARNING: refresh_pattern minimum age too high. Cropped back to 1 year.");
2682 i = 60*24*365;
2683 }
2684
f53969cc 2685 min = (time_t) (i * 60); /* convert minutes to seconds */
62e76326 2686
1454480a 2687 pct = GetPercentage(false); /* token: pct . with no limit on size */
62e76326 2688
f53969cc 2689 i = GetInteger(); /* token: max */
62e76326 2690
a632ba71
AJ
2691 /* catch negative and insanely huge values close to 32-bit wrap */
2692 if (i < 0) {
2693 debugs(3, DBG_IMPORTANT, "WARNING: refresh_pattern maximum age negative. Cropped back to zero.");
2694 i = 0;
2695 }
2696 if (i > 60*24*365) {
2697 debugs(3, DBG_IMPORTANT, "WARNING: refresh_pattern maximum age too high. Cropped back to 1 year.");
2698 i = 60*24*365;
2699 }
2700
f53969cc 2701 max = (time_t) (i * 60); /* convert minutes to seconds */
62e76326 2702
1dfa1d81 2703 /* Options */
2eceb328 2704 while ((token = ConfigParser::NextToken()) != NULL) {
4c3ef9b2 2705 if (!strcmp(token, "refresh-ims")) {
2706 refresh_ims = 1;
3d8b6ba4
AJ
2707 } else if (!strcmp(token, "store-stale")) {
2708 store_stale = 1;
570d3f75 2709 } else if (!strncmp(token, "max-stale=", 10)) {
54a063a2 2710 max_stale = xatoi(token + 10);
064679ea 2711
626096be 2712#if USE_HTTP_VIOLATIONS
62e76326 2713
4c3ef9b2 2714 } else if (!strcmp(token, "override-expire"))
62e76326 2715 override_expire = 1;
2716 else if (!strcmp(token, "override-lastmod"))
2717 override_lastmod = 1;
38f9c547 2718 else if (!strcmp(token, "ignore-no-store"))
2719 ignore_no_store = 1;
2720 else if (!strcmp(token, "ignore-private"))
2721 ignore_private = 1;
62e76326 2722 else if (!strcmp(token, "reload-into-ims")) {
2723 reload_into_ims = 1;
2724 refresh_nocache_hack = 1;
2725 /* tell client_side.c that this is used */
2726 } else if (!strcmp(token, "ignore-reload")) {
2727 ignore_reload = 1;
2728 refresh_nocache_hack = 1;
2729 /* tell client_side.c that this is used */
9f60cfdf 2730#endif
62e76326 2731
064679ea
AJ
2732 } else if (!strcmp(token, "ignore-no-cache") ||
2733 !strcmp(token, "ignore-must-revalidate") ||
2734 !strcmp(token, "ignore-auth")
7766dad3 2735 ) {
064679ea 2736 debugs(22, DBG_PARSE_NOTE(2), "UPGRADE: refresh_pattern option '" << token << "' is obsolete. Remove it.");
4c3ef9b2 2737 } else
fa84c01d 2738 debugs(22, DBG_CRITICAL, "refreshAddToList: Unknown option '" << pattern << "': " << token);
1dfa1d81 2739 }
62e76326 2740
f1dc9b30 2741 if ((errcode = regcomp(&comp, pattern, flags)) != 0) {
62e76326 2742 char errbuf[256];
2743 regerror(errcode, &comp, errbuf, sizeof errbuf);
fa84c01d
FC
2744 debugs(22, DBG_CRITICAL, "" << cfg_filename << " line " << config_lineno << ": " << config_input_line);
2745 debugs(22, DBG_CRITICAL, "refreshAddToList: Invalid regular expression '" << pattern << "': " << errbuf);
eeaa522f 2746 xfree(pattern);
62e76326 2747 return;
f1dc9b30 2748 }
62e76326 2749
c3f6d204 2750 pct = pct < 0.0 ? 0.0 : pct;
f1dc9b30 2751 max = max < 0 ? 0 : max;
95b8eae2
AJ
2752 t = new RefreshPattern(pattern, flags);
2753 t->pattern.regex = comp;
f1dc9b30 2754 t->min = min;
c3f6d204 2755 t->pct = pct;
f1dc9b30 2756 t->max = max;
62e76326 2757
4c3ef9b2 2758 if (refresh_ims)
be4d35dc 2759 t->flags.refresh_ims = true;
4c3ef9b2 2760
3d8b6ba4 2761 if (store_stale)
be4d35dc 2762 t->flags.store_stale = true;
3d8b6ba4 2763
570d3f75
AJ
2764 t->max_stale = max_stale;
2765
626096be 2766#if USE_HTTP_VIOLATIONS
62e76326 2767
1dfa1d81 2768 if (override_expire)
be4d35dc 2769 t->flags.override_expire = true;
62e76326 2770
1dfa1d81 2771 if (override_lastmod)
be4d35dc 2772 t->flags.override_lastmod = true;
62e76326 2773
cbe3a719 2774 if (reload_into_ims)
be4d35dc 2775 t->flags.reload_into_ims = true;
62e76326 2776
cbe3a719 2777 if (ignore_reload)
be4d35dc 2778 t->flags.ignore_reload = true;
62e76326 2779
38f9c547 2780 if (ignore_no_store)
be4d35dc 2781 t->flags.ignore_no_store = true;
38f9c547 2782
2783 if (ignore_private)
be4d35dc 2784 t->flags.ignore_private = true;
9f60cfdf 2785#endif
62e76326 2786
f1dc9b30 2787 t->next = NULL;
62e76326 2788
f1dc9b30 2789 while (*head)
62e76326 2790 head = &(*head)->next;
2791
f1dc9b30 2792 *head = t;
62e76326 2793
95b8eae2 2794 xfree(pattern);
270b86af 2795}
090089c4 2796
270b86af 2797static void
8d9a8184 2798free_refreshpattern(RefreshPattern ** head)
270b86af 2799{
95b8eae2
AJ
2800 delete *head;
2801 *head = nullptr;
c2066637 2802
626096be 2803#if USE_HTTP_VIOLATIONS
c2066637 2804 refresh_nocache_hack = 0;
8970d351 2805
2806#endif
270b86af 2807}
12b9e9b1 2808
270b86af 2809static void
a7d59104 2810dump_string(StoreEntry * entry, const char *name, char *var)
270b86af 2811{
f53b06f9 2812 if (var != NULL)
62e76326 2813 storeAppendPrintf(entry, "%s %s\n", name, var);
270b86af 2814}
98ffb7e4 2815
270b86af 2816static void
0153d498 2817parse_string(char **var)
270b86af 2818{
2eceb328 2819 char *token = ConfigParser::NextToken();
270b86af 2820 safe_free(*var);
62e76326 2821
270b86af 2822 if (token == NULL)
62e76326 2823 self_destruct();
2824
270b86af 2825 *var = xstrdup(token);
2826}
b15e6857 2827
0153d498 2828static void
2829free_string(char **var)
2830{
027acbaf 2831 safe_free(*var);
0153d498 2832}
caebbe00 2833
94439e4e 2834void
f1dc9b30 2835parse_eol(char *volatile *var)
270b86af 2836{
65657c1a
AJ
2837 if (!var) {
2838 self_destruct();
2839 return;
2840 }
2841
2eceb328 2842 unsigned char *token = (unsigned char *) ConfigParser::NextQuotedOrToEol();
270b86af 2843 safe_free(*var);
62e76326 2844
9eeb8e4b 2845 if (!token) {
62e76326 2846 self_destruct();
9eeb8e4b 2847 return;
2848 }
62e76326 2849
e4755e29 2850 while (*token && xisspace(*token))
a38ec4b1 2851 ++token;
62e76326 2852
9eeb8e4b 2853 if (!*token) {
62e76326 2854 self_destruct();
9eeb8e4b 2855 return;
2856 }
62e76326 2857
852751f7 2858 *var = xstrdup((char *) token);
270b86af 2859}
090089c4 2860
52d3f198 2861#define dump_eol dump_string
2862#define free_eol free_string
2863
b11724bb
CT
2864static void
2865parse_TokenOrQuotedString(char **var)
2866{
2867 char *token = ConfigParser::NextQuotedToken();
2868 safe_free(*var);
2869
2870 if (token == NULL)
2871 self_destruct();
2872
2873 *var = xstrdup(token);
2874}
2875
2876#define dump_TokenOrQuotedString dump_string
2877#define free_TokenOrQuotedString free_string
2878
270b86af 2879static void
a7d59104 2880dump_time_t(StoreEntry * entry, const char *name, time_t var)
090089c4 2881{
f53b06f9 2882 storeAppendPrintf(entry, "%s %d seconds\n", name, (int) var);
090089c4 2883}
2884
94439e4e 2885void
a47b9029 2886parse_time_t(time_t * var)
0ffd22bc 2887{
fd0f51c4 2888 time_msec_t tval;
9b741834 2889 parseTimeLine(&tval, T_SECOND_STR, false);
fd0f51c4 2890 *var = static_cast<time_t>(tval/1000);
0ffd22bc 2891}
2892
270b86af 2893static void
a47b9029 2894free_time_t(time_t * var)
270b86af 2895{
a47b9029 2896 *var = 0;
270b86af 2897}
9906e724 2898
fd0f51c4
CT
2899static void
2900dump_time_msec(StoreEntry * entry, const char *name, time_msec_t var)
2901{
2902 if (var % 1000)
c91ca3ce 2903 storeAppendPrintf(entry, "%s %" PRId64 " milliseconds\n", name, var);
fd0f51c4
CT
2904 else
2905 storeAppendPrintf(entry, "%s %d seconds\n", name, (int)(var/1000) );
2906}
2907
2908void
2909parse_time_msec(time_msec_t * var)
2910{
9b741834 2911 parseTimeLine(var, T_SECOND_STR, true);
fd0f51c4
CT
2912}
2913
2914static void
2915free_time_msec(time_msec_t * var)
2916{
2917 *var = 0;
2918}
2919
0477a072 2920#if UNUSED_CODE
9906e724 2921static void
a7d59104 2922dump_size_t(StoreEntry * entry, const char *name, size_t var)
1b635117 2923{
f53b06f9 2924 storeAppendPrintf(entry, "%s %d\n", name, (int) var);
1b635117 2925}
0477a072 2926#endif
1b635117 2927
2928static void
a7d59104 2929dump_b_size_t(StoreEntry * entry, const char *name, size_t var)
9906e724 2930{
f53b06f9 2931 storeAppendPrintf(entry, "%s %d %s\n", name, (int) var, B_BYTES_STR);
9906e724 2932}
2933
e210930b
AJ
2934static void
2935dump_b_ssize_t(StoreEntry * entry, const char *name, ssize_t var)
2936{
2937 storeAppendPrintf(entry, "%s %d %s\n", name, (int) var, B_BYTES_STR);
2938}
e210930b 2939
3e62bd58 2940#if UNUSED_CODE
9906e724 2941static void
a7d59104 2942dump_kb_size_t(StoreEntry * entry, const char *name, size_t var)
9906e724 2943{
f53b06f9 2944 storeAppendPrintf(entry, "%s %d %s\n", name, (int) var, B_KBYTES_STR);
9906e724 2945}
3e62bd58 2946#endif
9906e724 2947
47f6e231 2948static void
2949dump_b_int64_t(StoreEntry * entry, const char *name, int64_t var)
2950{
c91ca3ce 2951 storeAppendPrintf(entry, "%s %" PRId64 " %s\n", name, var, B_BYTES_STR);
47f6e231 2952}
2953
2954static void
2955dump_kb_int64_t(StoreEntry * entry, const char *name, int64_t var)
2956{
c91ca3ce 2957 storeAppendPrintf(entry, "%s %" PRId64 " %s\n", name, var, B_KBYTES_STR);
47f6e231 2958}
2959
0477a072 2960#if UNUSED_CODE
9906e724 2961static void
a47b9029 2962parse_size_t(size_t * var)
1b635117 2963{
1b635117 2964 int i;
0e4e0e7d 2965 i = GetInteger();
1b635117 2966 *var = (size_t) i;
2967}
0477a072 2968#endif
1b635117 2969
2970static void
2971parse_b_size_t(size_t * var)
9906e724 2972{
2973 parseBytesLine(var, B_BYTES_STR);
2974}
2975
e210930b
AJ
2976static void
2977parse_b_ssize_t(ssize_t * var)
2978{
2979 parseBytesLineSigned(var, B_BYTES_STR);
2980}
e210930b 2981
3e62bd58 2982#if UNUSED_CODE
9906e724 2983static void
a47b9029 2984parse_kb_size_t(size_t * var)
9906e724 2985{
2986 parseBytesLine(var, B_KBYTES_STR);
2987}
3e62bd58 2988#endif
9906e724 2989
47f6e231 2990static void
2991parse_b_int64_t(int64_t * var)
2992{
2993 parseBytesLine64(var, B_BYTES_STR);
2994}
2995
2996static void
2997parse_kb_int64_t(int64_t * var)
2998{
2999 parseBytesLine64(var, B_KBYTES_STR);
3000}
3001
9906e724 3002static void
a47b9029 3003free_size_t(size_t * var)
9906e724 3004{
a47b9029 3005 *var = 0;
9906e724 3006}
3007
e210930b
AJ
3008static void
3009free_ssize_t(ssize_t * var)
3010{
3011 *var = 0;
3012}
e210930b 3013
47f6e231 3014static void
3015free_b_int64_t(int64_t * var)
3016{
3017 *var = 0;
3018}
3019
1b635117 3020#define free_b_size_t free_size_t
e210930b 3021#define free_b_ssize_t free_ssize_t
9906e724 3022#define free_kb_size_t free_size_t
3023#define free_mb_size_t free_size_t
3024#define free_gb_size_t free_size_t
47f6e231 3025#define free_kb_int64_t free_b_int64_t
090089c4 3026
8203a132 3027static void
f45dd259 3028dump_u_short(StoreEntry * entry, const char *name, unsigned short var)
090089c4 3029{
f53b06f9 3030 storeAppendPrintf(entry, "%s %d\n", name, var);
270b86af 3031}
090089c4 3032
0153d498 3033static void
f45dd259 3034free_u_short(unsigned short * u)
0153d498 3035{
3036 *u = 0;
3037}
3038
270b86af 3039static void
f45dd259 3040parse_u_short(unsigned short * var)
b67e2c8c 3041{
3042 ConfigParser::ParseUShort(var);
3043}
3044
3045void
f45dd259 3046ConfigParser::ParseUShort(unsigned short *var)
270b86af 3047{
0e656b69 3048 *var = GetShort();
090089c4 3049}
3050
3a69ddf3 3051void
3052ConfigParser::ParseBool(bool *var)
3053{
3054 int i = GetInteger();
3055
3056 if (0 == i)
3057 *var = false;
3058 else if (1 == i)
3059 *var = true;
3060 else
3061 self_destruct();
3062}
3063
270b86af 3064static void
a7d59104 3065dump_wordlist(StoreEntry * entry, const char *name, wordlist * list)
270b86af 3066{
270b86af 3067 while (list != NULL) {
62e76326 3068 storeAppendPrintf(entry, "%s %s\n", name, list->key);
3069 list = list->next;
429fdbec 3070 }
429fdbec 3071}
3072
3a69ddf3 3073void
3074ConfigParser::ParseWordList(wordlist ** list)
3075{
3076 parse_wordlist(list);
3077}
3078
94439e4e 3079void
270b86af 3080parse_wordlist(wordlist ** list)
429fdbec 3081{
270b86af 3082 char *token;
bde7a8ce 3083 while ((token = ConfigParser::NextQuotedToken()))
62e76326 3084 wordlistAdd(list, token);
429fdbec 3085}
270b86af 3086
b3d8a9e8 3087#if 0 /* now unused */
f8d9f54a 3088static int
5da06f20 3089check_null_wordlist(wordlist * w)
f8d9f54a 3090{
3091 return w == NULL;
3092}
b3d8a9e8 3093#endif
f8d9f54a 3094
63e9d884 3095static int
c6d5b87b 3096check_null_acl_access(acl_access * a)
63e9d884 3097{
3098 return a == NULL;
3099}
3100
0153d498 3101#define free_wordlist wordlistDestroy
270b86af 3102
d548ee64 3103#define free_uri_whitespace free_int
3104
3105static void
3106parse_uri_whitespace(int *var)
3107{
2eceb328 3108 char *token = ConfigParser::NextToken();
62e76326 3109
d548ee64 3110 if (token == NULL)
62e76326 3111 self_destruct();
3112
a37d6070 3113 if (!strcmp(token, "strip"))
62e76326 3114 *var = URI_WHITESPACE_STRIP;
a37d6070 3115 else if (!strcmp(token, "deny"))
62e76326 3116 *var = URI_WHITESPACE_DENY;
a37d6070 3117 else if (!strcmp(token, "allow"))
62e76326 3118 *var = URI_WHITESPACE_ALLOW;
a37d6070 3119 else if (!strcmp(token, "encode"))
62e76326 3120 *var = URI_WHITESPACE_ENCODE;
a37d6070 3121 else if (!strcmp(token, "chop"))
62e76326 3122 *var = URI_WHITESPACE_CHOP;
54a063a2
TX
3123 else {
3124 debugs(0, DBG_PARSE_NOTE(2), "ERROR: Invalid option '" << token << "': 'uri_whitespace' accepts 'strip', 'deny', 'allow', 'encode', and 'chop'.");
62e76326 3125 self_destruct();
54a063a2 3126 }
d548ee64 3127}
3128
d548ee64 3129static void
3130dump_uri_whitespace(StoreEntry * entry, const char *name, int var)
3131{
c193c972 3132 const char *s;
62e76326 3133
d548ee64 3134 if (var == URI_WHITESPACE_ALLOW)
62e76326 3135 s = "allow";
d548ee64 3136 else if (var == URI_WHITESPACE_ENCODE)
62e76326 3137 s = "encode";
d548ee64 3138 else if (var == URI_WHITESPACE_CHOP)
62e76326 3139 s = "chop";
7e3ce7b9 3140 else if (var == URI_WHITESPACE_DENY)
62e76326 3141 s = "deny";
7e3ce7b9 3142 else
62e76326 3143 s = "strip";
3144
d548ee64 3145 storeAppendPrintf(entry, "%s %s\n", name, s);
3146}
3147
6a566b9c 3148static void
c1dd71ae 3149free_removalpolicy(RemovalPolicySettings ** settings)
6a566b9c 3150{
3151 if (!*settings)
62e76326 3152 return;
3153
6a566b9c 3154 free_string(&(*settings)->type);
62e76326 3155
6a566b9c 3156 free_wordlist(&(*settings)->args);
62e76326 3157
c8f4eac4 3158 delete *settings;
62e76326 3159
6a566b9c 3160 *settings = NULL;
3161}
3162
3163static void
c1dd71ae 3164parse_removalpolicy(RemovalPolicySettings ** settings)
6a566b9c 3165{
3166 if (*settings)
62e76326 3167 free_removalpolicy(settings);
3168
c8f4eac4 3169 *settings = new RemovalPolicySettings;
62e76326 3170
6a566b9c 3171 parse_string(&(*settings)->type);
62e76326 3172
6a566b9c 3173 parse_wordlist(&(*settings)->args);
3174}
3175
3176static void
c1dd71ae 3177dump_removalpolicy(StoreEntry * entry, const char *name, RemovalPolicySettings * settings)
6a566b9c 3178{
3179 wordlist *args;
3180 storeAppendPrintf(entry, "%s %s", name, settings->type);
3181 args = settings->args;
62e76326 3182
6a566b9c 3183 while (args) {
62e76326 3184 storeAppendPrintf(entry, " %s", args->key);
3185 args = args->next;
6a566b9c 3186 }
62e76326 3187
be58afb5 3188 storeAppendPrintf(entry, "\n");
6a566b9c 3189}
c1dd71ae 3190
de0cb3d8
AR
3191inline void
3192free_YesNoNone(YesNoNone *)
ced8def3 3193{}
57af1e3f
AR
3194
3195static void
3196parse_YesNoNone(YesNoNone *option)
3197{
3198 int value = 0;
3199 parse_onoff(&value);
3200 option->configure(value > 0);
3201}
3202
3203static void
3204dump_YesNoNone(StoreEntry * entry, const char *name, YesNoNone &option)
3205{
3206 if (option.configured())
3207 dump_onoff(entry, name, option ? 1 : 0);
3208}
3209
ea21d497 3210static void
ced8def3
AJ
3211free_memcachemode(SquidConfig *)
3212{}
ea21d497
HN
3213
3214static void
ced8def3 3215parse_memcachemode(SquidConfig *)
ea21d497 3216{
2eceb328 3217 char *token = ConfigParser::NextToken();
ea21d497 3218 if (!token)
e1381638 3219 self_destruct();
ea21d497 3220
10aeba1d 3221 if (strcmp(token, "always") == 0) {
e1381638
AJ
3222 Config.onoff.memory_cache_first = 1;
3223 Config.onoff.memory_cache_disk = 1;
10aeba1d 3224 } else if (strcmp(token, "disk") == 0) {
e1381638
AJ
3225 Config.onoff.memory_cache_first = 0;
3226 Config.onoff.memory_cache_disk = 1;
ea21d497 3227 } else if (strncmp(token, "net", 3) == 0) {
e1381638
AJ
3228 Config.onoff.memory_cache_first = 1;
3229 Config.onoff.memory_cache_disk = 0;
10aeba1d 3230 } else if (strcmp(token, "never") == 0) {
e1381638
AJ
3231 Config.onoff.memory_cache_first = 0;
3232 Config.onoff.memory_cache_disk = 0;
54a063a2
TX
3233 } else {
3234 debugs(0, DBG_PARSE_NOTE(2), "ERROR: Invalid option '" << token << "': 'memory_cache_mode' accepts 'always', 'disk', 'network', and 'never'.");
e1381638 3235 self_destruct();
54a063a2 3236 }
ea21d497
HN
3237}
3238
3239static void
ced8def3 3240dump_memcachemode(StoreEntry * entry, const char *name, SquidConfig &)
ea21d497
HN
3241{
3242 storeAppendPrintf(entry, "%s ", name);
3243 if (Config.onoff.memory_cache_first && Config.onoff.memory_cache_disk)
e1381638 3244 storeAppendPrintf(entry, "always");
ea21d497 3245 else if (!Config.onoff.memory_cache_first && Config.onoff.memory_cache_disk)
e1381638 3246 storeAppendPrintf(entry, "disk");
ea21d497 3247 else if (Config.onoff.memory_cache_first && !Config.onoff.memory_cache_disk)
e1381638 3248 storeAppendPrintf(entry, "network");
ea21d497 3249 else if (!Config.onoff.memory_cache_first && !Config.onoff.memory_cache_disk)
e1381638 3250 storeAppendPrintf(entry, "none");
ea21d497
HN
3251 storeAppendPrintf(entry, "\n");
3252}
3253
cca8ba0d 3254#include "cf_parser.cci"
f1dc9b30 3255
3256peer_t
3257parseNeighborType(const char *s)
3258{
a37d6070 3259 if (!strcmp(s, "parent"))
62e76326 3260 return PEER_PARENT;
3261
a37d6070 3262 if (!strcmp(s, "neighbor"))
62e76326 3263 return PEER_SIBLING;
3264
a37d6070 3265 if (!strcmp(s, "neighbour"))
62e76326 3266 return PEER_SIBLING;
3267
a37d6070 3268 if (!strcmp(s, "sibling"))
62e76326 3269 return PEER_SIBLING;
3270
a37d6070 3271 if (!strcmp(s, "multicast"))
62e76326 3272 return PEER_MULTICAST;
3273
fa84c01d 3274 debugs(15, DBG_CRITICAL, "WARNING: Unknown neighbor type: " << s);
62e76326 3275
f1dc9b30 3276 return PEER_SIBLING;
3277}
f150dd4b 3278
0b0cfcf2 3279#if USE_WCCPv2
52f772de 3280static void
b7ac5457 3281parse_IpAddress_list(Ip::Address_list ** head)
52f772de 3282{
3283 char *token;
b7ac5457
AJ
3284 Ip::Address_list *s;
3285 Ip::Address ipa;
62e76326 3286
2eceb328 3287 while ((token = ConfigParser::NextToken())) {
82b7abe3
AJ
3288 if (GetHostWithPort(token, &ipa)) {
3289
3290 while (*head)
3291 head = &(*head)->next;
3292
b7ac5457 3293 s = static_cast<Ip::Address_list *>(xcalloc(1, sizeof(*s)));
82b7abe3
AJ
3294 s->s = ipa;
3295
3296 *head = s;
9d65168e 3297 } else
82b7abe3 3298 self_destruct();
7e3ce7b9 3299 }
3300}
3301
3302static void
b7ac5457 3303dump_IpAddress_list(StoreEntry * e, const char *n, const Ip::Address_list * s)
7e3ce7b9 3304{
cc192b50 3305 char ntoabuf[MAX_IPSTRLEN];
3306
7e3ce7b9 3307 while (s) {
cc192b50 3308 storeAppendPrintf(e, "%s %s\n",
62e76326 3309 n,
4dd643d5 3310 s->s.toStr(ntoabuf,MAX_IPSTRLEN));
62e76326 3311 s = s->next;
7e3ce7b9 3312 }
3313}
3314
3315static void
b7ac5457 3316free_IpAddress_list(Ip::Address_list ** head)
7e3ce7b9 3317{
26ac0430
AJ
3318 if (*head) delete *head;
3319 *head = NULL;
7e3ce7b9 3320}
3321
0b0cfcf2 3322#if CURRENTLY_UNUSED
3323/* This code was previously used by http_port. Left as it really should
3324 * be used by icp_port and htcp_port
3325 */
7e3ce7b9 3326static int
b7ac5457 3327check_null_IpAddress_list(const Ip::Address_list * s)
7e3ce7b9 3328{
3329 return NULL == s;
3330}
62e76326 3331
3f38a55e 3332#endif /* CURRENTLY_UNUSED */
0b0cfcf2 3333#endif /* USE_WCCPv2 */
7e3ce7b9 3334
d193a436 3335static void
fa720bfb 3336parsePortSpecification(const AnyP::PortCfgPointer &s, char *token)
d193a436 3337{
3f38a55e 3338 char *host = NULL;
3f38a55e 3339 unsigned short port = 0;
cc192b50 3340 char *t = NULL;
3341 char *junk = NULL;
62e76326 3342
5529ca8a 3343 s->disable_pmtu_discovery = DISABLE_PMTU_OFF;
13adaf1f 3344 s->name = xstrdup(token);
d67acb4e 3345 s->connection_auth_disabled = false;
5529ca8a 3346
1ca54a54 3347 const char *portType = AnyP::UriScheme(s->transport.protocol).c_str();
eb6ac808 3348
cc192b50 3349 if (*token == '[') {
3350 /* [ipv6]:port */
26ac0430
AJ
3351 host = token + 1;
3352 t = strchr(host, ']');
3353 if (!t) {
eb6ac808 3354 debugs(3, DBG_CRITICAL, "FATAL: " << portType << "_port: missing ']' on IPv6 address: " << token);
26ac0430 3355 self_destruct();
cc192b50 3356 }
a38ec4b1
FC
3357 *t = '\0';
3358 ++t;
26ac0430 3359 if (*t != ':') {
eb6ac808 3360 debugs(3, DBG_CRITICAL, "FATAL: " << portType << "_port: missing Port in: " << token);
26ac0430 3361 self_destruct();
cc192b50 3362 }
055421ee 3363 if (!Ip::EnableIpv6) {
eb6ac808 3364 debugs(3, DBG_CRITICAL, "FATAL: " << portType << "_port: IPv6 is not available.");
26ac0430
AJ
3365 self_destruct();
3366 }
055421ee
AJ
3367 port = xatos(t + 1);
3368 } else if ((t = strchr(token, ':'))) {
3369 /* host:port */
3370 /* ipv4:port */
3371 host = token;
3372 *t = '\0';
3373 port = xatos(t + 1);
3374
54a063a2
TX
3375 } else if (strtol(token, &junk, 10) && !*junk) {
3376 port = xatos(token);
eb6ac808 3377 debugs(3, 3, portType << "_port: found Listen on Port: " << port);
055421ee 3378 } else {
eb6ac808 3379 debugs(3, DBG_CRITICAL, "FATAL: " << portType << "_port: missing Port: " << token);
055421ee
AJ
3380 self_destruct();
3381 }
62e76326 3382
859741ed 3383 if (port == 0 && host != NULL) {
eb6ac808 3384 debugs(3, DBG_CRITICAL, "FATAL: " << portType << "_port: Port cannot be 0: " << token);
0e656b69 3385 self_destruct();
cc192b50 3386 }
0e656b69 3387
cc192b50 3388 if (NULL == host) {
4dd643d5
AJ
3389 s->s.setAnyAddr();
3390 s->s.port(port);
6df7ad56 3391 if (!Ip::EnableIpv6)
4dd643d5 3392 s->s.setIPv4();
eb6ac808 3393 debugs(3, 3, portType << "_port: found Listen on wildcard address: *:" << s->s.port());
c49d44e1 3394 } else if ( (s->s = host) ) { /* check/parse numeric IPA */
4dd643d5 3395 s->s.port(port);
6df7ad56 3396 if (!Ip::EnableIpv6)
4dd643d5 3397 s->s.setIPv4();
eb6ac808 3398 debugs(3, 3, portType << "_port: Listen on Host/IP: " << host << " --> " << s->s);
26ac0430 3399 } else if ( s->s.GetHostByName(host) ) { /* check/parse for FQDN */
62e76326 3400 /* dont use ipcache */
62e76326 3401 s->defaultsite = xstrdup(host);
4dd643d5 3402 s->s.port(port);
6df7ad56 3403 if (!Ip::EnableIpv6)
4dd643d5 3404 s->s.setIPv4();
eb6ac808 3405 debugs(3, 3, portType << "_port: found Listen as Host " << s->defaultsite << " on IP: " << s->s);
26ac0430 3406 } else {
eb6ac808 3407 debugs(3, DBG_CRITICAL, "FATAL: " << portType << "_port: failed to resolve Host/IP: " << host);
62e76326 3408 self_destruct();
cc192b50 3409 }
3f38a55e 3410}
3411
ecb19f1a
AR
3412/// parses the protocol= option of the *_port directive, returning parsed value
3413/// unsupported option values result in a fatal error message
3cc0f4e7 3414/// upper case values required; caller may convert for backward compatibility
ecb19f1a 3415static AnyP::ProtocolVersion
3cc0f4e7 3416parsePortProtocol(const SBuf &value)
ecb19f1a
AR
3417{
3418 // HTTP/1.0 not supported because we are version 1.1 which contains a superset of 1.0
3419 // and RFC 2616 requires us to upgrade 1.0 to 1.1
3cc0f4e7 3420 if (value.cmp("HTTP") == 0 || value.cmp("HTTP/1.1") == 0)
ecb19f1a
AR
3421 return AnyP::ProtocolVersion(AnyP::PROTO_HTTP, 1,1);
3422
3cc0f4e7 3423 if (value.cmp("HTTPS") == 0 || value.cmp("HTTPS/1.1") == 0)
ecb19f1a
AR
3424 return AnyP::ProtocolVersion(AnyP::PROTO_HTTPS, 1,1);
3425
3cc0f4e7 3426 if (value.cmp("FTP") == 0)
aea65fec 3427 return Ftp::ProtocolVersion();
ecb19f1a 3428
3cc0f4e7 3429 fatalf("%s directive does not support protocol=" SQUIDSBUFPH "\n", cfg_directive, SQUIDSBUFPRINT(value));
ecb19f1a
AR
3430 return AnyP::ProtocolVersion(); // not reached
3431}
3432
3f38a55e 3433static void
fa720bfb 3434parse_port_option(AnyP::PortCfgPointer &s, char *token)
3f38a55e 3435{
c7b1dd5d
AJ
3436 /* modes first */
3437
3438 if (strcmp(token, "accel") == 0) {
151ba0d4 3439 if (s->flags.isIntercepted()) {
3cc0f4e7 3440 debugs(3, DBG_CRITICAL, "FATAL: " << cfg_directive << ": Accelerator mode requires its own port. It cannot be shared with other modes.");
c7b1dd5d
AJ
3441 self_destruct();
3442 }
6a25a046 3443 s->flags.accelSurrogate = true;
86ab7a90 3444 s->vhost = true;
c7b1dd5d 3445 } else if (strcmp(token, "transparent") == 0 || strcmp(token, "intercept") == 0) {
151ba0d4 3446 if (s->flags.accelSurrogate || s->flags.tproxyIntercept) {
3cc0f4e7 3447 debugs(3, DBG_CRITICAL, "FATAL: " << cfg_directive << ": Intercept mode requires its own interception port. It cannot be shared with other modes.");
c7b1dd5d
AJ
3448 self_destruct();
3449 }
6a25a046 3450 s->flags.natIntercept = true;
c7b1dd5d
AJ
3451 Ip::Interceptor.StartInterception();
3452 /* Log information regarding the port modes under interception. */
3453 debugs(3, DBG_IMPORTANT, "Starting Authentication on port " << s->s);
3454 debugs(3, DBG_IMPORTANT, "Disabling Authentication on port " << s->s << " (interception enabled)");
c7b1dd5d 3455 } else if (strcmp(token, "tproxy") == 0) {
151ba0d4 3456 if (s->flags.natIntercept || s->flags.accelSurrogate) {
3cc0f4e7 3457 debugs(3,DBG_CRITICAL, "FATAL: " << cfg_directive << ": TPROXY option requires its own interception port. It cannot be shared with other modes.");
c7b1dd5d
AJ
3458 self_destruct();
3459 }
6a25a046 3460 s->flags.tproxyIntercept = true;
c7b1dd5d
AJ
3461 Ip::Interceptor.StartTransparency();
3462 /* Log information regarding the port modes under transparency. */
0d901ef4 3463 debugs(3, DBG_IMPORTANT, "Disabling Authentication on port " << s->s << " (TPROXY enabled)");
c7b1dd5d 3464
151ba0d4 3465 if (s->flags.proxySurrogate) {
d3d92daa 3466 debugs(3, DBG_IMPORTANT, "Disabling TPROXY Spoofing on port " << s->s << " (require-proxy-header enabled)");
151ba0d4
AJ
3467 }
3468
c7b1dd5d 3469 if (!Ip::Interceptor.ProbeForTproxy(s->s)) {
3cc0f4e7 3470 debugs(3, DBG_CRITICAL, "FATAL: " << cfg_directive << ": TPROXY support in the system does not work.");
c7b1dd5d
AJ
3471 self_destruct();
3472 }
3473
d3d92daa 3474 } else if (strcmp(token, "require-proxy-header") == 0) {
00d0ce87 3475 s->flags.proxySurrogate = true;
13edf4cc
AJ
3476 if (s->flags.tproxyIntercept) {
3477 // receiving is still permitted, so we do not unset the TPROXY flag
3478 // spoofing access control override takes care of the spoof disable later
3479 debugs(3, DBG_IMPORTANT, "Disabling TPROXY Spoofing on port " << s->s << " (require-proxy-header enabled)");
3480 }
00d0ce87 3481
c7b1dd5d 3482 } else if (strncmp(token, "defaultsite=", 12) == 0) {
6a25a046 3483 if (!s->flags.accelSurrogate) {
3cc0f4e7 3484 debugs(3, DBG_CRITICAL, "FATAL: " << cfg_directive << ": defaultsite option requires Acceleration mode flag.");
c7b1dd5d
AJ
3485 self_destruct();
3486 }
62e76326 3487 safe_free(s->defaultsite);
3488 s->defaultsite = xstrdup(token + 12);
3f38a55e 3489 } else if (strcmp(token, "vhost") == 0) {
6a25a046 3490 if (!s->flags.accelSurrogate) {
3cc0f4e7 3491 debugs(3, DBG_CRITICAL, "WARNING: " << cfg_directive << ": vhost option is deprecated. Use 'accel' mode flag instead.");
cf673853 3492 }
6a25a046 3493 s->flags.accelSurrogate = true;
86ab7a90 3494 s->vhost = true;
cf673853 3495 } else if (strcmp(token, "no-vhost") == 0) {
6a25a046 3496 if (!s->flags.accelSurrogate) {
3cc0f4e7 3497 debugs(3, DBG_IMPORTANT, "ERROR: " << cfg_directive << ": no-vhost option requires Acceleration mode flag.");
c7b1dd5d 3498 }
86ab7a90 3499 s->vhost = false;
3f38a55e 3500 } else if (strcmp(token, "vport") == 0) {
6a25a046 3501 if (!s->flags.accelSurrogate) {
3cc0f4e7 3502 debugs(3, DBG_CRITICAL, "FATAL: " << cfg_directive << ": vport option requires Acceleration mode flag.");
c7b1dd5d
AJ
3503 self_destruct();
3504 }
62e76326 3505 s->vport = -1;
3f38a55e 3506 } else if (strncmp(token, "vport=", 6) == 0) {
6a25a046 3507 if (!s->flags.accelSurrogate) {
3cc0f4e7 3508 debugs(3, DBG_CRITICAL, "FATAL: " << cfg_directive << ": vport option requires Acceleration mode flag.");
c7b1dd5d
AJ
3509 self_destruct();
3510 }
0e656b69 3511 s->vport = xatos(token + 6);
3f38a55e 3512 } else if (strncmp(token, "protocol=", 9) == 0) {
6a25a046 3513 if (!s->flags.accelSurrogate) {
3cc0f4e7 3514 debugs(3, DBG_CRITICAL, "FATAL: " << cfg_directive << ": protocol option requires Acceleration mode flag.");
c7b1dd5d
AJ
3515 self_destruct();
3516 }
3cc0f4e7 3517 s->transport = parsePortProtocol(ToUpper(SBuf(token + 9)));
7f7bdd96 3518 } else if (strcmp(token, "allow-direct") == 0) {
6a25a046 3519 if (!s->flags.accelSurrogate) {
3cc0f4e7 3520 debugs(3, DBG_CRITICAL, "FATAL: " << cfg_directive << ": allow-direct option requires Acceleration mode flag.");
c7b1dd5d
AJ
3521 self_destruct();
3522 }
86ab7a90 3523 s->allow_direct = true;
90fa5816 3524 } else if (strcmp(token, "act-as-origin") == 0) {
6a25a046 3525 if (!s->flags.accelSurrogate) {
3cc0f4e7 3526 debugs(3, DBG_IMPORTANT, "ERROR: " << cfg_directive << ": act-as-origin option requires Acceleration mode flag.");
90fa5816 3527 } else
86ab7a90 3528 s->actAsOrigin = true;
432bc83c 3529 } else if (strcmp(token, "ignore-cc") == 0) {
626096be 3530#if !USE_HTTP_VIOLATIONS
6a25a046 3531 if (!s->flags.accelSurrogate) {
3cc0f4e7 3532 debugs(3, DBG_CRITICAL, "FATAL: " << cfg_directive << ": ignore-cc option requires Acceleration mode flag.");
838daf3f
A
3533 self_destruct();
3534 }
432bc83c 3535#endif
86ab7a90 3536 s->ignore_cc = true;
c7b1dd5d
AJ
3537 } else if (strncmp(token, "name=", 5) == 0) {
3538 safe_free(s->name);
3539 s->name = xstrdup(token + 5);
d67acb4e
AJ
3540 } else if (strcmp(token, "no-connection-auth") == 0) {
3541 s->connection_auth_disabled = true;
3542 } else if (strcmp(token, "connection-auth=off") == 0) {
26ac0430 3543 s->connection_auth_disabled = true;
d67acb4e 3544 } else if (strcmp(token, "connection-auth") == 0) {
26ac0430 3545 s->connection_auth_disabled = false;
d67acb4e 3546 } else if (strcmp(token, "connection-auth=on") == 0) {
26ac0430 3547 s->connection_auth_disabled = false;
5529ca8a 3548 } else if (strncmp(token, "disable-pmtu-discovery=", 23) == 0) {
a37d6070 3549 if (!strcmp(token + 23, "off"))
5529ca8a 3550 s->disable_pmtu_discovery = DISABLE_PMTU_OFF;
a37d6070 3551 else if (!strcmp(token + 23, "transparent"))
5529ca8a 3552 s->disable_pmtu_discovery = DISABLE_PMTU_TRANSPARENT;
a37d6070 3553 else if (!strcmp(token + 23, "always"))
5529ca8a 3554 s->disable_pmtu_discovery = DISABLE_PMTU_ALWAYS;
3555 else
3556 self_destruct();
cc192b50 3557 } else if (strcmp(token, "ipv4") == 0) {
4dd643d5 3558 if ( !s->s.setIPv4() ) {
3cc0f4e7 3559 debugs(3, DBG_CRITICAL, "FATAL: " << cfg_directive << ": IPv6 addresses cannot be used as IPv4-Only. " << s->s );
cc192b50 3560 self_destruct();
3561 }
26ac0430 3562 } else if (strcmp(token, "tcpkeepalive") == 0) {
86ab7a90 3563 s->tcp_keepalive.enabled = true;
b2130d58 3564 } else if (strncmp(token, "tcpkeepalive=", 13) == 0) {
26ac0430 3565 char *t = token + 13;
86ab7a90 3566 s->tcp_keepalive.enabled = true;
e398d16e 3567 s->tcp_keepalive.idle = xatoui(t,',');
26ac0430
AJ
3568 t = strchr(t, ',');
3569 if (t) {
a38ec4b1 3570 ++t;
e398d16e 3571 s->tcp_keepalive.interval = xatoui(t,',');
26ac0430
AJ
3572 t = strchr(t, ',');
3573 }
3574 if (t) {
a38ec4b1 3575 ++t;
54a063a2 3576 s->tcp_keepalive.timeout = xatoui(t);
26ac0430 3577 }
cb4f4424 3578#if USE_OPENSSL
a37d6070 3579 } else if (strcmp(token, "sslBump") == 0) {
9a622f3e 3580 debugs(3, DBG_PARSE_NOTE(1), "WARNING: '" << token << "' is deprecated " <<
3cc0f4e7 3581 "in " << cfg_directive << ". Use 'ssl-bump' instead.");
c3d24490 3582 s->flags.tunnelSslBumping = true;
3a0c8eb5 3583 } else if (strcmp(token, "ssl-bump") == 0) {
c3d24490 3584 s->flags.tunnelSslBumping = true;
154dc884 3585 } else if (strncmp(token, "cert=", 5) == 0) {
9a622f3e 3586 s->secure.parse(token);
154dc884 3587 } else if (strncmp(token, "key=", 4) == 0) {
9a622f3e 3588 s->secure.parse(token);
154dc884 3589 } else if (strncmp(token, "version=", 8) == 0) {
1cc44095
AJ
3590 debugs(3, DBG_PARSE_NOTE(1), "UPGRADE WARNING: '" << token << "' is deprecated " <<
3591 "in " << cfg_directive << ". Use 'options=' instead.");
9a622f3e 3592 s->secure.parse(token);
154dc884 3593 } else if (strncmp(token, "options=", 8) == 0) {
9a622f3e 3594 s->secure.parse(token);
154dc884 3595 } else if (strncmp(token, "cipher=", 7) == 0) {
9a622f3e 3596 s->secure.parse(token);
154dc884 3597 } else if (strncmp(token, "clientca=", 9) == 0) {
3598 safe_free(s->clientca);
3599 s->clientca = xstrdup(token + 9);
3600 } else if (strncmp(token, "cafile=", 7) == 0) {
86a84cc0
AJ
3601 debugs(3, DBG_PARSE_NOTE(1), "UPGRADE WARNING: '" << token << "' is deprecated " <<
3602 "in " << cfg_directive << ". Use 'tls-cafile=' instead.");
9a622f3e 3603 s->secure.parse(token);
154dc884 3604 } else if (strncmp(token, "capath=", 7) == 0) {
9a622f3e 3605 s->secure.parse(token);
154dc884 3606 } else if (strncmp(token, "crlfile=", 8) == 0) {
9a622f3e 3607 s->secure.parse(token);
154dc884 3608 } else if (strncmp(token, "dhparams=", 9) == 0) {
54fbe371
PM
3609 debugs(3, DBG_PARSE_NOTE(DBG_IMPORTANT), "WARNING: '" << token << "' is deprecated " <<
3610 "in " << cfg_directive << ". Use 'tls-dh=' instead.");
474f076e 3611 s->secure.parse(token);
154dc884 3612 } else if (strncmp(token, "sslflags=", 9) == 0) {
8b253b83 3613 // NP: deprecation warnings output by secure.parse() when relevant
9a622f3e 3614 s->secure.parse(token+3);
154dc884 3615 } else if (strncmp(token, "sslcontext=", 11) == 0) {
95d2589c
CT
3616 safe_free(s->sslContextSessionId);
3617 s->sslContextSessionId = xstrdup(token + 11);
3618 } else if (strcmp(token, "generate-host-certificates") == 0) {
3619 s->generateHostCertificates = true;
3620 } else if (strcmp(token, "generate-host-certificates=on") == 0) {
3621 s->generateHostCertificates = true;
3622 } else if (strcmp(token, "generate-host-certificates=off") == 0) {
3623 s->generateHostCertificates = false;
3624 } else if (strncmp(token, "dynamic_cert_mem_cache_size=", 28) == 0) {
3625 parseBytesOptionValue(&s->dynamicCertMemCacheSize, B_BYTES_STR, token + 28);
154dc884 3626#endif
9a622f3e
AJ
3627 } else if (strncmp(token, "tls-", 4) == 0) {
3628 s->secure.parse(token+4);
3cc0f4e7 3629 } else if (strcmp(token, "ftp-track-dirs") == 0) {
27c841f6 3630 s->ftp_track_dirs = true;
3f38a55e 3631 } else {
3cc0f4e7 3632 debugs(3, DBG_CRITICAL, "FATAL: Unknown " << cfg_directive << " option '" << token << "'.");
62e76326 3633 self_destruct();
3f38a55e 3634 }
3635}
3636
3f38a55e 3637void
3638add_http_port(char *portspec)
3639{
fa720bfb 3640 AnyP::PortCfgPointer s = new AnyP::PortCfg();
3cc0f4e7 3641 s->transport = parsePortProtocol(SBuf("HTTP"));
859741ed 3642 parsePortSpecification(s, portspec);
65d448bc 3643 // we may need to merge better if the above returns a list with clones
7e07ced1 3644 assert(s->next == NULL);
fa720bfb
AJ
3645 s->next = HttpPortList;
3646 HttpPortList = s;
3f38a55e 3647}
3648
3649static void
fa720bfb 3650parsePortCfg(AnyP::PortCfgPointer *head, const char *optionName)
d3388e1f 3651{
3cc0f4e7 3652 SBuf protoName;
d3388e1f 3653 if (strcmp(optionName, "http_port") == 0 ||
b5944cff 3654 strcmp(optionName, "ascii_port") == 0)
3cc0f4e7 3655 protoName = "HTTP";
d3388e1f 3656 else if (strcmp(optionName, "https_port") == 0)
3cc0f4e7 3657 protoName = "HTTPS";
434a79b0 3658 else if (strcmp(optionName, "ftp_port") == 0)
3cc0f4e7
AR
3659 protoName = "FTP";
3660 if (protoName.isEmpty()) {
d3388e1f
AR
3661 self_destruct();
3662 return;
3663 }
3664
2eceb328 3665 char *token = ConfigParser::NextToken();
62e76326 3666
9eeb8e4b 3667 if (!token) {
62e76326 3668 self_destruct();
9eeb8e4b 3669 return;
3670 }
62e76326 3671
fa720bfb 3672 AnyP::PortCfgPointer s = new AnyP::PortCfg();
3cc0f4e7 3673 s->transport = parsePortProtocol(protoName); // default; protocol=... overwrites
859741ed 3674 parsePortSpecification(s, token);
62e76326 3675
3f38a55e 3676 /* parse options ... */
2eceb328 3677 while ((token = ConfigParser::NextToken())) {
65d448bc 3678 parse_port_option(s, token);
3f38a55e 3679 }
62e76326 3680
86a84cc0
AJ
3681#if USE_OPENSSL
3682 // if clientca has been defined but not cafile, then use it to verify
3683 // but if cafile has been defined, only use that to verify
3684 if (s->clientca && !s->secure.caFiles.size())
3685 s->secure.caFiles.emplace_back(SBuf(s->clientca));
3686#endif
3687
6507233b 3688 if (s->transport.protocol == AnyP::PROTO_HTTPS) {
49d8cc6f 3689 s->secure.encryptTransport = true;
151ba0d4 3690#if USE_OPENSSL
38450a50 3691 /* ssl-bump on https_port configuration requires either tproxy or intercept, and vice versa */
c3d24490 3692 const bool hijacked = s->flags.isIntercepted();
6a25a046 3693 if (s->flags.tunnelSslBumping && !hijacked) {
38450a50 3694 debugs(3, DBG_CRITICAL, "FATAL: ssl-bump on https_port requires tproxy/intercept which is missing.");
03f00a11
CT
3695 self_destruct();
3696 }
6a25a046 3697 if (hijacked && !s->flags.tunnelSslBumping) {
38450a50 3698 debugs(3, DBG_CRITICAL, "FATAL: tproxy/intercept on https_port requires ssl-bump which is missing.");
03f00a11
CT
3699 self_destruct();
3700 }
03f00a11 3701#endif
a07a354f 3702 if (s->flags.proxySurrogate) {
d3d92daa 3703 debugs(3,DBG_CRITICAL, "FATAL: https_port: require-proxy-header option is not supported on HTTPS ports.");
151ba0d4
AJ
3704 self_destruct();
3705 }
3cc0f4e7 3706 } else if (protoName.cmp("FTP") == 0) {
434a79b0
DK
3707 /* ftp_port does not support ssl-bump */
3708 if (s->flags.tunnelSslBumping) {
3709 debugs(3, DBG_CRITICAL, "FATAL: ssl-bump is not supported for ftp_port.");
3710 self_destruct();
3711 }
a07a354f
AJ
3712 if (s->flags.proxySurrogate) {
3713 // Passive FTP data channel does not work without deep protocol inspection in the frontend.
3714 debugs(3,DBG_CRITICAL, "FATAL: require-proxy-header option is not supported on ftp_port.");
3715 self_destruct();
3716 }
151ba0d4 3717 }
03f00a11 3718
4dd643d5 3719 if (Ip::EnableIpv6&IPV6_SPECIAL_SPLITSTACK && s->s.isAnyAddr()) {
7e07ced1 3720 // clone the port options from *s to *(s->next)
fa720bfb 3721 s->next = s->clone();
4dd643d5 3722 s->next->s.setIPv4();
1ca54a54 3723 debugs(3, 3, AnyP::UriScheme(s->transport.protocol).c_str() << "_port: clone wildcard address for split-stack: " << s->s << " and " << s->next->s);
7e07ced1 3724 }
7e07ced1 3725
fa720bfb
AJ
3726 while (*head != NULL)
3727 head = &((*head)->next);
62e76326 3728
fa720bfb 3729 *head = s;
3f38a55e 3730}
3731
3732static void
fa720bfb 3733dump_generic_port(StoreEntry * e, const char *n, const AnyP::PortCfgPointer &s)
3f38a55e 3734{
cc192b50 3735 char buf[MAX_IPSTRLEN];
3736
3737 storeAppendPrintf(e, "%s %s",
62e76326 3738 n,
4dd643d5 3739 s->s.toUrl(buf,MAX_IPSTRLEN));
62e76326 3740
df1b20e4 3741 // MODES and specific sub-options.
6a25a046 3742 if (s->flags.natIntercept)
2ad20b4f 3743 storeAppendPrintf(e, " intercept");
62e76326 3744
6a25a046 3745 else if (s->flags.tproxyIntercept)
c7b1dd5d
AJ
3746 storeAppendPrintf(e, " tproxy");
3747
00d0ce87 3748 else if (s->flags.proxySurrogate)
d3d92daa 3749 storeAppendPrintf(e, " require-proxy-header");
00d0ce87 3750
6a25a046 3751 else if (s->flags.accelSurrogate) {
c7b1dd5d
AJ
3752 storeAppendPrintf(e, " accel");
3753
df1b20e4
AJ
3754 if (s->vhost)
3755 storeAppendPrintf(e, " vhost");
62e76326 3756
df1b20e4
AJ
3757 if (s->vport < 0)
3758 storeAppendPrintf(e, " vport");
3759 else if (s->vport > 0)
3760 storeAppendPrintf(e, " vport=%d", s->vport);
5529ca8a 3761
df1b20e4
AJ
3762 if (s->defaultsite)
3763 storeAppendPrintf(e, " defaultsite=%s", s->defaultsite);
3764
eb6ac808
AJ
3765 // TODO: compare against prefix of 'n' instead of assuming http_port
3766 if (s->transport.protocol != AnyP::PROTO_HTTP)
1ca54a54 3767 storeAppendPrintf(e, " protocol=%s", AnyP::UriScheme(s->transport.protocol).c_str());
df1b20e4
AJ
3768
3769 if (s->allow_direct)
3770 storeAppendPrintf(e, " allow-direct");
3771
3772 if (s->ignore_cc)
3773 storeAppendPrintf(e, " ignore-cc");
3774
3775 }
3776
3777 // Generic independent options
3778
3779 if (s->name)
3780 storeAppendPrintf(e, " name=%s", s->name);
3781
33e701f3 3782#if USE_HTTP_VIOLATIONS
6a25a046 3783 if (!s->flags.accelSurrogate && s->ignore_cc)
df1b20e4
AJ
3784 storeAppendPrintf(e, " ignore-cc");
3785#endif
c7b1dd5d 3786
d67acb4e
AJ
3787 if (s->connection_auth_disabled)
3788 storeAppendPrintf(e, " connection-auth=off");
3789 else
3790 storeAppendPrintf(e, " connection-auth=on");
3791
5529ca8a 3792 if (s->disable_pmtu_discovery != DISABLE_PMTU_OFF) {
3793 const char *pmtu;
3794
3795 if (s->disable_pmtu_discovery == DISABLE_PMTU_ALWAYS)
3796 pmtu = "always";
3797 else
3798 pmtu = "transparent";
3799
3800 storeAppendPrintf(e, " disable-pmtu-discovery=%s", pmtu);
3801 }
b2130d58 3802
4dd643d5 3803 if (s->s.isAnyAddr() && !s->s.isIPv6())
df1b20e4
AJ
3804 storeAppendPrintf(e, " ipv4");
3805
b2130d58 3806 if (s->tcp_keepalive.enabled) {
26ac0430 3807 if (s->tcp_keepalive.idle || s->tcp_keepalive.interval || s->tcp_keepalive.timeout) {
68924b6d 3808 storeAppendPrintf(e, " tcpkeepalive=%d,%d,%d", s->tcp_keepalive.idle, s->tcp_keepalive.interval, s->tcp_keepalive.timeout);
26ac0430 3809 } else {
68924b6d 3810 storeAppendPrintf(e, " tcpkeepalive");
26ac0430 3811 }
b2130d58 3812 }
154dc884 3813
cb4f4424 3814#if USE_OPENSSL
6a25a046 3815 if (s->flags.tunnelSslBumping)
df1b20e4 3816 storeAppendPrintf(e, " ssl-bump");
9a622f3e 3817#endif
c7b1dd5d 3818
8250ca31 3819 s->secure.dumpCfg(e, "tls-");
154dc884 3820
9a622f3e 3821#if USE_OPENSSL
95d2589c
CT
3822 if (s->sslContextSessionId)
3823 storeAppendPrintf(e, " sslcontext=%s", s->sslContextSessionId);
3824
3825 if (s->generateHostCertificates)
3826 storeAppendPrintf(e, " generate-host-certificates");
3827
3828 if (s->dynamicCertMemCacheSize != std::numeric_limits<size_t>::max())
3829 storeAppendPrintf(e, "dynamic_cert_mem_cache_size=%lu%s\n", (unsigned long)s->dynamicCertMemCacheSize, B_BYTES_STR);
154dc884 3830#endif
3f38a55e 3831}
62e76326 3832
3f38a55e 3833static void
fa720bfb 3834dump_PortCfg(StoreEntry * e, const char *n, const AnyP::PortCfgPointer &s)
3f38a55e 3835{
fa720bfb
AJ
3836 for (AnyP::PortCfgPointer p = s; p != NULL; p = p->next) {
3837 dump_generic_port(e, n, p);
62e76326 3838 storeAppendPrintf(e, "\n");
3f38a55e 3839 }
3840}
3841
f150dd4b 3842void
3843configFreeMemory(void)
3844{
23ff6968 3845 free_all();
cb4f4424 3846#if USE_OPENSSL
29bf4910 3847 SSL_CTX_free(Config.ssl_client.sslContext);
866be11c 3848 Ssl::unloadSquidUntrusted();
29bf4910 3849#endif
f150dd4b 3850}
f0b19334 3851
94439e4e 3852void
f0b19334 3853requirePathnameExists(const char *name, const char *path)
3854{
62e76326 3855
f0b19334 3856 struct stat sb;
08577e16 3857 char pathbuf[BUFSIZ];
f0b19334 3858 assert(path != NULL);
62e76326 3859
0b796acd 3860 if (Config.chroot_dir && (geteuid() == 0)) {
62e76326 3861 snprintf(pathbuf, BUFSIZ, "%s/%s", Config.chroot_dir, path);
3862 path = pathbuf;
08577e16 3863 }
62e76326 3864
a572d8be 3865 if (stat(path, &sb) < 0) {
fe89b8bb 3866 debugs(0, DBG_CRITICAL, (opt_parse_cfg_only?"FATAL: ":"ERROR: ") << name << " " << path << ": " << xstrerror());
a32a190b
AJ
3867 // keep going to find more issues if we are only checking the config file with "-k parse"
3868 if (opt_parse_cfg_only)
3869 return;
3870 // this is fatal if it is found during startup or reconfigure
a572d8be 3871 if (opt_send_signal == -1 || opt_send_signal == SIGHUP)
3872 fatalf("%s %s: %s", name, path, xstrerror());
a572d8be 3873 }
f0b19334 3874}
d860a1aa 3875
450e0c10 3876#include "AccessLogEntry.h"
7684c4b1 3877
fb0c2f17
NH
3878/**
3879 * We support several access_log configuration styles:
3880 *
3881 * #1: Deprecated ancient style without an explicit logging module:
3882 * access_log /var/log/access.log
3883 *
3884 * #2: The "none" logging module (i.e., no logging [of matching transactions]):
3885 * access_log none [acl ...]
3886 *
3887 * #3: Configurable logging module without named options:
3888 * Logformat or the first ACL name, whichever comes first, may not contain '='.
3889 * If no explicit logformat name is given, the first ACL name, if any,
3890 * should not be an existing logformat name or it will be treated as such.
3891 * access_log module:place [logformat_name] [acl ...]
3892 *
3893 * #4: Configurable logging module with name=value options such as logformat=x:
3894 * The first ACL name may not contain '='.
3895 * access_log module:place [option ...] [acl ...]
3896 *
3897 */
7684c4b1 3898static void
87ddff6e 3899parse_access_log(CustomLog ** logs)
7684c4b1 3900{
87ddff6e 3901 CustomLog *cl = (CustomLog *)xcalloc(1, sizeof(*cl));
7684c4b1 3902
fb0c2f17
NH
3903 // default buffer size and fatal settings
3904 cl->bufferSize = 8*MAX_URL;
3905 cl->fatal = true;
3906
3907 /* determine configuration style */
3908
2eceb328 3909 const char *filename = ConfigParser::NextToken();
fb0c2f17 3910 if (!filename) {
7684c4b1 3911 self_destruct();
9eeb8e4b 3912 return;
3913 }
7684c4b1 3914
3915 if (strcmp(filename, "none") == 0) {
20efa1c2 3916 cl->type = Log::Format::CLF_NONE;
6f58d7d7 3917 aclParseAclList(LegacyParser, &cl->aclList, filename);
6375a714
AJ
3918 while (*logs)
3919 logs = &(*logs)->next;
3920 *logs = cl;
3921 return;
7684c4b1 3922 }
3923
fb0c2f17
NH
3924 cl->filename = xstrdup(filename);
3925 cl->type = Log::Format::CLF_UNKNOWN;
efc23871 3926 cl->rotateCount = -1; // default: use global logfile_rotate setting.
fb0c2f17 3927
bde7a8ce 3928 const char *token = ConfigParser::PeekAtToken();
fb0c2f17
NH
3929 if (!token) { // style #1
3930 // no options to deal with
3931 } else if (!strchr(token, '=')) { // style #3
bde7a8ce
CT
3932 // if logformat name is recognized,
3933 // pop the previewed token; Else it must be an ACL name
3934 if (setLogformat(cl, token, false))
3935 (void)ConfigParser::NextToken();
fb0c2f17
NH
3936 } else { // style #4
3937 do {
3938 if (strncasecmp(token, "on-error=", 9) == 0) {
3939 if (strncasecmp(token+9, "die", 3) == 0) {
3940 cl->fatal = true;
3941 } else if (strncasecmp(token+9, "drop", 4) == 0) {
3942 cl->fatal = false;
3943 } else {
3944 debugs(3, DBG_CRITICAL, "Unknown value for on-error '" <<
3945 token << "' expected 'drop' or 'die'");
3946 self_destruct();
3947 }
3948 } else if (strncasecmp(token, "buffer-size=", 12) == 0) {
3949 parseBytesOptionValue(&cl->bufferSize, B_BYTES_STR, token+12);
efc23871
AJ
3950 } else if (strncasecmp(token, "rotate=", 7) == 0) {
3951 cl->rotateCount = xatoi(token + 7);
fb0c2f17
NH
3952 } else if (strncasecmp(token, "logformat=", 10) == 0) {
3953 setLogformat(cl, token+10, true);
3954 } else if (!strchr(token, '=')) {
bde7a8ce 3955 // Do not pop the token; it must be an ACL name
fb0c2f17
NH
3956 break; // done with name=value options, now to ACLs
3957 } else {
3958 debugs(3, DBG_CRITICAL, "Unknown access_log option " << token);
3959 self_destruct();
3960 }
bde7a8ce
CT
3961 // Pop the token, it was a valid "name=value" option
3962 (void)ConfigParser::NextToken();
3963 // Get next with preview ConfigParser::NextToken call.
3964 } while ((token = ConfigParser::PeekAtToken()) != NULL);
fb0c2f17 3965 }
7684c4b1 3966
fb0c2f17
NH
3967 // set format if it has not been specified explicitly
3968 if (cl->type == Log::Format::CLF_UNKNOWN)
3969 setLogformat(cl, "squid", true);
7684c4b1 3970
0a003ce5 3971 aclParseAclList(LegacyParser, &cl->aclList, cl->filename);
fb0c2f17
NH
3972
3973 while (*logs)
3974 logs = &(*logs)->next;
3975
3976 *logs = cl;
3977}
3978
3979/// sets CustomLog::type and, if needed, CustomLog::lf
3980/// returns false iff there is no named log format
3981static bool
3982setLogformat(CustomLog *cl, const char *logdef_name, const bool dieWhenMissing)
3983{
3984 assert(cl);
3985 assert(logdef_name);
3986
3987 debugs(3, 9, "possible " << cl->filename << " logformat: " << logdef_name);
3988
3989 if (cl->type != Log::Format::CLF_UNKNOWN) {
3990 debugs(3, DBG_CRITICAL, "Second logformat name in one access_log: " <<
3991 logdef_name << " " << cl->type << " ? " << Log::Format::CLF_NONE);
3992 self_destruct();
3993 return false;
3994 }
7684c4b1 3995
3996 /* look for the definition pointer corresponding to this name */
38e16f92 3997 Format::Format *lf = Log::TheConfig.logformats;
7684c4b1 3998
3999 while (lf != NULL) {
bf8fe701 4000 debugs(3, 9, "Comparing against '" << lf->name << "'");
7684c4b1 4001
4002 if (strcmp(lf->name, logdef_name) == 0)
4003 break;
4004
4005 lf = lf->next;
4006 }
4007
4008 if (lf != NULL) {
20efa1c2 4009 cl->type = Log::Format::CLF_CUSTOM;
7684c4b1 4010 cl->logFormat = lf;
4011 } else if (strcmp(logdef_name, "auto") == 0) {
fa84c01d 4012 debugs(0, DBG_CRITICAL, "WARNING: Log format 'auto' no longer exists. Using 'squid' instead.");
20efa1c2 4013 cl->type = Log::Format::CLF_SQUID;
7684c4b1 4014 } else if (strcmp(logdef_name, "squid") == 0) {
20efa1c2 4015 cl->type = Log::Format::CLF_SQUID;
7684c4b1 4016 } else if (strcmp(logdef_name, "common") == 0) {
20efa1c2
AJ
4017 cl->type = Log::Format::CLF_COMMON;
4018 } else if (strcmp(logdef_name, "combined") == 0) {
4019 cl->type = Log::Format::CLF_COMBINED;
3ff65596
AR
4020#if ICAP_CLIENT
4021 } else if (strcmp(logdef_name, "icap_squid") == 0) {
20efa1c2 4022 cl->type = Log::Format::CLF_ICAP_SQUID;
3ff65596 4023#endif
20efa1c2
AJ
4024 } else if (strcmp(logdef_name, "useragent") == 0) {
4025 cl->type = Log::Format::CLF_USERAGENT;
4026 } else if (strcmp(logdef_name, "referrer") == 0) {
4027 cl->type = Log::Format::CLF_REFERER;
fb0c2f17 4028 } else if (dieWhenMissing) {
fa84c01d 4029 debugs(3, DBG_CRITICAL, "Log format '" << logdef_name << "' is not defined");
7684c4b1 4030 self_destruct();
fb0c2f17
NH
4031 return false;
4032 } else {
4033 return false;
7684c4b1 4034 }
4035
fb0c2f17 4036 return true;
7684c4b1 4037}
4038
d64bef4c 4039static int
87ddff6e 4040check_null_access_log(CustomLog *customlog_definitions)
d64bef4c 4041{
4042 return customlog_definitions == NULL;
4043}
4044
7684c4b1 4045static void
87ddff6e 4046dump_access_log(StoreEntry * entry, const char *name, CustomLog * logs)
7684c4b1 4047{
87ddff6e 4048 CustomLog *log;
7684c4b1 4049
4050 for (log = logs; log; log = log->next) {
4051 storeAppendPrintf(entry, "%s ", name);
4052
4053 switch (log->type) {
4054
20efa1c2 4055 case Log::Format::CLF_CUSTOM:
efc23871 4056 storeAppendPrintf(entry, "%s logformat=%s", log->filename, log->logFormat->name);
7684c4b1 4057 break;
4058
20efa1c2 4059 case Log::Format::CLF_NONE:
efc23871 4060 storeAppendPrintf(entry, "logformat=none");
7684c4b1 4061 break;
4062
20efa1c2 4063 case Log::Format::CLF_SQUID:
efc23871 4064 storeAppendPrintf(entry, "%s logformat=squid", log->filename);
7684c4b1 4065 break;
4066
20efa1c2 4067 case Log::Format::CLF_COMBINED:
efc23871 4068 storeAppendPrintf(entry, "%s logformat=combined", log->filename);
20efa1c2
AJ
4069 break;
4070
4071 case Log::Format::CLF_COMMON:
efc23871 4072 storeAppendPrintf(entry, "%s logformat=common", log->filename);
7684c4b1 4073 break;
20efa1c2 4074
3ff65596 4075#if ICAP_CLIENT
20efa1c2 4076 case Log::Format::CLF_ICAP_SQUID:
efc23871 4077 storeAppendPrintf(entry, "%s logformat=icap_squid", log->filename);
3ff65596
AR
4078 break;
4079#endif
20efa1c2 4080 case Log::Format::CLF_USERAGENT:
efc23871 4081 storeAppendPrintf(entry, "%s logformat=useragent", log->filename);
20efa1c2 4082 break;
7684c4b1 4083
20efa1c2 4084 case Log::Format::CLF_REFERER:
efc23871 4085 storeAppendPrintf(entry, "%s logformat=referrer", log->filename);
7684c4b1 4086 break;
4087
20efa1c2 4088 case Log::Format::CLF_UNKNOWN:
7684c4b1 4089 break;
4090 }
4091
efc23871
AJ
4092 // default is on-error=die
4093 if (!log->fatal)
4094 storeAppendPrintf(entry, " on-error=drop");
4095
4096 // default: 64KB
4097 if (log->bufferSize != 64*1024)
32d3025c 4098 storeAppendPrintf(entry, " buffer-size=%" PRIuSIZE, log->bufferSize);
efc23871
AJ
4099
4100 if (log->rotateCount >= 0)
4101 storeAppendPrintf(entry, " rotate=%d", log->rotateCount);
4102
7684c4b1 4103 if (log->aclList)
4104 dump_acl_list(entry, log->aclList);
4105
4106 storeAppendPrintf(entry, "\n");
4107 }
4108}
4109
7684c4b1 4110static void
87ddff6e 4111free_access_log(CustomLog ** definitions)
7684c4b1 4112{
4113 while (*definitions) {
87ddff6e 4114 CustomLog *log = *definitions;
7684c4b1 4115 *definitions = log->next;
4116
4117 log->logFormat = NULL;
20efa1c2 4118 log->type = Log::Format::CLF_UNKNOWN;
7684c4b1 4119
4120 if (log->aclList)
4121 aclDestroyAclList(&log->aclList);
4122
4123 safe_free(log->filename);
4124
4125 xfree(log);
4126 }
4127}
3a69ddf3 4128
96c2bb61
AR
4129/// parses list of integers form name=N1,N2,N3,...
4130static bool
c8ea3cc0 4131parseNamedIntList(const char *data, const String &name, std::vector<int> &list)
96c2bb61
AR
4132{
4133 if (data && (strncmp(data, name.rawBuf(), name.size()) == 0)) {
4134 data += name.size();
4135 if (*data == '=') {
4136 while (true) {
4137 ++data;
4138 int value = 0;
4139 if (!StringToInt(data, value, &data, 10))
4140 break;
4141 list.push_back(value);
4142 if (*data == '\0' || *data != ',')
4143 break;
4144 }
4145 }
4146 }
cff99cec 4147 return data && *data == '\0';
96c2bb61
AR
4148}
4149
4150static void
212af65c
A
4151parse_CpuAffinityMap(CpuAffinityMap **const cpuAffinityMap)
4152{
96c2bb61
AR
4153#if !HAVE_CPU_AFFINITY
4154 debugs(3, DBG_CRITICAL, "FATAL: Squid built with no CPU affinity " <<
4155 "support, do not set 'cpu_affinity_map'");
4156 self_destruct();
4157#endif /* HAVE_CPU_AFFINITY */
4158
4159 if (!*cpuAffinityMap)
4160 *cpuAffinityMap = new CpuAffinityMap;
4161
2eceb328
CT
4162 const char *const pToken = ConfigParser::NextToken();
4163 const char *const cToken = ConfigParser::NextToken();
c8ea3cc0 4164 std::vector<int> processes, cores;
96c2bb61
AR
4165 if (!parseNamedIntList(pToken, "process_numbers", processes)) {
4166 debugs(3, DBG_CRITICAL, "FATAL: bad 'process_numbers' parameter " <<
4167 "in 'cpu_affinity_map'");
4168 self_destruct();
4169 } else if (!parseNamedIntList(cToken, "cores", cores)) {
4170 debugs(3, DBG_CRITICAL, "FATAL: bad 'cores' parameter in " <<
4171 "'cpu_affinity_map'");
4172 self_destruct();
4173 } else if (!(*cpuAffinityMap)->add(processes, cores)) {
4174 debugs(3, DBG_CRITICAL, "FATAL: bad 'cpu_affinity_map'; " <<
4175 "process_numbers and cores lists differ in length or " <<
4176 "contain numbers <= 0");
4177 self_destruct();
4178 }
4179}
4180
4181static void
212af65c
A
4182dump_CpuAffinityMap(StoreEntry *const entry, const char *const name, const CpuAffinityMap *const cpuAffinityMap)
4183{
96c2bb61
AR
4184 if (cpuAffinityMap) {
4185 storeAppendPrintf(entry, "%s process_numbers=", name);
4186 for (size_t i = 0; i < cpuAffinityMap->processes().size(); ++i) {
4187 storeAppendPrintf(entry, "%s%i", (i ? "," : ""),
4188 cpuAffinityMap->processes()[i]);
4189 }
4190 storeAppendPrintf(entry, " cores=");
eaed60cc 4191 for (size_t i = 0; i < cpuAffinityMap->cores().size(); ++i) {
96c2bb61
AR
4192 storeAppendPrintf(entry, "%s%i", (i ? "," : ""),
4193 cpuAffinityMap->cores()[i]);
4194 }
4195 storeAppendPrintf(entry, "\n");
4196 }
4197}
4198
4199static void
212af65c
A
4200free_CpuAffinityMap(CpuAffinityMap **const cpuAffinityMap)
4201{
96c2bb61
AR
4202 delete *cpuAffinityMap;
4203 *cpuAffinityMap = NULL;
4204}
4205
62c7f90e
AR
4206#if USE_ADAPTATION
4207
4208static void
4209parse_adaptation_service_set_type()
4210{
4211 Adaptation::Config::ParseServiceSet();
4212}
4213
a22e6cd3
AR
4214static void
4215parse_adaptation_service_chain_type()
4216{
4217 Adaptation::Config::ParseServiceChain();
4218}
4219
62c7f90e
AR
4220static void
4221parse_adaptation_access_type()
4222{
4223 Adaptation::Config::ParseAccess(LegacyParser);
4224}
62c7f90e
AR
4225#endif /* USE_ADAPTATION */
4226
3a69ddf3 4227#if ICAP_CLIENT
4228
4229static void
26cc52cb 4230parse_icap_service_type(Adaptation::Icap::Config * cfg)
3a69ddf3 4231{
462e63d3 4232 cfg->parseService();
3a69ddf3 4233}
4234
4235static void
26cc52cb 4236free_icap_service_type(Adaptation::Icap::Config * cfg)
3a69ddf3 4237{
462e63d3 4238 cfg->freeService();
3a69ddf3 4239}
4240
4241static void
26cc52cb 4242dump_icap_service_type(StoreEntry * entry, const char *name, const Adaptation::Icap::Config &cfg)
3a69ddf3 4243{
462e63d3 4244 cfg.dumpService(entry, name);
3a69ddf3 4245}
4246
4247static void
c939dc70 4248parse_icap_class_type()
3a69ddf3 4249{
fa84c01d 4250 debugs(93, DBG_CRITICAL, "WARNING: 'icap_class' is depricated. " <<
26ac0430 4251 "Use 'adaptation_service_set' instead");
62c7f90e 4252 Adaptation::Config::ParseServiceSet();
3a69ddf3 4253}
4254
3a69ddf3 4255static void
c939dc70 4256parse_icap_access_type()
3a69ddf3 4257{
fa84c01d 4258 debugs(93, DBG_CRITICAL, "WARNING: 'icap_access' is depricated. " <<
26ac0430 4259 "Use 'adaptation_access' instead");
21a26d31 4260 Adaptation::Config::ParseAccess(LegacyParser);
3a69ddf3 4261}
4262
3a69ddf3 4263#endif
21a26d31 4264
21a26d31
AR
4265#if USE_ECAP
4266
4267static void
574b508c 4268parse_ecap_service_type(Adaptation::Ecap::Config * cfg)
21a26d31
AR
4269{
4270 cfg->parseService();
4271}
4272
4273static void
574b508c 4274free_ecap_service_type(Adaptation::Ecap::Config * cfg)
21a26d31
AR
4275{
4276 cfg->freeService();
4277}
4278
4279static void
574b508c 4280dump_ecap_service_type(StoreEntry * entry, const char *name, const Adaptation::Ecap::Config &cfg)
21a26d31
AR
4281{
4282 cfg.dumpService(entry, name);
4283}
4284
4285#endif /* USE_ECAP */
8277060a
CT
4286
4287#if ICAP_CLIENT
4288static void parse_icap_service_failure_limit(Adaptation::Icap::Config *cfg)
4289{
4290 char *token;
4291 time_t d;
4292 time_t m;
4293 cfg->service_failure_limit = GetInteger();
4294
2eceb328 4295 if ((token = ConfigParser::NextToken()) == NULL)
8277060a
CT
4296 return;
4297
4298 if (strcmp(token,"in") != 0) {
fa84c01d 4299 debugs(3, DBG_CRITICAL, "expecting 'in' on'" << config_input_line << "'");
8277060a
CT
4300 self_destruct();
4301 }
4302
2eceb328 4303 if ((token = ConfigParser::NextToken()) == NULL) {
8277060a
CT
4304 self_destruct();
4305 }
4306
4307 d = static_cast<time_t> (xatoi(token));
a459e80a 4308
8277060a
CT
4309 m = static_cast<time_t> (1);
4310
4311 if (0 == d)
4312 (void) 0;
2eceb328 4313 else if ((token = ConfigParser::NextToken()) == NULL) {
fa84c01d 4314 debugs(3, DBG_CRITICAL, "No time-units on '" << config_input_line << "'");
8277060a 4315 self_destruct();
9b741834 4316 } else if ((m = parseTimeUnits(token, false)) == 0)
8277060a
CT
4317 self_destruct();
4318
4319 cfg->oldest_service_failure = (m * d);
4320}
4321
4322static void dump_icap_service_failure_limit(StoreEntry *entry, const char *name, const Adaptation::Icap::Config &cfg)
4323{
4324 storeAppendPrintf(entry, "%s %d", name, cfg.service_failure_limit);
4325 if (cfg.oldest_service_failure > 0) {
4326 storeAppendPrintf(entry, " in %d seconds", (int)cfg.oldest_service_failure);
4327 }
4328 storeAppendPrintf(entry, "\n");
4329}
4330
4331static void free_icap_service_failure_limit(Adaptation::Icap::Config *cfg)
4332{
4333 cfg->oldest_service_failure = 0;
4334 cfg->service_failure_limit = 0;
4335}
aebe6888 4336#endif
8277060a 4337
cb4f4424 4338#if USE_OPENSSL
fb2178bb
CT
4339static void parse_sslproxy_cert_adapt(sslproxy_cert_adapt **cert_adapt)
4340{
4341 char *al;
4342 sslproxy_cert_adapt *ca = (sslproxy_cert_adapt *) xcalloc(1, sizeof(sslproxy_cert_adapt));
2eceb328 4343 if ((al = ConfigParser::NextToken()) == NULL) {
fb2178bb
CT
4344 self_destruct();
4345 return;
4346 }
87f237a9 4347
fb2178bb
CT
4348 const char *param;
4349 if ( char *s = strchr(al, '{')) {
4350 *s = '\0'; // terminate the al string
a38ec4b1 4351 ++s;
fb2178bb
CT
4352 param = s;
4353 s = strchr(s, '}');
4354 if (!s) {
4355 self_destruct();
4356 return;
4357 }
4358 *s = '\0';
87f237a9 4359 } else
fb2178bb
CT
4360 param = NULL;
4361
4362 if (strcmp(al, Ssl::CertAdaptAlgorithmStr[Ssl::algSetValidAfter]) == 0) {
4363 ca->alg = Ssl::algSetValidAfter;
b8889258 4364 ca->param = xstrdup("on");
87f237a9 4365 } else if (strcmp(al, Ssl::CertAdaptAlgorithmStr[Ssl::algSetValidBefore]) == 0) {
fb2178bb 4366 ca->alg = Ssl::algSetValidBefore;
b8889258 4367 ca->param = xstrdup("on");
87f237a9 4368 } else if (strcmp(al, Ssl::CertAdaptAlgorithmStr[Ssl::algSetCommonName]) == 0) {
fb2178bb
CT
4369 ca->alg = Ssl::algSetCommonName;
4370 if (param) {
5367d845
CT
4371 if (strlen(param) > 64) {
4372 debugs(3, DBG_CRITICAL, "FATAL: sslproxy_cert_adapt: setCommonName{" <<param << "} : using common name longer than 64 bytes is not supported");
4373 self_destruct();
4374 return;
4375 }
b8889258 4376 ca->param = xstrdup(param);
fb2178bb
CT
4377 }
4378 } else {
4379 debugs(3, DBG_CRITICAL, "FATAL: sslproxy_cert_adapt: unknown cert adaptation algorithm: " << al);
4380 self_destruct();
4381 return;
4382 }
4383
6f58d7d7 4384 aclParseAclList(LegacyParser, &ca->aclList, al);
fb2178bb 4385
87f237a9 4386 while (*cert_adapt)
fb2178bb
CT
4387 cert_adapt = &(*cert_adapt)->next;
4388
4389 *cert_adapt = ca;
4390}
4391
4392static void dump_sslproxy_cert_adapt(StoreEntry *entry, const char *name, sslproxy_cert_adapt *cert_adapt)
4393{
4394 for (sslproxy_cert_adapt *ca = cert_adapt; ca != NULL; ca = ca->next) {
4395 storeAppendPrintf(entry, "%s ", name);
4396 storeAppendPrintf(entry, "%s{%s} ", Ssl::sslCertAdaptAlgoritm(ca->alg), ca->param);
4397 if (ca->aclList)
4398 dump_acl_list(entry, ca->aclList);
4399 storeAppendPrintf(entry, "\n");
4400 }
4401}
4402
4403static void free_sslproxy_cert_adapt(sslproxy_cert_adapt **cert_adapt)
4404{
87f237a9 4405 while (*cert_adapt) {
fb2178bb
CT
4406 sslproxy_cert_adapt *ca = *cert_adapt;
4407 *cert_adapt = ca->next;
4408 safe_free(ca->param);
4409
4410 if (ca->aclList)
4411 aclDestroyAclList(&ca->aclList);
4412
4413 safe_free(ca);
4414 }
4415}
aebe6888
CT
4416
4417static void parse_sslproxy_cert_sign(sslproxy_cert_sign **cert_sign)
4418{
4419 char *al;
4420 sslproxy_cert_sign *cs = (sslproxy_cert_sign *) xcalloc(1, sizeof(sslproxy_cert_sign));
2eceb328 4421 if ((al = ConfigParser::NextToken()) == NULL) {
aebe6888
CT
4422 self_destruct();
4423 return;
4424 }
4425
4426 if (strcmp(al, Ssl::CertSignAlgorithmStr[Ssl::algSignTrusted]) == 0)
4427 cs->alg = Ssl::algSignTrusted;
4428 else if (strcmp(al, Ssl::CertSignAlgorithmStr[Ssl::algSignUntrusted]) == 0)
4429 cs->alg = Ssl::algSignUntrusted;
4430 else if (strcmp(al, Ssl::CertSignAlgorithmStr[Ssl::algSignSelf]) == 0)
4431 cs->alg = Ssl::algSignSelf;
4432 else {
4433 debugs(3, DBG_CRITICAL, "FATAL: sslproxy_cert_sign: unknown cert signing algorithm: " << al);
4434 self_destruct();
4435 return;
4436 }
4437
6f58d7d7 4438 aclParseAclList(LegacyParser, &cs->aclList, al);
aebe6888 4439
87f237a9 4440 while (*cert_sign)
aebe6888
CT
4441 cert_sign = &(*cert_sign)->next;
4442
4443 *cert_sign = cs;
4444}
4445
4446static void dump_sslproxy_cert_sign(StoreEntry *entry, const char *name, sslproxy_cert_sign *cert_sign)
4447{
4448 sslproxy_cert_sign *cs;
4449 for (cs = cert_sign; cs != NULL; cs = cs->next) {
4450 storeAppendPrintf(entry, "%s ", name);
4451 storeAppendPrintf(entry, "%s ", Ssl::certSignAlgorithm(cs->alg));
4452 if (cs->aclList)
4453 dump_acl_list(entry, cs->aclList);
4454 storeAppendPrintf(entry, "\n");
4455 }
4456}
4457
4458static void free_sslproxy_cert_sign(sslproxy_cert_sign **cert_sign)
4459{
87f237a9 4460 while (*cert_sign) {
aebe6888
CT
4461 sslproxy_cert_sign *cs = *cert_sign;
4462 *cert_sign = cs->next;
4463
4464 if (cs->aclList)
4465 aclDestroyAclList(&cs->aclList);
4466
4467 safe_free(cs);
4468 }
4469}
fb2178bb 4470
38450a50
CT
4471class sslBumpCfgRr: public ::RegisteredRunner
4472{
4473public:
4474 static Ssl::BumpMode lastDeprecatedRule;
4475 /* RegisteredRunner API */
21b7990f 4476 virtual void finalizeConfig();
38450a50
CT
4477};
4478
4479Ssl::BumpMode sslBumpCfgRr::lastDeprecatedRule = Ssl::bumpEnd;
4480
21b7990f 4481RunnerRegistrationEntry(sslBumpCfgRr);
38450a50 4482
21b7990f
AR
4483void
4484sslBumpCfgRr::finalizeConfig()
38450a50
CT
4485{
4486 if (lastDeprecatedRule != Ssl::bumpEnd) {
4487 assert( lastDeprecatedRule == Ssl::bumpClientFirst || lastDeprecatedRule == Ssl::bumpNone);
4488 static char buf[1024];
4489 if (lastDeprecatedRule == Ssl::bumpClientFirst) {
4490 strcpy(buf, "ssl_bump deny all");
4491 debugs(3, DBG_CRITICAL, "WARNING: auto-converting deprecated implicit "
4492 "\"ssl_bump deny all\" to \"ssl_bump none all\". New ssl_bump configurations "
4493 "must not use implicit rules. Update your ssl_bump rules.");
4494 } else {
4495 strcpy(buf, "ssl_bump allow all");
4496 debugs(3, DBG_CRITICAL, "SECURITY NOTICE: auto-converting deprecated implicit "
4497 "\"ssl_bump allow all\" to \"ssl_bump client-first all\" which is usually "
4498 "inferior to the newer server-first bumping mode. New ssl_bump"
4499 " configurations must not use implicit rules. Update your ssl_bump rules.");
4500 }
4501 parse_line(buf);
4502 }
4503}
4504
caf3666d
AR
4505static void parse_sslproxy_ssl_bump(acl_access **ssl_bump)
4506{
38450a50
CT
4507 typedef const char *BumpCfgStyle;
4508 BumpCfgStyle bcsNone = NULL;
4509 BumpCfgStyle bcsNew = "new client/server-first/none";
4510 BumpCfgStyle bcsOld = "deprecated allow/deny";
4511 static BumpCfgStyle bumpCfgStyleLast = bcsNone;
4512 BumpCfgStyle bumpCfgStyleNow = bcsNone;
caf3666d 4513 char *bm;
2eceb328 4514 if ((bm = ConfigParser::NextToken()) == NULL) {
caf3666d
AR
4515 self_destruct();
4516 return;
4517 }
4518
38450a50
CT
4519 // if this is the first rule proccessed
4520 if (*ssl_bump == NULL) {
4521 bumpCfgStyleLast = bcsNone;
4522 sslBumpCfgRr::lastDeprecatedRule = Ssl::bumpEnd;
4523 }
4524
6f58d7d7 4525 allow_t action = allow_t(ACCESS_ALLOWED);
caf3666d 4526
38450a50 4527 if (strcmp(bm, Ssl::BumpModeStr[Ssl::bumpClientFirst]) == 0) {
6f58d7d7 4528 action.kind = Ssl::bumpClientFirst;
38450a50
CT
4529 bumpCfgStyleNow = bcsNew;
4530 } else if (strcmp(bm, Ssl::BumpModeStr[Ssl::bumpServerFirst]) == 0) {
6f58d7d7 4531 action.kind = Ssl::bumpServerFirst;
38450a50 4532 bumpCfgStyleNow = bcsNew;
5d65362c
CT
4533 } else if (strcmp(bm, Ssl::BumpModeStr[Ssl::bumpPeek]) == 0) {
4534 action.kind = Ssl::bumpPeek;
4535 bumpCfgStyleNow = bcsNew;
4536 } else if (strcmp(bm, Ssl::BumpModeStr[Ssl::bumpStare]) == 0) {
4537 action.kind = Ssl::bumpStare;
4538 bumpCfgStyleNow = bcsNew;
4539 } else if (strcmp(bm, Ssl::BumpModeStr[Ssl::bumpSplice]) == 0) {
4540 action.kind = Ssl::bumpSplice;
4541 bumpCfgStyleNow = bcsNew;
4542 } else if (strcmp(bm, Ssl::BumpModeStr[Ssl::bumpBump]) == 0) {
4543 action.kind = Ssl::bumpBump;
4544 bumpCfgStyleNow = bcsNew;
4545 } else if (strcmp(bm, Ssl::BumpModeStr[Ssl::bumpTerminate]) == 0) {
4546 action.kind = Ssl::bumpTerminate;
4547 bumpCfgStyleNow = bcsNew;
38450a50 4548 } else if (strcmp(bm, Ssl::BumpModeStr[Ssl::bumpNone]) == 0) {
6f58d7d7 4549 action.kind = Ssl::bumpNone;
38450a50
CT
4550 bumpCfgStyleNow = bcsNew;
4551 } else if (strcmp(bm, "allow") == 0) {
4552 debugs(3, DBG_CRITICAL, "SECURITY NOTICE: auto-converting deprecated "
4553 "\"ssl_bump allow <acl>\" to \"ssl_bump client-first <acl>\" which "
4554 "is usually inferior to the newer server-first "
4555 "bumping mode. Update your ssl_bump rules.");
6f58d7d7 4556 action.kind = Ssl::bumpClientFirst;
38450a50
CT
4557 bumpCfgStyleNow = bcsOld;
4558 sslBumpCfgRr::lastDeprecatedRule = Ssl::bumpClientFirst;
4559 } else if (strcmp(bm, "deny") == 0) {
4560 debugs(3, DBG_CRITICAL, "WARNING: auto-converting deprecated "
4561 "\"ssl_bump deny <acl>\" to \"ssl_bump none <acl>\". Update "
4562 "your ssl_bump rules.");
6f58d7d7 4563 action.kind = Ssl::bumpNone;
38450a50
CT
4564 bumpCfgStyleNow = bcsOld;
4565 sslBumpCfgRr::lastDeprecatedRule = Ssl::bumpNone;
caf3666d
AR
4566 } else {
4567 debugs(3, DBG_CRITICAL, "FATAL: unknown ssl_bump mode: " << bm);
4568 self_destruct();
4569 return;
4570 }
4571
38450a50 4572 if (bumpCfgStyleLast != bcsNone && bumpCfgStyleNow != bumpCfgStyleLast) {
87f237a9 4573 debugs(3, DBG_CRITICAL, "FATAL: do not mix " << bumpCfgStyleNow << " actions with " <<
38450a50
CT
4574 bumpCfgStyleLast << " actions. Update your ssl_bump rules.");
4575 self_destruct();
4576 return;
4577 }
4578
4579 bumpCfgStyleLast = bumpCfgStyleNow;
4580
8984b8cd 4581 Acl::AndNode *rule = new Acl::AndNode;
6f58d7d7 4582 rule->context("(ssl_bump rule)", config_input_line);
8984b8cd
CT
4583 rule->lineParse();
4584 // empty rule OK
6f58d7d7
AR
4585
4586 assert(ssl_bump);
4587 if (!*ssl_bump) {
4588 *ssl_bump = new Acl::Tree;
4589 (*ssl_bump)->context("(ssl_bump rules)", config_input_line);
4590 }
caf3666d 4591
6f58d7d7 4592 (*ssl_bump)->add(rule, action);
caf3666d
AR
4593}
4594
4595static void dump_sslproxy_ssl_bump(StoreEntry *entry, const char *name, acl_access *ssl_bump)
4596{
2cb8d372
FC
4597 if (ssl_bump)
4598 dump_SBufList(entry, ssl_bump->treeDump(name, Ssl::BumpModeStr));
caf3666d
AR
4599}
4600
4601static void free_sslproxy_ssl_bump(acl_access **ssl_bump)
4602{
4603 free_acl_access(ssl_bump);
4604}
8277060a
CT
4605
4606#endif
f4698e0b
CT
4607
4608static void dump_HeaderWithAclList(StoreEntry * entry, const char *name, HeaderWithAclList *headers)
4609{
4610 if (!headers)
4611 return;
4612
4613 for (HeaderWithAclList::iterator hwa = headers->begin(); hwa != headers->end(); ++hwa) {
ced8def3 4614 storeAppendPrintf(entry, "%s %s %s", name, hwa->fieldName.c_str(), hwa->fieldValue.c_str());
f4698e0b
CT
4615 if (hwa->aclList)
4616 dump_acl_list(entry, hwa->aclList);
4617 storeAppendPrintf(entry, "\n");
4618 }
4619}
4620
4621static void parse_HeaderWithAclList(HeaderWithAclList **headers)
4622{
4623 char *fn;
4624 if (!*headers) {
4625 *headers = new HeaderWithAclList;
4626 }
2eceb328 4627 if ((fn = ConfigParser::NextToken()) == NULL) {
f4698e0b
CT
4628 self_destruct();
4629 return;
4630 }
4631 HeaderWithAcl hwa;
4632 hwa.fieldName = fn;
81ab22b6 4633 hwa.fieldId = Http::HeaderLookupTable.lookup(hwa.fieldName).id;
789217a2
FC
4634 if (hwa.fieldId == Http::HdrType::BAD_HDR)
4635 hwa.fieldId = Http::HdrType::OTHER;
f4698e0b 4636
2eceb328
CT
4637 Format::Format *nlf = new ::Format::Format("hdrWithAcl");
4638 ConfigParser::EnableMacros();
bde7a8ce 4639 String buf = ConfigParser::NextQuotedToken();
2eceb328 4640 ConfigParser::DisableMacros();
f4698e0b 4641 hwa.fieldValue = buf.termedBuf();
2eceb328 4642 hwa.quoted = ConfigParser::LastTokenWasQuoted();
f4698e0b 4643 if (hwa.quoted) {
f4698e0b
CT
4644 if (!nlf->parse(hwa.fieldValue.c_str())) {
4645 self_destruct();
4646 return;
4647 }
4648 hwa.valueFormat = nlf;
2eceb328
CT
4649 } else
4650 delete nlf;
6f58d7d7 4651 aclParseAclList(LegacyParser, &hwa.aclList, (hwa.fieldName + ':' + hwa.fieldValue).c_str());
f4698e0b
CT
4652 (*headers)->push_back(hwa);
4653}
4654
4655static void free_HeaderWithAclList(HeaderWithAclList **header)
4656{
4657 if (!(*header))
4658 return;
4659
4660 for (HeaderWithAclList::iterator hwa = (*header)->begin(); hwa != (*header)->end(); ++hwa) {
4661 if (hwa->aclList)
4662 aclDestroyAclList(&hwa->aclList);
4663
4664 if (hwa->valueFormat) {
4665 delete hwa->valueFormat;
4666 hwa->valueFormat = NULL;
4667 }
4668 }
4669 delete *header;
4670 *header = NULL;
4671}
d7f4a0b7
CT
4672
4673static void parse_note(Notes *notes)
4674{
4675 assert(notes);
4676 notes->parse(LegacyParser);
4677}
4678
4679static void dump_note(StoreEntry *entry, const char *name, Notes &notes)
4680{
4681 notes.dump(entry, name);
4682}
4683
4684static void free_note(Notes *notes)
4685{
4686 notes->clean();
4687}
bde7a8ce 4688
ddf5aa2b
CT
4689static bool FtpEspvDeprecated = false;
4690static void parse_ftp_epsv(acl_access **ftp_epsv)
4691{
4692 allow_t ftpEpsvDeprecatedAction;
4693 bool ftpEpsvIsDeprecatedRule = false;
4694
4695 char *t = ConfigParser::PeekAtToken();
e2849af8 4696 if (!t) {
ddf5aa2b
CT
4697 self_destruct();
4698 return;
4699 }
4700
4701 if (!strcmp(t, "off")) {
4702 (void)ConfigParser::NextToken();
4703 ftpEpsvIsDeprecatedRule = true;
4704 ftpEpsvDeprecatedAction = allow_t(ACCESS_DENIED);
4705 } else if (!strcmp(t, "on")) {
4706 (void)ConfigParser::NextToken();
4707 ftpEpsvIsDeprecatedRule = true;
4708 ftpEpsvDeprecatedAction = allow_t(ACCESS_ALLOWED);
4709 }
4710
4711 // Check for mixing "ftp_epsv on|off" and "ftp_epsv allow|deny .." rules:
4712 // 1) if this line is "ftp_epsv allow|deny ..." and already exist rules of "ftp_epsv on|off"
4713 // 2) if this line is "ftp_epsv on|off" and already exist rules of "ftp_epsv allow|deny ..."
4714 // then abort
4715 if ((!ftpEpsvIsDeprecatedRule && FtpEspvDeprecated) ||
e2849af8 4716 (ftpEpsvIsDeprecatedRule && !FtpEspvDeprecated && *ftp_epsv != NULL)) {
ddf5aa2b
CT
4717 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.");
4718 self_destruct();
4719 }
4720
4721 if (ftpEpsvIsDeprecatedRule) {
4722 // overwrite previous ftp_epsv lines
4723 delete *ftp_epsv;
4724 if (ftpEpsvDeprecatedAction == allow_t(ACCESS_DENIED)) {
4725 Acl::AndNode *ftpEpsvRule = new Acl::AndNode;
4726 ftpEpsvRule->context("(ftp_epsv rule)", config_input_line);
4727 ACL *a = ACL::FindByName("all");
4728 if (!a) {
4729 self_destruct();
4730 return;
4731 }
4732 ftpEpsvRule->add(a);
4733 *ftp_epsv = new Acl::Tree;
4734 (*ftp_epsv)->context("(ftp_epsv rules)", config_input_line);
4735 (*ftp_epsv)->add(ftpEpsvRule, ftpEpsvDeprecatedAction);
4736 } else
4737 *ftp_epsv = NULL;
4738 FtpEspvDeprecated = true;
4739 } else {
4740 aclParseAccessLine(cfg_directive, LegacyParser, ftp_epsv);
4741 }
4742}
4743
4744static void dump_ftp_epsv(StoreEntry *entry, const char *name, acl_access *ftp_epsv)
4745{
dfad5100
FC
4746 if (ftp_epsv)
4747 dump_SBufList(entry, ftp_epsv->treeDump(name, NULL));
ddf5aa2b
CT
4748}
4749
4750static void free_ftp_epsv(acl_access **ftp_epsv)
4751{
4752 free_acl_access(ftp_epsv);
4753 FtpEspvDeprecated = false;
4754}
4755
bde7a8ce 4756static void
ced8def3 4757parse_UrlHelperTimeout(SquidConfig::UrlHelperTimeout *config)
32fd6d8a
CT
4758{
4759 time_msec_t tval;
4760 parseTimeLine(&tval, T_SECOND_STR, false, true);
4761 Config.Timeout.urlRewrite = static_cast<time_t>(tval/1000);
4762
4763 char *key, *value;
4764 while(ConfigParser::NextKvPair(key, value)) {
4765 if (strcasecmp(key, "on_timeout") == 0) {
4766 if (strcasecmp(value, "bypass") == 0)
ced8def3 4767 config->action = toutActBypass;
32fd6d8a 4768 else if (strcasecmp(value, "fail") == 0)
ced8def3 4769 config->action = toutActFail;
32fd6d8a 4770 else if (strcasecmp(value, "retry") == 0)
ced8def3 4771 config->action = toutActRetry;
32fd6d8a 4772 else if (strcasecmp(value, "use_configured_response") == 0) {
ced8def3 4773 config->action = toutActUseConfiguredResponse;
32fd6d8a
CT
4774 } else {
4775 debugs(3, DBG_CRITICAL, "FATAL: unsuported \"on_timeout\" action:" << value);
4776 self_destruct();
4777 }
4778 } else if (strcasecmp(key, "response") == 0) {
ced8def3 4779 config->response = xstrdup(value);
32fd6d8a
CT
4780 } else {
4781 debugs(3, DBG_CRITICAL, "FATAL: unsuported option " << key);
4782 self_destruct();
4783 }
4784 }
4785
ced8def3 4786 if (config->action == toutActUseConfiguredResponse && !config->response) {
32fd6d8a
CT
4787 debugs(3, DBG_CRITICAL, "FATAL: Expected 'response=' option after 'on_timeout=use_configured_response' option");
4788 self_destruct();
4789 }
4790
ced8def3 4791 if (config->action != toutActUseConfiguredResponse && config->response) {
32fd6d8a
CT
4792 debugs(3, DBG_CRITICAL, "FATAL: 'response=' option is valid only when used with the 'on_timeout=use_configured_response' option");
4793 self_destruct();
4794 }
4795}
4796
4797static void
ced8def3 4798dump_UrlHelperTimeout(StoreEntry *entry, const char *name, SquidConfig::UrlHelperTimeout &config)
32fd6d8a
CT
4799{
4800 const char *onTimedOutActions[] = {"bypass", "fail", "retry", "use_configured_response"};
ced8def3 4801 assert(config.action >= 0 && config.action <= toutActUseConfiguredResponse);
32fd6d8a
CT
4802
4803 dump_time_t(entry, name, Config.Timeout.urlRewrite);
ced8def3 4804 storeAppendPrintf(entry, " on_timeout=%s", onTimedOutActions[config.action]);
32fd6d8a 4805
ced8def3
AJ
4806 if (config.response)
4807 storeAppendPrintf(entry, " response=\"%s\"", config.response);
32fd6d8a
CT
4808
4809 storeAppendPrintf(entry, "\n");
4810}
4811
4812static void
ced8def3 4813free_UrlHelperTimeout(SquidConfig::UrlHelperTimeout *config)
32fd6d8a
CT
4814{
4815 Config.Timeout.urlRewrite = 0;
ced8def3
AJ
4816 config->action = 0;
4817 safe_free(config->response);
32fd6d8a
CT
4818}
4819
bde7a8ce 4820static void
ced8def3 4821parse_configuration_includes_quoted_values(bool *)
bde7a8ce
CT
4822{
4823 int val = 0;
4824 parse_onoff(&val);
4825
4826 // If quoted values is set to on then enable new strict mode parsing
4827 if (val) {
4828 ConfigParser::RecognizeQuotedValues = true;
4829 ConfigParser::StrictMode = true;
4830 } else {
4831 ConfigParser::RecognizeQuotedValues = false;
4832 ConfigParser::StrictMode = false;
4833 }
4834}
4835
4836static void
ced8def3 4837dump_configuration_includes_quoted_values(StoreEntry *const entry, const char *const name, bool)
bde7a8ce
CT
4838{
4839 int val = ConfigParser::RecognizeQuotedValues ? 1 : 0;
4840 dump_onoff(entry, name, val);
4841}
4842
4843static void
ced8def3 4844free_configuration_includes_quoted_values(bool *)
bde7a8ce
CT
4845{
4846 ConfigParser::RecognizeQuotedValues = false;
4847 ConfigParser::StrictMode = false;
4848}
f53969cc 4849
3248e962
CT
4850static void
4851parse_on_unsupported_protocol(acl_access **access)
4852{
44352c16 4853 char *tm;
3248e962
CT
4854 if ((tm = ConfigParser::NextToken()) == NULL) {
4855 self_destruct();
4856 return;
4857 }
4858
4859 allow_t action = allow_t(ACCESS_ALLOWED);
4860 if (strcmp(tm, "tunnel") == 0)
4861 action.kind = 1;
4862 else if (strcmp(tm, "respond") == 0)
4863 action.kind = 2;
4864 else {
4865 debugs(3, DBG_CRITICAL, "FATAL: unknown on_unsupported_protocol mode: " << tm);
4866 self_destruct();
4867 return;
4868 }
4869
4870 Acl::AndNode *rule = new Acl::AndNode;
4871 rule->context("(on_unsupported_protocol rule)", config_input_line);
4872 rule->lineParse();
4873 // empty rule OK
4874
4875 assert(access);
4876 if (!*access) {
4877 *access = new Acl::Tree;
4878 (*access)->context("(on_unsupported_protocol rules)", config_input_line);
4879 }
4880
4881 (*access)->add(rule, action);
4882}
4883
4884static void
4885dump_on_unsupported_protocol(StoreEntry *entry, const char *name, acl_access *access)
4886{
4887 const char *on_error_tunnel_mode_str[] = {
4888 "none",
4889 "tunnel",
4890 "respond",
4891 NULL
4892 };
4893 if (access) {
4894 SBufList lines = access->treeDump(name, on_error_tunnel_mode_str);
4895 dump_SBufList(entry, lines);
4896 }
4897}
4898
4899static void
4900free_on_unsupported_protocol(acl_access **access)
4901{
4902 free_acl_access(access);
4903}
44352c16 4904