]> git.ipfire.org Git - thirdparty/squid.git/blame - src/cache_cf.cc
Bugzilla #162
[thirdparty/squid.git] / src / cache_cf.cc
CommitLineData
be335c22 1
30a4f2a8 2/*
25a77f03 3 * $Id: cache_cf.cc,v 1.381 2001/05/08 15:24:35 hno Exp $
30a4f2a8 4 *
5 * DEBUG: section 3 Configuration File Parsing
6 * AUTHOR: Harvest Derived
7 *
2b6662ba 8 * SQUID Web Proxy Cache http://www.squid-cache.org/
e25c139f 9 * ----------------------------------------------------------
30a4f2a8 10 *
2b6662ba 11 * Squid is the result of efforts by numerous individuals from
12 * the Internet community; see the CONTRIBUTORS file for full
13 * details. Many organizations have provided support for Squid's
14 * development; see the SPONSORS file for full details. Squid is
15 * Copyrighted (C) 2001 by the Regents of the University of
16 * California; see the COPYRIGHT file for full details. Squid
17 * incorporates software developed and/or copyrighted by other
18 * sources; see the CREDITS file for full details.
30a4f2a8 19 *
20 * This program is free software; you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License as published by
22 * the Free Software Foundation; either version 2 of the License, or
23 * (at your option) any later version.
24 *
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
29 *
30 * You should have received a copy of the GNU General Public License
31 * along with this program; if not, write to the Free Software
cbdec147 32 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
e25c139f 33 *
30a4f2a8 34 */
cf5fd929 35
44a47c6e 36#include "squid.h"
1df370e3 37
38#if SQUID_SNMP
a97cfa48 39#include "snmp.h"
1df370e3 40#endif
090089c4 41
8813e606 42static const char *const T_SECOND_STR = "second";
43static const char *const T_MINUTE_STR = "minute";
44static const char *const T_HOUR_STR = "hour";
45static const char *const T_DAY_STR = "day";
46static const char *const T_WEEK_STR = "week";
47static const char *const T_FORTNIGHT_STR = "fortnight";
48static const char *const T_MONTH_STR = "month";
49static const char *const T_YEAR_STR = "year";
50static const char *const T_DECADE_STR = "decade";
aa0a0c7c 51
9906e724 52static const char *const B_BYTES_STR = "bytes";
53static const char *const B_KBYTES_STR = "KB";
54static const char *const B_MBYTES_STR = "MB";
55static const char *const B_GBYTES_STR = "GB";
56
4db43fab 57static const char *const list_sep = ", \t\n\r";
25a77f03 58static struct cache_dir_option common_cachedir_options[];
97474590 59
cd748f27 60static void update_maxobjsize(void);
f5b8bbc4 61static void configDoConfigure(void);
62static void parse_refreshpattern(refresh_t **);
63static int parseTimeUnits(const char *unit);
64static void parseTimeLine(time_t * tptr, const char *units);
59715b38 65static void parse_ushort(u_short * var);
f5b8bbc4 66static void parse_string(char **);
94439e4e 67void parse_wordlist(wordlist **);
f5b8bbc4 68static void default_all(void);
69static void defaults_if_none(void);
70static int parse_line(char *);
71static void parseBytesLine(size_t * bptr, const char *units);
72static size_t parseBytesUnits(const char *unit);
f5b8bbc4 73static void free_all(void);
94439e4e 74void requirePathnameExists(const char *name, const char *path);
ed7f5615 75static OBJH dump_config;
6bccf575 76static void dump_http_header_access(StoreEntry * entry, const char *name, header_mangler header[]);
77static void parse_http_header_access(header_mangler header[]);
78static void free_http_header_access(header_mangler header[]);
79static void dump_http_header_replace(StoreEntry * entry, const char *name, header_mangler header[]);
80static void parse_http_header_replace(header_mangler * header);
81static void free_http_header_replace(header_mangler * header);
82static void parse_denyinfo(acl_deny_info_list ** var);
83static void dump_denyinfo(StoreEntry * entry, const char *name, acl_deny_info_list * var);
84static void free_denyinfo(acl_deny_info_list ** var);
7e3ce7b9 85static void parse_sockaddr_in_list(sockaddr_in_list **);
86static void dump_sockaddr_in_list(StoreEntry *, const char *, const sockaddr_in_list *);
87static void free_sockaddr_in_list(sockaddr_in_list **);
88static int check_null_sockaddr_in_list(const sockaddr_in_list *);
d193a436 89#if USE_SSL
90static void parse_https_port_list(https_port_list **);
91static void dump_https_port_list(StoreEntry *, const char *, const https_port_list *);
92static void free_https_port_list(https_port_list **);
93#if 0
94static int check_null_https_port_list(const https_port_list *);
95#endif
96#endif /* USE_SSL */
270b86af 97
0e4e0e7d 98void
0673c0ba 99self_destruct(void)
090089c4 100{
137ee196 101 fatalf("Bungled %s line %d: %s",
b8de7ebe 102 cfg_filename, config_lineno, config_input_line);
090089c4 103}
104
8203a132 105void
106wordlistDestroy(wordlist ** list)
0ffd22bc 107{
108 wordlist *w = NULL;
79d39a72 109 while ((w = *list) != NULL) {
b5639035 110 *list = w->next;
0ffd22bc 111 safe_free(w->key);
db1cd23c 112 memFree(w, MEM_WORDLIST);
0ffd22bc 113 }
114 *list = NULL;
115}
116
858783c9 117const char *
fe4e214f 118wordlistAdd(wordlist ** list, const char *key)
090089c4 119{
c68e9c6b 120 while (*list)
121 list = &(*list)->next;
122 *list = memAllocate(MEM_WORDLIST);
123 (*list)->key = xstrdup(key);
124 (*list)->next = NULL;
858783c9 125 return (*list)->key;
126}
127
128void
a4b8110e 129wordlistJoin(wordlist ** list, wordlist ** wl)
858783c9 130{
131 while (*list)
132 list = &(*list)->next;
133 *list = *wl;
134 *wl = NULL;
135}
136
137void
a4b8110e 138wordlistAddWl(wordlist ** list, wordlist * wl)
858783c9 139{
140 while (*list)
141 list = &(*list)->next;
a4b8110e 142 for (; wl; wl = wl->next, list = &(*list)->next) {
858783c9 143 *list = memAllocate(MEM_WORDLIST);
144 (*list)->key = xstrdup(wl->key);
a4b8110e 145 (*list)->next = NULL;
858783c9 146 }
090089c4 147}
148
137ee196 149void
eeb423fb 150wordlistCat(const wordlist * w, MemBuf * mb)
5da06f20 151{
5da06f20 152 while (NULL != w) {
137ee196 153 memBufPrintf(mb, "%s\n", w->key);
5da06f20 154 w = w->next;
155 }
5da06f20 156}
157
6b8e7481 158wordlist *
159wordlistDup(const wordlist * w)
160{
161 wordlist *D = NULL;
162 while (NULL != w) {
163 wordlistAdd(&D, w->key);
164 w = w->next;
165 }
166 return D;
167}
168
8203a132 169void
170intlistDestroy(intlist ** list)
92a6f4b1 171{
172 intlist *w = NULL;
173 intlist *n = NULL;
92a6f4b1 174 for (w = *list; w; w = n) {
175 n = w->next;
db1cd23c 176 memFree(w, MEM_INTLIST);
92a6f4b1 177 }
178 *list = NULL;
179}
180
5db53b8f 181int
182intlistFind(intlist * list, int i)
183{
184 intlist *w = NULL;
185 for (w = list; w; w = w->next)
186 if (w->i == i)
187 return 1;
188 return 0;
189}
190
403279e0 191
3c5557f9 192/*
193 * Use this #define in all the parse*() functions. Assumes char *token is
194 * defined
195 */
090089c4 196
0e4e0e7d 197int
198GetInteger(void)
199{
200 char *token = strtok(NULL, w_space);
201 int i;
202 if (token == NULL)
203 self_destruct();
204 if (sscanf(token, "%d", &i) != 1)
205 self_destruct();
206 return i;
207}
090089c4 208
cd748f27 209static void
210update_maxobjsize(void)
211{
212 int i;
e8dbac8b 213 ssize_t ms = -1;
cd748f27 214
215 for (i = 0; i < Config.cacheSwap.n_configured; i++) {
a4b8110e 216 if (Config.cacheSwap.swapDirs[i].max_objsize > ms)
217 ms = Config.cacheSwap.swapDirs[i].max_objsize;
cd748f27 218 }
219 store_maxobjsize = ms;
220}
221
270b86af 222int
223parseConfigFile(const char *file_name)
2546fcb3 224{
270b86af 225 FILE *fp = NULL;
226 char *token = NULL;
72121e8b 227 char *tmp_line;
e13ee7ad 228 int err_count = 0;
0153d498 229 free_all();
230 default_all();
137ee196 231 if ((fp = fopen(file_name, "r")) == NULL)
232 fatalf("Unable to open configuration file: %s: %s",
270b86af 233 file_name, xstrerror());
c4aefe96 234#if defined(_SQUID_CYGWIN_)
235 setmode(fileno(fp), O_TEXT);
236#endif
270b86af 237 cfg_filename = file_name;
238 if ((token = strrchr(cfg_filename, '/')))
239 cfg_filename = token + 1;
240 memset(config_input_line, '\0', BUFSIZ);
241 config_lineno = 0;
242 while (fgets(config_input_line, BUFSIZ, fp)) {
243 config_lineno++;
244 if ((token = strchr(config_input_line, '\n')))
245 *token = '\0';
246 if (config_input_line[0] == '#')
247 continue;
248 if (config_input_line[0] == '\0')
249 continue;
250 debug(3, 5) ("Processing: '%s'\n", config_input_line);
72121e8b 251 tmp_line = xstrdup(config_input_line);
270b86af 252 if (!parse_line(tmp_line)) {
253 debug(3, 0) ("parseConfigFile: line %d unrecognized: '%s'\n",
254 config_lineno,
255 config_input_line);
e13ee7ad 256 err_count++;
270b86af 257 }
72121e8b 258 safe_free(tmp_line);
270b86af 259 }
f0b19334 260 fclose(fp);
261 defaults_if_none();
262 configDoConfigure();
22f3fd98 263 cachemgrRegister("config",
264 "Current Squid Configuration",
265 dump_config,
1da3b90b 266 1, 1);
e13ee7ad 267 return err_count;
f0b19334 268}
270b86af 269
f0b19334 270static void
271configDoConfigure(void)
272{
273 LOCAL_ARRAY(char, buf, BUFSIZ);
274 memset(&Config2, '\0', sizeof(SquidConfig2));
7021844c 275 /* init memory as early as possible */
276 memConfigure();
270b86af 277 /* Sanity checks */
a95856a0 278 if (Config.cacheSwap.swapDirs == NULL)
279 fatal("No cache_dir's specified in config file");
9fa8263f 280 /* calculate Config.Swap.maxSize */
f4e3fa54 281 storeDirConfigure();
82e2afd9 282 if (0 == Config.Swap.maxSize)
283 /* people might want a zero-sized cache on purpose */
284 (void) 0;
285 else if (Config.Swap.maxSize < (Config.memMaxSize >> 10))
f0b19334 286 fatal("cache_swap is lower than cache_mem");
84f42bac 287 if (Config.Announce.period > 0) {
288 Config.onoff.announce = 1;
289 } else if (Config.Announce.period < 1) {
f1dc9b30 290 Config.Announce.period = 86400 * 365; /* one year */
17a0a4ee 291 Config.onoff.announce = 0;
270b86af 292 }
6b53c392 293#if USE_DNSSERVERS
f0b19334 294 if (Config.dnsChildren < 1)
295 fatal("No dnsservers allocated");
efd900cb 296#endif
270b86af 297 if (Config.Program.redirect) {
298 if (Config.redirectChildren < 1) {
299 Config.redirectChildren = 0;
c6d5b87b 300 wordlistDestroy(&Config.Program.redirect);
270b86af 301 }
fea2e6e0 302 }
f1dc9b30 303 if (Config.Accel.host) {
a47b9029 304 snprintf(buf, BUFSIZ, "http://%s:%d", Config.Accel.host, Config.Accel.port);
305 Config2.Accel.prefix = xstrdup(buf);
306 Config2.Accel.on = 1;
f1dc9b30 307 }
308 if (Config.appendDomain)
309 if (*Config.appendDomain != '.')
310 fatal("append_domain must begin with a '.'");
270b86af 311 if (Config.errHtmlText == NULL)
312 Config.errHtmlText = xstrdup(null_string);
313 storeConfigure();
42b51993 314 if (Config2.Accel.on && !strcmp(Config.Accel.host, "virtual")) {
270b86af 315 vhost_mode = 1;
42b51993 316 if (Config.Accel.port == 0)
317 vport_mode = 1;
318 }
7e3ce7b9 319 if (Config.Sockaddr.http == NULL)
270b86af 320 fatal("No http_port specified!");
137ee196 321 snprintf(ThisCache, sizeof(ThisCache), "%s:%d (%s)",
98829f69 322 uniqueHostname(),
7e3ce7b9 323 (int) ntohs(Config.Sockaddr.http->s.sin_port),
137ee196 324 full_appname_string);
38a6c74e 325 /*
326 * the extra space is for loop detection in client_side.c -- we search
327 * for substrings in the Via header.
328 */
329 snprintf(ThisCache2, sizeof(ThisCache), " %s:%d (%s)",
330 uniqueHostname(),
7e3ce7b9 331 (int) ntohs(Config.Sockaddr.http->s.sin_port),
38a6c74e 332 full_appname_string);
270b86af 333 if (!Config.udpMaxHitObjsz || Config.udpMaxHitObjsz > SQUID_UDP_SO_SNDBUF)
334 Config.udpMaxHitObjsz = SQUID_UDP_SO_SNDBUF;
335 if (Config.appendDomain)
336 Config.appendDomainLen = strlen(Config.appendDomain);
337 else
338 Config.appendDomainLen = 0;
f1dc9b30 339 safe_free(debug_options)
340 debug_options = xstrdup(Config.debugOptions);
22c653cd 341 if (Config.retry.timeout < 5)
342 fatal("minimum_retry_timeout must be at least 5 seconds");
343 if (Config.retry.maxtries > 10)
344 fatal("maximum_single_addr_tries cannot be larger than 10");
345 if (Config.retry.maxtries < 1) {
22c653cd 346 debug(3, 0) ("WARNING: resetting 'maximum_single_addr_tries to 1\n");
5210854d 347 Config.retry.maxtries = 1;
348 }
f0b19334 349 requirePathnameExists("MIME Config Table", Config.mimeTablePathname);
6b53c392 350#if USE_DNSSERVERS
f0b19334 351 requirePathnameExists("cache_dns_program", Config.Program.dnsserver);
efd900cb 352#endif
a3d0a19d 353#if USE_UNLINKD
f0b19334 354 requirePathnameExists("unlinkd_program", Config.Program.unlinkd);
a3d0a19d 355#endif
f0b19334 356 if (Config.Program.redirect)
c6d5b87b 357 requirePathnameExists("redirect_program", Config.Program.redirect->key);
f0b19334 358 requirePathnameExists("Icon Directory", Config.icons.directory);
359 requirePathnameExists("Error Directory", Config.errorDirectory);
9f60cfdf 360#if HTTP_VIOLATIONS
361 {
49c0f46d 362 const refresh_t *R;
363 for (R = Config.Refresh; R; R = R->next) {
364 if (!R->flags.override_expire)
365 continue;
366 debug(22, 1) ("WARNING: use of 'override-expire' in 'refresh_pattern' violates HTTP\n");
367 break;
368 }
369 for (R = Config.Refresh; R; R = R->next) {
370 if (!R->flags.override_lastmod)
371 continue;
372 debug(22, 1) ("WARNING: use of 'override-lastmod' in 'refresh_pattern' violates HTTP\n");
373 break;
374 }
9f60cfdf 375 }
376#endif
db1cd23c 377 if (Config.Wais.relayHost) {
378 if (Config.Wais.peer)
379 cbdataFree(Config.Wais.peer);
72711e31 380 Config.Wais.peer = cbdataAlloc(peer);
b6a2f15e 381 Config.Wais.peer->host = xstrdup(Config.Wais.relayHost);
db1cd23c 382 Config.Wais.peer->http_port = Config.Wais.relayPort;
383 }
53cb32a9 384 if (aclPurgeMethodInUse(Config.accessList.http))
385 Config2.onoff.enable_purge = 1;
8749fa47 386 if (geteuid() == 0) {
387 if (NULL != Config.effectiveUser) {
2d72d4fd 388 struct passwd *pwd = getpwnam(Config.effectiveUser);
389 if (NULL == pwd)
390 /*
391 * Andres Kroonmaa <andre@online.ee>:
392 * Some getpwnam() implementations (Solaris?) require
393 * an available FD < 256 for opening a FILE* to the
394 * passwd file.
395 * DW:
396 * This should be safe at startup, but might still fail
397 * during reconfigure.
398 */
399 fatalf("getpwnam failed to find userid for effective user '%s'",
400 Config.effectiveUser,
401 xstrerror());
402 Config2.effectiveUserID = pwd->pw_uid;
963a4014 403 Config2.effectiveGroupID = pwd->pw_gid;
2d72d4fd 404 }
8749fa47 405 } else {
406 Config2.effectiveUserID = geteuid();
407 Config2.effectiveGroupID = getegid();
d20b1cd0 408 }
409 if (NULL != Config.effectiveGroup) {
410 struct group *grp = getgrnam(Config.effectiveGroup);
411 if (NULL == grp)
412 fatalf("getgrnam failed to find groupid for effective group '%s'",
413 Config.effectiveGroup,
414 xstrerror());
415 Config2.effectiveGroupID = grp->gr_gid;
416 }
56fe752e 417 urlExtMethodConfigure();
090089c4 418}
419
270b86af 420/* Parse a time specification from the config file. Store the
f1dc9b30 421 * result in 'tptr', after converting it to 'units' */
8203a132 422static void
a47b9029 423parseTimeLine(time_t * tptr, const char *units)
090089c4 424{
425 char *token;
270b86af 426 double d;
f1dc9b30 427 time_t m;
428 time_t u;
270b86af 429 if ((u = parseTimeUnits(units)) == 0)
3003c0f3 430 self_destruct();
270b86af 431 if ((token = strtok(NULL, w_space)) == NULL)
3003c0f3 432 self_destruct();
270b86af 433 d = atof(token);
434 m = u; /* default to 'units' if none specified */
10738561 435 if (0 == d)
436 (void) 0;
437 else if ((token = strtok(NULL, w_space)) == NULL)
a47b9029 438 debug(3, 0) ("WARNING: No units on '%s', assuming %f %s\n",
439 config_input_line, d, units);
9e975e4e 440 else if ((m = parseTimeUnits(token)) == 0)
a47b9029 441 self_destruct();
f1dc9b30 442 *tptr = m * d / u;
090089c4 443}
444
270b86af 445static int
446parseTimeUnits(const char *unit)
447{
448 if (!strncasecmp(unit, T_SECOND_STR, strlen(T_SECOND_STR)))
449 return 1;
450 if (!strncasecmp(unit, T_MINUTE_STR, strlen(T_MINUTE_STR)))
451 return 60;
452 if (!strncasecmp(unit, T_HOUR_STR, strlen(T_HOUR_STR)))
453 return 3600;
454 if (!strncasecmp(unit, T_DAY_STR, strlen(T_DAY_STR)))
455 return 86400;
456 if (!strncasecmp(unit, T_WEEK_STR, strlen(T_WEEK_STR)))
457 return 86400 * 7;
458 if (!strncasecmp(unit, T_FORTNIGHT_STR, strlen(T_FORTNIGHT_STR)))
459 return 86400 * 14;
460 if (!strncasecmp(unit, T_MONTH_STR, strlen(T_MONTH_STR)))
461 return 86400 * 30;
462 if (!strncasecmp(unit, T_YEAR_STR, strlen(T_YEAR_STR)))
463 return 86400 * 365.2522;
464 if (!strncasecmp(unit, T_DECADE_STR, strlen(T_DECADE_STR)))
465 return 86400 * 365.2522 * 10;
466 debug(3, 1) ("parseTimeUnits: unknown time unit '%s'\n", unit);
467 return 0;
468}
469
9906e724 470static void
9e975e4e 471parseBytesLine(size_t * bptr, const char *units)
9906e724 472{
473 char *token;
474 double d;
475 size_t m;
476 size_t u;
477 if ((u = parseBytesUnits(units)) == 0)
478 self_destruct();
479 if ((token = strtok(NULL, w_space)) == NULL)
480 self_destruct();
481 d = atof(token);
482 m = u; /* default to 'units' if none specified */
343f47a3 483 if (0.0 == d)
10738561 484 (void) 0;
4860dc1b 485 else if ((token = strtok(NULL, w_space)) == NULL)
9e975e4e 486 debug(3, 0) ("WARNING: No units on '%s', assuming %f %s\n",
487 config_input_line, d, units);
488 else if ((m = parseBytesUnits(token)) == 0)
489 self_destruct();
9906e724 490 *bptr = m * d / u;
491}
492
493static size_t
494parseBytesUnits(const char *unit)
495{
496 if (!strncasecmp(unit, B_BYTES_STR, strlen(B_BYTES_STR)))
497 return 1;
498 if (!strncasecmp(unit, B_KBYTES_STR, strlen(B_KBYTES_STR)))
a47b9029 499 return 1 << 10;
9906e724 500 if (!strncasecmp(unit, B_MBYTES_STR, strlen(B_MBYTES_STR)))
a47b9029 501 return 1 << 20;
9906e724 502 if (!strncasecmp(unit, B_GBYTES_STR, strlen(B_GBYTES_STR)))
a47b9029 503 return 1 << 30;
9906e724 504 debug(3, 1) ("parseBytesUnits: unknown bytes unit '%s'\n", unit);
505 return 0;
506}
507
270b86af 508/*****************************************************************************
509 * Max
510 *****************************************************************************/
511
8203a132 512static void
9ef28b60 513dump_acl(StoreEntry * entry, const char *name, acl * ae)
090089c4 514{
56b63fa1 515 wordlist *w;
516 wordlist *v;
9ef28b60 517 while (ae != NULL) {
c68e9c6b 518 debug(3, 3) ("dump_acl: %s %s\n", name, ae->name);
9ef28b60 519 v = w = aclDumpGeneric(ae);
56b63fa1 520 while (v != NULL) {
c68e9c6b 521 debug(3, 3) ("dump_acl: %s %s %s\n", name, ae->name, v->key);
16300b58 522 storeAppendPrintf(entry, "%s %s %s %s\n",
523 name,
9ef28b60 524 ae->name,
525 aclTypeToStr(ae->type),
16300b58 526 v->key);
56b63fa1 527 v = v->next;
528 }
529 wordlistDestroy(&w);
9ef28b60 530 ae = ae->next;
56b63fa1 531 }
090089c4 532}
533
8203a132 534static void
9ef28b60 535parse_acl(acl ** ae)
090089c4 536{
9ef28b60 537 aclParseAclLine(ae);
f1dc9b30 538}
539
540static void
9ef28b60 541free_acl(acl ** ae)
f1dc9b30 542{
9ef28b60 543 aclDestroyAcls(ae);
090089c4 544}
545
8203a132 546static void
16300b58 547dump_acl_access(StoreEntry * entry, const char *name, acl_access * head)
30a4f2a8 548{
56b63fa1 549 acl_list *l;
550 while (head != NULL) {
505e35db 551 storeAppendPrintf(entry, "%s %s",
0cdcddb9 552 name,
505e35db 553 head->allow ? "Allow" : "Deny");
554 for (l = head->acl_list; l != NULL; l = l->next) {
555 storeAppendPrintf(entry, " %s%s",
556 l->op ? null_string : "!",
56b63fa1 557 l->acl->name);
16300b58 558 }
505e35db 559 storeAppendPrintf(entry, "\n");
56b63fa1 560 head = head->next;
561 }
30a4f2a8 562}
090089c4 563
8203a132 564static void
16300b58 565parse_acl_access(acl_access ** head)
090089c4 566{
270b86af 567 aclParseAccessLine(head);
090089c4 568}
569
0153d498 570static void
16300b58 571free_acl_access(acl_access ** head)
0153d498 572{
a47b9029 573 aclDestroyAccessList(head);
0153d498 574}
575
8203a132 576static void
a7d59104 577dump_address(StoreEntry * entry, const char *name, struct in_addr addr)
270b86af 578{
f53b06f9 579 storeAppendPrintf(entry, "%s %s\n", name, inet_ntoa(addr));
270b86af 580}
581
582static void
583parse_address(struct in_addr *addr)
090089c4 584{
0ee4272b 585 const struct hostent *hp;
270b86af 586 char *token = strtok(NULL, w_space);
587
30a4f2a8 588 if (token == NULL)
589 self_destruct();
429fdbec 590 if (safe_inet_addr(token, addr) == 1)
591 (void) 0;
ceb8994e 592 else if ((hp = gethostbyname(token))) /* dont use ipcache */
1d73e33a 593 *addr = inaddrFromHostent(hp);
30a4f2a8 594 else
3003c0f3 595 self_destruct();
090089c4 596}
597
0153d498 598static void
599free_address(struct in_addr *addr)
600{
a47b9029 601 memset(addr, '\0', sizeof(struct in_addr));
0153d498 602}
603
59715b38 604#if DELAY_POOLS
605
606/* do nothing - free_delay_pool_count is the magic free function.
607 * this is why delay_pool_count isn't just marked TYPE: ushort
608 */
609#define free_delay_pool_class(X)
610#define free_delay_pool_access(X)
611#define free_delay_pool_rates(X)
612#define dump_delay_pool_class(X, Y, Z)
613#define dump_delay_pool_access(X, Y, Z)
614#define dump_delay_pool_rates(X, Y, Z)
615
616static void
617free_delay_pool_count(delayConfig * cfg)
618{
619 int i;
620
621 if (!cfg->pools)
622 return;
623 for (i = 0; i < cfg->pools; i++) {
624 if (cfg->class[i]) {
625 delayFreeDelayPool(i);
626 safe_free(cfg->rates[i]);
627 }
628 aclDestroyAccessList(&cfg->access[i]);
629 }
630 delayFreeDelayData();
631 xfree(cfg->class);
632 xfree(cfg->rates);
633 xfree(cfg->access);
634 memset(cfg, 0, sizeof(*cfg));
635}
636
637static void
638dump_delay_pool_count(StoreEntry * entry, const char *name, delayConfig cfg)
639{
640 int i;
641 LOCAL_ARRAY(char, nom, 32);
642
643 if (!cfg.pools) {
644 storeAppendPrintf(entry, "%s 0\n", name);
645 return;
646 }
647 storeAppendPrintf(entry, "%s %d\n", name, cfg.pools);
648 for (i = 0; i < cfg.pools; i++) {
649 storeAppendPrintf(entry, "delay_class %d %d\n", i + 1, cfg.class[i]);
650 snprintf(nom, 32, "delay_access %d", i + 1);
651 dump_acl_access(entry, nom, cfg.access[i]);
652 if (cfg.class[i] >= 1)
653 storeAppendPrintf(entry, "delay_parameters %d %d/%d", i + 1,
654 cfg.rates[i]->aggregate.restore_bps,
655 cfg.rates[i]->aggregate.max_bytes);
656 if (cfg.class[i] >= 3)
657 storeAppendPrintf(entry, " %d/%d",
658 cfg.rates[i]->network.restore_bps,
659 cfg.rates[i]->network.max_bytes);
660 if (cfg.class[i] >= 2)
661 storeAppendPrintf(entry, " %d/%d",
662 cfg.rates[i]->individual.restore_bps,
663 cfg.rates[i]->individual.max_bytes);
664 if (cfg.class[i] >= 1)
665 storeAppendPrintf(entry, "\n");
666 }
667}
668
669static void
670parse_delay_pool_count(delayConfig * cfg)
671{
672 if (cfg->pools) {
673 debug(3, 0) ("parse_delay_pool_count: multiple delay_pools lines, aborting all previous delay_pools config\n");
674 free_delay_pool_count(cfg);
675 }
676 parse_ushort(&cfg->pools);
677 delayInitDelayData(cfg->pools);
678 cfg->class = xcalloc(cfg->pools, sizeof(u_char));
679 cfg->rates = xcalloc(cfg->pools, sizeof(delaySpecSet *));
680 cfg->access = xcalloc(cfg->pools, sizeof(acl_access *));
681}
682
683static void
684parse_delay_pool_class(delayConfig * cfg)
685{
686 ushort pool, class;
687
688 parse_ushort(&pool);
689 if (pool < 1 || pool > cfg->pools) {
690 debug(3, 0) ("parse_delay_pool_class: Ignoring pool %d not in 1 .. %d\n", pool, cfg->pools);
691 return;
692 }
693 parse_ushort(&class);
694 if (class < 1 || class > 3) {
695 debug(3, 0) ("parse_delay_pool_class: Ignoring pool %d class %d not in 1 .. 3\n", pool, class);
696 return;
697 }
698 pool--;
699 if (cfg->class[pool]) {
700 delayFreeDelayPool(pool);
701 safe_free(cfg->rates[pool]);
702 }
703 cfg->rates[pool] = xmalloc(class * sizeof(delaySpec));
704 cfg->class[pool] = class;
705 cfg->rates[pool]->aggregate.restore_bps = cfg->rates[pool]->aggregate.max_bytes = -1;
706 if (cfg->class[pool] >= 3)
707 cfg->rates[pool]->network.restore_bps = cfg->rates[pool]->network.max_bytes = -1;
708 if (cfg->class[pool] >= 2)
709 cfg->rates[pool]->individual.restore_bps = cfg->rates[pool]->individual.max_bytes = -1;
710 delayCreateDelayPool(pool, class);
711}
712
713static void
714parse_delay_pool_rates(delayConfig * cfg)
715{
716 ushort pool, class;
717 int i;
718 delaySpec *ptr;
719 char *token;
720
721 parse_ushort(&pool);
722 if (pool < 1 || pool > cfg->pools) {
723 debug(3, 0) ("parse_delay_pool_rates: Ignoring pool %d not in 1 .. %d\n", pool, cfg->pools);
724 return;
725 }
726 pool--;
727 class = cfg->class[pool];
728 if (class == 0) {
729 debug(3, 0) ("parse_delay_pool_rates: Ignoring pool %d attempt to set rates with class not set\n", pool + 1);
730 return;
731 }
732 ptr = (delaySpec *) cfg->rates[pool];
733 /* read in "class" sets of restore,max pairs */
734 while (class--) {
735 token = strtok(NULL, "/");
736 if (token == NULL)
737 self_destruct();
738 if (sscanf(token, "%d", &i) != 1)
739 self_destruct();
740 ptr->restore_bps = i;
0e4e0e7d 741 i = GetInteger();
59715b38 742 ptr->max_bytes = i;
743 ptr++;
744 }
745 class = cfg->class[pool];
746 /* if class is 3, swap around network and individual */
747 if (class == 3) {
748 delaySpec tmp;
749
750 tmp = cfg->rates[pool]->individual;
751 cfg->rates[pool]->individual = cfg->rates[pool]->network;
752 cfg->rates[pool]->network = tmp;
753 }
754 /* initialize the delay pools */
755 delayInitDelayPool(pool, class, cfg->rates[pool]);
756}
757
758static void
759parse_delay_pool_access(delayConfig * cfg)
760{
761 ushort pool;
762
763 parse_ushort(&pool);
764 if (pool < 1 || pool > cfg->pools) {
765 debug(3, 0) ("parse_delay_pool_rates: Ignoring pool %d not in 1 .. %d\n", pool, cfg->pools);
766 return;
767 }
768 aclParseAccessLine(&cfg->access[pool - 1]);
769}
770#endif
771
97474590 772static void
6bccf575 773dump_http_header_access(StoreEntry * entry, const char *name, header_mangler header[])
e3dd531e 774{
efd900cb 775 int i;
6bccf575 776 for (i = 0; i < HDR_ENUM_END; i++) {
777 if (header[i].access_list != NULL) {
72711e31 778 storeAppendPrintf(entry, "%s ", name);
6bccf575 779 dump_acl_access(entry, httpHeaderNameById(i),
780 header[i].access_list);
781 }
efd900cb 782 }
97474590 783}
e3dd531e 784
97474590 785static void
6bccf575 786parse_http_header_access(header_mangler header[])
97474590 787{
6bccf575 788 int id, i;
97474590 789 char *t = NULL;
97474590 790 if ((t = strtok(NULL, w_space)) == NULL) {
791 debug(3, 0) ("%s line %d: %s\n",
792 cfg_filename, config_lineno, config_input_line);
6bccf575 793 debug(3, 0) ("parse_http_header_access: missing header name.\n");
97474590 794 return;
795 }
6bccf575 796 /* Now lookup index of header. */
797 id = httpHeaderIdByNameDef(t, strlen(t));
798 if (strcmp(t, "All") == 0)
799 id = HDR_ENUM_END;
800 else if (strcmp(t, "Other") == 0)
801 id = HDR_OTHER;
802 else if (id == -1) {
97474590 803 debug(3, 0) ("%s line %d: %s\n",
804 cfg_filename, config_lineno, config_input_line);
6bccf575 805 debug(3, 0) ("parse_http_header_access: unknown header name %s.\n", t);
97474590 806 return;
807 }
6bccf575 808 if (id != HDR_ENUM_END) {
809 parse_acl_access(&header[id].access_list);
810 } else {
72711e31 811 char *next_string = t + strlen(t) - 1;
de3ee917 812 *next_string = 'A';
72711e31 813 *(next_string + 1) = ' ';
6bccf575 814 for (i = 0; i < HDR_ENUM_END; i++) {
815 char *new_string = xstrdup(next_string);
de3ee917 816 strtok(new_string, w_space);
6bccf575 817 parse_acl_access(&header[i].access_list);
818 safe_free(new_string);
efd900cb 819 }
97474590 820 }
6bccf575 821}
822
823static void
824free_http_header_access(header_mangler header[])
825{
826 int i;
827 for (i = 0; i < HDR_ENUM_END; i++) {
828 free_acl_access(&header[i].access_list);
829 }
830}
831
832static void
833dump_http_header_replace(StoreEntry * entry, const char *name, header_mangler
834 header[])
835{
836 int i;
6bccf575 837 for (i = 0; i < HDR_ENUM_END; i++) {
838 if (NULL == header[i].replacement)
839 continue;
de3ee917 840 storeAppendPrintf(entry, "%s %s %s\n", name, httpHeaderNameById(i),
6bccf575 841 header[i].replacement);
97474590 842 }
843}
e3dd531e 844
97474590 845static void
6bccf575 846parse_http_header_replace(header_mangler header[])
e3dd531e 847{
6bccf575 848 int id, i;
849 char *t = NULL;
850 if ((t = strtok(NULL, w_space)) == NULL) {
851 debug(3, 0) ("%s line %d: %s\n",
852 cfg_filename, config_lineno, config_input_line);
853 debug(3, 0) ("parse_http_header_replace: missing header name.\n");
854 return;
855 }
856 /* Now lookup index of header. */
857 id = httpHeaderIdByNameDef(t, strlen(t));
858 if (strcmp(t, "All") == 0)
859 id = HDR_ENUM_END;
860 else if (strcmp(t, "Other") == 0)
861 id = HDR_OTHER;
862 else if (id == -1) {
863 debug(3, 0) ("%s line %d: %s\n",
864 cfg_filename, config_lineno, config_input_line);
865 debug(3, 0) ("parse_http_header_replace: unknown header name %s.\n",
866 t);
867 return;
868 }
869 if (id != HDR_ENUM_END) {
870 if (header[id].replacement != NULL)
871 safe_free(header[id].replacement);
872 header[id].replacement = xstrdup(t + strlen(t) + 1);
873 } else {
874 for (i = 0; i < HDR_ENUM_END; i++) {
875 if (header[i].replacement != NULL)
876 safe_free(header[i].replacement);
877 header[i].replacement = xstrdup(t + strlen(t) + 1);
878 }
879 }
880}
881
882static void
883free_http_header_replace(header_mangler header[])
884{
885 int i;
886 for (i = 0; i < HDR_ENUM_END; i++) {
887 if (header[i].replacement != NULL)
888 safe_free(header[i].replacement);
889 }
97474590 890}
891
25a77f03 892void
893dump_cachedir_options(StoreEntry * entry, struct cache_dir_option *options, SwapDir * sd)
894{
895 struct cache_dir_option *option;
896 if (!options)
897 return;
898 for (option = options; option->name; option++)
899 option->dump(entry, option->name, sd);
900}
901
e90100aa 902static void
56b63fa1 903dump_cachedir(StoreEntry * entry, const char *name, cacheSwap swap)
e90100aa 904{
f53b06f9 905 SwapDir *s;
906 int i;
a7d59104 907 for (i = 0; i < swap.n_configured; i++) {
908 s = swap.swapDirs + i;
25a77f03 909 storeAppendPrintf(entry, "%s %s %s", name, s->type, s->path);
910 if (s->dump)
911 s->dump(entry, s);
912 dump_cachedir_options(entry, common_cachedir_options, s);
913 storeAppendPrintf(entry, "\n");
f53b06f9 914 }
915}
916
917static int
56b63fa1 918check_null_cachedir(cacheSwap swap)
f53b06f9 919{
920 return swap.swapDirs == NULL;
e90100aa 921}
922
53ad48e6 923static int
924check_null_string(char *s)
925{
926 return s == NULL;
927}
928
2d72d4fd 929static void
94439e4e 930allocate_new_authScheme(authConfig * cfg)
931{
932 if (cfg->schemes == NULL) {
933 cfg->n_allocated = 4;
934 cfg->schemes = xcalloc(cfg->n_allocated, sizeof(authScheme));
935 }
936 if (cfg->n_allocated == cfg->n_configured) {
937 authScheme *tmp;
938 cfg->n_allocated <<= 1;
939 tmp = xcalloc(cfg->n_allocated, sizeof(authScheme));
940 xmemcpy(tmp, cfg->schemes, cfg->n_configured * sizeof(authScheme));
941 xfree(cfg->schemes);
942 cfg->schemes = tmp;
943 }
944}
945
946static void
947parse_authparam(authConfig * config)
948{
949 char *type_str;
950 char *param_str;
951 authScheme *scheme = NULL;
952 int type, i;
953
954 if ((type_str = strtok(NULL, w_space)) == NULL)
955 self_destruct();
956
957 if ((param_str = strtok(NULL, w_space)) == NULL)
958 self_destruct();
959
960 if ((type = authenticateAuthSchemeId(type_str)) == -1) {
961 debug(3, 0) ("Parsing Config File: Unknown authentication scheme '%s'.\n", type_str);
962 return;
963 }
964 for (i = 0; i < config->n_configured; i++) {
965 if (config->schemes[i].Id == type) {
966 scheme = config->schemes + i;
967 }
968 }
969
970 if (scheme == NULL) {
971 allocate_new_authScheme(config);
972 scheme = config->schemes + config->n_configured;
973 config->n_configured++;
974 scheme->Id = type;
975 scheme->typestr = authscheme_list[type].typestr;
976 }
977 authscheme_list[type].parse(scheme, config->n_configured, param_str);
978}
979
980static void
981free_authparam(authConfig * cfg)
982{
983 authScheme *scheme;
984 int i;
985 /* DON'T FREE THESE FOR RECONFIGURE */
986 if (reconfiguring)
987 return;
988 for (i = 0; i < cfg->n_configured; i++) {
989 scheme = cfg->schemes + i;
990 authscheme_list[scheme->Id].freeconfig(scheme);
991 }
992 safe_free(cfg->schemes);
993 cfg->schemes = NULL;
994 cfg->n_allocated = 0;
995 cfg->n_configured = 0;
996}
997
998static void
999dump_authparam(StoreEntry * entry, const char *name, authConfig cfg)
1000{
1001 authScheme *scheme;
1002 int i;
1003 for (i = 0; i < cfg.n_configured; i++) {
1004 scheme = cfg.schemes + i;
1005 authscheme_list[scheme->Id].dump(entry, name, scheme);
1006 }
1007}
1008
0e4e0e7d 1009void
1010allocate_new_swapdir(cacheSwap * swap)
090089c4 1011{
f1dc9b30 1012 if (swap->swapDirs == NULL) {
1013 swap->n_allocated = 4;
1014 swap->swapDirs = xcalloc(swap->n_allocated, sizeof(SwapDir));
1015 }
1016 if (swap->n_allocated == swap->n_configured) {
0e4e0e7d 1017 SwapDir *tmp;
f1dc9b30 1018 swap->n_allocated <<= 1;
1019 tmp = xcalloc(swap->n_allocated, sizeof(SwapDir));
1020 xmemcpy(tmp, swap->swapDirs, swap->n_configured * sizeof(SwapDir));
1021 xfree(swap->swapDirs);
1022 swap->swapDirs = tmp;
1023 }
0e4e0e7d 1024}
1025
cd748f27 1026static int
1027find_fstype(char *type)
1028{
1029 int i;
1030 for (i = 0; storefs_list[i].typestr != NULL; i++) {
a4b8110e 1031 if (strcasecmp(type, storefs_list[i].typestr) == 0) {
cd748f27 1032 return i;
1033 }
1034 }
1035 return (-1);
1036}
1037
0e4e0e7d 1038static void
1039parse_cachedir(cacheSwap * swap)
1040{
1041 char *type_str;
cd748f27 1042 char *path_str;
1043 SwapDir *sd;
1044 int i;
1045 int fs;
cd748f27 1046
0e4e0e7d 1047 if ((type_str = strtok(NULL, w_space)) == NULL)
1048 self_destruct();
cd748f27 1049
cd748f27 1050 if ((path_str = strtok(NULL, w_space)) == NULL)
1051 self_destruct();
1052
1053 /*
1054 * This bit of code is a little strange.
1055 * See, if we find a path and type match for a given line, then
1056 * as long as we're reconfiguring, we can just call its reconfigure
1057 * function. No harm there.
1058 *
1059 * Trouble is, if we find a path match, but not a type match, we have
1060 * a dilemma - we could gracefully shut down the fs, kill it, and
1061 * create a new one of a new type in its place, BUT at this stage the
1062 * fs is meant to be the *NEW* one, and so things go very strange. :-)
1063 *
1064 * So, we'll assume the person isn't going to change the fs type for now,
1065 * and XXX later on we will make sure that its picked up.
1066 *
1067 * (moving around cache_dir lines will be looked at later in a little
1068 * more sane detail..)
1069 */
1070
1071 for (i = 0; i < swap->n_configured; i++) {
a4b8110e 1072 if (0 == strcasecmp(path_str, swap->swapDirs[i].path)) {
cd748f27 1073 /* This is a little weird, you'll appreciate it later */
1074 fs = find_fstype(type_str);
1075 if (fs < 0) {
a4b8110e 1076 fatalf("Unknown cache_dir type '%s'\n", type_str);
cd748f27 1077 }
1078 sd = swap->swapDirs + i;
1079 storefs_list[fs].reconfigurefunc(sd, i, path_str);
a4b8110e 1080 update_maxobjsize();
1081 return;
cd748f27 1082 }
1083 }
1084
2b6662ba 1085 assert(swap->n_configured < 63); /* 7 bits, signed */
dc986280 1086
cd748f27 1087 fs = find_fstype(type_str);
1088 if (fs < 0) {
a4b8110e 1089 /* If we get here, we didn't find a matching cache_dir type */
1090 fatalf("Unknown cache_dir type '%s'\n", type_str);
0e4e0e7d 1091 }
cd748f27 1092 allocate_new_swapdir(swap);
1093 sd = swap->swapDirs + swap->n_configured;
cd748f27 1094 sd->type = storefs_list[fs].typestr;
90d42c28 1095 /* defaults in case fs implementation fails to set these */
8e8d4f30 1096 sd->max_objsize = -1;
90d42c28 1097 sd->fs.blksize = 1024;
8e8d4f30 1098 /* parse the FS parameters and options */
1099 storefs_list[fs].parsefunc(sd, swap->n_configured, path_str);
cd748f27 1100 swap->n_configured++;
1101 /* Update the max object size */
1102 update_maxobjsize();
752c3b27 1103}
1104
8e8d4f30 1105static void
1106parse_cachedir_option_readonly(SwapDir * sd, const char *option, const char *value, int reconfiguring)
1107{
1108 int read_only = 0;
1109 if (value)
1110 read_only = atoi(value);
1111 else
1112 read_only = 1;
1113 sd->flags.read_only = read_only;
1114}
1115
25a77f03 1116static void
1117dump_cachedir_option_readonly(StoreEntry * e, const char *option, SwapDir * sd)
1118{
1119 if (sd->flags.read_only)
1120 storeAppendPrintf(e, " %s", option);
1121}
1122
8e8d4f30 1123static void
1124parse_cachedir_option_maxsize(SwapDir * sd, const char *option, const char *value, int reconfiguring)
1125{
1126 ssize_t size;
1127
1128 if (!value)
1129 self_destruct();
1130
1131 size = atoi(value);
1132
1133 if (reconfiguring && sd->max_objsize != size)
1134 debug(3, 1) ("Cache dir '%s' max object size now %d\n", size);
1135
1136 sd->max_objsize = size;
1137}
1138
25a77f03 1139static void
1140dump_cachedir_option_maxsize(StoreEntry * e, const char *option, SwapDir * sd)
1141{
1142 if (sd->max_objsize != -1)
1143 storeAppendPrintf(e, " %s=%d", option, sd->max_objsize);
1144}
1145
8e8d4f30 1146static struct cache_dir_option common_cachedir_options[] =
1147{
25a77f03 1148 {"read-only", parse_cachedir_option_readonly, dump_cachedir_option_readonly},
1149 {"max-size", parse_cachedir_option_maxsize, dump_cachedir_option_maxsize},
8e8d4f30 1150 {NULL, NULL}
1151};
1152
1153void
1154parse_cachedir_options(SwapDir * sd, struct cache_dir_option *options, int reconfiguring)
1155{
1156 int old_read_only = sd->flags.read_only;
1157 char *name, *value;
1158 struct cache_dir_option *option, *op;
1159
1160 while ((name = strtok(NULL, w_space)) != NULL) {
1161 value = strchr(name, '=');
1162 if (value)
1163 *value++ = '\0'; /* cut on = */
1164 option = NULL;
1165 if (options) {
1166 for (op = options; !option && op->name; op++) {
1167 if (strcmp(op->name, name) == 0) {
1168 option = op;
1169 break;
1170 }
1171 }
1172 }
1173 for (op = common_cachedir_options; !option && op->name; op++) {
1174 if (strcmp(op->name, name) == 0) {
1175 option = op;
1176 break;
1177 }
1178 }
1179 if (!option || !option->parse)
1180 self_destruct();
1181 option->parse(sd, name, value, reconfiguring);
1182 }
1183 /*
1184 * Handle notifications about reconfigured single-options with no value
1185 * where the removal of the option cannot be easily detected in the
1186 * parsing...
1187 */
1188 if (reconfiguring) {
1189 if (old_read_only != sd->flags.read_only) {
1190 debug(3, 1) ("Cache dir '%s' now %s\n",
1191 sd->path, sd->flags.read_only ? "Read-Only" : "Read-Write");
1192 }
1193 }
1194}
1195
8203a132 1196static void
16300b58 1197free_cachedir(cacheSwap * swap)
f1dc9b30 1198{
a47b9029 1199 SwapDir *s;
1200 int i;
860ee7e3 1201 /* DON'T FREE THESE FOR RECONFIGURE */
5cd39a10 1202 if (reconfiguring)
860ee7e3 1203 return;
a47b9029 1204 for (i = 0; i < swap->n_configured; i++) {
1205 s = swap->swapDirs + i;
cd748f27 1206 s->freefs(s);
a47b9029 1207 xfree(s->path);
a47b9029 1208 }
1209 safe_free(swap->swapDirs);
1210 swap->swapDirs = NULL;
1211 swap->n_allocated = 0;
1212 swap->n_configured = 0;
f1dc9b30 1213}
1214
2d72d4fd 1215static const char *
505e35db 1216peer_type_str(const peer_t type)
1217{
0cdcddb9 1218 switch (type) {
505e35db 1219 case PEER_PARENT:
1220 return "parent";
1221 break;
1222 case PEER_SIBLING:
1223 return "sibling";
1224 break;
1225 case PEER_MULTICAST:
1226 return "multicast";
1227 break;
1228 default:
1229 return "unknown";
1230 break;
1231 }
1232}
1233
f1dc9b30 1234static void
a7d59104 1235dump_peer(StoreEntry * entry, const char *name, peer * p)
98ffb7e4 1236{
505e35db 1237 domain_ping *d;
505e35db 1238 domain_type *t;
1239 LOCAL_ARRAY(char, xname, 128);
d41de3c1 1240 while (p != NULL) {
1241 storeAppendPrintf(entry, "%s %s %s %d %d",
1242 name,
1243 p->host,
1244 neighborTypeStr(p),
1245 p->http_port,
399cabec 1246 p->icp.port);
a369131d 1247 dump_peer_options(entry, p);
b6a2f15e 1248 for (d = p->peer_domain; d; d = d->next) {
505e35db 1249 storeAppendPrintf(entry, "cache_peer_domain %s %s%s\n",
1250 p->host,
1251 d->do_ping ? null_string : "!",
1252 d->domain);
1253 }
1810dde6 1254 if (p->access) {
505e35db 1255 snprintf(xname, 128, "cache_peer_access %s", p->host);
1256 dump_acl_access(entry, xname, p->access);
1257 }
1258 for (t = p->typelist; t; t = t->next) {
1259 storeAppendPrintf(entry, "neighbor_type_domain %s %s %s\n",
1260 p->host,
1261 peer_type_str(t->type),
1262 t->domain);
1263 }
d41de3c1 1264 p = p->next;
1265 }
98ffb7e4 1266}
1267
8203a132 1268static void
40a1495e 1269parse_peer(peer ** head)
7813c6d5 1270{
270b86af 1271 char *token = NULL;
40a1495e 1272 peer *p;
7813c6d5 1273 int i;
72711e31 1274 p = cbdataAlloc(peer);
40a1495e 1275 p->http_port = CACHE_HTTP_PORT;
399cabec 1276 p->icp.port = CACHE_ICP_PORT;
40a1495e 1277 p->weight = 1;
dc835977 1278 p->stats.logged_state = PEER_ALIVE;
e481c2dc 1279 if ((token = strtok(NULL, w_space)) == NULL)
270b86af 1280 self_destruct();
40a1495e 1281 p->host = xstrdup(token);
e481c2dc 1282 if ((token = strtok(NULL, w_space)) == NULL)
270b86af 1283 self_destruct();
40a1495e 1284 p->type = parseNeighborType(token);
0e4e0e7d 1285 i = GetInteger();
40a1495e 1286 p->http_port = (u_short) i;
0e4e0e7d 1287 i = GetInteger();
399cabec 1288 p->icp.port = (u_short) i;
270b86af 1289 while ((token = strtok(NULL, w_space))) {
1290 if (!strcasecmp(token, "proxy-only")) {
cd196bc8 1291 p->options.proxy_only = 1;
270b86af 1292 } else if (!strcasecmp(token, "no-query")) {
cd196bc8 1293 p->options.no_query = 1;
8638fc66 1294 } else if (!strcasecmp(token, "no-digest")) {
cd196bc8 1295 p->options.no_digest = 1;
270b86af 1296 } else if (!strcasecmp(token, "multicast-responder")) {
cd196bc8 1297 p->options.mcast_responder = 1;
270b86af 1298 } else if (!strncasecmp(token, "weight=", 7)) {
40a1495e 1299 p->weight = atoi(token + 7);
0103c4c1 1300 } else if (!strcasecmp(token, "closest-only")) {
cd196bc8 1301 p->options.closest_only = 1;
270b86af 1302 } else if (!strncasecmp(token, "ttl=", 4)) {
40a1495e 1303 p->mcast.ttl = atoi(token + 4);
1304 if (p->mcast.ttl < 0)
1305 p->mcast.ttl = 0;
1306 if (p->mcast.ttl > 128)
1307 p->mcast.ttl = 128;
0103c4c1 1308 } else if (!strcasecmp(token, "default")) {
cd196bc8 1309 p->options.default_parent = 1;
0103c4c1 1310 } else if (!strcasecmp(token, "round-robin")) {
cd196bc8 1311 p->options.roundrobin = 1;
dc9d133b 1312#if USE_HTCP
0103c4c1 1313 } else if (!strcasecmp(token, "htcp")) {
cd196bc8 1314 p->options.htcp = 1;
dc9d133b 1315#endif
0103c4c1 1316 } else if (!strcasecmp(token, "no-netdb-exchange")) {
cd196bc8 1317 p->options.no_netdb_exchange = 1;
afd88fbe 1318#if USE_CARP
1319 } else if (!strncasecmp(token, "carp-load-factor=", 17)) {
1320 if (p->type != PEER_PARENT)
0cdcddb9 1321 debug(3, 0) ("parse_peer: Ignoring carp-load-factor for non-parent %s/%d\n", p->host, p->http_port);
1322 else
1323 p->carp.load_factor = atof(token + 17);
95e36d02 1324#endif
1325#if DELAY_POOLS
1326 } else if (!strcasecmp(token, "no-delay")) {
cd196bc8 1327 p->options.no_delay = 1;
afd88fbe 1328#endif
c68e9c6b 1329 } else if (!strncasecmp(token, "login=", 6)) {
1330 p->login = xstrdup(token + 6);
4472c779 1331 rfc1738_unescape(p->login);
3f62decd 1332 } else if (!strncasecmp(token, "connect-timeout=", 16)) {
1333 p->connect_timeout = atoi(token + 16);
7e3ce7b9 1334#if USE_CACHE_DIGESTS
1335 } else if (!strncasecmp(token, "digest-url=", 11)) {
1336 p->digest_url = xstrdup(token + 11);
1337#endif
987de783 1338 } else if (!strcasecmp(token, "allow-miss")) {
1339 p->options.allow_miss = 1;
c7f9eb6d 1340 } else if (!strcasecmp(token, "max-conn=")) {
1341 p->max_conn = atoi(token + 9);
270b86af 1342 } else {
40a1495e 1343 debug(3, 0) ("parse_peer: token='%s'\n", token);
270b86af 1344 self_destruct();
1345 }
1346 }
40a1495e 1347 if (p->weight < 1)
1348 p->weight = 1;
399cabec 1349 p->icp.version = ICP_VERSION_CURRENT;
98829f69 1350 p->tcp_up = PEER_TCP_MAGIC_COUNT;
eb406bb7 1351 p->test_fd = -1;
afd88fbe 1352#if USE_CARP
8ee9b49f 1353#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
afd88fbe 1354 if (p->carp.load_factor) {
e13ee7ad 1355 /* calculate this peers hash for use in CARP */
0cdcddb9 1356 p->carp.hash = 0;
1357 for (token = p->host; *token != 0; token++)
8ee9b49f 1358 p->carp.hash += ROTATE_LEFT(p->carp.hash, 19) + (unsigned int) *token;
d20b1cd0 1359 p->carp.hash += p->carp.hash * 0x62531965;
8ee9b49f 1360 p->carp.hash = ROTATE_LEFT(p->carp.hash, 21);
afd88fbe 1361 }
1362#endif
e13ee7ad 1363#if USE_CACHE_DIGESTS
1364 if (!p->options.no_digest) {
1365 p->digest = peerDigestCreate(p);
8a6218c6 1366 cbdataLock(p->digest); /* so we know when/if digest disappears */
1367 }
e13ee7ad 1368#endif
0153d498 1369 while (*head != NULL)
1370 head = &(*head)->next;
1371 *head = p;
40a1495e 1372 Config.npeers++;
82056f1e 1373 peerClearRR(p);
0153d498 1374}
1375
1376static void
40a1495e 1377free_peer(peer ** P)
0153d498 1378{
40a1495e 1379 peer *p;
79d39a72 1380 while ((p = *P) != NULL) {
a47b9029 1381 *P = p->next;
3855c318 1382#if USE_CACHE_DIGESTS
1383 if (p->digest)
1384 cbdataUnlock(p->digest);
1385 p->digest = NULL;
1386#endif
41042c81 1387 cbdataFree(p);
a47b9029 1388 }
987c67d1 1389 Config.npeers = 0;
270b86af 1390}
1391
1392static void
a7d59104 1393dump_cachemgrpasswd(StoreEntry * entry, const char *name, cachemgr_passwd * list)
270b86af 1394{
d41de3c1 1395 wordlist *w;
1396 while (list != NULL) {
74aa8456 1397 if (strcmp(list->passwd, "none") && strcmp(list->passwd, "disable"))
1398 storeAppendPrintf(entry, "%s XXXXXXXXXX", name);
1399 else
1400 storeAppendPrintf(entry, "%s %s", name, list->passwd);
d41de3c1 1401 for (w = list->actions; w != NULL; w = w->next) {
1402 storeAppendPrintf(entry, " %s", w->key);
1403 }
1404 storeAppendPrintf(entry, "\n");
1405 list = list->next;
1406 }
270b86af 1407}
1408
1409static void
a47b9029 1410parse_cachemgrpasswd(cachemgr_passwd ** head)
270b86af 1411{
1412 char *passwd = NULL;
1413 wordlist *actions = NULL;
22f3fd98 1414 cachemgr_passwd *p;
1415 cachemgr_passwd **P;
270b86af 1416 parse_string(&passwd);
1417 parse_wordlist(&actions);
22f3fd98 1418 p = xcalloc(1, sizeof(cachemgr_passwd));
1419 p->passwd = passwd;
1420 p->actions = actions;
1421 for (P = head; *P; P = &(*P)->next);
1422 *P = p;
270b86af 1423}
1424
1425static void
a47b9029 1426free_cachemgrpasswd(cachemgr_passwd ** head)
270b86af 1427{
a47b9029 1428 cachemgr_passwd *p;
79d39a72 1429 while ((p = *head) != NULL) {
a47b9029 1430 *head = p->next;
1431 xfree(p->passwd);
22f3fd98 1432 wordlistDestroy(&p->actions);
a47b9029 1433 xfree(p);
1434 }
270b86af 1435}
1436
8203a132 1437static void
16300b58 1438dump_denyinfo(StoreEntry * entry, const char *name, acl_deny_info_list * var)
270b86af 1439{
d41de3c1 1440 acl_name_list *a;
1441 while (var != NULL) {
02922e76 1442 storeAppendPrintf(entry, "%s %s", name, var->err_page_name);
d41de3c1 1443 for (a = var->acl_list; a != NULL; a = a->next)
1444 storeAppendPrintf(entry, " %s", a->name);
1445 storeAppendPrintf(entry, "\n");
1446 var = var->next;
1447 }
270b86af 1448}
1449
1450static void
16300b58 1451parse_denyinfo(acl_deny_info_list ** var)
6e40f263 1452{
f1dc9b30 1453 aclParseDenyInfoLine(var);
6e40f263 1454}
403279e0 1455
1273d501 1456void
a47b9029 1457free_denyinfo(acl_deny_info_list ** list)
3c5557f9 1458{
56b63fa1 1459 acl_deny_info_list *a = NULL;
1460 acl_deny_info_list *a_next = NULL;
1461 acl_name_list *l = NULL;
1462 acl_name_list *l_next = NULL;
1273d501 1463 for (a = *list; a; a = a_next) {
a47b9029 1464 for (l = a->acl_list; l; l = l_next) {
1465 l_next = l->next;
3314e65b 1466 memFree(l, MEM_ACL_NAME_LIST);
eeda4c4d 1467 l = NULL;
a47b9029 1468 }
1469 a_next = a->next;
eeda4c4d 1470 memFree(a, MEM_ACL_DENY_INFO_LIST);
1471 a = NULL;
1273d501 1472 }
1473 *list = NULL;
270b86af 1474}
1475
1476static void
505e35db 1477parse_peer_access(void)
270b86af 1478{
1479 char *host = NULL;
505e35db 1480 peer *p;
270b86af 1481 if (!(host = strtok(NULL, w_space)))
1482 self_destruct();
0cdcddb9 1483 if ((p = peerFindByName(host)) == NULL) {
1484 debug(15, 0) ("%s, line %d: No cache_peer '%s'\n",
1485 cfg_filename, config_lineno, host);
1486 return;
1487 }
505e35db 1488 aclParseAccessLine(&p->access);
270b86af 1489}
1490
270b86af 1491static void
1492parse_hostdomain(void)
1493{
1494 char *host = NULL;
1495 char *domain = NULL;
1496 if (!(host = strtok(NULL, w_space)))
1497 self_destruct();
f1dc9b30 1498 while ((domain = strtok(NULL, list_sep))) {
1499 domain_ping *l = NULL;
1500 domain_ping **L = NULL;
40a1495e 1501 peer *p;
1502 if ((p = peerFindByName(host)) == NULL) {
43c3424b 1503 debug(15, 0) ("%s, line %d: No cache_peer '%s'\n",
f1dc9b30 1504 cfg_filename, config_lineno, host);
1505 continue;
1506 }
56b63fa1 1507 l = xcalloc(1, sizeof(domain_ping));
f1dc9b30 1508 l->do_ping = 1;
1509 if (*domain == '!') { /* check for !.edu */
1510 l->do_ping = 0;
1511 domain++;
1512 }
1513 l->domain = xstrdup(domain);
b6a2f15e 1514 for (L = &(p->peer_domain); *L; L = &((*L)->next));
f1dc9b30 1515 *L = l;
1516 }
270b86af 1517}
1518
1519static void
1520parse_hostdomaintype(void)
1521{
1522 char *host = NULL;
1523 char *type = NULL;
1524 char *domain = NULL;
1525 if (!(host = strtok(NULL, w_space)))
1526 self_destruct();
1527 if (!(type = strtok(NULL, w_space)))
1528 self_destruct();
f1dc9b30 1529 while ((domain = strtok(NULL, list_sep))) {
1530 domain_type *l = NULL;
1531 domain_type **L = NULL;
40a1495e 1532 peer *p;
1533 if ((p = peerFindByName(host)) == NULL) {
43c3424b 1534 debug(15, 0) ("%s, line %d: No cache_peer '%s'\n",
f1dc9b30 1535 cfg_filename, config_lineno, host);
1536 return;
1537 }
56b63fa1 1538 l = xcalloc(1, sizeof(domain_type));
f1dc9b30 1539 l->type = parseNeighborType(type);
1540 l->domain = xstrdup(domain);
1541 for (L = &(p->typelist); *L; L = &((*L)->next));
1542 *L = l;
1543 }
270b86af 1544}
1545
7e3ce7b9 1546#if UNUSED_CODE
270b86af 1547static void
a7d59104 1548dump_ushortlist(StoreEntry * entry, const char *name, ushortlist * u)
090089c4 1549{
270b86af 1550 while (u) {
f53b06f9 1551 storeAppendPrintf(entry, "%s %d\n", name, (int) u->i);
270b86af 1552 u = u->next;
1553 }
1554}
090089c4 1555
f53b06f9 1556static int
1557check_null_ushortlist(ushortlist * u)
1558{
1559 return u == NULL;
1560}
1561
270b86af 1562static void
1563parse_ushortlist(ushortlist ** P)
1564{
1565 char *token;
1566 int i;
1567 ushortlist *u;
1568 ushortlist **U;
1569 while ((token = strtok(NULL, w_space))) {
1570 if (sscanf(token, "%d", &i) != 1)
1571 self_destruct();
1572 if (i < 0)
1573 i = 0;
1574 u = xcalloc(1, sizeof(ushortlist));
1575 u->i = (u_short) i;
1576 for (U = P; *U; U = &(*U)->next);
1577 *U = u;
090089c4 1578 }
270b86af 1579}
090089c4 1580
0153d498 1581static void
a47b9029 1582free_ushortlist(ushortlist ** P)
0153d498 1583{
a47b9029 1584 ushortlist *u;
79d39a72 1585 while ((u = *P) != NULL) {
a47b9029 1586 *P = u->next;
1587 xfree(u);
1588 }
0153d498 1589}
7e3ce7b9 1590#endif
0153d498 1591
270b86af 1592static void
a7d59104 1593dump_int(StoreEntry * entry, const char *name, int var)
270b86af 1594{
f53b06f9 1595 storeAppendPrintf(entry, "%s %d\n", name, var);
270b86af 1596}
c1c29eb6 1597
94439e4e 1598void
270b86af 1599parse_int(int *var)
1600{
270b86af 1601 int i;
0e4e0e7d 1602 i = GetInteger();
270b86af 1603 *var = i;
1604}
090089c4 1605
0153d498 1606static void
1607free_int(int *var)
1608{
a47b9029 1609 *var = 0;
0153d498 1610}
1611
270b86af 1612static void
a7d59104 1613dump_onoff(StoreEntry * entry, const char *name, int var)
270b86af 1614{
f53b06f9 1615 storeAppendPrintf(entry, "%s %s\n", name, var ? "on" : "off");
270b86af 1616}
090089c4 1617
270b86af 1618static void
1619parse_onoff(int *var)
1620{
1621 char *token = strtok(NULL, w_space);
090089c4 1622
270b86af 1623 if (token == NULL)
1624 self_destruct();
1625 if (!strcasecmp(token, "on") || !strcasecmp(token, "enable"))
1626 *var = 1;
1627 else
1628 *var = 0;
1629}
e90100aa 1630
0153d498 1631#define free_onoff free_int
f1dc9b30 1632#define dump_eol dump_string
1633#define free_eol free_string
30a4f2a8 1634
270b86af 1635static void
a7d59104 1636dump_refreshpattern(StoreEntry * entry, const char *name, refresh_t * head)
270b86af 1637{
d41de3c1 1638 while (head != NULL) {
9f60cfdf 1639 storeAppendPrintf(entry, "%s%s %s %d %d%% %d\n",
c3f6d204 1640 name,
1641 head->flags.icase ? " -i" : null_string,
1642 head->pattern,
1643 (int) head->min / 60,
1644 (int) (100.0 * head->pct + 0.5),
9f60cfdf 1645 (int) head->max / 60);
1646#if HTTP_VIOLATIONS
1647 if (head->flags.override_expire)
1648 storeAppendPrintf(entry, " override-expire");
1649 if (head->flags.override_lastmod)
1650 storeAppendPrintf(entry, " override-lastmod");
1651 if (head->flags.reload_into_ims)
1652 storeAppendPrintf(entry, " reload-into-ims");
1653 if (head->flags.ignore_reload)
1654 storeAppendPrintf(entry, " ignore-reload");
1655#endif
1656 storeAppendPrintf(entry, "\n");
d41de3c1 1657 head = head->next;
1658 }
270b86af 1659}
090089c4 1660
270b86af 1661static void
f1dc9b30 1662parse_refreshpattern(refresh_t ** head)
270b86af 1663{
f1dc9b30 1664 char *token;
1665 char *pattern;
1666 time_t min = 0;
c3f6d204 1667 double pct = 0.0;
f1dc9b30 1668 time_t max = 0;
9f60cfdf 1669#if HTTP_VIOLATIONS
1dfa1d81 1670 int override_expire = 0;
1671 int override_lastmod = 0;
cbe3a719 1672 int reload_into_ims = 0;
1673 int ignore_reload = 0;
9f60cfdf 1674#endif
f1dc9b30 1675 int i;
1676 refresh_t *t;
1677 regex_t comp;
1678 int errcode;
1679 int flags = REG_EXTENDED | REG_NOSUB;
1680 if ((token = strtok(NULL, w_space)) == NULL)
1681 self_destruct();
1682 if (strcmp(token, "-i") == 0) {
1683 flags |= REG_ICASE;
1684 token = strtok(NULL, w_space);
1685 } else if (strcmp(token, "+i") == 0) {
1686 flags &= ~REG_ICASE;
1687 token = strtok(NULL, w_space);
1688 }
1689 if (token == NULL)
1690 self_destruct();
1691 pattern = xstrdup(token);
0e4e0e7d 1692 i = GetInteger(); /* token: min */
f1dc9b30 1693 min = (time_t) (i * 60); /* convert minutes to seconds */
0e4e0e7d 1694 i = GetInteger(); /* token: pct */
c3f6d204 1695 pct = (double) i / 100.0;
0e4e0e7d 1696 i = GetInteger(); /* token: max */
f1dc9b30 1697 max = (time_t) (i * 60); /* convert minutes to seconds */
1dfa1d81 1698 /* Options */
1699 while ((token = strtok(NULL, w_space)) != NULL) {
9f60cfdf 1700#if HTTP_VIOLATIONS
1dfa1d81 1701 if (!strcmp(token, "override-expire"))
1702 override_expire = 1;
255d6021 1703 else if (!strcmp(token, "override-lastmod"))
1dfa1d81 1704 override_lastmod = 1;
cbe3a719 1705 else if (!strcmp(token, "reload-into-ims")) {
1706 reload_into_ims = 1;
1707 refresh_nocache_hack = 1;
1708 /* tell client_side.c that this is used */
1709 } else if (!strcmp(token, "ignore-reload")) {
1710 ignore_reload = 1;
1711 refresh_nocache_hack = 1;
1712 /* tell client_side.c that this is used */
1713 } else
9f60cfdf 1714#endif
cbe3a719 1715 debug(22, 0) ("redreshAddToList: Unknown option '%s': %s\n",
1dfa1d81 1716 pattern, token);
1717 }
f1dc9b30 1718 if ((errcode = regcomp(&comp, pattern, flags)) != 0) {
1719 char errbuf[256];
1720 regerror(errcode, &comp, errbuf, sizeof errbuf);
1721 debug(22, 0) ("%s line %d: %s\n",
1722 cfg_filename, config_lineno, config_input_line);
1723 debug(22, 0) ("refreshAddToList: Invalid regular expression '%s': %s\n",
1724 pattern, errbuf);
1725 return;
1726 }
c3f6d204 1727 pct = pct < 0.0 ? 0.0 : pct;
f1dc9b30 1728 max = max < 0 ? 0 : max;
1729 t = xcalloc(1, sizeof(refresh_t));
1730 t->pattern = (char *) xstrdup(pattern);
1731 t->compiled_pattern = comp;
1732 t->min = min;
c3f6d204 1733 t->pct = pct;
f1dc9b30 1734 t->max = max;
c3f6d204 1735 if (flags & REG_ICASE)
1736 t->flags.icase = 1;
9f60cfdf 1737#if HTTP_VIOLATIONS
1dfa1d81 1738 if (override_expire)
1739 t->flags.override_expire = 1;
1740 if (override_lastmod)
1741 t->flags.override_lastmod = 1;
cbe3a719 1742 if (reload_into_ims)
1743 t->flags.reload_into_ims = 1;
1744 if (ignore_reload)
1745 t->flags.ignore_reload = 1;
9f60cfdf 1746#endif
f1dc9b30 1747 t->next = NULL;
1748 while (*head)
1749 head = &(*head)->next;
1750 *head = t;
1751 safe_free(pattern);
270b86af 1752}
090089c4 1753
6b53c392 1754static int
a4b8110e 1755check_null_refreshpattern(refresh_t * data)
6b53c392 1756{
1757 return data != NULL;
1758}
1759
270b86af 1760static void
a47b9029 1761free_refreshpattern(refresh_t ** head)
270b86af 1762{
f1dc9b30 1763 refresh_t *t;
79d39a72 1764 while ((t = *head) != NULL) {
f1dc9b30 1765 *head = t->next;
1766 safe_free(t->pattern);
1767 regfree(&t->compiled_pattern);
1768 safe_free(t);
1769 }
270b86af 1770}
12b9e9b1 1771
270b86af 1772static void
a7d59104 1773dump_string(StoreEntry * entry, const char *name, char *var)
270b86af 1774{
f53b06f9 1775 if (var != NULL)
a7d59104 1776 storeAppendPrintf(entry, "%s %s\n", name, var);
270b86af 1777}
98ffb7e4 1778
270b86af 1779static void
0153d498 1780parse_string(char **var)
270b86af 1781{
1782 char *token = strtok(NULL, w_space);
270b86af 1783 safe_free(*var);
1784 if (token == NULL)
1785 self_destruct();
1786 *var = xstrdup(token);
1787}
b15e6857 1788
0153d498 1789static void
1790free_string(char **var)
1791{
027acbaf 1792 safe_free(*var);
0153d498 1793}
caebbe00 1794
94439e4e 1795void
f1dc9b30 1796parse_eol(char *volatile *var)
270b86af 1797{
20cb98c9 1798 unsigned char *token = strtok(NULL, null_string);
270b86af 1799 safe_free(*var);
f1dc9b30 1800 if (token == NULL)
2d72d4fd 1801 self_destruct();
1802 while (*token && isspace(*token))
1803 token++;
20cb98c9 1804 if (!*token)
f1dc9b30 1805 self_destruct();
270b86af 1806 *var = xstrdup(token);
1807}
090089c4 1808
270b86af 1809static void
a7d59104 1810dump_time_t(StoreEntry * entry, const char *name, time_t var)
090089c4 1811{
f53b06f9 1812 storeAppendPrintf(entry, "%s %d seconds\n", name, (int) var);
090089c4 1813}
1814
94439e4e 1815void
a47b9029 1816parse_time_t(time_t * var)
0ffd22bc 1817{
f1dc9b30 1818 parseTimeLine(var, T_SECOND_STR);
0ffd22bc 1819}
1820
270b86af 1821static void
a47b9029 1822free_time_t(time_t * var)
270b86af 1823{
a47b9029 1824 *var = 0;
270b86af 1825}
9906e724 1826
1827static void
a7d59104 1828dump_size_t(StoreEntry * entry, const char *name, size_t var)
1b635117 1829{
f53b06f9 1830 storeAppendPrintf(entry, "%s %d\n", name, (int) var);
1b635117 1831}
1832
1833static void
a7d59104 1834dump_b_size_t(StoreEntry * entry, const char *name, size_t var)
9906e724 1835{
f53b06f9 1836 storeAppendPrintf(entry, "%s %d %s\n", name, (int) var, B_BYTES_STR);
9906e724 1837}
1838
1839static void
a7d59104 1840dump_kb_size_t(StoreEntry * entry, const char *name, size_t var)
9906e724 1841{
f53b06f9 1842 storeAppendPrintf(entry, "%s %d %s\n", name, (int) var, B_KBYTES_STR);
9906e724 1843}
1844
1845static void
a47b9029 1846parse_size_t(size_t * var)
1b635117 1847{
1b635117 1848 int i;
0e4e0e7d 1849 i = GetInteger();
1b635117 1850 *var = (size_t) i;
1851}
1852
1853static void
1854parse_b_size_t(size_t * var)
9906e724 1855{
1856 parseBytesLine(var, B_BYTES_STR);
1857}
1858
1859static void
a47b9029 1860parse_kb_size_t(size_t * var)
9906e724 1861{
1862 parseBytesLine(var, B_KBYTES_STR);
1863}
1864
1865static void
a47b9029 1866free_size_t(size_t * var)
9906e724 1867{
a47b9029 1868 *var = 0;
9906e724 1869}
1870
1b635117 1871#define free_b_size_t free_size_t
9906e724 1872#define free_kb_size_t free_size_t
1873#define free_mb_size_t free_size_t
1874#define free_gb_size_t free_size_t
090089c4 1875
8203a132 1876static void
a7d59104 1877dump_ushort(StoreEntry * entry, const char *name, u_short var)
090089c4 1878{
f53b06f9 1879 storeAppendPrintf(entry, "%s %d\n", name, var);
270b86af 1880}
090089c4 1881
0153d498 1882static void
a47b9029 1883free_ushort(u_short * u)
0153d498 1884{
1885 *u = 0;
1886}
1887
270b86af 1888static void
1889parse_ushort(u_short * var)
1890{
270b86af 1891 int i;
090089c4 1892
0e4e0e7d 1893 i = GetInteger();
270b86af 1894 if (i < 0)
1895 i = 0;
1896 *var = (u_short) i;
090089c4 1897}
1898
270b86af 1899static void
a7d59104 1900dump_wordlist(StoreEntry * entry, const char *name, wordlist * list)
270b86af 1901{
270b86af 1902 while (list != NULL) {
f53b06f9 1903 storeAppendPrintf(entry, "%s %s\n", name, list->key);
270b86af 1904 list = list->next;
429fdbec 1905 }
429fdbec 1906}
1907
94439e4e 1908void
270b86af 1909parse_wordlist(wordlist ** list)
429fdbec 1910{
270b86af 1911 char *token;
270b86af 1912 while ((token = strtok(NULL, w_space)))
1913 wordlistAdd(list, token);
429fdbec 1914}
270b86af 1915
f8d9f54a 1916static int
5da06f20 1917check_null_wordlist(wordlist * w)
f8d9f54a 1918{
1919 return w == NULL;
1920}
1921
63e9d884 1922static int
c6d5b87b 1923check_null_acl_access(acl_access * a)
63e9d884 1924{
1925 return a == NULL;
1926}
1927
0153d498 1928#define free_wordlist wordlistDestroy
270b86af 1929
d548ee64 1930#define free_uri_whitespace free_int
1931
1932static void
1933parse_uri_whitespace(int *var)
1934{
1935 char *token = strtok(NULL, w_space);
1936 if (token == NULL)
1937 self_destruct();
7e3ce7b9 1938 if (!strcasecmp(token, "strip"))
1939 *var = URI_WHITESPACE_STRIP;
1940 else if (!strcasecmp(token, "deny"))
d548ee64 1941 *var = URI_WHITESPACE_DENY;
1942 else if (!strcasecmp(token, "allow"))
1943 *var = URI_WHITESPACE_ALLOW;
1944 else if (!strcasecmp(token, "encode"))
1945 *var = URI_WHITESPACE_ENCODE;
1946 else if (!strcasecmp(token, "chop"))
1947 *var = URI_WHITESPACE_CHOP;
1948 else
1949 self_destruct();
1950}
1951
1952
1953static void
1954dump_uri_whitespace(StoreEntry * entry, const char *name, int var)
1955{
1956 char *s;
1957 if (var == URI_WHITESPACE_ALLOW)
1958 s = "allow";
1959 else if (var == URI_WHITESPACE_ENCODE)
1960 s = "encode";
1961 else if (var == URI_WHITESPACE_CHOP)
1962 s = "chop";
7e3ce7b9 1963 else if (var == URI_WHITESPACE_DENY)
d548ee64 1964 s = "deny";
7e3ce7b9 1965 else
1966 s = "strip";
d548ee64 1967 storeAppendPrintf(entry, "%s %s\n", name, s);
1968}
1969
6a566b9c 1970static void
c1dd71ae 1971free_removalpolicy(RemovalPolicySettings ** settings)
6a566b9c 1972{
1973 if (!*settings)
1974 return;
1975 free_string(&(*settings)->type);
1976 free_wordlist(&(*settings)->args);
1977 xfree(*settings);
1978 *settings = NULL;
1979}
1980
1981static void
c1dd71ae 1982parse_removalpolicy(RemovalPolicySettings ** settings)
6a566b9c 1983{
1984 if (*settings)
1985 free_removalpolicy(settings);
1986 *settings = xcalloc(1, sizeof(**settings));
1987 parse_string(&(*settings)->type);
1988 parse_wordlist(&(*settings)->args);
1989}
1990
1991static void
c1dd71ae 1992dump_removalpolicy(StoreEntry * entry, const char *name, RemovalPolicySettings * settings)
6a566b9c 1993{
1994 wordlist *args;
1995 storeAppendPrintf(entry, "%s %s", name, settings->type);
1996 args = settings->args;
1997 while (args) {
1998 storeAppendPrintf(entry, " %s", args->key);
1999 args = args->next;
2000 }
be58afb5 2001 storeAppendPrintf(entry, "\n");
6a566b9c 2002}
c1dd71ae 2003
6a566b9c 2004
270b86af 2005#include "cf_parser.c"
f1dc9b30 2006
2007peer_t
2008parseNeighborType(const char *s)
2009{
2010 if (!strcasecmp(s, "parent"))
2011 return PEER_PARENT;
2012 if (!strcasecmp(s, "neighbor"))
2013 return PEER_SIBLING;
2014 if (!strcasecmp(s, "neighbour"))
2015 return PEER_SIBLING;
2016 if (!strcasecmp(s, "sibling"))
2017 return PEER_SIBLING;
2018 if (!strcasecmp(s, "multicast"))
2019 return PEER_MULTICAST;
2020 debug(15, 0) ("WARNING: Unknown neighbor type: %s\n", s);
2021 return PEER_SIBLING;
2022}
f150dd4b 2023
7e3ce7b9 2024static void
2025parse_sockaddr_in_list(sockaddr_in_list ** head)
2026{
2027 char *token;
2028 char *t;
efd900cb 2029 char *host;
7e3ce7b9 2030 const struct hostent *hp;
efd900cb 2031 unsigned short port;
7e3ce7b9 2032 sockaddr_in_list *s;
2033 while ((token = strtok(NULL, w_space))) {
efd900cb 2034 host = NULL;
2035 port = 0;
7e3ce7b9 2036 if ((t = strchr(token, ':'))) {
2037 /* host:port */
2038 host = token;
2039 *t = '\0';
efd900cb 2040 port = (unsigned short) atoi(t + 1);
2041 if (0 == port)
7e3ce7b9 2042 self_destruct();
efd900cb 2043 } else if ((port = atoi(token)) > 0) {
7e3ce7b9 2044 /* port */
2045 } else {
2046 self_destruct();
2047 }
2048 s = xcalloc(1, sizeof(*s));
efd900cb 2049 s->s.sin_port = htons(port);
7e3ce7b9 2050 if (NULL == host)
2051 s->s.sin_addr = any_addr;
2052 else if (1 == safe_inet_addr(host, &s->s.sin_addr))
2053 (void) 0;
efd900cb 2054 else if ((hp = gethostbyname(host))) /* dont use ipcache */
7e3ce7b9 2055 s->s.sin_addr = inaddrFromHostent(hp);
2056 else
2057 self_destruct();
2058 while (*head)
2059 head = &(*head)->next;
2060 *head = s;
2061 }
2062}
2063
2064static void
2065dump_sockaddr_in_list(StoreEntry * e, const char *n, const sockaddr_in_list * s)
2066{
2067 while (s) {
2068 storeAppendPrintf(e, "%s %s:%d\n",
2069 n,
2070 inet_ntoa(s->s.sin_addr),
2071 ntohs(s->s.sin_port));
2072 s = s->next;
2073 }
2074}
2075
2076static void
2077free_sockaddr_in_list(sockaddr_in_list ** head)
2078{
2079 sockaddr_in_list *s;
2080 while ((s = *head) != NULL) {
2081 *head = s->next;
2082 xfree(s);
2083 }
2084}
2085
2086static int
2087check_null_sockaddr_in_list(const sockaddr_in_list * s)
2088{
2089 return NULL == s;
2090}
2091
d193a436 2092#if USE_SSL
2093static void
2094parse_https_port_list(https_port_list ** head)
2095{
2096 char *token;
2097 char *t;
2098 char *host;
2099 const struct hostent *hp;
2100 unsigned short port;
2101 https_port_list *s;
2102 token = strtok(NULL, w_space);
2103 if (!token)
2104 self_destruct();
2105 host = NULL;
2106 port = 0;
2107 if ((t = strchr(token, ':'))) {
2108 /* host:port */
2109 host = token;
2110 *t = '\0';
2111 port = (unsigned short) atoi(t + 1);
2112 if (0 == port)
2113 self_destruct();
2114 } else if ((port = atoi(token)) > 0) {
2115 /* port */
2116 } else {
2117 self_destruct();
2118 }
2119 s = xcalloc(1, sizeof(*s));
2120 s->s.sin_port = htons(port);
2121 if (NULL == host)
2122 s->s.sin_addr = any_addr;
2123 else if (1 == safe_inet_addr(host, &s->s.sin_addr))
2124 (void) 0;
2125 else if ((hp = gethostbyname(host))) /* dont use ipcache */
2126 s->s.sin_addr = inaddrFromHostent(hp);
2127 else
2128 self_destruct();
2129 /* parse options ... */
2130 while ((token = strtok(NULL, w_space))) {
2131 if (strncmp(token, "cert=", 5) == 0) {
2132 safe_free(s->cert);
2133 s->cert = xstrdup(token + 5);
2134 } else if (strncmp(token, "key=", 4) == 0) {
2135 safe_free(s->key);
2136 s->key = xstrdup(token + 4);
2137 } else {
2138 self_destruct();
2139 }
2140 }
2141 while (*head)
2142 head = &(*head)->next;
2143 *head = s;
2144}
2145
2146static void
2147dump_https_port_list(StoreEntry * e, const char *n, const https_port_list * s)
2148{
2149 while (s) {
2150 storeAppendPrintf(e, "%s %s:%d cert=\"%s\" key=\"%s\"\n",
2151 n,
2152 inet_ntoa(s->s.sin_addr),
2153 ntohs(s->s.sin_port),
2154 s->cert,
2155 s->key);
2156 s = s->next;
2157 }
2158}
2159
2160static void
2161free_https_port_list(https_port_list ** head)
2162{
2163 https_port_list *s;
2164 while ((s = *head) != NULL) {
2165 *head = s->next;
2166 safe_free(s->cert);
2167 safe_free(s->key);
2168 safe_free(s);
2169 }
2170}
2171
2172#if 0
2173static int
2174check_null_https_port_list(const https_port_list * s)
2175{
2176 return NULL == s;
2177}
2178#endif
2179
2180#endif /* USE_SSL */
2181
f150dd4b 2182void
2183configFreeMemory(void)
2184{
23ff6968 2185 free_all();
f150dd4b 2186}
f0b19334 2187
94439e4e 2188void
f0b19334 2189requirePathnameExists(const char *name, const char *path)
2190{
2191 struct stat sb;
f0b19334 2192 assert(path != NULL);
137ee196 2193 if (stat(path, &sb) < 0)
2194 fatalf("%s: %s", path, xstrerror());
f0b19334 2195}