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

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