1 const char loaders_rcs[] = "$Id: loaders.c,v 1.58 2006/12/31 14:25:20 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.58 2006/12/31 14:25:20 fabiankeil
39 * Fix gcc43 compiler warnings.
41 * Revision 1.57 2006/12/21 12:22:22 fabiankeil
42 * html_encode filter descriptions.
44 * Have "Ignoring job ..." error messages
45 * print the filter file name correctly.
47 * Revision 1.56 2006/09/07 10:40:30 fabiankeil
48 * Turns out trusted referrers above our arbitrary
49 * limit are downgraded too ordinary trusted URLs.
50 * Adjusted error message.
52 * Revision 1.55 2006/09/07 10:25:39 fabiankeil
55 * Revision 1.54 2006/09/07 10:22:20 fabiankeil
56 * If too many trusted referrers are used,
57 * print only one error message instead of logging
58 * every single trusted referrer above the arbitrary
61 * Revision 1.53 2006/08/31 16:25:06 fabiankeil
62 * Work around a buffer overflow that caused Privoxy to
63 * segfault if too many trusted referrers were used. Good
64 * enough for now, but should be replaced with a real
65 * solution after the next release.
67 * Revision 1.52 2006/07/18 14:48:46 david__schmidt
68 * Reorganizing the repository: swapping out what was HEAD (the old 3.1 branch)
69 * with what was really the latest development (the v_3_0_branch branch)
71 * Revision 1.50.2.8 2006/01/30 15:16:25 david__schmidt
72 * Remove a little residual debugging info
74 * Revision 1.50.2.7 2006/01/29 23:10:56 david__schmidt
75 * Multiple filter file support
77 * Revision 1.50.2.6 2003/10/24 10:17:54 oes
78 * Nit: Allowed tabs as separators in filter headings
80 * Revision 1.50.2.5 2003/05/08 15:19:15 oes
81 * sweep: Made loop structure of sweep step mirror that of mark step
83 * Revision 1.50.2.4 2003/05/06 15:57:12 oes
84 * Bugfix: Update last_active pointer in sweep() before
85 * leaving an active client. Closes bugs #724395, #727882
87 * Revision 1.50.2.3 2002/11/20 17:12:30 oes
88 * Ooops, forgot one change.
90 * Revision 1.50.2.2 2002/11/20 14:38:15 oes
91 * Fixed delayed/incomplete freeing of client resources and
92 * simplified loop structure in sweep.
93 * Thanks to Oliver Stoeneberg for the hint.
95 * Revision 1.50.2.1 2002/07/26 15:19:24 oes
96 * - PCRS jobs now chained in order of appearance. Previous
97 * reverse chaining was counter-intuitive.
98 * - Changed loglevel of PCRS job compile errors to
101 * Revision 1.50 2002/04/24 02:12:16 oes
102 * Jon's multiple AF patch: Sweep now takes care of all AFs
104 * Revision 1.49 2002/04/19 16:53:25 jongfoster
105 * Optimize away a function call by using an equivalent macro
107 * Revision 1.48 2002/04/05 00:56:09 gliptak
108 * Correcting typo to clean up on realloc failure
110 * Revision 1.47 2002/03/26 22:29:55 swa
111 * we have a new homepage!
113 * Revision 1.46 2002/03/24 13:25:43 swa
114 * name change related issues
116 * Revision 1.45 2002/03/16 23:54:06 jongfoster
117 * Adding graceful termination feature, to help look for memory leaks.
118 * If you enable this (which, by design, has to be done by hand
119 * editing config.h) and then go to http://i.j.b/die, then the program
120 * will exit cleanly after the *next* request. It should free all the
121 * memory that was used.
123 * Revision 1.44 2002/03/16 21:51:00 jongfoster
126 * Revision 1.43 2002/03/16 20:28:34 oes
127 * Added descriptions to the filters so users will know what they select in the cgi editor
129 * Revision 1.42 2002/03/13 00:27:05 jongfoster
132 * Revision 1.41 2002/03/12 01:42:50 oes
133 * Introduced modular filters
135 * Revision 1.40 2002/03/08 17:46:04 jongfoster
136 * Fixing int/size_t warnings
138 * Revision 1.39 2002/03/07 03:46:17 oes
139 * Fixed compiler warnings
141 * Revision 1.38 2002/03/06 22:54:35 jongfoster
142 * Automated function-comment nitpicking.
144 * Revision 1.37 2002/03/03 15:07:49 oes
145 * Re-enabled automatic config reloading
147 * Revision 1.36 2002/01/22 23:46:18 jongfoster
148 * Moving edit_read_line() and simple_read_line() to loaders.c, and
149 * extending them to support reading MS-DOS, Mac and UNIX style files
152 * Modifying read_config_line() (without changing it's prototype) to
153 * be a trivial wrapper for edit_read_line(). This means that we have
154 * one function to read a line and handle comments, which is common
155 * between the initialization code and the edit interface.
157 * Revision 1.35 2002/01/17 21:03:08 jongfoster
158 * Moving all our URL and URL pattern parsing code to urlmatch.c.
160 * Renaming free_url to free_url_spec, since it frees a struct url_spec.
162 * Revision 1.34 2001/12/30 14:07:32 steudten
163 * - Add signal handling (unix)
164 * - Add SIGHUP handler (unix)
165 * - Add creation of pidfile (unix)
166 * - Add action 'top' in rc file (RH)
167 * - Add entry 'SIGNALS' to manpage
168 * - Add exit message to logfile (unix)
170 * Revision 1.33 2001/11/13 00:16:38 jongfoster
171 * Replacing references to malloc.h with the standard stdlib.h
172 * (See ANSI or K&R 2nd Ed)
174 * Revision 1.32 2001/11/07 00:02:13 steudten
175 * Add line number in error output for lineparsing for
176 * actionsfile and configfile.
177 * Special handling for CLF added.
179 * Revision 1.31 2001/10/26 17:39:01 oes
180 * Removed csp->referrer
181 * Moved ijb_isspace and ijb_tolower to project.h
183 * Revision 1.30 2001/10/25 03:40:48 david__schmidt
184 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
185 * threads to call select() simultaneously. So, it's time to do a real, live,
186 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
187 * (native). Both versions will work, but using __OS2__ offers multi-threading.
189 * Revision 1.29 2001/10/23 21:38:53 jongfoster
190 * Adding error-checking to create_url_spec()
192 * Revision 1.28 2001/10/07 15:40:39 oes
193 * Replaced 6 boolean members of csp with one bitmap (csp->flags)
195 * Revision 1.27 2001/09/22 16:36:59 jongfoster
196 * Removing unused parameter fs from read_config_line()
198 * Revision 1.26 2001/09/22 14:05:22 jongfoster
199 * Bugfix: Multiple escaped "#" characters in a configuration
200 * file are now permitted.
201 * Also removing 3 unused headers.
203 * Revision 1.25 2001/09/13 22:44:03 jongfoster
204 * Adding {} to an if statement
206 * Revision 1.24 2001/07/30 22:08:36 jongfoster
207 * Tidying up #defines:
208 * - All feature #defines are now of the form FEATURE_xxx
209 * - Permanently turned off WIN_GUI_EDIT
210 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
212 * Revision 1.23 2001/07/20 15:51:54 oes
213 * Fixed indentation of prepocessor commands
215 * Revision 1.22 2001/07/20 15:16:17 haroon
216 * - per Guy's suggestion, added a while loop in sweep() to catch not just
217 * the last inactive CSP but all other consecutive inactive CSPs after that
220 * Revision 1.21 2001/07/18 17:26:24 oes
221 * Changed to conform to new pcrs interface
223 * Revision 1.20 2001/07/17 13:07:01 oes
224 * Fixed segv when last line in config files
225 * lacked a terminating (\r)\n
227 * Revision 1.19 2001/07/13 14:01:54 oes
228 * Removed all #ifdef PCRS
230 * Revision 1.18 2001/06/29 21:45:41 oes
231 * Indentation, CRLF->LF, Tab-> Space
233 * Revision 1.17 2001/06/29 13:31:51 oes
236 * Revision 1.16 2001/06/09 10:55:28 jongfoster
237 * Changing BUFSIZ ==> BUFFER_SIZE
239 * Revision 1.15 2001/06/07 23:14:14 jongfoster
240 * Removing ACL and forward file loaders - these
241 * files have been merged into the config file.
242 * Cosmetic: Moving unloader funcs next to their
243 * respective loader funcs
245 * Revision 1.14 2001/06/01 03:27:04 oes
246 * Fixed line continuation problem
248 * Revision 1.13 2001/05/31 21:28:49 jongfoster
249 * Removed all permissionsfile code - it's now called the actions
250 * file, and (almost) all the code is in actions.c
252 * Revision 1.12 2001/05/31 17:32:31 oes
254 * - Enhanced domain part globbing with infix and prefix asterisk
255 * matching and optional unanchored operation
257 * Revision 1.11 2001/05/29 23:25:24 oes
259 * - load_config_line() and load_permissions_file() now use chomp()
261 * Revision 1.10 2001/05/29 09:50:24 jongfoster
262 * Unified blocklist/imagelist/permissionslist.
263 * File format is still under discussion, but the internal changes
266 * Also modified interceptor behaviour:
267 * - We now intercept all URLs beginning with one of the following
268 * prefixes (and *only* these prefixes):
270 * * http://ijbswa.sf.net/config/
271 * * http://ijbswa.sourceforge.net/config/
272 * - New interceptors "home page" - go to http://i.j.b/ to see it.
273 * - Internal changes so that intercepted and fast redirect pages
274 * are not replaced with an image.
275 * - Interceptors now have the option to send a binary page direct
276 * to the client. (i.e. ijb-send-banner uses this)
277 * - Implemented show-url-info interceptor. (Which is why I needed
278 * the above interceptors changes - a typical URL is
279 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
280 * The previous mechanism would not have intercepted that, and
281 * if it had been intercepted then it then it would have replaced
284 * Revision 1.9 2001/05/26 17:12:07 jongfoster
285 * Fatal errors loading configuration files now give better error messages.
287 * Revision 1.8 2001/05/26 00:55:20 jongfoster
288 * Removing duplicated code. load_forwardfile() now uses create_url_spec()
290 * Revision 1.7 2001/05/26 00:28:36 jongfoster
291 * Automatic reloading of config file.
292 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
293 * Most of the global variables have been moved to a new
294 * struct configuration_spec, accessed through csp->config->globalname
295 * Most of the globals remaining are used by the Win32 GUI.
297 * Revision 1.6 2001/05/23 12:27:33 oes
299 * Fixed ugly indentation of my last changes
301 * Revision 1.5 2001/05/23 10:39:05 oes
302 * - Added support for escaping the comment character
303 * in config files by a backslash
304 * - Added support for line continuation in config
306 * - Fixed a buffer overflow bug with long config lines
308 * Revision 1.4 2001/05/22 18:56:28 oes
311 * Revision 1.3 2001/05/20 01:21:20 jongfoster
312 * Version 2.9.4 checkin.
313 * - Merged popupfile and cookiefile, and added control over PCRS
314 * filtering, in new "permissionsfile".
315 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
316 * file error you now get a message box (in the Win32 GUI) rather
317 * than the program exiting with no explanation.
318 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
320 * - Removed tabs from "config"
321 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
322 * - Bumped up version number.
324 * Revision 1.2 2001/05/17 23:01:01 oes
325 * - Cleaned CRLF's from the sources and related files
327 * Revision 1.1.1.1 2001/05/15 13:58:59 oes
328 * Initial import of version 2.9.3 source tree
331 *********************************************************************/
338 #include <sys/types.h>
341 #include <sys/stat.h>
345 #if !defined(_WIN32) && !defined(__OS2__)
355 #include "miscutil.h"
358 #include "urlmatch.h"
361 const char loaders_h_rcs[] = LOADERS_H_VERSION;
364 * Currently active files.
365 * These are also entered in the main linked list of files.
369 static struct file_list *current_trustfile = NULL;
370 #endif /* def FEATURE_TRUST */
372 static int load_one_re_filterfile(struct client_state *csp, int fileid);
374 static struct file_list *current_re_filterfile[MAX_AF_FILES] = {
375 NULL, NULL, NULL, NULL, NULL,
376 NULL, NULL, NULL, NULL, NULL
381 /*********************************************************************
385 * Description : Basically a mark and sweep garbage collector, it is run
386 * (by the parent thread) every once in a while to reclaim memory.
388 * It uses a mark and sweep strategy:
389 * 1) mark all files as inactive
391 * 2) check with each client:
392 * if it is active, mark its files as active
393 * if it is inactive, free its resources
395 * 3) free the resources of all of the files that
396 * are still marked as inactive (and are obsolete).
398 * N.B. files that are not obsolete don't have an unloader defined.
404 *********************************************************************/
407 struct file_list *fl, *nfl;
408 struct client_state *csp, *last_active;
411 /* clear all of the file's active flags */
412 for ( fl = files->next; NULL != fl; fl = fl->next )
417 last_active = clients;
422 if (csp->flags & CSP_FLAG_ACTIVE)
424 /* Mark this client's files as active */
427 * Always have a configuration file.
428 * (Also note the slightly non-standard extra
431 csp->config->config_file_list->active = 1;
436 for (i = 0; i < MAX_AF_FILES; i++)
438 if (csp->actions_list[i])
440 csp->actions_list[i]->active = 1;
447 for (i = 0; i < MAX_AF_FILES; i++)
451 csp->rlist[i]->active = 1;
461 csp->tlist->active = 1;
463 #endif /* def FEATURE_TRUST */
471 * This client is not active. Free its resources.
474 last_active->next = csp->next;
476 freez(csp->ip_addr_str);
477 freez(csp->my_ip_addr_str);
478 freez(csp->my_hostname);
479 freez(csp->x_forwarded);
480 freez(csp->iob->buf);
481 freez(csp->error_message);
483 free_http_request(csp->http);
485 destroy_list(csp->headers);
486 destroy_list(csp->cookie_list);
488 free_current_action(csp->action);
490 #ifdef FEATURE_STATISTICS
492 if (csp->flags & CSP_FLAG_REJECTED)
496 #endif /* def FEATURE_STATISTICS */
500 csp = last_active->next;
509 if ( ( 0 == fl->active ) && ( NULL != fl->unloader ) )
511 nfl->next = fl->next;
513 (fl->unloader)(fl->f);
530 /*********************************************************************
532 * Function : check_file_changed
534 * Description : Helper function to check if a file needs reloading.
535 * If "current" is still current, return it. Otherwise
536 * allocates a new (zeroed) "struct file_list", fills
537 * in the disk file name and timestamp, and returns it.
540 * 1 : current = The file_list currently being used - will
541 * be checked to see if it is out of date.
542 * May be NULL (which is treated as out of
544 * 2 : filename = Name of file to check.
545 * 3 : newfl = New file list. [Output only]
546 * This will be set to NULL, OR a struct
547 * file_list newly allocated on the
548 * heap, with the filename and lastmodified
549 * fields filled, and all others zeroed.
551 * Returns : If file unchanged: 0 (and sets newfl == NULL)
552 * If file changed: 1 and sets newfl != NULL
553 * On error: 1 and sets newfl == NULL
555 *********************************************************************/
556 int check_file_changed(const struct file_list * current,
557 const char * filename,
558 struct file_list ** newfl)
560 struct file_list *fs;
561 struct stat statbuf[1];
565 if (stat(filename, statbuf) < 0)
567 /* Error, probably file not found. */
572 && (current->lastmodified == statbuf->st_mtime)
573 && (0 == strcmp(current->filename, filename)))
578 fs = (struct file_list *)zalloc(sizeof(struct file_list));
581 /* Out of memory error */
586 fs->filename = strdup(filename);
587 fs->lastmodified = statbuf->st_mtime;
589 if (fs->filename == NULL)
591 /* Out of memory error */
600 /*********************************************************************
602 * Function : simple_read_line
604 * Description : Read a single line from a file and return it.
605 * This is basically a version of fgets() that malloc()s
606 * it's own line buffer. Note that the buffer will
607 * always be a multiple of BUFFER_SIZE bytes long.
608 * Therefore if you are going to keep the string for
609 * an extended period of time, you should probably
610 * strdup() it and free() the original, to save memory.
614 * 1 : dest = destination for newly malloc'd pointer to
615 * line data. Will be set to NULL on error.
616 * 2 : fp = File to read from
617 * 3 : newline = Standard for newlines in the file.
618 * Will be unchanged if it's value on input is not
620 * On output, may be changed from NEWLINE_UNKNOWN to
621 * actual convention in file.
623 * Returns : JB_ERR_OK on success
624 * JB_ERR_MEMORY on out-of-memory
625 * JB_ERR_FILE on EOF.
627 *********************************************************************/
628 jb_err simple_read_line(FILE *fp, char **dest, int *newline)
631 size_t buflen = BUFFER_SIZE;
635 int realnewline = NEWLINE_UNKNOWN;
637 if (NULL == (buf = malloc(buflen)))
639 return JB_ERR_MEMORY;
645 * Character codes. If you have a wierd compiler and the following are
646 * incorrect, you also need to fix NEWLINE() in loaders.h
648 #define CHAR_CR '\r' /* ASCII 13 */
649 #define CHAR_LF '\n' /* ASCII 10 */
669 else if (ch == CHAR_CR)
674 if (*newline == NEWLINE_UNKNOWN)
676 *newline = NEWLINE_DOS;
685 if (*newline == NEWLINE_UNKNOWN)
687 *newline = NEWLINE_MAC;
692 if (*newline == NEWLINE_UNKNOWN)
694 *newline = realnewline;
698 else if (ch == CHAR_LF)
702 if (*newline == NEWLINE_UNKNOWN)
704 *newline = NEWLINE_UNIX;
719 buflen += BUFFER_SIZE;
720 if (NULL == (p = realloc(buf, buflen)))
723 return JB_ERR_MEMORY;
732 /*********************************************************************
734 * Function : edit_read_line
736 * Description : Read a single non-empty line from a file and return
737 * it. Trims comments, leading and trailing whitespace
738 * and respects escaping of newline and comment char.
739 * Provides the line in 2 alternative forms: raw and
741 * - raw is the raw data read from the file. If the
742 * line is not modified, then this should be written
744 * - prefix is any comments and blank lines that were
745 * read from the file. If the line is modified, then
746 * this should be written out to the file followed
747 * by the modified data. (If this string is non-empty
748 * then it will have a newline at the end).
749 * - data is the actual data that will be parsed
750 * further by appropriate routines.
751 * On EOF, the 3 strings will all be set to NULL and
752 * 0 will be returned.
755 * 1 : fp = File to read from
756 * 2 : raw_out = destination for newly malloc'd pointer to
757 * raw line data. May be NULL if you don't want it.
758 * 3 : prefix_out = destination for newly malloc'd pointer to
759 * comments. May be NULL if you don't want it.
760 * 4 : data_out = destination for newly malloc'd pointer to
761 * line data with comments and leading/trailing spaces
762 * removed, and line continuation performed. May be
763 * NULL if you don't want it.
764 * 5 : newline = Standard for newlines in the file.
765 * On input, set to value to use or NEWLINE_UNKNOWN.
766 * On output, may be changed from NEWLINE_UNKNOWN to
767 * actual convention in file. May be NULL if you
769 * 6 : line_number = Line number in file. In "lines" as
770 * reported by a text editor, not lines containing data.
772 * Returns : JB_ERR_OK on success
773 * JB_ERR_MEMORY on out-of-memory
774 * JB_ERR_FILE on EOF.
776 *********************************************************************/
777 jb_err edit_read_line(FILE *fp,
782 unsigned long *line_number)
784 char *p; /* Temporary pointer */
785 char *linebuf; /* Line read from file */
786 char *linestart; /* Start of linebuf, usually first non-whitespace char */
787 int contflag = 0; /* Nonzero for line continuation - i.e. line ends '\' */
788 int is_empty = 1; /* Flag if not got any data yet */
789 char *raw = NULL; /* String to be stored in raw_out */
790 char *prefix = NULL; /* String to be stored in prefix_out */
791 char *data = NULL; /* String to be stored in data_out */
792 int scrapnewline; /* Used for (*newline) if newline==NULL */
793 jb_err rval = JB_ERR_OK;
796 assert(raw_out || data_out);
797 assert(newline == NULL
798 || *newline == NEWLINE_UNKNOWN
799 || *newline == NEWLINE_UNIX
800 || *newline == NEWLINE_DOS
801 || *newline == NEWLINE_MAC);
805 scrapnewline = NEWLINE_UNKNOWN;
806 newline = &scrapnewline;
809 /* Set output parameters to NULL */
823 /* Set string variables to new, empty strings. */
827 if ((raw = malloc(1)) == NULL)
829 return JB_ERR_MEMORY;
835 if ((prefix = malloc(1)) == NULL)
838 return JB_ERR_MEMORY;
844 if ((data = malloc(1)) == NULL)
848 return JB_ERR_MEMORY;
853 /* Main loop. Loop while we need more data & it's not EOF. */
855 while ( (contflag || is_empty)
856 && (JB_ERR_OK == (rval = simple_read_line(fp, &linebuf, newline))))
864 string_append(&raw,linebuf);
865 if (string_append(&raw,NEWLINE(*newline)))
870 return JB_ERR_MEMORY;
874 /* Line continuation? Trim escape and set flag. */
875 p = linebuf + strlen(linebuf) - 1;
876 contflag = ((*linebuf != '\0') && (*p == '\\'));
882 /* Trim leading spaces if we're at the start of the line */
886 /* Trim leading spaces */
887 while (*linestart && isspace((int)(unsigned char)*linestart))
893 /* Handle comment characters. */
895 while ((p = strchr(p, '#')) != NULL)
897 /* Found a comment char.. */
898 if ((p != linebuf) && (*(p-1) == '\\'))
900 /* ..and it's escaped, left-shift the line over the escape. */
902 while ((*q = *(q + 1)) != '\0')
906 /* Now scan from just after the "#". */
910 /* Real comment. Save it... */
913 /* Special case: Line only contains a comment, so all the
914 * previous whitespace is considered part of the comment.
915 * Undo the whitespace skipping, if any.
922 string_append(&prefix,p);
923 if (string_append(&prefix, NEWLINE(*newline)))
928 return JB_ERR_MEMORY;
932 /* ... and chop off the rest of the line */
935 } /* END while (there's a # character) */
937 /* Write to the buffer */
943 if (string_append(&data, linestart))
948 return JB_ERR_MEMORY;
954 } /* END while(we need more data) */
956 /* Handle simple_read_line() errors - ignore EOF */
957 if ((rval != JB_ERR_OK) && (rval != JB_ERR_FILE))
965 if (raw ? (*raw == '\0') : is_empty)
967 /* EOF and no data there. (Definition of "data" depends on whether
968 * the caller cares about "raw" or just "data").
979 /* Got at least some data */
981 /* Remove trailing whitespace */
994 *prefix_out = prefix;
1013 /*********************************************************************
1015 * Function : read_config_line
1017 * Description : Read a single non-empty line from a file and return
1018 * it. Trims comments, leading and trailing whitespace
1019 * and respects escaping of newline and comment char.
1022 * 1 : buf = Buffer to use.
1023 * 2 : buflen = Size of buffer in bytes.
1024 * 3 : fp = File to read from
1025 * 4 : linenum = linenumber in file
1027 * Returns : NULL on EOF or error
1028 * Otherwise, returns buf.
1030 *********************************************************************/
1031 char *read_config_line(char *buf, size_t buflen, FILE *fp, unsigned long *linenum)
1035 err = edit_read_line(fp, NULL, NULL, &buf2, NULL, linenum);
1038 if (err == JB_ERR_MEMORY)
1040 log_error(LOG_LEVEL_FATAL, "Out of memory loading a config file");
1047 assert(strlen(buf2) + 1U < buflen);
1048 strncpy(buf, buf2, buflen - 1);
1050 buf[buflen - 1] = '\0';
1056 #ifdef FEATURE_TRUST
1057 /*********************************************************************
1059 * Function : unload_trustfile
1061 * Description : Unloads a trustfile.
1064 * 1 : f = the data structure associated with the trustfile.
1068 *********************************************************************/
1069 static void unload_trustfile(void *f)
1071 struct block_spec *cur = (struct block_spec *)f;
1072 struct block_spec *next;
1078 free_url_spec(cur->url);
1087 #ifdef FEATURE_GRACEFUL_TERMINATION
1088 /*********************************************************************
1090 * Function : unload_current_trust_file
1092 * Description : Unloads current trust file - reset to state at
1093 * beginning of program.
1099 *********************************************************************/
1100 void unload_current_trust_file(void)
1102 if (current_trustfile)
1104 current_trustfile->unloader = unload_trustfile;
1105 current_trustfile = NULL;
1108 #endif /* FEATURE_GRACEFUL_TERMINATION */
1111 /*********************************************************************
1113 * Function : load_trustfile
1115 * Description : Read and parse a trustfile and add to files list.
1118 * 1 : csp = Current client state (buffers, headers, etc...)
1120 * Returns : 0 => Ok, everything else is an error.
1122 *********************************************************************/
1123 int load_trustfile(struct client_state *csp)
1127 struct block_spec *b, *bl;
1128 struct url_spec **tl;
1130 char buf[BUFFER_SIZE], *p, *q;
1131 int reject, trusted;
1132 struct file_list *fs;
1133 unsigned long linenum = 0;
1134 int trusted_referrers = 0;
1136 if (!check_file_changed(current_trustfile, csp->config->trustfile, &fs))
1138 /* No need to load */
1141 csp->tlist = current_trustfile;
1147 goto load_trustfile_error;
1150 fs->f = bl = (struct block_spec *)zalloc(sizeof(*bl));
1153 goto load_trustfile_error;
1156 if ((fp = fopen(csp->config->trustfile, "r")) == NULL)
1158 goto load_trustfile_error;
1161 tl = csp->config->trust_list;
1163 while (read_config_line(buf, sizeof(buf), fp, &linenum) != NULL)
1179 while ((*p++ = *q++) != '\0')
1185 /* skip blank lines */
1191 /* allocate a new node */
1192 if ((b = zalloc(sizeof(*b))) == NULL)
1195 goto load_trustfile_error;
1198 /* add it to the list */
1204 /* Save the URL pattern */
1205 if (create_url_spec(b->url, buf))
1208 goto load_trustfile_error;
1212 * save a pointer to URL's spec in the list of trusted URL's, too
1216 if(++trusted_referrers < MAX_TRUSTED_REFERRERS)
1223 if(trusted_referrers >= MAX_TRUSTED_REFERRERS)
1226 * FIXME: ... after Privoxy 3.0.4 is out.
1228 log_error(LOG_LEVEL_ERROR, "Too many trusted referrers. Current limit is %d, you are using %d.\n"
1229 " Additional trusted referrers are treated like ordinary trusted URLs.\n"
1230 " (You can increase this limit by changing MAX_TRUSTED_REFERRERS in project.h and recompiling).",
1231 MAX_TRUSTED_REFERRERS, trusted_referrers);
1238 /* the old one is now obsolete */
1239 if (current_trustfile)
1241 current_trustfile->unloader = unload_trustfile;
1244 fs->next = files->next;
1246 current_trustfile = fs;
1255 load_trustfile_error:
1256 log_error(LOG_LEVEL_FATAL, "can't load trustfile '%s': %E",
1257 csp->config->trustfile);
1261 #endif /* def FEATURE_TRUST */
1264 /*********************************************************************
1266 * Function : unload_re_filterfile
1268 * Description : Unload the re_filter list by freeing all chained
1269 * re_filterfile specs and their data.
1272 * 1 : f = the data structure associated with the filterfile.
1276 *********************************************************************/
1277 static void unload_re_filterfile(void *f)
1279 struct re_filterfile_spec *a, *b = (struct re_filterfile_spec *)f;
1285 destroy_list(b->patterns);
1286 pcrs_free_joblist(b->joblist);
1288 freez(b->description);
1298 #ifdef FEATURE_GRACEFUL_TERMINATION
1299 /*********************************************************************
1301 * Function : unload_current_re_filterfile
1303 * Description : Unloads current re_filter file - reset to state at
1304 * beginning of program.
1310 *********************************************************************/
1311 void unload_current_re_filterfile(void)
1315 for (i = 0; i < MAX_AF_FILES; i++)
1317 if (current_re_filterfile[i])
1319 current_re_filterfile[i]->unloader = unload_re_filterfile;
1320 current_re_filterfile[i] = NULL;
1327 /*********************************************************************
1329 * Function : load_re_filterfile
1331 * Description : Load the re_filterfile.
1332 * Generate a chained list of re_filterfile_spec's from
1333 * the "FILTER: " blocks, compiling all their substitutions
1334 * into chained lists of pcrs_job structs.
1337 * 1 : csp = Current client state (buffers, headers, etc...)
1339 * Returns : 0 => Ok, everything else is an error.
1341 *********************************************************************/
1342 int load_re_filterfile(struct client_state *csp)
1347 for (i = 0; i < MAX_AF_FILES; i++)
1349 if (csp->config->re_filterfile[i])
1351 result = load_one_re_filterfile(csp, i);
1357 else if (current_re_filterfile[i])
1359 current_re_filterfile[i]->unloader = unload_re_filterfile;
1360 current_re_filterfile[i] = NULL;
1367 /*********************************************************************
1369 * Function : load_one_re_filterfile
1371 * Description : Load a re_filterfile.
1372 * Generate a chained list of re_filterfile_spec's from
1373 * the "FILTER: " blocks, compiling all their substitutions
1374 * into chained lists of pcrs_job structs.
1377 * 1 : csp = Current client state (buffers, headers, etc...)
1379 * Returns : 0 => Ok, everything else is an error.
1381 *********************************************************************/
1382 int load_one_re_filterfile(struct client_state *csp, int fileid)
1386 struct re_filterfile_spec *new_bl, *bl = NULL;
1387 struct file_list *fs;
1389 char buf[BUFFER_SIZE];
1391 unsigned long linenum = 0;
1392 pcrs_job *dummy, *lastjob = NULL;
1395 * No need to reload if unchanged
1397 if (!check_file_changed(current_re_filterfile[fileid], csp->config->re_filterfile[fileid], &fs))
1401 csp->rlist[fileid] = current_re_filterfile[fileid];
1407 goto load_re_filterfile_error;
1411 * Open the file or fail
1413 if ((fp = fopen(csp->config->re_filterfile[fileid], "r")) == NULL)
1415 goto load_re_filterfile_error;
1421 while (read_config_line(buf, sizeof(buf), fp, &linenum) != NULL)
1424 * If this is the head of a new filter block, make it a
1425 * re_filterfile spec of its own and chain it to the list:
1427 if (strncmp(buf, "FILTER:", 7) == 0)
1429 new_bl = (struct re_filterfile_spec *)zalloc(sizeof(*bl));
1432 goto load_re_filterfile_error;
1435 new_bl->name = chomp(buf + 7);
1438 * If a filter description is available,
1439 * encode it to HTML and save it.
1441 if (NULL != (new_bl->description = strpbrk(new_bl->name, " \t")))
1443 *new_bl->description++ = '\0';
1444 new_bl->description = html_encode(chomp(new_bl->description));
1445 if (NULL == new_bl->description)
1447 new_bl->description = strdup("Out of memory while encoding this filter's description to HTML");
1452 new_bl->description = strdup("No description available for this filter");
1455 new_bl->name = strdup(chomp(new_bl->name));
1458 * If this is the first filter block, chain it
1459 * to the file_list rather than its (nonexistant)
1472 log_error(LOG_LEVEL_RE_FILTER, "Reading in filter \"%s\" (\"%s\")", bl->name, bl->description);
1478 * Else, save the expression, make it a pcrs_job
1479 * and chain it into the current filter's joblist
1483 enlist(bl->patterns, buf);
1485 if ((dummy = pcrs_compile_command(buf, &error)) == NULL)
1487 log_error(LOG_LEVEL_ERROR,
1488 "Adding re_filter job %s to filter %s failed with error %d.", buf, bl->name, error);
1493 if (bl->joblist == NULL)
1495 bl->joblist = dummy;
1499 lastjob->next = dummy;
1502 log_error(LOG_LEVEL_RE_FILTER, "Adding re_filter job %s to filter %s succeeded.", buf, bl->name);
1507 log_error(LOG_LEVEL_ERROR, "Ignoring job %s outside filter block in %s, line %d",
1508 buf, csp->config->re_filterfile[fileid], linenum);
1515 * Schedule the now-obsolete old data for unloading
1517 if ( NULL != current_re_filterfile[fileid] )
1519 current_re_filterfile[fileid]->unloader = unload_re_filterfile;
1523 * Chain this file into the global list of loaded files
1525 fs->next = files->next;
1527 current_re_filterfile[fileid] = fs;
1531 csp->rlist[fileid] = fs;
1536 load_re_filterfile_error:
1537 log_error(LOG_LEVEL_FATAL, "can't load re_filterfile '%s': %E",
1538 csp->config->re_filterfile[fileid]);
1544 /*********************************************************************
1546 * Function : add_loader
1548 * Description : Called from `load_config'. Called once for each input
1549 * file found in config.
1552 * 1 : loader = pointer to a function that can parse and load
1553 * the appropriate config file.
1554 * 2 : config = The configuration_spec to add the loader to.
1558 *********************************************************************/
1559 void add_loader(int (*loader)(struct client_state *),
1560 struct configuration_spec * config)
1564 for (i=0; i < NLOADERS; i++)
1566 if (config->loaders[i] == NULL)
1568 config->loaders[i] = loader;
1576 /*********************************************************************
1578 * Function : run_loader
1580 * Description : Called from `load_config' and `listen_loop'. This
1581 * function keeps the "csp" current with any file mods
1582 * since the last loop. If a file is unchanged, the
1583 * loader functions do NOT reload the file.
1586 * 1 : csp = Current client state (buffers, headers, etc...)
1587 * Must be non-null. Reads: "csp->config"
1588 * Writes: various data members.
1590 * Returns : 0 => Ok, everything else is an error.
1592 *********************************************************************/
1593 int run_loader(struct client_state *csp)
1598 for (i=0; i < NLOADERS; i++)
1600 if (csp->config->loaders[i] == NULL)
1604 ret |= (csp->config->loaders[i])(csp);