1 const char loaders_rcs[] = "$Id: loaders.c,v 1.16 2001/06/09 10:55:28 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.16 2001/06/09 10:55:28 jongfoster
39 * Changing BUFSIZ ==> BUFFER_SIZE
41 * Revision 1.15 2001/06/07 23:14:14 jongfoster
42 * Removing ACL and forward file loaders - these
43 * files have been merged into the config file.
44 * Cosmetic: Moving unloader funcs next to their
45 * respective loader funcs
47 * Revision 1.14 2001/06/01 03:27:04 oes
48 * Fixed line continuation problem
50 * Revision 1.13 2001/05/31 21:28:49 jongfoster
51 * Removed all permissionsfile code - it's now called the actions
52 * file, and (almost) all the code is in actions.c
54 * Revision 1.12 2001/05/31 17:32:31 oes
56 * - Enhanced domain part globbing with infix and prefix asterisk
57 * matching and optional unanchored operation
59 * Revision 1.11 2001/05/29 23:25:24 oes
61 * - load_config_line() and load_permissions_file() now use chomp()
63 * Revision 1.10 2001/05/29 09:50:24 jongfoster
64 * Unified blocklist/imagelist/permissionslist.
65 * File format is still under discussion, but the internal changes
68 * Also modified interceptor behaviour:
69 * - We now intercept all URLs beginning with one of the following
70 * prefixes (and *only* these prefixes):
72 * * http://ijbswa.sf.net/config/
73 * * http://ijbswa.sourceforge.net/config/
74 * - New interceptors "home page" - go to http://i.j.b/ to see it.
75 * - Internal changes so that intercepted and fast redirect pages
76 * are not replaced with an image.
77 * - Interceptors now have the option to send a binary page direct
78 * to the client. (i.e. ijb-send-banner uses this)
79 * - Implemented show-url-info interceptor. (Which is why I needed
80 * the above interceptors changes - a typical URL is
81 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
82 * The previous mechanism would not have intercepted that, and
83 * if it had been intercepted then it then it would have replaced
86 * Revision 1.9 2001/05/26 17:12:07 jongfoster
87 * Fatal errors loading configuration files now give better error messages.
89 * Revision 1.8 2001/05/26 00:55:20 jongfoster
90 * Removing duplicated code. load_forwardfile() now uses create_url_spec()
92 * Revision 1.7 2001/05/26 00:28:36 jongfoster
93 * Automatic reloading of config file.
94 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
95 * Most of the global variables have been moved to a new
96 * struct configuration_spec, accessed through csp->config->globalname
97 * Most of the globals remaining are used by the Win32 GUI.
99 * Revision 1.6 2001/05/23 12:27:33 oes
101 * Fixed ugly indentation of my last changes
103 * Revision 1.5 2001/05/23 10:39:05 oes
104 * - Added support for escaping the comment character
105 * in config files by a backslash
106 * - Added support for line continuation in config
108 * - Fixed a buffer overflow bug with long config lines
110 * Revision 1.4 2001/05/22 18:56:28 oes
113 * Revision 1.3 2001/05/20 01:21:20 jongfoster
114 * Version 2.9.4 checkin.
115 * - Merged popupfile and cookiefile, and added control over PCRS
116 * filtering, in new "permissionsfile".
117 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
118 * file error you now get a message box (in the Win32 GUI) rather
119 * than the program exiting with no explanation.
120 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
122 * - Removed tabs from "config"
123 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
124 * - Bumped up version number.
126 * Revision 1.2 2001/05/17 23:01:01 oes
127 * - Cleaned CRLF's from the sources and related files
129 * Revision 1.1.1.1 2001/05/15 13:58:59 oes
130 * Initial import of version 2.9.3 source tree
133 *********************************************************************/
140 #include <sys/types.h>
144 #include <sys/stat.h>
159 #include "miscutil.h"
164 #ifndef SPLIT_PROXY_ARGS
166 #include "showargs.h"
167 #endif /* ndef SPLIT_PROXY_ARGS */
169 const char loaders_h_rcs[] = LOADERS_H_VERSION;
171 /* Fix a problem with Solaris. There should be no effect on other
173 * Solaris's isspace() is a macro which uses it's argument directly
174 * as an array index. Therefore we need to make sure that high-bit
175 * characters generate +ve values, and ideally we also want to make
176 * the argument match the declared parameter type of "int".
178 #define ijb_isspace(__X) isspace((int)(unsigned char)(__X))
182 * Currently active files.
183 * These are also entered in the main linked list of files.
187 static struct file_list *current_trustfile = NULL;
188 #endif /* def TRUST_FILES */
191 static struct file_list *current_re_filterfile = NULL;
192 #endif /* def PCRS */
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;
249 if (ncsp->rlist) /* perl re files */
251 ncsp->rlist->active = 1;
253 #endif /* def PCRS */
256 if (ncsp->tlist) /* trust files */
258 ncsp->tlist->active = 1;
260 #endif /* def TRUST_FILES */
265 /* this client one is not active, release its resources */
266 csp->next = ncsp->next;
268 freez(ncsp->ip_addr_str);
269 freez(ncsp->my_ip_addr_str);
270 freez(ncsp->my_hostname);
273 freez(ncsp->referrer);
274 #endif /* def TRUST_FILES */
275 freez(ncsp->x_forwarded);
276 freez(ncsp->iob->buf);
278 free_http_request(ncsp->http);
280 destroy_list(ncsp->headers);
281 destroy_list(ncsp->cookie_list);
283 free_current_action(ncsp->action);
291 #endif /* def STATISTICS */
297 for (fl = files; fl && (nfl = fl->next) ; fl = fl->next)
299 if ( ( 0 == nfl->active ) && ( NULL != nfl->unloader ) )
301 fl->next = nfl->next;
303 (nfl->unloader)(nfl->f);
305 #ifndef SPLIT_PROXY_ARGS
306 freez(nfl->proxy_args);
307 #endif /* ndef SPLIT_PROXY_ARGS */
309 freez(nfl->filename);
318 /*********************************************************************
320 * Function : create_url_spec
322 * Description : Creates a "url_spec" structure from a string.
323 * When finished, free with unload_url().
326 * 1 : url = Target url_spec to be filled in. Must be
327 * zeroed out before the call (e.g. using zalloc).
328 * 2 : buf = Source pattern, null terminated. NOTE: The
329 * contents of this buffer are destroyed by this
330 * function. If this function succeeds, the
331 * buffer is copied to url->spec. If this
332 * function fails, the contents of the buffer
335 * Returns : 0 => Ok, everything else is an error.
337 *********************************************************************/
338 int create_url_spec(struct url_spec * url, char * buf)
341 struct url_spec tmp_url[1];
343 /* paranoia - should never happen. */
344 if ((url == NULL) || (buf == NULL))
349 /* save a copy of the orignal specification */
350 if ((url->spec = strdup(buf)) == NULL)
355 if ((p = strchr(buf, '/')))
357 if (NULL == (url->path = strdup(p)))
362 url->pathlen = strlen(url->path);
374 char rebuf[BUFFER_SIZE];
376 if (NULL == (url->preg = zalloc(sizeof(*url->preg))))
383 sprintf(rebuf, "^(%s)", url->path);
385 errcode = regcomp(url->preg, rebuf,
386 (REG_EXTENDED|REG_NOSUB|REG_ICASE));
391 url->preg, buf, sizeof(buf));
395 log_error(LOG_LEVEL_ERROR, "error compiling %s: %s",
406 if ((p = strchr(buf, ':')) == NULL)
416 if ((url->domain = strdup(buf)) == NULL)
422 #endif /* def REGEX */
426 /* split domain into components */
428 *tmp_url = dsplit(url->domain);
429 url->dbuf = tmp_url->dbuf;
430 url->dcnt = tmp_url->dcnt;
431 url->dvec = tmp_url->dvec;
432 url->unanchored = tmp_url->unanchored;
438 /*********************************************************************
440 * Function : free_url
442 * Description : Called from the "unloaders". Freez the url
443 * structure elements.
446 * 1 : url = pointer to a url_spec structure.
450 *********************************************************************/
451 void free_url(struct url_spec *url)
453 if (url == NULL) return;
471 /*********************************************************************
473 * Function : check_file_changed
475 * Description : Helper function to check if a file needs reloading.
476 * If "current" is still current, return it. Otherwise
477 * allocates a new (zeroed) "struct file_list", fills
478 * in the disk file name and timestamp, and returns it.
481 * 1 : current = The file_list currently being used - will
482 * be checked to see if it is out of date.
483 * May be NULL (which is treated as out of
485 * 2 : filename = Name of file to check.
486 * 3 : newfl = New file list. [Output only]
487 * This will be set to NULL, OR a struct
488 * file_list newly allocated on the
489 * heap, with the filename and lastmodified
490 * fields filled, standard header giving file
491 * name in proxy_args, and all others zeroed.
492 * (proxy_args is only filled in if !defined
493 * SPLIT_PROXY_ARGS and !suppress_blocklists).
495 * Returns : If file unchanged: 0 (and sets newfl == NULL)
496 * If file changed: 1 and sets newfl != NULL
497 * On error: 1 and sets newfl == NULL
499 *********************************************************************/
500 int check_file_changed(const struct file_list * current,
501 const char * filename,
502 struct file_list ** newfl)
504 struct file_list *fs;
505 struct stat statbuf[1];
509 if (stat(filename, statbuf) < 0)
511 /* Error, probably file not found. */
516 && (current->lastmodified == statbuf->st_mtime)
517 && (0 == strcmp(current->filename, filename)))
522 fs = (struct file_list *)zalloc(sizeof(struct file_list));
526 /* Out of memory error */
530 fs->filename = strdup(filename);
531 fs->lastmodified = statbuf->st_mtime;
533 if (fs->filename == NULL)
535 /* Out of memory error */
540 #ifndef SPLIT_PROXY_ARGS
541 if (!suppress_blocklists)
543 char * p = html_encode(filename);
546 fs->proxy_args = strsav(fs->proxy_args, "<h2>The file `");
547 fs->proxy_args = strsav(fs->proxy_args, p);
548 fs->proxy_args = strsav(fs->proxy_args,
549 "' contains the following patterns</h2>\n");
552 fs->proxy_args = strsav(fs->proxy_args, "<pre>");
554 #endif /* ndef SPLIT_PROXY_ARGS */
561 /*********************************************************************
563 * Function : read_config_line
565 * Description : Read a single non-empty line from a file and return
566 * it. Trims comments, leading and trailing whitespace
567 * and respects escaping of newline and comment char.
568 * Also writes the file to fs->proxy_args.
571 * 1 : buf = Buffer to use.
572 * 2 : buflen = Size of buffer in bytes.
573 * 3 : fp = File to read from
574 * 4 : fs = File will be written to fs->proxy_args. May
575 * be NULL to disable this feature.
577 * Returns : NULL on EOF or error
578 * Otherwise, returns buf.
580 *********************************************************************/
581 char *read_config_line(char *buf, int buflen, FILE *fp, struct file_list *fs)
584 char linebuf[BUFFER_SIZE];
589 while (fgets(linebuf, sizeof(linebuf), fp))
591 #ifndef SPLIT_PROXY_ARGS
592 if (fs && !suppress_blocklists)
594 char *html_line = html_encode(linebuf);
595 if (html_line != NULL)
597 fs->proxy_args = strsav(fs->proxy_args, html_line);
600 fs->proxy_args = strsav(fs->proxy_args, "<br>");
602 #endif /* ndef SPLIT_PROXY_ARGS */
604 /* Trim off newline */
605 if ((p = strpbrk(linebuf, "\r\n")) != NULL)
610 /* Line continuation? Trim escape and set flag. */
611 if ((p != linebuf) && (*--p == '\\'))
617 /* If there's a comment char.. */
618 if ((p = strpbrk(linebuf, "#")) != NULL)
620 /* ..and it's escaped, left-shift the line over the escape. */
621 if ((p != linebuf) && (*(p-1) == '\\'))
624 while ((*q++ = *p++) != '\0') /* nop */;
626 /* Else, chop off the rest of the line */
633 /* Write to the buffer */
636 strncat(buf, linebuf, buflen - strlen(buf));
646 /* Remove leading and trailing whitespace */
662 /*********************************************************************
664 * Function : unload_trustfile
666 * Description : Unloads a trustfile.
669 * 1 : f = the data structure associated with the trustfile.
673 *********************************************************************/
674 static void unload_trustfile(void *f)
676 struct block_spec *b = (struct block_spec *)f;
677 if (b == NULL) return;
679 unload_trustfile(b->next);
688 /*********************************************************************
690 * Function : load_trustfile
692 * Description : Read and parse a trustfile and add to files list.
695 * 1 : csp = Current client state (buffers, headers, etc...)
697 * Returns : 0 => Ok, everything else is an error.
699 *********************************************************************/
700 int load_trustfile(struct client_state *csp)
704 struct block_spec *b, *bl;
705 struct url_spec **tl;
707 char buf[BUFFER_SIZE], *p, *q;
709 struct file_list *fs;
711 if (!check_file_changed(current_trustfile, csp->config->trustfile, &fs))
713 /* No need to load */
716 csp->tlist = current_trustfile;
722 goto load_trustfile_error;
725 fs->f = bl = (struct block_spec *)zalloc(sizeof(*bl));
728 goto load_trustfile_error;
731 if ((fp = fopen(csp->config->trustfile, "r")) == NULL)
733 goto load_trustfile_error;
736 tl = csp->config->trust_list;
738 while (read_config_line(buf, sizeof(buf), fp, fs) != NULL)
754 while ((*p++ = *q++))
760 /* skip blank lines */
766 /* allocate a new node */
767 if ((b = zalloc(sizeof(*b))) == NULL)
770 goto load_trustfile_error;
773 /* add it to the list */
779 /* Save the URL pattern */
780 if (create_url_spec(b->url, buf))
783 goto load_trustfile_error;
787 * save a pointer to URL's spec in the list of trusted URL's, too
799 #ifndef SPLIT_PROXY_ARGS
800 if (!suppress_blocklists)
802 fs->proxy_args = strsav(fs->proxy_args, "</pre>");
804 #endif /* ndef SPLIT_PROXY_ARGS */
806 /* the old one is now obsolete */
807 if (current_trustfile)
809 current_trustfile->unloader = unload_trustfile;
812 fs->next = files->next;
814 current_trustfile = fs;
823 load_trustfile_error:
824 log_error(LOG_LEVEL_FATAL, "can't load trustfile '%s': %E",
825 csp->config->trustfile);
829 #endif /* def TRUST_FILES */
833 /*********************************************************************
835 * Function : unload_re_filterfile
837 * Description : Unload the re_filter list.
840 * 1 : f = the data structure associated with the filterfile.
844 *********************************************************************/
845 static void unload_re_filterfile(void *f)
847 struct re_filterfile_spec *b = (struct re_filterfile_spec *)f;
849 if (b == NULL) return;
851 destroy_list(b->patterns);
853 pcrs_free_joblist(b->joblist);
859 /*********************************************************************
861 * Function : load_re_filterfile
863 * Description : Load the re_filterfile. Each non-comment, non-empty
864 * line is instantly added to the joblist, which is
865 * a chained list of pcrs_job structs.
868 * 1 : csp = Current client state (buffers, headers, etc...)
870 * Returns : 0 => Ok, everything else is an error.
872 *********************************************************************/
873 int load_re_filterfile(struct client_state *csp)
877 struct re_filterfile_spec *bl;
878 struct file_list *fs;
880 char buf[BUFFER_SIZE];
884 if (!check_file_changed(current_re_filterfile, csp->config->re_filterfile, &fs))
886 /* No need to load */
889 csp->rlist = current_re_filterfile;
895 goto load_re_filterfile_error;
898 fs->f = bl = (struct re_filterfile_spec *)zalloc(sizeof(*bl));
901 goto load_re_filterfile_error;
904 /* Open the file or fail */
905 if ((fp = fopen(csp->config->re_filterfile, "r")) == NULL)
907 goto load_re_filterfile_error;
910 /* Read line by line */
911 while (read_config_line(buf, sizeof(buf), fp, fs) != NULL)
913 enlist( bl->patterns, buf );
915 /* We have a meaningful line -> make it a job */
916 if ((dummy = pcrs_compile(buf, &error)) == NULL)
918 log_error(LOG_LEVEL_RE_FILTER,
919 "Adding re_filter job %s failed with error %d.", buf, error);
924 dummy->next = bl->joblist;
926 log_error(LOG_LEVEL_RE_FILTER, "Adding re_filter job %s succeeded.", buf);
932 #ifndef SPLIT_PROXY_ARGS
933 if (!suppress_blocklists)
935 fs->proxy_args = strsav(fs->proxy_args, "</pre>");
937 #endif /* ndef SPLIT_PROXY_ARGS */
939 /* the old one is now obsolete */
940 if ( NULL != current_re_filterfile )
942 current_re_filterfile->unloader = unload_re_filterfile;
945 fs->next = files->next;
947 current_re_filterfile = fs;
956 load_re_filterfile_error:
957 log_error(LOG_LEVEL_FATAL, "can't load re_filterfile '%s': %E",
958 csp->config->re_filterfile);
962 #endif /* def PCRS */
965 /*********************************************************************
967 * Function : add_loader
969 * Description : Called from `load_config'. Called once for each input
970 * file found in config.
973 * 1 : loader = pointer to a function that can parse and load
974 * the appropriate config file.
975 * 2 : config = The configuration_spec to add the loader to.
979 *********************************************************************/
980 void add_loader(int (*loader)(struct client_state *),
981 struct configuration_spec * config)
985 for (i=0; i < NLOADERS; i++)
987 if (config->loaders[i] == NULL)
989 config->loaders[i] = loader;
997 /*********************************************************************
999 * Function : run_loader
1001 * Description : Called from `load_config' and `listen_loop'. This
1002 * function keeps the "csp" current with any file mods
1003 * since the last loop. If a file is unchanged, the
1004 * loader functions do NOT reload the file.
1007 * 1 : csp = Current client state (buffers, headers, etc...)
1008 * Must be non-null. Reads: "csp->config"
1009 * Writes: various data members.
1011 * Returns : 0 => Ok, everything else is an error.
1013 *********************************************************************/
1014 int run_loader(struct client_state *csp)
1019 for (i=0; i < NLOADERS; i++)
1021 if (csp->config->loaders[i] == NULL)
1025 ret |= (csp->config->loaders[i])(csp);