Skip to contentSkip to conversation

implement --session-timeout

PATCH Superseded Antonio Quartulli 2 participants 10 messages Sep 17, 2022 – Oct 7, 2022

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 implement --session-timeout

From: Dmitry Zelenkovsky <[email protected]...>

Disconnect clients after session-timeout expires. session-timeout can be defined in ccd files in order to limit per-user connection time.

---
 src/openvpn/forward.c | 22 ++++++++++++++++++++++
 src/openvpn/init.c    |  7 +++++++
 src/openvpn/openvpn.h |  2 ++
 src/openvpn/options.c |  7 +++++++
 src/openvpn/options.h |  2 ++
 5 files changed, 40 insertions(+)
src/openvpn/forward.c +22 -0
index 3526dbf6..56ab5662 100644
@@ -626,6 +626,21 @@ encrypt_sign(struct context *c, bool comp_frag)
     buffer_turnover(orig_buf, &c->c2.to_link, &c->c2.buf, &b->read_tun_buf);
 }
 
+/*
+ * Should we exit due to session timeout?
+ */
+static void
+check_session_timeout(struct context *c)
+{
+    if (c->options.session_timeout
+        && event_timeout_trigger(&c->c2.session_interval, &c->c2.timeval,
+                                 ETT_DEFAULT))
+    {
+        msg(M_INFO, "Session timeout, exiting");
+        register_signal(c, SIGTERM, "session-timeout");
+    }
+}
+
 /*
  * Coarse timers work to 1 second resolution.
  */
@@ -677,6 +692,13 @@ process_coarse_timers(struct context *c)
         return;
     }
 
+    /* kill session if time is over */
+    check_session_timeout(c);
+    if (c->sig->signal_received)
+    {
+        return;
+    }
+
     /* restart if ping not received */
     check_ping_restart(c);
     if (c->sig->signal_received)
src/openvpn/init.c +7 -0
index f2db8dd9..7b817612 100644
@@ -1322,6 +1322,13 @@ do_init_timers(struct context *c, bool deferred)
         event_timeout_init(&c->c2.inactivity_interval, c->options.inactivity_timeout, now);
     }
 
+    /* initialize inactivity timeout */
+    if (c->options.session_timeout)
+    {
+        event_timeout_init(&c->c2.session_interval, c->options.session_timeout,
+                           now);
+    }
+
     /* initialize pings */
     if (dco_enabled(&c->options))
     {
src/openvpn/openvpn.h +2 -0
index 00cd652f..f74125aa 100644
@@ -288,6 +288,8 @@ struct context_2
     struct event_timeout inactivity_interval;
     int64_t inactivity_bytes;
 
+    struct event_timeout session_interval;
+
     /* the option strings must match across peers */
     char *options_string_local;
     char *options_string_remote;
src/openvpn/options.c +7 -0
index 3d48c2d9..76c09a0a 100644
@@ -261,6 +261,7 @@ static const char usage_message[] =
     "                  for m seconds.\n"
     "--inactive n [bytes] : Exit after n seconds of activity on tun/tap device\n"
     "                  produces a combined in/out byte count < bytes.\n"
+    "--session-timeout n: Limit connection time to n seconds.\n"
     "--ping-exit n   : Exit if n seconds pass without reception of remote ping.\n"
     "--ping-restart n: Restart if n seconds pass without reception of remote ping.\n"
     "--ping-timer-rem: Run the --ping-exit/--ping-restart timer only if we have a\n"
@@ -1818,6 +1819,7 @@ show_settings(const struct options *o)
     SHOW_INT(keepalive_ping);
     SHOW_INT(keepalive_timeout);
     SHOW_INT(inactivity_timeout);
+    SHOW_INT(session_timeout);
     SHOW_INT64(inactivity_minimum_bytes);
     SHOW_INT(ping_send_timeout);
     SHOW_INT(ping_rec_timeout);
@@ -6583,6 +6585,11 @@ add_option(struct options *options,
             }
         }
     }
