1 const char loaders_rcs[] = "$Id: loaders.c,v 1.17 2001/06/29 13:31:51 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.17 2001/06/29 13:31:51 oes
41 * Revision 1.16 2001/06/09 10:55:28 jongfoster
42 * Changing BUFSIZ ==> BUFFER_SIZE
44 * Revision 1.15 2001/06/07 23:14:14 jongfoster
45 * Removing ACL and forward file loaders - these
46 * files have been merged into the config file.
47 * Cosmetic: Moving unloader funcs next to their
48 * respective loader funcs
50 * Revision 1.14 2001/06/01 03:27:04 oes
51 * Fixed line continuation problem
53 * Revision 1.13 2001/05/31 21:28:49 jongfoster
54 * Removed all permissionsfile code - it's now called the actions
55 * file, and (almost) all the code is in actions.c
57 * Revision 1.12 2001/05/31 17:32:31 oes
59 * - Enhanced domain part globbing with infix and prefix asterisk
60 * matching and optional unanchored operation
62 * Revision 1.11 2001/05/29 23:25:24 oes
64 * - load_config_line() and load_permissions_file() now use chomp()
66 * Revision 1.10 2001/05/29 09:50:24 jongfoster
67 * Unified blocklist/imagelist/permissionslist.
68 * File format is still under discussion, but the internal changes
71 * Also modified interceptor behaviour:
72 * - We now intercept all URLs beginning with one of the following
73 * prefixes (and *only* these prefixes):
75 * * http://ijbswa.sf.net/config/
76 * * http://ijbswa.sourceforge.net/config/
77 * - New interceptors "home page" - go to http://i.j.b/ to see it.
78 * - Internal changes so that intercepted and fast redirect pages
79 * are not replaced with an image.
80 * - Interceptors now have the option to send a binary page direct
81 * to the client. (i.e. ijb-send-banner uses this)
82 * - Implemented show-url-info interceptor. (Which is why I needed
83 * the above interceptors changes - a typical URL is
84 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
85 * The previous mechanism would not have intercepted that, and
86 * if it had been intercepted then it then it would have replaced
89 * Revision 1.9 2001/05/26 17:12:07 jongfoster
90 * Fatal errors loading configuration files now give better error messages.
92 * Revision 1.8 2001/05/26 00:55:20 jongfoster
93 * Removing duplicated code. load_forwardfile() now uses create_url_spec()
95 * Revision 1.7 2001/05/26 00:28:36 jongfoster
96 * Automatic reloading of config file.
97 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
98 * Most of the global variables have been moved to a new
99 * struct configuration_spec, accessed through csp->config->globalname
100 * Most of the globals remaining are used by the Win32 GUI.
102 * Revision 1.6 2001/05/23 12:27:33 oes
104 * Fixed ugly indentation of my last changes
106 * Revision 1.5 2001/05/23 10:39:05 oes
107 * - Added support for escaping the comment character
108 * in config files by a backslash
109 * - Added support for line continuation in config
111 * - Fixed a buffer overflow bug with long config lines
113 * Revision 1.4 2001/05/22 18:56:28 oes
116 * Revision 1.3 2001/05/20 01:21:20 jongfoster
117 * Version 2.9.4 checkin.
118 * - Merged popupfile and cookiefile, and added control over PCRS
119 * filtering, in new "permissionsfile".
120 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
121 * file error you now get a message box (in the Win32 GUI) rather
122 * than the program exiting with no explanation.
123 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
125 * - Removed tabs from "config"
126 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
127 * - Bumped up version number.
129 * Revision 1.2 2001/05/17 23:01:01 oes
130 * - Cleaned CRLF's from the sources and related files
132 * Revision 1.1.1.1 2001/05/15 13:58:59 oes
133 * Initial import of version 2.9.3 source tree
136 *********************************************************************/
143 #include <sys/types.h>
147 #include <sys/stat.h>
162 #include "miscutil.h"
167 const char loaders_h_rcs[] = LOADERS_H_VERSION;
169 /* Fix a problem with Solaris. There should be no effect on other
171 * Solaris's isspace() is a macro which uses it's argument directly
172 * as an array index. Therefore we need to make sure that high-bit
173 * characters generate +ve values, and ideally we also want to make
174 * the argument match the declared parameter type of "int".
176 #define ijb_isspace(__X) isspace((int)(unsigned char)(__X))
180 * Currently active files.
181 * These are also entered in the main linked list of files.
185 static struct file_list *current_trustfile = NULL;
186 #endif /* def TRUST_FILES */
189 static struct file_list *current_re_filterfile = NULL;
190 #endif /* def PCRS */
193 /*********************************************************************
197 * Description : Basically a mark and sweep garbage collector, it is run
198 * (by the parent thread) every once in a while to reclaim memory.
200 * It uses a mark and sweep strategy:
201 * 1) mark all files as inactive
203 * 2) check with each client:
204 * if it is active, mark its files as active
205 * if it is inactive, free its resources
207 * 3) free the resources of all of the files that
208 * are still marked as inactive (and are obsolete).
210 * N.B. files that are not obsolete don't have an unloader defined.
216 *********************************************************************/
219 struct file_list *fl, *nfl;
220 struct client_state *csp, *ncsp;
222 /* clear all of the file's active flags */
223 for ( fl = files->next; NULL != fl; fl = fl->next )
228 for (csp = clients; csp && (ncsp = csp->next) ; csp = csp->next)
232 /* mark this client's files as active */
235 * Always have a configuration file.
236 * (Also note the slightly non-standard extra
239 ncsp->config->config_file_list->active = 1;
241 if (ncsp->actions_list) /* actions files */
243 ncsp->actions_list->active = 1;
247 if (ncsp->rlist) /* perl re files */
249 ncsp->rlist->active = 1;
251 #endif /* def PCRS */
254 if (ncsp->tlist) /* trust files */
256 ncsp->tlist->active = 1;
258 #endif /* def TRUST_FILES */
263 /* this client one is not active, release its resources */
264 csp->next = ncsp->next;
266 freez(ncsp->ip_addr_str);
267 freez(ncsp->my_ip_addr_str);
268 freez(ncsp->my_hostname);
271 freez(ncsp->referrer);
272 #endif /* def TRUST_FILES */
273 freez(ncsp->x_forwarded);
274 freez(ncsp->iob->buf);
276 free_http_request(ncsp->http);
278 destroy_list(ncsp->headers);
279 destroy_list(ncsp->cookie_list);
281 free_current_action(ncsp->action);
289 #endif /* def STATISTICS */
295 for (fl = files; fl && (nfl = fl->next) ; fl = fl->next)
297 if ( ( 0 == nfl->active ) && ( NULL != nfl->unloader ) )
299 fl->next = nfl->next;
301 (nfl->unloader)(nfl->f);
303 #ifndef SPLIT_PROXY_ARGS
304 freez(nfl->proxy_args);
305 #endif /* ndef SPLIT_PROXY_ARGS */
307 freez(nfl->filename);
316 /*********************************************************************
318 * Function : create_url_spec
320 * Description : Creates a "url_spec" structure from a string.
321 * When finished, free with unload_url().
324 * 1 : url = Target url_spec to be filled in. Must be
325 * zeroed out before the call (e.g. using zalloc).
326 * 2 : buf = Source pattern, null terminated. NOTE: The
327 * contents of this buffer are destroyed by this
328 * function. If this function succeeds, the
329 * buffer is copied to url->spec. If this
330 * function fails, the contents of the buffer
333 * Returns : 0 => Ok, everything else is an error.
335 *********************************************************************/
336 int create_url_spec(struct url_spec * url, char * buf)
339 struct url_spec tmp_url[1];
341 /* paranoia - should never happen. */
342 if ((url == NULL) || (buf == NULL))
347 /* save a copy of the orignal specification */
348 if ((url->spec = strdup(buf)) == NULL)
353 if ((p = strchr(buf, '/')))
355 if (NULL == (url->path = strdup(p)))
360 url->pathlen = strlen(url->path);
372 char rebuf[BUFFER_SIZE];
374 if (NULL == (url->preg = zalloc(sizeof(*url->preg))))
381 sprintf(rebuf, "^(%s)", url->path);
383 errcode = regcomp(url->preg, rebuf,
384 (REG_EXTENDED|REG_NOSUB|REG_ICASE));
387 size_t errlen = regerror(errcode,
388 url->preg, buf, sizeof(buf));
392 log_error(LOG_LEVEL_ERROR, "error compiling %s: %s",
403 if ((p = strchr(buf, ':')) == NULL)
413 if ((url->domain = strdup(buf)) == NULL)
419 #endif /* def REGEX */
423 /* split domain into components */
425 *tmp_url = dsplit(url->domain);
426 url->dbuf = tmp_url->dbuf;
427 url->dcnt = tmp_url->dcnt;
428 url->dvec = tmp_url->dvec;
429 url->unanchored = tmp_url->unanchored;
436 /*********************************************************************
438 * Function : free_url
440 * Description : Called from the "unloaders". Freez the url
441 * structure elements.
444 * 1 : url = pointer to a url_spec structure.
448 *********************************************************************/
449 void free_url(struct url_spec *url)
451 if (url == NULL) return;
469 /*********************************************************************
471 * Function : check_file_changed
473 * Description : Helper function to check if a file needs reloading.
474 * If "current" is still current, return it. Otherwise
475 * allocates a new (zeroed) "struct file_list", fills
476 * in the disk file name and timestamp, and returns it.
479 * 1 : current = The file_list currently being used - will
480 * be checked to see if it is out of date.
481 * May be NULL (which is treated as out of
483 * 2 : filename = Name of file to check.
484 * 3 : newfl = New file list. [Output only]
485 * This will be set to NULL, OR a struct
486 * file_list newly allocated on the
487 * heap, with the filename and lastmodified
488 * fields filled, standard header giving file
489 * name in proxy_args, and all others zeroed.
490 * (proxy_args is only filled in if !defined
491 * SPLIT_PROXY_ARGS and !suppress_blocklists).
493 * Returns : If file unchanged: 0 (and sets newfl == NULL)
494 * If file changed: 1 and sets newfl != NULL
495 * On error: 1 and sets newfl == NULL
497 *********************************************************************/
498 int check_file_changed(const struct file_list * current,
499 const char * filename,
500 struct file_list ** newfl)
502 struct file_list *fs;
503 struct stat statbuf[1];
507 if (stat(filename, statbuf) < 0)
509 /* Error, probably file not found. */
514 && (current->lastmodified == statbuf->st_mtime)
515 && (0 == strcmp(current->filename, filename)))
520 fs = (struct file_list *)zalloc(sizeof(struct file_list));
524 /* Out of memory error */
528 fs->filename = strdup(filename);
529 fs->lastmodified = statbuf->st_mtime;
531 if (fs->filename == NULL)
533 /* Out of memory error */
538 #ifndef SPLIT_PROXY_ARGS
539 if (!suppress_blocklists)
541 char * p = html_encode(filename);
544 fs->proxy_args = strsav(fs->proxy_args, "<h2>The file `");
545 fs->proxy_args = strsav(fs->proxy_args, p);
546 fs->proxy_args = strsav(fs->proxy_args,
547 "' contains the following patterns</h2>\n");
550 fs->proxy_args = strsav(fs->proxy_args, "<pre>");
552 #endif /* ndef SPLIT_PROXY_ARGS */
560 /*********************************************************************
562 * Function : read_config_line
564 * Description : Read a single non-empty line from a file and return
565 * it. Trims comments, leading and trailing whitespace
566 * and respects escaping of newline and comment char.
567 * Also writes the file to fs->proxy_args.
570 * 1 : buf = Buffer to use.
571 * 2 : buflen = Size of buffer in bytes.
572 * 3 : fp = File to read from
573 * 4 : fs = File will be written to fs->proxy_args. May
574 * be NULL to disable this feature.
576 * Returns : NULL on EOF or error
577 * Otherwise, returns buf.
579 *********************************************************************/
580 char *read_config_line(char *buf, int buflen, FILE *fp, struct file_list *fs)
583 char linebuf[BUFFER_SIZE];
588 while (fgets(linebuf, sizeof(linebuf), fp))
590 #ifndef SPLIT_PROXY_ARGS
591 if (fs && !suppress_blocklists)
593 char *html_line = html_encode(linebuf);
594 if (html_line != NULL)
596 fs->proxy_args = strsav(fs->proxy_args, html_line);
599 fs->proxy_args = strsav(fs->proxy_args, "<br>");
601 #endif /* ndef SPLIT_PROXY_ARGS */
603 /* Trim off newline */
604 if ((p = strpbrk(linebuf, "\r\n")) != NULL)
609 /* Line continuation? Trim escape and set flag. */
610 if ((p != linebuf) && (*--p == '\\'))
616 /* If there's a comment char.. */
617 if ((p = strpbrk(linebuf, "#")) != NULL)
619 /* ..and it's escaped, left-shift the line over the escape. */
620 if ((p != linebuf) && (*(p-1) == '\\'))
623 while ((*q++ = *p++) != '\0') /* nop */;
625 /* Else, chop off the rest of the line */
632 /* Write to the buffer */
635 strncat(buf, linebuf, buflen - strlen(buf));
645 /* Remove leading and trailing whitespace */
661 /*********************************************************************
663 * Function : unload_trustfile
665 * Description : Unloads a trustfile.
668 * 1 : f = the data structure associated with the trustfile.
672 *********************************************************************/
673 static void unload_trustfile(void *f)
675 struct block_spec *b = (struct block_spec *)f;
676 if (b == NULL) return;
678 unload_trustfile(b->next); /* Stack is cheap, isn't it? */
687 /*********************************************************************
689 * Function : load_trustfile
691 * Description : Read and parse a trustfile and add to files list.
694 * 1 : csp = Current client state (buffers, headers, etc...)
696 * Returns : 0 => Ok, everything else is an error.
698 *********************************************************************/
699 int load_trustfile(struct client_state *csp)
703 struct block_spec *b, *bl;
704 struct url_spec **tl;
706 char buf[BUFFER_SIZE], *p, *q;
708 struct file_list *fs;
710 if (!check_file_changed(current_trustfile, csp->config->trustfile, &fs))
712 /* No need to load */
715 csp->tlist = current_trustfile;
721 goto load_trustfile_error;
724 fs->f = bl = (struct block_spec *)zalloc(sizeof(*bl));
727 goto load_trustfile_error;
730 if ((fp = fopen(csp->config->trustfile, "r")) == NULL)
732 goto load_trustfile_error;
735 tl = csp->config->trust_list;
737 while (read_config_line(buf, sizeof(buf), fp, fs) != NULL)
753 while ((*p++ = *q++))
759 /* skip blank lines */
765 /* allocate a new node */
766 if ((b = zalloc(sizeof(*b))) == NULL)
769 goto load_trustfile_error;
772 /* add it to the list */
778 /* Save the URL pattern */
779 if (create_url_spec(b->url, buf))
782 goto load_trustfile_error;
786 * save a pointer to URL's spec in the list of trusted URL's, too
798 #ifndef SPLIT_PROXY_ARGS
799 if (!suppress_blocklists)
801 fs->proxy_args = strsav(fs->proxy_args, "</pre>");
803 #endif /* ndef SPLIT_PROXY_ARGS */
805 /* the old one is now obsolete */
806 if (current_trustfile)
808 current_trustfile->unloader = unload_trustfile;
811 fs->next = files->next;
813 current_trustfile = fs;
822 load_trustfile_error:
823 log_error(LOG_LEVEL_FATAL, "can't load trustfile '%s': %E",
824 csp->config->trustfile);
828 #endif /* def TRUST_FILES */
832 /*********************************************************************
834 * Function : unload_re_filterfile
836 * Description : Unload the re_filter list.
839 * 1 : f = the data structure associated with the filterfile.
843 *********************************************************************/
844 static void unload_re_filterfile(void *f)
846 struct re_filterfile_spec *b = (struct re_filterfile_spec *)f;
848 if (b == NULL) return;
850 destroy_list(b->patterns);
851 pcrs_free_joblist(b->joblist);
857 /*********************************************************************
859 * Function : load_re_filterfile
861 * Description : Load the re_filterfile. Each non-comment, non-empty
862 * line is instantly added to the joblist, which is
863 * a chained list of pcrs_job structs.
866 * 1 : csp = Current client state (buffers, headers, etc...)
868 * Returns : 0 => Ok, everything else is an error.
870 *********************************************************************/
871 int load_re_filterfile(struct client_state *csp)
875 struct re_filterfile_spec *bl;
876 struct file_list *fs;
878 char buf[BUFFER_SIZE];
882 if (!check_file_changed(current_re_filterfile, csp->config->re_filterfile, &fs))
884 /* No need to load */
887 csp->rlist = current_re_filterfile;
893 goto load_re_filterfile_error;
896 fs->f = bl = (struct re_filterfile_spec *)zalloc(sizeof(*bl));
899 goto load_re_filterfile_error;
902 /* Open the file or fail */
903 if ((fp = fopen(csp->config->re_filterfile, "r")) == NULL)
905 goto load_re_filterfile_error;
908 /* Read line by line */
909 while (read_config_line(buf, sizeof(buf), fp, fs) != NULL)
911 enlist( bl->patterns, buf );
913 /* We have a meaningful line -> make it a job */
914 if ((dummy = pcrs_compile(buf, &error)) == NULL)
916 log_error(LOG_LEVEL_RE_FILTER,
917 "Adding re_filter job %s failed with error %d.", buf, error);
922 dummy->next = bl->joblist;
924 log_error(LOG_LEVEL_RE_FILTER, "Adding re_filter job %s succeeded.", buf);
930 #ifndef SPLIT_PROXY_ARGS
931 if (!suppress_blocklists)
933 fs->proxy_args = strsav(fs->proxy_args, "</pre>");
935 #endif /* ndef SPLIT_PROXY_ARGS */
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);
960 #endif /* def PCRS */
963 /*********************************************************************
965 * Function : add_loader
967 * Description : Called from `load_config'. Called once for each input
968 * file found in config.
971 * 1 : loader = pointer to a function that can parse and load
972 * the appropriate config file.
973 * 2 : config = The configuration_spec to add the loader to.
977 *********************************************************************/
978 void add_loader(int (*loader)(struct client_state *),
979 struct configuration_spec * config)
983 for (i=0; i < NLOADERS; i++)
985 if (config->loaders[i] == NULL)
987 config->loaders[i] = loader;
995 /*********************************************************************
997 * Function : run_loader
999 * Description : Called from `load_config' and `listen_loop'. This
1000 * function keeps the "csp" current with any file mods
1001 * since the last loop. If a file is unchanged, the
1002 * loader functions do NOT reload the file.
1005 * 1 : csp = Current client state (buffers, headers, etc...)
1006 * Must be non-null. Reads: "csp->config"
1007 * Writes: various data members.
1009 * Returns : 0 => Ok, everything else is an error.
1011 *********************************************************************/
1012 int run_loader(struct client_state *csp)
1017 for (i=0; i < NLOADERS; i++)
1019 if (csp->config->loaders[i] == NULL)
1023 ret |= (csp->config->loaders[i])(csp);