Allow access to Privoxy's CGI pages, don't call trusted
[privoxy.git] / cgiedit.c
index c50ad20..e4f1d68 100644 (file)
--- a/cgiedit.c
+++ b/cgiedit.c
@@ -1,4 +1,4 @@
-const char cgiedit_rcs[] = "$Id: cgiedit.c,v 1.44 2006/12/09 13:49:16 fabiankeil Exp $";
+const char cgiedit_rcs[] = "$Id: cgiedit.c,v 1.47 2006/12/28 18:04:25 fabiankeil Exp $";
 /*********************************************************************
  *
  * File        :  $Source: /cvsroot/ijbswa/current/cgiedit.c,v $
@@ -15,7 +15,7 @@ const char cgiedit_rcs[] = "$Id: cgiedit.c,v 1.44 2006/12/09 13:49:16 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,17 @@ const char cgiedit_rcs[] = "$Id: cgiedit.c,v 1.44 2006/12/09 13:49:16 fabiankeil
  *
  * Revisions   :
  *    $Log: cgiedit.c,v $
+ *    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.
@@ -990,7 +1001,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;
@@ -1005,29 +1016,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)
                {
@@ -1288,7 +1314,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;
@@ -1984,7 +2010,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);
@@ -2241,7 +2267,7 @@ static jb_err map_radio(struct map * exports,
       }
    }
 
-   *p = value;
+   *p = (char)value;
    return map(exports, buf, 0, "checked", 1);
 }
 
@@ -3085,7 +3111,7 @@ jb_err cgi_edit_actions_for_url(struct client_state *csp,
    {
       /* We have some entries in the filter list */
       char * result;
-      int index = 0;
+      int filter_identifier = 0;
       char * filter_template;
 
       err = template_load(csp, &filter_template, "edit-actions-for-url-filter", 0);
@@ -3143,7 +3169,7 @@ 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();
@@ -3232,7 +3258,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;
@@ -3276,9 +3302,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)
             {
@@ -3319,7 +3345,7 @@ 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];
@@ -3327,9 +3353,9 @@ jb_err cgi_edit_actions_submit(struct client_state *csp,
       char value;
 
       /* Generate the keys */
-      snprintf(key_value, sizeof(key_value), "filter_r%x", index);
+      snprintf(key_value, sizeof(key_value), "filter_r%x", filter_identifier);
       key_value[sizeof(key_value) - 1] = '\0';
-      snprintf(key_name, sizeof(key_name), "filter_n%x", index);
+      snprintf(key_name, sizeof(key_name), "filter_n%x", filter_identifier);
       key_name[sizeof(key_name) - 1] = '\0';
 
       err = get_string_param(parameters, key_name, &name);