1 const char loaders_rcs[] = "$Id: loaders.c,v 1.71 2009/03/04 18:24:47 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.71 2009/03/04 18:24:47 fabiankeil
39 * No need to create empty strings manually, strdup("") FTW.
41 * Revision 1.70 2009/03/01 18:34:24 fabiankeil
42 * Help clang understand that we aren't dereferencing
45 * Revision 1.69 2008/09/21 13:36:52 fabiankeil
46 * If change-x-forwarded-for{add} is used and the client
47 * sends multiple X-Forwarded-For headers, append the client's
48 * IP address to each one of them. "Traditionally" we would
49 * lose all but the last one.
51 * Revision 1.68 2008/09/19 15:26:28 fabiankeil
52 * Add change-x-forwarded-for{} action to block or add
53 * X-Forwarded-For headers. Mostly based on code removed
56 * Revision 1.67 2008/03/30 14:52:08 fabiankeil
57 * Rename load_actions_file() and load_re_filterfile()
58 * as they load multiple files "now".
60 * Revision 1.66 2008/03/21 11:16:30 fabiankeil
61 * Garbage-collect csp->my_ip_addr_str and csp->my_hostname.
63 * Revision 1.65 2007/12/07 18:29:23 fabiankeil
64 * Remove now-obsolete csp member x_forwarded.
66 * Revision 1.64 2007/06/01 14:12:38 fabiankeil
67 * Add unload_forward_spec() in preparation for forward-override{}.
69 * Revision 1.63 2007/05/14 10:41:15 fabiankeil
70 * Ditch the csp member cookie_list[] which isn't used anymore.
72 * Revision 1.62 2007/04/30 15:02:18 fabiankeil
73 * Introduce dynamic pcrs jobs that can resolve variables.
75 * Revision 1.61 2007/04/15 16:39:21 fabiankeil
76 * Introduce tags as alternative way to specify which
77 * actions apply to a request. At the moment tags can be
78 * created based on client and server headers.
80 * Revision 1.60 2007/03/20 15:16:34 fabiankeil
81 * Use dedicated header filter actions instead of abusing "filter".
82 * Replace "filter-client-headers" and "filter-client-headers"
83 * with "server-header-filter" and "client-header-filter".
85 * Revision 1.59 2007/01/25 13:38:20 fabiankeil
86 * Freez csp->error_message in sweep().
88 * Revision 1.58 2006/12/31 14:25:20 fabiankeil
89 * Fix gcc43 compiler warnings.
91 * Revision 1.57 2006/12/21 12:22:22 fabiankeil
92 * html_encode filter descriptions.
94 * Have "Ignoring job ..." error messages
95 * print the filter file name correctly.
97 * Revision 1.56 2006/09/07 10:40:30 fabiankeil
98 * Turns out trusted referrers above our arbitrary
99 * limit are downgraded too ordinary trusted URLs.
100 * Adjusted error message.
102 * Revision 1.55 2006/09/07 10:25:39 fabiankeil
105 * Revision 1.54 2006/09/07 10:22:20 fabiankeil
106 * If too many trusted referrers are used,
107 * print only one error message instead of logging
108 * every single trusted referrer above the arbitrary
111 * Revision 1.53 2006/08/31 16:25:06 fabiankeil
112 * Work around a buffer overflow that caused Privoxy to
113 * segfault if too many trusted referrers were used. Good
114 * enough for now, but should be replaced with a real
115 * solution after the next release.
117 * Revision 1.52 2006/07/18 14:48:46 david__schmidt
118 * Reorganizing the repository: swapping out what was HEAD (the old 3.1 branch)
119 * with what was really the latest development (the v_3_0_branch branch)
121 * Revision 1.50.2.8 2006/01/30 15:16:25 david__schmidt
122 * Remove a little residual debugging info
124 * Revision 1.50.2.7 2006/01/29 23:10:56 david__schmidt
125 * Multiple filter file support
127 * Revision 1.50.2.6 2003/10/24 10:17:54 oes
128 * Nit: Allowed tabs as separators in filter headings
130 * Revision 1.50.2.5 2003/05/08 15:19:15 oes
131 * sweep: Made loop structure of sweep step mirror that of mark step
133 * Revision 1.50.2.4 2003/05/06 15:57:12 oes
134 * Bugfix: Update last_active pointer in sweep() before
135 * leaving an active client. Closes bugs #724395, #727882
137 * Revision 1.50.2.3 2002/11/20 17:12:30 oes
138 * Ooops, forgot one change.
140 * Revision 1.50.2.2 2002/11/20 14:38:15 oes
141 * Fixed delayed/incomplete freeing of client resources and
142 * simplified loop structure in sweep.
143 * Thanks to Oliver Stoeneberg for the hint.
145 * Revision 1.50.2.1 2002/07/26 15:19:24 oes
146 * - PCRS jobs now chained in order of appearance. Previous
147 * reverse chaining was counter-intuitive.
148 * - Changed loglevel of PCRS job compile errors to
151 * Revision 1.50 2002/04/24 02:12:16 oes
152 * Jon's multiple AF patch: Sweep now takes care of all AFs
154 * Revision 1.49 2002/04/19 16:53:25 jongfoster
155 * Optimize away a function call by using an equivalent macro
157 * Revision 1.48 2002/04/05 00:56:09 gliptak
158 * Correcting typo to clean up on realloc failure
160 * Revision 1.47 2002/03/26 22:29:55 swa
161 * we have a new homepage!
163 * Revision 1.46 2002/03/24 13:25:43 swa
164 * name change related issues
166 * Revision 1.45 2002/03/16 23:54:06 jongfoster
167 * Adding graceful termination feature, to help look for memory leaks.
168 * If you enable this (which, by design, has to be done by hand
169 * editing config.h) and then go to http://i.j.b/die, then the program
170 * will exit cleanly after the *next* request. It should free all the
171 * memory that was used.
173 * Revision 1.44 2002/03/16 21:51:00 jongfoster
176 * Revision 1.43 2002/03/16 20:28:34 oes
177 * Added descriptions to the filters so users will know what they select in the cgi editor
179 * Revision 1.42 2002/03/13 00:27:05 jongfoster
182 * Revision 1.41 2002/03/12 01:42:50 oes
183 * Introduced modular filters
185 * Revision 1.40 2002/03/08 17:46:04 jongfoster
186 * Fixing int/size_t warnings
188 * Revision 1.39 2002/03/07 03:46:17 oes
189 * Fixed compiler warnings
191 * Revision 1.38 2002/03/06 22:54:35 jongfoster
192 * Automated function-comment nitpicking.
194 * Revision 1.37 2002/03/03 15:07:49 oes
195 * Re-enabled automatic config reloading
197 * Revision 1.36 2002/01/22 23:46:18 jongfoster
198 * Moving edit_read_line() and simple_read_line() to loaders.c, and
199 * extending them to support reading MS-DOS, Mac and UNIX style files
202 * Modifying read_config_line() (without changing it's prototype) to
203 * be a trivial wrapper for edit_read_line(). This means that we have
204 * one function to read a line and handle comments, which is common
205 * between the initialization code and the edit interface.
207 * Revision 1.35 2002/01/17 21:03:08 jongfoster
208 * Moving all our URL and URL pattern parsing code to urlmatch.c.
210 * Renaming free_url to free_url_spec, since it frees a struct url_spec.
212 * Revision 1.34 2001/12/30 14:07:32 steudten
213 * - Add signal handling (unix)
214 * - Add SIGHUP handler (unix)
215 * - Add creation of pidfile (unix)
216 * - Add action 'top' in rc file (RH)
217 * - Add entry 'SIGNALS' to manpage
218 * - Add exit message to logfile (unix)
220 * Revision 1.33 2001/11/13 00:16:38 jongfoster
221 * Replacing references to malloc.h with the standard stdlib.h
222 * (See ANSI or K&R 2nd Ed)
224 * Revision 1.32 2001/11/07 00:02:13 steudten
225 * Add line number in error output for lineparsing for
226 * actionsfile and configfile.
227 * Special handling for CLF added.
229 * Revision 1.31 2001/10/26 17:39:01 oes
230 * Removed csp->referrer
231 * Moved ijb_isspace and ijb_tolower to project.h
233 * Revision 1.30 2001/10/25 03:40:48 david__schmidt
234 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
235 * threads to call select() simultaneously. So, it's time to do a real, live,
236 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
237 * (native). Both versions will work, but using __OS2__ offers multi-threading.
239 * Revision 1.29 2001/10/23 21:38:53 jongfoster
240 * Adding error-checking to create_url_spec()
242 * Revision 1.28 2001/10/07 15:40:39 oes
243 * Replaced 6 boolean members of csp with one bitmap (csp->flags)
245 * Revision 1.27 2001/09/22 16:36:59 jongfoster
246 * Removing unused parameter fs from read_config_line()
248 * Revision 1.26 2001/09/22 14:05:22 jongfoster
249 * Bugfix: Multiple escaped "#" characters in a configuration
250 * file are now permitted.
251 * Also removing 3 unused headers.
253 * Revision 1.25 2001/09/13 22:44:03 jongfoster
254 * Adding {} to an if statement
256 * Revision 1.24 2001/07/30 22:08:36 jongfoster
257 * Tidying up #defines:
258 * - All feature #defines are now of the form FEATURE_xxx
259 * - Permanently turned off WIN_GUI_EDIT
260 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
262 * Revision 1.23 2001/07/20 15:51:54 oes
263 * Fixed indentation of prepocessor commands
265 * Revision 1.22 2001/07/20 15:16:17 haroon
266 * - per Guy's suggestion, added a while loop in sweep() to catch not just
267 * the last inactive CSP but all other consecutive inactive CSPs after that
270 * Revision 1.21 2001/07/18 17:26:24 oes
271 * Changed to conform to new pcrs interface
273 * Revision 1.20 2001/07/17 13:07:01 oes
274 * Fixed segv when last line in config files
275 * lacked a terminating (\r)\n
277 * Revision 1.19 2001/07/13 14:01:54 oes
278 * Removed all #ifdef PCRS
280 * Revision 1.18 2001/06/29 21:45:41 oes
281 * Indentation, CRLF->LF, Tab-> Space
283 * Revision 1.17 2001/06/29 13:31:51 oes
286 * Revision 1.16 2001/06/09 10:55:28 jongfoster
287 * Changing BUFSIZ ==> BUFFER_SIZE
289 * Revision 1.15 2001/06/07 23:14:14 jongfoster
290 * Removing ACL and forward file loaders - these
291 * files have been merged into the config file.
292 * Cosmetic: Moving unloader funcs next to their
293 * respective loader funcs
295 * Revision 1.14 2001/06/01 03:27:04 oes
296 * Fixed line continuation problem
298 * Revision 1.13 2001/05/31 21:28:49 jongfoster
299 * Removed all permissionsfile code - it's now called the actions
300 * file, and (almost) all the code is in actions.c
302 * Revision 1.12 2001/05/31 17:32:31 oes
304 * - Enhanced domain part globbing with infix and prefix asterisk
305 * matching and optional unanchored operation
307 * Revision 1.11 2001/05/29 23:25:24 oes
309 * - load_config_line() and load_permissions_file() now use chomp()
311 * Revision 1.10 2001/05/29 09:50:24 jongfoster
312 * Unified blocklist/imagelist/permissionslist.
313 * File format is still under discussion, but the internal changes
316 * Also modified interceptor behaviour:
317 * - We now intercept all URLs beginning with one of the following
318 * prefixes (and *only* these prefixes):
320 * * http://ijbswa.sf.net/config/
321 * * http://ijbswa.sourceforge.net/config/
322 * - New interceptors "home page" - go to http://i.j.b/ to see it.
323 * - Internal changes so that intercepted and fast redirect pages
324 * are not replaced with an image.
325 * - Interceptors now have the option to send a binary page direct
326 * to the client. (i.e. ijb-send-banner uses this)
327 * - Implemented show-url-info interceptor. (Which is why I needed
328 * the above interceptors changes - a typical URL is
329 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
330 * The previous mechanism would not have intercepted that, and
331 * if it had been intercepted then it then it would have replaced
334 * Revision 1.9 2001/05/26 17:12:07 jongfoster
335 * Fatal errors loading configuration files now give better error messages.
337 * Revision 1.8 2001/05/26 00:55:20 jongfoster
338 * Removing duplicated code. load_forwardfile() now uses create_url_spec()
340 * Revision 1.7 2001/05/26 00:28:36 jongfoster
341 * Automatic reloading of config file.
342 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
343 * Most of the global variables have been moved to a new
344 * struct configuration_spec, accessed through csp->config->globalname
345 * Most of the globals remaining are used by the Win32 GUI.
347 * Revision 1.6 2001/05/23 12:27:33 oes
349 * Fixed ugly indentation of my last changes
351 * Revision 1.5 2001/05/23 10:39:05 oes
352 * - Added support for escaping the comment character
353 * in config files by a backslash
354 * - Added support for line continuation in config
356 * - Fixed a buffer overflow bug with long config lines
358 * Revision 1.4 2001/05/22 18:56:28 oes
361 * Revision 1.3 2001/05/20 01:21:20 jongfoster
362 * Version 2.9.4 checkin.
363 * - Merged popupfile and cookiefile, and added control over PCRS
364 * filtering, in new "permissionsfile".
365 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
366 * file error you now get a message box (in the Win32 GUI) rather
367 * than the program exiting with no explanation.
368 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
370 * - Removed tabs from "config"
371 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
372 * - Bumped up version number.
374 * Revision 1.2 2001/05/17 23:01:01 oes
375 * - Cleaned CRLF's from the sources and related files
377 * Revision 1.1.1.1 2001/05/15 13:58:59 oes
378 * Initial import of version 2.9.3 source tree
381 *********************************************************************/
388 #include <sys/types.h>
391 #include <sys/stat.h>
395 #if !defined(_WIN32) && !defined(__OS2__)
405 #include "miscutil.h"
408 #include "urlmatch.h"
411 const char loaders_h_rcs[] = LOADERS_H_VERSION;
414 * Currently active files.
415 * These are also entered in the main linked list of files.
419 static struct file_list *current_trustfile = NULL;
420 #endif /* def FEATURE_TRUST */
422 static int load_one_re_filterfile(struct client_state *csp, int fileid);
424 static struct file_list *current_re_filterfile[MAX_AF_FILES] = {
425 NULL, NULL, NULL, NULL, NULL,
426 NULL, NULL, NULL, NULL, NULL
430 * Pseudo filter type for load_one_re_filterfile
432 #define NO_NEW_FILTER -1
435 /*********************************************************************
439 * Description : Basically a mark and sweep garbage collector, it is run
440 * (by the parent thread) every once in a while to reclaim memory.
442 * It uses a mark and sweep strategy:
443 * 1) mark all files as inactive
445 * 2) check with each client:
446 * if it is active, mark its files as active
447 * if it is inactive, free its resources
449 * 3) free the resources of all of the files that
450 * are still marked as inactive (and are obsolete).
452 * N.B. files that are not obsolete don't have an unloader defined.
456 * Returns : The number of threads that are still active.
458 *********************************************************************/
459 unsigned int sweep(void)
461 struct file_list *fl, *nfl;
462 struct client_state *csp, *last_active;
464 unsigned int active_threads = 0;
466 /* clear all of the file's active flags */
467 for ( fl = files->next; NULL != fl; fl = fl->next )
472 last_active = clients;
477 if (csp->flags & CSP_FLAG_ACTIVE)
479 /* Mark this client's files as active */
482 * Always have a configuration file.
483 * (Also note the slightly non-standard extra
486 csp->config->config_file_list->active = 1;
491 for (i = 0; i < MAX_AF_FILES; i++)
493 if (csp->actions_list[i])
495 csp->actions_list[i]->active = 1;
502 for (i = 0; i < MAX_AF_FILES; i++)
506 csp->rlist[i]->active = 1;
516 csp->tlist->active = 1;
518 #endif /* def FEATURE_TRUST */
527 * This client is not active. Free its resources.
530 last_active->next = csp->next;
532 freez(csp->ip_addr_str);
533 freez(csp->iob->buf);
534 freez(csp->error_message);
536 if (csp->action->flags & ACTION_FORWARD_OVERRIDE &&
539 unload_forward_spec(csp->fwd);
541 free_http_request(csp->http);
543 destroy_list(csp->headers);
544 destroy_list(csp->tags);
546 free_current_action(csp->action);
548 #ifdef FEATURE_STATISTICS
550 if (csp->flags & CSP_FLAG_REJECTED)
554 #endif /* def FEATURE_STATISTICS */
558 csp = last_active->next;
567 if ( ( 0 == fl->active ) && ( NULL != fl->unloader ) )
569 nfl->next = fl->next;
571 (fl->unloader)(fl->f);
585 return active_threads;
590 /*********************************************************************
592 * Function : check_file_changed
594 * Description : Helper function to check if a file needs reloading.
595 * If "current" is still current, return it. Otherwise
596 * allocates a new (zeroed) "struct file_list", fills
597 * in the disk file name and timestamp, and returns it.
600 * 1 : current = The file_list currently being used - will
601 * be checked to see if it is out of date.
602 * May be NULL (which is treated as out of
604 * 2 : filename = Name of file to check.
605 * 3 : newfl = New file list. [Output only]
606 * This will be set to NULL, OR a struct
607 * file_list newly allocated on the
608 * heap, with the filename and lastmodified
609 * fields filled, and all others zeroed.
611 * Returns : If file unchanged: 0 (and sets newfl == NULL)
612 * If file changed: 1 and sets newfl != NULL
613 * On error: 1 and sets newfl == NULL
615 *********************************************************************/
616 int check_file_changed(const struct file_list * current,
617 const char * filename,
618 struct file_list ** newfl)
620 struct file_list *fs;
621 struct stat statbuf[1];
625 if (stat(filename, statbuf) < 0)
627 /* Error, probably file not found. */
632 && (current->lastmodified == statbuf->st_mtime)
633 && (0 == strcmp(current->filename, filename)))
638 fs = (struct file_list *)zalloc(sizeof(struct file_list));
641 /* Out of memory error */
646 fs->filename = strdup(filename);
647 fs->lastmodified = statbuf->st_mtime;
649 if (fs->filename == NULL)
651 /* Out of memory error */
660 /*********************************************************************
662 * Function : simple_read_line
664 * Description : Read a single line from a file and return it.
665 * This is basically a version of fgets() that malloc()s
666 * it's own line buffer. Note that the buffer will
667 * always be a multiple of BUFFER_SIZE bytes long.
668 * Therefore if you are going to keep the string for
669 * an extended period of time, you should probably
670 * strdup() it and free() the original, to save memory.
674 * 1 : dest = destination for newly malloc'd pointer to
675 * line data. Will be set to NULL on error.
676 * 2 : fp = File to read from
677 * 3 : newline = Standard for newlines in the file.
678 * Will be unchanged if it's value on input is not
680 * On output, may be changed from NEWLINE_UNKNOWN to
681 * actual convention in file.
683 * Returns : JB_ERR_OK on success
684 * JB_ERR_MEMORY on out-of-memory
685 * JB_ERR_FILE on EOF.
687 *********************************************************************/
688 jb_err simple_read_line(FILE *fp, char **dest, int *newline)
691 size_t buflen = BUFFER_SIZE;
695 int realnewline = NEWLINE_UNKNOWN;
697 if (NULL == (buf = malloc(buflen)))
699 return JB_ERR_MEMORY;
705 * Character codes. If you have a wierd compiler and the following are
706 * incorrect, you also need to fix NEWLINE() in loaders.h
708 #define CHAR_CR '\r' /* ASCII 13 */
709 #define CHAR_LF '\n' /* ASCII 10 */
729 else if (ch == CHAR_CR)
734 if (*newline == NEWLINE_UNKNOWN)
736 *newline = NEWLINE_DOS;
745 if (*newline == NEWLINE_UNKNOWN)
747 *newline = NEWLINE_MAC;
752 if (*newline == NEWLINE_UNKNOWN)
754 *newline = realnewline;
758 else if (ch == CHAR_LF)
762 if (*newline == NEWLINE_UNKNOWN)
764 *newline = NEWLINE_UNIX;
779 buflen += BUFFER_SIZE;
780 if (NULL == (p = realloc(buf, buflen)))
783 return JB_ERR_MEMORY;
792 /*********************************************************************
794 * Function : edit_read_line
796 * Description : Read a single non-empty line from a file and return
797 * it. Trims comments, leading and trailing whitespace
798 * and respects escaping of newline and comment char.
799 * Provides the line in 2 alternative forms: raw and
801 * - raw is the raw data read from the file. If the
802 * line is not modified, then this should be written
804 * - prefix is any comments and blank lines that were
805 * read from the file. If the line is modified, then
806 * this should be written out to the file followed
807 * by the modified data. (If this string is non-empty
808 * then it will have a newline at the end).
809 * - data is the actual data that will be parsed
810 * further by appropriate routines.
811 * On EOF, the 3 strings will all be set to NULL and
812 * 0 will be returned.
815 * 1 : fp = File to read from
816 * 2 : raw_out = destination for newly malloc'd pointer to
817 * raw line data. May be NULL if you don't want it.
818 * 3 : prefix_out = destination for newly malloc'd pointer to
819 * comments. May be NULL if you don't want it.
820 * 4 : data_out = destination for newly malloc'd pointer to
821 * line data with comments and leading/trailing spaces
822 * removed, and line continuation performed. May be
823 * NULL if you don't want it.
824 * 5 : newline = Standard for newlines in the file.
825 * On input, set to value to use or NEWLINE_UNKNOWN.
826 * On output, may be changed from NEWLINE_UNKNOWN to
827 * actual convention in file. May be NULL if you
829 * 6 : line_number = Line number in file. In "lines" as
830 * reported by a text editor, not lines containing data.
832 * Returns : JB_ERR_OK on success
833 * JB_ERR_MEMORY on out-of-memory
834 * JB_ERR_FILE on EOF.
836 *********************************************************************/
837 jb_err edit_read_line(FILE *fp,
842 unsigned long *line_number)
844 char *p; /* Temporary pointer */
845 char *linebuf; /* Line read from file */
846 char *linestart; /* Start of linebuf, usually first non-whitespace char */
847 int contflag = 0; /* Nonzero for line continuation - i.e. line ends '\' */
848 int is_empty = 1; /* Flag if not got any data yet */
849 char *raw = NULL; /* String to be stored in raw_out */
850 char *prefix = NULL; /* String to be stored in prefix_out */
851 char *data = NULL; /* String to be stored in data_out */
852 int scrapnewline; /* Used for (*newline) if newline==NULL */
853 jb_err rval = JB_ERR_OK;
856 assert(raw_out || data_out);
857 assert(newline == NULL
858 || *newline == NEWLINE_UNKNOWN
859 || *newline == NEWLINE_UNIX
860 || *newline == NEWLINE_DOS
861 || *newline == NEWLINE_MAC);
865 scrapnewline = NEWLINE_UNKNOWN;
866 newline = &scrapnewline;
869 /* Set output parameters to NULL */
883 /* Set string variables to new, empty strings. */
890 return JB_ERR_MEMORY;
899 return JB_ERR_MEMORY;
909 return JB_ERR_MEMORY;
913 /* Main loop. Loop while we need more data & it's not EOF. */
915 while ( (contflag || is_empty)
916 && (JB_ERR_OK == (rval = simple_read_line(fp, &linebuf, newline))))
924 string_append(&raw,linebuf);
925 if (string_append(&raw,NEWLINE(*newline)))
930 return JB_ERR_MEMORY;
934 /* Line continuation? Trim escape and set flag. */
935 p = linebuf + strlen(linebuf) - 1;
936 contflag = ((*linebuf != '\0') && (*p == '\\'));
942 /* Trim leading spaces if we're at the start of the line */
944 assert(NULL != data);
947 /* Trim leading spaces */
948 while (*linestart && isspace((int)(unsigned char)*linestart))
954 /* Handle comment characters. */
956 while ((p = strchr(p, '#')) != NULL)
958 /* Found a comment char.. */
959 if ((p != linebuf) && (*(p-1) == '\\'))
961 /* ..and it's escaped, left-shift the line over the escape. */
963 while ((*q = *(q + 1)) != '\0')
967 /* Now scan from just after the "#". */
971 /* Real comment. Save it... */
974 /* Special case: Line only contains a comment, so all the
975 * previous whitespace is considered part of the comment.
976 * Undo the whitespace skipping, if any.
983 string_append(&prefix,p);
984 if (string_append(&prefix, NEWLINE(*newline)))
989 return JB_ERR_MEMORY;
993 /* ... and chop off the rest of the line */
996 } /* END while (there's a # character) */
998 /* Write to the buffer */
1004 if (string_append(&data, linestart))
1009 return JB_ERR_MEMORY;
1015 } /* END while(we need more data) */
1017 /* Handle simple_read_line() errors - ignore EOF */
1018 if ((rval != JB_ERR_OK) && (rval != JB_ERR_FILE))
1026 if (raw ? (*raw == '\0') : is_empty)
1028 /* EOF and no data there. (Definition of "data" depends on whether
1029 * the caller cares about "raw" or just "data").
1040 /* Got at least some data */
1042 /* Remove trailing whitespace */
1055 *prefix_out = prefix;
1074 /*********************************************************************
1076 * Function : read_config_line
1078 * Description : Read a single non-empty line from a file and return
1079 * it. Trims comments, leading and trailing whitespace
1080 * and respects escaping of newline and comment char.
1083 * 1 : buf = Buffer to use.
1084 * 2 : buflen = Size of buffer in bytes.
1085 * 3 : fp = File to read from
1086 * 4 : linenum = linenumber in file
1088 * Returns : NULL on EOF or error
1089 * Otherwise, returns buf.
1091 *********************************************************************/
1092 char *read_config_line(char *buf, size_t buflen, FILE *fp, unsigned long *linenum)
1096 err = edit_read_line(fp, NULL, NULL, &buf2, NULL, linenum);
1099 if (err == JB_ERR_MEMORY)
1101 log_error(LOG_LEVEL_FATAL, "Out of memory loading a config file");
1108 assert(strlen(buf2) + 1U < buflen);
1109 strncpy(buf, buf2, buflen - 1);
1111 buf[buflen - 1] = '\0';
1117 #ifdef FEATURE_TRUST
1118 /*********************************************************************
1120 * Function : unload_trustfile
1122 * Description : Unloads a trustfile.
1125 * 1 : f = the data structure associated with the trustfile.
1129 *********************************************************************/
1130 static void unload_trustfile(void *f)
1132 struct block_spec *cur = (struct block_spec *)f;
1133 struct block_spec *next;
1139 free_url_spec(cur->url);
1148 #ifdef FEATURE_GRACEFUL_TERMINATION
1149 /*********************************************************************
1151 * Function : unload_current_trust_file
1153 * Description : Unloads current trust file - reset to state at
1154 * beginning of program.
1160 *********************************************************************/
1161 void unload_current_trust_file(void)
1163 if (current_trustfile)
1165 current_trustfile->unloader = unload_trustfile;
1166 current_trustfile = NULL;
1169 #endif /* FEATURE_GRACEFUL_TERMINATION */
1172 /*********************************************************************
1174 * Function : load_trustfile
1176 * Description : Read and parse a trustfile and add to files list.
1179 * 1 : csp = Current client state (buffers, headers, etc...)
1181 * Returns : 0 => Ok, everything else is an error.
1183 *********************************************************************/
1184 int load_trustfile(struct client_state *csp)
1188 struct block_spec *b, *bl;
1189 struct url_spec **tl;
1191 char buf[BUFFER_SIZE], *p, *q;
1192 int reject, trusted;
1193 struct file_list *fs;
1194 unsigned long linenum = 0;
1195 int trusted_referrers = 0;
1197 if (!check_file_changed(current_trustfile, csp->config->trustfile, &fs))
1199 /* No need to load */
1202 csp->tlist = current_trustfile;
1208 goto load_trustfile_error;
1211 fs->f = bl = (struct block_spec *)zalloc(sizeof(*bl));
1214 goto load_trustfile_error;
1217 if ((fp = fopen(csp->config->trustfile, "r")) == NULL)
1219 goto load_trustfile_error;
1222 tl = csp->config->trust_list;
1224 while (read_config_line(buf, sizeof(buf), fp, &linenum) != NULL)
1240 while ((*p++ = *q++) != '\0')
1246 /* skip blank lines */
1252 /* allocate a new node */
1253 if ((b = zalloc(sizeof(*b))) == NULL)
1256 goto load_trustfile_error;
1259 /* add it to the list */
1265 /* Save the URL pattern */
1266 if (create_url_spec(b->url, buf))
1269 goto load_trustfile_error;
1273 * save a pointer to URL's spec in the list of trusted URL's, too
1277 if(++trusted_referrers < MAX_TRUSTED_REFERRERS)
1284 if(trusted_referrers >= MAX_TRUSTED_REFERRERS)
1287 * FIXME: ... after Privoxy 3.0.4 is out.
1289 log_error(LOG_LEVEL_ERROR, "Too many trusted referrers. Current limit is %d, you are using %d.\n"
1290 " Additional trusted referrers are treated like ordinary trusted URLs.\n"
1291 " (You can increase this limit by changing MAX_TRUSTED_REFERRERS in project.h and recompiling).",
1292 MAX_TRUSTED_REFERRERS, trusted_referrers);
1299 /* the old one is now obsolete */
1300 if (current_trustfile)
1302 current_trustfile->unloader = unload_trustfile;
1305 fs->next = files->next;
1307 current_trustfile = fs;
1316 load_trustfile_error:
1317 log_error(LOG_LEVEL_FATAL, "can't load trustfile '%s': %E",
1318 csp->config->trustfile);
1322 #endif /* def FEATURE_TRUST */
1325 /*********************************************************************
1327 * Function : unload_re_filterfile
1329 * Description : Unload the re_filter list by freeing all chained
1330 * re_filterfile specs and their data.
1333 * 1 : f = the data structure associated with the filterfile.
1337 *********************************************************************/
1338 static void unload_re_filterfile(void *f)
1340 struct re_filterfile_spec *a, *b = (struct re_filterfile_spec *)f;
1346 destroy_list(b->patterns);
1347 pcrs_free_joblist(b->joblist);
1349 freez(b->description);
1358 /*********************************************************************
1360 * Function : unload_forward_spec
1362 * Description : Unload the forward spec settings by freeing all
1363 * memory referenced by members and the memory for
1367 * 1 : fwd = the forward spec.
1371 *********************************************************************/
1372 void unload_forward_spec(struct forward_spec *fwd)
1374 free_url_spec(fwd->url);
1375 freez(fwd->gateway_host);
1376 freez(fwd->forward_host);
1383 #ifdef FEATURE_GRACEFUL_TERMINATION
1384 /*********************************************************************
1386 * Function : unload_current_re_filterfile
1388 * Description : Unloads current re_filter file - reset to state at
1389 * beginning of program.
1395 *********************************************************************/
1396 void unload_current_re_filterfile(void)
1400 for (i = 0; i < MAX_AF_FILES; i++)
1402 if (current_re_filterfile[i])
1404 current_re_filterfile[i]->unloader = unload_re_filterfile;
1405 current_re_filterfile[i] = NULL;
1412 /*********************************************************************
1414 * Function : load_re_filterfiles
1416 * Description : Loads all the filterfiles.
1417 * Generate a chained list of re_filterfile_spec's from
1418 * the "FILTER: " blocks, compiling all their substitutions
1419 * into chained lists of pcrs_job structs.
1422 * 1 : csp = Current client state (buffers, headers, etc...)
1424 * Returns : 0 => Ok, everything else is an error.
1426 *********************************************************************/
1427 int load_re_filterfiles(struct client_state *csp)
1432 for (i = 0; i < MAX_AF_FILES; i++)
1434 if (csp->config->re_filterfile[i])
1436 result = load_one_re_filterfile(csp, i);
1442 else if (current_re_filterfile[i])
1444 current_re_filterfile[i]->unloader = unload_re_filterfile;
1445 current_re_filterfile[i] = NULL;
1453 /*********************************************************************
1455 * Function : load_one_re_filterfile
1457 * Description : Load a re_filterfile.
1458 * Generate a chained list of re_filterfile_spec's from
1459 * the "FILTER: " blocks, compiling all their substitutions
1460 * into chained lists of pcrs_job structs.
1463 * 1 : csp = Current client state (buffers, headers, etc...)
1465 * Returns : 0 => Ok, everything else is an error.
1467 *********************************************************************/
1468 int load_one_re_filterfile(struct client_state *csp, int fileid)
1472 struct re_filterfile_spec *new_bl, *bl = NULL;
1473 struct file_list *fs;
1475 char buf[BUFFER_SIZE];
1477 unsigned long linenum = 0;
1478 pcrs_job *dummy, *lastjob = NULL;
1481 * No need to reload if unchanged
1483 if (!check_file_changed(current_re_filterfile[fileid], csp->config->re_filterfile[fileid], &fs))
1487 csp->rlist[fileid] = current_re_filterfile[fileid];
1493 goto load_re_filterfile_error;
1497 * Open the file or fail
1499 if ((fp = fopen(csp->config->re_filterfile[fileid], "r")) == NULL)
1501 goto load_re_filterfile_error;
1507 while (read_config_line(buf, sizeof(buf), fp, &linenum) != NULL)
1509 int new_filter = NO_NEW_FILTER;
1511 if (strncmp(buf, "FILTER:", 7) == 0)
1513 new_filter = FT_CONTENT_FILTER;
1515 else if (strncmp(buf, "SERVER-HEADER-FILTER:", 21) == 0)
1517 new_filter = FT_SERVER_HEADER_FILTER;
1519 else if (strncmp(buf, "CLIENT-HEADER-FILTER:", 21) == 0)
1521 new_filter = FT_CLIENT_HEADER_FILTER;
1523 else if (strncmp(buf, "CLIENT-HEADER-TAGGER:", 21) == 0)
1525 new_filter = FT_CLIENT_HEADER_TAGGER;
1527 else if (strncmp(buf, "SERVER-HEADER-TAGGER:", 21) == 0)
1529 new_filter = FT_SERVER_HEADER_TAGGER;
1533 * If this is the head of a new filter block, make it a
1534 * re_filterfile spec of its own and chain it to the list:
1536 if (new_filter != NO_NEW_FILTER)
1538 new_bl = (struct re_filterfile_spec *)zalloc(sizeof(*bl));
1541 goto load_re_filterfile_error;
1543 if (new_filter == FT_CONTENT_FILTER)
1545 new_bl->name = chomp(buf + 7);
1549 new_bl->name = chomp(buf + 21);
1551 new_bl->type = new_filter;
1554 * If a filter description is available,
1555 * encode it to HTML and save it.
1557 if (NULL != (new_bl->description = strpbrk(new_bl->name, " \t")))
1559 *new_bl->description++ = '\0';
1560 new_bl->description = html_encode(chomp(new_bl->description));
1561 if (NULL == new_bl->description)
1563 new_bl->description = strdup("Out of memory while encoding this filter's description to HTML");
1568 new_bl->description = strdup("No description available for this filter");
1571 new_bl->name = strdup(chomp(new_bl->name));
1574 * If this is the first filter block, chain it
1575 * to the file_list rather than its (nonexistant)
1589 log_error(LOG_LEVEL_RE_FILTER, "Reading in filter \"%s\" (\"%s\")", bl->name, bl->description);
1595 * Else, save the expression, make it a pcrs_job
1596 * and chain it into the current filter's joblist
1600 error = enlist(bl->patterns, buf);
1601 if (JB_ERR_MEMORY == error)
1603 log_error(LOG_LEVEL_FATAL,
1604 "Out of memory while enlisting re_filter job \'%s\' for filter %s.", buf, bl->name);
1606 assert(JB_ERR_OK == error);
1608 if (pcrs_job_is_dynamic(buf))
1611 * Dynamic pattern that might contain variables
1612 * and has to be recompiled for every request
1614 if (bl->joblist != NULL)
1616 pcrs_free_joblist(bl->joblist);
1620 log_error(LOG_LEVEL_RE_FILTER,
1621 "Adding dynamic re_filter job \'%s\' to filter %s succeeded.", buf, bl->name);
1624 else if (bl->dynamic)
1627 * A previous job was dynamic and as we
1628 * recompile the whole filter anyway, it
1629 * makes no sense to compile this job now.
1631 log_error(LOG_LEVEL_RE_FILTER,
1632 "Adding static re_filter job \'%s\' to dynamic filter %s succeeded.", buf, bl->name);
1636 if ((dummy = pcrs_compile_command(buf, &error)) == NULL)
1638 log_error(LOG_LEVEL_ERROR,
1639 "Adding re_filter job \'%s\' to filter %s failed with error %d.", buf, bl->name, error);
1644 if (bl->joblist == NULL)
1646 bl->joblist = dummy;
1648 else if (NULL != lastjob)
1650 lastjob->next = dummy;
1653 log_error(LOG_LEVEL_RE_FILTER, "Adding re_filter job \'%s\' to filter %s succeeded.", buf, bl->name);
1658 log_error(LOG_LEVEL_ERROR, "Ignoring job %s outside filter block in %s, line %d",
1659 buf, csp->config->re_filterfile[fileid], linenum);
1666 * Schedule the now-obsolete old data for unloading
1668 if ( NULL != current_re_filterfile[fileid] )
1670 current_re_filterfile[fileid]->unloader = unload_re_filterfile;
1674 * Chain this file into the global list of loaded files
1676 fs->next = files->next;
1678 current_re_filterfile[fileid] = fs;
1682 csp->rlist[fileid] = fs;
1687 load_re_filterfile_error:
1688 log_error(LOG_LEVEL_FATAL, "can't load re_filterfile '%s': %E",
1689 csp->config->re_filterfile[fileid]);
1695 /*********************************************************************
1697 * Function : add_loader
1699 * Description : Called from `load_config'. Called once for each input
1700 * file found in config.
1703 * 1 : loader = pointer to a function that can parse and load
1704 * the appropriate config file.
1705 * 2 : config = The configuration_spec to add the loader to.
1709 *********************************************************************/
1710 void add_loader(int (*loader)(struct client_state *),
1711 struct configuration_spec * config)
1715 for (i=0; i < NLOADERS; i++)
1717 if (config->loaders[i] == NULL)
1719 config->loaders[i] = loader;
1727 /*********************************************************************
1729 * Function : run_loader
1731 * Description : Called from `load_config' and `listen_loop'. This
1732 * function keeps the "csp" current with any file mods
1733 * since the last loop. If a file is unchanged, the
1734 * loader functions do NOT reload the file.
1737 * 1 : csp = Current client state (buffers, headers, etc...)
1738 * Must be non-null. Reads: "csp->config"
1739 * Writes: various data members.
1741 * Returns : 0 => Ok, everything else is an error.
1743 *********************************************************************/
1744 int run_loader(struct client_state *csp)
1749 for (i=0; i < NLOADERS; i++)
1751 if (csp->config->loaders[i] == NULL)
1755 ret |= (csp->config->loaders[i])(csp);