From ca2c633a7088f74b569af0a99e7c5efcf3138532 Mon Sep 17 00:00:00 2001
From: Vision Systems GmbH <info@visionsystems.de>
Date: Mon, 20 Jul 2020 14:36:46 +0200
Subject: [PATCH] Pipe-Logging for VScom-NetComs

---
 src/openvpn/error.c   | 16 ++++++++++++++++
 src/openvpn/openvpn.c | 24 ++++++++++++++++++++++++
 src/openvpn/options.c |  7 +++++++
 src/openvpn/options.h |  2 ++
 4 files changed, 49 insertions(+)

diff --git a/src/openvpn/error.c b/src/openvpn/error.c
index bc14e8c..5a76895 100755
--- a/src/openvpn/error.c
+++ b/src/openvpn/error.c
@@ -52,6 +52,8 @@
 /* Globals */
 unsigned int x_debug_level; /* GLOBAL */
 
+extern int fd_fifo; // HME
+
 /* Mute state */
 static int mute_cutoff;     /* GLOBAL */
 static int mute_count;      /* GLOBAL */
@@ -323,6 +325,20 @@ x_msg_va(const unsigned int flags, const char *format, va_list arglist)
 
     if (!(flags & M_MSG_VIRT_OUT))
     {
+        // HMEa
+        if (1)
+        {
+              //printf("%s\n", m1);
+              if (fd_fifo > 0)
+              {
+                    //int len = openvpn_snprintf(m2, ERR_BUF_SIZE, "OVPN: %s\n", m1);
+                    write(fd_fifo, m1, strlen(m1));
+                    //write(fd_fifo, m2, len);
+                    fsync(fd_fifo);
+              }
+        }
+        else
+        // HMEe
         if (use_syslog && !std_redir && !forked)
         {
 #if SYSLOG_CAPABILITY
diff --git a/src/openvpn/openvpn.c b/src/openvpn/openvpn.c
old mode 100644
new mode 100755
index 3d244fc..9721908
--- a/src/openvpn/openvpn.c
+++ b/src/openvpn/openvpn.c
@@ -150,12 +150,31 @@ tunnel_point_to_point(struct context *c)
  * @param argc - Commandline argument count.
  * @param argv - Commandline argument values.
  */
+
+// HME
+#define FIFO_VSLOG      "/etc/fifo_vslog"
+int fd_fifo = 0;
+
 static
 int
 openvpn_main(int argc, char *argv[])
 {
     struct context c;
 
+    // HMEa
+    struct stat st;
+
+    if (stat(FIFO_VSLOG, &st) == 0)
+    {
+        if (S_ISFIFO(st.st_mode))
+        {
+            fd_fifo = open(FIFO_VSLOG, O_WRONLY);
+            if (fd_fifo == -1)
+                perror("open the fifo file failed");
+        }
+    }
+    // HMEe
+
 #if PEDANTIC
     fprintf(stderr, "Sorry, I was built with --enable-pedantic and I am incapable of doing any real work!\n");
     return 1;
@@ -346,6 +365,11 @@ openvpn_main(int argc, char *argv[])
     /* uninitialize program-wide statics */
     uninit_static();
 
+    // HMEa
+    if (fd_fifo)
+        close(fd_fifo);
+    // HMEe
+
     openvpn_exit(OPENVPN_EXIT_STATUS_GOOD); /* exit point */
     return 0;                               /* NOTREACHED */
 }
diff --git a/src/openvpn/options.c b/src/openvpn/options.c
old mode 100644
new mode 100755
index de30fcb..8f7325b
--- a/src/openvpn/options.c
+++ b/src/openvpn/options.c
@@ -8262,6 +8262,13 @@ add_option(struct options *options,
         VERIFY_PERMISSION(OPT_P_GENERAL);
         options->allow_recursive_routing = true;
     }
+    // HMEa
+    else if (streq (p[0], "vslog"))
+    {
+        VERIFY_PERMISSION (OPT_P_GENERAL);
+        options->vslog = true;
+    }
+    // HMEe
     else
     {
         int i;
diff --git a/src/openvpn/options.h b/src/openvpn/options.h
old mode 100644
new mode 100755
index f3cafea..4f8e1fd
--- a/src/openvpn/options.h
+++ b/src/openvpn/options.h
@@ -611,6 +611,8 @@ struct options
     /* Useful when packets sent by openvpn itself are not subject
      * to the routing tables that would move packets into the tunnel. */
     bool allow_recursive_routing;
+
+	bool vslog; // HME
 };
 
 #define streq(x, y) (!strcmp((x), (y)))
-- 
2.17.0

