1 const char loaders_rcs[] = "$Id: loaders.c,v 1.44 2002/03/16 21:51:00 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.44 2002/03/16 21:51:00 jongfoster
41 * Revision 1.43 2002/03/16 20:28:34 oes
42 * Added descriptions to the filters so users will know what they select in the cgi editor
44 * Revision 1.42 2002/03/13 00:27:05 jongfoster
47 * Revision 1.41 2002/03/12 01:42:50 oes
48 * Introduced modular filters
50 * Revision 1.40 2002/03/08 17:46:04 jongfoster
51 * Fixing int/size_t warnings
53 * Revision 1.39 2002/03/07 03:46:17 oes
54 * Fixed compiler warnings
56 * Revision 1.38 2002/03/06 22:54:35 jongfoster
57 * Automated function-comment nitpicking.
59 * Revision 1.37 2002/03/03 15:07:49 oes
60 * Re-enabled automatic config reloading
62 * Revision 1.36 2002/01/22 23:46:18 jongfoster
63 * Moving edit_read_line() and simple_read_line() to loaders.c, and
64 * extending them to support reading MS-DOS, Mac and UNIX style files
67 * Modifying read_config_line() (without changing it's prototype) to
68 * be a trivial wrapper for edit_read_line(). This means that we have
69 * one function to read a line and handle comments, which is common
70 * between the initialization code and the edit interface.
72 * Revision 1.35 2002/01/17 21:03:08 jongfoster
73 * Moving all our URL and URL pattern parsing code to urlmatch.c.
75 * Renaming free_url to free_url_spec, since it frees a struct url_spec.
77 * Revision 1.34 2001/12/30 14:07:32 steudten
78 * - Add signal handling (unix)
79 * - Add SIGHUP handler (unix)
80 * - Add creation of pidfile (unix)
81 * - Add action 'top' in rc file (RH)
82 * - Add entry 'SIGNALS' to manpage
83 * - Add exit message to logfile (unix)
85 * Revision 1.33 2001/11/13 00:16:38 jongfoster
86 * Replacing references to malloc.h with the standard stdlib.h
87 * (See ANSI or K&R 2nd Ed)
89 * Revision 1.32 2001/11/07 00:02:13 steudten
90 * Add line number in error output for lineparsing for
91 * actionsfile and configfile.
92 * Special handling for CLF added.
94 * Revision 1.31 2001/10/26 17:39:01 oes
95 * Removed csp->referrer
96 * Moved ijb_isspace and ijb_tolower to project.h
98 * Revision 1.30 2001/10/25 03:40:48 david__schmidt
99 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
100 * threads to call select() simultaneously. So, it's time to do a real, live,
101 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
102 * (native). Both versions will work, but using __OS2__ offers multi-threading.
104 * Revision 1.29 2001/10/23 21:38:53 jongfoster
105 * Adding error-checking to create_url_spec()
107 * Revision 1.28 2001/10/07 15:40:39 oes
108 * Replaced 6 boolean members of csp with one bitmap (csp->flags)
110 * Revision 1.27 2001/09/22 16:36:59 jongfoster
111 * Removing unused parameter fs from read_config_line()
113 * Revision 1.26 2001/09/22 14:05:22 jongfoster
114 * Bugfix: Multiple escaped "#" characters in a configuration
115 * file are now permitted.
116 * Also removing 3 unused headers.
118 * Revision 1.25 2001/09/13 22:44:03 jongfoster
119 * Adding {} to an if statement
121 * Revision 1.24 2001/07/30 22:08:36 jongfoster
122 * Tidying up #defines:
123 * - All feature #defines are now of the form FEATURE_xxx
124 * - Permanently turned off WIN_GUI_EDIT
125 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
127 * Revision 1.23 2001/07/20 15:51:54 oes
128 * Fixed indentation of prepocessor commands
130 * Revision 1.22 2001/07/20 15:16:17 haroon
131 * - per Guy's suggestion, added a while loop in sweep() to catch not just
132 * the last inactive CSP but all other consecutive inactive CSPs after that
135 * Revision 1.21 2001/07/18 17:26:24 oes
136 * Changed to conform to new pcrs interface
138 * Revision 1.20 2001/07/17 13:07:01 oes
139 * Fixed segv when last line in config files
140 * lacked a terminating (\r)\n
142 * Revision 1.19 2001/07/13 14:01:54 oes
143 * Removed all #ifdef PCRS
145 * Revision 1.18 2001/06/29 21:45:41 oes
146 * Indentation, CRLF->LF, Tab-> Space
148 * Revision 1.17 2001/06/29 13:31:51 oes
151 * Revision 1.16 2001/06/09 10:55:28 jongfoster
152 * Changing BUFSIZ ==> BUFFER_SIZE
154 * Revision 1.15 2001/06/07 23:14:14 jongfoster
155 * Removing ACL and forward file loaders - these
156 * files have been merged into the config file.
157 * Cosmetic: Moving unloader funcs next to their
158 * respective loader funcs
160 * Revision 1.14 2001/06/01 03:27:04 oes
161 * Fixed line continuation problem
163 * Revision 1.13 2001/05/31 21:28:49 jongfoster
164 * Removed all permissionsfile code - it's now called the actions
165 * file, and (almost) all the code is in actions.c
167 * Revision 1.12 2001/05/31 17:32:31 oes
169 * - Enhanced domain part globbing with infix and prefix asterisk
170 * matching and optional unanchored operation
172 * Revision 1.11 2001/05/29 23:25:24 oes
174 * - load_config_line() and load_permissions_file() now use chomp()
176 * Revision 1.10 2001/05/29 09:50:24 jongfoster
177 * Unified blocklist/imagelist/permissionslist.
178 * File format is still under discussion, but the internal changes
181 * Also modified interceptor behaviour:
182 * - We now intercept all URLs beginning with one of the following
183 * prefixes (and *only* these prefixes):
185 * * http://ijbswa.sf.net/config/
186 * * http://ijbswa.sourceforge.net/config/
187 * - New interceptors "home page" - go to http://i.j.b/ to see it.
188 * - Internal changes so that intercepted and fast redirect pages
189 * are not replaced with an image.
190 * - Interceptors now have the option to send a binary page direct
191 * to the client. (i.e. ijb-send-banner uses this)
192 * - Implemented show-url-info interceptor. (Which is why I needed
193 * the above interceptors changes - a typical URL is
194 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
195 * The previous mechanism would not have intercepted that, and
196 * if it had been intercepted then it then it would have replaced
199 * Revision 1.9 2001/05/26 17:12:07 jongfoster
200 * Fatal errors loading configuration files now give better error messages.
202 * Revision 1.8 2001/05/26 00:55:20 jongfoster
203 * Removing duplicated code. load_forwardfile() now uses create_url_spec()
205 * Revision 1.7 2001/05/26 00:28:36 jongfoster
206 * Automatic reloading of config file.
207 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
208 * Most of the global variables have been moved to a new
209 * struct configuration_spec, accessed through csp->config->globalname
210 * Most of the globals remaining are used by the Win32 GUI.
212 * Revision 1.6 2001/05/23 12:27:33 oes
214 * Fixed ugly indentation of my last changes
216 * Revision 1.5 2001/05/23 10:39:05 oes
217 * - Added support for escaping the comment character
218 * in config files by a backslash
219 * - Added support for line continuation in config
221 * - Fixed a buffer overflow bug with long config lines
223 * Revision 1.4 2001/05/22 18:56:28 oes
226 * Revision 1.3 2001/05/20 01:21:20 jongfoster
227 * Version 2.9.4 checkin.
228 * - Merged popupfile and cookiefile, and added control over PCRS
229 * filtering, in new "permissionsfile".
230 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
231 * file error you now get a message box (in the Win32 GUI) rather
232 * than the program exiting with no explanation.
233 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
235 * - Removed tabs from "config"
236 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
237 * - Bumped up version number.
239 * Revision 1.2 2001/05/17 23:01:01 oes
240 * - Cleaned CRLF's from the sources and related files
242 * Revision 1.1.1.1 2001/05/15 13:58:59 oes
243 * Initial import of version 2.9.3 source tree
246 *********************************************************************/
253 #include <sys/types.h>
256 #include <sys/stat.h>
260 #if !defined(_WIN32) && !defined(__OS2__)
270 #include "miscutil.h"
273 #include "urlmatch.h"
275 const char loaders_h_rcs[] = LOADERS_H_VERSION;
278 * Currently active files.
279 * These are also entered in the main linked list of files.
283 static struct file_list *current_trustfile = NULL;
284 #endif /* def FEATURE_TRUST */
286 static struct file_list *current_re_filterfile = NULL;
290 /*********************************************************************
294 * Description : Basically a mark and sweep garbage collector, it is run
295 * (by the parent thread) every once in a while to reclaim memory.
297 * It uses a mark and sweep strategy:
298 * 1) mark all files as inactive
300 * 2) check with each client:
301 * if it is active, mark its files as active
302 * if it is inactive, free its resources
304 * 3) free the resources of all of the files that
305 * are still marked as inactive (and are obsolete).
307 * N.B. files that are not obsolete don't have an unloader defined.
313 *********************************************************************/
316 struct file_list *fl, *nfl;
317 struct client_state *csp, *ncsp;
319 /* clear all of the file's active flags */
320 for ( fl = files->next; NULL != fl; fl = fl->next )
325 for (csp = clients; csp && (NULL != (ncsp = csp->next)) ; csp = csp->next)
327 if (ncsp->flags & CSP_FLAG_ACTIVE)
329 /* mark this client's files as active */
332 * Always have a configuration file.
333 * (Also note the slightly non-standard extra
336 ncsp->config->config_file_list->active = 1;
338 if (ncsp->actions_list) /* actions files */
340 ncsp->actions_list->active = 1;
343 if (ncsp->rlist) /* pcrsjob files */
345 ncsp->rlist->active = 1;
349 if (ncsp->tlist) /* trust files */
351 ncsp->tlist->active = 1;
353 #endif /* def FEATURE_TRUST */
358 * this client is not active, release its resources
359 * and the ones of all inactive clients that might
363 while (!(ncsp->flags & CSP_FLAG_ACTIVE))
365 csp->next = ncsp->next;
367 freez(ncsp->ip_addr_str);
368 freez(ncsp->my_ip_addr_str);
369 freez(ncsp->my_hostname);
370 freez(ncsp->x_forwarded);
371 freez(ncsp->iob->buf);
373 free_http_request(ncsp->http);
375 destroy_list(ncsp->headers);
376 destroy_list(ncsp->cookie_list);
378 free_current_action(ncsp->action);
380 #ifdef FEATURE_STATISTICS
382 if (ncsp->flags & CSP_FLAG_REJECTED)
386 #endif /* def FEATURE_STATISTICS */
390 /* are there any more in sequence after it? */
391 if( (ncsp = csp->next) == NULL)
397 for (fl = files; fl && ((nfl = fl->next) != NULL) ; fl = fl->next)
399 if ( ( 0 == nfl->active ) && ( NULL != nfl->unloader ) )
401 fl->next = nfl->next;
403 (nfl->unloader)(nfl->f);
405 freez(nfl->filename);
414 /*********************************************************************
416 * Function : check_file_changed
418 * Description : Helper function to check if a file needs reloading.
419 * If "current" is still current, return it. Otherwise
420 * allocates a new (zeroed) "struct file_list", fills
421 * in the disk file name and timestamp, and returns it.
424 * 1 : current = The file_list currently being used - will
425 * be checked to see if it is out of date.
426 * May be NULL (which is treated as out of
428 * 2 : filename = Name of file to check.
429 * 3 : newfl = New file list. [Output only]
430 * This will be set to NULL, OR a struct
431 * file_list newly allocated on the
432 * heap, with the filename and lastmodified
433 * fields filled, and all others zeroed.
435 * Returns : If file unchanged: 0 (and sets newfl == NULL)
436 * If file changed: 1 and sets newfl != NULL
437 * On error: 1 and sets newfl == NULL
439 *********************************************************************/
440 int check_file_changed(const struct file_list * current,
441 const char * filename,
442 struct file_list ** newfl)
444 struct file_list *fs;
445 struct stat statbuf[1];
449 if (stat(filename, statbuf) < 0)
451 /* Error, probably file not found. */
456 && (current->lastmodified == statbuf->st_mtime)
457 && (0 == strcmp(current->filename, filename)))
462 fs = (struct file_list *)zalloc(sizeof(struct file_list));
465 /* Out of memory error */
469 fs->filename = strdup(filename);
470 fs->lastmodified = statbuf->st_mtime;
472 if (fs->filename == NULL)
474 /* Out of memory error */
483 /*********************************************************************
485 * Function : simple_read_line
487 * Description : Read a single line from a file and return it.
488 * This is basically a version of fgets() that malloc()s
489 * it's own line buffer. Note that the buffer will
490 * always be a multiple of BUFFER_SIZE bytes long.
491 * Therefore if you are going to keep the string for
492 * an extended period of time, you should probably
493 * strdup() it and free() the original, to save memory.
497 * 1 : dest = destination for newly malloc'd pointer to
498 * line data. Will be set to NULL on error.
499 * 2 : fp = File to read from
500 * 3 : newline = Standard for newlines in the file.
501 * Will be unchanged if it's value on input is not
503 * On output, may be changed from NEWLINE_UNKNOWN to
504 * actual convention in file.
506 * Returns : JB_ERR_OK on success
507 * JB_ERR_MEMORY on out-of-memory
508 * JB_ERR_FILE on EOF.
510 *********************************************************************/
511 jb_err simple_read_line(FILE *fp, char **dest, int *newline)
514 size_t buflen = BUFFER_SIZE;
518 int realnewline = NEWLINE_UNKNOWN;
520 if (NULL == (buf = malloc(buflen)))
522 return JB_ERR_MEMORY;
528 * Character codes. If you have a wierd compiler and the following are
529 * incorrect, you also need to fix NEWLINE() in loaders.h
531 #define CHAR_CR '\r' /* ASCII 13 */
532 #define CHAR_LF '\n' /* ASCII 10 */
552 else if (ch == CHAR_CR)
557 if (*newline == NEWLINE_UNKNOWN)
559 *newline = NEWLINE_DOS;
568 if (*newline == NEWLINE_UNKNOWN)
570 *newline = NEWLINE_MAC;
575 if (*newline == NEWLINE_UNKNOWN)
577 *newline = realnewline;
581 else if (ch == CHAR_LF)
585 if (*newline == NEWLINE_UNKNOWN)
587 *newline = NEWLINE_UNIX;
602 buflen += BUFFER_SIZE;
603 if (NULL == (p = realloc(buf, buflen)));
606 return JB_ERR_MEMORY;
615 /*********************************************************************
617 * Function : edit_read_line
619 * Description : Read a single non-empty line from a file and return
620 * it. Trims comments, leading and trailing whitespace
621 * and respects escaping of newline and comment char.
622 * Provides the line in 2 alternative forms: raw and
624 * - raw is the raw data read from the file. If the
625 * line is not modified, then this should be written
627 * - prefix is any comments and blank lines that were
628 * read from the file. If the line is modified, then
629 * this should be written out to the file followed
630 * by the modified data. (If this string is non-empty
631 * then it will have a newline at the end).
632 * - data is the actual data that will be parsed
633 * further by appropriate routines.
634 * On EOF, the 3 strings will all be set to NULL and
635 * 0 will be returned.
638 * 1 : fp = File to read from
639 * 2 : raw_out = destination for newly malloc'd pointer to
640 * raw line data. May be NULL if you don't want it.
641 * 3 : prefix_out = destination for newly malloc'd pointer to
642 * comments. May be NULL if you don't want it.
643 * 4 : data_out = destination for newly malloc'd pointer to
644 * line data with comments and leading/trailing spaces
645 * removed, and line continuation performed. May be
646 * NULL if you don't want it.
647 * 5 : newline = Standard for newlines in the file.
648 * On input, set to value to use or NEWLINE_UNKNOWN.
649 * On output, may be changed from NEWLINE_UNKNOWN to
650 * actual convention in file. May be NULL if you
652 * 6 : line_number = Line number in file. In "lines" as
653 * reported by a text editor, not lines containing data.
655 * Returns : JB_ERR_OK on success
656 * JB_ERR_MEMORY on out-of-memory
657 * JB_ERR_FILE on EOF.
659 *********************************************************************/
660 jb_err edit_read_line(FILE *fp,
665 unsigned long *line_number)
667 char *p; /* Temporary pointer */
668 char *linebuf; /* Line read from file */
669 char *linestart; /* Start of linebuf, usually first non-whitespace char */
670 int contflag = 0; /* Nonzero for line continuation - i.e. line ends '\' */
671 int is_empty = 1; /* Flag if not got any data yet */
672 char *raw = NULL; /* String to be stored in raw_out */
673 char *prefix = NULL; /* String to be stored in prefix_out */
674 char *data = NULL; /* String to be stored in data_out */
675 int scrapnewline; /* Used for (*newline) if newline==NULL */
676 jb_err rval = JB_ERR_OK;
679 assert(raw_out || data_out);
680 assert(newline == NULL
681 || *newline == NEWLINE_UNKNOWN
682 || *newline == NEWLINE_UNIX
683 || *newline == NEWLINE_DOS
684 || *newline == NEWLINE_MAC);
688 scrapnewline = NEWLINE_UNKNOWN;
689 newline = &scrapnewline;
692 /* Set output parameters to NULL */
706 /* Set string variables to new, empty strings. */
710 if ((raw = malloc(1)) == NULL)
712 return JB_ERR_MEMORY;
718 if ((prefix = malloc(1)) == NULL)
721 return JB_ERR_MEMORY;
727 if ((data = malloc(1)) == NULL)
731 return JB_ERR_MEMORY;
736 /* Main loop. Loop while we need more data & it's not EOF. */
738 while ( (contflag || is_empty)
739 && (JB_ERR_OK == (rval = simple_read_line(fp, &linebuf, newline))))
747 string_append(&raw,linebuf);
748 if (string_append(&raw,NEWLINE(*newline)))
753 return JB_ERR_MEMORY;
757 /* Line continuation? Trim escape and set flag. */
758 p = linebuf + strlen(linebuf) - 1;
759 contflag = ((*linebuf != '\0') && (*p == '\\'));
765 /* Trim leading spaces if we're at the start of the line */
769 /* Trim leading spaces */
770 while (*linestart && isspace((int)(unsigned char)*linestart))
776 /* Handle comment characters. */
778 while ((p = strchr(p, '#')) != NULL)
780 /* Found a comment char.. */
781 if ((p != linebuf) && (*(p-1) == '\\'))
783 /* ..and it's escaped, left-shift the line over the escape. */
785 while ((*q = *(q + 1)) != '\0')
789 /* Now scan from just after the "#". */
793 /* Real comment. Save it... */
796 /* Special case: Line only contains a comment, so all the
797 * previous whitespace is considered part of the comment.
798 * Undo the whitespace skipping, if any.
805 string_append(&prefix,p);
806 if (string_append(&prefix, NEWLINE(*newline)))
811 return JB_ERR_MEMORY;
815 /* ... and chop off the rest of the line */
818 } /* END while (there's a # character) */
820 /* Write to the buffer */
826 if (string_append(&data, linestart))
831 return JB_ERR_MEMORY;
837 } /* END while(we need more data) */
839 /* Handle simple_read_line() errors - ignore EOF */
840 if ((rval != JB_ERR_OK) && (rval != JB_ERR_FILE))
848 if (raw ? (*raw == '\0') : is_empty)
850 /* EOF and no data there. (Definition of "data" depends on whether
851 * the caller cares about "raw" or just "data").
862 /* Got at least some data */
864 /* Remove trailing whitespace */
877 *prefix_out = prefix;
896 /*********************************************************************
898 * Function : read_config_line
900 * Description : Read a single non-empty line from a file and return
901 * it. Trims comments, leading and trailing whitespace
902 * and respects escaping of newline and comment char.
905 * 1 : buf = Buffer to use.
906 * 2 : buflen = Size of buffer in bytes.
907 * 3 : fp = File to read from
908 * 4 : linenum = linenumber in file
910 * Returns : NULL on EOF or error
911 * Otherwise, returns buf.
913 *********************************************************************/
914 char *read_config_line(char *buf, size_t buflen, FILE *fp, unsigned long *linenum)
918 err = edit_read_line(fp, NULL, NULL, &buf2, NULL, linenum);
921 if (err == JB_ERR_MEMORY)
923 log_error(LOG_LEVEL_FATAL, "Out of memory loading a config file");
930 assert(strlen(buf2) + 1U < buflen);
931 strncpy(buf, buf2, buflen - 1);
933 buf[buflen - 1] = '\0';
940 /*********************************************************************
942 * Function : unload_trustfile
944 * Description : Unloads a trustfile.
947 * 1 : f = the data structure associated with the trustfile.
951 *********************************************************************/
952 static void unload_trustfile(void *f)
954 struct block_spec *cur = (struct block_spec *)f;
955 struct block_spec *next;
961 free_url_spec(cur->url);
970 #ifdef FEATURE_GRACEFUL_TERMINATION
971 /*********************************************************************
973 * Function : unload_current_trust_file
975 * Description : Unloads current trust file - reset to state at
976 * beginning of program.
982 *********************************************************************/
983 void unload_current_trust_file(void)
985 if (current_trustfile)
987 current_trustfile->unloader = unload_trustfile;
988 current_trustfile = NULL;
991 #endif /* FEATURE_GRACEFUL_TERMINATION */
994 /*********************************************************************
996 * Function : load_trustfile
998 * Description : Read and parse a trustfile and add to files list.
1001 * 1 : csp = Current client state (buffers, headers, etc...)
1003 * Returns : 0 => Ok, everything else is an error.
1005 *********************************************************************/
1006 int load_trustfile(struct client_state *csp)
1010 struct block_spec *b, *bl;
1011 struct url_spec **tl;
1013 char buf[BUFFER_SIZE], *p, *q;
1014 int reject, trusted;
1015 struct file_list *fs;
1016 unsigned long linenum = 0;
1018 if (!check_file_changed(current_trustfile, csp->config->trustfile, &fs))
1020 /* No need to load */
1023 csp->tlist = current_trustfile;
1029 goto load_trustfile_error;
1032 fs->f = bl = (struct block_spec *)zalloc(sizeof(*bl));
1035 goto load_trustfile_error;
1038 if ((fp = fopen(csp->config->trustfile, "r")) == NULL)
1040 goto load_trustfile_error;
1043 tl = csp->config->trust_list;
1045 while (read_config_line(buf, sizeof(buf), fp, &linenum) != NULL)
1061 while ((*p++ = *q++) != '\0')
1067 /* skip blank lines */
1073 /* allocate a new node */
1074 if ((b = zalloc(sizeof(*b))) == NULL)
1077 goto load_trustfile_error;
1080 /* add it to the list */
1086 /* Save the URL pattern */
1087 if (create_url_spec(b->url, buf))
1090 goto load_trustfile_error;
1094 * save a pointer to URL's spec in the list of trusted URL's, too
1099 /* FIXME BUFFER OVERFLOW if >=64 entries */
1107 /* the old one is now obsolete */
1108 if (current_trustfile)
1110 current_trustfile->unloader = unload_trustfile;
1113 fs->next = files->next;
1115 current_trustfile = fs;
1124 load_trustfile_error:
1125 log_error(LOG_LEVEL_FATAL, "can't load trustfile '%s': %E",
1126 csp->config->trustfile);
1130 #endif /* def FEATURE_TRUST */
1133 /*********************************************************************
1135 * Function : unload_re_filterfile
1137 * Description : Unload the re_filter list by freeing all chained
1138 * re_filterfile specs and their data.
1141 * 1 : f = the data structure associated with the filterfile.
1145 *********************************************************************/
1146 static void unload_re_filterfile(void *f)
1148 struct re_filterfile_spec *a, *b = (struct re_filterfile_spec *)f;
1154 destroy_list(b->patterns);
1155 pcrs_free_joblist(b->joblist);
1157 freez(b->description);
1167 #ifdef FEATURE_GRACEFUL_TERMINATION
1168 /*********************************************************************
1170 * Function : unload_current_re_filterfile
1172 * Description : Unloads current re_filter file - reset to state at
1173 * beginning of program.
1179 *********************************************************************/
1180 void unload_current_re_filterfile(void)
1182 if (current_re_filterfile)
1184 current_re_filterfile->unloader = unload_re_filterfile;
1185 current_re_filterfile = NULL;
1191 /*********************************************************************
1193 * Function : load_re_filterfile
1195 * Description : Load the re_filterfile.
1196 * Generate a chained list of re_filterfile_spec's from
1197 * the "FILTER: " blocks, compiling all their substitutions
1198 * into chained lists of pcrs_job structs.
1201 * 1 : csp = Current client state (buffers, headers, etc...)
1203 * Returns : 0 => Ok, everything else is an error.
1205 *********************************************************************/
1206 int load_re_filterfile(struct client_state *csp)
1210 struct re_filterfile_spec *new_bl, *bl = NULL;
1211 struct file_list *fs;
1213 char buf[BUFFER_SIZE];
1215 unsigned long linenum = 0;
1219 * No need to reload if unchanged
1221 if (!check_file_changed(current_re_filterfile, csp->config->re_filterfile, &fs))
1225 csp->rlist = current_re_filterfile;
1231 goto load_re_filterfile_error;
1235 * Open the file or fail
1237 if ((fp = fopen(csp->config->re_filterfile, "r")) == NULL)
1239 goto load_re_filterfile_error;
1245 while (read_config_line(buf, sizeof(buf), fp, &linenum) != NULL)
1248 * If this is the head of a new filter block, make it a
1249 * re_filterfile spec of its own and chain it to the list:
1251 if (strncmp(buf, "FILTER:", 7) == 0)
1253 new_bl = (struct re_filterfile_spec *)zalloc(sizeof(*bl));
1256 goto load_re_filterfile_error;
1259 new_bl->name = chomp(buf + 7);
1261 if (NULL != (new_bl->description = strchr(new_bl->name, ' ')))
1263 *new_bl->description++ = '\0';
1264 new_bl->description = strdup(chomp(new_bl->description));
1268 new_bl->description = strdup("No description available for this filter");
1271 new_bl->name = strdup(chomp(new_bl->name));
1274 * If this is the first filter block, chain it
1275 * to the file_list rather than its (nonexistant)
1288 log_error(LOG_LEVEL_RE_FILTER, "Reading in filter \"%s\" (\"%s\")", bl->name, bl->description);
1294 * Else, save the expression, make it a pcrs_job
1295 * and chain it into the current filter's joblist
1299 enlist(bl->patterns, buf);
1301 if ((dummy = pcrs_compile_command(buf, &error)) == NULL)
1303 log_error(LOG_LEVEL_RE_FILTER,
1304 "Adding re_filter job %s to filter %s failed with error %d.", buf, bl->name, error);
1309 dummy->next = bl->joblist;
1310 bl->joblist = dummy;
1311 log_error(LOG_LEVEL_RE_FILTER, "Adding re_filter job %s to filter %s succeeded.", buf, bl->name);
1316 log_error(LOG_LEVEL_ERROR, "Ignoring job %s outside filter block in %s, line %d", buf, csp->config->re_filterfile, linenum);
1323 * Schedule the now-obsolete old data for unloading
1325 if ( NULL != current_re_filterfile )
1327 current_re_filterfile->unloader = unload_re_filterfile;
1331 * Chain this file into the global list of loaded files
1333 fs->next = files->next;
1335 current_re_filterfile = fs;
1344 load_re_filterfile_error:
1345 log_error(LOG_LEVEL_FATAL, "can't load re_filterfile '%s': %E",
1346 csp->config->re_filterfile);
1352 /*********************************************************************
1354 * Function : add_loader
1356 * Description : Called from `load_config'. Called once for each input
1357 * file found in config.
1360 * 1 : loader = pointer to a function that can parse and load
1361 * the appropriate config file.
1362 * 2 : config = The configuration_spec to add the loader to.
1366 *********************************************************************/
1367 void add_loader(int (*loader)(struct client_state *),
1368 struct configuration_spec * config)
1372 for (i=0; i < NLOADERS; i++)
1374 if (config->loaders[i] == NULL)
1376 config->loaders[i] = loader;
1384 /*********************************************************************
1386 * Function : run_loader
1388 * Description : Called from `load_config' and `listen_loop'. This
1389 * function keeps the "csp" current with any file mods
1390 * since the last loop. If a file is unchanged, the
1391 * loader functions do NOT reload the file.
1394 * 1 : csp = Current client state (buffers, headers, etc...)
1395 * Must be non-null. Reads: "csp->config"
1396 * Writes: various data members.
1398 * Returns : 0 => Ok, everything else is an error.
1400 *********************************************************************/
1401 int run_loader(struct client_state *csp)
1406 for (i=0; i < NLOADERS; i++)
1408 if (csp->config->loaders[i] == NULL)
1412 ret |= (csp->config->loaders[i])(csp);