1 const char loaders_rcs[] = "$Id: loaders.c,v 1.21 2001/07/18 17:26:24 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.21 2001/07/18 17:26:24 oes
39 * Changed to conform to new pcrs interface
41 * Revision 1.20 2001/07/17 13:07:01 oes
42 * Fixed segv when last line in config files
43 * lacked a terminating (\r)\n
45 * Revision 1.19 2001/07/13 14:01:54 oes
46 * Removed all #ifdef PCRS
48 * Revision 1.18 2001/06/29 21:45:41 oes
49 * Indentation, CRLF->LF, Tab-> Space
51 * Revision 1.17 2001/06/29 13:31:51 oes
54 * Revision 1.16 2001/06/09 10:55:28 jongfoster
55 * Changing BUFSIZ ==> BUFFER_SIZE
57 * Revision 1.15 2001/06/07 23:14:14 jongfoster
58 * Removing ACL and forward file loaders - these
59 * files have been merged into the config file.
60 * Cosmetic: Moving unloader funcs next to their
61 * respective loader funcs
63 * Revision 1.14 2001/06/01 03:27:04 oes
64 * Fixed line continuation problem
66 * Revision 1.13 2001/05/31 21:28:49 jongfoster
67 * Removed all permissionsfile code - it's now called the actions
68 * file, and (almost) all the code is in actions.c
70 * Revision 1.12 2001/05/31 17:32:31 oes
72 * - Enhanced domain part globbing with infix and prefix asterisk
73 * matching and optional unanchored operation
75 * Revision 1.11 2001/05/29 23:25:24 oes
77 * - load_config_line() and load_permissions_file() now use chomp()
79 * Revision 1.10 2001/05/29 09:50:24 jongfoster
80 * Unified blocklist/imagelist/permissionslist.
81 * File format is still under discussion, but the internal changes
84 * Also modified interceptor behaviour:
85 * - We now intercept all URLs beginning with one of the following
86 * prefixes (and *only* these prefixes):
88 * * http://ijbswa.sf.net/config/
89 * * http://ijbswa.sourceforge.net/config/
90 * - New interceptors "home page" - go to http://i.j.b/ to see it.
91 * - Internal changes so that intercepted and fast redirect pages
92 * are not replaced with an image.
93 * - Interceptors now have the option to send a binary page direct
94 * to the client. (i.e. ijb-send-banner uses this)
95 * - Implemented show-url-info interceptor. (Which is why I needed
96 * the above interceptors changes - a typical URL is
97 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
98 * The previous mechanism would not have intercepted that, and
99 * if it had been intercepted then it then it would have replaced
102 * Revision 1.9 2001/05/26 17:12:07 jongfoster
103 * Fatal errors loading configuration files now give better error messages.
105 * Revision 1.8 2001/05/26 00:55:20 jongfoster
106 * Removing duplicated code. load_forwardfile() now uses create_url_spec()
108 * Revision 1.7 2001/05/26 00:28:36 jongfoster
109 * Automatic reloading of config file.
110 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
111 * Most of the global variables have been moved to a new
112 * struct configuration_spec, accessed through csp->config->globalname
113 * Most of the globals remaining are used by the Win32 GUI.
115 * Revision 1.6 2001/05/23 12:27:33 oes
117 * Fixed ugly indentation of my last changes
119 * Revision 1.5 2001/05/23 10:39:05 oes
120 * - Added support for escaping the comment character
121 * in config files by a backslash
122 * - Added support for line continuation in config
124 * - Fixed a buffer overflow bug with long config lines
126 * Revision 1.4 2001/05/22 18:56:28 oes
129 * Revision 1.3 2001/05/20 01:21:20 jongfoster
130 * Version 2.9.4 checkin.
131 * - Merged popupfile and cookiefile, and added control over PCRS
132 * filtering, in new "permissionsfile".
133 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
134 * file error you now get a message box (in the Win32 GUI) rather
135 * than the program exiting with no explanation.
136 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
138 * - Removed tabs from "config"
139 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
140 * - Bumped up version number.
142 * Revision 1.2 2001/05/17 23:01:01 oes
143 * - Cleaned CRLF's from the sources and related files
145 * Revision 1.1.1.1 2001/05/15 13:58:59 oes
146 * Initial import of version 2.9.3 source tree
149 *********************************************************************/
156 #include <sys/types.h>
160 #include <sys/stat.h>
175 #include "miscutil.h"
180 const char loaders_h_rcs[] = LOADERS_H_VERSION;
182 /* Fix a problem with Solaris. There should be no effect on other
184 * Solaris's isspace() is a macro which uses it's argument directly
185 * as an array index. Therefore we need to make sure that high-bit
186 * characters generate +ve values, and ideally we also want to make
187 * the argument match the declared parameter type of "int".
189 #define ijb_isspace(__X) isspace((int)(unsigned char)(__X))
193 * Currently active files.
194 * These are also entered in the main linked list of files.
198 static struct file_list *current_trustfile = NULL;
199 #endif /* def TRUST_FILES */
201 static struct file_list *current_re_filterfile = NULL;
205 /*********************************************************************
209 * Description : Basically a mark and sweep garbage collector, it is run
210 * (by the parent thread) every once in a while to reclaim memory.
212 * It uses a mark and sweep strategy:
213 * 1) mark all files as inactive
215 * 2) check with each client:
216 * if it is active, mark its files as active
217 * if it is inactive, free its resources
219 * 3) free the resources of all of the files that
220 * are still marked as inactive (and are obsolete).
222 * N.B. files that are not obsolete don't have an unloader defined.
228 *********************************************************************/
231 struct file_list *fl, *nfl;
232 struct client_state *csp, *ncsp;
234 /* clear all of the file's active flags */
235 for ( fl = files->next; NULL != fl; fl = fl->next )
240 for (csp = clients; csp && (ncsp = csp->next) ; csp = csp->next)
244 /* mark this client's files as active */
247 * Always have a configuration file.
248 * (Also note the slightly non-standard extra
251 ncsp->config->config_file_list->active = 1;
253 if (ncsp->actions_list) /* actions files */
255 ncsp->actions_list->active = 1;
258 if (ncsp->rlist) /* pcrsjob files */
260 ncsp->rlist->active = 1;
264 if (ncsp->tlist) /* trust files */
266 ncsp->tlist->active = 1;
268 #endif /* def TRUST_FILES */
272 /* this client is not active, release its resources */
274 while( !ncsp->active )
276 csp->next = ncsp->next;
278 freez(ncsp->ip_addr_str);
279 freez(ncsp->my_ip_addr_str);
280 freez(ncsp->my_hostname);
283 freez(ncsp->referrer);
284 #endif /* def TRUST_FILES */
285 freez(ncsp->x_forwarded);
286 freez(ncsp->iob->buf);
288 free_http_request(ncsp->http);
290 destroy_list(ncsp->headers);
291 destroy_list(ncsp->cookie_list);
293 free_current_action(ncsp->action);
301 #endif /* def STATISTICS */
305 /* are there any more in sequence after it? */
306 if( !(ncsp = csp->next) )
312 for (fl = files; fl && (nfl = fl->next) ; fl = fl->next)
314 if ( ( 0 == nfl->active ) && ( NULL != nfl->unloader ) )
316 fl->next = nfl->next;
318 (nfl->unloader)(nfl->f);
320 #ifndef SPLIT_PROXY_ARGS
321 freez(nfl->proxy_args);
322 #endif /* ndef SPLIT_PROXY_ARGS */
324 freez(nfl->filename);
333 /*********************************************************************
335 * Function : create_url_spec
337 * Description : Creates a "url_spec" structure from a string.
338 * When finished, free with unload_url().
341 * 1 : url = Target url_spec to be filled in. Must be
342 * zeroed out before the call (e.g. using zalloc).
343 * 2 : buf = Source pattern, null terminated. NOTE: The
344 * contents of this buffer are destroyed by this
345 * function. If this function succeeds, the
346 * buffer is copied to url->spec. If this
347 * function fails, the contents of the buffer
350 * Returns : 0 => Ok, everything else is an error.
352 *********************************************************************/
353 int create_url_spec(struct url_spec * url, char * buf)
356 struct url_spec tmp_url[1];
358 /* paranoia - should never happen. */
359 if ((url == NULL) || (buf == NULL))
364 /* save a copy of the orignal specification */
365 if ((url->spec = strdup(buf)) == NULL)
370 if ((p = strchr(buf, '/')))
372 if (NULL == (url->path = strdup(p)))
377 url->pathlen = strlen(url->path);
389 char rebuf[BUFFER_SIZE];
391 if (NULL == (url->preg = zalloc(sizeof(*url->preg))))
398 sprintf(rebuf, "^(%s)", url->path);
400 errcode = regcomp(url->preg, rebuf,
401 (REG_EXTENDED|REG_NOSUB|REG_ICASE));
404 size_t errlen = regerror(errcode,
405 url->preg, buf, sizeof(buf));
409 log_error(LOG_LEVEL_ERROR, "error compiling %s: %s",
420 if ((p = strchr(buf, ':')) == NULL)
430 if ((url->domain = strdup(buf)) == NULL)
436 #endif /* def REGEX */
440 /* split domain into components */
442 *tmp_url = dsplit(url->domain);
443 url->dbuf = tmp_url->dbuf;
444 url->dcnt = tmp_url->dcnt;
445 url->dvec = tmp_url->dvec;
446 url->unanchored = tmp_url->unanchored;
453 /*********************************************************************
455 * Function : free_url
457 * Description : Called from the "unloaders". Freez the url
458 * structure elements.
461 * 1 : url = pointer to a url_spec structure.
465 *********************************************************************/
466 void free_url(struct url_spec *url)
468 if (url == NULL) return;
486 /*********************************************************************
488 * Function : check_file_changed
490 * Description : Helper function to check if a file needs reloading.
491 * If "current" is still current, return it. Otherwise
492 * allocates a new (zeroed) "struct file_list", fills
493 * in the disk file name and timestamp, and returns it.
496 * 1 : current = The file_list currently being used - will
497 * be checked to see if it is out of date.
498 * May be NULL (which is treated as out of
500 * 2 : filename = Name of file to check.
501 * 3 : newfl = New file list. [Output only]
502 * This will be set to NULL, OR a struct
503 * file_list newly allocated on the
504 * heap, with the filename and lastmodified
505 * fields filled, standard header giving file
506 * name in proxy_args, and all others zeroed.
507 * (proxy_args is only filled in if !defined
508 * SPLIT_PROXY_ARGS and !suppress_blocklists).
510 * Returns : If file unchanged: 0 (and sets newfl == NULL)
511 * If file changed: 1 and sets newfl != NULL
512 * On error: 1 and sets newfl == NULL
514 *********************************************************************/
515 int check_file_changed(const struct file_list * current,
516 const char * filename,
517 struct file_list ** newfl)
519 struct file_list *fs;
520 struct stat statbuf[1];
524 if (stat(filename, statbuf) < 0)
526 /* Error, probably file not found. */
531 && (current->lastmodified == statbuf->st_mtime)
532 && (0 == strcmp(current->filename, filename)))
537 fs = (struct file_list *)zalloc(sizeof(struct file_list));
541 /* Out of memory error */
545 fs->filename = strdup(filename);
546 fs->lastmodified = statbuf->st_mtime;
548 if (fs->filename == NULL)
550 /* Out of memory error */
555 #ifndef SPLIT_PROXY_ARGS
556 if (!suppress_blocklists)
558 char * p = html_encode(filename);
561 fs->proxy_args = strsav(fs->proxy_args, "<h2>The file `");
562 fs->proxy_args = strsav(fs->proxy_args, p);
563 fs->proxy_args = strsav(fs->proxy_args,
564 "' contains the following patterns</h2>\n");
567 fs->proxy_args = strsav(fs->proxy_args, "<pre>");
569 #endif /* ndef SPLIT_PROXY_ARGS */
577 /*********************************************************************
579 * Function : read_config_line
581 * Description : Read a single non-empty line from a file and return
582 * it. Trims comments, leading and trailing whitespace
583 * and respects escaping of newline and comment char.
584 * Also writes the file to fs->proxy_args.
587 * 1 : buf = Buffer to use.
588 * 2 : buflen = Size of buffer in bytes.
589 * 3 : fp = File to read from
590 * 4 : fs = File will be written to fs->proxy_args. May
591 * be NULL to disable this feature.
593 * Returns : NULL on EOF or error
594 * Otherwise, returns buf.
596 *********************************************************************/
597 char *read_config_line(char *buf, int buflen, FILE *fp, struct file_list *fs)
600 char linebuf[BUFFER_SIZE];
605 while (fgets(linebuf, sizeof(linebuf), fp))
607 #ifndef SPLIT_PROXY_ARGS
608 if (fs && !suppress_blocklists)
610 char *html_line = html_encode(linebuf);
611 if (html_line != NULL)
613 fs->proxy_args = strsav(fs->proxy_args, html_line);
616 fs->proxy_args = strsav(fs->proxy_args, "<br>");
618 #endif /* ndef SPLIT_PROXY_ARGS */
620 /* Trim off newline */
621 if ((p = strpbrk(linebuf, "\r\n")) != NULL)
627 p = linebuf + strlen(linebuf);
630 /* Line continuation? Trim escape and set flag. */
631 if ((p != linebuf) && (*--p == '\\'))
637 /* If there's a comment char.. */
638 if ((p = strpbrk(linebuf, "#")) != NULL)
640 /* ..and it's escaped, left-shift the line over the escape. */
641 if ((p != linebuf) && (*(p-1) == '\\'))
644 while ((*q++ = *p++) != '\0') /* nop */;
646 /* Else, chop off the rest of the line */
653 /* Write to the buffer */
656 strncat(buf, linebuf, buflen - strlen(buf));
666 /* Remove leading and trailing whitespace */
682 /*********************************************************************
684 * Function : unload_trustfile
686 * Description : Unloads a trustfile.
689 * 1 : f = the data structure associated with the trustfile.
693 *********************************************************************/
694 static void unload_trustfile(void *f)
696 struct block_spec *b = (struct block_spec *)f;
697 if (b == NULL) return;
699 unload_trustfile(b->next); /* Stack is cheap, isn't it? */
708 /*********************************************************************
710 * Function : load_trustfile
712 * Description : Read and parse a trustfile and add to files list.
715 * 1 : csp = Current client state (buffers, headers, etc...)
717 * Returns : 0 => Ok, everything else is an error.
719 *********************************************************************/
720 int load_trustfile(struct client_state *csp)
724 struct block_spec *b, *bl;
725 struct url_spec **tl;
727 char buf[BUFFER_SIZE], *p, *q;
729 struct file_list *fs;
731 if (!check_file_changed(current_trustfile, csp->config->trustfile, &fs))
733 /* No need to load */
736 csp->tlist = current_trustfile;
742 goto load_trustfile_error;
745 fs->f = bl = (struct block_spec *)zalloc(sizeof(*bl));
748 goto load_trustfile_error;
751 if ((fp = fopen(csp->config->trustfile, "r")) == NULL)
753 goto load_trustfile_error;
756 tl = csp->config->trust_list;
758 while (read_config_line(buf, sizeof(buf), fp, fs) != NULL)
774 while ((*p++ = *q++))
780 /* skip blank lines */
786 /* allocate a new node */
787 if ((b = zalloc(sizeof(*b))) == NULL)
790 goto load_trustfile_error;
793 /* add it to the list */
799 /* Save the URL pattern */
800 if (create_url_spec(b->url, buf))
803 goto load_trustfile_error;
807 * save a pointer to URL's spec in the list of trusted URL's, too
819 #ifndef SPLIT_PROXY_ARGS
820 if (!suppress_blocklists)
822 fs->proxy_args = strsav(fs->proxy_args, "</pre>");
824 #endif /* ndef SPLIT_PROXY_ARGS */
826 /* the old one is now obsolete */
827 if (current_trustfile)
829 current_trustfile->unloader = unload_trustfile;
832 fs->next = files->next;
834 current_trustfile = fs;
843 load_trustfile_error:
844 log_error(LOG_LEVEL_FATAL, "can't load trustfile '%s': %E",
845 csp->config->trustfile);
849 #endif /* def TRUST_FILES */
852 /*********************************************************************
854 * Function : unload_re_filterfile
856 * Description : Unload the re_filter list.
859 * 1 : f = the data structure associated with the filterfile.
863 *********************************************************************/
864 static void unload_re_filterfile(void *f)
866 struct re_filterfile_spec *b = (struct re_filterfile_spec *)f;
868 if (b == NULL) return;
870 destroy_list(b->patterns);
871 pcrs_free_joblist(b->joblist);
877 /*********************************************************************
879 * Function : load_re_filterfile
881 * Description : Load the re_filterfile. Each non-comment, non-empty
882 * line is instantly added to the joblist, which is
883 * a chained list of pcrs_job structs.
886 * 1 : csp = Current client state (buffers, headers, etc...)
888 * Returns : 0 => Ok, everything else is an error.
890 *********************************************************************/
891 int load_re_filterfile(struct client_state *csp)
895 struct re_filterfile_spec *bl;
896 struct file_list *fs;
898 char buf[BUFFER_SIZE];
902 if (!check_file_changed(current_re_filterfile, csp->config->re_filterfile, &fs))
904 /* No need to load */
907 csp->rlist = current_re_filterfile;
913 goto load_re_filterfile_error;
916 fs->f = bl = (struct re_filterfile_spec *)zalloc(sizeof(*bl));
919 goto load_re_filterfile_error;
922 /* Open the file or fail */
923 if ((fp = fopen(csp->config->re_filterfile, "r")) == NULL)
925 goto load_re_filterfile_error;
928 /* Read line by line */
929 while (read_config_line(buf, sizeof(buf), fp, fs) != NULL)
931 enlist( bl->patterns, buf );
933 /* We have a meaningful line -> make it a job */
934 if ((dummy = pcrs_compile_command(buf, &error)) == NULL)
936 log_error(LOG_LEVEL_RE_FILTER,
937 "Adding re_filter job %s failed with error %d.", buf, error);
942 dummy->next = bl->joblist;
944 log_error(LOG_LEVEL_RE_FILTER, "Adding re_filter job %s succeeded.", buf);
950 #ifndef SPLIT_PROXY_ARGS
951 if (!suppress_blocklists)
953 fs->proxy_args = strsav(fs->proxy_args, "</pre>");
955 #endif /* ndef SPLIT_PROXY_ARGS */
957 /* the old one is now obsolete */
958 if ( NULL != current_re_filterfile )
960 current_re_filterfile->unloader = unload_re_filterfile;
963 fs->next = files->next;
965 current_re_filterfile = fs;
974 load_re_filterfile_error:
975 log_error(LOG_LEVEL_FATAL, "can't load re_filterfile '%s': %E",
976 csp->config->re_filterfile);
982 /*********************************************************************
984 * Function : add_loader
986 * Description : Called from `load_config'. Called once for each input
987 * file found in config.
990 * 1 : loader = pointer to a function that can parse and load
991 * the appropriate config file.
992 * 2 : config = The configuration_spec to add the loader to.
996 *********************************************************************/
997 void add_loader(int (*loader)(struct client_state *),
998 struct configuration_spec * config)
1002 for (i=0; i < NLOADERS; i++)
1004 if (config->loaders[i] == NULL)
1006 config->loaders[i] = loader;
1014 /*********************************************************************
1016 * Function : run_loader
1018 * Description : Called from `load_config' and `listen_loop'. This
1019 * function keeps the "csp" current with any file mods
1020 * since the last loop. If a file is unchanged, the
1021 * loader functions do NOT reload the file.
1024 * 1 : csp = Current client state (buffers, headers, etc...)
1025 * Must be non-null. Reads: "csp->config"
1026 * Writes: various data members.
1028 * Returns : 0 => Ok, everything else is an error.
1030 *********************************************************************/
1031 int run_loader(struct client_state *csp)
1036 for (i=0; i < NLOADERS; i++)
1038 if (csp->config->loaders[i] == NULL)
1042 ret |= (csp->config->loaders[i])(csp);