1 const char loaders_rcs[] = "$Id: loaders.c,v 1.31 2001/10/26 17:39:01 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.31 2001/10/26 17:39:01 oes
39 * Removed csp->referrer
40 * Moved ijb_isspace and ijb_tolower to project.h
42 * Revision 1.30 2001/10/25 03:40:48 david__schmidt
43 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
44 * threads to call select() simultaneously. So, it's time to do a real, live,
45 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
46 * (native). Both versions will work, but using __OS2__ offers multi-threading.
48 * Revision 1.29 2001/10/23 21:38:53 jongfoster
49 * Adding error-checking to create_url_spec()
51 * Revision 1.28 2001/10/07 15:40:39 oes
52 * Replaced 6 boolean members of csp with one bitmap (csp->flags)
54 * Revision 1.27 2001/09/22 16:36:59 jongfoster
55 * Removing unused parameter fs from read_config_line()
57 * Revision 1.26 2001/09/22 14:05:22 jongfoster
58 * Bugfix: Multiple escaped "#" characters in a configuration
59 * file are now permitted.
60 * Also removing 3 unused headers.
62 * Revision 1.25 2001/09/13 22:44:03 jongfoster
63 * Adding {} to an if statement
65 * Revision 1.24 2001/07/30 22:08:36 jongfoster
66 * Tidying up #defines:
67 * - All feature #defines are now of the form FEATURE_xxx
68 * - Permanently turned off WIN_GUI_EDIT
69 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
71 * Revision 1.23 2001/07/20 15:51:54 oes
72 * Fixed indentation of prepocessor commands
74 * Revision 1.22 2001/07/20 15:16:17 haroon
75 * - per Guy's suggestion, added a while loop in sweep() to catch not just
76 * the last inactive CSP but all other consecutive inactive CSPs after that
79 * Revision 1.21 2001/07/18 17:26:24 oes
80 * Changed to conform to new pcrs interface
82 * Revision 1.20 2001/07/17 13:07:01 oes
83 * Fixed segv when last line in config files
84 * lacked a terminating (\r)\n
86 * Revision 1.19 2001/07/13 14:01:54 oes
87 * Removed all #ifdef PCRS
89 * Revision 1.18 2001/06/29 21:45:41 oes
90 * Indentation, CRLF->LF, Tab-> Space
92 * Revision 1.17 2001/06/29 13:31:51 oes
95 * Revision 1.16 2001/06/09 10:55:28 jongfoster
96 * Changing BUFSIZ ==> BUFFER_SIZE
98 * Revision 1.15 2001/06/07 23:14:14 jongfoster
99 * Removing ACL and forward file loaders - these
100 * files have been merged into the config file.
101 * Cosmetic: Moving unloader funcs next to their
102 * respective loader funcs
104 * Revision 1.14 2001/06/01 03:27:04 oes
105 * Fixed line continuation problem
107 * Revision 1.13 2001/05/31 21:28:49 jongfoster
108 * Removed all permissionsfile code - it's now called the actions
109 * file, and (almost) all the code is in actions.c
111 * Revision 1.12 2001/05/31 17:32:31 oes
113 * - Enhanced domain part globbing with infix and prefix asterisk
114 * matching and optional unanchored operation
116 * Revision 1.11 2001/05/29 23:25:24 oes
118 * - load_config_line() and load_permissions_file() now use chomp()
120 * Revision 1.10 2001/05/29 09:50:24 jongfoster
121 * Unified blocklist/imagelist/permissionslist.
122 * File format is still under discussion, but the internal changes
125 * Also modified interceptor behaviour:
126 * - We now intercept all URLs beginning with one of the following
127 * prefixes (and *only* these prefixes):
129 * * http://ijbswa.sf.net/config/
130 * * http://ijbswa.sourceforge.net/config/
131 * - New interceptors "home page" - go to http://i.j.b/ to see it.
132 * - Internal changes so that intercepted and fast redirect pages
133 * are not replaced with an image.
134 * - Interceptors now have the option to send a binary page direct
135 * to the client. (i.e. ijb-send-banner uses this)
136 * - Implemented show-url-info interceptor. (Which is why I needed
137 * the above interceptors changes - a typical URL is
138 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
139 * The previous mechanism would not have intercepted that, and
140 * if it had been intercepted then it then it would have replaced
143 * Revision 1.9 2001/05/26 17:12:07 jongfoster
144 * Fatal errors loading configuration files now give better error messages.
146 * Revision 1.8 2001/05/26 00:55:20 jongfoster
147 * Removing duplicated code. load_forwardfile() now uses create_url_spec()
149 * Revision 1.7 2001/05/26 00:28:36 jongfoster
150 * Automatic reloading of config file.
151 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
152 * Most of the global variables have been moved to a new
153 * struct configuration_spec, accessed through csp->config->globalname
154 * Most of the globals remaining are used by the Win32 GUI.
156 * Revision 1.6 2001/05/23 12:27:33 oes
158 * Fixed ugly indentation of my last changes
160 * Revision 1.5 2001/05/23 10:39:05 oes
161 * - Added support for escaping the comment character
162 * in config files by a backslash
163 * - Added support for line continuation in config
165 * - Fixed a buffer overflow bug with long config lines
167 * Revision 1.4 2001/05/22 18:56:28 oes
170 * Revision 1.3 2001/05/20 01:21:20 jongfoster
171 * Version 2.9.4 checkin.
172 * - Merged popupfile and cookiefile, and added control over PCRS
173 * filtering, in new "permissionsfile".
174 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
175 * file error you now get a message box (in the Win32 GUI) rather
176 * than the program exiting with no explanation.
177 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
179 * - Removed tabs from "config"
180 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
181 * - Bumped up version number.
183 * Revision 1.2 2001/05/17 23:01:01 oes
184 * - Cleaned CRLF's from the sources and related files
186 * Revision 1.1.1.1 2001/05/15 13:58:59 oes
187 * Initial import of version 2.9.3 source tree
190 *********************************************************************/
197 #include <sys/types.h>
201 #include <sys/stat.h>
205 #if !defined(_WIN32) && !defined(__OS2__)
215 #include "miscutil.h"
219 const char loaders_h_rcs[] = LOADERS_H_VERSION;
222 * Currently active files.
223 * These are also entered in the main linked list of files.
227 static struct file_list *current_trustfile = NULL;
228 #endif /* def FEATURE_TRUST */
230 static struct file_list *current_re_filterfile = NULL;
234 /*********************************************************************
238 * Description : Basically a mark and sweep garbage collector, it is run
239 * (by the parent thread) every once in a while to reclaim memory.
241 * It uses a mark and sweep strategy:
242 * 1) mark all files as inactive
244 * 2) check with each client:
245 * if it is active, mark its files as active
246 * if it is inactive, free its resources
248 * 3) free the resources of all of the files that
249 * are still marked as inactive (and are obsolete).
251 * N.B. files that are not obsolete don't have an unloader defined.
257 *********************************************************************/
260 struct file_list *fl, *nfl;
261 struct client_state *csp, *ncsp;
263 /* clear all of the file's active flags */
264 for ( fl = files->next; NULL != fl; fl = fl->next )
269 for (csp = clients; csp && (ncsp = csp->next) ; csp = csp->next)
271 if (ncsp->flags & CSP_FLAG_ACTIVE)
273 /* mark this client's files as active */
276 * Always have a configuration file.
277 * (Also note the slightly non-standard extra
280 ncsp->config->config_file_list->active = 1;
282 if (ncsp->actions_list) /* actions files */
284 ncsp->actions_list->active = 1;
287 if (ncsp->rlist) /* pcrsjob files */
289 ncsp->rlist->active = 1;
293 if (ncsp->tlist) /* trust files */
295 ncsp->tlist->active = 1;
297 #endif /* def FEATURE_TRUST */
302 * this client is not active, release its resources
303 * and the ones of all inactive clients that might
307 while (!(ncsp->flags & CSP_FLAG_ACTIVE))
309 csp->next = ncsp->next;
311 freez(ncsp->ip_addr_str);
312 freez(ncsp->my_ip_addr_str);
313 freez(ncsp->my_hostname);
314 freez(ncsp->x_forwarded);
315 freez(ncsp->iob->buf);
317 free_http_request(ncsp->http);
319 destroy_list(ncsp->headers);
320 destroy_list(ncsp->cookie_list);
322 free_current_action(ncsp->action);
324 #ifdef FEATURE_STATISTICS
326 if (ncsp->flags & CSP_FLAG_REJECTED)
330 #endif /* def FEATURE_STATISTICS */
334 /* are there any more in sequence after it? */
335 if( !(ncsp = csp->next) )
341 for (fl = files; fl && (nfl = fl->next) ; fl = fl->next)
343 if ( ( 0 == nfl->active ) && ( NULL != nfl->unloader ) )
345 fl->next = nfl->next;
347 (nfl->unloader)(nfl->f);
349 freez(nfl->filename);
358 /*********************************************************************
360 * Function : create_url_spec
362 * Description : Creates a "url_spec" structure from a string.
363 * When finished, free with unload_url().
366 * 1 : url = Target url_spec to be filled in. Must be
367 * zeroed out before the call (e.g. using zalloc).
368 * 2 : buf = Source pattern, null terminated. NOTE: The
369 * contents of this buffer are destroyed by this
370 * function. If this function succeeds, the
371 * buffer is copied to url->spec. If this
372 * function fails, the contents of the buffer
375 * Returns : JB_ERR_OK - Success
376 * JB_ERR_MEMORY - Out of memory
377 * JB_ERR_PARSE - Cannot parse regex (Detailed message
378 * written to system log)
380 *********************************************************************/
381 jb_err create_url_spec(struct url_spec * url, char * buf)
384 struct url_spec tmp_url[1];
389 /* save a copy of the orignal specification */
390 if ((url->spec = strdup(buf)) == NULL)
392 return JB_ERR_MEMORY;
395 if ((p = strchr(buf, '/')))
397 if (NULL == (url->path = strdup(p)))
400 return JB_ERR_MEMORY;
402 url->pathlen = strlen(url->path);
414 char rebuf[BUFFER_SIZE];
416 if (NULL == (url->preg = zalloc(sizeof(*url->preg))))
420 return JB_ERR_MEMORY;
423 sprintf(rebuf, "^(%s)", url->path);
425 errcode = regcomp(url->preg, rebuf,
426 (REG_EXTENDED|REG_NOSUB|REG_ICASE));
429 size_t errlen = regerror(errcode,
430 url->preg, rebuf, sizeof(rebuf));
432 if (errlen > (sizeof(rebuf) - (size_t)1))
434 errlen = sizeof(rebuf) - (size_t)1;
436 rebuf[errlen] = '\0';
438 log_error(LOG_LEVEL_ERROR, "error compiling %s: %s",
449 if ((p = strchr(buf, ':')) == NULL)
459 if ((url->domain = strdup(buf)) == NULL)
465 #endif /* def REGEX */
466 return JB_ERR_MEMORY;
469 /* split domain into components */
471 *tmp_url = dsplit(url->domain);
472 if (tmp_url->dbuf == NULL)
479 #endif /* def REGEX */
480 return JB_ERR_MEMORY;
483 url->dbuf = tmp_url->dbuf;
484 url->dcnt = tmp_url->dcnt;
485 url->dvec = tmp_url->dvec;
486 url->unanchored = tmp_url->unanchored;
493 /*********************************************************************
495 * Function : free_url
497 * Description : Called from the "unloaders". Freez the url
498 * structure elements.
501 * 1 : url = pointer to a url_spec structure.
505 *********************************************************************/
506 void free_url(struct url_spec *url)
508 if (url == NULL) return;
526 /*********************************************************************
528 * Function : check_file_changed
530 * Description : Helper function to check if a file needs reloading.
531 * If "current" is still current, return it. Otherwise
532 * allocates a new (zeroed) "struct file_list", fills
533 * in the disk file name and timestamp, and returns it.
536 * 1 : current = The file_list currently being used - will
537 * be checked to see if it is out of date.
538 * May be NULL (which is treated as out of
540 * 2 : filename = Name of file to check.
541 * 3 : newfl = New file list. [Output only]
542 * This will be set to NULL, OR a struct
543 * file_list newly allocated on the
544 * heap, with the filename and lastmodified
545 * fields filled, and all others zeroed.
547 * Returns : If file unchanged: 0 (and sets newfl == NULL)
548 * If file changed: 1 and sets newfl != NULL
549 * On error: 1 and sets newfl == NULL
551 *********************************************************************/
552 int check_file_changed(const struct file_list * current,
553 const char * filename,
554 struct file_list ** newfl)
556 struct file_list *fs;
557 struct stat statbuf[1];
561 if (stat(filename, statbuf) < 0)
563 /* Error, probably file not found. */
568 && (current->lastmodified == statbuf->st_mtime)
569 && (0 == strcmp(current->filename, filename)))
574 fs = (struct file_list *)zalloc(sizeof(struct file_list));
578 /* Out of memory error */
582 fs->filename = strdup(filename);
583 fs->lastmodified = statbuf->st_mtime;
585 if (fs->filename == NULL)
587 /* Out of memory error */
599 /*********************************************************************
601 * Function : read_config_line
603 * Description : Read a single non-empty line from a file and return
604 * it. Trims comments, leading and trailing whitespace
605 * and respects escaping of newline and comment char.
608 * 1 : buf = Buffer to use.
609 * 2 : buflen = Size of buffer in bytes.
610 * 3 : fp = File to read from
611 * 4 : linenum = linenumber in file
613 * Returns : NULL on EOF or error
614 * Otherwise, returns buf.
616 *********************************************************************/
617 char *read_config_line(char *buf, int buflen, FILE *fp, unsigned long *linenum)
622 char linebuf[BUFFER_SIZE];
627 while (fgets(linebuf, sizeof(linebuf), fp))
630 /* Trim off newline */
631 if ((p = strpbrk(linebuf, "\r\n")) != NULL)
637 p = linebuf + strlen(linebuf);
640 /* Line continuation? Trim escape and set flag. */
641 if ((p != linebuf) && (*--p == '\\'))
647 /* If there's a comment char.. */
649 while ((p = strchr(p, '#')) != NULL)
651 /* ..and it's escaped, left-shift the line over the escape. */
652 if ((p != linebuf) && (*(p-1) == '\\'))
656 while ((*dest++ = *src++) != '\0')
660 /* Now scan from just after the "#". */
662 /* Else, chop off the rest of the line */
669 /* Write to the buffer */
672 strncat(buf, linebuf, buflen - strlen(buf));
682 /* Remove leading and trailing whitespace */
698 /*********************************************************************
700 * Function : unload_trustfile
702 * Description : Unloads a trustfile.
705 * 1 : f = the data structure associated with the trustfile.
709 *********************************************************************/
710 static void unload_trustfile(void *f)
712 struct block_spec *b = (struct block_spec *)f;
713 if (b == NULL) return;
715 unload_trustfile(b->next); /* Stack is cheap, isn't it? */
724 /*********************************************************************
726 * Function : load_trustfile
728 * Description : Read and parse a trustfile and add to files list.
731 * 1 : csp = Current client state (buffers, headers, etc...)
733 * Returns : 0 => Ok, everything else is an error.
735 *********************************************************************/
736 int load_trustfile(struct client_state *csp)
740 struct block_spec *b, *bl;
741 struct url_spec **tl;
743 char buf[BUFFER_SIZE], *p, *q;
745 struct file_list *fs;
746 unsigned long linenum = 0;
748 if (!check_file_changed(current_trustfile, csp->config->trustfile, &fs))
750 /* No need to load */
753 csp->tlist = current_trustfile;
759 goto load_trustfile_error;
762 fs->f = bl = (struct block_spec *)zalloc(sizeof(*bl));
765 goto load_trustfile_error;
768 if ((fp = fopen(csp->config->trustfile, "r")) == NULL)
770 goto load_trustfile_error;
773 tl = csp->config->trust_list;
775 while (read_config_line(buf, sizeof(buf), fp, &linenum) != NULL)
791 while ((*p++ = *q++))
797 /* skip blank lines */
803 /* allocate a new node */
804 if ((b = zalloc(sizeof(*b))) == NULL)
807 goto load_trustfile_error;
810 /* add it to the list */
816 /* Save the URL pattern */
817 if (create_url_spec(b->url, buf))
820 goto load_trustfile_error;
824 * save a pointer to URL's spec in the list of trusted URL's, too
836 /* the old one is now obsolete */
837 if (current_trustfile)
839 current_trustfile->unloader = unload_trustfile;
842 fs->next = files->next;
844 current_trustfile = fs;
853 load_trustfile_error:
854 log_error(LOG_LEVEL_FATAL, "can't load trustfile '%s': %E",
855 csp->config->trustfile);
859 #endif /* def FEATURE_TRUST */
862 /*********************************************************************
864 * Function : unload_re_filterfile
866 * Description : Unload the re_filter list.
869 * 1 : f = the data structure associated with the filterfile.
873 *********************************************************************/
874 static void unload_re_filterfile(void *f)
876 struct re_filterfile_spec *b = (struct re_filterfile_spec *)f;
883 destroy_list(b->patterns);
884 pcrs_free_joblist(b->joblist);
890 /*********************************************************************
892 * Function : load_re_filterfile
894 * Description : Load the re_filterfile. Each non-comment, non-empty
895 * line is instantly added to the joblist, which is
896 * a chained list of pcrs_job structs.
899 * 1 : csp = Current client state (buffers, headers, etc...)
901 * Returns : 0 => Ok, everything else is an error.
903 *********************************************************************/
904 int load_re_filterfile(struct client_state *csp)
908 struct re_filterfile_spec *bl;
909 struct file_list *fs;
911 char buf[BUFFER_SIZE];
913 unsigned long linenum = 0;
916 if (!check_file_changed(current_re_filterfile, csp->config->re_filterfile, &fs))
918 /* No need to load */
921 csp->rlist = current_re_filterfile;
927 goto load_re_filterfile_error;
930 fs->f = bl = (struct re_filterfile_spec *)zalloc(sizeof(*bl));
933 goto load_re_filterfile_error;
936 /* Open the file or fail */
937 if ((fp = fopen(csp->config->re_filterfile, "r")) == NULL)
939 goto load_re_filterfile_error;
942 /* Read line by line */
943 while (read_config_line(buf, sizeof(buf), fp, &linenum) != NULL)
945 enlist( bl->patterns, buf );
947 /* We have a meaningful line -> make it a job */
948 if ((dummy = pcrs_compile_command(buf, &error)) == NULL)
950 log_error(LOG_LEVEL_RE_FILTER,
951 "Adding re_filter job %s failed with error %d.", buf, error);
956 dummy->next = bl->joblist;
958 log_error(LOG_LEVEL_RE_FILTER, "Adding re_filter job %s succeeded.", buf);
964 /* the old one is now obsolete */
965 if ( NULL != current_re_filterfile )
967 current_re_filterfile->unloader = unload_re_filterfile;
970 fs->next = files->next;
972 current_re_filterfile = fs;
981 load_re_filterfile_error:
982 log_error(LOG_LEVEL_FATAL, "can't load re_filterfile '%s': %E",
983 csp->config->re_filterfile);
989 /*********************************************************************
991 * Function : add_loader
993 * Description : Called from `load_config'. Called once for each input
994 * file found in config.
997 * 1 : loader = pointer to a function that can parse and load
998 * the appropriate config file.
999 * 2 : config = The configuration_spec to add the loader to.
1003 *********************************************************************/
1004 void add_loader(int (*loader)(struct client_state *),
1005 struct configuration_spec * config)
1009 for (i=0; i < NLOADERS; i++)
1011 if (config->loaders[i] == NULL)
1013 config->loaders[i] = loader;
1021 /*********************************************************************
1023 * Function : run_loader
1025 * Description : Called from `load_config' and `listen_loop'. This
1026 * function keeps the "csp" current with any file mods
1027 * since the last loop. If a file is unchanged, the
1028 * loader functions do NOT reload the file.
1031 * 1 : csp = Current client state (buffers, headers, etc...)
1032 * Must be non-null. Reads: "csp->config"
1033 * Writes: various data members.
1035 * Returns : 0 => Ok, everything else is an error.
1037 *********************************************************************/
1038 int run_loader(struct client_state *csp)
1043 for (i=0; i < NLOADERS; i++)
1045 if (csp->config->loaders[i] == NULL)
1049 ret |= (csp->config->loaders[i])(csp);