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