1 const char loaders_rcs[] = "$Id: loaders.c,v 1.20 2001/07/17 13:07:01 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.20 2001/07/17 13:07:01 oes
39 * Fixed segv when last line in config files
40 * lacked a terminating (\r)\n
42 * Revision 1.19 2001/07/13 14:01:54 oes
43 * Removed all #ifdef PCRS
45 * Revision 1.18 2001/06/29 21:45:41 oes
46 * Indentation, CRLF->LF, Tab-> Space
48 * Revision 1.17 2001/06/29 13:31:51 oes
51 * Revision 1.16 2001/06/09 10:55:28 jongfoster
52 * Changing BUFSIZ ==> BUFFER_SIZE
54 * Revision 1.15 2001/06/07 23:14:14 jongfoster
55 * Removing ACL and forward file loaders - these
56 * files have been merged into the config file.
57 * Cosmetic: Moving unloader funcs next to their
58 * respective loader funcs
60 * Revision 1.14 2001/06/01 03:27:04 oes
61 * Fixed line continuation problem
63 * Revision 1.13 2001/05/31 21:28:49 jongfoster
64 * Removed all permissionsfile code - it's now called the actions
65 * file, and (almost) all the code is in actions.c
67 * Revision 1.12 2001/05/31 17:32:31 oes
69 * - Enhanced domain part globbing with infix and prefix asterisk
70 * matching and optional unanchored operation
72 * Revision 1.11 2001/05/29 23:25:24 oes
74 * - load_config_line() and load_permissions_file() now use chomp()
76 * Revision 1.10 2001/05/29 09:50:24 jongfoster
77 * Unified blocklist/imagelist/permissionslist.
78 * File format is still under discussion, but the internal changes
81 * Also modified interceptor behaviour:
82 * - We now intercept all URLs beginning with one of the following
83 * prefixes (and *only* these prefixes):
85 * * http://ijbswa.sf.net/config/
86 * * http://ijbswa.sourceforge.net/config/
87 * - New interceptors "home page" - go to http://i.j.b/ to see it.
88 * - Internal changes so that intercepted and fast redirect pages
89 * are not replaced with an image.
90 * - Interceptors now have the option to send a binary page direct
91 * to the client. (i.e. ijb-send-banner uses this)
92 * - Implemented show-url-info interceptor. (Which is why I needed
93 * the above interceptors changes - a typical URL is
94 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
95 * The previous mechanism would not have intercepted that, and
96 * if it had been intercepted then it then it would have replaced
99 * Revision 1.9 2001/05/26 17:12:07 jongfoster
100 * Fatal errors loading configuration files now give better error messages.
102 * Revision 1.8 2001/05/26 00:55:20 jongfoster
103 * Removing duplicated code. load_forwardfile() now uses create_url_spec()
105 * Revision 1.7 2001/05/26 00:28:36 jongfoster
106 * Automatic reloading of config file.
107 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
108 * Most of the global variables have been moved to a new
109 * struct configuration_spec, accessed through csp->config->globalname
110 * Most of the globals remaining are used by the Win32 GUI.
112 * Revision 1.6 2001/05/23 12:27:33 oes
114 * Fixed ugly indentation of my last changes
116 * Revision 1.5 2001/05/23 10:39:05 oes
117 * - Added support for escaping the comment character
118 * in config files by a backslash
119 * - Added support for line continuation in config
121 * - Fixed a buffer overflow bug with long config lines
123 * Revision 1.4 2001/05/22 18:56:28 oes
126 * Revision 1.3 2001/05/20 01:21:20 jongfoster
127 * Version 2.9.4 checkin.
128 * - Merged popupfile and cookiefile, and added control over PCRS
129 * filtering, in new "permissionsfile".
130 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
131 * file error you now get a message box (in the Win32 GUI) rather
132 * than the program exiting with no explanation.
133 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
135 * - Removed tabs from "config"
136 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
137 * - Bumped up version number.
139 * Revision 1.2 2001/05/17 23:01:01 oes
140 * - Cleaned CRLF's from the sources and related files
142 * Revision 1.1.1.1 2001/05/15 13:58:59 oes
143 * Initial import of version 2.9.3 source tree
146 *********************************************************************/
153 #include <sys/types.h>
157 #include <sys/stat.h>
172 #include "miscutil.h"
177 const char loaders_h_rcs[] = LOADERS_H_VERSION;
179 /* Fix a problem with Solaris. There should be no effect on other
181 * Solaris's isspace() is a macro which uses it's argument directly
182 * as an array index. Therefore we need to make sure that high-bit
183 * characters generate +ve values, and ideally we also want to make
184 * the argument match the declared parameter type of "int".
186 #define ijb_isspace(__X) isspace((int)(unsigned char)(__X))
190 * Currently active files.
191 * These are also entered in the main linked list of files.
195 static struct file_list *current_trustfile = NULL;
196 #endif /* def TRUST_FILES */
198 static struct file_list *current_re_filterfile = NULL;
202 /*********************************************************************
206 * Description : Basically a mark and sweep garbage collector, it is run
207 * (by the parent thread) every once in a while to reclaim memory.
209 * It uses a mark and sweep strategy:
210 * 1) mark all files as inactive
212 * 2) check with each client:
213 * if it is active, mark its files as active
214 * if it is inactive, free its resources
216 * 3) free the resources of all of the files that
217 * are still marked as inactive (and are obsolete).
219 * N.B. files that are not obsolete don't have an unloader defined.
225 *********************************************************************/
228 struct file_list *fl, *nfl;
229 struct client_state *csp, *ncsp;
231 /* clear all of the file's active flags */
232 for ( fl = files->next; NULL != fl; fl = fl->next )
237 for (csp = clients; csp && (ncsp = csp->next) ; csp = csp->next)
241 /* mark this client's files as active */
244 * Always have a configuration file.
245 * (Also note the slightly non-standard extra
248 ncsp->config->config_file_list->active = 1;
250 if (ncsp->actions_list) /* actions files */
252 ncsp->actions_list->active = 1;
255 if (ncsp->rlist) /* pcrsjob files */
257 ncsp->rlist->active = 1;
261 if (ncsp->tlist) /* trust files */
263 ncsp->tlist->active = 1;
265 #endif /* def TRUST_FILES */
270 /* this client one is not active, release its resources */
271 csp->next = ncsp->next;
273 freez(ncsp->ip_addr_str);
274 freez(ncsp->my_ip_addr_str);
275 freez(ncsp->my_hostname);
278 freez(ncsp->referrer);
279 #endif /* def TRUST_FILES */
280 freez(ncsp->x_forwarded);
281 freez(ncsp->iob->buf);
283 free_http_request(ncsp->http);
285 destroy_list(ncsp->headers);
286 destroy_list(ncsp->cookie_list);
288 free_current_action(ncsp->action);
296 #endif /* def STATISTICS */
302 for (fl = files; fl && (nfl = fl->next) ; fl = fl->next)
304 if ( ( 0 == nfl->active ) && ( NULL != nfl->unloader ) )
306 fl->next = nfl->next;
308 (nfl->unloader)(nfl->f);
310 #ifndef SPLIT_PROXY_ARGS
311 freez(nfl->proxy_args);
312 #endif /* ndef SPLIT_PROXY_ARGS */
314 freez(nfl->filename);
323 /*********************************************************************
325 * Function : create_url_spec
327 * Description : Creates a "url_spec" structure from a string.
328 * When finished, free with unload_url().
331 * 1 : url = Target url_spec to be filled in. Must be
332 * zeroed out before the call (e.g. using zalloc).
333 * 2 : buf = Source pattern, null terminated. NOTE: The
334 * contents of this buffer are destroyed by this
335 * function. If this function succeeds, the
336 * buffer is copied to url->spec. If this
337 * function fails, the contents of the buffer
340 * Returns : 0 => Ok, everything else is an error.
342 *********************************************************************/
343 int create_url_spec(struct url_spec * url, char * buf)
346 struct url_spec tmp_url[1];
348 /* paranoia - should never happen. */
349 if ((url == NULL) || (buf == NULL))
354 /* save a copy of the orignal specification */
355 if ((url->spec = strdup(buf)) == NULL)
360 if ((p = strchr(buf, '/')))
362 if (NULL == (url->path = strdup(p)))
367 url->pathlen = strlen(url->path);
379 char rebuf[BUFFER_SIZE];
381 if (NULL == (url->preg = zalloc(sizeof(*url->preg))))
388 sprintf(rebuf, "^(%s)", url->path);
390 errcode = regcomp(url->preg, rebuf,
391 (REG_EXTENDED|REG_NOSUB|REG_ICASE));
394 size_t errlen = regerror(errcode,
395 url->preg, buf, sizeof(buf));
399 log_error(LOG_LEVEL_ERROR, "error compiling %s: %s",
410 if ((p = strchr(buf, ':')) == NULL)
420 if ((url->domain = strdup(buf)) == NULL)
426 #endif /* def REGEX */
430 /* split domain into components */
432 *tmp_url = dsplit(url->domain);
433 url->dbuf = tmp_url->dbuf;
434 url->dcnt = tmp_url->dcnt;
435 url->dvec = tmp_url->dvec;
436 url->unanchored = tmp_url->unanchored;
443 /*********************************************************************
445 * Function : free_url
447 * Description : Called from the "unloaders". Freez the url
448 * structure elements.
451 * 1 : url = pointer to a url_spec structure.
455 *********************************************************************/
456 void free_url(struct url_spec *url)
458 if (url == NULL) return;
476 /*********************************************************************
478 * Function : check_file_changed
480 * Description : Helper function to check if a file needs reloading.
481 * If "current" is still current, return it. Otherwise
482 * allocates a new (zeroed) "struct file_list", fills
483 * in the disk file name and timestamp, and returns it.
486 * 1 : current = The file_list currently being used - will
487 * be checked to see if it is out of date.
488 * May be NULL (which is treated as out of
490 * 2 : filename = Name of file to check.
491 * 3 : newfl = New file list. [Output only]
492 * This will be set to NULL, OR a struct
493 * file_list newly allocated on the
494 * heap, with the filename and lastmodified
495 * fields filled, standard header giving file
496 * name in proxy_args, and all others zeroed.
497 * (proxy_args is only filled in if !defined
498 * SPLIT_PROXY_ARGS and !suppress_blocklists).
500 * Returns : If file unchanged: 0 (and sets newfl == NULL)
501 * If file changed: 1 and sets newfl != NULL
502 * On error: 1 and sets newfl == NULL
504 *********************************************************************/
505 int check_file_changed(const struct file_list * current,
506 const char * filename,
507 struct file_list ** newfl)
509 struct file_list *fs;
510 struct stat statbuf[1];
514 if (stat(filename, statbuf) < 0)
516 /* Error, probably file not found. */
521 && (current->lastmodified == statbuf->st_mtime)
522 && (0 == strcmp(current->filename, filename)))
527 fs = (struct file_list *)zalloc(sizeof(struct file_list));
531 /* Out of memory error */
535 fs->filename = strdup(filename);
536 fs->lastmodified = statbuf->st_mtime;
538 if (fs->filename == NULL)
540 /* Out of memory error */
545 #ifndef SPLIT_PROXY_ARGS
546 if (!suppress_blocklists)
548 char * p = html_encode(filename);
551 fs->proxy_args = strsav(fs->proxy_args, "<h2>The file `");
552 fs->proxy_args = strsav(fs->proxy_args, p);
553 fs->proxy_args = strsav(fs->proxy_args,
554 "' contains the following patterns</h2>\n");
557 fs->proxy_args = strsav(fs->proxy_args, "<pre>");
559 #endif /* ndef SPLIT_PROXY_ARGS */
567 /*********************************************************************
569 * Function : read_config_line
571 * Description : Read a single non-empty line from a file and return
572 * it. Trims comments, leading and trailing whitespace
573 * and respects escaping of newline and comment char.
574 * Also writes the file to fs->proxy_args.
577 * 1 : buf = Buffer to use.
578 * 2 : buflen = Size of buffer in bytes.
579 * 3 : fp = File to read from
580 * 4 : fs = File will be written to fs->proxy_args. May
581 * be NULL to disable this feature.
583 * Returns : NULL on EOF or error
584 * Otherwise, returns buf.
586 *********************************************************************/
587 char *read_config_line(char *buf, int buflen, FILE *fp, struct file_list *fs)
590 char linebuf[BUFFER_SIZE];
595 while (fgets(linebuf, sizeof(linebuf), fp))
597 #ifndef SPLIT_PROXY_ARGS
598 if (fs && !suppress_blocklists)
600 char *html_line = html_encode(linebuf);
601 if (html_line != NULL)
603 fs->proxy_args = strsav(fs->proxy_args, html_line);
606 fs->proxy_args = strsav(fs->proxy_args, "<br>");
608 #endif /* ndef SPLIT_PROXY_ARGS */
610 /* Trim off newline */
611 if ((p = strpbrk(linebuf, "\r\n")) != NULL)
617 p = linebuf + strlen(linebuf);
620 /* Line continuation? Trim escape and set flag. */
621 if ((p != linebuf) && (*--p == '\\'))
627 /* If there's a comment char.. */
628 if ((p = strpbrk(linebuf, "#")) != NULL)
630 /* ..and it's escaped, left-shift the line over the escape. */
631 if ((p != linebuf) && (*(p-1) == '\\'))
634 while ((*q++ = *p++) != '\0') /* nop */;
636 /* Else, chop off the rest of the line */
643 /* Write to the buffer */
646 strncat(buf, linebuf, buflen - strlen(buf));
656 /* Remove leading and trailing whitespace */
672 /*********************************************************************
674 * Function : unload_trustfile
676 * Description : Unloads a trustfile.
679 * 1 : f = the data structure associated with the trustfile.
683 *********************************************************************/
684 static void unload_trustfile(void *f)
686 struct block_spec *b = (struct block_spec *)f;
687 if (b == NULL) return;
689 unload_trustfile(b->next); /* Stack is cheap, isn't it? */
698 /*********************************************************************
700 * Function : load_trustfile
702 * Description : Read and parse a trustfile and add to files list.
705 * 1 : csp = Current client state (buffers, headers, etc...)
707 * Returns : 0 => Ok, everything else is an error.
709 *********************************************************************/
710 int load_trustfile(struct client_state *csp)
714 struct block_spec *b, *bl;
715 struct url_spec **tl;
717 char buf[BUFFER_SIZE], *p, *q;
719 struct file_list *fs;
721 if (!check_file_changed(current_trustfile, csp->config->trustfile, &fs))
723 /* No need to load */
726 csp->tlist = current_trustfile;
732 goto load_trustfile_error;
735 fs->f = bl = (struct block_spec *)zalloc(sizeof(*bl));
738 goto load_trustfile_error;
741 if ((fp = fopen(csp->config->trustfile, "r")) == NULL)
743 goto load_trustfile_error;
746 tl = csp->config->trust_list;
748 while (read_config_line(buf, sizeof(buf), fp, fs) != NULL)
764 while ((*p++ = *q++))
770 /* skip blank lines */
776 /* allocate a new node */
777 if ((b = zalloc(sizeof(*b))) == NULL)
780 goto load_trustfile_error;
783 /* add it to the list */
789 /* Save the URL pattern */
790 if (create_url_spec(b->url, buf))
793 goto load_trustfile_error;
797 * save a pointer to URL's spec in the list of trusted URL's, too
809 #ifndef SPLIT_PROXY_ARGS
810 if (!suppress_blocklists)
812 fs->proxy_args = strsav(fs->proxy_args, "</pre>");
814 #endif /* ndef SPLIT_PROXY_ARGS */
816 /* the old one is now obsolete */
817 if (current_trustfile)
819 current_trustfile->unloader = unload_trustfile;
822 fs->next = files->next;
824 current_trustfile = fs;
833 load_trustfile_error:
834 log_error(LOG_LEVEL_FATAL, "can't load trustfile '%s': %E",
835 csp->config->trustfile);
839 #endif /* def TRUST_FILES */
842 /*********************************************************************
844 * Function : unload_re_filterfile
846 * Description : Unload the re_filter list.
849 * 1 : f = the data structure associated with the filterfile.
853 *********************************************************************/
854 static void unload_re_filterfile(void *f)
856 struct re_filterfile_spec *b = (struct re_filterfile_spec *)f;
858 if (b == NULL) return;
860 destroy_list(b->patterns);
861 pcrs_free_joblist(b->joblist);
867 /*********************************************************************
869 * Function : load_re_filterfile
871 * Description : Load the re_filterfile. Each non-comment, non-empty
872 * line is instantly added to the joblist, which is
873 * a chained list of pcrs_job structs.
876 * 1 : csp = Current client state (buffers, headers, etc...)
878 * Returns : 0 => Ok, everything else is an error.
880 *********************************************************************/
881 int load_re_filterfile(struct client_state *csp)
885 struct re_filterfile_spec *bl;
886 struct file_list *fs;
888 char buf[BUFFER_SIZE];
892 if (!check_file_changed(current_re_filterfile, csp->config->re_filterfile, &fs))
894 /* No need to load */
897 csp->rlist = current_re_filterfile;
903 goto load_re_filterfile_error;
906 fs->f = bl = (struct re_filterfile_spec *)zalloc(sizeof(*bl));
909 goto load_re_filterfile_error;
912 /* Open the file or fail */
913 if ((fp = fopen(csp->config->re_filterfile, "r")) == NULL)
915 goto load_re_filterfile_error;
918 /* Read line by line */
919 while (read_config_line(buf, sizeof(buf), fp, fs) != NULL)
921 enlist( bl->patterns, buf );
923 /* We have a meaningful line -> make it a job */
924 if ((dummy = pcrs_compile_command(buf, &error)) == NULL)
926 log_error(LOG_LEVEL_RE_FILTER,
927 "Adding re_filter job %s failed with error %d.", buf, error);
932 dummy->next = bl->joblist;
934 log_error(LOG_LEVEL_RE_FILTER, "Adding re_filter job %s succeeded.", buf);
940 #ifndef SPLIT_PROXY_ARGS
941 if (!suppress_blocklists)
943 fs->proxy_args = strsav(fs->proxy_args, "</pre>");
945 #endif /* ndef SPLIT_PROXY_ARGS */
947 /* the old one is now obsolete */
948 if ( NULL != current_re_filterfile )
950 current_re_filterfile->unloader = unload_re_filterfile;
953 fs->next = files->next;
955 current_re_filterfile = fs;
964 load_re_filterfile_error:
965 log_error(LOG_LEVEL_FATAL, "can't load re_filterfile '%s': %E",
966 csp->config->re_filterfile);
972 /*********************************************************************
974 * Function : add_loader
976 * Description : Called from `load_config'. Called once for each input
977 * file found in config.
980 * 1 : loader = pointer to a function that can parse and load
981 * the appropriate config file.
982 * 2 : config = The configuration_spec to add the loader to.
986 *********************************************************************/
987 void add_loader(int (*loader)(struct client_state *),
988 struct configuration_spec * config)
992 for (i=0; i < NLOADERS; i++)
994 if (config->loaders[i] == NULL)
996 config->loaders[i] = loader;
1004 /*********************************************************************
1006 * Function : run_loader
1008 * Description : Called from `load_config' and `listen_loop'. This
1009 * function keeps the "csp" current with any file mods
1010 * since the last loop. If a file is unchanged, the
1011 * loader functions do NOT reload the file.
1014 * 1 : csp = Current client state (buffers, headers, etc...)
1015 * Must be non-null. Reads: "csp->config"
1016 * Writes: various data members.
1018 * Returns : 0 => Ok, everything else is an error.
1020 *********************************************************************/
1021 int run_loader(struct client_state *csp)
1026 for (i=0; i < NLOADERS; i++)
1028 if (csp->config->loaders[i] == NULL)
1032 ret |= (csp->config->loaders[i])(csp);