1 const char loaders_rcs[] = "$Id: loaders.c,v 1.50.2.2 2002/11/20 14:38:15 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.2.2 2002/11/20 14:38:15 oes
39 * Fixed delayed/incomplete freeing of client resources and
40 * simplified loop structure in sweep.
41 * Thanks to Oliver Stoeneberg for the hint.
43 * Revision 1.50.2.1 2002/07/26 15:19:24 oes
44 * - PCRS jobs now chained in order of appearance. Previous
45 * reverse chaining was counter-intuitive.
46 * - Changed loglevel of PCRS job compile errors to
49 * Revision 1.50 2002/04/24 02:12:16 oes
50 * Jon's multiple AF patch: Sweep now takes care of all AFs
52 * Revision 1.49 2002/04/19 16:53:25 jongfoster
53 * Optimize away a function call by using an equivalent macro
55 * Revision 1.48 2002/04/05 00:56:09 gliptak
56 * Correcting typo to clean up on realloc failure
58 * Revision 1.47 2002/03/26 22:29:55 swa
59 * we have a new homepage!
61 * Revision 1.46 2002/03/24 13:25:43 swa
62 * name change related issues
64 * Revision 1.45 2002/03/16 23:54:06 jongfoster
65 * Adding graceful termination feature, to help look for memory leaks.
66 * If you enable this (which, by design, has to be done by hand
67 * editing config.h) and then go to http://i.j.b/die, then the program
68 * will exit cleanly after the *next* request. It should free all the
69 * memory that was used.
71 * Revision 1.44 2002/03/16 21:51:00 jongfoster
74 * Revision 1.43 2002/03/16 20:28:34 oes
75 * Added descriptions to the filters so users will know what they select in the cgi editor
77 * Revision 1.42 2002/03/13 00:27:05 jongfoster
80 * Revision 1.41 2002/03/12 01:42:50 oes
81 * Introduced modular filters
83 * Revision 1.40 2002/03/08 17:46:04 jongfoster
84 * Fixing int/size_t warnings
86 * Revision 1.39 2002/03/07 03:46:17 oes
87 * Fixed compiler warnings
89 * Revision 1.38 2002/03/06 22:54:35 jongfoster
90 * Automated function-comment nitpicking.
92 * Revision 1.37 2002/03/03 15:07:49 oes
93 * Re-enabled automatic config reloading
95 * Revision 1.36 2002/01/22 23:46:18 jongfoster
96 * Moving edit_read_line() and simple_read_line() to loaders.c, and
97 * extending them to support reading MS-DOS, Mac and UNIX style files
100 * Modifying read_config_line() (without changing it's prototype) to
101 * be a trivial wrapper for edit_read_line(). This means that we have
102 * one function to read a line and handle comments, which is common
103 * between the initialization code and the edit interface.
105 * Revision 1.35 2002/01/17 21:03:08 jongfoster
106 * Moving all our URL and URL pattern parsing code to urlmatch.c.
108 * Renaming free_url to free_url_spec, since it frees a struct url_spec.
110 * Revision 1.34 2001/12/30 14:07:32 steudten
111 * - Add signal handling (unix)
112 * - Add SIGHUP handler (unix)
113 * - Add creation of pidfile (unix)
114 * - Add action 'top' in rc file (RH)
115 * - Add entry 'SIGNALS' to manpage
116 * - Add exit message to logfile (unix)
118 * Revision 1.33 2001/11/13 00:16:38 jongfoster
119 * Replacing references to malloc.h with the standard stdlib.h
120 * (See ANSI or K&R 2nd Ed)
122 * Revision 1.32 2001/11/07 00:02:13 steudten
123 * Add line number in error output for lineparsing for
124 * actionsfile and configfile.
125 * Special handling for CLF added.
127 * Revision 1.31 2001/10/26 17:39:01 oes
128 * Removed csp->referrer
129 * Moved ijb_isspace and ijb_tolower to project.h
131 * Revision 1.30 2001/10/25 03:40:48 david__schmidt
132 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
133 * threads to call select() simultaneously. So, it's time to do a real, live,
134 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
135 * (native). Both versions will work, but using __OS2__ offers multi-threading.
137 * Revision 1.29 2001/10/23 21:38:53 jongfoster
138 * Adding error-checking to create_url_spec()
140 * Revision 1.28 2001/10/07 15:40:39 oes
141 * Replaced 6 boolean members of csp with one bitmap (csp->flags)
143 * Revision 1.27 2001/09/22 16:36:59 jongfoster
144 * Removing unused parameter fs from read_config_line()
146 * Revision 1.26 2001/09/22 14:05:22 jongfoster
147 * Bugfix: Multiple escaped "#" characters in a configuration
148 * file are now permitted.
149 * Also removing 3 unused headers.
151 * Revision 1.25 2001/09/13 22:44:03 jongfoster
152 * Adding {} to an if statement
154 * Revision 1.24 2001/07/30 22:08:36 jongfoster
155 * Tidying up #defines:
156 * - All feature #defines are now of the form FEATURE_xxx
157 * - Permanently turned off WIN_GUI_EDIT
158 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
160 * Revision 1.23 2001/07/20 15:51:54 oes
161 * Fixed indentation of prepocessor commands
163 * Revision 1.22 2001/07/20 15:16:17 haroon
164 * - per Guy's suggestion, added a while loop in sweep() to catch not just
165 * the last inactive CSP but all other consecutive inactive CSPs after that
168 * Revision 1.21 2001/07/18 17:26:24 oes
169 * Changed to conform to new pcrs interface
171 * Revision 1.20 2001/07/17 13:07:01 oes
172 * Fixed segv when last line in config files
173 * lacked a terminating (\r)\n
175 * Revision 1.19 2001/07/13 14:01:54 oes
176 * Removed all #ifdef PCRS
178 * Revision 1.18 2001/06/29 21:45:41 oes
179 * Indentation, CRLF->LF, Tab-> Space
181 * Revision 1.17 2001/06/29 13:31:51 oes
184 * Revision 1.16 2001/06/09 10:55:28 jongfoster
185 * Changing BUFSIZ ==> BUFFER_SIZE
187 * Revision 1.15 2001/06/07 23:14:14 jongfoster
188 * Removing ACL and forward file loaders - these
189 * files have been merged into the config file.
190 * Cosmetic: Moving unloader funcs next to their
191 * respective loader funcs
193 * Revision 1.14 2001/06/01 03:27:04 oes
194 * Fixed line continuation problem
196 * Revision 1.13 2001/05/31 21:28:49 jongfoster
197 * Removed all permissionsfile code - it's now called the actions
198 * file, and (almost) all the code is in actions.c
200 * Revision 1.12 2001/05/31 17:32:31 oes
202 * - Enhanced domain part globbing with infix and prefix asterisk
203 * matching and optional unanchored operation
205 * Revision 1.11 2001/05/29 23:25:24 oes
207 * - load_config_line() and load_permissions_file() now use chomp()
209 * Revision 1.10 2001/05/29 09:50:24 jongfoster
210 * Unified blocklist/imagelist/permissionslist.
211 * File format is still under discussion, but the internal changes
214 * Also modified interceptor behaviour:
215 * - We now intercept all URLs beginning with one of the following
216 * prefixes (and *only* these prefixes):
218 * * http://ijbswa.sf.net/config/
219 * * http://ijbswa.sourceforge.net/config/
220 * - New interceptors "home page" - go to http://i.j.b/ to see it.
221 * - Internal changes so that intercepted and fast redirect pages
222 * are not replaced with an image.
223 * - Interceptors now have the option to send a binary page direct
224 * to the client. (i.e. ijb-send-banner uses this)
225 * - Implemented show-url-info interceptor. (Which is why I needed
226 * the above interceptors changes - a typical URL is
227 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
228 * The previous mechanism would not have intercepted that, and
229 * if it had been intercepted then it then it would have replaced
232 * Revision 1.9 2001/05/26 17:12:07 jongfoster
233 * Fatal errors loading configuration files now give better error messages.
235 * Revision 1.8 2001/05/26 00:55:20 jongfoster
236 * Removing duplicated code. load_forwardfile() now uses create_url_spec()
238 * Revision 1.7 2001/05/26 00:28:36 jongfoster
239 * Automatic reloading of config file.
240 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
241 * Most of the global variables have been moved to a new
242 * struct configuration_spec, accessed through csp->config->globalname
243 * Most of the globals remaining are used by the Win32 GUI.
245 * Revision 1.6 2001/05/23 12:27:33 oes
247 * Fixed ugly indentation of my last changes
249 * Revision 1.5 2001/05/23 10:39:05 oes
250 * - Added support for escaping the comment character
251 * in config files by a backslash
252 * - Added support for line continuation in config
254 * - Fixed a buffer overflow bug with long config lines
256 * Revision 1.4 2001/05/22 18:56:28 oes
259 * Revision 1.3 2001/05/20 01:21:20 jongfoster
260 * Version 2.9.4 checkin.
261 * - Merged popupfile and cookiefile, and added control over PCRS
262 * filtering, in new "permissionsfile".
263 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
264 * file error you now get a message box (in the Win32 GUI) rather
265 * than the program exiting with no explanation.
266 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
268 * - Removed tabs from "config"
269 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
270 * - Bumped up version number.
272 * Revision 1.2 2001/05/17 23:01:01 oes
273 * - Cleaned CRLF's from the sources and related files
275 * Revision 1.1.1.1 2001/05/15 13:58:59 oes
276 * Initial import of version 2.9.3 source tree
279 *********************************************************************/
286 #include <sys/types.h>
289 #include <sys/stat.h>
293 #if !defined(_WIN32) && !defined(__OS2__)
303 #include "miscutil.h"
306 #include "urlmatch.h"
308 const char loaders_h_rcs[] = LOADERS_H_VERSION;
311 * Currently active files.
312 * These are also entered in the main linked list of files.
316 static struct file_list *current_trustfile = NULL;
317 #endif /* def FEATURE_TRUST */
319 static struct file_list *current_re_filterfile = NULL;
323 /*********************************************************************
327 * Description : Basically a mark and sweep garbage collector, it is run
328 * (by the parent thread) every once in a while to reclaim memory.
330 * It uses a mark and sweep strategy:
331 * 1) mark all files as inactive
333 * 2) check with each client:
334 * if it is active, mark its files as active
335 * if it is inactive, free its resources
337 * 3) free the resources of all of the files that
338 * are still marked as inactive (and are obsolete).
340 * N.B. files that are not obsolete don't have an unloader defined.
346 *********************************************************************/
349 struct file_list *fl, *nfl;
350 struct client_state *csp, *last_active;
353 /* clear all of the file's active flags */
354 for ( fl = files->next; NULL != fl; fl = fl->next )
359 last_active = clients;
364 if (csp->flags & CSP_FLAG_ACTIVE)
366 /* Mark this client's files as active */
369 * Always have a configuration file.
370 * (Also note the slightly non-standard extra
373 csp->config->config_file_list->active = 1;
378 for (i = 0; i < MAX_ACTION_FILES; i++)
380 if (csp->actions_list[i])
382 csp->actions_list[i]->active = 1;
391 csp->rlist->active = 1;
400 csp->tlist->active = 1;
402 #endif /* def FEATURE_TRUST */
409 * This client is not active. Free its resources.
412 last_active->next = csp->next;
414 freez(csp->ip_addr_str);
415 freez(csp->my_ip_addr_str);
416 freez(csp->my_hostname);
417 freez(csp->x_forwarded);
418 freez(csp->iob->buf);
420 free_http_request(csp->http);
422 destroy_list(csp->headers);
423 destroy_list(csp->cookie_list);
425 free_current_action(csp->action);
427 #ifdef FEATURE_STATISTICS
429 if (csp->flags & CSP_FLAG_REJECTED)
433 #endif /* def FEATURE_STATISTICS */
437 csp = last_active->next;
441 for (fl = files; fl && ((nfl = fl->next) != NULL) ; fl = fl->next)
443 if ( ( 0 == nfl->active ) && ( NULL != nfl->unloader ) )
445 fl->next = nfl->next;
447 (nfl->unloader)(nfl->f);
449 freez(nfl->filename);
458 /*********************************************************************
460 * Function : check_file_changed
462 * Description : Helper function to check if a file needs reloading.
463 * If "current" is still current, return it. Otherwise
464 * allocates a new (zeroed) "struct file_list", fills
465 * in the disk file name and timestamp, and returns it.
468 * 1 : current = The file_list currently being used - will
469 * be checked to see if it is out of date.
470 * May be NULL (which is treated as out of
472 * 2 : filename = Name of file to check.
473 * 3 : newfl = New file list. [Output only]
474 * This will be set to NULL, OR a struct
475 * file_list newly allocated on the
476 * heap, with the filename and lastmodified
477 * fields filled, and all others zeroed.
479 * Returns : If file unchanged: 0 (and sets newfl == NULL)
480 * If file changed: 1 and sets newfl != NULL
481 * On error: 1 and sets newfl == NULL
483 *********************************************************************/
484 int check_file_changed(const struct file_list * current,
485 const char * filename,
486 struct file_list ** newfl)
488 struct file_list *fs;
489 struct stat statbuf[1];
493 if (stat(filename, statbuf) < 0)
495 /* Error, probably file not found. */
500 && (current->lastmodified == statbuf->st_mtime)
501 && (0 == strcmp(current->filename, filename)))
506 fs = (struct file_list *)zalloc(sizeof(struct file_list));
509 /* Out of memory error */
513 fs->filename = strdup(filename);
514 fs->lastmodified = statbuf->st_mtime;
516 if (fs->filename == NULL)
518 /* Out of memory error */
527 /*********************************************************************
529 * Function : simple_read_line
531 * Description : Read a single line from a file and return it.
532 * This is basically a version of fgets() that malloc()s
533 * it's own line buffer. Note that the buffer will
534 * always be a multiple of BUFFER_SIZE bytes long.
535 * Therefore if you are going to keep the string for
536 * an extended period of time, you should probably
537 * strdup() it and free() the original, to save memory.
541 * 1 : dest = destination for newly malloc'd pointer to
542 * line data. Will be set to NULL on error.
543 * 2 : fp = File to read from
544 * 3 : newline = Standard for newlines in the file.
545 * Will be unchanged if it's value on input is not
547 * On output, may be changed from NEWLINE_UNKNOWN to
548 * actual convention in file.
550 * Returns : JB_ERR_OK on success
551 * JB_ERR_MEMORY on out-of-memory
552 * JB_ERR_FILE on EOF.
554 *********************************************************************/
555 jb_err simple_read_line(FILE *fp, char **dest, int *newline)
558 size_t buflen = BUFFER_SIZE;
562 int realnewline = NEWLINE_UNKNOWN;
564 if (NULL == (buf = malloc(buflen)))
566 return JB_ERR_MEMORY;
572 * Character codes. If you have a wierd compiler and the following are
573 * incorrect, you also need to fix NEWLINE() in loaders.h
575 #define CHAR_CR '\r' /* ASCII 13 */
576 #define CHAR_LF '\n' /* ASCII 10 */
596 else if (ch == CHAR_CR)
601 if (*newline == NEWLINE_UNKNOWN)
603 *newline = NEWLINE_DOS;
612 if (*newline == NEWLINE_UNKNOWN)
614 *newline = NEWLINE_MAC;
619 if (*newline == NEWLINE_UNKNOWN)
621 *newline = realnewline;
625 else if (ch == CHAR_LF)
629 if (*newline == NEWLINE_UNKNOWN)
631 *newline = NEWLINE_UNIX;
646 buflen += BUFFER_SIZE;
647 if (NULL == (p = realloc(buf, buflen)))
650 return JB_ERR_MEMORY;
659 /*********************************************************************
661 * Function : edit_read_line
663 * Description : Read a single non-empty line from a file and return
664 * it. Trims comments, leading and trailing whitespace
665 * and respects escaping of newline and comment char.
666 * Provides the line in 2 alternative forms: raw and
668 * - raw is the raw data read from the file. If the
669 * line is not modified, then this should be written
671 * - prefix is any comments and blank lines that were
672 * read from the file. If the line is modified, then
673 * this should be written out to the file followed
674 * by the modified data. (If this string is non-empty
675 * then it will have a newline at the end).
676 * - data is the actual data that will be parsed
677 * further by appropriate routines.
678 * On EOF, the 3 strings will all be set to NULL and
679 * 0 will be returned.
682 * 1 : fp = File to read from
683 * 2 : raw_out = destination for newly malloc'd pointer to
684 * raw line data. May be NULL if you don't want it.
685 * 3 : prefix_out = destination for newly malloc'd pointer to
686 * comments. May be NULL if you don't want it.
687 * 4 : data_out = destination for newly malloc'd pointer to
688 * line data with comments and leading/trailing spaces
689 * removed, and line continuation performed. May be
690 * NULL if you don't want it.
691 * 5 : newline = Standard for newlines in the file.
692 * On input, set to value to use or NEWLINE_UNKNOWN.
693 * On output, may be changed from NEWLINE_UNKNOWN to
694 * actual convention in file. May be NULL if you
696 * 6 : line_number = Line number in file. In "lines" as
697 * reported by a text editor, not lines containing data.
699 * Returns : JB_ERR_OK on success
700 * JB_ERR_MEMORY on out-of-memory
701 * JB_ERR_FILE on EOF.
703 *********************************************************************/
704 jb_err edit_read_line(FILE *fp,
709 unsigned long *line_number)
711 char *p; /* Temporary pointer */
712 char *linebuf; /* Line read from file */
713 char *linestart; /* Start of linebuf, usually first non-whitespace char */
714 int contflag = 0; /* Nonzero for line continuation - i.e. line ends '\' */
715 int is_empty = 1; /* Flag if not got any data yet */
716 char *raw = NULL; /* String to be stored in raw_out */
717 char *prefix = NULL; /* String to be stored in prefix_out */
718 char *data = NULL; /* String to be stored in data_out */
719 int scrapnewline; /* Used for (*newline) if newline==NULL */
720 jb_err rval = JB_ERR_OK;
723 assert(raw_out || data_out);
724 assert(newline == NULL
725 || *newline == NEWLINE_UNKNOWN
726 || *newline == NEWLINE_UNIX
727 || *newline == NEWLINE_DOS
728 || *newline == NEWLINE_MAC);
732 scrapnewline = NEWLINE_UNKNOWN;
733 newline = &scrapnewline;
736 /* Set output parameters to NULL */
750 /* Set string variables to new, empty strings. */
754 if ((raw = malloc(1)) == NULL)
756 return JB_ERR_MEMORY;
762 if ((prefix = malloc(1)) == NULL)
765 return JB_ERR_MEMORY;
771 if ((data = malloc(1)) == NULL)
775 return JB_ERR_MEMORY;
780 /* Main loop. Loop while we need more data & it's not EOF. */
782 while ( (contflag || is_empty)
783 && (JB_ERR_OK == (rval = simple_read_line(fp, &linebuf, newline))))
791 string_append(&raw,linebuf);
792 if (string_append(&raw,NEWLINE(*newline)))
797 return JB_ERR_MEMORY;
801 /* Line continuation? Trim escape and set flag. */
802 p = linebuf + strlen(linebuf) - 1;
803 contflag = ((*linebuf != '\0') && (*p == '\\'));
809 /* Trim leading spaces if we're at the start of the line */
813 /* Trim leading spaces */
814 while (*linestart && isspace((int)(unsigned char)*linestart))
820 /* Handle comment characters. */
822 while ((p = strchr(p, '#')) != NULL)
824 /* Found a comment char.. */
825 if ((p != linebuf) && (*(p-1) == '\\'))
827 /* ..and it's escaped, left-shift the line over the escape. */
829 while ((*q = *(q + 1)) != '\0')
833 /* Now scan from just after the "#". */
837 /* Real comment. Save it... */
840 /* Special case: Line only contains a comment, so all the
841 * previous whitespace is considered part of the comment.
842 * Undo the whitespace skipping, if any.
849 string_append(&prefix,p);
850 if (string_append(&prefix, NEWLINE(*newline)))
855 return JB_ERR_MEMORY;
859 /* ... and chop off the rest of the line */
862 } /* END while (there's a # character) */
864 /* Write to the buffer */
870 if (string_append(&data, linestart))
875 return JB_ERR_MEMORY;
881 } /* END while(we need more data) */
883 /* Handle simple_read_line() errors - ignore EOF */
884 if ((rval != JB_ERR_OK) && (rval != JB_ERR_FILE))
892 if (raw ? (*raw == '\0') : is_empty)
894 /* EOF and no data there. (Definition of "data" depends on whether
895 * the caller cares about "raw" or just "data").
906 /* Got at least some data */
908 /* Remove trailing whitespace */
921 *prefix_out = prefix;
940 /*********************************************************************
942 * Function : read_config_line
944 * Description : Read a single non-empty line from a file and return
945 * it. Trims comments, leading and trailing whitespace
946 * and respects escaping of newline and comment char.
949 * 1 : buf = Buffer to use.
950 * 2 : buflen = Size of buffer in bytes.
951 * 3 : fp = File to read from
952 * 4 : linenum = linenumber in file
954 * Returns : NULL on EOF or error
955 * Otherwise, returns buf.
957 *********************************************************************/
958 char *read_config_line(char *buf, size_t buflen, FILE *fp, unsigned long *linenum)
962 err = edit_read_line(fp, NULL, NULL, &buf2, NULL, linenum);
965 if (err == JB_ERR_MEMORY)
967 log_error(LOG_LEVEL_FATAL, "Out of memory loading a config file");
974 assert(strlen(buf2) + 1U < buflen);
975 strncpy(buf, buf2, buflen - 1);
977 buf[buflen - 1] = '\0';
984 /*********************************************************************
986 * Function : unload_trustfile
988 * Description : Unloads a trustfile.
991 * 1 : f = the data structure associated with the trustfile.
995 *********************************************************************/
996 static void unload_trustfile(void *f)
998 struct block_spec *cur = (struct block_spec *)f;
999 struct block_spec *next;
1005 free_url_spec(cur->url);
1014 #ifdef FEATURE_GRACEFUL_TERMINATION
1015 /*********************************************************************
1017 * Function : unload_current_trust_file
1019 * Description : Unloads current trust file - reset to state at
1020 * beginning of program.
1026 *********************************************************************/
1027 void unload_current_trust_file(void)
1029 if (current_trustfile)
1031 current_trustfile->unloader = unload_trustfile;
1032 current_trustfile = NULL;
1035 #endif /* FEATURE_GRACEFUL_TERMINATION */
1038 /*********************************************************************
1040 * Function : load_trustfile
1042 * Description : Read and parse a trustfile and add to files list.
1045 * 1 : csp = Current client state (buffers, headers, etc...)
1047 * Returns : 0 => Ok, everything else is an error.
1049 *********************************************************************/
1050 int load_trustfile(struct client_state *csp)
1054 struct block_spec *b, *bl;
1055 struct url_spec **tl;
1057 char buf[BUFFER_SIZE], *p, *q;
1058 int reject, trusted;
1059 struct file_list *fs;
1060 unsigned long linenum = 0;
1062 if (!check_file_changed(current_trustfile, csp->config->trustfile, &fs))
1064 /* No need to load */
1067 csp->tlist = current_trustfile;
1073 goto load_trustfile_error;
1076 fs->f = bl = (struct block_spec *)zalloc(sizeof(*bl));
1079 goto load_trustfile_error;
1082 if ((fp = fopen(csp->config->trustfile, "r")) == NULL)
1084 goto load_trustfile_error;
1087 tl = csp->config->trust_list;
1089 while (read_config_line(buf, sizeof(buf), fp, &linenum) != NULL)
1105 while ((*p++ = *q++) != '\0')
1111 /* skip blank lines */
1117 /* allocate a new node */
1118 if ((b = zalloc(sizeof(*b))) == NULL)
1121 goto load_trustfile_error;
1124 /* add it to the list */
1130 /* Save the URL pattern */
1131 if (create_url_spec(b->url, buf))
1134 goto load_trustfile_error;
1138 * save a pointer to URL's spec in the list of trusted URL's, too
1143 /* FIXME BUFFER OVERFLOW if >=64 entries */
1151 /* the old one is now obsolete */
1152 if (current_trustfile)
1154 current_trustfile->unloader = unload_trustfile;
1157 fs->next = files->next;
1159 current_trustfile = fs;
1168 load_trustfile_error:
1169 log_error(LOG_LEVEL_FATAL, "can't load trustfile '%s': %E",
1170 csp->config->trustfile);
1174 #endif /* def FEATURE_TRUST */
1177 /*********************************************************************
1179 * Function : unload_re_filterfile
1181 * Description : Unload the re_filter list by freeing all chained
1182 * re_filterfile specs and their data.
1185 * 1 : f = the data structure associated with the filterfile.
1189 *********************************************************************/
1190 static void unload_re_filterfile(void *f)
1192 struct re_filterfile_spec *a, *b = (struct re_filterfile_spec *)f;
1198 destroy_list(b->patterns);
1199 pcrs_free_joblist(b->joblist);
1201 freez(b->description);
1211 #ifdef FEATURE_GRACEFUL_TERMINATION
1212 /*********************************************************************
1214 * Function : unload_current_re_filterfile
1216 * Description : Unloads current re_filter file - reset to state at
1217 * beginning of program.
1223 *********************************************************************/
1224 void unload_current_re_filterfile(void)
1226 if (current_re_filterfile)
1228 current_re_filterfile->unloader = unload_re_filterfile;
1229 current_re_filterfile = NULL;
1235 /*********************************************************************
1237 * Function : load_re_filterfile
1239 * Description : Load the re_filterfile.
1240 * Generate a chained list of re_filterfile_spec's from
1241 * the "FILTER: " blocks, compiling all their substitutions
1242 * into chained lists of pcrs_job structs.
1245 * 1 : csp = Current client state (buffers, headers, etc...)
1247 * Returns : 0 => Ok, everything else is an error.
1249 *********************************************************************/
1250 int load_re_filterfile(struct client_state *csp)
1254 struct re_filterfile_spec *new_bl, *bl = NULL;
1255 struct file_list *fs;
1257 char buf[BUFFER_SIZE];
1259 unsigned long linenum = 0;
1260 pcrs_job *dummy, *lastjob = NULL;
1263 * No need to reload if unchanged
1265 if (!check_file_changed(current_re_filterfile, csp->config->re_filterfile, &fs))
1269 csp->rlist = current_re_filterfile;
1275 goto load_re_filterfile_error;
1279 * Open the file or fail
1281 if ((fp = fopen(csp->config->re_filterfile, "r")) == NULL)
1283 goto load_re_filterfile_error;
1289 while (read_config_line(buf, sizeof(buf), fp, &linenum) != NULL)
1292 * If this is the head of a new filter block, make it a
1293 * re_filterfile spec of its own and chain it to the list:
1295 if (strncmp(buf, "FILTER:", 7) == 0)
1297 new_bl = (struct re_filterfile_spec *)zalloc(sizeof(*bl));
1300 goto load_re_filterfile_error;
1303 new_bl->name = chomp(buf + 7);
1305 if (NULL != (new_bl->description = strchr(new_bl->name, ' ')))
1307 *new_bl->description++ = '\0';
1308 new_bl->description = strdup(chomp(new_bl->description));
1312 new_bl->description = strdup("No description available for this filter");
1315 new_bl->name = strdup(chomp(new_bl->name));
1318 * If this is the first filter block, chain it
1319 * to the file_list rather than its (nonexistant)
1332 log_error(LOG_LEVEL_RE_FILTER, "Reading in filter \"%s\" (\"%s\")", bl->name, bl->description);
1338 * Else, save the expression, make it a pcrs_job
1339 * and chain it into the current filter's joblist
1343 enlist(bl->patterns, buf);
1345 if ((dummy = pcrs_compile_command(buf, &error)) == NULL)
1347 log_error(LOG_LEVEL_ERROR,
1348 "Adding re_filter job %s to filter %s failed with error %d.", buf, bl->name, error);
1353 if (bl->joblist == NULL)
1355 bl->joblist = dummy;
1359 lastjob->next = dummy;
1362 log_error(LOG_LEVEL_RE_FILTER, "Adding re_filter job %s to filter %s succeeded.", buf, bl->name);
1367 log_error(LOG_LEVEL_ERROR, "Ignoring job %s outside filter block in %s, line %d", buf, csp->config->re_filterfile, linenum);
1374 * Schedule the now-obsolete old data for unloading
1376 if ( NULL != current_re_filterfile )
1378 current_re_filterfile->unloader = unload_re_filterfile;
1382 * Chain this file into the global list of loaded files
1384 fs->next = files->next;
1386 current_re_filterfile = fs;
1395 load_re_filterfile_error:
1396 log_error(LOG_LEVEL_FATAL, "can't load re_filterfile '%s': %E",
1397 csp->config->re_filterfile);
1403 /*********************************************************************
1405 * Function : add_loader
1407 * Description : Called from `load_config'. Called once for each input
1408 * file found in config.
1411 * 1 : loader = pointer to a function that can parse and load
1412 * the appropriate config file.
1413 * 2 : config = The configuration_spec to add the loader to.
1417 *********************************************************************/
1418 void add_loader(int (*loader)(struct client_state *),
1419 struct configuration_spec * config)
1423 for (i=0; i < NLOADERS; i++)
1425 if (config->loaders[i] == NULL)
1427 config->loaders[i] = loader;
1435 /*********************************************************************
1437 * Function : run_loader
1439 * Description : Called from `load_config' and `listen_loop'. This
1440 * function keeps the "csp" current with any file mods
1441 * since the last loop. If a file is unchanged, the
1442 * loader functions do NOT reload the file.
1445 * 1 : csp = Current client state (buffers, headers, etc...)
1446 * Must be non-null. Reads: "csp->config"
1447 * Writes: various data members.
1449 * Returns : 0 => Ok, everything else is an error.
1451 *********************************************************************/
1452 int run_loader(struct client_state *csp)
1457 for (i=0; i < NLOADERS; i++)
1459 if (csp->config->loaders[i] == NULL)
1463 ret |= (csp->config->loaders[i])(csp);