+    else if (streq(p[0], "session-timeout") && p[1] && !p[2])
+    {
+        VERIFY_PERMISSION(OPT_P_TIMER);
+        options->session_timeout = positive_atoi(p[1]);
+    }
     else if (streq(p[0], "proto") && p[1] && !p[2])
     {
         int proto;
src/openvpn/options.h +2 -0
index c9144154..a674a0a6 100644
@@ -317,6 +317,8 @@ struct options
     int inactivity_timeout;     /* --inactive */
     int64_t inactivity_minimum_bytes;
 
+    int session_timeout;        /* Kill session after n seconds, regardless activity */
+
     int ping_send_timeout;      /* Send a TCP/UDP ping to remote every n seconds */
     int ping_rec_timeout;       /* Expect a TCP/UDP ping from remote at least once every n seconds */
     bool ping_timer_remote;     /* Run ping timer only if we have a remote address */
Signature · 1 line
2.35.1
#1 lore raw

HI,

Quoted 5 lines · Antonio Quartulli

From: Dmitry Zelenkovsky <[email protected]...>

Disconnect clients after session-timeout expires. session-timeout can be defined in ccd files in order to limit per-user connection time.

I find this implementation needlessly complicated.

Quoted 4 lines
src/openvpn/forward.c +4 -0
+/*
+ * Should we exit due to session timeout?
+ */
+static void

src/openvpn/forward.c encrypt_sign(struct context *c, bool comp_frag) · line 642

Quoted 10 lines
src/openvpn/forward.c +10 -0
+check_session_timeout(struct context *c)
+{
+    if (c->options.session_timeout
+        && event_timeout_trigger(&c->c2.session_interval, &c->c2.timeval,
+                                 ETT_DEFAULT))
+    {
+        msg(M_INFO, "Session timeout, exiting");
+        register_signal(c, SIGTERM, "session-timeout");
+    }
+}

Why are we working with event triggers here, if all we *want* to do is a single-shot

if ( now > $somectx->session_must_end_at_this_time )

 {
      /* kick out this user now */
      ...

}

src/openvpn/options.c add_option(struct options *options, · line 6590

Quoted 3 lines
src/openvpn/options.c +3 -0
+    else if (streq(p[0], "session-timeout") && p[1] && !p[2])
+    {
+        VERIFY_PERMISSION(OPT_P_TIMER);

OPT_P_INSTANCE?

gert

Signature · 6 lines
"If was one thing all people took for granted, was conviction that if you 
 feed honest figures into a computer, honest figures come out. Never doubted 
 it myself till I met a computer with a sense of humor."
                             Robert A. Heinlein, The Moon is a Harsh Mistress

Gert Doering - Munich, Germany                             [email protected]...
#2 lore raw
Quoted 29 lines · Gert Doering

HI,

Quoted 5 lines · Antonio Quartulli

From: Dmitry Zelenkovsky <[email protected]...>

Disconnect clients after session-timeout expires. session-timeout can be defined in ccd files in order to limit per-user connection time.

I find this implementation needlessly complicated.

Quoted 4 lines
src/openvpn/forward.c +4 -0
+/*
+ * Should we exit due to session timeout?
+ */
+static void

src/openvpn/forward.c encrypt_sign(struct context *c, bool comp_frag) · line 642

Quoted 10 lines
src/openvpn/forward.c +10 -0
+check_session_timeout(struct context *c)
+{
+    if (c->options.session_timeout
+        && event_timeout_trigger(&c->c2.session_interval, &c->c2.timeval,
+                                 ETT_DEFAULT))
+    {
+        msg(M_INFO, "Session timeout, exiting");
+        register_signal(c, SIGTERM, "session-timeout");
+    }
+}

Why are we working with event triggers here, if all we *want* to do is a single-shot

Quoted 6 lines
  if ( now > $somectx->session_must_end_at_this_time )
  {
       /* kick out this user now */
       ...
  }

we don't get here at all, if we have no event object that is timing out.
So, although one shot, we still need to setup a timer object that will trigger the machinery upon timeout.

Quoted 5 lines

src/openvpn/options.c add_option(struct options *options, · line 6590

Quoted 3 lines
src/openvpn/options.c +3 -0
+    else if (streq(p[0], "session-timeout") && p[1] && !p[2])
+    {
+        VERIFY_PERMISSION(OPT_P_TIMER);

OPT_P_INSTANCE?

makes sense to add OPT_P_INSTANCE, although I wonder why other activity/timeout knobs are not marks as such, i.e. --inactivity)

Cheers,

Quoted 9 lines

gert

_______________________________________________
Openvpn-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openvpn-devel

Signature · 1 line
Antonio Quartulli
#3 lore raw

Hi,

Quoted 28 lines · Antonio Quartulli
Quoted 27 lines · Gert Doering
Quoted 5 lines · Antonio Quartulli

From: Dmitry Zelenkovsky <[email protected]...>

Disconnect clients after session-timeout expires. session-timeout can be defined in ccd files in order to limit per-user connection time.

I find this implementation needlessly complicated.

Quoted 4 lines
src/openvpn/forward.c +4 -0
+/*
+ * Should we exit due to session timeout?
+ */
+static void

src/openvpn/forward.c encrypt_sign(struct context *c, bool comp_frag) · line 642

Quoted 10 lines
src/openvpn/forward.c +10 -0
+check_session_timeout(struct context *c)
+{
+    if (c->options.session_timeout
+        && event_timeout_trigger(&c->c2.session_interval, &c->c2.timeval,
+                                 ETT_DEFAULT))
+    {
+        msg(M_INFO, "Session timeout, exiting");
+        register_signal(c, SIGTERM, "session-timeout");
+    }
+}

Why are we working with event triggers here, if all we *want* to do is a single-shot

Quoted 8 lines
Quoted 6 lines
  if ( now > $somectx->session_must_end_at_this_time )
  {
       /* kick out this user now */
       ...
  }

we don't get here at all, if we have no event object that is timing out.

process_coarse_timers() is run once per second, and does not need additional timers to be called.

This new check is added to process_coarse_timers().

Why do we need an extra event object, again?

I can see the need for event objects for recurring things, but for a single-shot "terminate client instance at this time, done", I'm not convinced.

Quoted 11 lines

So, although one shot, we still need to setup a timer object that will trigger the machinery upon timeout.

Quoted 5 lines

src/openvpn/options.c add_option(struct options *options, · line 6590

Quoted 3 lines
src/openvpn/options.c +3 -0
+    else if (streq(p[0], "session-timeout") && p[1] && !p[2])
+    {
+        VERIFY_PERMISSION(OPT_P_TIMER);

OPT_P_INSTANCE?

makes sense to add OPT_P_INSTANCE, although I wonder why other activity/timeout knobs are not marks as such, i.e. --inactivity)

Not "add" OPT_P_INSTANCE, but "just" OPT_P_INSTANCE.

This is not something you want in the client config file.

gert

Signature · 6 lines
"If was one thing all people took for granted, was conviction that if you 
 feed honest figures into a computer, honest figures come out. Never doubted 
 it myself till I met a computer with a sense of humor."
                             Robert A. Heinlein, The Moon is a Harsh Mistress

Gert Doering - Munich, Germany                             [email protected]...
#4 lore raw

Hi,

Quoted 8 lines · Gert Doering
Quoted 1 line

we don't get here at all, if we have no event object that is timing out.

process_coarse_timers() is run once per second, and does not need additional timers to be called.

This new check is added to process_coarse_timers().

Why do we need an extra event object, again?

Mmmmh. I think we actually might need one, to ensure that this fires only *once*, and we can then do what is needed (like, explicit-exit-notify, reliable TLS processing, etc.) without raising more SIGTERM on every future visit here.

(Of course we could avoid this by resetting the session end time, but then it might be easier to understand if we use the existing mechanisms, even if a bit heavyweight)

gert

Signature · 6 lines
"If was one thing all people took for granted, was conviction that if you 
 feed honest figures into a computer, honest figures come out. Never doubted 
 it myself till I met a computer with a sense of humor."
                             Robert A. Heinlein, The Moon is a Harsh Mistress

Gert Doering - Munich, Germany                             [email protected]...
#5 lore raw

Hi,

Quoted 12 lines · Gert Doering

Hi,

Quoted 8 lines · Gert Doering
Quoted 1 line

we don't get here at all, if we have no event object that is timing out.

process_coarse_timers() is run once per second, and does not need additional timers to be called.

This new check is added to process_coarse_timers().

Why do we need an extra event object, again?

Quoted 8 lines

Mmmmh. I think we actually might need one, to ensure that this fires only *once*, and we can then do what is needed (like, explicit-exit-notify, reliable TLS processing, etc.) without raising more SIGTERM on every future visit here.

(Of course we could avoid this by resetting the session end time, but then it might be easier to understand if we use the existing mechanisms, even if a bit heavyweight)

I went down the multi-rabbit-hole and managed to get back alive.

The coarse timer is set to the lowest needed value across all the active timers (through event_timer_trigger()) or required TLS action. However we don't have specific code to handle a bunch of "one-shot" events.

For this reason the simplest approach is to set an event object and simply not rearm it after it is triggered. The rearm happens via _reset(), which we don't call. So this patch looks good to me as is.

Cheers,

Quoted 9 lines

gert

_______________________________________________
Openvpn-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openvpn-devel

Signature · 1 line
Antonio Quartulli
#6 lore raw

PATCH v2 1/1 implement --session-timeout

In reply to Antonio Quartulli

From: Dmitry Zelenkovsky <[email protected]...>

Disconnect clients after session-timeout expires. session-timeout can be defined in ccd files in order to limit per-user connection time.

---
Changes from v1:
* added documentation to manpage
* added entry in Changes.rst

---
 Changes.rst                         |  6 ++++++
 doc/man-sections/link-options.rst   | 15 +++++++++++++++
 doc/man-sections/server-options.rst |  2 +-
 src/openvpn/forward.c               | 22 ++++++++++++++++++++++
 src/openvpn/init.c                  |  7 +++++++
 src/openvpn/openvpn.h               |  2 ++
 src/openvpn/options.c               |  7 +++++++
 src/openvpn/options.h               |  2 ++
 8 files changed, 62 insertions(+), 1 deletion(-)
Changes.rst +6 -0
index 2daa97fb..7c45a042 100644
@@ -93,6 +93,12 @@ Inline auth username and password
     missing OpenVPN will prompt for input via stdin. This applies to inline'd
     http-proxy-user-pass too.
 
+Session timeout
+    It is now possible to terminate a session (or all) after a specified amount
+    of seconds has passed session commencement. This behaviour can be configured
+    using ``--session-timeout``. This option can be configured on the server, on
+    the client or can also be pushed.
+
 
 Deprecated features
 -------------------
doc/man-sections/link-options.rst +15 -0
index 373193aa..5b310d92 100644
@@ -427,6 +427,21 @@ the local and the remote host.
   default) and you are using either ``--secret`` (shared-secret key mode)
   or TLS mode with ``--tls-auth``.
 
+--session-timeout n
+  Raises :code:`SIGTERM` for the client instance after ``n`` seconds since
+  the beginning of the session, forcing OpenVPN to disconnect.
+  In client mode, OpenVPN will disconnect and exit, while in server mode
+  all client sessions are terminated.
+
+  This option can also be specified in a client instance config file
+  using ``--client-config-dir`` or dynamically generated using a
+  ``--client-connect`` script. In these cases, only the related client
+  session is terminated.
+
+  When using option on the server side, it may be useful to push
+  ``--explicit-exit-notify`` in order to terminate a client session
+  and be informed about it.
+
 --socket-flags flags
   Apply the given flags to the OpenVPN transport socket. Currently, only
   :code:`TCP_NODELAY` is supported.
doc/man-sections/server-options.rst +1 -1
index 54ea8b66..9d0c73b6 100644
@@ -426,7 +426,7 @@ fast hardware. SSL/TLS authentication must be used in this mode.
   ``--inactive``, ``--ping``, ``--ping-exit``, ``--ping-restart``,
   ``--setenv``, ``--auth-token``, ``--persist-key``, ``--persist-tun``,
   ``--echo``, ``--comp-lzo``, ``--socket-flags``, ``--sndbuf``,
-  ``--rcvbuf``
+  ``--rcvbuf``, ``--session-timeout``
 
 --push-remove opt
   Selectively remove all ``--push`` options matching "opt" from the option
src/openvpn/forward.c +22 -0
index e5cee665..810cb8a7 100644
@@ -630,6 +630,21 @@ encrypt_sign(struct context *c, bool comp_frag)
     buffer_turnover(orig_buf, &c->c2.to_link, &c->c2.buf, &b->read_tun_buf);
 }
 
+/*
+ * Should we exit due to session timeout?
+ */
+static void
+check_session_timeout(struct context *c)
+{
+    if (c->options.session_timeout
+        && event_timeout_trigger(&c->c2.session_interval, &c->c2.timeval,
+                                 ETT_DEFAULT))
+    {
+        msg(M_INFO, "Session timeout, exiting");
+        register_signal(c, SIGTERM, "session-timeout");
+    }
+}
+
 /*
  * Coarse timers work to 1 second resolution.
  */
@@ -681,6 +696,13 @@ process_coarse_timers(struct context *c)
         return;
     }
 
+    /* kill session if time is over */
+    check_session_timeout(c);
+    if (c->sig->signal_received)
+    {
+        return;
+    }
+
     /* restart if ping not received */
     check_ping_restart(c);
     if (c->sig->signal_received)
src/openvpn/init.c +7 -0
index f2db8dd9..7b817612 100644
@@ -1322,6 +1322,13 @@ do_init_timers(struct context *c, bool deferred)
         event_timeout_init(&c->c2.inactivity_interval, c->options.inactivity_timeout, now);
     }
 
+    /* initialize inactivity timeout */
+    if (c->options.session_timeout)
+    {
+        event_timeout_init(&c->c2.session_interval, c->options.session_timeout,
+                           now);
+    }
+
     /* initialize pings */
     if (dco_enabled(&c->options))
     {
src/openvpn/openvpn.h +2 -0
index 00cd652f..f74125aa 100644
@@ -288,6 +288,8 @@ struct context_2
     struct event_timeout inactivity_interval;
     int64_t inactivity_bytes;
 
+    struct event_timeout session_interval;
+
     /* the option strings must match across peers */
     char *options_string_local;
     char *options_string_remote;
src/openvpn/options.c +7 -0
index 93db0865..4566172b 100644
@@ -261,6 +261,7 @@ static const char usage_message[] =
     "                  for m seconds.\n"
     "--inactive n [bytes] : Exit after n seconds of activity on tun/tap device\n"
     "                  produces a combined in/out byte count < bytes.\n"
+    "--session-timeout n: Limit connection time to n seconds.\n"
     "--ping-exit n   : Exit if n seconds pass without reception of remote ping.\n"
     "--ping-restart n: Restart if n seconds pass without reception of remote ping.\n"
     "--ping-timer-rem: Run the --ping-exit/--ping-restart timer only if we have a\n"
@@ -1823,6 +1824,7 @@ show_settings(const struct options *o)
     SHOW_INT(keepalive_ping);
     SHOW_INT(keepalive_timeout);
     SHOW_INT(inactivity_timeout);
+    SHOW_INT(session_timeout);
     SHOW_INT64(inactivity_minimum_bytes);
     SHOW_INT(ping_send_timeout);
     SHOW_INT(ping_rec_timeout);
@@ -6598,6 +6600,11 @@ add_option(struct options *options,
             }
         }
     }
