1 const char loaders_rcs[] = "$Id: loaders.c,v 1.57 2006/12/21 12:22:22 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 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.57 2006/12/21 12:22:22 fabiankeil
39 * html_encode filter descriptions.
41 * Have "Ignoring job ..." error messages
42 * print the filter file name correctly.
44 * Revision 1.56 2006/09/07 10:40:30 fabiankeil
45 * Turns out trusted referrers above our arbitrary
46 * limit are downgraded too ordinary trusted URLs.
47 * Adjusted error message.
49 * Revision 1.55 2006/09/07 10:25:39 fabiankeil
52 * Revision 1.54 2006/09/07 10:22:20 fabiankeil
53 * If too many trusted referrers are used,
54 * print only one error message instead of logging
55 * every single trusted referrer above the arbitrary
58 * Revision 1.53 2006/08/31 16:25:06 fabiankeil
59 * Work around a buffer overflow that caused Privoxy to
60 * segfault if too many trusted referrers were used. Good
61 * enough for now, but should be replaced with a real
62 * solution after the next release.
64 * Revision 1.52 2006/07/18 14:48:46 david__schmidt
65 * Reorganizing the repository: swapping out what was HEAD (the old 3.1 branch)
66 * with what was really the latest development (the v_3_0_branch branch)
68 * Revision 1.50.2.8 2006/01/30 15:16:25 david__schmidt
69 * Remove a little residual debugging info
71 * Revision 1.50.2.7 2006/01/29 23:10:56 david__schmidt
72 * Multiple filter file support
74 * Revision 1.50.2.6 2003/10/24 10:17:54 oes
75 * Nit: Allowed tabs as separators in filter headings
77 * Revision 1.50.2.5 2003/05/08 15:19:15 oes
78 * sweep: Made loop structure of sweep step mirror that of mark step
80 * Revision 1.50.2.4 2003/05/06 15:57:12 oes
81 * Bugfix: Update last_active pointer in sweep() before
82 * leaving an active client. Closes bugs #724395, #727882
84 * Revision 1.50.2.3 2002/11/20 17:12:30 oes
85 * Ooops, forgot one change.
87 * Revision 1.50.2.2 2002/11/20 14:38:15 oes
88 * Fixed delayed/incomplete freeing of client resources and
89 * simplified loop structure in sweep.
90 * Thanks to Oliver Stoeneberg for the hint.
92 * Revision 1.50.2.1 2002/07/26 15:19:24 oes
93 * - PCRS jobs now chained in order of appearance. Previous
94 * reverse chaining was counter-intuitive.
95 * - Changed loglevel of PCRS job compile errors to
98 * Revision 1.50 2002/04/24 02:12:16 oes
99 * Jon's multiple AF patch: Sweep now takes care of all AFs
101 * Revision 1.49 2002/04/19 16:53:25 jongfoster
102 * Optimize away a function call by using an equivalent macro
104 * Revision 1.48 2002/04/05 00:56:09 gliptak
105 * Correcting typo to clean up on realloc failure
107 * Revision 1.47 2002/03/26 22:29:55 swa
108 * we have a new homepage!
110 * Revision 1.46 2002/03/24 13:25:43 swa
111 * name change related issues
113 * Revision 1.45 2002/03/16 23:54:06 jongfoster
114 * Adding graceful termination feature, to help look for memory leaks.
115 * If you enable this (which, by design, has to be done by hand
116 * editing config.h) and then go to http://i.j.b/die, then the program
117 * will exit cleanly after the *next* request. It should free all the
118 * memory that was used.
120 * Revision 1.44 2002/03/16 21:51:00 jongfoster
123 * Revision 1.43 2002/03/16 20:28:34 oes
124 * Added descriptions to the filters so users will know what they select in the cgi editor
126 * Revision 1.42 2002/03/13 00:27:05 jongfoster
129 * Revision 1.41 2002/03/12 01:42:50 oes
130 * Introduced modular filters
132 * Revision 1.40 2002/03/08 17:46:04 jongfoster
133 * Fixing int/size_t warnings
135 * Revision 1.39 2002/03/07 03:46:17 oes
136 * Fixed compiler warnings
138 * Revision 1.38 2002/03/06 22:54:35 jongfoster
139 * Automated function-comment nitpicking.
141 * Revision 1.37 2002/03/03 15:07:49 oes
142 * Re-enabled automatic config reloading
144 * Revision 1.36 2002/01/22 23:46:18 jongfoster
145 * Moving edit_read_line() and simple_read_line() to loaders.c, and
146 * extending them to support reading MS-DOS, Mac and UNIX style files
149 * Modifying read_config_line() (without changing it's prototype) to
150 * be a trivial wrapper for edit_read_line(). This means that we have
151 * one function to read a line and handle comments, which is common
152 * between the initialization code and the edit interface.
154 * Revision 1.35 2002/01/17 21:03:08 jongfoster
155 * Moving all our URL and URL pattern parsing code to urlmatch.c.
157 * Renaming free_url to free_url_spec, since it frees a struct url_spec.
159 * Revision 1.34 2001/12/30 14:07:32 steudten
160 * - Add signal handling (unix)
161 * - Add SIGHUP handler (unix)
162 * - Add creation of pidfile (unix)
163 * - Add action 'top' in rc file (RH)
164 * - Add entry 'SIGNALS' to manpage
165 * - Add exit message to logfile (unix)
167 * Revision 1.33 2001/11/13 00:16:38 jongfoster
168 * Replacing references to malloc.h with the standard stdlib.h
169 * (See ANSI or K&R 2nd Ed)
171 * Revision 1.32 2001/11/07 00:02:13 steudten
172 * Add line number in error output for lineparsing for
173 * actionsfile and configfile.
174 * Special handling for CLF added.
176 * Revision 1.31 2001/10/26 17:39:01 oes
177 * Removed csp->referrer
178 * Moved ijb_isspace and ijb_tolower to project.h
180 * Revision 1.30 2001/10/25 03:40:48 david__schmidt
181 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
182 * threads to call select() simultaneously. So, it's time to do a real, live,
183 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
184 * (native). Both versions will work, but using __OS2__ offers multi-threading.
186 * Revision 1.29 2001/10/23 21:38:53 jongfoster
187 * Adding error-checking to create_url_spec()
189 * Revision 1.28 2001/10/07 15:40:39 oes
190 * Replaced 6 boolean members of csp with one bitmap (csp->flags)
192 * Revision 1.27 2001/09/22 16:36:59 jongfoster
193 * Removing unused parameter fs from read_config_line()
195 * Revision 1.26 2001/09/22 14:05:22 jongfoster
196 * Bugfix: Multiple escaped "#" characters in a configuration
197 * file are now permitted.
198 * Also removing 3 unused headers.
200 * Revision 1.25 2001/09/13 22:44:03 jongfoster
201 * Adding {} to an if statement
203 * Revision 1.24 2001/07/30 22:08:36 jongfoster
204 * Tidying up #defines:
205 * - All feature #defines are now of the form FEATURE_xxx
206 * - Permanently turned off WIN_GUI_EDIT
207 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
209 * Revision 1.23 2001/07/20 15:51:54 oes
210 * Fixed indentation of prepocessor commands
212 * Revision 1.22 2001/07/20 15:16:17 haroon
213 * - per Guy's suggestion, added a while loop in sweep() to catch not just
214 * the last inactive CSP but all other consecutive inactive CSPs after that
217 * Revision 1.21 2001/07/18 17:26:24 oes
218 * Changed to conform to new pcrs interface
220 * Revision 1.20 2001/07/17 13:07:01 oes
221 * Fixed segv when last line in config files
222 * lacked a terminating (\r)\n
224 * Revision 1.19 2001/07/13 14:01:54 oes
225 * Removed all #ifdef PCRS
227 * Revision 1.18 2001/06/29 21:45:41 oes
228 * Indentation, CRLF->LF, Tab-> Space
230 * Revision 1.17 2001/06/29 13:31:51 oes
233 * Revision 1.16 2001/06/09 10:55:28 jongfoster
234 * Changing BUFSIZ ==> BUFFER_SIZE
236 * Revision 1.15 2001/06/07 23:14:14 jongfoster
237 * Removing ACL and forward file loaders - these
238 * files have been merged into the config file.
239 * Cosmetic: Moving unloader funcs next to their
240 * respective loader funcs
242 * Revision 1.14 2001/06/01 03:27:04 oes
243 * Fixed line continuation problem
245 * Revision 1.13 2001/05/31 21:28:49 jongfoster
246 * Removed all permissionsfile code - it's now called the actions
247 * file, and (almost) all the code is in actions.c
249 * Revision 1.12 2001/05/31 17:32:31 oes
251 * - Enhanced domain part globbing with infix and prefix asterisk
252 * matching and optional unanchored operation
254 * Revision 1.11 2001/05/29 23:25:24 oes
256 * - load_config_line() and load_permissions_file() now use chomp()
258 * Revision 1.10 2001/05/29 09:50:24 jongfoster
259 * Unified blocklist/imagelist/permissionslist.
260 * File format is still under discussion, but the internal changes
263 * Also modified interceptor behaviour:
264 * - We now intercept all URLs beginning with one of the following
265 * prefixes (and *only* these prefixes):
267 * * http://ijbswa.sf.net/config/
268 * * http://ijbswa.sourceforge.net/config/
269 * - New interceptors "home page" - go to http://i.j.b/ to see it.
270 * - Internal changes so that intercepted and fast redirect pages
271 * are not replaced with an image.
272 * - Interceptors now have the option to send a binary page direct
273 * to the client. (i.e. ijb-send-banner uses this)
274 * - Implemented show-url-info interceptor. (Which is why I needed
275 * the above interceptors changes - a typical URL is
276 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
277 * The previous mechanism would not have intercepted that, and
278 * if it had been intercepted then it then it would have replaced
281 * Revision 1.9 2001/05/26 17:12:07 jongfoster
282 * Fatal errors loading configuration files now give better error messages.
284 * Revision 1.8 2001/05/26 00:55:20 jongfoster
285 * Removing duplicated code. load_forwardfile() now uses create_url_spec()
287 * Revision 1.7 2001/05/26 00:28:36 jongfoster
288 * Automatic reloading of config file.
289 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
290 * Most of the global variables have been moved to a new
291 * struct configuration_spec, accessed through csp->config->globalname
292 * Most of the globals remaining are used by the Win32 GUI.
294 * Revision 1.6 2001/05/23 12:27:33 oes
296 * Fixed ugly indentation of my last changes
298 * Revision 1.5 2001/05/23 10:39:05 oes
299 * - Added support for escaping the comment character
300 * in config files by a backslash
301 * - Added support for line continuation in config
303 * - Fixed a buffer overflow bug with long config lines
305 * Revision 1.4 2001/05/22 18:56:28 oes
308 * Revision 1.3 2001/05/20 01:21:20 jongfoster
309 * Version 2.9.4 checkin.
310 * - Merged popupfile and cookiefile, and added control over PCRS
311 * filtering, in new "permissionsfile".
312 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
313 * file error you now get a message box (in the Win32 GUI) rather
314 * than the program exiting with no explanation.
315 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
317 * - Removed tabs from "config"
318 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
319 * - Bumped up version number.
321 * Revision 1.2 2001/05/17 23:01:01 oes
322 * - Cleaned CRLF's from the sources and related files
324 * Revision 1.1.1.1 2001/05/15 13:58:59 oes
325 * Initial import of version 2.9.3 source tree
328 *********************************************************************/
335 #include <sys/types.h>
338 #include <sys/stat.h>
342 #if !defined(_WIN32) && !defined(__OS2__)
352 #include "miscutil.h"
355 #include "urlmatch.h"
358 const char loaders_h_rcs[] = LOADERS_H_VERSION;
361 * Currently active files.
362 * These are also entered in the main linked list of files.
366 static struct file_list *current_trustfile = NULL;
367 #endif /* def FEATURE_TRUST */
369 static int load_one_re_filterfile(struct client_state *csp, int fileid);
371 static struct file_list *current_re_filterfile[MAX_AF_FILES] = {
372 NULL, NULL, NULL, NULL, NULL,
373 NULL, NULL, NULL, NULL, NULL
378 /*********************************************************************
382 * Description : Basically a mark and sweep garbage collector, it is run
383 * (by the parent thread) every once in a while to reclaim memory.
385 * It uses a mark and sweep strategy:
386 * 1) mark all files as inactive
388 * 2) check with each client:
389 * if it is active, mark its files as active
390 * if it is inactive, free its resources
392 * 3) free the resources of all of the files that
393 * are still marked as inactive (and are obsolete).
395 * N.B. files that are not obsolete don't have an unloader defined.
401 *********************************************************************/
404 struct file_list *fl, *nfl;
405 struct client_state *csp, *last_active;
408 /* clear all of the file's active flags */
409 for ( fl = files->next; NULL != fl; fl = fl->next )
414 last_active = clients;
419 if (csp->flags & CSP_FLAG_ACTIVE)
421 /* Mark this client's files as active */
424 * Always have a configuration file.
425 * (Also note the slightly non-standard extra
428 csp->config->config_file_list->active = 1;
433 for (i = 0; i < MAX_AF_FILES; i++)
435 if (csp->actions_list[i])
437 csp->actions_list[i]->active = 1;
444 for (i = 0; i < MAX_AF_FILES; i++)
448 csp->rlist[i]->active = 1;
458 csp->tlist->active = 1;
460 #endif /* def FEATURE_TRUST */
468 * This client is not active. Free its resources.
471 last_active->next = csp->next;
473 freez(csp->ip_addr_str);
474 freez(csp->my_ip_addr_str);
475 freez(csp->my_hostname);
476 freez(csp->x_forwarded);
477 freez(csp->iob->buf);
479 free_http_request(csp->http);
481 destroy_list(csp->headers);
482 destroy_list(csp->cookie_list);
484 free_current_action(csp->action);
486 #ifdef FEATURE_STATISTICS
488 if (csp->flags & CSP_FLAG_REJECTED)
492 #endif /* def FEATURE_STATISTICS */
496 csp = last_active->next;
505 if ( ( 0 == fl->active ) && ( NULL != fl->unloader ) )
507 nfl->next = fl->next;
509 (fl->unloader)(fl->f);
526 /*********************************************************************
528 * Function : check_file_changed
530 * Description : Helper function to check if a file needs reloading.
531 * If "current" is still current, return it. Otherwise
532 * allocates a new (zeroed) "struct file_list", fills
533 * in the disk file name and timestamp, and returns it.
536 * 1 : current = The file_list currently being used - will
537 * be checked to see if it is out of date.
538 * May be NULL (which is treated as out of
540 * 2 : filename = Name of file to check.
541 * 3 : newfl = New file list. [Output only]
542 * This will be set to NULL, OR a struct
543 * file_list newly allocated on the
544 * heap, with the filename and lastmodified
545 * fields filled, and all others zeroed.
547 * Returns : If file unchanged: 0 (and sets newfl == NULL)
548 * If file changed: 1 and sets newfl != NULL
549 * On error: 1 and sets newfl == NULL
551 *********************************************************************/
552 int check_file_changed(const struct file_list * current,
553 const char * filename,
554 struct file_list ** newfl)
556 struct file_list *fs;
557 struct stat statbuf[1];
561 if (stat(filename, statbuf) < 0)
563 /* Error, probably file not found. */
568 && (current->lastmodified == statbuf->st_mtime)
569 && (0 == strcmp(current->filename, filename)))
574 fs = (struct file_list *)zalloc(sizeof(struct file_list));
577 /* Out of memory error */
582 fs->filename = strdup(filename);
583 fs->lastmodified = statbuf->st_mtime;
585 if (fs->filename == NULL)
587 /* Out of memory error */
596 /*********************************************************************
598 * Function : simple_read_line
600 * Description : Read a single line from a file and return it.
601 * This is basically a version of fgets() that malloc()s
602 * it's own line buffer. Note that the buffer will
603 * always be a multiple of BUFFER_SIZE bytes long.
604 * Therefore if you are going to keep the string for
605 * an extended period of time, you should probably
606 * strdup() it and free() the original, to save memory.
610 * 1 : dest = destination for newly malloc'd pointer to
611 * line data. Will be set to NULL on error.
612 * 2 : fp = File to read from
613 * 3 : newline = Standard for newlines in the file.
614 * Will be unchanged if it's value on input is not
616 * On output, may be changed from NEWLINE_UNKNOWN to
617 * actual convention in file.
619 * Returns : JB_ERR_OK on success
620 * JB_ERR_MEMORY on out-of-memory
621 * JB_ERR_FILE on EOF.
623 *********************************************************************/
624 jb_err simple_read_line(FILE *fp, char **dest, int *newline)
627 size_t buflen = BUFFER_SIZE;
631 int realnewline = NEWLINE_UNKNOWN;
633 if (NULL == (buf = malloc(buflen)))
635 return JB_ERR_MEMORY;
641 * Character codes. If you have a wierd compiler and the following are
642 * incorrect, you also need to fix NEWLINE() in loaders.h
644 #define CHAR_CR '\r' /* ASCII 13 */
645 #define CHAR_LF '\n' /* ASCII 10 */
665 else if (ch == CHAR_CR)
670 if (*newline == NEWLINE_UNKNOWN)
672 *newline = NEWLINE_DOS;
681 if (*newline == NEWLINE_UNKNOWN)
683 *newline = NEWLINE_MAC;
688 if (*newline == NEWLINE_UNKNOWN)
690 *newline = realnewline;
694 else if (ch == CHAR_LF)
698 if (*newline == NEWLINE_UNKNOWN)
700 *newline = NEWLINE_UNIX;
715 buflen += BUFFER_SIZE;
716 if (NULL == (p = realloc(buf, buflen)))
719 return JB_ERR_MEMORY;
728 /*********************************************************************
730 * Function : edit_read_line
732 * Description : Read a single non-empty line from a file and return
733 * it. Trims comments, leading and trailing whitespace
734 * and respects escaping of newline and comment char.
735 * Provides the line in 2 alternative forms: raw and
737 * - raw is the raw data read from the file. If the
738 * line is not modified, then this should be written
740 * - prefix is any comments and blank lines that were
741 * read from the file. If the line is modified, then
742 * this should be written out to the file followed
743 * by the modified data. (If this string is non-empty
744 * then it will have a newline at the end).
745 * - data is the actual data that will be parsed
746 * further by appropriate routines.
747 * On EOF, the 3 strings will all be set to NULL and
748 * 0 will be returned.
751 * 1 : fp = File to read from
752 * 2 : raw_out = destination for newly malloc'd pointer to
753 * raw line data. May be NULL if you don't want it.
754 * 3 : prefix_out = destination for newly malloc'd pointer to
755 * comments. May be NULL if you don't want it.
756 * 4 : data_out = destination for newly malloc'd pointer to
757 * line data with comments and leading/trailing spaces
758 * removed, and line continuation performed. May be
759 * NULL if you don't want it.
760 * 5 : newline = Standard for newlines in the file.
761 * On input, set to value to use or NEWLINE_UNKNOWN.
762 * On output, may be changed from NEWLINE_UNKNOWN to
763 * actual convention in file. May be NULL if you
765 * 6 : line_number = Line number in file. In "lines" as
766 * reported by a text editor, not lines containing data.
768 * Returns : JB_ERR_OK on success
769 * JB_ERR_MEMORY on out-of-memory
770 * JB_ERR_FILE on EOF.
772 *********************************************************************/
773 jb_err edit_read_line(FILE *fp,
778 unsigned long *line_number)
780 char *p; /* Temporary pointer */
781 char *linebuf; /* Line read from file */
782 char *linestart; /* Start of linebuf, usually first non-whitespace char */
783 int contflag = 0; /* Nonzero for line continuation - i.e. line ends '\' */
784 int is_empty = 1; /* Flag if not got any data yet */
785 char *raw = NULL; /* String to be stored in raw_out */
786 char *prefix = NULL; /* String to be stored in prefix_out */
787 char *data = NULL; /* String to be stored in data_out */
788 int scrapnewline; /* Used for (*newline) if newline==NULL */
789 jb_err rval = JB_ERR_OK;
792 assert(raw_out || data_out);
793 assert(newline == NULL
794 || *newline == NEWLINE_UNKNOWN
795 || *newline == NEWLINE_UNIX
796 || *newline == NEWLINE_DOS
797 || *newline == NEWLINE_MAC);
801 scrapnewline = NEWLINE_UNKNOWN;
802 newline = &scrapnewline;
805 /* Set output parameters to NULL */
819 /* Set string variables to new, empty strings. */
823 if ((raw = malloc(1)) == NULL)
825 return JB_ERR_MEMORY;
831 if ((prefix = malloc(1)) == NULL)
834 return JB_ERR_MEMORY;
840 if ((data = malloc(1)) == NULL)
844 return JB_ERR_MEMORY;
849 /* Main loop. Loop while we need more data & it's not EOF. */
851 while ( (contflag || is_empty)
852 && (JB_ERR_OK == (rval = simple_read_line(fp, &linebuf, newline))))
860 string_append(&raw,linebuf);
861 if (string_append(&raw,NEWLINE(*newline)))
866 return JB_ERR_MEMORY;
870 /* Line continuation? Trim escape and set flag. */
871 p = linebuf + strlen(linebuf) - 1;
872 contflag = ((*linebuf != '\0') && (*p == '\\'));
878 /* Trim leading spaces if we're at the start of the line */
882 /* Trim leading spaces */
883 while (*linestart && isspace((int)(unsigned char)*linestart))
889 /* Handle comment characters. */
891 while ((p = strchr(p, '#')) != NULL)
893 /* Found a comment char.. */
894 if ((p != linebuf) && (*(p-1) == '\\'))
896 /* ..and it's escaped, left-shift the line over the escape. */
898 while ((*q = *(q + 1)) != '\0')
902 /* Now scan from just after the "#". */
906 /* Real comment. Save it... */
909 /* Special case: Line only contains a comment, so all the
910 * previous whitespace is considered part of the comment.
911 * Undo the whitespace skipping, if any.
918 string_append(&prefix,p);
919 if (string_append(&prefix, NEWLINE(*newline)))
924 return JB_ERR_MEMORY;
928 /* ... and chop off the rest of the line */
931 } /* END while (there's a # character) */
933 /* Write to the buffer */
939 if (string_append(&data, linestart))
944 return JB_ERR_MEMORY;
950 } /* END while(we need more data) */
952 /* Handle simple_read_line() errors - ignore EOF */
953 if ((rval != JB_ERR_OK) && (rval != JB_ERR_FILE))
961 if (raw ? (*raw == '\0') : is_empty)
963 /* EOF and no data there. (Definition of "data" depends on whether
964 * the caller cares about "raw" or just "data").
975 /* Got at least some data */
977 /* Remove trailing whitespace */
990 *prefix_out = prefix;
1009 /*********************************************************************
1011 * Function : read_config_line
1013 * Description : Read a single non-empty line from a file and return
1014 * it. Trims comments, leading and trailing whitespace
1015 * and respects escaping of newline and comment char.
1018 * 1 : buf = Buffer to use.
1019 * 2 : buflen = Size of buffer in bytes.
1020 * 3 : fp = File to read from
1021 * 4 : linenum = linenumber in file
1023 * Returns : NULL on EOF or error
1024 * Otherwise, returns buf.
1026 *********************************************************************/
1027 char *read_config_line(char *buf, size_t buflen, FILE *fp, unsigned long *linenum)
1031 err = edit_read_line(fp, NULL, NULL, &buf2, NULL, linenum);
1034 if (err == JB_ERR_MEMORY)
1036 log_error(LOG_LEVEL_FATAL, "Out of memory loading a config file");
1043 assert(strlen(buf2) + 1U < buflen);
1044 strncpy(buf, buf2, buflen - 1);
1046 buf[buflen - 1] = '\0';
1052 #ifdef FEATURE_TRUST
1053 /*********************************************************************
1055 * Function : unload_trustfile
1057 * Description : Unloads a trustfile.
1060 * 1 : f = the data structure associated with the trustfile.
1064 *********************************************************************/
1065 static void unload_trustfile(void *f)
1067 struct block_spec *cur = (struct block_spec *)f;
1068 struct block_spec *next;
1074 free_url_spec(cur->url);
1083 #ifdef FEATURE_GRACEFUL_TERMINATION
1084 /*********************************************************************
1086 * Function : unload_current_trust_file
1088 * Description : Unloads current trust file - reset to state at
1089 * beginning of program.
1095 *********************************************************************/
1096 void unload_current_trust_file(void)
1098 if (current_trustfile)
1100 current_trustfile->unloader = unload_trustfile;
1101 current_trustfile = NULL;
1104 #endif /* FEATURE_GRACEFUL_TERMINATION */
1107 /*********************************************************************
1109 * Function : load_trustfile
1111 * Description : Read and parse a trustfile and add to files list.
1114 * 1 : csp = Current client state (buffers, headers, etc...)
1116 * Returns : 0 => Ok, everything else is an error.
1118 *********************************************************************/
1119 int load_trustfile(struct client_state *csp)
1123 struct block_spec *b, *bl;
1124 struct url_spec **tl;
1126 char buf[BUFFER_SIZE], *p, *q;
1127 int reject, trusted;
1128 struct file_list *fs;
1129 unsigned long linenum = 0;
1130 int trusted_referrers = 0;
1132 if (!check_file_changed(current_trustfile, csp->config->trustfile, &fs))
1134 /* No need to load */
1137 csp->tlist = current_trustfile;
1143 goto load_trustfile_error;
1146 fs->f = bl = (struct block_spec *)zalloc(sizeof(*bl));
1149 goto load_trustfile_error;
1152 if ((fp = fopen(csp->config->trustfile, "r")) == NULL)
1154 goto load_trustfile_error;
1157 tl = csp->config->trust_list;
1159 while (read_config_line(buf, sizeof(buf), fp, &linenum) != NULL)
1175 while ((*p++ = *q++) != '\0')
1181 /* skip blank lines */
1187 /* allocate a new node */
1188 if ((b = zalloc(sizeof(*b))) == NULL)
1191 goto load_trustfile_error;
1194 /* add it to the list */
1200 /* Save the URL pattern */
1201 if (create_url_spec(b->url, buf))
1204 goto load_trustfile_error;
1208 * save a pointer to URL's spec in the list of trusted URL's, too
1212 if(++trusted_referrers < MAX_TRUSTED_REFERRERS)
1219 if(trusted_referrers >= MAX_TRUSTED_REFERRERS)
1222 * FIXME: ... after Privoxy 3.0.4 is out.
1224 log_error(LOG_LEVEL_ERROR, "Too many trusted referrers. Current limit is %d, you are using %d.\n"
1225 " Additional trusted referrers are treated like ordinary trusted URLs.\n"
1226 " (You can increase this limit by changing MAX_TRUSTED_REFERRERS in project.h and recompiling).",
1227 MAX_TRUSTED_REFERRERS, trusted_referrers);
1234 /* the old one is now obsolete */
1235 if (current_trustfile)
1237 current_trustfile->unloader = unload_trustfile;
1240 fs->next = files->next;
1242 current_trustfile = fs;
1251 load_trustfile_error:
1252 log_error(LOG_LEVEL_FATAL, "can't load trustfile '%s': %E",
1253 csp->config->trustfile);
1257 #endif /* def FEATURE_TRUST */
1260 /*********************************************************************
1262 * Function : unload_re_filterfile
1264 * Description : Unload the re_filter list by freeing all chained
1265 * re_filterfile specs and their data.
1268 * 1 : f = the data structure associated with the filterfile.
1272 *********************************************************************/
1273 static void unload_re_filterfile(void *f)
1275 struct re_filterfile_spec *a, *b = (struct re_filterfile_spec *)f;
1281 destroy_list(b->patterns);
1282 pcrs_free_joblist(b->joblist);
1284 freez(b->description);
1294 #ifdef FEATURE_GRACEFUL_TERMINATION
1295 /*********************************************************************
1297 * Function : unload_current_re_filterfile
1299 * Description : Unloads current re_filter file - reset to state at
1300 * beginning of program.
1306 *********************************************************************/
1307 void unload_current_re_filterfile(void)
1311 for (i = 0; i < MAX_AF_FILES; i++)
1313 if (current_re_filterfile[i])
1315 current_re_filterfile[i]->unloader = unload_re_filterfile;
1316 current_re_filterfile[i] = NULL;
1323 /*********************************************************************
1325 * Function : load_re_filterfile
1327 * Description : Load the re_filterfile.
1328 * Generate a chained list of re_filterfile_spec's from
1329 * the "FILTER: " blocks, compiling all their substitutions
1330 * into chained lists of pcrs_job structs.
1333 * 1 : csp = Current client state (buffers, headers, etc...)
1335 * Returns : 0 => Ok, everything else is an error.
1337 *********************************************************************/
1338 int load_re_filterfile(struct client_state *csp)
1343 for (i = 0; i < MAX_AF_FILES; i++)
1345 if (csp->config->re_filterfile[i])
1347 result = load_one_re_filterfile(csp, i);
1353 else if (current_re_filterfile[i])
1355 current_re_filterfile[i]->unloader = unload_re_filterfile;
1356 current_re_filterfile[i] = NULL;
1363 /*********************************************************************
1365 * Function : load_one_re_filterfile
1367 * Description : Load a re_filterfile.
1368 * Generate a chained list of re_filterfile_spec's from
1369 * the "FILTER: " blocks, compiling all their substitutions
1370 * into chained lists of pcrs_job structs.
1373 * 1 : csp = Current client state (buffers, headers, etc...)
1375 * Returns : 0 => Ok, everything else is an error.
1377 *********************************************************************/
1378 int load_one_re_filterfile(struct client_state *csp, int fileid)
1382 struct re_filterfile_spec *new_bl, *bl = NULL;
1383 struct file_list *fs;
1385 char buf[BUFFER_SIZE];
1387 unsigned long linenum = 0;
1388 pcrs_job *dummy, *lastjob = NULL;
1391 * No need to reload if unchanged
1393 if (!check_file_changed(current_re_filterfile[fileid], csp->config->re_filterfile[fileid], &fs))
1397 csp->rlist[fileid] = current_re_filterfile[fileid];
1403 goto load_re_filterfile_error;
1407 * Open the file or fail
1409 if ((fp = fopen(csp->config->re_filterfile[fileid], "r")) == NULL)
1411 goto load_re_filterfile_error;
1417 while (read_config_line(buf, sizeof(buf), fp, &linenum) != NULL)
1420 * If this is the head of a new filter block, make it a
1421 * re_filterfile spec of its own and chain it to the list:
1423 if (strncmp(buf, "FILTER:", 7) == 0)
1425 new_bl = (struct re_filterfile_spec *)zalloc(sizeof(*bl));
1428 goto load_re_filterfile_error;
1431 new_bl->name = chomp(buf + 7);
1434 * If a filter description is available,
1435 * encode it to HTML and save it.
1437 if (NULL != (new_bl->description = strpbrk(new_bl->name, " \t")))
1439 *new_bl->description++ = '\0';
1440 new_bl->description = html_encode(chomp(new_bl->description));
1441 if (NULL == new_bl->description)
1443 new_bl->description = strdup("Out of memory while encoding this filter's description to HTML");
1448 new_bl->description = strdup("No description available for this filter");
1451 new_bl->name = strdup(chomp(new_bl->name));
1454 * If this is the first filter block, chain it
1455 * to the file_list rather than its (nonexistant)
1468 log_error(LOG_LEVEL_RE_FILTER, "Reading in filter \"%s\" (\"%s\")", bl->name, bl->description);
1474 * Else, save the expression, make it a pcrs_job
1475 * and chain it into the current filter's joblist
1479 enlist(bl->patterns, buf);
1481 if ((dummy = pcrs_compile_command(buf, &error)) == NULL)
1483 log_error(LOG_LEVEL_ERROR,
1484 "Adding re_filter job %s to filter %s failed with error %d.", buf, bl->name, error);
1489 if (bl->joblist == NULL)
1491 bl->joblist = dummy;
1495 lastjob->next = dummy;
1498 log_error(LOG_LEVEL_RE_FILTER, "Adding re_filter job %s to filter %s succeeded.", buf, bl->name);
1503 log_error(LOG_LEVEL_ERROR, "Ignoring job %s outside filter block in %s, line %d",
1504 buf, csp->config->re_filterfile[fileid], linenum);
1511 * Schedule the now-obsolete old data for unloading
1513 if ( NULL != current_re_filterfile[fileid] )
1515 current_re_filterfile[fileid]->unloader = unload_re_filterfile;
1519 * Chain this file into the global list of loaded files
1521 fs->next = files->next;
1523 current_re_filterfile[fileid] = fs;
1527 csp->rlist[fileid] = fs;
1532 load_re_filterfile_error:
1533 log_error(LOG_LEVEL_FATAL, "can't load re_filterfile '%s': %E",
1534 csp->config->re_filterfile[fileid]);
1540 /*********************************************************************
1542 * Function : add_loader
1544 * Description : Called from `load_config'. Called once for each input
1545 * file found in config.
1548 * 1 : loader = pointer to a function that can parse and load
1549 * the appropriate config file.
1550 * 2 : config = The configuration_spec to add the loader to.
1554 *********************************************************************/
1555 void add_loader(int (*loader)(struct client_state *),
1556 struct configuration_spec * config)
1560 for (i=0; i < NLOADERS; i++)
1562 if (config->loaders[i] == NULL)
1564 config->loaders[i] = loader;
1572 /*********************************************************************
1574 * Function : run_loader
1576 * Description : Called from `load_config' and `listen_loop'. This
1577 * function keeps the "csp" current with any file mods
1578 * since the last loop. If a file is unchanged, the
1579 * loader functions do NOT reload the file.
1582 * 1 : csp = Current client state (buffers, headers, etc...)
1583 * Must be non-null. Reads: "csp->config"
1584 * Writes: various data members.
1586 * Returns : 0 => Ok, everything else is an error.
1588 *********************************************************************/
1589 int run_loader(struct client_state *csp)
1594 for (i=0; i < NLOADERS; i++)
1596 if (csp->config->loaders[i] == NULL)
1600 ret |= (csp->config->loaders[i])(csp);