1 const char loaders_rcs[] = "$Id: loaders.c,v 1.55 2006/09/07 10:25:39 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.55 2006/09/07 10:25:39 fabiankeil
41 * Revision 1.54 2006/09/07 10:22:20 fabiankeil
42 * If too many trusted referrers are used,
43 * print only one error message instead of logging
44 * every single trusted referrer above the arbitrary
47 * Revision 1.53 2006/08/31 16:25:06 fabiankeil
48 * Work around a buffer overflow that caused Privoxy to
49 * segfault if too many trusted referrers were used. Good
50 * enough for now, but should be replaced with a real
51 * solution after the next release.
53 * Revision 1.52 2006/07/18 14:48:46 david__schmidt
54 * Reorganizing the repository: swapping out what was HEAD (the old 3.1 branch)
55 * with what was really the latest development (the v_3_0_branch branch)
57 * Revision 1.50.2.8 2006/01/30 15:16:25 david__schmidt
58 * Remove a little residual debugging info
60 * Revision 1.50.2.7 2006/01/29 23:10:56 david__schmidt
61 * Multiple filter file support
63 * Revision 1.50.2.6 2003/10/24 10:17:54 oes
64 * Nit: Allowed tabs as separators in filter headings
66 * Revision 1.50.2.5 2003/05/08 15:19:15 oes
67 * sweep: Made loop structure of sweep step mirror that of mark step
69 * Revision 1.50.2.4 2003/05/06 15:57:12 oes
70 * Bugfix: Update last_active pointer in sweep() before
71 * leaving an active client. Closes bugs #724395, #727882
73 * Revision 1.50.2.3 2002/11/20 17:12:30 oes
74 * Ooops, forgot one change.
76 * Revision 1.50.2.2 2002/11/20 14:38:15 oes
77 * Fixed delayed/incomplete freeing of client resources and
78 * simplified loop structure in sweep.
79 * Thanks to Oliver Stoeneberg for the hint.
81 * Revision 1.50.2.1 2002/07/26 15:19:24 oes
82 * - PCRS jobs now chained in order of appearance. Previous
83 * reverse chaining was counter-intuitive.
84 * - Changed loglevel of PCRS job compile errors to
87 * Revision 1.50 2002/04/24 02:12:16 oes
88 * Jon's multiple AF patch: Sweep now takes care of all AFs
90 * Revision 1.49 2002/04/19 16:53:25 jongfoster
91 * Optimize away a function call by using an equivalent macro
93 * Revision 1.48 2002/04/05 00:56:09 gliptak
94 * Correcting typo to clean up on realloc failure
96 * Revision 1.47 2002/03/26 22:29:55 swa
97 * we have a new homepage!
99 * Revision 1.46 2002/03/24 13:25:43 swa
100 * name change related issues
102 * Revision 1.45 2002/03/16 23:54:06 jongfoster
103 * Adding graceful termination feature, to help look for memory leaks.
104 * If you enable this (which, by design, has to be done by hand
105 * editing config.h) and then go to http://i.j.b/die, then the program
106 * will exit cleanly after the *next* request. It should free all the
107 * memory that was used.
109 * Revision 1.44 2002/03/16 21:51:00 jongfoster
112 * Revision 1.43 2002/03/16 20:28:34 oes
113 * Added descriptions to the filters so users will know what they select in the cgi editor
115 * Revision 1.42 2002/03/13 00:27:05 jongfoster
118 * Revision 1.41 2002/03/12 01:42:50 oes
119 * Introduced modular filters
121 * Revision 1.40 2002/03/08 17:46:04 jongfoster
122 * Fixing int/size_t warnings
124 * Revision 1.39 2002/03/07 03:46:17 oes
125 * Fixed compiler warnings
127 * Revision 1.38 2002/03/06 22:54:35 jongfoster
128 * Automated function-comment nitpicking.
130 * Revision 1.37 2002/03/03 15:07:49 oes
131 * Re-enabled automatic config reloading
133 * Revision 1.36 2002/01/22 23:46:18 jongfoster
134 * Moving edit_read_line() and simple_read_line() to loaders.c, and
135 * extending them to support reading MS-DOS, Mac and UNIX style files
138 * Modifying read_config_line() (without changing it's prototype) to
139 * be a trivial wrapper for edit_read_line(). This means that we have
140 * one function to read a line and handle comments, which is common
141 * between the initialization code and the edit interface.
143 * Revision 1.35 2002/01/17 21:03:08 jongfoster
144 * Moving all our URL and URL pattern parsing code to urlmatch.c.
146 * Renaming free_url to free_url_spec, since it frees a struct url_spec.
148 * Revision 1.34 2001/12/30 14:07:32 steudten
149 * - Add signal handling (unix)
150 * - Add SIGHUP handler (unix)
151 * - Add creation of pidfile (unix)
152 * - Add action 'top' in rc file (RH)
153 * - Add entry 'SIGNALS' to manpage
154 * - Add exit message to logfile (unix)
156 * Revision 1.33 2001/11/13 00:16:38 jongfoster
157 * Replacing references to malloc.h with the standard stdlib.h
158 * (See ANSI or K&R 2nd Ed)
160 * Revision 1.32 2001/11/07 00:02:13 steudten
161 * Add line number in error output for lineparsing for
162 * actionsfile and configfile.
163 * Special handling for CLF added.
165 * Revision 1.31 2001/10/26 17:39:01 oes
166 * Removed csp->referrer
167 * Moved ijb_isspace and ijb_tolower to project.h
169 * Revision 1.30 2001/10/25 03:40:48 david__schmidt
170 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
171 * threads to call select() simultaneously. So, it's time to do a real, live,
172 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
173 * (native). Both versions will work, but using __OS2__ offers multi-threading.
175 * Revision 1.29 2001/10/23 21:38:53 jongfoster
176 * Adding error-checking to create_url_spec()
178 * Revision 1.28 2001/10/07 15:40:39 oes
179 * Replaced 6 boolean members of csp with one bitmap (csp->flags)
181 * Revision 1.27 2001/09/22 16:36:59 jongfoster
182 * Removing unused parameter fs from read_config_line()
184 * Revision 1.26 2001/09/22 14:05:22 jongfoster
185 * Bugfix: Multiple escaped "#" characters in a configuration
186 * file are now permitted.
187 * Also removing 3 unused headers.
189 * Revision 1.25 2001/09/13 22:44:03 jongfoster
190 * Adding {} to an if statement
192 * Revision 1.24 2001/07/30 22:08:36 jongfoster
193 * Tidying up #defines:
194 * - All feature #defines are now of the form FEATURE_xxx
195 * - Permanently turned off WIN_GUI_EDIT
196 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
198 * Revision 1.23 2001/07/20 15:51:54 oes
199 * Fixed indentation of prepocessor commands
201 * Revision 1.22 2001/07/20 15:16:17 haroon
202 * - per Guy's suggestion, added a while loop in sweep() to catch not just
203 * the last inactive CSP but all other consecutive inactive CSPs after that
206 * Revision 1.21 2001/07/18 17:26:24 oes
207 * Changed to conform to new pcrs interface
209 * Revision 1.20 2001/07/17 13:07:01 oes
210 * Fixed segv when last line in config files
211 * lacked a terminating (\r)\n
213 * Revision 1.19 2001/07/13 14:01:54 oes
214 * Removed all #ifdef PCRS
216 * Revision 1.18 2001/06/29 21:45:41 oes
217 * Indentation, CRLF->LF, Tab-> Space
219 * Revision 1.17 2001/06/29 13:31:51 oes
222 * Revision 1.16 2001/06/09 10:55:28 jongfoster
223 * Changing BUFSIZ ==> BUFFER_SIZE
225 * Revision 1.15 2001/06/07 23:14:14 jongfoster
226 * Removing ACL and forward file loaders - these
227 * files have been merged into the config file.
228 * Cosmetic: Moving unloader funcs next to their
229 * respective loader funcs
231 * Revision 1.14 2001/06/01 03:27:04 oes
232 * Fixed line continuation problem
234 * Revision 1.13 2001/05/31 21:28:49 jongfoster
235 * Removed all permissionsfile code - it's now called the actions
236 * file, and (almost) all the code is in actions.c
238 * Revision 1.12 2001/05/31 17:32:31 oes
240 * - Enhanced domain part globbing with infix and prefix asterisk
241 * matching and optional unanchored operation
243 * Revision 1.11 2001/05/29 23:25:24 oes
245 * - load_config_line() and load_permissions_file() now use chomp()
247 * Revision 1.10 2001/05/29 09:50:24 jongfoster
248 * Unified blocklist/imagelist/permissionslist.
249 * File format is still under discussion, but the internal changes
252 * Also modified interceptor behaviour:
253 * - We now intercept all URLs beginning with one of the following
254 * prefixes (and *only* these prefixes):
256 * * http://ijbswa.sf.net/config/
257 * * http://ijbswa.sourceforge.net/config/
258 * - New interceptors "home page" - go to http://i.j.b/ to see it.
259 * - Internal changes so that intercepted and fast redirect pages
260 * are not replaced with an image.
261 * - Interceptors now have the option to send a binary page direct
262 * to the client. (i.e. ijb-send-banner uses this)
263 * - Implemented show-url-info interceptor. (Which is why I needed
264 * the above interceptors changes - a typical URL is
265 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
266 * The previous mechanism would not have intercepted that, and
267 * if it had been intercepted then it then it would have replaced
270 * Revision 1.9 2001/05/26 17:12:07 jongfoster
271 * Fatal errors loading configuration files now give better error messages.
273 * Revision 1.8 2001/05/26 00:55:20 jongfoster
274 * Removing duplicated code. load_forwardfile() now uses create_url_spec()
276 * Revision 1.7 2001/05/26 00:28:36 jongfoster
277 * Automatic reloading of config file.
278 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
279 * Most of the global variables have been moved to a new
280 * struct configuration_spec, accessed through csp->config->globalname
281 * Most of the globals remaining are used by the Win32 GUI.
283 * Revision 1.6 2001/05/23 12:27:33 oes
285 * Fixed ugly indentation of my last changes
287 * Revision 1.5 2001/05/23 10:39:05 oes
288 * - Added support for escaping the comment character
289 * in config files by a backslash
290 * - Added support for line continuation in config
292 * - Fixed a buffer overflow bug with long config lines
294 * Revision 1.4 2001/05/22 18:56:28 oes
297 * Revision 1.3 2001/05/20 01:21:20 jongfoster
298 * Version 2.9.4 checkin.
299 * - Merged popupfile and cookiefile, and added control over PCRS
300 * filtering, in new "permissionsfile".
301 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
302 * file error you now get a message box (in the Win32 GUI) rather
303 * than the program exiting with no explanation.
304 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
306 * - Removed tabs from "config"
307 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
308 * - Bumped up version number.
310 * Revision 1.2 2001/05/17 23:01:01 oes
311 * - Cleaned CRLF's from the sources and related files
313 * Revision 1.1.1.1 2001/05/15 13:58:59 oes
314 * Initial import of version 2.9.3 source tree
317 *********************************************************************/
324 #include <sys/types.h>
327 #include <sys/stat.h>
331 #if !defined(_WIN32) && !defined(__OS2__)
341 #include "miscutil.h"
344 #include "urlmatch.h"
346 const char loaders_h_rcs[] = LOADERS_H_VERSION;
349 * Currently active files.
350 * These are also entered in the main linked list of files.
354 static struct file_list *current_trustfile = NULL;
355 #endif /* def FEATURE_TRUST */
357 static int load_one_re_filterfile(struct client_state *csp, int fileid);
359 static struct file_list *current_re_filterfile[MAX_AF_FILES] = {
360 NULL, NULL, NULL, NULL, NULL,
361 NULL, NULL, NULL, NULL, NULL
366 /*********************************************************************
370 * Description : Basically a mark and sweep garbage collector, it is run
371 * (by the parent thread) every once in a while to reclaim memory.
373 * It uses a mark and sweep strategy:
374 * 1) mark all files as inactive
376 * 2) check with each client:
377 * if it is active, mark its files as active
378 * if it is inactive, free its resources
380 * 3) free the resources of all of the files that
381 * are still marked as inactive (and are obsolete).
383 * N.B. files that are not obsolete don't have an unloader defined.
389 *********************************************************************/
392 struct file_list *fl, *nfl;
393 struct client_state *csp, *last_active;
396 /* clear all of the file's active flags */
397 for ( fl = files->next; NULL != fl; fl = fl->next )
402 last_active = clients;
407 if (csp->flags & CSP_FLAG_ACTIVE)
409 /* Mark this client's files as active */
412 * Always have a configuration file.
413 * (Also note the slightly non-standard extra
416 csp->config->config_file_list->active = 1;
421 for (i = 0; i < MAX_AF_FILES; i++)
423 if (csp->actions_list[i])
425 csp->actions_list[i]->active = 1;
432 for (i = 0; i < MAX_AF_FILES; i++)
436 csp->rlist[i]->active = 1;
446 csp->tlist->active = 1;
448 #endif /* def FEATURE_TRUST */
456 * This client is not active. Free its resources.
459 last_active->next = csp->next;
461 freez(csp->ip_addr_str);
462 freez(csp->my_ip_addr_str);
463 freez(csp->my_hostname);
464 freez(csp->x_forwarded);
465 freez(csp->iob->buf);
467 free_http_request(csp->http);
469 destroy_list(csp->headers);
470 destroy_list(csp->cookie_list);
472 free_current_action(csp->action);
474 #ifdef FEATURE_STATISTICS
476 if (csp->flags & CSP_FLAG_REJECTED)
480 #endif /* def FEATURE_STATISTICS */
484 csp = last_active->next;
493 if ( ( 0 == fl->active ) && ( NULL != fl->unloader ) )
495 nfl->next = fl->next;
497 (fl->unloader)(fl->f);
514 /*********************************************************************
516 * Function : check_file_changed
518 * Description : Helper function to check if a file needs reloading.
519 * If "current" is still current, return it. Otherwise
520 * allocates a new (zeroed) "struct file_list", fills
521 * in the disk file name and timestamp, and returns it.
524 * 1 : current = The file_list currently being used - will
525 * be checked to see if it is out of date.
526 * May be NULL (which is treated as out of
528 * 2 : filename = Name of file to check.
529 * 3 : newfl = New file list. [Output only]
530 * This will be set to NULL, OR a struct
531 * file_list newly allocated on the
532 * heap, with the filename and lastmodified
533 * fields filled, and all others zeroed.
535 * Returns : If file unchanged: 0 (and sets newfl == NULL)
536 * If file changed: 1 and sets newfl != NULL
537 * On error: 1 and sets newfl == NULL
539 *********************************************************************/
540 int check_file_changed(const struct file_list * current,
541 const char * filename,
542 struct file_list ** newfl)
544 struct file_list *fs;
545 struct stat statbuf[1];
549 if (stat(filename, statbuf) < 0)
551 /* Error, probably file not found. */
556 && (current->lastmodified == statbuf->st_mtime)
557 && (0 == strcmp(current->filename, filename)))
562 fs = (struct file_list *)zalloc(sizeof(struct file_list));
565 /* Out of memory error */
570 fs->filename = strdup(filename);
571 fs->lastmodified = statbuf->st_mtime;
573 if (fs->filename == NULL)
575 /* Out of memory error */
584 /*********************************************************************
586 * Function : simple_read_line
588 * Description : Read a single line from a file and return it.
589 * This is basically a version of fgets() that malloc()s
590 * it's own line buffer. Note that the buffer will
591 * always be a multiple of BUFFER_SIZE bytes long.
592 * Therefore if you are going to keep the string for
593 * an extended period of time, you should probably
594 * strdup() it and free() the original, to save memory.
598 * 1 : dest = destination for newly malloc'd pointer to
599 * line data. Will be set to NULL on error.
600 * 2 : fp = File to read from
601 * 3 : newline = Standard for newlines in the file.
602 * Will be unchanged if it's value on input is not
604 * On output, may be changed from NEWLINE_UNKNOWN to
605 * actual convention in file.
607 * Returns : JB_ERR_OK on success
608 * JB_ERR_MEMORY on out-of-memory
609 * JB_ERR_FILE on EOF.
611 *********************************************************************/
612 jb_err simple_read_line(FILE *fp, char **dest, int *newline)
615 size_t buflen = BUFFER_SIZE;
619 int realnewline = NEWLINE_UNKNOWN;
621 if (NULL == (buf = malloc(buflen)))
623 return JB_ERR_MEMORY;
629 * Character codes. If you have a wierd compiler and the following are
630 * incorrect, you also need to fix NEWLINE() in loaders.h
632 #define CHAR_CR '\r' /* ASCII 13 */
633 #define CHAR_LF '\n' /* ASCII 10 */
653 else if (ch == CHAR_CR)
658 if (*newline == NEWLINE_UNKNOWN)
660 *newline = NEWLINE_DOS;
669 if (*newline == NEWLINE_UNKNOWN)
671 *newline = NEWLINE_MAC;
676 if (*newline == NEWLINE_UNKNOWN)
678 *newline = realnewline;
682 else if (ch == CHAR_LF)
686 if (*newline == NEWLINE_UNKNOWN)
688 *newline = NEWLINE_UNIX;
703 buflen += BUFFER_SIZE;
704 if (NULL == (p = realloc(buf, buflen)))
707 return JB_ERR_MEMORY;
716 /*********************************************************************
718 * Function : edit_read_line
720 * Description : Read a single non-empty line from a file and return
721 * it. Trims comments, leading and trailing whitespace
722 * and respects escaping of newline and comment char.
723 * Provides the line in 2 alternative forms: raw and
725 * - raw is the raw data read from the file. If the
726 * line is not modified, then this should be written
728 * - prefix is any comments and blank lines that were
729 * read from the file. If the line is modified, then
730 * this should be written out to the file followed
731 * by the modified data. (If this string is non-empty
732 * then it will have a newline at the end).
733 * - data is the actual data that will be parsed
734 * further by appropriate routines.
735 * On EOF, the 3 strings will all be set to NULL and
736 * 0 will be returned.
739 * 1 : fp = File to read from
740 * 2 : raw_out = destination for newly malloc'd pointer to
741 * raw line data. May be NULL if you don't want it.
742 * 3 : prefix_out = destination for newly malloc'd pointer to
743 * comments. May be NULL if you don't want it.
744 * 4 : data_out = destination for newly malloc'd pointer to
745 * line data with comments and leading/trailing spaces
746 * removed, and line continuation performed. May be
747 * NULL if you don't want it.
748 * 5 : newline = Standard for newlines in the file.
749 * On input, set to value to use or NEWLINE_UNKNOWN.
750 * On output, may be changed from NEWLINE_UNKNOWN to
751 * actual convention in file. May be NULL if you
753 * 6 : line_number = Line number in file. In "lines" as
754 * reported by a text editor, not lines containing data.
756 * Returns : JB_ERR_OK on success
757 * JB_ERR_MEMORY on out-of-memory
758 * JB_ERR_FILE on EOF.
760 *********************************************************************/
761 jb_err edit_read_line(FILE *fp,
766 unsigned long *line_number)
768 char *p; /* Temporary pointer */
769 char *linebuf; /* Line read from file */
770 char *linestart; /* Start of linebuf, usually first non-whitespace char */
771 int contflag = 0; /* Nonzero for line continuation - i.e. line ends '\' */
772 int is_empty = 1; /* Flag if not got any data yet */
773 char *raw = NULL; /* String to be stored in raw_out */
774 char *prefix = NULL; /* String to be stored in prefix_out */
775 char *data = NULL; /* String to be stored in data_out */
776 int scrapnewline; /* Used for (*newline) if newline==NULL */
777 jb_err rval = JB_ERR_OK;
780 assert(raw_out || data_out);
781 assert(newline == NULL
782 || *newline == NEWLINE_UNKNOWN
783 || *newline == NEWLINE_UNIX
784 || *newline == NEWLINE_DOS
785 || *newline == NEWLINE_MAC);
789 scrapnewline = NEWLINE_UNKNOWN;
790 newline = &scrapnewline;
793 /* Set output parameters to NULL */
807 /* Set string variables to new, empty strings. */
811 if ((raw = malloc(1)) == NULL)
813 return JB_ERR_MEMORY;
819 if ((prefix = malloc(1)) == NULL)
822 return JB_ERR_MEMORY;
828 if ((data = malloc(1)) == NULL)
832 return JB_ERR_MEMORY;
837 /* Main loop. Loop while we need more data & it's not EOF. */
839 while ( (contflag || is_empty)
840 && (JB_ERR_OK == (rval = simple_read_line(fp, &linebuf, newline))))
848 string_append(&raw,linebuf);
849 if (string_append(&raw,NEWLINE(*newline)))
854 return JB_ERR_MEMORY;
858 /* Line continuation? Trim escape and set flag. */
859 p = linebuf + strlen(linebuf) - 1;
860 contflag = ((*linebuf != '\0') && (*p == '\\'));
866 /* Trim leading spaces if we're at the start of the line */
870 /* Trim leading spaces */
871 while (*linestart && isspace((int)(unsigned char)*linestart))
877 /* Handle comment characters. */
879 while ((p = strchr(p, '#')) != NULL)
881 /* Found a comment char.. */
882 if ((p != linebuf) && (*(p-1) == '\\'))
884 /* ..and it's escaped, left-shift the line over the escape. */
886 while ((*q = *(q + 1)) != '\0')
890 /* Now scan from just after the "#". */
894 /* Real comment. Save it... */
897 /* Special case: Line only contains a comment, so all the
898 * previous whitespace is considered part of the comment.
899 * Undo the whitespace skipping, if any.
906 string_append(&prefix,p);
907 if (string_append(&prefix, NEWLINE(*newline)))
912 return JB_ERR_MEMORY;
916 /* ... and chop off the rest of the line */
919 } /* END while (there's a # character) */
921 /* Write to the buffer */
927 if (string_append(&data, linestart))
932 return JB_ERR_MEMORY;
938 } /* END while(we need more data) */
940 /* Handle simple_read_line() errors - ignore EOF */
941 if ((rval != JB_ERR_OK) && (rval != JB_ERR_FILE))
949 if (raw ? (*raw == '\0') : is_empty)
951 /* EOF and no data there. (Definition of "data" depends on whether
952 * the caller cares about "raw" or just "data").
963 /* Got at least some data */
965 /* Remove trailing whitespace */
978 *prefix_out = prefix;
997 /*********************************************************************
999 * Function : read_config_line
1001 * Description : Read a single non-empty line from a file and return
1002 * it. Trims comments, leading and trailing whitespace
1003 * and respects escaping of newline and comment char.
1006 * 1 : buf = Buffer to use.
1007 * 2 : buflen = Size of buffer in bytes.
1008 * 3 : fp = File to read from
1009 * 4 : linenum = linenumber in file
1011 * Returns : NULL on EOF or error
1012 * Otherwise, returns buf.
1014 *********************************************************************/
1015 char *read_config_line(char *buf, size_t buflen, FILE *fp, unsigned long *linenum)
1019 err = edit_read_line(fp, NULL, NULL, &buf2, NULL, linenum);
1022 if (err == JB_ERR_MEMORY)
1024 log_error(LOG_LEVEL_FATAL, "Out of memory loading a config file");
1031 assert(strlen(buf2) + 1U < buflen);
1032 strncpy(buf, buf2, buflen - 1);
1034 buf[buflen - 1] = '\0';
1040 #ifdef FEATURE_TRUST
1041 /*********************************************************************
1043 * Function : unload_trustfile
1045 * Description : Unloads a trustfile.
1048 * 1 : f = the data structure associated with the trustfile.
1052 *********************************************************************/
1053 static void unload_trustfile(void *f)
1055 struct block_spec *cur = (struct block_spec *)f;
1056 struct block_spec *next;
1062 free_url_spec(cur->url);
1071 #ifdef FEATURE_GRACEFUL_TERMINATION
1072 /*********************************************************************
1074 * Function : unload_current_trust_file
1076 * Description : Unloads current trust file - reset to state at
1077 * beginning of program.
1083 *********************************************************************/
1084 void unload_current_trust_file(void)
1086 if (current_trustfile)
1088 current_trustfile->unloader = unload_trustfile;
1089 current_trustfile = NULL;
1092 #endif /* FEATURE_GRACEFUL_TERMINATION */
1095 /*********************************************************************
1097 * Function : load_trustfile
1099 * Description : Read and parse a trustfile and add to files list.
1102 * 1 : csp = Current client state (buffers, headers, etc...)
1104 * Returns : 0 => Ok, everything else is an error.
1106 *********************************************************************/
1107 int load_trustfile(struct client_state *csp)
1111 struct block_spec *b, *bl;
1112 struct url_spec **tl;
1114 char buf[BUFFER_SIZE], *p, *q;
1115 int reject, trusted;
1116 struct file_list *fs;
1117 unsigned long linenum = 0;
1118 int trusted_referrers = 0;
1120 if (!check_file_changed(current_trustfile, csp->config->trustfile, &fs))
1122 /* No need to load */
1125 csp->tlist = current_trustfile;
1131 goto load_trustfile_error;
1134 fs->f = bl = (struct block_spec *)zalloc(sizeof(*bl));
1137 goto load_trustfile_error;
1140 if ((fp = fopen(csp->config->trustfile, "r")) == NULL)
1142 goto load_trustfile_error;
1145 tl = csp->config->trust_list;
1147 while (read_config_line(buf, sizeof(buf), fp, &linenum) != NULL)
1163 while ((*p++ = *q++) != '\0')
1169 /* skip blank lines */
1175 /* allocate a new node */
1176 if ((b = zalloc(sizeof(*b))) == NULL)
1179 goto load_trustfile_error;
1182 /* add it to the list */
1188 /* Save the URL pattern */
1189 if (create_url_spec(b->url, buf))
1192 goto load_trustfile_error;
1196 * save a pointer to URL's spec in the list of trusted URL's, too
1200 if(++trusted_referrers < MAX_TRUSTED_REFERRERS)
1207 if(trusted_referrers >= MAX_TRUSTED_REFERRERS)
1210 * FIXME: ... after Privoxy 3.0.4 is out.
1212 log_error(LOG_LEVEL_ERROR, "Too many trusted referrers. Current limit is %d, you are using %d.\n"
1213 " Additional trusted referrers are treated like ordinary trusted URLs.\n"
1214 " (You can increase this limit by changing MAX_TRUSTED_REFERRERS in project.h and recompiling).",
1215 MAX_TRUSTED_REFERRERS, trusted_referrers);
1222 /* the old one is now obsolete */
1223 if (current_trustfile)
1225 current_trustfile->unloader = unload_trustfile;
1228 fs->next = files->next;
1230 current_trustfile = fs;
1239 load_trustfile_error:
1240 log_error(LOG_LEVEL_FATAL, "can't load trustfile '%s': %E",
1241 csp->config->trustfile);
1245 #endif /* def FEATURE_TRUST */
1248 /*********************************************************************
1250 * Function : unload_re_filterfile
1252 * Description : Unload the re_filter list by freeing all chained
1253 * re_filterfile specs and their data.
1256 * 1 : f = the data structure associated with the filterfile.
1260 *********************************************************************/
1261 static void unload_re_filterfile(void *f)
1263 struct re_filterfile_spec *a, *b = (struct re_filterfile_spec *)f;
1269 destroy_list(b->patterns);
1270 pcrs_free_joblist(b->joblist);
1272 freez(b->description);
1282 #ifdef FEATURE_GRACEFUL_TERMINATION
1283 /*********************************************************************
1285 * Function : unload_current_re_filterfile
1287 * Description : Unloads current re_filter file - reset to state at
1288 * beginning of program.
1294 *********************************************************************/
1295 void unload_current_re_filterfile(void)
1299 for (i = 0; i < MAX_AF_FILES; i++)
1301 if (current_re_filterfile[i])
1303 current_re_filterfile[i]->unloader = unload_re_filterfile;
1304 current_re_filterfile[i] = NULL;
1311 /*********************************************************************
1313 * Function : load_re_filterfile
1315 * Description : Load the re_filterfile.
1316 * Generate a chained list of re_filterfile_spec's from
1317 * the "FILTER: " blocks, compiling all their substitutions
1318 * into chained lists of pcrs_job structs.
1321 * 1 : csp = Current client state (buffers, headers, etc...)
1323 * Returns : 0 => Ok, everything else is an error.
1325 *********************************************************************/
1326 int load_re_filterfile(struct client_state *csp)
1331 for (i = 0; i < MAX_AF_FILES; i++)
1333 if (csp->config->re_filterfile[i])
1335 result = load_one_re_filterfile(csp, i);
1341 else if (current_re_filterfile[i])
1343 current_re_filterfile[i]->unloader = unload_re_filterfile;
1344 current_re_filterfile[i] = NULL;
1351 /*********************************************************************
1353 * Function : load_one_re_filterfile
1355 * Description : Load a re_filterfile.
1356 * Generate a chained list of re_filterfile_spec's from
1357 * the "FILTER: " blocks, compiling all their substitutions
1358 * into chained lists of pcrs_job structs.
1361 * 1 : csp = Current client state (buffers, headers, etc...)
1363 * Returns : 0 => Ok, everything else is an error.
1365 *********************************************************************/
1366 int load_one_re_filterfile(struct client_state *csp, int fileid)
1370 struct re_filterfile_spec *new_bl, *bl = NULL;
1371 struct file_list *fs;
1373 char buf[BUFFER_SIZE];
1375 unsigned long linenum = 0;
1376 pcrs_job *dummy, *lastjob = NULL;
1379 * No need to reload if unchanged
1381 if (!check_file_changed(current_re_filterfile[fileid], csp->config->re_filterfile[fileid], &fs))
1385 csp->rlist[fileid] = current_re_filterfile[fileid];
1391 goto load_re_filterfile_error;
1395 * Open the file or fail
1397 if ((fp = fopen(csp->config->re_filterfile[fileid], "r")) == NULL)
1399 goto load_re_filterfile_error;
1405 while (read_config_line(buf, sizeof(buf), fp, &linenum) != NULL)
1408 * If this is the head of a new filter block, make it a
1409 * re_filterfile spec of its own and chain it to the list:
1411 if (strncmp(buf, "FILTER:", 7) == 0)
1413 new_bl = (struct re_filterfile_spec *)zalloc(sizeof(*bl));
1416 goto load_re_filterfile_error;
1419 new_bl->name = chomp(buf + 7);
1421 if (NULL != (new_bl->description = strpbrk(new_bl->name, " \t")))
1423 *new_bl->description++ = '\0';
1424 new_bl->description = strdup(chomp(new_bl->description));
1428 new_bl->description = strdup("No description available for this filter");
1431 new_bl->name = strdup(chomp(new_bl->name));
1434 * If this is the first filter block, chain it
1435 * to the file_list rather than its (nonexistant)
1448 log_error(LOG_LEVEL_RE_FILTER, "Reading in filter \"%s\" (\"%s\")", bl->name, bl->description);
1454 * Else, save the expression, make it a pcrs_job
1455 * and chain it into the current filter's joblist
1459 enlist(bl->patterns, buf);
1461 if ((dummy = pcrs_compile_command(buf, &error)) == NULL)
1463 log_error(LOG_LEVEL_ERROR,
1464 "Adding re_filter job %s to filter %s failed with error %d.", buf, bl->name, error);
1469 if (bl->joblist == NULL)
1471 bl->joblist = dummy;
1475 lastjob->next = dummy;
1478 log_error(LOG_LEVEL_RE_FILTER, "Adding re_filter job %s to filter %s succeeded.", buf, bl->name);
1483 log_error(LOG_LEVEL_ERROR, "Ignoring job %s outside filter block in %s, line %d", buf, csp->config->re_filterfile, linenum);
1490 * Schedule the now-obsolete old data for unloading
1492 if ( NULL != current_re_filterfile[fileid] )
1494 current_re_filterfile[fileid]->unloader = unload_re_filterfile;
1498 * Chain this file into the global list of loaded files
1500 fs->next = files->next;
1502 current_re_filterfile[fileid] = fs;
1506 csp->rlist[fileid] = fs;
1511 load_re_filterfile_error:
1512 log_error(LOG_LEVEL_FATAL, "can't load re_filterfile '%s': %E",
1513 csp->config->re_filterfile[fileid]);
1519 /*********************************************************************
1521 * Function : add_loader
1523 * Description : Called from `load_config'. Called once for each input
1524 * file found in config.
1527 * 1 : loader = pointer to a function that can parse and load
1528 * the appropriate config file.
1529 * 2 : config = The configuration_spec to add the loader to.
1533 *********************************************************************/
1534 void add_loader(int (*loader)(struct client_state *),
1535 struct configuration_spec * config)
1539 for (i=0; i < NLOADERS; i++)
1541 if (config->loaders[i] == NULL)
1543 config->loaders[i] = loader;
1551 /*********************************************************************
1553 * Function : run_loader
1555 * Description : Called from `load_config' and `listen_loop'. This
1556 * function keeps the "csp" current with any file mods
1557 * since the last loop. If a file is unchanged, the
1558 * loader functions do NOT reload the file.
1561 * 1 : csp = Current client state (buffers, headers, etc...)
1562 * Must be non-null. Reads: "csp->config"
1563 * Writes: various data members.
1565 * Returns : 0 => Ok, everything else is an error.
1567 *********************************************************************/
1568 int run_loader(struct client_state *csp)
1573 for (i=0; i < NLOADERS; i++)
1575 if (csp->config->loaders[i] == NULL)
1579 ret |= (csp->config->loaders[i])(csp);