+    else if (streq(p[0], "session-timeout") && p[1] && !p[2])
+    {
+        VERIFY_PERMISSION(OPT_P_TIMER);
+        options->session_timeout = positive_atoi(p[1]);
+    }
     else if (streq(p[0], "proto") && p[1] && !p[2])
     {
         int proto;
src/openvpn/options.h +2 -0
index 4332acd3..2fb85b58 100644
@@ -317,6 +317,8 @@ struct options
     int inactivity_timeout;     /* --inactive */
     int64_t inactivity_minimum_bytes;
 
+    int session_timeout;        /* Kill session after n seconds, regardless activity */
+
     int ping_send_timeout;      /* Send a TCP/UDP ping to remote every n seconds */
     int ping_rec_timeout;       /* Expect a TCP/UDP ping from remote at least once every n seconds */
     bool ping_timer_remote;     /* Run ping timer only if we have a remote address */
Signature · 1 line
2.35.1
#7 lore raw

Hi,

I have a few documentation / comment language issues, and a bit of grumbling about the code (which can be ignored).

Quoted 4 lines · Antonio Quartulli

+--session-timeout n

doc/man-sections/link-options.rst +3 -0
+  Raises :code:`SIGTERM` for the client instance after ``n`` seconds since
+  the beginning of the session, forcing OpenVPN to disconnect.
+  In client mode, OpenVPN will disconnect and exit, while in server mode

doc/man-sections/link-options.rst the local and the remote host. · line 443

Quoted 10 lines
doc/man-sections/link-options.rst +10 -0
+  all client sessions are terminated.
+
+  This option can also be specified in a client instance config file
+  using ``--client-config-dir`` or dynamically generated using a
+  ``--client-connect`` script. In these cases, only the related client
+  session is terminated.
+
+  When using option on the server side, it may be useful to push
+  ``--explicit-exit-notify`` in order to terminate a client session
+  and be informed about it.

This paragraph is a bit unclear. Why would you want to *push* --explicit-exit-notify? *Use* it, on the server, so the client gets an EEN, this I would understand.

Maybe reword this as

"If this is used on the server (main config or per-client), this works better if ``--explicit-exit-notify`` is also used in the server config, so clients are not just silently disconnected but get told about it."

Quoted 44 lines
doc/man-sections/link-options.rst +1 -0
+
 --socket-flags flags
   Apply the given flags to the OpenVPN transport socket. Currently, only
   :code:`TCP_NODELAY` is supported.
doc/man-sections/server-options.rst +1 -1
index 54ea8b66..9d0c73b6 100644
@@ -426,7 +426,7 @@ fast hardware. SSL/TLS authentication must be used in this mode.
   ``--inactive``, ``--ping``, ``--ping-exit``, ``--ping-restart``,
   ``--setenv``, ``--auth-token``, ``--persist-key``, ``--persist-tun``,
   ``--echo``, ``--comp-lzo``, ``--socket-flags``, ``--sndbuf``,
-  ``--rcvbuf``
+  ``--rcvbuf``, ``--session-timeout``
 
 --push-remove opt
   Selectively remove all ``--push`` options matching "opt" from the option
src/openvpn/forward.c +15 -0
index e5cee665..810cb8a7 100644
@@ -630,6 +630,21 @@ encrypt_sign(struct context *c, bool comp_frag)
     buffer_turnover(orig_buf, &c->c2.to_link, &c->c2.buf, &b->read_tun_buf);
 }
 
