1 const char loaders_rcs[] = "$Id: loaders.c,v 1.30 2001/10/25 03:40:48 david__schmidt 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.30 2001/10/25 03:40:48 david__schmidt
39 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
40 * threads to call select() simultaneously. So, it's time to do a real, live,
41 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
42 * (native). Both versions will work, but using __OS2__ offers multi-threading.
44 * Revision 1.29 2001/10/23 21:38:53 jongfoster
45 * Adding error-checking to create_url_spec()
47 * Revision 1.28 2001/10/07 15:40:39 oes
48 * Replaced 6 boolean members of csp with one bitmap (csp->flags)
50 * Revision 1.27 2001/09/22 16:36:59 jongfoster
51 * Removing unused parameter fs from read_config_line()
53 * Revision 1.26 2001/09/22 14:05:22 jongfoster
54 * Bugfix: Multiple escaped "#" characters in a configuration
55 * file are now permitted.
56 * Also removing 3 unused headers.
58 * Revision 1.25 2001/09/13 22:44:03 jongfoster
59 * Adding {} to an if statement
61 * Revision 1.24 2001/07/30 22:08:36 jongfoster
62 * Tidying up #defines:
63 * - All feature #defines are now of the form FEATURE_xxx
64 * - Permanently turned off WIN_GUI_EDIT
65 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
67 * Revision 1.23 2001/07/20 15:51:54 oes
68 * Fixed indentation of prepocessor commands
70 * Revision 1.22 2001/07/20 15:16:17 haroon
71 * - per Guy's suggestion, added a while loop in sweep() to catch not just
72 * the last inactive CSP but all other consecutive inactive CSPs after that
75 * Revision 1.21 2001/07/18 17:26:24 oes
76 * Changed to conform to new pcrs interface
78 * Revision 1.20 2001/07/17 13:07:01 oes
79 * Fixed segv when last line in config files
80 * lacked a terminating (\r)\n
82 * Revision 1.19 2001/07/13 14:01:54 oes
83 * Removed all #ifdef PCRS
85 * Revision 1.18 2001/06/29 21:45:41 oes
86 * Indentation, CRLF->LF, Tab-> Space
88 * Revision 1.17 2001/06/29 13:31:51 oes
91 * Revision 1.16 2001/06/09 10:55:28 jongfoster
92 * Changing BUFSIZ ==> BUFFER_SIZE
94 * Revision 1.15 2001/06/07 23:14:14 jongfoster
95 * Removing ACL and forward file loaders - these
96 * files have been merged into the config file.
97 * Cosmetic: Moving unloader funcs next to their
98 * respective loader funcs
100 * Revision 1.14 2001/06/01 03:27:04 oes
101 * Fixed line continuation problem
103 * Revision 1.13 2001/05/31 21:28:49 jongfoster
104 * Removed all permissionsfile code - it's now called the actions
105 * file, and (almost) all the code is in actions.c
107 * Revision 1.12 2001/05/31 17:32:31 oes
109 * - Enhanced domain part globbing with infix and prefix asterisk
110 * matching and optional unanchored operation
112 * Revision 1.11 2001/05/29 23:25:24 oes
114 * - load_config_line() and load_permissions_file() now use chomp()
116 * Revision 1.10 2001/05/29 09:50:24 jongfoster
117 * Unified blocklist/imagelist/permissionslist.
118 * File format is still under discussion, but the internal changes
121 * Also modified interceptor behaviour:
122 * - We now intercept all URLs beginning with one of the following
123 * prefixes (and *only* these prefixes):
125 * * http://ijbswa.sf.net/config/
126 * * http://ijbswa.sourceforge.net/config/
127 * - New interceptors "home page" - go to http://i.j.b/ to see it.
128 * - Internal changes so that intercepted and fast redirect pages
129 * are not replaced with an image.
130 * - Interceptors now have the option to send a binary page direct
131 * to the client. (i.e. ijb-send-banner uses this)
132 * - Implemented show-url-info interceptor. (Which is why I needed
133 * the above interceptors changes - a typical URL is
134 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
135 * The previous mechanism would not have intercepted that, and
136 * if it had been intercepted then it then it would have replaced
139 * Revision 1.9 2001/05/26 17:12:07 jongfoster
140 * Fatal errors loading configuration files now give better error messages.
142 * Revision 1.8 2001/05/26 00:55:20 jongfoster
143 * Removing duplicated code. load_forwardfile() now uses create_url_spec()
145 * Revision 1.7 2001/05/26 00:28:36 jongfoster
146 * Automatic reloading of config file.
147 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
148 * Most of the global variables have been moved to a new
149 * struct configuration_spec, accessed through csp->config->globalname
150 * Most of the globals remaining are used by the Win32 GUI.
152 * Revision 1.6 2001/05/23 12:27:33 oes
154 * Fixed ugly indentation of my last changes
156 * Revision 1.5 2001/05/23 10:39:05 oes
157 * - Added support for escaping the comment character
158 * in config files by a backslash
159 * - Added support for line continuation in config
161 * - Fixed a buffer overflow bug with long config lines
163 * Revision 1.4 2001/05/22 18:56:28 oes
166 * Revision 1.3 2001/05/20 01:21:20 jongfoster
167 * Version 2.9.4 checkin.
168 * - Merged popupfile and cookiefile, and added control over PCRS
169 * filtering, in new "permissionsfile".
170 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
171 * file error you now get a message box (in the Win32 GUI) rather
172 * than the program exiting with no explanation.
173 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
175 * - Removed tabs from "config"
176 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
177 * - Bumped up version number.
179 * Revision 1.2 2001/05/17 23:01:01 oes
180 * - Cleaned CRLF's from the sources and related files
182 * Revision 1.1.1.1 2001/05/15 13:58:59 oes
183 * Initial import of version 2.9.3 source tree
186 *********************************************************************/
193 #include <sys/types.h>
197 #include <sys/stat.h>
201 #if !defined(_WIN32) && !defined(__OS2__)
211 #include "miscutil.h"
215 const char loaders_h_rcs[] = LOADERS_H_VERSION;
218 * Currently active files.
219 * These are also entered in the main linked list of files.
223 static struct file_list *current_trustfile = NULL;
224 #endif /* def FEATURE_TRUST */
226 static struct file_list *current_re_filterfile = NULL;
230 /*********************************************************************
234 * Description : Basically a mark and sweep garbage collector, it is run
235 * (by the parent thread) every once in a while to reclaim memory.
237 * It uses a mark and sweep strategy:
238 * 1) mark all files as inactive
240 * 2) check with each client:
241 * if it is active, mark its files as active
242 * if it is inactive, free its resources
244 * 3) free the resources of all of the files that
245 * are still marked as inactive (and are obsolete).
247 * N.B. files that are not obsolete don't have an unloader defined.
253 *********************************************************************/
256 struct file_list *fl, *nfl;
257 struct client_state *csp, *ncsp;
259 /* clear all of the file's active flags */
260 for ( fl = files->next; NULL != fl; fl = fl->next )
265 for (csp = clients; csp && (ncsp = csp->next) ; csp = csp->next)
267 if (ncsp->flags & CSP_FLAG_ACTIVE)
269 /* mark this client's files as active */
272 * Always have a configuration file.
273 * (Also note the slightly non-standard extra
276 ncsp->config->config_file_list->active = 1;
278 if (ncsp->actions_list) /* actions files */
280 ncsp->actions_list->active = 1;
283 if (ncsp->rlist) /* pcrsjob files */
285 ncsp->rlist->active = 1;
289 if (ncsp->tlist) /* trust files */
291 ncsp->tlist->active = 1;
293 #endif /* def FEATURE_TRUST */
298 * this client is not active, release its resources
299 * and the ones of all inactive clients that might
303 while (!(ncsp->flags & CSP_FLAG_ACTIVE))
305 csp->next = ncsp->next;
307 freez(ncsp->ip_addr_str);
308 freez(ncsp->my_ip_addr_str);
309 freez(ncsp->my_hostname);
310 freez(ncsp->x_forwarded);
311 freez(ncsp->iob->buf);
313 free_http_request(ncsp->http);
315 destroy_list(ncsp->headers);
316 destroy_list(ncsp->cookie_list);
318 free_current_action(ncsp->action);
320 #ifdef FEATURE_STATISTICS
322 if (ncsp->flags & CSP_FLAG_REJECTED)
326 #endif /* def FEATURE_STATISTICS */
330 /* are there any more in sequence after it? */
331 if( !(ncsp = csp->next) )
337 for (fl = files; fl && (nfl = fl->next) ; fl = fl->next)
339 if ( ( 0 == nfl->active ) && ( NULL != nfl->unloader ) )
341 fl->next = nfl->next;
343 (nfl->unloader)(nfl->f);
345 freez(nfl->filename);
354 /*********************************************************************
356 * Function : create_url_spec
358 * Description : Creates a "url_spec" structure from a string.
359 * When finished, free with unload_url().
362 * 1 : url = Target url_spec to be filled in. Must be
363 * zeroed out before the call (e.g. using zalloc).
364 * 2 : buf = Source pattern, null terminated. NOTE: The
365 * contents of this buffer are destroyed by this
366 * function. If this function succeeds, the
367 * buffer is copied to url->spec. If this
368 * function fails, the contents of the buffer
371 * Returns : JB_ERR_OK - Success
372 * JB_ERR_MEMORY - Out of memory
373 * JB_ERR_PARSE - Cannot parse regex (Detailed message
374 * written to system log)
376 *********************************************************************/
377 jb_err create_url_spec(struct url_spec * url, char * buf)
380 struct url_spec tmp_url[1];
385 /* save a copy of the orignal specification */
386 if ((url->spec = strdup(buf)) == NULL)
388 return JB_ERR_MEMORY;
391 if ((p = strchr(buf, '/')))
393 if (NULL == (url->path = strdup(p)))
396 return JB_ERR_MEMORY;
398 url->pathlen = strlen(url->path);
410 char rebuf[BUFFER_SIZE];
412 if (NULL == (url->preg = zalloc(sizeof(*url->preg))))
416 return JB_ERR_MEMORY;
419 sprintf(rebuf, "^(%s)", url->path);
421 errcode = regcomp(url->preg, rebuf,
422 (REG_EXTENDED|REG_NOSUB|REG_ICASE));
425 size_t errlen = regerror(errcode,
426 url->preg, rebuf, sizeof(rebuf));
428 if (errlen > (sizeof(rebuf) - (size_t)1))
430 errlen = sizeof(rebuf) - (size_t)1;
432 rebuf[errlen] = '\0';
434 log_error(LOG_LEVEL_ERROR, "error compiling %s: %s",
445 if ((p = strchr(buf, ':')) == NULL)
455 if ((url->domain = strdup(buf)) == NULL)
461 #endif /* def REGEX */
462 return JB_ERR_MEMORY;
465 /* split domain into components */
467 *tmp_url = dsplit(url->domain);
468 if (tmp_url->dbuf == NULL)
475 #endif /* def REGEX */
476 return JB_ERR_MEMORY;
479 url->dbuf = tmp_url->dbuf;
480 url->dcnt = tmp_url->dcnt;
481 url->dvec = tmp_url->dvec;
482 url->unanchored = tmp_url->unanchored;
489 /*********************************************************************
491 * Function : free_url
493 * Description : Called from the "unloaders". Freez the url
494 * structure elements.
497 * 1 : url = pointer to a url_spec structure.
501 *********************************************************************/
502 void free_url(struct url_spec *url)
504 if (url == NULL) return;
522 /*********************************************************************
524 * Function : check_file_changed
526 * Description : Helper function to check if a file needs reloading.
527 * If "current" is still current, return it. Otherwise
528 * allocates a new (zeroed) "struct file_list", fills
529 * in the disk file name and timestamp, and returns it.
532 * 1 : current = The file_list currently being used - will
533 * be checked to see if it is out of date.
534 * May be NULL (which is treated as out of
536 * 2 : filename = Name of file to check.
537 * 3 : newfl = New file list. [Output only]
538 * This will be set to NULL, OR a struct
539 * file_list newly allocated on the
540 * heap, with the filename and lastmodified
541 * fields filled, and all others zeroed.
543 * Returns : If file unchanged: 0 (and sets newfl == NULL)
544 * If file changed: 1 and sets newfl != NULL
545 * On error: 1 and sets newfl == NULL
547 *********************************************************************/
548 int check_file_changed(const struct file_list * current,
549 const char * filename,
550 struct file_list ** newfl)
552 struct file_list *fs;
553 struct stat statbuf[1];
557 if (stat(filename, statbuf) < 0)
559 /* Error, probably file not found. */
564 && (current->lastmodified == statbuf->st_mtime)
565 && (0 == strcmp(current->filename, filename)))
570 fs = (struct file_list *)zalloc(sizeof(struct file_list));
574 /* Out of memory error */
578 fs->filename = strdup(filename);
579 fs->lastmodified = statbuf->st_mtime;
581 if (fs->filename == NULL)
583 /* Out of memory error */
595 /*********************************************************************
597 * Function : read_config_line
599 * Description : Read a single non-empty line from a file and return
600 * it. Trims comments, leading and trailing whitespace
601 * and respects escaping of newline and comment char.
604 * 1 : buf = Buffer to use.
605 * 2 : buflen = Size of buffer in bytes.
606 * 3 : fp = File to read from
608 * Returns : NULL on EOF or error
609 * Otherwise, returns buf.
611 *********************************************************************/
612 char *read_config_line(char *buf, int buflen, FILE *fp)
617 char linebuf[BUFFER_SIZE];
622 while (fgets(linebuf, sizeof(linebuf), fp))
624 /* Trim off newline */
625 if ((p = strpbrk(linebuf, "\r\n")) != NULL)
631 p = linebuf + strlen(linebuf);
634 /* Line continuation? Trim escape and set flag. */
635 if ((p != linebuf) && (*--p == '\\'))
641 /* If there's a comment char.. */
643 while ((p = strchr(p, '#')) != NULL)
645 /* ..and it's escaped, left-shift the line over the escape. */
646 if ((p != linebuf) && (*(p-1) == '\\'))
650 while ((*dest++ = *src++) != '\0')
654 /* Now scan from just after the "#". */
656 /* Else, chop off the rest of the line */
663 /* Write to the buffer */
666 strncat(buf, linebuf, buflen - strlen(buf));
676 /* Remove leading and trailing whitespace */
692 /*********************************************************************
694 * Function : unload_trustfile
696 * Description : Unloads a trustfile.
699 * 1 : f = the data structure associated with the trustfile.
703 *********************************************************************/
704 static void unload_trustfile(void *f)
706 struct block_spec *b = (struct block_spec *)f;
707 if (b == NULL) return;
709 unload_trustfile(b->next); /* Stack is cheap, isn't it? */
718 /*********************************************************************
720 * Function : load_trustfile
722 * Description : Read and parse a trustfile and add to files list.
725 * 1 : csp = Current client state (buffers, headers, etc...)
727 * Returns : 0 => Ok, everything else is an error.
729 *********************************************************************/
730 int load_trustfile(struct client_state *csp)
734 struct block_spec *b, *bl;
735 struct url_spec **tl;
737 char buf[BUFFER_SIZE], *p, *q;
739 struct file_list *fs;
741 if (!check_file_changed(current_trustfile, csp->config->trustfile, &fs))
743 /* No need to load */
746 csp->tlist = current_trustfile;
752 goto load_trustfile_error;
755 fs->f = bl = (struct block_spec *)zalloc(sizeof(*bl));
758 goto load_trustfile_error;
761 if ((fp = fopen(csp->config->trustfile, "r")) == NULL)
763 goto load_trustfile_error;
766 tl = csp->config->trust_list;
768 while (read_config_line(buf, sizeof(buf), fp) != NULL)
784 while ((*p++ = *q++))
790 /* skip blank lines */
796 /* allocate a new node */
797 if ((b = zalloc(sizeof(*b))) == NULL)
800 goto load_trustfile_error;
803 /* add it to the list */
809 /* Save the URL pattern */
810 if (create_url_spec(b->url, buf))
813 goto load_trustfile_error;
817 * save a pointer to URL's spec in the list of trusted URL's, too
829 /* the old one is now obsolete */
830 if (current_trustfile)
832 current_trustfile->unloader = unload_trustfile;
835 fs->next = files->next;
837 current_trustfile = fs;
846 load_trustfile_error:
847 log_error(LOG_LEVEL_FATAL, "can't load trustfile '%s': %E",
848 csp->config->trustfile);
852 #endif /* def FEATURE_TRUST */
855 /*********************************************************************
857 * Function : unload_re_filterfile
859 * Description : Unload the re_filter list.
862 * 1 : f = the data structure associated with the filterfile.
866 *********************************************************************/
867 static void unload_re_filterfile(void *f)
869 struct re_filterfile_spec *b = (struct re_filterfile_spec *)f;
876 destroy_list(b->patterns);
877 pcrs_free_joblist(b->joblist);
883 /*********************************************************************
885 * Function : load_re_filterfile
887 * Description : Load the re_filterfile. Each non-comment, non-empty
888 * line is instantly added to the joblist, which is
889 * a chained list of pcrs_job structs.
892 * 1 : csp = Current client state (buffers, headers, etc...)
894 * Returns : 0 => Ok, everything else is an error.
896 *********************************************************************/
897 int load_re_filterfile(struct client_state *csp)
901 struct re_filterfile_spec *bl;
902 struct file_list *fs;
904 char buf[BUFFER_SIZE];
908 if (!check_file_changed(current_re_filterfile, csp->config->re_filterfile, &fs))
910 /* No need to load */
913 csp->rlist = current_re_filterfile;
919 goto load_re_filterfile_error;
922 fs->f = bl = (struct re_filterfile_spec *)zalloc(sizeof(*bl));
925 goto load_re_filterfile_error;
928 /* Open the file or fail */
929 if ((fp = fopen(csp->config->re_filterfile, "r")) == NULL)
931 goto load_re_filterfile_error;
934 /* Read line by line */
935 while (read_config_line(buf, sizeof(buf), fp) != NULL)
937 enlist( bl->patterns, buf );
939 /* We have a meaningful line -> make it a job */
940 if ((dummy = pcrs_compile_command(buf, &error)) == NULL)
942 log_error(LOG_LEVEL_RE_FILTER,
943 "Adding re_filter job %s failed with error %d.", buf, error);
948 dummy->next = bl->joblist;
950 log_error(LOG_LEVEL_RE_FILTER, "Adding re_filter job %s succeeded.", buf);
956 /* the old one is now obsolete */
957 if ( NULL != current_re_filterfile )
959 current_re_filterfile->unloader = unload_re_filterfile;
962 fs->next = files->next;
964 current_re_filterfile = fs;
973 load_re_filterfile_error:
974 log_error(LOG_LEVEL_FATAL, "can't load re_filterfile '%s': %E",
975 csp->config->re_filterfile);
981 /*********************************************************************
983 * Function : add_loader
985 * Description : Called from `load_config'. Called once for each input
986 * file found in config.
989 * 1 : loader = pointer to a function that can parse and load
990 * the appropriate config file.
991 * 2 : config = The configuration_spec to add the loader to.
995 *********************************************************************/
996 void add_loader(int (*loader)(struct client_state *),
997 struct configuration_spec * config)
1001 for (i=0; i < NLOADERS; i++)
1003 if (config->loaders[i] == NULL)
1005 config->loaders[i] = loader;
1013 /*********************************************************************
1015 * Function : run_loader
1017 * Description : Called from `load_config' and `listen_loop'. This
1018 * function keeps the "csp" current with any file mods
1019 * since the last loop. If a file is unchanged, the
1020 * loader functions do NOT reload the file.
1023 * 1 : csp = Current client state (buffers, headers, etc...)
1024 * Must be non-null. Reads: "csp->config"
1025 * Writes: various data members.
1027 * Returns : 0 => Ok, everything else is an error.
1029 *********************************************************************/
1030 int run_loader(struct client_state *csp)
1035 for (i=0; i < NLOADERS; i++)
1037 if (csp->config->loaders[i] == NULL)
1041 ret |= (csp->config->loaders[i])(csp);