1 const char loaders_rcs[] = "$Id: loaders.c,v 1.15 2001/06/07 23:14:14 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.15 2001/06/07 23:14:14 jongfoster
39 * Removing ACL and forward file loaders - these
40 * files have been merged into the config file.
41 * Cosmetic: Moving unloader funcs next to their
42 * respective loader funcs
44 * Revision 1.14 2001/06/01 03:27:04 oes
45 * Fixed line continuation problem
47 * Revision 1.13 2001/05/31 21:28:49 jongfoster
48 * Removed all permissionsfile code - it's now called the actions
49 * file, and (almost) all the code is in actions.c
51 * Revision 1.12 2001/05/31 17:32:31 oes
53 * - Enhanced domain part globbing with infix and prefix asterisk
54 * matching and optional unanchored operation
56 * Revision 1.11 2001/05/29 23:25:24 oes
58 * - load_config_line() and load_permissions_file() now use chomp()
60 * Revision 1.10 2001/05/29 09:50:24 jongfoster
61 * Unified blocklist/imagelist/permissionslist.
62 * File format is still under discussion, but the internal changes
65 * Also modified interceptor behaviour:
66 * - We now intercept all URLs beginning with one of the following
67 * prefixes (and *only* these prefixes):
69 * * http://ijbswa.sf.net/config/
70 * * http://ijbswa.sourceforge.net/config/
71 * - New interceptors "home page" - go to http://i.j.b/ to see it.
72 * - Internal changes so that intercepted and fast redirect pages
73 * are not replaced with an image.
74 * - Interceptors now have the option to send a binary page direct
75 * to the client. (i.e. ijb-send-banner uses this)
76 * - Implemented show-url-info interceptor. (Which is why I needed
77 * the above interceptors changes - a typical URL is
78 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
79 * The previous mechanism would not have intercepted that, and
80 * if it had been intercepted then it then it would have replaced
83 * Revision 1.9 2001/05/26 17:12:07 jongfoster
84 * Fatal errors loading configuration files now give better error messages.
86 * Revision 1.8 2001/05/26 00:55:20 jongfoster
87 * Removing duplicated code. load_forwardfile() now uses create_url_spec()
89 * Revision 1.7 2001/05/26 00:28:36 jongfoster
90 * Automatic reloading of config file.
91 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
92 * Most of the global variables have been moved to a new
93 * struct configuration_spec, accessed through csp->config->globalname
94 * Most of the globals remaining are used by the Win32 GUI.
96 * Revision 1.6 2001/05/23 12:27:33 oes
98 * Fixed ugly indentation of my last changes
100 * Revision 1.5 2001/05/23 10:39:05 oes
101 * - Added support for escaping the comment character
102 * in config files by a backslash
103 * - Added support for line continuation in config
105 * - Fixed a buffer overflow bug with long config lines
107 * Revision 1.4 2001/05/22 18:56:28 oes
110 * Revision 1.3 2001/05/20 01:21:20 jongfoster
111 * Version 2.9.4 checkin.
112 * - Merged popupfile and cookiefile, and added control over PCRS
113 * filtering, in new "permissionsfile".
114 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
115 * file error you now get a message box (in the Win32 GUI) rather
116 * than the program exiting with no explanation.
117 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
119 * - Removed tabs from "config"
120 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
121 * - Bumped up version number.
123 * Revision 1.2 2001/05/17 23:01:01 oes
124 * - Cleaned CRLF's from the sources and related files
126 * Revision 1.1.1.1 2001/05/15 13:58:59 oes
127 * Initial import of version 2.9.3 source tree
130 *********************************************************************/
137 #include <sys/types.h>
141 #include <sys/stat.h>
156 #include "miscutil.h"
161 #ifndef SPLIT_PROXY_ARGS
163 #include "showargs.h"
164 #endif /* ndef SPLIT_PROXY_ARGS */
166 const char loaders_h_rcs[] = LOADERS_H_VERSION;
168 /* Fix a problem with Solaris. There should be no effect on other
170 * Solaris's isspace() is a macro which uses it's argument directly
171 * as an array index. Therefore we need to make sure that high-bit
172 * characters generate +ve values, and ideally we also want to make
173 * the argument match the declared parameter type of "int".
175 #define ijb_isspace(__X) isspace((int)(unsigned char)(__X))
179 * Currently active files.
180 * These are also entered in the main linked list of files.
184 static struct file_list *current_trustfile = NULL;
185 #endif /* def TRUST_FILES */
188 static struct file_list *current_re_filterfile = NULL;
189 #endif /* def PCRS */
192 /*********************************************************************
196 * Description : Basically a mark and sweep garbage collector, it is run
197 * (by the parent thread) every once in a while to reclaim memory.
199 * It uses a mark and sweep strategy:
200 * 1) mark all files as inactive
202 * 2) check with each client:
203 * if it is active, mark its files as active
204 * if it is inactive, free its resources
206 * 3) free the resources of all of the files that
207 * are still marked as inactive (and are obsolete).
209 * N.B. files that are not obsolete don't have an unloader defined.
215 *********************************************************************/
218 struct file_list *fl, *nfl;
219 struct client_state *csp, *ncsp;
221 /* clear all of the file's active flags */
222 for ( fl = files->next; NULL != fl; fl = fl->next )
227 for (csp = clients; csp && (ncsp = csp->next) ; csp = csp->next)
231 /* mark this client's files as active */
234 * Always have a configuration file.
235 * (Also note the slightly non-standard extra
238 ncsp->config->config_file_list->active = 1;
240 if (ncsp->actions_list) /* actions files */
242 ncsp->actions_list->active = 1;
246 if (ncsp->rlist) /* perl re files */
248 ncsp->rlist->active = 1;
250 #endif /* def PCRS */
253 if (ncsp->tlist) /* trust files */
255 ncsp->tlist->active = 1;
257 #endif /* def TRUST_FILES */
262 /* this client one is not active, release its resources */
263 csp->next = ncsp->next;
265 freez(ncsp->ip_addr_str);
267 freez(ncsp->referrer);
268 #endif /* def TRUST_FILES */
269 freez(ncsp->x_forwarded);
270 freez(ncsp->iob->buf);
272 free_http_request(ncsp->http);
274 destroy_list(ncsp->headers);
275 destroy_list(ncsp->cookie_list);
277 free_current_action(ncsp->action);
285 #endif /* def STATISTICS */
291 for (fl = files; fl && (nfl = fl->next) ; fl = fl->next)
293 if ( ( 0 == nfl->active ) && ( NULL != nfl->unloader ) )
295 fl->next = nfl->next;
297 (nfl->unloader)(nfl->f);
299 #ifndef SPLIT_PROXY_ARGS
300 freez(nfl->proxy_args);
301 #endif /* ndef SPLIT_PROXY_ARGS */
303 freez(nfl->filename);
312 /*********************************************************************
314 * Function : create_url_spec
316 * Description : Creates a "url_spec" structure from a string.
317 * When finished, free with unload_url().
320 * 1 : url = Target url_spec to be filled in. Must be
321 * zeroed out before the call (e.g. using zalloc).
322 * 2 : buf = Source pattern, null terminated. NOTE: The
323 * contents of this buffer are destroyed by this
324 * function. If this function succeeds, the
325 * buffer is copied to url->spec. If this
326 * function fails, the contents of the buffer
329 * Returns : 0 => Ok, everything else is an error.
331 *********************************************************************/
332 int create_url_spec(struct url_spec * url, char * buf)
335 struct url_spec tmp_url[1];
337 /* paranoia - should never happen. */
338 if ((url == NULL) || (buf == NULL))
343 /* save a copy of the orignal specification */
344 if ((url->spec = strdup(buf)) == NULL)
349 if ((p = strchr(buf, '/')))
351 if (NULL == (url->path = strdup(p)))
356 url->pathlen = strlen(url->path);
368 char rebuf[BUFFER_SIZE];
370 if (NULL == (url->preg = zalloc(sizeof(*url->preg))))
377 sprintf(rebuf, "^(%s)", url->path);
379 errcode = regcomp(url->preg, rebuf,
380 (REG_EXTENDED|REG_NOSUB|REG_ICASE));
385 url->preg, buf, sizeof(buf));
389 log_error(LOG_LEVEL_ERROR, "error compiling %s: %s",
400 if ((p = strchr(buf, ':')) == NULL)
410 if ((url->domain = strdup(buf)) == NULL)
416 #endif /* def REGEX */
420 /* split domain into components */
422 *tmp_url = dsplit(url->domain);
423 url->dbuf = tmp_url->dbuf;
424 url->dcnt = tmp_url->dcnt;
425 url->dvec = tmp_url->dvec;
426 url->unanchored = tmp_url->unanchored;
432 /*********************************************************************
434 * Function : free_url
436 * Description : Called from the "unloaders". Freez the url
437 * structure elements.
440 * 1 : url = pointer to a url_spec structure.
444 *********************************************************************/
445 void free_url(struct url_spec *url)
447 if (url == NULL) return;
465 /*********************************************************************
467 * Function : check_file_changed
469 * Description : Helper function to check if a file needs reloading.
470 * If "current" is still current, return it. Otherwise
471 * allocates a new (zeroed) "struct file_list", fills
472 * in the disk file name and timestamp, and returns it.
475 * 1 : current = The file_list currently being used - will
476 * be checked to see if it is out of date.
477 * May be NULL (which is treated as out of
479 * 2 : filename = Name of file to check.
480 * 3 : newfl = New file list. [Output only]
481 * This will be set to NULL, OR a struct
482 * file_list newly allocated on the
483 * heap, with the filename and lastmodified
484 * fields filled, standard header giving file
485 * name in proxy_args, and all others zeroed.
486 * (proxy_args is only filled in if !defined
487 * SPLIT_PROXY_ARGS and !suppress_blocklists).
489 * Returns : If file unchanged: 0 (and sets newfl == NULL)
490 * If file changed: 1 and sets newfl != NULL
491 * On error: 1 and sets newfl == NULL
493 *********************************************************************/
494 int check_file_changed(const struct file_list * current,
495 const char * filename,
496 struct file_list ** newfl)
498 struct file_list *fs;
499 struct stat statbuf[1];
503 if (stat(filename, statbuf) < 0)
505 /* Error, probably file not found. */
510 && (current->lastmodified == statbuf->st_mtime)
511 && (0 == strcmp(current->filename, filename)))
516 fs = (struct file_list *)zalloc(sizeof(struct file_list));
520 /* Out of memory error */
524 fs->filename = strdup(filename);
525 fs->lastmodified = statbuf->st_mtime;
527 if (fs->filename == NULL)
529 /* Out of memory error */
534 #ifndef SPLIT_PROXY_ARGS
535 if (!suppress_blocklists)
537 char * p = html_encode(filename);
540 fs->proxy_args = strsav(fs->proxy_args, "<h2>The file `");
541 fs->proxy_args = strsav(fs->proxy_args, p);
542 fs->proxy_args = strsav(fs->proxy_args,
543 "' contains the following patterns</h2>\n");
546 fs->proxy_args = strsav(fs->proxy_args, "<pre>");
548 #endif /* ndef SPLIT_PROXY_ARGS */
555 /*********************************************************************
557 * Function : read_config_line
559 * Description : Read a single non-empty line from a file and return
560 * it. Trims comments, leading and trailing whitespace
561 * and respects escaping of newline and comment char.
562 * Also writes the file to fs->proxy_args.
565 * 1 : buf = Buffer to use.
566 * 2 : buflen = Size of buffer in bytes.
567 * 3 : fp = File to read from
568 * 4 : fs = File will be written to fs->proxy_args. May
569 * be NULL to disable this feature.
571 * Returns : NULL on EOF or error
572 * Otherwise, returns buf.
574 *********************************************************************/
575 char *read_config_line(char *buf, int buflen, FILE *fp, struct file_list *fs)
578 char linebuf[BUFFER_SIZE];
583 while (fgets(linebuf, sizeof(linebuf), fp))
585 #ifndef SPLIT_PROXY_ARGS
586 if (fs && !suppress_blocklists)
588 char *html_line = html_encode(linebuf);
589 if (html_line != NULL)
591 fs->proxy_args = strsav(fs->proxy_args, html_line);
594 fs->proxy_args = strsav(fs->proxy_args, "<br>");
596 #endif /* ndef SPLIT_PROXY_ARGS */
598 /* Trim off newline */
599 if ((p = strpbrk(linebuf, "\r\n")) != NULL)
604 /* Line continuation? Trim escape and set flag. */
605 if ((p != linebuf) && (*--p == '\\'))
611 /* If there's a comment char.. */
612 if ((p = strpbrk(linebuf, "#")) != NULL)
614 /* ..and it's escaped, left-shift the line over the escape. */
615 if ((p != linebuf) && (*(p-1) == '\\'))
618 while ((*q++ = *p++) != '\0') /* nop */;
620 /* Else, chop off the rest of the line */
627 /* Write to the buffer */
630 strncat(buf, linebuf, buflen - strlen(buf));
640 /* Remove leading and trailing whitespace */
656 /*********************************************************************
658 * Function : unload_trustfile
660 * Description : Unloads a trustfile.
663 * 1 : f = the data structure associated with the trustfile.
667 *********************************************************************/
668 static void unload_trustfile(void *f)
670 struct block_spec *b = (struct block_spec *)f;
671 if (b == NULL) return;
673 unload_trustfile(b->next);
682 /*********************************************************************
684 * Function : load_trustfile
686 * Description : Read and parse a trustfile and add to files list.
689 * 1 : csp = Current client state (buffers, headers, etc...)
691 * Returns : 0 => Ok, everything else is an error.
693 *********************************************************************/
694 int load_trustfile(struct client_state *csp)
698 struct block_spec *b, *bl;
699 struct url_spec **tl;
701 char buf[BUFFER_SIZE], *p, *q;
703 struct file_list *fs;
705 if (!check_file_changed(current_trustfile, csp->config->trustfile, &fs))
707 /* No need to load */
710 csp->tlist = current_trustfile;
716 goto load_trustfile_error;
719 fs->f = bl = (struct block_spec *)zalloc(sizeof(*bl));
722 goto load_trustfile_error;
725 if ((fp = fopen(csp->config->trustfile, "r")) == NULL)
727 goto load_trustfile_error;
730 tl = csp->config->trust_list;
732 while (read_config_line(buf, sizeof(buf), fp, fs) != NULL)
748 while ((*p++ = *q++))
754 /* skip blank lines */
760 /* allocate a new node */
761 if ((b = zalloc(sizeof(*b))) == NULL)
764 goto load_trustfile_error;
767 /* add it to the list */
773 /* Save the URL pattern */
774 if (create_url_spec(b->url, buf))
777 goto load_trustfile_error;
781 * save a pointer to URL's spec in the list of trusted URL's, too
793 #ifndef SPLIT_PROXY_ARGS
794 if (!suppress_blocklists)
796 fs->proxy_args = strsav(fs->proxy_args, "</pre>");
798 #endif /* ndef SPLIT_PROXY_ARGS */
800 /* the old one is now obsolete */
801 if (current_trustfile)
803 current_trustfile->unloader = unload_trustfile;
806 fs->next = files->next;
808 current_trustfile = fs;
817 load_trustfile_error:
818 log_error(LOG_LEVEL_FATAL, "can't load trustfile '%s': %E",
819 csp->config->trustfile);
823 #endif /* def TRUST_FILES */
827 /*********************************************************************
829 * Function : unload_re_filterfile
831 * Description : Unload the re_filter list.
834 * 1 : f = the data structure associated with the filterfile.
838 *********************************************************************/
839 static void unload_re_filterfile(void *f)
842 struct re_filterfile_spec *b = (struct re_filterfile_spec *)f;
844 if (b == NULL) return;
846 destroy_list(b->patterns);
848 joblist = b->joblist;
849 while ( NULL != (joblist = pcrs_free_job(joblist)) ) {}
855 /*********************************************************************
857 * Function : load_re_filterfile
859 * Description : Load the re_filterfile. Each non-comment, non-empty
860 * line is instantly added to the joblist, which is
861 * a chained list of pcrs_job structs.
864 * 1 : csp = Current client state (buffers, headers, etc...)
866 * Returns : 0 => Ok, everything else is an error.
868 *********************************************************************/
869 int load_re_filterfile(struct client_state *csp)
873 struct re_filterfile_spec *bl;
874 struct file_list *fs;
876 char buf[BUFFER_SIZE];
880 if (!check_file_changed(current_re_filterfile, csp->config->re_filterfile, &fs))
882 /* No need to load */
885 csp->rlist = current_re_filterfile;
891 goto load_re_filterfile_error;
894 fs->f = bl = (struct re_filterfile_spec *)zalloc(sizeof(*bl));
897 goto load_re_filterfile_error;
900 /* Open the file or fail */
901 if ((fp = fopen(csp->config->re_filterfile, "r")) == NULL)
903 goto load_re_filterfile_error;
906 /* Read line by line */
907 while (read_config_line(buf, sizeof(buf), fp, fs) != NULL)
909 enlist( bl->patterns, buf );
911 /* We have a meaningful line -> make it a job */
912 if ((dummy = pcrs_make_job(buf, &error)) == NULL)
914 log_error(LOG_LEVEL_RE_FILTER,
915 "Adding re_filter job %s failed with error %d.", buf, error);
920 dummy->next = bl->joblist;
922 log_error(LOG_LEVEL_RE_FILTER, "Adding re_filter job %s succeeded.", buf);
928 #ifndef SPLIT_PROXY_ARGS
929 if (!suppress_blocklists)
931 fs->proxy_args = strsav(fs->proxy_args, "</pre>");
933 #endif /* ndef SPLIT_PROXY_ARGS */
935 /* the old one is now obsolete */
936 if ( NULL != current_re_filterfile )
938 current_re_filterfile->unloader = unload_re_filterfile;
941 fs->next = files->next;
943 current_re_filterfile = fs;
952 load_re_filterfile_error:
953 log_error(LOG_LEVEL_FATAL, "can't load re_filterfile '%s': %E",
954 csp->config->re_filterfile);
958 #endif /* def PCRS */
961 /*********************************************************************
963 * Function : add_loader
965 * Description : Called from `load_config'. Called once for each input
966 * file found in config.
969 * 1 : loader = pointer to a function that can parse and load
970 * the appropriate config file.
971 * 2 : config = The configuration_spec to add the loader to.
975 *********************************************************************/
976 void add_loader(int (*loader)(struct client_state *),
977 struct configuration_spec * config)
981 for (i=0; i < NLOADERS; i++)
983 if (config->loaders[i] == NULL)
985 config->loaders[i] = loader;
993 /*********************************************************************
995 * Function : run_loader
997 * Description : Called from `load_config' and `listen_loop'. This
998 * function keeps the "csp" current with any file mods
999 * since the last loop. If a file is unchanged, the
1000 * loader functions do NOT reload the file.
1003 * 1 : csp = Current client state (buffers, headers, etc...)
1004 * Must be non-null. Reads: "csp->config"
1005 * Writes: various data members.
1007 * Returns : 0 => Ok, everything else is an error.
1009 *********************************************************************/
1010 int run_loader(struct client_state *csp)
1015 for (i=0; i < NLOADERS; i++)
1017 if (csp->config->loaders[i] == NULL)
1021 ret |= (csp->config->loaders[i])(csp);