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

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