1 const char loaders_rcs[] = "$Id: loaders.c,v 1.64 2007/06/01 14:12:38 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.64 2007/06/01 14:12:38 fabiankeil
39 * Add unload_forward_spec() in preparation for forward-override{}.
41 * Revision 1.63 2007/05/14 10:41:15 fabiankeil
42 * Ditch the csp member cookie_list[] which isn't used anymore.
44 * Revision 1.62 2007/04/30 15:02:18 fabiankeil
45 * Introduce dynamic pcrs jobs that can resolve variables.
47 * Revision 1.61 2007/04/15 16:39:21 fabiankeil
48 * Introduce tags as alternative way to specify which
49 * actions apply to a request. At the moment tags can be
50 * created based on client and server headers.
52 * Revision 1.60 2007/03/20 15:16:34 fabiankeil
53 * Use dedicated header filter actions instead of abusing "filter".
54 * Replace "filter-client-headers" and "filter-client-headers"
55 * with "server-header-filter" and "client-header-filter".
57 * Revision 1.59 2007/01/25 13:38:20 fabiankeil
58 * Freez csp->error_message in sweep().
60 * Revision 1.58 2006/12/31 14:25:20 fabiankeil
61 * Fix gcc43 compiler warnings.
63 * Revision 1.57 2006/12/21 12:22:22 fabiankeil
64 * html_encode filter descriptions.
66 * Have "Ignoring job ..." error messages
67 * print the filter file name correctly.
69 * Revision 1.56 2006/09/07 10:40:30 fabiankeil
70 * Turns out trusted referrers above our arbitrary
71 * limit are downgraded too ordinary trusted URLs.
72 * Adjusted error message.
74 * Revision 1.55 2006/09/07 10:25:39 fabiankeil
77 * Revision 1.54 2006/09/07 10:22:20 fabiankeil
78 * If too many trusted referrers are used,
79 * print only one error message instead of logging
80 * every single trusted referrer above the arbitrary
83 * Revision 1.53 2006/08/31 16:25:06 fabiankeil
84 * Work around a buffer overflow that caused Privoxy to
85 * segfault if too many trusted referrers were used. Good
86 * enough for now, but should be replaced with a real
87 * solution after the next release.
89 * Revision 1.52 2006/07/18 14:48:46 david__schmidt
90 * Reorganizing the repository: swapping out what was HEAD (the old 3.1 branch)
91 * with what was really the latest development (the v_3_0_branch branch)
93 * Revision 1.50.2.8 2006/01/30 15:16:25 david__schmidt
94 * Remove a little residual debugging info
96 * Revision 1.50.2.7 2006/01/29 23:10:56 david__schmidt
97 * Multiple filter file support
99 * Revision 1.50.2.6 2003/10/24 10:17:54 oes
100 * Nit: Allowed tabs as separators in filter headings
102 * Revision 1.50.2.5 2003/05/08 15:19:15 oes
103 * sweep: Made loop structure of sweep step mirror that of mark step
105 * Revision 1.50.2.4 2003/05/06 15:57:12 oes
106 * Bugfix: Update last_active pointer in sweep() before
107 * leaving an active client. Closes bugs #724395, #727882
109 * Revision 1.50.2.3 2002/11/20 17:12:30 oes
110 * Ooops, forgot one change.
112 * Revision 1.50.2.2 2002/11/20 14:38:15 oes
113 * Fixed delayed/incomplete freeing of client resources and
114 * simplified loop structure in sweep.
115 * Thanks to Oliver Stoeneberg for the hint.
117 * Revision 1.50.2.1 2002/07/26 15:19:24 oes
118 * - PCRS jobs now chained in order of appearance. Previous
119 * reverse chaining was counter-intuitive.
120 * - Changed loglevel of PCRS job compile errors to
123 * Revision 1.50 2002/04/24 02:12:16 oes
124 * Jon's multiple AF patch: Sweep now takes care of all AFs
126 * Revision 1.49 2002/04/19 16:53:25 jongfoster
127 * Optimize away a function call by using an equivalent macro
129 * Revision 1.48 2002/04/05 00:56:09 gliptak
130 * Correcting typo to clean up on realloc failure
132 * Revision 1.47 2002/03/26 22:29:55 swa
133 * we have a new homepage!
135 * Revision 1.46 2002/03/24 13:25:43 swa
136 * name change related issues
138 * Revision 1.45 2002/03/16 23:54:06 jongfoster
139 * Adding graceful termination feature, to help look for memory leaks.
140 * If you enable this (which, by design, has to be done by hand
141 * editing config.h) and then go to http://i.j.b/die, then the program
142 * will exit cleanly after the *next* request. It should free all the
143 * memory that was used.
145 * Revision 1.44 2002/03/16 21:51:00 jongfoster
148 * Revision 1.43 2002/03/16 20:28:34 oes
149 * Added descriptions to the filters so users will know what they select in the cgi editor
151 * Revision 1.42 2002/03/13 00:27:05 jongfoster
154 * Revision 1.41 2002/03/12 01:42:50 oes
155 * Introduced modular filters
157 * Revision 1.40 2002/03/08 17:46:04 jongfoster
158 * Fixing int/size_t warnings
160 * Revision 1.39 2002/03/07 03:46:17 oes
161 * Fixed compiler warnings
163 * Revision 1.38 2002/03/06 22:54:35 jongfoster
164 * Automated function-comment nitpicking.
166 * Revision 1.37 2002/03/03 15:07:49 oes
167 * Re-enabled automatic config reloading
169 * Revision 1.36 2002/01/22 23:46:18 jongfoster
170 * Moving edit_read_line() and simple_read_line() to loaders.c, and
171 * extending them to support reading MS-DOS, Mac and UNIX style files
174 * Modifying read_config_line() (without changing it's prototype) to
175 * be a trivial wrapper for edit_read_line(). This means that we have
176 * one function to read a line and handle comments, which is common
177 * between the initialization code and the edit interface.
179 * Revision 1.35 2002/01/17 21:03:08 jongfoster
180 * Moving all our URL and URL pattern parsing code to urlmatch.c.
182 * Renaming free_url to free_url_spec, since it frees a struct url_spec.
184 * Revision 1.34 2001/12/30 14:07:32 steudten
185 * - Add signal handling (unix)
186 * - Add SIGHUP handler (unix)
187 * - Add creation of pidfile (unix)
188 * - Add action 'top' in rc file (RH)
189 * - Add entry 'SIGNALS' to manpage
190 * - Add exit message to logfile (unix)
192 * Revision 1.33 2001/11/13 00:16:38 jongfoster
193 * Replacing references to malloc.h with the standard stdlib.h
194 * (See ANSI or K&R 2nd Ed)
196 * Revision 1.32 2001/11/07 00:02:13 steudten
197 * Add line number in error output for lineparsing for
198 * actionsfile and configfile.
199 * Special handling for CLF added.
201 * Revision 1.31 2001/10/26 17:39:01 oes
202 * Removed csp->referrer
203 * Moved ijb_isspace and ijb_tolower to project.h
205 * Revision 1.30 2001/10/25 03:40:48 david__schmidt
206 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
207 * threads to call select() simultaneously. So, it's time to do a real, live,
208 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
209 * (native). Both versions will work, but using __OS2__ offers multi-threading.
211 * Revision 1.29 2001/10/23 21:38:53 jongfoster
212 * Adding error-checking to create_url_spec()
214 * Revision 1.28 2001/10/07 15:40:39 oes
215 * Replaced 6 boolean members of csp with one bitmap (csp->flags)
217 * Revision 1.27 2001/09/22 16:36:59 jongfoster
218 * Removing unused parameter fs from read_config_line()
220 * Revision 1.26 2001/09/22 14:05:22 jongfoster
221 * Bugfix: Multiple escaped "#" characters in a configuration
222 * file are now permitted.
223 * Also removing 3 unused headers.
225 * Revision 1.25 2001/09/13 22:44:03 jongfoster
226 * Adding {} to an if statement
228 * Revision 1.24 2001/07/30 22:08:36 jongfoster
229 * Tidying up #defines:
230 * - All feature #defines are now of the form FEATURE_xxx
231 * - Permanently turned off WIN_GUI_EDIT
232 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
234 * Revision 1.23 2001/07/20 15:51:54 oes
235 * Fixed indentation of prepocessor commands
237 * Revision 1.22 2001/07/20 15:16:17 haroon
238 * - per Guy's suggestion, added a while loop in sweep() to catch not just
239 * the last inactive CSP but all other consecutive inactive CSPs after that
242 * Revision 1.21 2001/07/18 17:26:24 oes
243 * Changed to conform to new pcrs interface
245 * Revision 1.20 2001/07/17 13:07:01 oes
246 * Fixed segv when last line in config files
247 * lacked a terminating (\r)\n
249 * Revision 1.19 2001/07/13 14:01:54 oes
250 * Removed all #ifdef PCRS
252 * Revision 1.18 2001/06/29 21:45:41 oes
253 * Indentation, CRLF->LF, Tab-> Space
255 * Revision 1.17 2001/06/29 13:31:51 oes
258 * Revision 1.16 2001/06/09 10:55:28 jongfoster
259 * Changing BUFSIZ ==> BUFFER_SIZE
261 * Revision 1.15 2001/06/07 23:14:14 jongfoster
262 * Removing ACL and forward file loaders - these
263 * files have been merged into the config file.
264 * Cosmetic: Moving unloader funcs next to their
265 * respective loader funcs
267 * Revision 1.14 2001/06/01 03:27:04 oes
268 * Fixed line continuation problem
270 * Revision 1.13 2001/05/31 21:28:49 jongfoster
271 * Removed all permissionsfile code - it's now called the actions
272 * file, and (almost) all the code is in actions.c
274 * Revision 1.12 2001/05/31 17:32:31 oes
276 * - Enhanced domain part globbing with infix and prefix asterisk
277 * matching and optional unanchored operation
279 * Revision 1.11 2001/05/29 23:25:24 oes
281 * - load_config_line() and load_permissions_file() now use chomp()
283 * Revision 1.10 2001/05/29 09:50:24 jongfoster
284 * Unified blocklist/imagelist/permissionslist.
285 * File format is still under discussion, but the internal changes
288 * Also modified interceptor behaviour:
289 * - We now intercept all URLs beginning with one of the following
290 * prefixes (and *only* these prefixes):
292 * * http://ijbswa.sf.net/config/
293 * * http://ijbswa.sourceforge.net/config/
294 * - New interceptors "home page" - go to http://i.j.b/ to see it.
295 * - Internal changes so that intercepted and fast redirect pages
296 * are not replaced with an image.
297 * - Interceptors now have the option to send a binary page direct
298 * to the client. (i.e. ijb-send-banner uses this)
299 * - Implemented show-url-info interceptor. (Which is why I needed
300 * the above interceptors changes - a typical URL is
301 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
302 * The previous mechanism would not have intercepted that, and
303 * if it had been intercepted then it then it would have replaced
306 * Revision 1.9 2001/05/26 17:12:07 jongfoster
307 * Fatal errors loading configuration files now give better error messages.
309 * Revision 1.8 2001/05/26 00:55:20 jongfoster
310 * Removing duplicated code. load_forwardfile() now uses create_url_spec()
312 * Revision 1.7 2001/05/26 00:28:36 jongfoster
313 * Automatic reloading of config file.
314 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
315 * Most of the global variables have been moved to a new
316 * struct configuration_spec, accessed through csp->config->globalname
317 * Most of the globals remaining are used by the Win32 GUI.
319 * Revision 1.6 2001/05/23 12:27:33 oes
321 * Fixed ugly indentation of my last changes
323 * Revision 1.5 2001/05/23 10:39:05 oes
324 * - Added support for escaping the comment character
325 * in config files by a backslash
326 * - Added support for line continuation in config
328 * - Fixed a buffer overflow bug with long config lines
330 * Revision 1.4 2001/05/22 18:56:28 oes
333 * Revision 1.3 2001/05/20 01:21:20 jongfoster
334 * Version 2.9.4 checkin.
335 * - Merged popupfile and cookiefile, and added control over PCRS
336 * filtering, in new "permissionsfile".
337 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
338 * file error you now get a message box (in the Win32 GUI) rather
339 * than the program exiting with no explanation.
340 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
342 * - Removed tabs from "config"
343 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
344 * - Bumped up version number.
346 * Revision 1.2 2001/05/17 23:01:01 oes
347 * - Cleaned CRLF's from the sources and related files
349 * Revision 1.1.1.1 2001/05/15 13:58:59 oes
350 * Initial import of version 2.9.3 source tree
353 *********************************************************************/
360 #include <sys/types.h>
363 #include <sys/stat.h>
367 #if !defined(_WIN32) && !defined(__OS2__)
377 #include "miscutil.h"
380 #include "urlmatch.h"
383 const char loaders_h_rcs[] = LOADERS_H_VERSION;
386 * Currently active files.
387 * These are also entered in the main linked list of files.
391 static struct file_list *current_trustfile = NULL;
392 #endif /* def FEATURE_TRUST */
394 static int load_one_re_filterfile(struct client_state *csp, int fileid);
396 static struct file_list *current_re_filterfile[MAX_AF_FILES] = {
397 NULL, NULL, NULL, NULL, NULL,
398 NULL, NULL, NULL, NULL, NULL
402 * Pseudo filter type for load_one_re_filterfile
404 #define NO_NEW_FILTER -1
407 /*********************************************************************
411 * Description : Basically a mark and sweep garbage collector, it is run
412 * (by the parent thread) every once in a while to reclaim memory.
414 * It uses a mark and sweep strategy:
415 * 1) mark all files as inactive
417 * 2) check with each client:
418 * if it is active, mark its files as active
419 * if it is inactive, free its resources
421 * 3) free the resources of all of the files that
422 * are still marked as inactive (and are obsolete).
424 * N.B. files that are not obsolete don't have an unloader defined.
430 *********************************************************************/
433 struct file_list *fl, *nfl;
434 struct client_state *csp, *last_active;
437 /* clear all of the file's active flags */
438 for ( fl = files->next; NULL != fl; fl = fl->next )
443 last_active = clients;
448 if (csp->flags & CSP_FLAG_ACTIVE)
450 /* Mark this client's files as active */
453 * Always have a configuration file.
454 * (Also note the slightly non-standard extra
457 csp->config->config_file_list->active = 1;
462 for (i = 0; i < MAX_AF_FILES; i++)
464 if (csp->actions_list[i])
466 csp->actions_list[i]->active = 1;
473 for (i = 0; i < MAX_AF_FILES; i++)
477 csp->rlist[i]->active = 1;
487 csp->tlist->active = 1;
489 #endif /* def FEATURE_TRUST */
497 * This client is not active. Free its resources.
500 last_active->next = csp->next;
502 freez(csp->ip_addr_str);
503 freez(csp->my_ip_addr_str);
504 freez(csp->my_hostname);
505 freez(csp->iob->buf);
506 freez(csp->error_message);
508 if (csp->action->flags & ACTION_FORWARD_OVERRIDE &&
511 unload_forward_spec(csp->fwd);
513 free_http_request(csp->http);
515 destroy_list(csp->headers);
516 destroy_list(csp->tags);
518 free_current_action(csp->action);
520 #ifdef FEATURE_STATISTICS
522 if (csp->flags & CSP_FLAG_REJECTED)
526 #endif /* def FEATURE_STATISTICS */
530 csp = last_active->next;
539 if ( ( 0 == fl->active ) && ( NULL != fl->unloader ) )
541 nfl->next = fl->next;
543 (fl->unloader)(fl->f);
560 /*********************************************************************
562 * Function : check_file_changed
564 * Description : Helper function to check if a file needs reloading.
565 * If "current" is still current, return it. Otherwise
566 * allocates a new (zeroed) "struct file_list", fills
567 * in the disk file name and timestamp, and returns it.
570 * 1 : current = The file_list currently being used - will
571 * be checked to see if it is out of date.
572 * May be NULL (which is treated as out of
574 * 2 : filename = Name of file to check.
575 * 3 : newfl = New file list. [Output only]
576 * This will be set to NULL, OR a struct
577 * file_list newly allocated on the
578 * heap, with the filename and lastmodified
579 * fields filled, and all others zeroed.
581 * Returns : If file unchanged: 0 (and sets newfl == NULL)
582 * If file changed: 1 and sets newfl != NULL
583 * On error: 1 and sets newfl == NULL
585 *********************************************************************/
586 int check_file_changed(const struct file_list * current,
587 const char * filename,
588 struct file_list ** newfl)
590 struct file_list *fs;
591 struct stat statbuf[1];
595 if (stat(filename, statbuf) < 0)
597 /* Error, probably file not found. */
602 && (current->lastmodified == statbuf->st_mtime)
603 && (0 == strcmp(current->filename, filename)))
608 fs = (struct file_list *)zalloc(sizeof(struct file_list));
611 /* Out of memory error */
616 fs->filename = strdup(filename);
617 fs->lastmodified = statbuf->st_mtime;
619 if (fs->filename == NULL)
621 /* Out of memory error */
630 /*********************************************************************
632 * Function : simple_read_line
634 * Description : Read a single line from a file and return it.
635 * This is basically a version of fgets() that malloc()s
636 * it's own line buffer. Note that the buffer will
637 * always be a multiple of BUFFER_SIZE bytes long.
638 * Therefore if you are going to keep the string for
639 * an extended period of time, you should probably
640 * strdup() it and free() the original, to save memory.
644 * 1 : dest = destination for newly malloc'd pointer to
645 * line data. Will be set to NULL on error.
646 * 2 : fp = File to read from
647 * 3 : newline = Standard for newlines in the file.
648 * Will be unchanged if it's value on input is not
650 * On output, may be changed from NEWLINE_UNKNOWN to
651 * actual convention in file.
653 * Returns : JB_ERR_OK on success
654 * JB_ERR_MEMORY on out-of-memory
655 * JB_ERR_FILE on EOF.
657 *********************************************************************/
658 jb_err simple_read_line(FILE *fp, char **dest, int *newline)
661 size_t buflen = BUFFER_SIZE;
665 int realnewline = NEWLINE_UNKNOWN;
667 if (NULL == (buf = malloc(buflen)))
669 return JB_ERR_MEMORY;
675 * Character codes. If you have a wierd compiler and the following are
676 * incorrect, you also need to fix NEWLINE() in loaders.h
678 #define CHAR_CR '\r' /* ASCII 13 */
679 #define CHAR_LF '\n' /* ASCII 10 */
699 else if (ch == CHAR_CR)
704 if (*newline == NEWLINE_UNKNOWN)
706 *newline = NEWLINE_DOS;
715 if (*newline == NEWLINE_UNKNOWN)
717 *newline = NEWLINE_MAC;
722 if (*newline == NEWLINE_UNKNOWN)
724 *newline = realnewline;
728 else if (ch == CHAR_LF)
732 if (*newline == NEWLINE_UNKNOWN)
734 *newline = NEWLINE_UNIX;
749 buflen += BUFFER_SIZE;
750 if (NULL == (p = realloc(buf, buflen)))
753 return JB_ERR_MEMORY;
762 /*********************************************************************
764 * Function : edit_read_line
766 * Description : Read a single non-empty line from a file and return
767 * it. Trims comments, leading and trailing whitespace
768 * and respects escaping of newline and comment char.
769 * Provides the line in 2 alternative forms: raw and
771 * - raw is the raw data read from the file. If the
772 * line is not modified, then this should be written
774 * - prefix is any comments and blank lines that were
775 * read from the file. If the line is modified, then
776 * this should be written out to the file followed
777 * by the modified data. (If this string is non-empty
778 * then it will have a newline at the end).
779 * - data is the actual data that will be parsed
780 * further by appropriate routines.
781 * On EOF, the 3 strings will all be set to NULL and
782 * 0 will be returned.
785 * 1 : fp = File to read from
786 * 2 : raw_out = destination for newly malloc'd pointer to
787 * raw line data. May be NULL if you don't want it.
788 * 3 : prefix_out = destination for newly malloc'd pointer to
789 * comments. May be NULL if you don't want it.
790 * 4 : data_out = destination for newly malloc'd pointer to
791 * line data with comments and leading/trailing spaces
792 * removed, and line continuation performed. May be
793 * NULL if you don't want it.
794 * 5 : newline = Standard for newlines in the file.
795 * On input, set to value to use or NEWLINE_UNKNOWN.
796 * On output, may be changed from NEWLINE_UNKNOWN to
797 * actual convention in file. May be NULL if you
799 * 6 : line_number = Line number in file. In "lines" as
800 * reported by a text editor, not lines containing data.
802 * Returns : JB_ERR_OK on success
803 * JB_ERR_MEMORY on out-of-memory
804 * JB_ERR_FILE on EOF.
806 *********************************************************************/
807 jb_err edit_read_line(FILE *fp,
812 unsigned long *line_number)
814 char *p; /* Temporary pointer */
815 char *linebuf; /* Line read from file */
816 char *linestart; /* Start of linebuf, usually first non-whitespace char */
817 int contflag = 0; /* Nonzero for line continuation - i.e. line ends '\' */
818 int is_empty = 1; /* Flag if not got any data yet */
819 char *raw = NULL; /* String to be stored in raw_out */
820 char *prefix = NULL; /* String to be stored in prefix_out */
821 char *data = NULL; /* String to be stored in data_out */
822 int scrapnewline; /* Used for (*newline) if newline==NULL */
823 jb_err rval = JB_ERR_OK;
826 assert(raw_out || data_out);
827 assert(newline == NULL
828 || *newline == NEWLINE_UNKNOWN
829 || *newline == NEWLINE_UNIX
830 || *newline == NEWLINE_DOS
831 || *newline == NEWLINE_MAC);
835 scrapnewline = NEWLINE_UNKNOWN;
836 newline = &scrapnewline;
839 /* Set output parameters to NULL */
853 /* Set string variables to new, empty strings. */
857 if ((raw = malloc(1)) == NULL)
859 return JB_ERR_MEMORY;
865 if ((prefix = malloc(1)) == NULL)
868 return JB_ERR_MEMORY;
874 if ((data = malloc(1)) == NULL)
878 return JB_ERR_MEMORY;
883 /* Main loop. Loop while we need more data & it's not EOF. */
885 while ( (contflag || is_empty)
886 && (JB_ERR_OK == (rval = simple_read_line(fp, &linebuf, newline))))
894 string_append(&raw,linebuf);
895 if (string_append(&raw,NEWLINE(*newline)))
900 return JB_ERR_MEMORY;
904 /* Line continuation? Trim escape and set flag. */
905 p = linebuf + strlen(linebuf) - 1;
906 contflag = ((*linebuf != '\0') && (*p == '\\'));
912 /* Trim leading spaces if we're at the start of the line */
916 /* Trim leading spaces */
917 while (*linestart && isspace((int)(unsigned char)*linestart))
923 /* Handle comment characters. */
925 while ((p = strchr(p, '#')) != NULL)
927 /* Found a comment char.. */
928 if ((p != linebuf) && (*(p-1) == '\\'))
930 /* ..and it's escaped, left-shift the line over the escape. */
932 while ((*q = *(q + 1)) != '\0')
936 /* Now scan from just after the "#". */
940 /* Real comment. Save it... */
943 /* Special case: Line only contains a comment, so all the
944 * previous whitespace is considered part of the comment.
945 * Undo the whitespace skipping, if any.
952 string_append(&prefix,p);
953 if (string_append(&prefix, NEWLINE(*newline)))
958 return JB_ERR_MEMORY;
962 /* ... and chop off the rest of the line */
965 } /* END while (there's a # character) */
967 /* Write to the buffer */
973 if (string_append(&data, linestart))
978 return JB_ERR_MEMORY;
984 } /* END while(we need more data) */
986 /* Handle simple_read_line() errors - ignore EOF */
987 if ((rval != JB_ERR_OK) && (rval != JB_ERR_FILE))
995 if (raw ? (*raw == '\0') : is_empty)
997 /* EOF and no data there. (Definition of "data" depends on whether
998 * the caller cares about "raw" or just "data").
1009 /* Got at least some data */
1011 /* Remove trailing whitespace */
1024 *prefix_out = prefix;
1043 /*********************************************************************
1045 * Function : read_config_line
1047 * Description : Read a single non-empty line from a file and return
1048 * it. Trims comments, leading and trailing whitespace
1049 * and respects escaping of newline and comment char.
1052 * 1 : buf = Buffer to use.
1053 * 2 : buflen = Size of buffer in bytes.
1054 * 3 : fp = File to read from
1055 * 4 : linenum = linenumber in file
1057 * Returns : NULL on EOF or error
1058 * Otherwise, returns buf.
1060 *********************************************************************/
1061 char *read_config_line(char *buf, size_t buflen, FILE *fp, unsigned long *linenum)
1065 err = edit_read_line(fp, NULL, NULL, &buf2, NULL, linenum);
1068 if (err == JB_ERR_MEMORY)
1070 log_error(LOG_LEVEL_FATAL, "Out of memory loading a config file");
1077 assert(strlen(buf2) + 1U < buflen);
1078 strncpy(buf, buf2, buflen - 1);
1080 buf[buflen - 1] = '\0';
1086 #ifdef FEATURE_TRUST
1087 /*********************************************************************
1089 * Function : unload_trustfile
1091 * Description : Unloads a trustfile.
1094 * 1 : f = the data structure associated with the trustfile.
1098 *********************************************************************/
1099 static void unload_trustfile(void *f)
1101 struct block_spec *cur = (struct block_spec *)f;
1102 struct block_spec *next;
1108 free_url_spec(cur->url);
1117 #ifdef FEATURE_GRACEFUL_TERMINATION
1118 /*********************************************************************
1120 * Function : unload_current_trust_file
1122 * Description : Unloads current trust file - reset to state at
1123 * beginning of program.
1129 *********************************************************************/
1130 void unload_current_trust_file(void)
1132 if (current_trustfile)
1134 current_trustfile->unloader = unload_trustfile;
1135 current_trustfile = NULL;
1138 #endif /* FEATURE_GRACEFUL_TERMINATION */
1141 /*********************************************************************
1143 * Function : load_trustfile
1145 * Description : Read and parse a trustfile and add to files list.
1148 * 1 : csp = Current client state (buffers, headers, etc...)
1150 * Returns : 0 => Ok, everything else is an error.
1152 *********************************************************************/
1153 int load_trustfile(struct client_state *csp)
1157 struct block_spec *b, *bl;
1158 struct url_spec **tl;
1160 char buf[BUFFER_SIZE], *p, *q;
1161 int reject, trusted;
1162 struct file_list *fs;
1163 unsigned long linenum = 0;
1164 int trusted_referrers = 0;
1166 if (!check_file_changed(current_trustfile, csp->config->trustfile, &fs))
1168 /* No need to load */
1171 csp->tlist = current_trustfile;
1177 goto load_trustfile_error;
1180 fs->f = bl = (struct block_spec *)zalloc(sizeof(*bl));
1183 goto load_trustfile_error;
1186 if ((fp = fopen(csp->config->trustfile, "r")) == NULL)
1188 goto load_trustfile_error;
1191 tl = csp->config->trust_list;
1193 while (read_config_line(buf, sizeof(buf), fp, &linenum) != NULL)
1209 while ((*p++ = *q++) != '\0')
1215 /* skip blank lines */
1221 /* allocate a new node */
1222 if ((b = zalloc(sizeof(*b))) == NULL)
1225 goto load_trustfile_error;
1228 /* add it to the list */
1234 /* Save the URL pattern */
1235 if (create_url_spec(b->url, buf))
1238 goto load_trustfile_error;
1242 * save a pointer to URL's spec in the list of trusted URL's, too
1246 if(++trusted_referrers < MAX_TRUSTED_REFERRERS)
1253 if(trusted_referrers >= MAX_TRUSTED_REFERRERS)
1256 * FIXME: ... after Privoxy 3.0.4 is out.
1258 log_error(LOG_LEVEL_ERROR, "Too many trusted referrers. Current limit is %d, you are using %d.\n"
1259 " Additional trusted referrers are treated like ordinary trusted URLs.\n"
1260 " (You can increase this limit by changing MAX_TRUSTED_REFERRERS in project.h and recompiling).",
1261 MAX_TRUSTED_REFERRERS, trusted_referrers);
1268 /* the old one is now obsolete */
1269 if (current_trustfile)
1271 current_trustfile->unloader = unload_trustfile;
1274 fs->next = files->next;
1276 current_trustfile = fs;
1285 load_trustfile_error:
1286 log_error(LOG_LEVEL_FATAL, "can't load trustfile '%s': %E",
1287 csp->config->trustfile);
1291 #endif /* def FEATURE_TRUST */
1294 /*********************************************************************
1296 * Function : unload_re_filterfile
1298 * Description : Unload the re_filter list by freeing all chained
1299 * re_filterfile specs and their data.
1302 * 1 : f = the data structure associated with the filterfile.
1306 *********************************************************************/
1307 static void unload_re_filterfile(void *f)
1309 struct re_filterfile_spec *a, *b = (struct re_filterfile_spec *)f;
1315 destroy_list(b->patterns);
1316 pcrs_free_joblist(b->joblist);
1318 freez(b->description);
1327 /*********************************************************************
1329 * Function : unload_forward_spec
1331 * Description : Unload the forward spec settings by freeing all
1332 * memory referenced by members and the memory for
1336 * 1 : fwd = the forward spec.
1340 *********************************************************************/
1341 void unload_forward_spec(struct forward_spec *fwd)
1343 free_url_spec(fwd->url);
1344 freez(fwd->gateway_host);
1345 freez(fwd->forward_host);
1352 #ifdef FEATURE_GRACEFUL_TERMINATION
1353 /*********************************************************************
1355 * Function : unload_current_re_filterfile
1357 * Description : Unloads current re_filter file - reset to state at
1358 * beginning of program.
1364 *********************************************************************/
1365 void unload_current_re_filterfile(void)
1369 for (i = 0; i < MAX_AF_FILES; i++)
1371 if (current_re_filterfile[i])
1373 current_re_filterfile[i]->unloader = unload_re_filterfile;
1374 current_re_filterfile[i] = NULL;
1381 /*********************************************************************
1383 * Function : load_re_filterfile
1385 * Description : Load the re_filterfile.
1386 * Generate a chained list of re_filterfile_spec's from
1387 * the "FILTER: " blocks, compiling all their substitutions
1388 * into chained lists of pcrs_job structs.
1391 * 1 : csp = Current client state (buffers, headers, etc...)
1393 * Returns : 0 => Ok, everything else is an error.
1395 *********************************************************************/
1396 int load_re_filterfile(struct client_state *csp)
1401 for (i = 0; i < MAX_AF_FILES; i++)
1403 if (csp->config->re_filterfile[i])
1405 result = load_one_re_filterfile(csp, i);
1411 else if (current_re_filterfile[i])
1413 current_re_filterfile[i]->unloader = unload_re_filterfile;
1414 current_re_filterfile[i] = NULL;
1422 /*********************************************************************
1424 * Function : load_one_re_filterfile
1426 * Description : Load a re_filterfile.
1427 * Generate a chained list of re_filterfile_spec's from
1428 * the "FILTER: " blocks, compiling all their substitutions
1429 * into chained lists of pcrs_job structs.
1432 * 1 : csp = Current client state (buffers, headers, etc...)
1434 * Returns : 0 => Ok, everything else is an error.
1436 *********************************************************************/
1437 int load_one_re_filterfile(struct client_state *csp, int fileid)
1441 struct re_filterfile_spec *new_bl, *bl = NULL;
1442 struct file_list *fs;
1444 char buf[BUFFER_SIZE];
1446 unsigned long linenum = 0;
1447 pcrs_job *dummy, *lastjob = NULL;
1450 * No need to reload if unchanged
1452 if (!check_file_changed(current_re_filterfile[fileid], csp->config->re_filterfile[fileid], &fs))
1456 csp->rlist[fileid] = current_re_filterfile[fileid];
1462 goto load_re_filterfile_error;
1466 * Open the file or fail
1468 if ((fp = fopen(csp->config->re_filterfile[fileid], "r")) == NULL)
1470 goto load_re_filterfile_error;
1476 while (read_config_line(buf, sizeof(buf), fp, &linenum) != NULL)
1478 int new_filter = NO_NEW_FILTER;
1480 if (strncmp(buf, "FILTER:", 7) == 0)
1482 new_filter = FT_CONTENT_FILTER;
1484 else if (strncmp(buf, "SERVER-HEADER-FILTER:", 21) == 0)
1486 new_filter = FT_SERVER_HEADER_FILTER;
1488 else if (strncmp(buf, "CLIENT-HEADER-FILTER:", 21) == 0)
1490 new_filter = FT_CLIENT_HEADER_FILTER;
1492 else if (strncmp(buf, "CLIENT-HEADER-TAGGER:", 21) == 0)
1494 new_filter = FT_CLIENT_HEADER_TAGGER;
1496 else if (strncmp(buf, "SERVER-HEADER-TAGGER:", 21) == 0)
1498 new_filter = FT_SERVER_HEADER_TAGGER;
1502 * If this is the head of a new filter block, make it a
1503 * re_filterfile spec of its own and chain it to the list:
1505 if (new_filter != NO_NEW_FILTER)
1507 new_bl = (struct re_filterfile_spec *)zalloc(sizeof(*bl));
1510 goto load_re_filterfile_error;
1512 if (new_filter == FT_CONTENT_FILTER)
1514 new_bl->name = chomp(buf + 7);
1518 new_bl->name = chomp(buf + 21);
1520 new_bl->type = new_filter;
1523 * If a filter description is available,
1524 * encode it to HTML and save it.
1526 if (NULL != (new_bl->description = strpbrk(new_bl->name, " \t")))
1528 *new_bl->description++ = '\0';
1529 new_bl->description = html_encode(chomp(new_bl->description));
1530 if (NULL == new_bl->description)
1532 new_bl->description = strdup("Out of memory while encoding this filter's description to HTML");
1537 new_bl->description = strdup("No description available for this filter");
1540 new_bl->name = strdup(chomp(new_bl->name));
1543 * If this is the first filter block, chain it
1544 * to the file_list rather than its (nonexistant)
1557 log_error(LOG_LEVEL_RE_FILTER, "Reading in filter \"%s\" (\"%s\")", bl->name, bl->description);
1563 * Else, save the expression, make it a pcrs_job
1564 * and chain it into the current filter's joblist
1568 error = enlist(bl->patterns, buf);
1569 if (JB_ERR_MEMORY == error)
1571 log_error(LOG_LEVEL_FATAL,
1572 "Out of memory while enlisting re_filter job \'%s\' for filter %s.", buf, bl->name);
1574 assert(JB_ERR_OK == error);
1576 if (pcrs_job_is_dynamic(buf))
1579 * Dynamic pattern that might contain variables
1580 * and has to be recompiled for every request
1582 if (bl->joblist != NULL)
1584 pcrs_free_joblist(bl->joblist);
1588 log_error(LOG_LEVEL_RE_FILTER,
1589 "Adding dynamic re_filter job \'%s\' to filter %s succeeded.", buf, bl->name);
1592 else if (bl->dynamic)
1595 * A previous job was dynamic and as we
1596 * recompile the whole filter anyway, it
1597 * makes no sense to compile this job now.
1599 log_error(LOG_LEVEL_RE_FILTER,
1600 "Adding static re_filter job \'%s\' to dynamic filter %s succeeded.", buf, bl->name);
1604 if ((dummy = pcrs_compile_command(buf, &error)) == NULL)
1606 log_error(LOG_LEVEL_ERROR,
1607 "Adding re_filter job \'%s\' to filter %s failed with error %d.", buf, bl->name, error);
1612 if (bl->joblist == NULL)
1614 bl->joblist = dummy;
1618 lastjob->next = dummy;
1621 log_error(LOG_LEVEL_RE_FILTER, "Adding re_filter job \'%s\' to filter %s succeeded.", buf, bl->name);
1626 log_error(LOG_LEVEL_ERROR, "Ignoring job %s outside filter block in %s, line %d",
1627 buf, csp->config->re_filterfile[fileid], linenum);
1634 * Schedule the now-obsolete old data for unloading
1636 if ( NULL != current_re_filterfile[fileid] )
1638 current_re_filterfile[fileid]->unloader = unload_re_filterfile;
1642 * Chain this file into the global list of loaded files
1644 fs->next = files->next;
1646 current_re_filterfile[fileid] = fs;
1650 csp->rlist[fileid] = fs;
1655 load_re_filterfile_error:
1656 log_error(LOG_LEVEL_FATAL, "can't load re_filterfile '%s': %E",
1657 csp->config->re_filterfile[fileid]);
1663 /*********************************************************************
1665 * Function : add_loader
1667 * Description : Called from `load_config'. Called once for each input
1668 * file found in config.
1671 * 1 : loader = pointer to a function that can parse and load
1672 * the appropriate config file.
1673 * 2 : config = The configuration_spec to add the loader to.
1677 *********************************************************************/
1678 void add_loader(int (*loader)(struct client_state *),
1679 struct configuration_spec * config)
1683 for (i=0; i < NLOADERS; i++)
1685 if (config->loaders[i] == NULL)
1687 config->loaders[i] = loader;
1695 /*********************************************************************
1697 * Function : run_loader
1699 * Description : Called from `load_config' and `listen_loop'. This
1700 * function keeps the "csp" current with any file mods
1701 * since the last loop. If a file is unchanged, the
1702 * loader functions do NOT reload the file.
1705 * 1 : csp = Current client state (buffers, headers, etc...)
1706 * Must be non-null. Reads: "csp->config"
1707 * Writes: various data members.
1709 * Returns : 0 => Ok, everything else is an error.
1711 *********************************************************************/
1712 int run_loader(struct client_state *csp)
1717 for (i=0; i < NLOADERS; i++)
1719 if (csp->config->loaders[i] == NULL)
1723 ret |= (csp->config->loaders[i])(csp);