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