1 const char loaders_rcs[] = "$Id: loaders.c,v 1.32 2001/11/07 00:02:13 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.32 2001/11/07 00:02:13 steudten
39 * Add line number in error output for lineparsing for
40 * actionsfile and configfile.
41 * Special handling for CLF added.
43 * Revision 1.31 2001/10/26 17:39:01 oes
44 * Removed csp->referrer
45 * Moved ijb_isspace and ijb_tolower to project.h
47 * Revision 1.30 2001/10/25 03:40:48 david__schmidt
48 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
49 * threads to call select() simultaneously. So, it's time to do a real, live,
50 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
51 * (native). Both versions will work, but using __OS2__ offers multi-threading.
53 * Revision 1.29 2001/10/23 21:38:53 jongfoster
54 * Adding error-checking to create_url_spec()
56 * Revision 1.28 2001/10/07 15:40:39 oes
57 * Replaced 6 boolean members of csp with one bitmap (csp->flags)
59 * Revision 1.27 2001/09/22 16:36:59 jongfoster
60 * Removing unused parameter fs from read_config_line()
62 * Revision 1.26 2001/09/22 14:05:22 jongfoster
63 * Bugfix: Multiple escaped "#" characters in a configuration
64 * file are now permitted.
65 * Also removing 3 unused headers.
67 * Revision 1.25 2001/09/13 22:44:03 jongfoster
68 * Adding {} to an if statement
70 * Revision 1.24 2001/07/30 22:08:36 jongfoster
71 * Tidying up #defines:
72 * - All feature #defines are now of the form FEATURE_xxx
73 * - Permanently turned off WIN_GUI_EDIT
74 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
76 * Revision 1.23 2001/07/20 15:51:54 oes
77 * Fixed indentation of prepocessor commands
79 * Revision 1.22 2001/07/20 15:16:17 haroon
80 * - per Guy's suggestion, added a while loop in sweep() to catch not just
81 * the last inactive CSP but all other consecutive inactive CSPs after that
84 * Revision 1.21 2001/07/18 17:26:24 oes
85 * Changed to conform to new pcrs interface
87 * Revision 1.20 2001/07/17 13:07:01 oes
88 * Fixed segv when last line in config files
89 * lacked a terminating (\r)\n
91 * Revision 1.19 2001/07/13 14:01:54 oes
92 * Removed all #ifdef PCRS
94 * Revision 1.18 2001/06/29 21:45:41 oes
95 * Indentation, CRLF->LF, Tab-> Space
97 * Revision 1.17 2001/06/29 13:31:51 oes
100 * Revision 1.16 2001/06/09 10:55:28 jongfoster
101 * Changing BUFSIZ ==> BUFFER_SIZE
103 * Revision 1.15 2001/06/07 23:14:14 jongfoster
104 * Removing ACL and forward file loaders - these
105 * files have been merged into the config file.
106 * Cosmetic: Moving unloader funcs next to their
107 * respective loader funcs
109 * Revision 1.14 2001/06/01 03:27:04 oes
110 * Fixed line continuation problem
112 * Revision 1.13 2001/05/31 21:28:49 jongfoster
113 * Removed all permissionsfile code - it's now called the actions
114 * file, and (almost) all the code is in actions.c
116 * Revision 1.12 2001/05/31 17:32:31 oes
118 * - Enhanced domain part globbing with infix and prefix asterisk
119 * matching and optional unanchored operation
121 * Revision 1.11 2001/05/29 23:25:24 oes
123 * - load_config_line() and load_permissions_file() now use chomp()
125 * Revision 1.10 2001/05/29 09:50:24 jongfoster
126 * Unified blocklist/imagelist/permissionslist.
127 * File format is still under discussion, but the internal changes
130 * Also modified interceptor behaviour:
131 * - We now intercept all URLs beginning with one of the following
132 * prefixes (and *only* these prefixes):
134 * * http://ijbswa.sf.net/config/
135 * * http://ijbswa.sourceforge.net/config/
136 * - New interceptors "home page" - go to http://i.j.b/ to see it.
137 * - Internal changes so that intercepted and fast redirect pages
138 * are not replaced with an image.
139 * - Interceptors now have the option to send a binary page direct
140 * to the client. (i.e. ijb-send-banner uses this)
141 * - Implemented show-url-info interceptor. (Which is why I needed
142 * the above interceptors changes - a typical URL is
143 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
144 * The previous mechanism would not have intercepted that, and
145 * if it had been intercepted then it then it would have replaced
148 * Revision 1.9 2001/05/26 17:12:07 jongfoster
149 * Fatal errors loading configuration files now give better error messages.
151 * Revision 1.8 2001/05/26 00:55:20 jongfoster
152 * Removing duplicated code. load_forwardfile() now uses create_url_spec()
154 * Revision 1.7 2001/05/26 00:28:36 jongfoster
155 * Automatic reloading of config file.
156 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
157 * Most of the global variables have been moved to a new
158 * struct configuration_spec, accessed through csp->config->globalname
159 * Most of the globals remaining are used by the Win32 GUI.
161 * Revision 1.6 2001/05/23 12:27:33 oes
163 * Fixed ugly indentation of my last changes
165 * Revision 1.5 2001/05/23 10:39:05 oes
166 * - Added support for escaping the comment character
167 * in config files by a backslash
168 * - Added support for line continuation in config
170 * - Fixed a buffer overflow bug with long config lines
172 * Revision 1.4 2001/05/22 18:56:28 oes
175 * Revision 1.3 2001/05/20 01:21:20 jongfoster
176 * Version 2.9.4 checkin.
177 * - Merged popupfile and cookiefile, and added control over PCRS
178 * filtering, in new "permissionsfile".
179 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
180 * file error you now get a message box (in the Win32 GUI) rather
181 * than the program exiting with no explanation.
182 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
184 * - Removed tabs from "config"
185 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
186 * - Bumped up version number.
188 * Revision 1.2 2001/05/17 23:01:01 oes
189 * - Cleaned CRLF's from the sources and related files
191 * Revision 1.1.1.1 2001/05/15 13:58:59 oes
192 * Initial import of version 2.9.3 source tree
195 *********************************************************************/
202 #include <sys/types.h>
205 #include <sys/stat.h>
209 #if !defined(_WIN32) && !defined(__OS2__)
219 #include "miscutil.h"
223 const char loaders_h_rcs[] = LOADERS_H_VERSION;
226 * Currently active files.
227 * These are also entered in the main linked list of files.
231 static struct file_list *current_trustfile = NULL;
232 #endif /* def FEATURE_TRUST */
234 static struct file_list *current_re_filterfile = NULL;
238 /*********************************************************************
242 * Description : Basically a mark and sweep garbage collector, it is run
243 * (by the parent thread) every once in a while to reclaim memory.
245 * It uses a mark and sweep strategy:
246 * 1) mark all files as inactive
248 * 2) check with each client:
249 * if it is active, mark its files as active
250 * if it is inactive, free its resources
252 * 3) free the resources of all of the files that
253 * are still marked as inactive (and are obsolete).
255 * N.B. files that are not obsolete don't have an unloader defined.
261 *********************************************************************/
264 struct file_list *fl, *nfl;
265 struct client_state *csp, *ncsp;
267 /* clear all of the file's active flags */
268 for ( fl = files->next; NULL != fl; fl = fl->next )
273 for (csp = clients; csp && (ncsp = csp->next) ; csp = csp->next)
275 if (ncsp->flags & CSP_FLAG_ACTIVE)
277 /* mark this client's files as active */
280 * Always have a configuration file.
281 * (Also note the slightly non-standard extra
284 ncsp->config->config_file_list->active = 1;
286 if (ncsp->actions_list) /* actions files */
288 ncsp->actions_list->active = 1;
291 if (ncsp->rlist) /* pcrsjob files */
293 ncsp->rlist->active = 1;
297 if (ncsp->tlist) /* trust files */
299 ncsp->tlist->active = 1;
301 #endif /* def FEATURE_TRUST */
306 * this client is not active, release its resources
307 * and the ones of all inactive clients that might
311 while (!(ncsp->flags & CSP_FLAG_ACTIVE))
313 csp->next = ncsp->next;
315 freez(ncsp->ip_addr_str);
316 freez(ncsp->my_ip_addr_str);
317 freez(ncsp->my_hostname);
318 freez(ncsp->x_forwarded);
319 freez(ncsp->iob->buf);
321 free_http_request(ncsp->http);
323 destroy_list(ncsp->headers);
324 destroy_list(ncsp->cookie_list);
326 free_current_action(ncsp->action);
328 #ifdef FEATURE_STATISTICS
330 if (ncsp->flags & CSP_FLAG_REJECTED)
334 #endif /* def FEATURE_STATISTICS */
338 /* are there any more in sequence after it? */
339 if( !(ncsp = csp->next) )
345 for (fl = files; fl && (nfl = fl->next) ; fl = fl->next)
347 if ( ( 0 == nfl->active ) && ( NULL != nfl->unloader ) )
349 fl->next = nfl->next;
351 (nfl->unloader)(nfl->f);
353 freez(nfl->filename);
362 /*********************************************************************
364 * Function : create_url_spec
366 * Description : Creates a "url_spec" structure from a string.
367 * When finished, free with unload_url().
370 * 1 : url = Target url_spec to be filled in. Must be
371 * zeroed out before the call (e.g. using zalloc).
372 * 2 : buf = Source pattern, null terminated. NOTE: The
373 * contents of this buffer are destroyed by this
374 * function. If this function succeeds, the
375 * buffer is copied to url->spec. If this
376 * function fails, the contents of the buffer
379 * Returns : JB_ERR_OK - Success
380 * JB_ERR_MEMORY - Out of memory
381 * JB_ERR_PARSE - Cannot parse regex (Detailed message
382 * written to system log)
384 *********************************************************************/
385 jb_err create_url_spec(struct url_spec * url, char * buf)
388 struct url_spec tmp_url[1];
393 /* save a copy of the orignal specification */
394 if ((url->spec = strdup(buf)) == NULL)
396 return JB_ERR_MEMORY;
399 if ((p = strchr(buf, '/')))
401 if (NULL == (url->path = strdup(p)))
404 return JB_ERR_MEMORY;
406 url->pathlen = strlen(url->path);
418 char rebuf[BUFFER_SIZE];
420 if (NULL == (url->preg = zalloc(sizeof(*url->preg))))
424 return JB_ERR_MEMORY;
427 sprintf(rebuf, "^(%s)", url->path);
429 errcode = regcomp(url->preg, rebuf,
430 (REG_EXTENDED|REG_NOSUB|REG_ICASE));
433 size_t errlen = regerror(errcode,
434 url->preg, rebuf, sizeof(rebuf));
436 if (errlen > (sizeof(rebuf) - (size_t)1))
438 errlen = sizeof(rebuf) - (size_t)1;
440 rebuf[errlen] = '\0';
442 log_error(LOG_LEVEL_ERROR, "error compiling %s: %s",
453 if ((p = strchr(buf, ':')) == NULL)
463 if ((url->domain = strdup(buf)) == NULL)
469 #endif /* def REGEX */
470 return JB_ERR_MEMORY;
473 /* split domain into components */
475 *tmp_url = dsplit(url->domain);
476 if (tmp_url->dbuf == NULL)
483 #endif /* def REGEX */
484 return JB_ERR_MEMORY;
487 url->dbuf = tmp_url->dbuf;
488 url->dcnt = tmp_url->dcnt;
489 url->dvec = tmp_url->dvec;
490 url->unanchored = tmp_url->unanchored;
497 /*********************************************************************
499 * Function : free_url
501 * Description : Called from the "unloaders". Freez the url
502 * structure elements.
505 * 1 : url = pointer to a url_spec structure.
509 *********************************************************************/
510 void free_url(struct url_spec *url)
512 if (url == NULL) return;
530 /*********************************************************************
532 * Function : check_file_changed
534 * Description : Helper function to check if a file needs reloading.
535 * If "current" is still current, return it. Otherwise
536 * allocates a new (zeroed) "struct file_list", fills
537 * in the disk file name and timestamp, and returns it.
540 * 1 : current = The file_list currently being used - will
541 * be checked to see if it is out of date.
542 * May be NULL (which is treated as out of
544 * 2 : filename = Name of file to check.
545 * 3 : newfl = New file list. [Output only]
546 * This will be set to NULL, OR a struct
547 * file_list newly allocated on the
548 * heap, with the filename and lastmodified
549 * fields filled, and all others zeroed.
551 * Returns : If file unchanged: 0 (and sets newfl == NULL)
552 * If file changed: 1 and sets newfl != NULL
553 * On error: 1 and sets newfl == NULL
555 *********************************************************************/
556 int check_file_changed(const struct file_list * current,
557 const char * filename,
558 struct file_list ** newfl)
560 struct file_list *fs;
561 struct stat statbuf[1];
565 if (stat(filename, statbuf) < 0)
567 /* Error, probably file not found. */
572 && (current->lastmodified == statbuf->st_mtime)
573 && (0 == strcmp(current->filename, filename)))
578 fs = (struct file_list *)zalloc(sizeof(struct file_list));
582 /* Out of memory error */
586 fs->filename = strdup(filename);
587 fs->lastmodified = statbuf->st_mtime;
589 if (fs->filename == NULL)
591 /* Out of memory error */
603 /*********************************************************************
605 * Function : read_config_line
607 * Description : Read a single non-empty line from a file and return
608 * it. Trims comments, leading and trailing whitespace
609 * and respects escaping of newline and comment char.
612 * 1 : buf = Buffer to use.
613 * 2 : buflen = Size of buffer in bytes.
614 * 3 : fp = File to read from
615 * 4 : linenum = linenumber in file
617 * Returns : NULL on EOF or error
618 * Otherwise, returns buf.
620 *********************************************************************/
621 char *read_config_line(char *buf, int buflen, FILE *fp, unsigned long *linenum)
626 char linebuf[BUFFER_SIZE];
631 while (fgets(linebuf, sizeof(linebuf), fp))
634 /* Trim off newline */
635 if ((p = strpbrk(linebuf, "\r\n")) != NULL)
641 p = linebuf + strlen(linebuf);
644 /* Line continuation? Trim escape and set flag. */
645 if ((p != linebuf) && (*--p == '\\'))
651 /* If there's a comment char.. */
653 while ((p = strchr(p, '#')) != NULL)
655 /* ..and it's escaped, left-shift the line over the escape. */
656 if ((p != linebuf) && (*(p-1) == '\\'))
660 while ((*dest++ = *src++) != '\0')
664 /* Now scan from just after the "#". */
666 /* Else, chop off the rest of the line */
673 /* Write to the buffer */
676 strncat(buf, linebuf, buflen - strlen(buf));
686 /* Remove leading and trailing whitespace */
702 /*********************************************************************
704 * Function : unload_trustfile
706 * Description : Unloads a trustfile.
709 * 1 : f = the data structure associated with the trustfile.
713 *********************************************************************/
714 static void unload_trustfile(void *f)
716 struct block_spec *b = (struct block_spec *)f;
717 if (b == NULL) return;
719 unload_trustfile(b->next); /* Stack is cheap, isn't it? */
728 /*********************************************************************
730 * Function : load_trustfile
732 * Description : Read and parse a trustfile and add to files list.
735 * 1 : csp = Current client state (buffers, headers, etc...)
737 * Returns : 0 => Ok, everything else is an error.
739 *********************************************************************/
740 int load_trustfile(struct client_state *csp)
744 struct block_spec *b, *bl;
745 struct url_spec **tl;
747 char buf[BUFFER_SIZE], *p, *q;
749 struct file_list *fs;
750 unsigned long linenum = 0;
752 if (!check_file_changed(current_trustfile, csp->config->trustfile, &fs))
754 /* No need to load */
757 csp->tlist = current_trustfile;
763 goto load_trustfile_error;
766 fs->f = bl = (struct block_spec *)zalloc(sizeof(*bl));
769 goto load_trustfile_error;
772 if ((fp = fopen(csp->config->trustfile, "r")) == NULL)
774 goto load_trustfile_error;
777 tl = csp->config->trust_list;
779 while (read_config_line(buf, sizeof(buf), fp, &linenum) != NULL)
795 while ((*p++ = *q++))
801 /* skip blank lines */
807 /* allocate a new node */
808 if ((b = zalloc(sizeof(*b))) == NULL)
811 goto load_trustfile_error;
814 /* add it to the list */
820 /* Save the URL pattern */
821 if (create_url_spec(b->url, buf))
824 goto load_trustfile_error;
828 * save a pointer to URL's spec in the list of trusted URL's, too
840 /* the old one is now obsolete */
841 if (current_trustfile)
843 current_trustfile->unloader = unload_trustfile;
846 fs->next = files->next;
848 current_trustfile = fs;
857 load_trustfile_error:
858 log_error(LOG_LEVEL_FATAL, "can't load trustfile '%s': %E",
859 csp->config->trustfile);
863 #endif /* def FEATURE_TRUST */
866 /*********************************************************************
868 * Function : unload_re_filterfile
870 * Description : Unload the re_filter list.
873 * 1 : f = the data structure associated with the filterfile.
877 *********************************************************************/
878 static void unload_re_filterfile(void *f)
880 struct re_filterfile_spec *b = (struct re_filterfile_spec *)f;
887 destroy_list(b->patterns);
888 pcrs_free_joblist(b->joblist);
894 /*********************************************************************
896 * Function : load_re_filterfile
898 * Description : Load the re_filterfile. Each non-comment, non-empty
899 * line is instantly added to the joblist, which is
900 * a chained list of pcrs_job structs.
903 * 1 : csp = Current client state (buffers, headers, etc...)
905 * Returns : 0 => Ok, everything else is an error.
907 *********************************************************************/
908 int load_re_filterfile(struct client_state *csp)
912 struct re_filterfile_spec *bl;
913 struct file_list *fs;
915 char buf[BUFFER_SIZE];
917 unsigned long linenum = 0;
920 if (!check_file_changed(current_re_filterfile, csp->config->re_filterfile, &fs))
922 /* No need to load */
925 csp->rlist = current_re_filterfile;
931 goto load_re_filterfile_error;
934 fs->f = bl = (struct re_filterfile_spec *)zalloc(sizeof(*bl));
937 goto load_re_filterfile_error;
940 /* Open the file or fail */
941 if ((fp = fopen(csp->config->re_filterfile, "r")) == NULL)
943 goto load_re_filterfile_error;
946 /* Read line by line */
947 while (read_config_line(buf, sizeof(buf), fp, &linenum) != NULL)
949 enlist( bl->patterns, buf );
951 /* We have a meaningful line -> make it a job */
952 if ((dummy = pcrs_compile_command(buf, &error)) == NULL)
954 log_error(LOG_LEVEL_RE_FILTER,
955 "Adding re_filter job %s failed with error %d.", buf, error);
960 dummy->next = bl->joblist;
962 log_error(LOG_LEVEL_RE_FILTER, "Adding re_filter job %s succeeded.", buf);
968 /* the old one is now obsolete */
969 if ( NULL != current_re_filterfile )
971 current_re_filterfile->unloader = unload_re_filterfile;
974 fs->next = files->next;
976 current_re_filterfile = fs;
985 load_re_filterfile_error:
986 log_error(LOG_LEVEL_FATAL, "can't load re_filterfile '%s': %E",
987 csp->config->re_filterfile);
993 /*********************************************************************
995 * Function : add_loader
997 * Description : Called from `load_config'. Called once for each input
998 * file found in config.
1001 * 1 : loader = pointer to a function that can parse and load
1002 * the appropriate config file.
1003 * 2 : config = The configuration_spec to add the loader to.
1007 *********************************************************************/
1008 void add_loader(int (*loader)(struct client_state *),
1009 struct configuration_spec * config)
1013 for (i=0; i < NLOADERS; i++)
1015 if (config->loaders[i] == NULL)
1017 config->loaders[i] = loader;
1025 /*********************************************************************
1027 * Function : run_loader
1029 * Description : Called from `load_config' and `listen_loop'. This
1030 * function keeps the "csp" current with any file mods
1031 * since the last loop. If a file is unchanged, the
1032 * loader functions do NOT reload the file.
1035 * 1 : csp = Current client state (buffers, headers, etc...)
1036 * Must be non-null. Reads: "csp->config"
1037 * Writes: various data members.
1039 * Returns : 0 => Ok, everything else is an error.
1041 *********************************************************************/
1042 int run_loader(struct client_state *csp)
1047 for (i=0; i < NLOADERS; i++)
1049 if (csp->config->loaders[i] == NULL)
1053 ret |= (csp->config->loaders[i])(csp);