1 const char loaders_rcs[] = "$Id: loaders.c,v 1.19 2001/07/13 14:01:54 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.19 2001/07/13 14:01:54 oes
39 * Removed all #ifdef PCRS
41 * Revision 1.18 2001/06/29 21:45:41 oes
42 * Indentation, CRLF->LF, Tab-> Space
44 * Revision 1.17 2001/06/29 13:31:51 oes
47 * Revision 1.16 2001/06/09 10:55:28 jongfoster
48 * Changing BUFSIZ ==> BUFFER_SIZE
50 * Revision 1.15 2001/06/07 23:14:14 jongfoster
51 * Removing ACL and forward file loaders - these
52 * files have been merged into the config file.
53 * Cosmetic: Moving unloader funcs next to their
54 * respective loader funcs
56 * Revision 1.14 2001/06/01 03:27:04 oes
57 * Fixed line continuation problem
59 * Revision 1.13 2001/05/31 21:28:49 jongfoster
60 * Removed all permissionsfile code - it's now called the actions
61 * file, and (almost) all the code is in actions.c
63 * Revision 1.12 2001/05/31 17:32:31 oes
65 * - Enhanced domain part globbing with infix and prefix asterisk
66 * matching and optional unanchored operation
68 * Revision 1.11 2001/05/29 23:25:24 oes
70 * - load_config_line() and load_permissions_file() now use chomp()
72 * Revision 1.10 2001/05/29 09:50:24 jongfoster
73 * Unified blocklist/imagelist/permissionslist.
74 * File format is still under discussion, but the internal changes
77 * Also modified interceptor behaviour:
78 * - We now intercept all URLs beginning with one of the following
79 * prefixes (and *only* these prefixes):
81 * * http://ijbswa.sf.net/config/
82 * * http://ijbswa.sourceforge.net/config/
83 * - New interceptors "home page" - go to http://i.j.b/ to see it.
84 * - Internal changes so that intercepted and fast redirect pages
85 * are not replaced with an image.
86 * - Interceptors now have the option to send a binary page direct
87 * to the client. (i.e. ijb-send-banner uses this)
88 * - Implemented show-url-info interceptor. (Which is why I needed
89 * the above interceptors changes - a typical URL is
90 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
91 * The previous mechanism would not have intercepted that, and
92 * if it had been intercepted then it then it would have replaced
95 * Revision 1.9 2001/05/26 17:12:07 jongfoster
96 * Fatal errors loading configuration files now give better error messages.
98 * Revision 1.8 2001/05/26 00:55:20 jongfoster
99 * Removing duplicated code. load_forwardfile() now uses create_url_spec()
101 * Revision 1.7 2001/05/26 00:28:36 jongfoster
102 * Automatic reloading of config file.
103 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
104 * Most of the global variables have been moved to a new
105 * struct configuration_spec, accessed through csp->config->globalname
106 * Most of the globals remaining are used by the Win32 GUI.
108 * Revision 1.6 2001/05/23 12:27:33 oes
110 * Fixed ugly indentation of my last changes
112 * Revision 1.5 2001/05/23 10:39:05 oes
113 * - Added support for escaping the comment character
114 * in config files by a backslash
115 * - Added support for line continuation in config
117 * - Fixed a buffer overflow bug with long config lines
119 * Revision 1.4 2001/05/22 18:56:28 oes
122 * Revision 1.3 2001/05/20 01:21:20 jongfoster
123 * Version 2.9.4 checkin.
124 * - Merged popupfile and cookiefile, and added control over PCRS
125 * filtering, in new "permissionsfile".
126 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
127 * file error you now get a message box (in the Win32 GUI) rather
128 * than the program exiting with no explanation.
129 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
131 * - Removed tabs from "config"
132 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
133 * - Bumped up version number.
135 * Revision 1.2 2001/05/17 23:01:01 oes
136 * - Cleaned CRLF's from the sources and related files
138 * Revision 1.1.1.1 2001/05/15 13:58:59 oes
139 * Initial import of version 2.9.3 source tree
142 *********************************************************************/
149 #include <sys/types.h>
153 #include <sys/stat.h>
168 #include "miscutil.h"
173 const char loaders_h_rcs[] = LOADERS_H_VERSION;
175 /* Fix a problem with Solaris. There should be no effect on other
177 * Solaris's isspace() is a macro which uses it's argument directly
178 * as an array index. Therefore we need to make sure that high-bit
179 * characters generate +ve values, and ideally we also want to make
180 * the argument match the declared parameter type of "int".
182 #define ijb_isspace(__X) isspace((int)(unsigned char)(__X))
186 * Currently active files.
187 * These are also entered in the main linked list of files.
191 static struct file_list *current_trustfile = NULL;
192 #endif /* def TRUST_FILES */
194 static struct file_list *current_re_filterfile = NULL;
198 /*********************************************************************
202 * Description : Basically a mark and sweep garbage collector, it is run
203 * (by the parent thread) every once in a while to reclaim memory.
205 * It uses a mark and sweep strategy:
206 * 1) mark all files as inactive
208 * 2) check with each client:
209 * if it is active, mark its files as active
210 * if it is inactive, free its resources
212 * 3) free the resources of all of the files that
213 * are still marked as inactive (and are obsolete).
215 * N.B. files that are not obsolete don't have an unloader defined.
221 *********************************************************************/
224 struct file_list *fl, *nfl;
225 struct client_state *csp, *ncsp;
227 /* clear all of the file's active flags */
228 for ( fl = files->next; NULL != fl; fl = fl->next )
233 for (csp = clients; csp && (ncsp = csp->next) ; csp = csp->next)
237 /* mark this client's files as active */
240 * Always have a configuration file.
241 * (Also note the slightly non-standard extra
244 ncsp->config->config_file_list->active = 1;
246 if (ncsp->actions_list) /* actions files */
248 ncsp->actions_list->active = 1;
251 if (ncsp->rlist) /* pcrsjob files */
253 ncsp->rlist->active = 1;
257 if (ncsp->tlist) /* trust files */
259 ncsp->tlist->active = 1;
261 #endif /* def TRUST_FILES */
266 /* this client one is not active, release its resources */
267 csp->next = ncsp->next;
269 freez(ncsp->ip_addr_str);
270 freez(ncsp->my_ip_addr_str);
271 freez(ncsp->my_hostname);
274 freez(ncsp->referrer);
275 #endif /* def TRUST_FILES */
276 freez(ncsp->x_forwarded);
277 freez(ncsp->iob->buf);
279 free_http_request(ncsp->http);
281 destroy_list(ncsp->headers);
282 destroy_list(ncsp->cookie_list);
284 free_current_action(ncsp->action);
292 #endif /* def STATISTICS */
298 for (fl = files; fl && (nfl = fl->next) ; fl = fl->next)
300 if ( ( 0 == nfl->active ) && ( NULL != nfl->unloader ) )
302 fl->next = nfl->next;
304 (nfl->unloader)(nfl->f);
306 #ifndef SPLIT_PROXY_ARGS
307 freez(nfl->proxy_args);
308 #endif /* ndef SPLIT_PROXY_ARGS */
310 freez(nfl->filename);
319 /*********************************************************************
321 * Function : create_url_spec
323 * Description : Creates a "url_spec" structure from a string.
324 * When finished, free with unload_url().
327 * 1 : url = Target url_spec to be filled in. Must be
328 * zeroed out before the call (e.g. using zalloc).
329 * 2 : buf = Source pattern, null terminated. NOTE: The
330 * contents of this buffer are destroyed by this
331 * function. If this function succeeds, the
332 * buffer is copied to url->spec. If this
333 * function fails, the contents of the buffer
336 * Returns : 0 => Ok, everything else is an error.
338 *********************************************************************/
339 int create_url_spec(struct url_spec * url, char * buf)
342 struct url_spec tmp_url[1];
344 /* paranoia - should never happen. */
345 if ((url == NULL) || (buf == NULL))
350 /* save a copy of the orignal specification */
351 if ((url->spec = strdup(buf)) == NULL)
356 if ((p = strchr(buf, '/')))
358 if (NULL == (url->path = strdup(p)))
363 url->pathlen = strlen(url->path);
375 char rebuf[BUFFER_SIZE];
377 if (NULL == (url->preg = zalloc(sizeof(*url->preg))))
384 sprintf(rebuf, "^(%s)", url->path);
386 errcode = regcomp(url->preg, rebuf,
387 (REG_EXTENDED|REG_NOSUB|REG_ICASE));
390 size_t errlen = regerror(errcode,
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;
439 /*********************************************************************
441 * Function : free_url
443 * Description : Called from the "unloaders". Freez the url
444 * structure elements.
447 * 1 : url = pointer to a url_spec structure.
451 *********************************************************************/
452 void free_url(struct url_spec *url)
454 if (url == NULL) return;
472 /*********************************************************************
474 * Function : check_file_changed
476 * Description : Helper function to check if a file needs reloading.
477 * If "current" is still current, return it. Otherwise
478 * allocates a new (zeroed) "struct file_list", fills
479 * in the disk file name and timestamp, and returns it.
482 * 1 : current = The file_list currently being used - will
483 * be checked to see if it is out of date.
484 * May be NULL (which is treated as out of
486 * 2 : filename = Name of file to check.
487 * 3 : newfl = New file list. [Output only]
488 * This will be set to NULL, OR a struct
489 * file_list newly allocated on the
490 * heap, with the filename and lastmodified
491 * fields filled, standard header giving file
492 * name in proxy_args, and all others zeroed.
493 * (proxy_args is only filled in if !defined
494 * SPLIT_PROXY_ARGS and !suppress_blocklists).
496 * Returns : If file unchanged: 0 (and sets newfl == NULL)
497 * If file changed: 1 and sets newfl != NULL
498 * On error: 1 and sets newfl == NULL
500 *********************************************************************/
501 int check_file_changed(const struct file_list * current,
502 const char * filename,
503 struct file_list ** newfl)
505 struct file_list *fs;
506 struct stat statbuf[1];
510 if (stat(filename, statbuf) < 0)
512 /* Error, probably file not found. */
517 && (current->lastmodified == statbuf->st_mtime)
518 && (0 == strcmp(current->filename, filename)))
523 fs = (struct file_list *)zalloc(sizeof(struct file_list));
527 /* Out of memory error */
531 fs->filename = strdup(filename);
532 fs->lastmodified = statbuf->st_mtime;
534 if (fs->filename == NULL)
536 /* Out of memory error */
541 #ifndef SPLIT_PROXY_ARGS
542 if (!suppress_blocklists)
544 char * p = html_encode(filename);
547 fs->proxy_args = strsav(fs->proxy_args, "<h2>The file `");
548 fs->proxy_args = strsav(fs->proxy_args, p);
549 fs->proxy_args = strsav(fs->proxy_args,
550 "' contains the following patterns</h2>\n");
553 fs->proxy_args = strsav(fs->proxy_args, "<pre>");
555 #endif /* ndef SPLIT_PROXY_ARGS */
563 /*********************************************************************
565 * Function : read_config_line
567 * Description : Read a single non-empty line from a file and return
568 * it. Trims comments, leading and trailing whitespace
569 * and respects escaping of newline and comment char.
570 * Also writes the file to fs->proxy_args.
573 * 1 : buf = Buffer to use.
574 * 2 : buflen = Size of buffer in bytes.
575 * 3 : fp = File to read from
576 * 4 : fs = File will be written to fs->proxy_args. May
577 * be NULL to disable this feature.
579 * Returns : NULL on EOF or error
580 * Otherwise, returns buf.
582 *********************************************************************/
583 char *read_config_line(char *buf, int buflen, FILE *fp, struct file_list *fs)
586 char linebuf[BUFFER_SIZE];
591 while (fgets(linebuf, sizeof(linebuf), fp))
593 #ifndef SPLIT_PROXY_ARGS
594 if (fs && !suppress_blocklists)
596 char *html_line = html_encode(linebuf);
597 if (html_line != NULL)
599 fs->proxy_args = strsav(fs->proxy_args, html_line);
602 fs->proxy_args = strsav(fs->proxy_args, "<br>");
604 #endif /* ndef SPLIT_PROXY_ARGS */
606 /* Trim off newline */
607 if ((p = strpbrk(linebuf, "\r\n")) != NULL)
613 p = linebuf + strlen(linebuf);
616 /* Line continuation? Trim escape and set flag. */
617 if ((p != linebuf) && (*--p == '\\'))
623 /* If there's a comment char.. */
624 if ((p = strpbrk(linebuf, "#")) != NULL)
626 /* ..and it's escaped, left-shift the line over the escape. */
627 if ((p != linebuf) && (*(p-1) == '\\'))
630 while ((*q++ = *p++) != '\0') /* nop */;
632 /* Else, chop off the rest of the line */
639 /* Write to the buffer */
642 strncat(buf, linebuf, buflen - strlen(buf));
652 /* Remove leading and trailing whitespace */
668 /*********************************************************************
670 * Function : unload_trustfile
672 * Description : Unloads a trustfile.
675 * 1 : f = the data structure associated with the trustfile.
679 *********************************************************************/
680 static void unload_trustfile(void *f)
682 struct block_spec *b = (struct block_spec *)f;
683 if (b == NULL) return;
685 unload_trustfile(b->next); /* Stack is cheap, isn't it? */
694 /*********************************************************************
696 * Function : load_trustfile
698 * Description : Read and parse a trustfile and add to files list.
701 * 1 : csp = Current client state (buffers, headers, etc...)
703 * Returns : 0 => Ok, everything else is an error.
705 *********************************************************************/
706 int load_trustfile(struct client_state *csp)
710 struct block_spec *b, *bl;
711 struct url_spec **tl;
713 char buf[BUFFER_SIZE], *p, *q;
715 struct file_list *fs;
717 if (!check_file_changed(current_trustfile, csp->config->trustfile, &fs))
719 /* No need to load */
722 csp->tlist = current_trustfile;
728 goto load_trustfile_error;
731 fs->f = bl = (struct block_spec *)zalloc(sizeof(*bl));
734 goto load_trustfile_error;
737 if ((fp = fopen(csp->config->trustfile, "r")) == NULL)
739 goto load_trustfile_error;
742 tl = csp->config->trust_list;
744 while (read_config_line(buf, sizeof(buf), fp, fs) != NULL)
760 while ((*p++ = *q++))
766 /* skip blank lines */
772 /* allocate a new node */
773 if ((b = zalloc(sizeof(*b))) == NULL)
776 goto load_trustfile_error;
779 /* add it to the list */
785 /* Save the URL pattern */
786 if (create_url_spec(b->url, buf))
789 goto load_trustfile_error;
793 * save a pointer to URL's spec in the list of trusted URL's, too
805 #ifndef SPLIT_PROXY_ARGS
806 if (!suppress_blocklists)
808 fs->proxy_args = strsav(fs->proxy_args, "</pre>");
810 #endif /* ndef SPLIT_PROXY_ARGS */
812 /* the old one is now obsolete */
813 if (current_trustfile)
815 current_trustfile->unloader = unload_trustfile;
818 fs->next = files->next;
820 current_trustfile = fs;
829 load_trustfile_error:
830 log_error(LOG_LEVEL_FATAL, "can't load trustfile '%s': %E",
831 csp->config->trustfile);
835 #endif /* def TRUST_FILES */
838 /*********************************************************************
840 * Function : unload_re_filterfile
842 * Description : Unload the re_filter list.
845 * 1 : f = the data structure associated with the filterfile.
849 *********************************************************************/
850 static void unload_re_filterfile(void *f)
852 struct re_filterfile_spec *b = (struct re_filterfile_spec *)f;
854 if (b == NULL) return;
856 destroy_list(b->patterns);
857 pcrs_free_joblist(b->joblist);
863 /*********************************************************************
865 * Function : load_re_filterfile
867 * Description : Load the re_filterfile. Each non-comment, non-empty
868 * line is instantly added to the joblist, which is
869 * a chained list of pcrs_job structs.
872 * 1 : csp = Current client state (buffers, headers, etc...)
874 * Returns : 0 => Ok, everything else is an error.
876 *********************************************************************/
877 int load_re_filterfile(struct client_state *csp)
881 struct re_filterfile_spec *bl;
882 struct file_list *fs;
884 char buf[BUFFER_SIZE];
888 if (!check_file_changed(current_re_filterfile, csp->config->re_filterfile, &fs))
890 /* No need to load */
893 csp->rlist = current_re_filterfile;
899 goto load_re_filterfile_error;
902 fs->f = bl = (struct re_filterfile_spec *)zalloc(sizeof(*bl));
905 goto load_re_filterfile_error;
908 /* Open the file or fail */
909 if ((fp = fopen(csp->config->re_filterfile, "r")) == NULL)
911 goto load_re_filterfile_error;
914 /* Read line by line */
915 while (read_config_line(buf, sizeof(buf), fp, fs) != NULL)
917 enlist( bl->patterns, buf );
919 /* We have a meaningful line -> make it a job */
920 if ((dummy = pcrs_compile(buf, &error)) == NULL)
922 log_error(LOG_LEVEL_RE_FILTER,
923 "Adding re_filter job %s failed with error %d.", buf, error);
928 dummy->next = bl->joblist;
930 log_error(LOG_LEVEL_RE_FILTER, "Adding re_filter job %s succeeded.", buf);
936 #ifndef SPLIT_PROXY_ARGS
937 if (!suppress_blocklists)
939 fs->proxy_args = strsav(fs->proxy_args, "</pre>");
941 #endif /* ndef SPLIT_PROXY_ARGS */
943 /* the old one is now obsolete */
944 if ( NULL != current_re_filterfile )
946 current_re_filterfile->unloader = unload_re_filterfile;
949 fs->next = files->next;
951 current_re_filterfile = fs;
960 load_re_filterfile_error:
961 log_error(LOG_LEVEL_FATAL, "can't load re_filterfile '%s': %E",
962 csp->config->re_filterfile);
968 /*********************************************************************
970 * Function : add_loader
972 * Description : Called from `load_config'. Called once for each input
973 * file found in config.
976 * 1 : loader = pointer to a function that can parse and load
977 * the appropriate config file.
978 * 2 : config = The configuration_spec to add the loader to.
982 *********************************************************************/
983 void add_loader(int (*loader)(struct client_state *),
984 struct configuration_spec * config)
988 for (i=0; i < NLOADERS; i++)
990 if (config->loaders[i] == NULL)
992 config->loaders[i] = loader;
1000 /*********************************************************************
1002 * Function : run_loader
1004 * Description : Called from `load_config' and `listen_loop'. This
1005 * function keeps the "csp" current with any file mods
1006 * since the last loop. If a file is unchanged, the
1007 * loader functions do NOT reload the file.
1010 * 1 : csp = Current client state (buffers, headers, etc...)
1011 * Must be non-null. Reads: "csp->config"
1012 * Writes: various data members.
1014 * Returns : 0 => Ok, everything else is an error.
1016 *********************************************************************/
1017 int run_loader(struct client_state *csp)
1022 for (i=0; i < NLOADERS; i++)
1024 if (csp->config->loaders[i] == NULL)
1028 ret |= (csp->config->loaders[i])(csp);