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