d7fdbd
From ee6af258e8cb1a7fada5e6d3e54429b89f12b158 Mon Sep 17 00:00:00 2001
d7fdbd
From: =?UTF-8?q?Ond=C5=99ej=20Lyson=C4=9Bk?= <olysonek@redhat.com>
d7fdbd
Date: Fri, 15 Jun 2018 12:02:21 +0200
d7fdbd
Subject: [PATCH 56/59] Log die() calls to syslog
d7fdbd
d7fdbd
Pass messages given to die(), die2() and bug() to syslog. Currently this
d7fdbd
functionality requires waiting for a short amount of time (1 second is
d7fdbd
used) after logging the message and before exiting. This is a workaround
d7fdbd
for the following systemd bug:
d7fdbd
https://github.com/systemd/systemd/issues/2913
d7fdbd
d7fdbd
The need for this workaround is the main reason why I decided not to
d7fdbd
enable this functionality by default.
d7fdbd
d7fdbd
Resolves: rhbz#1318198
d7fdbd
Resolves: rhbz#1582672
d7fdbd
---
d7fdbd
 logging.c     | 13 +++++++++----
d7fdbd
 logging.h     |  2 ++
d7fdbd
 main.c        |  4 ++++
d7fdbd
 parseconf.c   |  1 +
d7fdbd
 tcpwrap.c     |  3 ---
d7fdbd
 tunables.c    |  2 ++
d7fdbd
 tunables.h    |  2 ++
d7fdbd
 utility.c     | 11 +++++++++++
d7fdbd
 vsftpd.conf.5 | 10 ++++++++++
d7fdbd
 9 files changed, 41 insertions(+), 7 deletions(-)
d7fdbd
d7fdbd
diff --git a/logging.c b/logging.c
d7fdbd
index c4461f7..9e86808 100644
d7fdbd
--- a/logging.c
d7fdbd
+++ b/logging.c
d7fdbd
@@ -30,10 +30,6 @@ static void vsf_log_do_log_to_file(int fd, struct mystr* p_str);
d7fdbd
 void
d7fdbd
 vsf_log_init(struct vsf_session* p_sess)
d7fdbd
 {
d7fdbd
-  if (tunable_syslog_enable || tunable_tcp_wrappers)
d7fdbd
-  {
d7fdbd
-    vsf_sysutil_openlog(0);
d7fdbd
-  }
d7fdbd
   if (!tunable_xferlog_enable && !tunable_dual_log_enable)
d7fdbd
   {
d7fdbd
     return;
d7fdbd
@@ -389,3 +385,12 @@ vsf_log_do_log_vsftpd_format(struct vsf_session* p_sess, struct mystr* p_str,
d7fdbd
   }
d7fdbd
 }
d7fdbd
 
d7fdbd
+void
d7fdbd
+vsf_log_die(const char* p_text)
d7fdbd
+{
d7fdbd
+  struct mystr log_str = INIT_MYSTR;
d7fdbd
+
d7fdbd
+  str_append_text(&log_str, "ERROR: ");
d7fdbd
+  str_append_text(&log_str, p_text);
d7fdbd
+  str_syslog(&log_str, 1);
d7fdbd
+}
d7fdbd
diff --git a/logging.h b/logging.h
d7fdbd
index 1ff57d1..75f06c1 100644
d7fdbd
--- a/logging.h
d7fdbd
+++ b/logging.h
d7fdbd
@@ -91,5 +91,7 @@ void vsf_log_line(struct vsf_session* p_sess, enum EVSFLogEntryType what,
d7fdbd
 void vsf_log_failed_line(struct vsf_session* p_sess, enum EVSFLogEntryType what,
d7fdbd
                   struct mystr* p_str);
d7fdbd
 
d7fdbd
+void vsf_log_die(const char* p_text);
d7fdbd
+
d7fdbd
 #endif /* VSF_LOGGING_H */
d7fdbd
 
d7fdbd
diff --git a/main.c b/main.c
d7fdbd
index f039081..1178d44 100644
d7fdbd
--- a/main.c
d7fdbd
+++ b/main.c
d7fdbd
@@ -120,6 +120,10 @@ main(int argc, const char* argv[])
d7fdbd
     }
d7fdbd
     vsf_sysutil_free(p_statbuf);
d7fdbd
   }
d7fdbd
+  if (tunable_log_die || tunable_syslog_enable || tunable_tcp_wrappers)
d7fdbd
+  {
d7fdbd
+    vsf_sysutil_openlog(0);
d7fdbd
+  }
d7fdbd
   /* Resolve pasv_address if required */
d7fdbd
   if (tunable_pasv_address && tunable_pasv_addr_resolve)
d7fdbd
   {
d7fdbd
diff --git a/parseconf.c b/parseconf.c
d7fdbd
index 47b54f1..aeb401a 100644
d7fdbd
--- a/parseconf.c
d7fdbd
+++ b/parseconf.c
d7fdbd
@@ -112,6 +112,7 @@ parseconf_bool_array[] =
d7fdbd
   { "seccomp_sandbox", &tunable_seccomp_sandbox },
d7fdbd
   { "allow_writeable_chroot", &tunable_allow_writeable_chroot },
d7fdbd
   { "better_stou", &tunable_better_stou },
d7fdbd
+  { "log_die", &tunable_log_die },
d7fdbd
   { 0, 0 }
d7fdbd
 };
d7fdbd
 
d7fdbd
diff --git a/tcpwrap.c b/tcpwrap.c
d7fdbd
index 5bf57d3..132b771 100644
d7fdbd
--- a/tcpwrap.c
d7fdbd
+++ b/tcpwrap.c
d7fdbd
@@ -27,15 +27,12 @@ int
d7fdbd
 vsf_tcp_wrapper_ok(int remote_fd)
