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