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