1 const char loaders_rcs[] = "$Id: loaders.c,v 1.54 2006/09/07 10:22:20 fabiankeil 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.54 2006/09/07 10:22:20 fabiankeil
39 * If too many trusted referrers are used,
40 * print only one error message instead of logging
41 * every single trusted referrer above the arbitrary
44 * Revision 1.53 2006/08/31 16:25:06 fabiankeil
45 * Work around a buffer overflow that caused Privoxy to
46 * segfault if too many trusted referrers were used. Good
47 * enough for now, but should be replaced with a real
48 * solution after the next release.
50 * Revision 1.52 2006/07/18 14:48:46 david__schmidt
51 * Reorganizing the repository: swapping out what was HEAD (the old 3.1 branch)
52 * with what was really the latest development (the v_3_0_branch branch)
54 * Revision 1.50.2.8 2006/01/30 15:16:25 david__schmidt
55 * Remove a little residual debugging info
57 * Revision 1.50.2.7 2006/01/29 23:10:56 david__schmidt
58 * Multiple filter file support
60 * Revision 1.50.2.6 2003/10/24 10:17:54 oes
61 * Nit: Allowed tabs as separators in filter headings
63 * Revision 1.50.2.5 2003/05/08 15:19:15 oes
64 * sweep: Made loop structure of sweep step mirror that of mark step
66 * Revision 1.50.2.4 2003/05/06 15:57:12 oes
67 * Bugfix: Update last_active pointer in sweep() before
68 * leaving an active client. Closes bugs #724395, #727882
70 * Revision 1.50.2.3 2002/11/20 17:12:30 oes
71 * Ooops, forgot one change.
73 * Revision 1.50.2.2 2002/11/20 14:38:15 oes
74 * Fixed delayed/incomplete freeing of client resources and
75 * simplified loop structure in sweep.
76 * Thanks to Oliver Stoeneberg for the hint.
78 * Revision 1.50.2.1 2002/07/26 15:19:24 oes
79 * - PCRS jobs now chained in order of appearance. Previous
80 * reverse chaining was counter-intuitive.
81 * - Changed loglevel of PCRS job compile errors to
84 * Revision 1.50 2002/04/24 02:12:16 oes
85 * Jon's multiple AF patch: Sweep now takes care of all AFs
87 * Revision 1.49 2002/04/19 16:53:25 jongfoster
88 * Optimize away a function call by using an equivalent macro
90 * Revision 1.48 2002/04/05 00:56:09 gliptak
91 * Correcting typo to clean up on realloc failure
93 * Revision 1.47 2002/03/26 22:29:55 swa
94 * we have a new homepage!
96 * Revision 1.46 2002/03/24 13:25:43 swa
97 * name change related issues
99 * Revision 1.45 2002/03/16 23:54:06 jongfoster
100 * Adding graceful termination feature, to help look for memory leaks.
101 * If you enable this (which, by design, has to be done by hand
102 * editing config.h) and then go to http://i.j.b/die, then the program
103 * will exit cleanly after the *next* request. It should free all the
104 * memory that was used.
106 * Revision 1.44 2002/03/16 21:51:00 jongfoster
109 * Revision 1.43 2002/03/16 20:28:34 oes
110 * Added descriptions to the filters so users will know what they select in the cgi editor
112 * Revision 1.42 2002/03/13 00:27:05 jongfoster
115 * Revision 1.41 2002/03/12 01:42:50 oes
116 * Introduced modular filters
118 * Revision 1.40 2002/03/08 17:46:04 jongfoster
119 * Fixing int/size_t warnings
121 * Revision 1.39 2002/03/07 03:46:17 oes
122 * Fixed compiler warnings
124 * Revision 1.38 2002/03/06 22:54:35 jongfoster
125 * Automated function-comment nitpicking.
127 * Revision 1.37 2002/03/03 15:07:49 oes
128 * Re-enabled automatic config reloading
130 * Revision 1.36 2002/01/22 23:46:18 jongfoster
131 * Moving edit_read_line() and simple_read_line() to loaders.c, and
132 * extending them to support reading MS-DOS, Mac and UNIX style files
135 * Modifying read_config_line() (without changing it's prototype) to
136 * be a trivial wrapper for edit_read_line(). This means that we have
137 * one function to read a line and handle comments, which is common
138 * between the initialization code and the edit interface.
140 * Revision 1.35 2002/01/17 21:03:08 jongfoster
141 * Moving all our URL and URL pattern parsing code to urlmatch.c.
143 * Renaming free_url to free_url_spec, since it frees a struct url_spec.
145 * Revision 1.34 2001/12/30 14:07:32 steudten
146 * - Add signal handling (unix)
147 * - Add SIGHUP handler (unix)
148 * - Add creation of pidfile (unix)
149 * - Add action 'top' in rc file (RH)
150 * - Add entry 'SIGNALS' to manpage
151 * - Add exit message to logfile (unix)
153 * Revision 1.33 2001/11/13 00:16:38 jongfoster
154 * Replacing references to malloc.h with the standard stdlib.h
155 * (See ANSI or K&R 2nd Ed)
157 * Revision 1.32 2001/11/07 00:02:13 steudten
158 * Add line number in error output for lineparsing for
159 * actionsfile and configfile.
160 * Special handling for CLF added.
162 * Revision 1.31 2001/10/26 17:39:01 oes
163 * Removed csp->referrer
164 * Moved ijb_isspace and ijb_tolower to project.h
166 * Revision 1.30 2001/10/25 03:40:48 david__schmidt
167 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
168 * threads to call select() simultaneously. So, it's time to do a real, live,
169 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
170 * (native). Both versions will work, but using __OS2__ offers multi-threading.
172 * Revision 1.29 2001/10/23 21:38:53 jongfoster
173 * Adding error-checking to create_url_spec()
175 * Revision 1.28 2001/10/07 15:40:39 oes
176 * Replaced 6 boolean members of csp with one bitmap (csp->flags)
178 * Revision 1.27 2001/09/22 16:36:59 jongfoster
179 * Removing unused parameter fs from read_config_line()
181 * Revision 1.26 2001/09/22 14:05:22 jongfoster
182 * Bugfix: Multiple escaped "#" characters in a configuration
183 * file are now permitted.
184 * Also removing 3 unused headers.
186 * Revision 1.25 2001/09/13 22:44:03 jongfoster
187 * Adding {} to an if statement
189 * Revision 1.24 2001/07/30 22:08:36 jongfoster
190 * Tidying up #defines:
191 * - All feature #defines are now of the form FEATURE_xxx
192 * - Permanently turned off WIN_GUI_EDIT
193 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
195 * Revision 1.23 2001/07/20 15:51:54 oes
196 * Fixed indentation of prepocessor commands
198 * Revision 1.22 2001/07/20 15:16:17 haroon
199 * - per Guy's suggestion, added a while loop in sweep() to catch not just
200 * the last inactive CSP but all other consecutive inactive CSPs after that
203 * Revision 1.21 2001/07/18 17:26:24 oes
204 * Changed to conform to new pcrs interface
206 * Revision 1.20 2001/07/17 13:07:01 oes
207 * Fixed segv when last line in config files
208 * lacked a terminating (\r)\n
210 * Revision 1.19 2001/07/13 14:01:54 oes
211 * Removed all #ifdef PCRS
213 * Revision 1.18 2001/06/29 21:45:41 oes
214 * Indentation, CRLF->LF, Tab-> Space
216 * Revision 1.17 2001/06/29 13:31:51 oes
219 * Revision 1.16 2001/06/09 10:55:28 jongfoster
220 * Changing BUFSIZ ==> BUFFER_SIZE
222 * Revision 1.15 2001/06/07 23:14:14 jongfoster
223 * Removing ACL and forward file loaders - these
224 * files have been merged into the config file.
225 * Cosmetic: Moving unloader funcs next to their
226 * respective loader funcs
228 * Revision 1.14 2001/06/01 03:27:04 oes
229 * Fixed line continuation problem
231 * Revision 1.13 2001/05/31 21:28:49 jongfoster
232 * Removed all permissionsfile code - it's now called the actions
233 * file, and (almost) all the code is in actions.c
235 * Revision 1.12 2001/05/31 17:32:31 oes
237 * - Enhanced domain part globbing with infix and prefix asterisk
238 * matching and optional unanchored operation
240 * Revision 1.11 2001/05/29 23:25:24 oes
242 * - load_config_line() and load_permissions_file() now use chomp()
244 * Revision 1.10 2001/05/29 09:50:24 jongfoster
245 * Unified blocklist/imagelist/permissionslist.
246 * File format is still under discussion, but the internal changes
249 * Also modified interceptor behaviour:
250 * - We now intercept all URLs beginning with one of the following
251 * prefixes (and *only* these prefixes):
253 * * http://ijbswa.sf.net/config/
254 * * http://ijbswa.sourceforge.net/config/
255 * - New interceptors "home page" - go to http://i.j.b/ to see it.
256 * - Internal changes so that intercepted and fast redirect pages
257 * are not replaced with an image.
258 * - Interceptors now have the option to send a binary page direct
259 * to the client. (i.e. ijb-send-banner uses this)
260 * - Implemented show-url-info interceptor. (Which is why I needed
261 * the above interceptors changes - a typical URL is
262 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
263 * The previous mechanism would not have intercepted that, and
264 * if it had been intercepted then it then it would have replaced
267 * Revision 1.9 2001/05/26 17:12:07 jongfoster
268 * Fatal errors loading configuration files now give better error messages.
270 * Revision 1.8 2001/05/26 00:55:20 jongfoster
271 * Removing duplicated code. load_forwardfile() now uses create_url_spec()
273 * Revision 1.7 2001/05/26 00:28:36 jongfoster
274 * Automatic reloading of config file.
275 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
276 * Most of the global variables have been moved to a new
277 * struct configuration_spec, accessed through csp->config->globalname
278 * Most of the globals remaining are used by the Win32 GUI.
280 * Revision 1.6 2001/05/23 12:27:33 oes
282 * Fixed ugly indentation of my last changes
284 * Revision 1.5 2001/05/23 10:39:05 oes
285 * - Added support for escaping the comment character
286 * in config files by a backslash
287 * - Added support for line continuation in config
289 * - Fixed a buffer overflow bug with long config lines
291 * Revision 1.4 2001/05/22 18:56:28 oes
294 * Revision 1.3 2001/05/20 01:21:20 jongfoster
295 * Version 2.9.4 checkin.
296 * - Merged popupfile and cookiefile, and added control over PCRS
297 * filtering, in new "permissionsfile".
298 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
299 * file error you now get a message box (in the Win32 GUI) rather
300 * than the program exiting with no explanation.
301 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
303 * - Removed tabs from "config"
304 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
305 * - Bumped up version number.
307 * Revision 1.2 2001/05/17 23:01:01 oes
308 * - Cleaned CRLF's from the sources and related files
310 * Revision 1.1.1.1 2001/05/15 13:58:59 oes
311 * Initial import of version 2.9.3 source tree
314 *********************************************************************/
321 #include <sys/types.h>
324 #include <sys/stat.h>
328 #if !defined(_WIN32) && !defined(__OS2__)
338 #include "miscutil.h"
341 #include "urlmatch.h"
343 const char loaders_h_rcs[] = LOADERS_H_VERSION;
346 * Currently active files.
347 * These are also entered in the main linked list of files.
351 static struct file_list *current_trustfile = NULL;
352 #endif /* def FEATURE_TRUST */
354 static int load_one_re_filterfile(struct client_state *csp, int fileid);
356 static struct file_list *current_re_filterfile[MAX_AF_FILES] = {
357 NULL, NULL, NULL, NULL, NULL,
358 NULL, NULL, NULL, NULL, NULL
363 /*********************************************************************
367 * Description : Basically a mark and sweep garbage collector, it is run
368 * (by the parent thread) every once in a while to reclaim memory.
370 * It uses a mark and sweep strategy:
371 * 1) mark all files as inactive
373 * 2) check with each client:
374 * if it is active, mark its files as active
375 * if it is inactive, free its resources
377 * 3) free the resources of all of the files that
378 * are still marked as inactive (and are obsolete).
380 * N.B. files that are not obsolete don't have an unloader defined.
386 *********************************************************************/
389 struct file_list *fl, *nfl;
390 struct client_state *csp, *last_active;
393 /* clear all of the file's active flags */
394 for ( fl = files->next; NULL != fl; fl = fl->next )
399 last_active = clients;
404 if (csp->flags & CSP_FLAG_ACTIVE)
406 /* Mark this client's files as active */
409 * Always have a configuration file.
410 * (Also note the slightly non-standard extra
413 csp->config->config_file_list->active = 1;
418 for (i = 0; i < MAX_AF_FILES; i++)
420 if (csp->actions_list[i])
422 csp->actions_list[i]->active = 1;
429 for (i = 0; i < MAX_AF_FILES; i++)
433 csp->rlist[i]->active = 1;
443 csp->tlist->active = 1;
445 #endif /* def FEATURE_TRUST */
453 * This client is not active. Free its resources.
456 last_active->next = csp->next;
458 freez(csp->ip_addr_str);
459 freez(csp->my_ip_addr_str);
460 freez(csp->my_hostname);
461 freez(csp->x_forwarded);
462 freez(csp->iob->buf);
464 free_http_request(csp->http);
466 destroy_list(csp->headers);
467 destroy_list(csp->cookie_list);
469 free_current_action(csp->action);
471 #ifdef FEATURE_STATISTICS
473 if (csp->flags & CSP_FLAG_REJECTED)
477 #endif /* def FEATURE_STATISTICS */
481 csp = last_active->next;
490 if ( ( 0 == fl->active ) && ( NULL != fl->unloader ) )
492 nfl->next = fl->next;
494 (fl->unloader)(fl->f);
511 /*********************************************************************
513 * Function : check_file_changed
515 * Description : Helper function to check if a file needs reloading.
516 * If "current" is still current, return it. Otherwise
517 * allocates a new (zeroed) "struct file_list", fills
518 * in the disk file name and timestamp, and returns it.
521 * 1 : current = The file_list currently being used - will
522 * be checked to see if it is out of date.
523 * May be NULL (which is treated as out of
525 * 2 : filename = Name of file to check.
526 * 3 : newfl = New file list. [Output only]
527 * This will be set to NULL, OR a struct
528 * file_list newly allocated on the
529 * heap, with the filename and lastmodified
530 * fields filled, and all others zeroed.
532 * Returns : If file unchanged: 0 (and sets newfl == NULL)
533 * If file changed: 1 and sets newfl != NULL
534 * On error: 1 and sets newfl == NULL
536 *********************************************************************/
537 int check_file_changed(const struct file_list * current,
538 const char * filename,
539 struct file_list ** newfl)
541 struct file_list *fs;
542 struct stat statbuf[1];
546 if (stat(filename, statbuf) < 0)
548 /* Error, probably file not found. */
553 && (current->lastmodified == statbuf->st_mtime)
554 && (0 == strcmp(current->filename, filename)))
559 fs = (struct file_list *)zalloc(sizeof(struct file_list));
562 /* Out of memory error */
567 fs->filename = strdup(filename);
568 fs->lastmodified = statbuf->st_mtime;
570 if (fs->filename == NULL)
572 /* Out of memory error */
581 /*********************************************************************
583 * Function : simple_read_line
585 * Description : Read a single line from a file and return it.
586 * This is basically a version of fgets() that malloc()s
587 * it's own line buffer. Note that the buffer will
588 * always be a multiple of BUFFER_SIZE bytes long.
589 * Therefore if you are going to keep the string for
590 * an extended period of time, you should probably
591 * strdup() it and free() the original, to save memory.
595 * 1 : dest = destination for newly malloc'd pointer to
596 * line data. Will be set to NULL on error.
597 * 2 : fp = File to read from
598 * 3 : newline = Standard for newlines in the file.
599 * Will be unchanged if it's value on input is not
601 * On output, may be changed from NEWLINE_UNKNOWN to
602 * actual convention in file.
604 * Returns : JB_ERR_OK on success
605 * JB_ERR_MEMORY on out-of-memory
606 * JB_ERR_FILE on EOF.
608 *********************************************************************/
609 jb_err simple_read_line(FILE *fp, char **dest, int *newline)
612 size_t buflen = BUFFER_SIZE;
616 int realnewline = NEWLINE_UNKNOWN;
618 if (NULL == (buf = malloc(buflen)))
620 return JB_ERR_MEMORY;
626 * Character codes. If you have a wierd compiler and the following are
627 * incorrect, you also need to fix NEWLINE() in loaders.h
629 #define CHAR_CR '\r' /* ASCII 13 */
630 #define CHAR_LF '\n' /* ASCII 10 */
650 else if (ch == CHAR_CR)
655 if (*newline == NEWLINE_UNKNOWN)
657 *newline = NEWLINE_DOS;
666 if (*newline == NEWLINE_UNKNOWN)
668 *newline = NEWLINE_MAC;
673 if (*newline == NEWLINE_UNKNOWN)
675 *newline = realnewline;
679 else if (ch == CHAR_LF)
683 if (*newline == NEWLINE_UNKNOWN)
685 *newline = NEWLINE_UNIX;
700 buflen += BUFFER_SIZE;
701 if (NULL == (p = realloc(buf, buflen)))
704 return JB_ERR_MEMORY;
713 /*********************************************************************
715 * Function : edit_read_line
717 * Description : Read a single non-empty line from a file and return
718 * it. Trims comments, leading and trailing whitespace
719 * and respects escaping of newline and comment char.
720 * Provides the line in 2 alternative forms: raw and
722 * - raw is the raw data read from the file. If the
723 * line is not modified, then this should be written
725 * - prefix is any comments and blank lines that were
726 * read from the file. If the line is modified, then
727 * this should be written out to the file followed
728 * by the modified data. (If this string is non-empty
729 * then it will have a newline at the end).
730 * - data is the actual data that will be parsed
731 * further by appropriate routines.
732 * On EOF, the 3 strings will all be set to NULL and
733 * 0 will be returned.
736 * 1 : fp = File to read from
737 * 2 : raw_out = destination for newly malloc'd pointer to
738 * raw line data. May be NULL if you don't want it.
739 * 3 : prefix_out = destination for newly malloc'd pointer to
740 * comments. May be NULL if you don't want it.
741 * 4 : data_out = destination for newly malloc'd pointer to
742 * line data with comments and leading/trailing spaces
743 * removed, and line continuation performed. May be
744 * NULL if you don't want it.
745 * 5 : newline = Standard for newlines in the file.
746 * On input, set to value to use or NEWLINE_UNKNOWN.
747 * On output, may be changed from NEWLINE_UNKNOWN to
748 * actual convention in file. May be NULL if you
750 * 6 : line_number = Line number in file. In "lines" as
751 * reported by a text editor, not lines containing data.
753 * Returns : JB_ERR_OK on success
754 * JB_ERR_MEMORY on out-of-memory
755 * JB_ERR_FILE on EOF.
757 *********************************************************************/
758 jb_err edit_read_line(FILE *fp,
763 unsigned long *line_number)
765 char *p; /* Temporary pointer */
766 char *linebuf; /* Line read from file */
767 char *linestart; /* Start of linebuf, usually first non-whitespace char */
768 int contflag = 0; /* Nonzero for line continuation - i.e. line ends '\' */
769 int is_empty = 1; /* Flag if not got any data yet */
770 char *raw = NULL; /* String to be stored in raw_out */
771 char *prefix = NULL; /* String to be stored in prefix_out */
772 char *data = NULL; /* String to be stored in data_out */
773 int scrapnewline; /* Used for (*newline) if newline==NULL */
774 jb_err rval = JB_ERR_OK;
777 assert(raw_out || data_out);
778 assert(newline == NULL
779 || *newline == NEWLINE_UNKNOWN
780 || *newline == NEWLINE_UNIX
781 || *newline == NEWLINE_DOS
782 || *newline == NEWLINE_MAC);
786 scrapnewline = NEWLINE_UNKNOWN;
787 newline = &scrapnewline;
790 /* Set output parameters to NULL */
804 /* Set string variables to new, empty strings. */
808 if ((raw = malloc(1)) == NULL)
810 return JB_ERR_MEMORY;
816 if ((prefix = malloc(1)) == NULL)
819 return JB_ERR_MEMORY;
825 if ((data = malloc(1)) == NULL)
829 return JB_ERR_MEMORY;
834 /* Main loop. Loop while we need more data & it's not EOF. */
836 while ( (contflag || is_empty)
837 && (JB_ERR_OK == (rval = simple_read_line(fp, &linebuf, newline))))
845 string_append(&raw,linebuf);
846 if (string_append(&raw,NEWLINE(*newline)))
851 return JB_ERR_MEMORY;
855 /* Line continuation? Trim escape and set flag. */
856 p = linebuf + strlen(linebuf) - 1;
857 contflag = ((*linebuf != '\0') && (*p == '\\'));
863 /* Trim leading spaces if we're at the start of the line */
867 /* Trim leading spaces */
868 while (*linestart && isspace((int)(unsigned char)*linestart))
874 /* Handle comment characters. */
876 while ((p = strchr(p, '#')) != NULL)
878 /* Found a comment char.. */
879 if ((p != linebuf) && (*(p-1) == '\\'))
881 /* ..and it's escaped, left-shift the line over the escape. */
883 while ((*q = *(q + 1)) != '\0')
887 /* Now scan from just after the "#". */
891 /* Real comment. Save it... */
894 /* Special case: Line only contains a comment, so all the
895 * previous whitespace is considered part of the comment.
896 * Undo the whitespace skipping, if any.
903 string_append(&prefix,p);
904 if (string_append(&prefix, NEWLINE(*newline)))
909 return JB_ERR_MEMORY;
913 /* ... and chop off the rest of the line */
916 } /* END while (there's a # character) */
918 /* Write to the buffer */
924 if (string_append(&data, linestart))
929 return JB_ERR_MEMORY;
935 } /* END while(we need more data) */
937 /* Handle simple_read_line() errors - ignore EOF */
938 if ((rval != JB_ERR_OK) && (rval != JB_ERR_FILE))
946 if (raw ? (*raw == '\0') : is_empty)
948 /* EOF and no data there. (Definition of "data" depends on whether
949 * the caller cares about "raw" or just "data").
960 /* Got at least some data */
962 /* Remove trailing whitespace */
975 *prefix_out = prefix;
994 /*********************************************************************
996 * Function : read_config_line
998 * Description : Read a single non-empty line from a file and return
999 * it. Trims comments, leading and trailing whitespace
1000 * and respects escaping of newline and comment char.
1003 * 1 : buf = Buffer to use.
1004 * 2 : buflen = Size of buffer in bytes.
1005 * 3 : fp = File to read from
1006 * 4 : linenum = linenumber in file
1008 * Returns : NULL on EOF or error
1009 * Otherwise, returns buf.
1011 *********************************************************************/
1012 char *read_config_line(char *buf, size_t buflen, FILE *fp, unsigned long *linenum)
1016 err = edit_read_line(fp, NULL, NULL, &buf2, NULL, linenum);
1019 if (err == JB_ERR_MEMORY)
1021 log_error(LOG_LEVEL_FATAL, "Out of memory loading a config file");
1028 assert(strlen(buf2) + 1U < buflen);
1029 strncpy(buf, buf2, buflen - 1);
1031 buf[buflen - 1] = '\0';
1037 #ifdef FEATURE_TRUST
1038 /*********************************************************************
1040 * Function : unload_trustfile
1042 * Description : Unloads a trustfile.
1045 * 1 : f = the data structure associated with the trustfile.
1049 *********************************************************************/
1050 static void unload_trustfile(void *f)
1052 struct block_spec *cur = (struct block_spec *)f;
1053 struct block_spec *next;
1059 free_url_spec(cur->url);
1068 #ifdef FEATURE_GRACEFUL_TERMINATION
1069 /*********************************************************************
1071 * Function : unload_current_trust_file
1073 * Description : Unloads current trust file - reset to state at
1074 * beginning of program.
1080 *********************************************************************/
1081 void unload_current_trust_file(void)
1083 if (current_trustfile)
1085 current_trustfile->unloader = unload_trustfile;
1086 current_trustfile = NULL;
1089 #endif /* FEATURE_GRACEFUL_TERMINATION */
1092 /*********************************************************************
1094 * Function : load_trustfile
1096 * Description : Read and parse a trustfile and add to files list.
1099 * 1 : csp = Current client state (buffers, headers, etc...)
1101 * Returns : 0 => Ok, everything else is an error.
1103 *********************************************************************/
1104 int load_trustfile(struct client_state *csp)
1108 struct block_spec *b, *bl;
1109 struct url_spec **tl;
1111 char buf[BUFFER_SIZE], *p, *q;
1112 int reject, trusted;
1113 struct file_list *fs;
1114 unsigned long linenum = 0;
1115 int trusted_referrers = 0;
1117 if (!check_file_changed(current_trustfile, csp->config->trustfile, &fs))
1119 /* No need to load */
1122 csp->tlist = current_trustfile;
1128 goto load_trustfile_error;
1131 fs->f = bl = (struct block_spec *)zalloc(sizeof(*bl));
1134 goto load_trustfile_error;
1137 if ((fp = fopen(csp->config->trustfile, "r")) == NULL)
1139 goto load_trustfile_error;
1142 tl = csp->config->trust_list;
1144 while (read_config_line(buf, sizeof(buf), fp, &linenum) != NULL)
1160 while ((*p++ = *q++) != '\0')
1166 /* skip blank lines */
1172 /* allocate a new node */
1173 if ((b = zalloc(sizeof(*b))) == NULL)
1176 goto load_trustfile_error;
1179 /* add it to the list */
1185 /* Save the URL pattern */
1186 if (create_url_spec(b->url, buf))
1189 goto load_trustfile_error;
1193 * save a pointer to URL's spec in the list of trusted URL's, too
1197 if(++trusted_referrers < MAX_TRUSTED_REFERRERS)
1204 if(trusted_referrers >= MAX_TRUSTED_REFERRERS)
1207 * FIXME: csp->config->trust_list is only needed
1208 * to print the trusted referrers in Privoxy's blocking
1209 * message. Not printing all of them is certainly better
1210 * than writing them into memory that doesn't belong to us,
1211 * but when Privoxy 3.0.4 is out, we should look for a real
1214 log_error(LOG_LEVEL_ERROR, "Too many trusted referrers for Privoxy's webinterface to handle.\n"
1215 " Current limit is %d, you are using %d. Additional trusted referrers are recognized, "
1216 "but will not show up in the blocking message.\n"
1217 " (You can increase this limit by changing MAX_TRUSTED_REFERRERS in project.h and recompiling).",
1218 MAX_TRUSTED_REFERRERS, trusted_referrers);
1225 /* the old one is now obsolete */
1226 if (current_trustfile)
1228 current_trustfile->unloader = unload_trustfile;
1231 fs->next = files->next;
1233 current_trustfile = fs;
1242 load_trustfile_error:
1243 log_error(LOG_LEVEL_FATAL, "can't load trustfile '%s': %E",
1244 csp->config->trustfile);
1248 #endif /* def FEATURE_TRUST */
1251 /*********************************************************************
1253 * Function : unload_re_filterfile
1255 * Description : Unload the re_filter list by freeing all chained
1256 * re_filterfile specs and their data.
1259 * 1 : f = the data structure associated with the filterfile.
1263 *********************************************************************/
1264 static void unload_re_filterfile(void *f)
1266 struct re_filterfile_spec *a, *b = (struct re_filterfile_spec *)f;
1272 destroy_list(b->patterns);
1273 pcrs_free_joblist(b->joblist);
1275 freez(b->description);
1285 #ifdef FEATURE_GRACEFUL_TERMINATION
1286 /*********************************************************************
1288 * Function : unload_current_re_filterfile
1290 * Description : Unloads current re_filter file - reset to state at
1291 * beginning of program.
1297 *********************************************************************/
1298 void unload_current_re_filterfile(void)
1302 for (i = 0; i < MAX_AF_FILES; i++)
1304 if (current_re_filterfile[i])
1306 current_re_filterfile[i]->unloader = unload_re_filterfile;
1307 current_re_filterfile[i] = NULL;
1314 /*********************************************************************
1316 * Function : load_re_filterfile
1318 * Description : Load the re_filterfile.
1319 * Generate a chained list of re_filterfile_spec's from
1320 * the "FILTER: " blocks, compiling all their substitutions
1321 * into chained lists of pcrs_job structs.
1324 * 1 : csp = Current client state (buffers, headers, etc...)
1326 * Returns : 0 => Ok, everything else is an error.
1328 *********************************************************************/
1329 int load_re_filterfile(struct client_state *csp)
1334 for (i = 0; i < MAX_AF_FILES; i++)
1336 if (csp->config->re_filterfile[i])
1338 result = load_one_re_filterfile(csp, i);
1344 else if (current_re_filterfile[i])
1346 current_re_filterfile[i]->unloader = unload_re_filterfile;
1347 current_re_filterfile[i] = NULL;
1354 /*********************************************************************
1356 * Function : load_one_re_filterfile
1358 * Description : Load a re_filterfile.
1359 * Generate a chained list of re_filterfile_spec's from
1360 * the "FILTER: " blocks, compiling all their substitutions
1361 * into chained lists of pcrs_job structs.
1364 * 1 : csp = Current client state (buffers, headers, etc...)
1366 * Returns : 0 => Ok, everything else is an error.
1368 *********************************************************************/
1369 int load_one_re_filterfile(struct client_state *csp, int fileid)
1373 struct re_filterfile_spec *new_bl, *bl = NULL;
1374 struct file_list *fs;
1376 char buf[BUFFER_SIZE];
1378 unsigned long linenum = 0;
1379 pcrs_job *dummy, *lastjob = NULL;
1382 * No need to reload if unchanged
1384 if (!check_file_changed(current_re_filterfile[fileid], csp->config->re_filterfile[fileid], &fs))
1388 csp->rlist[fileid] = current_re_filterfile[fileid];
1394 goto load_re_filterfile_error;
1398 * Open the file or fail
1400 if ((fp = fopen(csp->config->re_filterfile[fileid], "r")) == NULL)
1402 goto load_re_filterfile_error;
1408 while (read_config_line(buf, sizeof(buf), fp, &linenum) != NULL)
1411 * If this is the head of a new filter block, make it a
1412 * re_filterfile spec of its own and chain it to the list:
1414 if (strncmp(buf, "FILTER:", 7) == 0)
1416 new_bl = (struct re_filterfile_spec *)zalloc(sizeof(*bl));
1419 goto load_re_filterfile_error;
1422 new_bl->name = chomp(buf + 7);
1424 if (NULL != (new_bl->description = strpbrk(new_bl->name, " \t")))
1426 *new_bl->description++ = '\0';
1427 new_bl->description = strdup(chomp(new_bl->description));
1431 new_bl->description = strdup("No description available for this filter");
1434 new_bl->name = strdup(chomp(new_bl->name));
1437 * If this is the first filter block, chain it
1438 * to the file_list rather than its (nonexistant)
1451 log_error(LOG_LEVEL_RE_FILTER, "Reading in filter \"%s\" (\"%s\")", bl->name, bl->description);
1457 * Else, save the expression, make it a pcrs_job
1458 * and chain it into the current filter's joblist
1462 enlist(bl->patterns, buf);
1464 if ((dummy = pcrs_compile_command(buf, &error)) == NULL)
1466 log_error(LOG_LEVEL_ERROR,
1467 "Adding re_filter job %s to filter %s failed with error %d.", buf, bl->name, error);
1472 if (bl->joblist == NULL)
1474 bl->joblist = dummy;
1478 lastjob->next = dummy;
1481 log_error(LOG_LEVEL_RE_FILTER, "Adding re_filter job %s to filter %s succeeded.", buf, bl->name);
1486 log_error(LOG_LEVEL_ERROR, "Ignoring job %s outside filter block in %s, line %d", buf, csp->config->re_filterfile, linenum);
1493 * Schedule the now-obsolete old data for unloading
1495 if ( NULL != current_re_filterfile[fileid] )
1497 current_re_filterfile[fileid]->unloader = unload_re_filterfile;
1501 * Chain this file into the global list of loaded files
1503 fs->next = files->next;
1505 current_re_filterfile[fileid] = fs;
1509 csp->rlist[fileid] = fs;
1514 load_re_filterfile_error:
1515 log_error(LOG_LEVEL_FATAL, "can't load re_filterfile '%s': %E",
1516 csp->config->re_filterfile[fileid]);
1522 /*********************************************************************
1524 * Function : add_loader
1526 * Description : Called from `load_config'. Called once for each input
1527 * file found in config.
1530 * 1 : loader = pointer to a function that can parse and load
1531 * the appropriate config file.
1532 * 2 : config = The configuration_spec to add the loader to.
1536 *********************************************************************/
1537 void add_loader(int (*loader)(struct client_state *),
1538 struct configuration_spec * config)
1542 for (i=0; i < NLOADERS; i++)
1544 if (config->loaders[i] == NULL)
1546 config->loaders[i] = loader;
1554 /*********************************************************************
1556 * Function : run_loader
1558 * Description : Called from `load_config' and `listen_loop'. This
1559 * function keeps the "csp" current with any file mods
1560 * since the last loop. If a file is unchanged, the
1561 * loader functions do NOT reload the file.
1564 * 1 : csp = Current client state (buffers, headers, etc...)
1565 * Must be non-null. Reads: "csp->config"
1566 * Writes: various data members.
1568 * Returns : 0 => Ok, everything else is an error.
1570 *********************************************************************/
1571 int run_loader(struct client_state *csp)
1576 for (i=0; i < NLOADERS; i++)
1578 if (csp->config->loaders[i] == NULL)
1582 ret |= (csp->config->loaders[i])(csp);