d7fdbd
 {
d7fdbd
   struct request_info req;
d7fdbd
-  vsf_sysutil_openlog(0);
d7fdbd
   request_init(&req, RQ_DAEMON, "vsftpd", RQ_FILE, remote_fd, 0);
d7fdbd
   fromhost(&req;;
d7fdbd
   if (!hosts_access(&req))
d7fdbd
   {
d7fdbd
-    vsf_sysutil_closelog();
d7fdbd
     return 0;
d7fdbd
   }
d7fdbd
-  vsf_sysutil_closelog();
d7fdbd
   return 1;
d7fdbd
 }
d7fdbd
 
d7fdbd
diff --git a/tunables.c b/tunables.c
d7fdbd
index 5ec2bdc..63de8e6 100644
d7fdbd
--- a/tunables.c
d7fdbd
+++ b/tunables.c
d7fdbd
@@ -93,6 +93,7 @@ int tunable_http_enable;
d7fdbd
 int tunable_seccomp_sandbox;
d7fdbd
 int tunable_allow_writeable_chroot;
d7fdbd
 int tunable_better_stou;
d7fdbd
+int tunable_log_die;
d7fdbd
 
d7fdbd
 unsigned int tunable_accept_timeout;
d7fdbd
 unsigned int tunable_connect_timeout;
d7fdbd
@@ -241,6 +242,7 @@ tunables_load_defaults()
d7fdbd
   tunable_seccomp_sandbox = 0;
d7fdbd
   tunable_allow_writeable_chroot = 0;
d7fdbd
   tunable_better_stou = 0;
d7fdbd
+  tunable_log_die = 0;
d7fdbd
 
d7fdbd
   tunable_accept_timeout = 60;
d7fdbd
   tunable_connect_timeout = 60;
d7fdbd
diff --git a/tunables.h b/tunables.h
d7fdbd
index 85ea1a8..8a4b8b2 100644
d7fdbd
--- a/tunables.h
d7fdbd
+++ b/tunables.h
d7fdbd
@@ -96,6 +96,8 @@ extern int tunable_allow_writeable_chroot;    /* Allow misconfiguration */
d7fdbd
 extern int tunable_better_stou;               /* Use better file name generation
d7fdbd
                                                * algorithm for the STOU command
d7fdbd
 					       */
d7fdbd
+extern int tunable_log_die;                   /* Log calls to die(), die2()
d7fdbd
+                                               * and bug() */
d7fdbd
 
d7fdbd
 /* Integer/numeric defines */
d7fdbd
 extern unsigned int tunable_accept_timeout;
d7fdbd
diff --git a/utility.c b/utility.c
d7fdbd
index 5fd714d..75e5bdd 100644
d7fdbd
--- a/utility.c
d7fdbd
+++ b/utility.c
d7fdbd
@@ -9,6 +9,8 @@
d7fdbd
 #include "sysutil.h"
d7fdbd
 #include "str.h"
d7fdbd
 #include "defs.h"
d7fdbd
+#include "logging.h"
d7fdbd
+#include "tunables.h"
d7fdbd
 
d7fdbd
 #define DIE_DEBUG
d7fdbd
 
d7fdbd
@@ -41,11 +43,20 @@ void
d7fdbd
 bug(const char* p_text)
d7fdbd
 {
d7fdbd
   /* Rats. Try and write the reason to the network for diagnostics */
d7fdbd
+  if (tunable_log_die)
d7fdbd
+  {
d7fdbd
+    vsf_log_die(p_text);
d7fdbd
+  }
d7fdbd
   vsf_sysutil_activate_noblock(VSFTP_COMMAND_FD);
d7fdbd
   (void) vsf_sysutil_write_loop(VSFTP_COMMAND_FD, "500 OOPS: ", 10);
d7fdbd
   (void) vsf_sysutil_write_loop(VSFTP_COMMAND_FD, p_text,
d7fdbd
                                 vsf_sysutil_strlen(p_text));
d7fdbd
   (void) vsf_sysutil_write_loop(VSFTP_COMMAND_FD, "\r\n", 2);
d7fdbd
+  if (tunable_log_die)
d7fdbd
+  {
d7fdbd
+    /* Workaround for https://github.com/systemd/systemd/issues/2913 */
d7fdbd
+    vsf_sysutil_sleep(1.0);
d7fdbd
+  }
d7fdbd
   vsf_sysutil_exit(2);
d7fdbd
 }
d7fdbd
 
d7fdbd
diff --git a/vsftpd.conf.5 b/vsftpd.conf.5
d7fdbd
index e9ae474..f246906 100644
d7fdbd
--- a/vsftpd.conf.5
d7fdbd
+++ b/vsftpd.conf.5
d7fdbd
@@ -358,6 +358,16 @@ wanting to e.g. append a file.
d7fdbd
 
d7fdbd
 Default: YES
d7fdbd
 .TP
d7fdbd
+.B log_die
d7fdbd
+Log an error to syslog when some error condition occurs and vsftpd decides
d7fdbd
+to quit. Internally, the error messages given to the functions die(), die2()
d7fdbd
+and bug() are passed to syslog. Currently this functionality requires waiting
d7fdbd
+for a short amount of time (1 second is used) after logging the message and
d7fdbd
+before exiting. This is a workaround for the following systemd bug:
d7fdbd
+https://github.com/systemd/systemd/issues/2913
d7fdbd
+
d7fdbd
+Default: NO
d7fdbd
+.TP
d7fdbd
 .B log_ftp_protocol
d7fdbd
 When enabled, all FTP requests and responses are logged, providing the option
d7fdbd
 xferlog_std_format is not enabled. Useful for debugging.
d7fdbd
-- 
d7fdbd
2.14.4
d7fdbd