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