1 const char loaders_rcs[] = "$Id: loaders.c,v 1.45 2002/03/16 23:54:06 jongfoster Exp $";
2 /*********************************************************************
4 * File : $Source: /cvsroot/ijbswa/current/loaders.c,v $
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.
11 * Copyright : Written by and Copyright (C) 2001 the SourceForge
12 * Privoxy team. http://ijbswa.sourceforge.net
14 * Based on the Internet Junkbuster originally written
15 * by and Copyright (C) 1997 Anonymous Coders and
16 * Junkbusters Corporation. http://www.junkbusters.com
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.
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.
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.
38 * Revision 1.45 2002/03/16 23:54:06 jongfoster
39 * Adding graceful termination feature, to help look for memory leaks.
40 * If you enable this (which, by design, has to be done by hand
41 * editing config.h) and then go to http://i.j.b/die, then the program
42 * will exit cleanly after the *next* request. It should free all the
43 * memory that was used.
45 * Revision 1.44 2002/03/16 21:51:00 jongfoster
48 * Revision 1.43 2002/03/16 20:28:34 oes
49 * Added descriptions to the filters so users will know what they select in the cgi editor
51 * Revision 1.42 2002/03/13 00:27:05 jongfoster
54 * Revision 1.41 2002/03/12 01:42:50 oes
55 * Introduced modular filters
57 * Revision 1.40 2002/03/08 17:46:04 jongfoster
58 * Fixing int/size_t warnings
60 * Revision 1.39 2002/03/07 03:46:17 oes
61 * Fixed compiler warnings
63 * Revision 1.38 2002/03/06 22:54:35 jongfoster
64 * Automated function-comment nitpicking.
66 * Revision 1.37 2002/03/03 15:07:49 oes
67 * Re-enabled automatic config reloading
69 * Revision 1.36 2002/01/22 23:46:18 jongfoster
70 * Moving edit_read_line() and simple_read_line() to loaders.c, and
71 * extending them to support reading MS-DOS, Mac and UNIX style files
74 * Modifying read_config_line() (without changing it's prototype) to
75 * be a trivial wrapper for edit_read_line(). This means that we have
76 * one function to read a line and handle comments, which is common
77 * between the initialization code and the edit interface.
79 * Revision 1.35 2002/01/17 21:03:08 jongfoster
80 * Moving all our URL and URL pattern parsing code to urlmatch.c.
82 * Renaming free_url to free_url_spec, since it frees a struct url_spec.
84 * Revision 1.34 2001/12/30 14:07:32 steudten
85 * - Add signal handling (unix)
86 * - Add SIGHUP handler (unix)
87 * - Add creation of pidfile (unix)
88 * - Add action 'top' in rc file (RH)
89 * - Add entry 'SIGNALS' to manpage
90 * - Add exit message to logfile (unix)
92 * Revision 1.33 2001/11/13 00:16:38 jongfoster
93 * Replacing references to malloc.h with the standard stdlib.h
94 * (See ANSI or K&R 2nd Ed)
96 * Revision 1.32 2001/11/07 00:02:13 steudten
97 * Add line number in error output for lineparsing for
98 * actionsfile and configfile.
99 * Special handling for CLF added.
101 * Revision 1.31 2001/10/26 17:39:01 oes
102 * Removed csp->referrer
103 * Moved ijb_isspace and ijb_tolower to project.h
105 * Revision 1.30 2001/10/25 03:40:48 david__schmidt
106 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
107 * threads to call select() simultaneously. So, it's time to do a real, live,
108 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
109 * (native). Both versions will work, but using __OS2__ offers multi-threading.
111 * Revision 1.29 2001/10/23 21:38:53 jongfoster
112 * Adding error-checking to create_url_spec()
114 * Revision 1.28 2001/10/07 15:40:39 oes
115 * Replaced 6 boolean members of csp with one bitmap (csp->flags)
117 * Revision 1.27 2001/09/22 16:36:59 jongfoster
118 * Removing unused parameter fs from read_config_line()
120 * Revision 1.26 2001/09/22 14:05:22 jongfoster
121 * Bugfix: Multiple escaped "#" characters in a configuration
122 * file are now permitted.
123 * Also removing 3 unused headers.
125 * Revision 1.25 2001/09/13 22:44:03 jongfoster
126 * Adding {} to an if statement
128 * Revision 1.24 2001/07/30 22:08:36 jongfoster
129 * Tidying up #defines:
130 * - All feature #defines are now of the form FEATURE_xxx
131 * - Permanently turned off WIN_GUI_EDIT
132 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
134 * Revision 1.23 2001/07/20 15:51:54 oes
135 * Fixed indentation of prepocessor commands
137 * Revision 1.22 2001/07/20 15:16:17 haroon
138 * - per Guy's suggestion, added a while loop in sweep() to catch not just
139 * the last inactive CSP but all other consecutive inactive CSPs after that
142 * Revision 1.21 2001/07/18 17:26:24 oes
143 * Changed to conform to new pcrs interface
145 * Revision 1.20 2001/07/17 13:07:01 oes
146 * Fixed segv when last line in config files
147 * lacked a terminating (\r)\n
149 * Revision 1.19 2001/07/13 14:01:54 oes
150 * Removed all #ifdef PCRS
152 * Revision 1.18 2001/06/29 21:45:41 oes
153 * Indentation, CRLF->LF, Tab-> Space
155 * Revision 1.17 2001/06/29 13:31:51 oes
158 * Revision 1.16 2001/06/09 10:55:28 jongfoster
159 * Changing BUFSIZ ==> BUFFER_SIZE
161 * Revision 1.15 2001/06/07 23:14:14 jongfoster
162 * Removing ACL and forward file loaders - these
163 * files have been merged into the config file.
164 * Cosmetic: Moving unloader funcs next to their
165 * respective loader funcs
167 * Revision 1.14 2001/06/01 03:27:04 oes
168 * Fixed line continuation problem
170 * Revision 1.13 2001/05/31 21:28:49 jongfoster
171 * Removed all permissionsfile code - it's now called the actions
172 * file, and (almost) all the code is in actions.c
174 * Revision 1.12 2001/05/31 17:32:31 oes
176 * - Enhanced domain part globbing with infix and prefix asterisk
177 * matching and optional unanchored operation
179 * Revision 1.11 2001/05/29 23:25:24 oes
181 * - load_config_line() and load_permissions_file() now use chomp()
183 * Revision 1.10 2001/05/29 09:50:24 jongfoster
184 * Unified blocklist/imagelist/permissionslist.
185 * File format is still under discussion, but the internal changes
188 * Also modified interceptor behaviour:
189 * - We now intercept all URLs beginning with one of the following
190 * prefixes (and *only* these prefixes):
192 * * http://ijbswa.sf.net/config/
193 * * http://ijbswa.sourceforge.net/config/
194 * - New interceptors "home page" - go to http://i.j.b/ to see it.
195 * - Internal changes so that intercepted and fast redirect pages
196 * are not replaced with an image.
197 * - Interceptors now have the option to send a binary page direct
198 * to the client. (i.e. ijb-send-banner uses this)
199 * - Implemented show-url-info interceptor. (Which is why I needed
200 * the above interceptors changes - a typical URL is
201 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
202 * The previous mechanism would not have intercepted that, and
203 * if it had been intercepted then it then it would have replaced
206 * Revision 1.9 2001/05/26 17:12:07 jongfoster
207 * Fatal errors loading configuration files now give better error messages.
209 * Revision 1.8 2001/05/26 00:55:20 jongfoster
210 * Removing duplicated code. load_forwardfile() now uses create_url_spec()
212 * Revision 1.7 2001/05/26 00:28:36 jongfoster
213 * Automatic reloading of config file.
214 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
215 * Most of the global variables have been moved to a new
216 * struct configuration_spec, accessed through csp->config->globalname
217 * Most of the globals remaining are used by the Win32 GUI.
219 * Revision 1.6 2001/05/23 12:27:33 oes
221 * Fixed ugly indentation of my last changes
223 * Revision 1.5 2001/05/23 10:39:05 oes
224 * - Added support for escaping the comment character
225 * in config files by a backslash
226 * - Added support for line continuation in config
228 * - Fixed a buffer overflow bug with long config lines
230 * Revision 1.4 2001/05/22 18:56:28 oes
233 * Revision 1.3 2001/05/20 01:21:20 jongfoster
234 * Version 2.9.4 checkin.
235 * - Merged popupfile and cookiefile, and added control over PCRS
236 * filtering, in new "permissionsfile".
237 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
238 * file error you now get a message box (in the Win32 GUI) rather
239 * than the program exiting with no explanation.
240 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
242 * - Removed tabs from "config"
243 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
244 * - Bumped up version number.
246 * Revision 1.2 2001/05/17 23:01:01 oes
247 * - Cleaned CRLF's from the sources and related files
249 * Revision 1.1.1.1 2001/05/15 13:58:59 oes
250 * Initial import of version 2.9.3 source tree
253 *********************************************************************/
260 #include <sys/types.h>
263 #include <sys/stat.h>
267 #if !defined(_WIN32) && !defined(__OS2__)
277 #include "miscutil.h"
280 #include "urlmatch.h"
282 const char loaders_h_rcs[] = LOADERS_H_VERSION;
285 * Currently active files.
286 * These are also entered in the main linked list of files.
290 static struct file_list *current_trustfile = NULL;
291 #endif /* def FEATURE_TRUST */
293 static struct file_list *current_re_filterfile = NULL;
297 /*********************************************************************
301 * Description : Basically a mark and sweep garbage collector, it is run
302 * (by the parent thread) every once in a while to reclaim memory.
304 * It uses a mark and sweep strategy:
305 * 1) mark all files as inactive
307 * 2) check with each client:
308 * if it is active, mark its files as active
309 * if it is inactive, free its resources
311 * 3) free the resources of all of the files that
312 * are still marked as inactive (and are obsolete).
314 * N.B. files that are not obsolete don't have an unloader defined.
320 *********************************************************************/
323 struct file_list *fl, *nfl;
324 struct client_state *csp, *ncsp;
326 /* clear all of the file's active flags */
327 for ( fl = files->next; NULL != fl; fl = fl->next )
332 for (csp = clients; csp && (NULL != (ncsp = csp->next)) ; csp = csp->next)
334 if (ncsp->flags & CSP_FLAG_ACTIVE)
336 /* mark this client's files as active */
339 * Always have a configuration file.
340 * (Also note the slightly non-standard extra
343 ncsp->config->config_file_list->active = 1;
345 if (ncsp->actions_list) /* actions files */
347 ncsp->actions_list->active = 1;
350 if (ncsp->rlist) /* pcrsjob files */
352 ncsp->rlist->active = 1;
356 if (ncsp->tlist) /* trust files */
358 ncsp->tlist->active = 1;
360 #endif /* def FEATURE_TRUST */
365 * this client is not active, release its resources
366 * and the ones of all inactive clients that might
370 while (!(ncsp->flags & CSP_FLAG_ACTIVE))
372 csp->next = ncsp->next;
374 freez(ncsp->ip_addr_str);
375 freez(ncsp->my_ip_addr_str);
376 freez(ncsp->my_hostname);
377 freez(ncsp->x_forwarded);
378 freez(ncsp->iob->buf);
380 free_http_request(ncsp->http);
382 destroy_list(ncsp->headers);
383 destroy_list(ncsp->cookie_list);
385 free_current_action(ncsp->action);
387 #ifdef FEATURE_STATISTICS
389 if (ncsp->flags & CSP_FLAG_REJECTED)
393 #endif /* def FEATURE_STATISTICS */
397 /* are there any more in sequence after it? */
398 if( (ncsp = csp->next) == NULL)
404 for (fl = files; fl && ((nfl = fl->next) != NULL) ; fl = fl->next)
406 if ( ( 0 == nfl->active ) && ( NULL != nfl->unloader ) )
408 fl->next = nfl->next;
410 (nfl->unloader)(nfl->f);
412 freez(nfl->filename);
421 /*********************************************************************
423 * Function : check_file_changed
425 * Description : Helper function to check if a file needs reloading.
426 * If "current" is still current, return it. Otherwise
427 * allocates a new (zeroed) "struct file_list", fills
428 * in the disk file name and timestamp, and returns it.
431 * 1 : current = The file_list currently being used - will
432 * be checked to see if it is out of date.
433 * May be NULL (which is treated as out of
435 * 2 : filename = Name of file to check.
436 * 3 : newfl = New file list. [Output only]
437 * This will be set to NULL, OR a struct
438 * file_list newly allocated on the
439 * heap, with the filename and lastmodified
440 * fields filled, and all others zeroed.
442 * Returns : If file unchanged: 0 (and sets newfl == NULL)
443 * If file changed: 1 and sets newfl != NULL
444 * On error: 1 and sets newfl == NULL
446 *********************************************************************/
447 int check_file_changed(const struct file_list * current,
448 const char * filename,
449 struct file_list ** newfl)
451 struct file_list *fs;
452 struct stat statbuf[1];
456 if (stat(filename, statbuf) < 0)
458 /* Error, probably file not found. */
463 && (current->lastmodified == statbuf->st_mtime)
464 && (0 == strcmp(current->filename, filename)))
469 fs = (struct file_list *)zalloc(sizeof(struct file_list));
472 /* Out of memory error */
476 fs->filename = strdup(filename);
477 fs->lastmodified = statbuf->st_mtime;
479 if (fs->filename == NULL)
481 /* Out of memory error */
490 /*********************************************************************
492 * Function : simple_read_line
494 * Description : Read a single line from a file and return it.
495 * This is basically a version of fgets() that malloc()s
496 * it's own line buffer. Note that the buffer will
497 * always be a multiple of BUFFER_SIZE bytes long.
498 * Therefore if you are going to keep the string for
499 * an extended period of time, you should probably
500 * strdup() it and free() the original, to save memory.
504 * 1 : dest = destination for newly malloc'd pointer to
505 * line data. Will be set to NULL on error.
506 * 2 : fp = File to read from
507 * 3 : newline = Standard for newlines in the file.
508 * Will be unchanged if it's value on input is not
510 * On output, may be changed from NEWLINE_UNKNOWN to
511 * actual convention in file.
513 * Returns : JB_ERR_OK on success
514 * JB_ERR_MEMORY on out-of-memory
515 * JB_ERR_FILE on EOF.
517 *********************************************************************/
518 jb_err simple_read_line(FILE *fp, char **dest, int *newline)
521 size_t buflen = BUFFER_SIZE;
525 int realnewline = NEWLINE_UNKNOWN;
527 if (NULL == (buf = malloc(buflen)))
529 return JB_ERR_MEMORY;
535 * Character codes. If you have a wierd compiler and the following are
536 * incorrect, you also need to fix NEWLINE() in loaders.h
538 #define CHAR_CR '\r' /* ASCII 13 */
539 #define CHAR_LF '\n' /* ASCII 10 */
559 else if (ch == CHAR_CR)
564 if (*newline == NEWLINE_UNKNOWN)
566 *newline = NEWLINE_DOS;
575 if (*newline == NEWLINE_UNKNOWN)
577 *newline = NEWLINE_MAC;
582 if (*newline == NEWLINE_UNKNOWN)
584 *newline = realnewline;
588 else if (ch == CHAR_LF)
592 if (*newline == NEWLINE_UNKNOWN)
594 *newline = NEWLINE_UNIX;
609 buflen += BUFFER_SIZE;
610 if (NULL == (p = realloc(buf, buflen)));
613 return JB_ERR_MEMORY;
622 /*********************************************************************
624 * Function : edit_read_line
626 * Description : Read a single non-empty line from a file and return
627 * it. Trims comments, leading and trailing whitespace
628 * and respects escaping of newline and comment char.
629 * Provides the line in 2 alternative forms: raw and
631 * - raw is the raw data read from the file. If the
632 * line is not modified, then this should be written
634 * - prefix is any comments and blank lines that were
635 * read from the file. If the line is modified, then
636 * this should be written out to the file followed
637 * by the modified data. (If this string is non-empty
638 * then it will have a newline at the end).
639 * - data is the actual data that will be parsed
640 * further by appropriate routines.
641 * On EOF, the 3 strings will all be set to NULL and
642 * 0 will be returned.
645 * 1 : fp = File to read from
646 * 2 : raw_out = destination for newly malloc'd pointer to
647 * raw line data. May be NULL if you don't want it.
648 * 3 : prefix_out = destination for newly malloc'd pointer to
649 * comments. May be NULL if you don't want it.
650 * 4 : data_out = destination for newly malloc'd pointer to
651 * line data with comments and leading/trailing spaces
652 * removed, and line continuation performed. May be
653 * NULL if you don't want it.
654 * 5 : newline = Standard for newlines in the file.
655 * On input, set to value to use or NEWLINE_UNKNOWN.
656 * On output, may be changed from NEWLINE_UNKNOWN to
657 * actual convention in file. May be NULL if you
659 * 6 : line_number = Line number in file. In "lines" as
660 * reported by a text editor, not lines containing data.
662 * Returns : JB_ERR_OK on success
663 * JB_ERR_MEMORY on out-of-memory
664 * JB_ERR_FILE on EOF.
666 *********************************************************************/
667 jb_err edit_read_line(FILE *fp,
672 unsigned long *line_number)
674 char *p; /* Temporary pointer */
675 char *linebuf; /* Line read from file */
676 char *linestart; /* Start of linebuf, usually first non-whitespace char */
677 int contflag = 0; /* Nonzero for line continuation - i.e. line ends '\' */
678 int is_empty = 1; /* Flag if not got any data yet */
679 char *raw = NULL; /* String to be stored in raw_out */
680 char *prefix = NULL; /* String to be stored in prefix_out */
681 char *data = NULL; /* String to be stored in data_out */
682 int scrapnewline; /* Used for (*newline) if newline==NULL */
683 jb_err rval = JB_ERR_OK;
686 assert(raw_out || data_out);
687 assert(newline == NULL
688 || *newline == NEWLINE_UNKNOWN
689 || *newline == NEWLINE_UNIX
690 || *newline == NEWLINE_DOS
691 || *newline == NEWLINE_MAC);
695 scrapnewline = NEWLINE_UNKNOWN;
696 newline = &scrapnewline;
699 /* Set output parameters to NULL */
713 /* Set string variables to new, empty strings. */
717 if ((raw = malloc(1)) == NULL)
719 return JB_ERR_MEMORY;
725 if ((prefix = malloc(1)) == NULL)
728 return JB_ERR_MEMORY;
734 if ((data = malloc(1)) == NULL)
738 return JB_ERR_MEMORY;
743 /* Main loop. Loop while we need more data & it's not EOF. */
745 while ( (contflag || is_empty)
746 && (JB_ERR_OK == (rval = simple_read_line(fp, &linebuf, newline))))
754 string_append(&raw,linebuf);
755 if (string_append(&raw,NEWLINE(*newline)))
760 return JB_ERR_MEMORY;
764 /* Line continuation? Trim escape and set flag. */
765 p = linebuf + strlen(linebuf) - 1;
766 contflag = ((*linebuf != '\0') && (*p == '\\'));
772 /* Trim leading spaces if we're at the start of the line */
776 /* Trim leading spaces */
777 while (*linestart && isspace((int)(unsigned char)*linestart))
783 /* Handle comment characters. */
785 while ((p = strchr(p, '#')) != NULL)
787 /* Found a comment char.. */
788 if ((p != linebuf) && (*(p-1) == '\\'))
790 /* ..and it's escaped, left-shift the line over the escape. */
792 while ((*q = *(q + 1)) != '\0')
796 /* Now scan from just after the "#". */
800 /* Real comment. Save it... */
803 /* Special case: Line only contains a comment, so all the
804 * previous whitespace is considered part of the comment.
805 * Undo the whitespace skipping, if any.
812 string_append(&prefix,p);
813 if (string_append(&prefix, NEWLINE(*newline)))
818 return JB_ERR_MEMORY;
822 /* ... and chop off the rest of the line */
825 } /* END while (there's a # character) */
827 /* Write to the buffer */
833 if (string_append(&data, linestart))
838 return JB_ERR_MEMORY;
844 } /* END while(we need more data) */
846 /* Handle simple_read_line() errors - ignore EOF */
847 if ((rval != JB_ERR_OK) && (rval != JB_ERR_FILE))
855 if (raw ? (*raw == '\0') : is_empty)
857 /* EOF and no data there. (Definition of "data" depends on whether
858 * the caller cares about "raw" or just "data").
869 /* Got at least some data */
871 /* Remove trailing whitespace */
884 *prefix_out = prefix;
903 /*********************************************************************
905 * Function : read_config_line
907 * Description : Read a single non-empty line from a file and return
908 * it. Trims comments, leading and trailing whitespace
909 * and respects escaping of newline and comment char.
912 * 1 : buf = Buffer to use.
913 * 2 : buflen = Size of buffer in bytes.
914 * 3 : fp = File to read from
915 * 4 : linenum = linenumber in file
917 * Returns : NULL on EOF or error
918 * Otherwise, returns buf.
920 *********************************************************************/
921 char *read_config_line(char *buf, size_t buflen, FILE *fp, unsigned long *linenum)
925 err = edit_read_line(fp, NULL, NULL, &buf2, NULL, linenum);
928 if (err == JB_ERR_MEMORY)
930 log_error(LOG_LEVEL_FATAL, "Out of memory loading a config file");
937 assert(strlen(buf2) + 1U < buflen);
938 strncpy(buf, buf2, buflen - 1);
940 buf[buflen - 1] = '\0';
947 /*********************************************************************
949 * Function : unload_trustfile
951 * Description : Unloads a trustfile.
954 * 1 : f = the data structure associated with the trustfile.
958 *********************************************************************/
959 static void unload_trustfile(void *f)
961 struct block_spec *cur = (struct block_spec *)f;
962 struct block_spec *next;
968 free_url_spec(cur->url);
977 #ifdef FEATURE_GRACEFUL_TERMINATION
978 /*********************************************************************
980 * Function : unload_current_trust_file
982 * Description : Unloads current trust file - reset to state at
983 * beginning of program.
989 *********************************************************************/
990 void unload_current_trust_file(void)
992 if (current_trustfile)
994 current_trustfile->unloader = unload_trustfile;
995 current_trustfile = NULL;
998 #endif /* FEATURE_GRACEFUL_TERMINATION */
1001 /*********************************************************************
1003 * Function : load_trustfile
1005 * Description : Read and parse a trustfile and add to files list.
1008 * 1 : csp = Current client state (buffers, headers, etc...)
1010 * Returns : 0 => Ok, everything else is an error.
1012 *********************************************************************/
1013 int load_trustfile(struct client_state *csp)
1017 struct block_spec *b, *bl;
1018 struct url_spec **tl;
1020 char buf[BUFFER_SIZE], *p, *q;
1021 int reject, trusted;
1022 struct file_list *fs;
1023 unsigned long linenum = 0;
1025 if (!check_file_changed(current_trustfile, csp->config->trustfile, &fs))
1027 /* No need to load */
1030 csp->tlist = current_trustfile;
1036 goto load_trustfile_error;
1039 fs->f = bl = (struct block_spec *)zalloc(sizeof(*bl));
1042 goto load_trustfile_error;
1045 if ((fp = fopen(csp->config->trustfile, "r")) == NULL)
1047 goto load_trustfile_error;
1050 tl = csp->config->trust_list;
1052 while (read_config_line(buf, sizeof(buf), fp, &linenum) != NULL)
1068 while ((*p++ = *q++) != '\0')
1074 /* skip blank lines */
1080 /* allocate a new node */
1081 if ((b = zalloc(sizeof(*b))) == NULL)
1084 goto load_trustfile_error;
1087 /* add it to the list */
1093 /* Save the URL pattern */
1094 if (create_url_spec(b->url, buf))
1097 goto load_trustfile_error;
1101 * save a pointer to URL's spec in the list of trusted URL's, too
1106 /* FIXME BUFFER OVERFLOW if >=64 entries */
1114 /* the old one is now obsolete */
1115 if (current_trustfile)
1117 current_trustfile->unloader = unload_trustfile;
1120 fs->next = files->next;
1122 current_trustfile = fs;
1131 load_trustfile_error:
1132 log_error(LOG_LEVEL_FATAL, "can't load trustfile '%s': %E",
1133 csp->config->trustfile);
1137 #endif /* def FEATURE_TRUST */
1140 /*********************************************************************
1142 * Function : unload_re_filterfile
1144 * Description : Unload the re_filter list by freeing all chained
1145 * re_filterfile specs and their data.
1148 * 1 : f = the data structure associated with the filterfile.
1152 *********************************************************************/
1153 static void unload_re_filterfile(void *f)
1155 struct re_filterfile_spec *a, *b = (struct re_filterfile_spec *)f;
1161 destroy_list(b->patterns);
1162 pcrs_free_joblist(b->joblist);
1164 freez(b->description);
1174 #ifdef FEATURE_GRACEFUL_TERMINATION
1175 /*********************************************************************
1177 * Function : unload_current_re_filterfile
1179 * Description : Unloads current re_filter file - reset to state at
1180 * beginning of program.
1186 *********************************************************************/
1187 void unload_current_re_filterfile(void)
1189 if (current_re_filterfile)
1191 current_re_filterfile->unloader = unload_re_filterfile;
1192 current_re_filterfile = NULL;
1198 /*********************************************************************
1200 * Function : load_re_filterfile
1202 * Description : Load the re_filterfile.
1203 * Generate a chained list of re_filterfile_spec's from
1204 * the "FILTER: " blocks, compiling all their substitutions
1205 * into chained lists of pcrs_job structs.
1208 * 1 : csp = Current client state (buffers, headers, etc...)
1210 * Returns : 0 => Ok, everything else is an error.
1212 *********************************************************************/
1213 int load_re_filterfile(struct client_state *csp)
1217 struct re_filterfile_spec *new_bl, *bl = NULL;
1218 struct file_list *fs;
1220 char buf[BUFFER_SIZE];
1222 unsigned long linenum = 0;
1226 * No need to reload if unchanged
1228 if (!check_file_changed(current_re_filterfile, csp->config->re_filterfile, &fs))
1232 csp->rlist = current_re_filterfile;
1238 goto load_re_filterfile_error;
1242 * Open the file or fail
1244 if ((fp = fopen(csp->config->re_filterfile, "r")) == NULL)
1246 goto load_re_filterfile_error;
1252 while (read_config_line(buf, sizeof(buf), fp, &linenum) != NULL)
1255 * If this is the head of a new filter block, make it a
1256 * re_filterfile spec of its own and chain it to the list:
1258 if (strncmp(buf, "FILTER:", 7) == 0)
1260 new_bl = (struct re_filterfile_spec *)zalloc(sizeof(*bl));
1263 goto load_re_filterfile_error;
1266 new_bl->name = chomp(buf + 7);
1268 if (NULL != (new_bl->description = strchr(new_bl->name, ' ')))
1270 *new_bl->description++ = '\0';
1271 new_bl->description = strdup(chomp(new_bl->description));
1275 new_bl->description = strdup("No description available for this filter");
1278 new_bl->name = strdup(chomp(new_bl->name));
1281 * If this is the first filter block, chain it
1282 * to the file_list rather than its (nonexistant)
1295 log_error(LOG_LEVEL_RE_FILTER, "Reading in filter \"%s\" (\"%s\")", bl->name, bl->description);
1301 * Else, save the expression, make it a pcrs_job
1302 * and chain it into the current filter's joblist
1306 enlist(bl->patterns, buf);
1308 if ((dummy = pcrs_compile_command(buf, &error)) == NULL)
1310 log_error(LOG_LEVEL_RE_FILTER,
1311 "Adding re_filter job %s to filter %s failed with error %d.", buf, bl->name, error);
1316 dummy->next = bl->joblist;
1317 bl->joblist = dummy;
1318 log_error(LOG_LEVEL_RE_FILTER, "Adding re_filter job %s to filter %s succeeded.", buf, bl->name);
1323 log_error(LOG_LEVEL_ERROR, "Ignoring job %s outside filter block in %s, line %d", buf, csp->config->re_filterfile, linenum);
1330 * Schedule the now-obsolete old data for unloading
1332 if ( NULL != current_re_filterfile )
1334 current_re_filterfile->unloader = unload_re_filterfile;
1338 * Chain this file into the global list of loaded files
1340 fs->next = files->next;
1342 current_re_filterfile = fs;
1351 load_re_filterfile_error:
1352 log_error(LOG_LEVEL_FATAL, "can't load re_filterfile '%s': %E",
1353 csp->config->re_filterfile);
1359 /*********************************************************************
1361 * Function : add_loader
1363 * Description : Called from `load_config'. Called once for each input
1364 * file found in config.
1367 * 1 : loader = pointer to a function that can parse and load
1368 * the appropriate config file.
1369 * 2 : config = The configuration_spec to add the loader to.
1373 *********************************************************************/
1374 void add_loader(int (*loader)(struct client_state *),
1375 struct configuration_spec * config)
1379 for (i=0; i < NLOADERS; i++)
1381 if (config->loaders[i] == NULL)
1383 config->loaders[i] = loader;
1391 /*********************************************************************
1393 * Function : run_loader
1395 * Description : Called from `load_config' and `listen_loop'. This
1396 * function keeps the "csp" current with any file mods
1397 * since the last loop. If a file is unchanged, the
1398 * loader functions do NOT reload the file.
1401 * 1 : csp = Current client state (buffers, headers, etc...)
1402 * Must be non-null. Reads: "csp->config"
1403 * Writes: various data members.
1405 * Returns : 0 => Ok, everything else is an error.
1407 *********************************************************************/
1408 int run_loader(struct client_state *csp)
1413 for (i=0; i < NLOADERS; i++)
1415 if (csp->config->loaders[i] == NULL)
1419 ret |= (csp->config->loaders[i])(csp);