Let parse_header_time() return a jb_err code
[privoxy.git] / cgiedit.c
index d4f66c6..60edc89 100644 (file)
--- a/cgiedit.c
+++ b/cgiedit.c
@@ -1,4 +1,4 @@
-const char cgiedit_rcs[] = "$Id: cgiedit.c,v 1.45 2006/12/21 12:57:48 fabiankeil Exp $";
+const char cgiedit_rcs[] = "$Id: cgiedit.c,v 1.48 2007/02/13 14:35:25 fabiankeil Exp $";
 /*********************************************************************
  *
  * File        :  $Source: /cvsroot/ijbswa/current/cgiedit.c,v $
@@ -15,7 +15,7 @@ const char cgiedit_rcs[] = "$Id: cgiedit.c,v 1.45 2006/12/21 12:57:48 fabiankeil
  *
  *                Stick to the short names in this file for consistency.
  *
- * Copyright   :  Written by and Copyright (C) 2001 the SourceForge
+ * Copyright   :  Written by and Copyright (C) 2001-2007 the SourceForge
  *                Privoxy team. http://www.privoxy.org/
  *
  *                Based on the Internet Junkbuster originally written
@@ -42,6 +42,16 @@ const char cgiedit_rcs[] = "$Id: cgiedit.c,v 1.45 2006/12/21 12:57:48 fabiankeil
  *
  * Revisions   :
  *    $Log: cgiedit.c,v $
+ *    Revision 1.48  2007/02/13 14:35:25  fabiankeil
+ *    Replace hash escaping code to prevent
+ *    crashes, memory and file corruption.
+ *
+ *    Revision 1.47  2006/12/28 18:04:25  fabiankeil
+ *    Fixed gcc43 conversion warnings.
+ *
+ *    Revision 1.46  2006/12/27 18:44:52  fabiankeil
+ *    Stop shadowing string.h's index().
+ *
  *    Revision 1.45  2006/12/21 12:57:48  fabiankeil
  *    Add config option "split-large-forms"
  *    to work around the browser bug reported
@@ -995,7 +1005,7 @@ jb_err edit_write_file(struct editable_file * file)
             {
                /* Must quote '#' characters */
                int numhash = 0;
-               int len;
+               size_t len;
                char * src;
                char * dest;
                char * str;
@@ -1010,29 +1020,44 @@ jb_err edit_write_file(struct editable_file * file)
                assert(numhash > 0);
 
                /* Allocate new memory for string */
-               len = strlen(cur_line->unprocessed);
-               if (NULL == (str = malloc((size_t) len + 1 + numhash)))
+               len = strlen(cur_line->unprocessed) + (size_t)numhash;
+               if (NULL == (str = malloc(len + 1)))
                {
                   /* Uh oh, just trashed file! */
                   fclose(fp);
                   return JB_ERR_MEMORY;
                }
 
-               /* Loop through string from end */
-               src  = cur_line->unprocessed + len;
-               dest = str + len + numhash;
-               for ( ; len >= 0; len--)
+               /* Copy string but quote hashes */
+               src  = cur_line->unprocessed;
+               dest = str;
+               while (*src)
                {
-                  if ((*dest-- = *src--) == '#')
+                  if (*src == '#')
                   {
-                     *dest-- = '\\';
+                     *dest++ = '\\';
                      numhash--;
                      assert(numhash >= 0);
                   }
+                  *dest++ = *src++;
                }
+               *dest = '\0';
+
                assert(numhash == 0);
