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