1 const char loaders_rcs[] = "$Id: loaders.c,v 1.26 2001/09/22 14:05:22 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.26 2001/09/22 14:05:22 jongfoster
39 * Bugfix: Multiple escaped "#" characters in a configuration
40 * file are now permitted.
41 * Also removing 3 unused headers.
43 * Revision 1.25 2001/09/13 22:44:03 jongfoster
44 * Adding {} to an if statement
46 * Revision 1.24 2001/07/30 22:08:36 jongfoster
47 * Tidying up #defines:
48 * - All feature #defines are now of the form FEATURE_xxx
49 * - Permanently turned off WIN_GUI_EDIT
50 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
52 * Revision 1.23 2001/07/20 15:51:54 oes
53 * Fixed indentation of prepocessor commands
55 * Revision 1.22 2001/07/20 15:16:17 haroon
56 * - per Guy's suggestion, added a while loop in sweep() to catch not just
57 * the last inactive CSP but all other consecutive inactive CSPs after that
60 * Revision 1.21 2001/07/18 17:26:24 oes
61 * Changed to conform to new pcrs interface
63 * Revision 1.20 2001/07/17 13:07:01 oes
64 * Fixed segv when last line in config files
65 * lacked a terminating (\r)\n
67 * Revision 1.19 2001/07/13 14:01:54 oes
68 * Removed all #ifdef PCRS
70 * Revision 1.18 2001/06/29 21:45:41 oes
71 * Indentation, CRLF->LF, Tab-> Space
73 * Revision 1.17 2001/06/29 13:31:51 oes
76 * Revision 1.16 2001/06/09 10:55:28 jongfoster
77 * Changing BUFSIZ ==> BUFFER_SIZE
79 * Revision 1.15 2001/06/07 23:14:14 jongfoster
80 * Removing ACL and forward file loaders - these
81 * files have been merged into the config file.
82 * Cosmetic: Moving unloader funcs next to their
83 * respective loader funcs
85 * Revision 1.14 2001/06/01 03:27:04 oes
86 * Fixed line continuation problem
88 * Revision 1.13 2001/05/31 21:28:49 jongfoster
89 * Removed all permissionsfile code - it's now called the actions
90 * file, and (almost) all the code is in actions.c
92 * Revision 1.12 2001/05/31 17:32:31 oes
94 * - Enhanced domain part globbing with infix and prefix asterisk
95 * matching and optional unanchored operation
97 * Revision 1.11 2001/05/29 23:25:24 oes
99 * - load_config_line() and load_permissions_file() now use chomp()
101 * Revision 1.10 2001/05/29 09:50:24 jongfoster
102 * Unified blocklist/imagelist/permissionslist.
103 * File format is still under discussion, but the internal changes
106 * Also modified interceptor behaviour:
107 * - We now intercept all URLs beginning with one of the following
108 * prefixes (and *only* these prefixes):
110 * * http://ijbswa.sf.net/config/
111 * * http://ijbswa.sourceforge.net/config/
112 * - New interceptors "home page" - go to http://i.j.b/ to see it.
113 * - Internal changes so that intercepted and fast redirect pages
114 * are not replaced with an image.
115 * - Interceptors now have the option to send a binary page direct
116 * to the client. (i.e. ijb-send-banner uses this)
117 * - Implemented show-url-info interceptor. (Which is why I needed
118 * the above interceptors changes - a typical URL is
119 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
120 * The previous mechanism would not have intercepted that, and
121 * if it had been intercepted then it then it would have replaced
124 * Revision 1.9 2001/05/26 17:12:07 jongfoster
125 * Fatal errors loading configuration files now give better error messages.
127 * Revision 1.8 2001/05/26 00:55:20 jongfoster
128 * Removing duplicated code. load_forwardfile() now uses create_url_spec()
130 * Revision 1.7 2001/05/26 00:28:36 jongfoster
131 * Automatic reloading of config file.
132 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
133 * Most of the global variables have been moved to a new
134 * struct configuration_spec, accessed through csp->config->globalname
135 * Most of the globals remaining are used by the Win32 GUI.
137 * Revision 1.6 2001/05/23 12:27:33 oes
139 * Fixed ugly indentation of my last changes
141 * Revision 1.5 2001/05/23 10:39:05 oes
142 * - Added support for escaping the comment character
143 * in config files by a backslash
144 * - Added support for line continuation in config
146 * - Fixed a buffer overflow bug with long config lines
148 * Revision 1.4 2001/05/22 18:56:28 oes
151 * Revision 1.3 2001/05/20 01:21:20 jongfoster
152 * Version 2.9.4 checkin.
153 * - Merged popupfile and cookiefile, and added control over PCRS
154 * filtering, in new "permissionsfile".
155 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
156 * file error you now get a message box (in the Win32 GUI) rather
157 * than the program exiting with no explanation.
158 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
160 * - Removed tabs from "config"
161 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
162 * - Bumped up version number.
164 * Revision 1.2 2001/05/17 23:01:01 oes
165 * - Cleaned CRLF's from the sources and related files
167 * Revision 1.1.1.1 2001/05/15 13:58:59 oes
168 * Initial import of version 2.9.3 source tree
171 *********************************************************************/
178 #include <sys/types.h>
182 #include <sys/stat.h>
195 #include "miscutil.h"
199 const char loaders_h_rcs[] = LOADERS_H_VERSION;
201 /* Fix a problem with Solaris. There should be no effect on other
203 * Solaris's isspace() is a macro which uses it's argument directly
204 * as an array index. Therefore we need to make sure that high-bit
205 * characters generate +ve values, and ideally we also want to make
206 * the argument match the declared parameter type of "int".
208 #define ijb_isspace(__X) isspace((int)(unsigned char)(__X))
212 * Currently active files.
213 * These are also entered in the main linked list of files.
217 static struct file_list *current_trustfile = NULL;
218 #endif /* def FEATURE_TRUST */
220 static struct file_list *current_re_filterfile = NULL;
224 /*********************************************************************
228 * Description : Basically a mark and sweep garbage collector, it is run
229 * (by the parent thread) every once in a while to reclaim memory.
231 * It uses a mark and sweep strategy:
232 * 1) mark all files as inactive
234 * 2) check with each client:
235 * if it is active, mark its files as active
236 * if it is inactive, free its resources
238 * 3) free the resources of all of the files that
239 * are still marked as inactive (and are obsolete).
241 * N.B. files that are not obsolete don't have an unloader defined.
247 *********************************************************************/
250 struct file_list *fl, *nfl;
251 struct client_state *csp, *ncsp;
253 /* clear all of the file's active flags */
254 for ( fl = files->next; NULL != fl; fl = fl->next )
259 for (csp = clients; csp && (ncsp = csp->next) ; csp = csp->next)
263 /* mark this client's files as active */
266 * Always have a configuration file.
267 * (Also note the slightly non-standard extra
270 ncsp->config->config_file_list->active = 1;
272 if (ncsp->actions_list) /* actions files */
274 ncsp->actions_list->active = 1;
277 if (ncsp->rlist) /* pcrsjob files */
279 ncsp->rlist->active = 1;
283 if (ncsp->tlist) /* trust files */
285 ncsp->tlist->active = 1;
287 #endif /* def FEATURE_TRUST */
292 * this client is not active, release its resources
293 * and the ones of all inactive clients that might
297 while( !ncsp->active )
299 csp->next = ncsp->next;
301 freez(ncsp->ip_addr_str);
302 freez(ncsp->my_ip_addr_str);
303 freez(ncsp->my_hostname);
306 freez(ncsp->referrer);
307 #endif /* def FEATURE_TRUST */
308 freez(ncsp->x_forwarded);
309 freez(ncsp->iob->buf);
311 free_http_request(ncsp->http);
313 destroy_list(ncsp->headers);
314 destroy_list(ncsp->cookie_list);
316 free_current_action(ncsp->action);
318 #ifdef FEATURE_STATISTICS
324 #endif /* def FEATURE_STATISTICS */
328 /* are there any more in sequence after it? */
329 if( !(ncsp = csp->next) )
335 for (fl = files; fl && (nfl = fl->next) ; fl = fl->next)
337 if ( ( 0 == nfl->active ) && ( NULL != nfl->unloader ) )
339 fl->next = nfl->next;
341 (nfl->unloader)(nfl->f);
343 freez(nfl->filename);
352 /*********************************************************************
354 * Function : create_url_spec
356 * Description : Creates a "url_spec" structure from a string.
357 * When finished, free with unload_url().
360 * 1 : url = Target url_spec to be filled in. Must be
361 * zeroed out before the call (e.g. using zalloc).
362 * 2 : buf = Source pattern, null terminated. NOTE: The
363 * contents of this buffer are destroyed by this
364 * function. If this function succeeds, the
365 * buffer is copied to url->spec. If this
366 * function fails, the contents of the buffer
369 * Returns : 0 => Ok, everything else is an error.
371 *********************************************************************/
372 int create_url_spec(struct url_spec * url, char * buf)
375 struct url_spec tmp_url[1];
377 /* paranoia - should never happen. */
378 if ((url == NULL) || (buf == NULL))
383 /* save a copy of the orignal specification */
384 if ((url->spec = strdup(buf)) == NULL)
389 if ((p = strchr(buf, '/')))
391 if (NULL == (url->path = strdup(p)))
396 url->pathlen = strlen(url->path);
408 char rebuf[BUFFER_SIZE];
410 if (NULL == (url->preg = zalloc(sizeof(*url->preg))))
417 sprintf(rebuf, "^(%s)", url->path);
419 errcode = regcomp(url->preg, rebuf,
420 (REG_EXTENDED|REG_NOSUB|REG_ICASE));
423 size_t errlen = regerror(errcode,
424 url->preg, buf, sizeof(buf));
428 log_error(LOG_LEVEL_ERROR, "error compiling %s: %s",
439 if ((p = strchr(buf, ':')) == NULL)
449 if ((url->domain = strdup(buf)) == NULL)
455 #endif /* def REGEX */
459 /* split domain into components */
461 *tmp_url = dsplit(url->domain);
462 url->dbuf = tmp_url->dbuf;
463 url->dcnt = tmp_url->dcnt;
464 url->dvec = tmp_url->dvec;
465 url->unanchored = tmp_url->unanchored;
472 /*********************************************************************
474 * Function : free_url
476 * Description : Called from the "unloaders". Freez the url
477 * structure elements.
480 * 1 : url = pointer to a url_spec structure.
484 *********************************************************************/
485 void free_url(struct url_spec *url)
487 if (url == NULL) return;
505 /*********************************************************************
507 * Function : check_file_changed
509 * Description : Helper function to check if a file needs reloading.
510 * If "current" is still current, return it. Otherwise
511 * allocates a new (zeroed) "struct file_list", fills
512 * in the disk file name and timestamp, and returns it.
515 * 1 : current = The file_list currently being used - will
516 * be checked to see if it is out of date.
517 * May be NULL (which is treated as out of
519 * 2 : filename = Name of file to check.
520 * 3 : newfl = New file list. [Output only]
521 * This will be set to NULL, OR a struct
522 * file_list newly allocated on the
523 * heap, with the filename and lastmodified
524 * fields filled, and all others zeroed.
526 * Returns : If file unchanged: 0 (and sets newfl == NULL)
527 * If file changed: 1 and sets newfl != NULL
528 * On error: 1 and sets newfl == NULL
530 *********************************************************************/
531 int check_file_changed(const struct file_list * current,
532 const char * filename,
533 struct file_list ** newfl)
535 struct file_list *fs;
536 struct stat statbuf[1];
540 if (stat(filename, statbuf) < 0)
542 /* Error, probably file not found. */
547 && (current->lastmodified == statbuf->st_mtime)
548 && (0 == strcmp(current->filename, filename)))
553 fs = (struct file_list *)zalloc(sizeof(struct file_list));
557 /* Out of memory error */
561 fs->filename = strdup(filename);
562 fs->lastmodified = statbuf->st_mtime;
564 if (fs->filename == NULL)
566 /* Out of memory error */
578 /*********************************************************************
580 * Function : read_config_line
582 * Description : Read a single non-empty line from a file and return
583 * it. Trims comments, leading and trailing whitespace
584 * and respects escaping of newline and comment char.
587 * 1 : buf = Buffer to use.
588 * 2 : buflen = Size of buffer in bytes.
589 * 3 : fp = File to read from
591 * Returns : NULL on EOF or error
592 * Otherwise, returns buf.
594 *********************************************************************/
595 char *read_config_line(char *buf, int buflen, FILE *fp)
600 char linebuf[BUFFER_SIZE];
605 while (fgets(linebuf, sizeof(linebuf), fp))
607 /* Trim off newline */
608 if ((p = strpbrk(linebuf, "\r\n")) != NULL)
614 p = linebuf + strlen(linebuf);
617 /* Line continuation? Trim escape and set flag. */
618 if ((p != linebuf) && (*--p == '\\'))
624 /* If there's a comment char.. */
626 while ((p = strchr(p, '#')) != NULL)
628 /* ..and it's escaped, left-shift the line over the escape. */
629 if ((p != linebuf) && (*(p-1) == '\\'))
633 while ((*dest++ = *src++) != '\0')
637 /* Now scan from just after the "#". */
639 /* Else, chop off the rest of the line */
646 /* Write to the buffer */
649 strncat(buf, linebuf, buflen - strlen(buf));
659 /* Remove leading and trailing whitespace */
675 /*********************************************************************
677 * Function : unload_trustfile
679 * Description : Unloads a trustfile.
682 * 1 : f = the data structure associated with the trustfile.
686 *********************************************************************/
687 static void unload_trustfile(void *f)
689 struct block_spec *b = (struct block_spec *)f;
690 if (b == NULL) return;
692 unload_trustfile(b->next); /* Stack is cheap, isn't it? */
701 /*********************************************************************
703 * Function : load_trustfile
705 * Description : Read and parse a trustfile and add to files list.
708 * 1 : csp = Current client state (buffers, headers, etc...)
710 * Returns : 0 => Ok, everything else is an error.
712 *********************************************************************/
713 int load_trustfile(struct client_state *csp)
717 struct block_spec *b, *bl;
718 struct url_spec **tl;
720 char buf[BUFFER_SIZE], *p, *q;
722 struct file_list *fs;
724 if (!check_file_changed(current_trustfile, csp->config->trustfile, &fs))
726 /* No need to load */
729 csp->tlist = current_trustfile;
735 goto load_trustfile_error;
738 fs->f = bl = (struct block_spec *)zalloc(sizeof(*bl));
741 goto load_trustfile_error;
744 if ((fp = fopen(csp->config->trustfile, "r")) == NULL)
746 goto load_trustfile_error;
749 tl = csp->config->trust_list;
751 while (read_config_line(buf, sizeof(buf), fp) != NULL)
767 while ((*p++ = *q++))
773 /* skip blank lines */
779 /* allocate a new node */
780 if ((b = zalloc(sizeof(*b))) == NULL)
783 goto load_trustfile_error;
786 /* add it to the list */
792 /* Save the URL pattern */
793 if (create_url_spec(b->url, buf))
796 goto load_trustfile_error;
800 * save a pointer to URL's spec in the list of trusted URL's, too
812 /* the old one is now obsolete */
813 if (current_trustfile)
815 current_trustfile->unloader = unload_trustfile;
818 fs->next = files->next;
820 current_trustfile = fs;
829 load_trustfile_error:
830 log_error(LOG_LEVEL_FATAL, "can't load trustfile '%s': %E",
831 csp->config->trustfile);
835 #endif /* def FEATURE_TRUST */
838 /*********************************************************************
840 * Function : unload_re_filterfile
842 * Description : Unload the re_filter list.
845 * 1 : f = the data structure associated with the filterfile.
849 *********************************************************************/
850 static void unload_re_filterfile(void *f)
852 struct re_filterfile_spec *b = (struct re_filterfile_spec *)f;
859 destroy_list(b->patterns);
860 pcrs_free_joblist(b->joblist);
866 /*********************************************************************
868 * Function : load_re_filterfile
870 * Description : Load the re_filterfile. Each non-comment, non-empty
871 * line is instantly added to the joblist, which is
872 * a chained list of pcrs_job structs.
875 * 1 : csp = Current client state (buffers, headers, etc...)
877 * Returns : 0 => Ok, everything else is an error.
879 *********************************************************************/
880 int load_re_filterfile(struct client_state *csp)
884 struct re_filterfile_spec *bl;
885 struct file_list *fs;
887 char buf[BUFFER_SIZE];
891 if (!check_file_changed(current_re_filterfile, csp->config->re_filterfile, &fs))
893 /* No need to load */
896 csp->rlist = current_re_filterfile;
902 goto load_re_filterfile_error;
905 fs->f = bl = (struct re_filterfile_spec *)zalloc(sizeof(*bl));
908 goto load_re_filterfile_error;
911 /* Open the file or fail */
912 if ((fp = fopen(csp->config->re_filterfile, "r")) == NULL)
914 goto load_re_filterfile_error;
917 /* Read line by line */
918 while (read_config_line(buf, sizeof(buf), fp) != NULL)
920 enlist( bl->patterns, buf );
922 /* We have a meaningful line -> make it a job */
923 if ((dummy = pcrs_compile_command(buf, &error)) == NULL)
925 log_error(LOG_LEVEL_RE_FILTER,
926 "Adding re_filter job %s failed with error %d.", buf, error);
931 dummy->next = bl->joblist;
933 log_error(LOG_LEVEL_RE_FILTER, "Adding re_filter job %s succeeded.", buf);
939 /* the old one is now obsolete */
940 if ( NULL != current_re_filterfile )
942 current_re_filterfile->unloader = unload_re_filterfile;
945 fs->next = files->next;
947 current_re_filterfile = fs;
956 load_re_filterfile_error:
957 log_error(LOG_LEVEL_FATAL, "can't load re_filterfile '%s': %E",
958 csp->config->re_filterfile);
964 /*********************************************************************
966 * Function : add_loader
968 * Description : Called from `load_config'. Called once for each input
969 * file found in config.
972 * 1 : loader = pointer to a function that can parse and load
973 * the appropriate config file.
974 * 2 : config = The configuration_spec to add the loader to.
978 *********************************************************************/
979 void add_loader(int (*loader)(struct client_state *),
980 struct configuration_spec * config)
984 for (i=0; i < NLOADERS; i++)
986 if (config->loaders[i] == NULL)
988 config->loaders[i] = loader;
996 /*********************************************************************
998 * Function : run_loader
1000 * Description : Called from `load_config' and `listen_loop'. This
1001 * function keeps the "csp" current with any file mods
1002 * since the last loop. If a file is unchanged, the
1003 * loader functions do NOT reload the file.
1006 * 1 : csp = Current client state (buffers, headers, etc...)
1007 * Must be non-null. Reads: "csp->config"
1008 * Writes: various data members.
1010 * Returns : 0 => Ok, everything else is an error.
1012 *********************************************************************/
1013 int run_loader(struct client_state *csp)
1018 for (i=0; i < NLOADERS; i++)
1020 if (csp->config->loaders[i] == NULL)
1024 ret |= (csp->config->loaders[i])(csp);