1 const char loaders_rcs[] = "$Id: loaders.c,v 1.42 2002/03/13 00:27:05 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.42 2002/03/13 00:27:05 jongfoster
41 * Revision 1.41 2002/03/12 01:42:50 oes
42 * Introduced modular filters
44 * Revision 1.40 2002/03/08 17:46:04 jongfoster
45 * Fixing int/size_t warnings
47 * Revision 1.39 2002/03/07 03:46:17 oes
48 * Fixed compiler warnings
50 * Revision 1.38 2002/03/06 22:54:35 jongfoster
51 * Automated function-comment nitpicking.
53 * Revision 1.37 2002/03/03 15:07:49 oes
54 * Re-enabled automatic config reloading
56 * Revision 1.36 2002/01/22 23:46:18 jongfoster
57 * Moving edit_read_line() and simple_read_line() to loaders.c, and
58 * extending them to support reading MS-DOS, Mac and UNIX style files
61 * Modifying read_config_line() (without changing it's prototype) to
62 * be a trivial wrapper for edit_read_line(). This means that we have
63 * one function to read a line and handle comments, which is common
64 * between the initialization code and the edit interface.
66 * Revision 1.35 2002/01/17 21:03:08 jongfoster
67 * Moving all our URL and URL pattern parsing code to urlmatch.c.
69 * Renaming free_url to free_url_spec, since it frees a struct url_spec.
71 * Revision 1.34 2001/12/30 14:07:32 steudten
72 * - Add signal handling (unix)
73 * - Add SIGHUP handler (unix)
74 * - Add creation of pidfile (unix)
75 * - Add action 'top' in rc file (RH)
76 * - Add entry 'SIGNALS' to manpage
77 * - Add exit message to logfile (unix)
79 * Revision 1.33 2001/11/13 00:16:38 jongfoster
80 * Replacing references to malloc.h with the standard stdlib.h
81 * (See ANSI or K&R 2nd Ed)
83 * Revision 1.32 2001/11/07 00:02:13 steudten
84 * Add line number in error output for lineparsing for
85 * actionsfile and configfile.
86 * Special handling for CLF added.
88 * Revision 1.31 2001/10/26 17:39:01 oes
89 * Removed csp->referrer
90 * Moved ijb_isspace and ijb_tolower to project.h
92 * Revision 1.30 2001/10/25 03:40:48 david__schmidt
93 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
94 * threads to call select() simultaneously. So, it's time to do a real, live,
95 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
96 * (native). Both versions will work, but using __OS2__ offers multi-threading.
98 * Revision 1.29 2001/10/23 21:38:53 jongfoster
99 * Adding error-checking to create_url_spec()
101 * Revision 1.28 2001/10/07 15:40:39 oes
102 * Replaced 6 boolean members of csp with one bitmap (csp->flags)
104 * Revision 1.27 2001/09/22 16:36:59 jongfoster
105 * Removing unused parameter fs from read_config_line()
107 * Revision 1.26 2001/09/22 14:05:22 jongfoster
108 * Bugfix: Multiple escaped "#" characters in a configuration
109 * file are now permitted.
110 * Also removing 3 unused headers.
112 * Revision 1.25 2001/09/13 22:44:03 jongfoster
113 * Adding {} to an if statement
115 * Revision 1.24 2001/07/30 22:08:36 jongfoster
116 * Tidying up #defines:
117 * - All feature #defines are now of the form FEATURE_xxx
118 * - Permanently turned off WIN_GUI_EDIT
119 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
121 * Revision 1.23 2001/07/20 15:51:54 oes
122 * Fixed indentation of prepocessor commands
124 * Revision 1.22 2001/07/20 15:16:17 haroon
125 * - per Guy's suggestion, added a while loop in sweep() to catch not just
126 * the last inactive CSP but all other consecutive inactive CSPs after that
129 * Revision 1.21 2001/07/18 17:26:24 oes
130 * Changed to conform to new pcrs interface
132 * Revision 1.20 2001/07/17 13:07:01 oes
133 * Fixed segv when last line in config files
134 * lacked a terminating (\r)\n
136 * Revision 1.19 2001/07/13 14:01:54 oes
137 * Removed all #ifdef PCRS
139 * Revision 1.18 2001/06/29 21:45:41 oes
140 * Indentation, CRLF->LF, Tab-> Space
142 * Revision 1.17 2001/06/29 13:31:51 oes
145 * Revision 1.16 2001/06/09 10:55:28 jongfoster
146 * Changing BUFSIZ ==> BUFFER_SIZE
148 * Revision 1.15 2001/06/07 23:14:14 jongfoster
149 * Removing ACL and forward file loaders - these
150 * files have been merged into the config file.
151 * Cosmetic: Moving unloader funcs next to their
152 * respective loader funcs
154 * Revision 1.14 2001/06/01 03:27:04 oes
155 * Fixed line continuation problem
157 * Revision 1.13 2001/05/31 21:28:49 jongfoster
158 * Removed all permissionsfile code - it's now called the actions
159 * file, and (almost) all the code is in actions.c
161 * Revision 1.12 2001/05/31 17:32:31 oes
163 * - Enhanced domain part globbing with infix and prefix asterisk
164 * matching and optional unanchored operation
166 * Revision 1.11 2001/05/29 23:25:24 oes
168 * - load_config_line() and load_permissions_file() now use chomp()
170 * Revision 1.10 2001/05/29 09:50:24 jongfoster
171 * Unified blocklist/imagelist/permissionslist.
172 * File format is still under discussion, but the internal changes
175 * Also modified interceptor behaviour:
176 * - We now intercept all URLs beginning with one of the following
177 * prefixes (and *only* these prefixes):
179 * * http://ijbswa.sf.net/config/
180 * * http://ijbswa.sourceforge.net/config/
181 * - New interceptors "home page" - go to http://i.j.b/ to see it.
182 * - Internal changes so that intercepted and fast redirect pages
183 * are not replaced with an image.
184 * - Interceptors now have the option to send a binary page direct
185 * to the client. (i.e. ijb-send-banner uses this)
186 * - Implemented show-url-info interceptor. (Which is why I needed
187 * the above interceptors changes - a typical URL is
188 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
189 * The previous mechanism would not have intercepted that, and
190 * if it had been intercepted then it then it would have replaced
193 * Revision 1.9 2001/05/26 17:12:07 jongfoster
194 * Fatal errors loading configuration files now give better error messages.
196 * Revision 1.8 2001/05/26 00:55:20 jongfoster
197 * Removing duplicated code. load_forwardfile() now uses create_url_spec()
199 * Revision 1.7 2001/05/26 00:28:36 jongfoster
200 * Automatic reloading of config file.
201 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
202 * Most of the global variables have been moved to a new
203 * struct configuration_spec, accessed through csp->config->globalname
204 * Most of the globals remaining are used by the Win32 GUI.
206 * Revision 1.6 2001/05/23 12:27:33 oes
208 * Fixed ugly indentation of my last changes
210 * Revision 1.5 2001/05/23 10:39:05 oes
211 * - Added support for escaping the comment character
212 * in config files by a backslash
213 * - Added support for line continuation in config
215 * - Fixed a buffer overflow bug with long config lines
217 * Revision 1.4 2001/05/22 18:56:28 oes
220 * Revision 1.3 2001/05/20 01:21:20 jongfoster
221 * Version 2.9.4 checkin.
222 * - Merged popupfile and cookiefile, and added control over PCRS
223 * filtering, in new "permissionsfile".
224 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
225 * file error you now get a message box (in the Win32 GUI) rather
226 * than the program exiting with no explanation.
227 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
229 * - Removed tabs from "config"
230 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
231 * - Bumped up version number.
233 * Revision 1.2 2001/05/17 23:01:01 oes
234 * - Cleaned CRLF's from the sources and related files
236 * Revision 1.1.1.1 2001/05/15 13:58:59 oes
237 * Initial import of version 2.9.3 source tree
240 *********************************************************************/
247 #include <sys/types.h>
250 #include <sys/stat.h>
254 #if !defined(_WIN32) && !defined(__OS2__)
264 #include "miscutil.h"
267 #include "urlmatch.h"
269 const char loaders_h_rcs[] = LOADERS_H_VERSION;
272 * Currently active files.
273 * These are also entered in the main linked list of files.
277 static struct file_list *current_trustfile = NULL;
278 #endif /* def FEATURE_TRUST */
280 static struct file_list *current_re_filterfile = NULL;
284 /*********************************************************************
288 * Description : Basically a mark and sweep garbage collector, it is run
289 * (by the parent thread) every once in a while to reclaim memory.
291 * It uses a mark and sweep strategy:
292 * 1) mark all files as inactive
294 * 2) check with each client:
295 * if it is active, mark its files as active
296 * if it is inactive, free its resources
298 * 3) free the resources of all of the files that
299 * are still marked as inactive (and are obsolete).
301 * N.B. files that are not obsolete don't have an unloader defined.
307 *********************************************************************/
310 struct file_list *fl, *nfl;
311 struct client_state *csp, *ncsp;
313 /* clear all of the file's active flags */
314 for ( fl = files->next; NULL != fl; fl = fl->next )
319 for (csp = clients; csp && (NULL != (ncsp = csp->next)) ; csp = csp->next)
321 if (ncsp->flags & CSP_FLAG_ACTIVE)
323 /* mark this client's files as active */
326 * Always have a configuration file.
327 * (Also note the slightly non-standard extra
330 ncsp->config->config_file_list->active = 1;
332 if (ncsp->actions_list) /* actions files */
334 ncsp->actions_list->active = 1;
337 if (ncsp->rlist) /* pcrsjob files */
339 ncsp->rlist->active = 1;
343 if (ncsp->tlist) /* trust files */
345 ncsp->tlist->active = 1;
347 #endif /* def FEATURE_TRUST */
352 * this client is not active, release its resources
353 * and the ones of all inactive clients that might
357 while (!(ncsp->flags & CSP_FLAG_ACTIVE))
359 csp->next = ncsp->next;
361 freez(ncsp->ip_addr_str);
362 freez(ncsp->my_ip_addr_str);
363 freez(ncsp->my_hostname);
364 freez(ncsp->x_forwarded);
365 freez(ncsp->iob->buf);
367 free_http_request(ncsp->http);
369 destroy_list(ncsp->headers);
370 destroy_list(ncsp->cookie_list);
372 free_current_action(ncsp->action);
374 #ifdef FEATURE_STATISTICS
376 if (ncsp->flags & CSP_FLAG_REJECTED)
380 #endif /* def FEATURE_STATISTICS */
384 /* are there any more in sequence after it? */
385 if( (ncsp = csp->next) == NULL)
391 for (fl = files; fl && ((nfl = fl->next) != NULL) ; fl = fl->next)
393 if ( ( 0 == nfl->active ) && ( NULL != nfl->unloader ) )
395 fl->next = nfl->next;
397 (nfl->unloader)(nfl->f);
399 freez(nfl->filename);
408 /*********************************************************************
410 * Function : check_file_changed
412 * Description : Helper function to check if a file needs reloading.
413 * If "current" is still current, return it. Otherwise
414 * allocates a new (zeroed) "struct file_list", fills
415 * in the disk file name and timestamp, and returns it.
418 * 1 : current = The file_list currently being used - will
419 * be checked to see if it is out of date.
420 * May be NULL (which is treated as out of
422 * 2 : filename = Name of file to check.
423 * 3 : newfl = New file list. [Output only]
424 * This will be set to NULL, OR a struct
425 * file_list newly allocated on the
426 * heap, with the filename and lastmodified
427 * fields filled, and all others zeroed.
429 * Returns : If file unchanged: 0 (and sets newfl == NULL)
430 * If file changed: 1 and sets newfl != NULL
431 * On error: 1 and sets newfl == NULL
433 *********************************************************************/
434 int check_file_changed(const struct file_list * current,
435 const char * filename,
436 struct file_list ** newfl)
438 struct file_list *fs;
439 struct stat statbuf[1];
443 if (stat(filename, statbuf) < 0)
445 /* Error, probably file not found. */
450 && (current->lastmodified == statbuf->st_mtime)
451 && (0 == strcmp(current->filename, filename)))
456 fs = (struct file_list *)zalloc(sizeof(struct file_list));
459 /* Out of memory error */
463 fs->filename = strdup(filename);
464 fs->lastmodified = statbuf->st_mtime;
466 if (fs->filename == NULL)
468 /* Out of memory error */
477 /*********************************************************************
479 * Function : simple_read_line
481 * Description : Read a single line from a file and return it.
482 * This is basically a version of fgets() that malloc()s
483 * it's own line buffer. Note that the buffer will
484 * always be a multiple of BUFFER_SIZE bytes long.
485 * Therefore if you are going to keep the string for
486 * an extended period of time, you should probably
487 * strdup() it and free() the original, to save memory.
491 * 1 : dest = destination for newly malloc'd pointer to
492 * line data. Will be set to NULL on error.
493 * 2 : fp = File to read from
494 * 3 : newline = Standard for newlines in the file.
495 * Will be unchanged if it's value on input is not
497 * On output, may be changed from NEWLINE_UNKNOWN to
498 * actual convention in file.
500 * Returns : JB_ERR_OK on success
501 * JB_ERR_MEMORY on out-of-memory
502 * JB_ERR_FILE on EOF.
504 *********************************************************************/
505 jb_err simple_read_line(FILE *fp, char **dest, int *newline)
508 size_t buflen = BUFFER_SIZE;
512 int realnewline = NEWLINE_UNKNOWN;
514 if (NULL == (buf = malloc(buflen)))
516 return JB_ERR_MEMORY;
522 * Character codes. If you have a wierd compiler and the following are
523 * incorrect, you also need to fix NEWLINE() in loaders.h
525 #define CHAR_CR '\r' /* ASCII 13 */
526 #define CHAR_LF '\n' /* ASCII 10 */
546 else if (ch == CHAR_CR)
551 if (*newline == NEWLINE_UNKNOWN)
553 *newline = NEWLINE_DOS;
562 if (*newline == NEWLINE_UNKNOWN)
564 *newline = NEWLINE_MAC;
569 if (*newline == NEWLINE_UNKNOWN)
571 *newline = realnewline;
575 else if (ch == CHAR_LF)
579 if (*newline == NEWLINE_UNKNOWN)
581 *newline = NEWLINE_UNIX;
596 buflen += BUFFER_SIZE;
597 if (NULL == (p = realloc(buf, buflen)));
600 return JB_ERR_MEMORY;
609 /*********************************************************************
611 * Function : edit_read_line
613 * Description : Read a single non-empty line from a file and return
614 * it. Trims comments, leading and trailing whitespace
615 * and respects escaping of newline and comment char.
616 * Provides the line in 2 alternative forms: raw and
618 * - raw is the raw data read from the file. If the
619 * line is not modified, then this should be written
621 * - prefix is any comments and blank lines that were
622 * read from the file. If the line is modified, then
623 * this should be written out to the file followed
624 * by the modified data. (If this string is non-empty
625 * then it will have a newline at the end).
626 * - data is the actual data that will be parsed
627 * further by appropriate routines.
628 * On EOF, the 3 strings will all be set to NULL and
629 * 0 will be returned.
632 * 1 : fp = File to read from
633 * 2 : raw_out = destination for newly malloc'd pointer to
634 * raw line data. May be NULL if you don't want it.
635 * 3 : prefix_out = destination for newly malloc'd pointer to
636 * comments. May be NULL if you don't want it.
637 * 4 : data_out = destination for newly malloc'd pointer to
638 * line data with comments and leading/trailing spaces
639 * removed, and line continuation performed. May be
640 * NULL if you don't want it.
641 * 5 : newline = Standard for newlines in the file.
642 * On input, set to value to use or NEWLINE_UNKNOWN.
643 * On output, may be changed from NEWLINE_UNKNOWN to
644 * actual convention in file. May be NULL if you
646 * 6 : line_number = Line number in file. In "lines" as
647 * reported by a text editor, not lines containing data.
649 * Returns : JB_ERR_OK on success
650 * JB_ERR_MEMORY on out-of-memory
651 * JB_ERR_FILE on EOF.
653 *********************************************************************/
654 jb_err edit_read_line(FILE *fp,
659 unsigned long *line_number)
661 char *p; /* Temporary pointer */
662 char *linebuf; /* Line read from file */
663 char *linestart; /* Start of linebuf, usually first non-whitespace char */
664 int contflag = 0; /* Nonzero for line continuation - i.e. line ends '\' */
665 int is_empty = 1; /* Flag if not got any data yet */
666 char *raw = NULL; /* String to be stored in raw_out */
667 char *prefix = NULL; /* String to be stored in prefix_out */
668 char *data = NULL; /* String to be stored in data_out */
669 int scrapnewline; /* Used for (*newline) if newline==NULL */
670 jb_err rval = JB_ERR_OK;
673 assert(raw_out || data_out);
674 assert(newline == NULL
675 || *newline == NEWLINE_UNKNOWN
676 || *newline == NEWLINE_UNIX
677 || *newline == NEWLINE_DOS
678 || *newline == NEWLINE_MAC);
682 scrapnewline = NEWLINE_UNKNOWN;
683 newline = &scrapnewline;
686 /* Set output parameters to NULL */
700 /* Set string variables to new, empty strings. */
704 if ((raw = malloc(1)) == NULL)
706 return JB_ERR_MEMORY;
712 if ((prefix = malloc(1)) == NULL)
715 return JB_ERR_MEMORY;
721 if ((data = malloc(1)) == NULL)
725 return JB_ERR_MEMORY;
730 /* Main loop. Loop while we need more data & it's not EOF. */
732 while ( (contflag || is_empty)
733 && (JB_ERR_OK == (rval = simple_read_line(fp, &linebuf, newline))))
741 string_append(&raw,linebuf);
742 if (string_append(&raw,NEWLINE(*newline)))
747 return JB_ERR_MEMORY;
751 /* Line continuation? Trim escape and set flag. */
752 p = linebuf + strlen(linebuf) - 1;
753 contflag = ((*linebuf != '\0') && (*p == '\\'));
759 /* Trim leading spaces if we're at the start of the line */
763 /* Trim leading spaces */
764 while (*linestart && isspace((int)(unsigned char)*linestart))
770 /* Handle comment characters. */
772 while ((p = strchr(p, '#')) != NULL)
774 /* Found a comment char.. */
775 if ((p != linebuf) && (*(p-1) == '\\'))
777 /* ..and it's escaped, left-shift the line over the escape. */
779 while ((*q = *(q + 1)) != '\0')
783 /* Now scan from just after the "#". */
787 /* Real comment. Save it... */
790 /* Special case: Line only contains a comment, so all the
791 * previous whitespace is considered part of the comment.
792 * Undo the whitespace skipping, if any.
799 string_append(&prefix,p);
800 if (string_append(&prefix, NEWLINE(*newline)))
805 return JB_ERR_MEMORY;
809 /* ... and chop off the rest of the line */
812 } /* END while (there's a # character) */
814 /* Write to the buffer */
820 if (string_append(&data, linestart))
825 return JB_ERR_MEMORY;
831 } /* END while(we need more data) */
833 /* Handle simple_read_line() errors - ignore EOF */
834 if ((rval != JB_ERR_OK) && (rval != JB_ERR_FILE))
842 if (raw ? (*raw == '\0') : is_empty)
844 /* EOF and no data there. (Definition of "data" depends on whether
845 * the caller cares about "raw" or just "data").
856 /* Got at least some data */
858 /* Remove trailing whitespace */
871 *prefix_out = prefix;
890 /*********************************************************************
892 * Function : read_config_line
894 * Description : Read a single non-empty line from a file and return
895 * it. Trims comments, leading and trailing whitespace
896 * and respects escaping of newline and comment char.
899 * 1 : buf = Buffer to use.
900 * 2 : buflen = Size of buffer in bytes.
901 * 3 : fp = File to read from
902 * 4 : linenum = linenumber in file
904 * Returns : NULL on EOF or error
905 * Otherwise, returns buf.
907 *********************************************************************/
908 char *read_config_line(char *buf, size_t buflen, FILE *fp, unsigned long *linenum)
912 err = edit_read_line(fp, NULL, NULL, &buf2, NULL, linenum);
915 if (err == JB_ERR_MEMORY)
917 log_error(LOG_LEVEL_FATAL, "Out of memory loading a config file");
924 assert(strlen(buf2) + 1U < buflen);
925 strncpy(buf, buf2, buflen - 1);
927 buf[buflen - 1] = '\0';
934 /*********************************************************************
936 * Function : unload_trustfile
938 * Description : Unloads a trustfile.
941 * 1 : f = the data structure associated with the trustfile.
945 *********************************************************************/
946 static void unload_trustfile(void *f)
948 struct block_spec *b = (struct block_spec *)f;
949 if (b == NULL) return;
951 unload_trustfile(b->next); /* Stack is cheap, isn't it? */
953 free_url_spec(b->url);
960 /*********************************************************************
962 * Function : load_trustfile
964 * Description : Read and parse a trustfile and add to files list.
967 * 1 : csp = Current client state (buffers, headers, etc...)
969 * Returns : 0 => Ok, everything else is an error.
971 *********************************************************************/
972 int load_trustfile(struct client_state *csp)
976 struct block_spec *b, *bl;
977 struct url_spec **tl;
979 char buf[BUFFER_SIZE], *p, *q;
981 struct file_list *fs;
982 unsigned long linenum = 0;
984 if (!check_file_changed(current_trustfile, csp->config->trustfile, &fs))
986 /* No need to load */
989 csp->tlist = current_trustfile;
995 goto load_trustfile_error;
998 fs->f = bl = (struct block_spec *)zalloc(sizeof(*bl));
1001 goto load_trustfile_error;
1004 if ((fp = fopen(csp->config->trustfile, "r")) == NULL)
1006 goto load_trustfile_error;
1009 tl = csp->config->trust_list;
1011 while (read_config_line(buf, sizeof(buf), fp, &linenum) != NULL)
1027 while ((*p++ = *q++) != '\0')
1033 /* skip blank lines */
1039 /* allocate a new node */
1040 if ((b = zalloc(sizeof(*b))) == NULL)
1043 goto load_trustfile_error;
1046 /* add it to the list */
1052 /* Save the URL pattern */
1053 if (create_url_spec(b->url, buf))
1056 goto load_trustfile_error;
1060 * save a pointer to URL's spec in the list of trusted URL's, too
1065 /* FIXME BUFFER OVERFLOW if >=64 entries */
1073 /* the old one is now obsolete */
1074 if (current_trustfile)
1076 current_trustfile->unloader = unload_trustfile;
1079 fs->next = files->next;
1081 current_trustfile = fs;
1090 load_trustfile_error:
1091 log_error(LOG_LEVEL_FATAL, "can't load trustfile '%s': %E",
1092 csp->config->trustfile);
1096 #endif /* def FEATURE_TRUST */
1099 /*********************************************************************
1101 * Function : unload_re_filterfile
1103 * Description : Unload the re_filter list by freeing all chained
1104 * re_filterfile specs and their data.
1107 * 1 : f = the data structure associated with the filterfile.
1111 *********************************************************************/
1112 static void unload_re_filterfile(void *f)
1114 struct re_filterfile_spec *a, *b = (struct re_filterfile_spec *)f;
1120 destroy_list(b->patterns);
1121 pcrs_free_joblist(b->joblist);
1123 freez(b->description);
1133 /*********************************************************************
1135 * Function : load_re_filterfile
1137 * Description : Load the re_filterfile.
1138 * Generate a chained list of re_filterfile_spec's from
1139 * the "FILTER: " blocks, compiling all their substitutions
1140 * into chained lists of pcrs_job structs.
1143 * 1 : csp = Current client state (buffers, headers, etc...)
1145 * Returns : 0 => Ok, everything else is an error.
1147 *********************************************************************/
1148 int load_re_filterfile(struct client_state *csp)
1152 struct re_filterfile_spec *new_bl, *bl = NULL;
1153 struct file_list *fs;
1155 char buf[BUFFER_SIZE];
1157 unsigned long linenum = 0;
1161 * No need to reload if unchanged
1163 if (!check_file_changed(current_re_filterfile, csp->config->re_filterfile, &fs))
1167 csp->rlist = current_re_filterfile;
1173 goto load_re_filterfile_error;
1177 * Open the file or fail
1179 if ((fp = fopen(csp->config->re_filterfile, "r")) == NULL)
1181 goto load_re_filterfile_error;
1187 while (read_config_line(buf, sizeof(buf), fp, &linenum) != NULL)
1190 * If this is the head of a new filter block, make it a
1191 * re_filterfile spec of its own and chain it to the list:
1193 if (strncmp(buf, "FILTER:", 7) == 0)
1195 new_bl = (struct re_filterfile_spec *)zalloc(sizeof(*bl));
1198 goto load_re_filterfile_error;
1201 new_bl->name = chomp(buf + 7);
1203 if (NULL != (new_bl->description = strchr(new_bl->name, ' ')))
1205 *new_bl->description++ = '\0';
1206 new_bl->description = strdup(chomp(new_bl->description));
1210 new_bl->description = strdup("No description available for this filter");
1213 new_bl->name = strdup(chomp(new_bl->name));
1216 * If this is the first filter block, chain it
1217 * to the file_list rather than its (nonexistant)
1230 log_error(LOG_LEVEL_RE_FILTER, "Reading in filter \"%s\" (\"%s\")", bl->name, bl->description);
1236 * Else, save the expression, make it a pcrs_job
1237 * and chain it into the current filter's joblist
1241 enlist(bl->patterns, buf);
1243 if ((dummy = pcrs_compile_command(buf, &error)) == NULL)
1245 log_error(LOG_LEVEL_RE_FILTER,
1246 "Adding re_filter job %s to filter %s failed with error %d.", buf, bl->name, error);
1251 dummy->next = bl->joblist;
1252 bl->joblist = dummy;
1253 log_error(LOG_LEVEL_RE_FILTER, "Adding re_filter job %s to filter %s succeeded.", buf, bl->name);
1258 log_error(LOG_LEVEL_ERROR, "Ignoring job %s outside filter block in %s, line %d", buf, csp->config->re_filterfile, linenum);
1265 * Schedule the now-obsolete old data for unloading
1267 if ( NULL != current_re_filterfile )
1269 current_re_filterfile->unloader = unload_re_filterfile;
1273 * Chain this file into the global list of loaded files
1275 fs->next = files->next;
1277 current_re_filterfile = fs;
1286 load_re_filterfile_error:
1287 log_error(LOG_LEVEL_FATAL, "can't load re_filterfile '%s': %E",
1288 csp->config->re_filterfile);
1294 /*********************************************************************
1296 * Function : add_loader
1298 * Description : Called from `load_config'. Called once for each input
1299 * file found in config.
1302 * 1 : loader = pointer to a function that can parse and load
1303 * the appropriate config file.
1304 * 2 : config = The configuration_spec to add the loader to.
1308 *********************************************************************/
1309 void add_loader(int (*loader)(struct client_state *),
1310 struct configuration_spec * config)
1314 for (i=0; i < NLOADERS; i++)
1316 if (config->loaders[i] == NULL)
1318 config->loaders[i] = loader;
1326 /*********************************************************************
1328 * Function : run_loader
1330 * Description : Called from `load_config' and `listen_loop'. This
1331 * function keeps the "csp" current with any file mods
1332 * since the last loop. If a file is unchanged, the
1333 * loader functions do NOT reload the file.
1336 * 1 : csp = Current client state (buffers, headers, etc...)
1337 * Must be non-null. Reads: "csp->config"
1338 * Writes: various data members.
1340 * Returns : 0 => Ok, everything else is an error.
1342 *********************************************************************/
1343 int run_loader(struct client_state *csp)
1348 for (i=0; i < NLOADERS; i++)
1350 if (csp->config->loaders[i] == NULL)
1354 ret |= (csp->config->loaders[i])(csp);