1 const char loaders_rcs[] = "$Id: loaders.c,v 1.69 2008/09/21 13:36:52 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.69 2008/09/21 13:36:52 fabiankeil
39 * If change-x-forwarded-for{add} is used and the client
40 * sends multiple X-Forwarded-For headers, append the client's
41 * IP address to each one of them. "Traditionally" we would
42 * lose all but the last one.
44 * Revision 1.68 2008/09/19 15:26:28 fabiankeil
45 * Add change-x-forwarded-for{} action to block or add
46 * X-Forwarded-For headers. Mostly based on code removed
49 * Revision 1.67 2008/03/30 14:52:08 fabiankeil
50 * Rename load_actions_file() and load_re_filterfile()
51 * as they load multiple files "now".
53 * Revision 1.66 2008/03/21 11:16:30 fabiankeil
54 * Garbage-collect csp->my_ip_addr_str and csp->my_hostname.
56 * Revision 1.65 2007/12/07 18:29:23 fabiankeil
57 * Remove now-obsolete csp member x_forwarded.
59 * Revision 1.64 2007/06/01 14:12:38 fabiankeil
60 * Add unload_forward_spec() in preparation for forward-override{}.
62 * Revision 1.63 2007/05/14 10:41:15 fabiankeil
63 * Ditch the csp member cookie_list[] which isn't used anymore.
65 * Revision 1.62 2007/04/30 15:02:18 fabiankeil
66 * Introduce dynamic pcrs jobs that can resolve variables.
68 * Revision 1.61 2007/04/15 16:39:21 fabiankeil
69 * Introduce tags as alternative way to specify which
70 * actions apply to a request. At the moment tags can be
71 * created based on client and server headers.
73 * Revision 1.60 2007/03/20 15:16:34 fabiankeil
74 * Use dedicated header filter actions instead of abusing "filter".
75 * Replace "filter-client-headers" and "filter-client-headers"
76 * with "server-header-filter" and "client-header-filter".
78 * Revision 1.59 2007/01/25 13:38:20 fabiankeil
79 * Freez csp->error_message in sweep().
81 * Revision 1.58 2006/12/31 14:25:20 fabiankeil
82 * Fix gcc43 compiler warnings.
84 * Revision 1.57 2006/12/21 12:22:22 fabiankeil
85 * html_encode filter descriptions.
87 * Have "Ignoring job ..." error messages
88 * print the filter file name correctly.
90 * Revision 1.56 2006/09/07 10:40:30 fabiankeil
91 * Turns out trusted referrers above our arbitrary
92 * limit are downgraded too ordinary trusted URLs.
93 * Adjusted error message.
95 * Revision 1.55 2006/09/07 10:25:39 fabiankeil
98 * Revision 1.54 2006/09/07 10:22:20 fabiankeil
99 * If too many trusted referrers are used,
100 * print only one error message instead of logging
101 * every single trusted referrer above the arbitrary
104 * Revision 1.53 2006/08/31 16:25:06 fabiankeil
105 * Work around a buffer overflow that caused Privoxy to
106 * segfault if too many trusted referrers were used. Good
107 * enough for now, but should be replaced with a real
108 * solution after the next release.
110 * Revision 1.52 2006/07/18 14:48:46 david__schmidt
111 * Reorganizing the repository: swapping out what was HEAD (the old 3.1 branch)
112 * with what was really the latest development (the v_3_0_branch branch)
114 * Revision 1.50.2.8 2006/01/30 15:16:25 david__schmidt
115 * Remove a little residual debugging info
117 * Revision 1.50.2.7 2006/01/29 23:10:56 david__schmidt
118 * Multiple filter file support
120 * Revision 1.50.2.6 2003/10/24 10:17:54 oes
121 * Nit: Allowed tabs as separators in filter headings
123 * Revision 1.50.2.5 2003/05/08 15:19:15 oes
124 * sweep: Made loop structure of sweep step mirror that of mark step
126 * Revision 1.50.2.4 2003/05/06 15:57:12 oes
127 * Bugfix: Update last_active pointer in sweep() before
128 * leaving an active client. Closes bugs #724395, #727882
130 * Revision 1.50.2.3 2002/11/20 17:12:30 oes
131 * Ooops, forgot one change.
133 * Revision 1.50.2.2 2002/11/20 14:38:15 oes
134 * Fixed delayed/incomplete freeing of client resources and
135 * simplified loop structure in sweep.
136 * Thanks to Oliver Stoeneberg for the hint.
138 * Revision 1.50.2.1 2002/07/26 15:19:24 oes
139 * - PCRS jobs now chained in order of appearance. Previous
140 * reverse chaining was counter-intuitive.
141 * - Changed loglevel of PCRS job compile errors to
144 * Revision 1.50 2002/04/24 02:12:16 oes
145 * Jon's multiple AF patch: Sweep now takes care of all AFs
147 * Revision 1.49 2002/04/19 16:53:25 jongfoster
148 * Optimize away a function call by using an equivalent macro
150 * Revision 1.48 2002/04/05 00:56:09 gliptak
151 * Correcting typo to clean up on realloc failure
153 * Revision 1.47 2002/03/26 22:29:55 swa
154 * we have a new homepage!
156 * Revision 1.46 2002/03/24 13:25:43 swa
157 * name change related issues
159 * Revision 1.45 2002/03/16 23:54:06 jongfoster
160 * Adding graceful termination feature, to help look for memory leaks.
161 * If you enable this (which, by design, has to be done by hand
162 * editing config.h) and then go to http://i.j.b/die, then the program
163 * will exit cleanly after the *next* request. It should free all the
164 * memory that was used.
166 * Revision 1.44 2002/03/16 21:51:00 jongfoster
169 * Revision 1.43 2002/03/16 20:28:34 oes
170 * Added descriptions to the filters so users will know what they select in the cgi editor
172 * Revision 1.42 2002/03/13 00:27:05 jongfoster
175 * Revision 1.41 2002/03/12 01:42:50 oes
176 * Introduced modular filters
178 * Revision 1.40 2002/03/08 17:46:04 jongfoster
179 * Fixing int/size_t warnings
181 * Revision 1.39 2002/03/07 03:46:17 oes
182 * Fixed compiler warnings
184 * Revision 1.38 2002/03/06 22:54:35 jongfoster
185 * Automated function-comment nitpicking.
187 * Revision 1.37 2002/03/03 15:07:49 oes
188 * Re-enabled automatic config reloading
190 * Revision 1.36 2002/01/22 23:46:18 jongfoster
191 * Moving edit_read_line() and simple_read_line() to loaders.c, and
192 * extending them to support reading MS-DOS, Mac and UNIX style files
195 * Modifying read_config_line() (without changing it's prototype) to
196 * be a trivial wrapper for edit_read_line(). This means that we have
197 * one function to read a line and handle comments, which is common
198 * between the initialization code and the edit interface.
200 * Revision 1.35 2002/01/17 21:03:08 jongfoster
201 * Moving all our URL and URL pattern parsing code to urlmatch.c.
203 * Renaming free_url to free_url_spec, since it frees a struct url_spec.
205 * Revision 1.34 2001/12/30 14:07:32 steudten
206 * - Add signal handling (unix)
207 * - Add SIGHUP handler (unix)
208 * - Add creation of pidfile (unix)
209 * - Add action 'top' in rc file (RH)
210 * - Add entry 'SIGNALS' to manpage
211 * - Add exit message to logfile (unix)
213 * Revision 1.33 2001/11/13 00:16:38 jongfoster
214 * Replacing references to malloc.h with the standard stdlib.h
215 * (See ANSI or K&R 2nd Ed)
217 * Revision 1.32 2001/11/07 00:02:13 steudten
218 * Add line number in error output for lineparsing for
219 * actionsfile and configfile.
220 * Special handling for CLF added.
222 * Revision 1.31 2001/10/26 17:39:01 oes
223 * Removed csp->referrer
224 * Moved ijb_isspace and ijb_tolower to project.h
226 * Revision 1.30 2001/10/25 03:40:48 david__schmidt
227 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
228 * threads to call select() simultaneously. So, it's time to do a real, live,
229 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
230 * (native). Both versions will work, but using __OS2__ offers multi-threading.
232 * Revision 1.29 2001/10/23 21:38:53 jongfoster
233 * Adding error-checking to create_url_spec()
235 * Revision 1.28 2001/10/07 15:40:39 oes
236 * Replaced 6 boolean members of csp with one bitmap (csp->flags)
238 * Revision 1.27 2001/09/22 16:36:59 jongfoster
239 * Removing unused parameter fs from read_config_line()
241 * Revision 1.26 2001/09/22 14:05:22 jongfoster
242 * Bugfix: Multiple escaped "#" characters in a configuration
243 * file are now permitted.
244 * Also removing 3 unused headers.
246 * Revision 1.25 2001/09/13 22:44:03 jongfoster
247 * Adding {} to an if statement
249 * Revision 1.24 2001/07/30 22:08:36 jongfoster
250 * Tidying up #defines:
251 * - All feature #defines are now of the form FEATURE_xxx
252 * - Permanently turned off WIN_GUI_EDIT
253 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
255 * Revision 1.23 2001/07/20 15:51:54 oes
256 * Fixed indentation of prepocessor commands
258 * Revision 1.22 2001/07/20 15:16:17 haroon
259 * - per Guy's suggestion, added a while loop in sweep() to catch not just
260 * the last inactive CSP but all other consecutive inactive CSPs after that
263 * Revision 1.21 2001/07/18 17:26:24 oes
264 * Changed to conform to new pcrs interface
266 * Revision 1.20 2001/07/17 13:07:01 oes
267 * Fixed segv when last line in config files
268 * lacked a terminating (\r)\n
270 * Revision 1.19 2001/07/13 14:01:54 oes
271 * Removed all #ifdef PCRS
273 * Revision 1.18 2001/06/29 21:45:41 oes
274 * Indentation, CRLF->LF, Tab-> Space
276 * Revision 1.17 2001/06/29 13:31:51 oes
279 * Revision 1.16 2001/06/09 10:55:28 jongfoster
280 * Changing BUFSIZ ==> BUFFER_SIZE
282 * Revision 1.15 2001/06/07 23:14:14 jongfoster
283 * Removing ACL and forward file loaders - these
284 * files have been merged into the config file.
285 * Cosmetic: Moving unloader funcs next to their
286 * respective loader funcs
288 * Revision 1.14 2001/06/01 03:27:04 oes
289 * Fixed line continuation problem
291 * Revision 1.13 2001/05/31 21:28:49 jongfoster
292 * Removed all permissionsfile code - it's now called the actions
293 * file, and (almost) all the code is in actions.c
295 * Revision 1.12 2001/05/31 17:32:31 oes
297 * - Enhanced domain part globbing with infix and prefix asterisk
298 * matching and optional unanchored operation
300 * Revision 1.11 2001/05/29 23:25:24 oes
302 * - load_config_line() and load_permissions_file() now use chomp()
304 * Revision 1.10 2001/05/29 09:50:24 jongfoster
305 * Unified blocklist/imagelist/permissionslist.
306 * File format is still under discussion, but the internal changes
309 * Also modified interceptor behaviour:
310 * - We now intercept all URLs beginning with one of the following
311 * prefixes (and *only* these prefixes):
313 * * http://ijbswa.sf.net/config/
314 * * http://ijbswa.sourceforge.net/config/
315 * - New interceptors "home page" - go to http://i.j.b/ to see it.
316 * - Internal changes so that intercepted and fast redirect pages
317 * are not replaced with an image.
318 * - Interceptors now have the option to send a binary page direct
319 * to the client. (i.e. ijb-send-banner uses this)
320 * - Implemented show-url-info interceptor. (Which is why I needed
321 * the above interceptors changes - a typical URL is
322 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
323 * The previous mechanism would not have intercepted that, and
324 * if it had been intercepted then it then it would have replaced
327 * Revision 1.9 2001/05/26 17:12:07 jongfoster
328 * Fatal errors loading configuration files now give better error messages.
330 * Revision 1.8 2001/05/26 00:55:20 jongfoster
331 * Removing duplicated code. load_forwardfile() now uses create_url_spec()
333 * Revision 1.7 2001/05/26 00:28:36 jongfoster
334 * Automatic reloading of config file.
335 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
336 * Most of the global variables have been moved to a new
337 * struct configuration_spec, accessed through csp->config->globalname
338 * Most of the globals remaining are used by the Win32 GUI.
340 * Revision 1.6 2001/05/23 12:27:33 oes
342 * Fixed ugly indentation of my last changes
344 * Revision 1.5 2001/05/23 10:39:05 oes
345 * - Added support for escaping the comment character
346 * in config files by a backslash
347 * - Added support for line continuation in config
349 * - Fixed a buffer overflow bug with long config lines
351 * Revision 1.4 2001/05/22 18:56:28 oes
354 * Revision 1.3 2001/05/20 01:21:20 jongfoster
355 * Version 2.9.4 checkin.
356 * - Merged popupfile and cookiefile, and added control over PCRS
357 * filtering, in new "permissionsfile".
358 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
359 * file error you now get a message box (in the Win32 GUI) rather
360 * than the program exiting with no explanation.
361 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
363 * - Removed tabs from "config"
364 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
365 * - Bumped up version number.
367 * Revision 1.2 2001/05/17 23:01:01 oes
368 * - Cleaned CRLF's from the sources and related files
370 * Revision 1.1.1.1 2001/05/15 13:58:59 oes
371 * Initial import of version 2.9.3 source tree
374 *********************************************************************/
381 #include <sys/types.h>
384 #include <sys/stat.h>
388 #if !defined(_WIN32) && !defined(__OS2__)
398 #include "miscutil.h"
401 #include "urlmatch.h"
404 const char loaders_h_rcs[] = LOADERS_H_VERSION;
407 * Currently active files.
408 * These are also entered in the main linked list of files.
412 static struct file_list *current_trustfile = NULL;
413 #endif /* def FEATURE_TRUST */
415 static int load_one_re_filterfile(struct client_state *csp, int fileid);
417 static struct file_list *current_re_filterfile[MAX_AF_FILES] = {
418 NULL, NULL, NULL, NULL, NULL,
419 NULL, NULL, NULL, NULL, NULL
423 * Pseudo filter type for load_one_re_filterfile
425 #define NO_NEW_FILTER -1
428 /*********************************************************************
432 * Description : Basically a mark and sweep garbage collector, it is run
433 * (by the parent thread) every once in a while to reclaim memory.
435 * It uses a mark and sweep strategy:
436 * 1) mark all files as inactive
438 * 2) check with each client:
439 * if it is active, mark its files as active
440 * if it is inactive, free its resources
442 * 3) free the resources of all of the files that
443 * are still marked as inactive (and are obsolete).
445 * N.B. files that are not obsolete don't have an unloader defined.
451 *********************************************************************/
454 struct file_list *fl, *nfl;
455 struct client_state *csp, *last_active;
458 /* clear all of the file's active flags */
459 for ( fl = files->next; NULL != fl; fl = fl->next )
464 last_active = clients;
469 if (csp->flags & CSP_FLAG_ACTIVE)
471 /* Mark this client's files as active */
474 * Always have a configuration file.
475 * (Also note the slightly non-standard extra
478 csp->config->config_file_list->active = 1;
483 for (i = 0; i < MAX_AF_FILES; i++)
485 if (csp->actions_list[i])
487 csp->actions_list[i]->active = 1;
494 for (i = 0; i < MAX_AF_FILES; i++)
498 csp->rlist[i]->active = 1;
508 csp->tlist->active = 1;
510 #endif /* def FEATURE_TRUST */
518 * This client is not active. Free its resources.
521 last_active->next = csp->next;
523 freez(csp->ip_addr_str);
524 freez(csp->iob->buf);
525 freez(csp->error_message);
527 if (csp->action->flags & ACTION_FORWARD_OVERRIDE &&
530 unload_forward_spec(csp->fwd);
532 free_http_request(csp->http);
534 destroy_list(csp->headers);
535 destroy_list(csp->tags);
537 free_current_action(csp->action);
539 #ifdef FEATURE_STATISTICS
541 if (csp->flags & CSP_FLAG_REJECTED)
545 #endif /* def FEATURE_STATISTICS */
549 csp = last_active->next;
558 if ( ( 0 == fl->active ) && ( NULL != fl->unloader ) )
560 nfl->next = fl->next;
562 (fl->unloader)(fl->f);
579 /*********************************************************************
581 * Function : check_file_changed
583 * Description : Helper function to check if a file needs reloading.
584 * If "current" is still current, return it. Otherwise
585 * allocates a new (zeroed) "struct file_list", fills
586 * in the disk file name and timestamp, and returns it.
589 * 1 : current = The file_list currently being used - will
590 * be checked to see if it is out of date.
591 * May be NULL (which is treated as out of
593 * 2 : filename = Name of file to check.
594 * 3 : newfl = New file list. [Output only]
595 * This will be set to NULL, OR a struct
596 * file_list newly allocated on the
597 * heap, with the filename and lastmodified
598 * fields filled, and all others zeroed.
600 * Returns : If file unchanged: 0 (and sets newfl == NULL)
601 * If file changed: 1 and sets newfl != NULL
602 * On error: 1 and sets newfl == NULL
604 *********************************************************************/
605 int check_file_changed(const struct file_list * current,
606 const char * filename,
607 struct file_list ** newfl)
609 struct file_list *fs;
610 struct stat statbuf[1];
614 if (stat(filename, statbuf) < 0)
616 /* Error, probably file not found. */
621 && (current->lastmodified == statbuf->st_mtime)
622 && (0 == strcmp(current->filename, filename)))
627 fs = (struct file_list *)zalloc(sizeof(struct file_list));
630 /* Out of memory error */
635 fs->filename = strdup(filename);
636 fs->lastmodified = statbuf->st_mtime;
638 if (fs->filename == NULL)
640 /* Out of memory error */
649 /*********************************************************************
651 * Function : simple_read_line
653 * Description : Read a single line from a file and return it.
654 * This is basically a version of fgets() that malloc()s
655 * it's own line buffer. Note that the buffer will
656 * always be a multiple of BUFFER_SIZE bytes long.
657 * Therefore if you are going to keep the string for
658 * an extended period of time, you should probably
659 * strdup() it and free() the original, to save memory.
663 * 1 : dest = destination for newly malloc'd pointer to
664 * line data. Will be set to NULL on error.
665 * 2 : fp = File to read from
666 * 3 : newline = Standard for newlines in the file.
667 * Will be unchanged if it's value on input is not
669 * On output, may be changed from NEWLINE_UNKNOWN to
670 * actual convention in file.
672 * Returns : JB_ERR_OK on success
673 * JB_ERR_MEMORY on out-of-memory
674 * JB_ERR_FILE on EOF.
676 *********************************************************************/
677 jb_err simple_read_line(FILE *fp, char **dest, int *newline)
680 size_t buflen = BUFFER_SIZE;
684 int realnewline = NEWLINE_UNKNOWN;
686 if (NULL == (buf = malloc(buflen)))
688 return JB_ERR_MEMORY;
694 * Character codes. If you have a wierd compiler and the following are
695 * incorrect, you also need to fix NEWLINE() in loaders.h
697 #define CHAR_CR '\r' /* ASCII 13 */
698 #define CHAR_LF '\n' /* ASCII 10 */
718 else if (ch == CHAR_CR)
723 if (*newline == NEWLINE_UNKNOWN)
725 *newline = NEWLINE_DOS;
734 if (*newline == NEWLINE_UNKNOWN)
736 *newline = NEWLINE_MAC;
741 if (*newline == NEWLINE_UNKNOWN)
743 *newline = realnewline;
747 else if (ch == CHAR_LF)
751 if (*newline == NEWLINE_UNKNOWN)
753 *newline = NEWLINE_UNIX;
768 buflen += BUFFER_SIZE;
769 if (NULL == (p = realloc(buf, buflen)))
772 return JB_ERR_MEMORY;
781 /*********************************************************************
783 * Function : edit_read_line
785 * Description : Read a single non-empty line from a file and return
786 * it. Trims comments, leading and trailing whitespace
787 * and respects escaping of newline and comment char.
788 * Provides the line in 2 alternative forms: raw and
790 * - raw is the raw data read from the file. If the
791 * line is not modified, then this should be written
793 * - prefix is any comments and blank lines that were
794 * read from the file. If the line is modified, then
795 * this should be written out to the file followed
796 * by the modified data. (If this string is non-empty
797 * then it will have a newline at the end).
798 * - data is the actual data that will be parsed
799 * further by appropriate routines.
800 * On EOF, the 3 strings will all be set to NULL and
801 * 0 will be returned.
804 * 1 : fp = File to read from
805 * 2 : raw_out = destination for newly malloc'd pointer to
806 * raw line data. May be NULL if you don't want it.
807 * 3 : prefix_out = destination for newly malloc'd pointer to
808 * comments. May be NULL if you don't want it.
809 * 4 : data_out = destination for newly malloc'd pointer to
810 * line data with comments and leading/trailing spaces
811 * removed, and line continuation performed. May be
812 * NULL if you don't want it.
813 * 5 : newline = Standard for newlines in the file.
814 * On input, set to value to use or NEWLINE_UNKNOWN.
815 * On output, may be changed from NEWLINE_UNKNOWN to
816 * actual convention in file. May be NULL if you
818 * 6 : line_number = Line number in file. In "lines" as
819 * reported by a text editor, not lines containing data.
821 * Returns : JB_ERR_OK on success
822 * JB_ERR_MEMORY on out-of-memory
823 * JB_ERR_FILE on EOF.
825 *********************************************************************/
826 jb_err edit_read_line(FILE *fp,
831 unsigned long *line_number)
833 char *p; /* Temporary pointer */
834 char *linebuf; /* Line read from file */
835 char *linestart; /* Start of linebuf, usually first non-whitespace char */
836 int contflag = 0; /* Nonzero for line continuation - i.e. line ends '\' */
837 int is_empty = 1; /* Flag if not got any data yet */
838 char *raw = NULL; /* String to be stored in raw_out */
839 char *prefix = NULL; /* String to be stored in prefix_out */
840 char *data = NULL; /* String to be stored in data_out */
841 int scrapnewline; /* Used for (*newline) if newline==NULL */
842 jb_err rval = JB_ERR_OK;
845 assert(raw_out || data_out);
846 assert(newline == NULL
847 || *newline == NEWLINE_UNKNOWN
848 || *newline == NEWLINE_UNIX
849 || *newline == NEWLINE_DOS
850 || *newline == NEWLINE_MAC);
854 scrapnewline = NEWLINE_UNKNOWN;
855 newline = &scrapnewline;
858 /* Set output parameters to NULL */
872 /* Set string variables to new, empty strings. */
876 if ((raw = malloc(1)) == NULL)
878 return JB_ERR_MEMORY;
884 if ((prefix = malloc(1)) == NULL)
887 return JB_ERR_MEMORY;
893 if ((data = malloc(1)) == NULL)
897 return JB_ERR_MEMORY;
902 /* Main loop. Loop while we need more data & it's not EOF. */
904 while ( (contflag || is_empty)
905 && (JB_ERR_OK == (rval = simple_read_line(fp, &linebuf, newline))))
913 string_append(&raw,linebuf);
914 if (string_append(&raw,NEWLINE(*newline)))
919 return JB_ERR_MEMORY;
923 /* Line continuation? Trim escape and set flag. */
924 p = linebuf + strlen(linebuf) - 1;
925 contflag = ((*linebuf != '\0') && (*p == '\\'));
931 /* Trim leading spaces if we're at the start of the line */
933 assert(NULL != data);
936 /* Trim leading spaces */
937 while (*linestart && isspace((int)(unsigned char)*linestart))
943 /* Handle comment characters. */
945 while ((p = strchr(p, '#')) != NULL)
947 /* Found a comment char.. */
948 if ((p != linebuf) && (*(p-1) == '\\'))
950 /* ..and it's escaped, left-shift the line over the escape. */
952 while ((*q = *(q + 1)) != '\0')
956 /* Now scan from just after the "#". */
960 /* Real comment. Save it... */
963 /* Special case: Line only contains a comment, so all the
964 * previous whitespace is considered part of the comment.
965 * Undo the whitespace skipping, if any.
972 string_append(&prefix,p);
973 if (string_append(&prefix, NEWLINE(*newline)))
978 return JB_ERR_MEMORY;
982 /* ... and chop off the rest of the line */
985 } /* END while (there's a # character) */
987 /* Write to the buffer */
993 if (string_append(&data, linestart))
998 return JB_ERR_MEMORY;
1004 } /* END while(we need more data) */
1006 /* Handle simple_read_line() errors - ignore EOF */
1007 if ((rval != JB_ERR_OK) && (rval != JB_ERR_FILE))
1015 if (raw ? (*raw == '\0') : is_empty)
1017 /* EOF and no data there. (Definition of "data" depends on whether
1018 * the caller cares about "raw" or just "data").
1029 /* Got at least some data */
1031 /* Remove trailing whitespace */
1044 *prefix_out = prefix;
1063 /*********************************************************************
1065 * Function : read_config_line
1067 * Description : Read a single non-empty line from a file and return
1068 * it. Trims comments, leading and trailing whitespace
1069 * and respects escaping of newline and comment char.
1072 * 1 : buf = Buffer to use.
1073 * 2 : buflen = Size of buffer in bytes.
1074 * 3 : fp = File to read from
1075 * 4 : linenum = linenumber in file
1077 * Returns : NULL on EOF or error
1078 * Otherwise, returns buf.
1080 *********************************************************************/
1081 char *read_config_line(char *buf, size_t buflen, FILE *fp, unsigned long *linenum)
1085 err = edit_read_line(fp, NULL, NULL, &buf2, NULL, linenum);
1088 if (err == JB_ERR_MEMORY)
1090 log_error(LOG_LEVEL_FATAL, "Out of memory loading a config file");
1097 assert(strlen(buf2) + 1U < buflen);
1098 strncpy(buf, buf2, buflen - 1);
1100 buf[buflen - 1] = '\0';
1106 #ifdef FEATURE_TRUST
1107 /*********************************************************************
1109 * Function : unload_trustfile
1111 * Description : Unloads a trustfile.
1114 * 1 : f = the data structure associated with the trustfile.
1118 *********************************************************************/
1119 static void unload_trustfile(void *f)
1121 struct block_spec *cur = (struct block_spec *)f;
1122 struct block_spec *next;
1128 free_url_spec(cur->url);
1137 #ifdef FEATURE_GRACEFUL_TERMINATION
1138 /*********************************************************************
1140 * Function : unload_current_trust_file
1142 * Description : Unloads current trust file - reset to state at
1143 * beginning of program.
1149 *********************************************************************/
1150 void unload_current_trust_file(void)
1152 if (current_trustfile)
1154 current_trustfile->unloader = unload_trustfile;
1155 current_trustfile = NULL;
1158 #endif /* FEATURE_GRACEFUL_TERMINATION */
1161 /*********************************************************************
1163 * Function : load_trustfile
1165 * Description : Read and parse a trustfile and add to files list.
1168 * 1 : csp = Current client state (buffers, headers, etc...)
1170 * Returns : 0 => Ok, everything else is an error.
1172 *********************************************************************/
1173 int load_trustfile(struct client_state *csp)
1177 struct block_spec *b, *bl;
1178 struct url_spec **tl;
1180 char buf[BUFFER_SIZE], *p, *q;
1181 int reject, trusted;
1182 struct file_list *fs;
1183 unsigned long linenum = 0;
1184 int trusted_referrers = 0;
1186 if (!check_file_changed(current_trustfile, csp->config->trustfile, &fs))
1188 /* No need to load */
1191 csp->tlist = current_trustfile;
1197 goto load_trustfile_error;
1200 fs->f = bl = (struct block_spec *)zalloc(sizeof(*bl));
1203 goto load_trustfile_error;
1206 if ((fp = fopen(csp->config->trustfile, "r")) == NULL)
1208 goto load_trustfile_error;
1211 tl = csp->config->trust_list;
1213 while (read_config_line(buf, sizeof(buf), fp, &linenum) != NULL)
1229 while ((*p++ = *q++) != '\0')
1235 /* skip blank lines */
1241 /* allocate a new node */
1242 if ((b = zalloc(sizeof(*b))) == NULL)
1245 goto load_trustfile_error;
1248 /* add it to the list */
1254 /* Save the URL pattern */
1255 if (create_url_spec(b->url, buf))
1258 goto load_trustfile_error;
1262 * save a pointer to URL's spec in the list of trusted URL's, too
1266 if(++trusted_referrers < MAX_TRUSTED_REFERRERS)
1273 if(trusted_referrers >= MAX_TRUSTED_REFERRERS)
1276 * FIXME: ... after Privoxy 3.0.4 is out.
1278 log_error(LOG_LEVEL_ERROR, "Too many trusted referrers. Current limit is %d, you are using %d.\n"
1279 " Additional trusted referrers are treated like ordinary trusted URLs.\n"
1280 " (You can increase this limit by changing MAX_TRUSTED_REFERRERS in project.h and recompiling).",
1281 MAX_TRUSTED_REFERRERS, trusted_referrers);
1288 /* the old one is now obsolete */
1289 if (current_trustfile)
1291 current_trustfile->unloader = unload_trustfile;
1294 fs->next = files->next;
1296 current_trustfile = fs;
1305 load_trustfile_error:
1306 log_error(LOG_LEVEL_FATAL, "can't load trustfile '%s': %E",
1307 csp->config->trustfile);
1311 #endif /* def FEATURE_TRUST */
1314 /*********************************************************************
1316 * Function : unload_re_filterfile
1318 * Description : Unload the re_filter list by freeing all chained
1319 * re_filterfile specs and their data.
1322 * 1 : f = the data structure associated with the filterfile.
1326 *********************************************************************/
1327 static void unload_re_filterfile(void *f)
1329 struct re_filterfile_spec *a, *b = (struct re_filterfile_spec *)f;
1335 destroy_list(b->patterns);
1336 pcrs_free_joblist(b->joblist);
1338 freez(b->description);
1347 /*********************************************************************
1349 * Function : unload_forward_spec
1351 * Description : Unload the forward spec settings by freeing all
1352 * memory referenced by members and the memory for
1356 * 1 : fwd = the forward spec.
1360 *********************************************************************/
1361 void unload_forward_spec(struct forward_spec *fwd)
1363 free_url_spec(fwd->url);
1364 freez(fwd->gateway_host);
1365 freez(fwd->forward_host);
1372 #ifdef FEATURE_GRACEFUL_TERMINATION
1373 /*********************************************************************
1375 * Function : unload_current_re_filterfile
1377 * Description : Unloads current re_filter file - reset to state at
1378 * beginning of program.
1384 *********************************************************************/
1385 void unload_current_re_filterfile(void)
1389 for (i = 0; i < MAX_AF_FILES; i++)
1391 if (current_re_filterfile[i])
1393 current_re_filterfile[i]->unloader = unload_re_filterfile;
1394 current_re_filterfile[i] = NULL;
1401 /*********************************************************************
1403 * Function : load_re_filterfiles
1405 * Description : Loads all the filterfiles.
1406 * Generate a chained list of re_filterfile_spec's from
1407 * the "FILTER: " blocks, compiling all their substitutions
1408 * into chained lists of pcrs_job structs.
1411 * 1 : csp = Current client state (buffers, headers, etc...)
1413 * Returns : 0 => Ok, everything else is an error.
1415 *********************************************************************/
1416 int load_re_filterfiles(struct client_state *csp)
1421 for (i = 0; i < MAX_AF_FILES; i++)
1423 if (csp->config->re_filterfile[i])
1425 result = load_one_re_filterfile(csp, i);
1431 else if (current_re_filterfile[i])
1433 current_re_filterfile[i]->unloader = unload_re_filterfile;
1434 current_re_filterfile[i] = NULL;
1442 /*********************************************************************
1444 * Function : load_one_re_filterfile
1446 * Description : Load a re_filterfile.
1447 * Generate a chained list of re_filterfile_spec's from
1448 * the "FILTER: " blocks, compiling all their substitutions
1449 * into chained lists of pcrs_job structs.
1452 * 1 : csp = Current client state (buffers, headers, etc...)
1454 * Returns : 0 => Ok, everything else is an error.
1456 *********************************************************************/
1457 int load_one_re_filterfile(struct client_state *csp, int fileid)
1461 struct re_filterfile_spec *new_bl, *bl = NULL;
1462 struct file_list *fs;
1464 char buf[BUFFER_SIZE];
1466 unsigned long linenum = 0;
1467 pcrs_job *dummy, *lastjob = NULL;
1470 * No need to reload if unchanged
1472 if (!check_file_changed(current_re_filterfile[fileid], csp->config->re_filterfile[fileid], &fs))
1476 csp->rlist[fileid] = current_re_filterfile[fileid];
1482 goto load_re_filterfile_error;
1486 * Open the file or fail
1488 if ((fp = fopen(csp->config->re_filterfile[fileid], "r")) == NULL)
1490 goto load_re_filterfile_error;
1496 while (read_config_line(buf, sizeof(buf), fp, &linenum) != NULL)
1498 int new_filter = NO_NEW_FILTER;
1500 if (strncmp(buf, "FILTER:", 7) == 0)
1502 new_filter = FT_CONTENT_FILTER;
1504 else if (strncmp(buf, "SERVER-HEADER-FILTER:", 21) == 0)
1506 new_filter = FT_SERVER_HEADER_FILTER;
1508 else if (strncmp(buf, "CLIENT-HEADER-FILTER:", 21) == 0)
1510 new_filter = FT_CLIENT_HEADER_FILTER;
1512 else if (strncmp(buf, "CLIENT-HEADER-TAGGER:", 21) == 0)
1514 new_filter = FT_CLIENT_HEADER_TAGGER;
1516 else if (strncmp(buf, "SERVER-HEADER-TAGGER:", 21) == 0)
1518 new_filter = FT_SERVER_HEADER_TAGGER;
1522 * If this is the head of a new filter block, make it a
1523 * re_filterfile spec of its own and chain it to the list:
1525 if (new_filter != NO_NEW_FILTER)
1527 new_bl = (struct re_filterfile_spec *)zalloc(sizeof(*bl));
1530 goto load_re_filterfile_error;
1532 if (new_filter == FT_CONTENT_FILTER)
1534 new_bl->name = chomp(buf + 7);
1538 new_bl->name = chomp(buf + 21);
1540 new_bl->type = new_filter;
1543 * If a filter description is available,
1544 * encode it to HTML and save it.
1546 if (NULL != (new_bl->description = strpbrk(new_bl->name, " \t")))
1548 *new_bl->description++ = '\0';
1549 new_bl->description = html_encode(chomp(new_bl->description));
1550 if (NULL == new_bl->description)
1552 new_bl->description = strdup("Out of memory while encoding this filter's description to HTML");
1557 new_bl->description = strdup("No description available for this filter");
1560 new_bl->name = strdup(chomp(new_bl->name));
1563 * If this is the first filter block, chain it
1564 * to the file_list rather than its (nonexistant)
1578 log_error(LOG_LEVEL_RE_FILTER, "Reading in filter \"%s\" (\"%s\")", bl->name, bl->description);
1584 * Else, save the expression, make it a pcrs_job
1585 * and chain it into the current filter's joblist
1589 error = enlist(bl->patterns, buf);
1590 if (JB_ERR_MEMORY == error)
1592 log_error(LOG_LEVEL_FATAL,
1593 "Out of memory while enlisting re_filter job \'%s\' for filter %s.", buf, bl->name);
1595 assert(JB_ERR_OK == error);
1597 if (pcrs_job_is_dynamic(buf))
1600 * Dynamic pattern that might contain variables
1601 * and has to be recompiled for every request
1603 if (bl->joblist != NULL)
1605 pcrs_free_joblist(bl->joblist);
1609 log_error(LOG_LEVEL_RE_FILTER,
1610 "Adding dynamic re_filter job \'%s\' to filter %s succeeded.", buf, bl->name);
1613 else if (bl->dynamic)
1616 * A previous job was dynamic and as we
1617 * recompile the whole filter anyway, it
1618 * makes no sense to compile this job now.
1620 log_error(LOG_LEVEL_RE_FILTER,
1621 "Adding static re_filter job \'%s\' to dynamic filter %s succeeded.", buf, bl->name);
1625 if ((dummy = pcrs_compile_command(buf, &error)) == NULL)
1627 log_error(LOG_LEVEL_ERROR,
1628 "Adding re_filter job \'%s\' to filter %s failed with error %d.", buf, bl->name, error);
1633 if (bl->joblist == NULL)
1635 bl->joblist = dummy;
1637 else if (NULL != lastjob)
1639 lastjob->next = dummy;
1642 log_error(LOG_LEVEL_RE_FILTER, "Adding re_filter job \'%s\' to filter %s succeeded.", buf, bl->name);
1647 log_error(LOG_LEVEL_ERROR, "Ignoring job %s outside filter block in %s, line %d",
1648 buf, csp->config->re_filterfile[fileid], linenum);
1655 * Schedule the now-obsolete old data for unloading
1657 if ( NULL != current_re_filterfile[fileid] )
1659 current_re_filterfile[fileid]->unloader = unload_re_filterfile;
1663 * Chain this file into the global list of loaded files
1665 fs->next = files->next;
1667 current_re_filterfile[fileid] = fs;
1671 csp->rlist[fileid] = fs;
1676 load_re_filterfile_error:
1677 log_error(LOG_LEVEL_FATAL, "can't load re_filterfile '%s': %E",
1678 csp->config->re_filterfile[fileid]);
1684 /*********************************************************************
1686 * Function : add_loader
1688 * Description : Called from `load_config'. Called once for each input
1689 * file found in config.
1692 * 1 : loader = pointer to a function that can parse and load
1693 * the appropriate config file.
1694 * 2 : config = The configuration_spec to add the loader to.
1698 *********************************************************************/
1699 void add_loader(int (*loader)(struct client_state *),
1700 struct configuration_spec * config)
1704 for (i=0; i < NLOADERS; i++)
1706 if (config->loaders[i] == NULL)
1708 config->loaders[i] = loader;
1716 /*********************************************************************
1718 * Function : run_loader
1720 * Description : Called from `load_config' and `listen_loop'. This
1721 * function keeps the "csp" current with any file mods
1722 * since the last loop. If a file is unchanged, the
1723 * loader functions do NOT reload the file.
1726 * 1 : csp = Current client state (buffers, headers, etc...)
1727 * Must be non-null. Reads: "csp->config"
1728 * Writes: various data members.
1730 * Returns : 0 => Ok, everything else is an error.
1732 *********************************************************************/
1733 int run_loader(struct client_state *csp)
1738 for (i=0; i < NLOADERS; i++)
1740 if (csp->config->loaders[i] == NULL)
1744 ret |= (csp->config->loaders[i])(csp);