+/*
+ * Should we exit due to session timeout?
+ */
+static void
+check_session_timeout(struct context *c)
+{
+    if (c->options.session_timeout
+        && event_timeout_trigger(&c->c2.session_interval, &c->c2.timeval,
+                                 ETT_DEFAULT))
+    {
+        msg(M_INFO, "Session timeout, exiting");
+        register_signal(c, SIGTERM, "session-timeout");
+    }
+}
+
 /*
  * Coarse timers work to 1 second resolution.
  */

src/openvpn/forward.c process_coarse_timers(struct context *c) · line 704

Quoted 9 lines
src/openvpn/forward.c +6 -0
@@ -681,6 +696,13 @@ process_coarse_timers(struct context *c)
         return;
     }
 
+    /* kill session if time is over */
+    check_session_timeout(c);
+    if (c->sig->signal_received)
+    {
+        return;
+    }

This now-triple "if (c->sig->signal_received)" still rubs my sensitivies, but I'm not sure if it's worth rewriting check_session_timeout() and check_ping_restart() to return a bool instead, and make this

    if (check_session_timeout(c))		/* game over? */
    {
        return;
    }

    if (check_ping_restart(c))			/* peer went to sleep? */
    {
        return;
    }

instead...

But anyway, this is more "material for a general cleanup patch", this patch is OK as it is, code-wise.

