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