1 const char loaders_rcs[] = "$Id: loaders.c,v 1.34 2001/12/30 14:07:32 steudten 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.34 2001/12/30 14:07:32 steudten
39 * - Add signal handling (unix)
40 * - Add SIGHUP handler (unix)
41 * - Add creation of pidfile (unix)
42 * - Add action 'top' in rc file (RH)
43 * - Add entry 'SIGNALS' to manpage
44 * - Add exit message to logfile (unix)
46 * Revision 1.33 2001/11/13 00:16:38 jongfoster
47 * Replacing references to malloc.h with the standard stdlib.h
48 * (See ANSI or K&R 2nd Ed)
50 * Revision 1.32 2001/11/07 00:02:13 steudten
51 * Add line number in error output for lineparsing for
52 * actionsfile and configfile.
53 * Special handling for CLF added.
55 * Revision 1.31 2001/10/26 17:39:01 oes
56 * Removed csp->referrer
57 * Moved ijb_isspace and ijb_tolower to project.h
59 * Revision 1.30 2001/10/25 03:40:48 david__schmidt
60 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
61 * threads to call select() simultaneously. So, it's time to do a real, live,
62 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
63 * (native). Both versions will work, but using __OS2__ offers multi-threading.
65 * Revision 1.29 2001/10/23 21:38:53 jongfoster
66 * Adding error-checking to create_url_spec()
68 * Revision 1.28 2001/10/07 15:40:39 oes
69 * Replaced 6 boolean members of csp with one bitmap (csp->flags)
71 * Revision 1.27 2001/09/22 16:36:59 jongfoster
72 * Removing unused parameter fs from read_config_line()
74 * Revision 1.26 2001/09/22 14:05:22 jongfoster
75 * Bugfix: Multiple escaped "#" characters in a configuration
76 * file are now permitted.
77 * Also removing 3 unused headers.
79 * Revision 1.25 2001/09/13 22:44:03 jongfoster
80 * Adding {} to an if statement
82 * Revision 1.24 2001/07/30 22:08:36 jongfoster
83 * Tidying up #defines:
84 * - All feature #defines are now of the form FEATURE_xxx
85 * - Permanently turned off WIN_GUI_EDIT
86 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
88 * Revision 1.23 2001/07/20 15:51:54 oes
89 * Fixed indentation of prepocessor commands
91 * Revision 1.22 2001/07/20 15:16:17 haroon
92 * - per Guy's suggestion, added a while loop in sweep() to catch not just
93 * the last inactive CSP but all other consecutive inactive CSPs after that
96 * Revision 1.21 2001/07/18 17:26:24 oes
97 * Changed to conform to new pcrs interface
99 * Revision 1.20 2001/07/17 13:07:01 oes
100 * Fixed segv when last line in config files
101 * lacked a terminating (\r)\n
103 * Revision 1.19 2001/07/13 14:01:54 oes
104 * Removed all #ifdef PCRS
106 * Revision 1.18 2001/06/29 21:45:41 oes
107 * Indentation, CRLF->LF, Tab-> Space
109 * Revision 1.17 2001/06/29 13:31:51 oes
112 * Revision 1.16 2001/06/09 10:55:28 jongfoster
113 * Changing BUFSIZ ==> BUFFER_SIZE
115 * Revision 1.15 2001/06/07 23:14:14 jongfoster
116 * Removing ACL and forward file loaders - these
117 * files have been merged into the config file.
118 * Cosmetic: Moving unloader funcs next to their
119 * respective loader funcs
121 * Revision 1.14 2001/06/01 03:27:04 oes
122 * Fixed line continuation problem
124 * Revision 1.13 2001/05/31 21:28:49 jongfoster
125 * Removed all permissionsfile code - it's now called the actions
126 * file, and (almost) all the code is in actions.c
128 * Revision 1.12 2001/05/31 17:32:31 oes
130 * - Enhanced domain part globbing with infix and prefix asterisk
131 * matching and optional unanchored operation
133 * Revision 1.11 2001/05/29 23:25:24 oes
135 * - load_config_line() and load_permissions_file() now use chomp()
137 * Revision 1.10 2001/05/29 09:50:24 jongfoster
138 * Unified blocklist/imagelist/permissionslist.
139 * File format is still under discussion, but the internal changes
142 * Also modified interceptor behaviour:
143 * - We now intercept all URLs beginning with one of the following
144 * prefixes (and *only* these prefixes):
146 * * http://ijbswa.sf.net/config/
147 * * http://ijbswa.sourceforge.net/config/
148 * - New interceptors "home page" - go to http://i.j.b/ to see it.
149 * - Internal changes so that intercepted and fast redirect pages
150 * are not replaced with an image.
151 * - Interceptors now have the option to send a binary page direct
152 * to the client. (i.e. ijb-send-banner uses this)
153 * - Implemented show-url-info interceptor. (Which is why I needed
154 * the above interceptors changes - a typical URL is
155 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
156 * The previous mechanism would not have intercepted that, and
157 * if it had been intercepted then it then it would have replaced
160 * Revision 1.9 2001/05/26 17:12:07 jongfoster
161 * Fatal errors loading configuration files now give better error messages.
163 * Revision 1.8 2001/05/26 00:55:20 jongfoster
164 * Removing duplicated code. load_forwardfile() now uses create_url_spec()
166 * Revision 1.7 2001/05/26 00:28:36 jongfoster
167 * Automatic reloading of config file.
168 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
169 * Most of the global variables have been moved to a new
170 * struct configuration_spec, accessed through csp->config->globalname
171 * Most of the globals remaining are used by the Win32 GUI.
173 * Revision 1.6 2001/05/23 12:27:33 oes
175 * Fixed ugly indentation of my last changes
177 * Revision 1.5 2001/05/23 10:39:05 oes
178 * - Added support for escaping the comment character
179 * in config files by a backslash
180 * - Added support for line continuation in config
182 * - Fixed a buffer overflow bug with long config lines
184 * Revision 1.4 2001/05/22 18:56:28 oes
187 * Revision 1.3 2001/05/20 01:21:20 jongfoster
188 * Version 2.9.4 checkin.
189 * - Merged popupfile and cookiefile, and added control over PCRS
190 * filtering, in new "permissionsfile".
191 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
192 * file error you now get a message box (in the Win32 GUI) rather
193 * than the program exiting with no explanation.
194 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
196 * - Removed tabs from "config"
197 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
198 * - Bumped up version number.
200 * Revision 1.2 2001/05/17 23:01:01 oes
201 * - Cleaned CRLF's from the sources and related files
203 * Revision 1.1.1.1 2001/05/15 13:58:59 oes
204 * Initial import of version 2.9.3 source tree
207 *********************************************************************/
214 #include <sys/types.h>
217 #include <sys/stat.h>
221 #if !defined(_WIN32) && !defined(__OS2__)
231 #include "miscutil.h"
234 #include "urlmatch.h"
236 const char loaders_h_rcs[] = LOADERS_H_VERSION;
239 * Currently active files.
240 * These are also entered in the main linked list of files.
244 static struct file_list *current_trustfile = NULL;
245 #endif /* def FEATURE_TRUST */
247 static struct file_list *current_re_filterfile = NULL;
251 /*********************************************************************
255 * Description : Basically a mark and sweep garbage collector, it is run
256 * (by the parent thread) every once in a while to reclaim memory.
258 * It uses a mark and sweep strategy:
259 * 1) mark all files as inactive
261 * 2) check with each client:
262 * if it is active, mark its files as active
263 * if it is inactive, free its resources
265 * 3) free the resources of all of the files that
266 * are still marked as inactive (and are obsolete).
268 * N.B. files that are not obsolete don't have an unloader defined.
274 *********************************************************************/
277 struct file_list *fl, *nfl;
278 struct client_state *csp, *ncsp;
280 /* clear all of the file's active flags */
281 for ( fl = files->next; NULL != fl; fl = fl->next )
286 for (csp = clients; csp && (ncsp = csp->next) ; csp = csp->next)
288 if (ncsp->flags & CSP_FLAG_ACTIVE)
290 /* mark this client's files as active */
293 * Always have a configuration file.
294 * (Also note the slightly non-standard extra
297 ncsp->config->config_file_list->active = 1;
299 if (ncsp->actions_list) /* actions files */
301 ncsp->actions_list->active = 1;
304 if (ncsp->rlist) /* pcrsjob files */
306 ncsp->rlist->active = 1;
310 if (ncsp->tlist) /* trust files */
312 ncsp->tlist->active = 1;
314 #endif /* def FEATURE_TRUST */
319 * this client is not active, release its resources
320 * and the ones of all inactive clients that might
324 while (!(ncsp->flags & CSP_FLAG_ACTIVE))
326 csp->next = ncsp->next;
328 freez(ncsp->ip_addr_str);
329 freez(ncsp->my_ip_addr_str);
330 freez(ncsp->my_hostname);
331 freez(ncsp->x_forwarded);
332 freez(ncsp->iob->buf);
334 free_http_request(ncsp->http);
336 destroy_list(ncsp->headers);
337 destroy_list(ncsp->cookie_list);
339 free_current_action(ncsp->action);
341 #ifdef FEATURE_STATISTICS
343 if (ncsp->flags & CSP_FLAG_REJECTED)
347 #endif /* def FEATURE_STATISTICS */
351 /* are there any more in sequence after it? */
352 if( !(ncsp = csp->next) )
358 for (fl = files; fl && (nfl = fl->next) ; fl = fl->next)
360 if ( ( 0 == nfl->active ) && ( NULL != nfl->unloader ) )
362 fl->next = nfl->next;
364 (nfl->unloader)(nfl->f);
366 freez(nfl->filename);
375 /*********************************************************************
377 * Function : check_file_changed
379 * Description : Helper function to check if a file needs reloading.
380 * If "current" is still current, return it. Otherwise
381 * allocates a new (zeroed) "struct file_list", fills
382 * in the disk file name and timestamp, and returns it.
385 * 1 : current = The file_list currently being used - will
386 * be checked to see if it is out of date.
387 * May be NULL (which is treated as out of
389 * 2 : filename = Name of file to check.
390 * 3 : newfl = New file list. [Output only]
391 * This will be set to NULL, OR a struct
392 * file_list newly allocated on the
393 * heap, with the filename and lastmodified
394 * fields filled, and all others zeroed.
396 * Returns : If file unchanged: 0 (and sets newfl == NULL)
397 * If file changed: 1 and sets newfl != NULL
398 * On error: 1 and sets newfl == NULL
400 *********************************************************************/
401 int check_file_changed(const struct file_list * current,
402 const char * filename,
403 struct file_list ** newfl)
405 struct file_list *fs;
406 struct stat statbuf[1];
410 if (stat(filename, statbuf) < 0)
412 /* Error, probably file not found. */
417 && (current->lastmodified == statbuf->st_mtime)
418 && (0 == strcmp(current->filename, filename)))
420 /* force reload of configfile and all the logs */
421 if ( !MustReload ) return 0;
424 fs = (struct file_list *)zalloc(sizeof(struct file_list));
427 /* Out of memory error */
431 fs->filename = strdup(filename);
432 fs->lastmodified = statbuf->st_mtime;
434 if (fs->filename == NULL)
436 /* Out of memory error */
445 /*********************************************************************
447 * Function : read_config_line
449 * Description : Read a single non-empty line from a file and return
450 * it. Trims comments, leading and trailing whitespace
451 * and respects escaping of newline and comment char.
454 * 1 : buf = Buffer to use.
455 * 2 : buflen = Size of buffer in bytes.
456 * 3 : fp = File to read from
457 * 4 : linenum = linenumber in file
459 * Returns : NULL on EOF or error
460 * Otherwise, returns buf.
462 *********************************************************************/
463 char *read_config_line(char *buf, int buflen, FILE *fp, unsigned long *linenum)
468 char linebuf[BUFFER_SIZE];
473 while (fgets(linebuf, sizeof(linebuf), fp))
476 /* Trim off newline */
477 if ((p = strpbrk(linebuf, "\r\n")) != NULL)
483 p = linebuf + strlen(linebuf);
486 /* Line continuation? Trim escape and set flag. */
487 if ((p != linebuf) && (*--p == '\\'))
493 /* If there's a comment char.. */
495 while ((p = strchr(p, '#')) != NULL)
497 /* ..and it's escaped, left-shift the line over the escape. */
498 if ((p != linebuf) && (*(p-1) == '\\'))
502 while ((*dest++ = *src++) != '\0')
506 /* Now scan from just after the "#". */
508 /* Else, chop off the rest of the line */
515 /* Write to the buffer */
518 strncat(buf, linebuf, buflen - strlen(buf));
528 /* Remove leading and trailing whitespace */
544 /*********************************************************************
546 * Function : unload_trustfile
548 * Description : Unloads a trustfile.
551 * 1 : f = the data structure associated with the trustfile.
555 *********************************************************************/
556 static void unload_trustfile(void *f)
558 struct block_spec *b = (struct block_spec *)f;
559 if (b == NULL) return;
561 unload_trustfile(b->next); /* Stack is cheap, isn't it? */
563 free_url_spec(b->url);
570 /*********************************************************************
572 * Function : load_trustfile
574 * Description : Read and parse a trustfile and add to files list.
577 * 1 : csp = Current client state (buffers, headers, etc...)
579 * Returns : 0 => Ok, everything else is an error.
581 *********************************************************************/
582 int load_trustfile(struct client_state *csp)
586 struct block_spec *b, *bl;
587 struct url_spec **tl;
589 char buf[BUFFER_SIZE], *p, *q;
591 struct file_list *fs;
592 unsigned long linenum = 0;
594 if (!check_file_changed(current_trustfile, csp->config->trustfile, &fs))
596 /* No need to load */
599 csp->tlist = current_trustfile;
605 goto load_trustfile_error;
608 fs->f = bl = (struct block_spec *)zalloc(sizeof(*bl));
611 goto load_trustfile_error;
614 if ((fp = fopen(csp->config->trustfile, "r")) == NULL)
616 goto load_trustfile_error;
619 tl = csp->config->trust_list;
621 while (read_config_line(buf, sizeof(buf), fp, &linenum) != NULL)
637 while ((*p++ = *q++))
643 /* skip blank lines */
649 /* allocate a new node */
650 if ((b = zalloc(sizeof(*b))) == NULL)
653 goto load_trustfile_error;
656 /* add it to the list */
662 /* Save the URL pattern */
663 if (create_url_spec(b->url, buf))
666 goto load_trustfile_error;
670 * save a pointer to URL's spec in the list of trusted URL's, too
675 /* FIXME BUFFER OVERFLOW if >=64 entries */
683 /* the old one is now obsolete */
684 if (current_trustfile)
686 current_trustfile->unloader = unload_trustfile;
689 fs->next = files->next;
691 current_trustfile = fs;
700 load_trustfile_error:
701 log_error(LOG_LEVEL_FATAL, "can't load trustfile '%s': %E",
702 csp->config->trustfile);
706 #endif /* def FEATURE_TRUST */
709 /*********************************************************************
711 * Function : unload_re_filterfile
713 * Description : Unload the re_filter list.
716 * 1 : f = the data structure associated with the filterfile.
720 *********************************************************************/
721 static void unload_re_filterfile(void *f)
723 struct re_filterfile_spec *b = (struct re_filterfile_spec *)f;
730 destroy_list(b->patterns);
731 pcrs_free_joblist(b->joblist);
737 /*********************************************************************
739 * Function : load_re_filterfile
741 * Description : Load the re_filterfile. Each non-comment, non-empty
742 * line is instantly added to the joblist, which is
743 * a chained list of pcrs_job structs.
746 * 1 : csp = Current client state (buffers, headers, etc...)
748 * Returns : 0 => Ok, everything else is an error.
750 *********************************************************************/
751 int load_re_filterfile(struct client_state *csp)
755 struct re_filterfile_spec *bl;
756 struct file_list *fs;
758 char buf[BUFFER_SIZE];
760 unsigned long linenum = 0;
763 if (!check_file_changed(current_re_filterfile, csp->config->re_filterfile, &fs))
765 /* No need to load */
768 csp->rlist = current_re_filterfile;
774 goto load_re_filterfile_error;
777 fs->f = bl = (struct re_filterfile_spec *)zalloc(sizeof(*bl));
780 goto load_re_filterfile_error;
783 /* Open the file or fail */
784 if ((fp = fopen(csp->config->re_filterfile, "r")) == NULL)
786 goto load_re_filterfile_error;
789 /* Read line by line */
790 while (read_config_line(buf, sizeof(buf), fp, &linenum) != NULL)
792 enlist( bl->patterns, buf );
794 /* We have a meaningful line -> make it a job */
795 if ((dummy = pcrs_compile_command(buf, &error)) == NULL)
797 log_error(LOG_LEVEL_RE_FILTER,
798 "Adding re_filter job %s failed with error %d.", buf, error);
803 dummy->next = bl->joblist;
805 log_error(LOG_LEVEL_RE_FILTER, "Adding re_filter job %s succeeded.", buf);
811 /* the old one is now obsolete */
812 if ( NULL != current_re_filterfile )
814 current_re_filterfile->unloader = unload_re_filterfile;
817 fs->next = files->next;
819 current_re_filterfile = fs;
828 load_re_filterfile_error:
829 log_error(LOG_LEVEL_FATAL, "can't load re_filterfile '%s': %E",
830 csp->config->re_filterfile);
836 /*********************************************************************
838 * Function : add_loader
840 * Description : Called from `load_config'. Called once for each input
841 * file found in config.
844 * 1 : loader = pointer to a function that can parse and load
845 * the appropriate config file.
846 * 2 : config = The configuration_spec to add the loader to.
850 *********************************************************************/
851 void add_loader(int (*loader)(struct client_state *),
852 struct configuration_spec * config)
856 for (i=0; i < NLOADERS; i++)
858 if (config->loaders[i] == NULL)
860 config->loaders[i] = loader;
868 /*********************************************************************
870 * Function : run_loader
872 * Description : Called from `load_config' and `listen_loop'. This
873 * function keeps the "csp" current with any file mods
874 * since the last loop. If a file is unchanged, the
875 * loader functions do NOT reload the file.
878 * 1 : csp = Current client state (buffers, headers, etc...)
879 * Must be non-null. Reads: "csp->config"
880 * Writes: various data members.
882 * Returns : 0 => Ok, everything else is an error.
884 *********************************************************************/
885 int run_loader(struct client_state *csp)
890 for (i=0; i < NLOADERS; i++)
892 if (csp->config->loaders[i] == NULL)
896 ret |= (csp->config->loaders[i])(csp);