1 const char loaders_rcs[] = "$Id: loaders.c,v 1.66 2008/03/21 11:16:30 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-2007 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.66 2008/03/21 11:16:30 fabiankeil
39 * Garbage-collect csp->my_ip_addr_str and csp->my_hostname.
41 * Revision 1.65 2007/12/07 18:29:23 fabiankeil
42 * Remove now-obsolete csp member x_forwarded.
44 * Revision 1.64 2007/06/01 14:12:38 fabiankeil
45 * Add unload_forward_spec() in preparation for forward-override{}.
47 * Revision 1.63 2007/05/14 10:41:15 fabiankeil
48 * Ditch the csp member cookie_list[] which isn't used anymore.
50 * Revision 1.62 2007/04/30 15:02:18 fabiankeil
51 * Introduce dynamic pcrs jobs that can resolve variables.
53 * Revision 1.61 2007/04/15 16:39:21 fabiankeil
54 * Introduce tags as alternative way to specify which
55 * actions apply to a request. At the moment tags can be
56 * created based on client and server headers.
58 * Revision 1.60 2007/03/20 15:16:34 fabiankeil
59 * Use dedicated header filter actions instead of abusing "filter".
60 * Replace "filter-client-headers" and "filter-client-headers"
61 * with "server-header-filter" and "client-header-filter".
63 * Revision 1.59 2007/01/25 13:38:20 fabiankeil
64 * Freez csp->error_message in sweep().
66 * Revision 1.58 2006/12/31 14:25:20 fabiankeil
67 * Fix gcc43 compiler warnings.
69 * Revision 1.57 2006/12/21 12:22:22 fabiankeil
70 * html_encode filter descriptions.
72 * Have "Ignoring job ..." error messages
73 * print the filter file name correctly.
75 * Revision 1.56 2006/09/07 10:40:30 fabiankeil
76 * Turns out trusted referrers above our arbitrary
77 * limit are downgraded too ordinary trusted URLs.
78 * Adjusted error message.
80 * Revision 1.55 2006/09/07 10:25:39 fabiankeil
83 * Revision 1.54 2006/09/07 10:22:20 fabiankeil
84 * If too many trusted referrers are used,
85 * print only one error message instead of logging
86 * every single trusted referrer above the arbitrary
89 * Revision 1.53 2006/08/31 16:25:06 fabiankeil
90 * Work around a buffer overflow that caused Privoxy to
91 * segfault if too many trusted referrers were used. Good
92 * enough for now, but should be replaced with a real
93 * solution after the next release.
95 * Revision 1.52 2006/07/18 14:48:46 david__schmidt
96 * Reorganizing the repository: swapping out what was HEAD (the old 3.1 branch)
97 * with what was really the latest development (the v_3_0_branch branch)
99 * Revision 1.50.2.8 2006/01/30 15:16:25 david__schmidt
100 * Remove a little residual debugging info
102 * Revision 1.50.2.7 2006/01/29 23:10:56 david__schmidt
103 * Multiple filter file support
105 * Revision 1.50.2.6 2003/10/24 10:17:54 oes
106 * Nit: Allowed tabs as separators in filter headings
108 * Revision 1.50.2.5 2003/05/08 15:19:15 oes
109 * sweep: Made loop structure of sweep step mirror that of mark step
111 * Revision 1.50.2.4 2003/05/06 15:57:12 oes
112 * Bugfix: Update last_active pointer in sweep() before
113 * leaving an active client. Closes bugs #724395, #727882
115 * Revision 1.50.2.3 2002/11/20 17:12:30 oes
116 * Ooops, forgot one change.
118 * Revision 1.50.2.2 2002/11/20 14:38:15 oes
119 * Fixed delayed/incomplete freeing of client resources and
120 * simplified loop structure in sweep.
121 * Thanks to Oliver Stoeneberg for the hint.
123 * Revision 1.50.2.1 2002/07/26 15:19:24 oes
124 * - PCRS jobs now chained in order of appearance. Previous
125 * reverse chaining was counter-intuitive.
126 * - Changed loglevel of PCRS job compile errors to
129 * Revision 1.50 2002/04/24 02:12:16 oes
130 * Jon's multiple AF patch: Sweep now takes care of all AFs
132 * Revision 1.49 2002/04/19 16:53:25 jongfoster
133 * Optimize away a function call by using an equivalent macro
135 * Revision 1.48 2002/04/05 00:56:09 gliptak
136 * Correcting typo to clean up on realloc failure
138 * Revision 1.47 2002/03/26 22:29:55 swa
139 * we have a new homepage!
141 * Revision 1.46 2002/03/24 13:25:43 swa
142 * name change related issues
144 * Revision 1.45 2002/03/16 23:54:06 jongfoster
145 * Adding graceful termination feature, to help look for memory leaks.
146 * If you enable this (which, by design, has to be done by hand
147 * editing config.h) and then go to http://i.j.b/die, then the program
148 * will exit cleanly after the *next* request. It should free all the
149 * memory that was used.
151 * Revision 1.44 2002/03/16 21:51:00 jongfoster
154 * Revision 1.43 2002/03/16 20:28:34 oes
155 * Added descriptions to the filters so users will know what they select in the cgi editor
157 * Revision 1.42 2002/03/13 00:27:05 jongfoster
160 * Revision 1.41 2002/03/12 01:42:50 oes
161 * Introduced modular filters
163 * Revision 1.40 2002/03/08 17:46:04 jongfoster
164 * Fixing int/size_t warnings
166 * Revision 1.39 2002/03/07 03:46:17 oes
167 * Fixed compiler warnings
169 * Revision 1.38 2002/03/06 22:54:35 jongfoster
170 * Automated function-comment nitpicking.
172 * Revision 1.37 2002/03/03 15:07:49 oes
173 * Re-enabled automatic config reloading
175 * Revision 1.36 2002/01/22 23:46:18 jongfoster
176 * Moving edit_read_line() and simple_read_line() to loaders.c, and
177 * extending them to support reading MS-DOS, Mac and UNIX style files
180 * Modifying read_config_line() (without changing it's prototype) to
181 * be a trivial wrapper for edit_read_line(). This means that we have
182 * one function to read a line and handle comments, which is common
183 * between the initialization code and the edit interface.
185 * Revision 1.35 2002/01/17 21:03:08 jongfoster
186 * Moving all our URL and URL pattern parsing code to urlmatch.c.
188 * Renaming free_url to free_url_spec, since it frees a struct url_spec.
190 * Revision 1.34 2001/12/30 14:07:32 steudten
191 * - Add signal handling (unix)
192 * - Add SIGHUP handler (unix)
193 * - Add creation of pidfile (unix)
194 * - Add action 'top' in rc file (RH)
195 * - Add entry 'SIGNALS' to manpage
196 * - Add exit message to logfile (unix)
198 * Revision 1.33 2001/11/13 00:16:38 jongfoster
199 * Replacing references to malloc.h with the standard stdlib.h
200 * (See ANSI or K&R 2nd Ed)
202 * Revision 1.32 2001/11/07 00:02:13 steudten
203 * Add line number in error output for lineparsing for
204 * actionsfile and configfile.
205 * Special handling for CLF added.
207 * Revision 1.31 2001/10/26 17:39:01 oes
208 * Removed csp->referrer
209 * Moved ijb_isspace and ijb_tolower to project.h
211 * Revision 1.30 2001/10/25 03:40:48 david__schmidt
212 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
213 * threads to call select() simultaneously. So, it's time to do a real, live,
214 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
215 * (native). Both versions will work, but using __OS2__ offers multi-threading.
217 * Revision 1.29 2001/10/23 21:38:53 jongfoster
218 * Adding error-checking to create_url_spec()
220 * Revision 1.28 2001/10/07 15:40:39 oes
221 * Replaced 6 boolean members of csp with one bitmap (csp->flags)
223 * Revision 1.27 2001/09/22 16:36:59 jongfoster
224 * Removing unused parameter fs from read_config_line()
226 * Revision 1.26 2001/09/22 14:05:22 jongfoster
227 * Bugfix: Multiple escaped "#" characters in a configuration
228 * file are now permitted.
229 * Also removing 3 unused headers.
231 * Revision 1.25 2001/09/13 22:44:03 jongfoster
232 * Adding {} to an if statement
234 * Revision 1.24 2001/07/30 22:08:36 jongfoster
235 * Tidying up #defines:
236 * - All feature #defines are now of the form FEATURE_xxx
237 * - Permanently turned off WIN_GUI_EDIT
238 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
240 * Revision 1.23 2001/07/20 15:51:54 oes
241 * Fixed indentation of prepocessor commands
243 * Revision 1.22 2001/07/20 15:16:17 haroon
244 * - per Guy's suggestion, added a while loop in sweep() to catch not just
245 * the last inactive CSP but all other consecutive inactive CSPs after that
248 * Revision 1.21 2001/07/18 17:26:24 oes
249 * Changed to conform to new pcrs interface
251 * Revision 1.20 2001/07/17 13:07:01 oes
252 * Fixed segv when last line in config files
253 * lacked a terminating (\r)\n
255 * Revision 1.19 2001/07/13 14:01:54 oes
256 * Removed all #ifdef PCRS
258 * Revision 1.18 2001/06/29 21:45:41 oes
259 * Indentation, CRLF->LF, Tab-> Space
261 * Revision 1.17 2001/06/29 13:31:51 oes
264 * Revision 1.16 2001/06/09 10:55:28 jongfoster
265 * Changing BUFSIZ ==> BUFFER_SIZE
267 * Revision 1.15 2001/06/07 23:14:14 jongfoster
268 * Removing ACL and forward file loaders - these
269 * files have been merged into the config file.
270 * Cosmetic: Moving unloader funcs next to their
271 * respective loader funcs
273 * Revision 1.14 2001/06/01 03:27:04 oes
274 * Fixed line continuation problem
276 * Revision 1.13 2001/05/31 21:28:49 jongfoster
277 * Removed all permissionsfile code - it's now called the actions
278 * file, and (almost) all the code is in actions.c
280 * Revision 1.12 2001/05/31 17:32:31 oes
282 * - Enhanced domain part globbing with infix and prefix asterisk
283 * matching and optional unanchored operation
285 * Revision 1.11 2001/05/29 23:25:24 oes
287 * - load_config_line() and load_permissions_file() now use chomp()
289 * Revision 1.10 2001/05/29 09:50:24 jongfoster
290 * Unified blocklist/imagelist/permissionslist.
291 * File format is still under discussion, but the internal changes
294 * Also modified interceptor behaviour:
295 * - We now intercept all URLs beginning with one of the following
296 * prefixes (and *only* these prefixes):
298 * * http://ijbswa.sf.net/config/
299 * * http://ijbswa.sourceforge.net/config/
300 * - New interceptors "home page" - go to http://i.j.b/ to see it.
301 * - Internal changes so that intercepted and fast redirect pages
302 * are not replaced with an image.
303 * - Interceptors now have the option to send a binary page direct
304 * to the client. (i.e. ijb-send-banner uses this)
305 * - Implemented show-url-info interceptor. (Which is why I needed
306 * the above interceptors changes - a typical URL is
307 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
308 * The previous mechanism would not have intercepted that, and
309 * if it had been intercepted then it then it would have replaced
312 * Revision 1.9 2001/05/26 17:12:07 jongfoster
313 * Fatal errors loading configuration files now give better error messages.
315 * Revision 1.8 2001/05/26 00:55:20 jongfoster
316 * Removing duplicated code. load_forwardfile() now uses create_url_spec()
318 * Revision 1.7 2001/05/26 00:28:36 jongfoster
319 * Automatic reloading of config file.
320 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
321 * Most of the global variables have been moved to a new
322 * struct configuration_spec, accessed through csp->config->globalname
323 * Most of the globals remaining are used by the Win32 GUI.
325 * Revision 1.6 2001/05/23 12:27:33 oes
327 * Fixed ugly indentation of my last changes
329 * Revision 1.5 2001/05/23 10:39:05 oes
330 * - Added support for escaping the comment character
331 * in config files by a backslash
332 * - Added support for line continuation in config
334 * - Fixed a buffer overflow bug with long config lines
336 * Revision 1.4 2001/05/22 18:56:28 oes
339 * Revision 1.3 2001/05/20 01:21:20 jongfoster
340 * Version 2.9.4 checkin.
341 * - Merged popupfile and cookiefile, and added control over PCRS
342 * filtering, in new "permissionsfile".
343 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
344 * file error you now get a message box (in the Win32 GUI) rather
345 * than the program exiting with no explanation.
346 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
348 * - Removed tabs from "config"
349 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
350 * - Bumped up version number.
352 * Revision 1.2 2001/05/17 23:01:01 oes
353 * - Cleaned CRLF's from the sources and related files
355 * Revision 1.1.1.1 2001/05/15 13:58:59 oes
356 * Initial import of version 2.9.3 source tree
359 *********************************************************************/
366 #include <sys/types.h>
369 #include <sys/stat.h>
373 #if !defined(_WIN32) && !defined(__OS2__)
383 #include "miscutil.h"
386 #include "urlmatch.h"
389 const char loaders_h_rcs[] = LOADERS_H_VERSION;
392 * Currently active files.
393 * These are also entered in the main linked list of files.
397 static struct file_list *current_trustfile = NULL;
398 #endif /* def FEATURE_TRUST */
400 static int load_one_re_filterfile(struct client_state *csp, int fileid);
402 static struct file_list *current_re_filterfile[MAX_AF_FILES] = {
403 NULL, NULL, NULL, NULL, NULL,
404 NULL, NULL, NULL, NULL, NULL
408 * Pseudo filter type for load_one_re_filterfile
410 #define NO_NEW_FILTER -1
413 /*********************************************************************
417 * Description : Basically a mark and sweep garbage collector, it is run
418 * (by the parent thread) every once in a while to reclaim memory.
420 * It uses a mark and sweep strategy:
421 * 1) mark all files as inactive
423 * 2) check with each client:
424 * if it is active, mark its files as active
425 * if it is inactive, free its resources
427 * 3) free the resources of all of the files that
428 * are still marked as inactive (and are obsolete).
430 * N.B. files that are not obsolete don't have an unloader defined.
436 *********************************************************************/
439 struct file_list *fl, *nfl;
440 struct client_state *csp, *last_active;
443 /* clear all of the file's active flags */
444 for ( fl = files->next; NULL != fl; fl = fl->next )
449 last_active = clients;
454 if (csp->flags & CSP_FLAG_ACTIVE)
456 /* Mark this client's files as active */
459 * Always have a configuration file.
460 * (Also note the slightly non-standard extra
463 csp->config->config_file_list->active = 1;
468 for (i = 0; i < MAX_AF_FILES; i++)
470 if (csp->actions_list[i])
472 csp->actions_list[i]->active = 1;
479 for (i = 0; i < MAX_AF_FILES; i++)
483 csp->rlist[i]->active = 1;
493 csp->tlist->active = 1;
495 #endif /* def FEATURE_TRUST */
503 * This client is not active. Free its resources.
506 last_active->next = csp->next;
508 freez(csp->ip_addr_str);
509 freez(csp->iob->buf);
510 freez(csp->error_message);
512 if (csp->action->flags & ACTION_FORWARD_OVERRIDE &&
515 unload_forward_spec(csp->fwd);
517 free_http_request(csp->http);
519 destroy_list(csp->headers);
520 destroy_list(csp->tags);
522 free_current_action(csp->action);
524 #ifdef FEATURE_STATISTICS
526 if (csp->flags & CSP_FLAG_REJECTED)
530 #endif /* def FEATURE_STATISTICS */
534 csp = last_active->next;
543 if ( ( 0 == fl->active ) && ( NULL != fl->unloader ) )
545 nfl->next = fl->next;
547 (fl->unloader)(fl->f);
564 /*********************************************************************
566 * Function : check_file_changed
568 * Description : Helper function to check if a file needs reloading.
569 * If "current" is still current, return it. Otherwise
570 * allocates a new (zeroed) "struct file_list", fills
571 * in the disk file name and timestamp, and returns it.
574 * 1 : current = The file_list currently being used - will
575 * be checked to see if it is out of date.
576 * May be NULL (which is treated as out of
578 * 2 : filename = Name of file to check.
579 * 3 : newfl = New file list. [Output only]
580 * This will be set to NULL, OR a struct
581 * file_list newly allocated on the
582 * heap, with the filename and lastmodified
583 * fields filled, and all others zeroed.
585 * Returns : If file unchanged: 0 (and sets newfl == NULL)
586 * If file changed: 1 and sets newfl != NULL
587 * On error: 1 and sets newfl == NULL
589 *********************************************************************/
590 int check_file_changed(const struct file_list * current,
591 const char * filename,
592 struct file_list ** newfl)
594 struct file_list *fs;
595 struct stat statbuf[1];
599 if (stat(filename, statbuf) < 0)
601 /* Error, probably file not found. */
606 && (current->lastmodified == statbuf->st_mtime)
607 && (0 == strcmp(current->filename, filename)))
612 fs = (struct file_list *)zalloc(sizeof(struct file_list));
615 /* Out of memory error */
620 fs->filename = strdup(filename);
621 fs->lastmodified = statbuf->st_mtime;
623 if (fs->filename == NULL)
625 /* Out of memory error */
634 /*********************************************************************
636 * Function : simple_read_line
638 * Description : Read a single line from a file and return it.
639 * This is basically a version of fgets() that malloc()s
640 * it's own line buffer. Note that the buffer will
641 * always be a multiple of BUFFER_SIZE bytes long.
642 * Therefore if you are going to keep the string for
643 * an extended period of time, you should probably
644 * strdup() it and free() the original, to save memory.
648 * 1 : dest = destination for newly malloc'd pointer to
649 * line data. Will be set to NULL on error.
650 * 2 : fp = File to read from
651 * 3 : newline = Standard for newlines in the file.
652 * Will be unchanged if it's value on input is not
654 * On output, may be changed from NEWLINE_UNKNOWN to
655 * actual convention in file.
657 * Returns : JB_ERR_OK on success
658 * JB_ERR_MEMORY on out-of-memory
659 * JB_ERR_FILE on EOF.
661 *********************************************************************/
662 jb_err simple_read_line(FILE *fp, char **dest, int *newline)
665 size_t buflen = BUFFER_SIZE;
669 int realnewline = NEWLINE_UNKNOWN;
671 if (NULL == (buf = malloc(buflen)))
673 return JB_ERR_MEMORY;
679 * Character codes. If you have a wierd compiler and the following are
680 * incorrect, you also need to fix NEWLINE() in loaders.h
682 #define CHAR_CR '\r' /* ASCII 13 */
683 #define CHAR_LF '\n' /* ASCII 10 */
703 else if (ch == CHAR_CR)
708 if (*newline == NEWLINE_UNKNOWN)
710 *newline = NEWLINE_DOS;
719 if (*newline == NEWLINE_UNKNOWN)
721 *newline = NEWLINE_MAC;
726 if (*newline == NEWLINE_UNKNOWN)
728 *newline = realnewline;
732 else if (ch == CHAR_LF)
736 if (*newline == NEWLINE_UNKNOWN)
738 *newline = NEWLINE_UNIX;
753 buflen += BUFFER_SIZE;
754 if (NULL == (p = realloc(buf, buflen)))
757 return JB_ERR_MEMORY;
766 /*********************************************************************
768 * Function : edit_read_line
770 * Description : Read a single non-empty line from a file and return
771 * it. Trims comments, leading and trailing whitespace
772 * and respects escaping of newline and comment char.
773 * Provides the line in 2 alternative forms: raw and
775 * - raw is the raw data read from the file. If the
776 * line is not modified, then this should be written
778 * - prefix is any comments and blank lines that were
779 * read from the file. If the line is modified, then
780 * this should be written out to the file followed
781 * by the modified data. (If this string is non-empty
782 * then it will have a newline at the end).
783 * - data is the actual data that will be parsed
784 * further by appropriate routines.
785 * On EOF, the 3 strings will all be set to NULL and
786 * 0 will be returned.
789 * 1 : fp = File to read from
790 * 2 : raw_out = destination for newly malloc'd pointer to
791 * raw line data. May be NULL if you don't want it.
792 * 3 : prefix_out = destination for newly malloc'd pointer to
793 * comments. May be NULL if you don't want it.
794 * 4 : data_out = destination for newly malloc'd pointer to
795 * line data with comments and leading/trailing spaces
796 * removed, and line continuation performed. May be
797 * NULL if you don't want it.
798 * 5 : newline = Standard for newlines in the file.
799 * On input, set to value to use or NEWLINE_UNKNOWN.
800 * On output, may be changed from NEWLINE_UNKNOWN to
801 * actual convention in file. May be NULL if you
803 * 6 : line_number = Line number in file. In "lines" as
804 * reported by a text editor, not lines containing data.
806 * Returns : JB_ERR_OK on success
807 * JB_ERR_MEMORY on out-of-memory
808 * JB_ERR_FILE on EOF.
810 *********************************************************************/
811 jb_err edit_read_line(FILE *fp,
816 unsigned long *line_number)
818 char *p; /* Temporary pointer */
819 char *linebuf; /* Line read from file */
820 char *linestart; /* Start of linebuf, usually first non-whitespace char */
821 int contflag = 0; /* Nonzero for line continuation - i.e. line ends '\' */
822 int is_empty = 1; /* Flag if not got any data yet */
823 char *raw = NULL; /* String to be stored in raw_out */
824 char *prefix = NULL; /* String to be stored in prefix_out */
825 char *data = NULL; /* String to be stored in data_out */
826 int scrapnewline; /* Used for (*newline) if newline==NULL */
827 jb_err rval = JB_ERR_OK;
830 assert(raw_out || data_out);
831 assert(newline == NULL
832 || *newline == NEWLINE_UNKNOWN
833 || *newline == NEWLINE_UNIX
834 || *newline == NEWLINE_DOS
835 || *newline == NEWLINE_MAC);
839 scrapnewline = NEWLINE_UNKNOWN;
840 newline = &scrapnewline;
843 /* Set output parameters to NULL */
857 /* Set string variables to new, empty strings. */
861 if ((raw = malloc(1)) == NULL)
863 return JB_ERR_MEMORY;
869 if ((prefix = malloc(1)) == NULL)
872 return JB_ERR_MEMORY;
878 if ((data = malloc(1)) == NULL)
882 return JB_ERR_MEMORY;
887 /* Main loop. Loop while we need more data & it's not EOF. */
889 while ( (contflag || is_empty)
890 && (JB_ERR_OK == (rval = simple_read_line(fp, &linebuf, newline))))
898 string_append(&raw,linebuf);
899 if (string_append(&raw,NEWLINE(*newline)))
904 return JB_ERR_MEMORY;
908 /* Line continuation? Trim escape and set flag. */
909 p = linebuf + strlen(linebuf) - 1;
910 contflag = ((*linebuf != '\0') && (*p == '\\'));
916 /* Trim leading spaces if we're at the start of the line */
920 /* Trim leading spaces */
921 while (*linestart && isspace((int)(unsigned char)*linestart))
927 /* Handle comment characters. */
929 while ((p = strchr(p, '#')) != NULL)
931 /* Found a comment char.. */
932 if ((p != linebuf) && (*(p-1) == '\\'))
934 /* ..and it's escaped, left-shift the line over the escape. */
936 while ((*q = *(q + 1)) != '\0')
940 /* Now scan from just after the "#". */
944 /* Real comment. Save it... */
947 /* Special case: Line only contains a comment, so all the
948 * previous whitespace is considered part of the comment.
949 * Undo the whitespace skipping, if any.
956 string_append(&prefix,p);
957 if (string_append(&prefix, NEWLINE(*newline)))
962 return JB_ERR_MEMORY;
966 /* ... and chop off the rest of the line */
969 } /* END while (there's a # character) */
971 /* Write to the buffer */
977 if (string_append(&data, linestart))
982 return JB_ERR_MEMORY;
988 } /* END while(we need more data) */
990 /* Handle simple_read_line() errors - ignore EOF */
991 if ((rval != JB_ERR_OK) && (rval != JB_ERR_FILE))
999 if (raw ? (*raw == '\0') : is_empty)
1001 /* EOF and no data there. (Definition of "data" depends on whether
1002 * the caller cares about "raw" or just "data").
1013 /* Got at least some data */
1015 /* Remove trailing whitespace */
1028 *prefix_out = prefix;
1047 /*********************************************************************
1049 * Function : read_config_line
1051 * Description : Read a single non-empty line from a file and return
1052 * it. Trims comments, leading and trailing whitespace
1053 * and respects escaping of newline and comment char.
1056 * 1 : buf = Buffer to use.
1057 * 2 : buflen = Size of buffer in bytes.
1058 * 3 : fp = File to read from
1059 * 4 : linenum = linenumber in file
1061 * Returns : NULL on EOF or error
1062 * Otherwise, returns buf.
1064 *********************************************************************/
1065 char *read_config_line(char *buf, size_t buflen, FILE *fp, unsigned long *linenum)
1069 err = edit_read_line(fp, NULL, NULL, &buf2, NULL, linenum);
1072 if (err == JB_ERR_MEMORY)
1074 log_error(LOG_LEVEL_FATAL, "Out of memory loading a config file");
1081 assert(strlen(buf2) + 1U < buflen);
1082 strncpy(buf, buf2, buflen - 1);
1084 buf[buflen - 1] = '\0';
1090 #ifdef FEATURE_TRUST
1091 /*********************************************************************
1093 * Function : unload_trustfile
1095 * Description : Unloads a trustfile.
1098 * 1 : f = the data structure associated with the trustfile.
1102 *********************************************************************/
1103 static void unload_trustfile(void *f)
1105 struct block_spec *cur = (struct block_spec *)f;
1106 struct block_spec *next;
1112 free_url_spec(cur->url);
1121 #ifdef FEATURE_GRACEFUL_TERMINATION
1122 /*********************************************************************
1124 * Function : unload_current_trust_file
1126 * Description : Unloads current trust file - reset to state at
1127 * beginning of program.
1133 *********************************************************************/
1134 void unload_current_trust_file(void)
1136 if (current_trustfile)
1138 current_trustfile->unloader = unload_trustfile;
1139 current_trustfile = NULL;
1142 #endif /* FEATURE_GRACEFUL_TERMINATION */
1145 /*********************************************************************
1147 * Function : load_trustfile
1149 * Description : Read and parse a trustfile and add to files list.
1152 * 1 : csp = Current client state (buffers, headers, etc...)
1154 * Returns : 0 => Ok, everything else is an error.
1156 *********************************************************************/
1157 int load_trustfile(struct client_state *csp)
1161 struct block_spec *b, *bl;
1162 struct url_spec **tl;
1164 char buf[BUFFER_SIZE], *p, *q;
1165 int reject, trusted;
1166 struct file_list *fs;
1167 unsigned long linenum = 0;
1168 int trusted_referrers = 0;
1170 if (!check_file_changed(current_trustfile, csp->config->trustfile, &fs))
1172 /* No need to load */
1175 csp->tlist = current_trustfile;
1181 goto load_trustfile_error;
1184 fs->f = bl = (struct block_spec *)zalloc(sizeof(*bl));
1187 goto load_trustfile_error;
1190 if ((fp = fopen(csp->config->trustfile, "r")) == NULL)
1192 goto load_trustfile_error;
1195 tl = csp->config->trust_list;
1197 while (read_config_line(buf, sizeof(buf), fp, &linenum) != NULL)
1213 while ((*p++ = *q++) != '\0')
1219 /* skip blank lines */
1225 /* allocate a new node */
1226 if ((b = zalloc(sizeof(*b))) == NULL)
1229 goto load_trustfile_error;
1232 /* add it to the list */
1238 /* Save the URL pattern */
1239 if (create_url_spec(b->url, buf))
1242 goto load_trustfile_error;
1246 * save a pointer to URL's spec in the list of trusted URL's, too
1250 if(++trusted_referrers < MAX_TRUSTED_REFERRERS)
1257 if(trusted_referrers >= MAX_TRUSTED_REFERRERS)
1260 * FIXME: ... after Privoxy 3.0.4 is out.
1262 log_error(LOG_LEVEL_ERROR, "Too many trusted referrers. Current limit is %d, you are using %d.\n"
1263 " Additional trusted referrers are treated like ordinary trusted URLs.\n"
1264 " (You can increase this limit by changing MAX_TRUSTED_REFERRERS in project.h and recompiling).",
1265 MAX_TRUSTED_REFERRERS, trusted_referrers);
1272 /* the old one is now obsolete */
1273 if (current_trustfile)
1275 current_trustfile->unloader = unload_trustfile;
1278 fs->next = files->next;
1280 current_trustfile = fs;
1289 load_trustfile_error:
1290 log_error(LOG_LEVEL_FATAL, "can't load trustfile '%s': %E",
1291 csp->config->trustfile);
1295 #endif /* def FEATURE_TRUST */
1298 /*********************************************************************
1300 * Function : unload_re_filterfile
1302 * Description : Unload the re_filter list by freeing all chained
1303 * re_filterfile specs and their data.
1306 * 1 : f = the data structure associated with the filterfile.
1310 *********************************************************************/
1311 static void unload_re_filterfile(void *f)
1313 struct re_filterfile_spec *a, *b = (struct re_filterfile_spec *)f;
1319 destroy_list(b->patterns);
1320 pcrs_free_joblist(b->joblist);
1322 freez(b->description);
1331 /*********************************************************************
1333 * Function : unload_forward_spec
1335 * Description : Unload the forward spec settings by freeing all
1336 * memory referenced by members and the memory for
1340 * 1 : fwd = the forward spec.
1344 *********************************************************************/
1345 void unload_forward_spec(struct forward_spec *fwd)
1347 free_url_spec(fwd->url);
1348 freez(fwd->gateway_host);
1349 freez(fwd->forward_host);
1356 #ifdef FEATURE_GRACEFUL_TERMINATION
1357 /*********************************************************************
1359 * Function : unload_current_re_filterfile
1361 * Description : Unloads current re_filter file - reset to state at
1362 * beginning of program.
1368 *********************************************************************/
1369 void unload_current_re_filterfile(void)
1373 for (i = 0; i < MAX_AF_FILES; i++)
1375 if (current_re_filterfile[i])
1377 current_re_filterfile[i]->unloader = unload_re_filterfile;
1378 current_re_filterfile[i] = NULL;
1385 /*********************************************************************
1387 * Function : load_re_filterfiles
1389 * Description : Loads all the filterfiles.
1390 * Generate a chained list of re_filterfile_spec's from
1391 * the "FILTER: " blocks, compiling all their substitutions
1392 * into chained lists of pcrs_job structs.
1395 * 1 : csp = Current client state (buffers, headers, etc...)
1397 * Returns : 0 => Ok, everything else is an error.
1399 *********************************************************************/
1400 int load_re_filterfiles(struct client_state *csp)
1405 for (i = 0; i < MAX_AF_FILES; i++)
1407 if (csp->config->re_filterfile[i])
1409 result = load_one_re_filterfile(csp, i);
1415 else if (current_re_filterfile[i])
1417 current_re_filterfile[i]->unloader = unload_re_filterfile;
1418 current_re_filterfile[i] = NULL;
1426 /*********************************************************************
1428 * Function : load_one_re_filterfile
1430 * Description : Load a re_filterfile.
1431 * Generate a chained list of re_filterfile_spec's from
1432 * the "FILTER: " blocks, compiling all their substitutions
1433 * into chained lists of pcrs_job structs.
1436 * 1 : csp = Current client state (buffers, headers, etc...)
1438 * Returns : 0 => Ok, everything else is an error.
1440 *********************************************************************/
1441 int load_one_re_filterfile(struct client_state *csp, int fileid)
1445 struct re_filterfile_spec *new_bl, *bl = NULL;
1446 struct file_list *fs;
1448 char buf[BUFFER_SIZE];
1450 unsigned long linenum = 0;
1451 pcrs_job *dummy, *lastjob = NULL;
1454 * No need to reload if unchanged
1456 if (!check_file_changed(current_re_filterfile[fileid], csp->config->re_filterfile[fileid], &fs))
1460 csp->rlist[fileid] = current_re_filterfile[fileid];
1466 goto load_re_filterfile_error;
1470 * Open the file or fail
1472 if ((fp = fopen(csp->config->re_filterfile[fileid], "r")) == NULL)
1474 goto load_re_filterfile_error;
1480 while (read_config_line(buf, sizeof(buf), fp, &linenum) != NULL)
1482 int new_filter = NO_NEW_FILTER;
1484 if (strncmp(buf, "FILTER:", 7) == 0)
1486 new_filter = FT_CONTENT_FILTER;
1488 else if (strncmp(buf, "SERVER-HEADER-FILTER:", 21) == 0)
1490 new_filter = FT_SERVER_HEADER_FILTER;
1492 else if (strncmp(buf, "CLIENT-HEADER-FILTER:", 21) == 0)
1494 new_filter = FT_CLIENT_HEADER_FILTER;
1496 else if (strncmp(buf, "CLIENT-HEADER-TAGGER:", 21) == 0)
1498 new_filter = FT_CLIENT_HEADER_TAGGER;
1500 else if (strncmp(buf, "SERVER-HEADER-TAGGER:", 21) == 0)
1502 new_filter = FT_SERVER_HEADER_TAGGER;
1506 * If this is the head of a new filter block, make it a
1507 * re_filterfile spec of its own and chain it to the list:
1509 if (new_filter != NO_NEW_FILTER)
1511 new_bl = (struct re_filterfile_spec *)zalloc(sizeof(*bl));
1514 goto load_re_filterfile_error;
1516 if (new_filter == FT_CONTENT_FILTER)
1518 new_bl->name = chomp(buf + 7);
1522 new_bl->name = chomp(buf + 21);
1524 new_bl->type = new_filter;
1527 * If a filter description is available,
1528 * encode it to HTML and save it.
1530 if (NULL != (new_bl->description = strpbrk(new_bl->name, " \t")))
1532 *new_bl->description++ = '\0';
1533 new_bl->description = html_encode(chomp(new_bl->description));
1534 if (NULL == new_bl->description)
1536 new_bl->description = strdup("Out of memory while encoding this filter's description to HTML");
1541 new_bl->description = strdup("No description available for this filter");
1544 new_bl->name = strdup(chomp(new_bl->name));
1547 * If this is the first filter block, chain it
1548 * to the file_list rather than its (nonexistant)
1561 log_error(LOG_LEVEL_RE_FILTER, "Reading in filter \"%s\" (\"%s\")", bl->name, bl->description);
1567 * Else, save the expression, make it a pcrs_job
1568 * and chain it into the current filter's joblist
1572 error = enlist(bl->patterns, buf);
1573 if (JB_ERR_MEMORY == error)
1575 log_error(LOG_LEVEL_FATAL,
1576 "Out of memory while enlisting re_filter job \'%s\' for filter %s.", buf, bl->name);
1578 assert(JB_ERR_OK == error);
1580 if (pcrs_job_is_dynamic(buf))
1583 * Dynamic pattern that might contain variables
1584 * and has to be recompiled for every request
1586 if (bl->joblist != NULL)
1588 pcrs_free_joblist(bl->joblist);
1592 log_error(LOG_LEVEL_RE_FILTER,
1593 "Adding dynamic re_filter job \'%s\' to filter %s succeeded.", buf, bl->name);
1596 else if (bl->dynamic)
1599 * A previous job was dynamic and as we
1600 * recompile the whole filter anyway, it
1601 * makes no sense to compile this job now.
1603 log_error(LOG_LEVEL_RE_FILTER,
1604 "Adding static re_filter job \'%s\' to dynamic filter %s succeeded.", buf, bl->name);
1608 if ((dummy = pcrs_compile_command(buf, &error)) == NULL)
1610 log_error(LOG_LEVEL_ERROR,
1611 "Adding re_filter job \'%s\' to filter %s failed with error %d.", buf, bl->name, error);
1616 if (bl->joblist == NULL)
1618 bl->joblist = dummy;
1622 lastjob->next = dummy;
1625 log_error(LOG_LEVEL_RE_FILTER, "Adding re_filter job \'%s\' to filter %s succeeded.", buf, bl->name);
1630 log_error(LOG_LEVEL_ERROR, "Ignoring job %s outside filter block in %s, line %d",
1631 buf, csp->config->re_filterfile[fileid], linenum);
1638 * Schedule the now-obsolete old data for unloading
1640 if ( NULL != current_re_filterfile[fileid] )
1642 current_re_filterfile[fileid]->unloader = unload_re_filterfile;
1646 * Chain this file into the global list of loaded files
1648 fs->next = files->next;
1650 current_re_filterfile[fileid] = fs;
1654 csp->rlist[fileid] = fs;
1659 load_re_filterfile_error:
1660 log_error(LOG_LEVEL_FATAL, "can't load re_filterfile '%s': %E",
1661 csp->config->re_filterfile[fileid]);
1667 /*********************************************************************
1669 * Function : add_loader
1671 * Description : Called from `load_config'. Called once for each input
1672 * file found in config.
1675 * 1 : loader = pointer to a function that can parse and load
1676 * the appropriate config file.
1677 * 2 : config = The configuration_spec to add the loader to.
1681 *********************************************************************/
1682 void add_loader(int (*loader)(struct client_state *),
1683 struct configuration_spec * config)
1687 for (i=0; i < NLOADERS; i++)
1689 if (config->loaders[i] == NULL)
1691 config->loaders[i] = loader;
1699 /*********************************************************************
1701 * Function : run_loader
1703 * Description : Called from `load_config' and `listen_loop'. This
1704 * function keeps the "csp" current with any file mods
1705 * since the last loop. If a file is unchanged, the
1706 * loader functions do NOT reload the file.
1709 * 1 : csp = Current client state (buffers, headers, etc...)
1710 * Must be non-null. Reads: "csp->config"
1711 * Writes: various data members.
1713 * Returns : 0 => Ok, everything else is an error.
1715 *********************************************************************/
1716 int run_loader(struct client_state *csp)
1721 for (i=0; i < NLOADERS; i++)
1723 if (csp->config->loaders[i] == NULL)
1727 ret |= (csp->config->loaders[i])(csp);