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