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

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