1 const char loaders_rcs[] = "$Id: loaders.c,v 1.50.2.8 2006/01/30 15:16:25 david__schmidt 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.8 2006/01/30 15:16:25 david__schmidt
39 * Remove a little residual debugging info
41 * Revision 1.50.2.7 2006/01/29 23:10:56 david__schmidt
42 * Multiple filter file support
44 * Revision 1.50.2.6 2003/10/24 10:17:54 oes
45 * Nit: Allowed tabs as separators in filter headings
47 * Revision 1.50.2.5 2003/05/08 15:19:15 oes
48 * sweep: Made loop structure of sweep step mirror that of mark step
50 * Revision 1.50.2.4 2003/05/06 15:57:12 oes
51 * Bugfix: Update last_active pointer in sweep() before
52 * leaving an active client. Closes bugs #724395, #727882
54 * Revision 1.50.2.3 2002/11/20 17:12:30 oes
55 * Ooops, forgot one change.
57 * Revision 1.50.2.2 2002/11/20 14:38:15 oes
58 * Fixed delayed/incomplete freeing of client resources and
59 * simplified loop structure in sweep.
60 * Thanks to Oliver Stoeneberg for the hint.
62 * Revision 1.50.2.1 2002/07/26 15:19:24 oes
63 * - PCRS jobs now chained in order of appearance. Previous
64 * reverse chaining was counter-intuitive.
65 * - Changed loglevel of PCRS job compile errors to
68 * Revision 1.50 2002/04/24 02:12:16 oes
69 * Jon's multiple AF patch: Sweep now takes care of all AFs
71 * Revision 1.49 2002/04/19 16:53:25 jongfoster
72 * Optimize away a function call by using an equivalent macro
74 * Revision 1.48 2002/04/05 00:56:09 gliptak
75 * Correcting typo to clean up on realloc failure
77 * Revision 1.47 2002/03/26 22:29:55 swa
78 * we have a new homepage!
80 * Revision 1.46 2002/03/24 13:25:43 swa
81 * name change related issues
83 * Revision 1.45 2002/03/16 23:54:06 jongfoster
84 * Adding graceful termination feature, to help look for memory leaks.
85 * If you enable this (which, by design, has to be done by hand
86 * editing config.h) and then go to http://i.j.b/die, then the program
87 * will exit cleanly after the *next* request. It should free all the
88 * memory that was used.
90 * Revision 1.44 2002/03/16 21:51:00 jongfoster
93 * Revision 1.43 2002/03/16 20:28:34 oes
94 * Added descriptions to the filters so users will know what they select in the cgi editor
96 * Revision 1.42 2002/03/13 00:27:05 jongfoster
99 * Revision 1.41 2002/03/12 01:42:50 oes
100 * Introduced modular filters
102 * Revision 1.40 2002/03/08 17:46:04 jongfoster
103 * Fixing int/size_t warnings
105 * Revision 1.39 2002/03/07 03:46:17 oes
106 * Fixed compiler warnings
108 * Revision 1.38 2002/03/06 22:54:35 jongfoster
109 * Automated function-comment nitpicking.
111 * Revision 1.37 2002/03/03 15:07:49 oes
112 * Re-enabled automatic config reloading
114 * Revision 1.36 2002/01/22 23:46:18 jongfoster
115 * Moving edit_read_line() and simple_read_line() to loaders.c, and
116 * extending them to support reading MS-DOS, Mac and UNIX style files
119 * Modifying read_config_line() (without changing it's prototype) to
120 * be a trivial wrapper for edit_read_line(). This means that we have
121 * one function to read a line and handle comments, which is common
122 * between the initialization code and the edit interface.
124 * Revision 1.35 2002/01/17 21:03:08 jongfoster
125 * Moving all our URL and URL pattern parsing code to urlmatch.c.
127 * Renaming free_url to free_url_spec, since it frees a struct url_spec.
129 * Revision 1.34 2001/12/30 14:07:32 steudten
130 * - Add signal handling (unix)
131 * - Add SIGHUP handler (unix)
132 * - Add creation of pidfile (unix)
133 * - Add action 'top' in rc file (RH)
134 * - Add entry 'SIGNALS' to manpage
135 * - Add exit message to logfile (unix)
137 * Revision 1.33 2001/11/13 00:16:38 jongfoster
138 * Replacing references to malloc.h with the standard stdlib.h
139 * (See ANSI or K&R 2nd Ed)
141 * Revision 1.32 2001/11/07 00:02:13 steudten
142 * Add line number in error output for lineparsing for
143 * actionsfile and configfile.
144 * Special handling for CLF added.
146 * Revision 1.31 2001/10/26 17:39:01 oes
147 * Removed csp->referrer
148 * Moved ijb_isspace and ijb_tolower to project.h
150 * Revision 1.30 2001/10/25 03:40:48 david__schmidt
151 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
152 * threads to call select() simultaneously. So, it's time to do a real, live,
153 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
154 * (native). Both versions will work, but using __OS2__ offers multi-threading.
156 * Revision 1.29 2001/10/23 21:38:53 jongfoster
157 * Adding error-checking to create_url_spec()
159 * Revision 1.28 2001/10/07 15:40:39 oes
160 * Replaced 6 boolean members of csp with one bitmap (csp->flags)
162 * Revision 1.27 2001/09/22 16:36:59 jongfoster
163 * Removing unused parameter fs from read_config_line()
165 * Revision 1.26 2001/09/22 14:05:22 jongfoster
166 * Bugfix: Multiple escaped "#" characters in a configuration
167 * file are now permitted.
168 * Also removing 3 unused headers.
170 * Revision 1.25 2001/09/13 22:44:03 jongfoster
171 * Adding {} to an if statement
173 * Revision 1.24 2001/07/30 22:08:36 jongfoster
174 * Tidying up #defines:
175 * - All feature #defines are now of the form FEATURE_xxx
176 * - Permanently turned off WIN_GUI_EDIT
177 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
179 * Revision 1.23 2001/07/20 15:51:54 oes
180 * Fixed indentation of prepocessor commands
182 * Revision 1.22 2001/07/20 15:16:17 haroon
183 * - per Guy's suggestion, added a while loop in sweep() to catch not just
184 * the last inactive CSP but all other consecutive inactive CSPs after that
187 * Revision 1.21 2001/07/18 17:26:24 oes
188 * Changed to conform to new pcrs interface
190 * Revision 1.20 2001/07/17 13:07:01 oes
191 * Fixed segv when last line in config files
192 * lacked a terminating (\r)\n
194 * Revision 1.19 2001/07/13 14:01:54 oes
195 * Removed all #ifdef PCRS
197 * Revision 1.18 2001/06/29 21:45:41 oes
198 * Indentation, CRLF->LF, Tab-> Space
200 * Revision 1.17 2001/06/29 13:31:51 oes
203 * Revision 1.16 2001/06/09 10:55:28 jongfoster
204 * Changing BUFSIZ ==> BUFFER_SIZE
206 * Revision 1.15 2001/06/07 23:14:14 jongfoster
207 * Removing ACL and forward file loaders - these
208 * files have been merged into the config file.
209 * Cosmetic: Moving unloader funcs next to their
210 * respective loader funcs
212 * Revision 1.14 2001/06/01 03:27:04 oes
213 * Fixed line continuation problem
215 * Revision 1.13 2001/05/31 21:28:49 jongfoster
216 * Removed all permissionsfile code - it's now called the actions
217 * file, and (almost) all the code is in actions.c
219 * Revision 1.12 2001/05/31 17:32:31 oes
221 * - Enhanced domain part globbing with infix and prefix asterisk
222 * matching and optional unanchored operation
224 * Revision 1.11 2001/05/29 23:25:24 oes
226 * - load_config_line() and load_permissions_file() now use chomp()
228 * Revision 1.10 2001/05/29 09:50:24 jongfoster
229 * Unified blocklist/imagelist/permissionslist.
230 * File format is still under discussion, but the internal changes
233 * Also modified interceptor behaviour:
234 * - We now intercept all URLs beginning with one of the following
235 * prefixes (and *only* these prefixes):
237 * * http://ijbswa.sf.net/config/
238 * * http://ijbswa.sourceforge.net/config/
239 * - New interceptors "home page" - go to http://i.j.b/ to see it.
240 * - Internal changes so that intercepted and fast redirect pages
241 * are not replaced with an image.
242 * - Interceptors now have the option to send a binary page direct
243 * to the client. (i.e. ijb-send-banner uses this)
244 * - Implemented show-url-info interceptor. (Which is why I needed
245 * the above interceptors changes - a typical URL is
246 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
247 * The previous mechanism would not have intercepted that, and
248 * if it had been intercepted then it then it would have replaced
251 * Revision 1.9 2001/05/26 17:12:07 jongfoster
252 * Fatal errors loading configuration files now give better error messages.
254 * Revision 1.8 2001/05/26 00:55:20 jongfoster
255 * Removing duplicated code. load_forwardfile() now uses create_url_spec()
257 * Revision 1.7 2001/05/26 00:28:36 jongfoster
258 * Automatic reloading of config file.
259 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
260 * Most of the global variables have been moved to a new
261 * struct configuration_spec, accessed through csp->config->globalname
262 * Most of the globals remaining are used by the Win32 GUI.
264 * Revision 1.6 2001/05/23 12:27:33 oes
266 * Fixed ugly indentation of my last changes
268 * Revision 1.5 2001/05/23 10:39:05 oes
269 * - Added support for escaping the comment character
270 * in config files by a backslash
271 * - Added support for line continuation in config
273 * - Fixed a buffer overflow bug with long config lines
275 * Revision 1.4 2001/05/22 18:56:28 oes
278 * Revision 1.3 2001/05/20 01:21:20 jongfoster
279 * Version 2.9.4 checkin.
280 * - Merged popupfile and cookiefile, and added control over PCRS
281 * filtering, in new "permissionsfile".
282 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
283 * file error you now get a message box (in the Win32 GUI) rather
284 * than the program exiting with no explanation.
285 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
287 * - Removed tabs from "config"
288 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
289 * - Bumped up version number.
291 * Revision 1.2 2001/05/17 23:01:01 oes
292 * - Cleaned CRLF's from the sources and related files
294 * Revision 1.1.1.1 2001/05/15 13:58:59 oes
295 * Initial import of version 2.9.3 source tree
298 *********************************************************************/
305 #include <sys/types.h>
308 #include <sys/stat.h>
312 #if !defined(_WIN32) && !defined(__OS2__)
322 #include "miscutil.h"
325 #include "urlmatch.h"
327 const char loaders_h_rcs[] = LOADERS_H_VERSION;
330 * Currently active files.
331 * These are also entered in the main linked list of files.
335 static struct file_list *current_trustfile = NULL;
336 #endif /* def FEATURE_TRUST */
338 static int load_one_re_filterfile(struct client_state *csp, int fileid);
340 static struct file_list *current_re_filterfile[MAX_AF_FILES] = {
341 NULL, NULL, NULL, NULL, NULL,
342 NULL, NULL, NULL, NULL, NULL
347 /*********************************************************************
351 * Description : Basically a mark and sweep garbage collector, it is run
352 * (by the parent thread) every once in a while to reclaim memory.
354 * It uses a mark and sweep strategy:
355 * 1) mark all files as inactive
357 * 2) check with each client:
358 * if it is active, mark its files as active
359 * if it is inactive, free its resources
361 * 3) free the resources of all of the files that
362 * are still marked as inactive (and are obsolete).
364 * N.B. files that are not obsolete don't have an unloader defined.
370 *********************************************************************/
373 struct file_list *fl, *nfl;
374 struct client_state *csp, *last_active;
377 /* clear all of the file's active flags */
378 for ( fl = files->next; NULL != fl; fl = fl->next )
383 last_active = clients;
388 if (csp->flags & CSP_FLAG_ACTIVE)
390 /* Mark this client's files as active */
393 * Always have a configuration file.
394 * (Also note the slightly non-standard extra
397 csp->config->config_file_list->active = 1;
402 for (i = 0; i < MAX_AF_FILES; i++)
404 if (csp->actions_list[i])
406 csp->actions_list[i]->active = 1;
413 for (i = 0; i < MAX_AF_FILES; i++)
417 csp->rlist[i]->active = 1;
427 csp->tlist->active = 1;
429 #endif /* def FEATURE_TRUST */
437 * This client is not active. Free its resources.
440 last_active->next = csp->next;
442 freez(csp->ip_addr_str);
443 freez(csp->my_ip_addr_str);
444 freez(csp->my_hostname);
445 freez(csp->x_forwarded);
446 freez(csp->iob->buf);
448 free_http_request(csp->http);
450 destroy_list(csp->headers);
451 destroy_list(csp->cookie_list);
453 free_current_action(csp->action);
455 #ifdef FEATURE_STATISTICS
457 if (csp->flags & CSP_FLAG_REJECTED)
461 #endif /* def FEATURE_STATISTICS */
465 csp = last_active->next;
474 if ( ( 0 == fl->active ) && ( NULL != fl->unloader ) )
476 nfl->next = fl->next;
478 (fl->unloader)(fl->f);
495 /*********************************************************************
497 * Function : check_file_changed
499 * Description : Helper function to check if a file needs reloading.
500 * If "current" is still current, return it. Otherwise
501 * allocates a new (zeroed) "struct file_list", fills
502 * in the disk file name and timestamp, and returns it.
505 * 1 : current = The file_list currently being used - will
506 * be checked to see if it is out of date.
507 * May be NULL (which is treated as out of
509 * 2 : filename = Name of file to check.
510 * 3 : newfl = New file list. [Output only]
511 * This will be set to NULL, OR a struct
512 * file_list newly allocated on the
513 * heap, with the filename and lastmodified
514 * fields filled, and all others zeroed.
516 * Returns : If file unchanged: 0 (and sets newfl == NULL)
517 * If file changed: 1 and sets newfl != NULL
518 * On error: 1 and sets newfl == NULL
520 *********************************************************************/
521 int check_file_changed(const struct file_list * current,
522 const char * filename,
523 struct file_list ** newfl)
525 struct file_list *fs;
526 struct stat statbuf[1];
530 if (stat(filename, statbuf) < 0)
532 /* Error, probably file not found. */
537 && (current->lastmodified == statbuf->st_mtime)
538 && (0 == strcmp(current->filename, filename)))
543 fs = (struct file_list *)zalloc(sizeof(struct file_list));
546 /* Out of memory error */
551 fs->filename = strdup(filename);
552 fs->lastmodified = statbuf->st_mtime;
554 if (fs->filename == NULL)
556 /* Out of memory error */
565 /*********************************************************************
567 * Function : simple_read_line
569 * Description : Read a single line from a file and return it.
570 * This is basically a version of fgets() that malloc()s
571 * it's own line buffer. Note that the buffer will
572 * always be a multiple of BUFFER_SIZE bytes long.
573 * Therefore if you are going to keep the string for
574 * an extended period of time, you should probably
575 * strdup() it and free() the original, to save memory.
579 * 1 : dest = destination for newly malloc'd pointer to
580 * line data. Will be set to NULL on error.
581 * 2 : fp = File to read from
582 * 3 : newline = Standard for newlines in the file.
583 * Will be unchanged if it's value on input is not
585 * On output, may be changed from NEWLINE_UNKNOWN to
586 * actual convention in file.
588 * Returns : JB_ERR_OK on success
589 * JB_ERR_MEMORY on out-of-memory
590 * JB_ERR_FILE on EOF.
592 *********************************************************************/
593 jb_err simple_read_line(FILE *fp, char **dest, int *newline)
596 size_t buflen = BUFFER_SIZE;
600 int realnewline = NEWLINE_UNKNOWN;
602 if (NULL == (buf = malloc(buflen)))
604 return JB_ERR_MEMORY;
610 * Character codes. If you have a wierd compiler and the following are
611 * incorrect, you also need to fix NEWLINE() in loaders.h
613 #define CHAR_CR '\r' /* ASCII 13 */
614 #define CHAR_LF '\n' /* ASCII 10 */
634 else if (ch == CHAR_CR)
639 if (*newline == NEWLINE_UNKNOWN)
641 *newline = NEWLINE_DOS;
650 if (*newline == NEWLINE_UNKNOWN)
652 *newline = NEWLINE_MAC;
657 if (*newline == NEWLINE_UNKNOWN)
659 *newline = realnewline;
663 else if (ch == CHAR_LF)
667 if (*newline == NEWLINE_UNKNOWN)
669 *newline = NEWLINE_UNIX;
684 buflen += BUFFER_SIZE;
685 if (NULL == (p = realloc(buf, buflen)))
688 return JB_ERR_MEMORY;
697 /*********************************************************************
699 * Function : edit_read_line
701 * Description : Read a single non-empty line from a file and return
702 * it. Trims comments, leading and trailing whitespace
703 * and respects escaping of newline and comment char.
704 * Provides the line in 2 alternative forms: raw and
706 * - raw is the raw data read from the file. If the
707 * line is not modified, then this should be written
709 * - prefix is any comments and blank lines that were
710 * read from the file. If the line is modified, then
711 * this should be written out to the file followed
712 * by the modified data. (If this string is non-empty
713 * then it will have a newline at the end).
714 * - data is the actual data that will be parsed
715 * further by appropriate routines.
716 * On EOF, the 3 strings will all be set to NULL and
717 * 0 will be returned.
720 * 1 : fp = File to read from
721 * 2 : raw_out = destination for newly malloc'd pointer to
722 * raw line data. May be NULL if you don't want it.
723 * 3 : prefix_out = destination for newly malloc'd pointer to
724 * comments. May be NULL if you don't want it.
725 * 4 : data_out = destination for newly malloc'd pointer to
726 * line data with comments and leading/trailing spaces
727 * removed, and line continuation performed. May be
728 * NULL if you don't want it.
729 * 5 : newline = Standard for newlines in the file.
730 * On input, set to value to use or NEWLINE_UNKNOWN.
731 * On output, may be changed from NEWLINE_UNKNOWN to
732 * actual convention in file. May be NULL if you
734 * 6 : line_number = Line number in file. In "lines" as
735 * reported by a text editor, not lines containing data.
737 * Returns : JB_ERR_OK on success
738 * JB_ERR_MEMORY on out-of-memory
739 * JB_ERR_FILE on EOF.
741 *********************************************************************/
742 jb_err edit_read_line(FILE *fp,
747 unsigned long *line_number)
749 char *p; /* Temporary pointer */
750 char *linebuf; /* Line read from file */
751 char *linestart; /* Start of linebuf, usually first non-whitespace char */
752 int contflag = 0; /* Nonzero for line continuation - i.e. line ends '\' */
753 int is_empty = 1; /* Flag if not got any data yet */
754 char *raw = NULL; /* String to be stored in raw_out */
755 char *prefix = NULL; /* String to be stored in prefix_out */
756 char *data = NULL; /* String to be stored in data_out */
757 int scrapnewline; /* Used for (*newline) if newline==NULL */
758 jb_err rval = JB_ERR_OK;
761 assert(raw_out || data_out);
762 assert(newline == NULL
763 || *newline == NEWLINE_UNKNOWN
764 || *newline == NEWLINE_UNIX
765 || *newline == NEWLINE_DOS
766 || *newline == NEWLINE_MAC);
770 scrapnewline = NEWLINE_UNKNOWN;
771 newline = &scrapnewline;
774 /* Set output parameters to NULL */
788 /* Set string variables to new, empty strings. */
792 if ((raw = malloc(1)) == NULL)
794 return JB_ERR_MEMORY;
800 if ((prefix = malloc(1)) == NULL)
803 return JB_ERR_MEMORY;
809 if ((data = malloc(1)) == NULL)
813 return JB_ERR_MEMORY;
818 /* Main loop. Loop while we need more data & it's not EOF. */
820 while ( (contflag || is_empty)
821 && (JB_ERR_OK == (rval = simple_read_line(fp, &linebuf, newline))))
829 string_append(&raw,linebuf);
830 if (string_append(&raw,NEWLINE(*newline)))
835 return JB_ERR_MEMORY;
839 /* Line continuation? Trim escape and set flag. */
840 p = linebuf + strlen(linebuf) - 1;
841 contflag = ((*linebuf != '\0') && (*p == '\\'));
847 /* Trim leading spaces if we're at the start of the line */
851 /* Trim leading spaces */
852 while (*linestart && isspace((int)(unsigned char)*linestart))
858 /* Handle comment characters. */
860 while ((p = strchr(p, '#')) != NULL)
862 /* Found a comment char.. */
863 if ((p != linebuf) && (*(p-1) == '\\'))
865 /* ..and it's escaped, left-shift the line over the escape. */
867 while ((*q = *(q + 1)) != '\0')
871 /* Now scan from just after the "#". */
875 /* Real comment. Save it... */
878 /* Special case: Line only contains a comment, so all the
879 * previous whitespace is considered part of the comment.
880 * Undo the whitespace skipping, if any.
887 string_append(&prefix,p);
888 if (string_append(&prefix, NEWLINE(*newline)))
893 return JB_ERR_MEMORY;
897 /* ... and chop off the rest of the line */
900 } /* END while (there's a # character) */
902 /* Write to the buffer */
908 if (string_append(&data, linestart))
913 return JB_ERR_MEMORY;
919 } /* END while(we need more data) */
921 /* Handle simple_read_line() errors - ignore EOF */
922 if ((rval != JB_ERR_OK) && (rval != JB_ERR_FILE))
930 if (raw ? (*raw == '\0') : is_empty)
932 /* EOF and no data there. (Definition of "data" depends on whether
933 * the caller cares about "raw" or just "data").
944 /* Got at least some data */
946 /* Remove trailing whitespace */
959 *prefix_out = prefix;
978 /*********************************************************************
980 * Function : read_config_line
982 * Description : Read a single non-empty line from a file and return
983 * it. Trims comments, leading and trailing whitespace
984 * and respects escaping of newline and comment char.
987 * 1 : buf = Buffer to use.
988 * 2 : buflen = Size of buffer in bytes.
989 * 3 : fp = File to read from
990 * 4 : linenum = linenumber in file
992 * Returns : NULL on EOF or error
993 * Otherwise, returns buf.
995 *********************************************************************/
996 char *read_config_line(char *buf, size_t buflen, FILE *fp, unsigned long *linenum)
1000 err = edit_read_line(fp, NULL, NULL, &buf2, NULL, linenum);
1003 if (err == JB_ERR_MEMORY)
1005 log_error(LOG_LEVEL_FATAL, "Out of memory loading a config file");
1012 assert(strlen(buf2) + 1U < buflen);
1013 strncpy(buf, buf2, buflen - 1);
1015 buf[buflen - 1] = '\0';
1021 #ifdef FEATURE_TRUST
1022 /*********************************************************************
1024 * Function : unload_trustfile
1026 * Description : Unloads a trustfile.
1029 * 1 : f = the data structure associated with the trustfile.
1033 *********************************************************************/
1034 static void unload_trustfile(void *f)
1036 struct block_spec *cur = (struct block_spec *)f;
1037 struct block_spec *next;
1043 free_url_spec(cur->url);
1052 #ifdef FEATURE_GRACEFUL_TERMINATION
1053 /*********************************************************************
1055 * Function : unload_current_trust_file
1057 * Description : Unloads current trust file - reset to state at
1058 * beginning of program.
1064 *********************************************************************/
1065 void unload_current_trust_file(void)
1067 if (current_trustfile)
1069 current_trustfile->unloader = unload_trustfile;
1070 current_trustfile = NULL;
1073 #endif /* FEATURE_GRACEFUL_TERMINATION */
1076 /*********************************************************************
1078 * Function : load_trustfile
1080 * Description : Read and parse a trustfile and add to files list.
1083 * 1 : csp = Current client state (buffers, headers, etc...)
1085 * Returns : 0 => Ok, everything else is an error.
1087 *********************************************************************/
1088 int load_trustfile(struct client_state *csp)
1092 struct block_spec *b, *bl;
1093 struct url_spec **tl;
1095 char buf[BUFFER_SIZE], *p, *q;
1096 int reject, trusted;
1097 struct file_list *fs;
1098 unsigned long linenum = 0;
1100 if (!check_file_changed(current_trustfile, csp->config->trustfile, &fs))
1102 /* No need to load */
1105 csp->tlist = current_trustfile;
1111 goto load_trustfile_error;
1114 fs->f = bl = (struct block_spec *)zalloc(sizeof(*bl));
1117 goto load_trustfile_error;
1120 if ((fp = fopen(csp->config->trustfile, "r")) == NULL)
1122 goto load_trustfile_error;
1125 tl = csp->config->trust_list;
1127 while (read_config_line(buf, sizeof(buf), fp, &linenum) != NULL)
1143 while ((*p++ = *q++) != '\0')
1149 /* skip blank lines */
1155 /* allocate a new node */
1156 if ((b = zalloc(sizeof(*b))) == NULL)
1159 goto load_trustfile_error;
1162 /* add it to the list */
1168 /* Save the URL pattern */
1169 if (create_url_spec(b->url, buf))
1172 goto load_trustfile_error;
1176 * save a pointer to URL's spec in the list of trusted URL's, too
1181 /* FIXME BUFFER OVERFLOW if >=64 entries */
1189 /* the old one is now obsolete */
1190 if (current_trustfile)
1192 current_trustfile->unloader = unload_trustfile;
1195 fs->next = files->next;
1197 current_trustfile = fs;
1206 load_trustfile_error:
1207 log_error(LOG_LEVEL_FATAL, "can't load trustfile '%s': %E",
1208 csp->config->trustfile);
1212 #endif /* def FEATURE_TRUST */
1215 /*********************************************************************
1217 * Function : unload_re_filterfile
1219 * Description : Unload the re_filter list by freeing all chained
1220 * re_filterfile specs and their data.
1223 * 1 : f = the data structure associated with the filterfile.
1227 *********************************************************************/
1228 static void unload_re_filterfile(void *f)
1230 struct re_filterfile_spec *a, *b = (struct re_filterfile_spec *)f;
1236 destroy_list(b->patterns);
1237 pcrs_free_joblist(b->joblist);
1239 freez(b->description);
1249 #ifdef FEATURE_GRACEFUL_TERMINATION
1250 /*********************************************************************
1252 * Function : unload_current_re_filterfile
1254 * Description : Unloads current re_filter file - reset to state at
1255 * beginning of program.
1261 *********************************************************************/
1262 void unload_current_re_filterfile(void)
1266 for (i = 0; i < MAX_AF_FILES; i++)
1268 if (current_re_filterfile[i])
1270 current_re_filterfile[i]->unloader = unload_re_filterfile;
1271 current_re_filterfile[i] = NULL;
1278 /*********************************************************************
1280 * Function : load_re_filterfile
1282 * Description : Load the re_filterfile.
1283 * Generate a chained list of re_filterfile_spec's from
1284 * the "FILTER: " blocks, compiling all their substitutions
1285 * into chained lists of pcrs_job structs.
1288 * 1 : csp = Current client state (buffers, headers, etc...)
1290 * Returns : 0 => Ok, everything else is an error.
1292 *********************************************************************/
1293 int load_re_filterfile(struct client_state *csp)
1298 for (i = 0; i < MAX_AF_FILES; i++)
1300 if (csp->config->re_filterfile[i])
1302 result = load_one_re_filterfile(csp, i);
1308 else if (current_re_filterfile[i])
1310 current_re_filterfile[i]->unloader = unload_re_filterfile;
1311 current_re_filterfile[i] = NULL;
1318 /*********************************************************************
1320 * Function : load_one_re_filterfile
1322 * Description : Load a re_filterfile.
1323 * Generate a chained list of re_filterfile_spec's from
1324 * the "FILTER: " blocks, compiling all their substitutions
1325 * into chained lists of pcrs_job structs.
1328 * 1 : csp = Current client state (buffers, headers, etc...)
1330 * Returns : 0 => Ok, everything else is an error.
1332 *********************************************************************/
1333 int load_one_re_filterfile(struct client_state *csp, int fileid)
1337 struct re_filterfile_spec *new_bl, *bl = NULL;
1338 struct file_list *fs;
1340 char buf[BUFFER_SIZE];
1342 unsigned long linenum = 0;
1343 pcrs_job *dummy, *lastjob = NULL;
1346 * No need to reload if unchanged
1348 if (!check_file_changed(current_re_filterfile[fileid], csp->config->re_filterfile[fileid], &fs))
1352 csp->rlist[fileid] = current_re_filterfile[fileid];
1358 goto load_re_filterfile_error;
1362 * Open the file or fail
1364 if ((fp = fopen(csp->config->re_filterfile[fileid], "r")) == NULL)
1366 goto load_re_filterfile_error;
1372 while (read_config_line(buf, sizeof(buf), fp, &linenum) != NULL)
1375 * If this is the head of a new filter block, make it a
1376 * re_filterfile spec of its own and chain it to the list:
1378 if (strncmp(buf, "FILTER:", 7) == 0)
1380 new_bl = (struct re_filterfile_spec *)zalloc(sizeof(*bl));
1383 goto load_re_filterfile_error;
1386 new_bl->name = chomp(buf + 7);
1388 if (NULL != (new_bl->description = strpbrk(new_bl->name, " \t")))
1390 *new_bl->description++ = '\0';
1391 new_bl->description = strdup(chomp(new_bl->description));
1395 new_bl->description = strdup("No description available for this filter");
1398 new_bl->name = strdup(chomp(new_bl->name));
1401 * If this is the first filter block, chain it
1402 * to the file_list rather than its (nonexistant)
1415 log_error(LOG_LEVEL_RE_FILTER, "Reading in filter \"%s\" (\"%s\")", bl->name, bl->description);
1421 * Else, save the expression, make it a pcrs_job
1422 * and chain it into the current filter's joblist
1426 enlist(bl->patterns, buf);
1428 if ((dummy = pcrs_compile_command(buf, &error)) == NULL)
1430 log_error(LOG_LEVEL_ERROR,
1431 "Adding re_filter job %s to filter %s failed with error %d.", buf, bl->name, error);
1436 if (bl->joblist == NULL)
1438 bl->joblist = dummy;
1442 lastjob->next = dummy;
1445 log_error(LOG_LEVEL_RE_FILTER, "Adding re_filter job %s to filter %s succeeded.", buf, bl->name);
1450 log_error(LOG_LEVEL_ERROR, "Ignoring job %s outside filter block in %s, line %d", buf, csp->config->re_filterfile, linenum);
1457 * Schedule the now-obsolete old data for unloading
1459 if ( NULL != current_re_filterfile[fileid] )
1461 current_re_filterfile[fileid]->unloader = unload_re_filterfile;
1465 * Chain this file into the global list of loaded files
1467 fs->next = files->next;
1469 current_re_filterfile[fileid] = fs;
1473 csp->rlist[fileid] = fs;
1478 load_re_filterfile_error:
1479 log_error(LOG_LEVEL_FATAL, "can't load re_filterfile '%s': %E",
1480 csp->config->re_filterfile[fileid]);
1486 /*********************************************************************
1488 * Function : add_loader
1490 * Description : Called from `load_config'. Called once for each input
1491 * file found in config.
1494 * 1 : loader = pointer to a function that can parse and load
1495 * the appropriate config file.
1496 * 2 : config = The configuration_spec to add the loader to.
1500 *********************************************************************/
1501 void add_loader(int (*loader)(struct client_state *),
1502 struct configuration_spec * config)
1506 for (i=0; i < NLOADERS; i++)
1508 if (config->loaders[i] == NULL)
1510 config->loaders[i] = loader;
1518 /*********************************************************************
1520 * Function : run_loader
1522 * Description : Called from `load_config' and `listen_loop'. This
1523 * function keeps the "csp" current with any file mods
1524 * since the last loop. If a file is unchanged, the
1525 * loader functions do NOT reload the file.
1528 * 1 : csp = Current client state (buffers, headers, etc...)
1529 * Must be non-null. Reads: "csp->config"
1530 * Writes: various data members.
1532 * Returns : 0 => Ok, everything else is an error.
1534 *********************************************************************/
1535 int run_loader(struct client_state *csp)
1540 for (i=0; i < NLOADERS; i++)
1542 if (csp->config->loaders[i] == NULL)
1546 ret |= (csp->config->loaders[i])(csp);