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

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