More Mac OSX support:
[privoxy.git] / loaders.c
1 const char loaders_rcs[] = "$Id: loaders.c,v 1.46 2002/03/24 13:25:43 swa Exp $";
2 /*********************************************************************
3  *
4  * File        :  $Source: /cvsroot/ijbswa/current/loaders.c,v $
5  *
6  * Purpose     :  Functions to load and unload the various
7  *                configuration files.  Also contains code to manage
8  *                the list of active loaders, and to automatically
9  *                unload files that are no longer in use.
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: loaders.c,v $
38  *    Revision 1.46  2002/03/24 13:25:43  swa
39  *    name change related issues
40  *
41  *    Revision 1.45  2002/03/16 23:54:06  jongfoster
42  *    Adding graceful termination feature, to help look for memory leaks.
43  *    If you enable this (which, by design, has to be done by hand
44  *    editing config.h) and then go to http://i.j.b/die, then the program
45  *    will exit cleanly after the *next* request.  It should free all the
46  *    memory that was used.
47  *
48  *    Revision 1.44  2002/03/16 21:51:00  jongfoster
49  *    Fixing free(NULL).
50  *
51  *    Revision 1.43  2002/03/16 20:28:34  oes
52  *    Added descriptions to the filters so users will know what they select in the cgi editor
53  *
54  *    Revision 1.42  2002/03/13 00:27:05  jongfoster
55  *    Killing warnings
56  *
57  *    Revision 1.41  2002/03/12 01:42:50  oes
58  *    Introduced modular filters
59  *
60  *    Revision 1.40  2002/03/08 17:46:04  jongfoster
61  *    Fixing int/size_t warnings
62  *
63  *    Revision 1.39  2002/03/07 03:46:17  oes
64  *    Fixed compiler warnings
65  *
66  *    Revision 1.38  2002/03/06 22:54:35  jongfoster
67  *    Automated function-comment nitpicking.
68  *
69  *    Revision 1.37  2002/03/03 15:07:49  oes
70  *    Re-enabled automatic config reloading
71  *
72  *    Revision 1.36  2002/01/22 23:46:18  jongfoster
73  *    Moving edit_read_line() and simple_read_line() to loaders.c, and
74  *    extending them to support reading MS-DOS, Mac and UNIX style files
75  *    on all platforms.
76  *
77  *    Modifying read_config_line() (without changing it's prototype) to
78  *    be a trivial wrapper for edit_read_line().  This means that we have
79  *    one function to read a line and handle comments, which is common
80  *    between the initialization code and the edit interface.
81  *
82  *    Revision 1.35  2002/01/17 21:03:08  jongfoster
83  *    Moving all our URL and URL pattern parsing code to urlmatch.c.
84  *
85  *    Renaming free_url to free_url_spec, since it frees a struct url_spec.
86  *
87  *    Revision 1.34  2001/12/30 14:07:32  steudten
88  *    - Add signal handling (unix)
89  *    - Add SIGHUP handler (unix)
90  *    - Add creation of pidfile (unix)
91  *    - Add action 'top' in rc file (RH)
92  *    - Add entry 'SIGNALS' to manpage
93  *    - Add exit message to logfile (unix)
94  *
95  *    Revision 1.33  2001/11/13 00:16:38  jongfoster
96  *    Replacing references to malloc.h with the standard stdlib.h
97  *    (See ANSI or K&R 2nd Ed)
98  *
99  *    Revision 1.32  2001/11/07 00:02:13  steudten
100  *    Add line number in error output for lineparsing for
101  *    actionsfile and configfile.
102  *    Special handling for CLF added.
103  *
104  *    Revision 1.31  2001/10/26 17:39:01  oes
105  *    Removed csp->referrer
106  *    Moved ijb_isspace and ijb_tolower to project.h
107  *
108  *    Revision 1.30  2001/10/25 03:40:48  david__schmidt
109  *    Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
110  *    threads to call select() simultaneously.  So, it's time to do a real, live,
111  *    native OS/2 port.  See defines for __EMX__ (the porting layer) vs. __OS2__
112  *    (native). Both versions will work, but using __OS2__ offers multi-threading.
113  *
114  *    Revision 1.29  2001/10/23 21:38:53  jongfoster
115  *    Adding error-checking to create_url_spec()
116  *
117  *    Revision 1.28  2001/10/07 15:40:39  oes
118  *    Replaced 6 boolean members of csp with one bitmap (csp->flags)
119  *
120  *    Revision 1.27  2001/09/22 16:36:59  jongfoster
121  *    Removing unused parameter fs from read_config_line()
122  *
123  *    Revision 1.26  2001/09/22 14:05:22  jongfoster
124  *    Bugfix: Multiple escaped "#" characters in a configuration
125  *    file are now permitted.
126  *    Also removing 3 unused headers.
127  *
128  *    Revision 1.25  2001/09/13 22:44:03  jongfoster
129  *    Adding {} to an if statement
130  *
131  *    Revision 1.24  2001/07/30 22:08:36  jongfoster
132  *    Tidying up #defines:
133  *    - All feature #defines are now of the form FEATURE_xxx
134  *    - Permanently turned off WIN_GUI_EDIT
135  *    - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
136  *
137  *    Revision 1.23  2001/07/20 15:51:54  oes
138  *    Fixed indentation of prepocessor commands
139  *
140  *    Revision 1.22  2001/07/20 15:16:17  haroon
141  *    - per Guy's suggestion, added a while loop in sweep() to catch not just
142  *      the last inactive CSP but all other consecutive inactive CSPs after that
143  *      as well
144  *
145  *    Revision 1.21  2001/07/18 17:26:24  oes
146  *    Changed to conform to new pcrs interface
147  *
148  *    Revision 1.20  2001/07/17 13:07:01  oes
149  *    Fixed segv when last line in config files
150  *     lacked a terminating (\r)\n
151  *
152  *    Revision 1.19  2001/07/13 14:01:54  oes
153  *    Removed all #ifdef PCRS
154  *
155  *    Revision 1.18  2001/06/29 21:45:41  oes
156  *    Indentation, CRLF->LF, Tab-> Space
157  *
158  *    Revision 1.17  2001/06/29 13:31:51  oes
159  *    Various adaptions
160  *
161  *    Revision 1.16  2001/06/09 10:55:28  jongfoster
162  *    Changing BUFSIZ ==> BUFFER_SIZE
163  *
164  *    Revision 1.15  2001/06/07 23:14:14  jongfoster
165  *    Removing ACL and forward file loaders - these
166  *    files have been merged into the config file.
167  *    Cosmetic: Moving unloader funcs next to their
168  *    respective loader funcs
169  *
170  *    Revision 1.14  2001/06/01 03:27:04  oes
171  *    Fixed line continuation problem
172  *
173  *    Revision 1.13  2001/05/31 21:28:49  jongfoster
174  *    Removed all permissionsfile code - it's now called the actions
175  *    file, and (almost) all the code is in actions.c
176  *
177  *    Revision 1.12  2001/05/31 17:32:31  oes
178  *
179  *     - Enhanced domain part globbing with infix and prefix asterisk
180  *       matching and optional unanchored operation
181  *
182  *    Revision 1.11  2001/05/29 23:25:24  oes
183  *
184  *     - load_config_line() and load_permissions_file() now use chomp()
185  *
186  *    Revision 1.10  2001/05/29 09:50:24  jongfoster
187  *    Unified blocklist/imagelist/permissionslist.
188  *    File format is still under discussion, but the internal changes
189  *    are (mostly) done.
190  *
191  *    Also modified interceptor behaviour:
192  *    - We now intercept all URLs beginning with one of the following
193  *      prefixes (and *only* these prefixes):
194  *        * http://i.j.b/
195  *        * http://ijbswa.sf.net/config/
196  *        * http://ijbswa.sourceforge.net/config/
197  *    - New interceptors "home page" - go to http://i.j.b/ to see it.
198  *    - Internal changes so that intercepted and fast redirect pages
199  *      are not replaced with an image.
200  *    - Interceptors now have the option to send a binary page direct
201  *      to the client. (i.e. ijb-send-banner uses this)
202  *    - Implemented show-url-info interceptor.  (Which is why I needed
203  *      the above interceptors changes - a typical URL is
204  *      "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
205  *      The previous mechanism would not have intercepted that, and
206  *      if it had been intercepted then it then it would have replaced
207  *      it with an image.)
208  *
209  *    Revision 1.9  2001/05/26 17:12:07  jongfoster
210  *    Fatal errors loading configuration files now give better error messages.
211  *
212  *    Revision 1.8  2001/05/26 00:55:20  jongfoster
213  *    Removing duplicated code.  load_forwardfile() now uses create_url_spec()
214  *
215  *    Revision 1.7  2001/05/26 00:28:36  jongfoster
216  *    Automatic reloading of config file.
217  *    Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
218  *    Most of the global variables have been moved to a new
219  *    struct configuration_spec, accessed through csp->config->globalname
220  *    Most of the globals remaining are used by the Win32 GUI.
221  *
222  *    Revision 1.6  2001/05/23 12:27:33  oes
223  *
224  *    Fixed ugly indentation of my last changes
225  *
226  *    Revision 1.5  2001/05/23 10:39:05  oes
227  *    - Added support for escaping the comment character
228  *      in config files by a backslash
229  *    - Added support for line continuation in config
230  *      files
231  *    - Fixed a buffer overflow bug with long config lines
232  *
233  *    Revision 1.4  2001/05/22 18:56:28  oes
234  *    CRLF -> LF
235  *
236  *    Revision 1.3  2001/05/20 01:21:20  jongfoster
237  *    Version 2.9.4 checkin.
238  *    - Merged popupfile and cookiefile, and added control over PCRS
239  *      filtering, in new "permissionsfile".
240  *    - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
241  *      file error you now get a message box (in the Win32 GUI) rather
242  *      than the program exiting with no explanation.
243  *    - Made killpopup use the PCRS MIME-type checking and HTTP-header
244  *      skipping.
245  *    - Removed tabs from "config"
246  *    - Moved duplicated url parsing code in "loaders.c" to a new funcition.
247  *    - Bumped up version number.
248  *
249  *    Revision 1.2  2001/05/17 23:01:01  oes
250  *     - Cleaned CRLF's from the sources and related files
251  *
252  *    Revision 1.1.1.1  2001/05/15 13:58:59  oes
253  *    Initial import of version 2.9.3 source tree
254  *
255  *
256  *********************************************************************/
257 \f
258
259 #include "config.h"
260
261 #include <stdio.h>
262 #include <stdlib.h>
263 #include <sys/types.h>
264 #include <string.h>
265 #include <errno.h>
266 #include <sys/stat.h>
267 #include <ctype.h>
268 #include <assert.h>
269
270 #if !defined(_WIN32) && !defined(__OS2__)
271 #include <unistd.h>
272 #endif
273
274 #include "project.h"
275 #include "list.h"
276 #include "loaders.h"
277 #include "filters.h"
278 #include "parsers.h"
279 #include "jcc.h"
280 #include "miscutil.h"
281 #include "errlog.h"
282 #include "actions.h"
283 #include "urlmatch.h"
284
285 const char loaders_h_rcs[] = LOADERS_H_VERSION;
286
287 /*
288  * Currently active files.
289  * These are also entered in the main linked list of files.
290  */
291
292 #ifdef FEATURE_TRUST
293 static struct file_list *current_trustfile      = NULL;
294 #endif /* def FEATURE_TRUST */
295
296 static struct file_list *current_re_filterfile  = NULL;
297
298
299
300 /*********************************************************************
301  *
302  * Function    :  sweep
303  *
304  * Description :  Basically a mark and sweep garbage collector, it is run
305  *                (by the parent thread) every once in a while to reclaim memory.
306  *
307  * It uses a mark and sweep strategy:
308  *   1) mark all files as inactive
309  *
310  *   2) check with each client:
311  *       if it is active,   mark its files as active
312  *       if it is inactive, free its resources
313  *
314  *   3) free the resources of all of the files that
315  *      are still marked as inactive (and are obsolete).
316  *
317  *   N.B. files that are not obsolete don't have an unloader defined.
318  *
319  * Parameters  :  None
320  *
321  * Returns     :  N/A
322  *
323  *********************************************************************/
324 void sweep(void)
325 {
326    struct file_list *fl, *nfl;
327    struct client_state *csp, *ncsp;
328
329    /* clear all of the file's active flags */
330    for ( fl = files->next; NULL != fl; fl = fl->next )
331    {
332       fl->active = 0;
333    }
334
335    for (csp = clients; csp && (NULL != (ncsp = csp->next)) ; csp = csp->next)
336    {
337       if (ncsp->flags & CSP_FLAG_ACTIVE)
338       {
339          /* mark this client's files as active */
340
341          /*
342           * Always have a configuration file.
343           * (Also note the slightly non-standard extra
344           * indirection here.)
345           */
346          ncsp->config->config_file_list->active = 1;
347
348          if (ncsp->actions_list)     /* actions files */
349          {
350             ncsp->actions_list->active = 1;
351          }
352
353          if (ncsp->rlist)     /* pcrsjob files */
354          {
355             ncsp->rlist->active = 1;
356          }
357
358 #ifdef FEATURE_TRUST
359          if (ncsp->tlist)     /* trust files */
360          {
361             ncsp->tlist->active = 1;
362          }
363 #endif /* def FEATURE_TRUST */
364
365       }
366       else
367       /*
368        * this client is not active, release its resources
369        * and the ones of all inactive clients that might
370        * follow it
371        */
372       {
373          while (!(ncsp->flags & CSP_FLAG_ACTIVE))
374          {
375             csp->next = ncsp->next;
376
377             freez(ncsp->ip_addr_str);
378             freez(ncsp->my_ip_addr_str);
379             freez(ncsp->my_hostname);
380             freez(ncsp->x_forwarded);
381             freez(ncsp->iob->buf);
382
383             free_http_request(ncsp->http);
384
385             destroy_list(ncsp->headers);
386             destroy_list(ncsp->cookie_list);
387
388             free_current_action(ncsp->action);
389
390 #ifdef FEATURE_STATISTICS
391             urls_read++;
392             if (ncsp->flags & CSP_FLAG_REJECTED)
393             {
394                urls_rejected++;
395             }
396 #endif /* def FEATURE_STATISTICS */
397
398             freez(ncsp);
399
400             /* are there any more in sequence after it? */
401             if( (ncsp = csp->next) == NULL)
402                break;
403          }
404       }
405    }
406
407    for (fl = files; fl && ((nfl = fl->next) != NULL) ; fl = fl->next)
408    {
409       if ( ( 0 == nfl->active ) && ( NULL != nfl->unloader ) )
410       {
411          fl->next = nfl->next;
412
413          (nfl->unloader)(nfl->f);
414
415          freez(nfl->filename);
416
417          freez(nfl);
418       }
419    }
420
421 }
422
423
424 /*********************************************************************
425  *
426  * Function    :  check_file_changed
427  *
428  * Description :  Helper function to check if a file needs reloading.
429  *                If "current" is still current, return it.  Otherwise
430  *                allocates a new (zeroed) "struct file_list", fills
431  *                in the disk file name and timestamp, and returns it.
432  *
433  * Parameters  :
434  *          1  :  current = The file_list currently being used - will
435  *                          be checked to see if it is out of date.
436  *                          May be NULL (which is treated as out of
437  *                          date).
438  *          2  :  filename = Name of file to check.
439  *          3  :  newfl    = New file list. [Output only]
440  *                           This will be set to NULL, OR a struct
441  *                           file_list newly allocated on the
442  *                           heap, with the filename and lastmodified
443  *                           fields filled, and all others zeroed.
444  *
445  * Returns     :  If file unchanged: 0 (and sets newfl == NULL)
446  *                If file changed: 1 and sets newfl != NULL
447  *                On error: 1 and sets newfl == NULL
448  *
449  *********************************************************************/
450 int check_file_changed(const struct file_list * current,
451                        const char * filename,
452                        struct file_list ** newfl)
453 {
454    struct file_list *fs;
455    struct stat statbuf[1];
456
457    *newfl = NULL;
458
459    if (stat(filename, statbuf) < 0)
460    {
461       /* Error, probably file not found. */
462       return 1;
463    }
464
465    if (current
466        && (current->lastmodified == statbuf->st_mtime)
467        && (0 == strcmp(current->filename, filename)))
468    {
469       return 0;
470    }
471
472    fs = (struct file_list *)zalloc(sizeof(struct file_list));
473    if (fs == NULL)
474    {
475       /* Out of memory error */
476       return 1;
477    }
478
479    fs->filename = strdup(filename);
480    fs->lastmodified = statbuf->st_mtime;
481
482    if (fs->filename == NULL)
483    {
484       /* Out of memory error */
485       freez (fs);
486       return 1;
487    }
488    *newfl = fs;
489    return 1;
490 }
491
492
493 /*********************************************************************
494  *
495  * Function    :  simple_read_line
496  *
497  * Description :  Read a single line from a file and return it.
498  *                This is basically a version of fgets() that malloc()s
499  *                it's own line buffer.  Note that the buffer will
500  *                always be a multiple of BUFFER_SIZE bytes long.
501  *                Therefore if you are going to keep the string for
502  *                an extended period of time, you should probably
503  *                strdup() it and free() the original, to save memory.
504  *
505  *
506  * Parameters  :
507  *          1  :  dest = destination for newly malloc'd pointer to
508  *                line data.  Will be set to NULL on error.
509  *          2  :  fp = File to read from
510  *          3  :  newline = Standard for newlines in the file.
511  *                Will be unchanged if it's value on input is not
512  *                NEWLINE_UNKNOWN.
513  *                On output, may be changed from NEWLINE_UNKNOWN to
514  *                actual convention in file.
515  *
516  * Returns     :  JB_ERR_OK     on success
517  *                JB_ERR_MEMORY on out-of-memory
518  *                JB_ERR_FILE   on EOF.
519  *
520  *********************************************************************/
521 jb_err simple_read_line(FILE *fp, char **dest, int *newline)
522 {
523    size_t len = 0;
524    size_t buflen = BUFFER_SIZE;
525    char * buf;
526    char * p;
527    int ch;
528    int realnewline = NEWLINE_UNKNOWN;
529
530    if (NULL == (buf = malloc(buflen)))
531    {
532       return JB_ERR_MEMORY;
533    }
534
535    p = buf;
536
537 /*
538  * Character codes.  If you have a wierd compiler and the following are
539  * incorrect, you also need to fix NEWLINE() in loaders.h
540  */
541 #define CHAR_CR '\r' /* ASCII 13 */
542 #define CHAR_LF '\n' /* ASCII 10 */
543
544    for (;;)
545    {
546       ch = fgetc(fp);
547       if (ch == EOF)
548       {
549          if (len > 0)
550          {
551             *p = '\0';
552             *dest = buf;
553             return JB_ERR_OK;
554          }
555          else
556          {
557             free(buf);
558             *dest = NULL;
559             return JB_ERR_FILE;
560          }
561       }
562       else if (ch == CHAR_CR)
563       {
564          ch = getc(fp);
565          if (ch == CHAR_LF)
566          {
567             if (*newline == NEWLINE_UNKNOWN)
568             {
569                *newline = NEWLINE_DOS;
570             }
571          }
572          else
573          {
574             if (ch != EOF)
575             {
576                ungetc(ch, fp);
577             }
578             if (*newline == NEWLINE_UNKNOWN)
579             {
580                *newline = NEWLINE_MAC;
581             }
582          }
583          *p = '\0';
584          *dest = buf;
585          if (*newline == NEWLINE_UNKNOWN)
586          {
587             *newline = realnewline;
588          }
589          return JB_ERR_OK;
590       }
591       else if (ch == CHAR_LF)
592       {
593          *p = '\0';
594          *dest = buf;
595          if (*newline == NEWLINE_UNKNOWN)
596          {
597             *newline = NEWLINE_UNIX;
598          }
599          return JB_ERR_OK;
600       }
601       else if (ch == 0)
602       {
603          *p = '\0';
604          *dest = buf;
605          return JB_ERR_OK;
606       }
607
608       *p++ = ch;
609
610       if (++len >= buflen)
611       {
612          buflen += BUFFER_SIZE;
613          if (NULL == (p = realloc(buf, buflen)));
614          {
615             free(buf);
616             return JB_ERR_MEMORY;
617          }
618          buf = p;
619          p = buf + len;
620       }
621    }
622 }
623
624
625 /*********************************************************************
626  *
627  * Function    :  edit_read_line
628  *
629  * Description :  Read a single non-empty line from a file and return
630  *                it.  Trims comments, leading and trailing whitespace
631  *                and respects escaping of newline and comment char.
632  *                Provides the line in 2 alternative forms: raw and
633  *                preprocessed.
634  *                - raw is the raw data read from the file.  If the
635  *                  line is not modified, then this should be written
636  *                  to the new file.
637  *                - prefix is any comments and blank lines that were
638  *                  read from the file.  If the line is modified, then
639  *                  this should be written out to the file followed
640  *                  by the modified data.  (If this string is non-empty
641  *                  then it will have a newline at the end).
642  *                - data is the actual data that will be parsed
643  *                  further by appropriate routines.
644  *                On EOF, the 3 strings will all be set to NULL and
645  *                0 will be returned.
646  *
647  * Parameters  :
648  *          1  :  fp = File to read from
649  *          2  :  raw_out = destination for newly malloc'd pointer to
650  *                raw line data.  May be NULL if you don't want it.
651  *          3  :  prefix_out = destination for newly malloc'd pointer to
652  *                comments.  May be NULL if you don't want it.
653  *          4  :  data_out = destination for newly malloc'd pointer to
654  *                line data with comments and leading/trailing spaces
655  *                removed, and line continuation performed.  May be
656  *                NULL if you don't want it.
657  *          5  :  newline = Standard for newlines in the file.
658  *                On input, set to value to use or NEWLINE_UNKNOWN.
659  *                On output, may be changed from NEWLINE_UNKNOWN to
660  *                actual convention in file.  May be NULL if you
661  *                don't want it.
662  *          6  :  line_number = Line number in file.  In "lines" as
663  *                reported by a text editor, not lines containing data.
664  *
665  * Returns     :  JB_ERR_OK     on success
666  *                JB_ERR_MEMORY on out-of-memory
667  *                JB_ERR_FILE   on EOF.
668  *
669  *********************************************************************/
670 jb_err edit_read_line(FILE *fp,
671                       char **raw_out,
672                       char **prefix_out,
673                       char **data_out,
674                       int *newline,
675                       unsigned long *line_number)
676 {
677    char *p;          /* Temporary pointer   */
678    char *linebuf;    /* Line read from file */
679    char *linestart;  /* Start of linebuf, usually first non-whitespace char */
680    int contflag = 0; /* Nonzero for line continuation - i.e. line ends '\' */
681    int is_empty = 1; /* Flag if not got any data yet */
682    char *raw    = NULL; /* String to be stored in raw_out    */
683    char *prefix = NULL; /* String to be stored in prefix_out */
684    char *data   = NULL; /* String to be stored in data_out   */
685    int scrapnewline;    /* Used for (*newline) if newline==NULL */
686    jb_err rval = JB_ERR_OK;
687
688    assert(fp);
689    assert(raw_out || data_out);
690    assert(newline == NULL
691        || *newline == NEWLINE_UNKNOWN
692        || *newline == NEWLINE_UNIX
693        || *newline == NEWLINE_DOS
694        || *newline == NEWLINE_MAC);
695
696    if (newline == NULL)
697    {
698       scrapnewline = NEWLINE_UNKNOWN;
699       newline = &scrapnewline;
700    }
701
702    /* Set output parameters to NULL */
703    if (raw_out)
704    {
705       *raw_out    = NULL;
706    }
707    if (prefix_out)
708    {
709       *prefix_out = NULL;
710    }
711    if (data_out)
712    {
713       *data_out   = NULL;
714    }
715
716    /* Set string variables to new, empty strings. */
717
718    if (raw_out)
719    {
720       if ((raw = malloc(1)) == NULL)
721       {
722          return JB_ERR_MEMORY;
723       }
724       *raw = '\0';
725    }
726    if (prefix_out)
727    {
728       if ((prefix = malloc(1)) == NULL)
729       {
730          freez(raw);
731          return JB_ERR_MEMORY;
732       }
733       *prefix = '\0';
734    }
735    if (data_out)
736    {
737       if ((data = malloc(1)) == NULL)
738       {
739          freez(raw);
740          freez(prefix);
741          return JB_ERR_MEMORY;
742       }
743       *data = '\0';
744    }
745
746    /* Main loop.  Loop while we need more data & it's not EOF. */
747
748    while ( (contflag || is_empty)
749         && (JB_ERR_OK == (rval = simple_read_line(fp, &linebuf, newline))))
750    {
751       if (line_number)
752       {
753          (*line_number)++;
754       }
755       if (raw)
756       {
757          string_append(&raw,linebuf);
758          if (string_append(&raw,NEWLINE(*newline)))
759          {
760             freez(prefix);
761             freez(data);
762             free(linebuf);
763             return JB_ERR_MEMORY;
764          }
765       }
766
767       /* Line continuation? Trim escape and set flag. */
768       p = linebuf + strlen(linebuf) - 1;
769       contflag = ((*linebuf != '\0') && (*p == '\\'));
770       if (contflag)
771       {
772          *p = '\0';
773       }
774
775       /* Trim leading spaces if we're at the start of the line */
776       linestart = linebuf;
777       if (*data == '\0')
778       {
779          /* Trim leading spaces */
780          while (*linestart && isspace((int)(unsigned char)*linestart))
781          {
782             linestart++;
783          }
784       }
785
786       /* Handle comment characters. */
787       p = linestart;
788       while ((p = strchr(p, '#')) != NULL)
789       {
790          /* Found a comment char.. */
791          if ((p != linebuf) && (*(p-1) == '\\'))
792          {
793             /* ..and it's escaped, left-shift the line over the escape. */
794             char *q = p - 1;
795             while ((*q = *(q + 1)) != '\0')
796             {
797                q++;
798             }
799             /* Now scan from just after the "#". */
800          }
801          else
802          {
803             /* Real comment.  Save it... */
804             if (p == linestart)
805             {
806                /* Special case:  Line only contains a comment, so all the
807                 * previous whitespace is considered part of the comment.
808                 * Undo the whitespace skipping, if any.
809                 */
810                linestart = linebuf;
811                p = linestart;
812             }
813             if (prefix)
814             {
815                string_append(&prefix,p);
816                if (string_append(&prefix, NEWLINE(*newline)))
817                {
818                   freez(raw);
819                   freez(data);
820                   free(linebuf);
821                   return JB_ERR_MEMORY;
822                }
823             }
824
825             /* ... and chop off the rest of the line */
826             *p = '\0';
827          }
828       } /* END while (there's a # character) */
829
830       /* Write to the buffer */
831       if (*linestart)
832       {
833          is_empty = 0;
834          if (data)
835          {
836             if (string_append(&data, linestart))
837             {
838                freez(raw);
839                freez(prefix);
840                free(linebuf);
841                return JB_ERR_MEMORY;
842             }
843          }
844       }
845
846       free(linebuf);
847    } /* END while(we need more data) */
848
849    /* Handle simple_read_line() errors - ignore EOF */
850    if ((rval != JB_ERR_OK) && (rval != JB_ERR_FILE))
851    {
852       freez(raw);
853       freez(prefix);
854       freez(data);
855       return rval;
856    }
857
858    if (raw ? (*raw == '\0') : is_empty)
859    {
860       /* EOF and no data there.  (Definition of "data" depends on whether
861        * the caller cares about "raw" or just "data").
862        */
863
864       freez(raw);
865       freez(prefix);
866       freez(data);
867
868       return JB_ERR_FILE;
869    }
870    else
871    {
872       /* Got at least some data */
873
874       /* Remove trailing whitespace */
875       chomp(data);
876
877       if (raw_out)
878       {
879          *raw_out    = raw;
880       }
881       else
882       {
883          freez(raw);
884       }
885       if (prefix_out)
886       {
887          *prefix_out = prefix;
888       }
889       else
890       {
891          freez(prefix);
892       }
893       if (data_out)
894       {
895          *data_out   = data;
896       }
897       else
898       {
899          freez(data);
900       }
901       return JB_ERR_OK;
902    }
903 }
904
905
906 /*********************************************************************
907  *
908  * Function    :  read_config_line
909  *
910  * Description :  Read a single non-empty line from a file and return
911  *                it.  Trims comments, leading and trailing whitespace
912  *                and respects escaping of newline and comment char.
913  *
914  * Parameters  :
915  *          1  :  buf = Buffer to use.
916  *          2  :  buflen = Size of buffer in bytes.
917  *          3  :  fp = File to read from
918  *          4  :  linenum = linenumber in file
919  *
920  * Returns     :  NULL on EOF or error
921  *                Otherwise, returns buf.
922  *
923  *********************************************************************/
924 char *read_config_line(char *buf, size_t buflen, FILE *fp, unsigned long *linenum)
925 {
926    jb_err err;
927    char *buf2 = NULL;
928    err = edit_read_line(fp, NULL, NULL, &buf2, NULL, linenum);
929    if (err)
930    {
931       if (err == JB_ERR_MEMORY)
932       {
933          log_error(LOG_LEVEL_FATAL, "Out of memory loading a config file");
934       }
935       return NULL;
936    }
937    else
938    {
939       assert(buf2);
940       assert(strlen(buf2) + 1U < buflen);
941       strncpy(buf, buf2, buflen - 1);
942       free(buf2);
943       buf[buflen - 1] = '\0';
944       return buf;
945    }
946 }
947
948
949 #ifdef FEATURE_TRUST
950 /*********************************************************************
951  *
952  * Function    :  unload_trustfile
953  *
954  * Description :  Unloads a trustfile.
955  *
956  * Parameters  :
957  *          1  :  f = the data structure associated with the trustfile.
958  *
959  * Returns     :  N/A
960  *
961  *********************************************************************/
962 static void unload_trustfile(void *f)
963 {
964    struct block_spec *cur = (struct block_spec *)f;
965    struct block_spec *next;
966
967    while (cur != NULL)
968    {
969       next = cur->next;
970
971       free_url_spec(cur->url);
972       free(cur);
973
974       cur = next;
975    }
976
977 }
978
979
980 #ifdef FEATURE_GRACEFUL_TERMINATION
981 /*********************************************************************
982  *
983  * Function    :  unload_current_trust_file
984  *
985  * Description :  Unloads current trust file - reset to state at
986  *                beginning of program.
987  *
988  * Parameters  :  None
989  *
990  * Returns     :  N/A
991  *
992  *********************************************************************/
993 void unload_current_trust_file(void)
994 {
995    if (current_trustfile)
996    {
997       current_trustfile->unloader = unload_trustfile;
998       current_trustfile = NULL;
999    }
1000 }
1001 #endif /* FEATURE_GRACEFUL_TERMINATION */
1002
1003
1004 /*********************************************************************
1005  *
1006  * Function    :  load_trustfile
1007  *
1008  * Description :  Read and parse a trustfile and add to files list.
1009  *
1010  * Parameters  :
1011  *          1  :  csp = Current client state (buffers, headers, etc...)
1012  *
1013  * Returns     :  0 => Ok, everything else is an error.
1014  *
1015  *********************************************************************/
1016 int load_trustfile(struct client_state *csp)
1017 {
1018    FILE *fp;
1019
1020    struct block_spec *b, *bl;
1021    struct url_spec **tl;
1022
1023    char  buf[BUFFER_SIZE], *p, *q;
1024    int reject, trusted;
1025    struct file_list *fs;
1026    unsigned long linenum = 0;
1027
1028    if (!check_file_changed(current_trustfile, csp->config->trustfile, &fs))
1029    {
1030       /* No need to load */
1031       if (csp)
1032       {
1033          csp->tlist = current_trustfile;
1034       }
1035       return(0);
1036    }
1037    if (!fs)
1038    {
1039       goto load_trustfile_error;
1040    }
1041
1042    fs->f = bl = (struct block_spec *)zalloc(sizeof(*bl));
1043    if (bl == NULL)
1044    {
1045       goto load_trustfile_error;
1046    }
1047
1048    if ((fp = fopen(csp->config->trustfile, "r")) == NULL)
1049    {
1050       goto load_trustfile_error;
1051    }
1052
1053    tl = csp->config->trust_list;
1054
1055    while (read_config_line(buf, sizeof(buf), fp, &linenum) != NULL)
1056    {
1057       trusted = 0;
1058       reject  = 1;
1059
1060       if (*buf == '+')
1061       {
1062          trusted = 1;
1063          *buf = '~';
1064       }
1065
1066       if (*buf == '~')
1067       {
1068          reject = 0;
1069          p = buf;
1070          q = p+1;
1071          while ((*p++ = *q++) != '\0')
1072          {
1073             /* nop */
1074          }
1075       }
1076
1077       /* skip blank lines */
1078       if (*buf == '\0')
1079       {
1080          continue;
1081       }
1082
1083       /* allocate a new node */
1084       if ((b = zalloc(sizeof(*b))) == NULL)
1085       {
1086          fclose(fp);
1087          goto load_trustfile_error;
1088       }
1089
1090       /* add it to the list */
1091       b->next  = bl->next;
1092       bl->next = b;
1093
1094       b->reject = reject;
1095
1096       /* Save the URL pattern */
1097       if (create_url_spec(b->url, buf))
1098       {
1099          fclose(fp);
1100          goto load_trustfile_error;
1101       }
1102
1103       /*
1104        * save a pointer to URL's spec in the list of trusted URL's, too
1105        */
1106       if (trusted)
1107       {
1108          *tl++ = b->url;
1109          /* FIXME BUFFER OVERFLOW if >=64 entries */
1110       }
1111    }
1112
1113    *tl = NULL;
1114
1115    fclose(fp);
1116
1117    /* the old one is now obsolete */
1118    if (current_trustfile)
1119    {
1120       current_trustfile->unloader = unload_trustfile;
1121    }
1122
1123    fs->next    = files->next;
1124    files->next = fs;
1125    current_trustfile = fs;
1126
1127    if (csp)
1128    {
1129       csp->tlist = fs;
1130    }
1131
1132    return(0);
1133
1134 load_trustfile_error:
1135    log_error(LOG_LEVEL_FATAL, "can't load trustfile '%s': %E",
1136              csp->config->trustfile);
1137    return(-1);
1138
1139 }
1140 #endif /* def FEATURE_TRUST */
1141
1142
1143 /*********************************************************************
1144  *
1145  * Function    :  unload_re_filterfile
1146  *
1147  * Description :  Unload the re_filter list by freeing all chained
1148  *                re_filterfile specs and their data.
1149  *
1150  * Parameters  :
1151  *          1  :  f = the data structure associated with the filterfile.
1152  *
1153  * Returns     :  N/A
1154  *
1155  *********************************************************************/
1156 static void unload_re_filterfile(void *f)
1157 {
1158    struct re_filterfile_spec *a, *b = (struct re_filterfile_spec *)f;
1159
1160    while (b != NULL)
1161    {
1162       a = b->next;
1163
1164       destroy_list(b->patterns);
1165       pcrs_free_joblist(b->joblist);
1166       freez(b->name);
1167       freez(b->description);
1168       freez(b);
1169
1170       b = a;
1171    }
1172
1173    return;
1174 }
1175
1176
1177 #ifdef FEATURE_GRACEFUL_TERMINATION
1178 /*********************************************************************
1179  *
1180  * Function    :  unload_current_re_filterfile
1181  *
1182  * Description :  Unloads current re_filter file - reset to state at
1183  *                beginning of program.
1184  *
1185  * Parameters  :  None
1186  *
1187  * Returns     :  N/A
1188  *
1189  *********************************************************************/
1190 void unload_current_re_filterfile(void)
1191 {
1192    if (current_re_filterfile)
1193    {
1194       current_re_filterfile->unloader = unload_re_filterfile;
1195       current_re_filterfile = NULL;
1196    }
1197 }
1198 #endif
1199
1200
1201 /*********************************************************************
1202  *
1203  * Function    :  load_re_filterfile
1204  *
1205  * Description :  Load the re_filterfile. 
1206  *                Generate a chained list of re_filterfile_spec's from
1207  *                the "FILTER: " blocks, compiling all their substitutions
1208  *                into chained lists of pcrs_job structs.
1209  *
1210  * Parameters  :
1211  *          1  :  csp = Current client state (buffers, headers, etc...)
1212  *
1213  * Returns     :  0 => Ok, everything else is an error.
1214  *
1215  *********************************************************************/
1216 int load_re_filterfile(struct client_state *csp)
1217 {
1218    FILE *fp;
1219
1220    struct re_filterfile_spec *new_bl, *bl = NULL;
1221    struct file_list *fs;
1222
1223    char  buf[BUFFER_SIZE];
1224    int error;
1225    unsigned long linenum = 0;
1226    pcrs_job *dummy;
1227
1228    /*
1229     * No need to reload if unchanged
1230     */
1231    if (!check_file_changed(current_re_filterfile, csp->config->re_filterfile, &fs))
1232    {
1233       if (csp)
1234       {
1235          csp->rlist = current_re_filterfile;
1236       }
1237       return(0);
1238    }
1239    if (!fs)
1240    {
1241       goto load_re_filterfile_error;
1242    }
1243
1244    /* 
1245     * Open the file or fail
1246     */
1247    if ((fp = fopen(csp->config->re_filterfile, "r")) == NULL)
1248    {
1249       goto load_re_filterfile_error;
1250    }
1251
1252    /* 
1253     * Read line by line
1254     */
1255    while (read_config_line(buf, sizeof(buf), fp, &linenum) != NULL)
1256    {
1257       /*
1258        * If this is the head of a new filter block, make it a
1259        * re_filterfile spec of its own and chain it to the list:
1260        */
1261       if (strncmp(buf, "FILTER:", 7) == 0)
1262       {
1263          new_bl = (struct re_filterfile_spec  *)zalloc(sizeof(*bl));
1264          if (new_bl == NULL)
1265          {
1266             goto load_re_filterfile_error;
1267          }
1268
1269          new_bl->name = chomp(buf + 7);
1270
1271          if (NULL != (new_bl->description = strchr(new_bl->name, ' ')))
1272          {
1273             *new_bl->description++ = '\0';
1274             new_bl->description = strdup(chomp(new_bl->description));
1275          }
1276          else
1277          {
1278             new_bl->description = strdup("No description available for this filter");
1279          }
1280
1281          new_bl->name = strdup(chomp(new_bl->name));
1282          
1283          /*
1284           * If this is the first filter block, chain it
1285           * to the file_list rather than its (nonexistant)
1286           * predecessor
1287           */
1288          if (fs->f == NULL)
1289          {
1290             fs->f = new_bl;
1291          }
1292          else
1293          {
1294             bl->next = new_bl;
1295          }
1296          bl = new_bl;
1297
1298          log_error(LOG_LEVEL_RE_FILTER, "Reading in filter \"%s\" (\"%s\")", bl->name, bl->description);
1299
1300          continue;
1301       }
1302
1303       /* 
1304        * Else, save the expression, make it a pcrs_job
1305        * and chain it into the current filter's joblist 
1306        */
1307       if (bl != NULL)
1308       {
1309          enlist(bl->patterns, buf);
1310
1311          if ((dummy = pcrs_compile_command(buf, &error)) == NULL)
1312          {
1313             log_error(LOG_LEVEL_RE_FILTER,
1314                       "Adding re_filter job %s to filter %s failed with error %d.", buf, bl->name, error);
1315             continue;
1316          }
1317          else
1318          {
1319             dummy->next = bl->joblist;
1320             bl->joblist = dummy;
1321             log_error(LOG_LEVEL_RE_FILTER, "Adding re_filter job %s to filter %s succeeded.", buf, bl->name);
1322          }
1323       }
1324       else
1325       {
1326          log_error(LOG_LEVEL_ERROR, "Ignoring job %s outside filter block in %s, line %d", buf, csp->config->re_filterfile, linenum);
1327       }
1328    }
1329
1330    fclose(fp);
1331
1332    /* 
1333     * Schedule the now-obsolete old data for unloading
1334     */
1335    if ( NULL != current_re_filterfile )
1336    {
1337       current_re_filterfile->unloader = unload_re_filterfile;
1338    }
1339
1340    /*
1341     * Chain this file into the global list of loaded files
1342     */
1343    fs->next    = files->next;
1344    files->next = fs;
1345    current_re_filterfile = fs;
1346
1347    if (csp)
1348    {
1349       csp->rlist = fs;
1350    }
1351
1352    return( 0 );
1353
1354 load_re_filterfile_error:
1355    log_error(LOG_LEVEL_FATAL, "can't load re_filterfile '%s': %E",
1356              csp->config->re_filterfile);
1357    return(-1);
1358
1359 }
1360
1361
1362 /*********************************************************************
1363  *
1364  * Function    :  add_loader
1365  *
1366  * Description :  Called from `load_config'.  Called once for each input
1367  *                file found in config.
1368  *
1369  * Parameters  :
1370  *          1  :  loader = pointer to a function that can parse and load
1371  *                the appropriate config file.
1372  *          2  :  config = The configuration_spec to add the loader to.
1373  *
1374  * Returns     :  N/A
1375  *
1376  *********************************************************************/
1377 void add_loader(int (*loader)(struct client_state *),
1378                 struct configuration_spec * config)
1379 {
1380    int i;
1381
1382    for (i=0; i < NLOADERS; i++)
1383    {
1384       if (config->loaders[i] == NULL)
1385       {
1386          config->loaders[i] = loader;
1387          break;
1388       }
1389    }
1390
1391 }
1392
1393
1394 /*********************************************************************
1395  *
1396  * Function    :  run_loader
1397  *
1398  * Description :  Called from `load_config' and `listen_loop'.  This
1399  *                function keeps the "csp" current with any file mods
1400  *                since the last loop.  If a file is unchanged, the
1401  *                loader functions do NOT reload the file.
1402  *
1403  * Parameters  :
1404  *          1  :  csp = Current client state (buffers, headers, etc...)
1405  *                      Must be non-null.  Reads: "csp->config"
1406  *                      Writes: various data members.
1407  *
1408  * Returns     :  0 => Ok, everything else is an error.
1409  *
1410  *********************************************************************/
1411 int run_loader(struct client_state *csp)
1412 {
1413    int ret = 0;
1414    int i;
1415
1416    for (i=0; i < NLOADERS; i++)
1417    {
1418       if (csp->config->loaders[i] == NULL)
1419       {
1420          break;
1421       }
1422       ret |= (csp->config->loaders[i])(csp);
1423    }
1424    return(ret);
1425
1426 }
1427
1428
1429 /*
1430   Local Variables:
1431   tab-width: 3
1432   end:
1433 */