src/openvpn/forward.c process_coarse_timers(struct context *c) · line 705

Quoted 1 line
src/openvpn/forward.c +1 -0
+    int session_timeout;        /* Kill session after n seconds, regardless activity */

"regardless activity" is no good, "regardless of activity" is getting a bit long.

What about "/* force-kill session after n seconds */"?

gert

Signature · 6 lines
"If was one thing all people took for granted, was conviction that if you 
 feed honest figures into a computer, honest figures come out. Never doubted 
 it myself till I met a computer with a sense of humor."
                             Robert A. Heinlein, The Moon is a Harsh Mistress

Gert Doering - Munich, Germany                             [email protected]...
#8 lore raw

PATCH v3 1/1 implement --session-timeout

In reply to Antonio Quartulli

From: Dmitry Zelenkovsky <[email protected]...>

Disconnect clients after session-timeout expires. session-timeout can be defined in ccd files in order to limit per-user connection time.

---
Changes from v2:
* improve manpage wording
* improve session_timeout comment

Changes from v1:
* added documentation to manpage
* added entry in Changes.rst

---
 Changes.rst                         |  6 ++++++
 doc/man-sections/link-options.rst   | 16 ++++++++++++++++
 doc/man-sections/server-options.rst |  2 +-
 src/openvpn/forward.c               | 22 ++++++++++++++++++++++
 src/openvpn/init.c                  |  7 +++++++
 src/openvpn/openvpn.h               |  2 ++
 src/openvpn/options.c               |  7 +++++++
 src/openvpn/options.h               |  2 ++
 8 files changed, 63 insertions(+), 1 deletion(-)
