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