1 const char loaders_rcs[] = "$Id: loaders.c,v 1.60 2007/03/20 15:16:34 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.60 2007/03/20 15:16:34 fabiankeil
39 * Use dedicated header filter actions instead of abusing "filter".
40 * Replace "filter-client-headers" and "filter-client-headers"
41 * with "server-header-filter" and "client-header-filter".
43 * Revision 1.59 2007/01/25 13:38:20 fabiankeil
44 * Freez csp->error_message in sweep().
46 * Revision 1.58 2006/12/31 14:25:20 fabiankeil
47 * Fix gcc43 compiler warnings.
49 * Revision 1.57 2006/12/21 12:22:22 fabiankeil
50 * html_encode filter descriptions.
52 * Have "Ignoring job ..." error messages
53 * print the filter file name correctly.
55 * Revision 1.56 2006/09/07 10:40:30 fabiankeil
56 * Turns out trusted referrers above our arbitrary
57 * limit are downgraded too ordinary trusted URLs.
58 * Adjusted error message.
60 * Revision 1.55 2006/09/07 10:25:39 fabiankeil
63 * Revision 1.54 2006/09/07 10:22:20 fabiankeil
64 * If too many trusted referrers are used,
65 * print only one error message instead of logging
66 * every single trusted referrer above the arbitrary
69 * Revision 1.53 2006/08/31 16:25:06 fabiankeil
70 * Work around a buffer overflow that caused Privoxy to
71 * segfault if too many trusted referrers were used. Good
72 * enough for now, but should be replaced with a real
73 * solution after the next release.
75 * Revision 1.52 2006/07/18 14:48:46 david__schmidt
76 * Reorganizing the repository: swapping out what was HEAD (the old 3.1 branch)
77 * with what was really the latest development (the v_3_0_branch branch)
79 * Revision 1.50.2.8 2006/01/30 15:16:25 david__schmidt
80 * Remove a little residual debugging info
82 * Revision 1.50.2.7 2006/01/29 23:10:56 david__schmidt
83 * Multiple filter file support
85 * Revision 1.50.2.6 2003/10/24 10:17:54 oes
86 * Nit: Allowed tabs as separators in filter headings
88 * Revision 1.50.2.5 2003/05/08 15:19:15 oes
89 * sweep: Made loop structure of sweep step mirror that of mark step
91 * Revision 1.50.2.4 2003/05/06 15:57:12 oes
92 * Bugfix: Update last_active pointer in sweep() before
93 * leaving an active client. Closes bugs #724395, #727882
95 * Revision 1.50.2.3 2002/11/20 17:12:30 oes
96 * Ooops, forgot one change.
98 * Revision 1.50.2.2 2002/11/20 14:38:15 oes
99 * Fixed delayed/incomplete freeing of client resources and
100 * simplified loop structure in sweep.
101 * Thanks to Oliver Stoeneberg for the hint.
103 * Revision 1.50.2.1 2002/07/26 15:19:24 oes
104 * - PCRS jobs now chained in order of appearance. Previous
105 * reverse chaining was counter-intuitive.
106 * - Changed loglevel of PCRS job compile errors to
109 * Revision 1.50 2002/04/24 02:12:16 oes
110 * Jon's multiple AF patch: Sweep now takes care of all AFs
112 * Revision 1.49 2002/04/19 16:53:25 jongfoster
113 * Optimize away a function call by using an equivalent macro
115 * Revision 1.48 2002/04/05 00:56:09 gliptak
116 * Correcting typo to clean up on realloc failure
118 * Revision 1.47 2002/03/26 22:29:55 swa
119 * we have a new homepage!
121 * Revision 1.46 2002/03/24 13:25:43 swa
122 * name change related issues
124 * Revision 1.45 2002/03/16 23:54:06 jongfoster
125 * Adding graceful termination feature, to help look for memory leaks.
126 * If you enable this (which, by design, has to be done by hand
127 * editing config.h) and then go to http://i.j.b/die, then the program
128 * will exit cleanly after the *next* request. It should free all the
129 * memory that was used.
131 * Revision 1.44 2002/03/16 21:51:00 jongfoster
134 * Revision 1.43 2002/03/16 20:28:34 oes
135 * Added descriptions to the filters so users will know what they select in the cgi editor
137 * Revision 1.42 2002/03/13 00:27:05 jongfoster
140 * Revision 1.41 2002/03/12 01:42:50 oes
141 * Introduced modular filters
143 * Revision 1.40 2002/03/08 17:46:04 jongfoster
144 * Fixing int/size_t warnings
146 * Revision 1.39 2002/03/07 03:46:17 oes
147 * Fixed compiler warnings
149 * Revision 1.38 2002/03/06 22:54:35 jongfoster
150 * Automated function-comment nitpicking.
152 * Revision 1.37 2002/03/03 15:07:49 oes
153 * Re-enabled automatic config reloading
155 * Revision 1.36 2002/01/22 23:46:18 jongfoster
156 * Moving edit_read_line() and simple_read_line() to loaders.c, and
157 * extending them to support reading MS-DOS, Mac and UNIX style files
160 * Modifying read_config_line() (without changing it's prototype) to
161 * be a trivial wrapper for edit_read_line(). This means that we have
162 * one function to read a line and handle comments, which is common
163 * between the initialization code and the edit interface.
165 * Revision 1.35 2002/01/17 21:03:08 jongfoster
166 * Moving all our URL and URL pattern parsing code to urlmatch.c.
168 * Renaming free_url to free_url_spec, since it frees a struct url_spec.
170 * Revision 1.34 2001/12/30 14:07:32 steudten
171 * - Add signal handling (unix)
172 * - Add SIGHUP handler (unix)
173 * - Add creation of pidfile (unix)
174 * - Add action 'top' in rc file (RH)
175 * - Add entry 'SIGNALS' to manpage
176 * - Add exit message to logfile (unix)
178 * Revision 1.33 2001/11/13 00:16:38 jongfoster
179 * Replacing references to malloc.h with the standard stdlib.h
180 * (See ANSI or K&R 2nd Ed)
182 * Revision 1.32 2001/11/07 00:02:13 steudten
183 * Add line number in error output for lineparsing for
184 * actionsfile and configfile.
185 * Special handling for CLF added.
187 * Revision 1.31 2001/10/26 17:39:01 oes
188 * Removed csp->referrer
189 * Moved ijb_isspace and ijb_tolower to project.h
191 * Revision 1.30 2001/10/25 03:40:48 david__schmidt
192 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
193 * threads to call select() simultaneously. So, it's time to do a real, live,
194 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
195 * (native). Both versions will work, but using __OS2__ offers multi-threading.
197 * Revision 1.29 2001/10/23 21:38:53 jongfoster
198 * Adding error-checking to create_url_spec()
200 * Revision 1.28 2001/10/07 15:40:39 oes
201 * Replaced 6 boolean members of csp with one bitmap (csp->flags)
203 * Revision 1.27 2001/09/22 16:36:59 jongfoster
204 * Removing unused parameter fs from read_config_line()
206 * Revision 1.26 2001/09/22 14:05:22 jongfoster
207 * Bugfix: Multiple escaped "#" characters in a configuration
208 * file are now permitted.
209 * Also removing 3 unused headers.
211 * Revision 1.25 2001/09/13 22:44:03 jongfoster
212 * Adding {} to an if statement
214 * Revision 1.24 2001/07/30 22:08:36 jongfoster
215 * Tidying up #defines:
216 * - All feature #defines are now of the form FEATURE_xxx
217 * - Permanently turned off WIN_GUI_EDIT
218 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
220 * Revision 1.23 2001/07/20 15:51:54 oes
221 * Fixed indentation of prepocessor commands
223 * Revision 1.22 2001/07/20 15:16:17 haroon
224 * - per Guy's suggestion, added a while loop in sweep() to catch not just
225 * the last inactive CSP but all other consecutive inactive CSPs after that
228 * Revision 1.21 2001/07/18 17:26:24 oes
229 * Changed to conform to new pcrs interface
231 * Revision 1.20 2001/07/17 13:07:01 oes
232 * Fixed segv when last line in config files
233 * lacked a terminating (\r)\n
235 * Revision 1.19 2001/07/13 14:01:54 oes
236 * Removed all #ifdef PCRS
238 * Revision 1.18 2001/06/29 21:45:41 oes
239 * Indentation, CRLF->LF, Tab-> Space
241 * Revision 1.17 2001/06/29 13:31:51 oes
244 * Revision 1.16 2001/06/09 10:55:28 jongfoster
245 * Changing BUFSIZ ==> BUFFER_SIZE
247 * Revision 1.15 2001/06/07 23:14:14 jongfoster
248 * Removing ACL and forward file loaders - these
249 * files have been merged into the config file.
250 * Cosmetic: Moving unloader funcs next to their
251 * respective loader funcs
253 * Revision 1.14 2001/06/01 03:27:04 oes
254 * Fixed line continuation problem
256 * Revision 1.13 2001/05/31 21:28:49 jongfoster
257 * Removed all permissionsfile code - it's now called the actions
258 * file, and (almost) all the code is in actions.c
260 * Revision 1.12 2001/05/31 17:32:31 oes
262 * - Enhanced domain part globbing with infix and prefix asterisk
263 * matching and optional unanchored operation
265 * Revision 1.11 2001/05/29 23:25:24 oes
267 * - load_config_line() and load_permissions_file() now use chomp()
269 * Revision 1.10 2001/05/29 09:50:24 jongfoster
270 * Unified blocklist/imagelist/permissionslist.
271 * File format is still under discussion, but the internal changes
274 * Also modified interceptor behaviour:
275 * - We now intercept all URLs beginning with one of the following
276 * prefixes (and *only* these prefixes):
278 * * http://ijbswa.sf.net/config/
279 * * http://ijbswa.sourceforge.net/config/
280 * - New interceptors "home page" - go to http://i.j.b/ to see it.
281 * - Internal changes so that intercepted and fast redirect pages
282 * are not replaced with an image.
283 * - Interceptors now have the option to send a binary page direct
284 * to the client. (i.e. ijb-send-banner uses this)
285 * - Implemented show-url-info interceptor. (Which is why I needed
286 * the above interceptors changes - a typical URL is
287 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
288 * The previous mechanism would not have intercepted that, and
289 * if it had been intercepted then it then it would have replaced
292 * Revision 1.9 2001/05/26 17:12:07 jongfoster
293 * Fatal errors loading configuration files now give better error messages.
295 * Revision 1.8 2001/05/26 00:55:20 jongfoster
296 * Removing duplicated code. load_forwardfile() now uses create_url_spec()
298 * Revision 1.7 2001/05/26 00:28:36 jongfoster
299 * Automatic reloading of config file.
300 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
301 * Most of the global variables have been moved to a new
302 * struct configuration_spec, accessed through csp->config->globalname
303 * Most of the globals remaining are used by the Win32 GUI.
305 * Revision 1.6 2001/05/23 12:27:33 oes
307 * Fixed ugly indentation of my last changes
309 * Revision 1.5 2001/05/23 10:39:05 oes
310 * - Added support for escaping the comment character
311 * in config files by a backslash
312 * - Added support for line continuation in config
314 * - Fixed a buffer overflow bug with long config lines
316 * Revision 1.4 2001/05/22 18:56:28 oes
319 * Revision 1.3 2001/05/20 01:21:20 jongfoster
320 * Version 2.9.4 checkin.
321 * - Merged popupfile and cookiefile, and added control over PCRS
322 * filtering, in new "permissionsfile".
323 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
324 * file error you now get a message box (in the Win32 GUI) rather
325 * than the program exiting with no explanation.
326 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
328 * - Removed tabs from "config"
329 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
330 * - Bumped up version number.
332 * Revision 1.2 2001/05/17 23:01:01 oes
333 * - Cleaned CRLF's from the sources and related files
335 * Revision 1.1.1.1 2001/05/15 13:58:59 oes
336 * Initial import of version 2.9.3 source tree
339 *********************************************************************/
346 #include <sys/types.h>
349 #include <sys/stat.h>
353 #if !defined(_WIN32) && !defined(__OS2__)
363 #include "miscutil.h"
366 #include "urlmatch.h"
369 const char loaders_h_rcs[] = LOADERS_H_VERSION;
372 * Currently active files.
373 * These are also entered in the main linked list of files.
377 static struct file_list *current_trustfile = NULL;
378 #endif /* def FEATURE_TRUST */
380 static int load_one_re_filterfile(struct client_state *csp, int fileid);
382 static struct file_list *current_re_filterfile[MAX_AF_FILES] = {
383 NULL, NULL, NULL, NULL, NULL,
384 NULL, NULL, NULL, NULL, NULL
388 * Pseudo filter type for load_one_re_filterfile
390 #define NO_NEW_FILTER -1
393 /*********************************************************************
397 * Description : Basically a mark and sweep garbage collector, it is run
398 * (by the parent thread) every once in a while to reclaim memory.
400 * It uses a mark and sweep strategy:
401 * 1) mark all files as inactive
403 * 2) check with each client:
404 * if it is active, mark its files as active
405 * if it is inactive, free its resources
407 * 3) free the resources of all of the files that
408 * are still marked as inactive (and are obsolete).
410 * N.B. files that are not obsolete don't have an unloader defined.
416 *********************************************************************/
419 struct file_list *fl, *nfl;
420 struct client_state *csp, *last_active;
423 /* clear all of the file's active flags */
424 for ( fl = files->next; NULL != fl; fl = fl->next )
429 last_active = clients;
434 if (csp->flags & CSP_FLAG_ACTIVE)
436 /* Mark this client's files as active */
439 * Always have a configuration file.
440 * (Also note the slightly non-standard extra
443 csp->config->config_file_list->active = 1;
448 for (i = 0; i < MAX_AF_FILES; i++)
450 if (csp->actions_list[i])
452 csp->actions_list[i]->active = 1;
459 for (i = 0; i < MAX_AF_FILES; i++)
463 csp->rlist[i]->active = 1;
473 csp->tlist->active = 1;
475 #endif /* def FEATURE_TRUST */
483 * This client is not active. Free its resources.
486 last_active->next = csp->next;
488 freez(csp->ip_addr_str);
489 freez(csp->my_ip_addr_str);
490 freez(csp->my_hostname);
491 freez(csp->x_forwarded);
492 freez(csp->iob->buf);
493 freez(csp->error_message);
495 free_http_request(csp->http);
497 destroy_list(csp->headers);
498 destroy_list(csp->tags);
499 destroy_list(csp->cookie_list);
501 free_current_action(csp->action);
503 #ifdef FEATURE_STATISTICS
505 if (csp->flags & CSP_FLAG_REJECTED)
509 #endif /* def FEATURE_STATISTICS */
513 csp = last_active->next;
522 if ( ( 0 == fl->active ) && ( NULL != fl->unloader ) )
524 nfl->next = fl->next;
526 (fl->unloader)(fl->f);
543 /*********************************************************************
545 * Function : check_file_changed
547 * Description : Helper function to check if a file needs reloading.
548 * If "current" is still current, return it. Otherwise
549 * allocates a new (zeroed) "struct file_list", fills
550 * in the disk file name and timestamp, and returns it.
553 * 1 : current = The file_list currently being used - will
554 * be checked to see if it is out of date.
555 * May be NULL (which is treated as out of
557 * 2 : filename = Name of file to check.
558 * 3 : newfl = New file list. [Output only]
559 * This will be set to NULL, OR a struct
560 * file_list newly allocated on the
561 * heap, with the filename and lastmodified
562 * fields filled, and all others zeroed.
564 * Returns : If file unchanged: 0 (and sets newfl == NULL)
565 * If file changed: 1 and sets newfl != NULL
566 * On error: 1 and sets newfl == NULL
568 *********************************************************************/
569 int check_file_changed(const struct file_list * current,
570 const char * filename,
571 struct file_list ** newfl)
573 struct file_list *fs;
574 struct stat statbuf[1];
578 if (stat(filename, statbuf) < 0)
580 /* Error, probably file not found. */
585 && (current->lastmodified == statbuf->st_mtime)
586 && (0 == strcmp(current->filename, filename)))
591 fs = (struct file_list *)zalloc(sizeof(struct file_list));
594 /* Out of memory error */
599 fs->filename = strdup(filename);
600 fs->lastmodified = statbuf->st_mtime;
602 if (fs->filename == NULL)
604 /* Out of memory error */
613 /*********************************************************************
615 * Function : simple_read_line
617 * Description : Read a single line from a file and return it.
618 * This is basically a version of fgets() that malloc()s
619 * it's own line buffer. Note that the buffer will
620 * always be a multiple of BUFFER_SIZE bytes long.
621 * Therefore if you are going to keep the string for
622 * an extended period of time, you should probably
623 * strdup() it and free() the original, to save memory.
627 * 1 : dest = destination for newly malloc'd pointer to
628 * line data. Will be set to NULL on error.
629 * 2 : fp = File to read from
630 * 3 : newline = Standard for newlines in the file.
631 * Will be unchanged if it's value on input is not
633 * On output, may be changed from NEWLINE_UNKNOWN to
634 * actual convention in file.
636 * Returns : JB_ERR_OK on success
637 * JB_ERR_MEMORY on out-of-memory
638 * JB_ERR_FILE on EOF.
640 *********************************************************************/
641 jb_err simple_read_line(FILE *fp, char **dest, int *newline)
644 size_t buflen = BUFFER_SIZE;
648 int realnewline = NEWLINE_UNKNOWN;
650 if (NULL == (buf = malloc(buflen)))
652 return JB_ERR_MEMORY;
658 * Character codes. If you have a wierd compiler and the following are
659 * incorrect, you also need to fix NEWLINE() in loaders.h
661 #define CHAR_CR '\r' /* ASCII 13 */
662 #define CHAR_LF '\n' /* ASCII 10 */
682 else if (ch == CHAR_CR)
687 if (*newline == NEWLINE_UNKNOWN)
689 *newline = NEWLINE_DOS;
698 if (*newline == NEWLINE_UNKNOWN)
700 *newline = NEWLINE_MAC;
705 if (*newline == NEWLINE_UNKNOWN)
707 *newline = realnewline;
711 else if (ch == CHAR_LF)
715 if (*newline == NEWLINE_UNKNOWN)
717 *newline = NEWLINE_UNIX;
732 buflen += BUFFER_SIZE;
733 if (NULL == (p = realloc(buf, buflen)))
736 return JB_ERR_MEMORY;
745 /*********************************************************************
747 * Function : edit_read_line
749 * Description : Read a single non-empty line from a file and return
750 * it. Trims comments, leading and trailing whitespace
751 * and respects escaping of newline and comment char.
752 * Provides the line in 2 alternative forms: raw and
754 * - raw is the raw data read from the file. If the
755 * line is not modified, then this should be written
757 * - prefix is any comments and blank lines that were
758 * read from the file. If the line is modified, then
759 * this should be written out to the file followed
760 * by the modified data. (If this string is non-empty
761 * then it will have a newline at the end).
762 * - data is the actual data that will be parsed
763 * further by appropriate routines.
764 * On EOF, the 3 strings will all be set to NULL and
765 * 0 will be returned.
768 * 1 : fp = File to read from
769 * 2 : raw_out = destination for newly malloc'd pointer to
770 * raw line data. May be NULL if you don't want it.
771 * 3 : prefix_out = destination for newly malloc'd pointer to
772 * comments. May be NULL if you don't want it.
773 * 4 : data_out = destination for newly malloc'd pointer to
774 * line data with comments and leading/trailing spaces
775 * removed, and line continuation performed. May be
776 * NULL if you don't want it.
777 * 5 : newline = Standard for newlines in the file.
778 * On input, set to value to use or NEWLINE_UNKNOWN.
779 * On output, may be changed from NEWLINE_UNKNOWN to
780 * actual convention in file. May be NULL if you
782 * 6 : line_number = Line number in file. In "lines" as
783 * reported by a text editor, not lines containing data.
785 * Returns : JB_ERR_OK on success
786 * JB_ERR_MEMORY on out-of-memory
787 * JB_ERR_FILE on EOF.
789 *********************************************************************/
790 jb_err edit_read_line(FILE *fp,
795 unsigned long *line_number)
797 char *p; /* Temporary pointer */
798 char *linebuf; /* Line read from file */
799 char *linestart; /* Start of linebuf, usually first non-whitespace char */
800 int contflag = 0; /* Nonzero for line continuation - i.e. line ends '\' */
801 int is_empty = 1; /* Flag if not got any data yet */
802 char *raw = NULL; /* String to be stored in raw_out */
803 char *prefix = NULL; /* String to be stored in prefix_out */
804 char *data = NULL; /* String to be stored in data_out */
805 int scrapnewline; /* Used for (*newline) if newline==NULL */
806 jb_err rval = JB_ERR_OK;
809 assert(raw_out || data_out);
810 assert(newline == NULL
811 || *newline == NEWLINE_UNKNOWN
812 || *newline == NEWLINE_UNIX
813 || *newline == NEWLINE_DOS
814 || *newline == NEWLINE_MAC);
818 scrapnewline = NEWLINE_UNKNOWN;
819 newline = &scrapnewline;
822 /* Set output parameters to NULL */
836 /* Set string variables to new, empty strings. */
840 if ((raw = malloc(1)) == NULL)
842 return JB_ERR_MEMORY;
848 if ((prefix = malloc(1)) == NULL)
851 return JB_ERR_MEMORY;
857 if ((data = malloc(1)) == NULL)
861 return JB_ERR_MEMORY;
866 /* Main loop. Loop while we need more data & it's not EOF. */
868 while ( (contflag || is_empty)
869 && (JB_ERR_OK == (rval = simple_read_line(fp, &linebuf, newline))))
877 string_append(&raw,linebuf);
878 if (string_append(&raw,NEWLINE(*newline)))
883 return JB_ERR_MEMORY;
887 /* Line continuation? Trim escape and set flag. */
888 p = linebuf + strlen(linebuf) - 1;
889 contflag = ((*linebuf != '\0') && (*p == '\\'));
895 /* Trim leading spaces if we're at the start of the line */
899 /* Trim leading spaces */
900 while (*linestart && isspace((int)(unsigned char)*linestart))
906 /* Handle comment characters. */
908 while ((p = strchr(p, '#')) != NULL)
910 /* Found a comment char.. */
911 if ((p != linebuf) && (*(p-1) == '\\'))
913 /* ..and it's escaped, left-shift the line over the escape. */
915 while ((*q = *(q + 1)) != '\0')
919 /* Now scan from just after the "#". */
923 /* Real comment. Save it... */
926 /* Special case: Line only contains a comment, so all the
927 * previous whitespace is considered part of the comment.
928 * Undo the whitespace skipping, if any.
935 string_append(&prefix,p);
936 if (string_append(&prefix, NEWLINE(*newline)))
941 return JB_ERR_MEMORY;
945 /* ... and chop off the rest of the line */
948 } /* END while (there's a # character) */
950 /* Write to the buffer */
956 if (string_append(&data, linestart))
961 return JB_ERR_MEMORY;
967 } /* END while(we need more data) */
969 /* Handle simple_read_line() errors - ignore EOF */
970 if ((rval != JB_ERR_OK) && (rval != JB_ERR_FILE))
978 if (raw ? (*raw == '\0') : is_empty)
980 /* EOF and no data there. (Definition of "data" depends on whether
981 * the caller cares about "raw" or just "data").
992 /* Got at least some data */
994 /* Remove trailing whitespace */
1007 *prefix_out = prefix;
1026 /*********************************************************************
1028 * Function : read_config_line
1030 * Description : Read a single non-empty line from a file and return
1031 * it. Trims comments, leading and trailing whitespace
1032 * and respects escaping of newline and comment char.
1035 * 1 : buf = Buffer to use.
1036 * 2 : buflen = Size of buffer in bytes.
1037 * 3 : fp = File to read from
1038 * 4 : linenum = linenumber in file
1040 * Returns : NULL on EOF or error
1041 * Otherwise, returns buf.
1043 *********************************************************************/
1044 char *read_config_line(char *buf, size_t buflen, FILE *fp, unsigned long *linenum)
1048 err = edit_read_line(fp, NULL, NULL, &buf2, NULL, linenum);
1051 if (err == JB_ERR_MEMORY)
1053 log_error(LOG_LEVEL_FATAL, "Out of memory loading a config file");
1060 assert(strlen(buf2) + 1U < buflen);
1061 strncpy(buf, buf2, buflen - 1);
1063 buf[buflen - 1] = '\0';
1069 #ifdef FEATURE_TRUST
1070 /*********************************************************************
1072 * Function : unload_trustfile
1074 * Description : Unloads a trustfile.
1077 * 1 : f = the data structure associated with the trustfile.
1081 *********************************************************************/
1082 static void unload_trustfile(void *f)
1084 struct block_spec *cur = (struct block_spec *)f;
1085 struct block_spec *next;
1091 free_url_spec(cur->url);
1100 #ifdef FEATURE_GRACEFUL_TERMINATION
1101 /*********************************************************************
1103 * Function : unload_current_trust_file
1105 * Description : Unloads current trust file - reset to state at
1106 * beginning of program.
1112 *********************************************************************/
1113 void unload_current_trust_file(void)
1115 if (current_trustfile)
1117 current_trustfile->unloader = unload_trustfile;
1118 current_trustfile = NULL;
1121 #endif /* FEATURE_GRACEFUL_TERMINATION */
1124 /*********************************************************************
1126 * Function : load_trustfile
1128 * Description : Read and parse a trustfile and add to files list.
1131 * 1 : csp = Current client state (buffers, headers, etc...)
1133 * Returns : 0 => Ok, everything else is an error.
1135 *********************************************************************/
1136 int load_trustfile(struct client_state *csp)
1140 struct block_spec *b, *bl;
1141 struct url_spec **tl;
1143 char buf[BUFFER_SIZE], *p, *q;
1144 int reject, trusted;
1145 struct file_list *fs;
1146 unsigned long linenum = 0;
1147 int trusted_referrers = 0;
1149 if (!check_file_changed(current_trustfile, csp->config->trustfile, &fs))
1151 /* No need to load */
1154 csp->tlist = current_trustfile;
1160 goto load_trustfile_error;
1163 fs->f = bl = (struct block_spec *)zalloc(sizeof(*bl));
1166 goto load_trustfile_error;
1169 if ((fp = fopen(csp->config->trustfile, "r")) == NULL)
1171 goto load_trustfile_error;
1174 tl = csp->config->trust_list;
1176 while (read_config_line(buf, sizeof(buf), fp, &linenum) != NULL)
1192 while ((*p++ = *q++) != '\0')
1198 /* skip blank lines */
1204 /* allocate a new node */
1205 if ((b = zalloc(sizeof(*b))) == NULL)
1208 goto load_trustfile_error;
1211 /* add it to the list */
1217 /* Save the URL pattern */
1218 if (create_url_spec(b->url, buf))
1221 goto load_trustfile_error;
1225 * save a pointer to URL's spec in the list of trusted URL's, too
1229 if(++trusted_referrers < MAX_TRUSTED_REFERRERS)
1236 if(trusted_referrers >= MAX_TRUSTED_REFERRERS)
1239 * FIXME: ... after Privoxy 3.0.4 is out.
1241 log_error(LOG_LEVEL_ERROR, "Too many trusted referrers. Current limit is %d, you are using %d.\n"
1242 " Additional trusted referrers are treated like ordinary trusted URLs.\n"
1243 " (You can increase this limit by changing MAX_TRUSTED_REFERRERS in project.h and recompiling).",
1244 MAX_TRUSTED_REFERRERS, trusted_referrers);
1251 /* the old one is now obsolete */
1252 if (current_trustfile)
1254 current_trustfile->unloader = unload_trustfile;
1257 fs->next = files->next;
1259 current_trustfile = fs;
1268 load_trustfile_error:
1269 log_error(LOG_LEVEL_FATAL, "can't load trustfile '%s': %E",
1270 csp->config->trustfile);
1274 #endif /* def FEATURE_TRUST */
1277 /*********************************************************************
1279 * Function : unload_re_filterfile
1281 * Description : Unload the re_filter list by freeing all chained
1282 * re_filterfile specs and their data.
1285 * 1 : f = the data structure associated with the filterfile.
1289 *********************************************************************/
1290 static void unload_re_filterfile(void *f)
1292 struct re_filterfile_spec *a, *b = (struct re_filterfile_spec *)f;
1298 destroy_list(b->patterns);
1299 pcrs_free_joblist(b->joblist);
1301 freez(b->description);
1311 #ifdef FEATURE_GRACEFUL_TERMINATION
1312 /*********************************************************************
1314 * Function : unload_current_re_filterfile
1316 * Description : Unloads current re_filter file - reset to state at
1317 * beginning of program.
1323 *********************************************************************/
1324 void unload_current_re_filterfile(void)
1328 for (i = 0; i < MAX_AF_FILES; i++)
1330 if (current_re_filterfile[i])
1332 current_re_filterfile[i]->unloader = unload_re_filterfile;
1333 current_re_filterfile[i] = NULL;
1340 /*********************************************************************
1342 * Function : load_re_filterfile
1344 * Description : Load the re_filterfile.
1345 * Generate a chained list of re_filterfile_spec's from
1346 * the "FILTER: " blocks, compiling all their substitutions
1347 * into chained lists of pcrs_job structs.
1350 * 1 : csp = Current client state (buffers, headers, etc...)
1352 * Returns : 0 => Ok, everything else is an error.
1354 *********************************************************************/
1355 int load_re_filterfile(struct client_state *csp)
1360 for (i = 0; i < MAX_AF_FILES; i++)
1362 if (csp->config->re_filterfile[i])
1364 result = load_one_re_filterfile(csp, i);
1370 else if (current_re_filterfile[i])
1372 current_re_filterfile[i]->unloader = unload_re_filterfile;
1373 current_re_filterfile[i] = NULL;
1380 /*********************************************************************
1382 * Function : load_one_re_filterfile
1384 * Description : Load a re_filterfile.
1385 * Generate a chained list of re_filterfile_spec's from
1386 * the "FILTER: " blocks, compiling all their substitutions
1387 * into chained lists of pcrs_job structs.
1390 * 1 : csp = Current client state (buffers, headers, etc...)
1392 * Returns : 0 => Ok, everything else is an error.
1394 *********************************************************************/
1395 int load_one_re_filterfile(struct client_state *csp, int fileid)
1399 struct re_filterfile_spec *new_bl, *bl = NULL;
1400 struct file_list *fs;
1402 char buf[BUFFER_SIZE];
1404 unsigned long linenum = 0;
1405 pcrs_job *dummy, *lastjob = NULL;
1408 * No need to reload if unchanged
1410 if (!check_file_changed(current_re_filterfile[fileid], csp->config->re_filterfile[fileid], &fs))
1414 csp->rlist[fileid] = current_re_filterfile[fileid];
1420 goto load_re_filterfile_error;
1424 * Open the file or fail
1426 if ((fp = fopen(csp->config->re_filterfile[fileid], "r")) == NULL)
1428 goto load_re_filterfile_error;
1434 while (read_config_line(buf, sizeof(buf), fp, &linenum) != NULL)
1436 int new_filter = NO_NEW_FILTER;
1438 if (strncmp(buf, "FILTER:", 7) == 0)
1440 new_filter = FT_CONTENT_FILTER;
1442 else if (strncmp(buf, "SERVER-HEADER-FILTER:", 21) == 0)
1444 new_filter = FT_SERVER_HEADER_FILTER;
1446 else if (strncmp(buf, "CLIENT-HEADER-FILTER:", 21) == 0)
1448 new_filter = FT_CLIENT_HEADER_FILTER;
1450 else if (strncmp(buf, "CLIENT-HEADER-TAGGER:", 21) == 0)
1452 new_filter = FT_CLIENT_HEADER_TAGGER;
1454 else if (strncmp(buf, "SERVER-HEADER-TAGGER:", 21) == 0)
1456 new_filter = FT_SERVER_HEADER_TAGGER;
1460 * If this is the head of a new filter block, make it a
1461 * re_filterfile spec of its own and chain it to the list:
1463 if (new_filter != NO_NEW_FILTER)
1465 new_bl = (struct re_filterfile_spec *)zalloc(sizeof(*bl));
1468 goto load_re_filterfile_error;
1470 if (new_filter == FT_CONTENT_FILTER)
1472 new_bl->name = chomp(buf + 7);
1476 new_bl->name = chomp(buf + 21);
1478 new_bl->type = new_filter;
1481 * If a filter description is available,
1482 * encode it to HTML and save it.
1484 if (NULL != (new_bl->description = strpbrk(new_bl->name, " \t")))
1486 *new_bl->description++ = '\0';
1487 new_bl->description = html_encode(chomp(new_bl->description));
1488 if (NULL == new_bl->description)
1490 new_bl->description = strdup("Out of memory while encoding this filter's description to HTML");
1495 new_bl->description = strdup("No description available for this filter");
1498 new_bl->name = strdup(chomp(new_bl->name));
1501 * If this is the first filter block, chain it
1502 * to the file_list rather than its (nonexistant)
1515 log_error(LOG_LEVEL_RE_FILTER, "Reading in filter \"%s\" (\"%s\")", bl->name, bl->description);
1521 * Else, save the expression, make it a pcrs_job
1522 * and chain it into the current filter's joblist
1526 enlist(bl->patterns, buf);
1528 if ((dummy = pcrs_compile_command(buf, &error)) == NULL)
1530 log_error(LOG_LEVEL_ERROR,
1531 "Adding re_filter job %s to filter %s failed with error %d.", buf, bl->name, error);
1536 if (bl->joblist == NULL)
1538 bl->joblist = dummy;
1542 lastjob->next = dummy;
1545 log_error(LOG_LEVEL_RE_FILTER, "Adding re_filter job %s to filter %s succeeded.", buf, bl->name);
1550 log_error(LOG_LEVEL_ERROR, "Ignoring job %s outside filter block in %s, line %d",
1551 buf, csp->config->re_filterfile[fileid], linenum);
1558 * Schedule the now-obsolete old data for unloading
1560 if ( NULL != current_re_filterfile[fileid] )
1562 current_re_filterfile[fileid]->unloader = unload_re_filterfile;
1566 * Chain this file into the global list of loaded files
1568 fs->next = files->next;
1570 current_re_filterfile[fileid] = fs;
1574 csp->rlist[fileid] = fs;
1579 load_re_filterfile_error:
1580 log_error(LOG_LEVEL_FATAL, "can't load re_filterfile '%s': %E",
1581 csp->config->re_filterfile[fileid]);
1587 /*********************************************************************
1589 * Function : add_loader
1591 * Description : Called from `load_config'. Called once for each input
1592 * file found in config.
1595 * 1 : loader = pointer to a function that can parse and load
1596 * the appropriate config file.
1597 * 2 : config = The configuration_spec to add the loader to.
1601 *********************************************************************/
1602 void add_loader(int (*loader)(struct client_state *),
1603 struct configuration_spec * config)
1607 for (i=0; i < NLOADERS; i++)
1609 if (config->loaders[i] == NULL)
1611 config->loaders[i] = loader;
1619 /*********************************************************************
1621 * Function : run_loader
1623 * Description : Called from `load_config' and `listen_loop'. This
1624 * function keeps the "csp" current with any file mods
1625 * since the last loop. If a file is unchanged, the
1626 * loader functions do NOT reload the file.
1629 * 1 : csp = Current client state (buffers, headers, etc...)
1630 * Must be non-null. Reads: "csp->config"
1631 * Writes: various data members.
1633 * Returns : 0 => Ok, everything else is an error.
1635 *********************************************************************/
1636 int run_loader(struct client_state *csp)
1641 for (i=0; i < NLOADERS; i++)
1643 if (csp->config->loaders[i] == NULL)
1647 ret |= (csp->config->loaders[i])(csp);