1 const char loaders_rcs[] = "$Id: loaders.c,v 1.41 2002/03/12 01:42:50 oes 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 * IJBSWA 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.41 2002/03/12 01:42:50 oes
39 * Introduced modular filters
41 * Revision 1.40 2002/03/08 17:46:04 jongfoster
42 * Fixing int/size_t warnings
44 * Revision 1.39 2002/03/07 03:46:17 oes
45 * Fixed compiler warnings
47 * Revision 1.38 2002/03/06 22:54:35 jongfoster
48 * Automated function-comment nitpicking.
50 * Revision 1.37 2002/03/03 15:07:49 oes
51 * Re-enabled automatic config reloading
53 * Revision 1.36 2002/01/22 23:46:18 jongfoster
54 * Moving edit_read_line() and simple_read_line() to loaders.c, and
55 * extending them to support reading MS-DOS, Mac and UNIX style files
58 * Modifying read_config_line() (without changing it's prototype) to
59 * be a trivial wrapper for edit_read_line(). This means that we have
60 * one function to read a line and handle comments, which is common
61 * between the initialization code and the edit interface.
63 * Revision 1.35 2002/01/17 21:03:08 jongfoster
64 * Moving all our URL and URL pattern parsing code to urlmatch.c.
66 * Renaming free_url to free_url_spec, since it frees a struct url_spec.
68 * Revision 1.34 2001/12/30 14:07:32 steudten
69 * - Add signal handling (unix)
70 * - Add SIGHUP handler (unix)
71 * - Add creation of pidfile (unix)
72 * - Add action 'top' in rc file (RH)
73 * - Add entry 'SIGNALS' to manpage
74 * - Add exit message to logfile (unix)
76 * Revision 1.33 2001/11/13 00:16:38 jongfoster
77 * Replacing references to malloc.h with the standard stdlib.h
78 * (See ANSI or K&R 2nd Ed)
80 * Revision 1.32 2001/11/07 00:02:13 steudten
81 * Add line number in error output for lineparsing for
82 * actionsfile and configfile.
83 * Special handling for CLF added.
85 * Revision 1.31 2001/10/26 17:39:01 oes
86 * Removed csp->referrer
87 * Moved ijb_isspace and ijb_tolower to project.h
89 * Revision 1.30 2001/10/25 03:40:48 david__schmidt
90 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
91 * threads to call select() simultaneously. So, it's time to do a real, live,
92 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
93 * (native). Both versions will work, but using __OS2__ offers multi-threading.
95 * Revision 1.29 2001/10/23 21:38:53 jongfoster
96 * Adding error-checking to create_url_spec()
98 * Revision 1.28 2001/10/07 15:40:39 oes
99 * Replaced 6 boolean members of csp with one bitmap (csp->flags)
101 * Revision 1.27 2001/09/22 16:36:59 jongfoster
102 * Removing unused parameter fs from read_config_line()
104 * Revision 1.26 2001/09/22 14:05:22 jongfoster
105 * Bugfix: Multiple escaped "#" characters in a configuration
106 * file are now permitted.
107 * Also removing 3 unused headers.
109 * Revision 1.25 2001/09/13 22:44:03 jongfoster
110 * Adding {} to an if statement
112 * Revision 1.24 2001/07/30 22:08:36 jongfoster
113 * Tidying up #defines:
114 * - All feature #defines are now of the form FEATURE_xxx
115 * - Permanently turned off WIN_GUI_EDIT
116 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
118 * Revision 1.23 2001/07/20 15:51:54 oes
119 * Fixed indentation of prepocessor commands
121 * Revision 1.22 2001/07/20 15:16:17 haroon
122 * - per Guy's suggestion, added a while loop in sweep() to catch not just
123 * the last inactive CSP but all other consecutive inactive CSPs after that
126 * Revision 1.21 2001/07/18 17:26:24 oes
127 * Changed to conform to new pcrs interface
129 * Revision 1.20 2001/07/17 13:07:01 oes
130 * Fixed segv when last line in config files
131 * lacked a terminating (\r)\n
133 * Revision 1.19 2001/07/13 14:01:54 oes
134 * Removed all #ifdef PCRS
136 * Revision 1.18 2001/06/29 21:45:41 oes
137 * Indentation, CRLF->LF, Tab-> Space
139 * Revision 1.17 2001/06/29 13:31:51 oes
142 * Revision 1.16 2001/06/09 10:55:28 jongfoster
143 * Changing BUFSIZ ==> BUFFER_SIZE
145 * Revision 1.15 2001/06/07 23:14:14 jongfoster
146 * Removing ACL and forward file loaders - these
147 * files have been merged into the config file.
148 * Cosmetic: Moving unloader funcs next to their
149 * respective loader funcs
151 * Revision 1.14 2001/06/01 03:27:04 oes
152 * Fixed line continuation problem
154 * Revision 1.13 2001/05/31 21:28:49 jongfoster
155 * Removed all permissionsfile code - it's now called the actions
156 * file, and (almost) all the code is in actions.c
158 * Revision 1.12 2001/05/31 17:32:31 oes
160 * - Enhanced domain part globbing with infix and prefix asterisk
161 * matching and optional unanchored operation
163 * Revision 1.11 2001/05/29 23:25:24 oes
165 * - load_config_line() and load_permissions_file() now use chomp()
167 * Revision 1.10 2001/05/29 09:50:24 jongfoster
168 * Unified blocklist/imagelist/permissionslist.
169 * File format is still under discussion, but the internal changes
172 * Also modified interceptor behaviour:
173 * - We now intercept all URLs beginning with one of the following
174 * prefixes (and *only* these prefixes):
176 * * http://ijbswa.sf.net/config/
177 * * http://ijbswa.sourceforge.net/config/
178 * - New interceptors "home page" - go to http://i.j.b/ to see it.
179 * - Internal changes so that intercepted and fast redirect pages
180 * are not replaced with an image.
181 * - Interceptors now have the option to send a binary page direct
182 * to the client. (i.e. ijb-send-banner uses this)
183 * - Implemented show-url-info interceptor. (Which is why I needed
184 * the above interceptors changes - a typical URL is
185 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
186 * The previous mechanism would not have intercepted that, and
187 * if it had been intercepted then it then it would have replaced
190 * Revision 1.9 2001/05/26 17:12:07 jongfoster
191 * Fatal errors loading configuration files now give better error messages.
193 * Revision 1.8 2001/05/26 00:55:20 jongfoster
194 * Removing duplicated code. load_forwardfile() now uses create_url_spec()
196 * Revision 1.7 2001/05/26 00:28:36 jongfoster
197 * Automatic reloading of config file.
198 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
199 * Most of the global variables have been moved to a new
200 * struct configuration_spec, accessed through csp->config->globalname
201 * Most of the globals remaining are used by the Win32 GUI.
203 * Revision 1.6 2001/05/23 12:27:33 oes
205 * Fixed ugly indentation of my last changes
207 * Revision 1.5 2001/05/23 10:39:05 oes
208 * - Added support for escaping the comment character
209 * in config files by a backslash
210 * - Added support for line continuation in config
212 * - Fixed a buffer overflow bug with long config lines
214 * Revision 1.4 2001/05/22 18:56:28 oes
217 * Revision 1.3 2001/05/20 01:21:20 jongfoster
218 * Version 2.9.4 checkin.
219 * - Merged popupfile and cookiefile, and added control over PCRS
220 * filtering, in new "permissionsfile".
221 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
222 * file error you now get a message box (in the Win32 GUI) rather
223 * than the program exiting with no explanation.
224 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
226 * - Removed tabs from "config"
227 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
228 * - Bumped up version number.
230 * Revision 1.2 2001/05/17 23:01:01 oes
231 * - Cleaned CRLF's from the sources and related files
233 * Revision 1.1.1.1 2001/05/15 13:58:59 oes
234 * Initial import of version 2.9.3 source tree
237 *********************************************************************/
244 #include <sys/types.h>
247 #include <sys/stat.h>
251 #if !defined(_WIN32) && !defined(__OS2__)
261 #include "miscutil.h"
264 #include "urlmatch.h"
266 const char loaders_h_rcs[] = LOADERS_H_VERSION;
269 * Currently active files.
270 * These are also entered in the main linked list of files.
274 static struct file_list *current_trustfile = NULL;
275 #endif /* def FEATURE_TRUST */
277 static struct file_list *current_re_filterfile = NULL;
281 /*********************************************************************
285 * Description : Basically a mark and sweep garbage collector, it is run
286 * (by the parent thread) every once in a while to reclaim memory.
288 * It uses a mark and sweep strategy:
289 * 1) mark all files as inactive
291 * 2) check with each client:
292 * if it is active, mark its files as active
293 * if it is inactive, free its resources
295 * 3) free the resources of all of the files that
296 * are still marked as inactive (and are obsolete).
298 * N.B. files that are not obsolete don't have an unloader defined.
304 *********************************************************************/
307 struct file_list *fl, *nfl;
308 struct client_state *csp, *ncsp;
310 /* clear all of the file's active flags */
311 for ( fl = files->next; NULL != fl; fl = fl->next )
316 for (csp = clients; csp && (NULL != (ncsp = csp->next)) ; csp = csp->next)
318 if (ncsp->flags & CSP_FLAG_ACTIVE)
320 /* mark this client's files as active */
323 * Always have a configuration file.
324 * (Also note the slightly non-standard extra
327 ncsp->config->config_file_list->active = 1;
329 if (ncsp->actions_list) /* actions files */
331 ncsp->actions_list->active = 1;
334 if (ncsp->rlist) /* pcrsjob files */
336 ncsp->rlist->active = 1;
340 if (ncsp->tlist) /* trust files */
342 ncsp->tlist->active = 1;
344 #endif /* def FEATURE_TRUST */
349 * this client is not active, release its resources
350 * and the ones of all inactive clients that might
354 while (!(ncsp->flags & CSP_FLAG_ACTIVE))
356 csp->next = ncsp->next;
358 freez(ncsp->ip_addr_str);
359 freez(ncsp->my_ip_addr_str);
360 freez(ncsp->my_hostname);
361 freez(ncsp->x_forwarded);
362 freez(ncsp->iob->buf);
364 free_http_request(ncsp->http);
366 destroy_list(ncsp->headers);
367 destroy_list(ncsp->cookie_list);
369 free_current_action(ncsp->action);
371 #ifdef FEATURE_STATISTICS
373 if (ncsp->flags & CSP_FLAG_REJECTED)
377 #endif /* def FEATURE_STATISTICS */
381 /* are there any more in sequence after it? */
382 if( (ncsp = csp->next) == NULL)
388 for (fl = files; fl && ((nfl = fl->next) != NULL) ; fl = fl->next)
390 if ( ( 0 == nfl->active ) && ( NULL != nfl->unloader ) )
392 fl->next = nfl->next;
394 (nfl->unloader)(nfl->f);
396 freez(nfl->filename);
405 /*********************************************************************
407 * Function : check_file_changed
409 * Description : Helper function to check if a file needs reloading.
410 * If "current" is still current, return it. Otherwise
411 * allocates a new (zeroed) "struct file_list", fills
412 * in the disk file name and timestamp, and returns it.
415 * 1 : current = The file_list currently being used - will
416 * be checked to see if it is out of date.
417 * May be NULL (which is treated as out of
419 * 2 : filename = Name of file to check.
420 * 3 : newfl = New file list. [Output only]
421 * This will be set to NULL, OR a struct
422 * file_list newly allocated on the
423 * heap, with the filename and lastmodified
424 * fields filled, and all others zeroed.
426 * Returns : If file unchanged: 0 (and sets newfl == NULL)
427 * If file changed: 1 and sets newfl != NULL
428 * On error: 1 and sets newfl == NULL
430 *********************************************************************/
431 int check_file_changed(const struct file_list * current,
432 const char * filename,
433 struct file_list ** newfl)
435 struct file_list *fs;
436 struct stat statbuf[1];
440 if (stat(filename, statbuf) < 0)
442 /* Error, probably file not found. */
447 && (current->lastmodified == statbuf->st_mtime)
448 && (0 == strcmp(current->filename, filename)))
453 fs = (struct file_list *)zalloc(sizeof(struct file_list));
456 /* Out of memory error */
460 fs->filename = strdup(filename);
461 fs->lastmodified = statbuf->st_mtime;
463 if (fs->filename == NULL)
465 /* Out of memory error */
474 /*********************************************************************
476 * Function : simple_read_line
478 * Description : Read a single line from a file and return it.
479 * This is basically a version of fgets() that malloc()s
480 * it's own line buffer. Note that the buffer will
481 * always be a multiple of BUFFER_SIZE bytes long.
482 * Therefore if you are going to keep the string for
483 * an extended period of time, you should probably
484 * strdup() it and free() the original, to save memory.
488 * 1 : dest = destination for newly malloc'd pointer to
489 * line data. Will be set to NULL on error.
490 * 2 : fp = File to read from
491 * 3 : newline = Standard for newlines in the file.
492 * Will be unchanged if it's value on input is not
494 * On output, may be changed from NEWLINE_UNKNOWN to
495 * actual convention in file.
497 * Returns : JB_ERR_OK on success
498 * JB_ERR_MEMORY on out-of-memory
499 * JB_ERR_FILE on EOF.
501 *********************************************************************/
502 jb_err simple_read_line(FILE *fp, char **dest, int *newline)
505 size_t buflen = BUFFER_SIZE;
509 int realnewline = NEWLINE_UNKNOWN;
511 if (NULL == (buf = malloc(buflen)))
513 return JB_ERR_MEMORY;
519 * Character codes. If you have a wierd compiler and the following are
520 * incorrect, you also need to fix NEWLINE() in loaders.h
522 #define CHAR_CR '\r' /* ASCII 13 */
523 #define CHAR_LF '\n' /* ASCII 10 */
543 else if (ch == CHAR_CR)
548 if (*newline == NEWLINE_UNKNOWN)
550 *newline = NEWLINE_DOS;
559 if (*newline == NEWLINE_UNKNOWN)
561 *newline = NEWLINE_MAC;
566 if (*newline == NEWLINE_UNKNOWN)
568 *newline = realnewline;
572 else if (ch == CHAR_LF)
576 if (*newline == NEWLINE_UNKNOWN)
578 *newline = NEWLINE_UNIX;
593 buflen += BUFFER_SIZE;
594 if (NULL == (p = realloc(buf, buflen)));
597 return JB_ERR_MEMORY;
606 /*********************************************************************
608 * Function : edit_read_line
610 * Description : Read a single non-empty line from a file and return
611 * it. Trims comments, leading and trailing whitespace
612 * and respects escaping of newline and comment char.
613 * Provides the line in 2 alternative forms: raw and
615 * - raw is the raw data read from the file. If the
616 * line is not modified, then this should be written
618 * - prefix is any comments and blank lines that were
619 * read from the file. If the line is modified, then
620 * this should be written out to the file followed
621 * by the modified data. (If this string is non-empty
622 * then it will have a newline at the end).
623 * - data is the actual data that will be parsed
624 * further by appropriate routines.
625 * On EOF, the 3 strings will all be set to NULL and
626 * 0 will be returned.
629 * 1 : fp = File to read from
630 * 2 : raw_out = destination for newly malloc'd pointer to
631 * raw line data. May be NULL if you don't want it.
632 * 3 : prefix_out = destination for newly malloc'd pointer to
633 * comments. May be NULL if you don't want it.
634 * 4 : data_out = destination for newly malloc'd pointer to
635 * line data with comments and leading/trailing spaces
636 * removed, and line continuation performed. May be
637 * NULL if you don't want it.
638 * 5 : newline = Standard for newlines in the file.
639 * On input, set to value to use or NEWLINE_UNKNOWN.
640 * On output, may be changed from NEWLINE_UNKNOWN to
641 * actual convention in file. May be NULL if you
643 * 6 : line_number = Line number in file. In "lines" as
644 * reported by a text editor, not lines containing data.
646 * Returns : JB_ERR_OK on success
647 * JB_ERR_MEMORY on out-of-memory
648 * JB_ERR_FILE on EOF.
650 *********************************************************************/
651 jb_err edit_read_line(FILE *fp,
656 unsigned long *line_number)
658 char *p; /* Temporary pointer */
659 char *linebuf; /* Line read from file */
660 char *linestart; /* Start of linebuf, usually first non-whitespace char */
661 int contflag = 0; /* Nonzero for line continuation - i.e. line ends '\' */
662 int is_empty = 1; /* Flag if not got any data yet */
663 char *raw = NULL; /* String to be stored in raw_out */
664 char *prefix = NULL; /* String to be stored in prefix_out */
665 char *data = NULL; /* String to be stored in data_out */
666 int scrapnewline; /* Used for (*newline) if newline==NULL */
667 jb_err rval = JB_ERR_OK;
670 assert(raw_out || data_out);
671 assert(newline == NULL
672 || *newline == NEWLINE_UNKNOWN
673 || *newline == NEWLINE_UNIX
674 || *newline == NEWLINE_DOS
675 || *newline == NEWLINE_MAC);
679 scrapnewline = NEWLINE_UNKNOWN;
680 newline = &scrapnewline;
683 /* Set output parameters to NULL */
697 /* Set string variables to new, empty strings. */
701 if ((raw = malloc(1)) == NULL)
703 return JB_ERR_MEMORY;
709 if ((prefix = malloc(1)) == NULL)
712 return JB_ERR_MEMORY;
718 if ((data = malloc(1)) == NULL)
722 return JB_ERR_MEMORY;
727 /* Main loop. Loop while we need more data & it's not EOF. */
729 while ( (contflag || is_empty)
730 && (JB_ERR_OK == (rval = simple_read_line(fp, &linebuf, newline))))
738 string_append(&raw,linebuf);
739 if (string_append(&raw,NEWLINE(*newline)))
744 return JB_ERR_MEMORY;
748 /* Line continuation? Trim escape and set flag. */
749 p = linebuf + strlen(linebuf) - 1;
750 contflag = ((*linebuf != '\0') && (*p == '\\'));
756 /* Trim leading spaces if we're at the start of the line */
760 /* Trim leading spaces */
761 while (*linestart && isspace((int)(unsigned char)*linestart))
767 /* Handle comment characters. */
769 while ((p = strchr(p, '#')) != NULL)
771 /* Found a comment char.. */
772 if ((p != linebuf) && (*(p-1) == '\\'))
774 /* ..and it's escaped, left-shift the line over the escape. */
776 while ((*q = *(q + 1)) != '\0')
780 /* Now scan from just after the "#". */
784 /* Real comment. Save it... */
787 /* Special case: Line only contains a comment, so all the
788 * previous whitespace is considered part of the comment.
789 * Undo the whitespace skipping, if any.
796 string_append(&prefix,p);
797 if (string_append(&prefix, NEWLINE(*newline)))
802 return JB_ERR_MEMORY;
806 /* ... and chop off the rest of the line */
809 } /* END while (there's a # character) */
811 /* Write to the buffer */
817 if (string_append(&data, linestart))
822 return JB_ERR_MEMORY;
828 } /* END while(we need more data) */
830 /* Handle simple_read_line() errors - ignore EOF */
831 if ((rval != JB_ERR_OK) && (rval != JB_ERR_FILE))
839 if (raw ? (*raw == '\0') : is_empty)
841 /* EOF and no data there. (Definition of "data" depends on whether
842 * the caller cares about "raw" or just "data").
853 /* Got at least some data */
855 /* Remove trailing whitespace */
868 *prefix_out = prefix;
887 /*********************************************************************
889 * Function : read_config_line
891 * Description : Read a single non-empty line from a file and return
892 * it. Trims comments, leading and trailing whitespace
893 * and respects escaping of newline and comment char.
896 * 1 : buf = Buffer to use.
897 * 2 : buflen = Size of buffer in bytes.
898 * 3 : fp = File to read from
899 * 4 : linenum = linenumber in file
901 * Returns : NULL on EOF or error
902 * Otherwise, returns buf.
904 *********************************************************************/
905 char *read_config_line(char *buf, size_t buflen, FILE *fp, unsigned long *linenum)
909 err = edit_read_line(fp, NULL, NULL, &buf2, NULL, linenum);
912 if (err == JB_ERR_MEMORY)
914 log_error(LOG_LEVEL_FATAL, "Out of memory loading a config file");
921 assert(strlen(buf2) + 1U < buflen);
922 strncpy(buf, buf2, buflen - 1);
924 buf[buflen - 1] = '\0';
931 /*********************************************************************
933 * Function : unload_trustfile
935 * Description : Unloads a trustfile.
938 * 1 : f = the data structure associated with the trustfile.
942 *********************************************************************/
943 static void unload_trustfile(void *f)
945 struct block_spec *b = (struct block_spec *)f;
946 if (b == NULL) return;
948 unload_trustfile(b->next); /* Stack is cheap, isn't it? */
950 free_url_spec(b->url);
957 /*********************************************************************
959 * Function : load_trustfile
961 * Description : Read and parse a trustfile and add to files list.
964 * 1 : csp = Current client state (buffers, headers, etc...)
966 * Returns : 0 => Ok, everything else is an error.
968 *********************************************************************/
969 int load_trustfile(struct client_state *csp)
973 struct block_spec *b, *bl;
974 struct url_spec **tl;
976 char buf[BUFFER_SIZE], *p, *q;
978 struct file_list *fs;
979 unsigned long linenum = 0;
981 if (!check_file_changed(current_trustfile, csp->config->trustfile, &fs))
983 /* No need to load */
986 csp->tlist = current_trustfile;
992 goto load_trustfile_error;
995 fs->f = bl = (struct block_spec *)zalloc(sizeof(*bl));
998 goto load_trustfile_error;
1001 if ((fp = fopen(csp->config->trustfile, "r")) == NULL)
1003 goto load_trustfile_error;
1006 tl = csp->config->trust_list;
1008 while (read_config_line(buf, sizeof(buf), fp, &linenum) != NULL)
1024 while ((*p++ = *q++) != '\0')
1030 /* skip blank lines */
1036 /* allocate a new node */
1037 if ((b = zalloc(sizeof(*b))) == NULL)
1040 goto load_trustfile_error;
1043 /* add it to the list */
1049 /* Save the URL pattern */
1050 if (create_url_spec(b->url, buf))
1053 goto load_trustfile_error;
1057 * save a pointer to URL's spec in the list of trusted URL's, too
1062 /* FIXME BUFFER OVERFLOW if >=64 entries */
1070 /* the old one is now obsolete */
1071 if (current_trustfile)
1073 current_trustfile->unloader = unload_trustfile;
1076 fs->next = files->next;
1078 current_trustfile = fs;
1087 load_trustfile_error:
1088 log_error(LOG_LEVEL_FATAL, "can't load trustfile '%s': %E",
1089 csp->config->trustfile);
1093 #endif /* def FEATURE_TRUST */
1096 /*********************************************************************
1098 * Function : unload_re_filterfile
1100 * Description : Unload the re_filter list by freeing all chained
1101 * re_filterfile specs and their data.
1104 * 1 : f = the data structure associated with the filterfile.
1108 *********************************************************************/
1109 static void unload_re_filterfile(void *f)
1111 struct re_filterfile_spec *a, *b = (struct re_filterfile_spec *)f;
1117 destroy_list(b->patterns);
1118 pcrs_free_joblist(b->joblist);
1128 /*********************************************************************
1130 * Function : load_re_filterfile
1132 * Description : Load the re_filterfile.
1133 * Generate a chained list of re_filterfile_spec's from
1134 * the "FILTER: " blocks, compiling all their substitutions
1135 * into chained lists of pcrs_job structs.
1138 * 1 : csp = Current client state (buffers, headers, etc...)
1140 * Returns : 0 => Ok, everything else is an error.
1142 *********************************************************************/
1143 int load_re_filterfile(struct client_state *csp)
1147 struct re_filterfile_spec *bl, *new_bl;
1148 struct file_list *fs;
1150 char buf[BUFFER_SIZE];
1152 unsigned long linenum = 0;
1156 * No need to reload if unchanged
1158 if (!check_file_changed(current_re_filterfile, csp->config->re_filterfile, &fs))
1162 csp->rlist = current_re_filterfile;
1168 goto load_re_filterfile_error;
1172 * Allocate the first re_filterfile_spec struct
1174 fs->f = bl = (struct re_filterfile_spec *)zalloc(sizeof(*bl));
1177 goto load_re_filterfile_error;
1181 * Initialize the name in case there are
1182 * expressions before the first block header
1184 bl->filtername = "default";
1187 * Open the file or fail
1189 if ((fp = fopen(csp->config->re_filterfile, "r")) == NULL)
1191 goto load_re_filterfile_error;
1197 while (read_config_line(buf, sizeof(buf), fp, &linenum) != NULL)
1200 * If this is the head of a new filter block, make it a
1201 * re_filterfile spec of its own and chain it to the list:
1203 if (strncmp(buf, "FILTER:", 7) == 0)
1205 new_bl = (struct re_filterfile_spec *)zalloc(sizeof(*bl));
1208 goto load_re_filterfile_error;
1212 new_bl->filtername = strdup(chomp(buf + 7));
1220 * Else, save the expression, make it a pcrs_job
1221 * and chain it into the current filter's joblist
1223 enlist(bl->patterns, buf);
1225 if ((dummy = pcrs_compile_command(buf, &error)) == NULL)
1227 log_error(LOG_LEVEL_RE_FILTER,
1228 "Adding re_filter job %s to filter %s failed with error %d.", buf, bl->filtername, error);
1233 dummy->next = bl->joblist;
1234 bl->joblist = dummy;
1235 log_error(LOG_LEVEL_RE_FILTER, "Adding re_filter job %s to filter %s succeeded.", buf, bl->filtername);
1242 * Schedule the now-obsolete old data for unloading
1244 if ( NULL != current_re_filterfile )
1246 current_re_filterfile->unloader = unload_re_filterfile;
1250 * Chain this file into the global list of loaded files
1252 fs->next = files->next;
1254 current_re_filterfile = fs;
1263 load_re_filterfile_error:
1264 log_error(LOG_LEVEL_FATAL, "can't load re_filterfile '%s': %E",
1265 csp->config->re_filterfile);
1271 /*********************************************************************
1273 * Function : add_loader
1275 * Description : Called from `load_config'. Called once for each input
1276 * file found in config.
1279 * 1 : loader = pointer to a function that can parse and load
1280 * the appropriate config file.
1281 * 2 : config = The configuration_spec to add the loader to.
1285 *********************************************************************/
1286 void add_loader(int (*loader)(struct client_state *),
1287 struct configuration_spec * config)
1291 for (i=0; i < NLOADERS; i++)
1293 if (config->loaders[i] == NULL)
1295 config->loaders[i] = loader;
1303 /*********************************************************************
1305 * Function : run_loader
1307 * Description : Called from `load_config' and `listen_loop'. This
1308 * function keeps the "csp" current with any file mods
1309 * since the last loop. If a file is unchanged, the
1310 * loader functions do NOT reload the file.
1313 * 1 : csp = Current client state (buffers, headers, etc...)
1314 * Must be non-null. Reads: "csp->config"
1315 * Writes: various data members.
1317 * Returns : 0 => Ok, everything else is an error.
1319 *********************************************************************/
1320 int run_loader(struct client_state *csp)
1325 for (i=0; i < NLOADERS; i++)
1327 if (csp->config->loaders[i] == NULL)
1331 ret |= (csp->config->loaders[i])(csp);