Show enablement/disablement status in almost all templates.
[privoxy.git] / loaders.c
1 const char loaders_rcs[] = "$Id: loaders.c,v 1.30 2001/10/25 03:40:48 david__schmidt 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  *                IJBSWA team.  http://ijbswa.sourceforge.net
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.30  2001/10/25 03:40:48  david__schmidt
39  *    Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
40  *    threads to call select() simultaneously.  So, it's time to do a real, live,
41  *    native OS/2 port.  See defines for __EMX__ (the porting layer) vs. __OS2__
42  *    (native). Both versions will work, but using __OS2__ offers multi-threading.
43  *
44  *    Revision 1.29  2001/10/23 21:38:53  jongfoster
45  *    Adding error-checking to create_url_spec()
46  *
47  *    Revision 1.28  2001/10/07 15:40:39  oes
48  *    Replaced 6 boolean members of csp with one bitmap (csp->flags)
49  *
50  *    Revision 1.27  2001/09/22 16:36:59  jongfoster
51  *    Removing unused parameter fs from read_config_line()
52  *
53  *    Revision 1.26  2001/09/22 14:05:22  jongfoster
54  *    Bugfix: Multiple escaped "#" characters in a configuration
55  *    file are now permitted.
56  *    Also removing 3 unused headers.
57  *
58  *    Revision 1.25  2001/09/13 22:44:03  jongfoster
59  *    Adding {} to an if statement
60  *
61  *    Revision 1.24  2001/07/30 22:08:36  jongfoster
62  *    Tidying up #defines:
63  *    - All feature #defines are now of the form FEATURE_xxx
64  *    - Permanently turned off WIN_GUI_EDIT
65  *    - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
66  *
67  *    Revision 1.23  2001/07/20 15:51:54  oes
68  *    Fixed indentation of prepocessor commands
69  *
70  *    Revision 1.22  2001/07/20 15:16:17  haroon
71  *    - per Guy's suggestion, added a while loop in sweep() to catch not just
72  *      the last inactive CSP but all other consecutive inactive CSPs after that
73  *      as well
74  *
75  *    Revision 1.21  2001/07/18 17:26:24  oes
76  *    Changed to conform to new pcrs interface
77  *
78  *    Revision 1.20  2001/07/17 13:07:01  oes
79  *    Fixed segv when last line in config files
80  *     lacked a terminating (\r)\n
81  *
82  *    Revision 1.19  2001/07/13 14:01:54  oes
83  *    Removed all #ifdef PCRS
84  *
85  *    Revision 1.18  2001/06/29 21:45:41  oes
86  *    Indentation, CRLF->LF, Tab-> Space
87  *
88  *    Revision 1.17  2001/06/29 13:31:51  oes
89  *    Various adaptions
90  *
91  *    Revision 1.16  2001/06/09 10:55:28  jongfoster
92  *    Changing BUFSIZ ==> BUFFER_SIZE
93  *
94  *    Revision 1.15  2001/06/07 23:14:14  jongfoster
95  *    Removing ACL and forward file loaders - these
96  *    files have been merged into the config file.
97  *    Cosmetic: Moving unloader funcs next to their
98  *    respective loader funcs
99  *
100  *    Revision 1.14  2001/06/01 03:27:04  oes
101  *    Fixed line continuation problem
102  *
103  *    Revision 1.13  2001/05/31 21:28:49  jongfoster
104  *    Removed all permissionsfile code - it's now called the actions
105  *    file, and (almost) all the code is in actions.c
106  *
107  *    Revision 1.12  2001/05/31 17:32:31  oes
108  *
109  *     - Enhanced domain part globbing with infix and prefix asterisk
110  *       matching and optional unanchored operation
111  *
112  *    Revision 1.11  2001/05/29 23:25:24  oes
113  *
114  *     - load_config_line() and load_permissions_file() now use chomp()
115  *
116  *    Revision 1.10  2001/05/29 09:50:24  jongfoster
117  *    Unified blocklist/imagelist/permissionslist.
118  *    File format is still under discussion, but the internal changes
119  *    are (mostly) done.
120  *
121  *    Also modified interceptor behaviour:
122  *    - We now intercept all URLs beginning with one of the following
123  *      prefixes (and *only* these prefixes):
124  *        * http://i.j.b/
125  *        * http://ijbswa.sf.net/config/
126  *        * http://ijbswa.sourceforge.net/config/
127  *    - New interceptors "home page" - go to http://i.j.b/ to see it.
128  *    - Internal changes so that intercepted and fast redirect pages
129  *      are not replaced with an image.
130  *    - Interceptors now have the option to send a binary page direct
131  *      to the client. (i.e. ijb-send-banner uses this)
132  *    - Implemented show-url-info interceptor.  (Which is why I needed
133  *      the above interceptors changes - a typical URL is
134  *      "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
135  *      The previous mechanism would not have intercepted that, and
136  *      if it had been intercepted then it then it would have replaced
137  *      it with an image.)
138  *
139  *    Revision 1.9  2001/05/26 17:12:07  jongfoster
140  *    Fatal errors loading configuration files now give better error messages.
141  *
142  *    Revision 1.8  2001/05/26 00:55:20  jongfoster
143  *    Removing duplicated code.  load_forwardfile() now uses create_url_spec()
144  *
145  *    Revision 1.7  2001/05/26 00:28:36  jongfoster
146  *    Automatic reloading of config file.
147  *    Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
148  *    Most of the global variables have been moved to a new
149  *    struct configuration_spec, accessed through csp->config->globalname
150  *    Most of the globals remaining are used by the Win32 GUI.
151  *
152  *    Revision 1.6  2001/05/23 12:27:33  oes
153  *
154  *    Fixed ugly indentation of my last changes
155  *
156  *    Revision 1.5  2001/05/23 10:39:05  oes
157  *    - Added support for escaping the comment character
158  *      in config files by a backslash
159  *    - Added support for line continuation in config
160  *      files
161  *    - Fixed a buffer overflow bug with long config lines
162  *
163  *    Revision 1.4  2001/05/22 18:56:28  oes
164  *    CRLF -> LF
165  *
166  *    Revision 1.3  2001/05/20 01:21:20  jongfoster
167  *    Version 2.9.4 checkin.
168  *    - Merged popupfile and cookiefile, and added control over PCRS
169  *      filtering, in new "permissionsfile".
170  *    - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
171  *      file error you now get a message box (in the Win32 GUI) rather
172  *      than the program exiting with no explanation.
173  *    - Made killpopup use the PCRS MIME-type checking and HTTP-header
174  *      skipping.
175  *    - Removed tabs from "config"
176  *    - Moved duplicated url parsing code in "loaders.c" to a new funcition.
177  *    - Bumped up version number.
178  *
179  *    Revision 1.2  2001/05/17 23:01:01  oes
180  *     - Cleaned CRLF's from the sources and related files
181  *
182  *    Revision 1.1.1.1  2001/05/15 13:58:59  oes
183  *    Initial import of version 2.9.3 source tree
184  *
185  *
186  *********************************************************************/
187 \f
188
189 #include "config.h"
190
191 #include <stdio.h>
192 #include <stdlib.h>
193 #include <sys/types.h>
194 #include <string.h>
195 #include <malloc.h>
196 #include <errno.h>
197 #include <sys/stat.h>
198 #include <ctype.h>
199 #include <assert.h>
200
201 #if !defined(_WIN32) && !defined(__OS2__)
202 #include <unistd.h>
203 #endif
204
205 #include "project.h"
206 #include "list.h"
207 #include "loaders.h"
208 #include "filters.h"
209 #include "parsers.h"
210 #include "jcc.h"
211 #include "miscutil.h"
212 #include "errlog.h"
213 #include "actions.h"
214
215 const char loaders_h_rcs[] = LOADERS_H_VERSION;
216
217 /*
218  * Currently active files.
219  * These are also entered in the main linked list of files.
220  */
221
222 #ifdef FEATURE_TRUST
223 static struct file_list *current_trustfile      = NULL;
224 #endif /* def FEATURE_TRUST */
225
226 static struct file_list *current_re_filterfile  = NULL;
227
228
229
230 /*********************************************************************
231  *
232  * Function    :  sweep
233  *
234  * Description :  Basically a mark and sweep garbage collector, it is run
235  *                (by the parent thread) every once in a while to reclaim memory.
236  *
237  * It uses a mark and sweep strategy:
238  *   1) mark all files as inactive
239  *
240  *   2) check with each client:
241  *       if it is active,   mark its files as active
242  *       if it is inactive, free its resources
243  *
244  *   3) free the resources of all of the files that
245  *      are still marked as inactive (and are obsolete).
246  *
247  *   N.B. files that are not obsolete don't have an unloader defined.
248  *
249  * Parameters  :  None
250  *
251  * Returns     :  N/A
252  *
253  *********************************************************************/
254 void sweep(void)
255 {
256    struct file_list *fl, *nfl;
257    struct client_state *csp, *ncsp;
258
259    /* clear all of the file's active flags */
260    for ( fl = files->next; NULL != fl; fl = fl->next )
261    {
262       fl->active = 0;
263    }
264
265    for (csp = clients; csp && (ncsp = csp->next) ; csp = csp->next)
266    {
267       if (ncsp->flags & CSP_FLAG_ACTIVE)
268       {
269          /* mark this client's files as active */
270
271          /*
272           * Always have a configuration file.
273           * (Also note the slightly non-standard extra
274           * indirection here.)
275           */
276          ncsp->config->config_file_list->active = 1;
277
278          if (ncsp->actions_list)     /* actions files */
279          {
280             ncsp->actions_list->active = 1;
281          }
282
283          if (ncsp->rlist)     /* pcrsjob files */
284          {
285             ncsp->rlist->active = 1;
286          }
287
288 #ifdef FEATURE_TRUST
289          if (ncsp->tlist)     /* trust files */
290          {
291             ncsp->tlist->active = 1;
292          }
293 #endif /* def FEATURE_TRUST */
294
295       }
296       else
297       /*
298        * this client is not active, release its resources
299        * and the ones of all inactive clients that might
300        * follow it
301        */
302       {
303          while (!(ncsp->flags & CSP_FLAG_ACTIVE))
304          {
305             csp->next = ncsp->next;
306
307             freez(ncsp->ip_addr_str);
308             freez(ncsp->my_ip_addr_str);
309             freez(ncsp->my_hostname);
310             freez(ncsp->x_forwarded);
311             freez(ncsp->iob->buf);
312
313             free_http_request(ncsp->http);
314
315             destroy_list(ncsp->headers);
316             destroy_list(ncsp->cookie_list);
317
318             free_current_action(ncsp->action);
319
320 #ifdef FEATURE_STATISTICS
321             urls_read++;
322             if (ncsp->flags & CSP_FLAG_REJECTED)
323             {
324                urls_rejected++;
325             }
326 #endif /* def FEATURE_STATISTICS */
327
328             freez(ncsp);
329
330             /* are there any more in sequence after it? */
331             if( !(ncsp = csp->next) )
332                break;
333          }
334       }
335    }
336
337    for (fl = files; fl && (nfl = fl->next) ; fl = fl->next)
338    {
339       if ( ( 0 == nfl->active ) && ( NULL != nfl->unloader ) )
340       {
341          fl->next = nfl->next;
342
343          (nfl->unloader)(nfl->f);
344
345          freez(nfl->filename);
346
347          freez(nfl);
348       }
349    }
350
351 }
352
353
354 /*********************************************************************
355  *
356  * Function    :  create_url_spec
357  *
358  * Description :  Creates a "url_spec" structure from a string.
359  *                When finished, free with unload_url().
360  *
361  * Parameters  :
362  *          1  :  url = Target url_spec to be filled in.  Must be
363  *                      zeroed out before the call (e.g. using zalloc).
364  *          2  :  buf = Source pattern, null terminated.  NOTE: The
365  *                      contents of this buffer are destroyed by this
366  *                      function.  If this function succeeds, the
367  *                      buffer is copied to url->spec.  If this
368  *                      function fails, the contents of the buffer
369  *                      are lost forever.
370  *
371  * Returns     :  JB_ERR_OK - Success
372  *                JB_ERR_MEMORY - Out of memory
373  *                JB_ERR_PARSE - Cannot parse regex (Detailed message
374  *                               written to system log)
375  *
376  *********************************************************************/
377 jb_err create_url_spec(struct url_spec * url, char * buf)
378 {
379    char *p;
380    struct url_spec tmp_url[1];
381
382    assert(url);
383    assert(buf);
384
385    /* save a copy of the orignal specification */
386    if ((url->spec = strdup(buf)) == NULL)
387    {
388       return JB_ERR_MEMORY;
389    }
390
391    if ((p = strchr(buf, '/')))
392    {
393       if (NULL == (url->path = strdup(p)))
394       {
395          freez(url->spec);
396          return JB_ERR_MEMORY;
397       }
398       url->pathlen = strlen(url->path);
399       *p = '\0';
400    }
401    else
402    {
403       url->path    = NULL;
404       url->pathlen = 0;
405    }
406 #ifdef REGEX
407    if (url->path)
408    {
409       int errcode;
410       char rebuf[BUFFER_SIZE];
411
412       if (NULL == (url->preg = zalloc(sizeof(*url->preg))))
413       {
414          freez(url->spec);
415          freez(url->path);
416          return JB_ERR_MEMORY;
417       }
418
419       sprintf(rebuf, "^(%s)", url->path);
420
421       errcode = regcomp(url->preg, rebuf,
422             (REG_EXTENDED|REG_NOSUB|REG_ICASE));
423       if (errcode)
424       {
425          size_t errlen = regerror(errcode,
426             url->preg, rebuf, sizeof(rebuf));
427
428          if (errlen > (sizeof(rebuf) - (size_t)1))
429          {
430             errlen = sizeof(rebuf) - (size_t)1;
431          }
432          rebuf[errlen] = '\0';
433
434          log_error(LOG_LEVEL_ERROR, "error compiling %s: %s",
435             url->spec, rebuf);
436
437          freez(url->spec);
438          freez(url->path);
439          freez(url->preg);
440
441          return JB_ERR_PARSE;
442       }
443    }
444 #endif
445    if ((p = strchr(buf, ':')) == NULL)
446    {
447       url->port = 0;
448    }
449    else
450    {
451       *p++ = '\0';
452       url->port = atoi(p);
453    }
454
455    if ((url->domain = strdup(buf)) == NULL)
456    {
457       freez(url->spec);
458       freez(url->path);
459 #ifdef REGEX
460       freez(url->preg);
461 #endif /* def REGEX */
462       return JB_ERR_MEMORY;
463    }
464
465    /* split domain into components */
466
467    *tmp_url = dsplit(url->domain);
468    if (tmp_url->dbuf == NULL)
469    {
470       freez(url->spec);
471       freez(url->path);
472       freez(url->domain);
473 #ifdef REGEX
474       freez(url->preg);
475 #endif /* def REGEX */
476       return JB_ERR_MEMORY;
477    }
478
479    url->dbuf = tmp_url->dbuf;
480    url->dcnt = tmp_url->dcnt;
481    url->dvec = tmp_url->dvec;
482    url->unanchored = tmp_url->unanchored;
483
484    return JB_ERR_OK;
485
486 }
487
488
489 /*********************************************************************
490  *
491  * Function    :  free_url
492  *
493  * Description :  Called from the "unloaders".  Freez the url
494  *                structure elements.
495  *
496  * Parameters  :
497  *          1  :  url = pointer to a url_spec structure.
498  *
499  * Returns     :  N/A
500  *
501  *********************************************************************/
502 void free_url(struct url_spec *url)
503 {
504    if (url == NULL) return;
505
506    freez(url->spec);
507    freez(url->domain);
508    freez(url->dbuf);
509    freez(url->dvec);
510    freez(url->path);
511 #ifdef REGEX
512    if (url->preg)
513    {
514       regfree(url->preg);
515       freez(url->preg);
516    }
517 #endif
518
519 }
520
521
522 /*********************************************************************
523  *
524  * Function    :  check_file_changed
525  *
526  * Description :  Helper function to check if a file needs reloading.
527  *                If "current" is still current, return it.  Otherwise
528  *                allocates a new (zeroed) "struct file_list", fills
529  *                in the disk file name and timestamp, and returns it.
530  *
531  * Parameters  :
532  *          1  :  current = The file_list currently being used - will
533  *                          be checked to see if it is out of date.
534  *                          May be NULL (which is treated as out of
535  *                          date).
536  *          2  :  filename = Name of file to check.
537  *          3  :  newfl    = New file list. [Output only]
538  *                           This will be set to NULL, OR a struct
539  *                           file_list newly allocated on the
540  *                           heap, with the filename and lastmodified
541  *                           fields filled, and all others zeroed.
542  *
543  * Returns     :  If file unchanged: 0 (and sets newfl == NULL)
544  *                If file changed: 1 and sets newfl != NULL
545  *                On error: 1 and sets newfl == NULL
546  *
547  *********************************************************************/
548 int check_file_changed(const struct file_list * current,
549                        const char * filename,
550                        struct file_list ** newfl)
551 {
552    struct file_list *fs;
553    struct stat statbuf[1];
554
555    *newfl = NULL;
556
557    if (stat(filename, statbuf) < 0)
558    {
559       /* Error, probably file not found. */
560       return 1;
561    }
562
563    if (current
564        && (current->lastmodified == statbuf->st_mtime)
565        && (0 == strcmp(current->filename, filename)))
566    {
567       return 0;
568    }
569
570    fs = (struct file_list *)zalloc(sizeof(struct file_list));
571
572    if (fs == NULL)
573    {
574       /* Out of memory error */
575       return 1;
576    }
577
578    fs->filename = strdup(filename);
579    fs->lastmodified = statbuf->st_mtime;
580
581    if (fs->filename == NULL)
582    {
583       /* Out of memory error */
584       freez (fs);
585       return 1;
586    }
587
588
589    *newfl = fs;
590    return 1;
591
592 }
593
594
595 /*********************************************************************
596  *
597  * Function    :  read_config_line
598  *
599  * Description :  Read a single non-empty line from a file and return
600  *                it.  Trims comments, leading and trailing whitespace
601  *                and respects escaping of newline and comment char.
602  *
603  * Parameters  :
604  *          1  :  buf = Buffer to use.
605  *          2  :  buflen = Size of buffer in bytes.
606  *          3  :  fp = File to read from
607  *
608  * Returns     :  NULL on EOF or error
609  *                Otherwise, returns buf.
610  *
611  *********************************************************************/
612 char *read_config_line(char *buf, int buflen, FILE *fp)
613 {
614    char *p;
615    char *src;
616    char *dest;
617    char linebuf[BUFFER_SIZE];
618    int contflag = 0;
619
620    *buf = '\0';
621
622    while (fgets(linebuf, sizeof(linebuf), fp))
623    {
624       /* Trim off newline */
625       if ((p = strpbrk(linebuf, "\r\n")) != NULL)
626       {
627          *p = '\0';
628       }
629       else
630       {
631          p = linebuf + strlen(linebuf);
632       }
633
634       /* Line continuation? Trim escape and set flag. */
635       if ((p != linebuf) && (*--p == '\\'))
636       {
637          contflag = 1;
638          *p = '\0';
639       }
640
641       /* If there's a comment char.. */
642       p = linebuf;
643       while ((p = strchr(p, '#')) != NULL)
644       {
645          /* ..and it's escaped, left-shift the line over the escape. */
646          if ((p != linebuf) && (*(p-1) == '\\'))
647          {
648             src = p;
649             dest = p - 1;
650             while ((*dest++ = *src++) != '\0')
651             {
652                /* nop */
653             }
654             /* Now scan from just after the "#". */
655          }
656          /* Else, chop off the rest of the line */
657          else
658          {
659             *p = '\0';
660          }
661       }
662
663       /* Write to the buffer */
664       if (*linebuf)
665       {
666          strncat(buf, linebuf, buflen - strlen(buf));
667       }
668
669       /* Continue? */
670       if (contflag)
671       {
672          contflag = 0;
673          continue;
674       }
675
676       /* Remove leading and trailing whitespace */
677       chomp(buf);
678
679       if (*buf)
680       {
681          return buf;
682       }
683    }
684
685    /* EOF */
686    return NULL;
687
688 }
689
690
691 #ifdef FEATURE_TRUST
692 /*********************************************************************
693  *
694  * Function    :  unload_trustfile
695  *
696  * Description :  Unloads a trustfile.
697  *
698  * Parameters  :
699  *          1  :  f = the data structure associated with the trustfile.
700  *
701  * Returns     :  N/A
702  *
703  *********************************************************************/
704 static void unload_trustfile(void *f)
705 {
706    struct block_spec *b = (struct block_spec *)f;
707    if (b == NULL) return;
708
709    unload_trustfile(b->next); /* Stack is cheap, isn't it? */
710
711    free_url(b->url);
712
713    freez(b);
714
715 }
716
717
718 /*********************************************************************
719  *
720  * Function    :  load_trustfile
721  *
722  * Description :  Read and parse a trustfile and add to files list.
723  *
724  * Parameters  :
725  *          1  :  csp = Current client state (buffers, headers, etc...)
726  *
727  * Returns     :  0 => Ok, everything else is an error.
728  *
729  *********************************************************************/
730 int load_trustfile(struct client_state *csp)
731 {
732    FILE *fp;
733
734    struct block_spec *b, *bl;
735    struct url_spec **tl;
736
737    char  buf[BUFFER_SIZE], *p, *q;
738    int reject, trusted;
739    struct file_list *fs;
740
741    if (!check_file_changed(current_trustfile, csp->config->trustfile, &fs))
742    {
743       /* No need to load */
744       if (csp)
745       {
746          csp->tlist = current_trustfile;
747       }
748       return(0);
749    }
750    if (!fs)
751    {
752       goto load_trustfile_error;
753    }
754
755    fs->f = bl = (struct block_spec *)zalloc(sizeof(*bl));
756    if (bl == NULL)
757    {
758       goto load_trustfile_error;
759    }
760
761    if ((fp = fopen(csp->config->trustfile, "r")) == NULL)
762    {
763       goto load_trustfile_error;
764    }
765
766    tl = csp->config->trust_list;
767
768    while (read_config_line(buf, sizeof(buf), fp) != NULL)
769    {
770       trusted = 0;
771       reject  = 1;
772
773       if (*buf == '+')
774       {
775          trusted = 1;
776          *buf = '~';
777       }
778
779       if (*buf == '~')
780       {
781          reject = 0;
782          p = buf;
783          q = p+1;
784          while ((*p++ = *q++))
785          {
786             /* nop */
787          }
788       }
789
790       /* skip blank lines */
791       if (*buf == '\0')
792       {
793          continue;
794       }
795
796       /* allocate a new node */
797       if ((b = zalloc(sizeof(*b))) == NULL)
798       {
799          fclose(fp);
800          goto load_trustfile_error;
801       }
802
803       /* add it to the list */
804       b->next  = bl->next;
805       bl->next = b;
806
807       b->reject = reject;
808
809       /* Save the URL pattern */
810       if (create_url_spec(b->url, buf))
811       {
812          fclose(fp);
813          goto load_trustfile_error;
814       }
815
816       /*
817        * save a pointer to URL's spec in the list of trusted URL's, too
818        */
819       if (trusted)
820       {
821          *tl++ = b->url;
822       }
823    }
824
825    *tl = NULL;
826
827    fclose(fp);
828
829    /* the old one is now obsolete */
830    if (current_trustfile)
831    {
832       current_trustfile->unloader = unload_trustfile;
833    }
834
835    fs->next    = files->next;
836    files->next = fs;
837    current_trustfile = fs;
838
839    if (csp)
840    {
841       csp->tlist = fs;
842    }
843
844    return(0);
845
846 load_trustfile_error:
847    log_error(LOG_LEVEL_FATAL, "can't load trustfile '%s': %E",
848              csp->config->trustfile);
849    return(-1);
850
851 }
852 #endif /* def FEATURE_TRUST */
853
854
855 /*********************************************************************
856  *
857  * Function    :  unload_re_filterfile
858  *
859  * Description :  Unload the re_filter list.
860  *
861  * Parameters  :
862  *          1  :  f = the data structure associated with the filterfile.
863  *
864  * Returns     :  N/A
865  *
866  *********************************************************************/
867 static void unload_re_filterfile(void *f)
868 {
869    struct re_filterfile_spec *b = (struct re_filterfile_spec *)f;
870
871    if (b == NULL)
872    {
873       return;
874    }
875
876    destroy_list(b->patterns);
877    pcrs_free_joblist(b->joblist);
878    freez(b);
879
880    return;
881 }
882
883 /*********************************************************************
884  *
885  * Function    :  load_re_filterfile
886  *
887  * Description :  Load the re_filterfile. Each non-comment, non-empty
888  *                line is instantly added to the joblist, which is
889  *                a chained list of pcrs_job structs.
890  *
891  * Parameters  :
892  *          1  :  csp = Current client state (buffers, headers, etc...)
893  *
894  * Returns     :  0 => Ok, everything else is an error.
895  *
896  *********************************************************************/
897 int load_re_filterfile(struct client_state *csp)
898 {
899    FILE *fp;
900
901    struct re_filterfile_spec *bl;
902    struct file_list *fs;
903
904    char  buf[BUFFER_SIZE];
905    int error;
906    pcrs_job *dummy;
907
908    if (!check_file_changed(current_re_filterfile, csp->config->re_filterfile, &fs))
909    {
910       /* No need to load */
911       if (csp)
912       {
913          csp->rlist = current_re_filterfile;
914       }
915       return(0);
916    }
917    if (!fs)
918    {
919       goto load_re_filterfile_error;
920    }
921
922    fs->f = bl = (struct re_filterfile_spec  *)zalloc(sizeof(*bl));
923    if (bl == NULL)
924    {
925       goto load_re_filterfile_error;
926    }
927
928    /* Open the file or fail */
929    if ((fp = fopen(csp->config->re_filterfile, "r")) == NULL)
930    {
931       goto load_re_filterfile_error;
932    }
933
934    /* Read line by line */
935    while (read_config_line(buf, sizeof(buf), fp) != NULL)
936    {
937       enlist( bl->patterns, buf );
938
939       /* We have a meaningful line -> make it a job */
940       if ((dummy = pcrs_compile_command(buf, &error)) == NULL)
941       {
942          log_error(LOG_LEVEL_RE_FILTER,
943                "Adding re_filter job %s failed with error %d.", buf, error);
944          continue;
945       }
946       else
947       {
948          dummy->next = bl->joblist;
949          bl->joblist = dummy;
950          log_error(LOG_LEVEL_RE_FILTER, "Adding re_filter job %s succeeded.", buf);
951       }
952    }
953
954    fclose(fp);
955
956    /* the old one is now obsolete */
957    if ( NULL != current_re_filterfile )
958    {
959       current_re_filterfile->unloader = unload_re_filterfile;
960    }
961
962    fs->next    = files->next;
963    files->next = fs;
964    current_re_filterfile = fs;
965
966    if (csp)
967    {
968       csp->rlist = fs;
969    }
970
971    return( 0 );
972
973 load_re_filterfile_error:
974    log_error(LOG_LEVEL_FATAL, "can't load re_filterfile '%s': %E",
975              csp->config->re_filterfile);
976    return(-1);
977
978 }
979
980
981 /*********************************************************************
982  *
983  * Function    :  add_loader
984  *
985  * Description :  Called from `load_config'.  Called once for each input
986  *                file found in config.
987  *
988  * Parameters  :
989  *          1  :  loader = pointer to a function that can parse and load
990  *                the appropriate config file.
991  *          2  :  config = The configuration_spec to add the loader to.
992  *
993  * Returns     :  N/A
994  *
995  *********************************************************************/
996 void add_loader(int (*loader)(struct client_state *),
997                 struct configuration_spec * config)
998 {
999    int i;
1000
1001    for (i=0; i < NLOADERS; i++)
1002    {
1003       if (config->loaders[i] == NULL)
1004       {
1005          config->loaders[i] = loader;
1006          break;
1007       }
1008    }
1009
1010 }
1011
1012
1013 /*********************************************************************
1014  *
1015  * Function    :  run_loader
1016  *
1017  * Description :  Called from `load_config' and `listen_loop'.  This
1018  *                function keeps the "csp" current with any file mods
1019  *                since the last loop.  If a file is unchanged, the
1020  *                loader functions do NOT reload the file.
1021  *
1022  * Parameters  :
1023  *          1  :  csp = Current client state (buffers, headers, etc...)
1024  *                      Must be non-null.  Reads: "csp->config"
1025  *                      Writes: various data members.
1026  *
1027  * Returns     :  0 => Ok, everything else is an error.
1028  *
1029  *********************************************************************/
1030 int run_loader(struct client_state *csp)
1031 {
1032    int ret = 0;
1033    int i;
1034
1035    for (i=0; i < NLOADERS; i++)
1036    {
1037       if (csp->config->loaders[i] == NULL)
1038       {
1039          break;
1040       }
1041       ret |= (csp->config->loaders[i])(csp);
1042    }
1043    return(ret);
1044
1045 }
1046
1047
1048 /*
1049   Local Variables:
1050   tab-width: 3
1051   end:
1052 */