Some updates regarding header filtering,
[privoxy.git] / cgiedit.c
index 5cd0fb3..fb268c2 100644 (file)
--- a/cgiedit.c
+++ b/cgiedit.c
@@ -1,4 +1,4 @@
-const char cgiedit_rcs[] = "$Id: cgiedit.c,v 1.43 2006/07/18 14:48:45 david__schmidt Exp $";
+const char cgiedit_rcs[] = "$Id: cgiedit.c,v 1.49 2007/03/20 15:16:34 fabiankeil Exp $";
 /*********************************************************************
  *
  * File        :  $Source: /cvsroot/ijbswa/current/cgiedit.c,v $
@@ -15,7 +15,7 @@ const char cgiedit_rcs[] = "$Id: cgiedit.c,v 1.43 2006/07/18 14:48:45 david__sch
  *
  *                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,30 @@ const char cgiedit_rcs[] = "$Id: cgiedit.c,v 1.43 2006/07/18 14:48:45 david__sch
  *
  * Revisions   :
  *    $Log: cgiedit.c,v $
+ *    Revision 1.49  2007/03/20 15:16:34  fabiankeil
+ *    Use dedicated header filter actions instead of abusing "filter".
+ *    Replace "filter-client-headers" and "filter-client-headers"
+ *    with "server-header-filter" and "client-header-filter".
+ *
+ *    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
+ *    in BR #1570678.
+ *
+ *    Revision 1.44  2006/12/09 13:49:16  fabiankeil
+ *    Fix configure option --disable-toggle.
+ *    Thanks to Peter Thoenen for reporting this.
+ *
  *    Revision 1.43  2006/07/18 14:48:45  david__schmidt
  *    Reorganizing the repository: swapping out what was HEAD (the old 3.1 branch)
  *    with what was really the latest development (the v_3_0_branch branch)
@@ -986,7 +1010,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;
@@ -1001,29 +1025,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)
                {
@@ -1284,7 +1323,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;
@@ -1980,7 +2019,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);
@@ -2237,7 +2276,7 @@ static jb_err map_radio(struct map * exports,
       }
    }
 
-   *p = value;
+   *p = (char)value;
    return map(exports, buf, 0, "checked", 1);
 }
 
@@ -3029,6 +3068,35 @@ jb_err cgi_edit_actions_for_url(struct client_state *csp,
 
    if (!err) err = actions_to_radio(exports, cur_line->data.action);
 
+   /*
+    * XXX: Some browsers (at least IE6 and IE7) have an artifical URL
+    * length limitation and ignore clicks on the Submit buttons if
+    * the resulting GET URL would be longer than their limit.
+    *
+    * In Privoxy 3.0.5 beta the standard edit-actions-for-url template
+    * reached this limit and action editing stopped working in these
+    * browsers (BR #1570678).
+    *
+    * The config option split-large-forms works around this browser
+    * bug (HTTP has no URL lenght limitation) by deviding the action
+    * list form into multiple smaller ones. It means the URLs are shorter
+    * and work in broken browsers as well, but the user can no longer change
+    * all actions with one submit.
+    *
+    * A better solution would be to switch to POST requests,
+    * but this will do for now.
+    */
+   if(!err && (csp->config->feature_flags & RUNTIME_FEATURE_SPLIT_LARGE_FORMS))
+   {
+      /* Generate multiple smaller form by killing the big one. */
+      err = map_block_killer(exports, "one-form-only");
+   }
+   else
+   {
+      /* Default: Generate one large form by killing the smaller ones. */
+      err = map_block_killer(exports, "multiple-forms");
+   }
+
    for (i = 0; i < MAX_AF_FILES; i++)
    {
       if ((csp->rlist[i] != NULL) && (csp->rlist[i]->f != NULL))
@@ -3047,13 +3115,19 @@ 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 */
-      char * result;
-      int index = 0;
-      char * filter_template;
+      /*
+       * List available filters and their settings.
+       */
+      char *content_filter_params;
+      char *server_header_filter_params;
+      char *client_header_filter_params;
+      char *filter_template;
+      int filter_identifier = 0;
 
       err = template_load(csp, &filter_template, "edit-actions-for-url-filter", 0);
       if (err)
@@ -3069,7 +3143,9 @@ jb_err cgi_edit_actions_for_url(struct client_state *csp,
 
       err = template_fill(&filter_template, exports);
 
-      result = strdup("");
+      content_filter_params = strdup("");
+      server_header_filter_params = strdup("");
+      client_header_filter_params = strdup("");
 
       for (i = 0; i < MAX_AF_FILES; i++)
       {
@@ -3079,12 +3155,49 @@ jb_err cgi_edit_actions_for_url(struct client_state *csp,
             for (;(!err) && (filter_group != NULL); filter_group = filter_group->next)
             {
                char current_mode = 'x';
+               char number[20];
+               int multi_action_index = 0;
                struct list_entry *filter_name;
-               char * this_line;
                struct map *line_exports;
-               char number[20];
+               char *this_line;
+               char *filter_type;
+               char *abbr_filter_type;
+               char *anchor;
+               char **current_params;
 
-               filter_name = cur_line->data.action->multi_add[ACTION_MULTI_FILTER]->first;
+               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;
+                     anchor = "FILTER";
+                     current_params = &content_filter_params;
+                     break;
+                  case FT_SERVER_HEADER_FILTER:
+                     filter_type = "server-header-filter"; 
+                     abbr_filter_type = "S"; 
+                     multi_action_index = ACTION_MULTI_SERVER_HEADER_FILTER;
+                     current_params = &server_header_filter_params;
+                     anchor = "SERVER-HEADER-FILTER"; /* XXX: no documentation available yet */
+                     break;
+                  case FT_CLIENT_HEADER_FILTER:
+                     filter_type = "client-header-filter"; 
+                     abbr_filter_type = "C"; 
+                     multi_action_index = ACTION_MULTI_CLIENT_HEADER_FILTER;
+                     current_params = &client_header_filter_params;
+                     anchor = "CLIENT-HEADER-FILTER"; /* XXX: no documentation available yet */
+                     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[multi_action_index]->first;
                while ((filter_name != NULL)
                    && (0 != strcmp(filter_group->name, filter_name->str)))
                {
@@ -3097,7 +3210,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)))
                   {
@@ -3110,14 +3223,14 @@ jb_err cgi_edit_actions_for_url(struct client_state *csp,
                }
 
                /* Generate a unique serial number */
-               snprintf(number, sizeof(number), "%x", index++);
+               snprintf(number, sizeof(number), "%x", filter_identifier++);
                number[sizeof(number) - 1] = '\0';
 
                line_exports = new_map();
                if (line_exports == NULL)
                {
                   err = JB_ERR_MEMORY;
-                  freez(result);
+                  freez(*current_params); /* XXX: really necessary? */
                }
                else
                {
@@ -3125,6 +3238,9 @@ 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);
+                  if (!err) err = map(line_exports, "anchor", 1, anchor, 1);
 
                   this_line = NULL;
                   if (!err)
@@ -3133,7 +3249,7 @@ jb_err cgi_edit_actions_for_url(struct client_state *csp,
                      if (this_line == NULL) err = JB_ERR_MEMORY;
                   }
                   if (!err) err = template_fill(&this_line, line_exports);
-                  string_join(&result, this_line);
+                  string_join(current_params, this_line);
 
                   free_map(line_exports);
                }
@@ -3142,13 +3258,14 @@ jb_err cgi_edit_actions_for_url(struct client_state *csp,
       }
       freez(filter_template);
 
-      if (!err)
-      {
-         err = map(exports, "filter-params", 1, result, 0);
-      }
-      else
+      if (!err) err = map(exports, "content-filter-params", 1, content_filter_params, 0);
+      if (!err) err = map(exports, "server-header-filter-params", 1, server_header_filter_params, 0);
+      if (!err) err = map(exports, "client-header-filter-params", 1, client_header_filter_params, 0);
+      if (err)
       {
-         freez(result);
+         freez(content_filter_params);
+         freez(server_header_filter_params);
+         freez(client_header_filter_params);
       }
    }
 
@@ -3199,7 +3316,7 @@ jb_err cgi_edit_actions_submit(struct client_state *csp,
    unsigned line_number;
    char target[1024];
    jb_err err;
-   int index;
+   int filter_identifier;
    const char * action_set_name;
    char ch;
    struct file_list * fl;
@@ -3243,9 +3360,9 @@ jb_err cgi_edit_actions_submit(struct client_state *csp,
    get_string_param(parameters, "p", &action_set_name);
    if (action_set_name != NULL)
    {
-      for (index = 0; index < MAX_AF_FILES; index++)
+      for (filter_identifier = 0; filter_identifier < MAX_AF_FILES; filter_identifier++)
       {
-         if (((fl = csp->actions_list[index]) != NULL) && ((b = fl->f) != NULL))
+         if (((fl = csp->actions_list[filter_identifier]) != NULL) && ((b = fl->f) != NULL))
          {
             for (b = b->next; NULL != b; b = b->next)
             {
@@ -3286,18 +3403,29 @@ jb_err cgi_edit_actions_submit(struct client_state *csp,
       cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER] = 0;
    }
 
-   for (index = 0; !err; index++)
+   for (filter_identifier = 0; !err; filter_identifier++)
    {
       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", index);
-      key_value[sizeof(key_value) - 1] = '\0';
-      snprintf(key_name, sizeof(key_name), "filter_n%x", index);
-      key_name[sizeof(key_name) - 1] = '\0';
+      snprintf(key_value, sizeof(key_value), "filter_r%x", filter_identifier);
+      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'; /* XXX: Why? */
+      snprintf(key_type, sizeof(key_type), "filter_t%x", filter_identifier);
 
       err = get_string_param(parameters, key_name, &name);
       if (err) break;
@@ -3308,26 +3436,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);
       }
    }