Changes.rst +6 -0
index 2daa97fb..7c45a042 100644
@@ -93,6 +93,12 @@ Inline auth username and password
     missing OpenVPN will prompt for input via stdin. This applies to inline'd
     http-proxy-user-pass too.
 
+Session timeout
+    It is now possible to terminate a session (or all) after a specified amount
+    of seconds has passed session commencement. This behaviour can be configured
+    using ``--session-timeout``. This option can be configured on the server, on
+    the client or can also be pushed.
+
 
 Deprecated features
 -------------------
doc/man-sections/link-options.rst +16 -0
index 373193aa..fe7760d7 100644
@@ -427,6 +427,22 @@ the local and the remote host.
   default) and you are using either ``--secret`` (shared-secret key mode)
   or TLS mode with ``--tls-auth``.
 
+--session-timeout n
+  Raises :code:`SIGTERM` for the client instance after ``n`` seconds since
+  the beginning of the session, forcing OpenVPN to disconnect.
+  In client mode, OpenVPN will disconnect and exit, while in server mode
+  all client sessions are terminated.
+
+  This option can also be specified in a client instance config file
+  using ``--client-config-dir`` or dynamically generated using a
+  ``--client-connect`` script. In these cases, only the related client
+  session is terminated.
+
+  If this option is used on the server (main config or per-client),
+  this works better if ``--explicit-exit-notify`` is also specified
+  in the server config, so clients are not just silently disconnected
+  but they are also informed about it.
+
 --socket-flags flags
   Apply the given flags to the OpenVPN transport socket. Currently, only
   :code:`TCP_NODELAY` is supported.
