Skip to contentSkip to conversation

WTMP support for OpenVPN 2.1

Openvpn-devel Maximilian Wilhelm 1 participant 1 message Oct 14, 2007 Kbuild

lore
Share & tools
lore
#0 lore raw

Hi!

This is my second try to get comments for my WTMP support for OpenVPN 2.1.

Description:

If compiled in and activated in the configuration, OpenVPN will put a wtmp entry into the logfile for each connection include the username, it's VPN ip, the real IP the client connected from and the times the session was opened and closed.

It is possible to let multiple instances of OpenVPN on the same host use the same wtmp file.

You will be able to distinguish between vpn servers in a commonly use file by specifying different "server ids" (two digit integer value), which will be used in the wtmp line string.

This provides an OpenVPN admin with a simple database of all connections to his VPN servers and the ability to easily track down when, who was logged in and from where.

One thing which is not perfect is that, the uid/gid for the wtmp file are looked up manually in wtmp_init() as context->c0 struct is not yet available.
Maybe wtmp_init() could be called a bit later at a usefull place to avoid this doulbe lookup.

I would very much like to get the WTMP logging support included in OpenVPN 2.1 as it will help people to get an overview about the usage of their VPNs and prepares abuse-people with relevant information if problems occur.

Some time ago, this feature was requested, btw.

Ciao
Max

Signature · 1 line
	Follow the white penguin.

Added field 'wtmp_line' to 'struct context_1' to store unique wtmp_line id. --

 openvpn.h |    4 ++++
 1 files changed, 4 insertions(+), 0 deletions(-)
openvpn.h +4 -0
index 0cdab62..70d2f28 100644
@@ -195,6 +195,10 @@ struct context_1
   /* save user/pass for authentication */
   struct user_pass *auth_user_pass;
 #endif
+
+#ifdef ENABLE_WTMP
+  unsigned long int wtmp_line;
+#endif
 };
 
 /*

The entire wtmp support for OpenVPN.
--

 wtmp.c |  397 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 wtmp.h |   20 ++++
 2 files changed, 417 insertions(+), 0 deletions(-)
wtmp.c new file +397 -0

397 lines not on this page · raw message

wtmp.h new file +20 -0
new file mode 100644
index 0000000..f0fb66e
@@ -0,0 +1,20 @@
+#ifndef _OPENVPN_WTMP_H
+#define _OPENVPN_WTMP_H
+
+#include <utmp.h>
+
+#include "multi.h"
+
+enum local_remote { LOCAL, REMOTE };
+
+int wtmp_init (const struct options *vpn_options);
+
+void wtmp_start (const struct multi_instance *mi);
+void wtmp_stop (const struct multi_instance *mi);
+
+
+static struct sockaddr_in * get_remote_sockaddr_from_multi_instance (const struct multi_instance *mi);
+extern unsigned long int gen_wtmp_line ();
+extern char * format_wtmp_line (unsigned long int n);
+
+#endif /* _OPENVPN_WTMP_H */

--log-wtmp server_id [file] : Enable logging to wtmp file using the two digit server_id a prefix for the utmp line. Optionally you can write
to an own wtmp file. (Default is /var/log/openvpn/wtmp)
--

 options.c |   32 ++++++++++++++++++++++++++++++++
 options.h |    6 ++++++
 2 files changed, 38 insertions(+), 0 deletions(-)
options.c +32 -0
index 46aad6d..e8567f7 100644
@@ -266,6 +266,11 @@ static const char usage_message[] =
   "--suppress-timestamps : Don't log timestamps to stdout/stderr.\n"
   "--writepid file : Write main process ID to file.\n"
   "--nice n        : Change process priority (>0 = lower, <0 = higher).\n"
+#ifdef ENABLE_WTMP
+  "--log-wtmp server_id [file] : Enable logging to wtmp file using the two digit\n"
+  "                  server_id a prefix for the utmp line. Optionally you can write\n"
+  "                  to an own wtmp file. (Default is /var/log/openvpn/wtmp)\n"
+#endif
 #if 0
 #ifdef USE_PTHREAD
   "--nice-work n   : Change thread priority of work thread.  The work\n"
@@ -697,6 +702,12 @@ init_options (struct options *o)
 #ifdef ENABLE_PKCS11
   o->pkcs11_pin_cache_period = -1;
 #endif			/* ENABLE_PKCS11 */
+
+#ifdef ENABLE_WTMP
+  o->log_wtmp = 0;
+  o->wtmp_server_id = 1;
+  o->wtmp_file = "/var/log/openvpn/wtmp";
+#endif
 }
 
 void
@@ -3473,6 +3484,26 @@ add_option (struct options *options,
       options->log = true;
       redirect_stdout_stderr (p[1], false);
     }
