1 const char loaders_rcs[] = "$Id: loaders.c,v 1.108 2017/06/26 12:17:57 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-2014 the
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.
36 *********************************************************************/
43 #include <sys/types.h>
50 #if !defined(_WIN32) && !defined(__OS2__)
66 const char loaders_h_rcs[] = LOADERS_H_VERSION;
69 * Currently active files.
70 * These are also entered in the main linked list of files.
74 static struct file_list *current_trustfile = NULL;
75 #endif /* def FEATURE_TRUST */
78 static int load_one_re_filterfile(struct client_state *csp, int fileid);
81 static struct file_list *current_re_filterfile[MAX_AF_FILES] = {
82 NULL, NULL, NULL, NULL, NULL,
83 NULL, NULL, NULL, NULL, NULL
86 /*********************************************************************
88 * Function : free_csp_resources
90 * Description : Frees memory referenced by the csp that isn't
91 * shared with other csps.
94 * 1 : csp = Current client state (buffers, headers, etc...)
98 *********************************************************************/
99 void free_csp_resources(struct client_state *csp)
101 freez(csp->ip_addr_str);
102 #ifdef FEATURE_CLIENT_TAGS
103 freez(csp->client_address);
105 freez(csp->listen_addr_str);
106 freez(csp->client_iob->buf);
107 freez(csp->iob->buf);
108 freez(csp->error_message);
110 if (csp->action->flags & ACTION_FORWARD_OVERRIDE &&
113 unload_forward_spec(csp->fwd);
115 free_http_request(csp->http);
117 destroy_list(csp->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);
1010 #ifdef FEATURE_GRACEFUL_TERMINATION
1011 /*********************************************************************
1013 * Function : unload_current_re_filterfile
1015 * Description : Unloads current re_filter file - reset to state at
1016 * beginning of program.
1022 *********************************************************************/
1023 void unload_current_re_filterfile(void)
1027 for (i = 0; i < MAX_AF_FILES; i++)
1029 if (current_re_filterfile[i])
1031 current_re_filterfile[i]->unloader = unload_re_filterfile;
1032 current_re_filterfile[i] = NULL;
1039 /*********************************************************************
1041 * Function : load_re_filterfiles
1043 * Description : Loads all the filterfiles.
1044 * Generate a chained list of re_filterfile_spec's from
1045 * the "FILTER: " blocks, compiling all their substitutions
1046 * into chained lists of pcrs_job structs.
1049 * 1 : csp = Current client state (buffers, headers, etc...)
1051 * Returns : 0 => Ok, everything else is an error.
1053 *********************************************************************/
1054 int load_re_filterfiles(struct client_state *csp)
1059 for (i = 0; i < MAX_AF_FILES; i++)
1061 if (csp->config->re_filterfile[i])
1063 result = load_one_re_filterfile(csp, i);
1069 else if (current_re_filterfile[i])
1071 current_re_filterfile[i]->unloader = unload_re_filterfile;
1072 current_re_filterfile[i] = NULL;
1080 /*********************************************************************
1082 * Function : load_one_re_filterfile
1084 * Description : Load a re_filterfile.
1085 * Generate a chained list of re_filterfile_spec's from
1086 * the "FILTER: " blocks, compiling all their substitutions
1087 * into chained lists of pcrs_job structs.
1090 * 1 : csp = Current client state (buffers, headers, etc...)
1092 * Returns : 0 => Ok, everything else is an error.
1094 *********************************************************************/
1095 int load_one_re_filterfile(struct client_state *csp, int fileid)
1099 struct re_filterfile_spec *new_bl, *bl = NULL;
1100 struct file_list *fs;
1104 unsigned long linenum = 0;
1105 pcrs_job *dummy, *lastjob = NULL;
1108 * No need to reload if unchanged
1110 if (!check_file_changed(current_re_filterfile[fileid], csp->config->re_filterfile[fileid], &fs))
1112 csp->rlist[fileid] = current_re_filterfile[fileid];
1117 goto load_re_filterfile_error;
1121 * Open the file or fail
1123 if ((fp = fopen(csp->config->re_filterfile[fileid], "r")) == NULL)
1125 goto load_re_filterfile_error;
1128 log_error(LOG_LEVEL_INFO, "Loading filter file: %s", csp->config->re_filterfile[fileid]);
1133 while (read_config_line(fp, &linenum, &buf) != NULL)
1135 enum filter_type new_filter = FT_INVALID_FILTER;
1137 if (strncmp(buf, "FILTER:", 7) == 0)
1139 new_filter = FT_CONTENT_FILTER;
1141 else if (strncmp(buf, "SERVER-HEADER-FILTER:", 21) == 0)
1143 new_filter = FT_SERVER_HEADER_FILTER;
1145 else if (strncmp(buf, "CLIENT-HEADER-FILTER:", 21) == 0)
1147 new_filter = FT_CLIENT_HEADER_FILTER;
1149 else if (strncmp(buf, "CLIENT-HEADER-TAGGER:", 21) == 0)
1151 new_filter = FT_CLIENT_HEADER_TAGGER;
1153 else if (strncmp(buf, "SERVER-HEADER-TAGGER:", 21) == 0)
1155 new_filter = FT_SERVER_HEADER_TAGGER;
1157 #ifdef FEATURE_EXTERNAL_FILTERS
1158 else if (strncmp(buf, "EXTERNAL-FILTER:", 16) == 0)
1160 new_filter = FT_EXTERNAL_CONTENT_FILTER;
1165 * If this is the head of a new filter block, make it a
1166 * re_filterfile spec of its own and chain it to the list:
1168 if (new_filter != FT_INVALID_FILTER)
1170 new_bl = zalloc_or_die(sizeof(*bl));
1171 if (new_filter == FT_CONTENT_FILTER)
1173 new_bl->name = chomp(buf + 7);
1175 #ifdef FEATURE_EXTERNAL_FILTERS
1176 else if (new_filter == FT_EXTERNAL_CONTENT_FILTER)
1178 new_bl->name = chomp(buf + 16);
1183 new_bl->name = chomp(buf + 21);
1185 new_bl->type = new_filter;
1188 * If a filter description is available,
1189 * encode it to HTML and save it.
1191 if (NULL != (new_bl->description = strpbrk(new_bl->name, " \t")))
1193 *new_bl->description++ = '\0';
1194 new_bl->description = html_encode(chomp(new_bl->description));
1195 if (NULL == new_bl->description)
1197 new_bl->description = strdup_or_die("Out of memory while "
1198 "encoding filter description to HTML");
1203 new_bl->description = strdup_or_die("No description available");
1206 new_bl->name = strdup_or_die(chomp(new_bl->name));
1209 * If this is the first filter block, chain it
1210 * to the file_list rather than its (nonexistant)
1224 log_error(LOG_LEVEL_RE_FILTER, "Reading in filter \"%s\" (\"%s\")", bl->name, bl->description);
1230 #ifdef FEATURE_EXTERNAL_FILTERS
1231 if ((bl != NULL) && (bl->type == FT_EXTERNAL_CONTENT_FILTER))
1233 /* Save the code as "pattern", but do not compile anything. */
1234 if (bl->patterns->first != NULL)
1236 log_error(LOG_LEVEL_FATAL, "External filter '%s' contains several jobss. "
1237 "Did you forget to escape a line break?",
1240 error = enlist(bl->patterns, buf);
1241 if (JB_ERR_MEMORY == error)
1243 log_error(LOG_LEVEL_FATAL,
1244 "Out of memory while enlisting external filter code \'%s\' for filter %s.",
1254 * Save the expression, make it a pcrs_job
1255 * and chain it into the current filter's joblist
1257 error = enlist(bl->patterns, buf);
1258 if (JB_ERR_MEMORY == error)
1260 log_error(LOG_LEVEL_FATAL,
1261 "Out of memory while enlisting re_filter job \'%s\' for filter %s.", buf, bl->name);
1263 assert(JB_ERR_OK == error);
1265 if (pcrs_job_is_dynamic(buf))
1268 * Dynamic pattern that might contain variables
1269 * and has to be recompiled for every request
1271 if (bl->joblist != NULL)
1273 pcrs_free_joblist(bl->joblist);
1277 log_error(LOG_LEVEL_RE_FILTER,
1278 "Adding dynamic re_filter job \'%s\' to filter %s succeeded.", buf, bl->name);
1282 else if (bl->dynamic)
1285 * A previous job was dynamic and as we
1286 * recompile the whole filter anyway, it
1287 * makes no sense to compile this job now.
1289 log_error(LOG_LEVEL_RE_FILTER,
1290 "Adding static re_filter job \'%s\' to dynamic filter %s succeeded.", buf, bl->name);
1295 if ((dummy = pcrs_compile_command(buf, &error)) == NULL)
1297 log_error(LOG_LEVEL_ERROR,
1298 "Adding re_filter job \'%s\' to filter %s failed: %s",
1299 buf, bl->name, pcrs_strerror(error));
1305 if (bl->joblist == NULL)
1307 bl->joblist = dummy;
1309 else if (NULL != lastjob)
1311 lastjob->next = dummy;
1314 log_error(LOG_LEVEL_RE_FILTER, "Adding re_filter job \'%s\' to filter %s succeeded.", buf, bl->name);
1319 log_error(LOG_LEVEL_ERROR, "Ignoring job %s outside filter block in %s, line %d",
1320 buf, csp->config->re_filterfile[fileid], linenum);
1328 * Schedule the now-obsolete old data for unloading
1330 if (NULL != current_re_filterfile[fileid])
1332 current_re_filterfile[fileid]->unloader = unload_re_filterfile;
1336 * Chain this file into the global list of loaded files
1338 fs->next = files->next;
1340 current_re_filterfile[fileid] = fs;
1341 csp->rlist[fileid] = fs;
1345 load_re_filterfile_error:
1346 log_error(LOG_LEVEL_FATAL, "can't load re_filterfile '%s': %E",
1347 csp->config->re_filterfile[fileid]);
1353 /*********************************************************************
1355 * Function : add_loader
1357 * Description : Called from `load_config'. Called once for each input
1358 * file found in config.
1361 * 1 : loader = pointer to a function that can parse and load
1362 * the appropriate config file.
1363 * 2 : config = The configuration_spec to add the loader to.
1367 *********************************************************************/
1368 void add_loader(int (*loader)(struct client_state *),
1369 struct configuration_spec * config)
1373 for (i = 0; i < NLOADERS; i++)
1375 if (config->loaders[i] == NULL)
1377 config->loaders[i] = loader;
1385 /*********************************************************************
1387 * Function : run_loader
1389 * Description : Called from `load_config' and `listen_loop'. This
1390 * function keeps the "csp" current with any file mods
1391 * since the last loop. If a file is unchanged, the
1392 * loader functions do NOT reload the file.
1395 * 1 : csp = Current client state (buffers, headers, etc...)
1396 * Must be non-null. Reads: "csp->config"
1397 * Writes: various data members.
1399 * Returns : 0 => Ok, everything else is an error.
1401 *********************************************************************/
1402 int run_loader(struct client_state *csp)
1407 for (i = 0; i < NLOADERS; i++)
1409 if (csp->config->loaders[i] == NULL)
1413 ret |= (csp->config->loaders[i])(csp);
1419 /*********************************************************************
1421 * Function : file_has_been_modified
1423 * Description : Helper function to check if a file has been changed
1426 * 1 : filename = The name of the file to check
1427 * 2 : last_known_modification = The time of the last known
1430 * Returns : TRUE if the file has been changed,
1433 *********************************************************************/
1434 static int file_has_been_modified(const char *filename, time_t last_know_modification)
1436 struct stat statbuf[1];
1438 if (stat(filename, statbuf) < 0)
1440 /* Error, probably file not found which counts as change. */
1444 return (last_know_modification != statbuf->st_mtime);
1448 /*********************************************************************
1450 * Function : any_loaded_file_changed
1452 * Description : Helper function to check if any loaded file has been
1453 * changed since the time it has been loaded.
1455 * XXX: Should we cache the return value for x seconds?
1458 * 1 : files_to_check = List of files to check
1460 * Returns : TRUE if any file has been changed,
1463 *********************************************************************/
1464 int any_loaded_file_changed(const struct client_state *csp)
1466 const struct file_list *file_to_check = csp->config->config_file_list;
1469 if (file_has_been_modified(file_to_check->filename, file_to_check->lastmodified))
1474 for (i = 0; i < MAX_AF_FILES; i++)
1476 if (csp->actions_list[i])
1478 file_to_check = csp->actions_list[i];
1479 if (file_has_been_modified(file_to_check->filename, file_to_check->lastmodified))
1486 for (i = 0; i < MAX_AF_FILES; i++)
1490 file_to_check = csp->rlist[i];
1491 if (file_has_been_modified(file_to_check->filename, file_to_check->lastmodified))
1498 #ifdef FEATURE_TRUST
1501 if (file_has_been_modified(csp->tlist->filename, csp->tlist->lastmodified))
1506 #endif /* def FEATURE_TRUST */