1 const char loaders_rcs[] = "$Id: loaders.c,v 1.40 2002/03/08 17:46:04 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 * 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.40 2002/03/08 17:46:04 jongfoster
39 * Fixing int/size_t warnings
41 * Revision 1.39 2002/03/07 03:46:17 oes
42 * Fixed compiler warnings
44 * Revision 1.38 2002/03/06 22:54:35 jongfoster
45 * Automated function-comment nitpicking.
47 * Revision 1.37 2002/03/03 15:07:49 oes
48 * Re-enabled automatic config reloading
50 * Revision 1.36 2002/01/22 23:46:18 jongfoster
51 * Moving edit_read_line() and simple_read_line() to loaders.c, and
52 * extending them to support reading MS-DOS, Mac and UNIX style files
55 * Modifying read_config_line() (without changing it's prototype) to
56 * be a trivial wrapper for edit_read_line(). This means that we have
57 * one function to read a line and handle comments, which is common
58 * between the initialization code and the edit interface.
60 * Revision 1.35 2002/01/17 21:03:08 jongfoster
61 * Moving all our URL and URL pattern parsing code to urlmatch.c.
63 * Renaming free_url to free_url_spec, since it frees a struct url_spec.
65 * Revision 1.34 2001/12/30 14:07:32 steudten
66 * - Add signal handling (unix)
67 * - Add SIGHUP handler (unix)
68 * - Add creation of pidfile (unix)
69 * - Add action 'top' in rc file (RH)
70 * - Add entry 'SIGNALS' to manpage
71 * - Add exit message to logfile (unix)
73 * Revision 1.33 2001/11/13 00:16:38 jongfoster
74 * Replacing references to malloc.h with the standard stdlib.h
75 * (See ANSI or K&R 2nd Ed)
77 * Revision 1.32 2001/11/07 00:02:13 steudten
78 * Add line number in error output for lineparsing for
79 * actionsfile and configfile.
80 * Special handling for CLF added.
82 * Revision 1.31 2001/10/26 17:39:01 oes
83 * Removed csp->referrer
84 * Moved ijb_isspace and ijb_tolower to project.h
86 * Revision 1.30 2001/10/25 03:40:48 david__schmidt
87 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
88 * threads to call select() simultaneously. So, it's time to do a real, live,
89 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
90 * (native). Both versions will work, but using __OS2__ offers multi-threading.
92 * Revision 1.29 2001/10/23 21:38:53 jongfoster
93 * Adding error-checking to create_url_spec()
95 * Revision 1.28 2001/10/07 15:40:39 oes
96 * Replaced 6 boolean members of csp with one bitmap (csp->flags)
98 * Revision 1.27 2001/09/22 16:36:59 jongfoster
99 * Removing unused parameter fs from read_config_line()
101 * Revision 1.26 2001/09/22 14:05:22 jongfoster
102 * Bugfix: Multiple escaped "#" characters in a configuration
103 * file are now permitted.
104 * Also removing 3 unused headers.
106 * Revision 1.25 2001/09/13 22:44:03 jongfoster
107 * Adding {} to an if statement
109 * Revision 1.24 2001/07/30 22:08:36 jongfoster
110 * Tidying up #defines:
111 * - All feature #defines are now of the form FEATURE_xxx
112 * - Permanently turned off WIN_GUI_EDIT
113 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
115 * Revision 1.23 2001/07/20 15:51:54 oes
116 * Fixed indentation of prepocessor commands
118 * Revision 1.22 2001/07/20 15:16:17 haroon
119 * - per Guy's suggestion, added a while loop in sweep() to catch not just
120 * the last inactive CSP but all other consecutive inactive CSPs after that
123 * Revision 1.21 2001/07/18 17:26:24 oes
124 * Changed to conform to new pcrs interface
126 * Revision 1.20 2001/07/17 13:07:01 oes
127 * Fixed segv when last line in config files
128 * lacked a terminating (\r)\n
130 * Revision 1.19 2001/07/13 14:01:54 oes
131 * Removed all #ifdef PCRS
133 * Revision 1.18 2001/06/29 21:45:41 oes
134 * Indentation, CRLF->LF, Tab-> Space
136 * Revision 1.17 2001/06/29 13:31:51 oes
139 * Revision 1.16 2001/06/09 10:55:28 jongfoster
140 * Changing BUFSIZ ==> BUFFER_SIZE
142 * Revision 1.15 2001/06/07 23:14:14 jongfoster
143 * Removing ACL and forward file loaders - these
144 * files have been merged into the config file.
145 * Cosmetic: Moving unloader funcs next to their
146 * respective loader funcs
148 * Revision 1.14 2001/06/01 03:27:04 oes
149 * Fixed line continuation problem
151 * Revision 1.13 2001/05/31 21:28:49 jongfoster
152 * Removed all permissionsfile code - it's now called the actions
153 * file, and (almost) all the code is in actions.c
155 * Revision 1.12 2001/05/31 17:32:31 oes
157 * - Enhanced domain part globbing with infix and prefix asterisk
158 * matching and optional unanchored operation
160 * Revision 1.11 2001/05/29 23:25:24 oes
162 * - load_config_line() and load_permissions_file() now use chomp()
164 * Revision 1.10 2001/05/29 09:50:24 jongfoster
165 * Unified blocklist/imagelist/permissionslist.
166 * File format is still under discussion, but the internal changes
169 * Also modified interceptor behaviour:
170 * - We now intercept all URLs beginning with one of the following
171 * prefixes (and *only* these prefixes):
173 * * http://ijbswa.sf.net/config/
174 * * http://ijbswa.sourceforge.net/config/
175 * - New interceptors "home page" - go to http://i.j.b/ to see it.
176 * - Internal changes so that intercepted and fast redirect pages
177 * are not replaced with an image.
178 * - Interceptors now have the option to send a binary page direct
179 * to the client. (i.e. ijb-send-banner uses this)
180 * - Implemented show-url-info interceptor. (Which is why I needed
181 * the above interceptors changes - a typical URL is
182 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
183 * The previous mechanism would not have intercepted that, and
184 * if it had been intercepted then it then it would have replaced
187 * Revision 1.9 2001/05/26 17:12:07 jongfoster
188 * Fatal errors loading configuration files now give better error messages.
190 * Revision 1.8 2001/05/26 00:55:20 jongfoster
191 * Removing duplicated code. load_forwardfile() now uses create_url_spec()
193 * Revision 1.7 2001/05/26 00:28:36 jongfoster
194 * Automatic reloading of config file.
195 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
196 * Most of the global variables have been moved to a new
197 * struct configuration_spec, accessed through csp->config->globalname
198 * Most of the globals remaining are used by the Win32 GUI.
200 * Revision 1.6 2001/05/23 12:27:33 oes
202 * Fixed ugly indentation of my last changes
204 * Revision 1.5 2001/05/23 10:39:05 oes
205 * - Added support for escaping the comment character
206 * in config files by a backslash
207 * - Added support for line continuation in config
209 * - Fixed a buffer overflow bug with long config lines
211 * Revision 1.4 2001/05/22 18:56:28 oes
214 * Revision 1.3 2001/05/20 01:21:20 jongfoster
215 * Version 2.9.4 checkin.
216 * - Merged popupfile and cookiefile, and added control over PCRS
217 * filtering, in new "permissionsfile".
218 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
219 * file error you now get a message box (in the Win32 GUI) rather
220 * than the program exiting with no explanation.
221 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
223 * - Removed tabs from "config"
224 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
225 * - Bumped up version number.
227 * Revision 1.2 2001/05/17 23:01:01 oes
228 * - Cleaned CRLF's from the sources and related files
230 * Revision 1.1.1.1 2001/05/15 13:58:59 oes
231 * Initial import of version 2.9.3 source tree
234 *********************************************************************/
241 #include <sys/types.h>
244 #include <sys/stat.h>
248 #if !defined(_WIN32) && !defined(__OS2__)
258 #include "miscutil.h"
261 #include "urlmatch.h"
263 const char loaders_h_rcs[] = LOADERS_H_VERSION;
266 * Currently active files.
267 * These are also entered in the main linked list of files.
271 static struct file_list *current_trustfile = NULL;
272 #endif /* def FEATURE_TRUST */
274 static struct file_list *current_re_filterfile = NULL;
278 /*********************************************************************
282 * Description : Basically a mark and sweep garbage collector, it is run
283 * (by the parent thread) every once in a while to reclaim memory.
285 * It uses a mark and sweep strategy:
286 * 1) mark all files as inactive
288 * 2) check with each client:
289 * if it is active, mark its files as active
290 * if it is inactive, free its resources
292 * 3) free the resources of all of the files that
293 * are still marked as inactive (and are obsolete).
295 * N.B. files that are not obsolete don't have an unloader defined.
301 *********************************************************************/
304 struct file_list *fl, *nfl;
305 struct client_state *csp, *ncsp;
307 /* clear all of the file's active flags */
308 for ( fl = files->next; NULL != fl; fl = fl->next )
313 for (csp = clients; csp && (ncsp = csp->next) ; csp = csp->next)
315 if (ncsp->flags & CSP_FLAG_ACTIVE)
317 /* mark this client's files as active */
320 * Always have a configuration file.
321 * (Also note the slightly non-standard extra
324 ncsp->config->config_file_list->active = 1;
326 if (ncsp->actions_list) /* actions files */
328 ncsp->actions_list->active = 1;
331 if (ncsp->rlist) /* pcrsjob files */
333 ncsp->rlist->active = 1;
337 if (ncsp->tlist) /* trust files */
339 ncsp->tlist->active = 1;
341 #endif /* def FEATURE_TRUST */
346 * this client is not active, release its resources
347 * and the ones of all inactive clients that might
351 while (!(ncsp->flags & CSP_FLAG_ACTIVE))
353 csp->next = ncsp->next;
355 freez(ncsp->ip_addr_str);
356 freez(ncsp->my_ip_addr_str);
357 freez(ncsp->my_hostname);
358 freez(ncsp->x_forwarded);
359 freez(ncsp->iob->buf);
361 free_http_request(ncsp->http);
363 destroy_list(ncsp->headers);
364 destroy_list(ncsp->cookie_list);
366 free_current_action(ncsp->action);
368 #ifdef FEATURE_STATISTICS
370 if (ncsp->flags & CSP_FLAG_REJECTED)
374 #endif /* def FEATURE_STATISTICS */
378 /* are there any more in sequence after it? */
379 if( !(ncsp = csp->next) )
385 for (fl = files; fl && (nfl = fl->next) ; fl = fl->next)
387 if ( ( 0 == nfl->active ) && ( NULL != nfl->unloader ) )
389 fl->next = nfl->next;
391 (nfl->unloader)(nfl->f);
393 freez(nfl->filename);
402 /*********************************************************************
404 * Function : check_file_changed
406 * Description : Helper function to check if a file needs reloading.
407 * If "current" is still current, return it. Otherwise
408 * allocates a new (zeroed) "struct file_list", fills
409 * in the disk file name and timestamp, and returns it.
412 * 1 : current = The file_list currently being used - will
413 * be checked to see if it is out of date.
414 * May be NULL (which is treated as out of
416 * 2 : filename = Name of file to check.
417 * 3 : newfl = New file list. [Output only]
418 * This will be set to NULL, OR a struct
419 * file_list newly allocated on the
420 * heap, with the filename and lastmodified
421 * fields filled, and all others zeroed.
423 * Returns : If file unchanged: 0 (and sets newfl == NULL)
424 * If file changed: 1 and sets newfl != NULL
425 * On error: 1 and sets newfl == NULL
427 *********************************************************************/
428 int check_file_changed(const struct file_list * current,
429 const char * filename,
430 struct file_list ** newfl)
432 struct file_list *fs;
433 struct stat statbuf[1];
437 if (stat(filename, statbuf) < 0)
439 /* Error, probably file not found. */
444 && (current->lastmodified == statbuf->st_mtime)
445 && (0 == strcmp(current->filename, filename)))
450 fs = (struct file_list *)zalloc(sizeof(struct file_list));
453 /* Out of memory error */
457 fs->filename = strdup(filename);
458 fs->lastmodified = statbuf->st_mtime;
460 if (fs->filename == NULL)
462 /* Out of memory error */
471 /*********************************************************************
473 * Function : simple_read_line
475 * Description : Read a single line from a file and return it.
476 * This is basically a version of fgets() that malloc()s
477 * it's own line buffer. Note that the buffer will
478 * always be a multiple of BUFFER_SIZE bytes long.
479 * Therefore if you are going to keep the string for
480 * an extended period of time, you should probably
481 * strdup() it and free() the original, to save memory.
485 * 1 : dest = destination for newly malloc'd pointer to
486 * line data. Will be set to NULL on error.
487 * 2 : fp = File to read from
488 * 3 : newline = Standard for newlines in the file.
489 * Will be unchanged if it's value on input is not
491 * On output, may be changed from NEWLINE_UNKNOWN to
492 * actual convention in file.
494 * Returns : JB_ERR_OK on success
495 * JB_ERR_MEMORY on out-of-memory
496 * JB_ERR_FILE on EOF.
498 *********************************************************************/
499 jb_err simple_read_line(FILE *fp, char **dest, int *newline)
502 size_t buflen = BUFFER_SIZE;
506 int realnewline = NEWLINE_UNKNOWN;
508 if (NULL == (buf = malloc(buflen)))
510 return JB_ERR_MEMORY;
516 * Character codes. If you have a wierd compiler and the following are
517 * incorrect, you also need to fix NEWLINE() in loaders.h
519 #define CHAR_CR '\r' /* ASCII 13 */
520 #define CHAR_LF '\n' /* ASCII 10 */
540 else if (ch == CHAR_CR)
545 if (*newline == NEWLINE_UNKNOWN)
547 *newline = NEWLINE_DOS;
556 if (*newline == NEWLINE_UNKNOWN)
558 *newline = NEWLINE_MAC;
563 if (*newline == NEWLINE_UNKNOWN)
565 *newline = realnewline;
569 else if (ch == CHAR_LF)
573 if (*newline == NEWLINE_UNKNOWN)
575 *newline = NEWLINE_UNIX;
590 buflen += BUFFER_SIZE;
591 if (NULL == (p = realloc(buf, buflen)));
594 return JB_ERR_MEMORY;
603 /*********************************************************************
605 * Function : edit_read_line
607 * Description : Read a single non-empty line from a file and return
608 * it. Trims comments, leading and trailing whitespace
609 * and respects escaping of newline and comment char.
610 * Provides the line in 2 alternative forms: raw and
612 * - raw is the raw data read from the file. If the
613 * line is not modified, then this should be written
615 * - prefix is any comments and blank lines that were
616 * read from the file. If the line is modified, then
617 * this should be written out to the file followed
618 * by the modified data. (If this string is non-empty
619 * then it will have a newline at the end).
620 * - data is the actual data that will be parsed
621 * further by appropriate routines.
622 * On EOF, the 3 strings will all be set to NULL and
623 * 0 will be returned.
626 * 1 : fp = File to read from
627 * 2 : raw_out = destination for newly malloc'd pointer to
628 * raw line data. May be NULL if you don't want it.
629 * 3 : prefix_out = destination for newly malloc'd pointer to
630 * comments. May be NULL if you don't want it.
631 * 4 : data_out = destination for newly malloc'd pointer to
632 * line data with comments and leading/trailing spaces
633 * removed, and line continuation performed. May be
634 * NULL if you don't want it.
635 * 5 : newline = Standard for newlines in the file.
636 * On input, set to value to use or NEWLINE_UNKNOWN.
637 * On output, may be changed from NEWLINE_UNKNOWN to
638 * actual convention in file. May be NULL if you
640 * 6 : line_number = Line number in file. In "lines" as
641 * reported by a text editor, not lines containing data.
643 * Returns : JB_ERR_OK on success
644 * JB_ERR_MEMORY on out-of-memory
645 * JB_ERR_FILE on EOF.
647 *********************************************************************/
648 jb_err edit_read_line(FILE *fp,
653 unsigned long *line_number)
655 char *p; /* Temporary pointer */
656 char *linebuf; /* Line read from file */
657 char *linestart; /* Start of linebuf, usually first non-whitespace char */
658 int contflag = 0; /* Nonzero for line continuation - i.e. line ends '\' */
659 int is_empty = 1; /* Flag if not got any data yet */
660 char *raw = NULL; /* String to be stored in raw_out */
661 char *prefix = NULL; /* String to be stored in prefix_out */
662 char *data = NULL; /* String to be stored in data_out */
663 int scrapnewline; /* Used for (*newline) if newline==NULL */
664 jb_err rval = JB_ERR_OK;
667 assert(raw_out || data_out);
668 assert(newline == NULL
669 || *newline == NEWLINE_UNKNOWN
670 || *newline == NEWLINE_UNIX
671 || *newline == NEWLINE_DOS
672 || *newline == NEWLINE_MAC);
676 scrapnewline = NEWLINE_UNKNOWN;
677 newline = &scrapnewline;
680 /* Set output parameters to NULL */
694 /* Set string variables to new, empty strings. */
698 if ((raw = malloc(1)) == NULL)
700 return JB_ERR_MEMORY;
706 if ((prefix = malloc(1)) == NULL)
709 return JB_ERR_MEMORY;
715 if ((data = malloc(1)) == NULL)
719 return JB_ERR_MEMORY;
724 /* Main loop. Loop while we need more data & it's not EOF. */
726 while ( (contflag || is_empty)
727 && (JB_ERR_OK == (rval = simple_read_line(fp, &linebuf, newline))))
735 string_append(&raw,linebuf);
736 if (string_append(&raw,NEWLINE(*newline)))
741 return JB_ERR_MEMORY;
745 /* Line continuation? Trim escape and set flag. */
746 p = linebuf + strlen(linebuf) - 1;
747 contflag = ((*linebuf != '\0') && (*p == '\\'));
753 /* Trim leading spaces if we're at the start of the line */
757 /* Trim leading spaces */
758 while (*linestart && isspace((int)(unsigned char)*linestart))
764 /* Handle comment characters. */
766 while ((p = strchr(p, '#')) != NULL)
768 /* Found a comment char.. */
769 if ((p != linebuf) && (*(p-1) == '\\'))
771 /* ..and it's escaped, left-shift the line over the escape. */
773 while ((*q = *(q + 1)) != '\0')
777 /* Now scan from just after the "#". */
781 /* Real comment. Save it... */
784 /* Special case: Line only contains a comment, so all the
785 * previous whitespace is considered part of the comment.
786 * Undo the whitespace skipping, if any.
793 string_append(&prefix,p);
794 if (string_append(&prefix, NEWLINE(*newline)))
799 return JB_ERR_MEMORY;
803 /* ... and chop off the rest of the line */
806 } /* END while (there's a # character) */
808 /* Write to the buffer */
814 if (string_append(&data, linestart))
819 return JB_ERR_MEMORY;
825 } /* END while(we need more data) */
827 /* Handle simple_read_line() errors - ignore EOF */
828 if ((rval != JB_ERR_OK) && (rval != JB_ERR_FILE))
836 if (raw ? (*raw == '\0') : is_empty)
838 /* EOF and no data there. (Definition of "data" depends on whether
839 * the caller cares about "raw" or just "data").
850 /* Got at least some data */
852 /* Remove trailing whitespace */
865 *prefix_out = prefix;
884 /*********************************************************************
886 * Function : read_config_line
888 * Description : Read a single non-empty line from a file and return
889 * it. Trims comments, leading and trailing whitespace
890 * and respects escaping of newline and comment char.
893 * 1 : buf = Buffer to use.
894 * 2 : buflen = Size of buffer in bytes.
895 * 3 : fp = File to read from
896 * 4 : linenum = linenumber in file
898 * Returns : NULL on EOF or error
899 * Otherwise, returns buf.
901 *********************************************************************/
902 char *read_config_line(char *buf, size_t buflen, FILE *fp, unsigned long *linenum)
906 err = edit_read_line(fp, NULL, NULL, &buf2, NULL, linenum);
909 if (err == JB_ERR_MEMORY)
911 log_error(LOG_LEVEL_FATAL, "Out of memory loading a config file");
918 assert(strlen(buf2) + 1U < buflen);
919 strncpy(buf, buf2, buflen - 1);
921 buf[buflen - 1] = '\0';
928 /*********************************************************************
930 * Function : unload_trustfile
932 * Description : Unloads a trustfile.
935 * 1 : f = the data structure associated with the trustfile.
939 *********************************************************************/
940 static void unload_trustfile(void *f)
942 struct block_spec *b = (struct block_spec *)f;
943 if (b == NULL) return;
945 unload_trustfile(b->next); /* Stack is cheap, isn't it? */
947 free_url_spec(b->url);
954 /*********************************************************************
956 * Function : load_trustfile
958 * Description : Read and parse a trustfile and add to files list.
961 * 1 : csp = Current client state (buffers, headers, etc...)
963 * Returns : 0 => Ok, everything else is an error.
965 *********************************************************************/
966 int load_trustfile(struct client_state *csp)
970 struct block_spec *b, *bl;
971 struct url_spec **tl;
973 char buf[BUFFER_SIZE], *p, *q;
975 struct file_list *fs;
976 unsigned long linenum = 0;
978 if (!check_file_changed(current_trustfile, csp->config->trustfile, &fs))
980 /* No need to load */
983 csp->tlist = current_trustfile;
989 goto load_trustfile_error;
992 fs->f = bl = (struct block_spec *)zalloc(sizeof(*bl));
995 goto load_trustfile_error;
998 if ((fp = fopen(csp->config->trustfile, "r")) == NULL)
1000 goto load_trustfile_error;
1003 tl = csp->config->trust_list;
1005 while (read_config_line(buf, sizeof(buf), fp, &linenum) != NULL)
1021 while ((*p++ = *q++))
1027 /* skip blank lines */
1033 /* allocate a new node */
1034 if ((b = zalloc(sizeof(*b))) == NULL)
1037 goto load_trustfile_error;
1040 /* add it to the list */
1046 /* Save the URL pattern */
1047 if (create_url_spec(b->url, buf))
1050 goto load_trustfile_error;
1054 * save a pointer to URL's spec in the list of trusted URL's, too
1059 /* FIXME BUFFER OVERFLOW if >=64 entries */
1067 /* the old one is now obsolete */
1068 if (current_trustfile)
1070 current_trustfile->unloader = unload_trustfile;
1073 fs->next = files->next;
1075 current_trustfile = fs;
1084 load_trustfile_error:
1085 log_error(LOG_LEVEL_FATAL, "can't load trustfile '%s': %E",
1086 csp->config->trustfile);
1090 #endif /* def FEATURE_TRUST */
1093 /*********************************************************************
1095 * Function : unload_re_filterfile
1097 * Description : Unload the re_filter list by freeing all chained
1098 * re_filterfile specs and their data.
1101 * 1 : f = the data structure associated with the filterfile.
1105 *********************************************************************/
1106 static void unload_re_filterfile(void *f)
1108 struct re_filterfile_spec *a, *b = (struct re_filterfile_spec *)f;
1114 destroy_list(b->patterns);
1115 pcrs_free_joblist(b->joblist);
1125 /*********************************************************************
1127 * Function : load_re_filterfile
1129 * Description : Load the re_filterfile.
1130 * Generate a chained list of re_filterfile_spec's from
1131 * the "FILTER: " blocks, compiling all their substitutions
1132 * into chained lists of pcrs_job structs.
1135 * 1 : csp = Current client state (buffers, headers, etc...)
1137 * Returns : 0 => Ok, everything else is an error.
1139 *********************************************************************/
1140 int load_re_filterfile(struct client_state *csp)
1144 struct re_filterfile_spec *bl, *new_bl;
1145 struct file_list *fs;
1147 char buf[BUFFER_SIZE];
1149 unsigned long linenum = 0;
1153 * No need to reload if unchanged
1155 if (!check_file_changed(current_re_filterfile, csp->config->re_filterfile, &fs))
1159 csp->rlist = current_re_filterfile;
1165 goto load_re_filterfile_error;
1169 * Allocate the first re_filterfile_spec struct
1171 fs->f = bl = (struct re_filterfile_spec *)zalloc(sizeof(*bl));
1174 goto load_re_filterfile_error;
1178 * Initialize the name in case there are
1179 * expressions before the first block header
1181 bl->filtername = "default";
1184 * Open the file or fail
1186 if ((fp = fopen(csp->config->re_filterfile, "r")) == NULL)
1188 goto load_re_filterfile_error;
1194 while (read_config_line(buf, sizeof(buf), fp, &linenum) != NULL)
1197 * If this is the head of a new filter block, make it a
1198 * re_filterfile spec of its own and chain it to the list:
1200 if (strncmp(buf, "FILTER:", 7) == 0)
1202 new_bl = (struct re_filterfile_spec *)zalloc(sizeof(*bl));
1205 goto load_re_filterfile_error;
1209 new_bl->filtername = strdup(chomp(buf + 7));
1217 * Else, save the expression, make it a pcrs_job
1218 * and chain it into the current filter's joblist
1220 enlist(bl->patterns, buf);
1222 if ((dummy = pcrs_compile_command(buf, &error)) == NULL)
1224 log_error(LOG_LEVEL_RE_FILTER,
1225 "Adding re_filter job %s to filter %s failed with error %d.", buf, bl->filtername, error);
1230 dummy->next = bl->joblist;
1231 bl->joblist = dummy;
1232 log_error(LOG_LEVEL_RE_FILTER, "Adding re_filter job %s to filter %s succeeded.", buf, bl->filtername);
1239 * Schedule the now-obsolete old data for unloading
1241 if ( NULL != current_re_filterfile )
1243 current_re_filterfile->unloader = unload_re_filterfile;
1247 * Chain this file into the global list of loaded files
1249 fs->next = files->next;
1251 current_re_filterfile = fs;
1260 load_re_filterfile_error:
1261 log_error(LOG_LEVEL_FATAL, "can't load re_filterfile '%s': %E",
1262 csp->config->re_filterfile);
1268 /*********************************************************************
1270 * Function : add_loader
1272 * Description : Called from `load_config'. Called once for each input
1273 * file found in config.
1276 * 1 : loader = pointer to a function that can parse and load
1277 * the appropriate config file.
1278 * 2 : config = The configuration_spec to add the loader to.
1282 *********************************************************************/
1283 void add_loader(int (*loader)(struct client_state *),
1284 struct configuration_spec * config)
1288 for (i=0; i < NLOADERS; i++)
1290 if (config->loaders[i] == NULL)
1292 config->loaders[i] = loader;
1300 /*********************************************************************
1302 * Function : run_loader
1304 * Description : Called from `load_config' and `listen_loop'. This
1305 * function keeps the "csp" current with any file mods
1306 * since the last loop. If a file is unchanged, the
1307 * loader functions do NOT reload the file.
1310 * 1 : csp = Current client state (buffers, headers, etc...)
1311 * Must be non-null. Reads: "csp->config"
1312 * Writes: various data members.
1314 * Returns : 0 => Ok, everything else is an error.
1316 *********************************************************************/
1317 int run_loader(struct client_state *csp)
1322 for (i=0; i < NLOADERS; i++)
1324 if (csp->config->loaders[i] == NULL)
1328 ret |= (csp->config->loaders[i])(csp);