doc/man-sections/server-options.rst +1 -1
index 54ea8b66..9d0c73b6 100644
@@ -426,7 +426,7 @@ fast hardware. SSL/TLS authentication must be used in this mode.
   ``--inactive``, ``--ping``, ``--ping-exit``, ``--ping-restart``,
   ``--setenv``, ``--auth-token``, ``--persist-key``, ``--persist-tun``,
   ``--echo``, ``--comp-lzo``, ``--socket-flags``, ``--sndbuf``,
-  ``--rcvbuf``
+  ``--rcvbuf``, ``--session-timeout``
 
 --push-remove opt
   Selectively remove all ``--push`` options matching "opt" from the option
src/openvpn/forward.c +22 -0
index e5cee665..810cb8a7 100644
@@ -630,6 +630,21 @@ encrypt_sign(struct context *c, bool comp_frag)
     buffer_turnover(orig_buf, &c->c2.to_link, &c->c2.buf, &b->read_tun_buf);
 }
 
+/*
+ * Should we exit due to session timeout?
+ */
+static void
+check_session_timeout(struct context *c)
+{
+    if (c->options.session_timeout
+        && event_timeout_trigger(&c->c2.session_interval, &c->c2.timeval,
+                                 ETT_DEFAULT))
+    {
+        msg(M_INFO, "Session timeout, exiting");
+        register_signal(c, SIGTERM, "session-timeout");
+    }
+}
+
 /*
  * Coarse timers work to 1 second resolution.
  */
@@ -681,6 +696,13 @@ process_coarse_timers(struct context *c)
         return;
     }
 
+    /* kill session if time is over */
+    check_session_timeout(c);
+    if (c->sig->signal_received)
+    {
+        return;
+    }
+
     /* restart if ping not received */
     check_ping_restart(c);
     if (c->sig->signal_received)
src/openvpn/init.c +7 -0
index 22a0e9c6..439ea010 100644
@@ -1324,6 +1324,13 @@ do_init_timers(struct context *c, bool deferred)
         event_timeout_init(&c->c2.inactivity_interval, c->options.inactivity_timeout, now);
     }
 
