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