1 const char loaders_rcs[] = "$Id: loaders.c,v 1.23 2001/07/20 15:51: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.23 2001/07/20 15:51:54 oes
39 * Fixed indentation of prepocessor commands
41 * Revision 1.22 2001/07/20 15:16:17 haroon
42 * - per Guy's suggestion, added a while loop in sweep() to catch not just
43 * the last inactive CSP but all other consecutive inactive CSPs after that
46 * Revision 1.21 2001/07/18 17:26:24 oes
47 * Changed to conform to new pcrs interface
49 * Revision 1.20 2001/07/17 13:07:01 oes
50 * Fixed segv when last line in config files
51 * lacked a terminating (\r)\n
53 * Revision 1.19 2001/07/13 14:01:54 oes
54 * Removed all #ifdef PCRS
56 * Revision 1.18 2001/06/29 21:45:41 oes
57 * Indentation, CRLF->LF, Tab-> Space
59 * Revision 1.17 2001/06/29 13:31:51 oes
62 * Revision 1.16 2001/06/09 10:55:28 jongfoster
63 * Changing BUFSIZ ==> BUFFER_SIZE
65 * Revision 1.15 2001/06/07 23:14:14 jongfoster
66 * Removing ACL and forward file loaders - these
67 * files have been merged into the config file.
68 * Cosmetic: Moving unloader funcs next to their
69 * respective loader funcs
71 * Revision 1.14 2001/06/01 03:27:04 oes
72 * Fixed line continuation problem
74 * Revision 1.13 2001/05/31 21:28:49 jongfoster
75 * Removed all permissionsfile code - it's now called the actions
76 * file, and (almost) all the code is in actions.c
78 * Revision 1.12 2001/05/31 17:32:31 oes
80 * - Enhanced domain part globbing with infix and prefix asterisk
81 * matching and optional unanchored operation
83 * Revision 1.11 2001/05/29 23:25:24 oes
85 * - load_config_line() and load_permissions_file() now use chomp()
87 * Revision 1.10 2001/05/29 09:50:24 jongfoster
88 * Unified blocklist/imagelist/permissionslist.
89 * File format is still under discussion, but the internal changes
92 * Also modified interceptor behaviour:
93 * - We now intercept all URLs beginning with one of the following
94 * prefixes (and *only* these prefixes):
96 * * http://ijbswa.sf.net/config/
97 * * http://ijbswa.sourceforge.net/config/
98 * - New interceptors "home page" - go to http://i.j.b/ to see it.
99 * - Internal changes so that intercepted and fast redirect pages
100 * are not replaced with an image.
101 * - Interceptors now have the option to send a binary page direct
102 * to the client. (i.e. ijb-send-banner uses this)
103 * - Implemented show-url-info interceptor. (Which is why I needed
104 * the above interceptors changes - a typical URL is
105 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
106 * The previous mechanism would not have intercepted that, and
107 * if it had been intercepted then it then it would have replaced
110 * Revision 1.9 2001/05/26 17:12:07 jongfoster
111 * Fatal errors loading configuration files now give better error messages.
113 * Revision 1.8 2001/05/26 00:55:20 jongfoster
114 * Removing duplicated code. load_forwardfile() now uses create_url_spec()
116 * Revision 1.7 2001/05/26 00:28:36 jongfoster
117 * Automatic reloading of config file.
118 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
119 * Most of the global variables have been moved to a new
120 * struct configuration_spec, accessed through csp->config->globalname
121 * Most of the globals remaining are used by the Win32 GUI.
123 * Revision 1.6 2001/05/23 12:27:33 oes
125 * Fixed ugly indentation of my last changes
127 * Revision 1.5 2001/05/23 10:39:05 oes
128 * - Added support for escaping the comment character
129 * in config files by a backslash
130 * - Added support for line continuation in config
132 * - Fixed a buffer overflow bug with long config lines
134 * Revision 1.4 2001/05/22 18:56:28 oes
137 * Revision 1.3 2001/05/20 01:21:20 jongfoster
138 * Version 2.9.4 checkin.
139 * - Merged popupfile and cookiefile, and added control over PCRS
140 * filtering, in new "permissionsfile".
141 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
142 * file error you now get a message box (in the Win32 GUI) rather
143 * than the program exiting with no explanation.
144 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
146 * - Removed tabs from "config"
147 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
148 * - Bumped up version number.
150 * Revision 1.2 2001/05/17 23:01:01 oes
151 * - Cleaned CRLF's from the sources and related files
153 * Revision 1.1.1.1 2001/05/15 13:58:59 oes
154 * Initial import of version 2.9.3 source tree
157 *********************************************************************/
164 #include <sys/types.h>
168 #include <sys/stat.h>
183 #include "miscutil.h"
188 const char loaders_h_rcs[] = LOADERS_H_VERSION;
190 /* Fix a problem with Solaris. There should be no effect on other
192 * Solaris's isspace() is a macro which uses it's argument directly
193 * as an array index. Therefore we need to make sure that high-bit
194 * characters generate +ve values, and ideally we also want to make
195 * the argument match the declared parameter type of "int".
197 #define ijb_isspace(__X) isspace((int)(unsigned char)(__X))
201 * Currently active files.
202 * These are also entered in the main linked list of files.
206 static struct file_list *current_trustfile = NULL;
207 #endif /* def FEATURE_TRUST */
209 static struct file_list *current_re_filterfile = NULL;
213 /*********************************************************************
217 * Description : Basically a mark and sweep garbage collector, it is run
218 * (by the parent thread) every once in a while to reclaim memory.
220 * It uses a mark and sweep strategy:
221 * 1) mark all files as inactive
223 * 2) check with each client:
224 * if it is active, mark its files as active
225 * if it is inactive, free its resources
227 * 3) free the resources of all of the files that
228 * are still marked as inactive (and are obsolete).
230 * N.B. files that are not obsolete don't have an unloader defined.
236 *********************************************************************/
239 struct file_list *fl, *nfl;
240 struct client_state *csp, *ncsp;
242 /* clear all of the file's active flags */
243 for ( fl = files->next; NULL != fl; fl = fl->next )
248 for (csp = clients; csp && (ncsp = csp->next) ; csp = csp->next)
252 /* mark this client's files as active */
255 * Always have a configuration file.
256 * (Also note the slightly non-standard extra
259 ncsp->config->config_file_list->active = 1;
261 if (ncsp->actions_list) /* actions files */
263 ncsp->actions_list->active = 1;
266 if (ncsp->rlist) /* pcrsjob files */
268 ncsp->rlist->active = 1;
272 if (ncsp->tlist) /* trust files */
274 ncsp->tlist->active = 1;
276 #endif /* def FEATURE_TRUST */
281 * this client is not active, release its resources
282 * and the ones of all inactive clients that might
286 while( !ncsp->active )
288 csp->next = ncsp->next;
290 freez(ncsp->ip_addr_str);
291 freez(ncsp->my_ip_addr_str);
292 freez(ncsp->my_hostname);
295 freez(ncsp->referrer);
296 #endif /* def FEATURE_TRUST */
297 freez(ncsp->x_forwarded);
298 freez(ncsp->iob->buf);
300 free_http_request(ncsp->http);
302 destroy_list(ncsp->headers);
303 destroy_list(ncsp->cookie_list);
305 free_current_action(ncsp->action);
307 #ifdef FEATURE_STATISTICS
313 #endif /* def FEATURE_STATISTICS */
317 /* are there any more in sequence after it? */
318 if( !(ncsp = csp->next) )
324 for (fl = files; fl && (nfl = fl->next) ; fl = fl->next)
326 if ( ( 0 == nfl->active ) && ( NULL != nfl->unloader ) )
328 fl->next = nfl->next;
330 (nfl->unloader)(nfl->f);
332 freez(nfl->filename);
341 /*********************************************************************
343 * Function : create_url_spec
345 * Description : Creates a "url_spec" structure from a string.
346 * When finished, free with unload_url().
349 * 1 : url = Target url_spec to be filled in. Must be
350 * zeroed out before the call (e.g. using zalloc).
351 * 2 : buf = Source pattern, null terminated. NOTE: The
352 * contents of this buffer are destroyed by this
353 * function. If this function succeeds, the
354 * buffer is copied to url->spec. If this
355 * function fails, the contents of the buffer
358 * Returns : 0 => Ok, everything else is an error.
360 *********************************************************************/
361 int create_url_spec(struct url_spec * url, char * buf)
364 struct url_spec tmp_url[1];
366 /* paranoia - should never happen. */
367 if ((url == NULL) || (buf == NULL))
372 /* save a copy of the orignal specification */
373 if ((url->spec = strdup(buf)) == NULL)
378 if ((p = strchr(buf, '/')))
380 if (NULL == (url->path = strdup(p)))
385 url->pathlen = strlen(url->path);
397 char rebuf[BUFFER_SIZE];
399 if (NULL == (url->preg = zalloc(sizeof(*url->preg))))
406 sprintf(rebuf, "^(%s)", url->path);
408 errcode = regcomp(url->preg, rebuf,
409 (REG_EXTENDED|REG_NOSUB|REG_ICASE));
412 size_t errlen = regerror(errcode,
413 url->preg, buf, sizeof(buf));
417 log_error(LOG_LEVEL_ERROR, "error compiling %s: %s",
428 if ((p = strchr(buf, ':')) == NULL)
438 if ((url->domain = strdup(buf)) == NULL)
444 #endif /* def REGEX */
448 /* split domain into components */
450 *tmp_url = dsplit(url->domain);
451 url->dbuf = tmp_url->dbuf;
452 url->dcnt = tmp_url->dcnt;
453 url->dvec = tmp_url->dvec;
454 url->unanchored = tmp_url->unanchored;
461 /*********************************************************************
463 * Function : free_url
465 * Description : Called from the "unloaders". Freez the url
466 * structure elements.
469 * 1 : url = pointer to a url_spec structure.
473 *********************************************************************/
474 void free_url(struct url_spec *url)
476 if (url == NULL) return;
494 /*********************************************************************
496 * Function : check_file_changed
498 * Description : Helper function to check if a file needs reloading.
499 * If "current" is still current, return it. Otherwise
500 * allocates a new (zeroed) "struct file_list", fills
501 * in the disk file name and timestamp, and returns it.
504 * 1 : current = The file_list currently being used - will
505 * be checked to see if it is out of date.
506 * May be NULL (which is treated as out of
508 * 2 : filename = Name of file to check.
509 * 3 : newfl = New file list. [Output only]
510 * This will be set to NULL, OR a struct
511 * file_list newly allocated on the
512 * heap, with the filename and lastmodified
513 * fields filled, and all others zeroed.
515 * Returns : If file unchanged: 0 (and sets newfl == NULL)
516 * If file changed: 1 and sets newfl != NULL
517 * On error: 1 and sets newfl == NULL
519 *********************************************************************/
520 int check_file_changed(const struct file_list * current,
521 const char * filename,
522 struct file_list ** newfl)
524 struct file_list *fs;
525 struct stat statbuf[1];
529 if (stat(filename, statbuf) < 0)
531 /* Error, probably file not found. */
536 && (current->lastmodified == statbuf->st_mtime)
537 && (0 == strcmp(current->filename, filename)))
542 fs = (struct file_list *)zalloc(sizeof(struct file_list));
546 /* Out of memory error */
550 fs->filename = strdup(filename);
551 fs->lastmodified = statbuf->st_mtime;
553 if (fs->filename == NULL)
555 /* Out of memory error */
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 /* Trim off newline */
598 if ((p = strpbrk(linebuf, "\r\n")) != NULL)
604 p = linebuf + strlen(linebuf);
607 /* Line continuation? Trim escape and set flag. */
608 if ((p != linebuf) && (*--p == '\\'))
614 /* If there's a comment char.. */
615 if ((p = strpbrk(linebuf, "#")) != NULL)
617 /* ..and it's escaped, left-shift the line over the escape. */
618 if ((p != linebuf) && (*(p-1) == '\\'))
621 while ((*q++ = *p++) != '\0') /* nop */;
623 /* Else, chop off the rest of the line */
630 /* Write to the buffer */
633 strncat(buf, linebuf, buflen - strlen(buf));
643 /* Remove leading and trailing whitespace */
659 /*********************************************************************
661 * Function : unload_trustfile
663 * Description : Unloads a trustfile.
666 * 1 : f = the data structure associated with the trustfile.
670 *********************************************************************/
671 static void unload_trustfile(void *f)
673 struct block_spec *b = (struct block_spec *)f;
674 if (b == NULL) return;
676 unload_trustfile(b->next); /* Stack is cheap, isn't it? */
685 /*********************************************************************
687 * Function : load_trustfile
689 * Description : Read and parse a trustfile and add to files list.
692 * 1 : csp = Current client state (buffers, headers, etc...)
694 * Returns : 0 => Ok, everything else is an error.
696 *********************************************************************/
697 int load_trustfile(struct client_state *csp)
701 struct block_spec *b, *bl;
702 struct url_spec **tl;
704 char buf[BUFFER_SIZE], *p, *q;
706 struct file_list *fs;
708 if (!check_file_changed(current_trustfile, csp->config->trustfile, &fs))
710 /* No need to load */
713 csp->tlist = current_trustfile;
719 goto load_trustfile_error;
722 fs->f = bl = (struct block_spec *)zalloc(sizeof(*bl));
725 goto load_trustfile_error;
728 if ((fp = fopen(csp->config->trustfile, "r")) == NULL)
730 goto load_trustfile_error;
733 tl = csp->config->trust_list;
735 while (read_config_line(buf, sizeof(buf), fp, fs) != NULL)
751 while ((*p++ = *q++))
757 /* skip blank lines */
763 /* allocate a new node */
764 if ((b = zalloc(sizeof(*b))) == NULL)
767 goto load_trustfile_error;
770 /* add it to the list */
776 /* Save the URL pattern */
777 if (create_url_spec(b->url, buf))
780 goto load_trustfile_error;
784 * save a pointer to URL's spec in the list of trusted URL's, too
796 /* the old one is now obsolete */
797 if (current_trustfile)
799 current_trustfile->unloader = unload_trustfile;
802 fs->next = files->next;
804 current_trustfile = fs;
813 load_trustfile_error:
814 log_error(LOG_LEVEL_FATAL, "can't load trustfile '%s': %E",
815 csp->config->trustfile);
819 #endif /* def FEATURE_TRUST */
822 /*********************************************************************
824 * Function : unload_re_filterfile
826 * Description : Unload the re_filter list.
829 * 1 : f = the data structure associated with the filterfile.
833 *********************************************************************/
834 static void unload_re_filterfile(void *f)
836 struct re_filterfile_spec *b = (struct re_filterfile_spec *)f;
838 if (b == NULL) return;
840 destroy_list(b->patterns);
841 pcrs_free_joblist(b->joblist);
847 /*********************************************************************
849 * Function : load_re_filterfile
851 * Description : Load the re_filterfile. Each non-comment, non-empty
852 * line is instantly added to the joblist, which is
853 * a chained list of pcrs_job structs.
856 * 1 : csp = Current client state (buffers, headers, etc...)
858 * Returns : 0 => Ok, everything else is an error.
860 *********************************************************************/
861 int load_re_filterfile(struct client_state *csp)
865 struct re_filterfile_spec *bl;
866 struct file_list *fs;
868 char buf[BUFFER_SIZE];
872 if (!check_file_changed(current_re_filterfile, csp->config->re_filterfile, &fs))
874 /* No need to load */
877 csp->rlist = current_re_filterfile;
883 goto load_re_filterfile_error;
886 fs->f = bl = (struct re_filterfile_spec *)zalloc(sizeof(*bl));
889 goto load_re_filterfile_error;
892 /* Open the file or fail */
893 if ((fp = fopen(csp->config->re_filterfile, "r")) == NULL)
895 goto load_re_filterfile_error;
898 /* Read line by line */
899 while (read_config_line(buf, sizeof(buf), fp, fs) != NULL)
901 enlist( bl->patterns, buf );
903 /* We have a meaningful line -> make it a job */
904 if ((dummy = pcrs_compile_command(buf, &error)) == NULL)
906 log_error(LOG_LEVEL_RE_FILTER,
907 "Adding re_filter job %s failed with error %d.", buf, error);
912 dummy->next = bl->joblist;
914 log_error(LOG_LEVEL_RE_FILTER, "Adding re_filter job %s succeeded.", buf);
920 /* the old one is now obsolete */
921 if ( NULL != current_re_filterfile )
923 current_re_filterfile->unloader = unload_re_filterfile;
926 fs->next = files->next;
928 current_re_filterfile = fs;
937 load_re_filterfile_error:
938 log_error(LOG_LEVEL_FATAL, "can't load re_filterfile '%s': %E",
939 csp->config->re_filterfile);
945 /*********************************************************************
947 * Function : add_loader
949 * Description : Called from `load_config'. Called once for each input
950 * file found in config.
953 * 1 : loader = pointer to a function that can parse and load
954 * the appropriate config file.
955 * 2 : config = The configuration_spec to add the loader to.
959 *********************************************************************/
960 void add_loader(int (*loader)(struct client_state *),
961 struct configuration_spec * config)
965 for (i=0; i < NLOADERS; i++)
967 if (config->loaders[i] == NULL)
969 config->loaders[i] = loader;
977 /*********************************************************************
979 * Function : run_loader
981 * Description : Called from `load_config' and `listen_loop'. This
982 * function keeps the "csp" current with any file mods
983 * since the last loop. If a file is unchanged, the
984 * loader functions do NOT reload the file.
987 * 1 : csp = Current client state (buffers, headers, etc...)
988 * Must be non-null. Reads: "csp->config"
989 * Writes: various data members.
991 * Returns : 0 => Ok, everything else is an error.
993 *********************************************************************/
994 int run_loader(struct client_state *csp)
999 for (i=0; i < NLOADERS; i++)
1001 if (csp->config->loaders[i] == NULL)
1005 ret |= (csp->config->loaders[i])(csp);