Add delay-response{} action
[privoxy.git] / loaders.c
1 /*********************************************************************
2  *
3  * File        :  $Source: /cvsroot/ijbswa/current/loaders.c,v $
4  *
5  * Purpose     :  Functions to load and unload the various
6  *                configuration files.  Also contains code to manage
7  *                the list of active loaders, and to automatically
8  *                unload files that are no longer in use.
9  *
10  * Copyright   :  Written by and Copyright (C) 2001-2014 the
11  *                Privoxy team. http://www.privoxy.org/
12  *
13  *                Based on the Internet Junkbuster originally written
14  *                by and Copyright (C) 1997 Anonymous Coders and
15  *                Junkbusters Corporation.  http://www.junkbusters.com
16  *
17  *                This program is free software; you can redistribute it
18  *                and/or modify it under the terms of the GNU General
19  *                Public License as published by the Free Software
20  *                Foundation; either version 2 of the License, or (at
21  *                your option) any later version.
22  *
23  *                This program is distributed in the hope that it will
24  *                be useful, but WITHOUT ANY WARRANTY; without even the
25  *                implied warranty of MERCHANTABILITY or FITNESS FOR A
26  *                PARTICULAR PURPOSE.  See the GNU General Public
27  *                License for more details.
28  *
29  *                The GNU General Public License should be included with
30  *                this file.  If not, you can view it at
31  *                http://www.gnu.org/copyleft/gpl.html
32  *                or write to the Free Software Foundation, Inc., 59
33  *                Temple Place - Suite 330, Boston, MA  02111-1307, USA.
34  *
35  *********************************************************************/
36
37
38 #include "config.h"
39
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <sys/types.h>
43 #include <string.h>
44 #include <errno.h>
45 #include <sys/stat.h>
46 #include <ctype.h>
47 #include <assert.h>
48
49 #if !defined(_WIN32) && !defined(__OS2__)
50 #include <unistd.h>
51 #endif
52
53 #include "project.h"
54 #include "list.h"
55 #include "loaders.h"
56 #include "filters.h"
57 #include "parsers.h"
58 #include "jcc.h"
59 #include "miscutil.h"
60 #include "errlog.h"
61 #include "actions.h"
62 #include "urlmatch.h"
63 #include "encode.h"
64
65 /*
66  * Currently active files.
67  * These are also entered in the main linked list of files.
68  */
69
70 #ifdef FEATURE_TRUST
71 static struct file_list *current_trustfile      = NULL;
72 #endif /* def FEATURE_TRUST */
73
74 #ifndef FUZZ
75 static int load_one_re_filterfile(struct client_state *csp, int fileid);
76 #endif
77
78 static struct file_list *current_re_filterfile[MAX_AF_FILES]  = {
79    NULL, NULL, NULL, NULL, NULL,
80    NULL, NULL, NULL, NULL, NULL
81 };
82
83 /*********************************************************************
84  *
85  * Function    :  free_csp_resources
86  *
87  * Description :  Frees memory referenced by the csp that isn't
88  *                shared with other csps.
89  *
90  * Parameters  :
91  *          1  :  csp = Current client state (buffers, headers, etc...)
92  *
93  * Returns     :  N/A
94  *
95  *********************************************************************/
96 void free_csp_resources(struct client_state *csp)
97 {
98    freez(csp->ip_addr_str);
99 #ifdef FEATURE_CLIENT_TAGS
100    freez(csp->client_address);
101 #endif
102    freez(csp->listen_addr_str);
103    freez(csp->client_iob->buf);
104    freez(csp->iob->buf);
105    freez(csp->error_message);
106
107    if (csp->action->flags & ACTION_FORWARD_OVERRIDE &&
108       NULL != csp->fwd)
109    {
110       unload_forward_spec(csp->fwd);
111    }
112    free_http_request(csp->http);
113
114    destroy_list(csp->headers);
115    destroy_list(csp->tags);
116
117    free_current_action(csp->action);
118 }
119
120 /*********************************************************************
121  *
122  * Function    :  sweep
123  *
124  * Description :  Basically a mark and sweep garbage collector, it is run
125  *                (by the parent thread) every once in a while to reclaim memory.
126  *
127  * It uses a mark and sweep strategy:
128  *   1) mark all files as inactive
129  *
130  *   2) check with each client:
131  *       if it is active,   mark its files as active
132  *       if it is inactive, free its resources
133  *
134  *   3) free the resources of all of the files that
135  *      are still marked as inactive (and are obsolete).
136  *
137  *   N.B. files that are not obsolete don't have an unloader defined.
138  *
139  * Parameters  :  None
140  *
141  * Returns     :  The number of threads that are still active.
142  *
143  *********************************************************************/
144 unsigned int sweep(void)
145 {
146    struct file_list *fl, *nfl;
147    struct client_state *csp;
148    struct client_states *last_active, *client_list;
149    int i;
150    unsigned int active_threads = 0;
151
152    /* clear all of the file's active flags */
153    for (fl = files->next; NULL != fl; fl = fl->next)
154    {
155       fl->active = 0;
156    }
157
158    last_active = clients;
159    client_list = clients->next;
160
161    while (NULL != client_list)
162    {
163       csp = &client_list->csp;
164       if (csp->flags & CSP_FLAG_ACTIVE)
165       {
166          /* Mark this client's files as active */
167
168          /*
169           * Always have a configuration file.
170           * (Also note the slightly non-standard extra
171           * indirection here.)
172           */
173          csp->config->config_file_list->active = 1;
174
175          /*
176           * Actions files
177           */
178          for (i = 0; i < MAX_AF_FILES; i++)
179          {
180             if (csp->actions_list[i])
181             {
182                csp->actions_list[i]->active = 1;
183             }
184          }
185
186          /*
187           * Filter files
188           */
189          for (i = 0; i < MAX_AF_FILES; i++)
190          {
191             if (csp->rlist[i])
192             {
193                csp->rlist[i]->active = 1;
194             }
195          }
196
197          /*
198           * Trust file
199           */
200 #ifdef FEATURE_TRUST
201          if (csp->tlist)
202          {
203             csp->tlist->active = 1;
204          }
205 #endif /* def FEATURE_TRUST */
206
207          active_threads++;
208
209          last_active = client_list;
210          client_list = client_list->next;
211       }
212       else
213       /*
214        * This client is not active. Free its resources.
215        */
216       {
217          last_active->next = client_list->next;
218
219 #ifdef FEATURE_STATISTICS
220          urls_read++;
221          if (csp->flags & CSP_FLAG_REJECTED)
222          {
223             urls_rejected++;
224          }
225 #endif /* def FEATURE_STATISTICS */
226
227          freez(client_list);
228
229          client_list = last_active->next;
230       }
231    }
232
233    nfl = files;
234    fl = files->next;
235
236    while (fl != NULL)
237    {
238       if ((0 == fl->active) && (NULL != fl->unloader))
239       {
240          nfl->next = fl->next;
241
242          (fl->unloader)(fl->f);
243
244          freez(fl->filename);
245          freez(fl);
246
247          fl = nfl->next;
248       }
249       else
250       {
251          nfl = fl;
252          fl = fl->next;
253       }
254    }
255
256    return active_threads;
257
258 }
259
260
261 /*********************************************************************
262  *
263  * Function    :  check_file_changed
264  *
265  * Description :  Helper function to check if a file needs reloading.
266  *                If "current" is still current, return it.  Otherwise
267  *                allocates a new (zeroed) "struct file_list", fills
268  *                in the disk file name and timestamp, and returns it.
269  *
270  * Parameters  :
271  *          1  :  current = The file_list currently being used - will
272  *                          be checked to see if it is out of date.
273  *                          May be NULL (which is treated as out of
274  *                          date).
275  *          2  :  filename = Name of file to check.
276  *          3  :  newfl    = New file list. [Output only]
277  *                           This will be set to NULL, OR a struct
278  *                           file_list newly allocated on the
279  *                           heap, with the filename and lastmodified
280  *                           fields filled, and all others zeroed.
281  *
282  * Returns     :  If file unchanged: 0 (and sets newfl == NULL)
283  *                If file changed: 1 and sets newfl != NULL
284  *                On error: 1 and sets newfl == NULL
285  *
286  *********************************************************************/
287 int check_file_changed(const struct file_list * current,
288                        const char * filename,
289                        struct file_list ** newfl)
290 {
291    struct file_list *fs;
292    struct stat statbuf[1];
293
294    *newfl = NULL;
295
296    if (stat(filename, statbuf) < 0)
297    {
298       /* Error, probably file not found. */
299       return 1;
300    }
301
302    if (current
303        && (current->lastmodified == statbuf->st_mtime)
304        && (0 == strcmp(current->filename, filename)))
305    {
306       return 0;
307    }
308
309    fs = zalloc_or_die(sizeof(struct file_list));
310    fs->filename = strdup_or_die(filename);
311    fs->lastmodified = statbuf->st_mtime;
312
313    *newfl = fs;
314    return 1;
315 }
316
317
318 /*********************************************************************
319  *
320  * Function    :  simple_read_line
321  *
322  * Description :  Read a single line from a file and return it.
323  *                This is basically a version of fgets() that malloc()s
324  *                it's own line buffer.  Note that the buffer will
325  *                always be a multiple of BUFFER_SIZE bytes long.
326  *                Therefore if you are going to keep the string for
327  *                an extended period of time, you should probably
328  *                strdup() it and free() the original, to save memory.
329  *
330  *
331  * Parameters  :
332  *          1  :  dest = destination for newly malloc'd pointer to
333  *                line data.  Will be set to NULL on error.
334  *          2  :  fp = File to read from
335  *          3  :  newline = Standard for newlines in the file.
336  *                Will be unchanged if it's value on input is not
337  *                NEWLINE_UNKNOWN.
338  *                On output, may be changed from NEWLINE_UNKNOWN to
339  *                actual convention in file.
340  *
341  * Returns     :  JB_ERR_OK     on success
342  *                JB_ERR_MEMORY on out-of-memory
343  *                JB_ERR_FILE   on EOF.
344  *
345  *********************************************************************/
346 jb_err simple_read_line(FILE *fp, char **dest, int *newline)
347 {
348    size_t len = 0;
349    size_t buflen = BUFFER_SIZE;
350    char * buf;
351    char * p;
352    int ch;
353    int realnewline = NEWLINE_UNKNOWN;
354
355    if (NULL == (buf = malloc(buflen)))
356    {
357       return JB_ERR_MEMORY;
358    }
359
360    p = buf;
361
362 /*
363  * Character codes.  If you have a weird compiler and the following are
364  * incorrect, you also need to fix NEWLINE() in loaders.h
365  */
366 #define CHAR_CR '\r' /* ASCII 13 */
367 #define CHAR_LF '\n' /* ASCII 10 */
368
369    for (;;)
370    {
371       ch = getc(fp);
372
373       if (ch == EOF)
374       {
375          if (len > 0)
376          {
377             *p = '\0';
378             *dest = buf;
379             return JB_ERR_OK;
380          }
381          else
382          {
383             free(buf);
384             *dest = NULL;
385             return JB_ERR_FILE;
386          }
387       }
388       else if (ch == CHAR_CR)
389       {
390          ch = getc(fp);
391          if (ch == CHAR_LF)
392          {
393             if (*newline == NEWLINE_UNKNOWN)
394             {
395                *newline = NEWLINE_DOS;
396             }
397          }
398          else
399          {
400             if (ch != EOF)
401             {
402                ungetc(ch, fp);
403             }
404             if (*newline == NEWLINE_UNKNOWN)
405             {
406                *newline = NEWLINE_MAC;
407             }
408          }
409          *p = '\0';
410          *dest = buf;
411          if (*newline == NEWLINE_UNKNOWN)
412          {
413             *newline = realnewline;
414          }
415          return JB_ERR_OK;
416       }
417       else if (ch == CHAR_LF)
418       {
419          *p = '\0';
420          *dest = buf;
421          if (*newline == NEWLINE_UNKNOWN)
422          {
423             *newline = NEWLINE_UNIX;
424          }
425          return JB_ERR_OK;
426       }
427       else if (ch == 0)
428       {
429          /* XXX: Why do we allow this anyway? */
430          *p = '\0';
431          *dest = buf;
432          return JB_ERR_OK;
433       }
434
435       *p++ = (char)ch;
436
437       if (++len >= buflen)
438       {
439          buflen += BUFFER_SIZE;
440          if (NULL == (p = realloc(buf, buflen)))
441          {
442             free(buf);
443             return JB_ERR_MEMORY;
444          }
445          buf = p;
446          p = buf + len;
447       }
448    }
449 }
450
451
452 /*********************************************************************
453  *
454  * Function    :  edit_read_line
455  *
456  * Description :  Read a single non-empty line from a file and return
457  *                it.  Trims comments, leading and trailing whitespace
458  *                and respects escaping of newline and comment char.
459  *                Provides the line in 2 alternative forms: raw and
460  *                preprocessed.
461  *                - raw is the raw data read from the file.  If the
462  *                  line is not modified, then this should be written
463  *                  to the new file.
464  *                - prefix is any comments and blank lines that were
465  *                  read from the file.  If the line is modified, then
466  *                  this should be written out to the file followed
467  *                  by the modified data.  (If this string is non-empty
468  *                  then it will have a newline at the end).
469  *                - data is the actual data that will be parsed
470  *                  further by appropriate routines.
471  *                On EOF, the 3 strings will all be set to NULL and
472  *                0 will be returned.
473  *
474  * Parameters  :
475  *          1  :  fp = File to read from
476  *          2  :  raw_out = destination for newly malloc'd pointer to
477  *                raw line data.  May be NULL if you don't want it.
478  *          3  :  prefix_out = destination for newly malloc'd pointer to
479  *                comments.  May be NULL if you don't want it.
480  *          4  :  data_out = destination for newly malloc'd pointer to
481  *                line data with comments and leading/trailing spaces
482  *                removed, and line continuation performed.  May be
483  *                NULL if you don't want it.
484  *          5  :  newline = Standard for newlines in the file.
485  *                On input, set to value to use or NEWLINE_UNKNOWN.
486  *                On output, may be changed from NEWLINE_UNKNOWN to
487  *                actual convention in file.  May be NULL if you
488  *                don't want it.
489  *          6  :  line_number = Line number in file.  In "lines" as
490  *                reported by a text editor, not lines containing data.
491  *
492  * Returns     :  JB_ERR_OK     on success
493  *                JB_ERR_MEMORY on out-of-memory
494  *                JB_ERR_FILE   on EOF.
495  *
496  *********************************************************************/
497 jb_err edit_read_line(FILE *fp,
498                       char **raw_out,
499                       char **prefix_out,
500                       char **data_out,
501                       int *newline,
502                       unsigned long *line_number)
503 {
504    char *p;          /* Temporary pointer   */
505    char *linebuf;    /* Line read from file */
506    char *linestart;  /* Start of linebuf, usually first non-whitespace char */
507    int contflag = 0; /* Nonzero for line continuation - i.e. line ends '\' */
508    int is_empty = 1; /* Flag if not got any data yet */
509    char *raw    = NULL; /* String to be stored in raw_out    */
510    char *prefix = NULL; /* String to be stored in prefix_out */
511    char *data   = NULL; /* String to be stored in data_out   */
512    int scrapnewline;    /* Used for (*newline) if newline==NULL */
513    jb_err rval = JB_ERR_OK;
514
515    assert(fp);
516    assert(raw_out || data_out);
517    assert(newline == NULL
518        || *newline == NEWLINE_UNKNOWN
519        || *newline == NEWLINE_UNIX
520        || *newline == NEWLINE_DOS
521        || *newline == NEWLINE_MAC);
522
523    if (newline == NULL)
524    {
525       scrapnewline = NEWLINE_UNKNOWN;
526       newline = &scrapnewline;
527    }
528
529    /* Set output parameters to NULL */
530    if (raw_out)
531    {
532       *raw_out    = NULL;
533    }
534    if (prefix_out)
535    {
536       *prefix_out = NULL;
537    }
538    if (data_out)
539    {
540       *data_out   = NULL;
541    }
542
543    /* Set string variables to new, empty strings. */
544
545    if (raw_out)
546    {
547       raw = strdup_or_die("");
548    }
549    if (prefix_out)
550    {
551       prefix = strdup_or_die("");
552    }
553    if (data_out)
554    {
555       data = strdup_or_die("");
556    }
557
558    /* Main loop.  Loop while we need more data & it's not EOF. */
559
560    while ((contflag || is_empty)
561        && (JB_ERR_OK == (rval = simple_read_line(fp, &linebuf, newline))))
562    {
563       if (line_number)
564       {
565          (*line_number)++;
566       }
567       if (raw)
568       {
569          string_append(&raw,linebuf);
570          if (string_append(&raw,NEWLINE(*newline)))
571          {
572             freez(prefix);
573             freez(data);
574             free(linebuf);
575             return JB_ERR_MEMORY;
576          }
577       }
578
579       /* Line continuation? Trim escape and set flag. */
580       p = linebuf + strlen(linebuf) - 1;
581       contflag = ((*linebuf != '\0') && (*p == '\\'));
582       if (contflag)
583       {
584          *p = '\0';
585       }
586
587       /* Trim leading spaces if we're at the start of the line */
588       linestart = linebuf;
589       assert(NULL != data);
590       if (*data == '\0')
591       {
592          /* Trim leading spaces */
593          while (*linestart && isspace((int)(unsigned char)*linestart))
594          {
595             linestart++;
596          }
597       }
598
599       /* Handle comment characters. */
600       p = linestart;
601       while ((p = strchr(p, '#')) != NULL)
602       {
603          /* Found a comment char.. */
604          if ((p != linebuf) && (*(p-1) == '\\'))
605          {
606             /* ..and it's escaped, left-shift the line over the escape. */
607             char *q = p - 1;
608             while ((*q = *(q + 1)) != '\0')
609             {
610                q++;
611             }
612             /* Now scan from just after the "#". */
613          }
614          else
615          {
616             /* Real comment.  Save it... */
617             if (p == linestart)
618             {
619                /* Special case:  Line only contains a comment, so all the
620                 * previous whitespace is considered part of the comment.
621                 * Undo the whitespace skipping, if any.
622                 */
623                linestart = linebuf;
624                p = linestart;
625             }
626             if (prefix)
627             {
628                string_append(&prefix,p);
629                if (string_append(&prefix, NEWLINE(*newline)))
630                {
631                   freez(raw);
632                   freez(data);
633                   free(linebuf);
634                   return JB_ERR_MEMORY;
635                }
636             }
637
638             /* ... and chop off the rest of the line */
639             *p = '\0';
640          }
641       } /* END while (there's a # character) */
642
643       /* Write to the buffer */
644       if (*linestart)
645       {
646          is_empty = 0;
647          if (string_append(&data, linestart))
648          {
649             freez(raw);
650             freez(prefix);
651             free(linebuf);
652             return JB_ERR_MEMORY;
653          }
654       }
655
656       free(linebuf);
657    } /* END while(we need more data) */
658
659    /* Handle simple_read_line() errors - ignore EOF */
660    if ((rval != JB_ERR_OK) && (rval != JB_ERR_FILE))
661    {
662       freez(raw);
663       freez(prefix);
664       freez(data);
665       return rval;
666    }
667
668    if (raw ? (*raw == '\0') : is_empty)
669    {
670       /* EOF and no data there.  (Definition of "data" depends on whether
671        * the caller cares about "raw" or just "data").
672        */
673
674       freez(raw);
675       freez(prefix);
676       freez(data);
677
678       return JB_ERR_FILE;
679    }
680    else
681    {
682       /* Got at least some data */
683
684       /* Remove trailing whitespace */
685       chomp(data);
686
687       if (raw_out)
688       {
689          *raw_out    = raw;
690       }
691       else
692       {
693          freez(raw);
694       }
695       if (prefix_out)
696       {
697          *prefix_out = prefix;
698       }
699       else
700       {
701          freez(prefix);
702       }
703       if (data_out)
704       {
705          *data_out   = data;
706       }
707       else
708       {
709          freez(data);
710       }
711       return JB_ERR_OK;
712    }
713 }
714
715
716 /*********************************************************************
717  *
718  * Function    :  read_config_line
719  *
720  * Description :  Read a single non-empty line from a file and return
721  *                it.  Trims comments, leading and trailing whitespace
722  *                and respects escaping of newline and comment char.
723  *
724  * Parameters  :
725  *          1  :  fp = File to read from
726  *          2  :  linenum = linenumber in file
727  *          3  :  buf = Pointer to a pointer to set to the data buffer.
728  *
729  * Returns     :  NULL on EOF or error
730  *                Otherwise, returns buf.
731  *
732  *********************************************************************/
733 char *read_config_line(FILE *fp, unsigned long *linenum, char **buf)
734 {
735    jb_err err;
736    err = edit_read_line(fp, NULL, NULL, buf, NULL, linenum);
737    if (err)
738    {
739       if (err == JB_ERR_MEMORY)
740       {
741          log_error(LOG_LEVEL_FATAL, "Out of memory loading a config file");
742       }
743       *buf = NULL;
744    }
745    return *buf;
746 }
747
748
749 #ifdef FEATURE_TRUST
750 /*********************************************************************
751  *
752  * Function    :  unload_trustfile
753  *
754  * Description :  Unloads a trustfile.
755  *
756  * Parameters  :
757  *          1  :  f = the data structure associated with the trustfile.
758  *
759  * Returns     :  N/A
760  *
761  *********************************************************************/
762 static void unload_trustfile(void *f)
763 {
764    struct block_spec *cur = (struct block_spec *)f;
765    struct block_spec *next;
766
767    while (cur != NULL)
768    {
769       next = cur->next;
770
771       free_pattern_spec(cur->url);
772       free(cur);
773
774       cur = next;
775    }
776
777 }
778
779
780 #ifdef FEATURE_GRACEFUL_TERMINATION
781 /*********************************************************************
782  *
783  * Function    :  unload_current_trust_file
784  *
785  * Description :  Unloads current trust file - reset to state at
786  *                beginning of program.
787  *
788  * Parameters  :  None
789  *
790  * Returns     :  N/A
791  *
792  *********************************************************************/
793 void unload_current_trust_file(void)
794 {
795    if (current_trustfile)
796    {
797       current_trustfile->unloader = unload_trustfile;
798       current_trustfile = NULL;
799    }
800 }
801 #endif /* FEATURE_GRACEFUL_TERMINATION */
802
803
804 /*********************************************************************
805  *
806  * Function    :  load_trustfile
807  *
808  * Description :  Read and parse a trustfile and add to files list.
809  *
810  * Parameters  :
811  *          1  :  csp = Current client state (buffers, headers, etc...)
812  *
813  * Returns     :  0 => Ok, everything else is an error.
814  *
815  *********************************************************************/
816 int load_trustfile(struct client_state *csp)
817 {
818    FILE *fp;
819
820    struct block_spec *b, *bl;
821    struct pattern_spec **tl;
822
823    char *buf = NULL;
824    int reject, trusted;
825    struct file_list *fs;
826    unsigned long linenum = 0;
827    int trusted_referrers = 0;
828
829    if (!check_file_changed(current_trustfile, csp->config->trustfile, &fs))
830    {
831       /* No need to load */
832       csp->tlist = current_trustfile;
833       return(0);
834    }
835    if (!fs)
836    {
837       goto load_trustfile_error;
838    }
839
840    fs->f = bl = zalloc_or_die(sizeof(*bl));
841
842    if ((fp = fopen(csp->config->trustfile, "r")) == NULL)
843    {
844       goto load_trustfile_error;
845    }
846    log_error(LOG_LEVEL_INFO, "Loading trust file: %s", csp->config->trustfile);
847
848    tl = csp->config->trust_list;
849
850    while (read_config_line(fp, &linenum, &buf) != NULL)
851    {
852       trusted = 0;
853       reject  = 1;
854
855       if (*buf == '+')
856       {
857          trusted = 1;
858          *buf = '~';
859       }
860
861       if (*buf == '~')
862       {
863          char *p;
864          char *q;
865
866          reject = 0;
867          p = buf;
868          q = p+1;
869          while ((*p++ = *q++) != '\0')
870          {
871             /* nop */
872          }
873       }
874
875       /* skip blank lines */
876       if (*buf == '\0')
877       {
878          freez(buf);
879          continue;
880       }
881
882       /* allocate a new node */
883       b = zalloc_or_die(sizeof(*b));
884
885       /* add it to the list */
886       b->next  = bl->next;
887       bl->next = b;
888
889       b->reject = reject;
890
891       /* Save the URL pattern */
892       if (create_pattern_spec(b->url, buf))
893       {
894          fclose(fp);
895          goto load_trustfile_error;
896       }
897
898       /*
899        * save a pointer to URL's spec in the list of trusted URL's, too
900        */
901       if (trusted)
902       {
903          if (++trusted_referrers < MAX_TRUSTED_REFERRERS)
904          {
905             *tl++ = b->url;
906          }
907       }
908       freez(buf);
909    }
910
911    if (trusted_referrers >= MAX_TRUSTED_REFERRERS)
912    {
913       /*
914        * FIXME: ... after Privoxy 3.0.4 is out.
915        */
916        log_error(LOG_LEVEL_ERROR, "Too many trusted referrers. Current limit is %d, you are using %d.\n"
917           "  Additional trusted referrers are treated like ordinary trusted URLs.\n"
918           "  (You can increase this limit by changing MAX_TRUSTED_REFERRERS in project.h and recompiling).",
919           MAX_TRUSTED_REFERRERS, trusted_referrers);
920    }
921
922    *tl = NULL;
923
924    fclose(fp);
925
926    /* the old one is now obsolete */
927    if (current_trustfile)
928    {
929       current_trustfile->unloader = unload_trustfile;
930    }
931
932    fs->next    = files->next;
933    files->next = fs;
934    current_trustfile = fs;
935    csp->tlist = fs;
936
937    return(0);
938
939 load_trustfile_error:
940    log_error(LOG_LEVEL_FATAL, "can't load trustfile '%s': %E",
941       csp->config->trustfile);
942    freez(buf);
943    return(-1);
944
945 }
946 #endif /* def FEATURE_TRUST */
947
948
949 /*********************************************************************
950  *
951  * Function    :  unload_re_filterfile
952  *
953  * Description :  Unload the re_filter list by freeing all chained
954  *                re_filterfile specs and their data.
955  *
956  * Parameters  :
957  *          1  :  f = the data structure associated with the filterfile.
958  *
959  * Returns     :  N/A
960  *
961  *********************************************************************/
962 static void unload_re_filterfile(void *f)
963 {
964    struct re_filterfile_spec *a, *b = (struct re_filterfile_spec *)f;
965
966    while (b != NULL)
967    {
968       a = b->next;
969
970       destroy_list(b->patterns);
971       pcrs_free_joblist(b->joblist);
972       freez(b->name);
973       freez(b->description);
974       freez(b);
975
976       b = a;
977    }
978
979    return;
980 }
981
982 /*********************************************************************
983  *
984  * Function    :  unload_forward_spec
985  *
986  * Description :  Unload the forward spec settings by freeing all
987  *                memory referenced by members and the memory for
988  *                the spec itself.
989  *
990  * Parameters  :
991  *          1  :  fwd = the forward spec.
992  *
993  * Returns     :  N/A
994  *
995  *********************************************************************/
996 void unload_forward_spec(struct forward_spec *fwd)
997 {
998    free_pattern_spec(fwd->url);
999    freez(fwd->gateway_host);
1000    freez(fwd->forward_host);
1001    free(fwd);
1002
1003    return;
1004 }
1005
1006
1007 #ifdef FEATURE_GRACEFUL_TERMINATION
1008 /*********************************************************************
1009  *
1010  * Function    :  unload_current_re_filterfile
1011  *
1012  * Description :  Unloads current re_filter file - reset to state at
1013  *                beginning of program.
1014  *
1015  * Parameters  :  None
1016  *
1017  * Returns     :  N/A
1018  *
1019  *********************************************************************/
1020 void unload_current_re_filterfile(void)
1021 {
1022    int i;
1023
1024    for (i = 0; i < MAX_AF_FILES; i++)
1025    {
1026       if (current_re_filterfile[i])
1027       {
1028          current_re_filterfile[i]->unloader = unload_re_filterfile;
1029          current_re_filterfile[i] = NULL;
1030       }
1031    }
1032 }
1033 #endif
1034
1035
1036 /*********************************************************************
1037  *
1038  * Function    :  load_re_filterfiles
1039  *
1040  * Description :  Loads all the filterfiles.
1041  *                Generate a chained list of re_filterfile_spec's from
1042  *                the "FILTER: " blocks, compiling all their substitutions
1043  *                into chained lists of pcrs_job structs.
1044  *
1045  * Parameters  :
1046  *          1  :  csp = Current client state (buffers, headers, etc...)
1047  *
1048  * Returns     :  0 => Ok, everything else is an error.
1049  *
1050  *********************************************************************/
1051 int load_re_filterfiles(struct client_state *csp)
1052 {
1053    int i;
1054    int result;
1055
1056    for (i = 0; i < MAX_AF_FILES; i++)
1057    {
1058       if (csp->config->re_filterfile[i])
1059       {
1060          result = load_one_re_filterfile(csp, i);
1061          if (result)
1062          {
1063             return result;
1064          }
1065       }
1066       else if (current_re_filterfile[i])
1067       {
1068          current_re_filterfile[i]->unloader = unload_re_filterfile;
1069          current_re_filterfile[i] = NULL;
1070       }
1071    }
1072
1073    return 0;
1074 }
1075
1076
1077 /*********************************************************************
1078  *
1079  * Function    :  load_one_re_filterfile
1080  *
1081  * Description :  Load a re_filterfile.
1082  *                Generate a chained list of re_filterfile_spec's from
1083  *                the "FILTER: " blocks, compiling all their substitutions
1084  *                into chained lists of pcrs_job structs.
1085  *
1086  * Parameters  :
1087  *          1  :  csp = Current client state (buffers, headers, etc...)
1088  *
1089  * Returns     :  0 => Ok, everything else is an error.
1090  *
1091  *********************************************************************/
1092 int load_one_re_filterfile(struct client_state *csp, int fileid)
1093 {
1094    FILE *fp;
1095
1096    struct re_filterfile_spec *new_bl, *bl = NULL;
1097    struct file_list *fs;
1098
1099    char *buf = NULL;
1100    unsigned long linenum = 0;
1101    pcrs_job *dummy, *lastjob = NULL;
1102
1103    /*
1104     * No need to reload if unchanged
1105     */
1106    if (!check_file_changed(current_re_filterfile[fileid], csp->config->re_filterfile[fileid], &fs))
1107    {
1108       csp->rlist[fileid] = current_re_filterfile[fileid];
1109       return(0);
1110    }
1111    if (!fs)
1112    {
1113       goto load_re_filterfile_error;
1114    }
1115
1116    /*
1117     * Open the file or fail
1118     */
1119    if ((fp = fopen(csp->config->re_filterfile[fileid], "r")) == NULL)
1120    {
1121       goto load_re_filterfile_error;
1122    }
1123
1124    log_error(LOG_LEVEL_INFO, "Loading filter file: %s", csp->config->re_filterfile[fileid]);
1125
1126    /*
1127     * Read line by line
1128     */
1129    while (read_config_line(fp, &linenum, &buf) != NULL)
1130    {
1131       enum filter_type new_filter = FT_INVALID_FILTER;
1132
1133       if (strncmp(buf, "FILTER:", 7) == 0)
1134       {
1135          new_filter = FT_CONTENT_FILTER;
1136       }
1137       else if (strncmp(buf, "SERVER-HEADER-FILTER:", 21) == 0)
1138       {
1139          new_filter = FT_SERVER_HEADER_FILTER;
1140       }
1141       else if (strncmp(buf, "CLIENT-HEADER-FILTER:", 21) == 0)
1142       {
1143          new_filter = FT_CLIENT_HEADER_FILTER;
1144       }
1145       else if (strncmp(buf, "CLIENT-HEADER-TAGGER:", 21) == 0)
1146       {
1147          new_filter = FT_CLIENT_HEADER_TAGGER;
1148       }
1149       else if (strncmp(buf, "SERVER-HEADER-TAGGER:", 21) == 0)
1150       {
1151          new_filter = FT_SERVER_HEADER_TAGGER;
1152       }
1153 #ifdef FEATURE_EXTERNAL_FILTERS
1154       else if (strncmp(buf, "EXTERNAL-FILTER:", 16) == 0)
1155       {
1156          new_filter = FT_EXTERNAL_CONTENT_FILTER;
1157       }
1158 #endif
1159
1160       /*
1161        * If this is the head of a new filter block, make it a
1162        * re_filterfile spec of its own and chain it to the list:
1163        */
1164       if (new_filter != FT_INVALID_FILTER)
1165       {
1166          new_bl = zalloc_or_die(sizeof(*bl));
1167          if (new_filter == FT_CONTENT_FILTER)
1168          {
1169             new_bl->name = chomp(buf + 7);
1170          }
1171 #ifdef FEATURE_EXTERNAL_FILTERS
1172          else if (new_filter == FT_EXTERNAL_CONTENT_FILTER)
1173          {
1174             new_bl->name = chomp(buf + 16);
1175          }
1176 #endif
1177          else
1178          {
1179             new_bl->name = chomp(buf + 21);
1180          }
1181          new_bl->type = new_filter;
1182
1183          /*
1184           * If a filter description is available,
1185           * encode it to HTML and save it.
1186           */
1187          if (NULL != (new_bl->description = strpbrk(new_bl->name, " \t")))
1188          {
1189             *new_bl->description++ = '\0';
1190             new_bl->description = html_encode(chomp(new_bl->description));
1191             if (NULL == new_bl->description)
1192             {
1193                new_bl->description = strdup_or_die("Out of memory while "
1194                   "encoding filter description to HTML");
1195             }
1196          }
1197          else
1198          {
1199             new_bl->description = strdup_or_die("No description available");
1200          }
1201
1202          new_bl->name = strdup_or_die(chomp(new_bl->name));
1203
1204          /*
1205           * If this is the first filter block, chain it
1206           * to the file_list rather than its (nonexistant)
1207           * predecessor
1208           */
1209          if (fs->f == NULL)
1210          {
1211             fs->f = new_bl;
1212          }
1213          else
1214          {
1215             assert(NULL != bl);
1216             bl->next = new_bl;
1217          }
1218          bl = new_bl;
1219
1220          log_error(LOG_LEVEL_RE_FILTER, "Reading in filter \"%s\" (\"%s\")", bl->name, bl->description);
1221
1222          freez(buf);
1223          continue;
1224       }
1225
1226 #ifdef FEATURE_EXTERNAL_FILTERS
1227       if ((bl != NULL) && (bl->type == FT_EXTERNAL_CONTENT_FILTER))
1228       {
1229          jb_err jb_error;
1230          /* Save the code as "pattern", but do not compile anything. */
1231          if (bl->patterns->first != NULL)
1232          {
1233             log_error(LOG_LEVEL_FATAL, "External filter '%s' contains several jobss. "
1234                "Did you forget to escape a line break?",
1235                bl->name);
1236          }
1237          jb_error = enlist(bl->patterns, buf);
1238          if (JB_ERR_MEMORY == jb_error)
1239          {
1240             log_error(LOG_LEVEL_FATAL,
1241                "Out of memory while enlisting external filter code \'%s\' for filter %s.",
1242                buf, bl->name);
1243          }
1244          freez(buf);
1245          continue;
1246       }
1247 #endif
1248       if (bl != NULL)
1249       {
1250          int pcrs_error;
1251          jb_err jb_error;
1252          /*
1253           * Save the expression, make it a pcrs_job
1254           * and chain it into the current filter's joblist
1255           */
1256          jb_error = enlist(bl->patterns, buf);
1257          if (JB_ERR_MEMORY == jb_error)
1258          {
1259             log_error(LOG_LEVEL_FATAL,
1260                "Out of memory while enlisting re_filter job \'%s\' for filter %s.", buf, bl->name);
1261          }
1262          assert(JB_ERR_OK == jb_error);
1263
1264          if (pcrs_job_is_dynamic(buf))
1265          {
1266             /*
1267              * Dynamic pattern that might contain variables
1268              * and has to be recompiled for every request
1269              */
1270             if (bl->joblist != NULL)
1271             {
1272                 pcrs_free_joblist(bl->joblist);
1273                 bl->joblist = NULL;
1274             }
1275             bl->dynamic = 1;
1276             log_error(LOG_LEVEL_RE_FILTER,
1277                "Adding dynamic re_filter job \'%s\' to filter %s succeeded.", buf, bl->name);
1278             freez(buf);
1279             continue;
1280          }
1281          else if (bl->dynamic)
1282          {
1283             /*
1284              * A previous job was dynamic and as we
1285              * recompile the whole filter anyway, it
1286              * makes no sense to compile this job now.
1287              */
1288             log_error(LOG_LEVEL_RE_FILTER,
1289                "Adding static re_filter job \'%s\' to dynamic filter %s succeeded.", buf, bl->name);
1290             freez(buf);
1291             continue;
1292          }
1293
1294          if ((dummy = pcrs_compile_command(buf, &pcrs_error)) == NULL)
1295          {
1296             log_error(LOG_LEVEL_ERROR,
1297                "Adding re_filter job \'%s\' to filter %s failed: %s",
1298                buf, bl->name, pcrs_strerror(pcrs_error));
1299             freez(buf);
1300             continue;
1301          }
1302          else
1303          {
1304             if (bl->joblist == NULL)
1305             {
1306                bl->joblist = dummy;
1307             }
1308             else if (NULL != lastjob)
1309             {
1310                lastjob->next = dummy;
1311             }
1312             lastjob = dummy;
1313             log_error(LOG_LEVEL_RE_FILTER, "Adding re_filter job \'%s\' to filter %s succeeded.", buf, bl->name);
1314          }
1315       }
1316       else
1317       {
1318          log_error(LOG_LEVEL_ERROR, "Ignoring job %s outside filter block in %s, line %d",
1319             buf, csp->config->re_filterfile[fileid], linenum);
1320       }
1321       freez(buf);
1322    }
1323
1324    fclose(fp);
1325
1326    /*
1327     * Schedule the now-obsolete old data for unloading
1328     */
1329    if (NULL != current_re_filterfile[fileid])
1330    {
1331       current_re_filterfile[fileid]->unloader = unload_re_filterfile;
1332    }
1333
1334    /*
1335     * Chain this file into the global list of loaded files
1336     */
1337    fs->next    = files->next;
1338    files->next = fs;
1339    current_re_filterfile[fileid] = fs;
1340    csp->rlist[fileid] = fs;
1341
1342    return(0);
1343
1344 load_re_filterfile_error:
1345    log_error(LOG_LEVEL_FATAL, "can't load re_filterfile '%s': %E",
1346              csp->config->re_filterfile[fileid]);
1347    return(-1);
1348
1349 }
1350
1351
1352 /*********************************************************************
1353  *
1354  * Function    :  add_loader
1355  *
1356  * Description :  Called from `load_config'.  Called once for each input
1357  *                file found in config.
1358  *
1359  * Parameters  :
1360  *          1  :  loader = pointer to a function that can parse and load
1361  *                the appropriate config file.
1362  *          2  :  config = The configuration_spec to add the loader to.
1363  *
1364  * Returns     :  N/A
1365  *
1366  *********************************************************************/
1367 void add_loader(int (*loader)(struct client_state *),
1368                 struct configuration_spec * config)
1369 {
1370    int i;
1371
1372    for (i = 0; i < NLOADERS; i++)
1373    {
1374       if (config->loaders[i] == NULL)
1375       {
1376          config->loaders[i] = loader;
1377          break;
1378       }
1379    }
1380
1381 }
1382
1383
1384 /*********************************************************************
1385  *
1386  * Function    :  run_loader
1387  *
1388  * Description :  Called from `load_config' and `listen_loop'.  This
1389  *                function keeps the "csp" current with any file mods
1390  *                since the last loop.  If a file is unchanged, the
1391  *                loader functions do NOT reload the file.
1392  *
1393  * Parameters  :
1394  *          1  :  csp = Current client state (buffers, headers, etc...)
1395  *                      Must be non-null.  Reads: "csp->config"
1396  *                      Writes: various data members.
1397  *
1398  * Returns     :  0 => Ok, everything else is an error.
1399  *
1400  *********************************************************************/
1401 int run_loader(struct client_state *csp)
1402 {
1403    int ret = 0;
1404    int i;
1405
1406    for (i = 0; i < NLOADERS; i++)
1407    {
1408       if (csp->config->loaders[i] == NULL)
1409       {
1410          break;
1411       }
1412       ret |= (csp->config->loaders[i])(csp);
1413    }
1414    return(ret);
1415
1416 }
1417
1418 /*********************************************************************
1419  *
1420  * Function    :  file_has_been_modified
1421  *
1422  * Description :  Helper function to check if a file has been changed
1423  *
1424  * Parameters  :
1425  *          1  : filename = The name of the file to check
1426  *          2  : last_known_modification = The time of the last known
1427  *                                         modification
1428  *
1429  * Returns     :  TRUE if the file has been changed,
1430  *                FALSE otherwise.
1431  *
1432  *********************************************************************/
1433 static int file_has_been_modified(const char *filename, time_t last_know_modification)
1434 {
1435    struct stat statbuf[1];
1436
1437    if (stat(filename, statbuf) < 0)
1438    {
1439       /* Error, probably file not found which counts as change. */
1440       return 1;
1441    }
1442
1443    return (last_know_modification != statbuf->st_mtime);
1444 }
1445
1446
1447 /*********************************************************************
1448  *
1449  * Function    :  any_loaded_file_changed
1450  *
1451  * Description :  Helper function to check if any loaded file has been
1452  *                changed since the time it has been loaded.
1453  *
1454  *                XXX: Should we cache the return value for x seconds?
1455  *
1456  * Parameters  :
1457  *          1  : files_to_check = List of files to check
1458  *
1459  * Returns     : TRUE if any file has been changed,
1460  *               FALSE otherwise.
1461  *
1462  *********************************************************************/
1463 int any_loaded_file_changed(const struct client_state *csp)
1464 {
1465    const struct file_list *file_to_check = csp->config->config_file_list;
1466    int i;
1467
1468    if (file_has_been_modified(file_to_check->filename, file_to_check->lastmodified))
1469    {
1470       return TRUE;
1471    }
1472
1473    for (i = 0; i < MAX_AF_FILES; i++)
1474    {
1475       if (csp->actions_list[i])
1476       {
1477          file_to_check = csp->actions_list[i];
1478          if (file_has_been_modified(file_to_check->filename, file_to_check->lastmodified))
1479          {
1480             return TRUE;
1481          }
1482       }
1483    }
1484
1485    for (i = 0; i < MAX_AF_FILES; i++)
1486    {
1487       if (csp->rlist[i])
1488       {
1489          file_to_check = csp->rlist[i];
1490          if (file_has_been_modified(file_to_check->filename, file_to_check->lastmodified))
1491          {
1492             return TRUE;
1493          }
1494       }
1495    }
1496
1497 #ifdef FEATURE_TRUST
1498    if (csp->tlist)
1499    {
1500       if (file_has_been_modified(csp->tlist->filename, csp->tlist->lastmodified))
1501       {
1502          return TRUE;
1503       }
1504    }
1505 #endif /* def FEATURE_TRUST */
1506
1507    return FALSE;
1508 }
1509
1510
1511 /*
1512   Local Variables:
1513   tab-width: 3
1514   end:
1515 */