- unload_cookiefile(b->next);
-
- unload_url(b->url);
-
- freez(b);
-
-}
-
-
-#ifdef TRUST_FILES
-/*********************************************************************
- *
- * Function : unload_trustfile
- *
- * Description : Unloads a trustfile.
- *
- * Parameters :
- * 1 : f = the data structure associated with the trustfile.
- *
- * Returns : N/A
- *
- *********************************************************************/
-static void unload_trustfile(void *f)
-{
- struct block_spec *b = (struct block_spec *)f;
- if (b == NULL) return;
-
- unload_trustfile(b->next);
-
- unload_url(b->url);
-
- freez(b);
-
-}
-#endif /* def TRUST_FILES */
-
-
-/*********************************************************************
- *
- * Function : unload_forwardfile
- *
- * Description : Unloads a forwardfile.
- *
- * Parameters :
- * 1 : f = the data structure associated with the forwardfile.
- *
- * Returns : N/A
- *
- *********************************************************************/
-static void unload_forwardfile(void *f)
-{
- struct forward_spec *b = (struct forward_spec *)f;
- if (b == NULL) return;
-
- unload_forwardfile(b->next);
-
- unload_url(b->url);
-
- freez(b->gw->gateway_host);
- freez(b->gw->forward_host);
-
- freez(b);
-
-}
-
-
-#ifdef PCRS
-/*********************************************************************
- *
- * Function : unload_re_filterfile
- *
- * Description : Unload the re_filter list.
- *
- * Parameters :
- * 1 : f = the data structure associated with the filterfile.
- *
- * Returns : N/A
- *
- *********************************************************************/
-static void unload_re_filterfile(void *f)
-{
- pcrs_job *joblist;
- struct re_filterfile_spec *b = (struct re_filterfile_spec *)f;
-
- if (b == NULL) return;
-
- destroy_list(b->patterns);
-
- joblist = b->joblist;
- while ( NULL != (joblist = pcrs_free_job(joblist)) ) {}
-
- freez(b);
-
-}
-#endif /* def PCRS */
-
-
-#ifdef KILLPOPUPS
-/*********************************************************************
- *
- * Function : unload_popupfile
- *
- * Description : Free the lists of blocked, and allowed popup sites.
- *
- * Parameters :
- * 1 : csp = Current client state (buffers, headers, etc...)
- *
- * Returns : N/A
- *
- *********************************************************************/
-static void unload_popupfile(void * b)
-{
- struct popup_settings * data = (struct popup_settings *) b;
- struct popup_blocklist * cur = NULL;
- struct popup_blocklist * temp= NULL;
-
- /* Free the blocked list. */
- cur = data->blocked;
- while (cur != NULL)
- {
- temp = cur->next;
- freez (cur->host_name);
- free (cur);
- cur = temp;
- }
- data->blocked = NULL;
-
- /* Free the allowed list. */
- cur = data->allowed;
- while (cur != NULL)
- {
- temp = cur->next;
- freez (cur->host_name);
- free (cur);
- cur = temp;
- }
- data->allowed = NULL;
-
-}
-#endif /* def KILLPOPUPS */
-
-
-/*********************************************************************
- *
- * Function : check_file_changed
- *
- * Description : Helper function to check if a file needs reloading.
- * If "current" is still current, return it. Otherwise
- * allocates a new (zeroed) "struct file_list", fills
- * in the disk file name and timestamp, and returns it.
- *
- * Parameters :
- * 1 : current = The file_list currently being used - will
- * be checked to see if it is out of date.
- * May be NULL (which is treated as out of
- * date).
- * 2 : filename = Name of file to check.
- * 3 : newfl = New file list. [Output only]
- * This will be set to NULL, OR a struct
- * file_list newly allocated on the
- * heap, with the filename and lastmodified
- * fields filled, standard header giving file
- * name in proxy_args, and all others zeroed.
- * (proxy_args is only filled in if
- * SPLIT_PROXY_ARGS and !suppress_blocklists).
- *
- * Returns : If file unchanged: 0 (and sets newfl == NULL)
- * If file changed: 1 and sets newfl != NULL
- * On error: 1 and sets newfl == NULL
- *
- *********************************************************************/
-static int check_file_changed(const struct file_list * current,
- const char * filename,
- struct file_list ** newfl)
-{
- struct file_list *fs;
- struct stat statbuf[1];
-
- *newfl = NULL;
-
- if (stat(filename, statbuf) < 0)
- {
- /* Error, probably file not found. */
- return 1;
- }
-
- if (current
- && (current->lastmodified == statbuf->st_mtime)
- && (0 == strcmp(current->filename, filename)))
- {
- return 0;
- }
-
- fs = (struct file_list *)zalloc(sizeof(struct file_list));
-
- if (fs == NULL)
- {
- /* Out of memory error */
- return 1;
- }
-
- fs->filename = strdup(filename);
- fs->lastmodified = statbuf->st_mtime;
-
- if (fs->filename == NULL)
- {
- /* Out of memory error */
- freez (fs);
- return 1;
- }
-
-#ifndef SPLIT_PROXY_ARGS
- if (!suppress_blocklists)
- {
- char * p = html_encode(filename);
- if (p)
- {
- fs->proxy_args = strsav(fs->proxy_args, "<h2>The file `");\r
- fs->proxy_args = strsav(fs->proxy_args, p);\r
- fs->proxy_args = strsav(fs->proxy_args, \r
- "' contains the following patterns</h2>\n");\r
- freez(p);\r
- }
- fs->proxy_args = strsav(fs->proxy_args, "<pre>");
- }
-#endif /* ndef SPLIT_PROXY_ARGS */
-
- *newfl = fs;
- return 1;
-}
-
-
-/*********************************************************************
- *
- * Function : read_config_line
- *
- * Description : Read a single non-empty line from a file and return
- * it. Trims comments, leading and trailing whitespace.
- * Also wites the file to fs->proxy_args.
- *
- * Parameters :
- * 1 : buf = Buffer to use.
- * 2 : buflen = Size of buffer in bytes.
- * 3 : fp = File to read from
- * 4 : fs = File will be written to fs->proxy_args. May
- * be NULL to disable this feature.
- *
- * Returns : NULL on EOF or error
- * Otherwise, returns buf.
- *
- *********************************************************************/
-char *read_config_line(char *buf, int buflen, FILE *fp, struct file_list *fs)
-{
- char *p, *q;
- char linebuf[BUFSIZ];
-
- while (fgets(linebuf, sizeof(linebuf), fp))
- {
-#ifndef SPLIT_PROXY_ARGS
- if (fs && !suppress_blocklists)
- {
- char *html_line = html_encode(linebuf);
- if (html_line != NULL)
- {
- fs->proxy_args = strsav(fs->proxy_args, html_line);
- freez(html_line);
- }
- fs->proxy_args = strsav(fs->proxy_args, "<br>");
- }
-#endif /* ndef SPLIT_PROXY_ARGS */
-
- /* Trim off newline and any comment */
- if ((p = strpbrk(linebuf, "\r\n#")) != NULL)
- {
- *p = '\0';
- }
-
- /* Trim leading whitespace */
- p = linebuf;
- while (*p && ijb_isspace(*p))
- {
- *p++;
- }
-
- if (*p)
- {
- /* There is something other than whitespace on the line. */
-
- /* Move the data to the start of buf */
- if (p != linebuf)
- {
- /* strcpy that can cope with overlap. */
- q = linebuf;
- while ((*q++ = *p++) != '\0')
- {
- /* Do nothing */
- }
- }
-
- /* Trim trailing whitespace */
- p = linebuf + strlen(linebuf) - 1;
-
- /*
- * Note: the (p >= retval) below is paranoia, it's not really needed.
- * When p == retval then ijb_isspace(*p) will be false and we'll drop
- * out of the loop.
- */
- while ((p >= linebuf) && ijb_isspace(*p))
- {
- p--;
- }
- p[1] = '\0';
-
- /* More paranoia. This if statement is always true. */
- if (*linebuf)
- {
- strcpy(buf, linebuf);
- return buf;
- }
- }
- }
-
- /* EOF */
- return NULL;
-}
-
-
-#ifdef ACL_FILES
-/*********************************************************************
- *
- * Function : load_aclfile
- *
- * Description : Read and parse an aclfile and add to files list.
- *
- * Parameters :
- * 1 : csp = Current client state (buffers, headers, etc...)
- *
- * Returns : 0 => Ok, everything else is an error.
- *
- *********************************************************************/
-int load_aclfile(struct client_state *csp)
-{
- FILE *fp;
- char buf[BUFSIZ], *v[3], *p;
- int i;
- struct access_control_list *a, *bl;
- struct file_list *fs;
-
- if (!check_file_changed(current_aclfile, aclfile, &fs))
- {
- /* No need to load */
- if (csp)
- {
- csp->alist = current_aclfile;
- }
- return(0);
- }
- if (!fs)
- {
- goto load_aclfile_error;
- }
-
- fs->f = bl = (struct access_control_list *)zalloc(sizeof(*bl));
- if (bl == NULL)
- {
- freez(fs->filename);
- freez(fs);
- goto load_aclfile_error;
- }
-
- fp = fopen(aclfile, "r");
-
- if (fp == NULL)
- {
- goto load_aclfile_error;
- }
-
- while (read_config_line(buf, sizeof(buf), fp, fs) != NULL)
- {
- i = ssplit(buf, " \t", v, SZ(v), 1, 1);
-
- /* allocate a new node */
- a = (struct access_control_list *) zalloc(sizeof(*a));
-
- if (a == NULL)
- {
- fclose(fp);
- freez(fs->f);
- freez(fs->filename);
- freez(fs);
- goto load_aclfile_error;
- }
-
- /* add it to the list */
- a->next = bl->next;
- bl->next = a;
-
- switch (i)
- {
- case 3:
- if (acl_addr(v[2], a->dst) < 0)
- {
- goto load_aclfile_error;
- }
- /* no break */
-
- case 2:
- if (acl_addr(v[1], a->src) < 0)
- {
- goto load_aclfile_error;
- }
-
- p = v[0];
- if (strcmpic(p, "permit") == 0)
- {
- a->action = ACL_PERMIT;
- break;
- }
-
- if (strcmpic(p, "deny") == 0)
- {
- a->action = ACL_DENY;
- break;
- }
- /* no break */
-
- default:
- goto load_aclfile_error;
- }
- }
-
- fclose(fp);
-
-#ifndef SPLIT_PROXY_ARGS
- if (!suppress_blocklists)
- {
- fs->proxy_args = strsav(fs->proxy_args, "</pre>");
- }
-#endif /* ndef SPLIT_PROXY_ARGS */
-
- if (current_aclfile)
- {
- current_aclfile->unloader = unload_aclfile;
- }
-
- fs->next = files->next;
- files->next = fs;
- current_aclfile = fs;
-
- if (csp)
- {
- csp->alist = fs;
- }
-
- return(0);
-
-load_aclfile_error:
- log_error(LOG_LEVEL_ERROR, "can't load access control list %s: %E", aclfile);
- return(-1);
-
-}
-#endif /* def ACL_FILES */
-
-
-/*********************************************************************
- *
- * Function : load_blockfile
- *
- * Description : Read and parse a blockfile and add to files list.
- *
- * Parameters :
- * 1 : csp = Current client state (buffers, headers, etc...)
- *
- * Returns : 0 => Ok, everything else is an error.
- *
- *********************************************************************/
-int load_blockfile(struct client_state *csp)
-{
- FILE *fp;
-
- struct block_spec *b, *bl;
- char buf[BUFSIZ], *p, *q;
- int port, reject;
- struct file_list *fs;
- struct url_spec url[1];
-
- if (!check_file_changed(current_blockfile, blockfile, &fs))
- {
- /* No need to load */
- if (csp)
- {
- csp->blist = current_blockfile;
- }
- return(0);
- }
- if (!fs)
- {
- goto load_blockfile_error;
- }
-
- fs->f = bl = (struct block_spec *) zalloc(sizeof(*bl));
- if (bl == NULL)
- {
- goto load_blockfile_error;
- }
-
- if ((fp = fopen(blockfile, "r")) == NULL)
- {
- goto load_blockfile_error;
- }
-
- while (read_config_line(buf, sizeof(buf), fp, fs) != NULL)
- {
- reject = 1;
-
- if (*buf == '~')
- {
- reject = 0;
- p = buf;
- q = p+1;
- while ((*p++ = *q++))
- {
- /* nop */
- }
- }
-
- /* skip lines containing only ~ */
- if (*buf == '\0')
- {
- continue;
- }
-
- /* allocate a new node */
- if (((b = zalloc(sizeof(*b))) == NULL)
-#ifdef REGEX
- || ((b->url->preg = zalloc(sizeof(*b->url->preg))) == NULL)
-#endif
- )
- {
- fclose(fp);
- goto load_blockfile_error;
- }
-
- /* add it to the list */
- b->next = bl->next;
- bl->next = b;
-
- /* save a copy of the orignal specification */
- if ((b->url->spec = strdup(buf)) == NULL)
- {
- fclose(fp);
- goto load_blockfile_error;
- }
-
- b->reject = reject;
-
- if ((p = strchr(buf, '/')))
- {
- b->url->path = strdup(p);
- b->url->pathlen = strlen(b->url->path);
- *p = '\0';
- }
- else
- {
- b->url->path = NULL;
- b->url->pathlen = 0;
- }
-#ifdef REGEX
- if (b->url->path)
- {
- int errcode;
- char rebuf[BUFSIZ];
-
- sprintf(rebuf, "^(%s)", b->url->path);
-
- errcode = regcomp(b->url->preg, rebuf,
- (REG_EXTENDED|REG_NOSUB|REG_ICASE));
-
- if (errcode)
- {
- size_t errlen =
- regerror(errcode,
- b->url->preg, buf, sizeof(buf));
-
- buf[errlen] = '\0';
-
- log_error(LOG_LEVEL_ERROR, "error compiling %s: %s\n",
- b->url->spec, buf);
- fclose(fp);
- goto load_blockfile_error;
- }
- }
- else
- {
- freez(b->url->preg);
- }
-#endif
- if ((p = strchr(buf, ':')) == NULL)
- {
- port = 0;
- }
- else
- {
- *p++ = '\0';
- port = atoi(p);
- }
-
- b->url->port = port;
-
- if ((b->url->domain = strdup(buf)) == NULL)
- {
- fclose(fp);
- goto load_blockfile_error;
- }
-
- /* split domain into components */
- *url = dsplit(b->url->domain);
- b->url->dbuf = url->dbuf;
- b->url->dcnt = url->dcnt;
- b->url->dvec = url->dvec;
- }
-
- fclose(fp);
-
-#ifndef SPLIT_PROXY_ARGS
- if (!suppress_blocklists)
- {
- fs->proxy_args = strsav(fs->proxy_args, "</pre>");
- }
-#endif /* ndef SPLIT_PROXY_ARGS */
-
- /* the old one is now obsolete */
- if (current_blockfile)
- {
- current_blockfile->unloader = unload_blockfile;
- }
-
- fs->next = files->next;
- files->next = fs;
- current_blockfile = fs;
-
- if (csp)
- {
- csp->blist = fs;
- }
-
- return(0);
-
-load_blockfile_error:
- log_error(LOG_LEVEL_ERROR, "can't load blockfile '%s': %E", blockfile);
- return(-1);
-
-}
-
-
-#ifdef USE_IMAGE_LIST
-/*********************************************************************
- *
- * Function : load_imagefile
- *
- * Description : Read and parse an imagefile and add to files list.
- *
- * Parameters :
- * 1 : csp = Current client state (buffers, headers, etc...)
- *
- * Returns : 0 => Ok, everything else is an error.
- *
- *********************************************************************/
-int load_imagefile(struct client_state *csp)
-{
- FILE *fp;
-
- struct block_spec *b, *bl;
- char buf[BUFSIZ], *p, *q;
- int port, reject;
- struct file_list *fs;
- struct url_spec url[1];
-
- if (!check_file_changed(current_imagefile, imagefile, &fs))
- {
- /* No need to load */
- if (csp)
- {
- csp->ilist = current_imagefile;
- }
- return(0);
- }
- if (!fs)
- {
- goto load_imagefile_error;
- }
-
- fs->f = bl = (struct block_spec *)zalloc(sizeof(*bl));
- if (bl == NULL)
- {
- goto load_imagefile_error;
- }
-
- if ((fp = fopen(imagefile, "r")) == NULL)
- {
- goto load_imagefile_error;
- }
-
- while (read_config_line(buf, sizeof(buf), fp, fs) != NULL)
- {
- reject = 1;
-
- if (*buf == '~')
- {
- reject = 0;
- p = buf;
- q = p+1;
- while ((*p++ = *q++))
- {
- /* nop */
- }
- }
-
- /* skip lines containing only ~ */
- if (*buf == '\0')
- {
- continue;
- }
-
- /* allocate a new node */
- if (((b = zalloc(sizeof(*b))) == NULL)
-#ifdef REGEX
- || ((b->url->preg = zalloc(sizeof(*b->url->preg))) == NULL)
-#endif
- )
- {
- fclose(fp);
- goto load_imagefile_error;
- }
-
- /* add it to the list */
- b->next = bl->next;
- bl->next = b;
-
- /* save a copy of the orignal specification */
- if ((b->url->spec = strdup(buf)) == NULL)
- {
- fclose(fp);
- goto load_imagefile_error;
- }
-
- b->reject = reject;
-
- if ((p = strchr(buf, '/')))
- {
- b->url->path = strdup(p);
- b->url->pathlen = strlen(b->url->path);
- *p = '\0';
- }
- else
- {
- b->url->path = NULL;
- b->url->pathlen = 0;
- }
-#ifdef REGEX
- if (b->url->path)
- {
- int errcode;
- char rebuf[BUFSIZ];
-
- sprintf(rebuf, "^(%s)", b->url->path);
-
- errcode = regcomp(b->url->preg, rebuf,
- (REG_EXTENDED|REG_NOSUB|REG_ICASE));
-
- if (errcode)
- {
- size_t errlen =
- regerror(errcode,
- b->url->preg, buf, sizeof(buf));
-
- buf[errlen] = '\0';
-
- log_error(LOG_LEVEL_ERROR, "error compiling %s: %s",
- b->url->spec, buf);
- fclose(fp);
- goto load_imagefile_error;
- }
- }
- else
- {
- freez(b->url->preg);
- }
-#endif
- if ((p = strchr(buf, ':')) == NULL)
- {
- port = 0;
- }
- else
- {
- *p++ = '\0';
- port = atoi(p);
- }
-
- b->url->port = port;
-
- if ((b->url->domain = strdup(buf)) == NULL)
- {
- fclose(fp);
- goto load_imagefile_error;
- }
-
- /* split domain into components */
- *url = dsplit(b->url->domain);
- b->url->dbuf = url->dbuf;
- b->url->dcnt = url->dcnt;
- b->url->dvec = url->dvec;
- }
-#ifndef SPLIT_PROXY_ARGS
- if (!suppress_blocklists)
- fs->proxy_args = strsav(fs->proxy_args, "</pre>");
-#endif /* ndef SPLIT_PROXY_ARGS */
-
- fclose(fp);
-
-#ifndef SPLIT_PROXY_ARGS
- if (!suppress_blocklists)
- {
- fs->proxy_args = strsav(fs->proxy_args, "</pre>");
- }
-#endif /* ndef SPLIT_PROXY_ARGS */
-
- /* the old one is now obsolete */
- if (current_imagefile)
- {
- current_imagefile->unloader = unload_imagefile;
- }
-
- fs->next = files->next;
- files->next = fs;
- current_imagefile = fs;
-
- if (csp)
- {
- csp->ilist = fs;
- }
-
- return(0);
-
-load_imagefile_error:
- log_error(LOG_LEVEL_ERROR, "can't load imagefile '%s': %E", imagefile);
- return(-1);
-
-}
-#endif /* def USE_IMAGE_LIST */
-
-
-/*********************************************************************
- *
- * Function : load_cookiefile
- *
- * Description : Read and parse a cookiefile and add to files list.
- *
- * Parameters :
- * 1 : csp = Current client state (buffers, headers, etc...)
- *
- * Returns : 0 => Ok, everything else is an error.
- *
- *********************************************************************/
-int load_cookiefile(struct client_state *csp)
-{
- FILE *fp;
-
- struct cookie_spec *b, *bl;
- char buf[BUFSIZ], *p, *q;
- int port, user_cookie, server_cookie;
- struct file_list *fs;
- struct url_spec url[1];
-
- if (!check_file_changed(current_cookiefile, cookiefile, &fs))
- {
- /* No need to load */
- if (csp)
- {
- csp->clist = current_cookiefile;
- }
- return(0);
- }
- if (!fs)
- {
- goto load_cookie_error;
- }
-
- fs->f = bl = (struct cookie_spec *)zalloc(sizeof(*bl));
- if (bl == NULL)
- {
- goto load_cookie_error;
- }
-
- if ((fp = fopen(cookiefile, "r")) == NULL)
- {
- goto load_cookie_error;
- }
-
- while (read_config_line(buf, sizeof(buf), fp, fs) != NULL)
- {
- p = buf;
-
- switch ((int)*p)
- {
- case '>':
- server_cookie = 0;
- user_cookie = 1;
- p++;
- break;
-
- case '<':
- server_cookie = 1;
- user_cookie = 0;
- p++;
- break;
-
- case '~':
- server_cookie = 0;
- user_cookie = 0;
- p++;
- break;
-
- default:
- server_cookie = 1;
- user_cookie = 1;
- break;
- }
-
- /*
- * Elide any of the "special" chars from the
- * front of the pattern
- */
- q = buf;
- if (p > q) while ((*q++ = *p++))
- {
- /* nop */
- }
-
- /* skip lines containing only "special" chars */
- if (*buf == '\0')
- {
- continue;
- }
-
- /* allocate a new node */
- if (((b = zalloc(sizeof(*b))) == NULL)
-#ifdef REGEX
- || ((b->url->preg = zalloc(sizeof(*b->url->preg))) == NULL)
-#endif
- )
- {
- fclose(fp);
- goto load_cookie_error;
- }
-
- /* add it to the list */
- b->next = bl->next;
- bl->next = b;
-
- /* save a copy of the orignal specification */
- if ((b->url->spec = strdup(buf)) == NULL)
- {
- fclose(fp);
- goto load_cookie_error;
- }
-
- b->send_user_cookie = user_cookie;
- b->accept_server_cookie = server_cookie;
-
- if ((p = strchr(buf, '/')))
- {
- b->url->path = strdup(p);
- b->url->pathlen = strlen(b->url->path);
- *p = '\0';
- }
- else
- {
- b->url->path = NULL;
- b->url->pathlen = 0;
- }
-#ifdef REGEX
- if (b->url->path)
- {
- int errcode;
- char rebuf[BUFSIZ];
-
- sprintf(rebuf, "^(%s)", b->url->path);
-
- errcode = regcomp(b->url->preg, rebuf,
- (REG_EXTENDED|REG_NOSUB|REG_ICASE));
- if (errcode)
- {
- size_t errlen =
- regerror(errcode,
- b->url->preg, buf, sizeof(buf));
-
- buf[errlen] = '\0';
-
- log_error(LOG_LEVEL_ERROR, "error compiling %s: %s",
- b->url->spec, buf);
- fclose(fp);
- goto load_cookie_error;
- }
- }
- else
- {
- freez(b->url->preg);
- }
-#endif
- if ((p = strchr(buf, ':')) == NULL)
- {
- port = 0;
- }
- else
- {
- *p++ = '\0';
- port = atoi(p);
- }
-
- b->url->port = port;
-
- if ((b->url->domain = strdup(buf)) == NULL)
- {
- fclose(fp);
- goto load_cookie_error;
- }
-
- /* split domain into components */
-
- *url = dsplit(b->url->domain);
- b->url->dbuf = url->dbuf;
- b->url->dcnt = url->dcnt;
- b->url->dvec = url->dvec;
- }