1 const char loaders_rcs[] = "$Id: loaders.c,v 1.70 2009/03/01 18:34:24 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-2009 the
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.70 2009/03/01 18:34:24 fabiankeil
39 * Help clang understand that we aren't dereferencing
42 * Revision 1.69 2008/09/21 13:36:52 fabiankeil
43 * If change-x-forwarded-for{add} is used and the client
44 * sends multiple X-Forwarded-For headers, append the client's
45 * IP address to each one of them. "Traditionally" we would
46 * lose all but the last one.
48 * Revision 1.68 2008/09/19 15:26:28 fabiankeil
49 * Add change-x-forwarded-for{} action to block or add
50 * X-Forwarded-For headers. Mostly based on code removed
53 * Revision 1.67 2008/03/30 14:52:08 fabiankeil
54 * Rename load_actions_file() and load_re_filterfile()
55 * as they load multiple files "now".
57 * Revision 1.66 2008/03/21 11:16:30 fabiankeil
58 * Garbage-collect csp->my_ip_addr_str and csp->my_hostname.
60 * Revision 1.65 2007/12/07 18:29:23 fabiankeil
61 * Remove now-obsolete csp member x_forwarded.
63 * Revision 1.64 2007/06/01 14:12:38 fabiankeil
64 * Add unload_forward_spec() in preparation for forward-override{}.
66 * Revision 1.63 2007/05/14 10:41:15 fabiankeil
67 * Ditch the csp member cookie_list[] which isn't used anymore.
69 * Revision 1.62 2007/04/30 15:02:18 fabiankeil
70 * Introduce dynamic pcrs jobs that can resolve variables.
72 * Revision 1.61 2007/04/15 16:39:21 fabiankeil
73 * Introduce tags as alternative way to specify which
74 * actions apply to a request. At the moment tags can be
75 * created based on client and server headers.
77 * Revision 1.60 2007/03/20 15:16:34 fabiankeil
78 * Use dedicated header filter actions instead of abusing "filter".
79 * Replace "filter-client-headers" and "filter-client-headers"
80 * with "server-header-filter" and "client-header-filter".
82 * Revision 1.59 2007/01/25 13:38:20 fabiankeil
83 * Freez csp->error_message in sweep().
85 * Revision 1.58 2006/12/31 14:25:20 fabiankeil
86 * Fix gcc43 compiler warnings.
88 * Revision 1.57 2006/12/21 12:22:22 fabiankeil
89 * html_encode filter descriptions.
91 * Have "Ignoring job ..." error messages
92 * print the filter file name correctly.
94 * Revision 1.56 2006/09/07 10:40:30 fabiankeil
95 * Turns out trusted referrers above our arbitrary
96 * limit are downgraded too ordinary trusted URLs.
97 * Adjusted error message.
99 * Revision 1.55 2006/09/07 10:25:39 fabiankeil
102 * Revision 1.54 2006/09/07 10:22:20 fabiankeil
103 * If too many trusted referrers are used,
104 * print only one error message instead of logging
105 * every single trusted referrer above the arbitrary
108 * Revision 1.53 2006/08/31 16:25:06 fabiankeil
109 * Work around a buffer overflow that caused Privoxy to
110 * segfault if too many trusted referrers were used. Good
111 * enough for now, but should be replaced with a real
112 * solution after the next release.
114 * Revision 1.52 2006/07/18 14:48:46 david__schmidt
115 * Reorganizing the repository: swapping out what was HEAD (the old 3.1 branch)
116 * with what was really the latest development (the v_3_0_branch branch)
118 * Revision 1.50.2.8 2006/01/30 15:16:25 david__schmidt
119 * Remove a little residual debugging info
121 * Revision 1.50.2.7 2006/01/29 23:10:56 david__schmidt
122 * Multiple filter file support
124 * Revision 1.50.2.6 2003/10/24 10:17:54 oes
125 * Nit: Allowed tabs as separators in filter headings
127 * Revision 1.50.2.5 2003/05/08 15:19:15 oes
128 * sweep: Made loop structure of sweep step mirror that of mark step
130 * Revision 1.50.2.4 2003/05/06 15:57:12 oes
131 * Bugfix: Update last_active pointer in sweep() before
132 * leaving an active client. Closes bugs #724395, #727882
134 * Revision 1.50.2.3 2002/11/20 17:12:30 oes
135 * Ooops, forgot one change.
137 * Revision 1.50.2.2 2002/11/20 14:38:15 oes
138 * Fixed delayed/incomplete freeing of client resources and
139 * simplified loop structure in sweep.
140 * Thanks to Oliver Stoeneberg for the hint.
142 * Revision 1.50.2.1 2002/07/26 15:19:24 oes
143 * - PCRS jobs now chained in order of appearance. Previous
144 * reverse chaining was counter-intuitive.
145 * - Changed loglevel of PCRS job compile errors to
148 * Revision 1.50 2002/04/24 02:12:16 oes
149 * Jon's multiple AF patch: Sweep now takes care of all AFs
151 * Revision 1.49 2002/04/19 16:53:25 jongfoster
152 * Optimize away a function call by using an equivalent macro
154 * Revision 1.48 2002/04/05 00:56:09 gliptak
155 * Correcting typo to clean up on realloc failure
157 * Revision 1.47 2002/03/26 22:29:55 swa
158 * we have a new homepage!
160 * Revision 1.46 2002/03/24 13:25:43 swa
161 * name change related issues
163 * Revision 1.45 2002/03/16 23:54:06 jongfoster
164 * Adding graceful termination feature, to help look for memory leaks.
165 * If you enable this (which, by design, has to be done by hand
166 * editing config.h) and then go to http://i.j.b/die, then the program
167 * will exit cleanly after the *next* request. It should free all the
168 * memory that was used.
170 * Revision 1.44 2002/03/16 21:51:00 jongfoster
173 * Revision 1.43 2002/03/16 20:28:34 oes
174 * Added descriptions to the filters so users will know what they select in the cgi editor
176 * Revision 1.42 2002/03/13 00:27:05 jongfoster
179 * Revision 1.41 2002/03/12 01:42:50 oes
180 * Introduced modular filters
182 * Revision 1.40 2002/03/08 17:46:04 jongfoster
183 * Fixing int/size_t warnings
185 * Revision 1.39 2002/03/07 03:46:17 oes
186 * Fixed compiler warnings
188 * Revision 1.38 2002/03/06 22:54:35 jongfoster
189 * Automated function-comment nitpicking.
191 * Revision 1.37 2002/03/03 15:07:49 oes
192 * Re-enabled automatic config reloading
194 * Revision 1.36 2002/01/22 23:46:18 jongfoster
195 * Moving edit_read_line() and simple_read_line() to loaders.c, and
196 * extending them to support reading MS-DOS, Mac and UNIX style files
199 * Modifying read_config_line() (without changing it's prototype) to
200 * be a trivial wrapper for edit_read_line(). This means that we have
201 * one function to read a line and handle comments, which is common
202 * between the initialization code and the edit interface.
204 * Revision 1.35 2002/01/17 21:03:08 jongfoster
205 * Moving all our URL and URL pattern parsing code to urlmatch.c.
207 * Renaming free_url to free_url_spec, since it frees a struct url_spec.
209 * Revision 1.34 2001/12/30 14:07:32 steudten
210 * - Add signal handling (unix)
211 * - Add SIGHUP handler (unix)
212 * - Add creation of pidfile (unix)
213 * - Add action 'top' in rc file (RH)
214 * - Add entry 'SIGNALS' to manpage
215 * - Add exit message to logfile (unix)
217 * Revision 1.33 2001/11/13 00:16:38 jongfoster
218 * Replacing references to malloc.h with the standard stdlib.h
219 * (See ANSI or K&R 2nd Ed)
221 * Revision 1.32 2001/11/07 00:02:13 steudten
222 * Add line number in error output for lineparsing for
223 * actionsfile and configfile.
224 * Special handling for CLF added.
226 * Revision 1.31 2001/10/26 17:39:01 oes
227 * Removed csp->referrer
228 * Moved ijb_isspace and ijb_tolower to project.h
230 * Revision 1.30 2001/10/25 03:40:48 david__schmidt
231 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
232 * threads to call select() simultaneously. So, it's time to do a real, live,
233 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
234 * (native). Both versions will work, but using __OS2__ offers multi-threading.
236 * Revision 1.29 2001/10/23 21:38:53 jongfoster
237 * Adding error-checking to create_url_spec()
239 * Revision 1.28 2001/10/07 15:40:39 oes
240 * Replaced 6 boolean members of csp with one bitmap (csp->flags)
242 * Revision 1.27 2001/09/22 16:36:59 jongfoster
243 * Removing unused parameter fs from read_config_line()
245 * Revision 1.26 2001/09/22 14:05:22 jongfoster
246 * Bugfix: Multiple escaped "#" characters in a configuration
247 * file are now permitted.
248 * Also removing 3 unused headers.
250 * Revision 1.25 2001/09/13 22:44:03 jongfoster
251 * Adding {} to an if statement
253 * Revision 1.24 2001/07/30 22:08:36 jongfoster
254 * Tidying up #defines:
255 * - All feature #defines are now of the form FEATURE_xxx
256 * - Permanently turned off WIN_GUI_EDIT
257 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
259 * Revision 1.23 2001/07/20 15:51:54 oes
260 * Fixed indentation of prepocessor commands
262 * Revision 1.22 2001/07/20 15:16:17 haroon
263 * - per Guy's suggestion, added a while loop in sweep() to catch not just
264 * the last inactive CSP but all other consecutive inactive CSPs after that
267 * Revision 1.21 2001/07/18 17:26:24 oes
268 * Changed to conform to new pcrs interface
270 * Revision 1.20 2001/07/17 13:07:01 oes
271 * Fixed segv when last line in config files
272 * lacked a terminating (\r)\n
274 * Revision 1.19 2001/07/13 14:01:54 oes
275 * Removed all #ifdef PCRS
277 * Revision 1.18 2001/06/29 21:45:41 oes
278 * Indentation, CRLF->LF, Tab-> Space
280 * Revision 1.17 2001/06/29 13:31:51 oes
283 * Revision 1.16 2001/06/09 10:55:28 jongfoster
284 * Changing BUFSIZ ==> BUFFER_SIZE
286 * Revision 1.15 2001/06/07 23:14:14 jongfoster
287 * Removing ACL and forward file loaders - these
288 * files have been merged into the config file.
289 * Cosmetic: Moving unloader funcs next to their
290 * respective loader funcs
292 * Revision 1.14 2001/06/01 03:27:04 oes
293 * Fixed line continuation problem
295 * Revision 1.13 2001/05/31 21:28:49 jongfoster
296 * Removed all permissionsfile code - it's now called the actions
297 * file, and (almost) all the code is in actions.c
299 * Revision 1.12 2001/05/31 17:32:31 oes
301 * - Enhanced domain part globbing with infix and prefix asterisk
302 * matching and optional unanchored operation
304 * Revision 1.11 2001/05/29 23:25:24 oes
306 * - load_config_line() and load_permissions_file() now use chomp()
308 * Revision 1.10 2001/05/29 09:50:24 jongfoster
309 * Unified blocklist/imagelist/permissionslist.
310 * File format is still under discussion, but the internal changes
313 * Also modified interceptor behaviour:
314 * - We now intercept all URLs beginning with one of the following
315 * prefixes (and *only* these prefixes):
317 * * http://ijbswa.sf.net/config/
318 * * http://ijbswa.sourceforge.net/config/
319 * - New interceptors "home page" - go to http://i.j.b/ to see it.
320 * - Internal changes so that intercepted and fast redirect pages
321 * are not replaced with an image.
322 * - Interceptors now have the option to send a binary page direct
323 * to the client. (i.e. ijb-send-banner uses this)
324 * - Implemented show-url-info interceptor. (Which is why I needed
325 * the above interceptors changes - a typical URL is
326 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
327 * The previous mechanism would not have intercepted that, and
328 * if it had been intercepted then it then it would have replaced
331 * Revision 1.9 2001/05/26 17:12:07 jongfoster
332 * Fatal errors loading configuration files now give better error messages.
334 * Revision 1.8 2001/05/26 00:55:20 jongfoster
335 * Removing duplicated code. load_forwardfile() now uses create_url_spec()
337 * Revision 1.7 2001/05/26 00:28:36 jongfoster
338 * Automatic reloading of config file.
339 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
340 * Most of the global variables have been moved to a new
341 * struct configuration_spec, accessed through csp->config->globalname
342 * Most of the globals remaining are used by the Win32 GUI.
344 * Revision 1.6 2001/05/23 12:27:33 oes
346 * Fixed ugly indentation of my last changes
348 * Revision 1.5 2001/05/23 10:39:05 oes
349 * - Added support for escaping the comment character
350 * in config files by a backslash
351 * - Added support for line continuation in config
353 * - Fixed a buffer overflow bug with long config lines
355 * Revision 1.4 2001/05/22 18:56:28 oes
358 * Revision 1.3 2001/05/20 01:21:20 jongfoster
359 * Version 2.9.4 checkin.
360 * - Merged popupfile and cookiefile, and added control over PCRS
361 * filtering, in new "permissionsfile".
362 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
363 * file error you now get a message box (in the Win32 GUI) rather
364 * than the program exiting with no explanation.
365 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
367 * - Removed tabs from "config"
368 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
369 * - Bumped up version number.
371 * Revision 1.2 2001/05/17 23:01:01 oes
372 * - Cleaned CRLF's from the sources and related files
374 * Revision 1.1.1.1 2001/05/15 13:58:59 oes
375 * Initial import of version 2.9.3 source tree
378 *********************************************************************/
385 #include <sys/types.h>
388 #include <sys/stat.h>
392 #if !defined(_WIN32) && !defined(__OS2__)
402 #include "miscutil.h"
405 #include "urlmatch.h"
408 const char loaders_h_rcs[] = LOADERS_H_VERSION;
411 * Currently active files.
412 * These are also entered in the main linked list of files.
416 static struct file_list *current_trustfile = NULL;
417 #endif /* def FEATURE_TRUST */
419 static int load_one_re_filterfile(struct client_state *csp, int fileid);
421 static struct file_list *current_re_filterfile[MAX_AF_FILES] = {
422 NULL, NULL, NULL, NULL, NULL,
423 NULL, NULL, NULL, NULL, NULL
427 * Pseudo filter type for load_one_re_filterfile
429 #define NO_NEW_FILTER -1
432 /*********************************************************************
436 * Description : Basically a mark and sweep garbage collector, it is run
437 * (by the parent thread) every once in a while to reclaim memory.
439 * It uses a mark and sweep strategy:
440 * 1) mark all files as inactive
442 * 2) check with each client:
443 * if it is active, mark its files as active
444 * if it is inactive, free its resources
446 * 3) free the resources of all of the files that
447 * are still marked as inactive (and are obsolete).
449 * N.B. files that are not obsolete don't have an unloader defined.
455 *********************************************************************/
458 struct file_list *fl, *nfl;
459 struct client_state *csp, *last_active;
462 /* clear all of the file's active flags */
463 for ( fl = files->next; NULL != fl; fl = fl->next )
468 last_active = clients;
473 if (csp->flags & CSP_FLAG_ACTIVE)
475 /* Mark this client's files as active */
478 * Always have a configuration file.
479 * (Also note the slightly non-standard extra
482 csp->config->config_file_list->active = 1;
487 for (i = 0; i < MAX_AF_FILES; i++)
489 if (csp->actions_list[i])
491 csp->actions_list[i]->active = 1;
498 for (i = 0; i < MAX_AF_FILES; i++)
502 csp->rlist[i]->active = 1;
512 csp->tlist->active = 1;
514 #endif /* def FEATURE_TRUST */
522 * This client is not active. Free its resources.
525 last_active->next = csp->next;
527 freez(csp->ip_addr_str);
528 freez(csp->iob->buf);
529 freez(csp->error_message);
531 if (csp->action->flags & ACTION_FORWARD_OVERRIDE &&
534 unload_forward_spec(csp->fwd);
536 free_http_request(csp->http);
538 destroy_list(csp->headers);
539 destroy_list(csp->tags);
541 free_current_action(csp->action);
543 #ifdef FEATURE_STATISTICS
545 if (csp->flags & CSP_FLAG_REJECTED)
549 #endif /* def FEATURE_STATISTICS */
553 csp = last_active->next;
562 if ( ( 0 == fl->active ) && ( NULL != fl->unloader ) )
564 nfl->next = fl->next;
566 (fl->unloader)(fl->f);
583 /*********************************************************************
585 * Function : check_file_changed
587 * Description : Helper function to check if a file needs reloading.
588 * If "current" is still current, return it. Otherwise
589 * allocates a new (zeroed) "struct file_list", fills
590 * in the disk file name and timestamp, and returns it.
593 * 1 : current = The file_list currently being used - will
594 * be checked to see if it is out of date.
595 * May be NULL (which is treated as out of
597 * 2 : filename = Name of file to check.
598 * 3 : newfl = New file list. [Output only]
599 * This will be set to NULL, OR a struct
600 * file_list newly allocated on the
601 * heap, with the filename and lastmodified
602 * fields filled, and all others zeroed.
604 * Returns : If file unchanged: 0 (and sets newfl == NULL)
605 * If file changed: 1 and sets newfl != NULL
606 * On error: 1 and sets newfl == NULL
608 *********************************************************************/
609 int check_file_changed(const struct file_list * current,
610 const char * filename,
611 struct file_list ** newfl)
613 struct file_list *fs;
614 struct stat statbuf[1];
618 if (stat(filename, statbuf) < 0)
620 /* Error, probably file not found. */
625 && (current->lastmodified == statbuf->st_mtime)
626 && (0 == strcmp(current->filename, filename)))
631 fs = (struct file_list *)zalloc(sizeof(struct file_list));
634 /* Out of memory error */
639 fs->filename = strdup(filename);
640 fs->lastmodified = statbuf->st_mtime;
642 if (fs->filename == NULL)
644 /* Out of memory error */
653 /*********************************************************************
655 * Function : simple_read_line
657 * Description : Read a single line from a file and return it.
658 * This is basically a version of fgets() that malloc()s
659 * it's own line buffer. Note that the buffer will
660 * always be a multiple of BUFFER_SIZE bytes long.
661 * Therefore if you are going to keep the string for
662 * an extended period of time, you should probably
663 * strdup() it and free() the original, to save memory.
667 * 1 : dest = destination for newly malloc'd pointer to
668 * line data. Will be set to NULL on error.
669 * 2 : fp = File to read from
670 * 3 : newline = Standard for newlines in the file.
671 * Will be unchanged if it's value on input is not
673 * On output, may be changed from NEWLINE_UNKNOWN to
674 * actual convention in file.
676 * Returns : JB_ERR_OK on success
677 * JB_ERR_MEMORY on out-of-memory
678 * JB_ERR_FILE on EOF.
680 *********************************************************************/
681 jb_err simple_read_line(FILE *fp, char **dest, int *newline)
684 size_t buflen = BUFFER_SIZE;
688 int realnewline = NEWLINE_UNKNOWN;
690 if (NULL == (buf = malloc(buflen)))
692 return JB_ERR_MEMORY;
698 * Character codes. If you have a wierd compiler and the following are
699 * incorrect, you also need to fix NEWLINE() in loaders.h
701 #define CHAR_CR '\r' /* ASCII 13 */
702 #define CHAR_LF '\n' /* ASCII 10 */
722 else if (ch == CHAR_CR)
727 if (*newline == NEWLINE_UNKNOWN)
729 *newline = NEWLINE_DOS;
738 if (*newline == NEWLINE_UNKNOWN)
740 *newline = NEWLINE_MAC;
745 if (*newline == NEWLINE_UNKNOWN)
747 *newline = realnewline;
751 else if (ch == CHAR_LF)
755 if (*newline == NEWLINE_UNKNOWN)
757 *newline = NEWLINE_UNIX;
772 buflen += BUFFER_SIZE;
773 if (NULL == (p = realloc(buf, buflen)))
776 return JB_ERR_MEMORY;
785 /*********************************************************************
787 * Function : edit_read_line
789 * Description : Read a single non-empty line from a file and return
790 * it. Trims comments, leading and trailing whitespace
791 * and respects escaping of newline and comment char.
792 * Provides the line in 2 alternative forms: raw and
794 * - raw is the raw data read from the file. If the
795 * line is not modified, then this should be written
797 * - prefix is any comments and blank lines that were
798 * read from the file. If the line is modified, then
799 * this should be written out to the file followed
800 * by the modified data. (If this string is non-empty
801 * then it will have a newline at the end).
802 * - data is the actual data that will be parsed
803 * further by appropriate routines.
804 * On EOF, the 3 strings will all be set to NULL and
805 * 0 will be returned.
808 * 1 : fp = File to read from
809 * 2 : raw_out = destination for newly malloc'd pointer to
810 * raw line data. May be NULL if you don't want it.
811 * 3 : prefix_out = destination for newly malloc'd pointer to
812 * comments. May be NULL if you don't want it.
813 * 4 : data_out = destination for newly malloc'd pointer to
814 * line data with comments and leading/trailing spaces
815 * removed, and line continuation performed. May be
816 * NULL if you don't want it.
817 * 5 : newline = Standard for newlines in the file.
818 * On input, set to value to use or NEWLINE_UNKNOWN.
819 * On output, may be changed from NEWLINE_UNKNOWN to
820 * actual convention in file. May be NULL if you
822 * 6 : line_number = Line number in file. In "lines" as
823 * reported by a text editor, not lines containing data.
825 * Returns : JB_ERR_OK on success
826 * JB_ERR_MEMORY on out-of-memory
827 * JB_ERR_FILE on EOF.
829 *********************************************************************/
830 jb_err edit_read_line(FILE *fp,
835 unsigned long *line_number)
837 char *p; /* Temporary pointer */
838 char *linebuf; /* Line read from file */
839 char *linestart; /* Start of linebuf, usually first non-whitespace char */
840 int contflag = 0; /* Nonzero for line continuation - i.e. line ends '\' */
841 int is_empty = 1; /* Flag if not got any data yet */
842 char *raw = NULL; /* String to be stored in raw_out */
843 char *prefix = NULL; /* String to be stored in prefix_out */
844 char *data = NULL; /* String to be stored in data_out */
845 int scrapnewline; /* Used for (*newline) if newline==NULL */
846 jb_err rval = JB_ERR_OK;
849 assert(raw_out || data_out);
850 assert(newline == NULL
851 || *newline == NEWLINE_UNKNOWN
852 || *newline == NEWLINE_UNIX
853 || *newline == NEWLINE_DOS
854 || *newline == NEWLINE_MAC);
858 scrapnewline = NEWLINE_UNKNOWN;
859 newline = &scrapnewline;
862 /* Set output parameters to NULL */
876 /* Set string variables to new, empty strings. */
883 return JB_ERR_MEMORY;
892 return JB_ERR_MEMORY;
902 return JB_ERR_MEMORY;
906 /* Main loop. Loop while we need more data & it's not EOF. */
908 while ( (contflag || is_empty)
909 && (JB_ERR_OK == (rval = simple_read_line(fp, &linebuf, newline))))
917 string_append(&raw,linebuf);
918 if (string_append(&raw,NEWLINE(*newline)))
923 return JB_ERR_MEMORY;
927 /* Line continuation? Trim escape and set flag. */
928 p = linebuf + strlen(linebuf) - 1;
929 contflag = ((*linebuf != '\0') && (*p == '\\'));
935 /* Trim leading spaces if we're at the start of the line */
937 assert(NULL != data);
940 /* Trim leading spaces */
941 while (*linestart && isspace((int)(unsigned char)*linestart))
947 /* Handle comment characters. */
949 while ((p = strchr(p, '#')) != NULL)
951 /* Found a comment char.. */
952 if ((p != linebuf) && (*(p-1) == '\\'))
954 /* ..and it's escaped, left-shift the line over the escape. */
956 while ((*q = *(q + 1)) != '\0')
960 /* Now scan from just after the "#". */
964 /* Real comment. Save it... */
967 /* Special case: Line only contains a comment, so all the
968 * previous whitespace is considered part of the comment.
969 * Undo the whitespace skipping, if any.
976 string_append(&prefix,p);
977 if (string_append(&prefix, NEWLINE(*newline)))
982 return JB_ERR_MEMORY;
986 /* ... and chop off the rest of the line */
989 } /* END while (there's a # character) */
991 /* Write to the buffer */
997 if (string_append(&data, linestart))
1002 return JB_ERR_MEMORY;
1008 } /* END while(we need more data) */
1010 /* Handle simple_read_line() errors - ignore EOF */
1011 if ((rval != JB_ERR_OK) && (rval != JB_ERR_FILE))
1019 if (raw ? (*raw == '\0') : is_empty)
1021 /* EOF and no data there. (Definition of "data" depends on whether
1022 * the caller cares about "raw" or just "data").
1033 /* Got at least some data */
1035 /* Remove trailing whitespace */
1048 *prefix_out = prefix;
1067 /*********************************************************************
1069 * Function : read_config_line
1071 * Description : Read a single non-empty line from a file and return
1072 * it. Trims comments, leading and trailing whitespace
1073 * and respects escaping of newline and comment char.
1076 * 1 : buf = Buffer to use.
1077 * 2 : buflen = Size of buffer in bytes.
1078 * 3 : fp = File to read from
1079 * 4 : linenum = linenumber in file
1081 * Returns : NULL on EOF or error
1082 * Otherwise, returns buf.
1084 *********************************************************************/
1085 char *read_config_line(char *buf, size_t buflen, FILE *fp, unsigned long *linenum)
1089 err = edit_read_line(fp, NULL, NULL, &buf2, NULL, linenum);
1092 if (err == JB_ERR_MEMORY)
1094 log_error(LOG_LEVEL_FATAL, "Out of memory loading a config file");
1101 assert(strlen(buf2) + 1U < buflen);
1102 strncpy(buf, buf2, buflen - 1);
1104 buf[buflen - 1] = '\0';
1110 #ifdef FEATURE_TRUST
1111 /*********************************************************************
1113 * Function : unload_trustfile
1115 * Description : Unloads a trustfile.
1118 * 1 : f = the data structure associated with the trustfile.
1122 *********************************************************************/
1123 static void unload_trustfile(void *f)
1125 struct block_spec *cur = (struct block_spec *)f;
1126 struct block_spec *next;
1132 free_url_spec(cur->url);
1141 #ifdef FEATURE_GRACEFUL_TERMINATION
1142 /*********************************************************************
1144 * Function : unload_current_trust_file
1146 * Description : Unloads current trust file - reset to state at
1147 * beginning of program.
1153 *********************************************************************/
1154 void unload_current_trust_file(void)
1156 if (current_trustfile)
1158 current_trustfile->unloader = unload_trustfile;
1159 current_trustfile = NULL;
1162 #endif /* FEATURE_GRACEFUL_TERMINATION */
1165 /*********************************************************************
1167 * Function : load_trustfile
1169 * Description : Read and parse a trustfile and add to files list.
1172 * 1 : csp = Current client state (buffers, headers, etc...)
1174 * Returns : 0 => Ok, everything else is an error.
1176 *********************************************************************/
1177 int load_trustfile(struct client_state *csp)
1181 struct block_spec *b, *bl;
1182 struct url_spec **tl;
1184 char buf[BUFFER_SIZE], *p, *q;
1185 int reject, trusted;
1186 struct file_list *fs;
1187 unsigned long linenum = 0;
1188 int trusted_referrers = 0;
1190 if (!check_file_changed(current_trustfile, csp->config->trustfile, &fs))
1192 /* No need to load */
1195 csp->tlist = current_trustfile;
1201 goto load_trustfile_error;
1204 fs->f = bl = (struct block_spec *)zalloc(sizeof(*bl));
1207 goto load_trustfile_error;
1210 if ((fp = fopen(csp->config->trustfile, "r")) == NULL)
1212 goto load_trustfile_error;
1215 tl = csp->config->trust_list;
1217 while (read_config_line(buf, sizeof(buf), fp, &linenum) != NULL)
1233 while ((*p++ = *q++) != '\0')
1239 /* skip blank lines */
1245 /* allocate a new node */
1246 if ((b = zalloc(sizeof(*b))) == NULL)
1249 goto load_trustfile_error;
1252 /* add it to the list */
1258 /* Save the URL pattern */
1259 if (create_url_spec(b->url, buf))
1262 goto load_trustfile_error;
1266 * save a pointer to URL's spec in the list of trusted URL's, too
1270 if(++trusted_referrers < MAX_TRUSTED_REFERRERS)
1277 if(trusted_referrers >= MAX_TRUSTED_REFERRERS)
1280 * FIXME: ... after Privoxy 3.0.4 is out.
1282 log_error(LOG_LEVEL_ERROR, "Too many trusted referrers. Current limit is %d, you are using %d.\n"
1283 " Additional trusted referrers are treated like ordinary trusted URLs.\n"
1284 " (You can increase this limit by changing MAX_TRUSTED_REFERRERS in project.h and recompiling).",
1285 MAX_TRUSTED_REFERRERS, trusted_referrers);
1292 /* the old one is now obsolete */
1293 if (current_trustfile)
1295 current_trustfile->unloader = unload_trustfile;
1298 fs->next = files->next;
1300 current_trustfile = fs;
1309 load_trustfile_error:
1310 log_error(LOG_LEVEL_FATAL, "can't load trustfile '%s': %E",
1311 csp->config->trustfile);
1315 #endif /* def FEATURE_TRUST */
1318 /*********************************************************************
1320 * Function : unload_re_filterfile
1322 * Description : Unload the re_filter list by freeing all chained
1323 * re_filterfile specs and their data.
1326 * 1 : f = the data structure associated with the filterfile.
1330 *********************************************************************/
1331 static void unload_re_filterfile(void *f)
1333 struct re_filterfile_spec *a, *b = (struct re_filterfile_spec *)f;
1339 destroy_list(b->patterns);
1340 pcrs_free_joblist(b->joblist);
1342 freez(b->description);
1351 /*********************************************************************
1353 * Function : unload_forward_spec
1355 * Description : Unload the forward spec settings by freeing all
1356 * memory referenced by members and the memory for
1360 * 1 : fwd = the forward spec.
1364 *********************************************************************/
1365 void unload_forward_spec(struct forward_spec *fwd)
1367 free_url_spec(fwd->url);
1368 freez(fwd->gateway_host);
1369 freez(fwd->forward_host);
1376 #ifdef FEATURE_GRACEFUL_TERMINATION
1377 /*********************************************************************
1379 * Function : unload_current_re_filterfile
1381 * Description : Unloads current re_filter file - reset to state at
1382 * beginning of program.
1388 *********************************************************************/
1389 void unload_current_re_filterfile(void)
1393 for (i = 0; i < MAX_AF_FILES; i++)
1395 if (current_re_filterfile[i])
1397 current_re_filterfile[i]->unloader = unload_re_filterfile;
1398 current_re_filterfile[i] = NULL;
1405 /*********************************************************************
1407 * Function : load_re_filterfiles
1409 * Description : Loads all the filterfiles.
1410 * Generate a chained list of re_filterfile_spec's from
1411 * the "FILTER: " blocks, compiling all their substitutions
1412 * into chained lists of pcrs_job structs.
1415 * 1 : csp = Current client state (buffers, headers, etc...)
1417 * Returns : 0 => Ok, everything else is an error.
1419 *********************************************************************/
1420 int load_re_filterfiles(struct client_state *csp)
1425 for (i = 0; i < MAX_AF_FILES; i++)
1427 if (csp->config->re_filterfile[i])
1429 result = load_one_re_filterfile(csp, i);
1435 else if (current_re_filterfile[i])
1437 current_re_filterfile[i]->unloader = unload_re_filterfile;
1438 current_re_filterfile[i] = NULL;
1446 /*********************************************************************
1448 * Function : load_one_re_filterfile
1450 * Description : Load a re_filterfile.
1451 * Generate a chained list of re_filterfile_spec's from
1452 * the "FILTER: " blocks, compiling all their substitutions
1453 * into chained lists of pcrs_job structs.
1456 * 1 : csp = Current client state (buffers, headers, etc...)
1458 * Returns : 0 => Ok, everything else is an error.
1460 *********************************************************************/
1461 int load_one_re_filterfile(struct client_state *csp, int fileid)
1465 struct re_filterfile_spec *new_bl, *bl = NULL;
1466 struct file_list *fs;
1468 char buf[BUFFER_SIZE];
1470 unsigned long linenum = 0;
1471 pcrs_job *dummy, *lastjob = NULL;
1474 * No need to reload if unchanged
1476 if (!check_file_changed(current_re_filterfile[fileid], csp->config->re_filterfile[fileid], &fs))
1480 csp->rlist[fileid] = current_re_filterfile[fileid];
1486 goto load_re_filterfile_error;
1490 * Open the file or fail
1492 if ((fp = fopen(csp->config->re_filterfile[fileid], "r")) == NULL)
1494 goto load_re_filterfile_error;
1500 while (read_config_line(buf, sizeof(buf), fp, &linenum) != NULL)
1502 int new_filter = NO_NEW_FILTER;
1504 if (strncmp(buf, "FILTER:", 7) == 0)
1506 new_filter = FT_CONTENT_FILTER;
1508 else if (strncmp(buf, "SERVER-HEADER-FILTER:", 21) == 0)
1510 new_filter = FT_SERVER_HEADER_FILTER;
1512 else if (strncmp(buf, "CLIENT-HEADER-FILTER:", 21) == 0)
1514 new_filter = FT_CLIENT_HEADER_FILTER;
1516 else if (strncmp(buf, "CLIENT-HEADER-TAGGER:", 21) == 0)
1518 new_filter = FT_CLIENT_HEADER_TAGGER;
1520 else if (strncmp(buf, "SERVER-HEADER-TAGGER:", 21) == 0)
1522 new_filter = FT_SERVER_HEADER_TAGGER;
1526 * If this is the head of a new filter block, make it a
1527 * re_filterfile spec of its own and chain it to the list:
1529 if (new_filter != NO_NEW_FILTER)
1531 new_bl = (struct re_filterfile_spec *)zalloc(sizeof(*bl));
1534 goto load_re_filterfile_error;
1536 if (new_filter == FT_CONTENT_FILTER)
1538 new_bl->name = chomp(buf + 7);
1542 new_bl->name = chomp(buf + 21);
1544 new_bl->type = new_filter;
1547 * If a filter description is available,
1548 * encode it to HTML and save it.
1550 if (NULL != (new_bl->description = strpbrk(new_bl->name, " \t")))
1552 *new_bl->description++ = '\0';
1553 new_bl->description = html_encode(chomp(new_bl->description));
1554 if (NULL == new_bl->description)
1556 new_bl->description = strdup("Out of memory while encoding this filter's description to HTML");
1561 new_bl->description = strdup("No description available for this filter");
1564 new_bl->name = strdup(chomp(new_bl->name));
1567 * If this is the first filter block, chain it
1568 * to the file_list rather than its (nonexistant)
1582 log_error(LOG_LEVEL_RE_FILTER, "Reading in filter \"%s\" (\"%s\")", bl->name, bl->description);
1588 * Else, save the expression, make it a pcrs_job
1589 * and chain it into the current filter's joblist
1593 error = enlist(bl->patterns, buf);
1594 if (JB_ERR_MEMORY == error)
1596 log_error(LOG_LEVEL_FATAL,
1597 "Out of memory while enlisting re_filter job \'%s\' for filter %s.", buf, bl->name);
1599 assert(JB_ERR_OK == error);
1601 if (pcrs_job_is_dynamic(buf))
1604 * Dynamic pattern that might contain variables
1605 * and has to be recompiled for every request
1607 if (bl->joblist != NULL)
1609 pcrs_free_joblist(bl->joblist);
1613 log_error(LOG_LEVEL_RE_FILTER,
1614 "Adding dynamic re_filter job \'%s\' to filter %s succeeded.", buf, bl->name);
1617 else if (bl->dynamic)
1620 * A previous job was dynamic and as we
1621 * recompile the whole filter anyway, it
1622 * makes no sense to compile this job now.
1624 log_error(LOG_LEVEL_RE_FILTER,
1625 "Adding static re_filter job \'%s\' to dynamic filter %s succeeded.", buf, bl->name);
1629 if ((dummy = pcrs_compile_command(buf, &error)) == NULL)
1631 log_error(LOG_LEVEL_ERROR,
1632 "Adding re_filter job \'%s\' to filter %s failed with error %d.", buf, bl->name, error);
1637 if (bl->joblist == NULL)
1639 bl->joblist = dummy;
1641 else if (NULL != lastjob)
1643 lastjob->next = dummy;
1646 log_error(LOG_LEVEL_RE_FILTER, "Adding re_filter job \'%s\' to filter %s succeeded.", buf, bl->name);
1651 log_error(LOG_LEVEL_ERROR, "Ignoring job %s outside filter block in %s, line %d",
1652 buf, csp->config->re_filterfile[fileid], linenum);
1659 * Schedule the now-obsolete old data for unloading
1661 if ( NULL != current_re_filterfile[fileid] )
1663 current_re_filterfile[fileid]->unloader = unload_re_filterfile;
1667 * Chain this file into the global list of loaded files
1669 fs->next = files->next;
1671 current_re_filterfile[fileid] = fs;
1675 csp->rlist[fileid] = fs;
1680 load_re_filterfile_error:
1681 log_error(LOG_LEVEL_FATAL, "can't load re_filterfile '%s': %E",
1682 csp->config->re_filterfile[fileid]);
1688 /*********************************************************************
1690 * Function : add_loader
1692 * Description : Called from `load_config'. Called once for each input
1693 * file found in config.
1696 * 1 : loader = pointer to a function that can parse and load
1697 * the appropriate config file.
1698 * 2 : config = The configuration_spec to add the loader to.
1702 *********************************************************************/
1703 void add_loader(int (*loader)(struct client_state *),
1704 struct configuration_spec * config)
1708 for (i=0; i < NLOADERS; i++)
1710 if (config->loaders[i] == NULL)
1712 config->loaders[i] = loader;
1720 /*********************************************************************
1722 * Function : run_loader
1724 * Description : Called from `load_config' and `listen_loop'. This
1725 * function keeps the "csp" current with any file mods
1726 * since the last loop. If a file is unchanged, the
1727 * loader functions do NOT reload the file.
1730 * 1 : csp = Current client state (buffers, headers, etc...)
1731 * Must be non-null. Reads: "csp->config"
1732 * Writes: various data members.
1734 * Returns : 0 => Ok, everything else is an error.
1736 *********************************************************************/
1737 int run_loader(struct client_state *csp)
1742 for (i=0; i < NLOADERS; i++)
1744 if (csp->config->loaders[i] == NULL)
1748 ret |= (csp->config->loaders[i])(csp);