1 const char loaders_rcs[] = "$Id: loaders.c,v 1.14 2001/06/01 03:27:04 oes 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.14 2001/06/01 03:27:04 oes
39 * Fixed line continuation problem
41 * Revision 1.13 2001/05/31 21:28:49 jongfoster
42 * Removed all permissionsfile code - it's now called the actions
43 * file, and (almost) all the code is in actions.c
45 * Revision 1.12 2001/05/31 17:32:31 oes
47 * - Enhanced domain part globbing with infix and prefix asterisk
48 * matching and optional unanchored operation
50 * Revision 1.11 2001/05/29 23:25:24 oes
52 * - load_config_line() and load_permissions_file() now use chomp()
54 * Revision 1.10 2001/05/29 09:50:24 jongfoster
55 * Unified blocklist/imagelist/permissionslist.
56 * File format is still under discussion, but the internal changes
59 * Also modified interceptor behaviour:
60 * - We now intercept all URLs beginning with one of the following
61 * prefixes (and *only* these prefixes):
63 * * http://ijbswa.sf.net/config/
64 * * http://ijbswa.sourceforge.net/config/
65 * - New interceptors "home page" - go to http://i.j.b/ to see it.
66 * - Internal changes so that intercepted and fast redirect pages
67 * are not replaced with an image.
68 * - Interceptors now have the option to send a binary page direct
69 * to the client. (i.e. ijb-send-banner uses this)
70 * - Implemented show-url-info interceptor. (Which is why I needed
71 * the above interceptors changes - a typical URL is
72 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
73 * The previous mechanism would not have intercepted that, and
74 * if it had been intercepted then it then it would have replaced
77 * Revision 1.9 2001/05/26 17:12:07 jongfoster
78 * Fatal errors loading configuration files now give better error messages.
80 * Revision 1.8 2001/05/26 00:55:20 jongfoster
81 * Removing duplicated code. load_forwardfile() now uses create_url_spec()
83 * Revision 1.7 2001/05/26 00:28:36 jongfoster
84 * Automatic reloading of config file.
85 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
86 * Most of the global variables have been moved to a new
87 * struct configuration_spec, accessed through csp->config->globalname
88 * Most of the globals remaining are used by the Win32 GUI.
90 * Revision 1.6 2001/05/23 12:27:33 oes
92 * Fixed ugly indentation of my last changes
94 * Revision 1.5 2001/05/23 10:39:05 oes
95 * - Added support for escaping the comment character
96 * in config files by a backslash
97 * - Added support for line continuation in config
99 * - Fixed a buffer overflow bug with long config lines
101 * Revision 1.4 2001/05/22 18:56:28 oes
104 * Revision 1.3 2001/05/20 01:21:20 jongfoster
105 * Version 2.9.4 checkin.
106 * - Merged popupfile and cookiefile, and added control over PCRS
107 * filtering, in new "permissionsfile".
108 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
109 * file error you now get a message box (in the Win32 GUI) rather
110 * than the program exiting with no explanation.
111 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
113 * - Removed tabs from "config"
114 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
115 * - Bumped up version number.
117 * Revision 1.2 2001/05/17 23:01:01 oes
118 * - Cleaned CRLF's from the sources and related files
120 * Revision 1.1.1.1 2001/05/15 13:58:59 oes
121 * Initial import of version 2.9.3 source tree
124 *********************************************************************/
131 #include <sys/types.h>
135 #include <sys/stat.h>
150 #include "miscutil.h"
155 #ifndef SPLIT_PROXY_ARGS
157 #include "showargs.h"
158 #endif /* ndef SPLIT_PROXY_ARGS */
160 const char loaders_h_rcs[] = LOADERS_H_VERSION;
162 /* Fix a problem with Solaris. There should be no effect on other
164 * Solaris's isspace() is a macro which uses it's argument directly
165 * as an array index. Therefore we need to make sure that high-bit
166 * characters generate +ve values, and ideally we also want to make
167 * the argument match the declared parameter type of "int".
169 #define ijb_isspace(__X) isspace((int)(unsigned char)(__X))
173 * Currently active files.
174 * These are also entered in the main linked list of files.
178 static struct file_list *current_trustfile = NULL;
179 #endif /* def TRUST_FILES */
182 static struct file_list *current_re_filterfile = NULL;
183 #endif /* def PCRS */
186 /*********************************************************************
190 * Description : Basically a mark and sweep garbage collector, it is run
191 * (by the parent thread) every once in a while to reclaim memory.
193 * It uses a mark and sweep strategy:
194 * 1) mark all files as inactive
196 * 2) check with each client:
197 * if it is active, mark its files as active
198 * if it is inactive, free its resources
200 * 3) free the resources of all of the files that
201 * are still marked as inactive (and are obsolete).
203 * N.B. files that are not obsolete don't have an unloader defined.
209 *********************************************************************/
212 struct file_list *fl, *nfl;
213 struct client_state *csp, *ncsp;
215 /* clear all of the file's active flags */
216 for ( fl = files->next; NULL != fl; fl = fl->next )
221 for (csp = clients; csp && (ncsp = csp->next) ; csp = csp->next)
225 /* mark this client's files as active */
228 * Always have a configuration file.
229 * (Also note the slightly non-standard extra
232 ncsp->config->config_file_list->active = 1;
234 if (ncsp->actions_list) /* actions files */
236 ncsp->actions_list->active = 1;
240 if (ncsp->rlist) /* perl re files */
242 ncsp->rlist->active = 1;
244 #endif /* def PCRS */
247 if (ncsp->tlist) /* trust files */
249 ncsp->tlist->active = 1;
251 #endif /* def TRUST_FILES */
256 /* this client one is not active, release its resources */
257 csp->next = ncsp->next;
259 freez(ncsp->ip_addr_str);
261 freez(ncsp->referrer);
262 #endif /* def TRUST_FILES */
263 freez(ncsp->x_forwarded);
264 freez(ncsp->iob->buf);
266 free_http_request(ncsp->http);
268 destroy_list(ncsp->headers);
269 destroy_list(ncsp->cookie_list);
271 free_current_action(ncsp->action);
279 #endif /* def STATISTICS */
285 for (fl = files; fl && (nfl = fl->next) ; fl = fl->next)
287 if ( ( 0 == nfl->active ) && ( NULL != nfl->unloader ) )
289 fl->next = nfl->next;
291 (nfl->unloader)(nfl->f);
293 #ifndef SPLIT_PROXY_ARGS
294 freez(nfl->proxy_args);
295 #endif /* ndef SPLIT_PROXY_ARGS */
297 freez(nfl->filename);
306 /*********************************************************************
308 * Function : create_url_spec
310 * Description : Creates a "url_spec" structure from a string.
311 * When finished, free with unload_url().
314 * 1 : url = Target url_spec to be filled in. Must be
315 * zeroed out before the call (e.g. using zalloc).
316 * 2 : buf = Source pattern, null terminated. NOTE: The
317 * contents of this buffer are destroyed by this
318 * function. If this function succeeds, the
319 * buffer is copied to url->spec. If this
320 * function fails, the contents of the buffer
323 * Returns : 0 => Ok, everything else is an error.
325 *********************************************************************/
326 int create_url_spec(struct url_spec * url, char * buf)
329 struct url_spec tmp_url[1];
331 /* paranoia - should never happen. */
332 if ((url == NULL) || (buf == NULL))
337 /* save a copy of the orignal specification */
338 if ((url->spec = strdup(buf)) == NULL)
343 if ((p = strchr(buf, '/')))
345 if (NULL == (url->path = strdup(p)))
350 url->pathlen = strlen(url->path);
364 if (NULL == (url->preg = zalloc(sizeof(*url->preg))))
371 sprintf(rebuf, "^(%s)", url->path);
373 errcode = regcomp(url->preg, rebuf,
374 (REG_EXTENDED|REG_NOSUB|REG_ICASE));
379 url->preg, buf, sizeof(buf));
383 log_error(LOG_LEVEL_ERROR, "error compiling %s: %s",
394 if ((p = strchr(buf, ':')) == NULL)
404 if ((url->domain = strdup(buf)) == NULL)
410 #endif /* def REGEX */
414 /* split domain into components */
416 *tmp_url = dsplit(url->domain);
417 url->dbuf = tmp_url->dbuf;
418 url->dcnt = tmp_url->dcnt;
419 url->dvec = tmp_url->dvec;
420 url->unanchored = tmp_url->unanchored;
426 /*********************************************************************
428 * Function : free_url
430 * Description : Called from the "unloaders". Freez the url
431 * structure elements.
434 * 1 : url = pointer to a url_spec structure.
438 *********************************************************************/
439 void free_url(struct url_spec *url)
441 if (url == NULL) return;
459 /*********************************************************************
461 * Function : check_file_changed
463 * Description : Helper function to check if a file needs reloading.
464 * If "current" is still current, return it. Otherwise
465 * allocates a new (zeroed) "struct file_list", fills
466 * in the disk file name and timestamp, and returns it.
469 * 1 : current = The file_list currently being used - will
470 * be checked to see if it is out of date.
471 * May be NULL (which is treated as out of
473 * 2 : filename = Name of file to check.
474 * 3 : newfl = New file list. [Output only]
475 * This will be set to NULL, OR a struct
476 * file_list newly allocated on the
477 * heap, with the filename and lastmodified
478 * fields filled, standard header giving file
479 * name in proxy_args, and all others zeroed.
480 * (proxy_args is only filled in if !defined
481 * SPLIT_PROXY_ARGS and !suppress_blocklists).
483 * Returns : If file unchanged: 0 (and sets newfl == NULL)
484 * If file changed: 1 and sets newfl != NULL
485 * On error: 1 and sets newfl == NULL
487 *********************************************************************/
488 int check_file_changed(const struct file_list * current,
489 const char * filename,
490 struct file_list ** newfl)
492 struct file_list *fs;
493 struct stat statbuf[1];
497 if (stat(filename, statbuf) < 0)
499 /* Error, probably file not found. */
504 && (current->lastmodified == statbuf->st_mtime)
505 && (0 == strcmp(current->filename, filename)))
510 fs = (struct file_list *)zalloc(sizeof(struct file_list));
514 /* Out of memory error */
518 fs->filename = strdup(filename);
519 fs->lastmodified = statbuf->st_mtime;
521 if (fs->filename == NULL)
523 /* Out of memory error */
528 #ifndef SPLIT_PROXY_ARGS
529 if (!suppress_blocklists)
531 char * p = html_encode(filename);
534 fs->proxy_args = strsav(fs->proxy_args, "<h2>The file `");
535 fs->proxy_args = strsav(fs->proxy_args, p);
536 fs->proxy_args = strsav(fs->proxy_args,
537 "' contains the following patterns</h2>\n");
540 fs->proxy_args = strsav(fs->proxy_args, "<pre>");
542 #endif /* ndef SPLIT_PROXY_ARGS */
549 /*********************************************************************
551 * Function : read_config_line
553 * Description : Read a single non-empty line from a file and return
554 * it. Trims comments, leading and trailing whitespace
555 * and respects escaping of newline and comment char.
556 * Also writes the file to fs->proxy_args.
559 * 1 : buf = Buffer to use.
560 * 2 : buflen = Size of buffer in bytes.
561 * 3 : fp = File to read from
562 * 4 : fs = File will be written to fs->proxy_args. May
563 * be NULL to disable this feature.
565 * Returns : NULL on EOF or error
566 * Otherwise, returns buf.
568 *********************************************************************/
569 char *read_config_line(char *buf, int buflen, FILE *fp, struct file_list *fs)
572 char linebuf[BUFSIZ];
577 while (fgets(linebuf, sizeof(linebuf), fp))
579 #ifndef SPLIT_PROXY_ARGS
580 if (fs && !suppress_blocklists)
582 char *html_line = html_encode(linebuf);
583 if (html_line != NULL)
585 fs->proxy_args = strsav(fs->proxy_args, html_line);
588 fs->proxy_args = strsav(fs->proxy_args, "<br>");
590 #endif /* ndef SPLIT_PROXY_ARGS */
592 /* Trim off newline */
593 if ((p = strpbrk(linebuf, "\r\n")) != NULL)
598 /* Line continuation? Trim escape and set flag. */
599 if ((p != linebuf) && (*--p == '\\'))
605 /* If there's a comment char.. */
606 if ((p = strpbrk(linebuf, "#")) != NULL)
608 /* ..and it's escaped, left-shift the line over the escape. */
609 if ((p != linebuf) && (*(p-1) == '\\'))
612 while ((*q++ = *p++) != '\0') /* nop */;
614 /* Else, chop off the rest of the line */
621 /* Write to the buffer */
624 strncat(buf, linebuf, buflen - strlen(buf));
634 /* Remove leading and trailing whitespace */
650 /*********************************************************************
652 * Function : unload_trustfile
654 * Description : Unloads a trustfile.
657 * 1 : f = the data structure associated with the trustfile.
661 *********************************************************************/
662 static void unload_trustfile(void *f)
664 struct block_spec *b = (struct block_spec *)f;
665 if (b == NULL) return;
667 unload_trustfile(b->next);
676 /*********************************************************************
678 * Function : load_trustfile
680 * Description : Read and parse a trustfile and add to files list.
683 * 1 : csp = Current client state (buffers, headers, etc...)
685 * Returns : 0 => Ok, everything else is an error.
687 *********************************************************************/
688 int load_trustfile(struct client_state *csp)
692 struct block_spec *b, *bl;
693 struct url_spec **tl;
695 char buf[BUFSIZ], *p, *q;
697 struct file_list *fs;
699 if (!check_file_changed(current_trustfile, csp->config->trustfile, &fs))
701 /* No need to load */
704 csp->tlist = current_trustfile;
710 goto load_trustfile_error;
713 fs->f = bl = (struct block_spec *)zalloc(sizeof(*bl));
716 goto load_trustfile_error;
719 if ((fp = fopen(csp->config->trustfile, "r")) == NULL)
721 goto load_trustfile_error;
724 tl = csp->config->trust_list;
726 while (read_config_line(buf, sizeof(buf), fp, fs) != NULL)
742 while ((*p++ = *q++))
748 /* skip blank lines */
754 /* allocate a new node */
755 if ((b = zalloc(sizeof(*b))) == NULL)
758 goto load_trustfile_error;
761 /* add it to the list */
767 /* Save the URL pattern */
768 if (create_url_spec(b->url, buf))
771 goto load_trustfile_error;
775 * save a pointer to URL's spec in the list of trusted URL's, too
787 #ifndef SPLIT_PROXY_ARGS
788 if (!suppress_blocklists)
790 fs->proxy_args = strsav(fs->proxy_args, "</pre>");
792 #endif /* ndef SPLIT_PROXY_ARGS */
794 /* the old one is now obsolete */
795 if (current_trustfile)
797 current_trustfile->unloader = unload_trustfile;
800 fs->next = files->next;
802 current_trustfile = fs;
811 load_trustfile_error:
812 log_error(LOG_LEVEL_FATAL, "can't load trustfile '%s': %E",
813 csp->config->trustfile);
817 #endif /* def TRUST_FILES */
821 /*********************************************************************
823 * Function : unload_re_filterfile
825 * Description : Unload the re_filter list.
828 * 1 : f = the data structure associated with the filterfile.
832 *********************************************************************/
833 static void unload_re_filterfile(void *f)
836 struct re_filterfile_spec *b = (struct re_filterfile_spec *)f;
838 if (b == NULL) return;
840 destroy_list(b->patterns);
842 joblist = b->joblist;
843 while ( NULL != (joblist = pcrs_free_job(joblist)) ) {}
849 /*********************************************************************
851 * Function : load_re_filterfile
853 * Description : Load the re_filterfile. Each non-comment, non-empty
854 * line is instantly added to the joblist, which is
855 * a chained list of pcrs_job structs.
858 * 1 : csp = Current client state (buffers, headers, etc...)
860 * Returns : 0 => Ok, everything else is an error.
862 *********************************************************************/
863 int load_re_filterfile(struct client_state *csp)
867 struct re_filterfile_spec *bl;
868 struct file_list *fs;
874 if (!check_file_changed(current_re_filterfile, csp->config->re_filterfile, &fs))
876 /* No need to load */
879 csp->rlist = current_re_filterfile;
885 goto load_re_filterfile_error;
888 fs->f = bl = (struct re_filterfile_spec *)zalloc(sizeof(*bl));
891 goto load_re_filterfile_error;
894 /* Open the file or fail */
895 if ((fp = fopen(csp->config->re_filterfile, "r")) == NULL)
897 goto load_re_filterfile_error;
900 /* Read line by line */
901 while (read_config_line(buf, sizeof(buf), fp, fs) != NULL)
903 enlist( bl->patterns, buf );
905 /* We have a meaningful line -> make it a job */
906 if ((dummy = pcrs_make_job(buf, &error)) == NULL)
908 log_error(LOG_LEVEL_RE_FILTER,
909 "Adding re_filter job %s failed with error %d.", buf, error);
914 dummy->next = bl->joblist;
916 log_error(LOG_LEVEL_RE_FILTER, "Adding re_filter job %s succeeded.", buf);
922 #ifndef SPLIT_PROXY_ARGS
923 if (!suppress_blocklists)
925 fs->proxy_args = strsav(fs->proxy_args, "</pre>");
927 #endif /* ndef SPLIT_PROXY_ARGS */
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);
952 #endif /* def PCRS */
955 /*********************************************************************
957 * Function : add_loader
959 * Description : Called from `load_config'. Called once for each input
960 * file found in config.
963 * 1 : loader = pointer to a function that can parse and load
964 * the appropriate config file.
965 * 2 : config = The configuration_spec to add the loader to.
969 *********************************************************************/
970 void add_loader(int (*loader)(struct client_state *),
971 struct configuration_spec * config)
975 for (i=0; i < NLOADERS; i++)
977 if (config->loaders[i] == NULL)
979 config->loaders[i] = loader;
987 /*********************************************************************
989 * Function : run_loader
991 * Description : Called from `load_config' and `listen_loop'. This
992 * function keeps the "csp" current with any file mods
993 * since the last loop. If a file is unchanged, the
994 * loader functions do NOT reload the file.
997 * 1 : csp = Current client state (buffers, headers, etc...)
998 * Must be non-null. Reads: "csp->config"
999 * Writes: various data members.
1001 * Returns : 0 => Ok, everything else is an error.
1003 *********************************************************************/
1004 int run_loader(struct client_state *csp)
1009 for (i=0; i < NLOADERS; i++)
1011 if (csp->config->loaders[i] == NULL)
1015 ret |= (csp->config->loaders[i])(csp);