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