+    /* initialize inactivity timeout */
+    if (c->options.session_timeout)
+    {
+        event_timeout_init(&c->c2.session_interval, c->options.session_timeout,
+                           now);
+    }
+
     /* initialize pings */
     if (dco_enabled(&c->options))
     {
src/openvpn/openvpn.h +2 -0
index 00cd652f..f74125aa 100644
@@ -288,6 +288,8 @@ struct context_2
     struct event_timeout inactivity_interval;
     int64_t inactivity_bytes;
 
+    struct event_timeout session_interval;
+
     /* the option strings must match across peers */
     char *options_string_local;
     char *options_string_remote;
src/openvpn/options.c +7 -0
index 93db0865..4566172b 100644
@@ -261,6 +261,7 @@ static const char usage_message[] =
     "                  for m seconds.\n"
     "--inactive n [bytes] : Exit after n seconds of activity on tun/tap device\n"
     "                  produces a combined in/out byte count < bytes.\n"
+    "--session-timeout n: Limit connection time to n seconds.\n"
     "--ping-exit n   : Exit if n seconds pass without reception of remote ping.\n"
     "--ping-restart n: Restart if n seconds pass without reception of remote ping.\n"
     "--ping-timer-rem: Run the --ping-exit/--ping-restart timer only if we have a\n"
@@ -1823,6 +1824,7 @@ show_settings(const struct options *o)
     SHOW_INT(keepalive_ping);
     SHOW_INT(keepalive_timeout);
     SHOW_INT(inactivity_timeout);
+    SHOW_INT(session_timeout);
     SHOW_INT64(inactivity_minimum_bytes);
     SHOW_INT(ping_send_timeout);
     SHOW_INT(ping_rec_timeout);
@@ -6598,6 +6600,11 @@ add_option(struct options *options,
             }
         }
     }
+    else if (streq(p[0], "session-timeout") && p[1] && !p[2])
+    {
+        VERIFY_PERMISSION(OPT_P_TIMER);
+        options->session_timeout = positive_atoi(p[1]);
+    }
     else if (streq(p[0], "proto") && p[1] && !p[2])
     {
         int proto;
src/openvpn/options.h +2 -0
index 6449dcdf..3d1d37d0 100644
@@ -324,6 +324,8 @@ struct options
     int inactivity_timeout;     /* --inactive */
     int64_t inactivity_minimum_bytes;
 
+    int session_timeout;        /* Force-kill session after n seconds */
+
     int ping_send_timeout;      /* Send a TCP/UDP ping to remote every n seconds */
     int ping_rec_timeout;       /* Expect a TCP/UDP ping from remote at least once every n seconds */
     bool ping_timer_remote;     /* Run ping timer only if we have a remote address */
Signature · 1 line
2.35.1
Acked-by on v3 1/1
#9 lore raw

This is a useful addition for situations with external constraints ("this VPN access may only be used between 10:00 and 18:00", so you can ensure the session ends at 17:59:59 without having extra managmeent logic around). Whether it's overly useful in "global server context" or on the client side stands to be debated - but this needs no extra code, so "it's just there and someone might find use for it".

I have tested client-side (works), server-side/global (will terminate each instance <n> seconds after connecting, but not itself) and server-side/ccd (per-instance kill switch with per-instance timer).

Unfortunately, the man page addition about explicit-exit-notify is wrong - on the server side, it never sends notifies, it just kills the client TLS instance...

2022-10-07 18:05:13 us=256095 cron2-freebsd-tc-amd64/194.97.140.21:29079 Session timeout, exiting
2022-10-07 18:05:13 us=256170 cron2-freebsd-tc-amd64/194.97.140.21:29079 SIGTERM[soft,session-timeout] received, client-instance exiting

.. without telling the client, so that one needs to run into --ping timeout

2022-10-07 18:05:42 [server] Inactivity timeout (--ping-restart), restarting

.. 30 seconds later, which is not really satisfying...

Can we do better?

I have, for the time being, removed the offending man page section about --explicit-exit-notify and merged the rest (no code change).

Your patch has been applied to the master branch.

commit f96290ff901f62717fdb4c1adef72142f359e992

Author: Dmitry Zelenkovsky
Date:   Thu Oct 6 22:37:31 2022 +0200
     implement --session-timeout

     Signed-off-by: Dmitry Zelenkovsky <[email protected]...>
     Acked-by: Gert Doering <[email protected]...>
     Message-Id: <[email protected]...>
     URL: https://www.mail-archive.com/[email protected]/msg25352.html
     Signed-off-by: Gert Doering <[email protected]...>

--
kind regards,

Gert Doering