1 const char loaders_rcs[] = "$Id: loaders.c,v 1.18 2001/06/29 21:45:41 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.18 2001/06/29 21:45:41 oes
39 * Indentation, CRLF->LF, Tab-> Space
41 * Revision 1.17 2001/06/29 13:31:51 oes
44 * Revision 1.16 2001/06/09 10:55:28 jongfoster
45 * Changing BUFSIZ ==> BUFFER_SIZE
47 * Revision 1.15 2001/06/07 23:14:14 jongfoster
48 * Removing ACL and forward file loaders - these
49 * files have been merged into the config file.
50 * Cosmetic: Moving unloader funcs next to their
51 * respective loader funcs
53 * Revision 1.14 2001/06/01 03:27:04 oes
54 * Fixed line continuation problem
56 * Revision 1.13 2001/05/31 21:28:49 jongfoster
57 * Removed all permissionsfile code - it's now called the actions
58 * file, and (almost) all the code is in actions.c
60 * Revision 1.12 2001/05/31 17:32:31 oes
62 * - Enhanced domain part globbing with infix and prefix asterisk
63 * matching and optional unanchored operation
65 * Revision 1.11 2001/05/29 23:25:24 oes
67 * - load_config_line() and load_permissions_file() now use chomp()
69 * Revision 1.10 2001/05/29 09:50:24 jongfoster
70 * Unified blocklist/imagelist/permissionslist.
71 * File format is still under discussion, but the internal changes
74 * Also modified interceptor behaviour:
75 * - We now intercept all URLs beginning with one of the following
76 * prefixes (and *only* these prefixes):
78 * * http://ijbswa.sf.net/config/
79 * * http://ijbswa.sourceforge.net/config/
80 * - New interceptors "home page" - go to http://i.j.b/ to see it.
81 * - Internal changes so that intercepted and fast redirect pages
82 * are not replaced with an image.
83 * - Interceptors now have the option to send a binary page direct
84 * to the client. (i.e. ijb-send-banner uses this)
85 * - Implemented show-url-info interceptor. (Which is why I needed
86 * the above interceptors changes - a typical URL is
87 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
88 * The previous mechanism would not have intercepted that, and
89 * if it had been intercepted then it then it would have replaced
92 * Revision 1.9 2001/05/26 17:12:07 jongfoster
93 * Fatal errors loading configuration files now give better error messages.
95 * Revision 1.8 2001/05/26 00:55:20 jongfoster
96 * Removing duplicated code. load_forwardfile() now uses create_url_spec()
98 * Revision 1.7 2001/05/26 00:28:36 jongfoster
99 * Automatic reloading of config file.
100 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
101 * Most of the global variables have been moved to a new
102 * struct configuration_spec, accessed through csp->config->globalname
103 * Most of the globals remaining are used by the Win32 GUI.
105 * Revision 1.6 2001/05/23 12:27:33 oes
107 * Fixed ugly indentation of my last changes
109 * Revision 1.5 2001/05/23 10:39:05 oes
110 * - Added support for escaping the comment character
111 * in config files by a backslash
112 * - Added support for line continuation in config
114 * - Fixed a buffer overflow bug with long config lines
116 * Revision 1.4 2001/05/22 18:56:28 oes
119 * Revision 1.3 2001/05/20 01:21:20 jongfoster
120 * Version 2.9.4 checkin.
121 * - Merged popupfile and cookiefile, and added control over PCRS
122 * filtering, in new "permissionsfile".
123 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
124 * file error you now get a message box (in the Win32 GUI) rather
125 * than the program exiting with no explanation.
126 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
128 * - Removed tabs from "config"
129 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
130 * - Bumped up version number.
132 * Revision 1.2 2001/05/17 23:01:01 oes
133 * - Cleaned CRLF's from the sources and related files
135 * Revision 1.1.1.1 2001/05/15 13:58:59 oes
136 * Initial import of version 2.9.3 source tree
139 *********************************************************************/
146 #include <sys/types.h>
150 #include <sys/stat.h>
165 #include "miscutil.h"
170 const char loaders_h_rcs[] = LOADERS_H_VERSION;
172 /* Fix a problem with Solaris. There should be no effect on other
174 * Solaris's isspace() is a macro which uses it's argument directly
175 * as an array index. Therefore we need to make sure that high-bit
176 * characters generate +ve values, and ideally we also want to make
177 * the argument match the declared parameter type of "int".
179 #define ijb_isspace(__X) isspace((int)(unsigned char)(__X))
183 * Currently active files.
184 * These are also entered in the main linked list of files.
188 static struct file_list *current_trustfile = NULL;
189 #endif /* def TRUST_FILES */
191 static struct file_list *current_re_filterfile = NULL;
195 /*********************************************************************
199 * Description : Basically a mark and sweep garbage collector, it is run
200 * (by the parent thread) every once in a while to reclaim memory.
202 * It uses a mark and sweep strategy:
203 * 1) mark all files as inactive
205 * 2) check with each client:
206 * if it is active, mark its files as active
207 * if it is inactive, free its resources
209 * 3) free the resources of all of the files that
210 * are still marked as inactive (and are obsolete).
212 * N.B. files that are not obsolete don't have an unloader defined.
218 *********************************************************************/
221 struct file_list *fl, *nfl;
222 struct client_state *csp, *ncsp;
224 /* clear all of the file's active flags */
225 for ( fl = files->next; NULL != fl; fl = fl->next )
230 for (csp = clients; csp && (ncsp = csp->next) ; csp = csp->next)
234 /* mark this client's files as active */
237 * Always have a configuration file.
238 * (Also note the slightly non-standard extra
241 ncsp->config->config_file_list->active = 1;
243 if (ncsp->actions_list) /* actions files */
245 ncsp->actions_list->active = 1;
248 if (ncsp->rlist) /* pcrsjob files */
250 ncsp->rlist->active = 1;
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 */
831 /*********************************************************************
833 * Function : unload_re_filterfile
835 * Description : Unload the re_filter list.
838 * 1 : f = the data structure associated with the filterfile.
842 *********************************************************************/
843 static void unload_re_filterfile(void *f)
845 struct re_filterfile_spec *b = (struct re_filterfile_spec *)f;
847 if (b == NULL) return;
849 destroy_list(b->patterns);
850 pcrs_free_joblist(b->joblist);
856 /*********************************************************************
858 * Function : load_re_filterfile
860 * Description : Load the re_filterfile. Each non-comment, non-empty
861 * line is instantly added to the joblist, which is
862 * a chained list of pcrs_job structs.
865 * 1 : csp = Current client state (buffers, headers, etc...)
867 * Returns : 0 => Ok, everything else is an error.
869 *********************************************************************/
870 int load_re_filterfile(struct client_state *csp)
874 struct re_filterfile_spec *bl;
875 struct file_list *fs;
877 char buf[BUFFER_SIZE];
881 if (!check_file_changed(current_re_filterfile, csp->config->re_filterfile, &fs))
883 /* No need to load */
886 csp->rlist = current_re_filterfile;
892 goto load_re_filterfile_error;
895 fs->f = bl = (struct re_filterfile_spec *)zalloc(sizeof(*bl));
898 goto load_re_filterfile_error;
901 /* Open the file or fail */
902 if ((fp = fopen(csp->config->re_filterfile, "r")) == NULL)
904 goto load_re_filterfile_error;
907 /* Read line by line */
908 while (read_config_line(buf, sizeof(buf), fp, fs) != NULL)
910 enlist( bl->patterns, buf );
912 /* We have a meaningful line -> make it a job */
913 if ((dummy = pcrs_compile(buf, &error)) == NULL)
915 log_error(LOG_LEVEL_RE_FILTER,
916 "Adding re_filter job %s failed with error %d.", buf, error);
921 dummy->next = bl->joblist;
923 log_error(LOG_LEVEL_RE_FILTER, "Adding re_filter job %s succeeded.", buf);
929 #ifndef SPLIT_PROXY_ARGS
930 if (!suppress_blocklists)
932 fs->proxy_args = strsav(fs->proxy_args, "</pre>");
934 #endif /* ndef SPLIT_PROXY_ARGS */
936 /* the old one is now obsolete */
937 if ( NULL != current_re_filterfile )
939 current_re_filterfile->unloader = unload_re_filterfile;
942 fs->next = files->next;
944 current_re_filterfile = fs;
953 load_re_filterfile_error:
954 log_error(LOG_LEVEL_FATAL, "can't load re_filterfile '%s': %E",
955 csp->config->re_filterfile);
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);