Bugfix: Multiple escaped "#" characters in a configuration
[privoxy.git] / loaders.c
1 const char loaders_rcs[] = "$Id: loaders.c,v 1.25 2001/09/13 22:44:03 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.25  2001/09/13 22:44:03  jongfoster
39  *    Adding {} to an if statement
40  *
41  *    Revision 1.24  2001/07/30 22:08:36  jongfoster
42  *    Tidying up #defines:
43  *    - All feature #defines are now of the form FEATURE_xxx
44  *    - Permanently turned off WIN_GUI_EDIT
45  *    - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
46  *
47  *    Revision 1.23  2001/07/20 15:51:54  oes
48  *    Fixed indentation of prepocessor commands
49  *
50  *    Revision 1.22  2001/07/20 15:16:17  haroon
51  *    - per Guy's suggestion, added a while loop in sweep() to catch not just
52  *      the last inactive CSP but all other consecutive inactive CSPs after that
53  *      as well
54  *
55  *    Revision 1.21  2001/07/18 17:26:24  oes
56  *    Changed to conform to new pcrs interface
57  *
58  *    Revision 1.20  2001/07/17 13:07:01  oes
59  *    Fixed segv when last line in config files
60  *     lacked a terminating (\r)\n
61  *
62  *    Revision 1.19  2001/07/13 14:01:54  oes
63  *    Removed all #ifdef PCRS
64  *
65  *    Revision 1.18  2001/06/29 21:45:41  oes
66  *    Indentation, CRLF->LF, Tab-> Space
67  *
68  *    Revision 1.17  2001/06/29 13:31:51  oes
69  *    Various adaptions
70  *
71  *    Revision 1.16  2001/06/09 10:55:28  jongfoster
72  *    Changing BUFSIZ ==> BUFFER_SIZE
73  *
74  *    Revision 1.15  2001/06/07 23:14:14  jongfoster
75  *    Removing ACL and forward file loaders - these
76  *    files have been merged into the config file.
77  *    Cosmetic: Moving unloader funcs next to their
78  *    respective loader funcs
79  *
80  *    Revision 1.14  2001/06/01 03:27:04  oes
81  *    Fixed line continuation problem
82  *
83  *    Revision 1.13  2001/05/31 21:28:49  jongfoster
84  *    Removed all permissionsfile code - it's now called the actions
85  *    file, and (almost) all the code is in actions.c
86  *
87  *    Revision 1.12  2001/05/31 17:32:31  oes
88  *
89  *     - Enhanced domain part globbing with infix and prefix asterisk
90  *       matching and optional unanchored operation
91  *
92  *    Revision 1.11  2001/05/29 23:25:24  oes
93  *
94  *     - load_config_line() and load_permissions_file() now use chomp()
95  *
96  *    Revision 1.10  2001/05/29 09:50:24  jongfoster
97  *    Unified blocklist/imagelist/permissionslist.
98  *    File format is still under discussion, but the internal changes
99  *    are (mostly) done.
100  *
101  *    Also modified interceptor behaviour:
102  *    - We now intercept all URLs beginning with one of the following
103  *      prefixes (and *only* these prefixes):
104  *        * http://i.j.b/
105  *        * http://ijbswa.sf.net/config/
106  *        * http://ijbswa.sourceforge.net/config/
107  *    - New interceptors "home page" - go to http://i.j.b/ to see it.
108  *    - Internal changes so that intercepted and fast redirect pages
109  *      are not replaced with an image.
110  *    - Interceptors now have the option to send a binary page direct
111  *      to the client. (i.e. ijb-send-banner uses this)
112  *    - Implemented show-url-info interceptor.  (Which is why I needed
113  *      the above interceptors changes - a typical URL is
114  *      "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
115  *      The previous mechanism would not have intercepted that, and
116  *      if it had been intercepted then it then it would have replaced
117  *      it with an image.)
118  *
119  *    Revision 1.9  2001/05/26 17:12:07  jongfoster
120  *    Fatal errors loading configuration files now give better error messages.
121  *
122  *    Revision 1.8  2001/05/26 00:55:20  jongfoster
123  *    Removing duplicated code.  load_forwardfile() now uses create_url_spec()
124  *
125  *    Revision 1.7  2001/05/26 00:28:36  jongfoster
126  *    Automatic reloading of config file.
127  *    Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
128  *    Most of the global variables have been moved to a new
129  *    struct configuration_spec, accessed through csp->config->globalname
130  *    Most of the globals remaining are used by the Win32 GUI.
131  *
132  *    Revision 1.6  2001/05/23 12:27:33  oes
133  *
134  *    Fixed ugly indentation of my last changes
135  *
136  *    Revision 1.5  2001/05/23 10:39:05  oes
137  *    - Added support for escaping the comment character
138  *      in config files by a backslash
139  *    - Added support for line continuation in config
140  *      files
141  *    - Fixed a buffer overflow bug with long config lines
142  *
143  *    Revision 1.4  2001/05/22 18:56:28  oes
144  *    CRLF -> LF
145  *
146  *    Revision 1.3  2001/05/20 01:21:20  jongfoster
147  *    Version 2.9.4 checkin.
148  *    - Merged popupfile and cookiefile, and added control over PCRS
149  *      filtering, in new "permissionsfile".
150  *    - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
151  *      file error you now get a message box (in the Win32 GUI) rather
152  *      than the program exiting with no explanation.
153  *    - Made killpopup use the PCRS MIME-type checking and HTTP-header
154  *      skipping.
155  *    - Removed tabs from "config"
156  *    - Moved duplicated url parsing code in "loaders.c" to a new funcition.
157  *    - Bumped up version number.
158  *
159  *    Revision 1.2  2001/05/17 23:01:01  oes
160  *     - Cleaned CRLF's from the sources and related files
161  *
162  *    Revision 1.1.1.1  2001/05/15 13:58:59  oes
163  *    Initial import of version 2.9.3 source tree
164  *
165  *
166  *********************************************************************/
167 \f
168
169 #include "config.h"
170
171 #include <stdio.h>
172 #include <stdlib.h>
173 #include <sys/types.h>
174 #include <string.h>
175 #include <malloc.h>
176 #include <errno.h>
177 #include <sys/stat.h>
178 #include <ctype.h>
179
180 #ifndef _WIN32
181 #include <unistd.h>
182 #endif
183
184 #include "project.h"
185 #include "list.h"
186 #include "loaders.h"
187 #include "filters.h"
188 #include "parsers.h"
189 #include "jcc.h"
190 #include "miscutil.h"
191 #include "errlog.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;
596    char *src;
597    char *dest;
598    char linebuf[BUFFER_SIZE];
599    int contflag = 0;
600
601    *buf = '\0';
602
603    while (fgets(linebuf, sizeof(linebuf), fp))
604    {
605       /* Trim off newline */
606       if ((p = strpbrk(linebuf, "\r\n")) != NULL)
607       {
608          *p = '\0';
609       }
610       else
611       {
612          p = linebuf + strlen(linebuf);
613       }
614
615       /* Line continuation? Trim escape and set flag. */
616       if ((p != linebuf) && (*--p == '\\'))
617       {
618          contflag = 1;
619          *p = '\0';
620       }
621
622       /* If there's a comment char.. */
623       p = linebuf;
624       while ((p = strchr(p, '#')) != NULL)
625       {
626          /* ..and it's escaped, left-shift the line over the escape. */
627          if ((p != linebuf) && (*(p-1) == '\\'))
628          {
629             src = p;
630             dest = p - 1;
631             while ((*dest++ = *src++) != '\0')
632             {
633                /* nop */
634             }
635             /* Now scan from just after the "#". */
636          }
637          /* Else, chop off the rest of the line */
638          else
639          {
640             *p = '\0';
641          }
642       }
643
644       /* Write to the buffer */
645       if (*linebuf)
646       {
647          strncat(buf, linebuf, buflen - strlen(buf));
648       }
649
650       /* Continue? */
651       if (contflag)
652       {
653          contflag = 0;
654          continue;
655       }
656
657       /* Remove leading and trailing whitespace */         
658       chomp(buf);
659
660       if (*buf)
661       {
662          return buf;
663       }
664    }
665
666    /* EOF */
667    return NULL;
668
669 }
670
671
672 #ifdef FEATURE_TRUST
673 /*********************************************************************
674  *
675  * Function    :  unload_trustfile
676  *
677  * Description :  Unloads a trustfile.
678  *
679  * Parameters  :
680  *          1  :  f = the data structure associated with the trustfile.
681  *
682  * Returns     :  N/A
683  *
684  *********************************************************************/
685 static void unload_trustfile(void *f)
686 {
687    struct block_spec *b = (struct block_spec *)f;
688    if (b == NULL) return;
689
690    unload_trustfile(b->next); /* Stack is cheap, isn't it? */
691
692    free_url(b->url);
693
694    freez(b);
695
696 }
697
698
699 /*********************************************************************
700  *
701  * Function    :  load_trustfile
702  *
703  * Description :  Read and parse a trustfile and add to files list.
704  *
705  * Parameters  :
706  *          1  :  csp = Current client state (buffers, headers, etc...)
707  *
708  * Returns     :  0 => Ok, everything else is an error.
709  *
710  *********************************************************************/
711 int load_trustfile(struct client_state *csp)
712 {
713    FILE *fp;
714
715    struct block_spec *b, *bl;
716    struct url_spec **tl;
717
718    char  buf[BUFFER_SIZE], *p, *q;
719    int reject, trusted;
720    struct file_list *fs;
721
722    if (!check_file_changed(current_trustfile, csp->config->trustfile, &fs))
723    {
724       /* No need to load */
725       if (csp)
726       {
727          csp->tlist = current_trustfile;
728       }
729       return(0);
730    }
731    if (!fs)
732    {
733       goto load_trustfile_error;
734    }
735
736    fs->f = bl = (struct block_spec *)zalloc(sizeof(*bl));
737    if (bl == NULL)
738    {
739       goto load_trustfile_error;
740    }
741
742    if ((fp = fopen(csp->config->trustfile, "r")) == NULL)
743    {
744       goto load_trustfile_error;
745    }
746
747    tl = csp->config->trust_list;
748
749    while (read_config_line(buf, sizeof(buf), fp, fs) != NULL)
750    {
751       trusted = 0;
752       reject  = 1;
753
754       if (*buf == '+')
755       {
756          trusted = 1;
757          *buf = '~';
758       }
759
760       if (*buf == '~')
761       {
762          reject = 0;
763          p = buf;
764          q = p+1;
765          while ((*p++ = *q++))
766          {
767             /* nop */
768          }
769       }
770
771       /* skip blank lines */
772       if (*buf == '\0')
773       {
774          continue;
775       }
776
777       /* allocate a new node */
778       if ((b = zalloc(sizeof(*b))) == NULL)
779       {
780          fclose(fp);
781          goto load_trustfile_error;
782       }
783
784       /* add it to the list */
785       b->next  = bl->next;
786       bl->next = b;
787
788       b->reject = reject;
789
790       /* Save the URL pattern */
791       if (create_url_spec(b->url, buf))
792       {
793          fclose(fp);
794          goto load_trustfile_error;
795       }
796
797       /*
798        * save a pointer to URL's spec in the list of trusted URL's, too
799        */
800       if (trusted)
801       {
802          *tl++ = b->url;
803       }
804    }
805
806    *tl = NULL;
807
808    fclose(fp);
809
810    /* the old one is now obsolete */
811    if (current_trustfile)
812    {
813       current_trustfile->unloader = unload_trustfile;
814    }
815
816    fs->next    = files->next;
817    files->next = fs;
818    current_trustfile = fs;
819
820    if (csp)
821    {
822       csp->tlist = fs;
823    }
824
825    return(0);
826
827 load_trustfile_error:
828    log_error(LOG_LEVEL_FATAL, "can't load trustfile '%s': %E",
829              csp->config->trustfile);
830    return(-1);
831
832 }
833 #endif /* def FEATURE_TRUST */
834
835
836 /*********************************************************************
837  *
838  * Function    :  unload_re_filterfile
839  *
840  * Description :  Unload the re_filter list.
841  *
842  * Parameters  :
843  *          1  :  f = the data structure associated with the filterfile.
844  *
845  * Returns     :  N/A
846  *
847  *********************************************************************/
848 static void unload_re_filterfile(void *f)
849 {
850    struct re_filterfile_spec *b = (struct re_filterfile_spec *)f;
851
852    if (b == NULL)
853    {
854       return;
855    }
856
857    destroy_list(b->patterns);
858    pcrs_free_joblist(b->joblist);
859    freez(b);
860
861    return;
862 }
863
864 /*********************************************************************
865  *
866  * Function    :  load_re_filterfile
867  *
868  * Description :  Load the re_filterfile. Each non-comment, non-empty
869  *                line is instantly added to the joblist, which is
870  *                a chained list of pcrs_job structs.
871  *
872  * Parameters  :
873  *          1  :  csp = Current client state (buffers, headers, etc...)
874  *
875  * Returns     :  0 => Ok, everything else is an error.
876  *
877  *********************************************************************/
878 int load_re_filterfile(struct client_state *csp)
879 {
880    FILE *fp;
881
882    struct re_filterfile_spec *bl;
883    struct file_list *fs;
884
885    char  buf[BUFFER_SIZE];
886    int error;
887    pcrs_job *dummy;
888
889    if (!check_file_changed(current_re_filterfile, csp->config->re_filterfile, &fs))
890    {
891       /* No need to load */
892       if (csp)
893       {
894          csp->rlist = current_re_filterfile;
895       }
896       return(0);
897    }
898    if (!fs)
899    {
900       goto load_re_filterfile_error;
901    }
902
903    fs->f = bl = (struct re_filterfile_spec  *)zalloc(sizeof(*bl));
904    if (bl == NULL)
905    {
906       goto load_re_filterfile_error;
907    }
908
909    /* Open the file or fail */
910    if ((fp = fopen(csp->config->re_filterfile, "r")) == NULL)
911    {
912       goto load_re_filterfile_error;
913    }
914
915    /* Read line by line */
916    while (read_config_line(buf, sizeof(buf), fp, fs) != NULL)
917    {
918       enlist( bl->patterns, buf );
919
920       /* We have a meaningful line -> make it a job */
921       if ((dummy = pcrs_compile_command(buf, &error)) == NULL)
922       {
923          log_error(LOG_LEVEL_RE_FILTER, 
924                "Adding re_filter job %s failed with error %d.", buf, error);
925          continue;
926       }
927       else
928       {
929          dummy->next = bl->joblist;
930          bl->joblist = dummy;
931          log_error(LOG_LEVEL_RE_FILTER, "Adding re_filter job %s succeeded.", buf);
932       }
933    }
934
935    fclose(fp);
936
937    /* the old one is now obsolete */
938    if ( NULL != current_re_filterfile )
939    {
940       current_re_filterfile->unloader = unload_re_filterfile;
941    }
942
943    fs->next    = files->next;
944    files->next = fs;
945    current_re_filterfile = fs;
946
947    if (csp)
948    {
949       csp->rlist = fs;
950    }
951
952    return( 0 );
953
954 load_re_filterfile_error:
955    log_error(LOG_LEVEL_FATAL, "can't load re_filterfile '%s': %E", 
956              csp->config->re_filterfile);
957    return(-1);
958
959 }
960
961
962 /*********************************************************************
963  *
964  * Function    :  add_loader
965  *
966  * Description :  Called from `load_config'.  Called once for each input
967  *                file found in config.
968  *
969  * Parameters  :
970  *          1  :  loader = pointer to a function that can parse and load
971  *                the appropriate config file.
972  *          2  :  config = The configuration_spec to add the loader to.
973  *
974  * Returns     :  N/A
975  *
976  *********************************************************************/
977 void add_loader(int (*loader)(struct client_state *), 
978                 struct configuration_spec * config)
979 {
980    int i;
981
982    for (i=0; i < NLOADERS; i++)
983    {
984       if (config->loaders[i] == NULL)
985       {
986          config->loaders[i] = loader;
987          break;
988       }
989    }
990
991 }
992
993
994 /*********************************************************************
995  *
996  * Function    :  run_loader
997  *
998  * Description :  Called from `load_config' and `listen_loop'.  This
999  *                function keeps the "csp" current with any file mods
1000  *                since the last loop.  If a file is unchanged, the
1001  *                loader functions do NOT reload the file.
1002  *
1003  * Parameters  :
1004  *          1  :  csp = Current client state (buffers, headers, etc...)
1005  *                      Must be non-null.  Reads: "csp->config"
1006  *                      Writes: various data members.
1007  *
1008  * Returns     :  0 => Ok, everything else is an error.
1009  *
1010  *********************************************************************/
1011 int run_loader(struct client_state *csp)
1012 {
1013    int ret = 0;
1014    int i;
1015
1016    for (i=0; i < NLOADERS; i++)
1017    {
1018       if (csp->config->loaders[i] == NULL)
1019       {
1020          break;
1021       }
1022       ret |= (csp->config->loaders[i])(csp);
1023    }
1024    return(ret);
1025
1026 }
1027
1028
1029 /*
1030   Local Variables:
1031   tab-width: 3
1032   end:
1033 */