1 const char loaders_rcs[] = "$Id: loaders.c,v 1.43 2002/03/16 20:28:34 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.43 2002/03/16 20:28:34 oes
39 * Added descriptions to the filters so users will know what they select in the cgi editor
41 * Revision 1.42 2002/03/13 00:27:05 jongfoster
44 * Revision 1.41 2002/03/12 01:42:50 oes
45 * Introduced modular filters
47 * Revision 1.40 2002/03/08 17:46:04 jongfoster
48 * Fixing int/size_t warnings
50 * Revision 1.39 2002/03/07 03:46:17 oes
51 * Fixed compiler warnings
53 * Revision 1.38 2002/03/06 22:54:35 jongfoster
54 * Automated function-comment nitpicking.
56 * Revision 1.37 2002/03/03 15:07:49 oes
57 * Re-enabled automatic config reloading
59 * Revision 1.36 2002/01/22 23:46:18 jongfoster
60 * Moving edit_read_line() and simple_read_line() to loaders.c, and
61 * extending them to support reading MS-DOS, Mac and UNIX style files
64 * Modifying read_config_line() (without changing it's prototype) to
65 * be a trivial wrapper for edit_read_line(). This means that we have
66 * one function to read a line and handle comments, which is common
67 * between the initialization code and the edit interface.
69 * Revision 1.35 2002/01/17 21:03:08 jongfoster
70 * Moving all our URL and URL pattern parsing code to urlmatch.c.
72 * Renaming free_url to free_url_spec, since it frees a struct url_spec.
74 * Revision 1.34 2001/12/30 14:07:32 steudten
75 * - Add signal handling (unix)
76 * - Add SIGHUP handler (unix)
77 * - Add creation of pidfile (unix)
78 * - Add action 'top' in rc file (RH)
79 * - Add entry 'SIGNALS' to manpage
80 * - Add exit message to logfile (unix)
82 * Revision 1.33 2001/11/13 00:16:38 jongfoster
83 * Replacing references to malloc.h with the standard stdlib.h
84 * (See ANSI or K&R 2nd Ed)
86 * Revision 1.32 2001/11/07 00:02:13 steudten
87 * Add line number in error output for lineparsing for
88 * actionsfile and configfile.
89 * Special handling for CLF added.
91 * Revision 1.31 2001/10/26 17:39:01 oes
92 * Removed csp->referrer
93 * Moved ijb_isspace and ijb_tolower to project.h
95 * Revision 1.30 2001/10/25 03:40:48 david__schmidt
96 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
97 * threads to call select() simultaneously. So, it's time to do a real, live,
98 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
99 * (native). Both versions will work, but using __OS2__ offers multi-threading.
101 * Revision 1.29 2001/10/23 21:38:53 jongfoster
102 * Adding error-checking to create_url_spec()
104 * Revision 1.28 2001/10/07 15:40:39 oes
105 * Replaced 6 boolean members of csp with one bitmap (csp->flags)
107 * Revision 1.27 2001/09/22 16:36:59 jongfoster
108 * Removing unused parameter fs from read_config_line()
110 * Revision 1.26 2001/09/22 14:05:22 jongfoster
111 * Bugfix: Multiple escaped "#" characters in a configuration
112 * file are now permitted.
113 * Also removing 3 unused headers.
115 * Revision 1.25 2001/09/13 22:44:03 jongfoster
116 * Adding {} to an if statement
118 * Revision 1.24 2001/07/30 22:08:36 jongfoster
119 * Tidying up #defines:
120 * - All feature #defines are now of the form FEATURE_xxx
121 * - Permanently turned off WIN_GUI_EDIT
122 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
124 * Revision 1.23 2001/07/20 15:51:54 oes
125 * Fixed indentation of prepocessor commands
127 * Revision 1.22 2001/07/20 15:16:17 haroon
128 * - per Guy's suggestion, added a while loop in sweep() to catch not just
129 * the last inactive CSP but all other consecutive inactive CSPs after that
132 * Revision 1.21 2001/07/18 17:26:24 oes
133 * Changed to conform to new pcrs interface
135 * Revision 1.20 2001/07/17 13:07:01 oes
136 * Fixed segv when last line in config files
137 * lacked a terminating (\r)\n
139 * Revision 1.19 2001/07/13 14:01:54 oes
140 * Removed all #ifdef PCRS
142 * Revision 1.18 2001/06/29 21:45:41 oes
143 * Indentation, CRLF->LF, Tab-> Space
145 * Revision 1.17 2001/06/29 13:31:51 oes
148 * Revision 1.16 2001/06/09 10:55:28 jongfoster
149 * Changing BUFSIZ ==> BUFFER_SIZE
151 * Revision 1.15 2001/06/07 23:14:14 jongfoster
152 * Removing ACL and forward file loaders - these
153 * files have been merged into the config file.
154 * Cosmetic: Moving unloader funcs next to their
155 * respective loader funcs
157 * Revision 1.14 2001/06/01 03:27:04 oes
158 * Fixed line continuation problem
160 * Revision 1.13 2001/05/31 21:28:49 jongfoster
161 * Removed all permissionsfile code - it's now called the actions
162 * file, and (almost) all the code is in actions.c
164 * Revision 1.12 2001/05/31 17:32:31 oes
166 * - Enhanced domain part globbing with infix and prefix asterisk
167 * matching and optional unanchored operation
169 * Revision 1.11 2001/05/29 23:25:24 oes
171 * - load_config_line() and load_permissions_file() now use chomp()
173 * Revision 1.10 2001/05/29 09:50:24 jongfoster
174 * Unified blocklist/imagelist/permissionslist.
175 * File format is still under discussion, but the internal changes
178 * Also modified interceptor behaviour:
179 * - We now intercept all URLs beginning with one of the following
180 * prefixes (and *only* these prefixes):
182 * * http://ijbswa.sf.net/config/
183 * * http://ijbswa.sourceforge.net/config/
184 * - New interceptors "home page" - go to http://i.j.b/ to see it.
185 * - Internal changes so that intercepted and fast redirect pages
186 * are not replaced with an image.
187 * - Interceptors now have the option to send a binary page direct
188 * to the client. (i.e. ijb-send-banner uses this)
189 * - Implemented show-url-info interceptor. (Which is why I needed
190 * the above interceptors changes - a typical URL is
191 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
192 * The previous mechanism would not have intercepted that, and
193 * if it had been intercepted then it then it would have replaced
196 * Revision 1.9 2001/05/26 17:12:07 jongfoster
197 * Fatal errors loading configuration files now give better error messages.
199 * Revision 1.8 2001/05/26 00:55:20 jongfoster
200 * Removing duplicated code. load_forwardfile() now uses create_url_spec()
202 * Revision 1.7 2001/05/26 00:28:36 jongfoster
203 * Automatic reloading of config file.
204 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
205 * Most of the global variables have been moved to a new
206 * struct configuration_spec, accessed through csp->config->globalname
207 * Most of the globals remaining are used by the Win32 GUI.
209 * Revision 1.6 2001/05/23 12:27:33 oes
211 * Fixed ugly indentation of my last changes
213 * Revision 1.5 2001/05/23 10:39:05 oes
214 * - Added support for escaping the comment character
215 * in config files by a backslash
216 * - Added support for line continuation in config
218 * - Fixed a buffer overflow bug with long config lines
220 * Revision 1.4 2001/05/22 18:56:28 oes
223 * Revision 1.3 2001/05/20 01:21:20 jongfoster
224 * Version 2.9.4 checkin.
225 * - Merged popupfile and cookiefile, and added control over PCRS
226 * filtering, in new "permissionsfile".
227 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
228 * file error you now get a message box (in the Win32 GUI) rather
229 * than the program exiting with no explanation.
230 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
232 * - Removed tabs from "config"
233 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
234 * - Bumped up version number.
236 * Revision 1.2 2001/05/17 23:01:01 oes
237 * - Cleaned CRLF's from the sources and related files
239 * Revision 1.1.1.1 2001/05/15 13:58:59 oes
240 * Initial import of version 2.9.3 source tree
243 *********************************************************************/
250 #include <sys/types.h>
253 #include <sys/stat.h>
257 #if !defined(_WIN32) && !defined(__OS2__)
267 #include "miscutil.h"
270 #include "urlmatch.h"
272 const char loaders_h_rcs[] = LOADERS_H_VERSION;
275 * Currently active files.
276 * These are also entered in the main linked list of files.
280 static struct file_list *current_trustfile = NULL;
281 #endif /* def FEATURE_TRUST */
283 static struct file_list *current_re_filterfile = NULL;
287 /*********************************************************************
291 * Description : Basically a mark and sweep garbage collector, it is run
292 * (by the parent thread) every once in a while to reclaim memory.
294 * It uses a mark and sweep strategy:
295 * 1) mark all files as inactive
297 * 2) check with each client:
298 * if it is active, mark its files as active
299 * if it is inactive, free its resources
301 * 3) free the resources of all of the files that
302 * are still marked as inactive (and are obsolete).
304 * N.B. files that are not obsolete don't have an unloader defined.
310 *********************************************************************/
313 struct file_list *fl, *nfl;
314 struct client_state *csp, *ncsp;
316 /* clear all of the file's active flags */
317 for ( fl = files->next; NULL != fl; fl = fl->next )
322 for (csp = clients; csp && (NULL != (ncsp = csp->next)) ; csp = csp->next)
324 if (ncsp->flags & CSP_FLAG_ACTIVE)
326 /* mark this client's files as active */
329 * Always have a configuration file.
330 * (Also note the slightly non-standard extra
333 ncsp->config->config_file_list->active = 1;
335 if (ncsp->actions_list) /* actions files */
337 ncsp->actions_list->active = 1;
340 if (ncsp->rlist) /* pcrsjob files */
342 ncsp->rlist->active = 1;
346 if (ncsp->tlist) /* trust files */
348 ncsp->tlist->active = 1;
350 #endif /* def FEATURE_TRUST */
355 * this client is not active, release its resources
356 * and the ones of all inactive clients that might
360 while (!(ncsp->flags & CSP_FLAG_ACTIVE))
362 csp->next = ncsp->next;
364 freez(ncsp->ip_addr_str);
365 freez(ncsp->my_ip_addr_str);
366 freez(ncsp->my_hostname);
367 freez(ncsp->x_forwarded);
368 freez(ncsp->iob->buf);
370 free_http_request(ncsp->http);
372 destroy_list(ncsp->headers);
373 destroy_list(ncsp->cookie_list);
375 free_current_action(ncsp->action);
377 #ifdef FEATURE_STATISTICS
379 if (ncsp->flags & CSP_FLAG_REJECTED)
383 #endif /* def FEATURE_STATISTICS */
387 /* are there any more in sequence after it? */
388 if( (ncsp = csp->next) == NULL)
394 for (fl = files; fl && ((nfl = fl->next) != NULL) ; fl = fl->next)
396 if ( ( 0 == nfl->active ) && ( NULL != nfl->unloader ) )
398 fl->next = nfl->next;
400 (nfl->unloader)(nfl->f);
402 freez(nfl->filename);
411 /*********************************************************************
413 * Function : check_file_changed
415 * Description : Helper function to check if a file needs reloading.
416 * If "current" is still current, return it. Otherwise
417 * allocates a new (zeroed) "struct file_list", fills
418 * in the disk file name and timestamp, and returns it.
421 * 1 : current = The file_list currently being used - will
422 * be checked to see if it is out of date.
423 * May be NULL (which is treated as out of
425 * 2 : filename = Name of file to check.
426 * 3 : newfl = New file list. [Output only]
427 * This will be set to NULL, OR a struct
428 * file_list newly allocated on the
429 * heap, with the filename and lastmodified
430 * fields filled, and all others zeroed.
432 * Returns : If file unchanged: 0 (and sets newfl == NULL)
433 * If file changed: 1 and sets newfl != NULL
434 * On error: 1 and sets newfl == NULL
436 *********************************************************************/
437 int check_file_changed(const struct file_list * current,
438 const char * filename,
439 struct file_list ** newfl)
441 struct file_list *fs;
442 struct stat statbuf[1];
446 if (stat(filename, statbuf) < 0)
448 /* Error, probably file not found. */
453 && (current->lastmodified == statbuf->st_mtime)
454 && (0 == strcmp(current->filename, filename)))
459 fs = (struct file_list *)zalloc(sizeof(struct file_list));
462 /* Out of memory error */
466 fs->filename = strdup(filename);
467 fs->lastmodified = statbuf->st_mtime;
469 if (fs->filename == NULL)
471 /* Out of memory error */
480 /*********************************************************************
482 * Function : simple_read_line
484 * Description : Read a single line from a file and return it.
485 * This is basically a version of fgets() that malloc()s
486 * it's own line buffer. Note that the buffer will
487 * always be a multiple of BUFFER_SIZE bytes long.
488 * Therefore if you are going to keep the string for
489 * an extended period of time, you should probably
490 * strdup() it and free() the original, to save memory.
494 * 1 : dest = destination for newly malloc'd pointer to
495 * line data. Will be set to NULL on error.
496 * 2 : fp = File to read from
497 * 3 : newline = Standard for newlines in the file.
498 * Will be unchanged if it's value on input is not
500 * On output, may be changed from NEWLINE_UNKNOWN to
501 * actual convention in file.
503 * Returns : JB_ERR_OK on success
504 * JB_ERR_MEMORY on out-of-memory
505 * JB_ERR_FILE on EOF.
507 *********************************************************************/
508 jb_err simple_read_line(FILE *fp, char **dest, int *newline)
511 size_t buflen = BUFFER_SIZE;
515 int realnewline = NEWLINE_UNKNOWN;
517 if (NULL == (buf = malloc(buflen)))
519 return JB_ERR_MEMORY;
525 * Character codes. If you have a wierd compiler and the following are
526 * incorrect, you also need to fix NEWLINE() in loaders.h
528 #define CHAR_CR '\r' /* ASCII 13 */
529 #define CHAR_LF '\n' /* ASCII 10 */
549 else if (ch == CHAR_CR)
554 if (*newline == NEWLINE_UNKNOWN)
556 *newline = NEWLINE_DOS;
565 if (*newline == NEWLINE_UNKNOWN)
567 *newline = NEWLINE_MAC;
572 if (*newline == NEWLINE_UNKNOWN)
574 *newline = realnewline;
578 else if (ch == CHAR_LF)
582 if (*newline == NEWLINE_UNKNOWN)
584 *newline = NEWLINE_UNIX;
599 buflen += BUFFER_SIZE;
600 if (NULL == (p = realloc(buf, buflen)));
603 return JB_ERR_MEMORY;
612 /*********************************************************************
614 * Function : edit_read_line
616 * Description : Read a single non-empty line from a file and return
617 * it. Trims comments, leading and trailing whitespace
618 * and respects escaping of newline and comment char.
619 * Provides the line in 2 alternative forms: raw and
621 * - raw is the raw data read from the file. If the
622 * line is not modified, then this should be written
624 * - prefix is any comments and blank lines that were
625 * read from the file. If the line is modified, then
626 * this should be written out to the file followed
627 * by the modified data. (If this string is non-empty
628 * then it will have a newline at the end).
629 * - data is the actual data that will be parsed
630 * further by appropriate routines.
631 * On EOF, the 3 strings will all be set to NULL and
632 * 0 will be returned.
635 * 1 : fp = File to read from
636 * 2 : raw_out = destination for newly malloc'd pointer to
637 * raw line data. May be NULL if you don't want it.
638 * 3 : prefix_out = destination for newly malloc'd pointer to
639 * comments. May be NULL if you don't want it.
640 * 4 : data_out = destination for newly malloc'd pointer to
641 * line data with comments and leading/trailing spaces
642 * removed, and line continuation performed. May be
643 * NULL if you don't want it.
644 * 5 : newline = Standard for newlines in the file.
645 * On input, set to value to use or NEWLINE_UNKNOWN.
646 * On output, may be changed from NEWLINE_UNKNOWN to
647 * actual convention in file. May be NULL if you
649 * 6 : line_number = Line number in file. In "lines" as
650 * reported by a text editor, not lines containing data.
652 * Returns : JB_ERR_OK on success
653 * JB_ERR_MEMORY on out-of-memory
654 * JB_ERR_FILE on EOF.
656 *********************************************************************/
657 jb_err edit_read_line(FILE *fp,
662 unsigned long *line_number)
664 char *p; /* Temporary pointer */
665 char *linebuf; /* Line read from file */
666 char *linestart; /* Start of linebuf, usually first non-whitespace char */
667 int contflag = 0; /* Nonzero for line continuation - i.e. line ends '\' */
668 int is_empty = 1; /* Flag if not got any data yet */
669 char *raw = NULL; /* String to be stored in raw_out */
670 char *prefix = NULL; /* String to be stored in prefix_out */
671 char *data = NULL; /* String to be stored in data_out */
672 int scrapnewline; /* Used for (*newline) if newline==NULL */
673 jb_err rval = JB_ERR_OK;
676 assert(raw_out || data_out);
677 assert(newline == NULL
678 || *newline == NEWLINE_UNKNOWN
679 || *newline == NEWLINE_UNIX
680 || *newline == NEWLINE_DOS
681 || *newline == NEWLINE_MAC);
685 scrapnewline = NEWLINE_UNKNOWN;
686 newline = &scrapnewline;
689 /* Set output parameters to NULL */
703 /* Set string variables to new, empty strings. */
707 if ((raw = malloc(1)) == NULL)
709 return JB_ERR_MEMORY;
715 if ((prefix = malloc(1)) == NULL)
718 return JB_ERR_MEMORY;
724 if ((data = malloc(1)) == NULL)
728 return JB_ERR_MEMORY;
733 /* Main loop. Loop while we need more data & it's not EOF. */
735 while ( (contflag || is_empty)
736 && (JB_ERR_OK == (rval = simple_read_line(fp, &linebuf, newline))))
744 string_append(&raw,linebuf);
745 if (string_append(&raw,NEWLINE(*newline)))
750 return JB_ERR_MEMORY;
754 /* Line continuation? Trim escape and set flag. */
755 p = linebuf + strlen(linebuf) - 1;
756 contflag = ((*linebuf != '\0') && (*p == '\\'));
762 /* Trim leading spaces if we're at the start of the line */
766 /* Trim leading spaces */
767 while (*linestart && isspace((int)(unsigned char)*linestart))
773 /* Handle comment characters. */
775 while ((p = strchr(p, '#')) != NULL)
777 /* Found a comment char.. */
778 if ((p != linebuf) && (*(p-1) == '\\'))
780 /* ..and it's escaped, left-shift the line over the escape. */
782 while ((*q = *(q + 1)) != '\0')
786 /* Now scan from just after the "#". */
790 /* Real comment. Save it... */
793 /* Special case: Line only contains a comment, so all the
794 * previous whitespace is considered part of the comment.
795 * Undo the whitespace skipping, if any.
802 string_append(&prefix,p);
803 if (string_append(&prefix, NEWLINE(*newline)))
808 return JB_ERR_MEMORY;
812 /* ... and chop off the rest of the line */
815 } /* END while (there's a # character) */
817 /* Write to the buffer */
823 if (string_append(&data, linestart))
828 return JB_ERR_MEMORY;
834 } /* END while(we need more data) */
836 /* Handle simple_read_line() errors - ignore EOF */
837 if ((rval != JB_ERR_OK) && (rval != JB_ERR_FILE))
845 if (raw ? (*raw == '\0') : is_empty)
847 /* EOF and no data there. (Definition of "data" depends on whether
848 * the caller cares about "raw" or just "data").
859 /* Got at least some data */
861 /* Remove trailing whitespace */
874 *prefix_out = prefix;
893 /*********************************************************************
895 * Function : read_config_line
897 * Description : Read a single non-empty line from a file and return
898 * it. Trims comments, leading and trailing whitespace
899 * and respects escaping of newline and comment char.
902 * 1 : buf = Buffer to use.
903 * 2 : buflen = Size of buffer in bytes.
904 * 3 : fp = File to read from
905 * 4 : linenum = linenumber in file
907 * Returns : NULL on EOF or error
908 * Otherwise, returns buf.
910 *********************************************************************/
911 char *read_config_line(char *buf, size_t buflen, FILE *fp, unsigned long *linenum)
915 err = edit_read_line(fp, NULL, NULL, &buf2, NULL, linenum);
918 if (err == JB_ERR_MEMORY)
920 log_error(LOG_LEVEL_FATAL, "Out of memory loading a config file");
927 assert(strlen(buf2) + 1U < buflen);
928 strncpy(buf, buf2, buflen - 1);
930 buf[buflen - 1] = '\0';
937 /*********************************************************************
939 * Function : unload_trustfile
941 * Description : Unloads a trustfile.
944 * 1 : f = the data structure associated with the trustfile.
948 *********************************************************************/
949 static void unload_trustfile(void *f)
951 struct block_spec *b = (struct block_spec *)f;
952 if (b == NULL) return;
954 unload_trustfile(b->next); /* Stack is cheap, isn't it? */
956 free_url_spec(b->url);
963 /*********************************************************************
965 * Function : load_trustfile
967 * Description : Read and parse a trustfile and add to files list.
970 * 1 : csp = Current client state (buffers, headers, etc...)
972 * Returns : 0 => Ok, everything else is an error.
974 *********************************************************************/
975 int load_trustfile(struct client_state *csp)
979 struct block_spec *b, *bl;
980 struct url_spec **tl;
982 char buf[BUFFER_SIZE], *p, *q;
984 struct file_list *fs;
985 unsigned long linenum = 0;
987 if (!check_file_changed(current_trustfile, csp->config->trustfile, &fs))
989 /* No need to load */
992 csp->tlist = current_trustfile;
998 goto load_trustfile_error;
1001 fs->f = bl = (struct block_spec *)zalloc(sizeof(*bl));
1004 goto load_trustfile_error;
1007 if ((fp = fopen(csp->config->trustfile, "r")) == NULL)
1009 goto load_trustfile_error;
1012 tl = csp->config->trust_list;
1014 while (read_config_line(buf, sizeof(buf), fp, &linenum) != NULL)
1030 while ((*p++ = *q++) != '\0')
1036 /* skip blank lines */
1042 /* allocate a new node */
1043 if ((b = zalloc(sizeof(*b))) == NULL)
1046 goto load_trustfile_error;
1049 /* add it to the list */
1055 /* Save the URL pattern */
1056 if (create_url_spec(b->url, buf))
1059 goto load_trustfile_error;
1063 * save a pointer to URL's spec in the list of trusted URL's, too
1068 /* FIXME BUFFER OVERFLOW if >=64 entries */
1076 /* the old one is now obsolete */
1077 if (current_trustfile)
1079 current_trustfile->unloader = unload_trustfile;
1082 fs->next = files->next;
1084 current_trustfile = fs;
1093 load_trustfile_error:
1094 log_error(LOG_LEVEL_FATAL, "can't load trustfile '%s': %E",
1095 csp->config->trustfile);
1099 #endif /* def FEATURE_TRUST */
1102 /*********************************************************************
1104 * Function : unload_re_filterfile
1106 * Description : Unload the re_filter list by freeing all chained
1107 * re_filterfile specs and their data.
1110 * 1 : f = the data structure associated with the filterfile.
1114 *********************************************************************/
1115 static void unload_re_filterfile(void *f)
1117 struct re_filterfile_spec *a, *b = (struct re_filterfile_spec *)f;
1123 destroy_list(b->patterns);
1124 pcrs_free_joblist(b->joblist);
1126 freez(b->description);
1136 /*********************************************************************
1138 * Function : load_re_filterfile
1140 * Description : Load the re_filterfile.
1141 * Generate a chained list of re_filterfile_spec's from
1142 * the "FILTER: " blocks, compiling all their substitutions
1143 * into chained lists of pcrs_job structs.
1146 * 1 : csp = Current client state (buffers, headers, etc...)
1148 * Returns : 0 => Ok, everything else is an error.
1150 *********************************************************************/
1151 int load_re_filterfile(struct client_state *csp)
1155 struct re_filterfile_spec *new_bl, *bl = NULL;
1156 struct file_list *fs;
1158 char buf[BUFFER_SIZE];
1160 unsigned long linenum = 0;
1164 * No need to reload if unchanged
1166 if (!check_file_changed(current_re_filterfile, csp->config->re_filterfile, &fs))
1170 csp->rlist = current_re_filterfile;
1176 goto load_re_filterfile_error;
1180 * Open the file or fail
1182 if ((fp = fopen(csp->config->re_filterfile, "r")) == NULL)
1184 goto load_re_filterfile_error;
1190 while (read_config_line(buf, sizeof(buf), fp, &linenum) != NULL)
1193 * If this is the head of a new filter block, make it a
1194 * re_filterfile spec of its own and chain it to the list:
1196 if (strncmp(buf, "FILTER:", 7) == 0)
1198 new_bl = (struct re_filterfile_spec *)zalloc(sizeof(*bl));
1201 goto load_re_filterfile_error;
1204 new_bl->name = chomp(buf + 7);
1206 if (NULL != (new_bl->description = strchr(new_bl->name, ' ')))
1208 *new_bl->description++ = '\0';
1209 new_bl->description = strdup(chomp(new_bl->description));
1213 new_bl->description = strdup("No description available for this filter");
1216 new_bl->name = strdup(chomp(new_bl->name));
1219 * If this is the first filter block, chain it
1220 * to the file_list rather than its (nonexistant)
1233 log_error(LOG_LEVEL_RE_FILTER, "Reading in filter \"%s\" (\"%s\")", bl->name, bl->description);
1239 * Else, save the expression, make it a pcrs_job
1240 * and chain it into the current filter's joblist
1244 enlist(bl->patterns, buf);
1246 if ((dummy = pcrs_compile_command(buf, &error)) == NULL)
1248 log_error(LOG_LEVEL_RE_FILTER,
1249 "Adding re_filter job %s to filter %s failed with error %d.", buf, bl->name, error);
1254 dummy->next = bl->joblist;
1255 bl->joblist = dummy;
1256 log_error(LOG_LEVEL_RE_FILTER, "Adding re_filter job %s to filter %s succeeded.", buf, bl->name);
1261 log_error(LOG_LEVEL_ERROR, "Ignoring job %s outside filter block in %s, line %d", buf, csp->config->re_filterfile, linenum);
1268 * Schedule the now-obsolete old data for unloading
1270 if ( NULL != current_re_filterfile )
1272 current_re_filterfile->unloader = unload_re_filterfile;
1276 * Chain this file into the global list of loaded files
1278 fs->next = files->next;
1280 current_re_filterfile = fs;
1289 load_re_filterfile_error:
1290 log_error(LOG_LEVEL_FATAL, "can't load re_filterfile '%s': %E",
1291 csp->config->re_filterfile);
1297 /*********************************************************************
1299 * Function : add_loader
1301 * Description : Called from `load_config'. Called once for each input
1302 * file found in config.
1305 * 1 : loader = pointer to a function that can parse and load
1306 * the appropriate config file.
1307 * 2 : config = The configuration_spec to add the loader to.
1311 *********************************************************************/
1312 void add_loader(int (*loader)(struct client_state *),
1313 struct configuration_spec * config)
1317 for (i=0; i < NLOADERS; i++)
1319 if (config->loaders[i] == NULL)
1321 config->loaders[i] = loader;
1329 /*********************************************************************
1331 * Function : run_loader
1333 * Description : Called from `load_config' and `listen_loop'. This
1334 * function keeps the "csp" current with any file mods
1335 * since the last loop. If a file is unchanged, the
1336 * loader functions do NOT reload the file.
1339 * 1 : csp = Current client state (buffers, headers, etc...)
1340 * Must be non-null. Reads: "csp->config"
1341 * Writes: various data members.
1343 * Returns : 0 => Ok, everything else is an error.
1345 *********************************************************************/
1346 int run_loader(struct client_state *csp)
1351 for (i=0; i < NLOADERS; i++)
1353 if (csp->config->loaders[i] == NULL)
1357 ret |= (csp->config->loaders[i])(csp);