]> git.ipfire.org Git - thirdparty/squid.git/blame - src/cache_cf.cc
Prep for 3.1.9
[thirdparty/squid.git] / src / cache_cf.cc
CommitLineData
30a4f2a8 1/*
262a0e14 2 * $Id$
30a4f2a8 3 *
b510f3a1 4 * DEBUG: section 03 Configuration File Parsing
30a4f2a8 5 * AUTHOR: Harvest Derived
6 *
2b6662ba 7 * SQUID Web Proxy Cache http://www.squid-cache.org/
e25c139f 8 * ----------------------------------------------------------
30a4f2a8 9 *
2b6662ba 10 * Squid is the result of efforts by numerous individuals from
11 * the Internet community; see the CONTRIBUTORS file for full
12 * details. Many organizations have provided support for Squid's
13 * development; see the SPONSORS file for full details. Squid is
14 * Copyrighted (C) 2001 by the Regents of the University of
15 * California; see the COPYRIGHT file for full details. Squid
16 * incorporates software developed and/or copyrighted by other
17 * sources; see the CREDITS file for full details.
30a4f2a8 18 *
19 * This program is free software; you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation; either version 2 of the License, or
22 * (at your option) any later version.
26ac0430 23 *
30a4f2a8 24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
26ac0430 28 *
30a4f2a8 29 * You should have received a copy of the GNU General Public License
30 * along with this program; if not, write to the Free Software
cbdec147 31 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
e25c139f 32 *
30a4f2a8 33 */
cf5fd929 34
44a47c6e 35#include "squid.h"
82b7abe3
AJ
36
37#include "acl/Acl.h"
38#include "acl/Gadgets.h"
39#include "acl/MethodData.h"
40#if USE_ADAPTATION
41#include "adaptation/Config.h"
42#endif
43#if ICAP_CLIENT
44#include "adaptation/icap/Config.h"
45#endif
46#if USE_ECAP
47#include "adaptation/ecap/Config.h"
48#endif
2d2b0bb7
AR
49#include "auth/Config.h"
50#include "auth/Scheme.h"
62ee09ca 51#include "CacheManager.h"
a80a77cf 52#include "ConfigParser.h"
96c2bb61 53#include "CpuAffinityMap.h"
82b7abe3
AJ
54#include "eui/Config.h"
55#if USE_SQUID_ESI
56#include "esi/Parser.h"
57#endif
58#include "HttpRequestMethod.h"
59#include "ident/Config.h"
96d89ea0 60#include "ip/Intercept.h"
b7ac5457 61#include "ip/QosConfig.h"
055421ee 62#include "ip/tools.h"
82b7abe3
AJ
63#include "log/Config.h"
64#include "MemBuf.h"
c8f4eac4 65#include "Parsing.h"
82b7abe3 66#include "ProtoPort.h"
1fa9b1a7 67#include "rfc1738.h"
82b7abe3
AJ
68#if SQUID_SNMP
69#include "snmp.h"
70#endif
71#include "Store.h"
72#include "StoreFileSystem.h"
73#include "SwapDir.h"
d295d770 74#include "wordlist.h"
40daaeb8 75#include "ipc/Kids.h"
04f87469 76
52303a3d 77#if HAVE_GLOB_H
592a09dc 78#include <glob.h>
52303a3d 79#endif
1df370e3 80
425de4c8
AJ
81#if HAVE_LIMITS_H
82#include <limits>
83#endif
84
62c7f90e 85#if USE_ADAPTATION
62c7f90e 86static void parse_adaptation_service_set_type();
a22e6cd3 87static void parse_adaptation_service_chain_type();
62c7f90e 88static void parse_adaptation_access_type();
62c7f90e
AR
89#endif
90
3a69ddf3 91#if ICAP_CLIENT
26cc52cb
AR
92static void parse_icap_service_type(Adaptation::Icap::Config *);
93static void dump_icap_service_type(StoreEntry *, const char *, const Adaptation::Icap::Config &);
94static void free_icap_service_type(Adaptation::Icap::Config *);
c939dc70
AR
95static void parse_icap_class_type();
96static void parse_icap_access_type();
8277060a
CT
97
98static void parse_icap_service_failure_limit(Adaptation::Icap::Config *);
99static void dump_icap_service_failure_limit(StoreEntry *, const char *, const Adaptation::Icap::Config &);
100static void free_icap_service_failure_limit(Adaptation::Icap::Config *);
3a69ddf3 101#endif
102
21a26d31 103#if USE_ECAP
574b508c
AR
104static void parse_ecap_service_type(Adaptation::Ecap::Config *);
105static void dump_ecap_service_type(StoreEntry *, const char *, const Adaptation::Ecap::Config &);
106static void free_ecap_service_type(Adaptation::Ecap::Config *);
21a26d31
AR
107#endif
108
f5691f9c 109CBDATA_TYPE(peer);
110
8813e606 111static const char *const T_SECOND_STR = "second";
112static const char *const T_MINUTE_STR = "minute";
113static const char *const T_HOUR_STR = "hour";
114static const char *const T_DAY_STR = "day";
115static const char *const T_WEEK_STR = "week";
116static const char *const T_FORTNIGHT_STR = "fortnight";
117static const char *const T_MONTH_STR = "month";
118static const char *const T_YEAR_STR = "year";
119static const char *const T_DECADE_STR = "decade";
aa0a0c7c 120
9906e724 121static const char *const B_BYTES_STR = "bytes";
122static const char *const B_KBYTES_STR = "KB";
123static const char *const B_MBYTES_STR = "MB";
124static const char *const B_GBYTES_STR = "GB";
125
4db43fab 126static const char *const list_sep = ", \t\n\r";
b0e7d2d5 127
7684c4b1 128static void parse_logformat(logformat ** logformat_definitions);
129static void parse_access_log(customlog ** customlog_definitions);
d64bef4c 130static int check_null_access_log(customlog *customlog_definitions);
131
7684c4b1 132static void dump_logformat(StoreEntry * entry, const char *name, logformat * definitions);
133static void dump_access_log(StoreEntry * entry, const char *name, customlog * definitions);
134static void free_logformat(logformat ** definitions);
135static void free_access_log(customlog ** definitions);
136
cd748f27 137static void update_maxobjsize(void);
f5b8bbc4 138static void configDoConfigure(void);
139static void parse_refreshpattern(refresh_t **);
140static int parseTimeUnits(const char *unit);
141static void parseTimeLine(time_t * tptr, const char *units);
59715b38 142static void parse_ushort(u_short * var);
f5b8bbc4 143static void parse_string(char **);
f5b8bbc4 144static void default_all(void);
145static void defaults_if_none(void);
146static int parse_line(char *);
76f44481 147static void parse_obsolete(const char *);
f5b8bbc4 148static void parseBytesLine(size_t * bptr, const char *units);
e210930b
AJ
149#if !USE_DNSSERVERS
150static void parseBytesLineSigned(ssize_t * bptr, const char *units);
151#endif
f5b8bbc4 152static size_t parseBytesUnits(const char *unit);
f5b8bbc4 153static void free_all(void);
94439e4e 154void requirePathnameExists(const char *name, const char *path);
ed7f5615 155static OBJH dump_config;
626096be 156#if USE_HTTP_VIOLATIONS
6bccf575 157static void dump_http_header_access(StoreEntry * entry, const char *name, header_mangler header[]);
158static void parse_http_header_access(header_mangler header[]);
159static void free_http_header_access(header_mangler header[]);
160static void dump_http_header_replace(StoreEntry * entry, const char *name, header_mangler header[]);
161static void parse_http_header_replace(header_mangler * header);
162static void free_http_header_replace(header_mangler * header);
9e8b2f1c 163#endif
6bccf575 164static void parse_denyinfo(acl_deny_info_list ** var);
165static void dump_denyinfo(StoreEntry * entry, const char *name, acl_deny_info_list * var);
166static void free_denyinfo(acl_deny_info_list ** var);
e1a88700 167
0b0cfcf2 168#if USE_WCCPv2
b7ac5457
AJ
169static void parse_IpAddress_list(Ip::Address_list **);
170static void dump_IpAddress_list(StoreEntry *, const char *, const Ip::Address_list *);
171static void free_IpAddress_list(Ip::Address_list **);
0b0cfcf2 172#if CURRENTLY_UNUSED
b7ac5457 173static int check_null_IpAddress_list(const Ip::Address_list *);
3f38a55e 174#endif /* CURRENTLY_UNUSED */
e1a88700 175#endif /* USE_WCCPv2 */
176
3f38a55e 177static void parse_http_port_list(http_port_list **);
178static void dump_http_port_list(StoreEntry *, const char *, const http_port_list *);
179static void free_http_port_list(http_port_list **);
e1a88700 180
d193a436 181#if USE_SSL
182static void parse_https_port_list(https_port_list **);
183static void dump_https_port_list(StoreEntry *, const char *, const https_port_list *);
184static void free_https_port_list(https_port_list **);
185#if 0
186static int check_null_https_port_list(const https_port_list *);
187#endif
188#endif /* USE_SSL */
e1a88700 189
ef1955a5 190static void parse_b_size_t(size_t * var);
47f6e231 191static void parse_b_int64_t(int64_t * var);
270b86af 192
96c2bb61
AR
193static bool parseNamedIntList(const char *data, const String &name, Vector<int> &list);
194
195static void parse_CpuAffinityMap(CpuAffinityMap **const cpuAffinityMap);
196static void dump_CpuAffinityMap(StoreEntry *const entry, const char *const name, const CpuAffinityMap *const cpuAffinityMap);
197static void free_CpuAffinityMap(CpuAffinityMap **const cpuAffinityMap);
198
cfcb6b30 199static int parseOneConfigFile(const char *file_name, unsigned int depth);
200
a385afc3 201/*
202 * LegacyParser is a parser for legacy code that uses the global
203 * approach. This is static so that it is only exposed to cache_cf.
204 * Other modules needing access to a ConfigParser should have it
205 * provided to them in their parserFOO methods.
206 */
207static ConfigParser LegacyParser = ConfigParser();
a9f20260 208
0e4e0e7d 209void
0673c0ba 210self_destruct(void)
090089c4 211{
a9f20260 212 LegacyParser.destruct();
6b8e7481 213}
214
cd748f27 215static void
216update_maxobjsize(void)
217{
218 int i;
47f6e231 219 int64_t ms = -1;
cd748f27 220
221 for (i = 0; i < Config.cacheSwap.n_configured; i++) {
c8f4eac4 222 assert (Config.cacheSwap.swapDirs[i].getRaw());
62e76326 223
c8f4eac4 224 if (dynamic_cast<SwapDir *>(Config.cacheSwap.swapDirs[i].getRaw())->
225 max_objsize > ms)
226 ms = dynamic_cast<SwapDir *>(Config.cacheSwap.swapDirs[i].getRaw())->max_objsize;
cd748f27 227 }
228 store_maxobjsize = ms;
229}
230
76b9daa5 231static void
232SetConfigFilename(char const *file_name, bool is_pipe)
233{
234 cfg_filename = file_name;
235
236 char const *token;
237
238 if (is_pipe)
239 cfg_filename = file_name + 1;
240 else if ((token = strrchr(cfg_filename, '/')))
241 cfg_filename = token + 1;
242}
243
130bff33 244static const char*
245skip_ws(const char* s)
246{
247 while (xisspace(*s))
248 ++s;
249
250 return s;
251}
252
cfcb6b30 253static int
254parseManyConfigFiles(char* files, int depth)
255{
256 int error_count = 0;
d7ac36b9 257 char* saveptr = NULL;
52303a3d 258#if HAVE_GLOB
592a09dc 259 char *path;
260 glob_t globbuf;
261 int i;
262 memset(&globbuf, 0, sizeof(globbuf));
263 for (path = strwordtok(files, &saveptr); path; path = strwordtok(NULL, &saveptr)) {
26ac0430
AJ
264 if (glob(path, globbuf.gl_pathc ? GLOB_APPEND : 0, NULL, &globbuf) != 0) {
265 fatalf("Unable to find configuration file: %s: %s",
266 path, xstrerror());
267 }
268 }
592a09dc 269 for (i = 0; i < (int)globbuf.gl_pathc; i++) {
26ac0430 270 error_count += parseOneConfigFile(globbuf.gl_pathv[i], depth);
cfcb6b30 271 }
592a09dc 272 globfree(&globbuf);
52303a3d 273#else
274 char* file = strwordtok(files, &saveptr);
275 while (file != NULL) {
26ac0430
AJ
276 error_count += parseOneConfigFile(file, depth);
277 file = strwordtok(NULL, &saveptr);
52303a3d 278 }
279#endif /* HAVE_GLOB */
cfcb6b30 280 return error_count;
281}
282
d4a3e179
AR
283static void
284ReplaceSubstr(char*& str, int& len, unsigned substrIdx, unsigned substrLen, const char* newSubstr)
285{
286 assert(str != NULL);
287 assert(newSubstr != NULL);
288
289 unsigned newSubstrLen = strlen(newSubstr);
290 if (newSubstrLen > substrLen)
291 str = (char*)realloc(str, len - substrLen + newSubstrLen + 1);
292
293 // move tail part including zero
294 memmove(str + substrIdx + newSubstrLen, str + substrIdx + substrLen, len - substrIdx - substrLen + 1);
295 // copy new substring in place
296 memcpy(str + substrIdx, newSubstr, newSubstrLen);
297
298 len = strlen(str);
299}
300
301static void
302SubstituteMacro(char*& line, int& len, const char* macroName, const char* substStr)
303{
304 assert(line != NULL);
305 assert(macroName != NULL);
306 assert(substStr != NULL);
307 unsigned macroNameLen = strlen(macroName);
308 while (const char* macroPos = strstr(line, macroName)) // we would replace all occurrences
309 ReplaceSubstr(line, len, macroPos - line, macroNameLen, substStr);
310}
311
312static void
313ProcessMacros(char*& line, int& len)
314{
9de6c973 315 SubstituteMacro(line, len, "${process_name}", TheKidName);
d4a3e179
AR
316 SubstituteMacro(line, len, "${process_number}", xitoa(KidIdentifier));
317}
318
a7ea9b13
AR
319static void
320trim_trailing_ws(char* str)
321{
322 assert(str != NULL);
323 unsigned i = strlen(str);
324 while ((i > 0) && xisspace(str[i - 1]))
325 --i;
326 str[i] = '\0';
327}
328
329static const char*
330FindStatement(const char* line, const char* statement)
331{
332 assert(line != NULL);
333 assert(statement != NULL);
334
335 const char* str = skip_ws(line);
336 unsigned len = strlen(statement);
337 if (strncmp(str, statement, len) == 0) {
338 str += len;
339 if (*str == '\0')
340 return str;
341 else if (xisspace(*str))
342 return skip_ws(str);
343 }
344
345 return NULL;
346}
347
348static bool
349StrToInt(const char* str, long& number)
350{
351 assert(str != NULL);
352
353 char* end;
354 number = strtol(str, &end, 0);
355
356 return (end != str) && (*end == '\0'); // returns true if string contains nothing except number
357}
358
359static bool
360EvalBoolExpr(const char* expr)
361{
362 assert(expr != NULL);
363 if (strcmp(expr, "true") == 0) {
364 return true;
365 } else if (strcmp(expr, "false") == 0) {
366 return false;
367 } else if (const char* equation = strchr(expr, '=')) {
368 const char* rvalue = skip_ws(equation + 1);
369 char* lvalue = (char*)xmalloc(equation - expr + 1);
370 xstrncpy(lvalue, expr, equation - expr + 1);
371 trim_trailing_ws(lvalue);
372
373 long number1;
374 if (!StrToInt(lvalue, number1))
375 fatalf("String is not a integer number: '%s'\n", lvalue);
376 long number2;
377 if (!StrToInt(rvalue, number2))
378 fatalf("String is not a integer number: '%s'\n", rvalue);
379
380 xfree(lvalue);
381 return number1 == number2;
382 }
383 fatalf("Unable to evaluate expression '%s'\n", expr);
384 return false; // this place cannot be reached
385}
386
cfcb6b30 387static int
388parseOneConfigFile(const char *file_name, unsigned int depth)
2546fcb3 389{
270b86af 390 FILE *fp = NULL;
cfcb6b30 391 const char *orig_cfg_filename = cfg_filename;
392 const int orig_config_lineno = config_lineno;
270b86af 393 char *token = NULL;
81c161d0 394 char *tmp_line = NULL;
395 int tmp_line_len = 0;
e13ee7ad 396 int err_count = 0;
1741cbad 397 int is_pipe = 0;
cfcb6b30 398
399 debugs(3, 1, "Processing Configuration File: " << file_name << " (depth " << depth << ")");
400 if (depth > 16) {
401 fatalf("WARNING: can't include %s: includes are nested too deeply (>16)!\n", file_name);
402 return 1;
403 }
62e76326 404
1741cbad 405 if (file_name[0] == '!' || file_name[0] == '|') {
62e76326 406 fp = popen(file_name + 1, "r");
407 is_pipe = 1;
1741cbad 408 } else {
62e76326 409 fp = fopen(file_name, "r");
1741cbad 410 }
62e76326 411
1741cbad 412 if (fp == NULL)
cfcb6b30 413 fatalf("Unable to open configuration file: %s: %s", file_name, xstrerror());
62e76326 414
ec4daaa5 415#ifdef _SQUID_WIN32_
62e76326 416
c4aefe96 417 setmode(fileno(fp), O_TEXT);
62e76326 418
c4aefe96 419#endif
62e76326 420
76b9daa5 421 SetConfigFilename(file_name, bool(is_pipe));
62e76326 422
270b86af 423 memset(config_input_line, '\0', BUFSIZ);
62e76326 424
270b86af 425 config_lineno = 0;
62e76326 426
a7ea9b13 427 Vector<bool> if_states;
270b86af 428 while (fgets(config_input_line, BUFSIZ, fp)) {
62e76326 429 config_lineno++;
430
431 if ((token = strchr(config_input_line, '\n')))
432 *token = '\0';
433
d866e2ad 434 if ((token = strchr(config_input_line, '\r')))
435 *token = '\0';
436
62e76326 437 if (strncmp(config_input_line, "#line ", 6) == 0) {
438 static char new_file_name[1024];
439 static char *file;
440 static char new_lineno;
441 token = config_input_line + 6;
442 new_lineno = strtol(token, &file, 0) - 1;
443
444 if (file == token)
445 continue; /* Not a valid #line directive, may be a comment */
446
e4755e29 447 while (*file && xisspace((unsigned char) *file))
62e76326 448 file++;
449
450 if (*file) {
451 if (*file != '"')
452 continue; /* Not a valid #line directive, may be a comment */
453
454 xstrncpy(new_file_name, file + 1, sizeof(new_file_name));
455
456 if ((token = strchr(new_file_name, '"')))
457 *token = '\0';
458
459 cfg_filename = new_file_name;
62e76326 460 }
461
462 config_lineno = new_lineno;
463 }
464
465 if (config_input_line[0] == '#')
466 continue;
467
468 if (config_input_line[0] == '\0')
469 continue;
470
130bff33 471 const char* append = tmp_line_len ? skip_ws(config_input_line) : config_input_line;
472
473 size_t append_len = strlen(append);
62e76326 474
130bff33 475 tmp_line = (char*)xrealloc(tmp_line, tmp_line_len + append_len + 1);
81c161d0 476
130bff33 477 strcpy(tmp_line + tmp_line_len, append);
81c161d0 478
130bff33 479 tmp_line_len += append_len;
81c161d0 480
481 if (tmp_line[tmp_line_len-1] == '\\') {
bf8fe701 482 debugs(3, 5, "parseConfigFile: tmp_line='" << tmp_line << "'");
81c161d0 483 tmp_line[--tmp_line_len] = '\0';
484 continue;
485 }
486
a7ea9b13 487 trim_trailing_ws(tmp_line);
d4a3e179 488 ProcessMacros(tmp_line, tmp_line_len);
bf8fe701 489 debugs(3, 5, "Processing: '" << tmp_line << "'");
62e76326 490
a7ea9b13
AR
491 if (const char* expr = FindStatement(tmp_line, "if")) {
492 if_states.push_back(EvalBoolExpr(expr)); // store last if-statement meaning
493 } else if (FindStatement(tmp_line, "endif")) {
494 if (!if_states.empty())
495 if_states.pop_back(); // remove last if-statement meaning
496 else
497 fatalf("'endif' without 'if'\n");
498 } else if (FindStatement(tmp_line, "else")) {
499 if (!if_states.empty())
500 if_states.back() = !if_states.back();
501 else
502 fatalf("'else' without 'if'\n");
503 } else if (if_states.empty() || if_states.back()) { // test last if-statement meaning if present
504 /* Handle includes here */
505 if (tmp_line_len >= 9 && strncmp(tmp_line, "include", 7) == 0 && xisspace(tmp_line[7])) {
506 err_count += parseManyConfigFiles(tmp_line + 8, depth + 1);
507 } else if (!parse_line(tmp_line)) {
508 debugs(3, 0, HERE << cfg_filename << ":" << config_lineno << " unrecognized: '" << tmp_line << "'");
509 err_count++;
510 }
26ac0430 511 }
62e76326 512
513 safe_free(tmp_line);
81c161d0 514 tmp_line_len = 0;
515
270b86af 516 }
a7ea9b13
AR
517 if (!if_states.empty())
518 fatalf("if-statement without 'endif'\n");
62e76326 519
1741cbad 520 if (is_pipe) {
62e76326 521 int ret = pclose(fp);
522
523 if (ret != 0)
524 fatalf("parseConfigFile: '%s' failed with exit code %d\n", file_name, ret);
1741cbad 525 } else {
62e76326 526 fclose(fp);
1741cbad 527 }
62e76326 528
cfcb6b30 529 cfg_filename = orig_cfg_filename;
530 config_lineno = orig_config_lineno;
531
532 return err_count;
533}
534
535int
dd9b1081 536parseConfigFile(const char *file_name)
cfcb6b30 537{
538 int err_count = 0;
dd9b1081 539 CacheManager *manager=CacheManager::GetInstance();
cfcb6b30 540
5817ee13
AJ
541 debugs(5, 4, HERE);
542
cfcb6b30 543 configFreeMemory();
544
5491c11e 545 ACLMethodData::ThePurgeCount = 0;
cfcb6b30 546 default_all();
547
548 err_count = parseOneConfigFile(file_name, 0);
549
f0b19334 550 defaults_if_none();
f9ad0106 551
9c46ca97 552 /*
553 * We must call configDoConfigure() before leave_suid() because
554 * configDoConfigure() is where we turn username strings into
555 * uid values.
556 */
557 configDoConfigure();
558
f9ad0106 559 if (!Config.chroot_dir) {
560 leave_suid();
c642c141 561 setUmask(Config.umask);
62493678 562 _db_init(Debug::cache_log, Debug::debugOptions);
f9ad0106 563 enter_suid();
564 }
565
478c4f2f 566 if (opt_send_signal == -1) {
dd9b1081 567 manager->registerAction("config",
26ac0430
AJ
568 "Current Squid Configuration",
569 dump_config,
570 1, 1);
478c4f2f 571 }
572
e13ee7ad 573 return err_count;
f0b19334 574}
270b86af 575
cfcb6b30 576
f0b19334 577static void
578configDoConfigure(void)
579{
f0b19334 580 memset(&Config2, '\0', sizeof(SquidConfig2));
7021844c 581 /* init memory as early as possible */
582 memConfigure();
270b86af 583 /* Sanity checks */
62e76326 584
7895fa18
AJ
585 if (Config.cacheSwap.swapDirs == NULL) {
586 /* Memory-only cache probably in effect. */
587 /* turn off the cache rebuild delays... */
588 StoreController::store_dirs_rebuilding = 0;
589 }
590
47df1aa7
AJ
591 if (Debug::rotateNumber < 0) {
592 Debug::rotateNumber = Config.Log.rotateNumber;
593 }
594
5aecb102 595#if SIZEOF_OFF_T <= 4
596 if (Config.Store.maxObjectSize > 0x7FFF0000) {
26ac0430
AJ
597 debugs(3, 0, "WARNING: This Squid binary can not handle files larger than 2GB. Limiting maximum_object_size to just below 2GB");
598 Config.Store.maxObjectSize = 0x7FFF0000;
5aecb102 599 }
600#endif
c8f4eac4 601 if (0 == Store::Root().maxSize())
62e76326 602 /* people might want a zero-sized cache on purpose */
603 (void) 0;
c8f4eac4 604 else if (Store::Root().maxSize() < (Config.memMaxSize >> 10))
605 /* This is bogus. folk with NULL caches will want this */
bf8fe701 606 debugs(3, 0, "WARNING cache_mem is larger than total disk cache space!");
62e76326 607
84f42bac 608 if (Config.Announce.period > 0) {
62e76326 609 Config.onoff.announce = 1;
84f42bac 610 } else if (Config.Announce.period < 1) {
62e76326 611 Config.Announce.period = 86400 * 365; /* one year */
612 Config.onoff.announce = 0;
270b86af 613 }
62e76326 614
d3caee79 615 if (Config.onoff.httpd_suppress_version_string)
616 visible_appname_string = (char *)appname_string;
617 else
7dbca7a4 618 visible_appname_string = (char const *)APP_FULLNAME;
d3caee79 619
6b53c392 620#if USE_DNSSERVERS
d3caee79 621
48d54e4d 622 if (Config.dnsChildren.n_max < 1)
62e76326 623 fatal("No dnsservers allocated");
624
efd900cb 625#endif
62e76326 626
270b86af 627 if (Config.Program.redirect) {
48d54e4d
AJ
628 if (Config.redirectChildren.n_max < 1) {
629 Config.redirectChildren.n_max = 0;
62e76326 630 wordlistDestroy(&Config.Program.redirect);
631 }
fea2e6e0 632 }
62e76326 633
f1dc9b30 634 if (Config.appendDomain)
62e76326 635 if (*Config.appendDomain != '.')
636 fatal("append_domain must begin with a '.'");
637
270b86af 638 if (Config.errHtmlText == NULL)
62e76326 639 Config.errHtmlText = xstrdup(null_string);
640
270b86af 641 storeConfigure();
62e76326 642
52f772de 643 snprintf(ThisCache, sizeof(ThisCache), "%s (%s)",
62e76326 644 uniqueHostname(),
d3caee79 645 visible_appname_string);
fbdba7c4 646
38a6c74e 647 /*
648 * the extra space is for loop detection in client_side.c -- we search
649 * for substrings in the Via header.
650 */
52f772de 651 snprintf(ThisCache2, sizeof(ThisCache), " %s (%s)",
62e76326 652 uniqueHostname(),
d3caee79 653 visible_appname_string);
62e76326 654
b2b40d8c
AJ
655 /* Use visible_hostname as default surrogate_id */
656 if (!Config.Accel.surrogate_id) {
657 const char *t = getMyHostname();
658 Config.Accel.surrogate_id = xstrdup( (t?t:"unset-id") );
659 }
660
270b86af 661 if (!Config.udpMaxHitObjsz || Config.udpMaxHitObjsz > SQUID_UDP_SO_SNDBUF)
62e76326 662 Config.udpMaxHitObjsz = SQUID_UDP_SO_SNDBUF;
663
270b86af 664 if (Config.appendDomain)
62e76326 665 Config.appendDomainLen = strlen(Config.appendDomain);
270b86af 666 else
62e76326 667 Config.appendDomainLen = 0;
668
22c653cd 669 if (Config.retry.maxtries > 10)
62e76326 670 fatal("maximum_single_addr_tries cannot be larger than 10");
671
22c653cd 672 if (Config.retry.maxtries < 1) {
bf8fe701 673 debugs(3, 0, "WARNING: resetting 'maximum_single_addr_tries to 1");
62e76326 674 Config.retry.maxtries = 1;
5210854d 675 }
62e76326 676
f0b19334 677 requirePathnameExists("MIME Config Table", Config.mimeTablePathname);
6b53c392 678#if USE_DNSSERVERS
62e76326 679
f0b19334 680 requirePathnameExists("cache_dns_program", Config.Program.dnsserver);
efd900cb 681#endif
a3d0a19d 682#if USE_UNLINKD
62e76326 683
f0b19334 684 requirePathnameExists("unlinkd_program", Config.Program.unlinkd);
a3d0a19d 685#endif
82b7abe3 686 requirePathnameExists("logfile_daemon", Log::TheConfig.logfile_daemon);
f0b19334 687 if (Config.Program.redirect)
62e76326 688 requirePathnameExists("redirect_program", Config.Program.redirect->key);
689
f0b19334 690 requirePathnameExists("Icon Directory", Config.icons.directory);
62e76326 691
26ac0430 692 if (Config.errorDirectory)
43000484 693 requirePathnameExists("Error Directory", Config.errorDirectory);
62e76326 694
626096be 695#if USE_HTTP_VIOLATIONS
62e76326 696
9f60cfdf 697 {
62e76326 698 const refresh_t *R;
699
26ac0430 700 for (R = Config.Refresh; R; R = R->next) {
62e76326 701 if (!R->flags.override_expire)
702 continue;
703
bf8fe701 704 debugs(22, 1, "WARNING: use of 'override-expire' in 'refresh_pattern' violates HTTP");
62e76326 705
706 break;
707 }
708
26ac0430 709 for (R = Config.Refresh; R; R = R->next) {
62e76326 710 if (!R->flags.override_lastmod)
711 continue;
712
bf8fe701 713 debugs(22, 1, "WARNING: use of 'override-lastmod' in 'refresh_pattern' violates HTTP");
62e76326 714
715 break;
716 }
717
26ac0430 718 for (R = Config.Refresh; R; R = R->next) {
38f9c547 719 if (!R->flags.reload_into_ims)
720 continue;
721
bf8fe701 722 debugs(22, 1, "WARNING: use of 'reload-into-ims' in 'refresh_pattern' violates HTTP");
38f9c547 723
724 break;
725 }
726
26ac0430 727 for (R = Config.Refresh; R; R = R->next) {
38f9c547 728 if (!R->flags.ignore_reload)
729 continue;
730
bf8fe701 731 debugs(22, 1, "WARNING: use of 'ignore-reload' in 'refresh_pattern' violates HTTP");
38f9c547 732
733 break;
734 }
735
26ac0430 736 for (R = Config.Refresh; R; R = R->next) {
38f9c547 737 if (!R->flags.ignore_no_cache)
738 continue;
739
bf8fe701 740 debugs(22, 1, "WARNING: use of 'ignore-no-cache' in 'refresh_pattern' violates HTTP");
38f9c547 741
742 break;
743 }
744
26ac0430 745 for (R = Config.Refresh; R; R = R->next) {
38f9c547 746 if (!R->flags.ignore_no_store)
747 continue;
748
bf8fe701 749 debugs(22, 1, "WARNING: use of 'ignore-no-store' in 'refresh_pattern' violates HTTP");
38f9c547 750
751 break;
752 }
753
4ca08219 754 for (R = Config.Refresh; R; R = R->next) {
04f7fd38
AJ
755 if (!R->flags.ignore_must_revalidate)
756 continue;
757 debugs(22, 1, "WARNING: use of 'ignore-must-revalidate' in 'refresh_pattern' violates HTTP");
758 break;
4ca08219
AJ
759 }
760
26ac0430 761 for (R = Config.Refresh; R; R = R->next) {
38f9c547 762 if (!R->flags.ignore_private)
763 continue;
764
bf8fe701 765 debugs(22, 1, "WARNING: use of 'ignore-private' 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_auth)
772 continue;
773
bf8fe701 774 debugs(22, 1, "WARNING: use of 'ignore-auth' in 'refresh_pattern' violates HTTP");
38f9c547 775
776 break;
777 }
778
9f60cfdf 779 }
c2f5c744 780#endif
626096be 781#if !USE_HTTP_VIOLATIONS
c2f5c744 782 Config.onoff.via = 1;
783#else
62e76326 784
c2f5c744 785 if (!Config.onoff.via)
bf8fe701 786 debugs(22, 1, "WARNING: HTTP requires the use of Via");
62e76326 787
9f60cfdf 788#endif
62e76326 789
5491c11e
AR
790 // we enable runtime PURGE checks if there is at least one PURGE method ACL
791 // TODO: replace with a dedicated "purge" ACL option?
3d9019c7 792 Config2.onoff.enable_purge = (ACLMethodData::ThePurgeCount > 0);
62e76326 793
4f56514c 794 Config2.onoff.mangle_request_headers = httpReqHdrManglersConfigured();
5967c0bf 795
8749fa47 796 if (geteuid() == 0) {
62e76326 797 if (NULL != Config.effectiveUser) {
798
799 struct passwd *pwd = getpwnam(Config.effectiveUser);
800
5ad8d199 801 if (NULL == pwd) {
62e76326 802 /*
803 * Andres Kroonmaa <andre@online.ee>:
804 * Some getpwnam() implementations (Solaris?) require
805 * an available FD < 256 for opening a FILE* to the
806 * passwd file.
807 * DW:
808 * This should be safe at startup, but might still fail
809 * during reconfigure.
810 */
811 fatalf("getpwnam failed to find userid for effective user '%s'",
812 Config.effectiveUser);
5ad8d199 813 return;
814 }
62e76326 815
816 Config2.effectiveUserID = pwd->pw_uid;
817
818 Config2.effectiveGroupID = pwd->pw_gid;
08ac5ea7 819
cff61cb8 820#if HAVE_PUTENV
821
822 if (pwd->pw_dir && *pwd->pw_dir) {
823 int len;
824 char *env_str = (char *)xcalloc((len = strlen(pwd->pw_dir) + 6), 1);
825 snprintf(env_str, len, "HOME=%s", pwd->pw_dir);
826 putenv(env_str);
827 }
828
829#endif
830
62e76326 831 }
8749fa47 832 } else {
62e76326 833 Config2.effectiveUserID = geteuid();
834 Config2.effectiveGroupID = getegid();
d20b1cd0 835 }
62e76326 836
d20b1cd0 837 if (NULL != Config.effectiveGroup) {
62e76326 838
839 struct group *grp = getgrnam(Config.effectiveGroup);
840
5ad8d199 841 if (NULL == grp) {
62e76326 842 fatalf("getgrnam failed to find groupid for effective group '%s'",
843 Config.effectiveGroup);
5ad8d199 844 return;
845 }
62e76326 846
847 Config2.effectiveGroupID = grp->gr_gid;
d20b1cd0 848 }
62e76326 849
985c86bc 850 HttpRequestMethod::Configure(Config);
a7ad6e4e 851#if USE_SSL
62e76326 852
bf8fe701 853 debugs(3, 1, "Initializing https proxy context");
f9ad0106 854
a82a4fe4 855 Config.ssl_client.sslContext = sslCreateClientContext(Config.ssl_client.cert, Config.ssl_client.key, Config.ssl_client.version, Config.ssl_client.cipher, Config.ssl_client.options, Config.ssl_client.flags, Config.ssl_client.cafile, Config.ssl_client.capath, Config.ssl_client.crlfile);
f9ad0106 856
857 {
858
859 peer *p;
860
861 for (p = Config.peers; p != NULL; p = p->next) {
862 if (p->use_ssl) {
bf8fe701 863 debugs(3, 1, "Initializing cache_peer " << p->name << " SSL context");
a82a4fe4 864 p->sslContext = sslCreateClientContext(p->sslcert, p->sslkey, p->sslversion, p->sslcipher, p->ssloptions, p->sslflags, p->sslcafile, p->sslcapath, p->sslcrlfile);
f9ad0106 865 }
866 }
867 }
868
154dc884 869 {
870
871 http_port_list *s;
872
873 for (s = Config.Sockaddr.http; s != NULL; s = (http_port_list *) s->next) {
874 if (!s->cert && !s->key)
875 continue;
876
877 debugs(3, 1, "Initializing http_port " << s->http.s << " SSL context");
878
879 s->sslContext = sslCreateServerContext(s->cert, s->key, s->version, s->cipher, s->options, s->sslflags, s->clientca, s->cafile, s->capath, s->crlfile, s->dhfile, s->sslcontext);
880 }
881 }
882
f9ad0106 883 {
884
885 https_port_list *s;
886
b844522c 887 for (s = Config.Sockaddr.https; s != NULL; s = (https_port_list *) s->http.next) {
cc192b50 888 debugs(3, 1, "Initializing https_port " << s->http.s << " SSL context");
bf8fe701 889
a82a4fe4 890 s->sslContext = sslCreateServerContext(s->cert, s->key, s->version, s->cipher, s->options, s->sslflags, s->clientca, s->cafile, s->capath, s->crlfile, s->dhfile, s->sslcontext);
f9ad0106 891 }
892 }
893
a7ad6e4e 894#endif
090089c4 895}
896
76f44481
AJ
897/** Parse a line containing an obsolete directive.
898 * To upgrade it where possible instead of just "Bungled config" for
899 * directives which cannot be marked as simply aliases of the some name.
900 * For example if the parameter order and content has changed.
901 * Or if the directive has been completely removed.
902 */
903void
904parse_obsolete(const char *name)
905{
906 // Directives which have been radically changed rather than removed
907 if (!strcmp(name, "url_rewrite_concurrency")) {
254d8ef1
AJ
908 int cval;
909 parse_int(&cval);
76f44481 910 debugs(3, DBG_CRITICAL, "WARNING: url_rewrite_concurrency upgrade overriding url_rewrite_children settings.");
254d8ef1 911 Config.redirectChildren.concurrency = cval;
76f44481
AJ
912 }
913}
914
270b86af 915/* Parse a time specification from the config file. Store the
f1dc9b30 916 * result in 'tptr', after converting it to 'units' */
8203a132 917static void
a47b9029 918parseTimeLine(time_t * tptr, const char *units)
090089c4 919{
920 char *token;
270b86af 921 double d;
f1dc9b30 922 time_t m;
923 time_t u;
62e76326 924
270b86af 925 if ((u = parseTimeUnits(units)) == 0)
62e76326 926 self_destruct();
927
270b86af 928 if ((token = strtok(NULL, w_space)) == NULL)
62e76326 929 self_destruct();
930
5c20d6fa 931 d = xatof(token);
62e76326 932
270b86af 933 m = u; /* default to 'units' if none specified */
62e76326 934
10738561 935 if (0 == d)
62e76326 936 (void) 0;
10738561 937 else if ((token = strtok(NULL, w_space)) == NULL)
26ac0430
AJ
938 debugs(3, 0, "WARNING: No units on '" <<
939 config_input_line << "', assuming " <<
940 d << " " << units );
9e975e4e 941 else if ((m = parseTimeUnits(token)) == 0)
62e76326 942 self_destruct();
943
e6ccf245 944 *tptr = static_cast<time_t> (m * d / u);
090089c4 945}
946
270b86af 947static int
948parseTimeUnits(const char *unit)
949{
950 if (!strncasecmp(unit, T_SECOND_STR, strlen(T_SECOND_STR)))
62e76326 951 return 1;
952
270b86af 953 if (!strncasecmp(unit, T_MINUTE_STR, strlen(T_MINUTE_STR)))
62e76326 954 return 60;
955
270b86af 956 if (!strncasecmp(unit, T_HOUR_STR, strlen(T_HOUR_STR)))
62e76326 957 return 3600;
958
270b86af 959 if (!strncasecmp(unit, T_DAY_STR, strlen(T_DAY_STR)))
62e76326 960 return 86400;
961
270b86af 962 if (!strncasecmp(unit, T_WEEK_STR, strlen(T_WEEK_STR)))
62e76326 963 return 86400 * 7;
964
270b86af 965 if (!strncasecmp(unit, T_FORTNIGHT_STR, strlen(T_FORTNIGHT_STR)))
62e76326 966 return 86400 * 14;
967
270b86af 968 if (!strncasecmp(unit, T_MONTH_STR, strlen(T_MONTH_STR)))
62e76326 969 return 86400 * 30;
970
270b86af 971 if (!strncasecmp(unit, T_YEAR_STR, strlen(T_YEAR_STR)))
62e76326 972 return static_cast<int>(86400 * 365.2522);
973
270b86af 974 if (!strncasecmp(unit, T_DECADE_STR, strlen(T_DECADE_STR)))
62e76326 975 return static_cast<int>(86400 * 365.2522 * 10);
976
bf8fe701 977 debugs(3, 1, "parseTimeUnits: unknown time unit '" << unit << "'");
62e76326 978
270b86af 979 return 0;
980}
981
47f6e231 982static void
983parseBytesLine64(int64_t * bptr, const char *units)
984{
985 char *token;
986 double d;
987 int64_t m;
988 int64_t u;
989
9eeb8e4b 990 if ((u = parseBytesUnits(units)) == 0) {
47f6e231 991 self_destruct();
9eeb8e4b 992 return;
993 }
47f6e231 994
9eeb8e4b 995 if ((token = strtok(NULL, w_space)) == NULL) {
47f6e231 996 self_destruct();
9eeb8e4b 997 return;
998 }
47f6e231 999
1000 if (strcmp(token, "none") == 0 || strcmp(token, "-1") == 0) {
bc41416d 1001 *bptr = -1;
47f6e231 1002 return;
1003 }
1004
1005 d = xatof(token);
1006
1007 m = u; /* default to 'units' if none specified */
1008
1009 if (0.0 == d)
1010 (void) 0;
1011 else if ((token = strtok(NULL, w_space)) == NULL)
26ac0430
AJ
1012 debugs(3, 0, "WARNING: No units on '" <<
1013 config_input_line << "', assuming " <<
1014 d << " " << units );
9eeb8e4b 1015 else if ((m = parseBytesUnits(token)) == 0) {
47f6e231 1016 self_destruct();
9eeb8e4b 1017 return;
1018 }
47f6e231 1019
1020 *bptr = static_cast<int64_t>(m * d / u);
1021
1022 if (static_cast<double>(*bptr) * 2 != m * d / u * 2)
1023 self_destruct();
1024}
1025
1026
9906e724 1027static void
9e975e4e 1028parseBytesLine(size_t * bptr, const char *units)
9906e724 1029{
1030 char *token;
1031 double d;
47f6e231 1032 int m;
1033 int u;
62e76326 1034
9eeb8e4b 1035 if ((u = parseBytesUnits(units)) == 0) {
62e76326 1036 self_destruct();
9eeb8e4b 1037 return;
1038 }
62e76326 1039
9eeb8e4b 1040 if ((token = strtok(NULL, w_space)) == NULL) {
62e76326 1041 self_destruct();
9eeb8e4b 1042 return;
1043 }
62e76326 1044
f8ecd7d7 1045 if (strcmp(token, "none") == 0 || strcmp(token, "-1") == 0) {
b2d729e6 1046 *bptr = static_cast<size_t>(-1);
ef1955a5 1047 return;
1048 }
1049
5c20d6fa 1050 d = xatof(token);
62e76326 1051
9906e724 1052 m = u; /* default to 'units' if none specified */
62e76326 1053
343f47a3 1054 if (0.0 == d)
62e76326 1055 (void) 0;
4860dc1b 1056 else if ((token = strtok(NULL, w_space)) == NULL)
26ac0430
AJ
1057 debugs(3, 0, "WARNING: No units on '" <<
1058 config_input_line << "', assuming " <<
1059 d << " " << units );
9eeb8e4b 1060 else if ((m = parseBytesUnits(token)) == 0) {
62e76326 1061 self_destruct();
9eeb8e4b 1062 return;
1063 }
62e76326 1064
e6ccf245 1065 *bptr = static_cast<size_t>(m * d / u);
347ae7c4 1066
a69d485c 1067 if (static_cast<double>(*bptr) * 2 != m * d / u * 2)
347ae7c4 1068 self_destruct();
9906e724 1069}
1070
e210930b
AJ
1071#if !USE_DNSSERVERS
1072static void
1073parseBytesLineSigned(ssize_t * bptr, const char *units)
1074{
1075 char *token;
1076 double d;
1077 int m;
1078 int u;
1079
1080 if ((u = parseBytesUnits(units)) == 0) {
1081 self_destruct();
1082 return;
1083 }
1084
1085 if ((token = strtok(NULL, w_space)) == NULL) {
1086 self_destruct();
1087 return;
1088 }
1089
1090 if (strcmp(token, "none") == 0 || token[0] == '-' /* -N */) {
1091 *bptr = -1;
1092 return;
1093 }
1094
1095 d = xatof(token);
1096
1097 m = u; /* default to 'units' if none specified */
1098
1099 if (0.0 == d)
1100 (void) 0;
1101 else if ((token = strtok(NULL, w_space)) == NULL)
1102 debugs(3, 0, "WARNING: No units on '" <<
1103 config_input_line << "', assuming " <<
1104 d << " " << units );
1105 else if ((m = parseBytesUnits(token)) == 0) {
1106 self_destruct();
1107 return;
1108 }
1109
1110 *bptr = static_cast<size_t>(m * d / u);
1111
1112 if (static_cast<double>(*bptr) * 2 != m * d / u * 2)
1113 self_destruct();
1114}
1115#endif
1116
9906e724 1117static size_t
1118parseBytesUnits(const char *unit)
1119{
1120 if (!strncasecmp(unit, B_BYTES_STR, strlen(B_BYTES_STR)))
62e76326 1121 return 1;
1122
9906e724 1123 if (!strncasecmp(unit, B_KBYTES_STR, strlen(B_KBYTES_STR)))
62e76326 1124 return 1 << 10;
1125
9906e724 1126 if (!strncasecmp(unit, B_MBYTES_STR, strlen(B_MBYTES_STR)))
62e76326 1127 return 1 << 20;
1128
9906e724 1129 if (!strncasecmp(unit, B_GBYTES_STR, strlen(B_GBYTES_STR)))
62e76326 1130 return 1 << 30;
1131
fa37412f 1132 debugs(3, DBG_CRITICAL, "WARNING: Unknown bytes unit '" << unit << "'");
62e76326 1133
9906e724 1134 return 0;
1135}
1136
270b86af 1137/*****************************************************************************
1138 * Max
1139 *****************************************************************************/
1140
8203a132 1141static void
97427e90 1142dump_acl(StoreEntry * entry, const char *name, ACL * ae)
090089c4 1143{
56b63fa1 1144 wordlist *w;
1145 wordlist *v;
62e76326 1146
9ef28b60 1147 while (ae != NULL) {
bf8fe701 1148 debugs(3, 3, "dump_acl: " << name << " " << ae->name);
62e76326 1149 storeAppendPrintf(entry, "%s %s %s ",
1150 name,
1151 ae->name,
1152 ae->typeString());
6bf4f823 1153 v = w = ae->dump();
62e76326 1154
1155 while (v != NULL) {
bf8fe701 1156 debugs(3, 3, "dump_acl: " << name << " " << ae->name << " " << v->key);
62e76326 1157 storeAppendPrintf(entry, "%s ", v->key);
1158 v = v->next;
1159 }
1160
1161 storeAppendPrintf(entry, "\n");
1162 wordlistDestroy(&w);
1163 ae = ae->next;
56b63fa1 1164 }
090089c4 1165}
1166
8203a132 1167static void
97427e90 1168parse_acl(ACL ** ae)
090089c4 1169{
a9f20260 1170 ACL::ParseAclLine(LegacyParser, ae);
f1dc9b30 1171}
1172
1173static void
97427e90 1174free_acl(ACL ** ae)
f1dc9b30 1175{
9ef28b60 1176 aclDestroyAcls(ae);
090089c4 1177}
1178
425de4c8 1179void
76cd39d7 1180dump_acl_list(StoreEntry * entry, ACLList * head)
30a4f2a8 1181{
76cd39d7 1182 ACLList *l;
62e76326 1183
d6827718 1184 for (l = head; l; l = l->next) {
62e76326 1185 storeAppendPrintf(entry, " %s%s",
1186 l->op ? null_string : "!",
1187 l->_acl->name);
d6827718 1188 }
1189}
1190
b67e2c8c 1191void
d6827718 1192dump_acl_access(StoreEntry * entry, const char *name, acl_access * head)
1193{
1194 acl_access *l;
62e76326 1195
d6827718 1196 for (l = head; l; l = l->next) {
62e76326 1197 storeAppendPrintf(entry, "%s %s",
1198 name,
1199 l->allow ? "Allow" : "Deny");
1200 dump_acl_list(entry, l->aclList);
1201 storeAppendPrintf(entry, "\n");
56b63fa1 1202 }
30a4f2a8 1203}
090089c4 1204
8203a132 1205static void
16300b58 1206parse_acl_access(acl_access ** head)
090089c4 1207{
a9f20260 1208 aclParseAccessLine(LegacyParser, head);
090089c4 1209}
1210
0153d498 1211static void
16300b58 1212free_acl_access(acl_access ** head)
0153d498 1213{
a47b9029 1214 aclDestroyAccessList(head);
0153d498 1215}
1216
8203a132 1217static void
b7ac5457 1218dump_address(StoreEntry * entry, const char *name, Ip::Address &addr)
270b86af 1219{
cc192b50 1220 char buf[MAX_IPSTRLEN];
1221 storeAppendPrintf(entry, "%s %s\n", name, addr.NtoA(buf,MAX_IPSTRLEN) );
270b86af 1222}
1223
1224static void
b7ac5457 1225parse_address(Ip::Address *addr)
090089c4 1226{
270b86af 1227 char *token = strtok(NULL, w_space);
1228
9eeb8e4b 1229 if (!token) {
62e76326 1230 self_destruct();
9eeb8e4b 1231 return;
1232 }
62e76326 1233
26ac0430 1234 if (!strcmp(token,"any_addr")) {
cc192b50 1235 addr->SetAnyAddr();
26ac0430
AJ
1236 (void) 0;
1237 } else if ( (!strcmp(token,"no_addr")) || (!strcmp(token,"full_mask")) ) {
cc192b50 1238 addr->SetNoAddr();
26ac0430
AJ
1239 (void) 0;
1240 } else
cc192b50 1241 *addr = token;
090089c4 1242}
1243
0153d498 1244static void
b7ac5457 1245free_address(Ip::Address *addr)
0153d498 1246{
cc192b50 1247 addr->SetEmpty();
0153d498 1248}
1249
d6827718 1250CBDATA_TYPE(acl_address);
1251
1252static void
1253dump_acl_address(StoreEntry * entry, const char *name, acl_address * head)
1254{
cc192b50 1255 char buf[MAX_IPSTRLEN];
d6827718 1256 acl_address *l;
62e76326 1257
d6827718 1258 for (l = head; l; l = l->next) {
cc192b50 1259 if (!l->addr.IsAnyAddr())
1260 storeAppendPrintf(entry, "%s %s", name, l->addr.NtoA(buf,MAX_IPSTRLEN));
62e76326 1261 else
1262 storeAppendPrintf(entry, "%s autoselect", name);
1263
1264 dump_acl_list(entry, l->aclList);
1265
1266 storeAppendPrintf(entry, "\n");
d6827718 1267 }
1268}
1269
1270static void
1271freed_acl_address(void *data)
1272{
e6ccf245 1273 acl_address *l = static_cast<acl_address *>(data);
29b8d8d6 1274 aclDestroyAclList(&l->aclList);
d6827718 1275}
1276
1277static void
1278parse_acl_address(acl_address ** head)
1279{
1280 acl_address *l;
1281 acl_address **tail = head; /* sane name below */
1282 CBDATA_INIT_TYPE_FREECB(acl_address, freed_acl_address);
1283 l = cbdataAlloc(acl_address);
1284 parse_address(&l->addr);
a9f20260 1285 aclParseAclList(LegacyParser, &l->aclList);
62e76326 1286
d6827718 1287 while (*tail)
62e76326 1288 tail = &(*tail)->next;
1289
d6827718 1290 *tail = l;
1291}
1292
1293static void
1294free_acl_address(acl_address ** head)
1295{
1296 while (*head) {
62e76326 1297 acl_address *l = *head;
1298 *head = l->next;
1299 cbdataFree(l);
d6827718 1300 }
1301}
1302
1303CBDATA_TYPE(acl_tos);
1304
1305static void
1306dump_acl_tos(StoreEntry * entry, const char *name, acl_tos * head)
1307{
1308 acl_tos *l;
62e76326 1309
d6827718 1310 for (l = head; l; l = l->next) {
62e76326 1311 if (l->tos > 0)
1312 storeAppendPrintf(entry, "%s 0x%02X", name, l->tos);
1313 else
1314 storeAppendPrintf(entry, "%s none", name);
1315
1316 dump_acl_list(entry, l->aclList);
1317
1318 storeAppendPrintf(entry, "\n");
d6827718 1319 }
1320}
1321
1322static void
1323freed_acl_tos(void *data)
1324{
e6ccf245 1325 acl_tos *l = static_cast<acl_tos *>(data);
29b8d8d6 1326 aclDestroyAclList(&l->aclList);
d6827718 1327}
1328
1329static void
1330parse_acl_tos(acl_tos ** head)
1331{
1332 acl_tos *l;
1333 acl_tos **tail = head; /* sane name below */
425de4c8 1334 unsigned int tos; /* Initially uint for strtoui. Casted to tos_t before return */
d6827718 1335 char *token = strtok(NULL, w_space);
62e76326 1336
9eeb8e4b 1337 if (!token) {
62e76326 1338 self_destruct();
9eeb8e4b 1339 return;
1340 }
62e76326 1341
425de4c8 1342 if (!xstrtoui(token, NULL, &tos, 0, std::numeric_limits<tos_t>::max())) {
62e76326 1343 self_destruct();
9eeb8e4b 1344 return;
1345 }
62e76326 1346
d6827718 1347 CBDATA_INIT_TYPE_FREECB(acl_tos, freed_acl_tos);
62e76326 1348
d6827718 1349 l = cbdataAlloc(acl_tos);
62e76326 1350
425de4c8 1351 l->tos = (tos_t)tos;
62e76326 1352
a9f20260 1353 aclParseAclList(LegacyParser, &l->aclList);
62e76326 1354
d6827718 1355 while (*tail)
62e76326 1356 tail = &(*tail)->next;
1357
d6827718 1358 *tail = l;
1359}
1360
1361static void
1362free_acl_tos(acl_tos ** head)
1363{
1364 while (*head) {
62e76326 1365 acl_tos *l = *head;
1366 *head = l->next;
1367 l->next = NULL;
1368 cbdataFree(l);
d6827718 1369 }
1370}
1371
f4f6c2e0
AJ
1372#if defined(SO_MARK)
1373
425de4c8
AJ
1374CBDATA_TYPE(acl_nfmark);
1375
1376static void
1377dump_acl_nfmark(StoreEntry * entry, const char *name, acl_nfmark * head)
1378{
1379 acl_nfmark *l;
1380
1381 for (l = head; l; l = l->next) {
1382 if (l->nfmark > 0)
1383 storeAppendPrintf(entry, "%s 0x%02X", name, l->nfmark);
1384 else
1385 storeAppendPrintf(entry, "%s none", name);
1386
1387 dump_acl_list(entry, l->aclList);
1388
1389 storeAppendPrintf(entry, "\n");
1390 }
1391}
1392
1393static void
1394freed_acl_nfmark(void *data)
1395{
1396 acl_nfmark *l = static_cast<acl_nfmark *>(data);
1397 aclDestroyAclList(&l->aclList);
1398}
1399
1400static void
1401parse_acl_nfmark(acl_nfmark ** head)
1402{
1403 acl_nfmark *l;
1404 acl_nfmark **tail = head; /* sane name below */
1405 nfmark_t mark;
1406 char *token = strtok(NULL, w_space);
1407
1408 if (!token) {
1409 self_destruct();
1410 return;
1411 }
1412
1413 if (!xstrtoui(token, NULL, &mark, 0, std::numeric_limits<nfmark_t>::max())) {
1414 self_destruct();
1415 return;
1416 }
1417
1418 CBDATA_INIT_TYPE_FREECB(acl_nfmark, freed_acl_nfmark);
1419
1420 l = cbdataAlloc(acl_nfmark);
1421
1422 l->nfmark = mark;
1423
1424 aclParseAclList(LegacyParser, &l->aclList);
1425
1426 while (*tail)
1427 tail = &(*tail)->next;
1428
1429 *tail = l;
1430}
1431
1432static void
1433free_acl_nfmark(acl_nfmark ** head)
1434{
1435 while (*head) {
1436 acl_nfmark *l = *head;
1437 *head = l->next;
1438 l->next = NULL;
1439 cbdataFree(l);
1440 }
1441}
f4f6c2e0 1442#endif /* SO_MARK */
425de4c8 1443
ef1955a5 1444CBDATA_TYPE(acl_size_t);
1445
1446static void
1447dump_acl_b_size_t(StoreEntry * entry, const char *name, acl_size_t * head)
1448{
1449 acl_size_t *l;
1450
1451 for (l = head; l; l = l->next) {
ed013b6c 1452 if (l->size != -1)
ef1955a5 1453 storeAppendPrintf(entry, "%s %d %s\n", name, (int) l->size, B_BYTES_STR);
1454 else
1455 storeAppendPrintf(entry, "%s none", name);
1456
1457 dump_acl_list(entry, l->aclList);
1458
1459 storeAppendPrintf(entry, "\n");
1460 }
1461}
1462
1463static void
1464freed_acl_b_size_t(void *data)
1465{
1466 acl_size_t *l = static_cast<acl_size_t *>(data);
1467 aclDestroyAclList(&l->aclList);
1468}
1469
1470static void
1471parse_acl_b_size_t(acl_size_t ** head)
1472{
1473 acl_size_t *l;
1474 acl_size_t **tail = head; /* sane name below */
1475
1476 CBDATA_INIT_TYPE_FREECB(acl_size_t, freed_acl_b_size_t);
1477
1478 l = cbdataAlloc(acl_size_t);
1479
47f6e231 1480 parse_b_int64_t(&l->size);
ef1955a5 1481
a9f20260 1482 aclParseAclList(LegacyParser, &l->aclList);
ef1955a5 1483
1484 while (*tail)
1485 tail = &(*tail)->next;
1486
1487 *tail = l;
1488}
1489
1490static void
1491free_acl_b_size_t(acl_size_t ** head)
1492{
1493 while (*head) {
1494 acl_size_t *l = *head;
1495 *head = l->next;
1496 l->next = NULL;
1497 cbdataFree(l);
1498 }
1499}
1500
59715b38 1501#if DELAY_POOLS
1502
b67e2c8c 1503#include "DelayPools.h"
1504#include "DelayConfig.h"
59715b38 1505/* do nothing - free_delay_pool_count is the magic free function.
1506 * this is why delay_pool_count isn't just marked TYPE: ushort
1507 */
1508#define free_delay_pool_class(X)
1509#define free_delay_pool_access(X)
1510#define free_delay_pool_rates(X)
1511#define dump_delay_pool_class(X, Y, Z)
1512#define dump_delay_pool_access(X, Y, Z)
1513#define dump_delay_pool_rates(X, Y, Z)
1514
1515static void
b67e2c8c 1516free_delay_pool_count(DelayConfig * cfg)
59715b38 1517{
b67e2c8c 1518 cfg->freePoolCount();
59715b38 1519}
1520
1521static void
b67e2c8c 1522dump_delay_pool_count(StoreEntry * entry, const char *name, DelayConfig &cfg)
59715b38 1523{
b67e2c8c 1524 cfg.dumpPoolCount (entry, name);
59715b38 1525}
1526
1527static void
b67e2c8c 1528parse_delay_pool_count(DelayConfig * cfg)
59715b38 1529{
b67e2c8c 1530 cfg->parsePoolCount();
59715b38 1531}
1532
1533static void
b67e2c8c 1534parse_delay_pool_class(DelayConfig * cfg)
59715b38 1535{
b67e2c8c 1536 cfg->parsePoolClass();
59715b38 1537}
1538
1539static void
b67e2c8c 1540parse_delay_pool_rates(DelayConfig * cfg)
59715b38 1541{
b67e2c8c 1542 cfg->parsePoolRates();
59715b38 1543}
1544
1545static void
b67e2c8c 1546parse_delay_pool_access(DelayConfig * cfg)
59715b38 1547{
a9f20260 1548 cfg->parsePoolAccess(LegacyParser);
59715b38 1549}
62e76326 1550
59715b38 1551#endif
1552
626096be 1553#if USE_HTTP_VIOLATIONS
97474590 1554static void
6bccf575 1555dump_http_header_access(StoreEntry * entry, const char *name, header_mangler header[])
e3dd531e 1556{
efd900cb 1557 int i;
62e76326 1558
6bccf575 1559 for (i = 0; i < HDR_ENUM_END; i++) {
62e76326 1560 if (header[i].access_list != NULL) {
1561 storeAppendPrintf(entry, "%s ", name);
1562 dump_acl_access(entry, httpHeaderNameById(i),
1563 header[i].access_list);
1564 }
efd900cb 1565 }
97474590 1566}
e3dd531e 1567
97474590 1568static void
6bccf575 1569parse_http_header_access(header_mangler header[])
97474590 1570{
6bccf575 1571 int id, i;
97474590 1572 char *t = NULL;
62e76326 1573
97474590 1574 if ((t = strtok(NULL, w_space)) == NULL) {
bf8fe701 1575 debugs(3, 0, "" << cfg_filename << " line " << config_lineno << ": " << config_input_line);
1576 debugs(3, 0, "parse_http_header_access: missing header name.");
62e76326 1577 return;
97474590 1578 }
62e76326 1579
6bccf575 1580 /* Now lookup index of header. */
1581 id = httpHeaderIdByNameDef(t, strlen(t));
62e76326 1582
6bccf575 1583 if (strcmp(t, "All") == 0)
62e76326 1584 id = HDR_ENUM_END;
6bccf575 1585 else if (strcmp(t, "Other") == 0)
62e76326 1586 id = HDR_OTHER;
6bccf575 1587 else if (id == -1) {
bf8fe701 1588 debugs(3, 0, "" << cfg_filename << " line " << config_lineno << ": " << config_input_line);
1589 debugs(3, 0, "parse_http_header_access: unknown header name '" << t << "'");
62e76326 1590 return;
97474590 1591 }
62e76326 1592
6bccf575 1593 if (id != HDR_ENUM_END) {
62e76326 1594 parse_acl_access(&header[id].access_list);
6bccf575 1595 } else {
62e76326 1596 char *next_string = t + strlen(t) - 1;
1597 *next_string = 'A';
1598 *(next_string + 1) = ' ';
1599
1600 for (i = 0; i < HDR_ENUM_END; i++) {
1601 char *new_string = xstrdup(next_string);
1602 strtok(new_string, w_space);
1603 parse_acl_access(&header[i].access_list);
1604 safe_free(new_string);
1605 }
97474590 1606 }
6bccf575 1607}
1608
1609static void
1610free_http_header_access(header_mangler header[])
1611{
1612 int i;
62e76326 1613
6bccf575 1614 for (i = 0; i < HDR_ENUM_END; i++) {
62e76326 1615 free_acl_access(&header[i].access_list);
6bccf575 1616 }
1617}
1618
1619static void
1620dump_http_header_replace(StoreEntry * entry, const char *name, header_mangler
62e76326 1621 header[])
6bccf575 1622{
1623 int i;
62e76326 1624
6bccf575 1625 for (i = 0; i < HDR_ENUM_END; i++) {
62e76326 1626 if (NULL == header[i].replacement)
1627 continue;
1628
1629 storeAppendPrintf(entry, "%s %s %s\n", name, httpHeaderNameById(i),
1630 header[i].replacement);
97474590 1631 }
1632}
e3dd531e 1633
97474590 1634static void
6bccf575 1635parse_http_header_replace(header_mangler header[])
e3dd531e 1636{
6bccf575 1637 int id, i;
1638 char *t = NULL;
62e76326 1639
6bccf575 1640 if ((t = strtok(NULL, w_space)) == NULL) {
bf8fe701 1641 debugs(3, 0, "" << cfg_filename << " line " << config_lineno << ": " << config_input_line);
1642 debugs(3, 0, "parse_http_header_replace: missing header name.");
62e76326 1643 return;
6bccf575 1644 }
62e76326 1645
6bccf575 1646 /* Now lookup index of header. */
1647 id = httpHeaderIdByNameDef(t, strlen(t));
62e76326 1648
6bccf575 1649 if (strcmp(t, "All") == 0)
62e76326 1650 id = HDR_ENUM_END;
6bccf575 1651 else if (strcmp(t, "Other") == 0)
62e76326 1652 id = HDR_OTHER;
6bccf575 1653 else if (id == -1) {
bf8fe701 1654 debugs(3, 0, "" << cfg_filename << " line " << config_lineno << ": " << config_input_line);
1655 debugs(3, 0, "parse_http_header_replace: unknown header name " << t << ".");
1656
62e76326 1657 return;
6bccf575 1658 }
62e76326 1659
6bccf575 1660 if (id != HDR_ENUM_END) {
62e76326 1661 if (header[id].replacement != NULL)
1662 safe_free(header[id].replacement);
1663
1664 header[id].replacement = xstrdup(t + strlen(t) + 1);
6bccf575 1665 } else {
62e76326 1666 for (i = 0; i < HDR_ENUM_END; i++) {
1667 if (header[i].replacement != NULL)
1668 safe_free(header[i].replacement);
1669
1670 header[i].replacement = xstrdup(t + strlen(t) + 1);
1671 }
6bccf575 1672 }
1673}
1674
1675static void
1676free_http_header_replace(header_mangler header[])
1677{
1678 int i;
62e76326 1679
6bccf575 1680 for (i = 0; i < HDR_ENUM_END; i++) {
62e76326 1681 if (header[i].replacement != NULL)
1682 safe_free(header[i].replacement);
6bccf575 1683 }
97474590 1684}
62e76326 1685
9e8b2f1c 1686#endif
97474590 1687
e90100aa 1688static void
e1f7507e 1689dump_cachedir(StoreEntry * entry, const char *name, SquidConfig::_cacheSwap swap)
e90100aa 1690{
f53b06f9 1691 SwapDir *s;
1692 int i;
d3b3ab85 1693 assert (entry);
62e76326 1694
a7d59104 1695 for (i = 0; i < swap.n_configured; i++) {
c8f4eac4 1696 s = dynamic_cast<SwapDir *>(swap.swapDirs[i].getRaw());
26ac0430 1697 if (!s) continue;
59b2d47f 1698 storeAppendPrintf(entry, "%s %s %s", name, s->type(), s->path);
62e76326 1699 s->dump(*entry);
62e76326 1700 storeAppendPrintf(entry, "\n");
f53b06f9 1701 }
1702}
1703
53ad48e6 1704static int
1705check_null_string(char *s)
1706{
1707 return s == NULL;
1708}
1709
94439e4e 1710static void
5817ee13 1711parse_authparam(Auth::authConfig * config)
94439e4e 1712{
1713 char *type_str;
1714 char *param_str;
94439e4e 1715
1716 if ((type_str = strtok(NULL, w_space)) == NULL)
62e76326 1717 self_destruct();
94439e4e 1718
1719 if ((param_str = strtok(NULL, w_space)) == NULL)
62e76326 1720 self_destruct();
94439e4e 1721
5817ee13
AJ
1722 /* find a configuration for the scheme in the currently parsed configs... */
1723 AuthConfig *schemeCfg = AuthConfig::Find(type_str);
62e76326 1724
5817ee13
AJ
1725 if (schemeCfg == NULL) {
1726 /* Create a configuration based on the scheme info */
1727 AuthScheme::Pointer theScheme = AuthScheme::Find(type_str);
f5691f9c 1728
5817ee13
AJ
1729 if (theScheme == NULL) {
1730 debugs(3, DBG_CRITICAL, "Parsing Config File: Unknown authentication scheme '" << type_str << "'.");
1731 self_destruct();
62e76326 1732 }
94439e4e 1733
f5691f9c 1734 config->push_back(theScheme->createConfig());
5817ee13
AJ
1735 schemeCfg = AuthConfig::Find(type_str);
1736 if (schemeCfg == NULL) {
1737 debugs(3, DBG_CRITICAL, "Parsing Config File: Corruption configuring authentication scheme '" << type_str << "'.");
1738 self_destruct();
1739 }
94439e4e 1740 }
62e76326 1741
5817ee13 1742 schemeCfg->parse(schemeCfg, config->size(), param_str);
94439e4e 1743}
1744
1745static void
5817ee13 1746free_authparam(Auth::authConfig * cfg)
94439e4e 1747{
5817ee13
AJ
1748 /* Wipe the Auth globals and Detach/Destruct component config + state. */
1749 cfg->clean();
62e76326 1750
5817ee13 1751 /* remove our pointers to the probably-dead sub-configs */
f5691f9c 1752 while (cfg->size()) {
5817ee13
AJ
1753 cfg->pop_back();
1754 }
1755
1756 /* on reconfigure initialize new auth schemes for the new config. */
ec5858ff 1757 if (reconfiguring) {
5817ee13 1758 InitAuthSchemes();
94439e4e 1759 }
94439e4e 1760}
1761
1762static void
1763dump_authparam(StoreEntry * entry, const char *name, authConfig cfg)
1764{
f5691f9c 1765 for (authConfig::iterator i = cfg.begin(); i != cfg.end(); ++i)
1766 (*i)->dump(entry, name, (*i));
94439e4e 1767}
1768
59b2d47f 1769/* TODO: just return the object, the # is irrelevant */
cd748f27 1770static int
1771find_fstype(char *type)
1772{
59b2d47f 1773 for (size_t i = 0; i < StoreFileSystem::FileSystems().size(); ++i)
1774 if (strcasecmp(type, StoreFileSystem::FileSystems().items[i]->type()) == 0)
1775 return (int)i;
62e76326 1776
cd748f27 1777 return (-1);
1778}
1779
0e4e0e7d 1780static void
e1f7507e 1781parse_cachedir(SquidConfig::_cacheSwap * swap)
0e4e0e7d 1782{
acf69d74
AJ
1783 // The workers option must preceed cache_dir for the IamWorkerProcess check
1784 // below to work. TODO: Redo IamWorkerProcess to work w/o Config and remove
1785 if (KidIdentifier > 1 && Config.workers == 1) {
1786 debugs(3, DBG_CRITICAL,
1787 "FATAL: cache_dir found before the workers option. Reorder.");
1788 self_destruct();
1789 }
1790
1791 // Among all processes, only workers may need and can handle cache_dir.
1792 if (!IamWorkerProcess())
1793 return;
1794
0e4e0e7d 1795 char *type_str;
cd748f27 1796 char *path_str;
c8f4eac4 1797 RefCount<SwapDir> sd;
cd748f27 1798 int i;
1799 int fs;
cd748f27 1800
0e4e0e7d 1801 if ((type_str = strtok(NULL, w_space)) == NULL)
62e76326 1802 self_destruct();
cd748f27 1803
cd748f27 1804 if ((path_str = strtok(NULL, w_space)) == NULL)
62e76326 1805 self_destruct();
cd748f27 1806
c9e2e0e8 1807 fs = find_fstype(type_str);
1808
1809 if (fs < 0)
1810 self_destruct();
1811
1812 /* reconfigure existing dir */
cd748f27 1813
1814 for (i = 0; i < swap->n_configured; i++) {
c8f4eac4 1815 assert (swap->swapDirs[i].getRaw());
62e76326 1816
2c9e9cba 1817 if ((strcasecmp(path_str, dynamic_cast<SwapDir *>(swap->swapDirs[i].getRaw())->path)) == 0) {
c9e2e0e8 1818 /* this is specific to on-fs Stores. The right
26ac0430 1819 * way to handle this is probably to have a mapping
c9e2e0e8 1820 * from paths to stores, and have on-fs stores
1821 * register with that, and lookip in that in their
1822 * own setup logic. RBC 20041225. TODO.
1823 */
c8f4eac4 1824
1825 sd = dynamic_cast<SwapDir *>(swap->swapDirs[i].getRaw());
1826
c9e2e0e8 1827 if (sd->type() != StoreFileSystem::FileSystems().items[fs]->type()) {
bf8fe701 1828 debugs(3, 0, "ERROR: Can't change type of existing cache_dir " <<
1829 sd->type() << " " << sd->path << " to " << type_str << ". Restart required");
c9e2e0e8 1830 return;
1831 }
1832
62e76326 1833 sd->reconfigure (i, path_str);
c8f4eac4 1834
62e76326 1835 update_maxobjsize();
c8f4eac4 1836
62e76326 1837 return;
1838 }
cd748f27 1839 }
1840
c9e2e0e8 1841 /* new cache_dir */
af6a12ee 1842 if (swap->n_configured > 63) {
2c9e9cba
AJ
1843 /* 7 bits, signed */
1844 debugs(3, DBG_CRITICAL, "WARNING: There is a fixed maximum of 63 cache_dir entries Squid can handle.");
1845 debugs(3, DBG_CRITICAL, "WARNING: '" << path_str << "' is one to many.");
1846 self_destruct();
1847 return;
1848 }
dc986280 1849
cd748f27 1850 allocate_new_swapdir(swap);
c9e2e0e8 1851
59b2d47f 1852 swap->swapDirs[swap->n_configured] = StoreFileSystem::FileSystems().items[fs]->createSwapDir();
c9e2e0e8 1853
c8f4eac4 1854 sd = dynamic_cast<SwapDir *>(swap->swapDirs[swap->n_configured].getRaw());
c9e2e0e8 1855
8e8d4f30 1856 /* parse the FS parameters and options */
d3b3ab85 1857 sd->parse(swap->n_configured, path_str);
c9e2e0e8 1858
d3b3ab85 1859 ++swap->n_configured;
c9e2e0e8 1860
cd748f27 1861 /* Update the max object size */
1862 update_maxobjsize();
752c3b27 1863}
1864
2d72d4fd 1865static const char *
505e35db 1866peer_type_str(const peer_t type)
1867{
1f140227 1868 const char * result;
1869
0cdcddb9 1870 switch (type) {
62e76326 1871
505e35db 1872 case PEER_PARENT:
1f140227 1873 result = "parent";
62e76326 1874 break;
1875
505e35db 1876 case PEER_SIBLING:
1f140227 1877 result = "sibling";
62e76326 1878 break;
1879
505e35db 1880 case PEER_MULTICAST:
1f140227 1881 result = "multicast";
62e76326 1882 break;
1883
505e35db 1884 default:
1f140227 1885 result = "unknown";
62e76326 1886 break;
505e35db 1887 }
1f140227 1888
1889 return result;
505e35db 1890}
1891
f1dc9b30 1892static void
a7d59104 1893dump_peer(StoreEntry * entry, const char *name, peer * p)
98ffb7e4 1894{
505e35db 1895 domain_ping *d;
505e35db 1896 domain_type *t;
1897 LOCAL_ARRAY(char, xname, 128);
62e76326 1898
d41de3c1 1899 while (p != NULL) {
4bc48d15 1900 storeAppendPrintf(entry, "%s %s %s %d %d name=%s",
62e76326 1901 name,
1902 p->host,
1903 neighborTypeStr(p),
1904 p->http_port,
4bc48d15
AJ
1905 p->icp.port,
1906 p->name);
62e76326 1907 dump_peer_options(entry, p);
1908
1909 for (d = p->peer_domain; d; d = d->next) {
1910 storeAppendPrintf(entry, "cache_peer_domain %s %s%s\n",
1911 p->host,
1912 d->do_ping ? null_string : "!",
1913 d->domain);
1914 }
1915
1916 if (p->access) {
1917 snprintf(xname, 128, "cache_peer_access %s", p->name);
1918 dump_acl_access(entry, xname, p->access);
1919 }
1920
1921 for (t = p->typelist; t; t = t->next) {
1922 storeAppendPrintf(entry, "neighbor_type_domain %s %s %s\n",
1923 p->host,
1924 peer_type_str(t->type),
1925 t->domain);
1926 }
1927
1928 p = p->next;
d41de3c1 1929 }
98ffb7e4 1930}
1931
86ae97bb
AJ
1932/**
1933 * utility function to prevent getservbyname() being called with a numeric value
1934 * on Windows at least it returns garage results.
1935 */
1936static bool
1937isUnsignedNumeric(const char *str, size_t len)
1938{
1939 if (len < 1) return false;
1940
1941 for (; len >0 && *str; str++, len--) {
1942 if (! isdigit(*str))
1943 return false;
1944 }
1945 return true;
1946}
1947
609fac72 1948/**
1949 \param proto 'tcp' or 'udp' for protocol
1950 \returns Port the named service is supposed to be listening on.
1951 */
1952static u_short
1953GetService(const char *proto)
1954{
1955 struct servent *port = NULL;
1956 /** Parses a port number or service name from the squid.conf */
1957 char *token = strtok(NULL, w_space);
1958 if (token == NULL) {
26ac0430
AJ
1959 self_destruct();
1960 return 0; /* NEVER REACHED */
609fac72 1961 }
1962 /** Returns either the service port number from /etc/services */
e1381638 1963 if ( !isUnsignedNumeric(token, strlen(token)) )
86ae97bb 1964 port = getservbyname(token, proto);
609fac72 1965 if (port != NULL) {
1966 return ntohs((u_short)port->s_port);
1967 }
1968 /** Or a numeric translation of the config text. */
1969 return xatos(token);
1970}
1971
1972/**
1973 \returns Port the named TCP service is supposed to be listening on.
1974 \copydoc GetService(const char *proto)
1975 */
1976inline u_short
1977GetTcpService(void)
1978{
1979 return GetService("tcp");
1980}
1981
1982/**
1983 \returns Port the named UDP service is supposed to be listening on.
1984 \copydoc GetService(const char *proto)
1985 */
1986inline u_short
1987GetUdpService(void)
1988{
1989 return GetService("udp");
1990}
1991
8203a132 1992static void
40a1495e 1993parse_peer(peer ** head)
7813c6d5 1994{
270b86af 1995 char *token = NULL;
40a1495e 1996 peer *p;
f5691f9c 1997 CBDATA_INIT_TYPE_FREECB(peer, peerDestroy);
72711e31 1998 p = cbdataAlloc(peer);
40a1495e 1999 p->http_port = CACHE_HTTP_PORT;
399cabec 2000 p->icp.port = CACHE_ICP_PORT;
40a1495e 2001 p->weight = 1;
d1b63fc8 2002 p->basetime = 0;
dc835977 2003 p->stats.logged_state = PEER_ALIVE;
62e76326 2004
e481c2dc 2005 if ((token = strtok(NULL, w_space)) == NULL)
62e76326 2006 self_destruct();
2007
40a1495e 2008 p->host = xstrdup(token);
26ac0430 2009
be753325 2010 p->name = xstrdup(token);
62e76326 2011
e481c2dc 2012 if ((token = strtok(NULL, w_space)) == NULL)
62e76326 2013 self_destruct();
2014
40a1495e 2015 p->type = parseNeighborType(token);
62e76326 2016
0d5a2006 2017 if (p->type == PEER_MULTICAST) {
2018 p->options.no_digest = 1;
2019 p->options.no_netdb_exchange = 1;
2020 }
2021
609fac72 2022 p->http_port = GetTcpService();
62e76326 2023
0e656b69 2024 if (!p->http_port)
2025 self_destruct();
62e76326 2026
609fac72 2027 p->icp.port = GetUdpService();
d67acb4e 2028 p->connection_auth = 2; /* auto */
62e76326 2029
270b86af 2030 while ((token = strtok(NULL, w_space))) {
62e76326 2031 if (!strcasecmp(token, "proxy-only")) {
2032 p->options.proxy_only = 1;
2033 } else if (!strcasecmp(token, "no-query")) {
2034 p->options.no_query = 1;
2035 } else if (!strcasecmp(token, "background-ping")) {
2036 p->options.background_ping = 1;
2037 } else if (!strcasecmp(token, "no-digest")) {
2038 p->options.no_digest = 1;
b0758e04
AJ
2039 } else if (!strcasecmp(token, "no-tproxy")) {
2040 p->options.no_tproxy = 1;
62e76326 2041 } else if (!strcasecmp(token, "multicast-responder")) {
2042 p->options.mcast_responder = 1;
8a368316 2043#if PEER_MULTICAST_SIBLINGS
79ddb8e2 2044 } else if (!strcasecmp(token, "multicast-siblings")) {
8a368316
AJ
2045 p->options.mcast_siblings = 1;
2046#endif
62e76326 2047 } else if (!strncasecmp(token, "weight=", 7)) {
2048 p->weight = xatoi(token + 7);
2049 } else if (!strncasecmp(token, "basetime=", 9)) {
2050 p->basetime = xatoi(token + 9);
2051 } else if (!strcasecmp(token, "closest-only")) {
2052 p->options.closest_only = 1;
2053 } else if (!strncasecmp(token, "ttl=", 4)) {
2054 p->mcast.ttl = xatoi(token + 4);
2055
2056 if (p->mcast.ttl < 0)
2057 p->mcast.ttl = 0;
2058
2059 if (p->mcast.ttl > 128)
2060 p->mcast.ttl = 128;
2061 } else if (!strcasecmp(token, "default")) {
2062 p->options.default_parent = 1;
2063 } else if (!strcasecmp(token, "round-robin")) {
2064 p->options.roundrobin = 1;
2065 } else if (!strcasecmp(token, "weighted-round-robin")) {
2066 p->options.weighted_roundrobin = 1;
dc9d133b 2067#if USE_HTCP
62e76326 2068 } else if (!strcasecmp(token, "htcp")) {
2069 p->options.htcp = 1;
18191440
AJ
2070 } else if (!strncasecmp(token, "htcp=", 5) || !strncasecmp(token, "htcp-", 5)) {
2071 /* Note: The htcp- form is deprecated, replaced by htcp= */
4f4fa815 2072 p->options.htcp = 1;
18191440
AJ
2073 char *tmp = xstrdup(token+5);
2074 char *mode, *nextmode;
2075 for (mode = nextmode = tmp; mode; mode = nextmode) {
2076 nextmode = strchr(mode, ',');
18191440
AJ
2077 if (nextmode)
2078 *nextmode++ = '\0';
2079 if (!strcasecmp(mode, "no-clr")) {
2080 if (p->options.htcp_only_clr)
2081 fatalf("parse_peer: can't set htcp-no-clr and htcp-only-clr simultaneously");
2082 p->options.htcp_no_clr = 1;
2083 } else if (!strcasecmp(mode, "no-purge-clr")) {
2084 p->options.htcp_no_purge_clr = 1;
2085 } else if (!strcasecmp(mode, "only-clr")) {
2086 if (p->options.htcp_no_clr)
2087 fatalf("parse_peer: can't set htcp no-clr and only-clr simultaneously");
2088 p->options.htcp_only_clr = 1;
2089 } else if (!strcasecmp(mode, "forward-clr")) {
2090 p->options.htcp_forward_clr = 1;
2091 } else if (!strcasecmp(mode, "oldsquid")) {
2092 p->options.htcp_oldsquid = 1;
2093 } else {
2094 fatalf("invalid HTCP mode '%s'", mode);
2095 }
2096 }
2097 safe_free(tmp);
dc9d133b 2098#endif
62e76326 2099 } else if (!strcasecmp(token, "no-netdb-exchange")) {
2100 p->options.no_netdb_exchange = 1;
62e76326 2101
2102 } else if (!strcasecmp(token, "carp")) {
2103 if (p->type != PEER_PARENT)
2104 fatalf("parse_peer: non-parent carp peer %s/%d\n", p->host, p->http_port);
2105
2106 p->options.carp = 1;
2107
f7e1d9ce
HN
2108 } else if (!strcasecmp(token, "userhash")) {
2109 if (p->type != PEER_PARENT)
2110 fatalf("parse_peer: non-parent userhash peer %s/%d\n", p->host, p->http_port);
2111
2112 p->options.userhash = 1;
2113
2114 } else if (!strcasecmp(token, "sourcehash")) {
2115 if (p->type != PEER_PARENT)
2116 fatalf("parse_peer: non-parent sourcehash peer %s/%d\n", p->host, p->http_port);
2117
2118 p->options.sourcehash = 1;
2119
95e36d02 2120#if DELAY_POOLS
62e76326 2121
2122 } else if (!strcasecmp(token, "no-delay")) {
2123 p->options.no_delay = 1;
afd88fbe 2124#endif
62e76326 2125
2126 } else if (!strncasecmp(token, "login=", 6)) {
2127 p->login = xstrdup(token + 6);
2128 rfc1738_unescape(p->login);
2129 } else if (!strncasecmp(token, "connect-timeout=", 16)) {
2130 p->connect_timeout = xatoi(token + 16);
ff9970cc
AJ
2131 } else if (!strncasecmp(token, "connect-fail-limit=", 19)) {
2132 p->connect_fail_limit = xatoi(token + 19);
7e3ce7b9 2133#if USE_CACHE_DIGESTS
62e76326 2134 } else if (!strncasecmp(token, "digest-url=", 11)) {
2135 p->digest_url = xstrdup(token + 11);
7e3ce7b9 2136#endif
62e76326 2137
2138 } else if (!strcasecmp(token, "allow-miss")) {
2139 p->options.allow_miss = 1;
2140 } else if (!strncasecmp(token, "max-conn=", 9)) {
2141 p->max_conn = xatoi(token + 9);
2142 } else if (!strcasecmp(token, "originserver")) {
2143 p->options.originserver = 1;
2144 } else if (!strncasecmp(token, "name=", 5)) {
2145 safe_free(p->name);
2146
2147 if (token[5])
2148 p->name = xstrdup(token + 5);
2149 } else if (!strncasecmp(token, "forceddomain=", 13)) {
2150 safe_free(p->domain);
2151
2152 if (token[13])
2153 p->domain = xstrdup(token + 13);
2154
a7ad6e4e 2155#if USE_SSL
62e76326 2156
2157 } else if (strcmp(token, "ssl") == 0) {
2158 p->use_ssl = 1;
2159 } else if (strncmp(token, "sslcert=", 8) == 0) {
2160 safe_free(p->sslcert);
2161 p->sslcert = xstrdup(token + 8);
2162 } else if (strncmp(token, "sslkey=", 7) == 0) {
2163 safe_free(p->sslkey);
2164 p->sslkey = xstrdup(token + 7);
2165 } else if (strncmp(token, "sslversion=", 11) == 0) {
2166 p->sslversion = atoi(token + 11);
2167 } else if (strncmp(token, "ssloptions=", 11) == 0) {
2168 safe_free(p->ssloptions);
2169 p->ssloptions = xstrdup(token + 11);
2170 } else if (strncmp(token, "sslcipher=", 10) == 0) {
2171 safe_free(p->sslcipher);
2172 p->sslcipher = xstrdup(token + 10);
2173 } else if (strncmp(token, "sslcafile=", 10) == 0) {
2174 safe_free(p->sslcafile);
a82a4fe4 2175 p->sslcafile = xstrdup(token + 10);
62e76326 2176 } else if (strncmp(token, "sslcapath=", 10) == 0) {
2177 safe_free(p->sslcapath);
a82a4fe4 2178 p->sslcapath = xstrdup(token + 10);
2179 } else if (strncmp(token, "sslcrlfile=", 11) == 0) {
2180 safe_free(p->sslcrlfile);
2181 p->sslcapath = xstrdup(token + 10);
62e76326 2182 } else if (strncmp(token, "sslflags=", 9) == 0) {
2183 safe_free(p->sslflags);
2184 p->sslflags = xstrdup(token + 9);
2185 } else if (strncmp(token, "ssldomain=", 10) == 0) {
2186 safe_free(p->ssldomain);
2187 p->ssldomain = xstrdup(token + 10);
a7ad6e4e 2188#endif
62e76326 2189
2190 } else if (strcmp(token, "front-end-https") == 0) {
2191 p->front_end_https = 1;
2192 } else if (strcmp(token, "front-end-https=on") == 0) {
2193 p->front_end_https = 1;
2194 } else if (strcmp(token, "front-end-https=auto") == 0) {
2195 p->front_end_https = 2;
26ac0430 2196 } else if (strcmp(token, "connection-auth=off") == 0) {
d67acb4e
AJ
2197 p->connection_auth = 0;
2198 } else if (strcmp(token, "connection-auth") == 0) {
2199 p->connection_auth = 1;
2200 } else if (strcmp(token, "connection-auth=on") == 0) {
2201 p->connection_auth = 1;
2202 } else if (strcmp(token, "connection-auth=auto") == 0) {
2203 p->connection_auth = 2;
62e76326 2204 } else {
bf8fe701 2205 debugs(3, 0, "parse_peer: token='" << token << "'");
62e76326 2206 self_destruct();
2207 }
270b86af 2208 }
62e76326 2209
be753325 2210 if (peerFindByName(p->name))
62e76326 2211 fatalf("ERROR: cache_peer %s specified twice\n", p->name);
2212
40a1495e 2213 if (p->weight < 1)
62e76326 2214 p->weight = 1;
2215
ff9970cc 2216 if (p->connect_fail_limit < 1)
a4d889e0 2217 p->connect_fail_limit = 10;
ff9970cc 2218
399cabec 2219 p->icp.version = ICP_VERSION_CURRENT;
62e76326 2220
eb406bb7 2221 p->test_fd = -1;
62e76326 2222
e13ee7ad 2223#if USE_CACHE_DIGESTS
62e76326 2224
e13ee7ad 2225 if (!p->options.no_digest) {
62e76326 2226 /* XXX This looks odd.. who has the original pointer
2227 * then?
2228 */
2229 PeerDigest *pd = peerDigestCreate(p);
2230 p->digest = cbdataReference(pd);
8a6218c6 2231 }
62e76326 2232
e13ee7ad 2233#endif
cc192b50 2234
2235 p->index = ++Config.npeers;
2236
0153d498 2237 while (*head != NULL)
62e76326 2238 head = &(*head)->next;
2239
0153d498 2240 *head = p;
62e76326 2241
32a47e3e 2242 peerClearRRStart();
0153d498 2243}
2244
2245static void
40a1495e 2246free_peer(peer ** P)
0153d498 2247{
40a1495e 2248 peer *p;
62e76326 2249
79d39a72 2250 while ((p = *P) != NULL) {
62e76326 2251 *P = p->next;
3855c318 2252#if USE_CACHE_DIGESTS
62e76326 2253
2254 cbdataReferenceDone(p->digest);
3855c318 2255#endif
62e76326 2256
2257 cbdataFree(p);
a47b9029 2258 }
62e76326 2259
987c67d1 2260 Config.npeers = 0;
270b86af 2261}
2262
2263static void
a7d59104 2264dump_cachemgrpasswd(StoreEntry * entry, const char *name, cachemgr_passwd * list)
270b86af 2265{
d41de3c1 2266 wordlist *w;
62e76326 2267
d41de3c1 2268 while (list != NULL) {
62e76326 2269 if (strcmp(list->passwd, "none") && strcmp(list->passwd, "disable"))
2270 storeAppendPrintf(entry, "%s XXXXXXXXXX", name);
2271 else
2272 storeAppendPrintf(entry, "%s %s", name, list->passwd);
2273
2274 for (w = list->actions; w != NULL; w = w->next) {
2275 storeAppendPrintf(entry, " %s", w->key);
2276 }
2277
2278 storeAppendPrintf(entry, "\n");
2279 list = list->next;
d41de3c1 2280 }
270b86af 2281}
2282
2283static void
a47b9029 2284parse_cachemgrpasswd(cachemgr_passwd ** head)
270b86af 2285{
2286 char *passwd = NULL;
2287 wordlist *actions = NULL;
22f3fd98 2288 cachemgr_passwd *p;
2289 cachemgr_passwd **P;
270b86af 2290 parse_string(&passwd);
2291 parse_wordlist(&actions);
e6ccf245 2292 p = static_cast<cachemgr_passwd *>(xcalloc(1, sizeof(cachemgr_passwd)));
22f3fd98 2293 p->passwd = passwd;
2294 p->actions = actions;
62e76326 2295
26aa7e31 2296 for (P = head; *P; P = &(*P)->next) {
62e76326 2297 /*
2298 * See if any of the actions from this line already have a
2299 * password from previous lines. The password checking
2300 * routines in cache_manager.c take the the password from
2301 * the first cachemgr_passwd struct that contains the
2302 * requested action. Thus, we should warn users who might
2303 * think they can have two passwords for the same action.
2304 */
2305 wordlist *w;
2306 wordlist *u;
2307
2308 for (w = (*P)->actions; w; w = w->next) {
2309 for (u = actions; u; u = u->next) {
2310 if (strcmp(w->key, u->key))
2311 continue;
2312
bf8fe701 2313 debugs(0, 0, "WARNING: action '" << u->key << "' (line " << config_lineno << ") already has a password");
62e76326 2314 }
2315 }
26aa7e31 2316 }
62e76326 2317
22f3fd98 2318 *P = p;
270b86af 2319}
2320
2321static void
a47b9029 2322free_cachemgrpasswd(cachemgr_passwd ** head)
270b86af 2323{
a47b9029 2324 cachemgr_passwd *p;
62e76326 2325
79d39a72 2326 while ((p = *head) != NULL) {
62e76326 2327 *head = p->next;
2328 xfree(p->passwd);
2329 wordlistDestroy(&p->actions);
2330 xfree(p);
a47b9029 2331 }
270b86af 2332}
2333
8203a132 2334static void
16300b58 2335dump_denyinfo(StoreEntry * entry, const char *name, acl_deny_info_list * var)
270b86af 2336{
d41de3c1 2337 acl_name_list *a;
62e76326 2338
d41de3c1 2339 while (var != NULL) {
62e76326 2340 storeAppendPrintf(entry, "%s %s", name, var->err_page_name);
2341
2342 for (a = var->acl_list; a != NULL; a = a->next)
2343 storeAppendPrintf(entry, " %s", a->name);
2344
2345 storeAppendPrintf(entry, "\n");
2346
2347 var = var->next;
d41de3c1 2348 }
270b86af 2349}
2350
2351static void
16300b58 2352parse_denyinfo(acl_deny_info_list ** var)
6e40f263 2353{
f1dc9b30 2354 aclParseDenyInfoLine(var);
6e40f263 2355}
403279e0 2356
1273d501 2357void
a47b9029 2358free_denyinfo(acl_deny_info_list ** list)
3c5557f9 2359{
56b63fa1 2360 acl_deny_info_list *a = NULL;
2361 acl_deny_info_list *a_next = NULL;
2362 acl_name_list *l = NULL;
2363 acl_name_list *l_next = NULL;
62e76326 2364
1273d501 2365 for (a = *list; a; a = a_next) {
62e76326 2366 for (l = a->acl_list; l; l = l_next) {
2367 l_next = l->next;
2368 memFree(l, MEM_ACL_NAME_LIST);
2369 l = NULL;
2370 }
2371
2372 a_next = a->next;
2373 memFree(a, MEM_ACL_DENY_INFO_LIST);
2374 a = NULL;
1273d501 2375 }
62e76326 2376
1273d501 2377 *list = NULL;
270b86af 2378}
2379
2380static void
505e35db 2381parse_peer_access(void)
270b86af 2382{
2383 char *host = NULL;
505e35db 2384 peer *p;
62e76326 2385
270b86af 2386 if (!(host = strtok(NULL, w_space)))
62e76326 2387 self_destruct();
2388
0cdcddb9 2389 if ((p = peerFindByName(host)) == NULL) {
bf8fe701 2390 debugs(15, 0, "" << cfg_filename << ", line " << config_lineno << ": No cache_peer '" << host << "'");
62e76326 2391 return;
0cdcddb9 2392 }
62e76326 2393
a9f20260 2394 aclParseAccessLine(LegacyParser, &p->access);
270b86af 2395}
2396
270b86af 2397static void
2398parse_hostdomain(void)
2399{
2400 char *host = NULL;
2401 char *domain = NULL;
62e76326 2402
270b86af 2403 if (!(host = strtok(NULL, w_space)))
62e76326 2404 self_destruct();
2405
f1dc9b30 2406 while ((domain = strtok(NULL, list_sep))) {
62e76326 2407 domain_ping *l = NULL;
2408 domain_ping **L = NULL;
2409 peer *p;
2410
2411 if ((p = peerFindByName(host)) == NULL) {
bf8fe701 2412 debugs(15, 0, "" << cfg_filename << ", line " << config_lineno << ": No cache_peer '" << host << "'");
62e76326 2413 continue;
2414 }
2415
2416 l = static_cast<domain_ping *>(xcalloc(1, sizeof(domain_ping)));
2417 l->do_ping = 1;
2418
2419 if (*domain == '!') { /* check for !.edu */
2420 l->do_ping = 0;
2421 domain++;
2422 }
2423
2424 l->domain = xstrdup(domain);
2425
3d0ac046 2426 for (L = &(p->peer_domain); *L; L = &((*L)->next));
62e76326 2427 *L = l;
f1dc9b30 2428 }
270b86af 2429}
2430
2431static void
2432parse_hostdomaintype(void)
2433{
2434 char *host = NULL;
2435 char *type = NULL;
2436 char *domain = NULL;
62e76326 2437
270b86af 2438 if (!(host = strtok(NULL, w_space)))
62e76326 2439 self_destruct();
2440
270b86af 2441 if (!(type = strtok(NULL, w_space)))
62e76326 2442 self_destruct();
2443
f1dc9b30 2444 while ((domain = strtok(NULL, list_sep))) {
62e76326 2445 domain_type *l = NULL;
2446 domain_type **L = NULL;
2447 peer *p;
2448
2449 if ((p = peerFindByName(host)) == NULL) {
bf8fe701 2450 debugs(15, 0, "" << cfg_filename << ", line " << config_lineno << ": No cache_peer '" << host << "'");
62e76326 2451 return;
2452 }
2453
2454 l = static_cast<domain_type *>(xcalloc(1, sizeof(domain_type)));
2455 l->type = parseNeighborType(type);
2456 l->domain = xstrdup(domain);
2457
3d0ac046 2458 for (L = &(p->typelist); *L; L = &((*L)->next));
62e76326 2459 *L = l;
f1dc9b30 2460 }
270b86af 2461}
2462
270b86af 2463static void
a7d59104 2464dump_int(StoreEntry * entry, const char *name, int var)
270b86af 2465{
f53b06f9 2466 storeAppendPrintf(entry, "%s %d\n", name, var);
270b86af 2467}
c1c29eb6 2468
94439e4e 2469void
270b86af 2470parse_int(int *var)
2471{
270b86af 2472 int i;
0e4e0e7d 2473 i = GetInteger();
270b86af 2474 *var = i;
2475}
090089c4 2476
0153d498 2477static void
2478free_int(int *var)
2479{
a47b9029 2480 *var = 0;
0153d498 2481}
2482
270b86af 2483static void
a7d59104 2484dump_onoff(StoreEntry * entry, const char *name, int var)
270b86af 2485{
f53b06f9 2486 storeAppendPrintf(entry, "%s %s\n", name, var ? "on" : "off");
270b86af 2487}
090089c4 2488
d205783b 2489void
270b86af 2490parse_onoff(int *var)
2491{
2492 char *token = strtok(NULL, w_space);
090089c4 2493
270b86af 2494 if (token == NULL)
62e76326 2495 self_destruct();
2496
270b86af 2497 if (!strcasecmp(token, "on") || !strcasecmp(token, "enable"))
62e76326 2498 *var = 1;
270b86af 2499 else
62e76326 2500 *var = 0;
270b86af 2501}
e90100aa 2502
0153d498 2503#define free_onoff free_int
52d3f198 2504
2505static void
2506dump_tristate(StoreEntry * entry, const char *name, int var)
2507{
2508 const char *state;
2509
2510 if (var > 0)
2511 state = "on";
2512 else if (var < 0)
2513 state = "warn";
2514 else
2515 state = "off";
2516
2517 storeAppendPrintf(entry, "%s %s\n", name, state);
2518}
2519
2520static void
2521parse_tristate(int *var)
2522{
2523 char *token = strtok(NULL, w_space);
2524
2525 if (token == NULL)
2526 self_destruct();
2527
2528 if (!strcasecmp(token, "on") || !strcasecmp(token, "enable"))
2529 *var = 1;
2530 else if (!strcasecmp(token, "warn"))
2531 *var = -1;
2532 else
2533 *var = 0;
2534}
2535
2536#define free_tristate free_int
30a4f2a8 2537
270b86af 2538static void
a7d59104 2539dump_refreshpattern(StoreEntry * entry, const char *name, refresh_t * head)
270b86af 2540{
d41de3c1 2541 while (head != NULL) {
0e1d7629 2542 storeAppendPrintf(entry, "%s%s %s %d %d%% %d",
62e76326 2543 name,
2544 head->flags.icase ? " -i" : null_string,
2545 head->pattern,
2546 (int) head->min / 60,
2547 (int) (100.0 * head->pct + 0.5),
2548 (int) head->max / 60);
4c3ef9b2 2549
2550 if (head->flags.refresh_ims)
2551 storeAppendPrintf(entry, " refresh-ims");
2552
3d8b6ba4
AJ
2553 if (head->flags.store_stale)
2554 storeAppendPrintf(entry, " store-stale");
2555
626096be 2556#if USE_HTTP_VIOLATIONS
62e76326 2557
2558 if (head->flags.override_expire)
2559 storeAppendPrintf(entry, " override-expire");
2560
2561 if (head->flags.override_lastmod)
2562 storeAppendPrintf(entry, " override-lastmod");
2563
2564 if (head->flags.reload_into_ims)
2565 storeAppendPrintf(entry, " reload-into-ims");
2566
2567 if (head->flags.ignore_reload)
2568 storeAppendPrintf(entry, " ignore-reload");
2569
38f9c547 2570 if (head->flags.ignore_no_cache)
2571 storeAppendPrintf(entry, " ignore-no-cache");
2572
2573 if (head->flags.ignore_no_store)
2574 storeAppendPrintf(entry, " ignore-no-store");
2575
4ca08219
AJ
2576 if (head->flags.ignore_must_revalidate)
2577 storeAppendPrintf(entry, " ignore-must-revalidate");
2578
38f9c547 2579 if (head->flags.ignore_private)
2580 storeAppendPrintf(entry, " ignore-private");
2581
2582 if (head->flags.ignore_auth)
2583 storeAppendPrintf(entry, " ignore-auth");
2584
9f60cfdf 2585#endif
62e76326 2586
2587 storeAppendPrintf(entry, "\n");
2588
2589 head = head->next;
d41de3c1 2590 }
270b86af 2591}
090089c4 2592
270b86af 2593static void
f1dc9b30 2594parse_refreshpattern(refresh_t ** head)
270b86af 2595{
f1dc9b30 2596 char *token;
2597 char *pattern;
2598 time_t min = 0;
c3f6d204 2599 double pct = 0.0;
f1dc9b30 2600 time_t max = 0;
4c3ef9b2 2601 int refresh_ims = 0;
3d8b6ba4
AJ
2602 int store_stale = 0;
2603
626096be 2604#if USE_HTTP_VIOLATIONS
62e76326 2605
1dfa1d81 2606 int override_expire = 0;
2607 int override_lastmod = 0;
cbe3a719 2608 int reload_into_ims = 0;
2609 int ignore_reload = 0;
38f9c547 2610 int ignore_no_cache = 0;
2611 int ignore_no_store = 0;
4ca08219 2612 int ignore_must_revalidate = 0;
38f9c547 2613 int ignore_private = 0;
2614 int ignore_auth = 0;
9f60cfdf 2615#endif
62e76326 2616
f1dc9b30 2617 int i;
2618 refresh_t *t;
2619 regex_t comp;
2620 int errcode;
2621 int flags = REG_EXTENDED | REG_NOSUB;
62e76326 2622
9eeb8e4b 2623 if ((token = strtok(NULL, w_space)) == NULL) {
62e76326 2624 self_destruct();
9eeb8e4b 2625 return;
2626 }
62e76326 2627
f1dc9b30 2628 if (strcmp(token, "-i") == 0) {
62e76326 2629 flags |= REG_ICASE;
2630 token = strtok(NULL, w_space);
f1dc9b30 2631 } else if (strcmp(token, "+i") == 0) {
62e76326 2632 flags &= ~REG_ICASE;
2633 token = strtok(NULL, w_space);
f1dc9b30 2634 }
62e76326 2635
9eeb8e4b 2636 if (token == NULL) {
62e76326 2637 self_destruct();
9eeb8e4b 2638 return;
2639 }
62e76326 2640
f1dc9b30 2641 pattern = xstrdup(token);
62e76326 2642
0e4e0e7d 2643 i = GetInteger(); /* token: min */
62e76326 2644
a632ba71
AJ
2645 /* catch negative and insanely huge values close to 32-bit wrap */
2646 if (i < 0) {
2647 debugs(3, DBG_IMPORTANT, "WARNING: refresh_pattern minimum age negative. Cropped back to zero.");
2648 i = 0;
2649 }
2650 if (i > 60*24*365) {
2651 debugs(3, DBG_IMPORTANT, "WARNING: refresh_pattern minimum age too high. Cropped back to 1 year.");
2652 i = 60*24*365;
2653 }
2654
f1dc9b30 2655 min = (time_t) (i * 60); /* convert minutes to seconds */
62e76326 2656
0e4e0e7d 2657 i = GetInteger(); /* token: pct */
62e76326 2658
c3f6d204 2659 pct = (double) i / 100.0;
62e76326 2660
0e4e0e7d 2661 i = GetInteger(); /* token: max */
62e76326 2662
a632ba71
AJ
2663 /* catch negative and insanely huge values close to 32-bit wrap */
2664 if (i < 0) {
2665 debugs(3, DBG_IMPORTANT, "WARNING: refresh_pattern maximum age negative. Cropped back to zero.");
2666 i = 0;
2667 }
2668 if (i > 60*24*365) {
2669 debugs(3, DBG_IMPORTANT, "WARNING: refresh_pattern maximum age too high. Cropped back to 1 year.");
2670 i = 60*24*365;
2671 }
2672
f1dc9b30 2673 max = (time_t) (i * 60); /* convert minutes to seconds */
62e76326 2674
1dfa1d81 2675 /* Options */
2676 while ((token = strtok(NULL, w_space)) != NULL) {
4c3ef9b2 2677 if (!strcmp(token, "refresh-ims")) {
2678 refresh_ims = 1;
3d8b6ba4
AJ
2679 } else if (!strcmp(token, "store-stale")) {
2680 store_stale = 1;
626096be 2681#if USE_HTTP_VIOLATIONS
62e76326 2682
4c3ef9b2 2683 } else if (!strcmp(token, "override-expire"))
62e76326 2684 override_expire = 1;
2685 else if (!strcmp(token, "override-lastmod"))
2686 override_lastmod = 1;
38f9c547 2687 else if (!strcmp(token, "ignore-no-cache"))
2688 ignore_no_cache = 1;
2689 else if (!strcmp(token, "ignore-no-store"))
2690 ignore_no_store = 1;
4ca08219
AJ
2691 else if (!strcmp(token, "ignore-must-revalidate"))
2692 ignore_must_revalidate = 1;
38f9c547 2693 else if (!strcmp(token, "ignore-private"))
2694 ignore_private = 1;
2695 else if (!strcmp(token, "ignore-auth"))
2696 ignore_auth = 1;
62e76326 2697 else if (!strcmp(token, "reload-into-ims")) {
2698 reload_into_ims = 1;
2699 refresh_nocache_hack = 1;
2700 /* tell client_side.c that this is used */
2701 } else if (!strcmp(token, "ignore-reload")) {
2702 ignore_reload = 1;
2703 refresh_nocache_hack = 1;
2704 /* tell client_side.c that this is used */
9f60cfdf 2705#endif
62e76326 2706
4c3ef9b2 2707 } else
26ac0430 2708 debugs(22, 0, "redreshAddToList: Unknown option '" << pattern << "': " << token);
1dfa1d81 2709 }
62e76326 2710
f1dc9b30 2711 if ((errcode = regcomp(&comp, pattern, flags)) != 0) {
62e76326 2712 char errbuf[256];
2713 regerror(errcode, &comp, errbuf, sizeof errbuf);
bf8fe701 2714 debugs(22, 0, "" << cfg_filename << " line " << config_lineno << ": " << config_input_line);
2715 debugs(22, 0, "refreshAddToList: Invalid regular expression '" << pattern << "': " << errbuf);
62e76326 2716 return;
f1dc9b30 2717 }
62e76326 2718
c3f6d204 2719 pct = pct < 0.0 ? 0.0 : pct;
f1dc9b30 2720 max = max < 0 ? 0 : max;
e6ccf245 2721 t = static_cast<refresh_t *>(xcalloc(1, sizeof(refresh_t)));
f1dc9b30 2722 t->pattern = (char *) xstrdup(pattern);
2723 t->compiled_pattern = comp;
2724 t->min = min;
c3f6d204 2725 t->pct = pct;
f1dc9b30 2726 t->max = max;
62e76326 2727
c3f6d204 2728 if (flags & REG_ICASE)
62e76326 2729 t->flags.icase = 1;
2730
4c3ef9b2 2731 if (refresh_ims)
2732 t->flags.refresh_ims = 1;
2733
3d8b6ba4
AJ
2734 if (store_stale)
2735 t->flags.store_stale = 1;
2736
626096be 2737#if USE_HTTP_VIOLATIONS
62e76326 2738
1dfa1d81 2739 if (override_expire)
62e76326 2740 t->flags.override_expire = 1;
2741
1dfa1d81 2742 if (override_lastmod)
62e76326 2743 t->flags.override_lastmod = 1;
2744
cbe3a719 2745 if (reload_into_ims)
62e76326 2746 t->flags.reload_into_ims = 1;
2747
cbe3a719 2748 if (ignore_reload)
62e76326 2749 t->flags.ignore_reload = 1;
2750
38f9c547 2751 if (ignore_no_cache)
2752 t->flags.ignore_no_cache = 1;
2753
2754 if (ignore_no_store)
2755 t->flags.ignore_no_store = 1;
2756
4ca08219
AJ
2757 if (ignore_must_revalidate)
2758 t->flags.ignore_must_revalidate = 1;
2759
38f9c547 2760 if (ignore_private)
2761 t->flags.ignore_private = 1;
2762
2763 if (ignore_auth)
2764 t->flags.ignore_auth = 1;
2765
9f60cfdf 2766#endif
62e76326 2767
f1dc9b30 2768 t->next = NULL;
62e76326 2769
f1dc9b30 2770 while (*head)
62e76326 2771 head = &(*head)->next;
2772
f1dc9b30 2773 *head = t;
62e76326 2774
f1dc9b30 2775 safe_free(pattern);
270b86af 2776}
090089c4 2777
270b86af 2778static void
a47b9029 2779free_refreshpattern(refresh_t ** head)
270b86af 2780{
f1dc9b30 2781 refresh_t *t;
62e76326 2782
79d39a72 2783 while ((t = *head) != NULL) {
62e76326 2784 *head = t->next;
2785 safe_free(t->pattern);
2786 regfree(&t->compiled_pattern);
2787 safe_free(t);
f1dc9b30 2788 }
c2066637 2789
626096be 2790#if USE_HTTP_VIOLATIONS
c2066637 2791 refresh_nocache_hack = 0;
8970d351 2792
2793#endif
270b86af 2794}
12b9e9b1 2795
270b86af 2796static void
a7d59104 2797dump_string(StoreEntry * entry, const char *name, char *var)
270b86af 2798{
f53b06f9 2799 if (var != NULL)
62e76326 2800 storeAppendPrintf(entry, "%s %s\n", name, var);
270b86af 2801}
98ffb7e4 2802
270b86af 2803static void
0153d498 2804parse_string(char **var)
270b86af 2805{
2806 char *token = strtok(NULL, w_space);
270b86af 2807 safe_free(*var);
62e76326 2808
270b86af 2809 if (token == NULL)
62e76326 2810 self_destruct();
2811
270b86af 2812 *var = xstrdup(token);
2813}
b15e6857 2814
3a69ddf3 2815void
2816ConfigParser::ParseString(char **var)
2817{
2818 parse_string(var);
2819}
2820
2821void
30abd221 2822ConfigParser::ParseString(String *var)
3a69ddf3 2823{
2824 char *token = strtok(NULL, w_space);
2825
2826 if (token == NULL)
2827 self_destruct();
2828
30abd221 2829 var->reset(token);
3a69ddf3 2830}
2831
0153d498 2832static void
2833free_string(char **var)
2834{
027acbaf 2835 safe_free(*var);
0153d498 2836}
caebbe00 2837
94439e4e 2838void
f1dc9b30 2839parse_eol(char *volatile *var)
270b86af 2840{
852751f7 2841 unsigned char *token = (unsigned char *) strtok(NULL, null_string);
270b86af 2842 safe_free(*var);
62e76326 2843
9eeb8e4b 2844 if (!token) {
62e76326 2845 self_destruct();
9eeb8e4b 2846 return;
2847 }
62e76326 2848
e4755e29 2849 while (*token && xisspace(*token))
62e76326 2850 token++;
2851
9eeb8e4b 2852 if (!*token) {
62e76326 2853 self_destruct();
9eeb8e4b 2854 return;
2855 }
62e76326 2856
852751f7 2857 *var = xstrdup((char *) token);
270b86af 2858}
090089c4 2859
52d3f198 2860#define dump_eol dump_string
2861#define free_eol free_string
2862
270b86af 2863static void
a7d59104 2864dump_time_t(StoreEntry * entry, const char *name, time_t var)
090089c4 2865{
f53b06f9 2866 storeAppendPrintf(entry, "%s %d seconds\n", name, (int) var);
090089c4 2867}
2868
94439e4e 2869void
a47b9029 2870parse_time_t(time_t * var)
0ffd22bc 2871{
f1dc9b30 2872 parseTimeLine(var, T_SECOND_STR);
0ffd22bc 2873}
2874
270b86af 2875static void
a47b9029 2876free_time_t(time_t * var)
270b86af 2877{
a47b9029 2878 *var = 0;
270b86af 2879}
9906e724 2880
0477a072 2881#if UNUSED_CODE
9906e724 2882static void
a7d59104 2883dump_size_t(StoreEntry * entry, const char *name, size_t var)
1b635117 2884{
f53b06f9 2885 storeAppendPrintf(entry, "%s %d\n", name, (int) var);
1b635117 2886}
0477a072 2887#endif
1b635117 2888
2889static void
a7d59104 2890dump_b_size_t(StoreEntry * entry, const char *name, size_t var)
9906e724 2891{
f53b06f9 2892 storeAppendPrintf(entry, "%s %d %s\n", name, (int) var, B_BYTES_STR);
9906e724 2893}
2894
e210930b
AJ
2895#if !USE_DNSSERVERS
2896static void
2897dump_b_ssize_t(StoreEntry * entry, const char *name, ssize_t var)
2898{
2899 storeAppendPrintf(entry, "%s %d %s\n", name, (int) var, B_BYTES_STR);
2900}
2901#endif
2902
3e62bd58 2903#if UNUSED_CODE
9906e724 2904static void
a7d59104 2905dump_kb_size_t(StoreEntry * entry, const char *name, size_t var)
9906e724 2906{
f53b06f9 2907 storeAppendPrintf(entry, "%s %d %s\n", name, (int) var, B_KBYTES_STR);
9906e724 2908}
3e62bd58 2909#endif
9906e724 2910
47f6e231 2911static void
2912dump_b_int64_t(StoreEntry * entry, const char *name, int64_t var)
2913{
2914 storeAppendPrintf(entry, "%s %"PRId64" %s\n", name, var, B_BYTES_STR);
2915}
2916
2917static void
2918dump_kb_int64_t(StoreEntry * entry, const char *name, int64_t var)
2919{
2920 storeAppendPrintf(entry, "%s %"PRId64" %s\n", name, var, B_KBYTES_STR);
2921}
2922
0477a072 2923#if UNUSED_CODE
9906e724 2924static void
a47b9029 2925parse_size_t(size_t * var)
1b635117 2926{
1b635117 2927 int i;
0e4e0e7d 2928 i = GetInteger();
1b635117 2929 *var = (size_t) i;
2930}
0477a072 2931#endif
1b635117 2932
2933static void
2934parse_b_size_t(size_t * var)
9906e724 2935{
2936 parseBytesLine(var, B_BYTES_STR);
2937}
2938
e210930b
AJ
2939#if !USE_DNSSERVERS
2940static void
2941parse_b_ssize_t(ssize_t * var)
2942{
2943 parseBytesLineSigned(var, B_BYTES_STR);
2944}
2945#endif
2946
3e62bd58 2947#if UNUSED_CODE
9906e724 2948static void
a47b9029 2949parse_kb_size_t(size_t * var)
9906e724 2950{
2951 parseBytesLine(var, B_KBYTES_STR);
2952}
3e62bd58 2953#endif
9906e724 2954
47f6e231 2955static void
2956parse_b_int64_t(int64_t * var)
2957{
2958 parseBytesLine64(var, B_BYTES_STR);
2959}
2960
2961static void
2962parse_kb_int64_t(int64_t * var)
2963{
2964 parseBytesLine64(var, B_KBYTES_STR);
2965}
2966
9906e724 2967static void
a47b9029 2968free_size_t(size_t * var)
9906e724 2969{
a47b9029 2970 *var = 0;
9906e724 2971}
2972
e210930b
AJ
2973#if !USE_DNSSERVERS
2974static void
2975free_ssize_t(ssize_t * var)
2976{
2977 *var = 0;
2978}
2979#endif
2980
47f6e231 2981static void
2982free_b_int64_t(int64_t * var)
2983{
2984 *var = 0;
2985}
2986
1b635117 2987#define free_b_size_t free_size_t
e210930b 2988#define free_b_ssize_t free_ssize_t
9906e724 2989#define free_kb_size_t free_size_t
2990#define free_mb_size_t free_size_t
2991#define free_gb_size_t free_size_t
47f6e231 2992#define free_kb_int64_t free_b_int64_t
090089c4 2993
8203a132 2994static void
a7d59104 2995dump_ushort(StoreEntry * entry, const char *name, u_short var)
090089c4 2996{
f53b06f9 2997 storeAppendPrintf(entry, "%s %d\n", name, var);
270b86af 2998}
090089c4 2999
0153d498 3000static void
a47b9029 3001free_ushort(u_short * u)
0153d498 3002{
3003 *u = 0;
3004}
3005
270b86af 3006static void
3007parse_ushort(u_short * var)
b67e2c8c 3008{
3009 ConfigParser::ParseUShort(var);
3010}
3011
3012void
3013ConfigParser::ParseUShort(u_short *var)
270b86af 3014{
0e656b69 3015 *var = GetShort();
090089c4 3016}
3017
3a69ddf3 3018void
3019ConfigParser::ParseBool(bool *var)
3020{
3021 int i = GetInteger();
3022
3023 if (0 == i)
3024 *var = false;
3025 else if (1 == i)
3026 *var = true;
3027 else
3028 self_destruct();
3029}
3030
270b86af 3031static void
a7d59104 3032dump_wordlist(StoreEntry * entry, const char *name, wordlist * list)
270b86af 3033{
270b86af 3034 while (list != NULL) {
62e76326 3035 storeAppendPrintf(entry, "%s %s\n", name, list->key);
3036 list = list->next;
429fdbec 3037 }
429fdbec 3038}
3039
3a69ddf3 3040void
3041ConfigParser::ParseWordList(wordlist ** list)
3042{
3043 parse_wordlist(list);
3044}
3045
94439e4e 3046void
270b86af 3047parse_wordlist(wordlist ** list)
429fdbec 3048{
270b86af 3049 char *token;
a3c6890f 3050 char *t = strtok(NULL, "");
62e76326 3051
a3c6890f 3052 while ((token = strwordtok(NULL, &t)))
62e76326 3053 wordlistAdd(list, token);
429fdbec 3054}
270b86af 3055
b3d8a9e8 3056#if 0 /* now unused */
f8d9f54a 3057static int
5da06f20 3058check_null_wordlist(wordlist * w)
f8d9f54a 3059{
3060 return w == NULL;
3061}
b3d8a9e8 3062#endif
f8d9f54a 3063
63e9d884 3064static int
c6d5b87b 3065check_null_acl_access(acl_access * a)
63e9d884 3066{
3067 return a == NULL;
3068}
3069
0153d498 3070#define free_wordlist wordlistDestroy
270b86af 3071
d548ee64 3072#define free_uri_whitespace free_int
3073
3074static void
3075parse_uri_whitespace(int *var)
3076{
3077 char *token = strtok(NULL, w_space);
62e76326 3078
d548ee64 3079 if (token == NULL)
62e76326 3080 self_destruct();
3081
7e3ce7b9 3082 if (!strcasecmp(token, "strip"))
62e76326 3083 *var = URI_WHITESPACE_STRIP;
7e3ce7b9 3084 else if (!strcasecmp(token, "deny"))
62e76326 3085 *var = URI_WHITESPACE_DENY;
d548ee64 3086 else if (!strcasecmp(token, "allow"))
62e76326 3087 *var = URI_WHITESPACE_ALLOW;
d548ee64 3088 else if (!strcasecmp(token, "encode"))
62e76326 3089 *var = URI_WHITESPACE_ENCODE;
d548ee64 3090 else if (!strcasecmp(token, "chop"))
62e76326 3091 *var = URI_WHITESPACE_CHOP;
d548ee64 3092 else
62e76326 3093 self_destruct();
d548ee64 3094}
3095
d548ee64 3096static void
3097dump_uri_whitespace(StoreEntry * entry, const char *name, int var)
3098{
c193c972 3099 const char *s;
62e76326 3100
d548ee64 3101 if (var == URI_WHITESPACE_ALLOW)
62e76326 3102 s = "allow";
d548ee64 3103 else if (var == URI_WHITESPACE_ENCODE)
62e76326 3104 s = "encode";
d548ee64 3105 else if (var == URI_WHITESPACE_CHOP)
62e76326 3106 s = "chop";
7e3ce7b9 3107 else if (var == URI_WHITESPACE_DENY)
62e76326 3108 s = "deny";
7e3ce7b9 3109 else
62e76326 3110 s = "strip";
3111
d548ee64 3112 storeAppendPrintf(entry, "%s %s\n", name, s);
3113}
3114
6a566b9c 3115static void
c1dd71ae 3116free_removalpolicy(RemovalPolicySettings ** settings)
6a566b9c 3117{
3118 if (!*settings)
62e76326 3119 return;
3120
6a566b9c 3121 free_string(&(*settings)->type);
62e76326 3122
6a566b9c 3123 free_wordlist(&(*settings)->args);
62e76326 3124
c8f4eac4 3125 delete *settings;
62e76326 3126
6a566b9c 3127 *settings = NULL;
3128}
3129
3130static void
c1dd71ae 3131parse_removalpolicy(RemovalPolicySettings ** settings)
6a566b9c 3132{
3133 if (*settings)
62e76326 3134 free_removalpolicy(settings);
3135
c8f4eac4 3136 *settings = new RemovalPolicySettings;
62e76326 3137
6a566b9c 3138 parse_string(&(*settings)->type);
62e76326 3139
6a566b9c 3140 parse_wordlist(&(*settings)->args);
3141}
3142
3143static void
c1dd71ae 3144dump_removalpolicy(StoreEntry * entry, const char *name, RemovalPolicySettings * settings)
6a566b9c 3145{
3146 wordlist *args;
3147 storeAppendPrintf(entry, "%s %s", name, settings->type);
3148 args = settings->args;
62e76326 3149
6a566b9c 3150 while (args) {
62e76326 3151 storeAppendPrintf(entry, " %s", args->key);
3152 args = args->next;
6a566b9c 3153 }
62e76326 3154
be58afb5 3155 storeAppendPrintf(entry, "\n");
6a566b9c 3156}
c1dd71ae 3157
ea21d497
HN
3158static void
3159free_memcachemode(SquidConfig * config)
3160{
3161 return;
3162}
3163
3164static void
3165parse_memcachemode(SquidConfig * config)
3166{
3167 char *token = strtok(NULL, w_space);
3168 if (!token)
e1381638 3169 self_destruct();
ea21d497 3170
10aeba1d 3171 if (strcmp(token, "always") == 0) {
e1381638
AJ
3172 Config.onoff.memory_cache_first = 1;
3173 Config.onoff.memory_cache_disk = 1;
10aeba1d 3174 } else if (strcmp(token, "disk") == 0) {
e1381638
AJ
3175 Config.onoff.memory_cache_first = 0;
3176 Config.onoff.memory_cache_disk = 1;
ea21d497 3177 } else if (strncmp(token, "net", 3) == 0) {
e1381638
AJ
3178 Config.onoff.memory_cache_first = 1;
3179 Config.onoff.memory_cache_disk = 0;
10aeba1d 3180 } else if (strcmp(token, "never") == 0) {
e1381638
AJ
3181 Config.onoff.memory_cache_first = 0;
3182 Config.onoff.memory_cache_disk = 0;
ea21d497 3183 } else
e1381638 3184 self_destruct();
ea21d497
HN
3185}
3186
3187static void
3188dump_memcachemode(StoreEntry * entry, const char *name, SquidConfig &config)
3189{
3190 storeAppendPrintf(entry, "%s ", name);
3191 if (Config.onoff.memory_cache_first && Config.onoff.memory_cache_disk)
e1381638 3192 storeAppendPrintf(entry, "always");
ea21d497 3193 else if (!Config.onoff.memory_cache_first && Config.onoff.memory_cache_disk)
e1381638 3194 storeAppendPrintf(entry, "disk");
ea21d497 3195 else if (Config.onoff.memory_cache_first && !Config.onoff.memory_cache_disk)
e1381638 3196 storeAppendPrintf(entry, "network");
ea21d497 3197 else if (!Config.onoff.memory_cache_first && !Config.onoff.memory_cache_disk)
e1381638 3198 storeAppendPrintf(entry, "none");
ea21d497
HN
3199 storeAppendPrintf(entry, "\n");
3200}
3201
cca8ba0d 3202#include "cf_parser.cci"
f1dc9b30 3203
3204peer_t
3205parseNeighborType(const char *s)
3206{
3207 if (!strcasecmp(s, "parent"))
62e76326 3208 return PEER_PARENT;
3209
f1dc9b30 3210 if (!strcasecmp(s, "neighbor"))
62e76326 3211 return PEER_SIBLING;
3212
f1dc9b30 3213 if (!strcasecmp(s, "neighbour"))
62e76326 3214 return PEER_SIBLING;
3215
f1dc9b30 3216 if (!strcasecmp(s, "sibling"))
62e76326 3217 return PEER_SIBLING;
3218
f1dc9b30 3219 if (!strcasecmp(s, "multicast"))
62e76326 3220 return PEER_MULTICAST;
3221
bf8fe701 3222 debugs(15, 0, "WARNING: Unknown neighbor type: " << s);
62e76326 3223
f1dc9b30 3224 return PEER_SIBLING;
3225}
f150dd4b 3226
0b0cfcf2 3227#if USE_WCCPv2
52f772de 3228static void
b7ac5457 3229parse_IpAddress_list(Ip::Address_list ** head)
52f772de 3230{
3231 char *token;
b7ac5457
AJ
3232 Ip::Address_list *s;
3233 Ip::Address ipa;
62e76326 3234
52f772de 3235 while ((token = strtok(NULL, w_space))) {
82b7abe3
AJ
3236 if (GetHostWithPort(token, &ipa)) {
3237
3238 while (*head)
3239 head = &(*head)->next;
3240
b7ac5457 3241 s = static_cast<Ip::Address_list *>(xcalloc(1, sizeof(*s)));
82b7abe3
AJ
3242 s->s = ipa;
3243
3244 *head = s;
9d65168e 3245 } else
82b7abe3 3246 self_destruct();
7e3ce7b9 3247 }
3248}
3249
3250static void
b7ac5457 3251dump_IpAddress_list(StoreEntry * e, const char *n, const Ip::Address_list * s)
7e3ce7b9 3252{
cc192b50 3253 char ntoabuf[MAX_IPSTRLEN];
3254
7e3ce7b9 3255 while (s) {
cc192b50 3256 storeAppendPrintf(e, "%s %s\n",
62e76326 3257 n,
cc192b50 3258 s->s.NtoA(ntoabuf,MAX_IPSTRLEN));
62e76326 3259 s = s->next;
7e3ce7b9 3260 }
3261}
3262
3263static void
b7ac5457 3264free_IpAddress_list(Ip::Address_list ** head)
7e3ce7b9 3265{
26ac0430
AJ
3266 if (*head) delete *head;
3267 *head = NULL;
7e3ce7b9 3268}
3269
0b0cfcf2 3270#if CURRENTLY_UNUSED
3271/* This code was previously used by http_port. Left as it really should
3272 * be used by icp_port and htcp_port
3273 */
7e3ce7b9 3274static int
b7ac5457 3275check_null_IpAddress_list(const Ip::Address_list * s)
7e3ce7b9 3276{
3277 return NULL == s;
3278}
62e76326 3279
3f38a55e 3280#endif /* CURRENTLY_UNUSED */
0b0cfcf2 3281#endif /* USE_WCCPv2 */
7e3ce7b9 3282
154dc884 3283CBDATA_CLASS_INIT(http_port_list);
3284
d193a436 3285static void
3f38a55e 3286parse_http_port_specification(http_port_list * s, char *token)
d193a436 3287{
3f38a55e 3288 char *host = NULL;
3f38a55e 3289 unsigned short port = 0;
cc192b50 3290 char *t = NULL;
3291 char *junk = NULL;
62e76326 3292
5529ca8a 3293 s->disable_pmtu_discovery = DISABLE_PMTU_OFF;
13adaf1f 3294 s->name = xstrdup(token);
d67acb4e 3295 s->connection_auth_disabled = false;
5529ca8a 3296
cc192b50 3297 if (*token == '[') {
3298 /* [ipv6]:port */
26ac0430
AJ
3299 host = token + 1;
3300 t = strchr(host, ']');
3301 if (!t) {
cc192b50 3302 debugs(3, 0, "http(s)_port: missing ']' on IPv6 address: " << token);
26ac0430 3303 self_destruct();
cc192b50 3304 }
26ac0430
AJ
3305 *t++ = '\0';
3306 if (*t != ':') {
cc192b50 3307 debugs(3, 0, "http(s)_port: missing Port in: " << token);
26ac0430 3308 self_destruct();
cc192b50 3309 }
055421ee
AJ
3310 if (!Ip::EnableIpv6) {
3311 debugs(3, DBG_CRITICAL, "FATAL: http(s)_port: IPv6 is not available.");
26ac0430
AJ
3312 self_destruct();
3313 }
055421ee
AJ
3314 port = xatos(t + 1);
3315 } else if ((t = strchr(token, ':'))) {
3316 /* host:port */
3317 /* ipv4:port */
3318 host = token;
3319 *t = '\0';
3320 port = xatos(t + 1);
3321
3322 } else if ((port = strtol(token, &junk, 10)), !*junk) {
3323 /* port */
3324 debugs(3, 3, "http(s)_port: found Listen on Port: " << port);
3325 } else {
3326 debugs(3, 0, "http(s)_port: missing Port: " << token);
3327 self_destruct();
3328 }
62e76326 3329
cc192b50 3330 if (port == 0) {
3331 debugs(3, 0, "http(s)_port: Port cannot be 0: " << token);
0e656b69 3332 self_destruct();
cc192b50 3333 }
0e656b69 3334
cc192b50 3335 if (NULL == host) {
3336 s->s.SetAnyAddr();
3337 s->s.SetPort(port);
6df7ad56
AJ
3338 if (!Ip::EnableIpv6)
3339 s->s.SetIPv4();
7e07ced1 3340 debugs(3, 3, "http(s)_port: found Listen on wildcard address: *:" << s->s.GetPort() );
26ac0430 3341 } else if ( s->s = host ) { /* check/parse numeric IPA */
cc192b50 3342 s->s.SetPort(port);
6df7ad56
AJ
3343 if (!Ip::EnableIpv6)
3344 s->s.SetIPv4();
cc192b50 3345 debugs(3, 3, "http(s)_port: Listen on Host/IP: " << host << " --> " << s->s);
26ac0430 3346 } else if ( s->s.GetHostByName(host) ) { /* check/parse for FQDN */
62e76326 3347 /* dont use ipcache */
62e76326 3348 s->defaultsite = xstrdup(host);
cc192b50 3349 s->s.SetPort(port);
6df7ad56
AJ
3350 if (!Ip::EnableIpv6)
3351 s->s.SetIPv4();
cc192b50 3352 debugs(3, 3, "http(s)_port: found Listen as Host " << s->defaultsite << " on IP: " << s->s);
26ac0430 3353 } else {
cc192b50 3354 debugs(3, 0, "http(s)_port: failed to resolve Host/IP: " << host);
62e76326 3355 self_destruct();
cc192b50 3356 }
3f38a55e 3357}
3358
3359static void
3360parse_http_port_option(http_port_list * s, char *token)
3361{
c7b1dd5d
AJ
3362 /* modes first */
3363
3364 if (strcmp(token, "accel") == 0) {
3365 if (s->intercepted || s->spoof_client_ip) {
3366 debugs(3, DBG_CRITICAL, "FATAL: http(s)_port: Accelerator mode requires its own port. It cannot be shared with other modes.");
3367 self_destruct();
3368 }
3369 s->accel = 1;
3370 } else if (strcmp(token, "transparent") == 0 || strcmp(token, "intercept") == 0) {
3371 if (s->accel || s->spoof_client_ip) {
3372 debugs(3, DBG_CRITICAL, "FATAL: http(s)_port: Intercept mode requires its own interception port. It cannot be shared with other modes.");
3373 self_destruct();
3374 }
3375 s->intercepted = 1;
3376 Ip::Interceptor.StartInterception();
3377 /* Log information regarding the port modes under interception. */
3378 debugs(3, DBG_IMPORTANT, "Starting Authentication on port " << s->s);
3379 debugs(3, DBG_IMPORTANT, "Disabling Authentication on port " << s->s << " (interception enabled)");
3380
c7b1dd5d 3381 /* INET6: until transparent REDIRECT works on IPv6 SOCKET, force wildcard to IPv4 */
055421ee
AJ
3382 if (Ip::EnableIpv6)
3383 debugs(3, DBG_IMPORTANT, "Disabling IPv6 on port " << s->s << " (interception enabled)");
c7b1dd5d
AJ
3384 if ( !s->s.SetIPv4() ) {
3385 debugs(3, DBG_CRITICAL, "FATAL: http(s)_port: IPv6 addresses cannot be transparent (protocol does not provide NAT)" << s->s );
3386 self_destruct();
3387 }
c7b1dd5d
AJ
3388 } else if (strcmp(token, "tproxy") == 0) {
3389 if (s->intercepted || s->accel) {
3390 debugs(3,DBG_CRITICAL, "FATAL: http(s)_port: TPROXY option requires its own interception port. It cannot be shared with other modes.");
3391 self_destruct();
3392 }
3393 s->spoof_client_ip = 1;
3394 Ip::Interceptor.StartTransparency();
3395 /* Log information regarding the port modes under transparency. */
3396 debugs(3, DBG_IMPORTANT, "Starting IP Spoofing on port " << s->s);
3397 debugs(3, DBG_IMPORTANT, "Disabling Authentication on port " << s->s << " (IP spoofing enabled)");
3398
3399 if (!Ip::Interceptor.ProbeForTproxy(s->s)) {
3400 debugs(3, DBG_CRITICAL, "FATAL: http(s)_port: TPROXY support in the system does not work.");
3401 self_destruct();
3402 }
3403
3404 } else if (strncmp(token, "defaultsite=", 12) == 0) {
3405 if (!s->accel) {
3406 debugs(3, DBG_CRITICAL, "FATAL: http(s)_port: defaultsite option requires Acceleration mode flag.");
3407 self_destruct();
3408 }
62e76326 3409 safe_free(s->defaultsite);
3410 s->defaultsite = xstrdup(token + 12);
3f38a55e 3411 } else if (strcmp(token, "vhost") == 0) {
c7b1dd5d
AJ
3412 if (!s->accel) {
3413 debugs(3, DBG_CRITICAL, "FATAL: http(s)_port: vhost option requires Acceleration mode flag.");
3414 self_destruct();
3415 }
62e76326 3416 s->vhost = 1;
3f38a55e 3417 } else if (strcmp(token, "vport") == 0) {
c7b1dd5d
AJ
3418 if (!s->accel) {
3419 debugs(3, DBG_CRITICAL, "FATAL: http(s)_port: vport option requires Acceleration mode flag.");
3420 self_destruct();
3421 }
62e76326 3422 s->vport = -1;
3f38a55e 3423 } else if (strncmp(token, "vport=", 6) == 0) {
c7b1dd5d
AJ
3424 if (!s->accel) {
3425 debugs(3, DBG_CRITICAL, "FATAL: http(s)_port: vport option requires Acceleration mode flag.");
3426 self_destruct();
3427 }
0e656b69 3428 s->vport = xatos(token + 6);
3f38a55e 3429 } else if (strncmp(token, "protocol=", 9) == 0) {
c7b1dd5d
AJ
3430 if (!s->accel) {
3431 debugs(3, DBG_CRITICAL, "FATAL: http(s)_port: protocol option requires Acceleration mode flag.");
3432 self_destruct();
3433 }
62e76326 3434 s->protocol = xstrdup(token + 9);
7f7bdd96 3435 } else if (strcmp(token, "allow-direct") == 0) {
c7b1dd5d
AJ
3436 if (!s->accel) {
3437 debugs(3, DBG_CRITICAL, "FATAL: http(s)_port: vport option requires Acceleration mode flag.");
3438 self_destruct();
3439 }
7f7bdd96 3440 s->allow_direct = 1;
432bc83c 3441 } else if (strcmp(token, "ignore-cc") == 0) {
626096be 3442#if !USE_HTTP_VIOLATIONS
838daf3f 3443 if (!s->accel) {
c7b1dd5d 3444 debugs(3, DBG_CRITICAL, "FATAL: http(s)_port: ignore-cc option requires Scceleration mode flag.");
838daf3f
A
3445 self_destruct();
3446 }
432bc83c 3447#endif
c7b1dd5d
AJ
3448 s->ignore_cc = 1;
3449 } else if (strncmp(token, "name=", 5) == 0) {
3450 safe_free(s->name);
3451 s->name = xstrdup(token + 5);
d67acb4e
AJ
3452 } else if (strcmp(token, "no-connection-auth") == 0) {
3453 s->connection_auth_disabled = true;
3454 } else if (strcmp(token, "connection-auth=off") == 0) {
26ac0430 3455 s->connection_auth_disabled = true;
d67acb4e 3456 } else if (strcmp(token, "connection-auth") == 0) {
26ac0430 3457 s->connection_auth_disabled = false;
d67acb4e 3458 } else if (strcmp(token, "connection-auth=on") == 0) {
26ac0430 3459 s->connection_auth_disabled = false;
5529ca8a 3460 } else if (strncmp(token, "disable-pmtu-discovery=", 23) == 0) {
3461 if (!strcasecmp(token + 23, "off"))
3462 s->disable_pmtu_discovery = DISABLE_PMTU_OFF;
3463 else if (!strcasecmp(token + 23, "transparent"))
3464 s->disable_pmtu_discovery = DISABLE_PMTU_TRANSPARENT;
3465 else if (!strcasecmp(token + 23, "always"))
3466 s->disable_pmtu_discovery = DISABLE_PMTU_ALWAYS;
3467 else
3468 self_destruct();
cc192b50 3469 } else if (strcmp(token, "ipv4") == 0) {
26ac0430 3470 if ( !s->s.SetIPv4() ) {
055421ee 3471 debugs(3, DBG_CRITICAL, "FATAL: http(s)_port: IPv6 addresses cannot be used as IPv4-Only. " << s->s );
cc192b50 3472 self_destruct();
3473 }
26ac0430
AJ
3474 } else if (strcmp(token, "tcpkeepalive") == 0) {
3475 s->tcp_keepalive.enabled = 1;
b2130d58 3476 } else if (strncmp(token, "tcpkeepalive=", 13) == 0) {
26ac0430
AJ
3477 char *t = token + 13;
3478 s->tcp_keepalive.enabled = 1;
3479 s->tcp_keepalive.idle = atoi(t);
3480 t = strchr(t, ',');
3481 if (t) {
3482 t++;
3483 s->tcp_keepalive.interval = atoi(t);
3484 t = strchr(t, ',');
3485 }
3486 if (t) {
3487 t++;
3488 s->tcp_keepalive.timeout = atoi(t);
3489 t = strchr(t, ',');
3490 }
154dc884 3491#if USE_SSL
3a0c8eb5
AR
3492 } else if (strcasecmp(token, "sslBump") == 0) {
3493 debugs(3, DBG_CRITICAL, "WARNING: '" << token << "' is deprecated " <<
9945bf3f 3494 "in http_port. Use 'ssl-bump' instead.");
3a0c8eb5
AR
3495 s->sslBump = 1; // accelerated when bumped, otherwise not
3496 } else if (strcmp(token, "ssl-bump") == 0) {
c7b1dd5d 3497 s->sslBump = 1; // accelerated when bumped, otherwise not
154dc884 3498 } else if (strncmp(token, "cert=", 5) == 0) {
3499 safe_free(s->cert);
3500 s->cert = xstrdup(token + 5);
3501 } else if (strncmp(token, "key=", 4) == 0) {
3502 safe_free(s->key);
3503 s->key = xstrdup(token + 4);
3504 } else if (strncmp(token, "version=", 8) == 0) {
3505 s->version = xatoi(token + 8);
154dc884 3506 if (s->version < 1 || s->version > 4)
3507 self_destruct();
3508 } else if (strncmp(token, "options=", 8) == 0) {
3509 safe_free(s->options);
3510 s->options = xstrdup(token + 8);
3511 } else if (strncmp(token, "cipher=", 7) == 0) {
3512 safe_free(s->cipher);
3513 s->cipher = xstrdup(token + 7);
3514 } else if (strncmp(token, "clientca=", 9) == 0) {
3515 safe_free(s->clientca);
3516 s->clientca = xstrdup(token + 9);
3517 } else if (strncmp(token, "cafile=", 7) == 0) {
3518 safe_free(s->cafile);
3519 s->cafile = xstrdup(token + 7);
3520 } else if (strncmp(token, "capath=", 7) == 0) {
3521 safe_free(s->capath);
3522 s->capath = xstrdup(token + 7);
3523 } else if (strncmp(token, "crlfile=", 8) == 0) {
3524 safe_free(s->crlfile);
3525 s->crlfile = xstrdup(token + 8);
3526 } else if (strncmp(token, "dhparams=", 9) == 0) {
3527 safe_free(s->dhfile);
3528 s->dhfile = xstrdup(token + 9);
3529 } else if (strncmp(token, "sslflags=", 9) == 0) {
3530 safe_free(s->sslflags);
3531 s->sslflags = xstrdup(token + 9);
3532 } else if (strncmp(token, "sslcontext=", 11) == 0) {
3533 safe_free(s->sslcontext);
3534 s->sslcontext = xstrdup(token + 11);
154dc884 3535#endif
3f38a55e 3536 } else {
62e76326 3537 self_destruct();
3f38a55e 3538 }
3539}
3540
3f38a55e 3541static http_port_list *
3542create_http_port(char *portspec)
3543{
154dc884 3544 http_port_list *s = new http_port_list("http");
3f38a55e 3545 parse_http_port_specification(s, portspec);
3546 return s;
3547}
3548
3549void
3550add_http_port(char *portspec)
3551{
3552 http_port_list *s = create_http_port(portspec);
7e07ced1
AJ
3553 // we may need to merge better of the above returns a list with clones
3554 assert(s->next == NULL);
3f38a55e 3555 s->next = Config.Sockaddr.http;
3556 Config.Sockaddr.http = s;
3557}
3558
7e07ced1
AJ
3559http_port_list *
3560clone_http_port_list(http_port_list *a)
3561{
3562 http_port_list *b = new http_port_list(a->protocol);
3563
3564 b->s = a->s;
3565 if (a->name)
3566 b->name = xstrdup(a->name);
3567 if (a->defaultsite)
3568 b->defaultsite = xstrdup(a->defaultsite);
3569
3570 b->intercepted = a->intercepted;
3571 b->spoof_client_ip = a->spoof_client_ip;
3572 b->accel = a->accel;
3573 b->allow_direct = a->allow_direct;
3574 b->vhost = a->vhost;
3575 b->sslBump = a->sslBump;
3576 b->vport = a->vport;
3577 b->connection_auth_disabled = a->connection_auth_disabled;
3578 b->disable_pmtu_discovery = a->disable_pmtu_discovery;
3579
3580 memcpy( &(b->tcp_keepalive), &(a->tcp_keepalive), sizeof(a->tcp_keepalive));
3581
3582#if 0
3583 // AYJ: 2009-07-18: for now SSL does not clone. Configure separate ports with IPs and SSL settings
3584
3585#if USE_SSL
3586 // XXX: temporary hack to ease move of SSL options to http_port
3587 http_port_list &http;
3588
3589 char *cert;
3590 char *key;
3591 int version;
3592 char *cipher;
3593 char *options;
3594 char *clientca;
3595 char *cafile;
3596 char *capath;
3597 char *crlfile;
3598 char *dhfile;
3599 char *sslflags;
3600 char *sslcontext;
3601 SSL_CTX *sslContext;
3602#endif
3603
3604#endif /*0*/
3605
3606 return b;
3607}
7e07ced1 3608
3f38a55e 3609static void
3610parse_http_port_list(http_port_list ** head)
3611{
3612 char *token = strtok(NULL, w_space);
62e76326 3613
9eeb8e4b 3614 if (!token) {
62e76326 3615 self_destruct();
9eeb8e4b 3616 return;
3617 }
62e76326 3618
3f38a55e 3619 http_port_list *s = create_http_port(token);
62e76326 3620
3f38a55e 3621 /* parse options ... */
3622 while ((token = strtok(NULL, w_space))) {
62e76326 3623 parse_http_port_option(s, token);
3f38a55e 3624 }
62e76326 3625
055421ee 3626 if (Ip::EnableIpv6&IPV6_SPECIAL_SPLITSTACK && s->s.IsAnyAddr()) {
7e07ced1
AJ
3627 // clone the port options from *s to *(s->next)
3628 s->next = clone_http_port_list(s);
3629 s->next->s.SetIPv4();
3630 debugs(3, 3, "http(s)_port: clone wildcard address for split-stack: " << s->s << " and " << s->next->s);
3631 }
7e07ced1 3632
3f38a55e 3633 while (*head)
62e76326 3634 head = &(*head)->next;
3635
3f38a55e 3636 *head = s;
3637}
3638
3639static void
3640dump_generic_http_port(StoreEntry * e, const char *n, const http_port_list * s)
3641{
cc192b50 3642 char buf[MAX_IPSTRLEN];
3643
3644 storeAppendPrintf(e, "%s %s",
62e76326 3645 n,
cc192b50 3646 s->s.ToURL(buf,MAX_IPSTRLEN));
62e76326 3647
2ad20b4f
AJ
3648 if (s->intercepted)
3649 storeAppendPrintf(e, " intercept");
62e76326 3650
c7b1dd5d
AJ
3651 if (s->spoof_client_ip)
3652 storeAppendPrintf(e, " tproxy");
3653
3654 if (s->accel)
3655 storeAppendPrintf(e, " accel");
3656
3f38a55e 3657 if (s->vhost)
62e76326 3658 storeAppendPrintf(e, " vhost");
3659
3f38a55e 3660 if (s->vport)
62e76326 3661 storeAppendPrintf(e, " vport");
5529ca8a 3662
c7b1dd5d
AJ
3663 if (s->defaultsite)
3664 storeAppendPrintf(e, " defaultsite=%s", s->defaultsite);
3665
d67acb4e
AJ
3666 if (s->connection_auth_disabled)
3667 storeAppendPrintf(e, " connection-auth=off");
3668 else
3669 storeAppendPrintf(e, " connection-auth=on");
3670
5529ca8a 3671 if (s->disable_pmtu_discovery != DISABLE_PMTU_OFF) {
3672 const char *pmtu;
3673
3674 if (s->disable_pmtu_discovery == DISABLE_PMTU_ALWAYS)
3675 pmtu = "always";
3676 else
3677 pmtu = "transparent";
3678
3679 storeAppendPrintf(e, " disable-pmtu-discovery=%s", pmtu);
3680 }
b2130d58 3681
3682 if (s->tcp_keepalive.enabled) {
26ac0430 3683 if (s->tcp_keepalive.idle || s->tcp_keepalive.interval || s->tcp_keepalive.timeout) {
68924b6d 3684 storeAppendPrintf(e, " tcpkeepalive=%d,%d,%d", s->tcp_keepalive.idle, s->tcp_keepalive.interval, s->tcp_keepalive.timeout);
26ac0430 3685 } else {
68924b6d 3686 storeAppendPrintf(e, " tcpkeepalive");
26ac0430 3687 }
b2130d58 3688 }
154dc884 3689
3690#if USE_SSL
c7b1dd5d
AJ
3691 if (s->sslBump)
3692 storeAppendPrintf(e, " sslBump");
3693
26ac0430
AJ
3694 if (s->cert)
3695 storeAppendPrintf(e, " cert=%s", s->cert);
154dc884 3696
26ac0430
AJ
3697 if (s->key)
3698 storeAppendPrintf(e, " key=%s", s->key);
154dc884 3699
26ac0430
AJ
3700 if (s->version)
3701 storeAppendPrintf(e, " version=%d", s->version);
154dc884 3702
26ac0430
AJ
3703 if (s->options)
3704 storeAppendPrintf(e, " options=%s", s->options);
154dc884 3705
26ac0430
AJ
3706 if (s->cipher)
3707 storeAppendPrintf(e, " cipher=%s", s->cipher);
154dc884 3708
26ac0430
AJ
3709 if (s->cafile)
3710 storeAppendPrintf(e, " cafile=%s", s->cafile);
154dc884 3711
26ac0430
AJ
3712 if (s->capath)
3713 storeAppendPrintf(e, " capath=%s", s->capath);
154dc884 3714
26ac0430
AJ
3715 if (s->crlfile)
3716 storeAppendPrintf(e, " crlfile=%s", s->crlfile);
154dc884 3717
26ac0430
AJ
3718 if (s->dhfile)
3719 storeAppendPrintf(e, " dhparams=%s", s->dhfile);
154dc884 3720
26ac0430
AJ
3721 if (s->sslflags)
3722 storeAppendPrintf(e, " sslflags=%s", s->sslflags);
154dc884 3723
26ac0430
AJ
3724 if (s->sslcontext)
3725 storeAppendPrintf(e, " sslcontext=%s", s->sslcontext);
154dc884 3726#endif
3f38a55e 3727}
62e76326 3728
3f38a55e 3729static void
3730dump_http_port_list(StoreEntry * e, const char *n, const http_port_list * s)
3731{
3732 while (s) {
62e76326 3733 dump_generic_http_port(e, n, s);
3734 storeAppendPrintf(e, "\n");
3735 s = s->next;
3f38a55e 3736 }
3737}
3738
3739static void
3740free_http_port_list(http_port_list ** head)
3741{
3742 http_port_list *s;
62e76326 3743
3f38a55e 3744 while ((s = *head) != NULL) {
62e76326 3745 *head = s->next;
154dc884 3746 delete s;
3f38a55e 3747 }
3748}
3749
3f38a55e 3750#if USE_SSL
3f38a55e 3751
154dc884 3752// TODO: merge better with parse_http_port_list
3f38a55e 3753static void
3754parse_https_port_list(https_port_list ** head)
3755{
3f38a55e 3756 char *token;
3757 https_port_list *s;
154dc884 3758
3f38a55e 3759 token = strtok(NULL, w_space);
62e76326 3760
3f38a55e 3761 if (!token)
62e76326 3762 self_destruct();
3763
154dc884 3764 s = new https_port_list;
3f38a55e 3765 parse_http_port_specification(&s->http, token);
62e76326 3766
d193a436 3767 /* parse options ... */
3768 while ((token = strtok(NULL, w_space))) {
154dc884 3769 parse_http_port_option(s, token);
d193a436 3770 }
62e76326 3771
8abf232c 3772 while (*head) {
3773 http_port_list ** headTmp = &(*head)->http.next;
3774 head = (https_port_list **)headTmp;
3775 }
62e76326 3776
d193a436 3777 *head = s;
3778}
3779
3780static void
3781dump_https_port_list(StoreEntry * e, const char *n, const https_port_list * s)
3782{
154dc884 3783 dump_http_port_list(e, n, s);
d193a436 3784}
3785
3786static void
3787free_https_port_list(https_port_list ** head)
3788{
154dc884 3789 free_http_port_list((http_port_list**)head);
d193a436 3790}
3791
3792#if 0
3793static int
3794check_null_https_port_list(const https_port_list * s)
3795{
3796 return NULL == s;
3797}
62e76326 3798
d193a436 3799#endif
3800
3801#endif /* USE_SSL */
3802
f150dd4b 3803void
3804configFreeMemory(void)
3805{
23ff6968 3806 free_all();
f150dd4b 3807}
f0b19334 3808
94439e4e 3809void
f0b19334 3810requirePathnameExists(const char *name, const char *path)
3811{
62e76326 3812
f0b19334 3813 struct stat sb;
08577e16 3814 char pathbuf[BUFSIZ];
f0b19334 3815 assert(path != NULL);
62e76326 3816
0b796acd 3817 if (Config.chroot_dir && (geteuid() == 0)) {
62e76326 3818 snprintf(pathbuf, BUFSIZ, "%s/%s", Config.chroot_dir, path);
3819 path = pathbuf;
08577e16 3820 }
62e76326 3821
a572d8be 3822 if (stat(path, &sb) < 0) {
3823 if (opt_send_signal == -1 || opt_send_signal == SIGHUP)
3824 fatalf("%s %s: %s", name, path, xstrerror());
3825 else
3826 fprintf(stderr, "WARNING: %s %s: %s\n", name, path, xstrerror());
3827 }
f0b19334 3828}
d860a1aa 3829
3830char *
3831strtokFile(void)
3832{
d295d770 3833 return ConfigParser::strtokFile();
d860a1aa 3834}
7684c4b1 3835
450e0c10 3836#include "AccessLogEntry.h"
3837/* TODO: split out parsing somehow ...*/
7684c4b1 3838static void
3839parse_logformat(logformat ** logformat_definitions)
3840{
3841 logformat *nlf;
3842 char *name, *def;
3843
3844 if ((name = strtok(NULL, w_space)) == NULL)
3845 self_destruct();
3846
9eeb8e4b 3847 if ((def = strtok(NULL, "\r\n")) == NULL) {
7684c4b1 3848 self_destruct();
9eeb8e4b 3849 return;
3850 }
7684c4b1 3851
bf8fe701 3852 debugs(3, 2, "Logformat for '" << name << "' is '" << def << "'");
7684c4b1 3853
3854 nlf = (logformat *)xcalloc(1, sizeof(logformat));
3855
3856 nlf->name = xstrdup(name);
3857
9eeb8e4b 3858 if (!accessLogParseLogFormat(&nlf->format, def)) {
7684c4b1 3859 self_destruct();
9eeb8e4b 3860 return;
3861 }
7684c4b1 3862
3863 nlf->next = *logformat_definitions;
3864
3865 *logformat_definitions = nlf;
3866}
3867
3868static void
3869parse_access_log(customlog ** logs)
3870{
3871 const char *filename, *logdef_name;
3872 customlog *cl;
3873 logformat *lf;
3874
3875 cl = (customlog *)xcalloc(1, sizeof(*cl));
3876
9eeb8e4b 3877 if ((filename = strtok(NULL, w_space)) == NULL) {
7684c4b1 3878 self_destruct();
9eeb8e4b 3879 return;
3880 }
7684c4b1 3881
3882 if (strcmp(filename, "none") == 0) {
3883 cl->type = CLF_NONE;
3884 goto done;
3885 }
3886
3887 if ((logdef_name = strtok(NULL, w_space)) == NULL)
3888 logdef_name = "auto";
3889
bf8fe701 3890 debugs(3, 9, "Log definition name '" << logdef_name << "' file '" << filename << "'");
7684c4b1 3891
3892 cl->filename = xstrdup(filename);
3893
3894 /* look for the definition pointer corresponding to this name */
3895 lf = Config.Log.logformats;
3896
3897 while (lf != NULL) {
bf8fe701 3898 debugs(3, 9, "Comparing against '" << lf->name << "'");
7684c4b1 3899
3900 if (strcmp(lf->name, logdef_name) == 0)
3901 break;
3902
3903 lf = lf->next;
3904 }
3905
3906 if (lf != NULL) {
3907 cl->type = CLF_CUSTOM;
3908 cl->logFormat = lf;
3909 } else if (strcmp(logdef_name, "auto") == 0) {
3910 cl->type = CLF_AUTO;
3911 } else if (strcmp(logdef_name, "squid") == 0) {
3912 cl->type = CLF_SQUID;
3913 } else if (strcmp(logdef_name, "common") == 0) {
3914 cl->type = CLF_COMMON;
3ff65596
AR
3915#if ICAP_CLIENT
3916 } else if (strcmp(logdef_name, "icap_squid") == 0) {
3917 cl->type = CLF_ICAP_SQUID;
3918#endif
7684c4b1 3919 } else {
bf8fe701 3920 debugs(3, 0, "Log format '" << logdef_name << "' is not defined");
7684c4b1 3921 self_destruct();
9eeb8e4b 3922 return;
7684c4b1 3923 }
3924
3925done:
a9f20260 3926 aclParseAclList(LegacyParser, &cl->aclList);
7684c4b1 3927
3928 while (*logs)
3929 logs = &(*logs)->next;
3930
3931 *logs = cl;
3932}
3933
d64bef4c 3934static int
3935check_null_access_log(customlog *customlog_definitions)
3936{
3937 return customlog_definitions == NULL;
3938}
3939
7684c4b1 3940static void
3941dump_logformat(StoreEntry * entry, const char *name, logformat * definitions)
3942{
3943 accessLogDumpLogFormat(entry, name, definitions);
3944}
3945
3946static void
3947dump_access_log(StoreEntry * entry, const char *name, customlog * logs)
3948{
3949 customlog *log;
3950
3951 for (log = logs; log; log = log->next) {
3952 storeAppendPrintf(entry, "%s ", name);
3953
3954 switch (log->type) {
3955
3956 case CLF_CUSTOM:
3957 storeAppendPrintf(entry, "%s %s", log->filename, log->logFormat->name);
3958 break;
3959
3960 case CLF_NONE:
3961 storeAppendPrintf(entry, "none");
3962 break;
3963
3964 case CLF_SQUID:
3965 storeAppendPrintf(entry, "%s squid", log->filename);
3966 break;
3967
3968 case CLF_COMMON:
3969 storeAppendPrintf(entry, "%s squid", log->filename);
3970 break;
3ff65596
AR
3971#if ICAP_CLIENT
3972 case CLF_ICAP_SQUID:
3973 storeAppendPrintf(entry, "%s icap_squid", log->filename);
3974 break;
3975#endif
7684c4b1 3976 case CLF_AUTO:
3977
3978 if (log->aclList)
3979 storeAppendPrintf(entry, "%s auto", log->filename);
3980 else
3981 storeAppendPrintf(entry, "%s", log->filename);
3982
3983 break;
3984
3985 case CLF_UNKNOWN:
3986 break;
3987 }
3988
3989 if (log->aclList)
3990 dump_acl_list(entry, log->aclList);
3991
3992 storeAppendPrintf(entry, "\n");
3993 }
3994}
3995
3996static void
3997free_logformat(logformat ** definitions)
3998{
3999 while (*definitions) {
4000 logformat *format = *definitions;
4001 *definitions = format->next;
61831b4a 4002 safe_free(format->name);
7684c4b1 4003 accessLogFreeLogFormat(&format->format);
4004 xfree(format);
4005 }
4006}
4007
4008static void
4009free_access_log(customlog ** definitions)
4010{
4011 while (*definitions) {
4012 customlog *log = *definitions;
4013 *definitions = log->next;
4014
4015 log->logFormat = NULL;
4016 log->type = CLF_UNKNOWN;
4017
4018 if (log->aclList)
4019 aclDestroyAclList(&log->aclList);
4020
4021 safe_free(log->filename);
4022
4023 xfree(log);
4024 }
4025}
3a69ddf3 4026
96c2bb61
AR
4027/// parses list of integers form name=N1,N2,N3,...
4028static bool
4029parseNamedIntList(const char *data, const String &name, Vector<int> &list)
4030{
4031 if (data && (strncmp(data, name.rawBuf(), name.size()) == 0)) {
4032 data += name.size();
4033 if (*data == '=') {
4034 while (true) {
4035 ++data;
4036 int value = 0;
4037 if (!StringToInt(data, value, &data, 10))
4038 break;
4039 list.push_back(value);
4040 if (*data == '\0' || *data != ',')
4041 break;
4042 }
4043 }
4044 }
cff99cec 4045 return data && *data == '\0';
96c2bb61
AR
4046}
4047
4048static void
212af65c
A
4049parse_CpuAffinityMap(CpuAffinityMap **const cpuAffinityMap)
4050{
96c2bb61
AR
4051#if !HAVE_CPU_AFFINITY
4052 debugs(3, DBG_CRITICAL, "FATAL: Squid built with no CPU affinity " <<
4053 "support, do not set 'cpu_affinity_map'");
4054 self_destruct();
4055#endif /* HAVE_CPU_AFFINITY */
4056
4057 if (!*cpuAffinityMap)
4058 *cpuAffinityMap = new CpuAffinityMap;
4059
4060 const char *const pToken = strtok(NULL, w_space);
4061 const char *const cToken = strtok(NULL, w_space);
4062 Vector<int> processes, cores;
4063 if (!parseNamedIntList(pToken, "process_numbers", processes)) {
4064 debugs(3, DBG_CRITICAL, "FATAL: bad 'process_numbers' parameter " <<
4065 "in 'cpu_affinity_map'");
4066 self_destruct();
4067 } else if (!parseNamedIntList(cToken, "cores", cores)) {
4068 debugs(3, DBG_CRITICAL, "FATAL: bad 'cores' parameter in " <<
4069 "'cpu_affinity_map'");
4070 self_destruct();
4071 } else if (!(*cpuAffinityMap)->add(processes, cores)) {
4072 debugs(3, DBG_CRITICAL, "FATAL: bad 'cpu_affinity_map'; " <<
4073 "process_numbers and cores lists differ in length or " <<
4074 "contain numbers <= 0");
4075 self_destruct();
4076 }
4077}
4078
4079static void
212af65c
A
4080dump_CpuAffinityMap(StoreEntry *const entry, const char *const name, const CpuAffinityMap *const cpuAffinityMap)
4081{
96c2bb61
AR
4082 if (cpuAffinityMap) {
4083 storeAppendPrintf(entry, "%s process_numbers=", name);
4084 for (size_t i = 0; i < cpuAffinityMap->processes().size(); ++i) {
4085 storeAppendPrintf(entry, "%s%i", (i ? "," : ""),
4086 cpuAffinityMap->processes()[i]);
4087 }
4088 storeAppendPrintf(entry, " cores=");
4089 for (size_t i = 0; i < cpuAffinityMap->processes().size(); ++i) {
4090 storeAppendPrintf(entry, "%s%i", (i ? "," : ""),
4091 cpuAffinityMap->cores()[i]);
4092 }
4093 storeAppendPrintf(entry, "\n");
4094 }
4095}
4096
4097static void
212af65c
A
4098free_CpuAffinityMap(CpuAffinityMap **const cpuAffinityMap)
4099{
96c2bb61
AR
4100 delete *cpuAffinityMap;
4101 *cpuAffinityMap = NULL;
4102}
4103
62c7f90e
AR
4104#if USE_ADAPTATION
4105
4106static void
4107parse_adaptation_service_set_type()
4108{
4109 Adaptation::Config::ParseServiceSet();
4110}
4111
a22e6cd3
AR
4112static void
4113parse_adaptation_service_chain_type()
4114{
4115 Adaptation::Config::ParseServiceChain();
4116}
4117
62c7f90e
AR
4118static void
4119parse_adaptation_access_type()
4120{
4121 Adaptation::Config::ParseAccess(LegacyParser);
4122}
4123
62c7f90e
AR
4124#endif /* USE_ADAPTATION */
4125
4126
3a69ddf3 4127#if ICAP_CLIENT
4128
4129static void
26cc52cb 4130parse_icap_service_type(Adaptation::Icap::Config * cfg)
3a69ddf3 4131{
462e63d3 4132 cfg->parseService();
3a69ddf3 4133}
4134
4135static void
26cc52cb 4136free_icap_service_type(Adaptation::Icap::Config * cfg)
3a69ddf3 4137{
462e63d3 4138 cfg->freeService();
3a69ddf3 4139}
4140
4141static void
26cc52cb 4142dump_icap_service_type(StoreEntry * entry, const char *name, const Adaptation::Icap::Config &cfg)
3a69ddf3 4143{
462e63d3 4144 cfg.dumpService(entry, name);
3a69ddf3 4145}
4146
4147static void
c939dc70 4148parse_icap_class_type()
3a69ddf3 4149{
62c7f90e 4150 debugs(93, 0, "WARNING: 'icap_class' is depricated. " <<
26ac0430 4151 "Use 'adaptation_service_set' instead");
62c7f90e 4152 Adaptation::Config::ParseServiceSet();
3a69ddf3 4153}
4154
3a69ddf3 4155static void
c939dc70 4156parse_icap_access_type()
3a69ddf3 4157{
62c7f90e 4158 debugs(93, 0, "WARNING: 'icap_access' is depricated. " <<
26ac0430 4159 "Use 'adaptation_access' instead");
21a26d31 4160 Adaptation::Config::ParseAccess(LegacyParser);
3a69ddf3 4161}
4162
3a69ddf3 4163#endif
21a26d31
AR
4164
4165
4166#if USE_ECAP
4167
4168static void
574b508c 4169parse_ecap_service_type(Adaptation::Ecap::Config * cfg)
21a26d31
AR
4170{
4171 cfg->parseService();
4172}
4173
4174static void
574b508c 4175free_ecap_service_type(Adaptation::Ecap::Config * cfg)
21a26d31
AR
4176{
4177 cfg->freeService();
4178}
4179
4180static void
574b508c 4181dump_ecap_service_type(StoreEntry * entry, const char *name, const Adaptation::Ecap::Config &cfg)
21a26d31
AR
4182{
4183 cfg.dumpService(entry, name);
4184}
4185
4186#endif /* USE_ECAP */
8277060a
CT
4187
4188#if ICAP_CLIENT
4189static void parse_icap_service_failure_limit(Adaptation::Icap::Config *cfg)
4190{
4191 char *token;
4192 time_t d;
4193 time_t m;
4194 cfg->service_failure_limit = GetInteger();
4195
4196 if ((token = strtok(NULL, w_space)) == NULL)
4197 return;
4198
4199 if (strcmp(token,"in") != 0) {
4200 debugs(3, 0, "expecting 'in' on'" << config_input_line << "'");
4201 self_destruct();
4202 }
4203
4204 if ((token = strtok(NULL, w_space)) == NULL) {
4205 self_destruct();
4206 }
4207
4208 d = static_cast<time_t> (xatoi(token));
a459e80a 4209
8277060a
CT
4210 m = static_cast<time_t> (1);
4211
4212 if (0 == d)
4213 (void) 0;
4214 else if ((token = strtok(NULL, w_space)) == NULL) {
4215 debugs(3, 0, "No time-units on '" << config_input_line << "'");
4216 self_destruct();
a459e80a 4217 } else if ((m = parseTimeUnits(token)) == 0)
8277060a
CT
4218 self_destruct();
4219
4220 cfg->oldest_service_failure = (m * d);
4221}
4222
4223static void dump_icap_service_failure_limit(StoreEntry *entry, const char *name, const Adaptation::Icap::Config &cfg)
4224{
4225 storeAppendPrintf(entry, "%s %d", name, cfg.service_failure_limit);
4226 if (cfg.oldest_service_failure > 0) {
4227 storeAppendPrintf(entry, " in %d seconds", (int)cfg.oldest_service_failure);
4228 }
4229 storeAppendPrintf(entry, "\n");
4230}
4231
4232static void free_icap_service_failure_limit(Adaptation::Icap::Config *cfg)
4233{
4234 cfg->oldest_service_failure = 0;
4235 cfg->service_failure_limit = 0;
4236}
4237
4238#endif