1 const char loaders_rcs[] = "$Id: loaders.c,v 1.47 2002/03/26 22:29:55 swa 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 * 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.47 2002/03/26 22:29:55 swa
39 * we have a new homepage!
41 * Revision 1.46 2002/03/24 13:25:43 swa
42 * name change related issues
44 * Revision 1.45 2002/03/16 23:54:06 jongfoster
45 * Adding graceful termination feature, to help look for memory leaks.
46 * If you enable this (which, by design, has to be done by hand
47 * editing config.h) and then go to http://i.j.b/die, then the program
48 * will exit cleanly after the *next* request. It should free all the
49 * memory that was used.
51 * Revision 1.44 2002/03/16 21:51:00 jongfoster
54 * Revision 1.43 2002/03/16 20:28:34 oes
55 * Added descriptions to the filters so users will know what they select in the cgi editor
57 * Revision 1.42 2002/03/13 00:27:05 jongfoster
60 * Revision 1.41 2002/03/12 01:42:50 oes
61 * Introduced modular filters
63 * Revision 1.40 2002/03/08 17:46:04 jongfoster
64 * Fixing int/size_t warnings
66 * Revision 1.39 2002/03/07 03:46:17 oes
67 * Fixed compiler warnings
69 * Revision 1.38 2002/03/06 22:54:35 jongfoster
70 * Automated function-comment nitpicking.
72 * Revision 1.37 2002/03/03 15:07:49 oes
73 * Re-enabled automatic config reloading
75 * Revision 1.36 2002/01/22 23:46:18 jongfoster
76 * Moving edit_read_line() and simple_read_line() to loaders.c, and
77 * extending them to support reading MS-DOS, Mac and UNIX style files
80 * Modifying read_config_line() (without changing it's prototype) to
81 * be a trivial wrapper for edit_read_line(). This means that we have
82 * one function to read a line and handle comments, which is common
83 * between the initialization code and the edit interface.
85 * Revision 1.35 2002/01/17 21:03:08 jongfoster
86 * Moving all our URL and URL pattern parsing code to urlmatch.c.
88 * Renaming free_url to free_url_spec, since it frees a struct url_spec.
90 * Revision 1.34 2001/12/30 14:07:32 steudten
91 * - Add signal handling (unix)
92 * - Add SIGHUP handler (unix)
93 * - Add creation of pidfile (unix)
94 * - Add action 'top' in rc file (RH)
95 * - Add entry 'SIGNALS' to manpage
96 * - Add exit message to logfile (unix)
98 * Revision 1.33 2001/11/13 00:16:38 jongfoster
99 * Replacing references to malloc.h with the standard stdlib.h
100 * (See ANSI or K&R 2nd Ed)
102 * Revision 1.32 2001/11/07 00:02:13 steudten
103 * Add line number in error output for lineparsing for
104 * actionsfile and configfile.
105 * Special handling for CLF added.
107 * Revision 1.31 2001/10/26 17:39:01 oes
108 * Removed csp->referrer
109 * Moved ijb_isspace and ijb_tolower to project.h
111 * Revision 1.30 2001/10/25 03:40:48 david__schmidt
112 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
113 * threads to call select() simultaneously. So, it's time to do a real, live,
114 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
115 * (native). Both versions will work, but using __OS2__ offers multi-threading.
117 * Revision 1.29 2001/10/23 21:38:53 jongfoster
118 * Adding error-checking to create_url_spec()
120 * Revision 1.28 2001/10/07 15:40:39 oes
121 * Replaced 6 boolean members of csp with one bitmap (csp->flags)
123 * Revision 1.27 2001/09/22 16:36:59 jongfoster
124 * Removing unused parameter fs from read_config_line()
126 * Revision 1.26 2001/09/22 14:05:22 jongfoster
127 * Bugfix: Multiple escaped "#" characters in a configuration
128 * file are now permitted.
129 * Also removing 3 unused headers.
131 * Revision 1.25 2001/09/13 22:44:03 jongfoster
132 * Adding {} to an if statement
134 * Revision 1.24 2001/07/30 22:08:36 jongfoster
135 * Tidying up #defines:
136 * - All feature #defines are now of the form FEATURE_xxx
137 * - Permanently turned off WIN_GUI_EDIT
138 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
140 * Revision 1.23 2001/07/20 15:51:54 oes
141 * Fixed indentation of prepocessor commands
143 * Revision 1.22 2001/07/20 15:16:17 haroon
144 * - per Guy's suggestion, added a while loop in sweep() to catch not just
145 * the last inactive CSP but all other consecutive inactive CSPs after that
148 * Revision 1.21 2001/07/18 17:26:24 oes
149 * Changed to conform to new pcrs interface
151 * Revision 1.20 2001/07/17 13:07:01 oes
152 * Fixed segv when last line in config files
153 * lacked a terminating (\r)\n
155 * Revision 1.19 2001/07/13 14:01:54 oes
156 * Removed all #ifdef PCRS
158 * Revision 1.18 2001/06/29 21:45:41 oes
159 * Indentation, CRLF->LF, Tab-> Space
161 * Revision 1.17 2001/06/29 13:31:51 oes
164 * Revision 1.16 2001/06/09 10:55:28 jongfoster
165 * Changing BUFSIZ ==> BUFFER_SIZE
167 * Revision 1.15 2001/06/07 23:14:14 jongfoster
168 * Removing ACL and forward file loaders - these
169 * files have been merged into the config file.
170 * Cosmetic: Moving unloader funcs next to their
171 * respective loader funcs
173 * Revision 1.14 2001/06/01 03:27:04 oes
174 * Fixed line continuation problem
176 * Revision 1.13 2001/05/31 21:28:49 jongfoster
177 * Removed all permissionsfile code - it's now called the actions
178 * file, and (almost) all the code is in actions.c
180 * Revision 1.12 2001/05/31 17:32:31 oes
182 * - Enhanced domain part globbing with infix and prefix asterisk
183 * matching and optional unanchored operation
185 * Revision 1.11 2001/05/29 23:25:24 oes
187 * - load_config_line() and load_permissions_file() now use chomp()
189 * Revision 1.10 2001/05/29 09:50:24 jongfoster
190 * Unified blocklist/imagelist/permissionslist.
191 * File format is still under discussion, but the internal changes
194 * Also modified interceptor behaviour:
195 * - We now intercept all URLs beginning with one of the following
196 * prefixes (and *only* these prefixes):
198 * * http://ijbswa.sf.net/config/
199 * * http://ijbswa.sourceforge.net/config/
200 * - New interceptors "home page" - go to http://i.j.b/ to see it.
201 * - Internal changes so that intercepted and fast redirect pages
202 * are not replaced with an image.
203 * - Interceptors now have the option to send a binary page direct
204 * to the client. (i.e. ijb-send-banner uses this)
205 * - Implemented show-url-info interceptor. (Which is why I needed
206 * the above interceptors changes - a typical URL is
207 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
208 * The previous mechanism would not have intercepted that, and
209 * if it had been intercepted then it then it would have replaced
212 * Revision 1.9 2001/05/26 17:12:07 jongfoster
213 * Fatal errors loading configuration files now give better error messages.
215 * Revision 1.8 2001/05/26 00:55:20 jongfoster
216 * Removing duplicated code. load_forwardfile() now uses create_url_spec()
218 * Revision 1.7 2001/05/26 00:28:36 jongfoster
219 * Automatic reloading of config file.
220 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
221 * Most of the global variables have been moved to a new
222 * struct configuration_spec, accessed through csp->config->globalname
223 * Most of the globals remaining are used by the Win32 GUI.
225 * Revision 1.6 2001/05/23 12:27:33 oes
227 * Fixed ugly indentation of my last changes
229 * Revision 1.5 2001/05/23 10:39:05 oes
230 * - Added support for escaping the comment character
231 * in config files by a backslash
232 * - Added support for line continuation in config
234 * - Fixed a buffer overflow bug with long config lines
236 * Revision 1.4 2001/05/22 18:56:28 oes
239 * Revision 1.3 2001/05/20 01:21:20 jongfoster
240 * Version 2.9.4 checkin.
241 * - Merged popupfile and cookiefile, and added control over PCRS
242 * filtering, in new "permissionsfile".
243 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
244 * file error you now get a message box (in the Win32 GUI) rather
245 * than the program exiting with no explanation.
246 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
248 * - Removed tabs from "config"
249 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
250 * - Bumped up version number.
252 * Revision 1.2 2001/05/17 23:01:01 oes
253 * - Cleaned CRLF's from the sources and related files
255 * Revision 1.1.1.1 2001/05/15 13:58:59 oes
256 * Initial import of version 2.9.3 source tree
259 *********************************************************************/
266 #include <sys/types.h>
269 #include <sys/stat.h>
273 #if !defined(_WIN32) && !defined(__OS2__)
283 #include "miscutil.h"
286 #include "urlmatch.h"
288 const char loaders_h_rcs[] = LOADERS_H_VERSION;
291 * Currently active files.
292 * These are also entered in the main linked list of files.
296 static struct file_list *current_trustfile = NULL;
297 #endif /* def FEATURE_TRUST */
299 static struct file_list *current_re_filterfile = NULL;
303 /*********************************************************************
307 * Description : Basically a mark and sweep garbage collector, it is run
308 * (by the parent thread) every once in a while to reclaim memory.
310 * It uses a mark and sweep strategy:
311 * 1) mark all files as inactive
313 * 2) check with each client:
314 * if it is active, mark its files as active
315 * if it is inactive, free its resources
317 * 3) free the resources of all of the files that
318 * are still marked as inactive (and are obsolete).
320 * N.B. files that are not obsolete don't have an unloader defined.
326 *********************************************************************/
329 struct file_list *fl, *nfl;
330 struct client_state *csp, *ncsp;
332 /* clear all of the file's active flags */
333 for ( fl = files->next; NULL != fl; fl = fl->next )
338 for (csp = clients; csp && (NULL != (ncsp = csp->next)) ; csp = csp->next)
340 if (ncsp->flags & CSP_FLAG_ACTIVE)
342 /* mark this client's files as active */
345 * Always have a configuration file.
346 * (Also note the slightly non-standard extra
349 ncsp->config->config_file_list->active = 1;
351 if (ncsp->actions_list) /* actions files */
353 ncsp->actions_list->active = 1;
356 if (ncsp->rlist) /* pcrsjob files */
358 ncsp->rlist->active = 1;
362 if (ncsp->tlist) /* trust files */
364 ncsp->tlist->active = 1;
366 #endif /* def FEATURE_TRUST */
371 * this client is not active, release its resources
372 * and the ones of all inactive clients that might
376 while (!(ncsp->flags & CSP_FLAG_ACTIVE))
378 csp->next = ncsp->next;
380 freez(ncsp->ip_addr_str);
381 freez(ncsp->my_ip_addr_str);
382 freez(ncsp->my_hostname);
383 freez(ncsp->x_forwarded);
384 freez(ncsp->iob->buf);
386 free_http_request(ncsp->http);
388 destroy_list(ncsp->headers);
389 destroy_list(ncsp->cookie_list);
391 free_current_action(ncsp->action);
393 #ifdef FEATURE_STATISTICS
395 if (ncsp->flags & CSP_FLAG_REJECTED)
399 #endif /* def FEATURE_STATISTICS */
403 /* are there any more in sequence after it? */
404 if( (ncsp = csp->next) == NULL)
410 for (fl = files; fl && ((nfl = fl->next) != NULL) ; fl = fl->next)
412 if ( ( 0 == nfl->active ) && ( NULL != nfl->unloader ) )
414 fl->next = nfl->next;
416 (nfl->unloader)(nfl->f);
418 freez(nfl->filename);
427 /*********************************************************************
429 * Function : check_file_changed
431 * Description : Helper function to check if a file needs reloading.
432 * If "current" is still current, return it. Otherwise
433 * allocates a new (zeroed) "struct file_list", fills
434 * in the disk file name and timestamp, and returns it.
437 * 1 : current = The file_list currently being used - will
438 * be checked to see if it is out of date.
439 * May be NULL (which is treated as out of
441 * 2 : filename = Name of file to check.
442 * 3 : newfl = New file list. [Output only]
443 * This will be set to NULL, OR a struct
444 * file_list newly allocated on the
445 * heap, with the filename and lastmodified
446 * fields filled, and all others zeroed.
448 * Returns : If file unchanged: 0 (and sets newfl == NULL)
449 * If file changed: 1 and sets newfl != NULL
450 * On error: 1 and sets newfl == NULL
452 *********************************************************************/
453 int check_file_changed(const struct file_list * current,
454 const char * filename,
455 struct file_list ** newfl)
457 struct file_list *fs;
458 struct stat statbuf[1];
462 if (stat(filename, statbuf) < 0)
464 /* Error, probably file not found. */
469 && (current->lastmodified == statbuf->st_mtime)
470 && (0 == strcmp(current->filename, filename)))
475 fs = (struct file_list *)zalloc(sizeof(struct file_list));
478 /* Out of memory error */
482 fs->filename = strdup(filename);
483 fs->lastmodified = statbuf->st_mtime;
485 if (fs->filename == NULL)
487 /* Out of memory error */
496 /*********************************************************************
498 * Function : simple_read_line
500 * Description : Read a single line from a file and return it.
501 * This is basically a version of fgets() that malloc()s
502 * it's own line buffer. Note that the buffer will
503 * always be a multiple of BUFFER_SIZE bytes long.
504 * Therefore if you are going to keep the string for
505 * an extended period of time, you should probably
506 * strdup() it and free() the original, to save memory.
510 * 1 : dest = destination for newly malloc'd pointer to
511 * line data. Will be set to NULL on error.
512 * 2 : fp = File to read from
513 * 3 : newline = Standard for newlines in the file.
514 * Will be unchanged if it's value on input is not
516 * On output, may be changed from NEWLINE_UNKNOWN to
517 * actual convention in file.
519 * Returns : JB_ERR_OK on success
520 * JB_ERR_MEMORY on out-of-memory
521 * JB_ERR_FILE on EOF.
523 *********************************************************************/
524 jb_err simple_read_line(FILE *fp, char **dest, int *newline)
527 size_t buflen = BUFFER_SIZE;
531 int realnewline = NEWLINE_UNKNOWN;
533 if (NULL == (buf = malloc(buflen)))
535 return JB_ERR_MEMORY;
541 * Character codes. If you have a wierd compiler and the following are
542 * incorrect, you also need to fix NEWLINE() in loaders.h
544 #define CHAR_CR '\r' /* ASCII 13 */
545 #define CHAR_LF '\n' /* ASCII 10 */
565 else if (ch == CHAR_CR)
570 if (*newline == NEWLINE_UNKNOWN)
572 *newline = NEWLINE_DOS;
581 if (*newline == NEWLINE_UNKNOWN)
583 *newline = NEWLINE_MAC;
588 if (*newline == NEWLINE_UNKNOWN)
590 *newline = realnewline;
594 else if (ch == CHAR_LF)
598 if (*newline == NEWLINE_UNKNOWN)
600 *newline = NEWLINE_UNIX;
615 buflen += BUFFER_SIZE;
616 if (NULL == (p = realloc(buf, buflen)))
619 return JB_ERR_MEMORY;
628 /*********************************************************************
630 * Function : edit_read_line
632 * Description : Read a single non-empty line from a file and return
633 * it. Trims comments, leading and trailing whitespace
634 * and respects escaping of newline and comment char.
635 * Provides the line in 2 alternative forms: raw and
637 * - raw is the raw data read from the file. If the
638 * line is not modified, then this should be written
640 * - prefix is any comments and blank lines that were
641 * read from the file. If the line is modified, then
642 * this should be written out to the file followed
643 * by the modified data. (If this string is non-empty
644 * then it will have a newline at the end).
645 * - data is the actual data that will be parsed
646 * further by appropriate routines.
647 * On EOF, the 3 strings will all be set to NULL and
648 * 0 will be returned.
651 * 1 : fp = File to read from
652 * 2 : raw_out = destination for newly malloc'd pointer to
653 * raw line data. May be NULL if you don't want it.
654 * 3 : prefix_out = destination for newly malloc'd pointer to
655 * comments. May be NULL if you don't want it.
656 * 4 : data_out = destination for newly malloc'd pointer to
657 * line data with comments and leading/trailing spaces
658 * removed, and line continuation performed. May be
659 * NULL if you don't want it.
660 * 5 : newline = Standard for newlines in the file.
661 * On input, set to value to use or NEWLINE_UNKNOWN.
662 * On output, may be changed from NEWLINE_UNKNOWN to
663 * actual convention in file. May be NULL if you
665 * 6 : line_number = Line number in file. In "lines" as
666 * reported by a text editor, not lines containing data.
668 * Returns : JB_ERR_OK on success
669 * JB_ERR_MEMORY on out-of-memory
670 * JB_ERR_FILE on EOF.
672 *********************************************************************/
673 jb_err edit_read_line(FILE *fp,
678 unsigned long *line_number)
680 char *p; /* Temporary pointer */
681 char *linebuf; /* Line read from file */
682 char *linestart; /* Start of linebuf, usually first non-whitespace char */
683 int contflag = 0; /* Nonzero for line continuation - i.e. line ends '\' */
684 int is_empty = 1; /* Flag if not got any data yet */
685 char *raw = NULL; /* String to be stored in raw_out */
686 char *prefix = NULL; /* String to be stored in prefix_out */
687 char *data = NULL; /* String to be stored in data_out */
688 int scrapnewline; /* Used for (*newline) if newline==NULL */
689 jb_err rval = JB_ERR_OK;
692 assert(raw_out || data_out);
693 assert(newline == NULL
694 || *newline == NEWLINE_UNKNOWN
695 || *newline == NEWLINE_UNIX
696 || *newline == NEWLINE_DOS
697 || *newline == NEWLINE_MAC);
701 scrapnewline = NEWLINE_UNKNOWN;
702 newline = &scrapnewline;
705 /* Set output parameters to NULL */
719 /* Set string variables to new, empty strings. */
723 if ((raw = malloc(1)) == NULL)
725 return JB_ERR_MEMORY;
731 if ((prefix = malloc(1)) == NULL)
734 return JB_ERR_MEMORY;
740 if ((data = malloc(1)) == NULL)
744 return JB_ERR_MEMORY;
749 /* Main loop. Loop while we need more data & it's not EOF. */
751 while ( (contflag || is_empty)
752 && (JB_ERR_OK == (rval = simple_read_line(fp, &linebuf, newline))))
760 string_append(&raw,linebuf);
761 if (string_append(&raw,NEWLINE(*newline)))
766 return JB_ERR_MEMORY;
770 /* Line continuation? Trim escape and set flag. */
771 p = linebuf + strlen(linebuf) - 1;
772 contflag = ((*linebuf != '\0') && (*p == '\\'));
778 /* Trim leading spaces if we're at the start of the line */
782 /* Trim leading spaces */
783 while (*linestart && isspace((int)(unsigned char)*linestart))
789 /* Handle comment characters. */
791 while ((p = strchr(p, '#')) != NULL)
793 /* Found a comment char.. */
794 if ((p != linebuf) && (*(p-1) == '\\'))
796 /* ..and it's escaped, left-shift the line over the escape. */
798 while ((*q = *(q + 1)) != '\0')
802 /* Now scan from just after the "#". */
806 /* Real comment. Save it... */
809 /* Special case: Line only contains a comment, so all the
810 * previous whitespace is considered part of the comment.
811 * Undo the whitespace skipping, if any.
818 string_append(&prefix,p);
819 if (string_append(&prefix, NEWLINE(*newline)))
824 return JB_ERR_MEMORY;
828 /* ... and chop off the rest of the line */
831 } /* END while (there's a # character) */
833 /* Write to the buffer */
839 if (string_append(&data, linestart))
844 return JB_ERR_MEMORY;
850 } /* END while(we need more data) */
852 /* Handle simple_read_line() errors - ignore EOF */
853 if ((rval != JB_ERR_OK) && (rval != JB_ERR_FILE))
861 if (raw ? (*raw == '\0') : is_empty)
863 /* EOF and no data there. (Definition of "data" depends on whether
864 * the caller cares about "raw" or just "data").
875 /* Got at least some data */
877 /* Remove trailing whitespace */
890 *prefix_out = prefix;
909 /*********************************************************************
911 * Function : read_config_line
913 * Description : Read a single non-empty line from a file and return
914 * it. Trims comments, leading and trailing whitespace
915 * and respects escaping of newline and comment char.
918 * 1 : buf = Buffer to use.
919 * 2 : buflen = Size of buffer in bytes.
920 * 3 : fp = File to read from
921 * 4 : linenum = linenumber in file
923 * Returns : NULL on EOF or error
924 * Otherwise, returns buf.
926 *********************************************************************/
927 char *read_config_line(char *buf, size_t buflen, FILE *fp, unsigned long *linenum)
931 err = edit_read_line(fp, NULL, NULL, &buf2, NULL, linenum);
934 if (err == JB_ERR_MEMORY)
936 log_error(LOG_LEVEL_FATAL, "Out of memory loading a config file");
943 assert(strlen(buf2) + 1U < buflen);
944 strncpy(buf, buf2, buflen - 1);
946 buf[buflen - 1] = '\0';
953 /*********************************************************************
955 * Function : unload_trustfile
957 * Description : Unloads a trustfile.
960 * 1 : f = the data structure associated with the trustfile.
964 *********************************************************************/
965 static void unload_trustfile(void *f)
967 struct block_spec *cur = (struct block_spec *)f;
968 struct block_spec *next;
974 free_url_spec(cur->url);
983 #ifdef FEATURE_GRACEFUL_TERMINATION
984 /*********************************************************************
986 * Function : unload_current_trust_file
988 * Description : Unloads current trust file - reset to state at
989 * beginning of program.
995 *********************************************************************/
996 void unload_current_trust_file(void)
998 if (current_trustfile)
1000 current_trustfile->unloader = unload_trustfile;
1001 current_trustfile = NULL;
1004 #endif /* FEATURE_GRACEFUL_TERMINATION */
1007 /*********************************************************************
1009 * Function : load_trustfile
1011 * Description : Read and parse a trustfile and add to files list.
1014 * 1 : csp = Current client state (buffers, headers, etc...)
1016 * Returns : 0 => Ok, everything else is an error.
1018 *********************************************************************/
1019 int load_trustfile(struct client_state *csp)
1023 struct block_spec *b, *bl;
1024 struct url_spec **tl;
1026 char buf[BUFFER_SIZE], *p, *q;
1027 int reject, trusted;
1028 struct file_list *fs;
1029 unsigned long linenum = 0;
1031 if (!check_file_changed(current_trustfile, csp->config->trustfile, &fs))
1033 /* No need to load */
1036 csp->tlist = current_trustfile;
1042 goto load_trustfile_error;
1045 fs->f = bl = (struct block_spec *)zalloc(sizeof(*bl));
1048 goto load_trustfile_error;
1051 if ((fp = fopen(csp->config->trustfile, "r")) == NULL)
1053 goto load_trustfile_error;
1056 tl = csp->config->trust_list;
1058 while (read_config_line(buf, sizeof(buf), fp, &linenum) != NULL)
1074 while ((*p++ = *q++) != '\0')
1080 /* skip blank lines */
1086 /* allocate a new node */
1087 if ((b = zalloc(sizeof(*b))) == NULL)
1090 goto load_trustfile_error;
1093 /* add it to the list */
1099 /* Save the URL pattern */
1100 if (create_url_spec(b->url, buf))
1103 goto load_trustfile_error;
1107 * save a pointer to URL's spec in the list of trusted URL's, too
1112 /* FIXME BUFFER OVERFLOW if >=64 entries */
1120 /* the old one is now obsolete */
1121 if (current_trustfile)
1123 current_trustfile->unloader = unload_trustfile;
1126 fs->next = files->next;
1128 current_trustfile = fs;
1137 load_trustfile_error:
1138 log_error(LOG_LEVEL_FATAL, "can't load trustfile '%s': %E",
1139 csp->config->trustfile);
1143 #endif /* def FEATURE_TRUST */
1146 /*********************************************************************
1148 * Function : unload_re_filterfile
1150 * Description : Unload the re_filter list by freeing all chained
1151 * re_filterfile specs and their data.
1154 * 1 : f = the data structure associated with the filterfile.
1158 *********************************************************************/
1159 static void unload_re_filterfile(void *f)
1161 struct re_filterfile_spec *a, *b = (struct re_filterfile_spec *)f;
1167 destroy_list(b->patterns);
1168 pcrs_free_joblist(b->joblist);
1170 freez(b->description);
1180 #ifdef FEATURE_GRACEFUL_TERMINATION
1181 /*********************************************************************
1183 * Function : unload_current_re_filterfile
1185 * Description : Unloads current re_filter file - reset to state at
1186 * beginning of program.
1192 *********************************************************************/
1193 void unload_current_re_filterfile(void)
1195 if (current_re_filterfile)
1197 current_re_filterfile->unloader = unload_re_filterfile;
1198 current_re_filterfile = NULL;
1204 /*********************************************************************
1206 * Function : load_re_filterfile
1208 * Description : Load the re_filterfile.
1209 * Generate a chained list of re_filterfile_spec's from
1210 * the "FILTER: " blocks, compiling all their substitutions
1211 * into chained lists of pcrs_job structs.
1214 * 1 : csp = Current client state (buffers, headers, etc...)
1216 * Returns : 0 => Ok, everything else is an error.
1218 *********************************************************************/
1219 int load_re_filterfile(struct client_state *csp)
1223 struct re_filterfile_spec *new_bl, *bl = NULL;
1224 struct file_list *fs;
1226 char buf[BUFFER_SIZE];
1228 unsigned long linenum = 0;
1232 * No need to reload if unchanged
1234 if (!check_file_changed(current_re_filterfile, csp->config->re_filterfile, &fs))
1238 csp->rlist = current_re_filterfile;
1244 goto load_re_filterfile_error;
1248 * Open the file or fail
1250 if ((fp = fopen(csp->config->re_filterfile, "r")) == NULL)
1252 goto load_re_filterfile_error;
1258 while (read_config_line(buf, sizeof(buf), fp, &linenum) != NULL)
1261 * If this is the head of a new filter block, make it a
1262 * re_filterfile spec of its own and chain it to the list:
1264 if (strncmp(buf, "FILTER:", 7) == 0)
1266 new_bl = (struct re_filterfile_spec *)zalloc(sizeof(*bl));
1269 goto load_re_filterfile_error;
1272 new_bl->name = chomp(buf + 7);
1274 if (NULL != (new_bl->description = strchr(new_bl->name, ' ')))
1276 *new_bl->description++ = '\0';
1277 new_bl->description = strdup(chomp(new_bl->description));
1281 new_bl->description = strdup("No description available for this filter");
1284 new_bl->name = strdup(chomp(new_bl->name));
1287 * If this is the first filter block, chain it
1288 * to the file_list rather than its (nonexistant)
1301 log_error(LOG_LEVEL_RE_FILTER, "Reading in filter \"%s\" (\"%s\")", bl->name, bl->description);
1307 * Else, save the expression, make it a pcrs_job
1308 * and chain it into the current filter's joblist
1312 enlist(bl->patterns, buf);
1314 if ((dummy = pcrs_compile_command(buf, &error)) == NULL)
1316 log_error(LOG_LEVEL_RE_FILTER,
1317 "Adding re_filter job %s to filter %s failed with error %d.", buf, bl->name, error);
1322 dummy->next = bl->joblist;
1323 bl->joblist = dummy;
1324 log_error(LOG_LEVEL_RE_FILTER, "Adding re_filter job %s to filter %s succeeded.", buf, bl->name);
1329 log_error(LOG_LEVEL_ERROR, "Ignoring job %s outside filter block in %s, line %d", buf, csp->config->re_filterfile, linenum);
1336 * Schedule the now-obsolete old data for unloading
1338 if ( NULL != current_re_filterfile )
1340 current_re_filterfile->unloader = unload_re_filterfile;
1344 * Chain this file into the global list of loaded files
1346 fs->next = files->next;
1348 current_re_filterfile = fs;
1357 load_re_filterfile_error:
1358 log_error(LOG_LEVEL_FATAL, "can't load re_filterfile '%s': %E",
1359 csp->config->re_filterfile);
1365 /*********************************************************************
1367 * Function : add_loader
1369 * Description : Called from `load_config'. Called once for each input
1370 * file found in config.
1373 * 1 : loader = pointer to a function that can parse and load
1374 * the appropriate config file.
1375 * 2 : config = The configuration_spec to add the loader to.
1379 *********************************************************************/
1380 void add_loader(int (*loader)(struct client_state *),
1381 struct configuration_spec * config)
1385 for (i=0; i < NLOADERS; i++)
1387 if (config->loaders[i] == NULL)
1389 config->loaders[i] = loader;
1397 /*********************************************************************
1399 * Function : run_loader
1401 * Description : Called from `load_config' and `listen_loop'. This
1402 * function keeps the "csp" current with any file mods
1403 * since the last loop. If a file is unchanged, the
1404 * loader functions do NOT reload the file.
1407 * 1 : csp = Current client state (buffers, headers, etc...)
1408 * Must be non-null. Reads: "csp->config"
1409 * Writes: various data members.
1411 * Returns : 0 => Ok, everything else is an error.
1413 *********************************************************************/
1414 int run_loader(struct client_state *csp)
1419 for (i=0; i < NLOADERS; i++)
1421 if (csp->config->loaders[i] == NULL)
1425 ret |= (csp->config->loaders[i])(csp);