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