1 const char loaders_rcs[] = "$Id: loaders.c,v 1.22 2001/07/20 15:16:17 haroon 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.22 2001/07/20 15:16:17 haroon
39 * - per Guy's suggestion, added a while loop in sweep() to catch not just
40 * the last inactive CSP but all other consecutive inactive CSPs after that
43 * Revision 1.21 2001/07/18 17:26:24 oes
44 * Changed to conform to new pcrs interface
46 * Revision 1.20 2001/07/17 13:07:01 oes
47 * Fixed segv when last line in config files
48 * lacked a terminating (\r)\n
50 * Revision 1.19 2001/07/13 14:01:54 oes
51 * Removed all #ifdef PCRS
53 * Revision 1.18 2001/06/29 21:45:41 oes
54 * Indentation, CRLF->LF, Tab-> Space
56 * Revision 1.17 2001/06/29 13:31:51 oes
59 * Revision 1.16 2001/06/09 10:55:28 jongfoster
60 * Changing BUFSIZ ==> BUFFER_SIZE
62 * Revision 1.15 2001/06/07 23:14:14 jongfoster
63 * Removing ACL and forward file loaders - these
64 * files have been merged into the config file.
65 * Cosmetic: Moving unloader funcs next to their
66 * respective loader funcs
68 * Revision 1.14 2001/06/01 03:27:04 oes
69 * Fixed line continuation problem
71 * Revision 1.13 2001/05/31 21:28:49 jongfoster
72 * Removed all permissionsfile code - it's now called the actions
73 * file, and (almost) all the code is in actions.c
75 * Revision 1.12 2001/05/31 17:32:31 oes
77 * - Enhanced domain part globbing with infix and prefix asterisk
78 * matching and optional unanchored operation
80 * Revision 1.11 2001/05/29 23:25:24 oes
82 * - load_config_line() and load_permissions_file() now use chomp()
84 * Revision 1.10 2001/05/29 09:50:24 jongfoster
85 * Unified blocklist/imagelist/permissionslist.
86 * File format is still under discussion, but the internal changes
89 * Also modified interceptor behaviour:
90 * - We now intercept all URLs beginning with one of the following
91 * prefixes (and *only* these prefixes):
93 * * http://ijbswa.sf.net/config/
94 * * http://ijbswa.sourceforge.net/config/
95 * - New interceptors "home page" - go to http://i.j.b/ to see it.
96 * - Internal changes so that intercepted and fast redirect pages
97 * are not replaced with an image.
98 * - Interceptors now have the option to send a binary page direct
99 * to the client. (i.e. ijb-send-banner uses this)
100 * - Implemented show-url-info interceptor. (Which is why I needed
101 * the above interceptors changes - a typical URL is
102 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
103 * The previous mechanism would not have intercepted that, and
104 * if it had been intercepted then it then it would have replaced
107 * Revision 1.9 2001/05/26 17:12:07 jongfoster
108 * Fatal errors loading configuration files now give better error messages.
110 * Revision 1.8 2001/05/26 00:55:20 jongfoster
111 * Removing duplicated code. load_forwardfile() now uses create_url_spec()
113 * Revision 1.7 2001/05/26 00:28:36 jongfoster
114 * Automatic reloading of config file.
115 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
116 * Most of the global variables have been moved to a new
117 * struct configuration_spec, accessed through csp->config->globalname
118 * Most of the globals remaining are used by the Win32 GUI.
120 * Revision 1.6 2001/05/23 12:27:33 oes
122 * Fixed ugly indentation of my last changes
124 * Revision 1.5 2001/05/23 10:39:05 oes
125 * - Added support for escaping the comment character
126 * in config files by a backslash
127 * - Added support for line continuation in config
129 * - Fixed a buffer overflow bug with long config lines
131 * Revision 1.4 2001/05/22 18:56:28 oes
134 * Revision 1.3 2001/05/20 01:21:20 jongfoster
135 * Version 2.9.4 checkin.
136 * - Merged popupfile and cookiefile, and added control over PCRS
137 * filtering, in new "permissionsfile".
138 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
139 * file error you now get a message box (in the Win32 GUI) rather
140 * than the program exiting with no explanation.
141 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
143 * - Removed tabs from "config"
144 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
145 * - Bumped up version number.
147 * Revision 1.2 2001/05/17 23:01:01 oes
148 * - Cleaned CRLF's from the sources and related files
150 * Revision 1.1.1.1 2001/05/15 13:58:59 oes
151 * Initial import of version 2.9.3 source tree
154 *********************************************************************/
161 #include <sys/types.h>
165 #include <sys/stat.h>
180 #include "miscutil.h"
185 const char loaders_h_rcs[] = LOADERS_H_VERSION;
187 /* Fix a problem with Solaris. There should be no effect on other
189 * Solaris's isspace() is a macro which uses it's argument directly
190 * as an array index. Therefore we need to make sure that high-bit
191 * characters generate +ve values, and ideally we also want to make
192 * the argument match the declared parameter type of "int".
194 #define ijb_isspace(__X) isspace((int)(unsigned char)(__X))
198 * Currently active files.
199 * These are also entered in the main linked list of files.
203 static struct file_list *current_trustfile = NULL;
204 #endif /* def TRUST_FILES */
206 static struct file_list *current_re_filterfile = NULL;
210 /*********************************************************************
214 * Description : Basically a mark and sweep garbage collector, it is run
215 * (by the parent thread) every once in a while to reclaim memory.
217 * It uses a mark and sweep strategy:
218 * 1) mark all files as inactive
220 * 2) check with each client:
221 * if it is active, mark its files as active
222 * if it is inactive, free its resources
224 * 3) free the resources of all of the files that
225 * are still marked as inactive (and are obsolete).
227 * N.B. files that are not obsolete don't have an unloader defined.
233 *********************************************************************/
236 struct file_list *fl, *nfl;
237 struct client_state *csp, *ncsp;
239 /* clear all of the file's active flags */
240 for ( fl = files->next; NULL != fl; fl = fl->next )
245 for (csp = clients; csp && (ncsp = csp->next) ; csp = csp->next)
249 /* mark this client's files as active */
252 * Always have a configuration file.
253 * (Also note the slightly non-standard extra
256 ncsp->config->config_file_list->active = 1;
258 if (ncsp->actions_list) /* actions files */
260 ncsp->actions_list->active = 1;
263 if (ncsp->rlist) /* pcrsjob files */
265 ncsp->rlist->active = 1;
269 if (ncsp->tlist) /* trust files */
271 ncsp->tlist->active = 1;
273 #endif /* def TRUST_FILES */
278 * this client is not active, release its resources
279 * and the ones of all inactive clients that might
283 while( !ncsp->active )
285 csp->next = ncsp->next;
287 freez(ncsp->ip_addr_str);
288 freez(ncsp->my_ip_addr_str);
289 freez(ncsp->my_hostname);
292 freez(ncsp->referrer);
293 #endif /* def TRUST_FILES */
294 freez(ncsp->x_forwarded);
295 freez(ncsp->iob->buf);
297 free_http_request(ncsp->http);
299 destroy_list(ncsp->headers);
300 destroy_list(ncsp->cookie_list);
302 free_current_action(ncsp->action);
310 #endif /* def STATISTICS */
314 /* are there any more in sequence after it? */
315 if( !(ncsp = csp->next) )
321 for (fl = files; fl && (nfl = fl->next) ; fl = fl->next)
323 if ( ( 0 == nfl->active ) && ( NULL != nfl->unloader ) )
325 fl->next = nfl->next;
327 (nfl->unloader)(nfl->f);
329 #ifndef SPLIT_PROXY_ARGS
330 freez(nfl->proxy_args);
331 #endif /* ndef SPLIT_PROXY_ARGS */
333 freez(nfl->filename);
342 /*********************************************************************
344 * Function : create_url_spec
346 * Description : Creates a "url_spec" structure from a string.
347 * When finished, free with unload_url().
350 * 1 : url = Target url_spec to be filled in. Must be
351 * zeroed out before the call (e.g. using zalloc).
352 * 2 : buf = Source pattern, null terminated. NOTE: The
353 * contents of this buffer are destroyed by this
354 * function. If this function succeeds, the
355 * buffer is copied to url->spec. If this
356 * function fails, the contents of the buffer
359 * Returns : 0 => Ok, everything else is an error.
361 *********************************************************************/
362 int create_url_spec(struct url_spec * url, char * buf)
365 struct url_spec tmp_url[1];
367 /* paranoia - should never happen. */
368 if ((url == NULL) || (buf == NULL))
373 /* save a copy of the orignal specification */
374 if ((url->spec = strdup(buf)) == NULL)
379 if ((p = strchr(buf, '/')))
381 if (NULL == (url->path = strdup(p)))
386 url->pathlen = strlen(url->path);
398 char rebuf[BUFFER_SIZE];
400 if (NULL == (url->preg = zalloc(sizeof(*url->preg))))
407 sprintf(rebuf, "^(%s)", url->path);
409 errcode = regcomp(url->preg, rebuf,
410 (REG_EXTENDED|REG_NOSUB|REG_ICASE));
413 size_t errlen = regerror(errcode,
414 url->preg, buf, sizeof(buf));
418 log_error(LOG_LEVEL_ERROR, "error compiling %s: %s",
429 if ((p = strchr(buf, ':')) == NULL)
439 if ((url->domain = strdup(buf)) == NULL)
445 #endif /* def REGEX */
449 /* split domain into components */
451 *tmp_url = dsplit(url->domain);
452 url->dbuf = tmp_url->dbuf;
453 url->dcnt = tmp_url->dcnt;
454 url->dvec = tmp_url->dvec;
455 url->unanchored = tmp_url->unanchored;
462 /*********************************************************************
464 * Function : free_url
466 * Description : Called from the "unloaders". Freez the url
467 * structure elements.
470 * 1 : url = pointer to a url_spec structure.
474 *********************************************************************/
475 void free_url(struct url_spec *url)
477 if (url == NULL) return;
495 /*********************************************************************
497 * Function : check_file_changed
499 * Description : Helper function to check if a file needs reloading.
500 * If "current" is still current, return it. Otherwise
501 * allocates a new (zeroed) "struct file_list", fills
502 * in the disk file name and timestamp, and returns it.
505 * 1 : current = The file_list currently being used - will
506 * be checked to see if it is out of date.
507 * May be NULL (which is treated as out of
509 * 2 : filename = Name of file to check.
510 * 3 : newfl = New file list. [Output only]
511 * This will be set to NULL, OR a struct
512 * file_list newly allocated on the
513 * heap, with the filename and lastmodified
514 * fields filled, standard header giving file
515 * name in proxy_args, and all others zeroed.
516 * (proxy_args is only filled in if !defined
517 * SPLIT_PROXY_ARGS and !suppress_blocklists).
519 * Returns : If file unchanged: 0 (and sets newfl == NULL)
520 * If file changed: 1 and sets newfl != NULL
521 * On error: 1 and sets newfl == NULL
523 *********************************************************************/
524 int check_file_changed(const struct file_list * current,
525 const char * filename,
526 struct file_list ** newfl)
528 struct file_list *fs;
529 struct stat statbuf[1];
533 if (stat(filename, statbuf) < 0)
535 /* Error, probably file not found. */
540 && (current->lastmodified == statbuf->st_mtime)
541 && (0 == strcmp(current->filename, filename)))
546 fs = (struct file_list *)zalloc(sizeof(struct file_list));
550 /* Out of memory error */
554 fs->filename = strdup(filename);
555 fs->lastmodified = statbuf->st_mtime;
557 if (fs->filename == NULL)
559 /* Out of memory error */
564 #ifndef SPLIT_PROXY_ARGS
565 if (!suppress_blocklists)
567 char * p = html_encode(filename);
570 fs->proxy_args = strsav(fs->proxy_args, "<h2>The file `");
571 fs->proxy_args = strsav(fs->proxy_args, p);
572 fs->proxy_args = strsav(fs->proxy_args,
573 "' contains the following patterns</h2>\n");
576 fs->proxy_args = strsav(fs->proxy_args, "<pre>");
578 #endif /* ndef SPLIT_PROXY_ARGS */
586 /*********************************************************************
588 * Function : read_config_line
590 * Description : Read a single non-empty line from a file and return
591 * it. Trims comments, leading and trailing whitespace
592 * and respects escaping of newline and comment char.
593 * Also writes the file to fs->proxy_args.
596 * 1 : buf = Buffer to use.
597 * 2 : buflen = Size of buffer in bytes.
598 * 3 : fp = File to read from
599 * 4 : fs = File will be written to fs->proxy_args. May
600 * be NULL to disable this feature.
602 * Returns : NULL on EOF or error
603 * Otherwise, returns buf.
605 *********************************************************************/
606 char *read_config_line(char *buf, int buflen, FILE *fp, struct file_list *fs)
609 char linebuf[BUFFER_SIZE];
614 while (fgets(linebuf, sizeof(linebuf), fp))
616 #ifndef SPLIT_PROXY_ARGS
617 if (fs && !suppress_blocklists)
619 char *html_line = html_encode(linebuf);
620 if (html_line != NULL)
622 fs->proxy_args = strsav(fs->proxy_args, html_line);
625 fs->proxy_args = strsav(fs->proxy_args, "<br>");
627 #endif /* ndef SPLIT_PROXY_ARGS */
629 /* Trim off newline */
630 if ((p = strpbrk(linebuf, "\r\n")) != NULL)
636 p = linebuf + strlen(linebuf);
639 /* Line continuation? Trim escape and set flag. */
640 if ((p != linebuf) && (*--p == '\\'))
646 /* If there's a comment char.. */
647 if ((p = strpbrk(linebuf, "#")) != NULL)
649 /* ..and it's escaped, left-shift the line over the escape. */
650 if ((p != linebuf) && (*(p-1) == '\\'))
653 while ((*q++ = *p++) != '\0') /* nop */;
655 /* Else, chop off the rest of the line */
662 /* Write to the buffer */
665 strncat(buf, linebuf, buflen - strlen(buf));
675 /* Remove leading and trailing whitespace */
691 /*********************************************************************
693 * Function : unload_trustfile
695 * Description : Unloads a trustfile.
698 * 1 : f = the data structure associated with the trustfile.
702 *********************************************************************/
703 static void unload_trustfile(void *f)
705 struct block_spec *b = (struct block_spec *)f;
706 if (b == NULL) return;
708 unload_trustfile(b->next); /* Stack is cheap, isn't it? */
717 /*********************************************************************
719 * Function : load_trustfile
721 * Description : Read and parse a trustfile and add to files list.
724 * 1 : csp = Current client state (buffers, headers, etc...)
726 * Returns : 0 => Ok, everything else is an error.
728 *********************************************************************/
729 int load_trustfile(struct client_state *csp)
733 struct block_spec *b, *bl;
734 struct url_spec **tl;
736 char buf[BUFFER_SIZE], *p, *q;
738 struct file_list *fs;
740 if (!check_file_changed(current_trustfile, csp->config->trustfile, &fs))
742 /* No need to load */
745 csp->tlist = current_trustfile;
751 goto load_trustfile_error;
754 fs->f = bl = (struct block_spec *)zalloc(sizeof(*bl));
757 goto load_trustfile_error;
760 if ((fp = fopen(csp->config->trustfile, "r")) == NULL)
762 goto load_trustfile_error;
765 tl = csp->config->trust_list;
767 while (read_config_line(buf, sizeof(buf), fp, fs) != NULL)
783 while ((*p++ = *q++))
789 /* skip blank lines */
795 /* allocate a new node */
796 if ((b = zalloc(sizeof(*b))) == NULL)
799 goto load_trustfile_error;
802 /* add it to the list */
808 /* Save the URL pattern */
809 if (create_url_spec(b->url, buf))
812 goto load_trustfile_error;
816 * save a pointer to URL's spec in the list of trusted URL's, too
828 #ifndef SPLIT_PROXY_ARGS
829 if (!suppress_blocklists)
831 fs->proxy_args = strsav(fs->proxy_args, "</pre>");
833 #endif /* ndef SPLIT_PROXY_ARGS */
835 /* the old one is now obsolete */
836 if (current_trustfile)
838 current_trustfile->unloader = unload_trustfile;
841 fs->next = files->next;
843 current_trustfile = fs;
852 load_trustfile_error:
853 log_error(LOG_LEVEL_FATAL, "can't load trustfile '%s': %E",
854 csp->config->trustfile);
858 #endif /* def TRUST_FILES */
861 /*********************************************************************
863 * Function : unload_re_filterfile
865 * Description : Unload the re_filter list.
868 * 1 : f = the data structure associated with the filterfile.
872 *********************************************************************/
873 static void unload_re_filterfile(void *f)
875 struct re_filterfile_spec *b = (struct re_filterfile_spec *)f;
877 if (b == NULL) return;
879 destroy_list(b->patterns);
880 pcrs_free_joblist(b->joblist);
886 /*********************************************************************
888 * Function : load_re_filterfile
890 * Description : Load the re_filterfile. Each non-comment, non-empty
891 * line is instantly added to the joblist, which is
892 * a chained list of pcrs_job structs.
895 * 1 : csp = Current client state (buffers, headers, etc...)
897 * Returns : 0 => Ok, everything else is an error.
899 *********************************************************************/
900 int load_re_filterfile(struct client_state *csp)
904 struct re_filterfile_spec *bl;
905 struct file_list *fs;
907 char buf[BUFFER_SIZE];
911 if (!check_file_changed(current_re_filterfile, csp->config->re_filterfile, &fs))
913 /* No need to load */
916 csp->rlist = current_re_filterfile;
922 goto load_re_filterfile_error;
925 fs->f = bl = (struct re_filterfile_spec *)zalloc(sizeof(*bl));
928 goto load_re_filterfile_error;
931 /* Open the file or fail */
932 if ((fp = fopen(csp->config->re_filterfile, "r")) == NULL)
934 goto load_re_filterfile_error;
937 /* Read line by line */
938 while (read_config_line(buf, sizeof(buf), fp, fs) != NULL)
940 enlist( bl->patterns, buf );
942 /* We have a meaningful line -> make it a job */
943 if ((dummy = pcrs_compile_command(buf, &error)) == NULL)
945 log_error(LOG_LEVEL_RE_FILTER,
946 "Adding re_filter job %s failed with error %d.", buf, error);
951 dummy->next = bl->joblist;
953 log_error(LOG_LEVEL_RE_FILTER, "Adding re_filter job %s succeeded.", buf);
959 #ifndef SPLIT_PROXY_ARGS
960 if (!suppress_blocklists)
962 fs->proxy_args = strsav(fs->proxy_args, "</pre>");
964 #endif /* ndef SPLIT_PROXY_ARGS */
966 /* the old one is now obsolete */
967 if ( NULL != current_re_filterfile )
969 current_re_filterfile->unloader = unload_re_filterfile;
972 fs->next = files->next;
974 current_re_filterfile = fs;
983 load_re_filterfile_error:
984 log_error(LOG_LEVEL_FATAL, "can't load re_filterfile '%s': %E",
985 csp->config->re_filterfile);
991 /*********************************************************************
993 * Function : add_loader
995 * Description : Called from `load_config'. Called once for each input
996 * file found in config.
999 * 1 : loader = pointer to a function that can parse and load
1000 * the appropriate config file.
1001 * 2 : config = The configuration_spec to add the loader to.
1005 *********************************************************************/
1006 void add_loader(int (*loader)(struct client_state *),
1007 struct configuration_spec * config)
1011 for (i=0; i < NLOADERS; i++)
1013 if (config->loaders[i] == NULL)
1015 config->loaders[i] = loader;
1023 /*********************************************************************
1025 * Function : run_loader
1027 * Description : Called from `load_config' and `listen_loop'. This
1028 * function keeps the "csp" current with any file mods
1029 * since the last loop. If a file is unchanged, the
1030 * loader functions do NOT reload the file.
1033 * 1 : csp = Current client state (buffers, headers, etc...)
1034 * Must be non-null. Reads: "csp->config"
1035 * Writes: various data members.
1037 * Returns : 0 => Ok, everything else is an error.
1039 *********************************************************************/
1040 int run_loader(struct client_state *csp)
1045 for (i=0; i < NLOADERS; i++)
1047 if (csp->config->loaders[i] == NULL)
1051 ret |= (csp->config->loaders[i])(csp);