Add some CSP_FLAGs for the header parsers.
[privoxy.git] / loaders.c
1 const char loaders_rcs[] = "$Id: loaders.c,v 1.58 2006/12/31 14:25:20 fabiankeil 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-2007 the SourceForge
12  *                Privoxy team. http://www.privoxy.org/
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.58  2006/12/31 14:25:20  fabiankeil
39  *    Fix gcc43 compiler warnings.
40  *
41  *    Revision 1.57  2006/12/21 12:22:22  fabiankeil
42  *    html_encode filter descriptions.
43  *
44  *    Have "Ignoring job ..." error messages
45  *    print the filter file name correctly.
46  *
47  *    Revision 1.56  2006/09/07 10:40:30  fabiankeil
48  *    Turns out trusted referrers above our arbitrary
49  *    limit are downgraded too ordinary trusted URLs.
50  *    Adjusted error message.
51  *
52  *    Revision 1.55  2006/09/07 10:25:39  fabiankeil
53  *    Fix typo.
54  *
55  *    Revision 1.54  2006/09/07 10:22:20  fabiankeil
56  *    If too many trusted referrers are used,
57  *    print only one error message instead of logging
58  *    every single trusted referrer above the arbitrary
59  *    limit.
60  *
61  *    Revision 1.53  2006/08/31 16:25:06  fabiankeil
62  *    Work around a buffer overflow that caused Privoxy to
63  *    segfault if too many trusted referrers were used. Good
64  *    enough for now, but should be replaced with a real
65  *    solution after the next release.
66  *
67  *    Revision 1.52  2006/07/18 14:48:46  david__schmidt
68  *    Reorganizing the repository: swapping out what was HEAD (the old 3.1 branch)
69  *    with what was really the latest development (the v_3_0_branch branch)
70  *
71  *    Revision 1.50.2.8  2006/01/30 15:16:25  david__schmidt
72  *    Remove a little residual debugging info
73  *
74  *    Revision 1.50.2.7  2006/01/29 23:10:56  david__schmidt
75  *    Multiple filter file support
76  *
77  *    Revision 1.50.2.6  2003/10/24 10:17:54  oes
78  *    Nit: Allowed tabs as separators in filter headings
79  *
80  *    Revision 1.50.2.5  2003/05/08 15:19:15  oes
81  *    sweep: Made loop structure of sweep step mirror that of mark step
82  *
83  *    Revision 1.50.2.4  2003/05/06 15:57:12  oes
84  *    Bugfix: Update last_active pointer in sweep() before
85  *    leaving an active client. Closes bugs #724395, #727882
86  *
87  *    Revision 1.50.2.3  2002/11/20 17:12:30  oes
88  *    Ooops, forgot one change.
89  *
90  *    Revision 1.50.2.2  2002/11/20 14:38:15  oes
91  *    Fixed delayed/incomplete freeing of client resources and
92  *    simplified loop structure in sweep.
93  *    Thanks to Oliver Stoeneberg for the hint.
94  *
95  *    Revision 1.50.2.1  2002/07/26 15:19:24  oes
96  *    - PCRS jobs now chained in order of appearance. Previous
97  *      reverse chaining was counter-intuitive.
98  *    - Changed loglevel of PCRS job compile errors to
99  *      LOG_LEVEL_ERROR
100  *
101  *    Revision 1.50  2002/04/24 02:12:16  oes
102  *    Jon's multiple AF patch: Sweep now takes care of all AFs
103  *
104  *    Revision 1.49  2002/04/19 16:53:25  jongfoster
105  *    Optimize away a function call by using an equivalent macro
106  *
107  *    Revision 1.48  2002/04/05 00:56:09  gliptak
108  *    Correcting typo to clean up on realloc failure
109  *
110  *    Revision 1.47  2002/03/26 22:29:55  swa
111  *    we have a new homepage!
112  *
113  *    Revision 1.46  2002/03/24 13:25:43  swa
114  *    name change related issues
115  *
116  *    Revision 1.45  2002/03/16 23:54:06  jongfoster
117  *    Adding graceful termination feature, to help look for memory leaks.
118  *    If you enable this (which, by design, has to be done by hand
119  *    editing config.h) and then go to http://i.j.b/die, then the program
120  *    will exit cleanly after the *next* request.  It should free all the
121  *    memory that was used.
122  *
123  *    Revision 1.44  2002/03/16 21:51:00  jongfoster
124  *    Fixing free(NULL).
125  *
126  *    Revision 1.43  2002/03/16 20:28:34  oes
127  *    Added descriptions to the filters so users will know what they select in the cgi editor
128  *
129  *    Revision 1.42  2002/03/13 00:27:05  jongfoster
130  *    Killing warnings
131  *
132  *    Revision 1.41  2002/03/12 01:42:50  oes
133  *    Introduced modular filters
134  *
135  *    Revision 1.40  2002/03/08 17:46:04  jongfoster
136  *    Fixing int/size_t warnings
137  *
138  *    Revision 1.39  2002/03/07 03:46:17  oes
139  *    Fixed compiler warnings
140  *
141  *    Revision 1.38  2002/03/06 22:54:35  jongfoster
142  *    Automated function-comment nitpicking.
143  *
144  *    Revision 1.37  2002/03/03 15:07:49  oes
145  *    Re-enabled automatic config reloading
146  *
147  *    Revision 1.36  2002/01/22 23:46:18  jongfoster
148  *    Moving edit_read_line() and simple_read_line() to loaders.c, and
149  *    extending them to support reading MS-DOS, Mac and UNIX style files
150  *    on all platforms.
151  *
152  *    Modifying read_config_line() (without changing it's prototype) to
153  *    be a trivial wrapper for edit_read_line().  This means that we have
154  *    one function to read a line and handle comments, which is common
155  *    between the initialization code and the edit interface.
156  *
157  *    Revision 1.35  2002/01/17 21:03:08  jongfoster
158  *    Moving all our URL and URL pattern parsing code to urlmatch.c.
159  *
160  *    Renaming free_url to free_url_spec, since it frees a struct url_spec.
161  *
162  *    Revision 1.34  2001/12/30 14:07:32  steudten
163  *    - Add signal handling (unix)
164  *    - Add SIGHUP handler (unix)
165  *    - Add creation of pidfile (unix)
166  *    - Add action 'top' in rc file (RH)
167  *    - Add entry 'SIGNALS' to manpage
168  *    - Add exit message to logfile (unix)
169  *
170  *    Revision 1.33  2001/11/13 00:16:38  jongfoster
171  *    Replacing references to malloc.h with the standard stdlib.h
172  *    (See ANSI or K&R 2nd Ed)
173  *
174  *    Revision 1.32  2001/11/07 00:02:13  steudten
175  *    Add line number in error output for lineparsing for
176  *    actionsfile and configfile.
177  *    Special handling for CLF added.
178  *
179  *    Revision 1.31  2001/10/26 17:39:01  oes
180  *    Removed csp->referrer
181  *    Moved ijb_isspace and ijb_tolower to project.h
182  *
183  *    Revision 1.30  2001/10/25 03:40:48  david__schmidt
184  *    Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
185  *    threads to call select() simultaneously.  So, it's time to do a real, live,
186  *    native OS/2 port.  See defines for __EMX__ (the porting layer) vs. __OS2__
187  *    (native). Both versions will work, but using __OS2__ offers multi-threading.
188  *
189  *    Revision 1.29  2001/10/23 21:38:53  jongfoster
190  *    Adding error-checking to create_url_spec()
191  *
192  *    Revision 1.28  2001/10/07 15:40:39  oes
193  *    Replaced 6 boolean members of csp with one bitmap (csp->flags)
194  *
195  *    Revision 1.27  2001/09/22 16:36:59  jongfoster
196  *    Removing unused parameter fs from read_config_line()
197  *
198  *    Revision 1.26  2001/09/22 14:05:22  jongfoster
199  *    Bugfix: Multiple escaped "#" characters in a configuration
200  *    file are now permitted.
201  *    Also removing 3 unused headers.
202  *
203  *    Revision 1.25  2001/09/13 22:44:03  jongfoster
204  *    Adding {} to an if statement
205  *
206  *    Revision 1.24  2001/07/30 22:08:36  jongfoster
207  *    Tidying up #defines:
208  *    - All feature #defines are now of the form FEATURE_xxx
209  *    - Permanently turned off WIN_GUI_EDIT
210  *    - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
211  *
212  *    Revision 1.23  2001/07/20 15:51:54  oes
213  *    Fixed indentation of prepocessor commands
214  *
215  *    Revision 1.22  2001/07/20 15:16:17  haroon
216  *    - per Guy's suggestion, added a while loop in sweep() to catch not just
217  *      the last inactive CSP but all other consecutive inactive CSPs after that
218  *      as well
219  *
220  *    Revision 1.21  2001/07/18 17:26:24  oes
221  *    Changed to conform to new pcrs interface
222  *
223  *    Revision 1.20  2001/07/17 13:07:01  oes
224  *    Fixed segv when last line in config files
225  *     lacked a terminating (\r)\n
226  *
227  *    Revision 1.19  2001/07/13 14:01:54  oes
228  *    Removed all #ifdef PCRS
229  *
230  *    Revision 1.18  2001/06/29 21:45:41  oes
231  *    Indentation, CRLF->LF, Tab-> Space
232  *
233  *    Revision 1.17  2001/06/29 13:31:51  oes
234  *    Various adaptions
235  *
236  *    Revision 1.16  2001/06/09 10:55:28  jongfoster
237  *    Changing BUFSIZ ==> BUFFER_SIZE
238  *
239  *    Revision 1.15  2001/06/07 23:14:14  jongfoster
240  *    Removing ACL and forward file loaders - these
241  *    files have been merged into the config file.
242  *    Cosmetic: Moving unloader funcs next to their
243  *    respective loader funcs
244  *
245  *    Revision 1.14  2001/06/01 03:27:04  oes
246  *    Fixed line continuation problem
247  *
248  *    Revision 1.13  2001/05/31 21:28:49  jongfoster
249  *    Removed all permissionsfile code - it's now called the actions
250  *    file, and (almost) all the code is in actions.c
251  *
252  *    Revision 1.12  2001/05/31 17:32:31  oes
253  *
254  *     - Enhanced domain part globbing with infix and prefix asterisk
255  *       matching and optional unanchored operation
256  *
257  *    Revision 1.11  2001/05/29 23:25:24  oes
258  *
259  *     - load_config_line() and load_permissions_file() now use chomp()
260  *
261  *    Revision 1.10  2001/05/29 09:50:24  jongfoster
262  *    Unified blocklist/imagelist/permissionslist.
263  *    File format is still under discussion, but the internal changes
264  *    are (mostly) done.
265  *
266  *    Also modified interceptor behaviour:
267  *    - We now intercept all URLs beginning with one of the following
268  *      prefixes (and *only* these prefixes):
269  *        * http://i.j.b/
270  *        * http://ijbswa.sf.net/config/
271  *        * http://ijbswa.sourceforge.net/config/
272  *    - New interceptors "home page" - go to http://i.j.b/ to see it.
273  *    - Internal changes so that intercepted and fast redirect pages
274  *      are not replaced with an image.
275  *    - Interceptors now have the option to send a binary page direct
276  *      to the client. (i.e. ijb-send-banner uses this)
277  *    - Implemented show-url-info interceptor.  (Which is why I needed
278  *      the above interceptors changes - a typical URL is
279  *      "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
280  *      The previous mechanism would not have intercepted that, and
281  *      if it had been intercepted then it then it would have replaced
282  *      it with an image.)
283  *
284  *    Revision 1.9  2001/05/26 17:12:07  jongfoster
285  *    Fatal errors loading configuration files now give better error messages.
286  *
287  *    Revision 1.8  2001/05/26 00:55:20  jongfoster
288  *    Removing duplicated code.  load_forwardfile() now uses create_url_spec()
289  *
290  *    Revision 1.7  2001/05/26 00:28:36  jongfoster
291  *    Automatic reloading of config file.
292  *    Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
293  *    Most of the global variables have been moved to a new
294  *    struct configuration_spec, accessed through csp->config->globalname
295  *    Most of the globals remaining are used by the Win32 GUI.
296  *
297  *    Revision 1.6  2001/05/23 12:27:33  oes
298  *
299  *    Fixed ugly indentation of my last changes
300  *
301  *    Revision 1.5  2001/05/23 10:39:05  oes
302  *    - Added support for escaping the comment character
303  *      in config files by a backslash
304  *    - Added support for line continuation in config
305  *      files
306  *    - Fixed a buffer overflow bug with long config lines
307  *
308  *    Revision 1.4  2001/05/22 18:56:28  oes
309  *    CRLF -> LF
310  *
311  *    Revision 1.3  2001/05/20 01:21:20  jongfoster
312  *    Version 2.9.4 checkin.
313  *    - Merged popupfile and cookiefile, and added control over PCRS
314  *      filtering, in new "permissionsfile".
315  *    - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
316  *      file error you now get a message box (in the Win32 GUI) rather
317  *      than the program exiting with no explanation.
318  *    - Made killpopup use the PCRS MIME-type checking and HTTP-header
319  *      skipping.
320  *    - Removed tabs from "config"
321  *    - Moved duplicated url parsing code in "loaders.c" to a new funcition.
322  *    - Bumped up version number.
323  *
324  *    Revision 1.2  2001/05/17 23:01:01  oes
325  *     - Cleaned CRLF's from the sources and related files
326  *
327  *    Revision 1.1.1.1  2001/05/15 13:58:59  oes
328  *    Initial import of version 2.9.3 source tree
329  *
330  *
331  *********************************************************************/
332 \f
333
334 #include "config.h"
335
336 #include <stdio.h>
337 #include <stdlib.h>
338 #include <sys/types.h>
339 #include <string.h>
340 #include <errno.h>
341 #include <sys/stat.h>
342 #include <ctype.h>
343 #include <assert.h>
344
345 #if !defined(_WIN32) && !defined(__OS2__)
346 #include <unistd.h>
347 #endif
348
349 #include "project.h"
350 #include "list.h"
351 #include "loaders.h"
352 #include "filters.h"
353 #include "parsers.h"
354 #include "jcc.h"
355 #include "miscutil.h"
356 #include "errlog.h"
357 #include "actions.h"
358 #include "urlmatch.h"
359 #include "encode.h"
360
361 const char loaders_h_rcs[] = LOADERS_H_VERSION;
362
363 /*
364  * Currently active files.
365  * These are also entered in the main linked list of files.
366  */
367
368 #ifdef FEATURE_TRUST
369 static struct file_list *current_trustfile      = NULL;
370 #endif /* def FEATURE_TRUST */
371
372 static int load_one_re_filterfile(struct client_state *csp, int fileid);
373
374 static struct file_list *current_re_filterfile[MAX_AF_FILES]  = {
375    NULL, NULL, NULL, NULL, NULL,
376    NULL, NULL, NULL, NULL, NULL
377 };
378
379
380
381 /*********************************************************************
382  *
383  * Function    :  sweep
384  *
385  * Description :  Basically a mark and sweep garbage collector, it is run
386  *                (by the parent thread) every once in a while to reclaim memory.
387  *
388  * It uses a mark and sweep strategy:
389  *   1) mark all files as inactive
390  *
391  *   2) check with each client:
392  *       if it is active,   mark its files as active
393  *       if it is inactive, free its resources
394  *
395  *   3) free the resources of all of the files that
396  *      are still marked as inactive (and are obsolete).
397  *
398  *   N.B. files that are not obsolete don't have an unloader defined.
399  *
400  * Parameters  :  None
401  *
402  * Returns     :  N/A
403  *
404  *********************************************************************/
405 void sweep(void)
406 {
407    struct file_list *fl, *nfl;
408    struct client_state *csp, *last_active;
409    int i;
410
411    /* clear all of the file's active flags */
412    for ( fl = files->next; NULL != fl; fl = fl->next )
413    {
414       fl->active = 0;
415    }
416
417    last_active = clients;
418    csp = clients->next;
419
420    while (NULL != csp)
421    {
422       if (csp->flags & CSP_FLAG_ACTIVE)
423       {
424          /* Mark this client's files as active */
425
426          /*
427           * Always have a configuration file.
428           * (Also note the slightly non-standard extra
429           * indirection here.)
430           */
431          csp->config->config_file_list->active = 1;
432
433          /* 
434           * Actions files
435           */
436          for (i = 0; i < MAX_AF_FILES; i++)
437          {
438             if (csp->actions_list[i])     
439             {
440                csp->actions_list[i]->active = 1;
441             }
442          }
443
444          /*
445           * Filter files
446           */
447          for (i = 0; i < MAX_AF_FILES; i++)
448          {
449             if (csp->rlist[i])     
450             {
451                csp->rlist[i]->active = 1;
452             }
453          }
454
455          /*
456           * Trust file
457           */
458 #ifdef FEATURE_TRUST
459          if (csp->tlist)
460          {
461             csp->tlist->active = 1;
462          }
463 #endif /* def FEATURE_TRUST */
464          
465          last_active = csp;
466          csp = csp->next;
467
468       }
469       else 
470       /*
471        * This client is not active. Free its resources.
472        */
473       {
474          last_active->next = csp->next;
475
476          freez(csp->ip_addr_str);
477          freez(csp->my_ip_addr_str);
478          freez(csp->my_hostname);
479          freez(csp->x_forwarded);
480          freez(csp->iob->buf);
481          freez(csp->error_message);
482
483          free_http_request(csp->http);
484
485          destroy_list(csp->headers);
486          destroy_list(csp->cookie_list);
487
488          free_current_action(csp->action);
489
490 #ifdef FEATURE_STATISTICS
491          urls_read++;
492          if (csp->flags & CSP_FLAG_REJECTED)
493          {
494             urls_rejected++;
495          }
496 #endif /* def FEATURE_STATISTICS */
497
498          freez(csp);
499          
500          csp = last_active->next;
501       }
502    }
503
504    nfl = files;
505    fl = files->next;
506
507    while (fl != NULL)
508    {
509       if ( ( 0 == fl->active ) && ( NULL != fl->unloader ) )
510       {
511          nfl->next = fl->next;
512
513          (fl->unloader)(fl->f);
514
515          freez(fl->filename);
516          freez(fl);
517
518          fl = nfl->next;
519       }
520       else
521       {
522          nfl = fl;
523          fl = fl->next;
524       }
525    }
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    if (fs == NULL)
580    {
581       /* Out of memory error */
582       return 1;
583    }
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    *newfl = fs;
596    return 1;
597 }
598
599
600 /*********************************************************************
601  *
602  * Function    :  simple_read_line
603  *
604  * Description :  Read a single line from a file and return it.
605  *                This is basically a version of fgets() that malloc()s
606  *                it's own line buffer.  Note that the buffer will
607  *                always be a multiple of BUFFER_SIZE bytes long.
608  *                Therefore if you are going to keep the string for
609  *                an extended period of time, you should probably
610  *                strdup() it and free() the original, to save memory.
611  *
612  *
613  * Parameters  :
614  *          1  :  dest = destination for newly malloc'd pointer to
615  *                line data.  Will be set to NULL on error.
616  *          2  :  fp = File to read from
617  *          3  :  newline = Standard for newlines in the file.
618  *                Will be unchanged if it's value on input is not
619  *                NEWLINE_UNKNOWN.
620  *                On output, may be changed from NEWLINE_UNKNOWN to
621  *                actual convention in file.
622  *
623  * Returns     :  JB_ERR_OK     on success
624  *                JB_ERR_MEMORY on out-of-memory
625  *                JB_ERR_FILE   on EOF.
626  *
627  *********************************************************************/
628 jb_err simple_read_line(FILE *fp, char **dest, int *newline)
629 {
630    size_t len = 0;
631    size_t buflen = BUFFER_SIZE;
632    char * buf;
633    char * p;
634    int ch;
635    int realnewline = NEWLINE_UNKNOWN;
636
637    if (NULL == (buf = malloc(buflen)))
638    {
639       return JB_ERR_MEMORY;
640    }
641
642    p = buf;
643
644 /*
645  * Character codes.  If you have a wierd compiler and the following are
646  * incorrect, you also need to fix NEWLINE() in loaders.h
647  */
648 #define CHAR_CR '\r' /* ASCII 13 */
649 #define CHAR_LF '\n' /* ASCII 10 */
650
651    for (;;)
652    {
653       ch = getc(fp);
654       if (ch == EOF)
655       {
656          if (len > 0)
657          {
658             *p = '\0';
659             *dest = buf;
660             return JB_ERR_OK;
661          }
662          else
663          {
664             free(buf);
665             *dest = NULL;
666             return JB_ERR_FILE;
667          }
668       }
669       else if (ch == CHAR_CR)
670       {
671          ch = getc(fp);
672          if (ch == CHAR_LF)
673          {
674             if (*newline == NEWLINE_UNKNOWN)
675             {
676                *newline = NEWLINE_DOS;
677             }
678          }
679          else
680          {
681             if (ch != EOF)
682             {
683                ungetc(ch, fp);
684             }
685             if (*newline == NEWLINE_UNKNOWN)
686             {
687                *newline = NEWLINE_MAC;
688             }
689          }
690          *p = '\0';
691          *dest = buf;
692          if (*newline == NEWLINE_UNKNOWN)
693          {
694             *newline = realnewline;
695          }
696          return JB_ERR_OK;
697       }
698       else if (ch == CHAR_LF)
699       {
700          *p = '\0';
701          *dest = buf;
702          if (*newline == NEWLINE_UNKNOWN)
703          {
704             *newline = NEWLINE_UNIX;
705          }
706          return JB_ERR_OK;
707       }
708       else if (ch == 0)
709       {
710          *p = '\0';
711          *dest = buf;
712          return JB_ERR_OK;
713       }
714
715       *p++ = (char)ch;
716
717       if (++len >= buflen)
718       {
719          buflen += BUFFER_SIZE;
720          if (NULL == (p = realloc(buf, buflen)))
721          {
722             free(buf);
723             return JB_ERR_MEMORY;
724          }
725          buf = p;
726          p = buf + len;
727       }
728    }
729 }
730
731
732 /*********************************************************************
733  *
734  * Function    :  edit_read_line
735  *
736  * Description :  Read a single non-empty line from a file and return
737  *                it.  Trims comments, leading and trailing whitespace
738  *                and respects escaping of newline and comment char.
739  *                Provides the line in 2 alternative forms: raw and
740  *                preprocessed.
741  *                - raw is the raw data read from the file.  If the
742  *                  line is not modified, then this should be written
743  *                  to the new file.
744  *                - prefix is any comments and blank lines that were
745  *                  read from the file.  If the line is modified, then
746  *                  this should be written out to the file followed
747  *                  by the modified data.  (If this string is non-empty
748  *                  then it will have a newline at the end).
749  *                - data is the actual data that will be parsed
750  *                  further by appropriate routines.
751  *                On EOF, the 3 strings will all be set to NULL and
752  *                0 will be returned.
753  *
754  * Parameters  :
755  *          1  :  fp = File to read from
756  *          2  :  raw_out = destination for newly malloc'd pointer to
757  *                raw line data.  May be NULL if you don't want it.
758  *          3  :  prefix_out = destination for newly malloc'd pointer to
759  *                comments.  May be NULL if you don't want it.
760  *          4  :  data_out = destination for newly malloc'd pointer to
761  *                line data with comments and leading/trailing spaces
762  *                removed, and line continuation performed.  May be
763  *                NULL if you don't want it.
764  *          5  :  newline = Standard for newlines in the file.
765  *                On input, set to value to use or NEWLINE_UNKNOWN.
766  *                On output, may be changed from NEWLINE_UNKNOWN to
767  *                actual convention in file.  May be NULL if you
768  *                don't want it.
769  *          6  :  line_number = Line number in file.  In "lines" as
770  *                reported by a text editor, not lines containing data.
771  *
772  * Returns     :  JB_ERR_OK     on success
773  *                JB_ERR_MEMORY on out-of-memory
774  *                JB_ERR_FILE   on EOF.
775  *
776  *********************************************************************/
777 jb_err edit_read_line(FILE *fp,
778                       char **raw_out,
779                       char **prefix_out,
780                       char **data_out,
781                       int *newline,
782                       unsigned long *line_number)
783 {
784    char *p;          /* Temporary pointer   */
785    char *linebuf;    /* Line read from file */
786    char *linestart;  /* Start of linebuf, usually first non-whitespace char */
787    int contflag = 0; /* Nonzero for line continuation - i.e. line ends '\' */
788    int is_empty = 1; /* Flag if not got any data yet */
789    char *raw    = NULL; /* String to be stored in raw_out    */
790    char *prefix = NULL; /* String to be stored in prefix_out */
791    char *data   = NULL; /* String to be stored in data_out   */
792    int scrapnewline;    /* Used for (*newline) if newline==NULL */
793    jb_err rval = JB_ERR_OK;
794
795    assert(fp);
796    assert(raw_out || data_out);
797    assert(newline == NULL
798        || *newline == NEWLINE_UNKNOWN
799        || *newline == NEWLINE_UNIX
800        || *newline == NEWLINE_DOS
801        || *newline == NEWLINE_MAC);
802
803    if (newline == NULL)
804    {
805       scrapnewline = NEWLINE_UNKNOWN;
806       newline = &scrapnewline;
807    }
808
809    /* Set output parameters to NULL */
810    if (raw_out)
811    {
812       *raw_out    = NULL;
813    }
814    if (prefix_out)
815    {
816       *prefix_out = NULL;
817    }
818    if (data_out)
819    {
820       *data_out   = NULL;
821    }
822
823    /* Set string variables to new, empty strings. */
824
825    if (raw_out)
826    {
827       if ((raw = malloc(1)) == NULL)
828       {
829          return JB_ERR_MEMORY;
830       }
831       *raw = '\0';
832    }
833    if (prefix_out)
834    {
835       if ((prefix = malloc(1)) == NULL)
836       {
837          freez(raw);
838          return JB_ERR_MEMORY;
839       }
840       *prefix = '\0';
841    }
842    if (data_out)
843    {
844       if ((data = malloc(1)) == NULL)
845       {
846          freez(raw);
847          freez(prefix);
848          return JB_ERR_MEMORY;
849       }
850       *data = '\0';
851    }
852
853    /* Main loop.  Loop while we need more data & it's not EOF. */
854
855    while ( (contflag || is_empty)
856         && (JB_ERR_OK == (rval = simple_read_line(fp, &linebuf, newline))))
857    {
858       if (line_number)
859       {
860          (*line_number)++;
861       }
862       if (raw)
863       {
864          string_append(&raw,linebuf);
865          if (string_append(&raw,NEWLINE(*newline)))
866          {
867             freez(prefix);
868             freez(data);
869             free(linebuf);
870             return JB_ERR_MEMORY;
871          }
872       }
873
874       /* Line continuation? Trim escape and set flag. */
875       p = linebuf + strlen(linebuf) - 1;
876       contflag = ((*linebuf != '\0') && (*p == '\\'));
877       if (contflag)
878       {
879          *p = '\0';
880       }
881
882       /* Trim leading spaces if we're at the start of the line */
883       linestart = linebuf;
884       if (*data == '\0')
885       {
886          /* Trim leading spaces */
887          while (*linestart && isspace((int)(unsigned char)*linestart))
888          {
889             linestart++;
890          }
891       }
892
893       /* Handle comment characters. */
894       p = linestart;
895       while ((p = strchr(p, '#')) != NULL)
896       {
897          /* Found a comment char.. */
898          if ((p != linebuf) && (*(p-1) == '\\'))
899          {
900             /* ..and it's escaped, left-shift the line over the escape. */
901             char *q = p - 1;
902             while ((*q = *(q + 1)) != '\0')
903             {
904                q++;
905             }
906             /* Now scan from just after the "#". */
907          }
908          else
909          {
910             /* Real comment.  Save it... */
911             if (p == linestart)
912             {
913                /* Special case:  Line only contains a comment, so all the
914                 * previous whitespace is considered part of the comment.
915                 * Undo the whitespace skipping, if any.
916                 */
917                linestart = linebuf;
918                p = linestart;
919             }
920             if (prefix)
921             {
922                string_append(&prefix,p);
923                if (string_append(&prefix, NEWLINE(*newline)))
924                {
925                   freez(raw);
926                   freez(data);
927                   free(linebuf);
928                   return JB_ERR_MEMORY;
929                }
930             }
931
932             /* ... and chop off the rest of the line */
933             *p = '\0';
934          }
935       } /* END while (there's a # character) */
936
937       /* Write to the buffer */
938       if (*linestart)
939       {
940          is_empty = 0;
941          if (data)
942          {
943             if (string_append(&data, linestart))
944             {
945                freez(raw);
946                freez(prefix);
947                free(linebuf);
948                return JB_ERR_MEMORY;
949             }
950          }
951       }
952
953       free(linebuf);
954    } /* END while(we need more data) */
955
956    /* Handle simple_read_line() errors - ignore EOF */
957    if ((rval != JB_ERR_OK) && (rval != JB_ERR_FILE))
958    {
959       freez(raw);
960       freez(prefix);
961       freez(data);
962       return rval;
963    }
964
965    if (raw ? (*raw == '\0') : is_empty)
966    {
967       /* EOF and no data there.  (Definition of "data" depends on whether
968        * the caller cares about "raw" or just "data").
969        */
970
971       freez(raw);
972       freez(prefix);
973       freez(data);
974
975       return JB_ERR_FILE;
976    }
977    else
978    {
979       /* Got at least some data */
980
981       /* Remove trailing whitespace */
982       chomp(data);
983
984       if (raw_out)
985       {
986          *raw_out    = raw;
987       }
988       else
989       {
990          freez(raw);
991       }
992       if (prefix_out)
993       {
994          *prefix_out = prefix;
995       }
996       else
997       {
998          freez(prefix);
999       }
1000       if (data_out)
1001       {
1002          *data_out   = data;
1003       }
1004       else
1005       {
1006          freez(data);
1007       }
1008       return JB_ERR_OK;
1009    }
1010 }
1011
1012
1013 /*********************************************************************
1014  *
1015  * Function    :  read_config_line
1016  *
1017  * Description :  Read a single non-empty line from a file and return
1018  *                it.  Trims comments, leading and trailing whitespace
1019  *                and respects escaping of newline and comment char.
1020  *
1021  * Parameters  :
1022  *          1  :  buf = Buffer to use.
1023  *          2  :  buflen = Size of buffer in bytes.
1024  *          3  :  fp = File to read from
1025  *          4  :  linenum = linenumber in file
1026  *
1027  * Returns     :  NULL on EOF or error
1028  *                Otherwise, returns buf.
1029  *
1030  *********************************************************************/
1031 char *read_config_line(char *buf, size_t buflen, FILE *fp, unsigned long *linenum)
1032 {
1033    jb_err err;
1034    char *buf2 = NULL;
1035    err = edit_read_line(fp, NULL, NULL, &buf2, NULL, linenum);
1036    if (err)
1037    {
1038       if (err == JB_ERR_MEMORY)
1039       {
1040          log_error(LOG_LEVEL_FATAL, "Out of memory loading a config file");
1041       }
1042       return NULL;
1043    }
1044    else
1045    {
1046       assert(buf2);
1047       assert(strlen(buf2) + 1U < buflen);
1048       strncpy(buf, buf2, buflen - 1);
1049       free(buf2);
1050       buf[buflen - 1] = '\0';
1051       return buf;
1052    }
1053 }
1054
1055
1056 #ifdef FEATURE_TRUST
1057 /*********************************************************************
1058  *
1059  * Function    :  unload_trustfile
1060  *
1061  * Description :  Unloads a trustfile.
1062  *
1063  * Parameters  :
1064  *          1  :  f = the data structure associated with the trustfile.
1065  *
1066  * Returns     :  N/A
1067  *
1068  *********************************************************************/
1069 static void unload_trustfile(void *f)
1070 {
1071    struct block_spec *cur = (struct block_spec *)f;
1072    struct block_spec *next;
1073
1074    while (cur != NULL)
1075    {
1076       next = cur->next;
1077
1078       free_url_spec(cur->url);
1079       free(cur);
1080
1081       cur = next;
1082    }
1083
1084 }
1085
1086
1087 #ifdef FEATURE_GRACEFUL_TERMINATION
1088 /*********************************************************************
1089  *
1090  * Function    :  unload_current_trust_file
1091  *
1092  * Description :  Unloads current trust file - reset to state at
1093  *                beginning of program.
1094  *
1095  * Parameters  :  None
1096  *
1097  * Returns     :  N/A
1098  *
1099  *********************************************************************/
1100 void unload_current_trust_file(void)
1101 {
1102    if (current_trustfile)
1103    {
1104       current_trustfile->unloader = unload_trustfile;
1105       current_trustfile = NULL;
1106    }
1107 }
1108 #endif /* FEATURE_GRACEFUL_TERMINATION */
1109
1110
1111 /*********************************************************************
1112  *
1113  * Function    :  load_trustfile
1114  *
1115  * Description :  Read and parse a trustfile and add to files list.
1116  *
1117  * Parameters  :
1118  *          1  :  csp = Current client state (buffers, headers, etc...)
1119  *
1120  * Returns     :  0 => Ok, everything else is an error.
1121  *
1122  *********************************************************************/
1123 int load_trustfile(struct client_state *csp)
1124 {
1125    FILE *fp;
1126
1127    struct block_spec *b, *bl;
1128    struct url_spec **tl;
1129
1130    char  buf[BUFFER_SIZE], *p, *q;
1131    int reject, trusted;
1132    struct file_list *fs;
1133    unsigned long linenum = 0;
1134    int trusted_referrers = 0;
1135
1136    if (!check_file_changed(current_trustfile, csp->config->trustfile, &fs))
1137    {
1138       /* No need to load */
1139       if (csp)
1140       {
1141          csp->tlist = current_trustfile;
1142       }
1143       return(0);
1144    }
1145    if (!fs)
1146    {
1147       goto load_trustfile_error;
1148    }
1149
1150    fs->f = bl = (struct block_spec *)zalloc(sizeof(*bl));
1151    if (bl == NULL)
1152    {
1153       goto load_trustfile_error;
1154    }
1155
1156    if ((fp = fopen(csp->config->trustfile, "r")) == NULL)
1157    {
1158       goto load_trustfile_error;
1159    }
1160
1161    tl = csp->config->trust_list;
1162
1163    while (read_config_line(buf, sizeof(buf), fp, &linenum) != NULL)
1164    {
1165       trusted = 0;
1166       reject  = 1;
1167
1168       if (*buf == '+')
1169       {
1170          trusted = 1;
1171          *buf = '~';
1172       }
1173
1174       if (*buf == '~')
1175       {
1176          reject = 0;
1177          p = buf;
1178          q = p+1;
1179          while ((*p++ = *q++) != '\0')
1180          {
1181             /* nop */
1182          }
1183       }
1184
1185       /* skip blank lines */
1186       if (*buf == '\0')
1187       {
1188          continue;
1189       }
1190
1191       /* allocate a new node */
1192       if ((b = zalloc(sizeof(*b))) == NULL)
1193       {
1194          fclose(fp);
1195          goto load_trustfile_error;
1196       }
1197
1198       /* add it to the list */
1199       b->next  = bl->next;
1200       bl->next = b;
1201
1202       b->reject = reject;
1203
1204       /* Save the URL pattern */
1205       if (create_url_spec(b->url, buf))
1206       {
1207          fclose(fp);
1208          goto load_trustfile_error;
1209       }
1210
1211       /*
1212        * save a pointer to URL's spec in the list of trusted URL's, too
1213        */
1214       if (trusted)
1215       {
1216          if(++trusted_referrers < MAX_TRUSTED_REFERRERS)
1217          {
1218             *tl++ = b->url;
1219          }
1220       }
1221    }
1222
1223    if(trusted_referrers >= MAX_TRUSTED_REFERRERS) 
1224    {
1225       /*
1226        * FIXME: ... after Privoxy 3.0.4 is out.
1227        */
1228        log_error(LOG_LEVEL_ERROR, "Too many trusted referrers. Current limit is %d, you are using %d.\n"
1229           "  Additional trusted referrers are treated like ordinary trusted URLs.\n"
1230           "  (You can increase this limit by changing MAX_TRUSTED_REFERRERS in project.h and recompiling).",
1231           MAX_TRUSTED_REFERRERS, trusted_referrers);
1232    }
1233
1234    *tl = NULL;
1235
1236    fclose(fp);
1237
1238    /* the old one is now obsolete */
1239    if (current_trustfile)
1240    {
1241       current_trustfile->unloader = unload_trustfile;
1242    }
1243
1244    fs->next    = files->next;
1245    files->next = fs;
1246    current_trustfile = fs;
1247
1248    if (csp)
1249    {
1250       csp->tlist = fs;
1251    }
1252
1253    return(0);
1254
1255 load_trustfile_error:
1256    log_error(LOG_LEVEL_FATAL, "can't load trustfile '%s': %E",
1257              csp->config->trustfile);
1258    return(-1);
1259
1260 }
1261 #endif /* def FEATURE_TRUST */
1262
1263
1264 /*********************************************************************
1265  *
1266  * Function    :  unload_re_filterfile
1267  *
1268  * Description :  Unload the re_filter list by freeing all chained
1269  *                re_filterfile specs and their data.
1270  *
1271  * Parameters  :
1272  *          1  :  f = the data structure associated with the filterfile.
1273  *
1274  * Returns     :  N/A
1275  *
1276  *********************************************************************/
1277 static void unload_re_filterfile(void *f)
1278 {
1279    struct re_filterfile_spec *a, *b = (struct re_filterfile_spec *)f;
1280
1281    while (b != NULL)
1282    {
1283       a = b->next;
1284
1285       destroy_list(b->patterns);
1286       pcrs_free_joblist(b->joblist);
1287       freez(b->name);
1288       freez(b->description);
1289       freez(b);
1290
1291       b = a;
1292    }
1293
1294    return;
1295 }
1296
1297
1298 #ifdef FEATURE_GRACEFUL_TERMINATION
1299 /*********************************************************************
1300  *
1301  * Function    :  unload_current_re_filterfile
1302  *
1303  * Description :  Unloads current re_filter file - reset to state at
1304  *                beginning of program.
1305  *
1306  * Parameters  :  None
1307  *
1308  * Returns     :  N/A
1309  *
1310  *********************************************************************/
1311 void unload_current_re_filterfile(void)
1312 {
1313    int i;
1314
1315    for (i = 0; i < MAX_AF_FILES; i++)
1316    {
1317       if (current_re_filterfile[i])
1318       {
1319          current_re_filterfile[i]->unloader = unload_re_filterfile;
1320          current_re_filterfile[i] = NULL;
1321       }
1322    }
1323 }
1324 #endif
1325
1326
1327 /*********************************************************************
1328  *
1329  * Function    :  load_re_filterfile
1330  *
1331  * Description :  Load the re_filterfile. 
1332  *                Generate a chained list of re_filterfile_spec's from
1333  *                the "FILTER: " blocks, compiling all their substitutions
1334  *                into chained lists of pcrs_job structs.
1335  *
1336  * Parameters  :
1337  *          1  :  csp = Current client state (buffers, headers, etc...)
1338  *
1339  * Returns     :  0 => Ok, everything else is an error.
1340  *
1341  *********************************************************************/
1342 int load_re_filterfile(struct client_state *csp)
1343 {
1344    int i;
1345    int result;
1346
1347    for (i = 0; i < MAX_AF_FILES; i++)
1348    {
1349       if (csp->config->re_filterfile[i])
1350       {
1351          result = load_one_re_filterfile(csp, i);
1352          if (result)
1353          {
1354             return result;
1355          }
1356       }
1357       else if (current_re_filterfile[i])
1358       {
1359          current_re_filterfile[i]->unloader = unload_re_filterfile;
1360          current_re_filterfile[i] = NULL;
1361       }
1362    }
1363
1364    return 0;
1365 }
1366
1367 /*********************************************************************
1368  *
1369  * Function    :  load_one_re_filterfile
1370  *
1371  * Description :  Load a re_filterfile. 
1372  *                Generate a chained list of re_filterfile_spec's from
1373  *                the "FILTER: " blocks, compiling all their substitutions
1374  *                into chained lists of pcrs_job structs.
1375  *
1376  * Parameters  :
1377  *          1  :  csp = Current client state (buffers, headers, etc...)
1378  *
1379  * Returns     :  0 => Ok, everything else is an error.
1380  *
1381  *********************************************************************/
1382 int load_one_re_filterfile(struct client_state *csp, int fileid)
1383 {
1384    FILE *fp;
1385
1386    struct re_filterfile_spec *new_bl, *bl = NULL;
1387    struct file_list *fs;
1388
1389    char  buf[BUFFER_SIZE];
1390    int error;
1391    unsigned long linenum = 0;
1392    pcrs_job *dummy, *lastjob = NULL;
1393
1394    /*
1395     * No need to reload if unchanged
1396     */
1397    if (!check_file_changed(current_re_filterfile[fileid], csp->config->re_filterfile[fileid], &fs))
1398    {
1399       if (csp)
1400       {
1401          csp->rlist[fileid] = current_re_filterfile[fileid];
1402       }
1403       return(0);
1404    }
1405    if (!fs)
1406    {
1407       goto load_re_filterfile_error;
1408    }
1409
1410    /* 
1411     * Open the file or fail
1412     */
1413    if ((fp = fopen(csp->config->re_filterfile[fileid], "r")) == NULL)
1414    {
1415       goto load_re_filterfile_error;
1416    }
1417
1418    /* 
1419     * Read line by line
1420     */
1421    while (read_config_line(buf, sizeof(buf), fp, &linenum) != NULL)
1422    {
1423       /*
1424        * If this is the head of a new filter block, make it a
1425        * re_filterfile spec of its own and chain it to the list:
1426        */
1427       if (strncmp(buf, "FILTER:", 7) == 0)
1428       {
1429          new_bl = (struct re_filterfile_spec  *)zalloc(sizeof(*bl));
1430          if (new_bl == NULL)
1431          {
1432             goto load_re_filterfile_error;
1433          }
1434
1435          new_bl->name = chomp(buf + 7);
1436
1437          /*
1438           * If a filter description is available,
1439           * encode it to HTML and save it.
1440           */
1441          if (NULL != (new_bl->description = strpbrk(new_bl->name, " \t")))
1442          {
1443             *new_bl->description++ = '\0';
1444             new_bl->description = html_encode(chomp(new_bl->description));
1445             if (NULL == new_bl->description)
1446             {
1447                new_bl->description = strdup("Out of memory while encoding this filter's description to HTML");
1448             }
1449          }
1450          else
1451          {
1452             new_bl->description = strdup("No description available for this filter");
1453          }
1454
1455          new_bl->name = strdup(chomp(new_bl->name));
1456          
1457          /*
1458           * If this is the first filter block, chain it
1459           * to the file_list rather than its (nonexistant)
1460           * predecessor
1461           */
1462          if (fs->f == NULL)
1463          {
1464             fs->f = new_bl;
1465          }
1466          else
1467          {
1468             bl->next = new_bl;
1469          }
1470          bl = new_bl;
1471
1472          log_error(LOG_LEVEL_RE_FILTER, "Reading in filter \"%s\" (\"%s\")", bl->name, bl->description);
1473
1474          continue;
1475       }
1476
1477       /* 
1478        * Else, save the expression, make it a pcrs_job
1479        * and chain it into the current filter's joblist 
1480        */
1481       if (bl != NULL)
1482       {
1483          enlist(bl->patterns, buf);
1484
1485          if ((dummy = pcrs_compile_command(buf, &error)) == NULL)
1486          {
1487             log_error(LOG_LEVEL_ERROR,
1488                       "Adding re_filter job %s to filter %s failed with error %d.", buf, bl->name, error);
1489             continue;
1490          }
1491          else
1492          {
1493             if (bl->joblist == NULL)
1494             {
1495                bl->joblist = dummy;
1496             }
1497             else
1498             {
1499                lastjob->next = dummy;
1500             }
1501             lastjob = dummy;
1502             log_error(LOG_LEVEL_RE_FILTER, "Adding re_filter job %s to filter %s succeeded.", buf, bl->name);
1503          }
1504       }
1505       else
1506       {
1507          log_error(LOG_LEVEL_ERROR, "Ignoring job %s outside filter block in %s, line %d",
1508             buf, csp->config->re_filterfile[fileid], linenum);
1509       }
1510    }
1511
1512    fclose(fp);
1513
1514    /* 
1515     * Schedule the now-obsolete old data for unloading
1516     */
1517    if ( NULL != current_re_filterfile[fileid] )
1518    {
1519       current_re_filterfile[fileid]->unloader = unload_re_filterfile;
1520    }
1521
1522    /*
1523     * Chain this file into the global list of loaded files
1524     */
1525    fs->next    = files->next;
1526    files->next = fs;
1527    current_re_filterfile[fileid] = fs;
1528
1529    if (csp)
1530    {
1531       csp->rlist[fileid] = fs;
1532    }
1533
1534    return( 0 );
1535
1536 load_re_filterfile_error:
1537    log_error(LOG_LEVEL_FATAL, "can't load re_filterfile '%s': %E",
1538              csp->config->re_filterfile[fileid]);
1539    return(-1);
1540
1541 }
1542
1543
1544 /*********************************************************************
1545  *
1546  * Function    :  add_loader
1547  *
1548  * Description :  Called from `load_config'.  Called once for each input
1549  *                file found in config.
1550  *
1551  * Parameters  :
1552  *          1  :  loader = pointer to a function that can parse and load
1553  *                the appropriate config file.
1554  *          2  :  config = The configuration_spec to add the loader to.
1555  *
1556  * Returns     :  N/A
1557  *
1558  *********************************************************************/
1559 void add_loader(int (*loader)(struct client_state *),
1560                 struct configuration_spec * config)
1561 {
1562    int i;
1563
1564    for (i=0; i < NLOADERS; i++)
1565    {
1566       if (config->loaders[i] == NULL)
1567       {
1568          config->loaders[i] = loader;
1569          break;
1570       }
1571    }
1572
1573 }
1574
1575
1576 /*********************************************************************
1577  *
1578  * Function    :  run_loader
1579  *
1580  * Description :  Called from `load_config' and `listen_loop'.  This
1581  *                function keeps the "csp" current with any file mods
1582  *                since the last loop.  If a file is unchanged, the
1583  *                loader functions do NOT reload the file.
1584  *
1585  * Parameters  :
1586  *          1  :  csp = Current client state (buffers, headers, etc...)
1587  *                      Must be non-null.  Reads: "csp->config"
1588  *                      Writes: various data members.
1589  *
1590  * Returns     :  0 => Ok, everything else is an error.
1591  *
1592  *********************************************************************/
1593 int run_loader(struct client_state *csp)
1594 {
1595    int ret = 0;
1596    int i;
1597
1598    for (i=0; i < NLOADERS; i++)
1599    {
1600       if (csp->config->loaders[i] == NULL)
1601       {
1602          break;
1603       }
1604       ret |= (csp->config->loaders[i])(csp);
1605    }
1606    return(ret);
1607
1608 }
1609
1610
1611 /*
1612   Local Variables:
1613   tab-width: 3
1614   end:
1615 */