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