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