1 const char loaders_rcs[] = "$Id: loaders.c,v 1.25 2001/09/13 22:44:03 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.25 2001/09/13 22:44:03 jongfoster
39 * Adding {} to an if statement
41 * Revision 1.24 2001/07/30 22:08:36 jongfoster
42 * Tidying up #defines:
43 * - All feature #defines are now of the form FEATURE_xxx
44 * - Permanently turned off WIN_GUI_EDIT
45 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
47 * Revision 1.23 2001/07/20 15:51:54 oes
48 * Fixed indentation of prepocessor commands
50 * Revision 1.22 2001/07/20 15:16:17 haroon
51 * - per Guy's suggestion, added a while loop in sweep() to catch not just
52 * the last inactive CSP but all other consecutive inactive CSPs after that
55 * Revision 1.21 2001/07/18 17:26:24 oes
56 * Changed to conform to new pcrs interface
58 * Revision 1.20 2001/07/17 13:07:01 oes
59 * Fixed segv when last line in config files
60 * lacked a terminating (\r)\n
62 * Revision 1.19 2001/07/13 14:01:54 oes
63 * Removed all #ifdef PCRS
65 * Revision 1.18 2001/06/29 21:45:41 oes
66 * Indentation, CRLF->LF, Tab-> Space
68 * Revision 1.17 2001/06/29 13:31:51 oes
71 * Revision 1.16 2001/06/09 10:55:28 jongfoster
72 * Changing BUFSIZ ==> BUFFER_SIZE
74 * Revision 1.15 2001/06/07 23:14:14 jongfoster
75 * Removing ACL and forward file loaders - these
76 * files have been merged into the config file.
77 * Cosmetic: Moving unloader funcs next to their
78 * respective loader funcs
80 * Revision 1.14 2001/06/01 03:27:04 oes
81 * Fixed line continuation problem
83 * Revision 1.13 2001/05/31 21:28:49 jongfoster
84 * Removed all permissionsfile code - it's now called the actions
85 * file, and (almost) all the code is in actions.c
87 * Revision 1.12 2001/05/31 17:32:31 oes
89 * - Enhanced domain part globbing with infix and prefix asterisk
90 * matching and optional unanchored operation
92 * Revision 1.11 2001/05/29 23:25:24 oes
94 * - load_config_line() and load_permissions_file() now use chomp()
96 * Revision 1.10 2001/05/29 09:50:24 jongfoster
97 * Unified blocklist/imagelist/permissionslist.
98 * File format is still under discussion, but the internal changes
101 * Also modified interceptor behaviour:
102 * - We now intercept all URLs beginning with one of the following
103 * prefixes (and *only* these prefixes):
105 * * http://ijbswa.sf.net/config/
106 * * http://ijbswa.sourceforge.net/config/
107 * - New interceptors "home page" - go to http://i.j.b/ to see it.
108 * - Internal changes so that intercepted and fast redirect pages
109 * are not replaced with an image.
110 * - Interceptors now have the option to send a binary page direct
111 * to the client. (i.e. ijb-send-banner uses this)
112 * - Implemented show-url-info interceptor. (Which is why I needed
113 * the above interceptors changes - a typical URL is
114 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
115 * The previous mechanism would not have intercepted that, and
116 * if it had been intercepted then it then it would have replaced
119 * Revision 1.9 2001/05/26 17:12:07 jongfoster
120 * Fatal errors loading configuration files now give better error messages.
122 * Revision 1.8 2001/05/26 00:55:20 jongfoster
123 * Removing duplicated code. load_forwardfile() now uses create_url_spec()
125 * Revision 1.7 2001/05/26 00:28:36 jongfoster
126 * Automatic reloading of config file.
127 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
128 * Most of the global variables have been moved to a new
129 * struct configuration_spec, accessed through csp->config->globalname
130 * Most of the globals remaining are used by the Win32 GUI.
132 * Revision 1.6 2001/05/23 12:27:33 oes
134 * Fixed ugly indentation of my last changes
136 * Revision 1.5 2001/05/23 10:39:05 oes
137 * - Added support for escaping the comment character
138 * in config files by a backslash
139 * - Added support for line continuation in config
141 * - Fixed a buffer overflow bug with long config lines
143 * Revision 1.4 2001/05/22 18:56:28 oes
146 * Revision 1.3 2001/05/20 01:21:20 jongfoster
147 * Version 2.9.4 checkin.
148 * - Merged popupfile and cookiefile, and added control over PCRS
149 * filtering, in new "permissionsfile".
150 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
151 * file error you now get a message box (in the Win32 GUI) rather
152 * than the program exiting with no explanation.
153 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
155 * - Removed tabs from "config"
156 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
157 * - Bumped up version number.
159 * Revision 1.2 2001/05/17 23:01:01 oes
160 * - Cleaned CRLF's from the sources and related files
162 * Revision 1.1.1.1 2001/05/15 13:58:59 oes
163 * Initial import of version 2.9.3 source tree
166 *********************************************************************/
173 #include <sys/types.h>
177 #include <sys/stat.h>
190 #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)
598 char linebuf[BUFFER_SIZE];
603 while (fgets(linebuf, sizeof(linebuf), fp))
605 /* Trim off newline */
606 if ((p = strpbrk(linebuf, "\r\n")) != NULL)
612 p = linebuf + strlen(linebuf);
615 /* Line continuation? Trim escape and set flag. */
616 if ((p != linebuf) && (*--p == '\\'))
622 /* If there's a comment char.. */
624 while ((p = strchr(p, '#')) != NULL)
626 /* ..and it's escaped, left-shift the line over the escape. */
627 if ((p != linebuf) && (*(p-1) == '\\'))
631 while ((*dest++ = *src++) != '\0')
635 /* Now scan from just after the "#". */
637 /* Else, chop off the rest of the line */
644 /* Write to the buffer */
647 strncat(buf, linebuf, buflen - strlen(buf));
657 /* Remove leading and trailing whitespace */
673 /*********************************************************************
675 * Function : unload_trustfile
677 * Description : Unloads a trustfile.
680 * 1 : f = the data structure associated with the trustfile.
684 *********************************************************************/
685 static void unload_trustfile(void *f)
687 struct block_spec *b = (struct block_spec *)f;
688 if (b == NULL) return;
690 unload_trustfile(b->next); /* Stack is cheap, isn't it? */
699 /*********************************************************************
701 * Function : load_trustfile
703 * Description : Read and parse a trustfile and add to files list.
706 * 1 : csp = Current client state (buffers, headers, etc...)
708 * Returns : 0 => Ok, everything else is an error.
710 *********************************************************************/
711 int load_trustfile(struct client_state *csp)
715 struct block_spec *b, *bl;
716 struct url_spec **tl;
718 char buf[BUFFER_SIZE], *p, *q;
720 struct file_list *fs;
722 if (!check_file_changed(current_trustfile, csp->config->trustfile, &fs))
724 /* No need to load */
727 csp->tlist = current_trustfile;
733 goto load_trustfile_error;
736 fs->f = bl = (struct block_spec *)zalloc(sizeof(*bl));
739 goto load_trustfile_error;
742 if ((fp = fopen(csp->config->trustfile, "r")) == NULL)
744 goto load_trustfile_error;
747 tl = csp->config->trust_list;
749 while (read_config_line(buf, sizeof(buf), fp, fs) != NULL)
765 while ((*p++ = *q++))
771 /* skip blank lines */
777 /* allocate a new node */
778 if ((b = zalloc(sizeof(*b))) == NULL)
781 goto load_trustfile_error;
784 /* add it to the list */
790 /* Save the URL pattern */
791 if (create_url_spec(b->url, buf))
794 goto load_trustfile_error;
798 * save a pointer to URL's spec in the list of trusted URL's, too
810 /* the old one is now obsolete */
811 if (current_trustfile)
813 current_trustfile->unloader = unload_trustfile;
816 fs->next = files->next;
818 current_trustfile = fs;
827 load_trustfile_error:
828 log_error(LOG_LEVEL_FATAL, "can't load trustfile '%s': %E",
829 csp->config->trustfile);
833 #endif /* def FEATURE_TRUST */
836 /*********************************************************************
838 * Function : unload_re_filterfile
840 * Description : Unload the re_filter list.
843 * 1 : f = the data structure associated with the filterfile.
847 *********************************************************************/
848 static void unload_re_filterfile(void *f)
850 struct re_filterfile_spec *b = (struct re_filterfile_spec *)f;
857 destroy_list(b->patterns);
858 pcrs_free_joblist(b->joblist);
864 /*********************************************************************
866 * Function : load_re_filterfile
868 * Description : Load the re_filterfile. Each non-comment, non-empty
869 * line is instantly added to the joblist, which is
870 * a chained list of pcrs_job structs.
873 * 1 : csp = Current client state (buffers, headers, etc...)
875 * Returns : 0 => Ok, everything else is an error.
877 *********************************************************************/
878 int load_re_filterfile(struct client_state *csp)
882 struct re_filterfile_spec *bl;
883 struct file_list *fs;
885 char buf[BUFFER_SIZE];
889 if (!check_file_changed(current_re_filterfile, csp->config->re_filterfile, &fs))
891 /* No need to load */
894 csp->rlist = current_re_filterfile;
900 goto load_re_filterfile_error;
903 fs->f = bl = (struct re_filterfile_spec *)zalloc(sizeof(*bl));
906 goto load_re_filterfile_error;
909 /* Open the file or fail */
910 if ((fp = fopen(csp->config->re_filterfile, "r")) == NULL)
912 goto load_re_filterfile_error;
915 /* Read line by line */
916 while (read_config_line(buf, sizeof(buf), fp, fs) != NULL)
918 enlist( bl->patterns, buf );
920 /* We have a meaningful line -> make it a job */
921 if ((dummy = pcrs_compile_command(buf, &error)) == NULL)
923 log_error(LOG_LEVEL_RE_FILTER,
924 "Adding re_filter job %s failed with error %d.", buf, error);
929 dummy->next = bl->joblist;
931 log_error(LOG_LEVEL_RE_FILTER, "Adding re_filter job %s succeeded.", buf);
937 /* the old one is now obsolete */
938 if ( NULL != current_re_filterfile )
940 current_re_filterfile->unloader = unload_re_filterfile;
943 fs->next = files->next;
945 current_re_filterfile = fs;
954 load_re_filterfile_error:
955 log_error(LOG_LEVEL_FATAL, "can't load re_filterfile '%s': %E",
956 csp->config->re_filterfile);
962 /*********************************************************************
964 * Function : add_loader
966 * Description : Called from `load_config'. Called once for each input
967 * file found in config.
970 * 1 : loader = pointer to a function that can parse and load
971 * the appropriate config file.
972 * 2 : config = The configuration_spec to add the loader to.
976 *********************************************************************/
977 void add_loader(int (*loader)(struct client_state *),
978 struct configuration_spec * config)
982 for (i=0; i < NLOADERS; i++)
984 if (config->loaders[i] == NULL)
986 config->loaders[i] = loader;
994 /*********************************************************************
996 * Function : run_loader
998 * Description : Called from `load_config' and `listen_loop'. This
999 * function keeps the "csp" current with any file mods
1000 * since the last loop. If a file is unchanged, the
1001 * loader functions do NOT reload the file.
1004 * 1 : csp = Current client state (buffers, headers, etc...)
1005 * Must be non-null. Reads: "csp->config"
1006 * Writes: various data members.
1008 * Returns : 0 => Ok, everything else is an error.
1010 *********************************************************************/
1011 int run_loader(struct client_state *csp)
1016 for (i=0; i < NLOADERS; i++)
1018 if (csp->config->loaders[i] == NULL)
1022 ret |= (csp->config->loaders[i])(csp);