Blame SOURCES/vsftpd-3.0.3-enable_wc_logs-replace_unprintable_with_hex.patch

11ff82
diff --git a/logging.c b/logging.c
11ff82
index 9e86808..613ff4b 100644
11ff82
--- a/logging.c
11ff82
+++ b/logging.c
11ff82
@@ -171,7 +171,14 @@ vsf_log_do_log_to_file(int fd, struct mystr* p_str)
11ff82
       return;
11ff82
     }
11ff82
   }
11ff82
-  str_replace_unprintable(p_str, '?');
11ff82
+  if (tunable_wc_logs_enable)
11ff82
+  {
11ff82
+    str_replace_unprintable_with_hex_wc(p_str);
11ff82
+  }
11ff82
+  else
11ff82
+  {
11ff82
+    str_replace_unprintable_with_hex(p_str);
11ff82
+  }
11ff82
   str_append_char(p_str, '\n');
11ff82
   /* Ignore write failure; maybe the disk filled etc. */
11ff82
   (void) str_write_loop(p_str, fd);
11ff82
diff --git a/parseconf.c b/parseconf.c
11ff82
index 3cfe7da..3729818 100644
11ff82
--- a/parseconf.c
11ff82
+++ b/parseconf.c
11ff82
@@ -113,6 +113,7 @@ parseconf_bool_array[] =
11ff82
   { "allow_writeable_chroot", &tunable_allow_writeable_chroot },
11ff82
   { "better_stou", &tunable_better_stou },
11ff82
   { "log_die", &tunable_log_die },
11ff82
+  { "wc_logs_enable", &tunable_wc_logs_enable },
11ff82
   { 0, 0 }
11ff82
 };
11ff82
 
11ff82
diff --git a/str.c b/str.c
11ff82
index 82b8ae4..c03e7d8 100644
11ff82
--- a/str.c
11ff82
+++ b/str.c
11ff82
@@ -20,6 +20,11 @@
11ff82
 #include "utility.h"
11ff82
 #include "sysutil.h"
11ff82
 
11ff82
+#include <stdio.h>
11ff82
+#include <string.h>
11ff82
+#include <wchar.h>
11ff82
+#include <wctype.h>
11ff82
+
11ff82
 /* File local functions */
11ff82
 static void str_split_text_common(struct mystr* p_src, struct mystr* p_rhs,
11ff82
                                   const char* p_text, int is_reverse);
11ff82
@@ -723,6 +728,102 @@ str_replace_unprintable(struct mystr* p_str, char new_char)
11ff82
   }
11ff82
 }
11ff82
 
11ff82
+void
11ff82
+str_replace_unprintable_with_hex(struct mystr* p_str)
11ff82
+{
11ff82
+  unsigned int ups_size = sizeof(unsigned int) * (p_str->len);
11ff82
+  if (ups_size < p_str->len)
11ff82
+  {
11ff82
+    str_replace_unprintable(p_str, '?');
11ff82
+    str_append_text(p_str, ": BUG: string is too long");
11ff82
+    bug(p_str->p_buf);
11ff82
+  }
11ff82
+  unsigned int* ups = vsf_sysutil_malloc(ups_size);
11ff82
+  unsigned int up_count = 0;
11ff82
+  for (unsigned int i=0; i < p_str->len; i++)
11ff82
+  {
11ff82
+    if (!vsf_sysutil_isprint(p_str->p_buf[i]))
11ff82
+    {
11ff82
+      ups[up_count++] = i;
11ff82
+    }
11ff82
+  }
11ff82
+  str_replace_positions_with_hex(p_str, ups, up_count);
11ff82
+  vsf_sysutil_free(ups);
11ff82
+}
11ff82
+
11ff82
+void str_replace_unprintable_with_hex_wc(struct mystr* p_str)
11ff82
+{
11ff82
+  unsigned int ups_size = sizeof(unsigned int) * (p_str->len);
11ff82
+  if (ups_size < p_str->len)
11ff82
+  {
11ff82
+    str_replace_unprintable(p_str, '?');
11ff82
+    str_append_text(p_str, ": BUG: string is too long");
11ff82
+    bug(p_str->p_buf);
11ff82
+  }
11ff82
+  unsigned int* ups = vsf_sysutil_malloc(ups_size);
11ff82
+  unsigned int up_count = 0;
11ff82
+
11ff82
+  size_t current = 0;
11ff82
+  wchar_t pwc;
11ff82
+  mbstate_t ps;
11ff82
+  memset(&ps, 0, sizeof(ps));
11ff82
+  ssize_t len = 0;
11ff82
+  while ((len = mbrtowc(&pwc, p_str->p_buf, p_str->len - current, &ps)) > 0)
11ff82
+  {
11ff82
+    if (!iswprint(pwc))
11ff82
+    {
11ff82
+      for (int i = 0; i < len; i++)
11ff82
+      {
11ff82
+        ups[up_count++] = current++;
11ff82
+      }
11ff82
+    }
11ff82
+    else
11ff82
+    {
11ff82
+      current += len;
11ff82
+    }
11ff82
+  }
11ff82
+  if (len < 0)
11ff82
+  {
11ff82
+    while (current < p_str->len)
11ff82
+    {
11ff82
+      ups[up_count++] = current++;
11ff82
+    }
11ff82
+  }
11ff82
+  str_replace_positions_with_hex(p_str, ups, up_count);
11ff82
+  vsf_sysutil_free(ups);
11ff82
+}
11ff82
+
11ff82
+void
11ff82
+str_replace_positions_with_hex(struct mystr* p_str, const unsigned int* poss, const unsigned int pos_count)
11ff82
+{
11ff82
+  if (pos_count == 0)
11ff82
+    return;
11ff82
+
11ff82
+  struct mystr tmp_str = INIT_MYSTR;
11ff82
+  str_reserve(&tmp_str, p_str->len + 3 * pos_count);
11ff82
+  unsigned int current = 0;
11ff82
+
11ff82
+  for (unsigned int i=0; i < pos_count; i++)
11ff82
+  {
11ff82
+    unsigned int pos = poss[i];
11ff82
+
11ff82
+    if (current < pos)
11ff82
+      private_str_append_memchunk(&tmp_str, p_str->p_buf + current, pos - current);
11ff82
+
11ff82
+    char hex_buf[5];
11ff82
+    memset(hex_buf, 0, sizeof(hex_buf));
11ff82
+    sprintf(hex_buf, "\\x%02X", (unsigned char) p_str->p_buf[pos]);
11ff82
+    str_append_text(&tmp_str, hex_buf);
11ff82
+    current = pos + 1;
11ff82
+  }
11ff82
+
11ff82
+  if (current < p_str->len)
11ff82
+    private_str_append_memchunk(&tmp_str, p_str->p_buf + current, p_str->len - current);
11ff82
+
11ff82
+  str_copy(p_str, &tmp_str);
11ff82
+  str_free(&tmp_str);
11ff82
+}
11ff82
+
11ff82
 void
