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