1 const char loaders_rcs[] = "$Id: loaders.c,v 1.62 2007/04/30 15:02:18 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.62 2007/04/30 15:02:18 fabiankeil
39 * Introduce dynamic pcrs jobs that can resolve variables.
41 * Revision 1.61 2007/04/15 16:39:21 fabiankeil
42 * Introduce tags as alternative way to specify which
43 * actions apply to a request. At the moment tags can be
44 * created based on client and server headers.
46 * Revision 1.60 2007/03/20 15:16:34 fabiankeil
47 * Use dedicated header filter actions instead of abusing "filter".
48 * Replace "filter-client-headers" and "filter-client-headers"
49 * with "server-header-filter" and "client-header-filter".
51 * Revision 1.59 2007/01/25 13:38:20 fabiankeil
52 * Freez csp->error_message in sweep().
54 * Revision 1.58 2006/12/31 14:25:20 fabiankeil
55 * Fix gcc43 compiler warnings.
57 * Revision 1.57 2006/12/21 12:22:22 fabiankeil
58 * html_encode filter descriptions.
60 * Have "Ignoring job ..." error messages
61 * print the filter file name correctly.
63 * Revision 1.56 2006/09/07 10:40:30 fabiankeil
64 * Turns out trusted referrers above our arbitrary
65 * limit are downgraded too ordinary trusted URLs.
66 * Adjusted error message.
68 * Revision 1.55 2006/09/07 10:25:39 fabiankeil
71 * Revision 1.54 2006/09/07 10:22:20 fabiankeil
72 * If too many trusted referrers are used,
73 * print only one error message instead of logging
74 * every single trusted referrer above the arbitrary
77 * Revision 1.53 2006/08/31 16:25:06 fabiankeil
78 * Work around a buffer overflow that caused Privoxy to
79 * segfault if too many trusted referrers were used. Good
80 * enough for now, but should be replaced with a real
81 * solution after the next release.
83 * Revision 1.52 2006/07/18 14:48:46 david__schmidt
84 * Reorganizing the repository: swapping out what was HEAD (the old 3.1 branch)
85 * with what was really the latest development (the v_3_0_branch branch)
87 * Revision 1.50.2.8 2006/01/30 15:16:25 david__schmidt
88 * Remove a little residual debugging info
90 * Revision 1.50.2.7 2006/01/29 23:10:56 david__schmidt
91 * Multiple filter file support
93 * Revision 1.50.2.6 2003/10/24 10:17:54 oes
94 * Nit: Allowed tabs as separators in filter headings
96 * Revision 1.50.2.5 2003/05/08 15:19:15 oes
97 * sweep: Made loop structure of sweep step mirror that of mark step
99 * Revision 1.50.2.4 2003/05/06 15:57:12 oes
100 * Bugfix: Update last_active pointer in sweep() before
101 * leaving an active client. Closes bugs #724395, #727882
103 * Revision 1.50.2.3 2002/11/20 17:12:30 oes
104 * Ooops, forgot one change.
106 * Revision 1.50.2.2 2002/11/20 14:38:15 oes
107 * Fixed delayed/incomplete freeing of client resources and
108 * simplified loop structure in sweep.
109 * Thanks to Oliver Stoeneberg for the hint.
111 * Revision 1.50.2.1 2002/07/26 15:19:24 oes
112 * - PCRS jobs now chained in order of appearance. Previous
113 * reverse chaining was counter-intuitive.
114 * - Changed loglevel of PCRS job compile errors to
117 * Revision 1.50 2002/04/24 02:12:16 oes
118 * Jon's multiple AF patch: Sweep now takes care of all AFs
120 * Revision 1.49 2002/04/19 16:53:25 jongfoster
121 * Optimize away a function call by using an equivalent macro
123 * Revision 1.48 2002/04/05 00:56:09 gliptak
124 * Correcting typo to clean up on realloc failure
126 * Revision 1.47 2002/03/26 22:29:55 swa
127 * we have a new homepage!
129 * Revision 1.46 2002/03/24 13:25:43 swa
130 * name change related issues
132 * Revision 1.45 2002/03/16 23:54:06 jongfoster
133 * Adding graceful termination feature, to help look for memory leaks.
134 * If you enable this (which, by design, has to be done by hand
135 * editing config.h) and then go to http://i.j.b/die, then the program
136 * will exit cleanly after the *next* request. It should free all the
137 * memory that was used.
139 * Revision 1.44 2002/03/16 21:51:00 jongfoster
142 * Revision 1.43 2002/03/16 20:28:34 oes
143 * Added descriptions to the filters so users will know what they select in the cgi editor
145 * Revision 1.42 2002/03/13 00:27:05 jongfoster
148 * Revision 1.41 2002/03/12 01:42:50 oes
149 * Introduced modular filters
151 * Revision 1.40 2002/03/08 17:46:04 jongfoster
152 * Fixing int/size_t warnings
154 * Revision 1.39 2002/03/07 03:46:17 oes
155 * Fixed compiler warnings
157 * Revision 1.38 2002/03/06 22:54:35 jongfoster
158 * Automated function-comment nitpicking.
160 * Revision 1.37 2002/03/03 15:07:49 oes
161 * Re-enabled automatic config reloading
163 * Revision 1.36 2002/01/22 23:46:18 jongfoster
164 * Moving edit_read_line() and simple_read_line() to loaders.c, and
165 * extending them to support reading MS-DOS, Mac and UNIX style files
168 * Modifying read_config_line() (without changing it's prototype) to
169 * be a trivial wrapper for edit_read_line(). This means that we have
170 * one function to read a line and handle comments, which is common
171 * between the initialization code and the edit interface.
173 * Revision 1.35 2002/01/17 21:03:08 jongfoster
174 * Moving all our URL and URL pattern parsing code to urlmatch.c.
176 * Renaming free_url to free_url_spec, since it frees a struct url_spec.
178 * Revision 1.34 2001/12/30 14:07:32 steudten
179 * - Add signal handling (unix)
180 * - Add SIGHUP handler (unix)
181 * - Add creation of pidfile (unix)
182 * - Add action 'top' in rc file (RH)
183 * - Add entry 'SIGNALS' to manpage
184 * - Add exit message to logfile (unix)
186 * Revision 1.33 2001/11/13 00:16:38 jongfoster
187 * Replacing references to malloc.h with the standard stdlib.h
188 * (See ANSI or K&R 2nd Ed)
190 * Revision 1.32 2001/11/07 00:02:13 steudten
191 * Add line number in error output for lineparsing for
192 * actionsfile and configfile.
193 * Special handling for CLF added.
195 * Revision 1.31 2001/10/26 17:39:01 oes
196 * Removed csp->referrer
197 * Moved ijb_isspace and ijb_tolower to project.h
199 * Revision 1.30 2001/10/25 03:40:48 david__schmidt
200 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
201 * threads to call select() simultaneously. So, it's time to do a real, live,
202 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
203 * (native). Both versions will work, but using __OS2__ offers multi-threading.
205 * Revision 1.29 2001/10/23 21:38:53 jongfoster
206 * Adding error-checking to create_url_spec()
208 * Revision 1.28 2001/10/07 15:40:39 oes
209 * Replaced 6 boolean members of csp with one bitmap (csp->flags)
211 * Revision 1.27 2001/09/22 16:36:59 jongfoster
212 * Removing unused parameter fs from read_config_line()
214 * Revision 1.26 2001/09/22 14:05:22 jongfoster
215 * Bugfix: Multiple escaped "#" characters in a configuration
216 * file are now permitted.
217 * Also removing 3 unused headers.
219 * Revision 1.25 2001/09/13 22:44:03 jongfoster
220 * Adding {} to an if statement
222 * Revision 1.24 2001/07/30 22:08:36 jongfoster
223 * Tidying up #defines:
224 * - All feature #defines are now of the form FEATURE_xxx
225 * - Permanently turned off WIN_GUI_EDIT
226 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
228 * Revision 1.23 2001/07/20 15:51:54 oes
229 * Fixed indentation of prepocessor commands
231 * Revision 1.22 2001/07/20 15:16:17 haroon
232 * - per Guy's suggestion, added a while loop in sweep() to catch not just
233 * the last inactive CSP but all other consecutive inactive CSPs after that
236 * Revision 1.21 2001/07/18 17:26:24 oes
237 * Changed to conform to new pcrs interface
239 * Revision 1.20 2001/07/17 13:07:01 oes
240 * Fixed segv when last line in config files
241 * lacked a terminating (\r)\n
243 * Revision 1.19 2001/07/13 14:01:54 oes
244 * Removed all #ifdef PCRS
246 * Revision 1.18 2001/06/29 21:45:41 oes
247 * Indentation, CRLF->LF, Tab-> Space
249 * Revision 1.17 2001/06/29 13:31:51 oes
252 * Revision 1.16 2001/06/09 10:55:28 jongfoster
253 * Changing BUFSIZ ==> BUFFER_SIZE
255 * Revision 1.15 2001/06/07 23:14:14 jongfoster
256 * Removing ACL and forward file loaders - these
257 * files have been merged into the config file.
258 * Cosmetic: Moving unloader funcs next to their
259 * respective loader funcs
261 * Revision 1.14 2001/06/01 03:27:04 oes
262 * Fixed line continuation problem
264 * Revision 1.13 2001/05/31 21:28:49 jongfoster
265 * Removed all permissionsfile code - it's now called the actions
266 * file, and (almost) all the code is in actions.c
268 * Revision 1.12 2001/05/31 17:32:31 oes
270 * - Enhanced domain part globbing with infix and prefix asterisk
271 * matching and optional unanchored operation
273 * Revision 1.11 2001/05/29 23:25:24 oes
275 * - load_config_line() and load_permissions_file() now use chomp()
277 * Revision 1.10 2001/05/29 09:50:24 jongfoster
278 * Unified blocklist/imagelist/permissionslist.
279 * File format is still under discussion, but the internal changes
282 * Also modified interceptor behaviour:
283 * - We now intercept all URLs beginning with one of the following
284 * prefixes (and *only* these prefixes):
286 * * http://ijbswa.sf.net/config/
287 * * http://ijbswa.sourceforge.net/config/
288 * - New interceptors "home page" - go to http://i.j.b/ to see it.
289 * - Internal changes so that intercepted and fast redirect pages
290 * are not replaced with an image.
291 * - Interceptors now have the option to send a binary page direct
292 * to the client. (i.e. ijb-send-banner uses this)
293 * - Implemented show-url-info interceptor. (Which is why I needed
294 * the above interceptors changes - a typical URL is
295 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
296 * The previous mechanism would not have intercepted that, and
297 * if it had been intercepted then it then it would have replaced
300 * Revision 1.9 2001/05/26 17:12:07 jongfoster
301 * Fatal errors loading configuration files now give better error messages.
303 * Revision 1.8 2001/05/26 00:55:20 jongfoster
304 * Removing duplicated code. load_forwardfile() now uses create_url_spec()
306 * Revision 1.7 2001/05/26 00:28:36 jongfoster
307 * Automatic reloading of config file.
308 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
309 * Most of the global variables have been moved to a new
310 * struct configuration_spec, accessed through csp->config->globalname
311 * Most of the globals remaining are used by the Win32 GUI.
313 * Revision 1.6 2001/05/23 12:27:33 oes
315 * Fixed ugly indentation of my last changes
317 * Revision 1.5 2001/05/23 10:39:05 oes
318 * - Added support for escaping the comment character
319 * in config files by a backslash
320 * - Added support for line continuation in config
322 * - Fixed a buffer overflow bug with long config lines
324 * Revision 1.4 2001/05/22 18:56:28 oes
327 * Revision 1.3 2001/05/20 01:21:20 jongfoster
328 * Version 2.9.4 checkin.
329 * - Merged popupfile and cookiefile, and added control over PCRS
330 * filtering, in new "permissionsfile".
331 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
332 * file error you now get a message box (in the Win32 GUI) rather
333 * than the program exiting with no explanation.
334 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
336 * - Removed tabs from "config"
337 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
338 * - Bumped up version number.
340 * Revision 1.2 2001/05/17 23:01:01 oes
341 * - Cleaned CRLF's from the sources and related files
343 * Revision 1.1.1.1 2001/05/15 13:58:59 oes
344 * Initial import of version 2.9.3 source tree
347 *********************************************************************/
354 #include <sys/types.h>
357 #include <sys/stat.h>
361 #if !defined(_WIN32) && !defined(__OS2__)
371 #include "miscutil.h"
374 #include "urlmatch.h"
377 const char loaders_h_rcs[] = LOADERS_H_VERSION;
380 * Currently active files.
381 * These are also entered in the main linked list of files.
385 static struct file_list *current_trustfile = NULL;
386 #endif /* def FEATURE_TRUST */
388 static int load_one_re_filterfile(struct client_state *csp, int fileid);
390 static struct file_list *current_re_filterfile[MAX_AF_FILES] = {
391 NULL, NULL, NULL, NULL, NULL,
392 NULL, NULL, NULL, NULL, NULL
396 * Pseudo filter type for load_one_re_filterfile
398 #define NO_NEW_FILTER -1
401 /*********************************************************************
405 * Description : Basically a mark and sweep garbage collector, it is run
406 * (by the parent thread) every once in a while to reclaim memory.
408 * It uses a mark and sweep strategy:
409 * 1) mark all files as inactive
411 * 2) check with each client:
412 * if it is active, mark its files as active
413 * if it is inactive, free its resources
415 * 3) free the resources of all of the files that
416 * are still marked as inactive (and are obsolete).
418 * N.B. files that are not obsolete don't have an unloader defined.
424 *********************************************************************/
427 struct file_list *fl, *nfl;
428 struct client_state *csp, *last_active;
431 /* clear all of the file's active flags */
432 for ( fl = files->next; NULL != fl; fl = fl->next )
437 last_active = clients;
442 if (csp->flags & CSP_FLAG_ACTIVE)
444 /* Mark this client's files as active */
447 * Always have a configuration file.
448 * (Also note the slightly non-standard extra
451 csp->config->config_file_list->active = 1;
456 for (i = 0; i < MAX_AF_FILES; i++)
458 if (csp->actions_list[i])
460 csp->actions_list[i]->active = 1;
467 for (i = 0; i < MAX_AF_FILES; i++)
471 csp->rlist[i]->active = 1;
481 csp->tlist->active = 1;
483 #endif /* def FEATURE_TRUST */
491 * This client is not active. Free its resources.
494 last_active->next = csp->next;
496 freez(csp->ip_addr_str);
497 freez(csp->my_ip_addr_str);
498 freez(csp->my_hostname);
499 freez(csp->x_forwarded);
500 freez(csp->iob->buf);
501 freez(csp->error_message);
503 free_http_request(csp->http);
505 destroy_list(csp->headers);
506 destroy_list(csp->tags);
508 free_current_action(csp->action);
510 #ifdef FEATURE_STATISTICS
512 if (csp->flags & CSP_FLAG_REJECTED)
516 #endif /* def FEATURE_STATISTICS */
520 csp = last_active->next;
529 if ( ( 0 == fl->active ) && ( NULL != fl->unloader ) )
531 nfl->next = fl->next;
533 (fl->unloader)(fl->f);
550 /*********************************************************************
552 * Function : check_file_changed
554 * Description : Helper function to check if a file needs reloading.
555 * If "current" is still current, return it. Otherwise
556 * allocates a new (zeroed) "struct file_list", fills
557 * in the disk file name and timestamp, and returns it.
560 * 1 : current = The file_list currently being used - will
561 * be checked to see if it is out of date.
562 * May be NULL (which is treated as out of
564 * 2 : filename = Name of file to check.
565 * 3 : newfl = New file list. [Output only]
566 * This will be set to NULL, OR a struct
567 * file_list newly allocated on the
568 * heap, with the filename and lastmodified
569 * fields filled, and all others zeroed.
571 * Returns : If file unchanged: 0 (and sets newfl == NULL)
572 * If file changed: 1 and sets newfl != NULL
573 * On error: 1 and sets newfl == NULL
575 *********************************************************************/
576 int check_file_changed(const struct file_list * current,
577 const char * filename,
578 struct file_list ** newfl)
580 struct file_list *fs;
581 struct stat statbuf[1];
585 if (stat(filename, statbuf) < 0)
587 /* Error, probably file not found. */
592 && (current->lastmodified == statbuf->st_mtime)
593 && (0 == strcmp(current->filename, filename)))
598 fs = (struct file_list *)zalloc(sizeof(struct file_list));
601 /* Out of memory error */
606 fs->filename = strdup(filename);
607 fs->lastmodified = statbuf->st_mtime;
609 if (fs->filename == NULL)
611 /* Out of memory error */
620 /*********************************************************************
622 * Function : simple_read_line
624 * Description : Read a single line from a file and return it.
625 * This is basically a version of fgets() that malloc()s
626 * it's own line buffer. Note that the buffer will
627 * always be a multiple of BUFFER_SIZE bytes long.
628 * Therefore if you are going to keep the string for
629 * an extended period of time, you should probably
630 * strdup() it and free() the original, to save memory.
634 * 1 : dest = destination for newly malloc'd pointer to
635 * line data. Will be set to NULL on error.
636 * 2 : fp = File to read from
637 * 3 : newline = Standard for newlines in the file.
638 * Will be unchanged if it's value on input is not
640 * On output, may be changed from NEWLINE_UNKNOWN to
641 * actual convention in file.
643 * Returns : JB_ERR_OK on success
644 * JB_ERR_MEMORY on out-of-memory
645 * JB_ERR_FILE on EOF.
647 *********************************************************************/
648 jb_err simple_read_line(FILE *fp, char **dest, int *newline)
651 size_t buflen = BUFFER_SIZE;
655 int realnewline = NEWLINE_UNKNOWN;
657 if (NULL == (buf = malloc(buflen)))
659 return JB_ERR_MEMORY;
665 * Character codes. If you have a wierd compiler and the following are
666 * incorrect, you also need to fix NEWLINE() in loaders.h
668 #define CHAR_CR '\r' /* ASCII 13 */
669 #define CHAR_LF '\n' /* ASCII 10 */
689 else if (ch == CHAR_CR)
694 if (*newline == NEWLINE_UNKNOWN)
696 *newline = NEWLINE_DOS;
705 if (*newline == NEWLINE_UNKNOWN)
707 *newline = NEWLINE_MAC;
712 if (*newline == NEWLINE_UNKNOWN)
714 *newline = realnewline;
718 else if (ch == CHAR_LF)
722 if (*newline == NEWLINE_UNKNOWN)
724 *newline = NEWLINE_UNIX;
739 buflen += BUFFER_SIZE;
740 if (NULL == (p = realloc(buf, buflen)))
743 return JB_ERR_MEMORY;
752 /*********************************************************************
754 * Function : edit_read_line
756 * Description : Read a single non-empty line from a file and return
757 * it. Trims comments, leading and trailing whitespace
758 * and respects escaping of newline and comment char.
759 * Provides the line in 2 alternative forms: raw and
761 * - raw is the raw data read from the file. If the
762 * line is not modified, then this should be written
764 * - prefix is any comments and blank lines that were
765 * read from the file. If the line is modified, then
766 * this should be written out to the file followed
767 * by the modified data. (If this string is non-empty
768 * then it will have a newline at the end).
769 * - data is the actual data that will be parsed
770 * further by appropriate routines.
771 * On EOF, the 3 strings will all be set to NULL and
772 * 0 will be returned.
775 * 1 : fp = File to read from
776 * 2 : raw_out = destination for newly malloc'd pointer to
777 * raw line data. May be NULL if you don't want it.
778 * 3 : prefix_out = destination for newly malloc'd pointer to
779 * comments. May be NULL if you don't want it.
780 * 4 : data_out = destination for newly malloc'd pointer to
781 * line data with comments and leading/trailing spaces
782 * removed, and line continuation performed. May be
783 * NULL if you don't want it.
784 * 5 : newline = Standard for newlines in the file.
785 * On input, set to value to use or NEWLINE_UNKNOWN.
786 * On output, may be changed from NEWLINE_UNKNOWN to
787 * actual convention in file. May be NULL if you
789 * 6 : line_number = Line number in file. In "lines" as
790 * reported by a text editor, not lines containing data.
792 * Returns : JB_ERR_OK on success
793 * JB_ERR_MEMORY on out-of-memory
794 * JB_ERR_FILE on EOF.
796 *********************************************************************/
797 jb_err edit_read_line(FILE *fp,
802 unsigned long *line_number)
804 char *p; /* Temporary pointer */
805 char *linebuf; /* Line read from file */
806 char *linestart; /* Start of linebuf, usually first non-whitespace char */
807 int contflag = 0; /* Nonzero for line continuation - i.e. line ends '\' */
808 int is_empty = 1; /* Flag if not got any data yet */
809 char *raw = NULL; /* String to be stored in raw_out */
810 char *prefix = NULL; /* String to be stored in prefix_out */
811 char *data = NULL; /* String to be stored in data_out */
812 int scrapnewline; /* Used for (*newline) if newline==NULL */
813 jb_err rval = JB_ERR_OK;
816 assert(raw_out || data_out);
817 assert(newline == NULL
818 || *newline == NEWLINE_UNKNOWN
819 || *newline == NEWLINE_UNIX
820 || *newline == NEWLINE_DOS
821 || *newline == NEWLINE_MAC);
825 scrapnewline = NEWLINE_UNKNOWN;
826 newline = &scrapnewline;
829 /* Set output parameters to NULL */
843 /* Set string variables to new, empty strings. */
847 if ((raw = malloc(1)) == NULL)
849 return JB_ERR_MEMORY;
855 if ((prefix = malloc(1)) == NULL)
858 return JB_ERR_MEMORY;
864 if ((data = malloc(1)) == NULL)
868 return JB_ERR_MEMORY;
873 /* Main loop. Loop while we need more data & it's not EOF. */
875 while ( (contflag || is_empty)
876 && (JB_ERR_OK == (rval = simple_read_line(fp, &linebuf, newline))))
884 string_append(&raw,linebuf);
885 if (string_append(&raw,NEWLINE(*newline)))
890 return JB_ERR_MEMORY;
894 /* Line continuation? Trim escape and set flag. */
895 p = linebuf + strlen(linebuf) - 1;
896 contflag = ((*linebuf != '\0') && (*p == '\\'));
902 /* Trim leading spaces if we're at the start of the line */
906 /* Trim leading spaces */
907 while (*linestart && isspace((int)(unsigned char)*linestart))
913 /* Handle comment characters. */
915 while ((p = strchr(p, '#')) != NULL)
917 /* Found a comment char.. */
918 if ((p != linebuf) && (*(p-1) == '\\'))
920 /* ..and it's escaped, left-shift the line over the escape. */
922 while ((*q = *(q + 1)) != '\0')
926 /* Now scan from just after the "#". */
930 /* Real comment. Save it... */
933 /* Special case: Line only contains a comment, so all the
934 * previous whitespace is considered part of the comment.
935 * Undo the whitespace skipping, if any.
942 string_append(&prefix,p);
943 if (string_append(&prefix, NEWLINE(*newline)))
948 return JB_ERR_MEMORY;
952 /* ... and chop off the rest of the line */
955 } /* END while (there's a # character) */
957 /* Write to the buffer */
963 if (string_append(&data, linestart))
968 return JB_ERR_MEMORY;
974 } /* END while(we need more data) */
976 /* Handle simple_read_line() errors - ignore EOF */
977 if ((rval != JB_ERR_OK) && (rval != JB_ERR_FILE))
985 if (raw ? (*raw == '\0') : is_empty)
987 /* EOF and no data there. (Definition of "data" depends on whether
988 * the caller cares about "raw" or just "data").
999 /* Got at least some data */
1001 /* Remove trailing whitespace */
1014 *prefix_out = prefix;
1033 /*********************************************************************
1035 * Function : read_config_line
1037 * Description : Read a single non-empty line from a file and return
1038 * it. Trims comments, leading and trailing whitespace
1039 * and respects escaping of newline and comment char.
1042 * 1 : buf = Buffer to use.
1043 * 2 : buflen = Size of buffer in bytes.
1044 * 3 : fp = File to read from
1045 * 4 : linenum = linenumber in file
1047 * Returns : NULL on EOF or error
1048 * Otherwise, returns buf.
1050 *********************************************************************/
1051 char *read_config_line(char *buf, size_t buflen, FILE *fp, unsigned long *linenum)
1055 err = edit_read_line(fp, NULL, NULL, &buf2, NULL, linenum);
1058 if (err == JB_ERR_MEMORY)
1060 log_error(LOG_LEVEL_FATAL, "Out of memory loading a config file");
1067 assert(strlen(buf2) + 1U < buflen);
1068 strncpy(buf, buf2, buflen - 1);
1070 buf[buflen - 1] = '\0';
1076 #ifdef FEATURE_TRUST
1077 /*********************************************************************
1079 * Function : unload_trustfile
1081 * Description : Unloads a trustfile.
1084 * 1 : f = the data structure associated with the trustfile.
1088 *********************************************************************/
1089 static void unload_trustfile(void *f)
1091 struct block_spec *cur = (struct block_spec *)f;
1092 struct block_spec *next;
1098 free_url_spec(cur->url);
1107 #ifdef FEATURE_GRACEFUL_TERMINATION
1108 /*********************************************************************
1110 * Function : unload_current_trust_file
1112 * Description : Unloads current trust file - reset to state at
1113 * beginning of program.
1119 *********************************************************************/
1120 void unload_current_trust_file(void)
1122 if (current_trustfile)
1124 current_trustfile->unloader = unload_trustfile;
1125 current_trustfile = NULL;
1128 #endif /* FEATURE_GRACEFUL_TERMINATION */
1131 /*********************************************************************
1133 * Function : load_trustfile
1135 * Description : Read and parse a trustfile and add to files list.
1138 * 1 : csp = Current client state (buffers, headers, etc...)
1140 * Returns : 0 => Ok, everything else is an error.
1142 *********************************************************************/
1143 int load_trustfile(struct client_state *csp)
1147 struct block_spec *b, *bl;
1148 struct url_spec **tl;
1150 char buf[BUFFER_SIZE], *p, *q;
1151 int reject, trusted;
1152 struct file_list *fs;
1153 unsigned long linenum = 0;
1154 int trusted_referrers = 0;
1156 if (!check_file_changed(current_trustfile, csp->config->trustfile, &fs))
1158 /* No need to load */
1161 csp->tlist = current_trustfile;
1167 goto load_trustfile_error;
1170 fs->f = bl = (struct block_spec *)zalloc(sizeof(*bl));
1173 goto load_trustfile_error;
1176 if ((fp = fopen(csp->config->trustfile, "r")) == NULL)
1178 goto load_trustfile_error;
1181 tl = csp->config->trust_list;
1183 while (read_config_line(buf, sizeof(buf), fp, &linenum) != NULL)
1199 while ((*p++ = *q++) != '\0')
1205 /* skip blank lines */
1211 /* allocate a new node */
1212 if ((b = zalloc(sizeof(*b))) == NULL)
1215 goto load_trustfile_error;
1218 /* add it to the list */
1224 /* Save the URL pattern */
1225 if (create_url_spec(b->url, buf))
1228 goto load_trustfile_error;
1232 * save a pointer to URL's spec in the list of trusted URL's, too
1236 if(++trusted_referrers < MAX_TRUSTED_REFERRERS)
1243 if(trusted_referrers >= MAX_TRUSTED_REFERRERS)
1246 * FIXME: ... after Privoxy 3.0.4 is out.
1248 log_error(LOG_LEVEL_ERROR, "Too many trusted referrers. Current limit is %d, you are using %d.\n"
1249 " Additional trusted referrers are treated like ordinary trusted URLs.\n"
1250 " (You can increase this limit by changing MAX_TRUSTED_REFERRERS in project.h and recompiling).",
1251 MAX_TRUSTED_REFERRERS, trusted_referrers);
1258 /* the old one is now obsolete */
1259 if (current_trustfile)
1261 current_trustfile->unloader = unload_trustfile;
1264 fs->next = files->next;
1266 current_trustfile = fs;
1275 load_trustfile_error:
1276 log_error(LOG_LEVEL_FATAL, "can't load trustfile '%s': %E",
1277 csp->config->trustfile);
1281 #endif /* def FEATURE_TRUST */
1284 /*********************************************************************
1286 * Function : unload_re_filterfile
1288 * Description : Unload the re_filter list by freeing all chained
1289 * re_filterfile specs and their data.
1292 * 1 : f = the data structure associated with the filterfile.
1296 *********************************************************************/
1297 static void unload_re_filterfile(void *f)
1299 struct re_filterfile_spec *a, *b = (struct re_filterfile_spec *)f;
1305 destroy_list(b->patterns);
1306 pcrs_free_joblist(b->joblist);
1308 freez(b->description);
1318 #ifdef FEATURE_GRACEFUL_TERMINATION
1319 /*********************************************************************
1321 * Function : unload_current_re_filterfile
1323 * Description : Unloads current re_filter file - reset to state at
1324 * beginning of program.
1330 *********************************************************************/
1331 void unload_current_re_filterfile(void)
1335 for (i = 0; i < MAX_AF_FILES; i++)
1337 if (current_re_filterfile[i])
1339 current_re_filterfile[i]->unloader = unload_re_filterfile;
1340 current_re_filterfile[i] = NULL;
1347 /*********************************************************************
1349 * Function : load_re_filterfile
1351 * Description : Load the re_filterfile.
1352 * Generate a chained list of re_filterfile_spec's from
1353 * the "FILTER: " blocks, compiling all their substitutions
1354 * into chained lists of pcrs_job structs.
1357 * 1 : csp = Current client state (buffers, headers, etc...)
1359 * Returns : 0 => Ok, everything else is an error.
1361 *********************************************************************/
1362 int load_re_filterfile(struct client_state *csp)
1367 for (i = 0; i < MAX_AF_FILES; i++)
1369 if (csp->config->re_filterfile[i])
1371 result = load_one_re_filterfile(csp, i);
1377 else if (current_re_filterfile[i])
1379 current_re_filterfile[i]->unloader = unload_re_filterfile;
1380 current_re_filterfile[i] = NULL;
1388 /*********************************************************************
1390 * Function : load_one_re_filterfile
1392 * Description : Load a re_filterfile.
1393 * Generate a chained list of re_filterfile_spec's from
1394 * the "FILTER: " blocks, compiling all their substitutions
1395 * into chained lists of pcrs_job structs.
1398 * 1 : csp = Current client state (buffers, headers, etc...)
1400 * Returns : 0 => Ok, everything else is an error.
1402 *********************************************************************/
1403 int load_one_re_filterfile(struct client_state *csp, int fileid)
1407 struct re_filterfile_spec *new_bl, *bl = NULL;
1408 struct file_list *fs;
1410 char buf[BUFFER_SIZE];
1412 unsigned long linenum = 0;
1413 pcrs_job *dummy, *lastjob = NULL;
1416 * No need to reload if unchanged
1418 if (!check_file_changed(current_re_filterfile[fileid], csp->config->re_filterfile[fileid], &fs))
1422 csp->rlist[fileid] = current_re_filterfile[fileid];
1428 goto load_re_filterfile_error;
1432 * Open the file or fail
1434 if ((fp = fopen(csp->config->re_filterfile[fileid], "r")) == NULL)
1436 goto load_re_filterfile_error;
1442 while (read_config_line(buf, sizeof(buf), fp, &linenum) != NULL)
1444 int new_filter = NO_NEW_FILTER;
1446 if (strncmp(buf, "FILTER:", 7) == 0)
1448 new_filter = FT_CONTENT_FILTER;
1450 else if (strncmp(buf, "SERVER-HEADER-FILTER:", 21) == 0)
1452 new_filter = FT_SERVER_HEADER_FILTER;
1454 else if (strncmp(buf, "CLIENT-HEADER-FILTER:", 21) == 0)
1456 new_filter = FT_CLIENT_HEADER_FILTER;
1458 else if (strncmp(buf, "CLIENT-HEADER-TAGGER:", 21) == 0)
1460 new_filter = FT_CLIENT_HEADER_TAGGER;
1462 else if (strncmp(buf, "SERVER-HEADER-TAGGER:", 21) == 0)
1464 new_filter = FT_SERVER_HEADER_TAGGER;
1468 * If this is the head of a new filter block, make it a
1469 * re_filterfile spec of its own and chain it to the list:
1471 if (new_filter != NO_NEW_FILTER)
1473 new_bl = (struct re_filterfile_spec *)zalloc(sizeof(*bl));
1476 goto load_re_filterfile_error;
1478 if (new_filter == FT_CONTENT_FILTER)
1480 new_bl->name = chomp(buf + 7);
1484 new_bl->name = chomp(buf + 21);
1486 new_bl->type = new_filter;
1489 * If a filter description is available,
1490 * encode it to HTML and save it.
1492 if (NULL != (new_bl->description = strpbrk(new_bl->name, " \t")))
1494 *new_bl->description++ = '\0';
1495 new_bl->description = html_encode(chomp(new_bl->description));
1496 if (NULL == new_bl->description)
1498 new_bl->description = strdup("Out of memory while encoding this filter's description to HTML");
1503 new_bl->description = strdup("No description available for this filter");
1506 new_bl->name = strdup(chomp(new_bl->name));
1509 * If this is the first filter block, chain it
1510 * to the file_list rather than its (nonexistant)
1523 log_error(LOG_LEVEL_RE_FILTER, "Reading in filter \"%s\" (\"%s\")", bl->name, bl->description);
1529 * Else, save the expression, make it a pcrs_job
1530 * and chain it into the current filter's joblist
1534 error = enlist(bl->patterns, buf);
1535 if (JB_ERR_MEMORY == error)
1537 log_error(LOG_LEVEL_FATAL,
1538 "Out of memory while enlisting re_filter job \'%s\' for filter %s.", buf, bl->name);
1540 assert(JB_ERR_OK == error);
1542 if (pcrs_job_is_dynamic(buf))
1545 * Dynamic pattern that might contain variables
1546 * and has to be recompiled for every request
1548 if (bl->joblist != NULL)
1550 pcrs_free_joblist(bl->joblist);
1554 log_error(LOG_LEVEL_RE_FILTER,
1555 "Adding dynamic re_filter job \'%s\' to filter %s succeeded.", buf, bl->name);
1558 else if (bl->dynamic)
1561 * A previous job was dynamic and as we
1562 * recompile the whole filter anyway, it
1563 * makes no sense to compile this job now.
1565 log_error(LOG_LEVEL_RE_FILTER,
1566 "Adding static re_filter job \'%s\' to dynamic filter %s succeeded.", buf, bl->name);
1570 if ((dummy = pcrs_compile_command(buf, &error)) == NULL)
1572 log_error(LOG_LEVEL_ERROR,
1573 "Adding re_filter job \'%s\' to filter %s failed with error %d.", buf, bl->name, error);
1578 if (bl->joblist == NULL)
1580 bl->joblist = dummy;
1584 lastjob->next = dummy;
1587 log_error(LOG_LEVEL_RE_FILTER, "Adding re_filter job \'%s\' to filter %s succeeded.", buf, bl->name);
1592 log_error(LOG_LEVEL_ERROR, "Ignoring job %s outside filter block in %s, line %d",
1593 buf, csp->config->re_filterfile[fileid], linenum);
1600 * Schedule the now-obsolete old data for unloading
1602 if ( NULL != current_re_filterfile[fileid] )
1604 current_re_filterfile[fileid]->unloader = unload_re_filterfile;
1608 * Chain this file into the global list of loaded files
1610 fs->next = files->next;
1612 current_re_filterfile[fileid] = fs;
1616 csp->rlist[fileid] = fs;
1621 load_re_filterfile_error:
1622 log_error(LOG_LEVEL_FATAL, "can't load re_filterfile '%s': %E",
1623 csp->config->re_filterfile[fileid]);
1629 /*********************************************************************
1631 * Function : add_loader
1633 * Description : Called from `load_config'. Called once for each input
1634 * file found in config.
1637 * 1 : loader = pointer to a function that can parse and load
1638 * the appropriate config file.
1639 * 2 : config = The configuration_spec to add the loader to.
1643 *********************************************************************/
1644 void add_loader(int (*loader)(struct client_state *),
1645 struct configuration_spec * config)
1649 for (i=0; i < NLOADERS; i++)
1651 if (config->loaders[i] == NULL)
1653 config->loaders[i] = loader;
1661 /*********************************************************************
1663 * Function : run_loader
1665 * Description : Called from `load_config' and `listen_loop'. This
1666 * function keeps the "csp" current with any file mods
1667 * since the last loop. If a file is unchanged, the
1668 * loader functions do NOT reload the file.
1671 * 1 : csp = Current client state (buffers, headers, etc...)
1672 * Must be non-null. Reads: "csp->config"
1673 * Writes: various data members.
1675 * Returns : 0 => Ok, everything else is an error.
1677 *********************************************************************/
1678 int run_loader(struct client_state *csp)
1683 for (i=0; i < NLOADERS; i++)
1685 if (csp->config->loaders[i] == NULL)
1689 ret |= (csp->config->loaders[i])(csp);