11ff82
 str_basename (struct mystr* d_str, const struct mystr* path)
11ff82
 {
11ff82
diff --git a/str.h b/str.h
11ff82
index 44270da..95a83b5 100644
11ff82
--- a/str.h
11ff82
+++ b/str.h
11ff82
@@ -98,6 +98,10 @@ int str_contains_space(const struct mystr* p_str);
11ff82
 int str_all_space(const struct mystr* p_str);
11ff82
 int str_contains_unprintable(const struct mystr* p_str);
11ff82
 void str_replace_unprintable(struct mystr* p_str, char new_char);
11ff82
+void str_replace_unprintable_with_hex(struct mystr* p_str);
11ff82
+void str_replace_unprintable_with_hex_wc(struct mystr* p_str);
11ff82
+void str_replace_positions_with_hex(struct mystr* p_str, const unsigned int* poss,
11ff82
+                                    const unsigned int pos_count);
11ff82
 int str_atoi(const struct mystr* p_str);
11ff82
 filesize_t str_a_to_filesize_t(const struct mystr* p_str);
11ff82
 unsigned int str_octal_to_uint(const struct mystr* p_str);
11ff82
diff --git a/tunables.c b/tunables.c
11ff82
index a7ce9c8..c96c1ac 100644
11ff82
--- a/tunables.c
11ff82
+++ b/tunables.c
11ff82
@@ -94,6 +94,7 @@ int tunable_seccomp_sandbox;
11ff82
 int tunable_allow_writeable_chroot;
11ff82
 int tunable_better_stou;
11ff82
 int tunable_log_die;
11ff82
+int tunable_wc_logs_enable;
11ff82
 
11ff82
 unsigned int tunable_accept_timeout;
11ff82
 unsigned int tunable_connect_timeout;
11ff82
@@ -244,6 +245,7 @@ tunables_load_defaults()
11ff82
   tunable_allow_writeable_chroot = 0;
11ff82
   tunable_better_stou = 0;
11ff82
   tunable_log_die = 0;
11ff82
+  tunable_wc_logs_enable = 0;
11ff82
 
11ff82
   tunable_accept_timeout = 60;
11ff82
   tunable_connect_timeout = 60;
11ff82
diff --git a/tunables.h b/tunables.h
11ff82
index 029d645..8d50150 100644
11ff82
--- a/tunables.h
11ff82
+++ b/tunables.h
11ff82
@@ -98,6 +98,7 @@ extern int tunable_better_stou;               /* Use better file name generation
11ff82
 					       */
11ff82
 extern int tunable_log_die;                   /* Log calls to die(), die2()
11ff82
                                                * and bug() */
11ff82
+extern int tunable_wc_logs_enable;            /* Allow non ASCII characters in logs */
11ff82
 
11ff82
 /* Integer/numeric defines */
11ff82
 extern unsigned int tunable_accept_timeout;
11ff82
diff --git a/vsftpd.conf.5 b/vsftpd.conf.5
11ff82
index ce3fba3..815773f 100644
11ff82
--- a/vsftpd.conf.5
11ff82
+++ b/vsftpd.conf.5
11ff82
@@ -735,6 +735,12 @@ If enabled, use CLONE_NEWPID and CLONE_NEWIPC to isolate processes to their
11ff82
 ipc and pid namespaces. So separated processes can not interact with each other.
11ff82
 
11ff82
 Default: YES
11ff82
+.TP
11ff82
+.B wc_logs_enable
11ff82
+If enabled, logs will be treated as wide-character strings and not just
11ff82
+ASCII strings when filtering out non-printable characters.
11ff82
+
11ff82
+Default: NO
11ff82
 
11ff82
 .SH NUMERIC OPTIONS
11ff82
 Below is a list of numeric options. A numeric option must be set to a non