sig_handler(): Split a long line in two
[privoxy.git] / loaders.c
index 0e67da2..61d2763 100644 (file)
--- a/loaders.c
+++ b/loaders.c
@@ -1,4 +1,3 @@
-const char loaders_rcs[] = "$Id: loaders.c,v 1.92 2012/07/23 12:43:56 fabiankeil Exp $";
 /*********************************************************************
  *
  * File        :  $Source: /cvsroot/ijbswa/current/loaders.c,v $
@@ -8,8 +7,8 @@ const char loaders_rcs[] = "$Id: loaders.c,v 1.92 2012/07/23 12:43:56 fabiankeil
  *                the list of active loaders, and to automatically
  *                unload files that are no longer in use.
  *
- * Copyright   :  Written by and Copyright (C) 2001-2012 the
- *                Privoxy team. http://www.privoxy.org/
+ * Copyright   :  Written by and Copyright (C) 2001-2014 the
+ *                Privoxy team. https://www.privoxy.org/
  *
  *                Based on the Internet Junkbuster originally written
  *                by and Copyright (C) 1997 Anonymous Coders and
@@ -47,7 +46,7 @@ const char loaders_rcs[] = "$Id: loaders.c,v 1.92 2012/07/23 12:43:56 fabiankeil
 #include <ctype.h>
 #include <assert.h>
 
-#if !defined(_WIN32) && !defined(__OS2__)
+#if !defined(_WIN32)
 #include <unistd.h>
 #endif
 
@@ -63,8 +62,6 @@ const char loaders_rcs[] = "$Id: loaders.c,v 1.92 2012/07/23 12:43:56 fabiankeil
 #include "urlmatch.h"
 #include "encode.h"
 
-const char loaders_h_rcs[] = LOADERS_H_VERSION;
-
 /*
  * Currently active files.
  * These are also entered in the main linked list of files.
@@ -74,13 +71,57 @@ const char loaders_h_rcs[] = LOADERS_H_VERSION;
 static struct file_list *current_trustfile      = NULL;
 #endif /* def FEATURE_TRUST */
 
+#ifndef FUZZ
 static int load_one_re_filterfile(struct client_state *csp, int fileid);
+#endif
 
 static struct file_list *current_re_filterfile[MAX_AF_FILES]  = {
    NULL, NULL, NULL, NULL, NULL,
    NULL, NULL, NULL, NULL, NULL
 };
 
+/*********************************************************************
+ *
+ * Function    :  free_csp_resources
+ *
+ * Description :  Frees memory referenced by the csp that isn't
+ *                shared with other csps.
+ *
+ * Parameters  :
+ *          1  :  csp = Current client state (buffers, headers, etc...)
+ *
+ * Returns     :  N/A
+ *
+ *********************************************************************/
+void free_csp_resources(struct client_state *csp)
+{
+   freez(csp->ip_addr_str);
+#ifdef FEATURE_CLIENT_TAGS
+   freez(csp->client_address);
+#endif
+   freez(csp->listen_addr_str);
+   freez(csp->client_iob->buf);
+   freez(csp->iob->buf);
+   freez(csp->error_message);
+
+   if (csp->action->flags & ACTION_FORWARD_OVERRIDE &&
+      NULL != csp->fwd)
+   {
+      unload_forward_spec(csp->fwd);
+   }
+   free_http_request(csp->http);
+
+   destroy_list(csp->headers);
+#ifdef FEATURE_HTTPS_INSPECTION
+   destroy_list(csp->https_headers);
+#endif
+   destroy_list(csp->tags);
+#ifdef FEATURE_CLIENT_TAGS
+   destroy_list(csp->client_tags);
+#endif
+
+   free_current_action(csp->action);
+}
 
 /*********************************************************************
  *
@@ -181,23 +222,6 @@ unsigned int sweep(void)
       {
          last_active->next = client_list->next;
 
-         freez(csp->ip_addr_str);
-         freez(csp->client_iob->buf);
-         freez(csp->iob->buf);
-         freez(csp->error_message);
-
-         if (csp->action->flags & ACTION_FORWARD_OVERRIDE &&
-             NULL != csp->fwd)
-         {
-            unload_forward_spec(csp->fwd);
-         }
-         free_http_request(csp->http);
-
-         destroy_list(csp->headers);
-         destroy_list(csp->tags);
-
-         free_current_action(csp->action);
-
 #ifdef FEATURE_STATISTICS
          urls_read++;
          if (csp->flags & CSP_FLAG_REJECTED)
@@ -288,23 +312,10 @@ int check_file_changed(const struct file_list * current,
       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 = zalloc_or_die(sizeof(struct file_list));
+   fs->filename = strdup_or_die(filename);
    fs->lastmodified = statbuf->st_mtime;
 
-   if (fs->filename == NULL)
-   {
-      /* Out of memory error */
-      freez (fs);
-      return 1;
-   }
    *newfl = fs;
    return 1;
 }
