- Use strlcpy() instead of strcpy().
[privoxy.git] / loadcfg.c
1 const char loadcfg_rcs[] = "$Id: loadcfg.c,v 1.63 2007/04/09 18:11:36 fabiankeil Exp $";
2 /*********************************************************************
3  *
4  * File        :  $Source: /cvsroot/ijbswa/current/loadcfg.c,v $
5  *
6  * Purpose     :  Loads settings from the configuration file into
7  *                global variables.  This file contains both the
8  *                routine to load the configuration and the global
9  *                variables it writes to.
10  *
11  * Copyright   :  Written by and Copyright (C) 2001-2007 the SourceForge
12  *                Privoxy team. http://www.privoxy.org/
13  *
14  *                Based on the Internet Junkbuster originally written
15  *                by and Copyright (C) 1997 Anonymous Coders and
16  *                Junkbusters Corporation.  http://www.junkbusters.com
17  *
18  *                This program is free software; you can redistribute it
19  *                and/or modify it under the terms of the GNU General
20  *                Public License as published by the Free Software
21  *                Foundation; either version 2 of the License, or (at
22  *                your option) any later version.
23  *
24  *                This program is distributed in the hope that it will
25  *                be useful, but WITHOUT ANY WARRANTY; without even the
26  *                implied warranty of MERCHANTABILITY or FITNESS FOR A
27  *                PARTICULAR PURPOSE.  See the GNU General Public
28  *                License for more details.
29  *
30  *                The GNU General Public License should be included with
31  *                this file.  If not, you can view it at
32  *                http://www.gnu.org/copyleft/gpl.html
33  *                or write to the Free Software Foundation, Inc., 59
34  *                Temple Place - Suite 330, Boston, MA  02111-1307, USA.
35  *
36  * Revisions   :
37  *    $Log: loadcfg.c,v $
38  *    Revision 1.63  2007/04/09 18:11:36  fabiankeil
39  *    Don't mistake VC++'s _snprintf() for a snprintf() replacement.
40  *
41  *    Revision 1.62  2007/03/17 15:20:05  fabiankeil
42  *    New config option: enforce-blocks.
43  *
44  *    Revision 1.61  2007/03/16 16:47:35  fabiankeil
45  *    - Mention other reasons why acl directive loading might have failed.
46  *    - Don't log the acl source if the acl destination is to blame.
47  *
48  *    Revision 1.60  2007/01/27 13:09:16  fabiankeil
49  *    Add new config option "templdir" to
50  *    change the templates directory.
51  *
52  *    Revision 1.59  2006/12/31 17:56:38  fabiankeil
53  *    Added config option accept-intercepted-requests
54  *    and disabled it by default.
55  *
56  *    Revision 1.58  2006/12/31 14:24:29  fabiankeil
57  *    Fix gcc43 compiler warnings.
58  *
59  *    Revision 1.57  2006/12/21 12:57:48  fabiankeil
60  *    Add config option "split-large-forms"
61  *    to work around the browser bug reported
62  *    in BR #1570678.
63  *
64  *    Revision 1.56  2006/12/17 17:04:51  fabiankeil
65  *    Move the <br> in the generated HTML for the config
66  *    options from the beginning of the string to its end.
67  *    Keeps the white space in balance.
68  *
69  *    Revision 1.55  2006/11/28 15:31:52  fabiankeil
70  *    Fix memory leak in case of config file reloads.
71  *
72  *    Revision 1.54  2006/10/21 16:04:22  fabiankeil
73  *    Modified kludge for win32 to make ming32 menu
74  *    "Options/Edit Filters" (sort of) work again.
75  *    Same limitations as for the action files apply.
76  *    Fixes BR 1567373.
77  *
78  *    Revision 1.53  2006/09/06 18:45:03  fabiankeil
79  *    Incorporate modified version of Roland Rosenfeld's patch to
80  *    optionally access the user-manual via Privoxy. Closes patch 679075.
81  *
82  *    Formatting changed to Privoxy style, added call to
83  *    cgi_error_no_template if the requested file doesn't
84  *    exist and modified check whether or not Privoxy itself
85  *    should serve the manual. Should work cross-platform now.
86  *
87  *    Revision 1.52  2006/09/06 10:43:32  fabiankeil
88  *    Added config option enable-remote-http-toggle
89  *    to specify if Privoxy should recognize special
90  *    headers (currently only X-Filter) to change its
91  *    behaviour. Disabled by default.
92  *
93  *    Revision 1.51  2006/09/06 09:23:37  fabiankeil
94  *    Make number of retries in case of forwarded-connect problems
95  *    a config file option (forwarded-connect-retries) and use 0 as
96  *    default.
97  *
98  *    Revision 1.50  2006/07/18 14:48:46  david__schmidt
99  *    Reorganizing the repository: swapping out what was HEAD (the old 3.1 branch)
100  *    with what was really the latest development (the v_3_0_branch branch)
101  *
102  *    Revision 1.48.2.7  2006/02/02 17:29:16  david__schmidt
103  *    Don't forget to malloc space for the null terminator...
104  *
105  *    Revision 1.48.2.6  2006/01/29 23:10:56  david__schmidt
106  *    Multiple filter file support
107  *
108  *    Revision 1.48.2.5  2003/05/08 15:17:25  oes
109  *    Closed two memory leaks; hopefully the last remaining ones
110  *    (in the main execution paths, anyway).
111  *
112  *    Revision 1.48.2.4  2003/04/11 12:06:14  oes
113  *    Addressed bug #719435
114  *     - Extraneous filterfile directives now logged as errors
115  *     - This and unrecnonised directives now really obvious on status page
116  *
117  *    Revision 1.48.2.3  2003/03/11 11:53:59  oes
118  *    Cosmetic: Renamed cryptic variable
119  *
120  *    Revision 1.48.2.2  2002/11/12 16:28:20  oes
121  *    Move unrelated variable declaration out of #ifdef FEATURE_ACL; fixes bug #636655
122  *
123  *    Revision 1.48.2.1  2002/08/21 17:58:05  oes
124  *    Temp kludge to let user and default action file be edited through win32 GUI (FR 592080)
125  *
126  *    Revision 1.48  2002/05/14 21:30:38  oes
127  *    savearg now uses own linking code instead of (now special-cased) add_help_link
128  *
129  *    Revision 1.47  2002/05/12 21:36:29  jongfoster
130  *    Correcting function comments
131  *
132  *    Revision 1.46  2002/04/26 12:55:14  oes
133  *     - New option "user-manual", defaults to our site
134  *       via project.h #define
135  *     - savearg now embeds option names in help links
136  *
137  *    Revision 1.45  2002/04/24 02:11:54  oes
138  *    Jon's multiple AF patch: Allow up to MAX_AF_FILES actionsfile options
139  *
140  *    Revision 1.44  2002/04/08 20:37:13  swa
141  *    fixed JB spelling
142  *
143  *    Revision 1.43  2002/04/08 20:36:50  swa
144  *    fixed JB spelling
145  *
146  *    Revision 1.42  2002/04/05 15:50:15  oes
147  *    fix for invalid HTML proxy_args
148  *
149  *    Revision 1.41  2002/03/31 17:19:00  jongfoster
150  *    Win32 only: Enabling STRICT to fix a VC++ compile warning.
151  *
152  *    Revision 1.40  2002/03/26 22:29:55  swa
153  *    we have a new homepage!
154  *
155  *    Revision 1.39  2002/03/24 13:25:43  swa
156  *    name change related issues
157  *
158  *    Revision 1.38  2002/03/24 13:05:48  jongfoster
159  *    Renaming re_filterfile to filterfile
160  *
161  *    Revision 1.37  2002/03/16 23:54:06  jongfoster
162  *    Adding graceful termination feature, to help look for memory leaks.
163  *    If you enable this (which, by design, has to be done by hand
164  *    editing config.h) and then go to http://i.j.b/die, then the program
165  *    will exit cleanly after the *next* request.  It should free all the
166  *    memory that was used.
167  *
168  *    Revision 1.36  2002/03/13 00:27:05  jongfoster
169  *    Killing warnings
170  *
171  *    Revision 1.35  2002/03/07 03:52:44  oes
172  *    Set logging to tty for --no-daemon mode
173  *
174  *    Revision 1.34  2002/03/06 23:14:35  jongfoster
175  *    Trivial cosmetic changes to make function comments easier to find.
176  *
177  *    Revision 1.33  2002/03/05 04:52:42  oes
178  *    Deleted non-errlog debugging code
179  *
180  *    Revision 1.32  2002/03/04 18:24:53  oes
181  *    Re-enabled output of unknown config directive hash
182  *
183  *    Revision 1.31  2002/03/03 15:07:20  oes
184  *    Re-enabled automatic config reloading
185  *
186  *    Revision 1.30  2002/01/22 23:31:43  jongfoster
187  *    Replacing strsav() with string_append()
188  *
189  *    Revision 1.29  2002/01/17 21:02:30  jongfoster
190  *    Moving all our URL and URL pattern parsing code to urlmatch.c.
191  *
192  *    Renaming free_url to free_url_spec, since it frees a struct url_spec.
193  *
194  *    Revision 1.28  2001/12/30 14:07:32  steudten
195  *    - Add signal handling (unix)
196  *    - Add SIGHUP handler (unix)
197  *    - Add creation of pidfile (unix)
198  *    - Add action 'top' in rc file (RH)
199  *    - Add entry 'SIGNALS' to manpage
200  *    - Add exit message to logfile (unix)
201  *
202  *    Revision 1.27  2001/11/07 00:02:13  steudten
203  *    Add line number in error output for lineparsing for
204  *    actionsfile and configfile.
205  *    Special handling for CLF added.
206  *
207  *    Revision 1.26  2001/11/05 21:41:43  steudten
208  *    Add changes to be a real daemon just for unix os.
209  *    (change cwd to /, detach from controlling tty, set
210  *    process group and session leader to the own process.
211  *    Add DBG() Macro.
212  *    Add some fatal-error log message for failed malloc().
213  *    Add '-d' if compiled with 'configure --with-debug' to
214  *    enable debug output.
215  *
216  *    Revision 1.25  2001/10/25 03:40:48  david__schmidt
217  *    Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
218  *    threads to call select() simultaneously.  So, it's time to do a real, live,
219  *    native OS/2 port.  See defines for __EMX__ (the porting layer) vs. __OS2__
220  *    (native). Both versions will work, but using __OS2__ offers multi-threading.
221  *
222  *    Revision 1.24  2001/10/23 21:40:30  jongfoster
223  *    Added support for enable-edit-actions and enable-remote-toggle config
224  *    file options.
225  *
226  *    Revision 1.23  2001/10/07 15:36:00  oes
227  *    Introduced new config option "buffer-limit"
228  *
229  *    Revision 1.22  2001/09/22 16:36:59  jongfoster
230  *    Removing unused parameter fs from read_config_line()
231  *
232  *    Revision 1.21  2001/09/16 17:10:43  jongfoster
233  *    Moving function savearg() here, since it was the only thing left in
234  *    showargs.c.
235  *
236  *    Revision 1.20  2001/07/30 22:08:36  jongfoster
237  *    Tidying up #defines:
238  *    - All feature #defines are now of the form FEATURE_xxx
239  *    - Permanently turned off WIN_GUI_EDIT
240  *    - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
241  *
242  *    Revision 1.19  2001/07/15 17:45:16  jongfoster
243  *    Removing some unused #includes
244  *
245  *    Revision 1.18  2001/07/13 14:01:14  oes
246  *     - Removed all #ifdef PCRS
247  *     - Removed vim-settings
248  *
249  *    Revision 1.17  2001/06/29 13:31:03  oes
250  *    - Improved comments
251  *    - Fixed (actionsfile) and sorted hashes
252  *    - Introduced admin_address and proxy-info-url
253  *      as config parameters
254  *    - Renamed config->proxy_args_invocation (which didn't have
255  *      the invocation but the options!) to config->proxy_args
256  *    - Various adaptions
257  *    - Removed logentry from cancelled commit
258  *
259  *    Revision 1.16  2001/06/09 10:55:28  jongfoster
260  *    Changing BUFSIZ ==> BUFFER_SIZE
261  *
262  *    Revision 1.15  2001/06/07 23:13:40  jongfoster
263  *    Merging ACL and forward files into config file.
264  *    Cosmetic: Sorting config file options alphabetically.
265  *    Cosmetic: Adding brief syntax comments to config file options.
266  *
267  *    Revision 1.14  2001/06/07 14:46:25  joergs
268  *    Missing make_path() added for re_filterfile.
269  *
270  *    Revision 1.13  2001/06/05 22:33:54  jongfoster
271  *
272  *    Fixed minor memory leak.
273  *    Also now uses make_path to prepend the pathnames.
274  *
275  *    Revision 1.12  2001/06/05 20:04:09  jongfoster
276  *    Now uses _snprintf() in place of snprintf() under Win32.
277  *
278  *    Revision 1.11  2001/06/04 18:31:58  swa
279  *    files are now prefixed with either `confdir' or `logdir'.
280  *    `make redhat-dist' replaces both entries confdir and logdir
281  *    with redhat values
282  *
283  *    Revision 1.10  2001/06/03 19:11:54  oes
284  *    introduced confdir option
285  *
286  *    Revision 1.9  2001/06/01 20:06:24  jongfoster
287  *    Removed support for "tinygif" option - moved to actions file.
288  *
289  *    Revision 1.8  2001/05/31 21:27:13  jongfoster
290  *    Removed many options from the config file and into the
291  *    "actions" file: add_forwarded, suppress_vanilla_wafer,
292  *    wafer, add_header, user_agent, referer, from
293  *    Also globally replaced "permission" with "action".
294  *
295  *    Revision 1.7  2001/05/29 09:50:24  jongfoster
296  *    Unified blocklist/imagelist/permissionslist.
297  *    File format is still under discussion, but the internal changes
298  *    are (mostly) done.
299  *
300  *    Also modified interceptor behaviour:
301  *    - We now intercept all URLs beginning with one of the following
302  *      prefixes (and *only* these prefixes):
303  *        * http://i.j.b/
304  *        * http://ijbswa.sf.net/config/
305  *        * http://ijbswa.sourceforge.net/config/
306  *    - New interceptors "home page" - go to http://i.j.b/ to see it.
307  *    - Internal changes so that intercepted and fast redirect pages
308  *      are not replaced with an image.
309  *    - Interceptors now have the option to send a binary page direct
310  *      to the client. (i.e. ijb-send-banner uses this)
311  *    - Implemented show-url-info interceptor.  (Which is why I needed
312  *      the above interceptors changes - a typical URL is
313  *      "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
314  *      The previous mechanism would not have intercepted that, and
315  *      if it had been intercepted then it then it would have replaced
316  *      it with an image.)
317  *
318  *    Revision 1.6  2001/05/26 00:28:36  jongfoster
319  *    Automatic reloading of config file.
320  *    Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
321  *    Most of the global variables have been moved to a new
322  *    struct configuration_spec, accessed through csp->config->globalname
323  *    Most of the globals remaining are used by the Win32 GUI.
324  *
325  *    Revision 1.5  2001/05/25 22:34:30  jongfoster
326  *    Hard tabs->Spaces
327  *
328  *    Revision 1.4  2001/05/22 18:46:04  oes
329  *
330  *    - Enabled filtering banners by size rather than URL
331  *      by adding patterns that replace all standard banner
332  *      sizes with the "Junkbuster" gif to the re_filterfile
333  *
334  *    - Enabled filtering WebBugs by providing a pattern
335  *      which kills all 1x1 images
336  *
337  *    - Added support for PCRE_UNGREEDY behaviour to pcrs,
338  *      which is selected by the (nonstandard and therefore
339  *      capital) letter 'U' in the option string.
340  *      It causes the quantifiers to be ungreedy by default.
341  *      Appending a ? turns back to greedy (!).
342  *
343  *    - Added a new interceptor ijb-send-banner, which
344  *      sends back the "Junkbuster" gif. Without imagelist or
345  *      MSIE detection support, or if tinygif = 1, or the
346  *      URL isn't recognized as an imageurl, a lame HTML
347  *      explanation is sent instead.
348  *
349  *    - Added new feature, which permits blocking remote
350  *      script redirects and firing back a local redirect
351  *      to the browser.
352  *      The feature is conditionally compiled, i.e. it
353  *      can be disabled with --disable-fast-redirects,
354  *      plus it must be activated by a "fast-redirects"
355  *      line in the config file, has its own log level
356  *      and of course wants to be displayed by show-proxy-args
357  *      Note: Boy, all the #ifdefs in 1001 locations and
358  *      all the fumbling with configure.in and acconfig.h
359  *      were *way* more work than the feature itself :-(
360  *
361  *    - Because a generic redirect template was needed for
362  *      this, tinygif = 3 now uses the same.
363  *
364  *    - Moved GIFs, and other static HTTP response templates
365  *      to project.h
366  *
367  *    - Some minor fixes
368  *
369  *    - Removed some >400 CRs again (Jon, you really worked
370  *      a lot! ;-)
371  *
372  *    Revision 1.3  2001/05/20 01:21:20  jongfoster
373  *    Version 2.9.4 checkin.
374  *    - Merged popupfile and cookiefile, and added control over PCRS
375  *      filtering, in new "permissionsfile".
376  *    - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
377  *      file error you now get a message box (in the Win32 GUI) rather
378  *      than the program exiting with no explanation.
379  *    - Made killpopup use the PCRS MIME-type checking and HTTP-header
380  *      skipping.
381  *    - Removed tabs from "config"
382  *    - Moved duplicated url parsing code in "loaders.c" to a new funcition.
383  *    - Bumped up version number.
384  *
385  *    Revision 1.2  2001/05/17 23:01:01  oes
386  *     - Cleaned CRLF's from the sources and related files
387  *
388  *    Revision 1.1.1.1  2001/05/15 13:58:58  oes
389  *    Initial import of version 2.9.3 source tree
390  *
391  *
392  *********************************************************************/
393 \f
394
395 #include "config.h"
396
397 #include <stdio.h>
398 #include <sys/types.h>
399 #include <stdlib.h>
400 #include <string.h>
401 #include <signal.h>
402 #include <fcntl.h>
403 #include <errno.h>
404 #include <ctype.h>
405 #include <assert.h>
406
407 #ifdef _WIN32
408
409 # ifndef STRICT
410 #  define STRICT
411 # endif
412 # include <windows.h>
413
414 # include "win32.h"
415 # ifndef _WIN_CONSOLE
416 #  include "w32log.h"
417 # endif /* ndef _WIN_CONSOLE */
418
419 #else /* ifndef _WIN32 */
420
421 #ifndef __OS2__
422 # include <unistd.h>
423 # include <sys/wait.h>
424 #endif
425 # include <sys/time.h>
426 # include <sys/stat.h>
427 # include <signal.h>
428
429 #endif
430
431 #include "loadcfg.h"
432 #include "list.h"
433 #include "jcc.h"
434 #include "filters.h"
435 #include "loaders.h"
436 #include "miscutil.h"
437 #include "errlog.h"
438 #include "ssplit.h"
439 #include "encode.h"
440 #include "urlmatch.h"
441 #include "cgi.h"
442
443 const char loadcfg_h_rcs[] = LOADCFG_H_VERSION;
444
445 /*
446  * Fix a problem with Solaris.  There should be no effect on other
447  * platforms.
448  * Solaris's isspace() is a macro which uses it's argument directly
449  * as an array index.  Therefore we need to make sure that high-bit
450  * characters generate +ve values, and ideally we also want to make
451  * the argument match the declared parameter type of "int".
452  */
453 #define ijb_isupper(__X) isupper((int)(unsigned char)(__X))
454 #define ijb_tolower(__X) tolower((int)(unsigned char)(__X))
455
456 #ifdef FEATURE_TOGGLE
457 /* Privoxy is enabled by default. */
458 int global_toggle_state = 1;
459 #endif /* def FEATURE_TOGGLE */
460
461 /* The filename of the configfile */
462 const char *configfile  = NULL;
463
464 /*
465  * CGI functions will later need access to the invocation args,
466  * so we will make argc and argv global.
467  */
468 int Argc = 0;
469 const char **Argv = NULL;
470
471 static struct file_list *current_configfile = NULL;
472
473
474 /*
475  * This takes the "cryptic" hash of each keyword and aliases them to
476  * something a little more readable.  This also makes changing the
477  * hash values easier if they should change or the hash algorthm changes.
478  * Use the included "hash" program to find out what the hash will be
479  * for any string supplied on the command line.  (Or just put it in the
480  * config file and read the number from the error message in the log).
481  *
482  * Please keep this list sorted alphabetically (but with the Windows
483  * console and GUI specific options last).
484  */
485
486 #define hash_actions_file                1196306641ul /* "actionsfile" */
487 #define hash_accept_intercepted_requests 1513024973ul /* "accept-intercepted-requests" */
488 #define hash_admin_address               4112573064ul /* "admin-address" */
489 #define hash_buffer_limit                1881726070ul /* "buffer-limit */
490 #define hash_confdir                        1978389ul /* "confdir" */
491 #define hash_debug                            78263ul /* "debug" */
492 #define hash_deny_access                 1227333715ul /* "deny-access" */
493 #define hash_enable_edit_actions         2517097536ul /* "enable-edit-actions" */
494 #define hash_enable_remote_toggle        2979744683ul /* "enable-remote-toggle" */
495 #define hash_enable_remote_http_toggle    110543988ul /* "enable-remote-http-toggle" */
496 #define hash_enforce_blocks              1862427469ul /* "enforce-blocks" */
497 #define hash_filterfile                   250887266ul /* "filterfile" */
498 #define hash_forward                        2029845ul /* "forward" */
499 #define hash_forward_socks4              3963965521ul /* "forward-socks4" */
500 #define hash_forward_socks4a             2639958518ul /* "forward-socks4a" */
501 #define hash_forwarded_connect_retries    101465292ul /* "forwarded-connect-retries" */
502 #define hash_jarfile                        2046641ul /* "jarfile" */
503 #define hash_listen_address              1255650842ul /* "listen-address" */
504 #define hash_logdir                          422889ul /* "logdir" */
505 #define hash_logfile                        2114766ul /* "logfile" */
506 #define hash_permit_access               3587953268ul /* "permit-access" */
507 #define hash_proxy_info_url              3903079059ul /* "proxy-info-url" */
508 #define hash_single_threaded             4250084780ul /* "single-threaded" */
509 #define hash_split_large_cgi_forms        671658948ul /* "split-large-cgi-forms" */
510 #define hash_suppress_blocklists         1948693308ul /* "suppress-blocklists" */
511 #define hash_templdir                      11067889ul /* "templdir" */
512 #define hash_toggle                          447966ul /* "toggle" */
513 #define hash_trust_info_url               430331967ul /* "trust-info-url" */
514 #define hash_trustfile                     56494766ul /* "trustfile" */
515 #define hash_usermanual                  1416668518ul /* "user-manual" */
516 #define hash_activity_animation          1817904738ul /* "activity-animation" */
517 #define hash_close_button_minimizes      3651284693ul /* "close-button-minimizes" */
518 #define hash_hide_console                2048809870ul /* "hide-console" */
519 #define hash_log_buffer_size             2918070425ul /* "log-buffer-size" */
520 #define hash_log_font_name               2866730124ul /* "log-font-name" */
521 #define hash_log_font_size               2866731014ul /* "log-font-size" */
522 #define hash_log_highlight_messages      4032101240ul /* "log-highlight-messages" */
523 #define hash_log_max_lines               2868344173ul /* "log-max-lines" */
524 #define hash_log_messages                2291744899ul /* "log-messages" */
525 #define hash_show_on_task_bar             215410365ul /* "show-on-task-bar" */
526
527
528 static void savearg(char *command, char *argument, struct configuration_spec * config);
529
530 /*********************************************************************
531  *
532  * Function    :  unload_configfile
533  *
534  * Description :  Free the config structure and all components.
535  *
536  * Parameters  :
537  *          1  :  data: struct configuration_spec to unload
538  *
539  * Returns     :  N/A
540  *
541  *********************************************************************/
542 void unload_configfile (void * data)
543 {
544    struct configuration_spec * config = (struct configuration_spec *)data;
545    struct forward_spec *cur_fwd = config->forward;
546    int i;
547
548 #ifdef FEATURE_ACL
549    struct access_control_list *cur_acl = config->acl;
550
551    while (cur_acl != NULL)
552    {
553       struct access_control_list * next_acl = cur_acl->next;
554       free(cur_acl);
555       cur_acl = next_acl;
556    }
557    config->acl = NULL;
558 #endif /* def FEATURE_ACL */
559
560    while (cur_fwd != NULL)
561    {
562       struct forward_spec * next_fwd = cur_fwd->next;
563       free_url_spec(cur_fwd->url);
564
565       freez(cur_fwd->gateway_host);
566       freez(cur_fwd->forward_host);
567       free(cur_fwd);
568       cur_fwd = next_fwd;
569    }
570    config->forward = NULL;
571
572 #ifdef FEATURE_COOKIE_JAR
573    if ( NULL != config->jar )
574    {
575       fclose( config->jar );
576       config->jar = NULL;
577    }
578 #endif /* def FEATURE_COOKIE_JAR */
579
580    freez(config->confdir);
581    freez(config->logdir);
582    freez(config->templdir);
583
584    freez(config->haddr);
585    freez(config->logfile);
586
587    for (i = 0; i < MAX_AF_FILES; i++)
588    {
589       freez(config->actions_file_short[i]);
590       freez(config->actions_file[i]);
591       freez(config->re_filterfile_short[i]);
592       freez(config->re_filterfile[i]);
593    }
594
595    freez(config->admin_address);
596    freez(config->proxy_info_url);
597    freez(config->proxy_args);
598    freez(config->usermanual);
599
600 #ifdef FEATURE_COOKIE_JAR
601    freez(config->jarfile);
602 #endif /* def FEATURE_COOKIE_JAR */
603
604 #ifdef FEATURE_TRUST
605    freez(config->trustfile);
606    list_remove_all(config->trust_info);
607 #endif /* def FEATURE_TRUST */
608
609    for (i = 0; i < MAX_AF_FILES; i++)
610    {
611       freez(config->re_filterfile[i]);
612    }
613
614    freez(config);
615 }
616
617
618 #ifdef FEATURE_GRACEFUL_TERMINATION
619 /*********************************************************************
620  *
621  * Function    :  unload_current_config_file
622  *
623  * Description :  Unloads current config file - reset to state at
624  *                beginning of program.
625  *
626  * Parameters  :  None
627  *
628  * Returns     :  N/A
629  *
630  *********************************************************************/
631 void unload_current_config_file(void)
632 {
633    if (current_configfile)
634    {
635       current_configfile->unloader = unload_configfile;
636       current_configfile = NULL;
637    }
638 }
639 #endif
640
641
642 /*********************************************************************
643  *
644  * Function    :  load_config
645  *
646  * Description :  Load the config file and all parameters.
647  *
648  * Parameters  :  None
649  *
650  * Returns     :  The configuration_spec, or NULL on error.
651  *
652  *********************************************************************/
653 struct configuration_spec * load_config(void)
654 {
655    char buf[BUFFER_SIZE];
656    char *p, *q;
657    FILE *configfp = NULL;
658    struct configuration_spec * config = NULL;
659    struct client_state * fake_csp;
660    struct file_list *fs;
661    unsigned long linenum = 0;
662    int i;
663
664    if ( !check_file_changed(current_configfile, configfile, &fs))
665    {
666       /* No need to load */
667       return ((struct configuration_spec *)current_configfile->f);
668    }
669    if (!fs)
670    {
671       log_error(LOG_LEVEL_FATAL, "can't check configuration file '%s':  %E",
672                 configfile);
673    }
674
675    if (NULL != current_configfile)
676    {
677       log_error(LOG_LEVEL_INFO, "Reloading configuration file '%s'", configfile);
678    }
679
680 #ifdef FEATURE_TOGGLE
681    global_toggle_state      = 1;
682 #endif /* def FEATURE_TOGGLE */
683
684    fs->f = config = (struct configuration_spec *)zalloc(sizeof(*config));
685
686    if (config==NULL)
687    {
688       freez(fs->filename);
689       freez(fs);
690       log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
691       /* Never get here - LOG_LEVEL_FATAL causes program exit */
692    }
693
694    /*
695     * This is backwards from how it's usually done.
696     * Following the usual pattern, "fs" would be stored in a member
697     * variable in "csp", and then we'd access "config" from "fs->f",
698     * using a cast.  However, "config" is used so often that a
699     * cast each time would be very ugly, and the extra indirection
700     * would waste CPU cycles.  Therefore we store "config" in
701     * "csp->config", and "fs" in "csp->config->config_file_list".
702     */
703    config->config_file_list = fs;
704
705    /*
706     * Set to defaults
707     */
708    config->multi_threaded            = 1;
709    config->hport                     = HADDR_PORT;
710    config->buffer_limit              = 4096 * 1024;
711    config->usermanual                = strdup(USER_MANUAL_URL);
712    config->proxy_args                = strdup("");
713    config->forwarded_connect_retries = 0;
714    config->feature_flags            &= ~RUNTIME_FEATURE_CGI_TOGGLE;
715    config->feature_flags            &= ~RUNTIME_FEATURE_SPLIT_LARGE_FORMS;
716    config->feature_flags            &= ~RUNTIME_FEATURE_ACCEPT_INTERCEPTED_REQUESTS;
717
718    if ((configfp = fopen(configfile, "r")) == NULL)
719    {
720       log_error(LOG_LEVEL_FATAL, "can't open configuration file '%s':  %E",
721               configfile);
722       /* Never get here - LOG_LEVEL_FATAL causes program exit */
723    }
724
725    while (read_config_line(buf, sizeof(buf), configfp, &linenum) != NULL)
726    {
727       char cmd[BUFFER_SIZE];
728       char arg[BUFFER_SIZE];
729       char tmp[BUFFER_SIZE];
730 #ifdef FEATURE_ACL
731       struct access_control_list *cur_acl;
732 #endif /* def FEATURE_ACL */
733       struct forward_spec *cur_fwd;
734       int vec_count;
735       char *vec[3];
736
737       strlcpy(tmp, buf, sizeof(tmp));
738
739       /* Copy command (i.e. up to space or tab) into cmd */
740       p = buf;
741       q = cmd;
742       while (*p && (*p != ' ') && (*p != '\t'))
743       {
744          *q++ = *p++;
745       }
746       *q = '\0';
747
748       /* Skip over the whitespace in buf */
749       while (*p && ((*p == ' ') || (*p == '\t')))
750       {
751          p++;
752       }
753
754       /* Copy the argument into arg */
755       strlcpy(arg, p, sizeof(arg));
756
757       /* Should never happen, but check this anyway */
758       if (*cmd == '\0')
759       {
760          continue;
761       }
762
763       /* Make sure the command field is lower case */
764       for (p=cmd; *p; p++)
765       {
766          if (ijb_isupper(*p))
767          {
768             *p = (char)ijb_tolower(*p);
769          }
770       }
771
772       /* Save the argument for show-proxy-args */
773       savearg(cmd, arg, config);
774
775
776       switch( hash_string( cmd ) )
777       {
778 /* *************************************************************************
779  * actionsfile actions-file-name
780  * In confdir by default
781  * *************************************************************************/
782          case hash_actions_file :
783             i = 0;
784             while ((i < MAX_AF_FILES) && (NULL != config->actions_file[i]))
785             {
786                i++;
787             }
788
789             if (i >= MAX_AF_FILES)
790             {
791                log_error(LOG_LEVEL_FATAL, "Too many 'actionsfile' directives in config file - limit is %d.\n"
792                   "(You can increase this limit by changing MAX_AF_FILES in project.h and recompiling).",
793                   MAX_AF_FILES);
794             }
795             config->actions_file_short[i] = strdup(arg);
796             config->actions_file[i] = make_path(config->confdir, arg);
797
798             continue;
799 /* *************************************************************************
800  * accept-intercepted-requests
801  * *************************************************************************/
802          case hash_accept_intercepted_requests:
803             if ((*arg != '\0') && (0 != atoi(arg)))
804             {
805                config->feature_flags |= RUNTIME_FEATURE_ACCEPT_INTERCEPTED_REQUESTS;
806             }
807             else
808             {
809                config->feature_flags &= ~RUNTIME_FEATURE_ACCEPT_INTERCEPTED_REQUESTS;
810             }
811             continue;
812
813 /* *************************************************************************
814  * admin-address email-address
815  * *************************************************************************/
816          case hash_admin_address :
817             freez(config->admin_address);
818             config->admin_address = strdup(arg);
819             continue;
820
821 /* *************************************************************************
822  * buffer-limit n
823  * *************************************************************************/
824          case hash_buffer_limit :
825             config->buffer_limit = (size_t)(1024 * atoi(arg));
826             continue;
827
828 /* *************************************************************************
829  * confdir directory-name
830  * *************************************************************************/
831          case hash_confdir :
832             freez(config->confdir);
833             config->confdir = make_path( NULL, arg);
834             continue;
835
836 /* *************************************************************************
837  * debug n
838  * Specifies debug level, multiple values are ORed together.
839  * *************************************************************************/
840          case hash_debug :
841             config->debug |= atoi(arg);
842             continue;
843
844 /* *************************************************************************
845  * deny-access source-ip[/significant-bits] [dest-ip[/significant-bits]]
846  * *************************************************************************/
847 #ifdef FEATURE_ACL
848          case hash_deny_access:
849             vec_count = ssplit(arg, " \t", vec, SZ(vec), 1, 1);
850
851             if ((vec_count != 1) && (vec_count != 2))
852             {
853                log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
854                      "deny-access directive in configuration file.");
855                string_append(&config->proxy_args,
856                   "<br>\nWARNING: Wrong number of parameters for "
857                   "deny-access directive in configuration file.<br><br>\n");
858                continue;
859             }
860
861             /* allocate a new node */
862             cur_acl = (struct access_control_list *) zalloc(sizeof(*cur_acl));
863
864             if (cur_acl == NULL)
865             {
866                log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
867                /* Never get here - LOG_LEVEL_FATAL causes program exit */
868                continue;
869             }
870             cur_acl->action = ACL_DENY;
871
872             if (acl_addr(vec[0], cur_acl->src) < 0)
873             {
874                log_error(LOG_LEVEL_ERROR, "Invalid source address, port or netmask "
875                   "for deny-access directive in configuration file: \"%s\"", vec[0]);
876                string_append(&config->proxy_args,
877                   "<br>\nWARNING: Invalid source address, port or netmask "
878                   "for deny-access directive in configuration file: \"");
879                string_append(&config->proxy_args,
880                   vec[0]);
881                string_append(&config->proxy_args,
882                   "\"<br><br>\n");
883                freez(cur_acl);
884                continue;
885             }
886             if (vec_count == 2)
887             {
888                if (acl_addr(vec[1], cur_acl->dst) < 0)
889                {
890                   log_error(LOG_LEVEL_ERROR, "Invalid destination address, port or netmask "
891                      "for deny-access directive in configuration file: \"%s\"", vec[1]);
892                   string_append(&config->proxy_args,
893                      "<br>\nWARNING: Invalid destination address, port or netmask "
894                      "for deny-access directive in configuration file: \"");
895                   string_append(&config->proxy_args,
896                      vec[1]);
897                   string_append(&config->proxy_args,
898                      "\"<br><br>\n");
899                   freez(cur_acl);
900                   continue;
901                }
902             }
903
904             /*
905              * Add it to the list.  Note we reverse the list to get the
906              * behaviour the user expects.  With both the ACL and
907              * actions file, the last match wins.  However, the internal
908              * implementations are different:  The actions file is stored
909              * in the same order as the file, and scanned completely.
910              * With the ACL, we reverse the order as we load it, then
911              * when we scan it we stop as soon as we get a match.
912              */
913             cur_acl->next  = config->acl;
914             config->acl = cur_acl;
915
916             continue;
917 #endif /* def FEATURE_ACL */
918
919 /* *************************************************************************
920  * enable-edit-actions 0|1
921  * *************************************************************************/
922 #ifdef FEATURE_CGI_EDIT_ACTIONS
923          case hash_enable_edit_actions:
924             if ((*arg != '\0') && (0 != atoi(arg)))
925             {
926                config->feature_flags |= RUNTIME_FEATURE_CGI_EDIT_ACTIONS;
927             }
928             else
929             {
930                config->feature_flags &= ~RUNTIME_FEATURE_CGI_EDIT_ACTIONS;
931             }
932             continue;
933 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
934
935 /* *************************************************************************
936  * enable-remote-toggle 0|1
937  * *************************************************************************/
938 #ifdef FEATURE_CGI_EDIT_ACTIONS
939          case hash_enable_remote_toggle:
940             if ((*arg != '\0') && (0 != atoi(arg)))
941             {
942                config->feature_flags |= RUNTIME_FEATURE_CGI_TOGGLE;
943             }
944             else
945             {
946                config->feature_flags &= ~RUNTIME_FEATURE_CGI_TOGGLE;
947             }
948             continue;
949 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
950
951 /* *************************************************************************
952  * enable-remote-http-toggle 0|1
953  * *************************************************************************/
954          case hash_enable_remote_http_toggle:
955             if ((*arg != '\0') && (0 != atoi(arg)))
956             {
957                config->feature_flags |= RUNTIME_FEATURE_HTTP_TOGGLE;
958             }
959             else
960             {
961                config->feature_flags &= ~RUNTIME_FEATURE_HTTP_TOGGLE;
962             }
963             continue;
964
965 /* *************************************************************************
966  * hash_enforce_blocks 0|1
967  * *************************************************************************/
968 #ifdef FEATURE_FORCE_LOAD
969          case hash_enforce_blocks:
970             if ((*arg != '\0') && (0 != atoi(arg)))
971             {
972                config->feature_flags |= RUNTIME_FEATURE_ENFORCE_BLOCKS;
973             }
974             else
975             {
976                config->feature_flags &= ~RUNTIME_FEATURE_ENFORCE_BLOCKS;
977             }
978             continue;
979 #endif /* def FEATURE_FORCE_LOAD */
980
981 /* *************************************************************************
982  * filterfile file-name
983  * In confdir by default.
984  * *************************************************************************/
985          case hash_filterfile :
986             i = 0;
987             while ((i < MAX_AF_FILES) && (NULL != config->re_filterfile[i]))
988             {
989                i++;
990             }
991
992             if (i >= MAX_AF_FILES)
993             {
994                log_error(LOG_LEVEL_FATAL, "Too many 'filterfile' directives in config file - limit is %d.\n"
995                   "(You can increase this limit by changing MAX_AF_FILES in project.h and recompiling).",
996                   MAX_AF_FILES);
997             }
998             config->re_filterfile_short[i] = strdup(arg);
999             config->re_filterfile[i] = make_path(config->confdir, arg);
1000
1001             continue;
1002
1003 /* *************************************************************************
1004  * forward url-pattern (.|http-proxy-host[:port])
1005  * *************************************************************************/
1006          case hash_forward:
1007             vec_count = ssplit(arg, " \t", vec, SZ(vec), 1, 1);
1008
1009             if (vec_count != 2)
1010             {
1011                log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for forward "
1012                      "directive in configuration file.");
1013                string_append(&config->proxy_args,
1014                   "<br>\nWARNING: Wrong number of parameters for "
1015                   "forward directive in configuration file.");
1016                continue;
1017             }
1018
1019             /* allocate a new node */
1020             cur_fwd = zalloc(sizeof(*cur_fwd));
1021             if (cur_fwd == NULL)
1022             {
1023                log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
1024                /* Never get here - LOG_LEVEL_FATAL causes program exit */
1025                continue;
1026             }
1027
1028             cur_fwd->type = SOCKS_NONE;
1029
1030             /* Save the URL pattern */
1031             if (create_url_spec(cur_fwd->url, vec[0]))
1032             {
1033                log_error(LOG_LEVEL_ERROR, "Bad URL specifier for forward "
1034                      "directive in configuration file.");
1035                string_append(&config->proxy_args,
1036                   "<br>\nWARNING: Bad URL specifier for "
1037                   "forward directive in configuration file.");
1038                continue;
1039             }
1040
1041             /* Parse the parent HTTP proxy host:port */
1042             p = vec[1];
1043
1044             if (strcmp(p, ".") != 0)
1045             {
1046                cur_fwd->forward_host = strdup(p);
1047
1048                if (NULL != (p = strchr(cur_fwd->forward_host, ':')))
1049                {
1050                   *p++ = '\0';
1051                   cur_fwd->forward_port = atoi(p);
1052                }
1053
1054                if (cur_fwd->forward_port <= 0)
1055                {
1056                   cur_fwd->forward_port = 8000;
1057                }
1058             }
1059
1060             /* Add to list. */
1061             cur_fwd->next = config->forward;
1062             config->forward = cur_fwd;
1063
1064             continue;
1065
1066 /* *************************************************************************
1067  * forward-socks4 url-pattern socks-proxy[:port] (.|http-proxy[:port])
1068  * *************************************************************************/
1069          case hash_forward_socks4:
1070             vec_count = ssplit(arg, " \t", vec, SZ(vec), 1, 1);
1071
1072             if (vec_count != 3)
1073             {
1074                log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
1075                      "forward-socks4 directive in configuration file.");
1076                string_append(&config->proxy_args,
1077                   "<br>\nWARNING: Wrong number of parameters for "
1078                   "forward-socks4 directive in configuration file.");
1079                continue;
1080             }
1081
1082             /* allocate a new node */
1083             cur_fwd = zalloc(sizeof(*cur_fwd));
1084             if (cur_fwd == NULL)
1085             {
1086                log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
1087                /* Never get here - LOG_LEVEL_FATAL causes program exit */
1088                continue;
1089             }
1090
1091             cur_fwd->type = SOCKS_4;
1092
1093             /* Save the URL pattern */
1094             if (create_url_spec(cur_fwd->url, vec[0]))
1095             {
1096                log_error(LOG_LEVEL_ERROR, "Bad URL specifier for forward-socks4 "
1097                      "directive in configuration file.");
1098                string_append(&config->proxy_args,
1099                   "<br>\nWARNING: Bad URL specifier for "
1100                   "forward-socks4 directive in configuration file.");
1101                continue;
1102             }
1103
1104             /* Parse the SOCKS proxy host[:port] */
1105             p = vec[1];
1106
1107             if (strcmp(p, ".") != 0)
1108             {
1109                cur_fwd->gateway_host = strdup(p);
1110
1111                if (NULL != (p = strchr(cur_fwd->gateway_host, ':')))
1112                {
1113                   *p++ = '\0';
1114                   cur_fwd->gateway_port = atoi(p);
1115                }
1116                if (cur_fwd->gateway_port <= 0)
1117                {
1118                   cur_fwd->gateway_port = 1080;
1119                }
1120             }
1121
1122             /* Parse the parent HTTP proxy host[:port] */
1123             p = vec[2];
1124
1125             if (strcmp(p, ".") != 0)
1126             {
1127                cur_fwd->forward_host = strdup(p);
1128
1129                if (NULL != (p = strchr(cur_fwd->forward_host, ':')))
1130                {
1131                   *p++ = '\0';
1132                   cur_fwd->forward_port = atoi(p);
1133                }
1134
1135                if (cur_fwd->forward_port <= 0)
1136                {
1137                   cur_fwd->forward_port = 8000;
1138                }
1139             }
1140
1141             /* Add to list. */
1142             cur_fwd->next = config->forward;
1143             config->forward = cur_fwd;
1144
1145             continue;
1146
1147 /* *************************************************************************
1148  * forward-socks4a url-pattern socks-proxy[:port] (.|http-proxy[:port])
1149  * *************************************************************************/
1150          case hash_forward_socks4a:
1151             vec_count = ssplit(arg, " \t", vec, SZ(vec), 1, 1);
1152
1153             if (vec_count != 3)
1154             {
1155                log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
1156                      "forward-socks4a directive in configuration file.");
1157                string_append(&config->proxy_args,
1158                   "<br>\nWARNING: Wrong number of parameters for "
1159                   "forward-socks4a directive in configuration file.");
1160                continue;
1161             }
1162
1163             /* allocate a new node */
1164             cur_fwd = zalloc(sizeof(*cur_fwd));
1165             if (cur_fwd == NULL)
1166             {
1167                log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
1168                /* Never get here - LOG_LEVEL_FATAL causes program exit */
1169                continue;
1170             }
1171
1172             cur_fwd->type = SOCKS_4A;
1173
1174             /* Save the URL pattern */
1175             if (create_url_spec(cur_fwd->url, vec[0]))
1176             {
1177                log_error(LOG_LEVEL_ERROR, "Bad URL specifier for forward-socks4a "
1178                      "directive in configuration file.");
1179                string_append(&config->proxy_args,
1180                   "<br>\nWARNING: Bad URL specifier for "
1181                   "forward-socks4a directive in configuration file.");
1182                continue;
1183             }
1184
1185             /* Parse the SOCKS proxy host[:port] */
1186             p = vec[1];
1187
1188             cur_fwd->gateway_host = strdup(p);
1189
1190             if (NULL != (p = strchr(cur_fwd->gateway_host, ':')))
1191             {
1192                *p++ = '\0';
1193                cur_fwd->gateway_port = atoi(p);
1194             }
1195             if (cur_fwd->gateway_port <= 0)
1196             {
1197                cur_fwd->gateway_port = 1080;
1198             }
1199
1200             /* Parse the parent HTTP proxy host[:port] */
1201             p = vec[2];
1202
1203             if (strcmp(p, ".") != 0)
1204             {
1205                cur_fwd->forward_host = strdup(p);
1206
1207                if (NULL != (p = strchr(cur_fwd->forward_host, ':')))
1208                {
1209                   *p++ = '\0';
1210                   cur_fwd->forward_port = atoi(p);
1211                }
1212
1213                if (cur_fwd->forward_port <= 0)
1214                {
1215                   cur_fwd->forward_port = 8000;
1216                }
1217             }
1218
1219             /* Add to list. */
1220             cur_fwd->next = config->forward;
1221             config->forward = cur_fwd;
1222
1223             continue;
1224
1225 /* *************************************************************************
1226  * forwarded-connect-retries n
1227  * *************************************************************************/
1228          case hash_forwarded_connect_retries :
1229             config->forwarded_connect_retries = atoi(arg);
1230             continue;
1231
1232 /* *************************************************************************
1233  * jarfile jar-file-name
1234  * In logdir by default
1235  * *************************************************************************/
1236 #ifdef FEATURE_COOKIE_JAR
1237          case hash_jarfile :
1238             freez(config->jarfile);
1239             config->jarfile = make_path(config->logdir, arg);
1240             continue;
1241 #endif /* def FEATURE_COOKIE_JAR */
1242
1243 /* *************************************************************************
1244  * listen-address [ip][:port]
1245  * *************************************************************************/
1246          case hash_listen_address :
1247             freez(config->haddr);
1248             config->haddr = strdup(arg);
1249             continue;
1250
1251 /* *************************************************************************
1252  * logdir directory-name
1253  * *************************************************************************/
1254          case hash_logdir :
1255             freez(config->logdir);
1256             config->logdir = make_path(NULL, arg);
1257             continue;
1258
1259 /* *************************************************************************
1260  * logfile log-file-name
1261  * In logdir by default
1262  * *************************************************************************/
1263          case hash_logfile :
1264             freez(config->logfile);
1265             config->logfile = no_daemon ? NULL : make_path(config->logdir, arg);
1266             continue;
1267
1268 /* *************************************************************************
1269  * permit-access source-ip[/significant-bits] [dest-ip[/significant-bits]]
1270  * *************************************************************************/
1271 #ifdef FEATURE_ACL
1272          case hash_permit_access:
1273             vec_count = ssplit(arg, " \t", vec, SZ(vec), 1, 1);
1274
1275             if ((vec_count != 1) && (vec_count != 2))
1276             {
1277                log_error(LOG_LEVEL_ERROR, "Wrong number of parameters for "
1278                      "permit-access directive in configuration file.");
1279                string_append(&config->proxy_args,
1280                   "<br>\nWARNING: Wrong number of parameters for "
1281                   "permit-access directive in configuration file.<br><br>\n");
1282
1283                continue;
1284             }
1285
1286             /* allocate a new node */
1287             cur_acl = (struct access_control_list *) zalloc(sizeof(*cur_acl));
1288
1289             if (cur_acl == NULL)
1290             {
1291                log_error(LOG_LEVEL_FATAL, "can't allocate memory for configuration");
1292                /* Never get here - LOG_LEVEL_FATAL causes program exit */
1293                continue;
1294             }
1295             cur_acl->action = ACL_PERMIT;
1296
1297             if (acl_addr(vec[0], cur_acl->src) < 0)
1298             {
1299                log_error(LOG_LEVEL_ERROR, "Invalid source address, port or netmask "
1300                   "for permit-access directive in configuration file: \"%s\"", vec[0]);
1301                string_append(&config->proxy_args,
1302                   "<br>\nWARNING: Invalid source address, port or netmask for "
1303                   "permit-access directive in configuration file: \"");
1304                string_append(&config->proxy_args,
1305                   vec[0]);
1306                string_append(&config->proxy_args,
1307                   "\"<br><br>\n");
1308                freez(cur_acl);
1309                continue;
1310             }
1311             if (vec_count == 2)
1312             {
1313                if (acl_addr(vec[1], cur_acl->dst) < 0)
1314                {
1315                   log_error(LOG_LEVEL_ERROR, "Invalid destination address, port or netmask "
1316                      "for permit-access directive in configuration file: \"%s\"", vec[1]);
1317                   string_append(&config->proxy_args,
1318                      "<br>\nWARNING: Invalid destination address, port or netmask for "
1319                      "permit-access directive in configuration file: \"");
1320                   string_append(&config->proxy_args,
1321                      vec[1]);
1322                   string_append(&config->proxy_args,
1323                      "\"<br><br>\n");
1324                   freez(cur_acl);
1325                   continue;
1326                }
1327             }
1328
1329             /*
1330              * Add it to the list.  Note we reverse the list to get the
1331              * behaviour the user expects.  With both the ACL and
1332              * actions file, the last match wins.  However, the internal
1333              * implementations are different:  The actions file is stored
1334              * in the same order as the file, and scanned completely.
1335              * With the ACL, we reverse the order as we load it, then
1336              * when we scan it we stop as soon as we get a match.
1337              */
1338             cur_acl->next  = config->acl;
1339             config->acl = cur_acl;
1340
1341             continue;
1342 #endif /* def FEATURE_ACL */
1343
1344 /* *************************************************************************
1345  * proxy-info-url url
1346  * *************************************************************************/
1347          case hash_proxy_info_url :
1348             freez(config->proxy_info_url);
1349             config->proxy_info_url = strdup(arg);
1350             continue;
1351
1352 /* *************************************************************************
1353  * single-threaded
1354  * *************************************************************************/
1355          case hash_single_threaded :
1356             config->multi_threaded = 0;
1357             continue;
1358
1359 /* *************************************************************************
1360  * split-large-cgi-forms
1361  * *************************************************************************/
1362          case hash_split_large_cgi_forms :
1363             if ((*arg != '\0') && (0 != atoi(arg)))
1364             {
1365                config->feature_flags |= RUNTIME_FEATURE_SPLIT_LARGE_FORMS;
1366             }
1367             else
1368             {
1369                config->feature_flags &= ~RUNTIME_FEATURE_SPLIT_LARGE_FORMS;
1370             }
1371             continue;
1372
1373 /* *************************************************************************
1374  * templdir directory-name
1375  * *************************************************************************/
1376          case hash_templdir :
1377             freez(config->templdir);
1378             config->templdir = make_path(NULL, arg);
1379             continue;
1380
1381 /* *************************************************************************
1382  * toggle (0|1)
1383  * *************************************************************************/
1384 #ifdef FEATURE_TOGGLE
1385          case hash_toggle :
1386             global_toggle_state = atoi(arg);
1387             continue;
1388 #endif /* def FEATURE_TOGGLE */
1389
1390 /* *************************************************************************
1391  * trust-info-url url
1392  * *************************************************************************/
1393 #ifdef FEATURE_TRUST
1394          case hash_trust_info_url :
1395             enlist(config->trust_info, arg);
1396             continue;
1397 #endif /* def FEATURE_TRUST */
1398
1399 /* *************************************************************************
1400  * trustfile filename
1401  * (In confdir by default.)
1402  * *************************************************************************/
1403 #ifdef FEATURE_TRUST
1404          case hash_trustfile :
1405             freez(config->trustfile);
1406             config->trustfile = make_path(config->confdir, arg);
1407             continue;
1408 #endif /* def FEATURE_TRUST */
1409
1410 /* *************************************************************************
1411  * usermanual url
1412  * *************************************************************************/
1413          case hash_usermanual :
1414             freez(config->usermanual);
1415             config->usermanual = strdup(arg);
1416             continue;
1417
1418 /* *************************************************************************
1419  * Win32 Console options:
1420  * *************************************************************************/
1421
1422 /* *************************************************************************
1423  * hide-console
1424  * *************************************************************************/
1425 #ifdef _WIN_CONSOLE
1426          case hash_hide_console :
1427             hideConsole = 1;
1428             continue;
1429 #endif /*def _WIN_CONSOLE*/
1430
1431
1432 /* *************************************************************************
1433  * Win32 GUI options:
1434  * *************************************************************************/
1435
1436 #if defined(_WIN32) && ! defined(_WIN_CONSOLE)
1437 /* *************************************************************************
1438  * activity-animation (0|1)
1439  * *************************************************************************/
1440          case hash_activity_animation :
1441             g_bShowActivityAnimation = atoi(arg);
1442             continue;
1443
1444 /* *************************************************************************
1445  *  close-button-minimizes (0|1)
1446  * *************************************************************************/
1447          case hash_close_button_minimizes :
1448             g_bCloseHidesWindow = atoi(arg);
1449             continue;
1450
1451 /* *************************************************************************
1452  * log-buffer-size (0|1)
1453  * *************************************************************************/
1454          case hash_log_buffer_size :
1455             g_bLimitBufferSize = atoi(arg);
1456             continue;
1457
1458 /* *************************************************************************
1459  * log-font-name fontnane
1460  * *************************************************************************/
1461          case hash_log_font_name :
1462             strcpy( g_szFontFaceName, arg );
1463             continue;
1464
1465 /* *************************************************************************
1466  * log-font-size n
1467  * *************************************************************************/
1468          case hash_log_font_size :
1469             g_nFontSize = atoi(arg);
1470             continue;
1471
1472 /* *************************************************************************
1473  * log-highlight-messages (0|1)
1474  * *************************************************************************/
1475          case hash_log_highlight_messages :
1476             g_bHighlightMessages = atoi(arg);
1477             continue;
1478
1479 /* *************************************************************************
1480  * log-max-lines n
1481  * *************************************************************************/
1482          case hash_log_max_lines :
1483             g_nMaxBufferLines = atoi(arg);
1484             continue;
1485
1486 /* *************************************************************************
1487  * log-messages (0|1)
1488  * *************************************************************************/
1489          case hash_log_messages :
1490             g_bLogMessages = atoi(arg);
1491             continue;
1492
1493 /* *************************************************************************
1494  * show-on-task-bar (0|1)
1495  * *************************************************************************/
1496          case hash_show_on_task_bar :
1497             g_bShowOnTaskBar = atoi(arg);
1498             continue;
1499
1500 #endif /* defined(_WIN32) && ! defined(_WIN_CONSOLE) */
1501
1502
1503 /* *************************************************************************
1504  * Warnings about unsupported features
1505  * *************************************************************************/
1506 #ifndef FEATURE_ACL
1507          case hash_deny_access:
1508 #endif /* ndef FEATURE_ACL */
1509 #ifndef FEATURE_CGI_EDIT_ACTIONS
1510          case hash_enable_edit_actions:
1511          case hash_enable_remote_toggle:
1512 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
1513 #ifndef FEATURE_COOKIE_JAR
1514          case hash_jarfile :
1515 #endif /* ndef FEATURE_COOKIE_JAR */
1516 #ifndef FEATURE_ACL
1517          case hash_permit_access:
1518 #endif /* ndef FEATURE_ACL */
1519 #ifndef FEATURE_TOGGLE
1520          case hash_toggle :
1521 #endif /* ndef FEATURE_TOGGLE */
1522 #ifndef FEATURE_TRUST
1523          case hash_trustfile :
1524          case hash_trust_info_url :
1525 #endif /* ndef FEATURE_TRUST */
1526
1527 #ifndef _WIN_CONSOLE
1528          case hash_hide_console :
1529 #endif /* ndef _WIN_CONSOLE */
1530
1531 #if defined(_WIN_CONSOLE) || ! defined(_WIN32)
1532          case hash_activity_animation :
1533          case hash_close_button_minimizes :
1534          case hash_log_buffer_size :
1535          case hash_log_font_name :
1536          case hash_log_font_size :
1537          case hash_log_highlight_messages :
1538          case hash_log_max_lines :
1539          case hash_log_messages :
1540          case hash_show_on_task_bar :
1541 #endif /* defined(_WIN_CONSOLE) || ! defined(_WIN32) */
1542             /* These warnings are annoying - so hide them. -- Jon */
1543             /* log_error(LOG_LEVEL_INFO, "Unsupported directive \"%s\" ignored.", cmd); */
1544             continue;
1545
1546 /* *************************************************************************/
1547          default :
1548 /* *************************************************************************/
1549             /*
1550              * I decided that I liked this better as a warning than an
1551              * error.  To change back to an error, just change log level
1552              * to LOG_LEVEL_FATAL.
1553              */
1554             log_error(LOG_LEVEL_ERROR, "Ignoring unrecognized directive '%s' (%luul) in line %lu "
1555                   "in configuration file (%s).",  buf, hash_string(cmd), linenum, configfile);
1556             string_append(&config->proxy_args,
1557                " <b><font color=\"red\">WARNING: unrecognized directive, ignored</font></b>");
1558             continue;
1559
1560 /* *************************************************************************/
1561       } /* end switch( hash_string(cmd) ) */
1562    } /* end while ( read_config_line(...) ) */
1563
1564    fclose(configfp);
1565
1566    if (NULL == config->proxy_args)
1567    {
1568       log_error(LOG_LEVEL_FATAL, "Out of memory loading config - insufficient memory for config->proxy_args");
1569    }
1570
1571    init_error_log(Argv[0], config->logfile, config->debug);
1572
1573    if (config->actions_file[0])
1574    {
1575       add_loader(load_actions_file, config);
1576    }
1577
1578    if (config->re_filterfile)
1579    {
1580       add_loader(load_re_filterfile, config);
1581    }
1582
1583 #ifdef FEATURE_TRUST
1584    if (config->trustfile)
1585    {
1586       add_loader(load_trustfile, config);
1587    }
1588 #endif /* def FEATURE_TRUST */
1589
1590 #ifdef FEATURE_COOKIE_JAR
1591    if ( NULL != config->jarfile )
1592    {
1593       if ( NULL == (config->jar = fopen(config->jarfile, "a")) )
1594       {
1595          log_error(LOG_LEVEL_FATAL, "can't open jarfile '%s': %E", config->jarfile);
1596          /* Never get here - LOG_LEVEL_FATAL causes program exit */
1597       }
1598       setbuf(config->jar, NULL);
1599    }
1600 #endif /* def FEATURE_COOKIE_JAR */
1601
1602    if ( NULL == config->haddr )
1603    {
1604       config->haddr = strdup( HADDR_DEFAULT );
1605    }
1606
1607    if ( NULL != config->haddr )
1608    {
1609       if (NULL != (p = strchr(config->haddr, ':')))
1610       {
1611          *p++ = '\0';
1612          if (*p)
1613          {
1614             config->hport = atoi(p);
1615          }
1616       }
1617
1618       if (config->hport <= 0)
1619       {
1620          *--p = ':';
1621          log_error(LOG_LEVEL_FATAL, "invalid bind port spec %s", config->haddr);
1622          /* Never get here - LOG_LEVEL_FATAL causes program exit */
1623       }
1624       if (*config->haddr == '\0')
1625       {
1626          config->haddr = NULL;
1627       }
1628    }
1629
1630    /*
1631     * Want to run all the loaders once now.
1632     *
1633     * Need to set up a fake csp, so they can get to the config.
1634     */
1635    fake_csp = (struct client_state *) zalloc (sizeof(*fake_csp));
1636    fake_csp->config = config;
1637
1638    if (run_loader(fake_csp))
1639    {
1640       freez(fake_csp);
1641       log_error(LOG_LEVEL_FATAL, "A loader failed while loading config file. Exiting.");
1642       /* Never get here - LOG_LEVEL_FATAL causes program exit */
1643    }
1644    freez(fake_csp);
1645
1646 /* FIXME: this is a kludge for win32 */
1647 #if defined(_WIN32) && !defined (_WIN_CONSOLE)
1648
1649    g_default_actions_file  = config->actions_file[1]; /* FIXME Hope this is default.action */
1650    g_user_actions_file = config->actions_file[2]; /* FIXME Hope this is user.action */
1651    g_re_filterfile    = config->re_filterfile[0]; /* FIXME Hope this is default.filter */
1652
1653 #ifdef FEATURE_TRUST
1654    g_trustfile        = config->trustfile;
1655 #endif /* def FEATURE_TRUST */
1656
1657
1658 #endif /* defined(_WIN32) && !defined (_WIN_CONSOLE) */
1659 /* FIXME: end kludge */
1660
1661
1662    config->need_bind = 1;
1663
1664    if (current_configfile)
1665    {
1666       struct configuration_spec * oldcfg = (struct configuration_spec *)
1667                                            current_configfile->f;
1668       /*
1669        * Check if config->haddr,hport == oldcfg->haddr,hport
1670        *
1671        * The following could be written more compactly as a single,
1672        * (unreadably long) if statement.
1673        */
1674       config->need_bind = 0;
1675       if (config->hport != oldcfg->hport)
1676       {
1677          config->need_bind = 1;
1678       }
1679       else if (config->haddr == NULL)
1680       {
1681          if (oldcfg->haddr != NULL)
1682          {
1683             config->need_bind = 1;
1684          }
1685       }
1686       else if (oldcfg->haddr == NULL)
1687       {
1688          config->need_bind = 1;
1689       }
1690       else if (0 != strcmp(config->haddr, oldcfg->haddr))
1691       {
1692          config->need_bind = 1;
1693       }
1694
1695       current_configfile->unloader = unload_configfile;
1696    }
1697
1698    fs->next = files->next;
1699    files->next = fs;
1700
1701    current_configfile = fs;
1702
1703    return (config);
1704 }
1705
1706
1707 /*********************************************************************
1708  *
1709  * Function    :  savearg
1710  *
1711  * Description :  Called from `load_config'.  It saves each non-empty
1712  *                and non-comment line from config into
1713  *                config->proxy_args.  This is used to create the
1714  *                show-proxy-args page.  On error, frees
1715  *                config->proxy_args and sets it to NULL
1716  *
1717  * Parameters  :
1718  *          1  :  command = config setting that was found
1719  *          2  :  argument = the setting's argument (if any)
1720  *          3  :  config = Configuration to save into.
1721  *
1722  * Returns     :  N/A
1723  *
1724  *********************************************************************/
1725 static void savearg(char *command, char *argument, struct configuration_spec * config)
1726 {
1727    char * buf;
1728    char * s;
1729
1730    assert(command);
1731    assert(*command);
1732    assert(argument);
1733
1734    /*
1735     * Add config option name embedded in
1736     * link to it's section in the user-manual
1737     */
1738    buf = strdup("\n<a href=\"");
1739    if (!strncmpic(config->usermanual, "file://", 7) ||
1740        !strncmpic(config->usermanual, "http", 4))
1741    {
1742       string_append(&buf, config->usermanual);
1743    }
1744    else
1745    {
1746       string_append(&buf, "http://");
1747       string_append(&buf, CGI_SITE_2_HOST);
1748       string_append(&buf, "/user-manual/");
1749    }
1750    string_append(&buf, CONFIG_HELP_PREFIX);
1751    string_join  (&buf, string_toupper(command));
1752    string_append(&buf, "\">");
1753    string_append(&buf, command);
1754    string_append(&buf, "</a> ");
1755
1756    if (NULL == buf)
1757    {
1758       freez(config->proxy_args);
1759       return;
1760    }
1761
1762    if ( (NULL != argument) && ('\0' != *argument) )
1763    {
1764       s = html_encode(argument);
1765       if (NULL == s)
1766       {
1767          freez(buf);
1768          freez(config->proxy_args);
1769          return;
1770       }
1771
1772       if (strncmpic(argument, "http://", 7) == 0)
1773       {
1774          string_append(&buf, "<a href=\"");
1775          string_append(&buf, s);
1776          string_append(&buf, "\">");
1777          string_join  (&buf, s);
1778          string_append(&buf, "</a>");
1779       }
1780       else
1781       {
1782          string_join  (&buf, s);
1783       }
1784    }
1785
1786    string_append(&buf, "<br>");
1787    string_join(&config->proxy_args, buf);
1788 }
1789
1790
1791 /*
1792   Local Variables:
1793   tab-width: 3
1794   end:
1795 */