-               assert(src  + 1 == cur_line->unprocessed);
-               assert(dest + 1 == str);
+               assert(strlen(str) == len);
+               assert(str == dest - len);
+               assert(src - len <= cur_line->unprocessed);
+
+               if ((strlen(str) != len) || (numhash != 0))
+               {
+                  /*
+                   * Escaping didn't work as expected, go spread the news.
+                   * Only reached in non-debugging builds.
+                   */
+                  log_error(LOG_LEVEL_ERROR,
+                     "Looks like hash escaping failed. %s might be corrupted now.",
+                     file->filename);
+               }
 
                if (fputs(str, fp) < 0)
                {
@@ -1293,7 +1318,7 @@ static jb_err split_line_on_equals(const char * line, char ** pname, char ** pva
       name_end--;
    }
 
-   name_len = name_end - line + 1; /* Length excluding \0 */
+   name_len = (size_t)(name_end - line) + 1; /* Length excluding \0 */
    if (NULL == (*pname = (char *) malloc(name_len + 1)))
    {
       return JB_ERR_MEMORY;
@@ -1989,7 +2014,7 @@ static jb_err get_file_name_param(struct client_state *csp,
    char *name;
    char *fullpath;
    char ch;
-   int len;
+   size_t len;
 
    assert(csp);
    assert(parameters);
@@ -2246,7 +2271,7 @@ static jb_err map_radio(struct map * exports,
       }
    }
 
-   *p = value;
+   *p = (char)value;
    return map(exports, buf, 0, "checked", 1);
 }
 
@@ -3085,10 +3110,20 @@ jb_err cgi_edit_actions_for_url(struct client_state *csp,
    }
 
    if (0 == have_filters)
+   {
       err = map(exports, "filter-params", 1, "", 1);
+   }
    else
    {
-      /* We have some entries in the filter list */
+      /*
+       * List available filters and their settings.
+       *
+       * XXX: Different types of filters should use different
+       * @substitution strings@. They are currently listed
+       * in the order they appear in the filter files and
+       * are all inserted as @filter-params@ which can lead
+       * to an "interesting" mix. 
+       */
       char * result;
       int filter_identifier = 0;
       char * filter_template;
@@ -3121,8 +3156,37 @@ jb_err cgi_edit_actions_for_url(struct client_state *csp,
                char * this_line;
                struct map *line_exports;
                char number[20];
+               int multi_action_index = 0;
+               char *filter_type;
+               char *abbr_filter_type;
+
+               switch (filter_group->type)
+               {
+                  case FT_CONTENT_FILTER:
+                     /* XXX: Should we call it content-filter instead? */
+                     filter_type = "filter"; 
+                     abbr_filter_type = "F";
+                     multi_action_index = ACTION_MULTI_FILTER;
+                     break;
+                  case FT_SERVER_HEADER_FILTER:
+                     filter_type = "server-header-filter"; 
+                     abbr_filter_type = "S"; 
+                     multi_action_index = ACTION_MULTI_SERVER_HEADER_FILTER;
+                     break;
+                  case FT_CLIENT_HEADER_FILTER:
+                     filter_type = "client-header-filter"; 
+                     abbr_filter_type = "C"; 
+                     multi_action_index = ACTION_MULTI_CLIENT_HEADER_FILTER;
+                     break;
+                  default:
+                     log_error(LOG_LEVEL_FATAL,
+                        "cgi_edit_actions_for_url: Unknown filter type: %u for filter %s.",
+                        filter_group->type, filter_group->name);
+                     /* Not reached. */
+               }
+               assert(multi_action_index);
 
-               filter_name = cur_line->data.action->multi_add[ACTION_MULTI_FILTER]->first;
+               filter_name = cur_line->data.action->multi_add[multi_action_index]->first;
                while ((filter_name != NULL)
                    && (0 != strcmp(filter_group->name, filter_name->str)))
                {
@@ -3135,7 +3199,7 @@ jb_err cgi_edit_actions_for_url(struct client_state *csp,
                }
                else
                {
-                  filter_name = cur_line->data.action->multi_remove[ACTION_MULTI_FILTER]->first;
+                  filter_name = cur_line->data.action->multi_remove[multi_action_index]->first;
                   while ((filter_name != NULL)
                       && (0 != strcmp(filter_group->name, filter_name->str)))
                   {
@@ -3163,6 +3227,8 @@ jb_err cgi_edit_actions_for_url(struct client_state *csp,
                   if (!err) err = map(line_exports, "name",  1, filter_group->name, 1);
                   if (!err) err = map(line_exports, "description",  1, filter_group->description, 1);
                   if (!err) err = map_radio(line_exports, "this-filter", "ynx", current_mode);
+                  if (!err) err = map(line_exports, "filter-type", 1, filter_type, 1);
+                  if (!err) err = map(line_exports, "abbr-filter-type", 1, abbr_filter_type, 1);
 
                   this_line = NULL;
                   if (!err)
@@ -3328,14 +3394,25 @@ jb_err cgi_edit_actions_submit(struct client_state *csp,
    {
       char key_value[30];
       char key_name[30];
+      char key_type[30];
       const char *name;
-      char value;
+      char value; /*
+                   * Filter state. Valid states are: 'Y' (active),
+                   * 'N' (inactive) and 'X' (no change).
+                   * XXX: bad name.
+                   */
+      char type;  /*
+                   * Abbreviated filter type. Valid types are: 'F' (content filter),
+                   * 'S' (server-header filter) and 'C' (client-header filter).
+                   */
+      int multi_action_index = 0;
 
       /* Generate the keys */
       snprintf(key_value, sizeof(key_value), "filter_r%x", filter_identifier);
-      key_value[sizeof(key_value) - 1] = '\0';
+      key_value[sizeof(key_value) - 1] = '\0'; /* XXX: Why? */
       snprintf(key_name, sizeof(key_name), "filter_n%x", filter_identifier);
-      key_name[sizeof(key_name) - 1] = '\0';
+      key_name[sizeof(key_name) - 1] = '\0'; /* XXX: Why? */
+      snprintf(key_type, sizeof(key_type), "filter_t%x", filter_identifier);
 
       err = get_string_param(parameters, key_name, &name);
       if (err) break;
@@ -3346,26 +3423,45 @@ jb_err cgi_edit_actions_submit(struct client_state *csp,
          break;
       }
 
+      type = get_char_param(parameters, key_type);
+      switch (type)
+      {
+         case 'F':
+            multi_action_index = ACTION_MULTI_FILTER;
+            break;
+         case 'S':
+            multi_action_index = ACTION_MULTI_SERVER_HEADER_FILTER;
+            break;
+         case 'C':
+            multi_action_index = ACTION_MULTI_CLIENT_HEADER_FILTER;
+            break;
+         default:
+            log_error(LOG_LEVEL_ERROR,
+               "Unknown filter type: %c for filter %s. Filter ignored.", type, name);
+            continue;
+      }
+      assert(multi_action_index);
+
       value = get_char_param(parameters, key_value);
       if (value == 'Y')
       {
-         list_remove_item(cur_line->data.action->multi_add[ACTION_MULTI_FILTER], name);
-         if (!err) err = enlist(cur_line->data.action->multi_add[ACTION_MULTI_FILTER], name);
-         list_remove_item(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER], name);
+         list_remove_item(cur_line->data.action->multi_add[multi_action_index], name);
+         if (!err) err = enlist(cur_line->data.action->multi_add[multi_action_index], name);
+         list_remove_item(cur_line->data.action->multi_remove[multi_action_index], name);
       }
       else if (value == 'N')
       {
-         list_remove_item(cur_line->data.action->multi_add[ACTION_MULTI_FILTER], name);
-         if (!cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER])
+         list_remove_item(cur_line->data.action->multi_add[multi_action_index], name);
+         if (!cur_line->data.action->multi_remove_all[multi_action_index])
          {
-            list_remove_item(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER], name);
-            if (!err) err = enlist(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER], name);
+            list_remove_item(cur_line->data.action->multi_remove[multi_action_index], name);
+            if (!err) err = enlist(cur_line->data.action->multi_remove[multi_action_index], name);
          }
       }
       else if (value == 'X')
       {
-         list_remove_item(cur_line->data.action->multi_add[ACTION_MULTI_FILTER], name);
-         list_remove_item(cur_line->data.action->multi_remove[ACTION_MULTI_FILTER], name);
+         list_remove_item(cur_line->data.action->multi_add[multi_action_index], name);
+         list_remove_item(cur_line->data.action->multi_remove[multi_action_index], name);
       }
    }