@@ -364,6 +375,7 @@ jb_err simple_read_line(FILE *fp, char **dest, int *newline)
    for (;;)
    {
       ch = getc(fp);
+
       if (ch == EOF)
       {
          if (len > 0)
@@ -420,6 +432,7 @@ jb_err simple_read_line(FILE *fp, char **dest, int *newline)
       }
       else if (ch == 0)
       {
+         /* XXX: Why do we allow this anyway? */
          *p = '\0';
          *dest = buf;
          return JB_ERR_OK;
@@ -537,30 +550,15 @@ jb_err edit_read_line(FILE *fp,
 
    if (raw_out)
    {
-      raw = strdup("");
-      if (NULL == raw)
-      {
-         return JB_ERR_MEMORY;
-      }
+      raw = strdup_or_die("");
    }
    if (prefix_out)
    {
-      prefix = strdup("");
-      if (NULL == prefix)
-      {
-         freez(raw);
-         return JB_ERR_MEMORY;
-      }
+      prefix = strdup_or_die("");
    }
    if (data_out)
    {
-      data = strdup("");
-      if (NULL == data)
-      {
-         freez(raw);
-         freez(prefix);
-         return JB_ERR_MEMORY;
-      }
+      data = strdup_or_die("");
    }
 
    /* Main loop.  Loop while we need more data & it's not EOF. */
@@ -652,15 +650,12 @@ jb_err edit_read_line(FILE *fp,
       if (*linestart)
       {
          is_empty = 0;
-         if (data)
+         if (string_append(&data, linestart))
          {
-            if (string_append(&data, linestart))
-            {
-               freez(raw);
-               freez(prefix);
-               free(linebuf);
-               return JB_ERR_MEMORY;
-            }
+            freez(raw);
+            freez(prefix);
+            free(linebuf);
+            return JB_ERR_MEMORY;
          }
       }
 
@@ -779,7 +774,7 @@ static void unload_trustfile(void *f)
    {
       next = cur->next;
 
-      free_url_spec(cur->url);
+      free_pattern_spec(cur->url);
       free(cur);
 
       cur = next;
@@ -829,7 +824,7 @@ int load_trustfile(struct client_state *csp)
    FILE *fp;
 
    struct block_spec *b, *bl;
-   struct url_spec **tl;
+   struct pattern_spec **tl;
 
    char *buf = NULL;
    int reject, trusted;
@@ -848,11 +843,7 @@ int load_trustfile(struct client_state *csp)
       goto load_trustfile_error;
    }
 
-   fs->f = bl = (struct block_spec *)zalloc(sizeof(*bl));
-   if (bl == NULL)
-   {
-      goto load_trustfile_error;
-   }
+   fs->f = bl = zalloc_or_die(sizeof(*bl));
 
    if ((fp = fopen(csp->config->trustfile, "r")) == NULL)
    {
@@ -895,11 +886,7 @@ int load_trustfile(struct client_state *csp)
       }
 
       /* allocate a new node */
-      if ((b = zalloc(sizeof(*b))) == NULL)
-      {
-         fclose(fp);
-         goto load_trustfile_error;
-      }
+      b = zalloc_or_die(sizeof(*b));
 
       /* add it to the list */
       b->next  = bl->next;
@@ -908,7 +895,7 @@ int load_trustfile(struct client_state *csp)
       b->reject = reject;
 
       /* Save the URL pattern */
-      if (create_url_spec(b->url, buf))
+      if (create_pattern_spec(b->url, buf))
       {
          fclose(fp);
          goto load_trustfile_error;
@@ -1014,9 +1001,11 @@ static void unload_re_filterfile(void *f)
  *********************************************************************/
 void unload_forward_spec(struct forward_spec *fwd)
 {
-   free_url_spec(fwd->url);
+   free_pattern_spec(fwd->url);
    freez(fwd->gateway_host);
    freez(fwd->forward_host);
+   freez(fwd->auth_username);
+   freez(fwd->auth_password);
    free(fwd);
 
    return;
@@ -1116,7 +1105,6 @@ int load_one_re_filterfile(struct client_state *csp, int fileid)
    struct file_list *fs;
 
    char *buf = NULL;
-   int error;
    unsigned long linenum = 0;
    pcrs_job *dummy, *lastjob = NULL;
 
@@ -1170,6 +1158,16 @@ int load_one_re_filterfile(struct client_state *csp, int fileid)
       {
          new_filter = FT_SERVER_HEADER_TAGGER;
       }
+#ifdef FEATURE_EXTERNAL_FILTERS
+      else if (strncmp(buf, "EXTERNAL-FILTER:", 16) == 0)
+      {
+         new_filter = FT_EXTERNAL_CONTENT_FILTER;
+      }
+#endif
+      else if (strncmp(buf, "CLIENT-BODY-FILTER:", 19) == 0)
+      {
+         new_filter = FT_CLIENT_BODY_FILTER;
+      }
 
       /*
        * If this is the head of a new filter block, make it a
@@ -1177,15 +1175,21 @@ int load_one_re_filterfile(struct client_state *csp, int fileid)
        */
       if (new_filter != FT_INVALID_FILTER)
       {
-         new_bl = (struct re_filterfile_spec  *)zalloc(sizeof(*bl));
-         if (new_bl == NULL)
-         {
-            goto load_re_filterfile_error;
-         }
+         new_bl = zalloc_or_die(sizeof(*bl));
          if (new_filter == FT_CONTENT_FILTER)
          {
             new_bl->name = chomp(buf + 7);
          }
+#ifdef FEATURE_EXTERNAL_FILTERS
+         else if (new_filter == FT_EXTERNAL_CONTENT_FILTER)
+         {
+            new_bl->name = chomp(buf + 16);
+         }
+#endif
+         else if (new_filter == FT_CLIENT_BODY_FILTER)
+         {
+            new_bl->name = chomp(buf + 19);
+         }
          else
          {
             new_bl->name = chomp(buf + 21);
@@ -1202,19 +1206,20 @@ int load_one_re_filterfile(struct client_state *csp, int fileid)
             new_bl->description = html_encode(chomp(new_bl->description));
             if (NULL == new_bl->description)
             {
-               new_bl->description = strdup("Out of memory while encoding this filter's description to HTML");
+               new_bl->description = strdup_or_die("Out of memory while "
+                  "encoding filter description to HTML");
             }
          }
          else
          {
-            new_bl->description = strdup("No description available for this filter");
+            new_bl->description = strdup_or_die("No description available");
          }
 
-         new_bl->name = strdup(chomp(new_bl->name));
+         new_bl->name = strdup_or_die(chomp(new_bl->name));
 
          /*
           * If this is the first filter block, chain it
-          * to the file_list rather than its (nonexistant)
+          * to the file_list rather than its (nonexistent)
           * predecessor
           */
          if (fs->f == NULL)
@@ -1229,24 +1234,50 @@ int load_one_re_filterfile(struct client_state *csp, int fileid)
          bl = new_bl;
 
          log_error(LOG_LEVEL_RE_FILTER, "Reading in filter \"%s\" (\"%s\")", bl->name, bl->description);
-
+#ifdef FEATURE_EXTENDED_STATISTICS
+         register_filter_for_statistics(bl->name);
+#endif
          freez(buf);
          continue;
       }
 
-      /*
-       * Else, save the expression, make it a pcrs_job
-       * and chain it into the current filter's joblist
-       */
+#ifdef FEATURE_EXTERNAL_FILTERS
+      if ((bl != NULL) && (bl->type == FT_EXTERNAL_CONTENT_FILTER))
+      {
+         jb_err jb_error;
+         /* Save the code as "pattern", but do not compile anything. */
+         if (bl->patterns->first != NULL)
+         {
+            log_error(LOG_LEVEL_FATAL, "External filter '%s' contains several jobs. "
+               "Did you forget to escape a line break?",
+               bl->name);
+         }
+         jb_error = enlist(bl->patterns, buf);
+         if (JB_ERR_MEMORY == jb_error)
+         {
+            log_error(LOG_LEVEL_FATAL,
+               "Out of memory while enlisting external filter code \'%s\' for filter %s.",
+               buf, bl->name);
+         }
+         freez(buf);
+         continue;
+      }
+#endif
       if (bl != NULL)
       {
-         error = enlist(bl->patterns, buf);
-         if (JB_ERR_MEMORY == error)
+         int pcrs_error;
+         jb_err jb_error;
+         /*
+          * Save the expression, make it a pcrs_job
+          * and chain it into the current filter's joblist
+          */
+         jb_error = enlist(bl->patterns, buf);
+         if (JB_ERR_MEMORY == jb_error)
          {
             log_error(LOG_LEVEL_FATAL,
                "Out of memory while enlisting re_filter job \'%s\' for filter %s.", buf, bl->name);
          }
-         assert(JB_ERR_OK == error);
+         assert(JB_ERR_OK == jb_error);
 
          if (pcrs_job_is_dynamic(buf))
          {
@@ -1278,10 +1309,11 @@ int load_one_re_filterfile(struct client_state *csp, int fileid)
             continue;
          }
 
-         if ((dummy = pcrs_compile_command(buf, &error)) == NULL)
+         if ((dummy = pcrs_compile_command(buf, &pcrs_error)) == NULL)
          {
             log_error(LOG_LEVEL_ERROR,
-               "Adding re_filter job \'%s\' to filter %s failed with error %d.", buf, bl->name, error);
+               "Adding re_filter job \'%s\' to filter %s failed: %s",
+               buf, bl->name, pcrs_strerror(pcrs_error));
             freez(buf);
             continue;
          }
@@ -1301,7 +1333,8 @@ int load_one_re_filterfile(struct client_state *csp, int fileid)
       }
       else
       {
-         log_error(LOG_LEVEL_ERROR, "Ignoring job %s outside filter block in %s, line %d",
+         log_error(LOG_LEVEL_ERROR,
+            "Ignoring job %s outside filter block in %s, line %lu",
             buf, csp->config->re_filterfile[fileid], linenum);
       }
       freez(buf);
@@ -1446,18 +1479,50 @@ static int file_has_been_modified(const char *filename, time_t last_know_modific
  *               FALSE otherwise.
  *
  *********************************************************************/
-int any_loaded_file_changed(const struct file_list *files_to_check)
+int any_loaded_file_changed(const struct client_state *csp)
 {
-   const struct file_list *file_to_check = files_to_check;
+   const struct file_list *file_to_check = csp->config->config_file_list;
+   int i;
+
+   if (file_has_been_modified(file_to_check->filename, file_to_check->lastmodified))
+   {
+      return TRUE;
+   }
 
-   while (file_to_check != NULL)
+   for (i = 0; i < MAX_AF_FILES; i++)
    {
-      if (file_has_been_modified(file_to_check->filename, file_to_check->lastmodified))
+      if (csp->actions_list[i])
+      {
+         file_to_check = csp->actions_list[i];
+         if (file_has_been_modified(file_to_check->filename, file_to_check->lastmodified))
+         {
+            return TRUE;
+         }
+      }
+   }
+
+   for (i = 0; i < MAX_AF_FILES; i++)
+   {
+      if (csp->rlist[i])
+      {
+         file_to_check = csp->rlist[i];
+         if (file_has_been_modified(file_to_check->filename, file_to_check->lastmodified))
+         {
+            return TRUE;
+         }
+      }
+   }
+
+#ifdef FEATURE_TRUST
+   if (csp->tlist)
+   {
+      if (file_has_been_modified(csp->tlist->filename, csp->tlist->lastmodified))
       {
          return TRUE;
       }
-      file_to_check = file_to_check->next;
    }
+#endif /* def FEATURE_TRUST */
+
    return FALSE;
 }