1 /*********************************************************************
3 * File : $Source: /cvsroot/ijbswa/current/loaders.c,v $
5 * Purpose : Functions to load and unload the various
6 * configuration files. Also contains code to manage
7 * the list of active loaders, and to automatically
8 * unload files that are no longer in use.
10 * Copyright : Written by and Copyright (C) 2001-2014 the
11 * Privoxy team. https://www.privoxy.org/
13 * Based on the Internet Junkbuster originally written
14 * by and Copyright (C) 1997 Anonymous Coders and
15 * Junkbusters Corporation. http://www.junkbusters.com
17 * This program is free software; you can redistribute it
18 * and/or modify it under the terms of the GNU General
19 * Public License as published by the Free Software
20 * Foundation; either version 2 of the License, or (at
21 * your option) any later version.
23 * This program is distributed in the hope that it will
24 * be useful, but WITHOUT ANY WARRANTY; without even the
25 * implied warranty of MERCHANTABILITY or FITNESS FOR A
26 * PARTICULAR PURPOSE. See the GNU General Public
27 * License for more details.
29 * The GNU General Public License should be included with
30 * this file. If not, you can view it at
31 * http://www.gnu.org/copyleft/gpl.html
32 * or write to the Free Software Foundation, Inc., 59
33 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
35 *********************************************************************/
42 #include <sys/types.h>
49 #if !defined(_WIN32) && !defined(__OS2__)
66 * Currently active files.
67 * These are also entered in the main linked list of files.
71 static struct file_list *current_trustfile = NULL;
72 #endif /* def FEATURE_TRUST */
75 static int load_one_re_filterfile(struct client_state *csp, int fileid);
78 static struct file_list *current_re_filterfile[MAX_AF_FILES] = {
79 NULL, NULL, NULL, NULL, NULL,
80 NULL, NULL, NULL, NULL, NULL
83 /*********************************************************************
85 * Function : free_csp_resources
87 * Description : Frees memory referenced by the csp that isn't
88 * shared with other csps.
91 * 1 : csp = Current client state (buffers, headers, etc...)
95 *********************************************************************/
96 void free_csp_resources(struct client_state *csp)
98 freez(csp->ip_addr_str);
99 #ifdef FEATURE_CLIENT_TAGS
100 freez(csp->client_address);
102 freez(csp->listen_addr_str);
103 freez(csp->client_iob->buf);
104 freez(csp->iob->buf);
105 freez(csp->error_message);
107 if (csp->action->flags & ACTION_FORWARD_OVERRIDE &&
110 unload_forward_spec(csp->fwd);
112 free_http_request(csp->http);
114 destroy_list(csp->headers);
115 #ifdef FEATURE_HTTPS_INSPECTION
116 destroy_list(csp->https_headers);
118 destroy_list(csp->tags);
120 free_current_action(csp->action);
123 /*********************************************************************
127 * Description : Basically a mark and sweep garbage collector, it is run
128 * (by the parent thread) every once in a while to reclaim memory.
130 * It uses a mark and sweep strategy:
131 * 1) mark all files as inactive
133 * 2) check with each client:
134 * if it is active, mark its files as active
135 * if it is inactive, free its resources
137 * 3) free the resources of all of the files that
138 * are still marked as inactive (and are obsolete).
140 * N.B. files that are not obsolete don't have an unloader defined.
144 * Returns : The number of threads that are still active.
146 *********************************************************************/
147 unsigned int sweep(void)
149 struct file_list *fl, *nfl;
150 struct client_state *csp;
151 struct client_states *last_active, *client_list;
153 unsigned int active_threads = 0;
155 /* clear all of the file's active flags */
156 for (fl = files->next; NULL != fl; fl = fl->next)
161 last_active = clients;
162 client_list = clients->next;
164 while (NULL != client_list)
166 csp = &client_list->csp;
167 if (csp->flags & CSP_FLAG_ACTIVE)
169 /* Mark this client's files as active */
172 * Always have a configuration file.
173 * (Also note the slightly non-standard extra
176 csp->config->config_file_list->active = 1;
181 for (i = 0; i < MAX_AF_FILES; i++)
183 if (csp->actions_list[i])
185 csp->actions_list[i]->active = 1;
192 for (i = 0; i < MAX_AF_FILES; i++)
196 csp->rlist[i]->active = 1;
206 csp->tlist->active = 1;
208 #endif /* def FEATURE_TRUST */
212 last_active = client_list;
213 client_list = client_list->next;
217 * This client is not active. Free its resources.
220 last_active->next = client_list->next;
222 #ifdef FEATURE_STATISTICS
224 if (csp->flags & CSP_FLAG_REJECTED)
228 #endif /* def FEATURE_STATISTICS */
232 client_list = last_active->next;
241 if ((0 == fl->active) && (NULL != fl->unloader))
243 nfl->next = fl->next;
245 (fl->unloader)(fl->f);
259 return active_threads;
264 /*********************************************************************
266 * Function : check_file_changed
268 * Description : Helper function to check if a file needs reloading.
269 * If "current" is still current, return it. Otherwise
270 * allocates a new (zeroed) "struct file_list", fills
271 * in the disk file name and timestamp, and returns it.
274 * 1 : current = The file_list currently being used - will
275 * be checked to see if it is out of date.
276 * May be NULL (which is treated as out of
278 * 2 : filename = Name of file to check.
279 * 3 : newfl = New file list. [Output only]
280 * This will be set to NULL, OR a struct
281 * file_list newly allocated on the
282 * heap, with the filename and lastmodified
283 * fields filled, and all others zeroed.
285 * Returns : If file unchanged: 0 (and sets newfl == NULL)
286 * If file changed: 1 and sets newfl != NULL
287 * On error: 1 and sets newfl == NULL
289 *********************************************************************/
290 int check_file_changed(const struct file_list * current,
291 const char * filename,
292 struct file_list ** newfl)
294 struct file_list *fs;
295 struct stat statbuf[1];
299 if (stat(filename, statbuf) < 0)
301 /* Error, probably file not found. */
306 && (current->lastmodified == statbuf->st_mtime)
307 && (0 == strcmp(current->filename, filename)))
312 fs = zalloc_or_die(sizeof(struct file_list));
313 fs->filename = strdup_or_die(filename);
314 fs->lastmodified = statbuf->st_mtime;
321 /*********************************************************************
323 * Function : simple_read_line
325 * Description : Read a single line from a file and return it.
326 * This is basically a version of fgets() that malloc()s
327 * it's own line buffer. Note that the buffer will
328 * always be a multiple of BUFFER_SIZE bytes long.
329 * Therefore if you are going to keep the string for
330 * an extended period of time, you should probably
331 * strdup() it and free() the original, to save memory.
335 * 1 : dest = destination for newly malloc'd pointer to
336 * line data. Will be set to NULL on error.
337 * 2 : fp = File to read from
338 * 3 : newline = Standard for newlines in the file.
339 * Will be unchanged if it's value on input is not
341 * On output, may be changed from NEWLINE_UNKNOWN to
342 * actual convention in file.
344 * Returns : JB_ERR_OK on success
345 * JB_ERR_MEMORY on out-of-memory
346 * JB_ERR_FILE on EOF.
348 *********************************************************************/
349 jb_err simple_read_line(FILE *fp, char **dest, int *newline)
352 size_t buflen = BUFFER_SIZE;
356 int realnewline = NEWLINE_UNKNOWN;
358 if (NULL == (buf = malloc(buflen)))
360 return JB_ERR_MEMORY;
366 * Character codes. If you have a weird compiler and the following are
367 * incorrect, you also need to fix NEWLINE() in loaders.h
369 #define CHAR_CR '\r' /* ASCII 13 */
370 #define CHAR_LF '\n' /* ASCII 10 */
391 else if (ch == CHAR_CR)
396 if (*newline == NEWLINE_UNKNOWN)
398 *newline = NEWLINE_DOS;
407 if (*newline == NEWLINE_UNKNOWN)
409 *newline = NEWLINE_MAC;
414 if (*newline == NEWLINE_UNKNOWN)
416 *newline = realnewline;
420 else if (ch == CHAR_LF)
424 if (*newline == NEWLINE_UNKNOWN)
426 *newline = NEWLINE_UNIX;
432 /* XXX: Why do we allow this anyway? */
442 buflen += BUFFER_SIZE;
443 if (NULL == (p = realloc(buf, buflen)))
446 return JB_ERR_MEMORY;
455 /*********************************************************************
457 * Function : edit_read_line
459 * Description : Read a single non-empty line from a file and return
460 * it. Trims comments, leading and trailing whitespace
461 * and respects escaping of newline and comment char.
462 * Provides the line in 2 alternative forms: raw and
464 * - raw is the raw data read from the file. If the
465 * line is not modified, then this should be written
467 * - prefix is any comments and blank lines that were
468 * read from the file. If the line is modified, then
469 * this should be written out to the file followed
470 * by the modified data. (If this string is non-empty
471 * then it will have a newline at the end).
472 * - data is the actual data that will be parsed
473 * further by appropriate routines.
474 * On EOF, the 3 strings will all be set to NULL and
475 * 0 will be returned.
478 * 1 : fp = File to read from
479 * 2 : raw_out = destination for newly malloc'd pointer to
480 * raw line data. May be NULL if you don't want it.
481 * 3 : prefix_out = destination for newly malloc'd pointer to
482 * comments. May be NULL if you don't want it.
483 * 4 : data_out = destination for newly malloc'd pointer to
484 * line data with comments and leading/trailing spaces
485 * removed, and line continuation performed. May be
486 * NULL if you don't want it.
487 * 5 : newline = Standard for newlines in the file.
488 * On input, set to value to use or NEWLINE_UNKNOWN.
489 * On output, may be changed from NEWLINE_UNKNOWN to
490 * actual convention in file. May be NULL if you
492 * 6 : line_number = Line number in file. In "lines" as
493 * reported by a text editor, not lines containing data.
495 * Returns : JB_ERR_OK on success
496 * JB_ERR_MEMORY on out-of-memory
497 * JB_ERR_FILE on EOF.
499 *********************************************************************/
500 jb_err edit_read_line(FILE *fp,
505 unsigned long *line_number)
507 char *p; /* Temporary pointer */
508 char *linebuf; /* Line read from file */
509 char *linestart; /* Start of linebuf, usually first non-whitespace char */
510 int contflag = 0; /* Nonzero for line continuation - i.e. line ends '\' */
511 int is_empty = 1; /* Flag if not got any data yet */
512 char *raw = NULL; /* String to be stored in raw_out */
513 char *prefix = NULL; /* String to be stored in prefix_out */
514 char *data = NULL; /* String to be stored in data_out */
515 int scrapnewline; /* Used for (*newline) if newline==NULL */
516 jb_err rval = JB_ERR_OK;
519 assert(raw_out || data_out);
520 assert(newline == NULL
521 || *newline == NEWLINE_UNKNOWN
522 || *newline == NEWLINE_UNIX
523 || *newline == NEWLINE_DOS
524 || *newline == NEWLINE_MAC);
528 scrapnewline = NEWLINE_UNKNOWN;
529 newline = &scrapnewline;
532 /* Set output parameters to NULL */
546 /* Set string variables to new, empty strings. */
550 raw = strdup_or_die("");
554 prefix = strdup_or_die("");
558 data = strdup_or_die("");
561 /* Main loop. Loop while we need more data & it's not EOF. */
563 while ((contflag || is_empty)
564 && (JB_ERR_OK == (rval = simple_read_line(fp, &linebuf, newline))))
572 string_append(&raw,linebuf);
573 if (string_append(&raw,NEWLINE(*newline)))
578 return JB_ERR_MEMORY;
582 /* Line continuation? Trim escape and set flag. */
583 p = linebuf + strlen(linebuf) - 1;
584 contflag = ((*linebuf != '\0') && (*p == '\\'));
590 /* Trim leading spaces if we're at the start of the line */
592 assert(NULL != data);
595 /* Trim leading spaces */
596 while (*linestart && isspace((int)(unsigned char)*linestart))
602 /* Handle comment characters. */
604 while ((p = strchr(p, '#')) != NULL)
606 /* Found a comment char.. */
607 if ((p != linebuf) && (*(p-1) == '\\'))
609 /* ..and it's escaped, left-shift the line over the escape. */
611 while ((*q = *(q + 1)) != '\0')
615 /* Now scan from just after the "#". */
619 /* Real comment. Save it... */
622 /* Special case: Line only contains a comment, so all the
623 * previous whitespace is considered part of the comment.
624 * Undo the whitespace skipping, if any.
631 string_append(&prefix,p);
632 if (string_append(&prefix, NEWLINE(*newline)))
637 return JB_ERR_MEMORY;
641 /* ... and chop off the rest of the line */
644 } /* END while (there's a # character) */
646 /* Write to the buffer */
650 if (string_append(&data, linestart))
655 return JB_ERR_MEMORY;
660 } /* END while(we need more data) */
662 /* Handle simple_read_line() errors - ignore EOF */
663 if ((rval != JB_ERR_OK) && (rval != JB_ERR_FILE))
671 if (raw ? (*raw == '\0') : is_empty)
673 /* EOF and no data there. (Definition of "data" depends on whether
674 * the caller cares about "raw" or just "data").
685 /* Got at least some data */
687 /* Remove trailing whitespace */
700 *prefix_out = prefix;
719 /*********************************************************************
721 * Function : read_config_line
723 * Description : Read a single non-empty line from a file and return
724 * it. Trims comments, leading and trailing whitespace
725 * and respects escaping of newline and comment char.
728 * 1 : fp = File to read from
729 * 2 : linenum = linenumber in file
730 * 3 : buf = Pointer to a pointer to set to the data buffer.
732 * Returns : NULL on EOF or error
733 * Otherwise, returns buf.
735 *********************************************************************/
736 char *read_config_line(FILE *fp, unsigned long *linenum, char **buf)
739 err = edit_read_line(fp, NULL, NULL, buf, NULL, linenum);
742 if (err == JB_ERR_MEMORY)
744 log_error(LOG_LEVEL_FATAL, "Out of memory loading a config file");
753 /*********************************************************************
755 * Function : unload_trustfile
757 * Description : Unloads a trustfile.
760 * 1 : f = the data structure associated with the trustfile.
764 *********************************************************************/
765 static void unload_trustfile(void *f)
767 struct block_spec *cur = (struct block_spec *)f;
768 struct block_spec *next;
774 free_pattern_spec(cur->url);
783 #ifdef FEATURE_GRACEFUL_TERMINATION
784 /*********************************************************************
786 * Function : unload_current_trust_file
788 * Description : Unloads current trust file - reset to state at
789 * beginning of program.
795 *********************************************************************/
796 void unload_current_trust_file(void)
798 if (current_trustfile)
800 current_trustfile->unloader = unload_trustfile;
801 current_trustfile = NULL;
804 #endif /* FEATURE_GRACEFUL_TERMINATION */
807 /*********************************************************************
809 * Function : load_trustfile
811 * Description : Read and parse a trustfile and add to files list.
814 * 1 : csp = Current client state (buffers, headers, etc...)
816 * Returns : 0 => Ok, everything else is an error.
818 *********************************************************************/
819 int load_trustfile(struct client_state *csp)
823 struct block_spec *b, *bl;
824 struct pattern_spec **tl;
828 struct file_list *fs;
829 unsigned long linenum = 0;
830 int trusted_referrers = 0;
832 if (!check_file_changed(current_trustfile, csp->config->trustfile, &fs))
834 /* No need to load */
835 csp->tlist = current_trustfile;
840 goto load_trustfile_error;
843 fs->f = bl = zalloc_or_die(sizeof(*bl));
845 if ((fp = fopen(csp->config->trustfile, "r")) == NULL)
847 goto load_trustfile_error;
849 log_error(LOG_LEVEL_INFO, "Loading trust file: %s", csp->config->trustfile);
851 tl = csp->config->trust_list;
853 while (read_config_line(fp, &linenum, &buf) != NULL)
872 while ((*p++ = *q++) != '\0')
878 /* skip blank lines */
885 /* allocate a new node */
886 b = zalloc_or_die(sizeof(*b));
888 /* add it to the list */
894 /* Save the URL pattern */
895 if (create_pattern_spec(b->url, buf))
898 goto load_trustfile_error;
902 * save a pointer to URL's spec in the list of trusted URL's, too
906 if (++trusted_referrers < MAX_TRUSTED_REFERRERS)
914 if (trusted_referrers >= MAX_TRUSTED_REFERRERS)
917 * FIXME: ... after Privoxy 3.0.4 is out.
919 log_error(LOG_LEVEL_ERROR, "Too many trusted referrers. Current limit is %d, you are using %d.\n"
920 " Additional trusted referrers are treated like ordinary trusted URLs.\n"
921 " (You can increase this limit by changing MAX_TRUSTED_REFERRERS in project.h and recompiling).",
922 MAX_TRUSTED_REFERRERS, trusted_referrers);
929 /* the old one is now obsolete */
930 if (current_trustfile)
932 current_trustfile->unloader = unload_trustfile;
935 fs->next = files->next;
937 current_trustfile = fs;
942 load_trustfile_error:
943 log_error(LOG_LEVEL_FATAL, "can't load trustfile '%s': %E",
944 csp->config->trustfile);
949 #endif /* def FEATURE_TRUST */
952 /*********************************************************************
954 * Function : unload_re_filterfile
956 * Description : Unload the re_filter list by freeing all chained
957 * re_filterfile specs and their data.
960 * 1 : f = the data structure associated with the filterfile.
964 *********************************************************************/
965 static void unload_re_filterfile(void *f)
967 struct re_filterfile_spec *a, *b = (struct re_filterfile_spec *)f;
973 destroy_list(b->patterns);
974 pcrs_free_joblist(b->joblist);
976 freez(b->description);
985 /*********************************************************************
987 * Function : unload_forward_spec
989 * Description : Unload the forward spec settings by freeing all
990 * memory referenced by members and the memory for
994 * 1 : fwd = the forward spec.
998 *********************************************************************/
999 void unload_forward_spec(struct forward_spec *fwd)
1001 free_pattern_spec(fwd->url);
1002 freez(fwd->gateway_host);
1003 freez(fwd->forward_host);
1004 freez(fwd->auth_username);
1005 freez(fwd->auth_password);
1012 #ifdef FEATURE_GRACEFUL_TERMINATION
1013 /*********************************************************************
1015 * Function : unload_current_re_filterfile
1017 * Description : Unloads current re_filter file - reset to state at
1018 * beginning of program.
1024 *********************************************************************/
1025 void unload_current_re_filterfile(void)
1029 for (i = 0; i < MAX_AF_FILES; i++)
1031 if (current_re_filterfile[i])
1033 current_re_filterfile[i]->unloader = unload_re_filterfile;
1034 current_re_filterfile[i] = NULL;
1041 /*********************************************************************
1043 * Function : load_re_filterfiles
1045 * Description : Loads all the filterfiles.
1046 * Generate a chained list of re_filterfile_spec's from
1047 * the "FILTER: " blocks, compiling all their substitutions
1048 * into chained lists of pcrs_job structs.
1051 * 1 : csp = Current client state (buffers, headers, etc...)
1053 * Returns : 0 => Ok, everything else is an error.
1055 *********************************************************************/
1056 int load_re_filterfiles(struct client_state *csp)
1061 for (i = 0; i < MAX_AF_FILES; i++)
1063 if (csp->config->re_filterfile[i])
1065 result = load_one_re_filterfile(csp, i);
1071 else if (current_re_filterfile[i])
1073 current_re_filterfile[i]->unloader = unload_re_filterfile;
1074 current_re_filterfile[i] = NULL;
1082 /*********************************************************************
1084 * Function : load_one_re_filterfile
1086 * Description : Load a re_filterfile.
1087 * Generate a chained list of re_filterfile_spec's from
1088 * the "FILTER: " blocks, compiling all their substitutions
1089 * into chained lists of pcrs_job structs.
1092 * 1 : csp = Current client state (buffers, headers, etc...)
1094 * Returns : 0 => Ok, everything else is an error.
1096 *********************************************************************/
1097 int load_one_re_filterfile(struct client_state *csp, int fileid)
1101 struct re_filterfile_spec *new_bl, *bl = NULL;
1102 struct file_list *fs;
1105 unsigned long linenum = 0;
1106 pcrs_job *dummy, *lastjob = NULL;
1109 * No need to reload if unchanged
1111 if (!check_file_changed(current_re_filterfile[fileid], csp->config->re_filterfile[fileid], &fs))
1113 csp->rlist[fileid] = current_re_filterfile[fileid];
1118 goto load_re_filterfile_error;
1122 * Open the file or fail
1124 if ((fp = fopen(csp->config->re_filterfile[fileid], "r")) == NULL)
1126 goto load_re_filterfile_error;
1129 log_error(LOG_LEVEL_INFO, "Loading filter file: %s", csp->config->re_filterfile[fileid]);
1134 while (read_config_line(fp, &linenum, &buf) != NULL)
1136 enum filter_type new_filter = FT_INVALID_FILTER;
1138 if (strncmp(buf, "FILTER:", 7) == 0)
1140 new_filter = FT_CONTENT_FILTER;
1142 else if (strncmp(buf, "SERVER-HEADER-FILTER:", 21) == 0)
1144 new_filter = FT_SERVER_HEADER_FILTER;
1146 else if (strncmp(buf, "CLIENT-HEADER-FILTER:", 21) == 0)
1148 new_filter = FT_CLIENT_HEADER_FILTER;
1150 else if (strncmp(buf, "CLIENT-HEADER-TAGGER:", 21) == 0)
1152 new_filter = FT_CLIENT_HEADER_TAGGER;
1154 else if (strncmp(buf, "SERVER-HEADER-TAGGER:", 21) == 0)
1156 new_filter = FT_SERVER_HEADER_TAGGER;
1158 #ifdef FEATURE_EXTERNAL_FILTERS
1159 else if (strncmp(buf, "EXTERNAL-FILTER:", 16) == 0)
1161 new_filter = FT_EXTERNAL_CONTENT_FILTER;
1166 * If this is the head of a new filter block, make it a
1167 * re_filterfile spec of its own and chain it to the list:
1169 if (new_filter != FT_INVALID_FILTER)
1171 new_bl = zalloc_or_die(sizeof(*bl));
1172 if (new_filter == FT_CONTENT_FILTER)
1174 new_bl->name = chomp(buf + 7);
1176 #ifdef FEATURE_EXTERNAL_FILTERS
1177 else if (new_filter == FT_EXTERNAL_CONTENT_FILTER)
1179 new_bl->name = chomp(buf + 16);
1184 new_bl->name = chomp(buf + 21);
1186 new_bl->type = new_filter;
1189 * If a filter description is available,
1190 * encode it to HTML and save it.
1192 if (NULL != (new_bl->description = strpbrk(new_bl->name, " \t")))
1194 *new_bl->description++ = '\0';
1195 new_bl->description = html_encode(chomp(new_bl->description));
1196 if (NULL == new_bl->description)
1198 new_bl->description = strdup_or_die("Out of memory while "
1199 "encoding filter description to HTML");
1204 new_bl->description = strdup_or_die("No description available");
1207 new_bl->name = strdup_or_die(chomp(new_bl->name));
1210 * If this is the first filter block, chain it
1211 * to the file_list rather than its (nonexistant)
1225 log_error(LOG_LEVEL_RE_FILTER, "Reading in filter \"%s\" (\"%s\")", bl->name, bl->description);
1231 #ifdef FEATURE_EXTERNAL_FILTERS
1232 if ((bl != NULL) && (bl->type == FT_EXTERNAL_CONTENT_FILTER))
1235 /* Save the code as "pattern", but do not compile anything. */
1236 if (bl->patterns->first != NULL)
1238 log_error(LOG_LEVEL_FATAL, "External filter '%s' contains several jobss. "
1239 "Did you forget to escape a line break?",
1242 jb_error = enlist(bl->patterns, buf);
1243 if (JB_ERR_MEMORY == jb_error)
1245 log_error(LOG_LEVEL_FATAL,
1246 "Out of memory while enlisting external filter code \'%s\' for filter %s.",
1258 * Save the expression, make it a pcrs_job
1259 * and chain it into the current filter's joblist
1261 jb_error = enlist(bl->patterns, buf);
1262 if (JB_ERR_MEMORY == jb_error)
1264 log_error(LOG_LEVEL_FATAL,
1265 "Out of memory while enlisting re_filter job \'%s\' for filter %s.", buf, bl->name);
1267 assert(JB_ERR_OK == jb_error);
1269 if (pcrs_job_is_dynamic(buf))
1272 * Dynamic pattern that might contain variables
1273 * and has to be recompiled for every request
1275 if (bl->joblist != NULL)
1277 pcrs_free_joblist(bl->joblist);
1281 log_error(LOG_LEVEL_RE_FILTER,
1282 "Adding dynamic re_filter job \'%s\' to filter %s succeeded.", buf, bl->name);
1286 else if (bl->dynamic)
1289 * A previous job was dynamic and as we
1290 * recompile the whole filter anyway, it
1291 * makes no sense to compile this job now.
1293 log_error(LOG_LEVEL_RE_FILTER,
1294 "Adding static re_filter job \'%s\' to dynamic filter %s succeeded.", buf, bl->name);
1299 if ((dummy = pcrs_compile_command(buf, &pcrs_error)) == NULL)
1301 log_error(LOG_LEVEL_ERROR,
1302 "Adding re_filter job \'%s\' to filter %s failed: %s",
1303 buf, bl->name, pcrs_strerror(pcrs_error));
1309 if (bl->joblist == NULL)
1311 bl->joblist = dummy;
1313 else if (NULL != lastjob)
1315 lastjob->next = dummy;
1318 log_error(LOG_LEVEL_RE_FILTER, "Adding re_filter job \'%s\' to filter %s succeeded.", buf, bl->name);
1323 log_error(LOG_LEVEL_ERROR, "Ignoring job %s outside filter block in %s, line %d",
1324 buf, csp->config->re_filterfile[fileid], linenum);
1332 * Schedule the now-obsolete old data for unloading
1334 if (NULL != current_re_filterfile[fileid])
1336 current_re_filterfile[fileid]->unloader = unload_re_filterfile;
1340 * Chain this file into the global list of loaded files
1342 fs->next = files->next;
1344 current_re_filterfile[fileid] = fs;
1345 csp->rlist[fileid] = fs;
1349 load_re_filterfile_error:
1350 log_error(LOG_LEVEL_FATAL, "can't load re_filterfile '%s': %E",
1351 csp->config->re_filterfile[fileid]);
1357 /*********************************************************************
1359 * Function : add_loader
1361 * Description : Called from `load_config'. Called once for each input
1362 * file found in config.
1365 * 1 : loader = pointer to a function that can parse and load
1366 * the appropriate config file.
1367 * 2 : config = The configuration_spec to add the loader to.
1371 *********************************************************************/
1372 void add_loader(int (*loader)(struct client_state *),
1373 struct configuration_spec * config)
1377 for (i = 0; i < NLOADERS; i++)
1379 if (config->loaders[i] == NULL)
1381 config->loaders[i] = loader;
1389 /*********************************************************************
1391 * Function : run_loader
1393 * Description : Called from `load_config' and `listen_loop'. This
1394 * function keeps the "csp" current with any file mods
1395 * since the last loop. If a file is unchanged, the
1396 * loader functions do NOT reload the file.
1399 * 1 : csp = Current client state (buffers, headers, etc...)
1400 * Must be non-null. Reads: "csp->config"
1401 * Writes: various data members.
1403 * Returns : 0 => Ok, everything else is an error.
1405 *********************************************************************/
1406 int run_loader(struct client_state *csp)
1411 for (i = 0; i < NLOADERS; i++)
1413 if (csp->config->loaders[i] == NULL)
1417 ret |= (csp->config->loaders[i])(csp);
1423 /*********************************************************************
1425 * Function : file_has_been_modified
1427 * Description : Helper function to check if a file has been changed
1430 * 1 : filename = The name of the file to check
1431 * 2 : last_known_modification = The time of the last known
1434 * Returns : TRUE if the file has been changed,
1437 *********************************************************************/
1438 static int file_has_been_modified(const char *filename, time_t last_know_modification)
1440 struct stat statbuf[1];
1442 if (stat(filename, statbuf) < 0)
1444 /* Error, probably file not found which counts as change. */
1448 return (last_know_modification != statbuf->st_mtime);
1452 /*********************************************************************
1454 * Function : any_loaded_file_changed
1456 * Description : Helper function to check if any loaded file has been
1457 * changed since the time it has been loaded.
1459 * XXX: Should we cache the return value for x seconds?
1462 * 1 : files_to_check = List of files to check
1464 * Returns : TRUE if any file has been changed,
1467 *********************************************************************/
1468 int any_loaded_file_changed(const struct client_state *csp)
1470 const struct file_list *file_to_check = csp->config->config_file_list;
1473 if (file_has_been_modified(file_to_check->filename, file_to_check->lastmodified))
1478 for (i = 0; i < MAX_AF_FILES; i++)
1480 if (csp->actions_list[i])
1482 file_to_check = csp->actions_list[i];
1483 if (file_has_been_modified(file_to_check->filename, file_to_check->lastmodified))
1490 for (i = 0; i < MAX_AF_FILES; i++)
1494 file_to_check = csp->rlist[i];
1495 if (file_has_been_modified(file_to_check->filename, file_to_check->lastmodified))
1502 #ifdef FEATURE_TRUST
1505 if (file_has_been_modified(csp->tlist->filename, csp->tlist->lastmodified))
1510 #endif /* def FEATURE_TRUST */