+
+#ifdef ENABLE_WTMP
+  else if (streq (p[0], "log-wtmp") && p[1])
+    {
+      VERIFY_PERMISSION (OPT_P_GENERAL);
+
+      /* Activate WTMP logging */
+      options->log_wtmp = 1;
+
+      /* Use server_id if set */
+      if (p[1])
+        options->wtmp_server_id = atoi (p[1]);
+
+      /* User specified wtmp file if set */
+      if (p[2])
+        options->wtmp_file = p[2];
+
+    }
+#endif
+
   else if (streq (p[0], "suppress-timestamps"))
     {
       VERIFY_PERMISSION (OPT_P_GENERAL);
@@ -3773,6 +3804,7 @@ add_option (struct options *options,
       openvpn_exit (OPENVPN_EXIT_STATUS_GOOD); /* exit point */
     }
 #endif /* GENERAL_PROXY_SUPPORT */
+
 #ifdef ENABLE_HTTP_PROXY
   else if (streq (p[0], "http-proxy") && p[1])
     {
options.h +6 -0
index 21d131e..e7a49b9 100644
@@ -214,6 +214,12 @@ struct options
   /* inetd modes defined in socket.h */
   int inetd;
 
+#ifdef ENABLE_WTMP
+  int log_wtmp;
+  int wtmp_server_id;
+  char *wtmp_file;
+#endif
+
   bool log;
   bool suppress_timestamps;
   int nice;

If WTMP support was enabled at compile time and activated in configuration call wtmp_*() functions at startup and connection startup/end. --

 multi.c |   22 ++++++++++++++++++++++
 1 files changed, 22 insertions(+), 0 deletions(-)
multi.c +22 -0
index fa924f1..2977029 100644
@@ -42,6 +42,10 @@
 
 #include "forward-inline.h"
 
+#ifdef ENABLE_WTMP
+#include "wtmp.h"
+#endif
+
 /*#define MULTI_DEBUG_EVENT_LOOP*/
 
 #ifdef MULTI_DEBUG_EVENT_LOOP
@@ -457,6 +461,11 @@ multi_close_instance (struct multi_context *m,
 
   dmsg (D_MULTI_DEBUG, "MULTI: multi_close_instance called");
 
+#ifdef ENABLE_WTMP
+  if (m->top.options.log_wtmp)
+    wtmp_stop (mi);
+#endif
+
   /* prevent dangling pointers */
   if (m->pending == mi)
     multi_set_pending (m, NULL);
@@ -1444,6 +1453,14 @@ multi_connection_established (struct multi_context *m, struct multi_instance *mi
 	}
 #endif
 
+#ifdef ENABLE_WTMP
+      if (m->top.options.log_wtmp)
+	{
+	  mi->context.c1.wtmp_line = gen_wtmp_line ();
+	  wtmp_start (mi);
+	}
+#endif
+
       /*
        * Run --client-connect script.
        */
@@ -2289,6 +2306,11 @@ tunnel_server (struct context *top)
 {
   ASSERT (top->options.mode == MODE_SERVER);
 
+#ifdef ENABLE_WTMP
+  if (top->options.log_wtmp)
+    wtmp_init (&top->options);
+#endif
+
   switch (top->options.proto) {
   case PROTO_UDPv4:
     tunnel_server_udp (top);

Add '--enable-wtmp' option to configure script to let users easily enable WTMP log support.

If '--enable-wtmp' was given, check if 'utmp.h' is available on the system and bail out if header file could not be found.
--

 configure.ac |   15 +++++++++++++++
 1 files changed, 15 insertions(+), 0 deletions(-)
configure.ac +15 -0
index af57577..3706eb4 100644
@@ -137,6 +137,12 @@ AC_ARG_ENABLE(iproute2,
    AC_DEFINE(CONFIG_FEATURE_IPROUTE, 1, [enable iproute2 support])   
 )
 
+AC_ARG_ENABLE(wtmp,
+   [  --enable-wtmp           Enable WTMP support (--log-wtmp)],
+   [LOG_WTMP="$enableval"],
+   [LOG_WTMP="no"]
+)
+
 AC_ARG_ENABLE(strict,
    [  --enable-strict         Enable strict compiler warnings (debugging option)],
    [STRICT="$enableval"],
@@ -705,4 +711,13 @@ if test "$PASSWORD_SAVE" = "yes"; then
    AC_DEFINE(ENABLE_PASSWORD_SAVE, 1, [Allow --askpass and --auth-user-pass passwords to be read from a file])
 fi
 
+dnl enable wtmp logging
+if test "$LOG_WTMP" = "yes"; then
+
+  AC_CHECK_HEADER(utmp.h,
+  	[AC_DEFINE(ENABLE_WTMP, 1, [Enable wtmp logging support])],
+ 	[AC_MSG_ERROR([WTMP headers not found])]
+  )
+fi
+
 AC_OUTPUT(Makefile openvpn.spec)

Updated Makefile.am to build wtmp object file.
--

 Makefile.am |    3 ++-
 1 files changed, 2 insertions(+), 1 deletions(-)
Makefile.am +2 -1
index 36e2cf9..5f1489b 100644
@@ -94,7 +94,8 @@ openvpn_SOURCES = \
 	status.c status.h \
 	syshead.h \
 	thread.c thread.h \
-	tun.c tun.h
+	tun.c tun.h \
+	wtmp.c wtmp.h
 
 LDADD = @LIBOBJS@
 

Updated 'get_link_socket_info' function to use argument 'struct context *' as a const value as it is not modified in this function.

This silences a gcc warning in wtmp.c.
--

 forward-inline.h |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
forward-inline.h +1 -1
index 09b9809..c3f60af 100644
@@ -215,7 +215,7 @@ context_reschedule_sec (struct context *c, int sec)
 }
 
 static inline struct link_socket_info *
-get_link_socket_info (struct context *c)
+get_link_socket_info (const struct context *c)
 {
   if (c->c2.link_socket_info)
     return c->c2.link_socket_info;