1 const char loaders_rcs[] = "$Id: loaders.c,v 1.50 2002/04/24 02:12:16 oes Exp $";
2 /*********************************************************************
4 * File : $Source: /cvsroot/ijbswa/current/Attic/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 * Privoxy team. http://www.privoxy.org/
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.50 2002/04/24 02:12:16 oes
39 * Jon's multiple AF patch: Sweep now takes care of all AFs
41 * Revision 1.49 2002/04/19 16:53:25 jongfoster
42 * Optimize away a function call by using an equivalent macro
44 * Revision 1.48 2002/04/05 00:56:09 gliptak
45 * Correcting typo to clean up on realloc failure
47 * Revision 1.47 2002/03/26 22:29:55 swa
48 * we have a new homepage!
50 * Revision 1.46 2002/03/24 13:25:43 swa
51 * name change related issues
53 * Revision 1.45 2002/03/16 23:54:06 jongfoster
54 * Adding graceful termination feature, to help look for memory leaks.
55 * If you enable this (which, by design, has to be done by hand
56 * editing config.h) and then go to http://i.j.b/die, then the program
57 * will exit cleanly after the *next* request. It should free all the
58 * memory that was used.
60 * Revision 1.44 2002/03/16 21:51:00 jongfoster
63 * Revision 1.43 2002/03/16 20:28:34 oes
64 * Added descriptions to the filters so users will know what they select in the cgi editor
66 * Revision 1.42 2002/03/13 00:27:05 jongfoster
69 * Revision 1.41 2002/03/12 01:42:50 oes
70 * Introduced modular filters
72 * Revision 1.40 2002/03/08 17:46:04 jongfoster
73 * Fixing int/size_t warnings
75 * Revision 1.39 2002/03/07 03:46:17 oes
76 * Fixed compiler warnings
78 * Revision 1.38 2002/03/06 22:54:35 jongfoster
79 * Automated function-comment nitpicking.
81 * Revision 1.37 2002/03/03 15:07:49 oes
82 * Re-enabled automatic config reloading
84 * Revision 1.36 2002/01/22 23:46:18 jongfoster
85 * Moving edit_read_line() and simple_read_line() to loaders.c, and
86 * extending them to support reading MS-DOS, Mac and UNIX style files
89 * Modifying read_config_line() (without changing it's prototype) to
90 * be a trivial wrapper for edit_read_line(). This means that we have
91 * one function to read a line and handle comments, which is common
92 * between the initialization code and the edit interface.
94 * Revision 1.35 2002/01/17 21:03:08 jongfoster
95 * Moving all our URL and URL pattern parsing code to urlmatch.c.
97 * Renaming free_url to free_url_spec, since it frees a struct url_spec.
99 * Revision 1.34 2001/12/30 14:07:32 steudten
100 * - Add signal handling (unix)
101 * - Add SIGHUP handler (unix)
102 * - Add creation of pidfile (unix)
103 * - Add action 'top' in rc file (RH)
104 * - Add entry 'SIGNALS' to manpage
105 * - Add exit message to logfile (unix)
107 * Revision 1.33 2001/11/13 00:16:38 jongfoster
108 * Replacing references to malloc.h with the standard stdlib.h
109 * (See ANSI or K&R 2nd Ed)
111 * Revision 1.32 2001/11/07 00:02:13 steudten
112 * Add line number in error output for lineparsing for
113 * actionsfile and configfile.
114 * Special handling for CLF added.
116 * Revision 1.31 2001/10/26 17:39:01 oes
117 * Removed csp->referrer
118 * Moved ijb_isspace and ijb_tolower to project.h
120 * Revision 1.30 2001/10/25 03:40:48 david__schmidt
121 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
122 * threads to call select() simultaneously. So, it's time to do a real, live,
123 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
124 * (native). Both versions will work, but using __OS2__ offers multi-threading.
126 * Revision 1.29 2001/10/23 21:38:53 jongfoster
127 * Adding error-checking to create_url_spec()
129 * Revision 1.28 2001/10/07 15:40:39 oes
130 * Replaced 6 boolean members of csp with one bitmap (csp->flags)
132 * Revision 1.27 2001/09/22 16:36:59 jongfoster
133 * Removing unused parameter fs from read_config_line()
135 * Revision 1.26 2001/09/22 14:05:22 jongfoster
136 * Bugfix: Multiple escaped "#" characters in a configuration
137 * file are now permitted.
138 * Also removing 3 unused headers.
140 * Revision 1.25 2001/09/13 22:44:03 jongfoster
141 * Adding {} to an if statement
143 * Revision 1.24 2001/07/30 22:08:36 jongfoster
144 * Tidying up #defines:
145 * - All feature #defines are now of the form FEATURE_xxx
146 * - Permanently turned off WIN_GUI_EDIT
147 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
149 * Revision 1.23 2001/07/20 15:51:54 oes
150 * Fixed indentation of prepocessor commands
152 * Revision 1.22 2001/07/20 15:16:17 haroon
153 * - per Guy's suggestion, added a while loop in sweep() to catch not just
154 * the last inactive CSP but all other consecutive inactive CSPs after that
157 * Revision 1.21 2001/07/18 17:26:24 oes
158 * Changed to conform to new pcrs interface
160 * Revision 1.20 2001/07/17 13:07:01 oes
161 * Fixed segv when last line in config files
162 * lacked a terminating (\r)\n
164 * Revision 1.19 2001/07/13 14:01:54 oes
165 * Removed all #ifdef PCRS
167 * Revision 1.18 2001/06/29 21:45:41 oes
168 * Indentation, CRLF->LF, Tab-> Space
170 * Revision 1.17 2001/06/29 13:31:51 oes
173 * Revision 1.16 2001/06/09 10:55:28 jongfoster
174 * Changing BUFSIZ ==> BUFFER_SIZE
176 * Revision 1.15 2001/06/07 23:14:14 jongfoster
177 * Removing ACL and forward file loaders - these
178 * files have been merged into the config file.
179 * Cosmetic: Moving unloader funcs next to their
180 * respective loader funcs
182 * Revision 1.14 2001/06/01 03:27:04 oes
183 * Fixed line continuation problem
185 * Revision 1.13 2001/05/31 21:28:49 jongfoster
186 * Removed all permissionsfile code - it's now called the actions
187 * file, and (almost) all the code is in actions.c
189 * Revision 1.12 2001/05/31 17:32:31 oes
191 * - Enhanced domain part globbing with infix and prefix asterisk
192 * matching and optional unanchored operation
194 * Revision 1.11 2001/05/29 23:25:24 oes
196 * - load_config_line() and load_permissions_file() now use chomp()
198 * Revision 1.10 2001/05/29 09:50:24 jongfoster
199 * Unified blocklist/imagelist/permissionslist.
200 * File format is still under discussion, but the internal changes
203 * Also modified interceptor behaviour:
204 * - We now intercept all URLs beginning with one of the following
205 * prefixes (and *only* these prefixes):
207 * * http://ijbswa.sf.net/config/
208 * * http://ijbswa.sourceforge.net/config/
209 * - New interceptors "home page" - go to http://i.j.b/ to see it.
210 * - Internal changes so that intercepted and fast redirect pages
211 * are not replaced with an image.
212 * - Interceptors now have the option to send a binary page direct
213 * to the client. (i.e. ijb-send-banner uses this)
214 * - Implemented show-url-info interceptor. (Which is why I needed
215 * the above interceptors changes - a typical URL is
216 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
217 * The previous mechanism would not have intercepted that, and
218 * if it had been intercepted then it then it would have replaced
221 * Revision 1.9 2001/05/26 17:12:07 jongfoster
222 * Fatal errors loading configuration files now give better error messages.
224 * Revision 1.8 2001/05/26 00:55:20 jongfoster
225 * Removing duplicated code. load_forwardfile() now uses create_url_spec()
227 * Revision 1.7 2001/05/26 00:28:36 jongfoster
228 * Automatic reloading of config file.
229 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
230 * Most of the global variables have been moved to a new
231 * struct configuration_spec, accessed through csp->config->globalname
232 * Most of the globals remaining are used by the Win32 GUI.
234 * Revision 1.6 2001/05/23 12:27:33 oes
236 * Fixed ugly indentation of my last changes
238 * Revision 1.5 2001/05/23 10:39:05 oes
239 * - Added support for escaping the comment character
240 * in config files by a backslash
241 * - Added support for line continuation in config
243 * - Fixed a buffer overflow bug with long config lines
245 * Revision 1.4 2001/05/22 18:56:28 oes
248 * Revision 1.3 2001/05/20 01:21:20 jongfoster
249 * Version 2.9.4 checkin.
250 * - Merged popupfile and cookiefile, and added control over PCRS
251 * filtering, in new "permissionsfile".
252 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
253 * file error you now get a message box (in the Win32 GUI) rather
254 * than the program exiting with no explanation.
255 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
257 * - Removed tabs from "config"
258 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
259 * - Bumped up version number.
261 * Revision 1.2 2001/05/17 23:01:01 oes
262 * - Cleaned CRLF's from the sources and related files
264 * Revision 1.1.1.1 2001/05/15 13:58:59 oes
265 * Initial import of version 2.9.3 source tree
268 *********************************************************************/
275 #include <sys/types.h>
278 #include <sys/stat.h>
282 #if !defined(_WIN32) && !defined(__OS2__)
292 #include "miscutil.h"
295 #include "urlmatch.h"
297 const char loaders_h_rcs[] = LOADERS_H_VERSION;
300 * Currently active files.
301 * These are also entered in the main linked list of files.
305 static struct file_list *current_trustfile = NULL;
306 #endif /* def FEATURE_TRUST */
308 static struct file_list *current_re_filterfile = NULL;
312 /*********************************************************************
316 * Description : Basically a mark and sweep garbage collector, it is run
317 * (by the parent thread) every once in a while to reclaim memory.
319 * It uses a mark and sweep strategy:
320 * 1) mark all files as inactive
322 * 2) check with each client:
323 * if it is active, mark its files as active
324 * if it is inactive, free its resources
326 * 3) free the resources of all of the files that
327 * are still marked as inactive (and are obsolete).
329 * N.B. files that are not obsolete don't have an unloader defined.
335 *********************************************************************/
338 struct file_list *fl, *nfl;
339 struct client_state *csp, *ncsp;
342 /* clear all of the file's active flags */
343 for ( fl = files->next; NULL != fl; fl = fl->next )
348 for (csp = clients; csp && (NULL != (ncsp = csp->next)) ; csp = csp->next)
350 if (ncsp->flags & CSP_FLAG_ACTIVE)
352 /* mark this client's files as active */
355 * Always have a configuration file.
356 * (Also note the slightly non-standard extra
359 ncsp->config->config_file_list->active = 1;
361 for (i = 0; i < MAX_ACTION_FILES; i++)
363 if (ncsp->actions_list[i]) /* actions files */
365 ncsp->actions_list[i]->active = 1;
369 if (ncsp->rlist) /* pcrsjob files */
371 ncsp->rlist->active = 1;
375 if (ncsp->tlist) /* trust files */
377 ncsp->tlist->active = 1;
379 #endif /* def FEATURE_TRUST */
384 * this client is not active, release its resources
385 * and the ones of all inactive clients that might
389 while (!(ncsp->flags & CSP_FLAG_ACTIVE))
391 csp->next = ncsp->next;
393 freez(ncsp->ip_addr_str);
394 freez(ncsp->my_ip_addr_str);
395 freez(ncsp->my_hostname);
396 freez(ncsp->x_forwarded);
397 freez(ncsp->iob->buf);
399 free_http_request(ncsp->http);
401 destroy_list(ncsp->headers);
402 destroy_list(ncsp->cookie_list);
404 free_current_action(ncsp->action);
406 #ifdef FEATURE_STATISTICS
408 if (ncsp->flags & CSP_FLAG_REJECTED)
412 #endif /* def FEATURE_STATISTICS */
416 /* are there any more in sequence after it? */
417 if( (ncsp = csp->next) == NULL)
423 for (fl = files; fl && ((nfl = fl->next) != NULL) ; fl = fl->next)
425 if ( ( 0 == nfl->active ) && ( NULL != nfl->unloader ) )
427 fl->next = nfl->next;
429 (nfl->unloader)(nfl->f);
431 freez(nfl->filename);
440 /*********************************************************************
442 * Function : check_file_changed
444 * Description : Helper function to check if a file needs reloading.
445 * If "current" is still current, return it. Otherwise
446 * allocates a new (zeroed) "struct file_list", fills
447 * in the disk file name and timestamp, and returns it.
450 * 1 : current = The file_list currently being used - will
451 * be checked to see if it is out of date.
452 * May be NULL (which is treated as out of
454 * 2 : filename = Name of file to check.
455 * 3 : newfl = New file list. [Output only]
456 * This will be set to NULL, OR a struct
457 * file_list newly allocated on the
458 * heap, with the filename and lastmodified
459 * fields filled, and all others zeroed.
461 * Returns : If file unchanged: 0 (and sets newfl == NULL)
462 * If file changed: 1 and sets newfl != NULL
463 * On error: 1 and sets newfl == NULL
465 *********************************************************************/
466 int check_file_changed(const struct file_list * current,
467 const char * filename,
468 struct file_list ** newfl)
470 struct file_list *fs;
471 struct stat statbuf[1];
475 if (stat(filename, statbuf) < 0)
477 /* Error, probably file not found. */
482 && (current->lastmodified == statbuf->st_mtime)
483 && (0 == strcmp(current->filename, filename)))
488 fs = (struct file_list *)zalloc(sizeof(struct file_list));
491 /* Out of memory error */
495 fs->filename = strdup(filename);
496 fs->lastmodified = statbuf->st_mtime;
498 if (fs->filename == NULL)
500 /* Out of memory error */
509 /*********************************************************************
511 * Function : simple_read_line
513 * Description : Read a single line from a file and return it.
514 * This is basically a version of fgets() that malloc()s
515 * it's own line buffer. Note that the buffer will
516 * always be a multiple of BUFFER_SIZE bytes long.
517 * Therefore if you are going to keep the string for
518 * an extended period of time, you should probably
519 * strdup() it and free() the original, to save memory.
523 * 1 : dest = destination for newly malloc'd pointer to
524 * line data. Will be set to NULL on error.
525 * 2 : fp = File to read from
526 * 3 : newline = Standard for newlines in the file.
527 * Will be unchanged if it's value on input is not
529 * On output, may be changed from NEWLINE_UNKNOWN to
530 * actual convention in file.
532 * Returns : JB_ERR_OK on success
533 * JB_ERR_MEMORY on out-of-memory
534 * JB_ERR_FILE on EOF.
536 *********************************************************************/
537 jb_err simple_read_line(FILE *fp, char **dest, int *newline)
540 size_t buflen = BUFFER_SIZE;
544 int realnewline = NEWLINE_UNKNOWN;
546 if (NULL == (buf = malloc(buflen)))
548 return JB_ERR_MEMORY;
554 * Character codes. If you have a wierd compiler and the following are
555 * incorrect, you also need to fix NEWLINE() in loaders.h
557 #define CHAR_CR '\r' /* ASCII 13 */
558 #define CHAR_LF '\n' /* ASCII 10 */
578 else if (ch == CHAR_CR)
583 if (*newline == NEWLINE_UNKNOWN)
585 *newline = NEWLINE_DOS;
594 if (*newline == NEWLINE_UNKNOWN)
596 *newline = NEWLINE_MAC;
601 if (*newline == NEWLINE_UNKNOWN)
603 *newline = realnewline;
607 else if (ch == CHAR_LF)
611 if (*newline == NEWLINE_UNKNOWN)
613 *newline = NEWLINE_UNIX;
628 buflen += BUFFER_SIZE;
629 if (NULL == (p = realloc(buf, buflen)))
632 return JB_ERR_MEMORY;
641 /*********************************************************************
643 * Function : edit_read_line
645 * Description : Read a single non-empty line from a file and return
646 * it. Trims comments, leading and trailing whitespace
647 * and respects escaping of newline and comment char.
648 * Provides the line in 2 alternative forms: raw and
650 * - raw is the raw data read from the file. If the
651 * line is not modified, then this should be written
653 * - prefix is any comments and blank lines that were
654 * read from the file. If the line is modified, then
655 * this should be written out to the file followed
656 * by the modified data. (If this string is non-empty
657 * then it will have a newline at the end).
658 * - data is the actual data that will be parsed
659 * further by appropriate routines.
660 * On EOF, the 3 strings will all be set to NULL and
661 * 0 will be returned.
664 * 1 : fp = File to read from
665 * 2 : raw_out = destination for newly malloc'd pointer to
666 * raw line data. May be NULL if you don't want it.
667 * 3 : prefix_out = destination for newly malloc'd pointer to
668 * comments. May be NULL if you don't want it.
669 * 4 : data_out = destination for newly malloc'd pointer to
670 * line data with comments and leading/trailing spaces
671 * removed, and line continuation performed. May be
672 * NULL if you don't want it.
673 * 5 : newline = Standard for newlines in the file.
674 * On input, set to value to use or NEWLINE_UNKNOWN.
675 * On output, may be changed from NEWLINE_UNKNOWN to
676 * actual convention in file. May be NULL if you
678 * 6 : line_number = Line number in file. In "lines" as
679 * reported by a text editor, not lines containing data.
681 * Returns : JB_ERR_OK on success
682 * JB_ERR_MEMORY on out-of-memory
683 * JB_ERR_FILE on EOF.
685 *********************************************************************/
686 jb_err edit_read_line(FILE *fp,
691 unsigned long *line_number)
693 char *p; /* Temporary pointer */
694 char *linebuf; /* Line read from file */
695 char *linestart; /* Start of linebuf, usually first non-whitespace char */
696 int contflag = 0; /* Nonzero for line continuation - i.e. line ends '\' */
697 int is_empty = 1; /* Flag if not got any data yet */
698 char *raw = NULL; /* String to be stored in raw_out */
699 char *prefix = NULL; /* String to be stored in prefix_out */
700 char *data = NULL; /* String to be stored in data_out */
701 int scrapnewline; /* Used for (*newline) if newline==NULL */
702 jb_err rval = JB_ERR_OK;
705 assert(raw_out || data_out);
706 assert(newline == NULL
707 || *newline == NEWLINE_UNKNOWN
708 || *newline == NEWLINE_UNIX
709 || *newline == NEWLINE_DOS
710 || *newline == NEWLINE_MAC);
714 scrapnewline = NEWLINE_UNKNOWN;
715 newline = &scrapnewline;
718 /* Set output parameters to NULL */
732 /* Set string variables to new, empty strings. */
736 if ((raw = malloc(1)) == NULL)
738 return JB_ERR_MEMORY;
744 if ((prefix = malloc(1)) == NULL)
747 return JB_ERR_MEMORY;
753 if ((data = malloc(1)) == NULL)
757 return JB_ERR_MEMORY;
762 /* Main loop. Loop while we need more data & it's not EOF. */
764 while ( (contflag || is_empty)
765 && (JB_ERR_OK == (rval = simple_read_line(fp, &linebuf, newline))))
773 string_append(&raw,linebuf);
774 if (string_append(&raw,NEWLINE(*newline)))
779 return JB_ERR_MEMORY;
783 /* Line continuation? Trim escape and set flag. */
784 p = linebuf + strlen(linebuf) - 1;
785 contflag = ((*linebuf != '\0') && (*p == '\\'));
791 /* Trim leading spaces if we're at the start of the line */
795 /* Trim leading spaces */
796 while (*linestart && isspace((int)(unsigned char)*linestart))
802 /* Handle comment characters. */
804 while ((p = strchr(p, '#')) != NULL)
806 /* Found a comment char.. */
807 if ((p != linebuf) && (*(p-1) == '\\'))
809 /* ..and it's escaped, left-shift the line over the escape. */
811 while ((*q = *(q + 1)) != '\0')
815 /* Now scan from just after the "#". */
819 /* Real comment. Save it... */
822 /* Special case: Line only contains a comment, so all the
823 * previous whitespace is considered part of the comment.
824 * Undo the whitespace skipping, if any.
831 string_append(&prefix,p);
832 if (string_append(&prefix, NEWLINE(*newline)))
837 return JB_ERR_MEMORY;
841 /* ... and chop off the rest of the line */
844 } /* END while (there's a # character) */
846 /* Write to the buffer */
852 if (string_append(&data, linestart))
857 return JB_ERR_MEMORY;
863 } /* END while(we need more data) */
865 /* Handle simple_read_line() errors - ignore EOF */
866 if ((rval != JB_ERR_OK) && (rval != JB_ERR_FILE))
874 if (raw ? (*raw == '\0') : is_empty)
876 /* EOF and no data there. (Definition of "data" depends on whether
877 * the caller cares about "raw" or just "data").
888 /* Got at least some data */
890 /* Remove trailing whitespace */
903 *prefix_out = prefix;
922 /*********************************************************************
924 * Function : read_config_line
926 * Description : Read a single non-empty line from a file and return
927 * it. Trims comments, leading and trailing whitespace
928 * and respects escaping of newline and comment char.
931 * 1 : buf = Buffer to use.
932 * 2 : buflen = Size of buffer in bytes.
933 * 3 : fp = File to read from
934 * 4 : linenum = linenumber in file
936 * Returns : NULL on EOF or error
937 * Otherwise, returns buf.
939 *********************************************************************/
940 char *read_config_line(char *buf, size_t buflen, FILE *fp, unsigned long *linenum)
944 err = edit_read_line(fp, NULL, NULL, &buf2, NULL, linenum);
947 if (err == JB_ERR_MEMORY)
949 log_error(LOG_LEVEL_FATAL, "Out of memory loading a config file");
956 assert(strlen(buf2) + 1U < buflen);
957 strncpy(buf, buf2, buflen - 1);
959 buf[buflen - 1] = '\0';
966 /*********************************************************************
968 * Function : unload_trustfile
970 * Description : Unloads a trustfile.
973 * 1 : f = the data structure associated with the trustfile.
977 *********************************************************************/
978 static void unload_trustfile(void *f)
980 struct block_spec *cur = (struct block_spec *)f;
981 struct block_spec *next;
987 free_url_spec(cur->url);
996 #ifdef FEATURE_GRACEFUL_TERMINATION
997 /*********************************************************************
999 * Function : unload_current_trust_file
1001 * Description : Unloads current trust file - reset to state at
1002 * beginning of program.
1008 *********************************************************************/
1009 void unload_current_trust_file(void)
1011 if (current_trustfile)
1013 current_trustfile->unloader = unload_trustfile;
1014 current_trustfile = NULL;
1017 #endif /* FEATURE_GRACEFUL_TERMINATION */
1020 /*********************************************************************
1022 * Function : load_trustfile
1024 * Description : Read and parse a trustfile and add to files list.
1027 * 1 : csp = Current client state (buffers, headers, etc...)
1029 * Returns : 0 => Ok, everything else is an error.
1031 *********************************************************************/
1032 int load_trustfile(struct client_state *csp)
1036 struct block_spec *b, *bl;
1037 struct url_spec **tl;
1039 char buf[BUFFER_SIZE], *p, *q;
1040 int reject, trusted;
1041 struct file_list *fs;
1042 unsigned long linenum = 0;
1044 if (!check_file_changed(current_trustfile, csp->config->trustfile, &fs))
1046 /* No need to load */
1049 csp->tlist = current_trustfile;
1055 goto load_trustfile_error;
1058 fs->f = bl = (struct block_spec *)zalloc(sizeof(*bl));
1061 goto load_trustfile_error;
1064 if ((fp = fopen(csp->config->trustfile, "r")) == NULL)
1066 goto load_trustfile_error;
1069 tl = csp->config->trust_list;
1071 while (read_config_line(buf, sizeof(buf), fp, &linenum) != NULL)
1087 while ((*p++ = *q++) != '\0')
1093 /* skip blank lines */
1099 /* allocate a new node */
1100 if ((b = zalloc(sizeof(*b))) == NULL)
1103 goto load_trustfile_error;
1106 /* add it to the list */
1112 /* Save the URL pattern */
1113 if (create_url_spec(b->url, buf))
1116 goto load_trustfile_error;
1120 * save a pointer to URL's spec in the list of trusted URL's, too
1125 /* FIXME BUFFER OVERFLOW if >=64 entries */
1133 /* the old one is now obsolete */
1134 if (current_trustfile)
1136 current_trustfile->unloader = unload_trustfile;
1139 fs->next = files->next;
1141 current_trustfile = fs;
1150 load_trustfile_error:
1151 log_error(LOG_LEVEL_FATAL, "can't load trustfile '%s': %E",
1152 csp->config->trustfile);
1156 #endif /* def FEATURE_TRUST */
1159 /*********************************************************************
1161 * Function : unload_re_filterfile
1163 * Description : Unload the re_filter list by freeing all chained
1164 * re_filterfile specs and their data.
1167 * 1 : f = the data structure associated with the filterfile.
1171 *********************************************************************/
1172 static void unload_re_filterfile(void *f)
1174 struct re_filterfile_spec *a, *b = (struct re_filterfile_spec *)f;
1180 destroy_list(b->patterns);
1181 pcrs_free_joblist(b->joblist);
1183 freez(b->description);
1193 #ifdef FEATURE_GRACEFUL_TERMINATION
1194 /*********************************************************************
1196 * Function : unload_current_re_filterfile
1198 * Description : Unloads current re_filter file - reset to state at
1199 * beginning of program.
1205 *********************************************************************/
1206 void unload_current_re_filterfile(void)
1208 if (current_re_filterfile)
1210 current_re_filterfile->unloader = unload_re_filterfile;
1211 current_re_filterfile = NULL;
1217 /*********************************************************************
1219 * Function : load_re_filterfile
1221 * Description : Load the re_filterfile.
1222 * Generate a chained list of re_filterfile_spec's from
1223 * the "FILTER: " blocks, compiling all their substitutions
1224 * into chained lists of pcrs_job structs.
1227 * 1 : csp = Current client state (buffers, headers, etc...)
1229 * Returns : 0 => Ok, everything else is an error.
1231 *********************************************************************/
1232 int load_re_filterfile(struct client_state *csp)
1236 struct re_filterfile_spec *new_bl, *bl = NULL;
1237 struct file_list *fs;
1239 char buf[BUFFER_SIZE];
1241 unsigned long linenum = 0;
1242 pcrs_job *dummy, *lastjob = NULL;
1245 * No need to reload if unchanged
1247 if (!check_file_changed(current_re_filterfile, csp->config->re_filterfile, &fs))
1251 csp->rlist = current_re_filterfile;
1257 goto load_re_filterfile_error;
1261 * Open the file or fail
1263 if ((fp = fopen(csp->config->re_filterfile, "r")) == NULL)
1265 goto load_re_filterfile_error;
1271 while (read_config_line(buf, sizeof(buf), fp, &linenum) != NULL)
1274 * If this is the head of a new filter block, make it a
1275 * re_filterfile spec of its own and chain it to the list:
1277 if (strncmp(buf, "FILTER:", 7) == 0)
1279 new_bl = (struct re_filterfile_spec *)zalloc(sizeof(*bl));
1282 goto load_re_filterfile_error;
1285 new_bl->name = chomp(buf + 7);
1287 if (NULL != (new_bl->description = strchr(new_bl->name, ' ')))
1289 *new_bl->description++ = '\0';
1290 new_bl->description = strdup(chomp(new_bl->description));
1294 new_bl->description = strdup("No description available for this filter");
1297 new_bl->name = strdup(chomp(new_bl->name));
1300 * If this is the first filter block, chain it
1301 * to the file_list rather than its (nonexistant)
1314 log_error(LOG_LEVEL_RE_FILTER, "Reading in filter \"%s\" (\"%s\")", bl->name, bl->description);
1320 * Else, save the expression, make it a pcrs_job
1321 * and chain it into the current filter's joblist
1325 enlist(bl->patterns, buf);
1327 if ((dummy = pcrs_compile_command(buf, &error)) == NULL)
1329 log_error(LOG_LEVEL_ERROR,
1330 "Adding re_filter job %s to filter %s failed with error %d.", buf, bl->name, error);
1335 if (bl->joblist == NULL)
1337 bl->joblist = dummy;
1341 lastjob->next = dummy;
1344 log_error(LOG_LEVEL_RE_FILTER, "Adding re_filter job %s to filter %s succeeded.", buf, bl->name);
1349 log_error(LOG_LEVEL_ERROR, "Ignoring job %s outside filter block in %s, line %d", buf, csp->config->re_filterfile, linenum);
1356 * Schedule the now-obsolete old data for unloading
1358 if ( NULL != current_re_filterfile )
1360 current_re_filterfile->unloader = unload_re_filterfile;
1364 * Chain this file into the global list of loaded files
1366 fs->next = files->next;
1368 current_re_filterfile = fs;
1377 load_re_filterfile_error:
1378 log_error(LOG_LEVEL_FATAL, "can't load re_filterfile '%s': %E",
1379 csp->config->re_filterfile);
1385 /*********************************************************************
1387 * Function : add_loader
1389 * Description : Called from `load_config'. Called once for each input
1390 * file found in config.
1393 * 1 : loader = pointer to a function that can parse and load
1394 * the appropriate config file.
1395 * 2 : config = The configuration_spec to add the loader to.
1399 *********************************************************************/
1400 void add_loader(int (*loader)(struct client_state *),
1401 struct configuration_spec * config)
1405 for (i=0; i < NLOADERS; i++)
1407 if (config->loaders[i] == NULL)
1409 config->loaders[i] = loader;
1417 /*********************************************************************
1419 * Function : run_loader
1421 * Description : Called from `load_config' and `listen_loop'. This
1422 * function keeps the "csp" current with any file mods
1423 * since the last loop. If a file is unchanged, the
1424 * loader functions do NOT reload the file.
1427 * 1 : csp = Current client state (buffers, headers, etc...)
1428 * Must be non-null. Reads: "csp->config"
1429 * Writes: various data members.
1431 * Returns : 0 => Ok, everything else is an error.
1433 *********************************************************************/
1434 int run_loader(struct client_state *csp)
1439 for (i=0; i < NLOADERS; i++)
1441 if (csp->config->loaders[i] == NULL)
1445 ret |= (csp->config->loaders[i])(csp);