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