1 const char loaders_rcs[] = "$Id: loaders.c,v 1.27 2001/09/22 16:36:59 jongfoster 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.27 2001/09/22 16:36:59 jongfoster
39 * Removing unused parameter fs from read_config_line()
41 * Revision 1.26 2001/09/22 14:05:22 jongfoster
42 * Bugfix: Multiple escaped "#" characters in a configuration
43 * file are now permitted.
44 * Also removing 3 unused headers.
46 * Revision 1.25 2001/09/13 22:44:03 jongfoster
47 * Adding {} to an if statement
49 * Revision 1.24 2001/07/30 22:08:36 jongfoster
50 * Tidying up #defines:
51 * - All feature #defines are now of the form FEATURE_xxx
52 * - Permanently turned off WIN_GUI_EDIT
53 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
55 * Revision 1.23 2001/07/20 15:51:54 oes
56 * Fixed indentation of prepocessor commands
58 * Revision 1.22 2001/07/20 15:16:17 haroon
59 * - per Guy's suggestion, added a while loop in sweep() to catch not just
60 * the last inactive CSP but all other consecutive inactive CSPs after that
63 * Revision 1.21 2001/07/18 17:26:24 oes
64 * Changed to conform to new pcrs interface
66 * Revision 1.20 2001/07/17 13:07:01 oes
67 * Fixed segv when last line in config files
68 * lacked a terminating (\r)\n
70 * Revision 1.19 2001/07/13 14:01:54 oes
71 * Removed all #ifdef PCRS
73 * Revision 1.18 2001/06/29 21:45:41 oes
74 * Indentation, CRLF->LF, Tab-> Space
76 * Revision 1.17 2001/06/29 13:31:51 oes
79 * Revision 1.16 2001/06/09 10:55:28 jongfoster
80 * Changing BUFSIZ ==> BUFFER_SIZE
82 * Revision 1.15 2001/06/07 23:14:14 jongfoster
83 * Removing ACL and forward file loaders - these
84 * files have been merged into the config file.
85 * Cosmetic: Moving unloader funcs next to their
86 * respective loader funcs
88 * Revision 1.14 2001/06/01 03:27:04 oes
89 * Fixed line continuation problem
91 * Revision 1.13 2001/05/31 21:28:49 jongfoster
92 * Removed all permissionsfile code - it's now called the actions
93 * file, and (almost) all the code is in actions.c
95 * Revision 1.12 2001/05/31 17:32:31 oes
97 * - Enhanced domain part globbing with infix and prefix asterisk
98 * matching and optional unanchored operation
100 * Revision 1.11 2001/05/29 23:25:24 oes
102 * - load_config_line() and load_permissions_file() now use chomp()
104 * Revision 1.10 2001/05/29 09:50:24 jongfoster
105 * Unified blocklist/imagelist/permissionslist.
106 * File format is still under discussion, but the internal changes
109 * Also modified interceptor behaviour:
110 * - We now intercept all URLs beginning with one of the following
111 * prefixes (and *only* these prefixes):
113 * * http://ijbswa.sf.net/config/
114 * * http://ijbswa.sourceforge.net/config/
115 * - New interceptors "home page" - go to http://i.j.b/ to see it.
116 * - Internal changes so that intercepted and fast redirect pages
117 * are not replaced with an image.
118 * - Interceptors now have the option to send a binary page direct
119 * to the client. (i.e. ijb-send-banner uses this)
120 * - Implemented show-url-info interceptor. (Which is why I needed
121 * the above interceptors changes - a typical URL is
122 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
123 * The previous mechanism would not have intercepted that, and
124 * if it had been intercepted then it then it would have replaced
127 * Revision 1.9 2001/05/26 17:12:07 jongfoster
128 * Fatal errors loading configuration files now give better error messages.
130 * Revision 1.8 2001/05/26 00:55:20 jongfoster
131 * Removing duplicated code. load_forwardfile() now uses create_url_spec()
133 * Revision 1.7 2001/05/26 00:28:36 jongfoster
134 * Automatic reloading of config file.
135 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
136 * Most of the global variables have been moved to a new
137 * struct configuration_spec, accessed through csp->config->globalname
138 * Most of the globals remaining are used by the Win32 GUI.
140 * Revision 1.6 2001/05/23 12:27:33 oes
142 * Fixed ugly indentation of my last changes
144 * Revision 1.5 2001/05/23 10:39:05 oes
145 * - Added support for escaping the comment character
146 * in config files by a backslash
147 * - Added support for line continuation in config
149 * - Fixed a buffer overflow bug with long config lines
151 * Revision 1.4 2001/05/22 18:56:28 oes
154 * Revision 1.3 2001/05/20 01:21:20 jongfoster
155 * Version 2.9.4 checkin.
156 * - Merged popupfile and cookiefile, and added control over PCRS
157 * filtering, in new "permissionsfile".
158 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
159 * file error you now get a message box (in the Win32 GUI) rather
160 * than the program exiting with no explanation.
161 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
163 * - Removed tabs from "config"
164 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
165 * - Bumped up version number.
167 * Revision 1.2 2001/05/17 23:01:01 oes
168 * - Cleaned CRLF's from the sources and related files
170 * Revision 1.1.1.1 2001/05/15 13:58:59 oes
171 * Initial import of version 2.9.3 source tree
174 *********************************************************************/
181 #include <sys/types.h>
185 #include <sys/stat.h>
198 #include "miscutil.h"
202 const char loaders_h_rcs[] = LOADERS_H_VERSION;
204 /* Fix a problem with Solaris. There should be no effect on other
206 * Solaris's isspace() is a macro which uses it's argument directly
207 * as an array index. Therefore we need to make sure that high-bit
208 * characters generate +ve values, and ideally we also want to make
209 * the argument match the declared parameter type of "int".
211 #define ijb_isspace(__X) isspace((int)(unsigned char)(__X))
215 * Currently active files.
216 * These are also entered in the main linked list of files.
220 static struct file_list *current_trustfile = NULL;
221 #endif /* def FEATURE_TRUST */
223 static struct file_list *current_re_filterfile = NULL;
227 /*********************************************************************
231 * Description : Basically a mark and sweep garbage collector, it is run
232 * (by the parent thread) every once in a while to reclaim memory.
234 * It uses a mark and sweep strategy:
235 * 1) mark all files as inactive
237 * 2) check with each client:
238 * if it is active, mark its files as active
239 * if it is inactive, free its resources
241 * 3) free the resources of all of the files that
242 * are still marked as inactive (and are obsolete).
244 * N.B. files that are not obsolete don't have an unloader defined.
250 *********************************************************************/
253 struct file_list *fl, *nfl;
254 struct client_state *csp, *ncsp;
256 /* clear all of the file's active flags */
257 for ( fl = files->next; NULL != fl; fl = fl->next )
262 for (csp = clients; csp && (ncsp = csp->next) ; csp = csp->next)
264 if (ncsp->flags & CSP_FLAG_ACTIVE)
266 /* mark this client's files as active */
269 * Always have a configuration file.
270 * (Also note the slightly non-standard extra
273 ncsp->config->config_file_list->active = 1;
275 if (ncsp->actions_list) /* actions files */
277 ncsp->actions_list->active = 1;
280 if (ncsp->rlist) /* pcrsjob files */
282 ncsp->rlist->active = 1;
286 if (ncsp->tlist) /* trust files */
288 ncsp->tlist->active = 1;
290 #endif /* def FEATURE_TRUST */
295 * this client is not active, release its resources
296 * and the ones of all inactive clients that might
300 while (!(ncsp->flags & CSP_FLAG_ACTIVE))
302 csp->next = ncsp->next;
304 freez(ncsp->ip_addr_str);
305 freez(ncsp->my_ip_addr_str);
306 freez(ncsp->my_hostname);
309 freez(ncsp->referrer);
310 #endif /* def FEATURE_TRUST */
311 freez(ncsp->x_forwarded);
312 freez(ncsp->iob->buf);
314 free_http_request(ncsp->http);
316 destroy_list(ncsp->headers);
317 destroy_list(ncsp->cookie_list);
319 free_current_action(ncsp->action);
321 #ifdef FEATURE_STATISTICS
323 if (ncsp->flags & CSP_FLAG_REJECTED)
327 #endif /* def FEATURE_STATISTICS */
331 /* are there any more in sequence after it? */
332 if( !(ncsp = csp->next) )
338 for (fl = files; fl && (nfl = fl->next) ; fl = fl->next)
340 if ( ( 0 == nfl->active ) && ( NULL != nfl->unloader ) )
342 fl->next = nfl->next;
344 (nfl->unloader)(nfl->f);
346 freez(nfl->filename);
355 /*********************************************************************
357 * Function : create_url_spec
359 * Description : Creates a "url_spec" structure from a string.
360 * When finished, free with unload_url().
363 * 1 : url = Target url_spec to be filled in. Must be
364 * zeroed out before the call (e.g. using zalloc).
365 * 2 : buf = Source pattern, null terminated. NOTE: The
366 * contents of this buffer are destroyed by this
367 * function. If this function succeeds, the
368 * buffer is copied to url->spec. If this
369 * function fails, the contents of the buffer
372 * Returns : 0 => Ok, everything else is an error.
374 *********************************************************************/
375 int create_url_spec(struct url_spec * url, char * buf)
378 struct url_spec tmp_url[1];
380 /* paranoia - should never happen. */
381 if ((url == NULL) || (buf == NULL))
386 /* save a copy of the orignal specification */
387 if ((url->spec = strdup(buf)) == NULL)
392 if ((p = strchr(buf, '/')))
394 if (NULL == (url->path = strdup(p)))
399 url->pathlen = strlen(url->path);
411 char rebuf[BUFFER_SIZE];
413 if (NULL == (url->preg = zalloc(sizeof(*url->preg))))
420 sprintf(rebuf, "^(%s)", url->path);
422 errcode = regcomp(url->preg, rebuf,
423 (REG_EXTENDED|REG_NOSUB|REG_ICASE));
426 size_t errlen = regerror(errcode,
427 url->preg, buf, sizeof(buf));
431 log_error(LOG_LEVEL_ERROR, "error compiling %s: %s",
442 if ((p = strchr(buf, ':')) == NULL)
452 if ((url->domain = strdup(buf)) == NULL)
458 #endif /* def REGEX */
462 /* split domain into components */
464 *tmp_url = dsplit(url->domain);
465 url->dbuf = tmp_url->dbuf;
466 url->dcnt = tmp_url->dcnt;
467 url->dvec = tmp_url->dvec;
468 url->unanchored = tmp_url->unanchored;
475 /*********************************************************************
477 * Function : free_url
479 * Description : Called from the "unloaders". Freez the url
480 * structure elements.
483 * 1 : url = pointer to a url_spec structure.
487 *********************************************************************/
488 void free_url(struct url_spec *url)
490 if (url == NULL) return;
508 /*********************************************************************
510 * Function : check_file_changed
512 * Description : Helper function to check if a file needs reloading.
513 * If "current" is still current, return it. Otherwise
514 * allocates a new (zeroed) "struct file_list", fills
515 * in the disk file name and timestamp, and returns it.
518 * 1 : current = The file_list currently being used - will
519 * be checked to see if it is out of date.
520 * May be NULL (which is treated as out of
522 * 2 : filename = Name of file to check.
523 * 3 : newfl = New file list. [Output only]
524 * This will be set to NULL, OR a struct
525 * file_list newly allocated on the
526 * heap, with the filename and lastmodified
527 * fields filled, and all others zeroed.
529 * Returns : If file unchanged: 0 (and sets newfl == NULL)
530 * If file changed: 1 and sets newfl != NULL
531 * On error: 1 and sets newfl == NULL
533 *********************************************************************/
534 int check_file_changed(const struct file_list * current,
535 const char * filename,
536 struct file_list ** newfl)
538 struct file_list *fs;
539 struct stat statbuf[1];
543 if (stat(filename, statbuf) < 0)
545 /* Error, probably file not found. */
550 && (current->lastmodified == statbuf->st_mtime)
551 && (0 == strcmp(current->filename, filename)))
556 fs = (struct file_list *)zalloc(sizeof(struct file_list));
560 /* Out of memory error */
564 fs->filename = strdup(filename);
565 fs->lastmodified = statbuf->st_mtime;
567 if (fs->filename == NULL)
569 /* Out of memory error */
581 /*********************************************************************
583 * Function : read_config_line
585 * Description : Read a single non-empty line from a file and return
586 * it. Trims comments, leading and trailing whitespace
587 * and respects escaping of newline and comment char.
590 * 1 : buf = Buffer to use.
591 * 2 : buflen = Size of buffer in bytes.
592 * 3 : fp = File to read from
594 * Returns : NULL on EOF or error
595 * Otherwise, returns buf.
597 *********************************************************************/
598 char *read_config_line(char *buf, int buflen, FILE *fp)
603 char linebuf[BUFFER_SIZE];
608 while (fgets(linebuf, sizeof(linebuf), fp))
610 /* Trim off newline */
611 if ((p = strpbrk(linebuf, "\r\n")) != NULL)
617 p = linebuf + strlen(linebuf);
620 /* Line continuation? Trim escape and set flag. */
621 if ((p != linebuf) && (*--p == '\\'))
627 /* If there's a comment char.. */
629 while ((p = strchr(p, '#')) != NULL)
631 /* ..and it's escaped, left-shift the line over the escape. */
632 if ((p != linebuf) && (*(p-1) == '\\'))
636 while ((*dest++ = *src++) != '\0')
640 /* Now scan from just after the "#". */
642 /* Else, chop off the rest of the line */
649 /* Write to the buffer */
652 strncat(buf, linebuf, buflen - strlen(buf));
662 /* Remove leading and trailing whitespace */
678 /*********************************************************************
680 * Function : unload_trustfile
682 * Description : Unloads a trustfile.
685 * 1 : f = the data structure associated with the trustfile.
689 *********************************************************************/
690 static void unload_trustfile(void *f)
692 struct block_spec *b = (struct block_spec *)f;
693 if (b == NULL) return;
695 unload_trustfile(b->next); /* Stack is cheap, isn't it? */
704 /*********************************************************************
706 * Function : load_trustfile
708 * Description : Read and parse a trustfile and add to files list.
711 * 1 : csp = Current client state (buffers, headers, etc...)
713 * Returns : 0 => Ok, everything else is an error.
715 *********************************************************************/
716 int load_trustfile(struct client_state *csp)
720 struct block_spec *b, *bl;
721 struct url_spec **tl;
723 char buf[BUFFER_SIZE], *p, *q;
725 struct file_list *fs;
727 if (!check_file_changed(current_trustfile, csp->config->trustfile, &fs))
729 /* No need to load */
732 csp->tlist = current_trustfile;
738 goto load_trustfile_error;
741 fs->f = bl = (struct block_spec *)zalloc(sizeof(*bl));
744 goto load_trustfile_error;
747 if ((fp = fopen(csp->config->trustfile, "r")) == NULL)
749 goto load_trustfile_error;
752 tl = csp->config->trust_list;
754 while (read_config_line(buf, sizeof(buf), fp) != NULL)
770 while ((*p++ = *q++))
776 /* skip blank lines */
782 /* allocate a new node */
783 if ((b = zalloc(sizeof(*b))) == NULL)
786 goto load_trustfile_error;
789 /* add it to the list */
795 /* Save the URL pattern */
796 if (create_url_spec(b->url, buf))
799 goto load_trustfile_error;
803 * save a pointer to URL's spec in the list of trusted URL's, too
815 /* the old one is now obsolete */
816 if (current_trustfile)
818 current_trustfile->unloader = unload_trustfile;
821 fs->next = files->next;
823 current_trustfile = fs;
832 load_trustfile_error:
833 log_error(LOG_LEVEL_FATAL, "can't load trustfile '%s': %E",
834 csp->config->trustfile);
838 #endif /* def FEATURE_TRUST */
841 /*********************************************************************
843 * Function : unload_re_filterfile
845 * Description : Unload the re_filter list.
848 * 1 : f = the data structure associated with the filterfile.
852 *********************************************************************/
853 static void unload_re_filterfile(void *f)
855 struct re_filterfile_spec *b = (struct re_filterfile_spec *)f;
862 destroy_list(b->patterns);
863 pcrs_free_joblist(b->joblist);
869 /*********************************************************************
871 * Function : load_re_filterfile
873 * Description : Load the re_filterfile. Each non-comment, non-empty
874 * line is instantly added to the joblist, which is
875 * a chained list of pcrs_job structs.
878 * 1 : csp = Current client state (buffers, headers, etc...)
880 * Returns : 0 => Ok, everything else is an error.
882 *********************************************************************/
883 int load_re_filterfile(struct client_state *csp)
887 struct re_filterfile_spec *bl;
888 struct file_list *fs;
890 char buf[BUFFER_SIZE];
894 if (!check_file_changed(current_re_filterfile, csp->config->re_filterfile, &fs))
896 /* No need to load */
899 csp->rlist = current_re_filterfile;
905 goto load_re_filterfile_error;
908 fs->f = bl = (struct re_filterfile_spec *)zalloc(sizeof(*bl));
911 goto load_re_filterfile_error;
914 /* Open the file or fail */
915 if ((fp = fopen(csp->config->re_filterfile, "r")) == NULL)
917 goto load_re_filterfile_error;
920 /* Read line by line */
921 while (read_config_line(buf, sizeof(buf), fp) != NULL)
923 enlist( bl->patterns, buf );
925 /* We have a meaningful line -> make it a job */
926 if ((dummy = pcrs_compile_command(buf, &error)) == NULL)
928 log_error(LOG_LEVEL_RE_FILTER,
929 "Adding re_filter job %s failed with error %d.", buf, error);
934 dummy->next = bl->joblist;
936 log_error(LOG_LEVEL_RE_FILTER, "Adding re_filter job %s succeeded.", buf);
942 /* the old one is now obsolete */
943 if ( NULL != current_re_filterfile )
945 current_re_filterfile->unloader = unload_re_filterfile;
948 fs->next = files->next;
950 current_re_filterfile = fs;
959 load_re_filterfile_error:
960 log_error(LOG_LEVEL_FATAL, "can't load re_filterfile '%s': %E",
961 csp->config->re_filterfile);
967 /*********************************************************************
969 * Function : add_loader
971 * Description : Called from `load_config'. Called once for each input
972 * file found in config.
975 * 1 : loader = pointer to a function that can parse and load
976 * the appropriate config file.
977 * 2 : config = The configuration_spec to add the loader to.
981 *********************************************************************/
982 void add_loader(int (*loader)(struct client_state *),
983 struct configuration_spec * config)
987 for (i=0; i < NLOADERS; i++)
989 if (config->loaders[i] == NULL)
991 config->loaders[i] = loader;
999 /*********************************************************************
1001 * Function : run_loader
1003 * Description : Called from `load_config' and `listen_loop'. This
1004 * function keeps the "csp" current with any file mods
1005 * since the last loop. If a file is unchanged, the
1006 * loader functions do NOT reload the file.
1009 * 1 : csp = Current client state (buffers, headers, etc...)
1010 * Must be non-null. Reads: "csp->config"
1011 * Writes: various data members.
1013 * Returns : 0 => Ok, everything else is an error.
1015 *********************************************************************/
1016 int run_loader(struct client_state *csp)
1021 for (i=0; i < NLOADERS; i++)
1023 if (csp->config->loaders[i] == NULL)
1027 ret |= (csp->config->loaders[i])(csp);