1 const char loaders_rcs[] = "$Id: loaders.c,v 1.28 2001/10/07 15:40:39 oes 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 * IJBSWA team. http://ijbswa.sourceforge.net
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.28 2001/10/07 15:40:39 oes
39 * Replaced 6 boolean members of csp with one bitmap (csp->flags)
41 * Revision 1.27 2001/09/22 16:36:59 jongfoster
42 * Removing unused parameter fs from read_config_line()
44 * Revision 1.26 2001/09/22 14:05:22 jongfoster
45 * Bugfix: Multiple escaped "#" characters in a configuration
46 * file are now permitted.
47 * Also removing 3 unused headers.
49 * Revision 1.25 2001/09/13 22:44:03 jongfoster
50 * Adding {} to an if statement
52 * Revision 1.24 2001/07/30 22:08:36 jongfoster
53 * Tidying up #defines:
54 * - All feature #defines are now of the form FEATURE_xxx
55 * - Permanently turned off WIN_GUI_EDIT
56 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
58 * Revision 1.23 2001/07/20 15:51:54 oes
59 * Fixed indentation of prepocessor commands
61 * Revision 1.22 2001/07/20 15:16:17 haroon
62 * - per Guy's suggestion, added a while loop in sweep() to catch not just
63 * the last inactive CSP but all other consecutive inactive CSPs after that
66 * Revision 1.21 2001/07/18 17:26:24 oes
67 * Changed to conform to new pcrs interface
69 * Revision 1.20 2001/07/17 13:07:01 oes
70 * Fixed segv when last line in config files
71 * lacked a terminating (\r)\n
73 * Revision 1.19 2001/07/13 14:01:54 oes
74 * Removed all #ifdef PCRS
76 * Revision 1.18 2001/06/29 21:45:41 oes
77 * Indentation, CRLF->LF, Tab-> Space
79 * Revision 1.17 2001/06/29 13:31:51 oes
82 * Revision 1.16 2001/06/09 10:55:28 jongfoster
83 * Changing BUFSIZ ==> BUFFER_SIZE
85 * Revision 1.15 2001/06/07 23:14:14 jongfoster
86 * Removing ACL and forward file loaders - these
87 * files have been merged into the config file.
88 * Cosmetic: Moving unloader funcs next to their
89 * respective loader funcs
91 * Revision 1.14 2001/06/01 03:27:04 oes
92 * Fixed line continuation problem
94 * Revision 1.13 2001/05/31 21:28:49 jongfoster
95 * Removed all permissionsfile code - it's now called the actions
96 * file, and (almost) all the code is in actions.c
98 * Revision 1.12 2001/05/31 17:32:31 oes
100 * - Enhanced domain part globbing with infix and prefix asterisk
101 * matching and optional unanchored operation
103 * Revision 1.11 2001/05/29 23:25:24 oes
105 * - load_config_line() and load_permissions_file() now use chomp()
107 * Revision 1.10 2001/05/29 09:50:24 jongfoster
108 * Unified blocklist/imagelist/permissionslist.
109 * File format is still under discussion, but the internal changes
112 * Also modified interceptor behaviour:
113 * - We now intercept all URLs beginning with one of the following
114 * prefixes (and *only* these prefixes):
116 * * http://ijbswa.sf.net/config/
117 * * http://ijbswa.sourceforge.net/config/
118 * - New interceptors "home page" - go to http://i.j.b/ to see it.
119 * - Internal changes so that intercepted and fast redirect pages
120 * are not replaced with an image.
121 * - Interceptors now have the option to send a binary page direct
122 * to the client. (i.e. ijb-send-banner uses this)
123 * - Implemented show-url-info interceptor. (Which is why I needed
124 * the above interceptors changes - a typical URL is
125 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
126 * The previous mechanism would not have intercepted that, and
127 * if it had been intercepted then it then it would have replaced
130 * Revision 1.9 2001/05/26 17:12:07 jongfoster
131 * Fatal errors loading configuration files now give better error messages.
133 * Revision 1.8 2001/05/26 00:55:20 jongfoster
134 * Removing duplicated code. load_forwardfile() now uses create_url_spec()
136 * Revision 1.7 2001/05/26 00:28:36 jongfoster
137 * Automatic reloading of config file.
138 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
139 * Most of the global variables have been moved to a new
140 * struct configuration_spec, accessed through csp->config->globalname
141 * Most of the globals remaining are used by the Win32 GUI.
143 * Revision 1.6 2001/05/23 12:27:33 oes
145 * Fixed ugly indentation of my last changes
147 * Revision 1.5 2001/05/23 10:39:05 oes
148 * - Added support for escaping the comment character
149 * in config files by a backslash
150 * - Added support for line continuation in config
152 * - Fixed a buffer overflow bug with long config lines
154 * Revision 1.4 2001/05/22 18:56:28 oes
157 * Revision 1.3 2001/05/20 01:21:20 jongfoster
158 * Version 2.9.4 checkin.
159 * - Merged popupfile and cookiefile, and added control over PCRS
160 * filtering, in new "permissionsfile".
161 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
162 * file error you now get a message box (in the Win32 GUI) rather
163 * than the program exiting with no explanation.
164 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
166 * - Removed tabs from "config"
167 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
168 * - Bumped up version number.
170 * Revision 1.2 2001/05/17 23:01:01 oes
171 * - Cleaned CRLF's from the sources and related files
173 * Revision 1.1.1.1 2001/05/15 13:58:59 oes
174 * Initial import of version 2.9.3 source tree
177 *********************************************************************/
184 #include <sys/types.h>
188 #include <sys/stat.h>
202 #include "miscutil.h"
206 const char loaders_h_rcs[] = LOADERS_H_VERSION;
208 /* Fix a problem with Solaris. There should be no effect on other
210 * Solaris's isspace() is a macro which uses it's argument directly
211 * as an array index. Therefore we need to make sure that high-bit
212 * characters generate +ve values, and ideally we also want to make
213 * the argument match the declared parameter type of "int".
215 #define ijb_isspace(__X) isspace((int)(unsigned char)(__X))
219 * Currently active files.
220 * These are also entered in the main linked list of files.
224 static struct file_list *current_trustfile = NULL;
225 #endif /* def FEATURE_TRUST */
227 static struct file_list *current_re_filterfile = NULL;
231 /*********************************************************************
235 * Description : Basically a mark and sweep garbage collector, it is run
236 * (by the parent thread) every once in a while to reclaim memory.
238 * It uses a mark and sweep strategy:
239 * 1) mark all files as inactive
241 * 2) check with each client:
242 * if it is active, mark its files as active
243 * if it is inactive, free its resources
245 * 3) free the resources of all of the files that
246 * are still marked as inactive (and are obsolete).
248 * N.B. files that are not obsolete don't have an unloader defined.
254 *********************************************************************/
257 struct file_list *fl, *nfl;
258 struct client_state *csp, *ncsp;
260 /* clear all of the file's active flags */
261 for ( fl = files->next; NULL != fl; fl = fl->next )
266 for (csp = clients; csp && (ncsp = csp->next) ; csp = csp->next)
268 if (ncsp->flags & CSP_FLAG_ACTIVE)
270 /* mark this client's files as active */
273 * Always have a configuration file.
274 * (Also note the slightly non-standard extra
277 ncsp->config->config_file_list->active = 1;
279 if (ncsp->actions_list) /* actions files */
281 ncsp->actions_list->active = 1;
284 if (ncsp->rlist) /* pcrsjob files */
286 ncsp->rlist->active = 1;
290 if (ncsp->tlist) /* trust files */
292 ncsp->tlist->active = 1;
294 #endif /* def FEATURE_TRUST */
299 * this client is not active, release its resources
300 * and the ones of all inactive clients that might
304 while (!(ncsp->flags & CSP_FLAG_ACTIVE))
306 csp->next = ncsp->next;
308 freez(ncsp->ip_addr_str);
309 freez(ncsp->my_ip_addr_str);
310 freez(ncsp->my_hostname);
313 freez(ncsp->referrer);
314 #endif /* def FEATURE_TRUST */
315 freez(ncsp->x_forwarded);
316 freez(ncsp->iob->buf);
318 free_http_request(ncsp->http);
320 destroy_list(ncsp->headers);
321 destroy_list(ncsp->cookie_list);
323 free_current_action(ncsp->action);
325 #ifdef FEATURE_STATISTICS
327 if (ncsp->flags & CSP_FLAG_REJECTED)
331 #endif /* def FEATURE_STATISTICS */
335 /* are there any more in sequence after it? */
336 if( !(ncsp = csp->next) )
342 for (fl = files; fl && (nfl = fl->next) ; fl = fl->next)
344 if ( ( 0 == nfl->active ) && ( NULL != nfl->unloader ) )
346 fl->next = nfl->next;
348 (nfl->unloader)(nfl->f);
350 freez(nfl->filename);
359 /*********************************************************************
361 * Function : create_url_spec
363 * Description : Creates a "url_spec" structure from a string.
364 * When finished, free with unload_url().
367 * 1 : url = Target url_spec to be filled in. Must be
368 * zeroed out before the call (e.g. using zalloc).
369 * 2 : buf = Source pattern, null terminated. NOTE: The
370 * contents of this buffer are destroyed by this
371 * function. If this function succeeds, the
372 * buffer is copied to url->spec. If this
373 * function fails, the contents of the buffer
376 * Returns : JB_ERR_OK - Success
377 * JB_ERR_MEMORY - Out of memory
378 * JB_ERR_PARSE - Cannot parse regex (Detailed message
379 * written to system log)
381 *********************************************************************/
382 jb_err create_url_spec(struct url_spec * url, char * buf)
385 struct url_spec tmp_url[1];
390 /* save a copy of the orignal specification */
391 if ((url->spec = strdup(buf)) == NULL)
393 return JB_ERR_MEMORY;
396 if ((p = strchr(buf, '/')))
398 if (NULL == (url->path = strdup(p)))
401 return JB_ERR_MEMORY;
403 url->pathlen = strlen(url->path);
415 char rebuf[BUFFER_SIZE];
417 if (NULL == (url->preg = zalloc(sizeof(*url->preg))))
421 return JB_ERR_MEMORY;
424 sprintf(rebuf, "^(%s)", url->path);
426 errcode = regcomp(url->preg, rebuf,
427 (REG_EXTENDED|REG_NOSUB|REG_ICASE));
430 size_t errlen = regerror(errcode,
431 url->preg, rebuf, sizeof(rebuf));
433 if (errlen > (sizeof(rebuf) - (size_t)1))
435 errlen = sizeof(rebuf) - (size_t)1;
437 rebuf[errlen] = '\0';
439 log_error(LOG_LEVEL_ERROR, "error compiling %s: %s",
450 if ((p = strchr(buf, ':')) == NULL)
460 if ((url->domain = strdup(buf)) == NULL)
466 #endif /* def REGEX */
467 return JB_ERR_MEMORY;
470 /* split domain into components */
472 *tmp_url = dsplit(url->domain);
473 if (tmp_url->dbuf == NULL)
480 #endif /* def REGEX */
481 return JB_ERR_MEMORY;
484 url->dbuf = tmp_url->dbuf;
485 url->dcnt = tmp_url->dcnt;
486 url->dvec = tmp_url->dvec;
487 url->unanchored = tmp_url->unanchored;
494 /*********************************************************************
496 * Function : free_url
498 * Description : Called from the "unloaders". Freez the url
499 * structure elements.
502 * 1 : url = pointer to a url_spec structure.
506 *********************************************************************/
507 void free_url(struct url_spec *url)
509 if (url == NULL) return;
527 /*********************************************************************
529 * Function : check_file_changed
531 * Description : Helper function to check if a file needs reloading.
532 * If "current" is still current, return it. Otherwise
533 * allocates a new (zeroed) "struct file_list", fills
534 * in the disk file name and timestamp, and returns it.
537 * 1 : current = The file_list currently being used - will
538 * be checked to see if it is out of date.
539 * May be NULL (which is treated as out of
541 * 2 : filename = Name of file to check.
542 * 3 : newfl = New file list. [Output only]
543 * This will be set to NULL, OR a struct
544 * file_list newly allocated on the
545 * heap, with the filename and lastmodified
546 * fields filled, and all others zeroed.
548 * Returns : If file unchanged: 0 (and sets newfl == NULL)
549 * If file changed: 1 and sets newfl != NULL
550 * On error: 1 and sets newfl == NULL
552 *********************************************************************/
553 int check_file_changed(const struct file_list * current,
554 const char * filename,
555 struct file_list ** newfl)
557 struct file_list *fs;
558 struct stat statbuf[1];
562 if (stat(filename, statbuf) < 0)
564 /* Error, probably file not found. */
569 && (current->lastmodified == statbuf->st_mtime)
570 && (0 == strcmp(current->filename, filename)))
575 fs = (struct file_list *)zalloc(sizeof(struct file_list));
579 /* Out of memory error */
583 fs->filename = strdup(filename);
584 fs->lastmodified = statbuf->st_mtime;
586 if (fs->filename == NULL)
588 /* Out of memory error */
600 /*********************************************************************
602 * Function : read_config_line
604 * Description : Read a single non-empty line from a file and return
605 * it. Trims comments, leading and trailing whitespace
606 * and respects escaping of newline and comment char.
609 * 1 : buf = Buffer to use.
610 * 2 : buflen = Size of buffer in bytes.
611 * 3 : fp = File to read from
613 * Returns : NULL on EOF or error
614 * Otherwise, returns buf.
616 *********************************************************************/
617 char *read_config_line(char *buf, int buflen, FILE *fp)
622 char linebuf[BUFFER_SIZE];
627 while (fgets(linebuf, sizeof(linebuf), fp))
629 /* Trim off newline */
630 if ((p = strpbrk(linebuf, "\r\n")) != NULL)
636 p = linebuf + strlen(linebuf);
639 /* Line continuation? Trim escape and set flag. */
640 if ((p != linebuf) && (*--p == '\\'))
646 /* If there's a comment char.. */
648 while ((p = strchr(p, '#')) != NULL)
650 /* ..and it's escaped, left-shift the line over the escape. */
651 if ((p != linebuf) && (*(p-1) == '\\'))
655 while ((*dest++ = *src++) != '\0')
659 /* Now scan from just after the "#". */
661 /* Else, chop off the rest of the line */
668 /* Write to the buffer */
671 strncat(buf, linebuf, buflen - strlen(buf));
681 /* Remove leading and trailing whitespace */
697 /*********************************************************************
699 * Function : unload_trustfile
701 * Description : Unloads a trustfile.
704 * 1 : f = the data structure associated with the trustfile.
708 *********************************************************************/
709 static void unload_trustfile(void *f)
711 struct block_spec *b = (struct block_spec *)f;
712 if (b == NULL) return;
714 unload_trustfile(b->next); /* Stack is cheap, isn't it? */
723 /*********************************************************************
725 * Function : load_trustfile
727 * Description : Read and parse a trustfile and add to files list.
730 * 1 : csp = Current client state (buffers, headers, etc...)
732 * Returns : 0 => Ok, everything else is an error.
734 *********************************************************************/
735 int load_trustfile(struct client_state *csp)
739 struct block_spec *b, *bl;
740 struct url_spec **tl;
742 char buf[BUFFER_SIZE], *p, *q;
744 struct file_list *fs;
746 if (!check_file_changed(current_trustfile, csp->config->trustfile, &fs))
748 /* No need to load */
751 csp->tlist = current_trustfile;
757 goto load_trustfile_error;
760 fs->f = bl = (struct block_spec *)zalloc(sizeof(*bl));
763 goto load_trustfile_error;
766 if ((fp = fopen(csp->config->trustfile, "r")) == NULL)
768 goto load_trustfile_error;
771 tl = csp->config->trust_list;
773 while (read_config_line(buf, sizeof(buf), fp) != NULL)
789 while ((*p++ = *q++))
795 /* skip blank lines */
801 /* allocate a new node */
802 if ((b = zalloc(sizeof(*b))) == NULL)
805 goto load_trustfile_error;
808 /* add it to the list */
814 /* Save the URL pattern */
815 if (create_url_spec(b->url, buf))
818 goto load_trustfile_error;
822 * save a pointer to URL's spec in the list of trusted URL's, too
834 /* the old one is now obsolete */
835 if (current_trustfile)
837 current_trustfile->unloader = unload_trustfile;
840 fs->next = files->next;
842 current_trustfile = fs;
851 load_trustfile_error:
852 log_error(LOG_LEVEL_FATAL, "can't load trustfile '%s': %E",
853 csp->config->trustfile);
857 #endif /* def FEATURE_TRUST */
860 /*********************************************************************
862 * Function : unload_re_filterfile
864 * Description : Unload the re_filter list.
867 * 1 : f = the data structure associated with the filterfile.
871 *********************************************************************/
872 static void unload_re_filterfile(void *f)
874 struct re_filterfile_spec *b = (struct re_filterfile_spec *)f;
881 destroy_list(b->patterns);
882 pcrs_free_joblist(b->joblist);
888 /*********************************************************************
890 * Function : load_re_filterfile
892 * Description : Load the re_filterfile. Each non-comment, non-empty
893 * line is instantly added to the joblist, which is
894 * a chained list of pcrs_job structs.
897 * 1 : csp = Current client state (buffers, headers, etc...)
899 * Returns : 0 => Ok, everything else is an error.
901 *********************************************************************/
902 int load_re_filterfile(struct client_state *csp)
906 struct re_filterfile_spec *bl;
907 struct file_list *fs;
909 char buf[BUFFER_SIZE];
913 if (!check_file_changed(current_re_filterfile, csp->config->re_filterfile, &fs))
915 /* No need to load */
918 csp->rlist = current_re_filterfile;
924 goto load_re_filterfile_error;
927 fs->f = bl = (struct re_filterfile_spec *)zalloc(sizeof(*bl));
930 goto load_re_filterfile_error;
933 /* Open the file or fail */
934 if ((fp = fopen(csp->config->re_filterfile, "r")) == NULL)
936 goto load_re_filterfile_error;
939 /* Read line by line */
940 while (read_config_line(buf, sizeof(buf), fp) != NULL)
942 enlist( bl->patterns, buf );
944 /* We have a meaningful line -> make it a job */
945 if ((dummy = pcrs_compile_command(buf, &error)) == NULL)
947 log_error(LOG_LEVEL_RE_FILTER,
948 "Adding re_filter job %s failed with error %d.", buf, error);
953 dummy->next = bl->joblist;
955 log_error(LOG_LEVEL_RE_FILTER, "Adding re_filter job %s succeeded.", buf);
961 /* the old one is now obsolete */
962 if ( NULL != current_re_filterfile )
964 current_re_filterfile->unloader = unload_re_filterfile;
967 fs->next = files->next;
969 current_re_filterfile = fs;
978 load_re_filterfile_error:
979 log_error(LOG_LEVEL_FATAL, "can't load re_filterfile '%s': %E",
980 csp->config->re_filterfile);
986 /*********************************************************************
988 * Function : add_loader
990 * Description : Called from `load_config'. Called once for each input
991 * file found in config.
994 * 1 : loader = pointer to a function that can parse and load
995 * the appropriate config file.
996 * 2 : config = The configuration_spec to add the loader to.
1000 *********************************************************************/
1001 void add_loader(int (*loader)(struct client_state *),
1002 struct configuration_spec * config)
1006 for (i=0; i < NLOADERS; i++)
1008 if (config->loaders[i] == NULL)
1010 config->loaders[i] = loader;
1018 /*********************************************************************
1020 * Function : run_loader
1022 * Description : Called from `load_config' and `listen_loop'. This
1023 * function keeps the "csp" current with any file mods
1024 * since the last loop. If a file is unchanged, the
1025 * loader functions do NOT reload the file.
1028 * 1 : csp = Current client state (buffers, headers, etc...)
1029 * Must be non-null. Reads: "csp->config"
1030 * Writes: various data members.
1032 * Returns : 0 => Ok, everything else is an error.
1034 *********************************************************************/
1035 int run_loader(struct client_state *csp)
1040 for (i=0; i < NLOADERS; i++)
1042 if (csp->config->loaders[i] == NULL)
1046 ret |= (csp->config->loaders[i])(csp);