1 const char loaders_rcs[] = "$Id: loaders.c,v 1.24 2001/07/30 22:08:36 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.24 2001/07/30 22:08:36 jongfoster
39 * Tidying up #defines:
40 * - All feature #defines are now of the form FEATURE_xxx
41 * - Permanently turned off WIN_GUI_EDIT
42 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
44 * Revision 1.23 2001/07/20 15:51:54 oes
45 * Fixed indentation of prepocessor commands
47 * Revision 1.22 2001/07/20 15:16:17 haroon
48 * - per Guy's suggestion, added a while loop in sweep() to catch not just
49 * the last inactive CSP but all other consecutive inactive CSPs after that
52 * Revision 1.21 2001/07/18 17:26:24 oes
53 * Changed to conform to new pcrs interface
55 * Revision 1.20 2001/07/17 13:07:01 oes
56 * Fixed segv when last line in config files
57 * lacked a terminating (\r)\n
59 * Revision 1.19 2001/07/13 14:01:54 oes
60 * Removed all #ifdef PCRS
62 * Revision 1.18 2001/06/29 21:45:41 oes
63 * Indentation, CRLF->LF, Tab-> Space
65 * Revision 1.17 2001/06/29 13:31:51 oes
68 * Revision 1.16 2001/06/09 10:55:28 jongfoster
69 * Changing BUFSIZ ==> BUFFER_SIZE
71 * Revision 1.15 2001/06/07 23:14:14 jongfoster
72 * Removing ACL and forward file loaders - these
73 * files have been merged into the config file.
74 * Cosmetic: Moving unloader funcs next to their
75 * respective loader funcs
77 * Revision 1.14 2001/06/01 03:27:04 oes
78 * Fixed line continuation problem
80 * Revision 1.13 2001/05/31 21:28:49 jongfoster
81 * Removed all permissionsfile code - it's now called the actions
82 * file, and (almost) all the code is in actions.c
84 * Revision 1.12 2001/05/31 17:32:31 oes
86 * - Enhanced domain part globbing with infix and prefix asterisk
87 * matching and optional unanchored operation
89 * Revision 1.11 2001/05/29 23:25:24 oes
91 * - load_config_line() and load_permissions_file() now use chomp()
93 * Revision 1.10 2001/05/29 09:50:24 jongfoster
94 * Unified blocklist/imagelist/permissionslist.
95 * File format is still under discussion, but the internal changes
98 * Also modified interceptor behaviour:
99 * - We now intercept all URLs beginning with one of the following
100 * prefixes (and *only* these prefixes):
102 * * http://ijbswa.sf.net/config/
103 * * http://ijbswa.sourceforge.net/config/
104 * - New interceptors "home page" - go to http://i.j.b/ to see it.
105 * - Internal changes so that intercepted and fast redirect pages
106 * are not replaced with an image.
107 * - Interceptors now have the option to send a binary page direct
108 * to the client. (i.e. ijb-send-banner uses this)
109 * - Implemented show-url-info interceptor. (Which is why I needed
110 * the above interceptors changes - a typical URL is
111 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
112 * The previous mechanism would not have intercepted that, and
113 * if it had been intercepted then it then it would have replaced
116 * Revision 1.9 2001/05/26 17:12:07 jongfoster
117 * Fatal errors loading configuration files now give better error messages.
119 * Revision 1.8 2001/05/26 00:55:20 jongfoster
120 * Removing duplicated code. load_forwardfile() now uses create_url_spec()
122 * Revision 1.7 2001/05/26 00:28:36 jongfoster
123 * Automatic reloading of config file.
124 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
125 * Most of the global variables have been moved to a new
126 * struct configuration_spec, accessed through csp->config->globalname
127 * Most of the globals remaining are used by the Win32 GUI.
129 * Revision 1.6 2001/05/23 12:27:33 oes
131 * Fixed ugly indentation of my last changes
133 * Revision 1.5 2001/05/23 10:39:05 oes
134 * - Added support for escaping the comment character
135 * in config files by a backslash
136 * - Added support for line continuation in config
138 * - Fixed a buffer overflow bug with long config lines
140 * Revision 1.4 2001/05/22 18:56:28 oes
143 * Revision 1.3 2001/05/20 01:21:20 jongfoster
144 * Version 2.9.4 checkin.
145 * - Merged popupfile and cookiefile, and added control over PCRS
146 * filtering, in new "permissionsfile".
147 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
148 * file error you now get a message box (in the Win32 GUI) rather
149 * than the program exiting with no explanation.
150 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
152 * - Removed tabs from "config"
153 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
154 * - Bumped up version number.
156 * Revision 1.2 2001/05/17 23:01:01 oes
157 * - Cleaned CRLF's from the sources and related files
159 * Revision 1.1.1.1 2001/05/15 13:58:59 oes
160 * Initial import of version 2.9.3 source tree
163 *********************************************************************/
170 #include <sys/types.h>
174 #include <sys/stat.h>
189 #include "miscutil.h"
194 const char loaders_h_rcs[] = LOADERS_H_VERSION;
196 /* Fix a problem with Solaris. There should be no effect on other
198 * Solaris's isspace() is a macro which uses it's argument directly
199 * as an array index. Therefore we need to make sure that high-bit
200 * characters generate +ve values, and ideally we also want to make
201 * the argument match the declared parameter type of "int".
203 #define ijb_isspace(__X) isspace((int)(unsigned char)(__X))
207 * Currently active files.
208 * These are also entered in the main linked list of files.
212 static struct file_list *current_trustfile = NULL;
213 #endif /* def FEATURE_TRUST */
215 static struct file_list *current_re_filterfile = NULL;
219 /*********************************************************************
223 * Description : Basically a mark and sweep garbage collector, it is run
224 * (by the parent thread) every once in a while to reclaim memory.
226 * It uses a mark and sweep strategy:
227 * 1) mark all files as inactive
229 * 2) check with each client:
230 * if it is active, mark its files as active
231 * if it is inactive, free its resources
233 * 3) free the resources of all of the files that
234 * are still marked as inactive (and are obsolete).
236 * N.B. files that are not obsolete don't have an unloader defined.
242 *********************************************************************/
245 struct file_list *fl, *nfl;
246 struct client_state *csp, *ncsp;
248 /* clear all of the file's active flags */
249 for ( fl = files->next; NULL != fl; fl = fl->next )
254 for (csp = clients; csp && (ncsp = csp->next) ; csp = csp->next)
258 /* mark this client's files as active */
261 * Always have a configuration file.
262 * (Also note the slightly non-standard extra
265 ncsp->config->config_file_list->active = 1;
267 if (ncsp->actions_list) /* actions files */
269 ncsp->actions_list->active = 1;
272 if (ncsp->rlist) /* pcrsjob files */
274 ncsp->rlist->active = 1;
278 if (ncsp->tlist) /* trust files */
280 ncsp->tlist->active = 1;
282 #endif /* def FEATURE_TRUST */
287 * this client is not active, release its resources
288 * and the ones of all inactive clients that might
292 while( !ncsp->active )
294 csp->next = ncsp->next;
296 freez(ncsp->ip_addr_str);
297 freez(ncsp->my_ip_addr_str);
298 freez(ncsp->my_hostname);
301 freez(ncsp->referrer);
302 #endif /* def FEATURE_TRUST */
303 freez(ncsp->x_forwarded);
304 freez(ncsp->iob->buf);
306 free_http_request(ncsp->http);
308 destroy_list(ncsp->headers);
309 destroy_list(ncsp->cookie_list);
311 free_current_action(ncsp->action);
313 #ifdef FEATURE_STATISTICS
319 #endif /* def FEATURE_STATISTICS */
323 /* are there any more in sequence after it? */
324 if( !(ncsp = csp->next) )
330 for (fl = files; fl && (nfl = fl->next) ; fl = fl->next)
332 if ( ( 0 == nfl->active ) && ( NULL != nfl->unloader ) )
334 fl->next = nfl->next;
336 (nfl->unloader)(nfl->f);
338 freez(nfl->filename);
347 /*********************************************************************
349 * Function : create_url_spec
351 * Description : Creates a "url_spec" structure from a string.
352 * When finished, free with unload_url().
355 * 1 : url = Target url_spec to be filled in. Must be
356 * zeroed out before the call (e.g. using zalloc).
357 * 2 : buf = Source pattern, null terminated. NOTE: The
358 * contents of this buffer are destroyed by this
359 * function. If this function succeeds, the
360 * buffer is copied to url->spec. If this
361 * function fails, the contents of the buffer
364 * Returns : 0 => Ok, everything else is an error.
366 *********************************************************************/
367 int create_url_spec(struct url_spec * url, char * buf)
370 struct url_spec tmp_url[1];
372 /* paranoia - should never happen. */
373 if ((url == NULL) || (buf == NULL))
378 /* save a copy of the orignal specification */
379 if ((url->spec = strdup(buf)) == NULL)
384 if ((p = strchr(buf, '/')))
386 if (NULL == (url->path = strdup(p)))
391 url->pathlen = strlen(url->path);
403 char rebuf[BUFFER_SIZE];
405 if (NULL == (url->preg = zalloc(sizeof(*url->preg))))
412 sprintf(rebuf, "^(%s)", url->path);
414 errcode = regcomp(url->preg, rebuf,
415 (REG_EXTENDED|REG_NOSUB|REG_ICASE));
418 size_t errlen = regerror(errcode,
419 url->preg, buf, sizeof(buf));
423 log_error(LOG_LEVEL_ERROR, "error compiling %s: %s",
434 if ((p = strchr(buf, ':')) == NULL)
444 if ((url->domain = strdup(buf)) == NULL)
450 #endif /* def REGEX */
454 /* split domain into components */
456 *tmp_url = dsplit(url->domain);
457 url->dbuf = tmp_url->dbuf;
458 url->dcnt = tmp_url->dcnt;
459 url->dvec = tmp_url->dvec;
460 url->unanchored = tmp_url->unanchored;
467 /*********************************************************************
469 * Function : free_url
471 * Description : Called from the "unloaders". Freez the url
472 * structure elements.
475 * 1 : url = pointer to a url_spec structure.
479 *********************************************************************/
480 void free_url(struct url_spec *url)
482 if (url == NULL) return;
500 /*********************************************************************
502 * Function : check_file_changed
504 * Description : Helper function to check if a file needs reloading.
505 * If "current" is still current, return it. Otherwise
506 * allocates a new (zeroed) "struct file_list", fills
507 * in the disk file name and timestamp, and returns it.
510 * 1 : current = The file_list currently being used - will
511 * be checked to see if it is out of date.
512 * May be NULL (which is treated as out of
514 * 2 : filename = Name of file to check.
515 * 3 : newfl = New file list. [Output only]
516 * This will be set to NULL, OR a struct
517 * file_list newly allocated on the
518 * heap, with the filename and lastmodified
519 * fields filled, and all others zeroed.
521 * Returns : If file unchanged: 0 (and sets newfl == NULL)
522 * If file changed: 1 and sets newfl != NULL
523 * On error: 1 and sets newfl == NULL
525 *********************************************************************/
526 int check_file_changed(const struct file_list * current,
527 const char * filename,
528 struct file_list ** newfl)
530 struct file_list *fs;
531 struct stat statbuf[1];
535 if (stat(filename, statbuf) < 0)
537 /* Error, probably file not found. */
542 && (current->lastmodified == statbuf->st_mtime)
543 && (0 == strcmp(current->filename, filename)))
548 fs = (struct file_list *)zalloc(sizeof(struct file_list));
552 /* Out of memory error */
556 fs->filename = strdup(filename);
557 fs->lastmodified = statbuf->st_mtime;
559 if (fs->filename == NULL)
561 /* Out of memory error */
573 /*********************************************************************
575 * Function : read_config_line
577 * Description : Read a single non-empty line from a file and return
578 * it. Trims comments, leading and trailing whitespace
579 * and respects escaping of newline and comment char.
580 * Also writes the file to fs->proxy_args.
583 * 1 : buf = Buffer to use.
584 * 2 : buflen = Size of buffer in bytes.
585 * 3 : fp = File to read from
586 * 4 : fs = File will be written to fs->proxy_args. May
587 * be NULL to disable this feature.
589 * Returns : NULL on EOF or error
590 * Otherwise, returns buf.
592 *********************************************************************/
593 char *read_config_line(char *buf, int buflen, FILE *fp, struct file_list *fs)
596 char linebuf[BUFFER_SIZE];
601 while (fgets(linebuf, sizeof(linebuf), fp))
603 /* Trim off newline */
604 if ((p = strpbrk(linebuf, "\r\n")) != NULL)
610 p = linebuf + strlen(linebuf);
613 /* Line continuation? Trim escape and set flag. */
614 if ((p != linebuf) && (*--p == '\\'))
620 /* If there's a comment char.. */
621 if ((p = strpbrk(linebuf, "#")) != NULL)
623 /* ..and it's escaped, left-shift the line over the escape. */
624 if ((p != linebuf) && (*(p-1) == '\\'))
627 while ((*q++ = *p++) != '\0') /* nop */;
629 /* Else, chop off the rest of the line */
636 /* Write to the buffer */
639 strncat(buf, linebuf, buflen - strlen(buf));
649 /* Remove leading and trailing whitespace */
665 /*********************************************************************
667 * Function : unload_trustfile
669 * Description : Unloads a trustfile.
672 * 1 : f = the data structure associated with the trustfile.
676 *********************************************************************/
677 static void unload_trustfile(void *f)
679 struct block_spec *b = (struct block_spec *)f;
680 if (b == NULL) return;
682 unload_trustfile(b->next); /* Stack is cheap, isn't it? */
691 /*********************************************************************
693 * Function : load_trustfile
695 * Description : Read and parse a trustfile and add to files list.
698 * 1 : csp = Current client state (buffers, headers, etc...)
700 * Returns : 0 => Ok, everything else is an error.
702 *********************************************************************/
703 int load_trustfile(struct client_state *csp)
707 struct block_spec *b, *bl;
708 struct url_spec **tl;
710 char buf[BUFFER_SIZE], *p, *q;
712 struct file_list *fs;
714 if (!check_file_changed(current_trustfile, csp->config->trustfile, &fs))
716 /* No need to load */
719 csp->tlist = current_trustfile;
725 goto load_trustfile_error;
728 fs->f = bl = (struct block_spec *)zalloc(sizeof(*bl));
731 goto load_trustfile_error;
734 if ((fp = fopen(csp->config->trustfile, "r")) == NULL)
736 goto load_trustfile_error;
739 tl = csp->config->trust_list;
741 while (read_config_line(buf, sizeof(buf), fp, fs) != NULL)
757 while ((*p++ = *q++))
763 /* skip blank lines */
769 /* allocate a new node */
770 if ((b = zalloc(sizeof(*b))) == NULL)
773 goto load_trustfile_error;
776 /* add it to the list */
782 /* Save the URL pattern */
783 if (create_url_spec(b->url, buf))
786 goto load_trustfile_error;
790 * save a pointer to URL's spec in the list of trusted URL's, too
802 /* the old one is now obsolete */
803 if (current_trustfile)
805 current_trustfile->unloader = unload_trustfile;
808 fs->next = files->next;
810 current_trustfile = fs;
819 load_trustfile_error:
820 log_error(LOG_LEVEL_FATAL, "can't load trustfile '%s': %E",
821 csp->config->trustfile);
825 #endif /* def FEATURE_TRUST */
828 /*********************************************************************
830 * Function : unload_re_filterfile
832 * Description : Unload the re_filter list.
835 * 1 : f = the data structure associated with the filterfile.
839 *********************************************************************/
840 static void unload_re_filterfile(void *f)
842 struct re_filterfile_spec *b = (struct re_filterfile_spec *)f;
849 destroy_list(b->patterns);
850 pcrs_free_joblist(b->joblist);
856 /*********************************************************************
858 * Function : load_re_filterfile
860 * Description : Load the re_filterfile. Each non-comment, non-empty
861 * line is instantly added to the joblist, which is
862 * a chained list of pcrs_job structs.
865 * 1 : csp = Current client state (buffers, headers, etc...)
867 * Returns : 0 => Ok, everything else is an error.
869 *********************************************************************/
870 int load_re_filterfile(struct client_state *csp)
874 struct re_filterfile_spec *bl;
875 struct file_list *fs;
877 char buf[BUFFER_SIZE];
881 if (!check_file_changed(current_re_filterfile, csp->config->re_filterfile, &fs))
883 /* No need to load */
886 csp->rlist = current_re_filterfile;
892 goto load_re_filterfile_error;
895 fs->f = bl = (struct re_filterfile_spec *)zalloc(sizeof(*bl));
898 goto load_re_filterfile_error;
901 /* Open the file or fail */
902 if ((fp = fopen(csp->config->re_filterfile, "r")) == NULL)
904 goto load_re_filterfile_error;
907 /* Read line by line */
908 while (read_config_line(buf, sizeof(buf), fp, fs) != NULL)
910 enlist( bl->patterns, buf );
912 /* We have a meaningful line -> make it a job */
913 if ((dummy = pcrs_compile_command(buf, &error)) == NULL)
915 log_error(LOG_LEVEL_RE_FILTER,
916 "Adding re_filter job %s failed with error %d.", buf, error);
921 dummy->next = bl->joblist;
923 log_error(LOG_LEVEL_RE_FILTER, "Adding re_filter job %s succeeded.", buf);
929 /* the old one is now obsolete */
930 if ( NULL != current_re_filterfile )
932 current_re_filterfile->unloader = unload_re_filterfile;
935 fs->next = files->next;
937 current_re_filterfile = fs;
946 load_re_filterfile_error:
947 log_error(LOG_LEVEL_FATAL, "can't load re_filterfile '%s': %E",
948 csp->config->re_filterfile);
954 /*********************************************************************
956 * Function : add_loader
958 * Description : Called from `load_config'. Called once for each input
959 * file found in config.
962 * 1 : loader = pointer to a function that can parse and load
963 * the appropriate config file.
964 * 2 : config = The configuration_spec to add the loader to.
968 *********************************************************************/
969 void add_loader(int (*loader)(struct client_state *),
970 struct configuration_spec * config)
974 for (i=0; i < NLOADERS; i++)
976 if (config->loaders[i] == NULL)
978 config->loaders[i] = loader;
986 /*********************************************************************
988 * Function : run_loader
990 * Description : Called from `load_config' and `listen_loop'. This
991 * function keeps the "csp" current with any file mods
992 * since the last loop. If a file is unchanged, the
993 * loader functions do NOT reload the file.
996 * 1 : csp = Current client state (buffers, headers, etc...)
997 * Must be non-null. Reads: "csp->config"
998 * Writes: various data members.
1000 * Returns : 0 => Ok, everything else is an error.
1002 *********************************************************************/
1003 int run_loader(struct client_state *csp)
1008 for (i=0; i < NLOADERS; i++)
1010 if (csp->config->loaders[i] == NULL)
1014 ret |= (csp->config->loaders[i])(csp);