1 const char loaders_rcs[] = "$Id: loaders.c,v 1.33 2001/11/13 00:16:38 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.33 2001/11/13 00:16:38 jongfoster
39 * Replacing references to malloc.h with the standard stdlib.h
40 * (See ANSI or K&R 2nd Ed)
42 * Revision 1.32 2001/11/07 00:02:13 steudten
43 * Add line number in error output for lineparsing for
44 * actionsfile and configfile.
45 * Special handling for CLF added.
47 * Revision 1.31 2001/10/26 17:39:01 oes
48 * Removed csp->referrer
49 * Moved ijb_isspace and ijb_tolower to project.h
51 * Revision 1.30 2001/10/25 03:40:48 david__schmidt
52 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
53 * threads to call select() simultaneously. So, it's time to do a real, live,
54 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
55 * (native). Both versions will work, but using __OS2__ offers multi-threading.
57 * Revision 1.29 2001/10/23 21:38:53 jongfoster
58 * Adding error-checking to create_url_spec()
60 * Revision 1.28 2001/10/07 15:40:39 oes
61 * Replaced 6 boolean members of csp with one bitmap (csp->flags)
63 * Revision 1.27 2001/09/22 16:36:59 jongfoster
64 * Removing unused parameter fs from read_config_line()
66 * Revision 1.26 2001/09/22 14:05:22 jongfoster
67 * Bugfix: Multiple escaped "#" characters in a configuration
68 * file are now permitted.
69 * Also removing 3 unused headers.
71 * Revision 1.25 2001/09/13 22:44:03 jongfoster
72 * Adding {} to an if statement
74 * Revision 1.24 2001/07/30 22:08:36 jongfoster
75 * Tidying up #defines:
76 * - All feature #defines are now of the form FEATURE_xxx
77 * - Permanently turned off WIN_GUI_EDIT
78 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
80 * Revision 1.23 2001/07/20 15:51:54 oes
81 * Fixed indentation of prepocessor commands
83 * Revision 1.22 2001/07/20 15:16:17 haroon
84 * - per Guy's suggestion, added a while loop in sweep() to catch not just
85 * the last inactive CSP but all other consecutive inactive CSPs after that
88 * Revision 1.21 2001/07/18 17:26:24 oes
89 * Changed to conform to new pcrs interface
91 * Revision 1.20 2001/07/17 13:07:01 oes
92 * Fixed segv when last line in config files
93 * lacked a terminating (\r)\n
95 * Revision 1.19 2001/07/13 14:01:54 oes
96 * Removed all #ifdef PCRS
98 * Revision 1.18 2001/06/29 21:45:41 oes
99 * Indentation, CRLF->LF, Tab-> Space
101 * Revision 1.17 2001/06/29 13:31:51 oes
104 * Revision 1.16 2001/06/09 10:55:28 jongfoster
105 * Changing BUFSIZ ==> BUFFER_SIZE
107 * Revision 1.15 2001/06/07 23:14:14 jongfoster
108 * Removing ACL and forward file loaders - these
109 * files have been merged into the config file.
110 * Cosmetic: Moving unloader funcs next to their
111 * respective loader funcs
113 * Revision 1.14 2001/06/01 03:27:04 oes
114 * Fixed line continuation problem
116 * Revision 1.13 2001/05/31 21:28:49 jongfoster
117 * Removed all permissionsfile code - it's now called the actions
118 * file, and (almost) all the code is in actions.c
120 * Revision 1.12 2001/05/31 17:32:31 oes
122 * - Enhanced domain part globbing with infix and prefix asterisk
123 * matching and optional unanchored operation
125 * Revision 1.11 2001/05/29 23:25:24 oes
127 * - load_config_line() and load_permissions_file() now use chomp()
129 * Revision 1.10 2001/05/29 09:50:24 jongfoster
130 * Unified blocklist/imagelist/permissionslist.
131 * File format is still under discussion, but the internal changes
134 * Also modified interceptor behaviour:
135 * - We now intercept all URLs beginning with one of the following
136 * prefixes (and *only* these prefixes):
138 * * http://ijbswa.sf.net/config/
139 * * http://ijbswa.sourceforge.net/config/
140 * - New interceptors "home page" - go to http://i.j.b/ to see it.
141 * - Internal changes so that intercepted and fast redirect pages
142 * are not replaced with an image.
143 * - Interceptors now have the option to send a binary page direct
144 * to the client. (i.e. ijb-send-banner uses this)
145 * - Implemented show-url-info interceptor. (Which is why I needed
146 * the above interceptors changes - a typical URL is
147 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
148 * The previous mechanism would not have intercepted that, and
149 * if it had been intercepted then it then it would have replaced
152 * Revision 1.9 2001/05/26 17:12:07 jongfoster
153 * Fatal errors loading configuration files now give better error messages.
155 * Revision 1.8 2001/05/26 00:55:20 jongfoster
156 * Removing duplicated code. load_forwardfile() now uses create_url_spec()
158 * Revision 1.7 2001/05/26 00:28:36 jongfoster
159 * Automatic reloading of config file.
160 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
161 * Most of the global variables have been moved to a new
162 * struct configuration_spec, accessed through csp->config->globalname
163 * Most of the globals remaining are used by the Win32 GUI.
165 * Revision 1.6 2001/05/23 12:27:33 oes
167 * Fixed ugly indentation of my last changes
169 * Revision 1.5 2001/05/23 10:39:05 oes
170 * - Added support for escaping the comment character
171 * in config files by a backslash
172 * - Added support for line continuation in config
174 * - Fixed a buffer overflow bug with long config lines
176 * Revision 1.4 2001/05/22 18:56:28 oes
179 * Revision 1.3 2001/05/20 01:21:20 jongfoster
180 * Version 2.9.4 checkin.
181 * - Merged popupfile and cookiefile, and added control over PCRS
182 * filtering, in new "permissionsfile".
183 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
184 * file error you now get a message box (in the Win32 GUI) rather
185 * than the program exiting with no explanation.
186 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
188 * - Removed tabs from "config"
189 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
190 * - Bumped up version number.
192 * Revision 1.2 2001/05/17 23:01:01 oes
193 * - Cleaned CRLF's from the sources and related files
195 * Revision 1.1.1.1 2001/05/15 13:58:59 oes
196 * Initial import of version 2.9.3 source tree
199 *********************************************************************/
206 #include <sys/types.h>
209 #include <sys/stat.h>
213 #if !defined(_WIN32) && !defined(__OS2__)
223 #include "miscutil.h"
227 const char loaders_h_rcs[] = LOADERS_H_VERSION;
230 * Currently active files.
231 * These are also entered in the main linked list of files.
235 static struct file_list *current_trustfile = NULL;
236 #endif /* def FEATURE_TRUST */
238 static struct file_list *current_re_filterfile = NULL;
242 /*********************************************************************
246 * Description : Basically a mark and sweep garbage collector, it is run
247 * (by the parent thread) every once in a while to reclaim memory.
249 * It uses a mark and sweep strategy:
250 * 1) mark all files as inactive
252 * 2) check with each client:
253 * if it is active, mark its files as active
254 * if it is inactive, free its resources
256 * 3) free the resources of all of the files that
257 * are still marked as inactive (and are obsolete).
259 * N.B. files that are not obsolete don't have an unloader defined.
265 *********************************************************************/
268 struct file_list *fl, *nfl;
269 struct client_state *csp, *ncsp;
271 /* clear all of the file's active flags */
272 for ( fl = files->next; NULL != fl; fl = fl->next )
277 for (csp = clients; csp && (ncsp = csp->next) ; csp = csp->next)
279 if (ncsp->flags & CSP_FLAG_ACTIVE)
281 /* mark this client's files as active */
284 * Always have a configuration file.
285 * (Also note the slightly non-standard extra
288 ncsp->config->config_file_list->active = 1;
290 if (ncsp->actions_list) /* actions files */
292 ncsp->actions_list->active = 1;
295 if (ncsp->rlist) /* pcrsjob files */
297 ncsp->rlist->active = 1;
301 if (ncsp->tlist) /* trust files */
303 ncsp->tlist->active = 1;
305 #endif /* def FEATURE_TRUST */
310 * this client is not active, release its resources
311 * and the ones of all inactive clients that might
315 while (!(ncsp->flags & CSP_FLAG_ACTIVE))
317 csp->next = ncsp->next;
319 freez(ncsp->ip_addr_str);
320 freez(ncsp->my_ip_addr_str);
321 freez(ncsp->my_hostname);
322 freez(ncsp->x_forwarded);
323 freez(ncsp->iob->buf);
325 free_http_request(ncsp->http);
327 destroy_list(ncsp->headers);
328 destroy_list(ncsp->cookie_list);
330 free_current_action(ncsp->action);
332 #ifdef FEATURE_STATISTICS
334 if (ncsp->flags & CSP_FLAG_REJECTED)
338 #endif /* def FEATURE_STATISTICS */
342 /* are there any more in sequence after it? */
343 if( !(ncsp = csp->next) )
349 for (fl = files; fl && (nfl = fl->next) ; fl = fl->next)
351 if ( ( 0 == nfl->active ) && ( NULL != nfl->unloader ) )
353 fl->next = nfl->next;
355 (nfl->unloader)(nfl->f);
357 freez(nfl->filename);
366 /*********************************************************************
368 * Function : create_url_spec
370 * Description : Creates a "url_spec" structure from a string.
371 * When finished, free with unload_url().
374 * 1 : url = Target url_spec to be filled in. Must be
375 * zeroed out before the call (e.g. using zalloc).
376 * 2 : buf = Source pattern, null terminated. NOTE: The
377 * contents of this buffer are destroyed by this
378 * function. If this function succeeds, the
379 * buffer is copied to url->spec. If this
380 * function fails, the contents of the buffer
383 * Returns : JB_ERR_OK - Success
384 * JB_ERR_MEMORY - Out of memory
385 * JB_ERR_PARSE - Cannot parse regex (Detailed message
386 * written to system log)
388 *********************************************************************/
389 jb_err create_url_spec(struct url_spec * url, char * buf)
392 struct url_spec tmp_url[1];
397 /* save a copy of the orignal specification */
398 if ((url->spec = strdup(buf)) == NULL)
400 return JB_ERR_MEMORY;
403 if ((p = strchr(buf, '/')))
405 if (NULL == (url->path = strdup(p)))
408 return JB_ERR_MEMORY;
410 url->pathlen = strlen(url->path);
422 char rebuf[BUFFER_SIZE];
424 if (NULL == (url->preg = zalloc(sizeof(*url->preg))))
428 return JB_ERR_MEMORY;
431 sprintf(rebuf, "^(%s)", url->path);
433 errcode = regcomp(url->preg, rebuf,
434 (REG_EXTENDED|REG_NOSUB|REG_ICASE));
437 size_t errlen = regerror(errcode,
438 url->preg, rebuf, sizeof(rebuf));
440 if (errlen > (sizeof(rebuf) - (size_t)1))
442 errlen = sizeof(rebuf) - (size_t)1;
444 rebuf[errlen] = '\0';
446 log_error(LOG_LEVEL_ERROR, "error compiling %s: %s",
457 if ((p = strchr(buf, ':')) == NULL)
467 if ((url->domain = strdup(buf)) == NULL)
473 #endif /* def REGEX */
474 return JB_ERR_MEMORY;
477 /* split domain into components */
479 *tmp_url = dsplit(url->domain);
480 if (tmp_url->dbuf == NULL)
487 #endif /* def REGEX */
488 return JB_ERR_MEMORY;
491 url->dbuf = tmp_url->dbuf;
492 url->dcnt = tmp_url->dcnt;
493 url->dvec = tmp_url->dvec;
494 url->unanchored = tmp_url->unanchored;
501 /*********************************************************************
503 * Function : free_url
505 * Description : Called from the "unloaders". Freez the url
506 * structure elements.
509 * 1 : url = pointer to a url_spec structure.
513 *********************************************************************/
514 void free_url(struct url_spec *url)
516 if (url == NULL) return;
534 /*********************************************************************
536 * Function : check_file_changed
538 * Description : Helper function to check if a file needs reloading.
539 * If "current" is still current, return it. Otherwise
540 * allocates a new (zeroed) "struct file_list", fills
541 * in the disk file name and timestamp, and returns it.
544 * 1 : current = The file_list currently being used - will
545 * be checked to see if it is out of date.
546 * May be NULL (which is treated as out of
548 * 2 : filename = Name of file to check.
549 * 3 : newfl = New file list. [Output only]
550 * This will be set to NULL, OR a struct
551 * file_list newly allocated on the
552 * heap, with the filename and lastmodified
553 * fields filled, and all others zeroed.
555 * Returns : If file unchanged: 0 (and sets newfl == NULL)
556 * If file changed: 1 and sets newfl != NULL
557 * On error: 1 and sets newfl == NULL
559 *********************************************************************/
560 int check_file_changed(const struct file_list * current,
561 const char * filename,
562 struct file_list ** newfl)
564 struct file_list *fs;
565 struct stat statbuf[1];
569 if (stat(filename, statbuf) < 0)
571 /* Error, probably file not found. */
576 && (current->lastmodified == statbuf->st_mtime)
577 && (0 == strcmp(current->filename, filename)))
579 /* force reload of configfile and all the logs */
580 if ( !MustReload ) return 0;
583 fs = (struct file_list *)zalloc(sizeof(struct file_list));
586 /* Out of memory error */
590 fs->filename = strdup(filename);
591 fs->lastmodified = statbuf->st_mtime;
593 if (fs->filename == NULL)
595 /* Out of memory error */
604 /*********************************************************************
606 * Function : read_config_line
608 * Description : Read a single non-empty line from a file and return
609 * it. Trims comments, leading and trailing whitespace
610 * and respects escaping of newline and comment char.
613 * 1 : buf = Buffer to use.
614 * 2 : buflen = Size of buffer in bytes.
615 * 3 : fp = File to read from
616 * 4 : linenum = linenumber in file
618 * Returns : NULL on EOF or error
619 * Otherwise, returns buf.
621 *********************************************************************/
622 char *read_config_line(char *buf, int buflen, FILE *fp, unsigned long *linenum)
627 char linebuf[BUFFER_SIZE];
632 while (fgets(linebuf, sizeof(linebuf), fp))
635 /* Trim off newline */
636 if ((p = strpbrk(linebuf, "\r\n")) != NULL)
642 p = linebuf + strlen(linebuf);
645 /* Line continuation? Trim escape and set flag. */
646 if ((p != linebuf) && (*--p == '\\'))
652 /* If there's a comment char.. */
654 while ((p = strchr(p, '#')) != NULL)
656 /* ..and it's escaped, left-shift the line over the escape. */
657 if ((p != linebuf) && (*(p-1) == '\\'))
661 while ((*dest++ = *src++) != '\0')
665 /* Now scan from just after the "#". */
667 /* Else, chop off the rest of the line */
674 /* Write to the buffer */
677 strncat(buf, linebuf, buflen - strlen(buf));
687 /* Remove leading and trailing whitespace */
703 /*********************************************************************
705 * Function : unload_trustfile
707 * Description : Unloads a trustfile.
710 * 1 : f = the data structure associated with the trustfile.
714 *********************************************************************/
715 static void unload_trustfile(void *f)
717 struct block_spec *b = (struct block_spec *)f;
718 if (b == NULL) return;
720 unload_trustfile(b->next); /* Stack is cheap, isn't it? */
729 /*********************************************************************
731 * Function : load_trustfile
733 * Description : Read and parse a trustfile and add to files list.
736 * 1 : csp = Current client state (buffers, headers, etc...)
738 * Returns : 0 => Ok, everything else is an error.
740 *********************************************************************/
741 int load_trustfile(struct client_state *csp)
745 struct block_spec *b, *bl;
746 struct url_spec **tl;
748 char buf[BUFFER_SIZE], *p, *q;
750 struct file_list *fs;
751 unsigned long linenum = 0;
753 if (!check_file_changed(current_trustfile, csp->config->trustfile, &fs))
755 /* No need to load */
758 csp->tlist = current_trustfile;
764 goto load_trustfile_error;
767 fs->f = bl = (struct block_spec *)zalloc(sizeof(*bl));
770 goto load_trustfile_error;
773 if ((fp = fopen(csp->config->trustfile, "r")) == NULL)
775 goto load_trustfile_error;
778 tl = csp->config->trust_list;
780 while (read_config_line(buf, sizeof(buf), fp, &linenum) != NULL)
796 while ((*p++ = *q++))
802 /* skip blank lines */
808 /* allocate a new node */
809 if ((b = zalloc(sizeof(*b))) == NULL)
812 goto load_trustfile_error;
815 /* add it to the list */
821 /* Save the URL pattern */
822 if (create_url_spec(b->url, buf))
825 goto load_trustfile_error;
829 * save a pointer to URL's spec in the list of trusted URL's, too
841 /* the old one is now obsolete */
842 if (current_trustfile)
844 current_trustfile->unloader = unload_trustfile;
847 fs->next = files->next;
849 current_trustfile = fs;
858 load_trustfile_error:
859 log_error(LOG_LEVEL_FATAL, "can't load trustfile '%s': %E",
860 csp->config->trustfile);
864 #endif /* def FEATURE_TRUST */
867 /*********************************************************************
869 * Function : unload_re_filterfile
871 * Description : Unload the re_filter list.
874 * 1 : f = the data structure associated with the filterfile.
878 *********************************************************************/
879 static void unload_re_filterfile(void *f)
881 struct re_filterfile_spec *b = (struct re_filterfile_spec *)f;
888 destroy_list(b->patterns);
889 pcrs_free_joblist(b->joblist);
895 /*********************************************************************
897 * Function : load_re_filterfile
899 * Description : Load the re_filterfile. Each non-comment, non-empty
900 * line is instantly added to the joblist, which is
901 * a chained list of pcrs_job structs.
904 * 1 : csp = Current client state (buffers, headers, etc...)
906 * Returns : 0 => Ok, everything else is an error.
908 *********************************************************************/
909 int load_re_filterfile(struct client_state *csp)
913 struct re_filterfile_spec *bl;
914 struct file_list *fs;
916 char buf[BUFFER_SIZE];
918 unsigned long linenum = 0;
921 if (!check_file_changed(current_re_filterfile, csp->config->re_filterfile, &fs))
923 /* No need to load */
926 csp->rlist = current_re_filterfile;
932 goto load_re_filterfile_error;
935 fs->f = bl = (struct re_filterfile_spec *)zalloc(sizeof(*bl));
938 goto load_re_filterfile_error;
941 /* Open the file or fail */
942 if ((fp = fopen(csp->config->re_filterfile, "r")) == NULL)
944 goto load_re_filterfile_error;
947 /* Read line by line */
948 while (read_config_line(buf, sizeof(buf), fp, &linenum) != NULL)
950 enlist( bl->patterns, buf );
952 /* We have a meaningful line -> make it a job */
953 if ((dummy = pcrs_compile_command(buf, &error)) == NULL)
955 log_error(LOG_LEVEL_RE_FILTER,
956 "Adding re_filter job %s failed with error %d.", buf, error);
961 dummy->next = bl->joblist;
963 log_error(LOG_LEVEL_RE_FILTER, "Adding re_filter job %s succeeded.", buf);
969 /* the old one is now obsolete */
970 if ( NULL != current_re_filterfile )
972 current_re_filterfile->unloader = unload_re_filterfile;
975 fs->next = files->next;
977 current_re_filterfile = fs;
986 load_re_filterfile_error:
987 log_error(LOG_LEVEL_FATAL, "can't load re_filterfile '%s': %E",
988 csp->config->re_filterfile);
994 /*********************************************************************
996 * Function : add_loader
998 * Description : Called from `load_config'. Called once for each input
999 * file found in config.
1002 * 1 : loader = pointer to a function that can parse and load
1003 * the appropriate config file.
1004 * 2 : config = The configuration_spec to add the loader to.
1008 *********************************************************************/
1009 void add_loader(int (*loader)(struct client_state *),
1010 struct configuration_spec * config)
1014 for (i=0; i < NLOADERS; i++)
1016 if (config->loaders[i] == NULL)
1018 config->loaders[i] = loader;
1026 /*********************************************************************
1028 * Function : run_loader
1030 * Description : Called from `load_config' and `listen_loop'. This
1031 * function keeps the "csp" current with any file mods
1032 * since the last loop. If a file is unchanged, the
1033 * loader functions do NOT reload the file.
1036 * 1 : csp = Current client state (buffers, headers, etc...)
1037 * Must be non-null. Reads: "csp->config"
1038 * Writes: various data members.
1040 * Returns : 0 => Ok, everything else is an error.
1042 *********************************************************************/
1043 int run_loader(struct client_state *csp)
1048 for (i=0; i < NLOADERS; i++)
1050 if (csp->config->loaders[i] == NULL)
1054 ret |= (csp->config->loaders[i])(csp);