1 const char loaders_rcs[] = "$Id: loaders.c,v 1.56 2006/09/07 10:40:30 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.56 2006/09/07 10:40:30 fabiankeil
39 * Turns out trusted referrers above our arbitrary
40 * limit are downgraded too ordinary trusted URLs.
41 * Adjusted error message.
43 * Revision 1.55 2006/09/07 10:25:39 fabiankeil
46 * Revision 1.54 2006/09/07 10:22:20 fabiankeil
47 * If too many trusted referrers are used,
48 * print only one error message instead of logging
49 * every single trusted referrer above the arbitrary
52 * Revision 1.53 2006/08/31 16:25:06 fabiankeil
53 * Work around a buffer overflow that caused Privoxy to
54 * segfault if too many trusted referrers were used. Good
55 * enough for now, but should be replaced with a real
56 * solution after the next release.
58 * Revision 1.52 2006/07/18 14:48:46 david__schmidt
59 * Reorganizing the repository: swapping out what was HEAD (the old 3.1 branch)
60 * with what was really the latest development (the v_3_0_branch branch)
62 * Revision 1.50.2.8 2006/01/30 15:16:25 david__schmidt
63 * Remove a little residual debugging info
65 * Revision 1.50.2.7 2006/01/29 23:10:56 david__schmidt
66 * Multiple filter file support
68 * Revision 1.50.2.6 2003/10/24 10:17:54 oes
69 * Nit: Allowed tabs as separators in filter headings
71 * Revision 1.50.2.5 2003/05/08 15:19:15 oes
72 * sweep: Made loop structure of sweep step mirror that of mark step
74 * Revision 1.50.2.4 2003/05/06 15:57:12 oes
75 * Bugfix: Update last_active pointer in sweep() before
76 * leaving an active client. Closes bugs #724395, #727882
78 * Revision 1.50.2.3 2002/11/20 17:12:30 oes
79 * Ooops, forgot one change.
81 * Revision 1.50.2.2 2002/11/20 14:38:15 oes
82 * Fixed delayed/incomplete freeing of client resources and
83 * simplified loop structure in sweep.
84 * Thanks to Oliver Stoeneberg for the hint.
86 * Revision 1.50.2.1 2002/07/26 15:19:24 oes
87 * - PCRS jobs now chained in order of appearance. Previous
88 * reverse chaining was counter-intuitive.
89 * - Changed loglevel of PCRS job compile errors to
92 * Revision 1.50 2002/04/24 02:12:16 oes
93 * Jon's multiple AF patch: Sweep now takes care of all AFs
95 * Revision 1.49 2002/04/19 16:53:25 jongfoster
96 * Optimize away a function call by using an equivalent macro
98 * Revision 1.48 2002/04/05 00:56:09 gliptak
99 * Correcting typo to clean up on realloc failure
101 * Revision 1.47 2002/03/26 22:29:55 swa
102 * we have a new homepage!
104 * Revision 1.46 2002/03/24 13:25:43 swa
105 * name change related issues
107 * Revision 1.45 2002/03/16 23:54:06 jongfoster
108 * Adding graceful termination feature, to help look for memory leaks.
109 * If you enable this (which, by design, has to be done by hand
110 * editing config.h) and then go to http://i.j.b/die, then the program
111 * will exit cleanly after the *next* request. It should free all the
112 * memory that was used.
114 * Revision 1.44 2002/03/16 21:51:00 jongfoster
117 * Revision 1.43 2002/03/16 20:28:34 oes
118 * Added descriptions to the filters so users will know what they select in the cgi editor
120 * Revision 1.42 2002/03/13 00:27:05 jongfoster
123 * Revision 1.41 2002/03/12 01:42:50 oes
124 * Introduced modular filters
126 * Revision 1.40 2002/03/08 17:46:04 jongfoster
127 * Fixing int/size_t warnings
129 * Revision 1.39 2002/03/07 03:46:17 oes
130 * Fixed compiler warnings
132 * Revision 1.38 2002/03/06 22:54:35 jongfoster
133 * Automated function-comment nitpicking.
135 * Revision 1.37 2002/03/03 15:07:49 oes
136 * Re-enabled automatic config reloading
138 * Revision 1.36 2002/01/22 23:46:18 jongfoster
139 * Moving edit_read_line() and simple_read_line() to loaders.c, and
140 * extending them to support reading MS-DOS, Mac and UNIX style files
143 * Modifying read_config_line() (without changing it's prototype) to
144 * be a trivial wrapper for edit_read_line(). This means that we have
145 * one function to read a line and handle comments, which is common
146 * between the initialization code and the edit interface.
148 * Revision 1.35 2002/01/17 21:03:08 jongfoster
149 * Moving all our URL and URL pattern parsing code to urlmatch.c.
151 * Renaming free_url to free_url_spec, since it frees a struct url_spec.
153 * Revision 1.34 2001/12/30 14:07:32 steudten
154 * - Add signal handling (unix)
155 * - Add SIGHUP handler (unix)
156 * - Add creation of pidfile (unix)
157 * - Add action 'top' in rc file (RH)
158 * - Add entry 'SIGNALS' to manpage
159 * - Add exit message to logfile (unix)
161 * Revision 1.33 2001/11/13 00:16:38 jongfoster
162 * Replacing references to malloc.h with the standard stdlib.h
163 * (See ANSI or K&R 2nd Ed)
165 * Revision 1.32 2001/11/07 00:02:13 steudten
166 * Add line number in error output for lineparsing for
167 * actionsfile and configfile.
168 * Special handling for CLF added.
170 * Revision 1.31 2001/10/26 17:39:01 oes
171 * Removed csp->referrer
172 * Moved ijb_isspace and ijb_tolower to project.h
174 * Revision 1.30 2001/10/25 03:40:48 david__schmidt
175 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
176 * threads to call select() simultaneously. So, it's time to do a real, live,
177 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
178 * (native). Both versions will work, but using __OS2__ offers multi-threading.
180 * Revision 1.29 2001/10/23 21:38:53 jongfoster
181 * Adding error-checking to create_url_spec()
183 * Revision 1.28 2001/10/07 15:40:39 oes
184 * Replaced 6 boolean members of csp with one bitmap (csp->flags)
186 * Revision 1.27 2001/09/22 16:36:59 jongfoster
187 * Removing unused parameter fs from read_config_line()
189 * Revision 1.26 2001/09/22 14:05:22 jongfoster
190 * Bugfix: Multiple escaped "#" characters in a configuration
191 * file are now permitted.
192 * Also removing 3 unused headers.
194 * Revision 1.25 2001/09/13 22:44:03 jongfoster
195 * Adding {} to an if statement
197 * Revision 1.24 2001/07/30 22:08:36 jongfoster
198 * Tidying up #defines:
199 * - All feature #defines are now of the form FEATURE_xxx
200 * - Permanently turned off WIN_GUI_EDIT
201 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
203 * Revision 1.23 2001/07/20 15:51:54 oes
204 * Fixed indentation of prepocessor commands
206 * Revision 1.22 2001/07/20 15:16:17 haroon
207 * - per Guy's suggestion, added a while loop in sweep() to catch not just
208 * the last inactive CSP but all other consecutive inactive CSPs after that
211 * Revision 1.21 2001/07/18 17:26:24 oes
212 * Changed to conform to new pcrs interface
214 * Revision 1.20 2001/07/17 13:07:01 oes
215 * Fixed segv when last line in config files
216 * lacked a terminating (\r)\n
218 * Revision 1.19 2001/07/13 14:01:54 oes
219 * Removed all #ifdef PCRS
221 * Revision 1.18 2001/06/29 21:45:41 oes
222 * Indentation, CRLF->LF, Tab-> Space
224 * Revision 1.17 2001/06/29 13:31:51 oes
227 * Revision 1.16 2001/06/09 10:55:28 jongfoster
228 * Changing BUFSIZ ==> BUFFER_SIZE
230 * Revision 1.15 2001/06/07 23:14:14 jongfoster
231 * Removing ACL and forward file loaders - these
232 * files have been merged into the config file.
233 * Cosmetic: Moving unloader funcs next to their
234 * respective loader funcs
236 * Revision 1.14 2001/06/01 03:27:04 oes
237 * Fixed line continuation problem
239 * Revision 1.13 2001/05/31 21:28:49 jongfoster
240 * Removed all permissionsfile code - it's now called the actions
241 * file, and (almost) all the code is in actions.c
243 * Revision 1.12 2001/05/31 17:32:31 oes
245 * - Enhanced domain part globbing with infix and prefix asterisk
246 * matching and optional unanchored operation
248 * Revision 1.11 2001/05/29 23:25:24 oes
250 * - load_config_line() and load_permissions_file() now use chomp()
252 * Revision 1.10 2001/05/29 09:50:24 jongfoster
253 * Unified blocklist/imagelist/permissionslist.
254 * File format is still under discussion, but the internal changes
257 * Also modified interceptor behaviour:
258 * - We now intercept all URLs beginning with one of the following
259 * prefixes (and *only* these prefixes):
261 * * http://ijbswa.sf.net/config/
262 * * http://ijbswa.sourceforge.net/config/
263 * - New interceptors "home page" - go to http://i.j.b/ to see it.
264 * - Internal changes so that intercepted and fast redirect pages
265 * are not replaced with an image.
266 * - Interceptors now have the option to send a binary page direct
267 * to the client. (i.e. ijb-send-banner uses this)
268 * - Implemented show-url-info interceptor. (Which is why I needed
269 * the above interceptors changes - a typical URL is
270 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
271 * The previous mechanism would not have intercepted that, and
272 * if it had been intercepted then it then it would have replaced
275 * Revision 1.9 2001/05/26 17:12:07 jongfoster
276 * Fatal errors loading configuration files now give better error messages.
278 * Revision 1.8 2001/05/26 00:55:20 jongfoster
279 * Removing duplicated code. load_forwardfile() now uses create_url_spec()
281 * Revision 1.7 2001/05/26 00:28:36 jongfoster
282 * Automatic reloading of config file.
283 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
284 * Most of the global variables have been moved to a new
285 * struct configuration_spec, accessed through csp->config->globalname
286 * Most of the globals remaining are used by the Win32 GUI.
288 * Revision 1.6 2001/05/23 12:27:33 oes
290 * Fixed ugly indentation of my last changes
292 * Revision 1.5 2001/05/23 10:39:05 oes
293 * - Added support for escaping the comment character
294 * in config files by a backslash
295 * - Added support for line continuation in config
297 * - Fixed a buffer overflow bug with long config lines
299 * Revision 1.4 2001/05/22 18:56:28 oes
302 * Revision 1.3 2001/05/20 01:21:20 jongfoster
303 * Version 2.9.4 checkin.
304 * - Merged popupfile and cookiefile, and added control over PCRS
305 * filtering, in new "permissionsfile".
306 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
307 * file error you now get a message box (in the Win32 GUI) rather
308 * than the program exiting with no explanation.
309 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
311 * - Removed tabs from "config"
312 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
313 * - Bumped up version number.
315 * Revision 1.2 2001/05/17 23:01:01 oes
316 * - Cleaned CRLF's from the sources and related files
318 * Revision 1.1.1.1 2001/05/15 13:58:59 oes
319 * Initial import of version 2.9.3 source tree
322 *********************************************************************/
329 #include <sys/types.h>
332 #include <sys/stat.h>
336 #if !defined(_WIN32) && !defined(__OS2__)
346 #include "miscutil.h"
349 #include "urlmatch.h"
352 const char loaders_h_rcs[] = LOADERS_H_VERSION;
355 * Currently active files.
356 * These are also entered in the main linked list of files.
360 static struct file_list *current_trustfile = NULL;
361 #endif /* def FEATURE_TRUST */
363 static int load_one_re_filterfile(struct client_state *csp, int fileid);
365 static struct file_list *current_re_filterfile[MAX_AF_FILES] = {
366 NULL, NULL, NULL, NULL, NULL,
367 NULL, NULL, NULL, NULL, NULL
372 /*********************************************************************
376 * Description : Basically a mark and sweep garbage collector, it is run
377 * (by the parent thread) every once in a while to reclaim memory.
379 * It uses a mark and sweep strategy:
380 * 1) mark all files as inactive
382 * 2) check with each client:
383 * if it is active, mark its files as active
384 * if it is inactive, free its resources
386 * 3) free the resources of all of the files that
387 * are still marked as inactive (and are obsolete).
389 * N.B. files that are not obsolete don't have an unloader defined.
395 *********************************************************************/
398 struct file_list *fl, *nfl;
399 struct client_state *csp, *last_active;
402 /* clear all of the file's active flags */
403 for ( fl = files->next; NULL != fl; fl = fl->next )
408 last_active = clients;
413 if (csp->flags & CSP_FLAG_ACTIVE)
415 /* Mark this client's files as active */
418 * Always have a configuration file.
419 * (Also note the slightly non-standard extra
422 csp->config->config_file_list->active = 1;
427 for (i = 0; i < MAX_AF_FILES; i++)
429 if (csp->actions_list[i])
431 csp->actions_list[i]->active = 1;
438 for (i = 0; i < MAX_AF_FILES; i++)
442 csp->rlist[i]->active = 1;
452 csp->tlist->active = 1;
454 #endif /* def FEATURE_TRUST */
462 * This client is not active. Free its resources.
465 last_active->next = csp->next;
467 freez(csp->ip_addr_str);
468 freez(csp->my_ip_addr_str);
469 freez(csp->my_hostname);
470 freez(csp->x_forwarded);
471 freez(csp->iob->buf);
473 free_http_request(csp->http);
475 destroy_list(csp->headers);
476 destroy_list(csp->cookie_list);
478 free_current_action(csp->action);
480 #ifdef FEATURE_STATISTICS
482 if (csp->flags & CSP_FLAG_REJECTED)
486 #endif /* def FEATURE_STATISTICS */
490 csp = last_active->next;
499 if ( ( 0 == fl->active ) && ( NULL != fl->unloader ) )
501 nfl->next = fl->next;
503 (fl->unloader)(fl->f);
520 /*********************************************************************
522 * Function : check_file_changed
524 * Description : Helper function to check if a file needs reloading.
525 * If "current" is still current, return it. Otherwise
526 * allocates a new (zeroed) "struct file_list", fills
527 * in the disk file name and timestamp, and returns it.
530 * 1 : current = The file_list currently being used - will
531 * be checked to see if it is out of date.
532 * May be NULL (which is treated as out of
534 * 2 : filename = Name of file to check.
535 * 3 : newfl = New file list. [Output only]
536 * This will be set to NULL, OR a struct
537 * file_list newly allocated on the
538 * heap, with the filename and lastmodified
539 * fields filled, and all others zeroed.
541 * Returns : If file unchanged: 0 (and sets newfl == NULL)
542 * If file changed: 1 and sets newfl != NULL
543 * On error: 1 and sets newfl == NULL
545 *********************************************************************/
546 int check_file_changed(const struct file_list * current,
547 const char * filename,
548 struct file_list ** newfl)
550 struct file_list *fs;
551 struct stat statbuf[1];
555 if (stat(filename, statbuf) < 0)
557 /* Error, probably file not found. */
562 && (current->lastmodified == statbuf->st_mtime)
563 && (0 == strcmp(current->filename, filename)))
568 fs = (struct file_list *)zalloc(sizeof(struct file_list));
571 /* Out of memory error */
576 fs->filename = strdup(filename);
577 fs->lastmodified = statbuf->st_mtime;
579 if (fs->filename == NULL)
581 /* Out of memory error */
590 /*********************************************************************
592 * Function : simple_read_line
594 * Description : Read a single line from a file and return it.
595 * This is basically a version of fgets() that malloc()s
596 * it's own line buffer. Note that the buffer will
597 * always be a multiple of BUFFER_SIZE bytes long.
598 * Therefore if you are going to keep the string for
599 * an extended period of time, you should probably
600 * strdup() it and free() the original, to save memory.
604 * 1 : dest = destination for newly malloc'd pointer to
605 * line data. Will be set to NULL on error.
606 * 2 : fp = File to read from
607 * 3 : newline = Standard for newlines in the file.
608 * Will be unchanged if it's value on input is not
610 * On output, may be changed from NEWLINE_UNKNOWN to
611 * actual convention in file.
613 * Returns : JB_ERR_OK on success
614 * JB_ERR_MEMORY on out-of-memory
615 * JB_ERR_FILE on EOF.
617 *********************************************************************/
618 jb_err simple_read_line(FILE *fp, char **dest, int *newline)
621 size_t buflen = BUFFER_SIZE;
625 int realnewline = NEWLINE_UNKNOWN;
627 if (NULL == (buf = malloc(buflen)))
629 return JB_ERR_MEMORY;
635 * Character codes. If you have a wierd compiler and the following are
636 * incorrect, you also need to fix NEWLINE() in loaders.h
638 #define CHAR_CR '\r' /* ASCII 13 */
639 #define CHAR_LF '\n' /* ASCII 10 */
659 else if (ch == CHAR_CR)
664 if (*newline == NEWLINE_UNKNOWN)
666 *newline = NEWLINE_DOS;
675 if (*newline == NEWLINE_UNKNOWN)
677 *newline = NEWLINE_MAC;
682 if (*newline == NEWLINE_UNKNOWN)
684 *newline = realnewline;
688 else if (ch == CHAR_LF)
692 if (*newline == NEWLINE_UNKNOWN)
694 *newline = NEWLINE_UNIX;
709 buflen += BUFFER_SIZE;
710 if (NULL == (p = realloc(buf, buflen)))
713 return JB_ERR_MEMORY;
722 /*********************************************************************
724 * Function : edit_read_line
726 * Description : Read a single non-empty line from a file and return
727 * it. Trims comments, leading and trailing whitespace
728 * and respects escaping of newline and comment char.
729 * Provides the line in 2 alternative forms: raw and
731 * - raw is the raw data read from the file. If the
732 * line is not modified, then this should be written
734 * - prefix is any comments and blank lines that were
735 * read from the file. If the line is modified, then
736 * this should be written out to the file followed
737 * by the modified data. (If this string is non-empty
738 * then it will have a newline at the end).
739 * - data is the actual data that will be parsed
740 * further by appropriate routines.
741 * On EOF, the 3 strings will all be set to NULL and
742 * 0 will be returned.
745 * 1 : fp = File to read from
746 * 2 : raw_out = destination for newly malloc'd pointer to
747 * raw line data. May be NULL if you don't want it.
748 * 3 : prefix_out = destination for newly malloc'd pointer to
749 * comments. May be NULL if you don't want it.
750 * 4 : data_out = destination for newly malloc'd pointer to
751 * line data with comments and leading/trailing spaces
752 * removed, and line continuation performed. May be
753 * NULL if you don't want it.
754 * 5 : newline = Standard for newlines in the file.
755 * On input, set to value to use or NEWLINE_UNKNOWN.
756 * On output, may be changed from NEWLINE_UNKNOWN to
757 * actual convention in file. May be NULL if you
759 * 6 : line_number = Line number in file. In "lines" as
760 * reported by a text editor, not lines containing data.
762 * Returns : JB_ERR_OK on success
763 * JB_ERR_MEMORY on out-of-memory
764 * JB_ERR_FILE on EOF.
766 *********************************************************************/
767 jb_err edit_read_line(FILE *fp,
772 unsigned long *line_number)
774 char *p; /* Temporary pointer */
775 char *linebuf; /* Line read from file */
776 char *linestart; /* Start of linebuf, usually first non-whitespace char */
777 int contflag = 0; /* Nonzero for line continuation - i.e. line ends '\' */
778 int is_empty = 1; /* Flag if not got any data yet */
779 char *raw = NULL; /* String to be stored in raw_out */
780 char *prefix = NULL; /* String to be stored in prefix_out */
781 char *data = NULL; /* String to be stored in data_out */
782 int scrapnewline; /* Used for (*newline) if newline==NULL */
783 jb_err rval = JB_ERR_OK;
786 assert(raw_out || data_out);
787 assert(newline == NULL
788 || *newline == NEWLINE_UNKNOWN
789 || *newline == NEWLINE_UNIX
790 || *newline == NEWLINE_DOS
791 || *newline == NEWLINE_MAC);
795 scrapnewline = NEWLINE_UNKNOWN;
796 newline = &scrapnewline;
799 /* Set output parameters to NULL */
813 /* Set string variables to new, empty strings. */
817 if ((raw = malloc(1)) == NULL)
819 return JB_ERR_MEMORY;
825 if ((prefix = malloc(1)) == NULL)
828 return JB_ERR_MEMORY;
834 if ((data = malloc(1)) == NULL)
838 return JB_ERR_MEMORY;
843 /* Main loop. Loop while we need more data & it's not EOF. */
845 while ( (contflag || is_empty)
846 && (JB_ERR_OK == (rval = simple_read_line(fp, &linebuf, newline))))
854 string_append(&raw,linebuf);
855 if (string_append(&raw,NEWLINE(*newline)))
860 return JB_ERR_MEMORY;
864 /* Line continuation? Trim escape and set flag. */
865 p = linebuf + strlen(linebuf) - 1;
866 contflag = ((*linebuf != '\0') && (*p == '\\'));
872 /* Trim leading spaces if we're at the start of the line */
876 /* Trim leading spaces */
877 while (*linestart && isspace((int)(unsigned char)*linestart))
883 /* Handle comment characters. */
885 while ((p = strchr(p, '#')) != NULL)
887 /* Found a comment char.. */
888 if ((p != linebuf) && (*(p-1) == '\\'))
890 /* ..and it's escaped, left-shift the line over the escape. */
892 while ((*q = *(q + 1)) != '\0')
896 /* Now scan from just after the "#". */
900 /* Real comment. Save it... */
903 /* Special case: Line only contains a comment, so all the
904 * previous whitespace is considered part of the comment.
905 * Undo the whitespace skipping, if any.
912 string_append(&prefix,p);
913 if (string_append(&prefix, NEWLINE(*newline)))
918 return JB_ERR_MEMORY;
922 /* ... and chop off the rest of the line */
925 } /* END while (there's a # character) */
927 /* Write to the buffer */
933 if (string_append(&data, linestart))
938 return JB_ERR_MEMORY;
944 } /* END while(we need more data) */
946 /* Handle simple_read_line() errors - ignore EOF */
947 if ((rval != JB_ERR_OK) && (rval != JB_ERR_FILE))
955 if (raw ? (*raw == '\0') : is_empty)
957 /* EOF and no data there. (Definition of "data" depends on whether
958 * the caller cares about "raw" or just "data").
969 /* Got at least some data */
971 /* Remove trailing whitespace */
984 *prefix_out = prefix;
1003 /*********************************************************************
1005 * Function : read_config_line
1007 * Description : Read a single non-empty line from a file and return
1008 * it. Trims comments, leading and trailing whitespace
1009 * and respects escaping of newline and comment char.
1012 * 1 : buf = Buffer to use.
1013 * 2 : buflen = Size of buffer in bytes.
1014 * 3 : fp = File to read from
1015 * 4 : linenum = linenumber in file
1017 * Returns : NULL on EOF or error
1018 * Otherwise, returns buf.
1020 *********************************************************************/
1021 char *read_config_line(char *buf, size_t buflen, FILE *fp, unsigned long *linenum)
1025 err = edit_read_line(fp, NULL, NULL, &buf2, NULL, linenum);
1028 if (err == JB_ERR_MEMORY)
1030 log_error(LOG_LEVEL_FATAL, "Out of memory loading a config file");
1037 assert(strlen(buf2) + 1U < buflen);
1038 strncpy(buf, buf2, buflen - 1);
1040 buf[buflen - 1] = '\0';
1046 #ifdef FEATURE_TRUST
1047 /*********************************************************************
1049 * Function : unload_trustfile
1051 * Description : Unloads a trustfile.
1054 * 1 : f = the data structure associated with the trustfile.
1058 *********************************************************************/
1059 static void unload_trustfile(void *f)
1061 struct block_spec *cur = (struct block_spec *)f;
1062 struct block_spec *next;
1068 free_url_spec(cur->url);
1077 #ifdef FEATURE_GRACEFUL_TERMINATION
1078 /*********************************************************************
1080 * Function : unload_current_trust_file
1082 * Description : Unloads current trust file - reset to state at
1083 * beginning of program.
1089 *********************************************************************/
1090 void unload_current_trust_file(void)
1092 if (current_trustfile)
1094 current_trustfile->unloader = unload_trustfile;
1095 current_trustfile = NULL;
1098 #endif /* FEATURE_GRACEFUL_TERMINATION */
1101 /*********************************************************************
1103 * Function : load_trustfile
1105 * Description : Read and parse a trustfile and add to files list.
1108 * 1 : csp = Current client state (buffers, headers, etc...)
1110 * Returns : 0 => Ok, everything else is an error.
1112 *********************************************************************/
1113 int load_trustfile(struct client_state *csp)
1117 struct block_spec *b, *bl;
1118 struct url_spec **tl;
1120 char buf[BUFFER_SIZE], *p, *q;
1121 int reject, trusted;
1122 struct file_list *fs;
1123 unsigned long linenum = 0;
1124 int trusted_referrers = 0;
1126 if (!check_file_changed(current_trustfile, csp->config->trustfile, &fs))
1128 /* No need to load */
1131 csp->tlist = current_trustfile;
1137 goto load_trustfile_error;
1140 fs->f = bl = (struct block_spec *)zalloc(sizeof(*bl));
1143 goto load_trustfile_error;
1146 if ((fp = fopen(csp->config->trustfile, "r")) == NULL)
1148 goto load_trustfile_error;
1151 tl = csp->config->trust_list;
1153 while (read_config_line(buf, sizeof(buf), fp, &linenum) != NULL)
1169 while ((*p++ = *q++) != '\0')
1175 /* skip blank lines */
1181 /* allocate a new node */
1182 if ((b = zalloc(sizeof(*b))) == NULL)
1185 goto load_trustfile_error;
1188 /* add it to the list */
1194 /* Save the URL pattern */
1195 if (create_url_spec(b->url, buf))
1198 goto load_trustfile_error;
1202 * save a pointer to URL's spec in the list of trusted URL's, too
1206 if(++trusted_referrers < MAX_TRUSTED_REFERRERS)
1213 if(trusted_referrers >= MAX_TRUSTED_REFERRERS)
1216 * FIXME: ... after Privoxy 3.0.4 is out.
1218 log_error(LOG_LEVEL_ERROR, "Too many trusted referrers. Current limit is %d, you are using %d.\n"
1219 " Additional trusted referrers are treated like ordinary trusted URLs.\n"
1220 " (You can increase this limit by changing MAX_TRUSTED_REFERRERS in project.h and recompiling).",
1221 MAX_TRUSTED_REFERRERS, trusted_referrers);
1228 /* the old one is now obsolete */
1229 if (current_trustfile)
1231 current_trustfile->unloader = unload_trustfile;
1234 fs->next = files->next;
1236 current_trustfile = fs;
1245 load_trustfile_error:
1246 log_error(LOG_LEVEL_FATAL, "can't load trustfile '%s': %E",
1247 csp->config->trustfile);
1251 #endif /* def FEATURE_TRUST */
1254 /*********************************************************************
1256 * Function : unload_re_filterfile
1258 * Description : Unload the re_filter list by freeing all chained
1259 * re_filterfile specs and their data.
1262 * 1 : f = the data structure associated with the filterfile.
1266 *********************************************************************/
1267 static void unload_re_filterfile(void *f)
1269 struct re_filterfile_spec *a, *b = (struct re_filterfile_spec *)f;
1275 destroy_list(b->patterns);
1276 pcrs_free_joblist(b->joblist);
1278 freez(b->description);
1288 #ifdef FEATURE_GRACEFUL_TERMINATION
1289 /*********************************************************************
1291 * Function : unload_current_re_filterfile
1293 * Description : Unloads current re_filter file - reset to state at
1294 * beginning of program.
1300 *********************************************************************/
1301 void unload_current_re_filterfile(void)
1305 for (i = 0; i < MAX_AF_FILES; i++)
1307 if (current_re_filterfile[i])
1309 current_re_filterfile[i]->unloader = unload_re_filterfile;
1310 current_re_filterfile[i] = NULL;
1317 /*********************************************************************
1319 * Function : load_re_filterfile
1321 * Description : Load the re_filterfile.
1322 * Generate a chained list of re_filterfile_spec's from
1323 * the "FILTER: " blocks, compiling all their substitutions
1324 * into chained lists of pcrs_job structs.
1327 * 1 : csp = Current client state (buffers, headers, etc...)
1329 * Returns : 0 => Ok, everything else is an error.
1331 *********************************************************************/
1332 int load_re_filterfile(struct client_state *csp)
1337 for (i = 0; i < MAX_AF_FILES; i++)
1339 if (csp->config->re_filterfile[i])
1341 result = load_one_re_filterfile(csp, i);
1347 else if (current_re_filterfile[i])
1349 current_re_filterfile[i]->unloader = unload_re_filterfile;
1350 current_re_filterfile[i] = NULL;
1357 /*********************************************************************
1359 * Function : load_one_re_filterfile
1361 * Description : Load a re_filterfile.
1362 * Generate a chained list of re_filterfile_spec's from
1363 * the "FILTER: " blocks, compiling all their substitutions
1364 * into chained lists of pcrs_job structs.
1367 * 1 : csp = Current client state (buffers, headers, etc...)
1369 * Returns : 0 => Ok, everything else is an error.
1371 *********************************************************************/
1372 int load_one_re_filterfile(struct client_state *csp, int fileid)
1376 struct re_filterfile_spec *new_bl, *bl = NULL;
1377 struct file_list *fs;
1379 char buf[BUFFER_SIZE];
1381 unsigned long linenum = 0;
1382 pcrs_job *dummy, *lastjob = NULL;
1385 * No need to reload if unchanged
1387 if (!check_file_changed(current_re_filterfile[fileid], csp->config->re_filterfile[fileid], &fs))
1391 csp->rlist[fileid] = current_re_filterfile[fileid];
1397 goto load_re_filterfile_error;
1401 * Open the file or fail
1403 if ((fp = fopen(csp->config->re_filterfile[fileid], "r")) == NULL)
1405 goto load_re_filterfile_error;
1411 while (read_config_line(buf, sizeof(buf), fp, &linenum) != NULL)
1414 * If this is the head of a new filter block, make it a
1415 * re_filterfile spec of its own and chain it to the list:
1417 if (strncmp(buf, "FILTER:", 7) == 0)
1419 new_bl = (struct re_filterfile_spec *)zalloc(sizeof(*bl));
1422 goto load_re_filterfile_error;
1425 new_bl->name = chomp(buf + 7);
1428 * If a filter description is available,
1429 * encode it to HTML and save it.
1431 if (NULL != (new_bl->description = strpbrk(new_bl->name, " \t")))
1433 *new_bl->description++ = '\0';
1434 new_bl->description = html_encode(chomp(new_bl->description));
1435 if (NULL == new_bl->description)
1437 new_bl->description = strdup("Out of memory while encoding this filter's description to HTML");
1442 new_bl->description = strdup("No description available for this filter");
1445 new_bl->name = strdup(chomp(new_bl->name));
1448 * If this is the first filter block, chain it
1449 * to the file_list rather than its (nonexistant)
1462 log_error(LOG_LEVEL_RE_FILTER, "Reading in filter \"%s\" (\"%s\")", bl->name, bl->description);
1468 * Else, save the expression, make it a pcrs_job
1469 * and chain it into the current filter's joblist
1473 enlist(bl->patterns, buf);
1475 if ((dummy = pcrs_compile_command(buf, &error)) == NULL)
1477 log_error(LOG_LEVEL_ERROR,
1478 "Adding re_filter job %s to filter %s failed with error %d.", buf, bl->name, error);
1483 if (bl->joblist == NULL)
1485 bl->joblist = dummy;
1489 lastjob->next = dummy;
1492 log_error(LOG_LEVEL_RE_FILTER, "Adding re_filter job %s to filter %s succeeded.", buf, bl->name);
1497 log_error(LOG_LEVEL_ERROR, "Ignoring job %s outside filter block in %s, line %d",
1498 buf, csp->config->re_filterfile[fileid], linenum);
1505 * Schedule the now-obsolete old data for unloading
1507 if ( NULL != current_re_filterfile[fileid] )
1509 current_re_filterfile[fileid]->unloader = unload_re_filterfile;
1513 * Chain this file into the global list of loaded files
1515 fs->next = files->next;
1517 current_re_filterfile[fileid] = fs;
1521 csp->rlist[fileid] = fs;
1526 load_re_filterfile_error:
1527 log_error(LOG_LEVEL_FATAL, "can't load re_filterfile '%s': %E",
1528 csp->config->re_filterfile[fileid]);
1534 /*********************************************************************
1536 * Function : add_loader
1538 * Description : Called from `load_config'. Called once for each input
1539 * file found in config.
1542 * 1 : loader = pointer to a function that can parse and load
1543 * the appropriate config file.
1544 * 2 : config = The configuration_spec to add the loader to.
1548 *********************************************************************/
1549 void add_loader(int (*loader)(struct client_state *),
1550 struct configuration_spec * config)
1554 for (i=0; i < NLOADERS; i++)
1556 if (config->loaders[i] == NULL)
1558 config->loaders[i] = loader;
1566 /*********************************************************************
1568 * Function : run_loader
1570 * Description : Called from `load_config' and `listen_loop'. This
1571 * function keeps the "csp" current with any file mods
1572 * since the last loop. If a file is unchanged, the
1573 * loader functions do NOT reload the file.
1576 * 1 : csp = Current client state (buffers, headers, etc...)
1577 * Must be non-null. Reads: "csp->config"
1578 * Writes: various data members.
1580 * Returns : 0 => Ok, everything else is an error.
1582 *********************************************************************/
1583 int run_loader(struct client_state *csp)
1588 for (i=0; i < NLOADERS; i++)
1590 if (csp->config->loaders[i] == NULL)
1594 ret |= (csp->config->loaders[i])(csp);