Skip to contentSkip to conversation

Allow changing fallback cipher from ccd files/client-connect

PATCH In review Arne Schwabe 5 participants 59 messages Jul 11, 2020 – Jul 16, 2020

lore
Share & tools
lore
#0 lore raw

PATCH v5 1/14 Allow changing fallback cipher from ccd files/client-connect

This allows to control the fallback cipher that is used when the client/server do have any common cipher on a per client basis.

The patch is similar to Steffan's
[PATCH v4] Allow changing cipher from a ccd file.

Steffan's old patch also moves the cipher negotiation to multi_established_connection() which I independently discovered and implemented in

Extract process_incoming_push_reply from process_incoming_push_msg (#FIXME add commitsh when commited to master)

---
 src/openvpn/options.c | 2 +-
 src/openvpn/options.h | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
src/openvpn/options.c +1 -1
index b93fd4fe..bf2760e1 100644
@@ -7892,7 +7892,7 @@ add_option(struct options *options,
     }
     else if (streq(p[0], "cipher") && p[1] && !p[2])
     {
-        VERIFY_PERMISSION(OPT_P_NCP);
+        VERIFY_PERMISSION(OPT_P_NCP|OPT_P_INSTANCE);
         options->ciphername = p[1];
     }
     else if (streq(p[0], "ncp-ciphers") && p[1] && !p[2])
src/openvpn/options.h +1 -1
index c83a46aa..c37006d3 100644
@@ -677,7 +677,7 @@ struct options
 #define OPT_P_MTU             (1<<14) /* TODO */
 #define OPT_P_NICE            (1<<15)
 #define OPT_P_PUSH            (1<<16)
-#define OPT_P_INSTANCE        (1<<17)
+#define OPT_P_INSTANCE        (1<<17) /**< allowed in ccd, client-connect etc*/
 #define OPT_P_CONFIG          (1<<18)
 #define OPT_P_EXPLICIT_NOTIFY (1<<19)
 #define OPT_P_ECHO            (1<<20)
Signature · 1 line
2.26.2
#1 lore raw

PATCH v5 2/14 client-connect: Split multi_connection_established into separate functions

From: Fabian Knittel <[email protected]...>

This patch splits up the multi_connection_established() function. Each new helper function does a specific job. Functions that do a similar job receive a similar calling interface.

The patch tries not to reindent code, so that the real changes are as clearly visible as possible. (A follow-up patch will only do indentation changes.)

PATCH v3: Since the code has changed enough from the time the original patch to the current master, the splitting has been redone from the current code. Also some style and minor code changes have been added doing this patch. This elimininates and the big reformatting done before eliminates the follow up patch with indentation changes.

The original patch already replaces some instances of option_permission_mask with CLIENT_CONNECT_OPT_MASK. The V3 version does this more consistenly.

Patch v4: Move config -> mi->cc_config into its own commit

Patch v5: Clean up some minor issues, add one missing check on temporary file deletion, rebase on latest master.

---
 src/openvpn/multi.c | 588 ++++++++++++++++++++++++++------------------
 src/openvpn/multi.h |   4 +-
 2 files changed, 350 insertions(+), 242 deletions(-)
src/openvpn/multi.c +347 -241
index a2af071a..3c4ceeb5 100644
@@ -1638,7 +1638,6 @@ static void
 multi_client_connect_post(struct multi_context *m,
                           struct multi_instance *mi,
                           const char *dc_file,
-                          unsigned int option_permissions_mask,
                           unsigned int *option_types_found)
 {
     /* Did script generate a dynamic config file? */
@@ -1647,7 +1646,7 @@ multi_client_connect_post(struct multi_context *m,
         options_server_import(&mi->context.options,
                               dc_file,
                               D_IMPORT_ERRORS|M_OPTERR,
-                              option_permissions_mask,
+                              CLIENT_CONNECT_OPT_MASK,
                               option_types_found,
                               mi->context.c2.es);
 
@@ -1671,7 +1670,6 @@ static void
 multi_client_connect_post_plugin(struct multi_context *m,
                                  struct multi_instance *mi,
                                  const struct plugin_return *pr,
-                                 unsigned int option_permissions_mask,
                                  unsigned int *option_types_found)
 {
     struct plugin_return config;
@@ -1689,7 +1687,7 @@ multi_client_connect_post_plugin(struct multi_context *m,
                 options_string_import(&mi->context.options,
                                       config.list[i]->value,
                                       D_IMPORT_ERRORS|M_OPTERR,
-                                      option_permissions_mask,
+                                      CLIENT_CONNECT_OPT_MASK,
                                       option_types_found,
                                       mi->context.c2.es);
             }
@@ -1716,7 +1714,6 @@ multi_client_connect_post_plugin(struct multi_context *m,
 static void
 multi_client_connect_mda(struct multi_context *m,
                          struct multi_instance *mi,
-                         unsigned int option_permissions_mask,
                          unsigned int *option_types_found)
 {
     if (mi->cc_config)
@@ -1729,7 +1726,7 @@ multi_client_connect_mda(struct multi_context *m,
             options_string_import(&mi->context.options,
                                   opt,
                                   D_IMPORT_ERRORS|M_OPTERR,
-                                  option_permissions_mask,
+                                  CLIENT_CONNECT_OPT_MASK,
                                   option_types_found,
                                   mi->context.c2.es);
         }
@@ -1843,160 +1840,46 @@ multi_client_set_protocol_options(struct context *c)
     }
 }
 
-/**
- * Generates the data channel keys
- */
-static bool
-multi_client_generate_tls_keys(struct context *c)
-{
-    struct frame *frame_fragment = NULL;
-#ifdef ENABLE_FRAGMENT
-    if (c->options.ce.fragment)
-    {
-        frame_fragment = &c->c2.frame_fragment;
-    }
-#endif
-    struct tls_session *session = &c->c2.tls_multi->session[TM_ACTIVE];
-    if (!tls_session_update_crypto_params(session, &c->options,
-                                          &c->c2.frame, frame_fragment))
-    {
-        msg(D_TLS_ERRORS, "TLS Error: initializing data channel failed");
-        register_signal(c, SIGUSR1, "process-push-msg-failed");
-        return false;
-    }
-
-    return true;
-}
-
-/*
- * Called as soon as the SSL/TLS connection authenticates.
- *
- * Instance-specific directives to be processed:
- *
- *   iroute start-ip end-ip
- *   ifconfig-push local remote-netmask
- *   push
- */
 static void
-multi_connection_established(struct multi_context *m, struct multi_instance *mi)
+multi_client_connect_call_plugin_v1(struct multi_context *m,
+                                    struct multi_instance *mi,
+                                    unsigned int *option_types_found,
+                                    int *cc_succeeded,
+                                    int *cc_succeeded_count)
 {
-    if (tls_authentication_status(mi->context.c2.tls_multi, 0) != TLS_AUTHENTICATION_SUCCEEDED)
-    {
-        return;
-    }
-
-    struct gc_arena gc = gc_new();
-    unsigned int option_types_found = 0;
-
-    const unsigned int option_permissions_mask =
-        OPT_P_INSTANCE
-        | OPT_P_INHERIT
-        | OPT_P_PUSH
-        | OPT_P_TIMER
-        | OPT_P_CONFIG
-        | OPT_P_ECHO
-        | OPT_P_COMP
-        | OPT_P_SOCKFLAGS;
-
-    int cc_succeeded = true;     /* client connect script status */
-    int cc_succeeded_count = 0;
-
-    ASSERT(mi->context.c1.tuntap);
-
-    /* lock down the common name and cert hashes so they can't change during future TLS renegotiations */
-    tls_lock_common_name(mi->context.c2.tls_multi);
-    tls_lock_cert_hash_set(mi->context.c2.tls_multi);
-
-    /* generate a msg() prefix for this client instance */
-    generate_prefix(mi);
-
-    /* delete instances of previous clients with same common-name */
-    if (!mi->context.options.duplicate_cn)
-    {
-        multi_delete_dup(m, mi);
-    }
-
-    /* reset pool handle to null */
-    mi->vaddr_handle = -1;
-
-    /*
-     * Try to source a dynamic config file from the
-     * --client-config-dir directory.
-     */
-    if (mi->context.options.client_config_dir)
-    {
-        const char *ccd_file;
-
-        ccd_file = platform_gen_path(mi->context.options.client_config_dir,
-                                     tls_common_name(mi->context.c2.tls_multi,
-                                                     false),
-                                     &gc);
-
-        /* try common-name file */
-        if (platform_test_file(ccd_file))
-        {
-            options_server_import(&mi->context.options,
-                                  ccd_file,
-                                  D_IMPORT_ERRORS|M_OPTERR,
-                                  option_permissions_mask,
-                                  &option_types_found,
-                                  mi->context.c2.es);
-        }
-        else     /* try default file */
-        {
-            ccd_file = platform_gen_path(mi->context.options.client_config_dir,
-                                         CCD_DEFAULT,
-                                         &gc);
-
-            if (platform_test_file(ccd_file))
-            {
-                options_server_import(&mi->context.options,
-                                      ccd_file,
-                                      D_IMPORT_ERRORS|M_OPTERR,
-                                      option_permissions_mask,
-                                      &option_types_found,
-                                      mi->context.c2.es);
-            }
-        }
-    }
-
-    /*
-     * Select a virtual address from either --ifconfig-push in --client-config-dir file
-     * or --ifconfig-pool.
-     */
-    multi_select_virtual_addr(m, mi);
-
-    /* do --client-connect setenvs */
-    multi_client_connect_setenv(m, mi);
-
 #ifdef ENABLE_PLUGIN
-    /*
-     * Call client-connect plug-in.
-     */
+    ASSERT(m);
+    ASSERT(mi);
+    ASSERT(option_types_found);
+    ASSERT(cc_succeeded);
+    ASSERT(cc_succeeded_count);
 
     /* deprecated callback, use a file for passing back return info */
     if (plugin_defined(mi->context.plugins, OPENVPN_PLUGIN_CLIENT_CONNECT))
     {
         struct argv argv = argv_new();
-        const char *dc_file = platform_create_temp_file(mi->context.options.tmp_dir,
-                                                        "cc", &gc);
+        struct gc_arena gc = gc_new();
+        const char *dc_file =
+            platform_create_temp_file(mi->context.options.tmp_dir, "cc", &gc);
 
         if (!dc_file)
         {
             cc_succeeded = false;
-            goto script_depr_failed;
+            goto cleanup;
         }
 
         argv_printf(&argv, "%s", dc_file);
-        if (plugin_call(mi->context.plugins, OPENVPN_PLUGIN_CLIENT_CONNECT, &argv, NULL, mi->context.c2.es) != OPENVPN_PLUGIN_FUNC_SUCCESS)
+        if (plugin_call(mi->context.plugins, OPENVPN_PLUGIN_CLIENT_CONNECT,
+                        &argv, NULL, mi->context.c2.es)
+            != OPENVPN_PLUGIN_FUNC_SUCCESS)
         {
             msg(M_WARN, "WARNING: client-connect plugin call failed");
-            cc_succeeded = false;
+            *cc_succeeded = false;
         }
         else
         {
-            multi_client_connect_post(m, mi, dc_file, option_permissions_mask, &option_types_found);
-            ++cc_succeeded_count;
+            multi_client_connect_post(m, mi, dc_file, option_types_found);
+            (*cc_succeeded_count)++;
         }
 
         if (!platform_unlink(dc_file))
@@ -2005,9 +1888,26 @@ multi_connection_established(struct multi_context *m, struct multi_instance *mi)
                 dc_file);
         }
 
-script_depr_failed:
+cleanup:
         argv_free(&argv);
+        gc_free(&gc);
     }
+#endif /* ifdef ENABLE_PLUGIN */
+}
+
+static void
+multi_client_connect_call_plugin_v2(struct multi_context *m,
+                                    struct multi_instance *mi,
+                                    unsigned int *option_types_found,
+                                    int *cc_succeeded,
+                                    int *cc_succeeded_count)
+{
+#ifdef ENABLE_PLUGIN
+    ASSERT(m);
+    ASSERT(mi);
+    ASSERT(option_types_found);
+    ASSERT(cc_succeeded);
+    ASSERT(cc_succeeded_count);
 
     /* V2 callback, use a plugin_return struct for passing back return info */
     if (plugin_defined(mi->context.plugins, OPENVPN_PLUGIN_CLIENT_CONNECT_V2))
@@ -2016,27 +1916,42 @@ script_depr_failed:
 
         plugin_return_init(&pr);
 
-        if (plugin_call(mi->context.plugins, OPENVPN_PLUGIN_CLIENT_CONNECT_V2, NULL, &pr, mi->context.c2.es) != OPENVPN_PLUGIN_FUNC_SUCCESS)
+        if (plugin_call(mi->context.plugins, OPENVPN_PLUGIN_CLIENT_CONNECT_V2,
+                        NULL, &pr, mi->context.c2.es)
+            != OPENVPN_PLUGIN_FUNC_SUCCESS)
         {
             msg(M_WARN, "WARNING: client-connect-v2 plugin call failed");
-            cc_succeeded = false;
+            *cc_succeeded = false;
         }
         else
         {
-            multi_client_connect_post_plugin(m, mi, &pr, option_permissions_mask, &option_types_found);
-            ++cc_succeeded_count;
+            multi_client_connect_post_plugin(m, mi, &pr, option_types_found);
+            (*cc_succeeded_count)++;
         }
 
         plugin_return_free(&pr);
     }
 #endif /* ifdef ENABLE_PLUGIN */
+}
 
-    /*
-     * Run --client-connect script.
-     */
-    if (mi->context.options.client_connect_script && cc_succeeded)
+
+
+/**
+ * Runs the --client-connect script if one is defined.
+ */
+static void
+multi_client_connect_call_script(struct multi_context *m,
+                                 struct multi_instance *mi,
+                                 unsigned int *option_types_found,
+                                 int *cc_succeeded,
+                                 int *cc_succeeded_count)
+{
+    ASSERT(m);
+    ASSERT(mi);
+    if (mi->context.options.client_connect_script)
     {
         struct argv argv = argv_new();
+        struct gc_arena gc = gc_new();
         const char *dc_file = NULL;
 
         setenv_str(mi->context.c2.es, "script_type", "client-connect");
@@ -2045,8 +1960,8 @@ script_depr_failed:
                                             "cc", &gc);
         if (!dc_file)
         {
-            cc_succeeded = false;
-            goto script_failed;
+            *cc_succeeded = false;
+            goto cleanup;
         }
 
         argv_parse_cmd(&argv, mi->context.options.client_connect_script);
@@ -2054,12 +1969,12 @@ script_depr_failed:
 
         if (openvpn_run_script(&argv, mi->context.c2.es, 0, "--client-connect"))
         {
-            multi_client_connect_post(m, mi, dc_file, option_permissions_mask, &option_types_found);
-            ++cc_succeeded_count;
+            multi_client_connect_post(m, mi, dc_file, option_types_found);
+            (*cc_succeeded_count)++;
         }
         else
         {
-            cc_succeeded = false;
+            *cc_succeeded = false;
         }
 
         if (!platform_unlink(dc_file))
@@ -2067,130 +1982,322 @@ script_depr_failed:
             msg(D_MULTI_ERRORS, "MULTI: problem deleting temporary file: %s",
                 dc_file);
         }
-
-script_failed:
+cleanup:
         argv_free(&argv);
+        gc_free(&gc);
     }
+}
 
-    /*
-     * Check for client-connect script left by management interface client
-     */
-#ifdef MANAGEMENT_DEF_AUTH
-    if (cc_succeeded && mi->cc_config)
+/**
+ * Generates the data channel keys
+ */
+static bool
+multi_client_generate_tls_keys(struct context *c)
+{
+  struct frame *frame_fragment = NULL;
+#ifdef ENABLE_FRAGMENT
+  if (c->options.ce.fragment)
     {
-        multi_client_connect_mda(m, mi, option_permissions_mask, &option_types_found);
-        ++cc_succeeded_count;
+      frame_fragment = &c->c2.frame_fragment;
     }
 #endif
+  struct tls_session *session = &c->c2.tls_multi->session[TM_ACTIVE];
+  if (!tls_session_update_crypto_params(session, &c->options,
+                                        &c->c2.frame, frame_fragment))
+    {
+      msg(D_TLS_ERRORS, "TLS Error: initializing data channel failed");
+      register_signal(c, SIGUSR1, "process-push-msg-failed");
+      return false;
+    }
 
+  return true;
+}
+
+static void
+multi_client_connect_late_setup(struct multi_context *m,
+                                struct multi_instance *mi,
+                                const unsigned int option_types_found)
+{
+    ASSERT(m);
+    ASSERT(mi);
+
+    struct gc_arena gc = gc_new();
     /*
-     * Check for "disable" directive in client-config-dir file
-     * or config file generated by --client-connect script.
+     * Process sourced options.
      */
-    if (mi->context.options.disable)
+    do_deferred_options(&mi->context, option_types_found);
+
+    /*
+     * make sure we got ifconfig settings from somewhere
+     */
+    if (!mi->context.c2.push_ifconfig_defined)
     {
-        msg(D_MULTI_ERRORS, "MULTI: client has been rejected due to 'disable' directive");
-        cc_succeeded = false;
-        cc_succeeded_count = 0;
+        msg(D_MULTI_ERRORS, "MULTI: no dynamic or static remote"
+            "--ifconfig address is available for %s",
+            multi_instance_string(mi, false, &gc));
     }
 
-    if (cc_succeeded)
+    /*
+     * make sure that ifconfig settings comply with constraints
+     */
+    if (!ifconfig_push_constraint_satisfied(&mi->context))
     {
-        /*
-         * Process sourced options.
-         */
-        do_deferred_options(&mi->context, option_types_found);
+        const char *ifconfig_constraint_network =
+            print_in_addr_t(mi->context.options.push_ifconfig_constraint_network, 0, &gc);
+        const char *ifconfig_constraint_netmask =
+            print_in_addr_t(mi->context.options.push_ifconfig_constraint_netmask, 0, &gc);
 
-        /*
-         * make sure we got ifconfig settings from somewhere
-         */
-        if (!mi->context.c2.push_ifconfig_defined)
+        /* JYFIXME -- this should cause the connection to fail */
+        msg(D_MULTI_ERRORS, "MULTI ERROR: primary virtual IP for %s (%s)"
+            "violates tunnel network/netmask constraint (%s/%s)",
+            multi_instance_string(mi, false, &gc),
+            print_in_addr_t(mi->context.c2.push_ifconfig_local, 0, &gc),
+            ifconfig_constraint_network, ifconfig_constraint_netmask);
+    }
+
+    /*
+     * For routed tunnels, set up internal route to endpoint
+     * plus add all iroute routes.
+     */
+    if (TUNNEL_TYPE(mi->context.c1.tuntap) == DEV_TYPE_TUN)
+    {
+        if (mi->context.c2.push_ifconfig_defined)
         {
-            msg(D_MULTI_ERRORS, "MULTI: no dynamic or static remote --ifconfig address is available for %s",
-                multi_instance_string(mi, false, &gc));
+            multi_learn_in_addr_t(m, mi,
+                                  mi->context.c2.push_ifconfig_local,
+                                  -1, true);
+            msg(D_MULTI_LOW, "MULTI: primary virtual IP for %s: %s",
+                multi_instance_string(mi, false, &gc),
+                print_in_addr_t(mi->context.c2.push_ifconfig_local, 0, &gc));
         }
 
-        /*
-         * make sure that ifconfig settings comply with constraints
-         */
-        if (!ifconfig_push_constraint_satisfied(&mi->context))
+        if (mi->context.c2.push_ifconfig_ipv6_defined)
         {
-            /* JYFIXME -- this should cause the connection to fail */
-            msg(D_MULTI_ERRORS, "MULTI ERROR: primary virtual IP for %s (%s) violates tunnel network/netmask constraint (%s/%s)",
+            multi_learn_in6_addr(m, mi,
+                                 mi->context.c2.push_ifconfig_ipv6_local,
+                                 -1, true);
+            /* TODO: find out where addresses are "unlearned"!! */
+            const char *ifconfig_local_ipv6 =
+                print_in6_addr(mi->context.c2.push_ifconfig_ipv6_local, 0, &gc);
+            msg(D_MULTI_LOW, "MULTI: primary virtual IPv6 for %s: %s",
                 multi_instance_string(mi, false, &gc),
-                print_in_addr_t(mi->context.c2.push_ifconfig_local, 0, &gc),
-                print_in_addr_t(mi->context.options.push_ifconfig_constraint_network, 0, &gc),
-                print_in_addr_t(mi->context.options.push_ifconfig_constraint_netmask, 0, &gc));
+                ifconfig_local_ipv6);
         }
 
+        /* add routes locally, pointing to new client, if
+         * --iroute options have been specified */
+        multi_add_iroutes(m, mi);
+
         /*
-         * For routed tunnels, set up internal route to endpoint
-         * plus add all iroute routes.
+         * iroutes represent subnets which are "owned" by a particular
+         * client.  Therefore, do not actually push a route to a client
+         * if it matches one of the client's iroutes.
          */
-        if (TUNNEL_TYPE(mi->context.c1.tuntap) == DEV_TYPE_TUN)
-        {
-            if (mi->context.c2.push_ifconfig_defined)
-            {
-                multi_learn_in_addr_t(m, mi, mi->context.c2.push_ifconfig_local, -1, true);
-                msg(D_MULTI_LOW, "MULTI: primary virtual IP for %s: %s",
-                    multi_instance_string(mi, false, &gc),
-                    print_in_addr_t(mi->context.c2.push_ifconfig_local, 0, &gc));
-            }
+        remove_iroutes_from_push_route_list(&mi->context.options);
+    }
+    else if (mi->context.options.iroutes)
+    {
+        msg(D_MULTI_ERRORS, "MULTI: --iroute options rejected for %s -- iroute"
+            "only works with tun-style tunnels",
+            multi_instance_string(mi, false, &gc));
+    }
 
-            if (mi->context.c2.push_ifconfig_ipv6_defined)
-            {
-                multi_learn_in6_addr(m, mi, mi->context.c2.push_ifconfig_ipv6_local, -1, true);
-                /* TODO: find out where addresses are "unlearned"!! */
-                msg(D_MULTI_LOW, "MULTI: primary virtual IPv6 for %s: %s",
-                    multi_instance_string(mi, false, &gc),
-                    print_in6_addr(mi->context.c2.push_ifconfig_ipv6_local, 0, &gc));
-            }
+    /* set our client's VPN endpoint for status reporting purposes */
+    mi->reporting_addr = mi->context.c2.push_ifconfig_local;
+    mi->reporting_addr_ipv6 = mi->context.c2.push_ifconfig_ipv6_local;
 
-            /* add routes locally, pointing to new client, if
-             * --iroute options have been specified */
-            multi_add_iroutes(m, mi);
+    /* set context-level authentication flag */
+    mi->context.c2.context_auth = CAS_SUCCEEDED;
 
-            /*
-             * iroutes represent subnets which are "owned" by a particular
-             * client.  Therefore, do not actually push a route to a client
-             * if it matches one of the client's iroutes.
-             */
-            remove_iroutes_from_push_route_list(&mi->context.options);
-        }
-        else if (mi->context.options.iroutes)
-        {
-            msg(D_MULTI_ERRORS, "MULTI: --iroute options rejected for %s -- iroute only works with tun-style tunnels",
-                multi_instance_string(mi, false, &gc));
-        }
+    /* authentication complete, calculate dynamic client specific options */
+    multi_client_set_protocol_options(&mi->context);
+
+    /* Generate data channel keys */
+    if (!multi_client_generate_tls_keys(&mi->context))
+    {
+        mi->context.c2.context_auth = CAS_FAILED;
+    }
 
-        /* set our client's VPN endpoint for status reporting purposes */
-        mi->reporting_addr = mi->context.c2.push_ifconfig_local;
-        mi->reporting_addr_ipv6 = mi->context.c2.push_ifconfig_ipv6_local;
+    /* send push reply if ready */
+    if (mi->context.c2.push_request_received)
+    {
+        process_incoming_push_request(&mi->context);
+    }
 
-        /* set context-level authentication flag */
-        mi->context.c2.context_auth = CAS_SUCCEEDED;
+    gc_free(&gc);
+}
+
+static void
+multi_client_connect_early_setup(struct multi_context *m,
+                                 struct multi_instance *mi)
+{
+    ASSERT(mi->context.c1.tuntap);
+    /*
+     * lock down the common name and cert hashes so they can't change
+     * during future TLS renegotiations
+     */
+    tls_lock_common_name(mi->context.c2.tls_multi);
+    tls_lock_cert_hash_set(mi->context.c2.tls_multi);
+
+    /* generate a msg() prefix for this client instance */
+    generate_prefix(mi);
+
+    /* delete instances of previous clients with same common-name */
+    if (!mi->context.options.duplicate_cn)
+    {
+        multi_delete_dup(m, mi);
+    }
+
+    /* reset pool handle to null */
+    mi->vaddr_handle = -1;
+}
+
+/**
+ * Try to source a dynamic config file from the
+ * --client-config-dir directory.
+ */
+static void
+multi_client_connect_source_ccd(struct multi_context *m,
+                                struct multi_instance *mi,
+                                unsigned int *option_types_found)
+{
+    if (mi->context.options.client_config_dir)
+    {
+        struct gc_arena gc = gc_new();
+        const char *ccd_file;
 
-        /* authentication complete, calculate dynamic client specific options */
-        multi_client_set_protocol_options(&mi->context);
+        ccd_file = platform_gen_path(mi->context.options.client_config_dir,
+                                     tls_common_name(mi->context.c2.tls_multi,
+                                                     false),
+                                     &gc);
 
-        /* Generate data channel keys */
-        if (!multi_client_generate_tls_keys(&mi->context))
+        /* try common-name file */
+        if (platform_test_file(ccd_file))
         {
-            mi->context.c2.context_auth = CAS_FAILED;
+            options_server_import(&mi->context.options,
+                                  ccd_file,
+                                  D_IMPORT_ERRORS|M_OPTERR,
+                                  CLIENT_CONNECT_OPT_MASK,
+                                  option_types_found,
+                                  mi->context.c2.es);
         }
-
-        /* send push reply if ready */
-        if (mi->context.c2.push_request_received)
+        else /* try default file */
         {
-            process_incoming_push_request(&mi->context);
+            ccd_file = platform_gen_path(mi->context.options.client_config_dir,
+                                         CCD_DEFAULT,
+                                         &gc);
+
+            if (platform_test_file(ccd_file))
+            {
+                options_server_import(&mi->context.options,
+                                      ccd_file,
+                                      D_IMPORT_ERRORS|M_OPTERR,
+                                      CLIENT_CONNECT_OPT_MASK,
+                                      option_types_found,
+                                      mi->context.c2.es);
+            }
         }
+        gc_free(&gc);
+    }
+}
+
+/*
+ * Called as soon as the SSL/TLS connection is authenticated.
+ *
+ * Will collect the client specific configuration from the different
+ * sources like ccd files, connect plugins and management interface.
+ *
+ * This method starts with cas_context CAS_PENDING and will move the
+ * state machine to either CAS_SUCCEEDED on success or
+ * CAS_FAILED/CAS_PARTIAL on failure.
+ *
+ * Instance-specific directives to be processed (CLIENT_CONNECT_OPT_MASK)
+ * include:
+ *
+ *   iroute start-ip end-ip
+ *   ifconfig-push local remote-netmask
+ *   push
+ *
+ *
+ */
+static void
+multi_connection_established(struct multi_context *m, struct multi_instance *mi)
+{
+    if (tls_authentication_status(mi->context.c2.tls_multi, 0)
+        != TLS_AUTHENTICATION_SUCCEEDED)
+    {
+        return;
+    }
+    unsigned int option_types_found = 0;
+
+    int cc_succeeded = true;     /* client connect script status */
+    int cc_succeeded_count = 0;
+
+    multi_client_connect_early_setup(m, mi);
+
+    multi_client_connect_source_ccd(m, mi, &option_types_found);
+
+    /*
+     * Select a virtual address from either --ifconfig-push in
+     * --client-config-dir file or --ifconfig-pool.
+     */
+    multi_select_virtual_addr(m, mi);
+
+    /* do --client-connect setenvs */
+    multi_client_connect_setenv(m, mi);
+
+    multi_client_connect_call_plugin_v1(m, mi, &option_types_found,
+                                        &cc_succeeded,
+                                        &cc_succeeded_count);
+
+    multi_client_connect_call_plugin_v2(m, mi, &option_types_found,
+                                        &cc_succeeded,
+                                        &cc_succeeded_count);
+
+    /*
+     * Check for client-connect script left by management interface client
+     */
+    if (cc_succeeded)
+    {
+        multi_client_connect_call_script(m, mi, &option_types_found,
+                                         &cc_succeeded,
+                                         &cc_succeeded_count);
+    }
+
+#ifdef MANAGEMENT_DEF_AUTH
+    if (cc_succeeded && mi->cc_config)
+    {
+        multi_client_connect_mda(m, mi, &option_types_found);
+        ++cc_succeeded_count;
+    }
+#endif
+
+    /*
+     * Check for "disable" directive in client-config-dir file
+     * or config file generated by --client-connect script.
+     */
+    if (mi->context.options.disable)
+    {
+        msg(D_MULTI_ERRORS, "MULTI: client has been rejected due to "
+            " 'disable' directive");
+        cc_succeeded = false;
+        cc_succeeded_count = 0;
+    }
+
+
+
+    if (cc_succeeded)
+    {
+        multi_client_connect_late_setup(m, mi, option_types_found);
     }
     else
     {
         /* set context-level authentication flag */
-        mi->context.c2.context_auth = cc_succeeded_count ? CAS_PARTIAL : CAS_FAILED;
+        mi->context.c2.context_auth =
+            cc_succeeded_count ? CAS_PARTIAL : CAS_FAILED;
     }
 
+
     /* increment number of current authenticated clients */
     ++m->n_clients;
     update_mstat_n_clients(m->n_clients);
@@ -2199,11 +2306,10 @@ script_failed:
 #ifdef MANAGEMENT_DEF_AUTH
     if (management)
     {
-        management_connection_established(management, &mi->context.c2.mda_context, mi->context.c2.es);
+        management_connection_established(management,
+                                          &mi->context.c2.mda_context, mi->context.c2.es);
     }
 #endif
-
-    gc_free(&gc);
 }
 
 #ifdef ENABLE_ASYNC_PUSH
src/openvpn/multi.h +3 -1
index 8c9c4609..c51107f4 100644
@@ -623,7 +623,9 @@ multi_process_outgoing_tun(struct multi_context *m, const unsigned int mpp_flags
     return ret;
 }
 
-
+#define CLIENT_CONNECT_OPT_MASK (OPT_P_INSTANCE | OPT_P_INHERIT   \
+                                 |OPT_P_PUSH | OPT_P_TIMER | OPT_P_CONFIG   \
+                                 |OPT_P_ECHO | OPT_P_COMP | OPT_P_SOCKFLAGS)
 
 static inline bool
 multi_process_outgoing_link_dowork(struct multi_context *m, struct multi_instance *mi, const unsigned int mpp_flags)
Signature · 1 line
2.26.2
#2 lore raw

Re: [Openvpn-devel] [PATCH v5 02/14] client-connect: Split multi_connection_established into separate functions

Hi,

Quoted 5 lines · Arne Schwabe

From: Fabian Knittel <[email protected]...>

This patch splits up the multi_connection_established() function. Each new helper function does a specific job. Functions that do a similar job receive a similar calling interface.

Tested on the server test rig, it seems to be happy with all test cases I have

start client jobs...
23...
Test sets succeeded: 1 1a 1b 1d 2 2a 2b 2c 2d 3 4 5 6 8 8a 9.
Test sets failed: none.
24...
Test sets succeeded: 1 1a 1b 1c 1d 1e 2 2a 2b 2c 2d 2e 3 4 4a 5 6 8 8a 9. Test sets failed: none.
master...
Test sets succeeded: 1 1a 1b 1c 1d 1e 2 2a 2b 2c 2d 2e 3 4 5 6 7 7a 8 8a 9 2f 4b. Test sets failed: none.

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]...
#3 lore raw

Re: [Openvpn-devel] [PATCH v5 02/14] client-connect: Split multi_connection_established into separate functions

In reply to Arne Schwabe

spelling, 1x grammer:

Quoted 7 lines · Arne Schwabe

From: Fabian Knittel <[email protected]...>

This patch splits up the multi_connection_established() function. Each new helper function does a specific job. Functions that do a similar job receive a similar calling interface.

The patch tries not to reindent code, so that the real changes are as clearly

Technically: reindent -> re-indent
(that is how I would spell it)

Quoted 9 lines

visible as possible. (A follow-up patch will only do indentation changes.)

PATCH v3: Since the code has changed enough from the time the original patch to the current master, the splitting has been redone from the current code.

Also some style and minor code changes have been added doing this patch. This elimininates and the big reformatting done before eliminates the follow

elimininates -> eliminates

The grammer:

This elimininates >and< the big reformatting ..

I don't know how to re-word this paragraph to make sense.

Quoted 4 lines

up patch with indentation changes.

The original patch already replaces some instances of option_permission_mask with CLIENT_CONNECT_OPT_MASK. The V3 version does this more consistenly.

consistenly -> consistently

I found no further issues with the patch.

Quoted 791 lines

Patch v4: Move config -> mi->cc_config into its own commit

Patch v5: Clean up some minor issues, add one missing check on temporary file deletion, rebase on latest master.

---
  src/openvpn/multi.c | 588 ++++++++++++++++++++++++++------------------
  src/openvpn/multi.h |   4 +-
  2 files changed, 350 insertions(+), 242 deletions(-)
src/openvpn/multi.c +347 -241
index a2af071a..3c4ceeb5 100644
@@ -1638,7 +1638,6 @@ static void
  multi_client_connect_post(struct multi_context *m,
                            struct multi_instance *mi,
                            const char *dc_file,
-                          unsigned int option_permissions_mask,
                            unsigned int *option_types_found)
  {
      /* Did script generate a dynamic config file? */
@@ -1647,7 +1646,7 @@ multi_client_connect_post(struct multi_context *m,
          options_server_import(&mi->context.options,
                                dc_file,
                                D_IMPORT_ERRORS|M_OPTERR,
-                              option_permissions_mask,
+                              CLIENT_CONNECT_OPT_MASK,
                                option_types_found,
                                mi->context.c2.es);
 
@@ -1671,7 +1670,6 @@ static void
  multi_client_connect_post_plugin(struct multi_context *m,
                                   struct multi_instance *mi,
                                   const struct plugin_return *pr,
-                                 unsigned int option_permissions_mask,
                                   unsigned int *option_types_found)
  {
      struct plugin_return config;
@@ -1689,7 +1687,7 @@ multi_client_connect_post_plugin(struct multi_context *m,
                  options_string_import(&mi->context.options,
                                        config.list[i]->value,
                                        D_IMPORT_ERRORS|M_OPTERR,
-                                      option_permissions_mask,
+                                      CLIENT_CONNECT_OPT_MASK,
                                        option_types_found,
                                        mi->context.c2.es);
              }
@@ -1716,7 +1714,6 @@ multi_client_connect_post_plugin(struct multi_context *m,
  static void
  multi_client_connect_mda(struct multi_context *m,
                           struct multi_instance *mi,
-                         unsigned int option_permissions_mask,
                           unsigned int *option_types_found)
  {
      if (mi->cc_config)
@@ -1729,7 +1726,7 @@ multi_client_connect_mda(struct multi_context *m,
              options_string_import(&mi->context.options,
                                    opt,
                                    D_IMPORT_ERRORS|M_OPTERR,
-                                  option_permissions_mask,
+                                  CLIENT_CONNECT_OPT_MASK,
                                    option_types_found,
                                    mi->context.c2.es);
          }
@@ -1843,160 +1840,46 @@ multi_client_set_protocol_options(struct context *c)
      }
  }
 
-/**
- * Generates the data channel keys
- */
-static bool
-multi_client_generate_tls_keys(struct context *c)
-{
-    struct frame *frame_fragment = NULL;
-#ifdef ENABLE_FRAGMENT
-    if (c->options.ce.fragment)
-    {
-        frame_fragment = &c->c2.frame_fragment;
-    }
-#endif
-    struct tls_session *session = &c->c2.tls_multi->session[TM_ACTIVE];
-    if (!tls_session_update_crypto_params(session, &c->options,
-                                          &c->c2.frame, frame_fragment))
-    {
-        msg(D_TLS_ERRORS, "TLS Error: initializing data channel failed");
-        register_signal(c, SIGUSR1, "process-push-msg-failed");
-        return false;
-    }
-
-    return true;
-}
-
-/*
- * Called as soon as the SSL/TLS connection authenticates.
- *
- * Instance-specific directives to be processed:
- *
- *   iroute start-ip end-ip
- *   ifconfig-push local remote-netmask
- *   push
- */
  static void
-multi_connection_established(struct multi_context *m, struct multi_instance *mi)
+multi_client_connect_call_plugin_v1(struct multi_context *m,
+                                    struct multi_instance *mi,
+                                    unsigned int *option_types_found,
+                                    int *cc_succeeded,
+                                    int *cc_succeeded_count)
  {
-    if (tls_authentication_status(mi->context.c2.tls_multi, 0) != TLS_AUTHENTICATION_SUCCEEDED)
-    {
-        return;
-    }
-
-    struct gc_arena gc = gc_new();
-    unsigned int option_types_found = 0;
-
-    const unsigned int option_permissions_mask =
-        OPT_P_INSTANCE
-        | OPT_P_INHERIT
-        | OPT_P_PUSH
-        | OPT_P_TIMER
-        | OPT_P_CONFIG
-        | OPT_P_ECHO
-        | OPT_P_COMP
-        | OPT_P_SOCKFLAGS;
-
-    int cc_succeeded = true;     /* client connect script status */
-    int cc_succeeded_count = 0;
-
-    ASSERT(mi->context.c1.tuntap);
-
-    /* lock down the common name and cert hashes so they can't change during future TLS renegotiations */
-    tls_lock_common_name(mi->context.c2.tls_multi);
-    tls_lock_cert_hash_set(mi->context.c2.tls_multi);
-
-    /* generate a msg() prefix for this client instance */
-    generate_prefix(mi);
-
-    /* delete instances of previous clients with same common-name */
-    if (!mi->context.options.duplicate_cn)
-    {
-        multi_delete_dup(m, mi);
-    }
-
-    /* reset pool handle to null */
-    mi->vaddr_handle = -1;
-
-    /*
-     * Try to source a dynamic config file from the
-     * --client-config-dir directory.
-     */
-    if (mi->context.options.client_config_dir)
-    {
-        const char *ccd_file;
-
-        ccd_file = platform_gen_path(mi->context.options.client_config_dir,
-                                     tls_common_name(mi->context.c2.tls_multi,
-                                                     false),
-                                     &gc);
-
-        /* try common-name file */
-        if (platform_test_file(ccd_file))
-        {
-            options_server_import(&mi->context.options,
-                                  ccd_file,
-                                  D_IMPORT_ERRORS|M_OPTERR,
-                                  option_permissions_mask,
-                                  &option_types_found,
-                                  mi->context.c2.es);
-        }
-        else     /* try default file */
-        {
-            ccd_file = platform_gen_path(mi->context.options.client_config_dir,
-                                         CCD_DEFAULT,
-                                         &gc);
-
-            if (platform_test_file(ccd_file))
-            {
-                options_server_import(&mi->context.options,
-                                      ccd_file,
-                                      D_IMPORT_ERRORS|M_OPTERR,
-                                      option_permissions_mask,
-                                      &option_types_found,
-                                      mi->context.c2.es);
-            }
-        }
-    }
-
-    /*
-     * Select a virtual address from either --ifconfig-push in --client-config-dir file
-     * or --ifconfig-pool.
-     */
-    multi_select_virtual_addr(m, mi);
-
-    /* do --client-connect setenvs */
-    multi_client_connect_setenv(m, mi);
-
  #ifdef ENABLE_PLUGIN
-    /*
-     * Call client-connect plug-in.
-     */
+    ASSERT(m);
+    ASSERT(mi);
+    ASSERT(option_types_found);
+    ASSERT(cc_succeeded);
+    ASSERT(cc_succeeded_count);
 
      /* deprecated callback, use a file for passing back return info */
      if (plugin_defined(mi->context.plugins, OPENVPN_PLUGIN_CLIENT_CONNECT))
      {
          struct argv argv = argv_new();
-        const char *dc_file = platform_create_temp_file(mi->context.options.tmp_dir,
-                                                        "cc", &gc);
+        struct gc_arena gc = gc_new();
+        const char *dc_file =
+            platform_create_temp_file(mi->context.options.tmp_dir, "cc", &gc);
 
          if (!dc_file)
          {
              cc_succeeded = false;
-            goto script_depr_failed;
+            goto cleanup;
          }
 
          argv_printf(&argv, "%s", dc_file);
-        if (plugin_call(mi->context.plugins, OPENVPN_PLUGIN_CLIENT_CONNECT, &argv, NULL, mi->context.c2.es) != OPENVPN_PLUGIN_FUNC_SUCCESS)
+        if (plugin_call(mi->context.plugins, OPENVPN_PLUGIN_CLIENT_CONNECT,
+                        &argv, NULL, mi->context.c2.es)
+            != OPENVPN_PLUGIN_FUNC_SUCCESS)
          {
              msg(M_WARN, "WARNING: client-connect plugin call failed");
-            cc_succeeded = false;
+            *cc_succeeded = false;
          }
          else
          {
-            multi_client_connect_post(m, mi, dc_file, option_permissions_mask, &option_types_found);
-            ++cc_succeeded_count;
+            multi_client_connect_post(m, mi, dc_file, option_types_found);
+            (*cc_succeeded_count)++;
          }
 
          if (!platform_unlink(dc_file))
@@ -2005,9 +1888,26 @@ multi_connection_established(struct multi_context *m, struct multi_instance *mi)
                  dc_file);
          }
 
-script_depr_failed:
+cleanup:
          argv_free(&argv);
+        gc_free(&gc);
      }
+#endif /* ifdef ENABLE_PLUGIN */
+}
+
+static void
+multi_client_connect_call_plugin_v2(struct multi_context *m,
+                                    struct multi_instance *mi,
+                                    unsigned int *option_types_found,
+                                    int *cc_succeeded,
+                                    int *cc_succeeded_count)
+{
+#ifdef ENABLE_PLUGIN
+    ASSERT(m);
+    ASSERT(mi);
+    ASSERT(option_types_found);
+    ASSERT(cc_succeeded);
+    ASSERT(cc_succeeded_count);
 
      /* V2 callback, use a plugin_return struct for passing back return info */
      if (plugin_defined(mi->context.plugins, OPENVPN_PLUGIN_CLIENT_CONNECT_V2))
@@ -2016,27 +1916,42 @@ script_depr_failed:
 
          plugin_return_init(&pr);
 
-        if (plugin_call(mi->context.plugins, OPENVPN_PLUGIN_CLIENT_CONNECT_V2, NULL, &pr, mi->context.c2.es) != OPENVPN_PLUGIN_FUNC_SUCCESS)
+        if (plugin_call(mi->context.plugins, OPENVPN_PLUGIN_CLIENT_CONNECT_V2,
+                        NULL, &pr, mi->context.c2.es)
+            != OPENVPN_PLUGIN_FUNC_SUCCESS)
          {
              msg(M_WARN, "WARNING: client-connect-v2 plugin call failed");
-            cc_succeeded = false;
+            *cc_succeeded = false;
          }
          else
          {
-            multi_client_connect_post_plugin(m, mi, &pr, option_permissions_mask, &option_types_found);
-            ++cc_succeeded_count;
+            multi_client_connect_post_plugin(m, mi, &pr, option_types_found);
+            (*cc_succeeded_count)++;
          }
 
          plugin_return_free(&pr);
      }
  #endif /* ifdef ENABLE_PLUGIN */
+}
 
-    /*
-     * Run --client-connect script.
-     */
-    if (mi->context.options.client_connect_script && cc_succeeded)
+
+
+/**
+ * Runs the --client-connect script if one is defined.
+ */
+static void
+multi_client_connect_call_script(struct multi_context *m,
+                                 struct multi_instance *mi,
+                                 unsigned int *option_types_found,
+                                 int *cc_succeeded,
+                                 int *cc_succeeded_count)
+{
+    ASSERT(m);
+    ASSERT(mi);
+    if (mi->context.options.client_connect_script)
      {
          struct argv argv = argv_new();
+        struct gc_arena gc = gc_new();
          const char *dc_file = NULL;
 
          setenv_str(mi->context.c2.es, "script_type", "client-connect");
@@ -2045,8 +1960,8 @@ script_depr_failed:
                                              "cc", &gc);
          if (!dc_file)
          {
-            cc_succeeded = false;
-            goto script_failed;
+            *cc_succeeded = false;
+            goto cleanup;
          }
 
          argv_parse_cmd(&argv, mi->context.options.client_connect_script);
@@ -2054,12 +1969,12 @@ script_depr_failed:
 
          if (openvpn_run_script(&argv, mi->context.c2.es, 0, "--client-connect"))
          {
-            multi_client_connect_post(m, mi, dc_file, option_permissions_mask, &option_types_found);
-            ++cc_succeeded_count;
+            multi_client_connect_post(m, mi, dc_file, option_types_found);
+            (*cc_succeeded_count)++;
          }
          else
          {
-            cc_succeeded = false;
+            *cc_succeeded = false;
          }
 
          if (!platform_unlink(dc_file))
@@ -2067,130 +1982,322 @@ script_depr_failed:
              msg(D_MULTI_ERRORS, "MULTI: problem deleting temporary file: %s",
                  dc_file);
          }
-
-script_failed:
+cleanup:
          argv_free(&argv);
+        gc_free(&gc);
      }
+}
 
-    /*
-     * Check for client-connect script left by management interface client
-     */
-#ifdef MANAGEMENT_DEF_AUTH
-    if (cc_succeeded && mi->cc_config)
+/**
+ * Generates the data channel keys
+ */
+static bool
+multi_client_generate_tls_keys(struct context *c)
+{
+  struct frame *frame_fragment = NULL;
+#ifdef ENABLE_FRAGMENT
+  if (c->options.ce.fragment)
      {
-        multi_client_connect_mda(m, mi, option_permissions_mask, &option_types_found);
-        ++cc_succeeded_count;
+      frame_fragment = &c->c2.frame_fragment;
      }
  #endif
+  struct tls_session *session = &c->c2.tls_multi->session[TM_ACTIVE];
+  if (!tls_session_update_crypto_params(session, &c->options,
+                                        &c->c2.frame, frame_fragment))
+    {
+      msg(D_TLS_ERRORS, "TLS Error: initializing data channel failed");
+      register_signal(c, SIGUSR1, "process-push-msg-failed");
+      return false;
+    }
 
+  return true;
+}
+
+static void
+multi_client_connect_late_setup(struct multi_context *m,
+                                struct multi_instance *mi,
+                                const unsigned int option_types_found)
+{
+    ASSERT(m);
+    ASSERT(mi);
+
+    struct gc_arena gc = gc_new();
      /*
-     * Check for "disable" directive in client-config-dir file
-     * or config file generated by --client-connect script.
+     * Process sourced options.
       */
-    if (mi->context.options.disable)
+    do_deferred_options(&mi->context, option_types_found);
+
+    /*
+     * make sure we got ifconfig settings from somewhere
+     */
+    if (!mi->context.c2.push_ifconfig_defined)
      {
-        msg(D_MULTI_ERRORS, "MULTI: client has been rejected due to 'disable' directive");
-        cc_succeeded = false;
-        cc_succeeded_count = 0;
+        msg(D_MULTI_ERRORS, "MULTI: no dynamic or static remote"
+            "--ifconfig address is available for %s",
+            multi_instance_string(mi, false, &gc));
      }
 
-    if (cc_succeeded)
+    /*
+     * make sure that ifconfig settings comply with constraints
+     */
+    if (!ifconfig_push_constraint_satisfied(&mi->context))
      {
-        /*
-         * Process sourced options.
-         */
-        do_deferred_options(&mi->context, option_types_found);
+        const char *ifconfig_constraint_network =
+            print_in_addr_t(mi->context.options.push_ifconfig_constraint_network, 0, &gc);
+        const char *ifconfig_constraint_netmask =
+            print_in_addr_t(mi->context.options.push_ifconfig_constraint_netmask, 0, &gc);
 
-        /*
-         * make sure we got ifconfig settings from somewhere
-         */
-        if (!mi->context.c2.push_ifconfig_defined)
+        /* JYFIXME -- this should cause the connection to fail */
+        msg(D_MULTI_ERRORS, "MULTI ERROR: primary virtual IP for %s (%s)"
+            "violates tunnel network/netmask constraint (%s/%s)",
+            multi_instance_string(mi, false, &gc),
+            print_in_addr_t(mi->context.c2.push_ifconfig_local, 0, &gc),
+            ifconfig_constraint_network, ifconfig_constraint_netmask);
+    }
+
+    /*
+     * For routed tunnels, set up internal route to endpoint
+     * plus add all iroute routes.
+     */
+    if (TUNNEL_TYPE(mi->context.c1.tuntap) == DEV_TYPE_TUN)
+    {
+        if (mi->context.c2.push_ifconfig_defined)
          {
-            msg(D_MULTI_ERRORS, "MULTI: no dynamic or static remote --ifconfig address is available for %s",
-                multi_instance_string(mi, false, &gc));
+            multi_learn_in_addr_t(m, mi,
+                                  mi->context.c2.push_ifconfig_local,
+                                  -1, true);
+            msg(D_MULTI_LOW, "MULTI: primary virtual IP for %s: %s",
+                multi_instance_string(mi, false, &gc),
+                print_in_addr_t(mi->context.c2.push_ifconfig_local, 0, &gc));
          }
 
-        /*
-         * make sure that ifconfig settings comply with constraints
-         */
-        if (!ifconfig_push_constraint_satisfied(&mi->context))
+        if (mi->context.c2.push_ifconfig_ipv6_defined)
          {
-            /* JYFIXME -- this should cause the connection to fail */
-            msg(D_MULTI_ERRORS, "MULTI ERROR: primary virtual IP for %s (%s) violates tunnel network/netmask constraint (%s/%s)",
+            multi_learn_in6_addr(m, mi,
+                                 mi->context.c2.push_ifconfig_ipv6_local,
+                                 -1, true);
+            /* TODO: find out where addresses are "unlearned"!! */
+            const char *ifconfig_local_ipv6 =
+                print_in6_addr(mi->context.c2.push_ifconfig_ipv6_local, 0, &gc);
+            msg(D_MULTI_LOW, "MULTI: primary virtual IPv6 for %s: %s",
                  multi_instance_string(mi, false, &gc),
-                print_in_addr_t(mi->context.c2.push_ifconfig_local, 0, &gc),
-                print_in_addr_t(mi->context.options.push_ifconfig_constraint_network, 0, &gc),
-                print_in_addr_t(mi->context.options.push_ifconfig_constraint_netmask, 0, &gc));
+                ifconfig_local_ipv6);
          }
 
+        /* add routes locally, pointing to new client, if
+         * --iroute options have been specified */
+        multi_add_iroutes(m, mi);
+
          /*
-         * For routed tunnels, set up internal route to endpoint
-         * plus add all iroute routes.
+         * iroutes represent subnets which are "owned" by a particular
+         * client.  Therefore, do not actually push a route to a client
+         * if it matches one of the client's iroutes.
           */
-        if (TUNNEL_TYPE(mi->context.c1.tuntap) == DEV_TYPE_TUN)
-        {
-            if (mi->context.c2.push_ifconfig_defined)
-            {
-                multi_learn_in_addr_t(m, mi, mi->context.c2.push_ifconfig_local, -1, true);
-                msg(D_MULTI_LOW, "MULTI: primary virtual IP for %s: %s",
-                    multi_instance_string(mi, false, &gc),
-                    print_in_addr_t(mi->context.c2.push_ifconfig_local, 0, &gc));
-            }
+        remove_iroutes_from_push_route_list(&mi->context.options);
+    }
+    else if (mi->context.options.iroutes)
+    {
+        msg(D_MULTI_ERRORS, "MULTI: --iroute options rejected for %s -- iroute"
+            "only works with tun-style tunnels",
+            multi_instance_string(mi, false, &gc));
+    }
 
-            if (mi->context.c2.push_ifconfig_ipv6_defined)
-            {
-                multi_learn_in6_addr(m, mi, mi->context.c2.push_ifconfig_ipv6_local, -1, true);
-                /* TODO: find out where addresses are "unlearned"!! */
-                msg(D_MULTI_LOW, "MULTI: primary virtual IPv6 for %s: %s",
-                    multi_instance_string(mi, false, &gc),
-                    print_in6_addr(mi->context.c2.push_ifconfig_ipv6_local, 0, &gc));
-            }
+    /* set our client's VPN endpoint for status reporting purposes */
+    mi->reporting_addr = mi->context.c2.push_ifconfig_local;
+    mi->reporting_addr_ipv6 = mi->context.c2.push_ifconfig_ipv6_local;
 
-            /* add routes locally, pointing to new client, if
-             * --iroute options have been specified */
-            multi_add_iroutes(m, mi);
+    /* set context-level authentication flag */
+    mi->context.c2.context_auth = CAS_SUCCEEDED;
 
-            /*
-             * iroutes represent subnets which are "owned" by a particular
-             * client.  Therefore, do not actually push a route to a client
-             * if it matches one of the client's iroutes.
-             */
-            remove_iroutes_from_push_route_list(&mi->context.options);
-        }
-        else if (mi->context.options.iroutes)
-        {
-            msg(D_MULTI_ERRORS, "MULTI: --iroute options rejected for %s -- iroute only works with tun-style tunnels",
-                multi_instance_string(mi, false, &gc));
-        }
+    /* authentication complete, calculate dynamic client specific options */
+    multi_client_set_protocol_options(&mi->context);
+
+    /* Generate data channel keys */
+    if (!multi_client_generate_tls_keys(&mi->context))
+    {
+        mi->context.c2.context_auth = CAS_FAILED;
+    }
 
-        /* set our client's VPN endpoint for status reporting purposes */
-        mi->reporting_addr = mi->context.c2.push_ifconfig_local;
-        mi->reporting_addr_ipv6 = mi->context.c2.push_ifconfig_ipv6_local;
+    /* send push reply if ready */
+    if (mi->context.c2.push_request_received)
+    {
+        process_incoming_push_request(&mi->context);
+    }
 
-        /* set context-level authentication flag */
-        mi->context.c2.context_auth = CAS_SUCCEEDED;
+    gc_free(&gc);
+}
+
+static void
+multi_client_connect_early_setup(struct multi_context *m,
+                                 struct multi_instance *mi)
+{
+    ASSERT(mi->context.c1.tuntap);
+    /*
+     * lock down the common name and cert hashes so they can't change
+     * during future TLS renegotiations
+     */
+    tls_lock_common_name(mi->context.c2.tls_multi);
+    tls_lock_cert_hash_set(mi->context.c2.tls_multi);
+
+    /* generate a msg() prefix for this client instance */
+    generate_prefix(mi);
+
+    /* delete instances of previous clients with same common-name */
+    if (!mi->context.options.duplicate_cn)
+    {
+        multi_delete_dup(m, mi);
+    }
+
+    /* reset pool handle to null */
+    mi->vaddr_handle = -1;
+}
+
+/**
+ * Try to source a dynamic config file from the
+ * --client-config-dir directory.
+ */
+static void
+multi_client_connect_source_ccd(struct multi_context *m,
+                                struct multi_instance *mi,
+                                unsigned int *option_types_found)
+{
+    if (mi->context.options.client_config_dir)
+    {
+        struct gc_arena gc = gc_new();
+        const char *ccd_file;
 
-        /* authentication complete, calculate dynamic client specific options */
-        multi_client_set_protocol_options(&mi->context);
+        ccd_file = platform_gen_path(mi->context.options.client_config_dir,
+                                     tls_common_name(mi->context.c2.tls_multi,
+                                                     false),
+                                     &gc);
 
-        /* Generate data channel keys */
-        if (!multi_client_generate_tls_keys(&mi->context))
+        /* try common-name file */
+        if (platform_test_file(ccd_file))
          {
-            mi->context.c2.context_auth = CAS_FAILED;
+            options_server_import(&mi->context.options,
+                                  ccd_file,
+                                  D_IMPORT_ERRORS|M_OPTERR,
+                                  CLIENT_CONNECT_OPT_MASK,
+                                  option_types_found,
+                                  mi->context.c2.es);
          }
-
-        /* send push reply if ready */
-        if (mi->context.c2.push_request_received)
+        else /* try default file */
          {
-            process_incoming_push_request(&mi->context);
+            ccd_file = platform_gen_path(mi->context.options.client_config_dir,
+                                         CCD_DEFAULT,
+                                         &gc);
+
+            if (platform_test_file(ccd_file))
+            {
+                options_server_import(&mi->context.options,
+                                      ccd_file,
+                                      D_IMPORT_ERRORS|M_OPTERR,
+                                      CLIENT_CONNECT_OPT_MASK,
+                                      option_types_found,
+                                      mi->context.c2.es);
+            }
          }
+        gc_free(&gc);
+    }
+}
+
+/*
+ * Called as soon as the SSL/TLS connection is authenticated.
+ *
+ * Will collect the client specific configuration from the different
+ * sources like ccd files, connect plugins and management interface.
+ *
+ * This method starts with cas_context CAS_PENDING and will move the
+ * state machine to either CAS_SUCCEEDED on success or
+ * CAS_FAILED/CAS_PARTIAL on failure.
+ *
+ * Instance-specific directives to be processed (CLIENT_CONNECT_OPT_MASK)
+ * include:
+ *
+ *   iroute start-ip end-ip
+ *   ifconfig-push local remote-netmask
+ *   push
+ *
+ *
+ */
+static void
+multi_connection_established(struct multi_context *m, struct multi_instance *mi)
+{
+    if (tls_authentication_status(mi->context.c2.tls_multi, 0)
+        != TLS_AUTHENTICATION_SUCCEEDED)
+    {
+        return;
+    }
+    unsigned int option_types_found = 0;
+
+    int cc_succeeded = true;     /* client connect script status */
+    int cc_succeeded_count = 0;
+
+    multi_client_connect_early_setup(m, mi);
+
+    multi_client_connect_source_ccd(m, mi, &option_types_found);
+
+    /*
+     * Select a virtual address from either --ifconfig-push in
+     * --client-config-dir file or --ifconfig-pool.
+     */
+    multi_select_virtual_addr(m, mi);
+
+    /* do --client-connect setenvs */
+    multi_client_connect_setenv(m, mi);
+
+    multi_client_connect_call_plugin_v1(m, mi, &option_types_found,
+                                        &cc_succeeded,
+                                        &cc_succeeded_count);
+
+    multi_client_connect_call_plugin_v2(m, mi, &option_types_found,
+                                        &cc_succeeded,
+                                        &cc_succeeded_count);
+
+    /*
+     * Check for client-connect script left by management interface client
+     */
+    if (cc_succeeded)
+    {
+        multi_client_connect_call_script(m, mi, &option_types_found,
+                                         &cc_succeeded,
+                                         &cc_succeeded_count);
+    }
+
+#ifdef MANAGEMENT_DEF_AUTH
+    if (cc_succeeded && mi->cc_config)
+    {
+        multi_client_connect_mda(m, mi, &option_types_found);
+        ++cc_succeeded_count;
+    }
+#endif
+
+    /*
+     * Check for "disable" directive in client-config-dir file
+     * or config file generated by --client-connect script.
+     */
+    if (mi->context.options.disable)
+    {
+        msg(D_MULTI_ERRORS, "MULTI: client has been rejected due to "
+            " 'disable' directive");
+        cc_succeeded = false;
+        cc_succeeded_count = 0;
+    }
+
+
+
+    if (cc_succeeded)
+    {
+        multi_client_connect_late_setup(m, mi, option_types_found);
      }
      else
      {
          /* set context-level authentication flag */
-        mi->context.c2.context_auth = cc_succeeded_count ? CAS_PARTIAL : CAS_FAILED;
+        mi->context.c2.context_auth =
+            cc_succeeded_count ? CAS_PARTIAL : CAS_FAILED;
      }
 
+
      /* increment number of current authenticated clients */
      ++m->n_clients;
      update_mstat_n_clients(m->n_clients);
@@ -2199,11 +2306,10 @@ script_failed:
  #ifdef MANAGEMENT_DEF_AUTH
      if (management)
      {
-        management_connection_established(management, &mi->context.c2.mda_context, mi->context.c2.es);
+        management_connection_established(management,
+                                          &mi->context.c2.mda_context, mi->context.c2.es);
      }
  #endif
-
-    gc_free(&gc);
  }
 
  #ifdef ENABLE_ASYNC_PUSH
src/openvpn/multi.h +3 -1
index 8c9c4609..c51107f4 100644
@@ -623,7 +623,9 @@ multi_process_outgoing_tun(struct multi_context *m, const unsigned int mpp_flags
      return ret;
  }
 
-
+#define CLIENT_CONNECT_OPT_MASK (OPT_P_INSTANCE | OPT_P_INHERIT   \
+                                 |OPT_P_PUSH | OPT_P_TIMER | OPT_P_CONFIG   \
+                                 |OPT_P_ECHO | OPT_P_COMP | OPT_P_SOCKFLAGS)
 
  static inline bool
  multi_process_outgoing_link_dowork(struct multi_context *m, struct multi_instance *mi, const unsigned int mpp_flags)
Acked-by on v5 2/14
#4 lore raw

Re: [Openvpn-devel] [PATCH v5 02/14] client-connect: Split multi_connection_established into separate functions

In reply to Arne Schwabe

Hi,

Quoted 20 lines · Arne Schwabe

From: Fabian Knittel <[email protected]...>

This patch splits up the multi_connection_established() function. Each new helper function does a specific job. Functions that do a similar job receive a similar calling interface.

The patch tries not to reindent code, so that the real changes are as clearly visible as possible. (A follow-up patch will only do indentation changes.)

PATCH v3: Since the code has changed enough from the time the original patch to the current master, the splitting has been redone from the current code. Also some style and minor code changes have been added doing this patch. This elimininates and the big reformatting done before eliminates the follow up patch with indentation changes.

The original patch already replaces some instances of option_permission_mask with CLIENT_CONNECT_OPT_MASK. The V3 version does this more consistenly.

Quoted 6 lines

Patch v4: Move config -> mi->cc_config into its own commit

Patch v5: Clean up some minor issues, add one missing check on temporary file deletion, rebase on latest master.

Thanks for taking care of my concerns.

The patch looks good and does what it says. Unfortunately the code itself is already pretty convoluted, so this refactoring patch looks convoluted as well...but after looking through it the change is minimal.

Signature · 1 line
Antonio Quartulli
#5 lore raw

[Openvpn-devel] [PATCH applied] Re: client-connect: Split multi_connection_established into separate functions

In reply to Arne Schwabe

Your patch has been applied to the master branch.

I have fixed typo and grammar in the commit message as requested, and re-wrapped the commit message a bit.

Tested yesterday with a "full t_client and t_server" test with no issues.

commit 0c8c50ca93392e1d0534ac35637899e0017863b9

Author: Fabian Knittel
Date:   Sat Jul 11 11:36:43 2020 +0200
     client-connect: Split multi_connection_established into separate functions

     Signed-off-by: Fabian Knittel <[email protected]...>
     Signed-off-by: Arne Schwabe <[email protected]...>
     Acked-by: Antonio Quartulli <[email protected]...>
     Message-Id: <[email protected]...>
     URL: https://www.mail-archive.com/[email protected]/msg20289.html
     Signed-off-by: Gert Doering <[email protected]...>

--
kind regards,

Gert Doering

#6 lore raw

PATCH v5 3/14 client-connect: Refactor multi_client_connect_source_ccd

In reply to Arne Schwabe

From: Fabian Knittel <[email protected]...>

Refactor multi_client_connect_source_ccd(), so that options_server_import() (or the success path in general) is only entered in one place within the function.

Patch V5: Simplify the logic even further to make more easy to understand.

---
 src/openvpn/multi.c | 43 +++++++++++++++++++++----------------------
 1 file changed, 21 insertions(+), 22 deletions(-)
src/openvpn/multi.c +21 -22
index 3c4ceeb5..35e0bd10 100644
@@ -2164,15 +2164,30 @@ multi_client_connect_source_ccd(struct multi_context *m,
     if (mi->context.options.client_config_dir)
     {
         struct gc_arena gc = gc_new();
-        const char *ccd_file;
+        const char *ccd_file = NULL;
+
+        const char *ccd_client = platform_gen_path(mi->context.options.client_config_dir,
+                                                   tls_common_name(mi->context.c2.tls_multi,
+                                                                   false),
+                                                   &gc);
+
+        const char *ccd_default = platform_gen_path(mi->context.options.client_config_dir,
+                                                    CCD_DEFAULT,
+                                                    &gc);
 
-        ccd_file = platform_gen_path(mi->context.options.client_config_dir,
-                                     tls_common_name(mi->context.c2.tls_multi,
-                                                     false),
-                                     &gc);
 
         /* try common-name file */
-        if (platform_test_file(ccd_file))
+        if (platform_test_file(ccd_client))
+        {
+            ccd_file = ccd_client;
+        }
+        /* try default file */
+        else if (platform_test_file(ccd_default))
+        {
+            ccd_file = ccd_default;
+        }
+
+        if (ccd_file)
         {
             options_server_import(&mi->context.options,
                                   ccd_file,
@@ -2181,22 +2196,6 @@ multi_client_connect_source_ccd(struct multi_context *m,
                                   option_types_found,
                                   mi->context.c2.es);
         }
-        else /* try default file */
-        {
-            ccd_file = platform_gen_path(mi->context.options.client_config_dir,
-                                         CCD_DEFAULT,
-                                         &gc);
-
-            if (platform_test_file(ccd_file))
-            {
-                options_server_import(&mi->context.options,
-                                      ccd_file,
-                                      D_IMPORT_ERRORS|M_OPTERR,
-                                      CLIENT_CONNECT_OPT_MASK,
-                                      option_types_found,
-                                      mi->context.c2.es);
-            }
-        }
         gc_free(&gc);
     }
 }
Signature · 1 line
2.26.2
#7 lore raw

Re: [Openvpn-devel] [PATCH v5 03/14] client-connect: Refactor multi_client_connect_source_ccd

Hi,

Quoted 6 lines · Arne Schwabe

From: Fabian Knittel <[email protected]...>

Refactor multi_client_connect_source_ccd(), so that options_server_import() (or the success path in general) is only entered in one place within the function.

All server side tests passed

23...
Test sets succeeded: 1 1a 1b 1d 2 2a 2b 2c 2d 3 4 5 6 8 8a 9.
Test sets failed: none.
24...
Test sets succeeded: 1 1a 1b 1c 1d 1e 2 2a 2b 2c 2d 2e 3 4 4a 5 6 8 8a 9. Test sets failed: none.
master...
Test sets succeeded: 1 1a 1b 1c 1d 1e 2 2a 2b 2c 2d 2e 3 4 5 6 7 7a 8 8a 9 2f 4b. Test sets failed: none.

which is NOT a conclusive test in this case, because I do not have clients setup with ccd/DEFAULT here. So this is more "it does not break anything else".

I do not like the indentation and wrapping, though:

src/openvpn/multi.c multi_client_connect_source_ccd(struct multi_context *m, · line 2172

Quoted 4 lines
src/openvpn/multi.c +4 -0
+        const char *ccd_client = platform_gen_path(mi->context.options.client_config_dir,
+                                                   tls_common_name(mi->context.c2.tls_multi,
+                                                                   false),
+                                                   &gc);

instead, maybe this?

Review

Quoted 4 lines
(quoted diff) +4 -0
+        const char *ccd_client = 
+             platform_gen_path(mi->context.options.client_config_dir,
+                               tls_common_name(mi->context.c2.tls_multi,
+                                               false), &gc);

or maybe we want to extract "tls_common_name(..., false)" into a temp variable here?

Review

Quoted 3 lines
(quoted diff) +3 -0
+        const char *cn = tls_common_name(mi->context.c2.tls_multi, false);
+        const char *ccd_client = 
+             platform_gen_path(mi->context.options.client_config_dir, 
                                  cn, &gc);

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

Re: [Openvpn-devel] [PATCH v5 03/14] client-connect: Refactor multi_client_connect_source_ccd

Hi,

Quoted 7 lines · Gert Doering

instead, maybe this?

Quoted 4 lines
(quoted diff) +4 -0
+        const char *ccd_client = 
+             platform_gen_path(mi->context.options.client_config_dir,
+                               tls_common_name(mi->context.c2.tls_multi,
+                                               false), &gc);

I also like the above.

Quoted 8 lines

or maybe we want to extract "tls_common_name(..., false)" into a temp variable here?

Review

Quoted 3 lines
(quoted diff) +3 -0
+        const char *cn = tls_common_name(mi->context.c2.tls_multi, false);
+        const char *ccd_client = 
+             platform_gen_path(mi->context.options.client_config_dir, 
                                  cn, &gc);

Imho this is not prettier than the version above :D so I'd personally go with the version above instead of this.

Cheers,

Signature · 1 line
Antonio Quartulli
#9 lore raw

Re: [Openvpn-devel] [PATCH v5 03/14] client-connect: Refactor multi_client_connect_source_ccd

In reply to Arne Schwabe

1x grammar

Quoted 8 lines · Arne Schwabe

From: Fabian Knittel <[email protected]...>

Refactor multi_client_connect_source_ccd(), so that options_server_import() (or the success path in general) is only entered in one place within the function.

Patch V5: Simplify the logic even further to make more easy to understand.

grammar: to make it more easy

Quoted 71 lines
---
  src/openvpn/multi.c | 43 +++++++++++++++++++++----------------------
  1 file changed, 21 insertions(+), 22 deletions(-)
src/openvpn/multi.c +21 -22
index 3c4ceeb5..35e0bd10 100644
@@ -2164,15 +2164,30 @@ multi_client_connect_source_ccd(struct multi_context *m,
      if (mi->context.options.client_config_dir)
      {
          struct gc_arena gc = gc_new();
-        const char *ccd_file;
+        const char *ccd_file = NULL;
+
+        const char *ccd_client = platform_gen_path(mi->context.options.client_config_dir,
+                                                   tls_common_name(mi->context.c2.tls_multi,
+                                                                   false),
+                                                   &gc);
+
+        const char *ccd_default = platform_gen_path(mi->context.options.client_config_dir,
+                                                    CCD_DEFAULT,
+                                                    &gc);
 
-        ccd_file = platform_gen_path(mi->context.options.client_config_dir,
-                                     tls_common_name(mi->context.c2.tls_multi,
-                                                     false),
-                                     &gc);
 
          /* try common-name file */
-        if (platform_test_file(ccd_file))
+        if (platform_test_file(ccd_client))
+        {
+            ccd_file = ccd_client;
+        }
+        /* try default file */
+        else if (platform_test_file(ccd_default))
+        {
+            ccd_file = ccd_default;
+        }
+
+        if (ccd_file)
          {
              options_server_import(&mi->context.options,
                                    ccd_file,
@@ -2181,22 +2196,6 @@ multi_client_connect_source_ccd(struct multi_context *m,
                                    option_types_found,
                                    mi->context.c2.es);
          }
-        else /* try default file */
-        {
-            ccd_file = platform_gen_path(mi->context.options.client_config_dir,
-                                         CCD_DEFAULT,
-                                         &gc);
-
-            if (platform_test_file(ccd_file))
-            {
-                options_server_import(&mi->context.options,
-                                      ccd_file,
-                                      D_IMPORT_ERRORS|M_OPTERR,
-                                      CLIENT_CONNECT_OPT_MASK,
-                                      option_types_found,
-                                      mi->context.c2.es);
-            }
-        }
          gc_free(&gc);
      }
  }
Acked-by on v5 3/14
#10 lore raw

Re: [Openvpn-devel] [PATCH v5 03/14] client-connect: Refactor multi_client_connect_source_ccd

In reply to Arne Schwabe

Hi,

Quoted 11 lines · Arne Schwabe

From: Fabian Knittel <[email protected]...>

Refactor multi_client_connect_source_ccd(), so that options_server_import() (or the success path in general) is only entered in one place within the function.

Patch V5: Simplify the logic even further to make more easy to understand.

---

Shouldn't this patch also bear Fabian's signature?

Anyway, other than the stylistic point made by Gert this patch looks good. It's mostly creating a new interface without really introducing any radical change.

Signature · 1 line
Antonio Quartulli
#11 lore raw

[Openvpn-devel] [PATCH applied] Re: client-connect: Refactor multi_client_connect_source_ccd

In reply to Arne Schwabe

Your patch has been applied to the master branch.

Tested on the server side framework (though I still do not have a ccd/DEFAULT file set up...) - it's not breaking anything unexpected, and the change "should" not break DEFAULT.

Rewrapped as discussed on the list (... I have the itch to go through all the source and rewrap all these "long assignments and the function arguments line up in the last 20 characters of each line"... but that's a different patch)

commit 62a840e2ab3096ded56971a1fa789fe22896ba35

Author: Fabian Knittel
Date:   Sat Jul 11 11:36:44 2020 +0200
     client-connect: Refactor multi_client_connect_source_ccd

     Signed-off-by: Fabian Knittel <[email protected]...>
     Signed-off-by: Arne Schwabe <[email protected]...>
     Acked-by: Antonio Quartulli <[email protected]...>
     Message-Id: <[email protected]...>
     URL: https://www.mail-archive.com/[email protected]/msg20287.html
     Signed-off-by: Gert Doering <[email protected]...>

--
kind regards,

Gert Doering

#12 lore raw

PATCH v5 4/14 client-connect: Move multi_client_connect_setenv into early_setup

In reply to Arne Schwabe

From: Fabian Knittel <[email protected]...>

This patch moves multi_client_connect_setenv into multi_client_connect_early_setup and makes sure that every client-connect handling function updates the virtual address selection.

Background: This unifies how the client-connect handling functions work.

Patch V5: Rebase on master

---
 src/openvpn/multi.c | 22 +++++++++++++---------
 1 file changed, 13 insertions(+), 9 deletions(-)
src/openvpn/multi.c +13 -9
index 35e0bd10..539ebfc0 100644
@@ -2150,6 +2150,12 @@ multi_client_connect_early_setup(struct multi_context *m,
 
     /* reset pool handle to null */
     mi->vaddr_handle = -1;
+
+    /* do --client-connect setenvs */
+    multi_select_virtual_addr(m, mi);
+
+    multi_client_connect_setenv(m, mi);
+
 }
 
 /**
@@ -2195,6 +2201,13 @@ multi_client_connect_source_ccd(struct multi_context *m,
                                   CLIENT_CONNECT_OPT_MASK,
                                   option_types_found,
                                   mi->context.c2.es);
+            /*
+             * Select a virtual address from either --ifconfig-push in
+             * --client-config-dir file or --ifconfig-pool.
+             */
+            multi_select_virtual_addr(m, mi);
+
+            multi_client_connect_setenv(m, mi);
         }
         gc_free(&gc);
     }
@@ -2236,15 +2249,6 @@ multi_connection_established(struct multi_context *m, struct multi_instance *mi)
 
     multi_client_connect_source_ccd(m, mi, &option_types_found);
 
-    /*
-     * Select a virtual address from either --ifconfig-push in
-     * --client-config-dir file or --ifconfig-pool.
-     */
-    multi_select_virtual_addr(m, mi);
-
-    /* do --client-connect setenvs */
-    multi_client_connect_setenv(m, mi);
-
     multi_client_connect_call_plugin_v1(m, mi, &option_types_found,
                                         &cc_succeeded,
                                         &cc_succeeded_count);
Signature · 1 line
2.26.2
#13 lore raw

Re: [Openvpn-devel] [PATCH v5 04/14] client-connect: Move multi_client_connect_setenv into early_setup

Quoted 7 lines · Arne Schwabe

From: Fabian Knittel <[email protected]...>

This patch moves multi_client_connect_setenv into multi_client_connect_early_setup and makes sure that every client-connect handling function updates the virtual address selection.

Background: This unifies how the client-connect handling functions work.

Tested on the server side framework, all tests succeed

23...
Test sets succeeded: 1 1a 1b 1d 2 2a 2b 2c 2d 3 4 5 6 8 8a 9.
Test sets failed: none.
24...
Test sets succeeded: 1 1a 1b 1c 1d 1e 2 2a 2b 2c 2d 2e 3 4 4a 5 6 8 8a 9. Test sets failed: none.
master...
Test sets succeeded: 1 1a 1b 1c 1d 1e 2 2a 2b 2c 2d 2e 3 4 5 6 7 7a 8 8a 9 2f 4b .
Test sets failed: none.

The patch itself looks a bit strange, now code seems to be duplicated...

... hopefully this gets better in the next patches.

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]...
#14 lore raw

Re: [Openvpn-devel] [PATCH v5 04/14] client-connect: Move multi_client_connect_setenv into early_setup

In reply to Arne Schwabe

Hi,

Quoted 39 lines · Arne Schwabe

From: Fabian Knittel <[email protected]...>

This patch moves multi_client_connect_setenv into multi_client_connect_early_setup and makes sure that every client-connect handling function updates the virtual address selection.

Background: This unifies how the client-connect handling functions work.

Patch V5: Rebase on master

---
 src/openvpn/multi.c | 22 +++++++++++++---------
 1 file changed, 13 insertions(+), 9 deletions(-)
src/openvpn/multi.c +6 -0
index 35e0bd10..539ebfc0 100644
@@ -2150,6 +2150,12 @@ multi_client_connect_early_setup(struct multi_context *m,
 
     /* reset pool handle to null */
     mi->vaddr_handle = -1;
+
+    /* do --client-connect setenvs */
+    multi_select_virtual_addr(m, mi);
+
+    multi_client_connect_setenv(m, mi);
+
 }
 
 /**
@@ -2195,6 +2201,13 @@ multi_client_connect_source_ccd(struct multi_context *m,
                                   CLIENT_CONNECT_OPT_MASK,
                                   option_types_found,
                                   mi->context.c2.es);

src/openvpn/multi.c multi_client_connect_source_ccd(struct multi_context *m, · line 2213

Quoted 10 lines
src/openvpn/multi.c +7 -0
+            /*
+             * Select a virtual address from either --ifconfig-push in
+             * --client-config-dir file or --ifconfig-pool.
+             */
+            multi_select_virtual_addr(m, mi);
+
+            multi_client_connect_setenv(m, mi);
         }
         gc_free(&gc);
     }

With this patch applied we are moving these 2 lines to:
- multi_client_connect_source_ccd()
- multi_client_connect_early_setup()

In multi_connection_established() we call the two aforementioned functions one after the other. This means we are really executing the select_virtual_addr() twice in a row. Am I wrong?

Maybe this gets fixed in a later patch, but are we sure we are not breaking anything? If this double call gets rearranged in a later patch...maybe it's better to merge these 2 patches? Bisect may become quite ugly otherwise.

Cheers,

Quoted 17 lines
src/openvpn/multi.c +0 -9
@@ -2236,15 +2249,6 @@ multi_connection_established(struct multi_context *m, struct multi_instance *mi)
 
     multi_client_connect_source_ccd(m, mi, &option_types_found);
 
-    /*
-     * Select a virtual address from either --ifconfig-push in
-     * --client-config-dir file or --ifconfig-pool.
-     */
-    multi_select_virtual_addr(m, mi);
-
-    /* do --client-connect setenvs */
-    multi_client_connect_setenv(m, mi);
-
     multi_client_connect_call_plugin_v1(m, mi, &option_types_found,
                                         &cc_succeeded,
                                         &cc_succeeded_count);
Signature · 1 line
Antonio Quartulli
Acked-by on v5 4/14
#15 lore raw

[Openvpn-devel] [PATCH applied] Re: client-connect: Move multi_client_connect_setenv into early_setup

In reply to Arne Schwabe

Your patch has been applied to the master branch.

It has been stared-at, and tested on the server side test rig.

We've had quite a bit of discussion about this on IRC, and the conclusion is "multiple calls to these functions might be needed in some situations" (like, when a ccd/ file sets up a virtual address and a plugin or script wants to act on the assigned IP address, by means of $ENV{ifconfig_pool_local} etc.) - we might want to make it more clever ("if nothing changes, do not free/reallocate stuff all the time"), or at least join them all in one place with a clear comment

13:10 <@cron2> moving this to multi_client_connect_early_setup() seems wrong 13:10 <@cron2> we should move it to the very end, after all "bring in new
config" bits have been concluded
13:11 <@plaisthos> this might want to have a connect script act on the assigned ip 13:11 <@plaisthos> like dynamic routing etc
13:11 <@plaisthos> and if you move it to the end then you loose the environment
variable with the assigned IP
13:12 <@cron2> in that case, you actually need to really call it after every
single "could alter config" call
13:12 <@cron2> and before the first
13:12 <@plaisthos> technically you don't need it before the first 13:12 <@plaisthos> because the first is ccd
13:12 <@plaisthos> that does not evaluate env
13:12 <@cron2> true
13:13 <@plaisthos> but for consistency and the later refactoring it is better
to do it anyway I guess
13:14 <@cron2> okay. I think 04/ can go in, then, but after the change to
"just walk the table of handlers" I'd move all calls to both multi_select_virtual_addr() and multi_client_connect_setenv() into this loop - so we know "it's evalued after each handler, because further handles might need the environment" - and then we can kick it from early_setup() again
13:14 <@cron2> the stuff is all reentrant, so 04/ is not the most elegant, but
it is not doing harm

commit 380a142a6b397e615ef809a94c5e6be7fcddad06

Author: Fabian Knittel
Date:   Sat Jul 11 11:36:45 2020 +0200
     client-connect: Move multi_client_connect_setenv into early_setup

     Signed-off-by: Fabian Knittel <[email protected]...>
     Signed-off-by: Arne Schwabe <[email protected]...>
     Signed-off-by: Arne Schwabe <[email protected]...>
     Acked-by: Gert Doering <[email protected]...>
     Message-Id: <[email protected]...>
     URL: https://www.mail-archive.com/[email protected]/msg20288.html
     Signed-off-by: Gert Doering <[email protected]...>

--
kind regards,

Gert Doering

#16 lore raw

PATCH v5 5/14 client-connect: Refactor to use return values instead of modifying a passed-in flag

In reply to Arne Schwabe

From: Fabian Knittel <[email protected]...>

This patch changes the way the client-connect helper functions communicate with the main function. Instead of updating cc_succeeded and cc_succeeded_count, they now return either CC_RET_SUCCEEDED, CC_RET_FAILED or CC_RET_SKIPPED.

In addition, the client-connect helpers are now called in completely identical ways. This is in preparation of handling the helpers as simple call-backs.

Patch V5: Minor style fixes

---
 src/openvpn/multi.c | 135 ++++++++++++++++++++++++++------------------
 src/openvpn/multi.h |  10 ++++
 2 files changed, 91 insertions(+), 54 deletions(-)
src/openvpn/multi.c +81 -54
index 539ebfc0..9bb52ef7 100644
@@ -1706,20 +1706,21 @@ multi_client_connect_post_plugin(struct multi_context *m,
 
 #endif /* ifdef ENABLE_PLUGIN */
 
-#ifdef MANAGEMENT_DEF_AUTH
+
 
 /*
  * Called to load management-derived client-connect config
  */
-static void
+enum client_connect_return
 multi_client_connect_mda(struct multi_context *m,
                          struct multi_instance *mi,
                          unsigned int *option_types_found)
 {
+    enum client_connect_return ret = CC_RET_SKIPPED;
+#ifdef MANAGEMENT_DEF_AUTH
     if (mi->cc_config)
     {
         struct buffer_entry *be;
-
         for (be = mi->cc_config->head; be != NULL; be = be->next)
         {
             const char *opt = BSTR(&be->buf);
@@ -1739,10 +1740,12 @@ multi_client_connect_mda(struct multi_context *m,
          */
         multi_select_virtual_addr(m, mi);
         multi_set_virtual_addr_env(mi);
-    }
-}
 
+        ret = CC_RET_SUCCEEDED;
+    }
 #endif /* ifdef MANAGEMENT_DEF_AUTH */
+    return ret;
+}
 
 static void
 multi_client_connect_setenv(struct multi_context *m,
@@ -1840,19 +1843,16 @@ multi_client_set_protocol_options(struct context *c)
     }
 }
 
-static void
+static enum client_connect_return
 multi_client_connect_call_plugin_v1(struct multi_context *m,
                                     struct multi_instance *mi,
-                                    unsigned int *option_types_found,
-                                    int *cc_succeeded,
-                                    int *cc_succeeded_count)
+                                    unsigned int *option_types_found)
 {
+    enum client_connect_return ret = CC_RET_SKIPPED;
 #ifdef ENABLE_PLUGIN
     ASSERT(m);
     ASSERT(mi);
     ASSERT(option_types_found);
-    ASSERT(cc_succeeded);
-    ASSERT(cc_succeeded_count);
 
     /* deprecated callback, use a file for passing back return info */
     if (plugin_defined(mi->context.plugins, OPENVPN_PLUGIN_CLIENT_CONNECT))
@@ -1864,7 +1864,7 @@ multi_client_connect_call_plugin_v1(struct multi_context *m,
 
         if (!dc_file)
         {
-            cc_succeeded = false;
+            ret = CC_RET_FAILED;
             goto cleanup;
         }
 
@@ -1874,12 +1874,12 @@ multi_client_connect_call_plugin_v1(struct multi_context *m,
             != OPENVPN_PLUGIN_FUNC_SUCCESS)
         {
             msg(M_WARN, "WARNING: client-connect plugin call failed");
-            *cc_succeeded = false;
+            ret = CC_RET_FAILED;
         }
         else
         {
             multi_client_connect_post(m, mi, dc_file, option_types_found);
-            (*cc_succeeded_count)++;
+            ret = CC_RET_SUCCEEDED;
         }
 
         if (!platform_unlink(dc_file))
@@ -1893,21 +1893,19 @@ cleanup:
         gc_free(&gc);
     }
 #endif /* ifdef ENABLE_PLUGIN */
+    return ret;
 }
 
-static void
+static enum client_connect_return
 multi_client_connect_call_plugin_v2(struct multi_context *m,
                                     struct multi_instance *mi,
-                                    unsigned int *option_types_found,
-                                    int *cc_succeeded,
-                                    int *cc_succeeded_count)
+                                    unsigned int *option_types_found)
 {
+    enum client_connect_return ret = CC_RET_SKIPPED;
 #ifdef ENABLE_PLUGIN
     ASSERT(m);
     ASSERT(mi);
     ASSERT(option_types_found);
-    ASSERT(cc_succeeded);
-    ASSERT(cc_succeeded_count);
 
     /* V2 callback, use a plugin_return struct for passing back return info */
     if (plugin_defined(mi->context.plugins, OPENVPN_PLUGIN_CLIENT_CONNECT_V2))
@@ -1921,17 +1919,18 @@ multi_client_connect_call_plugin_v2(struct multi_context *m,
             != OPENVPN_PLUGIN_FUNC_SUCCESS)
         {
             msg(M_WARN, "WARNING: client-connect-v2 plugin call failed");
-            *cc_succeeded = false;
+            ret = CC_RET_FAILED;
         }
         else
         {
             multi_client_connect_post_plugin(m, mi, &pr, option_types_found);
-            (*cc_succeeded_count)++;
+            ret = CC_RET_SUCCEEDED;
         }
 
         plugin_return_free(&pr);
     }
 #endif /* ifdef ENABLE_PLUGIN */
+    return ret;
 }
 
 
@@ -1939,15 +1938,17 @@ multi_client_connect_call_plugin_v2(struct multi_context *m,
 /**
  * Runs the --client-connect script if one is defined.
  */
-static void
+static enum client_connect_return
 multi_client_connect_call_script(struct multi_context *m,
                                  struct multi_instance *mi,
-                                 unsigned int *option_types_found,
-                                 int *cc_succeeded,
-                                 int *cc_succeeded_count)
+                                 unsigned int *option_types_found)
 {
+
     ASSERT(m);
     ASSERT(mi);
+
+    enum client_connect_return ret = CC_RET_SKIPPED;
+
     if (mi->context.options.client_connect_script)
     {
         struct argv argv = argv_new();
@@ -1960,7 +1961,7 @@ multi_client_connect_call_script(struct multi_context *m,
                                             "cc", &gc);
         if (!dc_file)
         {
-            *cc_succeeded = false;
+            ret = CC_RET_FAILED;
             goto cleanup;
         }
 
@@ -1970,11 +1971,11 @@ multi_client_connect_call_script(struct multi_context *m,
         if (openvpn_run_script(&argv, mi->context.c2.es, 0, "--client-connect"))
         {
             multi_client_connect_post(m, mi, dc_file, option_types_found);
-            (*cc_succeeded_count)++;
+            ret = CC_RET_SUCCEEDED;
         }
         else
         {
-            *cc_succeeded = false;
+            ret = CC_RET_FAILED;
         }
 
         if (!platform_unlink(dc_file))
@@ -1986,6 +1987,7 @@ cleanup:
         argv_free(&argv);
         gc_free(&gc);
     }
+    return ret;
 }
 
 /**
@@ -2155,18 +2157,18 @@ multi_client_connect_early_setup(struct multi_context *m,
     multi_select_virtual_addr(m, mi);
 
     multi_client_connect_setenv(m, mi);
-
 }
 
 /**
  * Try to source a dynamic config file from the
  * --client-config-dir directory.
  */
-static void
+enum client_connect_return
 multi_client_connect_source_ccd(struct multi_context *m,
                                 struct multi_instance *mi,
                                 unsigned int *option_types_found)
 {
+    enum client_connect_return ret = CC_RET_SKIPPED;
     if (mi->context.options.client_config_dir)
     {
         struct gc_arena gc = gc_new();
@@ -2208,9 +2210,35 @@ multi_client_connect_source_ccd(struct multi_context *m,
             multi_select_virtual_addr(m, mi);
 
             multi_client_connect_setenv(m, mi);
+
+            ret = CC_RET_SUCCEEDED;
         }
         gc_free(&gc);
     }
+    return ret;
+}
+
+static inline bool
+cc_check_return(int *cc_succeeded_count,
+                enum client_connect_return ret)
+{
+    if (ret == CC_RET_SUCCEEDED)
+    {
+        (*cc_succeeded_count)++;
+        return true;
+    }
+    else if (ret == CC_RET_FAILED)
+    {
+        return false;
+    }
+    else if (ret == CC_RET_SKIPPED)
+    {
+        return true;
+    }
+    else
+    {
+        ASSERT(0);
+    }
 }
 
 /*
@@ -2242,38 +2270,40 @@ multi_connection_established(struct multi_context *m, struct multi_instance *mi)
     }
     unsigned int option_types_found = 0;
 
-    int cc_succeeded = true;     /* client connect script status */
+    int cc_succeeded = true; /* client connect script status */
     int cc_succeeded_count = 0;
+    enum client_connect_return ret;
 
     multi_client_connect_early_setup(m, mi);
 
-    multi_client_connect_source_ccd(m, mi, &option_types_found);
+    ret = multi_client_connect_source_ccd(m, mi, &option_types_found);
+    cc_succeeded = cc_check_return(&cc_succeeded_count, ret);
 
-    multi_client_connect_call_plugin_v1(m, mi, &option_types_found,
-                                        &cc_succeeded,
-                                        &cc_succeeded_count);
+    if (cc_succeeded)
+    {
+        ret = multi_client_connect_call_plugin_v1(m, mi, &option_types_found);
+        cc_succeeded = cc_check_return(&cc_succeeded_count, ret);
+    }
+
+    if (cc_succeeded)
+    {
+        ret = multi_client_connect_call_plugin_v2(m, mi, &option_types_found);
+        cc_succeeded = cc_check_return(&cc_succeeded_count, ret);
+    }
 
-    multi_client_connect_call_plugin_v2(m, mi, &option_types_found,
-                                        &cc_succeeded,
-                                        &cc_succeeded_count);
 
-    /*
-     * Check for client-connect script left by management interface client
-     */
     if (cc_succeeded)
     {
-        multi_client_connect_call_script(m, mi, &option_types_found,
-                                         &cc_succeeded,
-                                         &cc_succeeded_count);
+        ret = multi_client_connect_call_script(m, mi, &option_types_found);
+        cc_succeeded = cc_check_return(&cc_succeeded_count, ret);
     }
 
-#ifdef MANAGEMENT_DEF_AUTH
-    if (cc_succeeded && mi->cc_config)
+    if (cc_succeeded)
     {
-        multi_client_connect_mda(m, mi, &option_types_found);
-        ++cc_succeeded_count;
+
+        ret = multi_client_connect_mda(m, mi, &option_types_found);
+        cc_succeeded = cc_check_return(&cc_succeeded_count, ret);
     }
-#endif
 
     /*
      * Check for "disable" directive in client-config-dir file
@@ -2282,13 +2312,11 @@ multi_connection_established(struct multi_context *m, struct multi_instance *mi)
     if (mi->context.options.disable)
     {
         msg(D_MULTI_ERRORS, "MULTI: client has been rejected due to "
-            " 'disable' directive");
+            "'disable' directive");
         cc_succeeded = false;
         cc_succeeded_count = 0;
     }
 
-
-
     if (cc_succeeded)
     {
         multi_client_connect_late_setup(m, mi, option_types_found);
@@ -2300,7 +2328,6 @@ multi_connection_established(struct multi_context *m, struct multi_instance *mi)
             cc_succeeded_count ? CAS_PARTIAL : CAS_FAILED;
     }
 
-
     /* increment number of current authenticated clients */
     ++m->n_clients;
     update_mstat_n_clients(m->n_clients);
src/openvpn/multi.h +10 -0
index c51107f4..4fb4d0b6 100644
@@ -187,6 +187,16 @@ struct multi_context {
     struct deferred_signal_schedule_entry deferred_shutdown_signal;
 };
 
+/**
+ * Return values used by the client connect call-back functions.
+ */
+enum client_connect_return
+{
+    CC_RET_FAILED,
+    CC_RET_SUCCEEDED,
+    CC_RET_SKIPPED
+};
+
 /*
  * Host route
  */
Signature · 1 line
2.26.2
#17 lore raw

Re: [Openvpn-devel] [PATCH v5 05/14] client-connect: Refactor to use return values instead of modifying a passed-in flag

Hi,

Quoted 8 lines · Arne Schwabe

From: Fabian Knittel <[email protected]...>

This patch changes the way the client-connect helper functions communicate with the main function. Instead of updating cc_succeeded and cc_succeeded_count, they now return either CC_RET_SUCCEEDED, CC_RET_FAILED or CC_RET_SKIPPED.

In addition, the client-connect helpers are now called in completely identical ways. This is in preparation of handling the helpers as simple call-backs.

23...
Test sets succeeded: 1 1a 1b 1d 2 2a 2b 2c 2d 3 4 5 6 8 8a 9.
Test sets failed: none.
24...
Test sets succeeded: 1 1a 1b 1c 1d 1e 2 2a 2b 2c 2d 2e 3 4 4a 5 6 8 8a 9. Test sets failed: none.
master...
Test sets succeeded: 1 1a 1b 1c 1d 1e 2 2a 2b 2c 2d 2e 3 4 5 6 7 7a 8 8a 9 2f 4b. Test sets failed: none.

Not sufficient to do instead of a full review, as I only have client-connect scripts (no plugins) and my client-connect scripts do not fail (need to add that to the test set :-) )

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]...
Acked-by on v5 5/14
#18 lore raw

Re: [Openvpn-devel] [PATCH v5 05/14] client-connect: Refactor to use return values instead of modifying a passed-in flag

In reply to Arne Schwabe

Hi,

Quoted 24 lines · Arne Schwabe

From: Fabian Knittel <[email protected]...>

This patch changes the way the client-connect helper functions communicate with the main function. Instead of updating cc_succeeded and cc_succeeded_count, they now return either CC_RET_SUCCEEDED, CC_RET_FAILED or CC_RET_SKIPPED.

In addition, the client-connect helpers are now called in completely identical ways. This is in preparation of handling the helpers as simple call-backs.

Patch V5: Minor style fixes

---
 src/openvpn/multi.c | 135 ++++++++++++++++++++++++++------------------
 src/openvpn/multi.h |  10 ++++
 2 files changed, 91 insertions(+), 54 deletions(-)
src/openvpn/multi.c +0 -0
index 539ebfc0..9bb52ef7 100644

src/openvpn/multi.c multi_client_connect_post_plugin(struct multi_context *m, · line 1709

Quoted 5 lines
src/openvpn/multi.c +1 -1
@@ -1706,20 +1706,21 @@ multi_client_connect_post_plugin(struct multi_context *m,
 
 #endif /* ifdef ENABLE_PLUGIN */
 
-#ifdef MANAGEMENT_DEF_AUTH
+

I would not add this empty line - but nothing critical.

Quoted 132 lines
src/openvpn/multi.c +20 -21
 /*
  * Called to load management-derived client-connect config
  */
-static void
+enum client_connect_return
 multi_client_connect_mda(struct multi_context *m,
                          struct multi_instance *mi,
                          unsigned int *option_types_found)
 {
+    enum client_connect_return ret = CC_RET_SKIPPED;
+#ifdef MANAGEMENT_DEF_AUTH
     if (mi->cc_config)
     {
         struct buffer_entry *be;
-
         for (be = mi->cc_config->head; be != NULL; be = be->next)
         {
             const char *opt = BSTR(&be->buf);
@@ -1739,10 +1740,12 @@ multi_client_connect_mda(struct multi_context *m,
          */
         multi_select_virtual_addr(m, mi);
         multi_set_virtual_addr_env(mi);
-    }
-}
 
+        ret = CC_RET_SUCCEEDED;
+    }
 #endif /* ifdef MANAGEMENT_DEF_AUTH */
+    return ret;
+}
 
 static void
 multi_client_connect_setenv(struct multi_context *m,
@@ -1840,19 +1843,16 @@ multi_client_set_protocol_options(struct context *c)
     }
 }
 
-static void
+static enum client_connect_return
 multi_client_connect_call_plugin_v1(struct multi_context *m,
                                     struct multi_instance *mi,
-                                    unsigned int *option_types_found,
-                                    int *cc_succeeded,
-                                    int *cc_succeeded_count)
+                                    unsigned int *option_types_found)
 {
+    enum client_connect_return ret = CC_RET_SKIPPED;
 #ifdef ENABLE_PLUGIN
     ASSERT(m);
     ASSERT(mi);
     ASSERT(option_types_found);
-    ASSERT(cc_succeeded);
-    ASSERT(cc_succeeded_count);
 
     /* deprecated callback, use a file for passing back return info */
     if (plugin_defined(mi->context.plugins, OPENVPN_PLUGIN_CLIENT_CONNECT))
@@ -1864,7 +1864,7 @@ multi_client_connect_call_plugin_v1(struct multi_context *m,
 
         if (!dc_file)
         {
-            cc_succeeded = false;
+            ret = CC_RET_FAILED;
             goto cleanup;
         }
 
@@ -1874,12 +1874,12 @@ multi_client_connect_call_plugin_v1(struct multi_context *m,
             != OPENVPN_PLUGIN_FUNC_SUCCESS)
         {
             msg(M_WARN, "WARNING: client-connect plugin call failed");
-            *cc_succeeded = false;
+            ret = CC_RET_FAILED;
         }
         else
         {
             multi_client_connect_post(m, mi, dc_file, option_types_found);
-            (*cc_succeeded_count)++;
+            ret = CC_RET_SUCCEEDED;
         }
 
         if (!platform_unlink(dc_file))
@@ -1893,21 +1893,19 @@ cleanup:
         gc_free(&gc);
     }
 #endif /* ifdef ENABLE_PLUGIN */
+    return ret;
 }
 
-static void
+static enum client_connect_return
 multi_client_connect_call_plugin_v2(struct multi_context *m,
                                     struct multi_instance *mi,
-                                    unsigned int *option_types_found,
-                                    int *cc_succeeded,
-                                    int *cc_succeeded_count)
+                                    unsigned int *option_types_found)
 {
+    enum client_connect_return ret = CC_RET_SKIPPED;
 #ifdef ENABLE_PLUGIN
     ASSERT(m);
     ASSERT(mi);
     ASSERT(option_types_found);
-    ASSERT(cc_succeeded);
-    ASSERT(cc_succeeded_count);
 
     /* V2 callback, use a plugin_return struct for passing back return info */
     if (plugin_defined(mi->context.plugins, OPENVPN_PLUGIN_CLIENT_CONNECT_V2))
@@ -1921,17 +1919,18 @@ multi_client_connect_call_plugin_v2(struct multi_context *m,
             != OPENVPN_PLUGIN_FUNC_SUCCESS)
         {
             msg(M_WARN, "WARNING: client-connect-v2 plugin call failed");
-            *cc_succeeded = false;
+            ret = CC_RET_FAILED;
         }
         else
         {
             multi_client_connect_post_plugin(m, mi, &pr, option_types_found);
-            (*cc_succeeded_count)++;
+            ret = CC_RET_SUCCEEDED;
         }
 
         plugin_return_free(&pr);
     }
 #endif /* ifdef ENABLE_PLUGIN */
+    return ret;
 }
 
 
@@ -1939,15 +1938,17 @@ multi_client_connect_call_plugin_v2(struct multi_context *m,
 /**
  * Runs the --client-connect script if one is defined.
  */

src/openvpn/multi.c multi_client_connect_call_plugin_v2(struct multi_context *m, · line 1946

Quoted 10 lines
src/openvpn/multi.c +3 -4
-static void
+static enum client_connect_return
 multi_client_connect_call_script(struct multi_context *m,
                                  struct multi_instance *mi,
-                                 unsigned int *option_types_found,
-                                 int *cc_succeeded,
-                                 int *cc_succeeded_count)
+                                 unsigned int *option_types_found)
 {
+

random empty line?

Quoted 97 lines
src/openvpn/multi.c +35 -5
     ASSERT(m);
     ASSERT(mi);
+
+    enum client_connect_return ret = CC_RET_SKIPPED;
+
     if (mi->context.options.client_connect_script)
     {
         struct argv argv = argv_new();
@@ -1960,7 +1961,7 @@ multi_client_connect_call_script(struct multi_context *m,
                                             "cc", &gc);
         if (!dc_file)
         {
-            *cc_succeeded = false;
+            ret = CC_RET_FAILED;
             goto cleanup;
         }
 
@@ -1970,11 +1971,11 @@ multi_client_connect_call_script(struct multi_context *m,
         if (openvpn_run_script(&argv, mi->context.c2.es, 0, "--client-connect"))
         {
             multi_client_connect_post(m, mi, dc_file, option_types_found);
-            (*cc_succeeded_count)++;
+            ret = CC_RET_SUCCEEDED;
         }
         else
         {
-            *cc_succeeded = false;
+            ret = CC_RET_FAILED;
         }
 
         if (!platform_unlink(dc_file))
@@ -1986,6 +1987,7 @@ cleanup:
         argv_free(&argv);
         gc_free(&gc);
     }
+    return ret;
 }
 
 /**
@@ -2155,18 +2157,18 @@ multi_client_connect_early_setup(struct multi_context *m,
     multi_select_virtual_addr(m, mi);
 
     multi_client_connect_setenv(m, mi);
-
 }
 
 /**
  * Try to source a dynamic config file from the
  * --client-config-dir directory.
  */
-static void
+enum client_connect_return
 multi_client_connect_source_ccd(struct multi_context *m,
                                 struct multi_instance *mi,
                                 unsigned int *option_types_found)
 {
+    enum client_connect_return ret = CC_RET_SKIPPED;
     if (mi->context.options.client_config_dir)
     {
         struct gc_arena gc = gc_new();
@@ -2208,9 +2210,35 @@ multi_client_connect_source_ccd(struct multi_context *m,
             multi_select_virtual_addr(m, mi);
 
             multi_client_connect_setenv(m, mi);
+
+            ret = CC_RET_SUCCEEDED;
         }
         gc_free(&gc);
     }
+    return ret;
+}
+
+static inline bool
+cc_check_return(int *cc_succeeded_count,
+                enum client_connect_return ret)
+{
+    if (ret == CC_RET_SUCCEEDED)
+    {
+        (*cc_succeeded_count)++;
+        return true;
+    }
+    else if (ret == CC_RET_FAILED)
+    {
+        return false;
+    }
+    else if (ret == CC_RET_SKIPPED)
+    {
+        return true;
+    }
+    else
+    {
+        ASSERT(0);
+    }
 }
 
 /*

src/openvpn/multi.c multi_connection_established(struct multi_context *m, struct multi_instance *mi) · line 2273

Quoted 5 lines
src/openvpn/multi.c +1 -1
@@ -2242,38 +2270,40 @@ multi_connection_established(struct multi_context *m, struct multi_instance *mi)
     }
     unsigned int option_types_found = 0;
 
-    int cc_succeeded = true;     /* client connect script status */
+    int cc_succeeded = true; /* client connect script status */

hmm..whatever

Quoted 37 lines
src/openvpn/multi.c +15 -13
     int cc_succeeded_count = 0;
+    enum client_connect_return ret;
 
     multi_client_connect_early_setup(m, mi);
 
-    multi_client_connect_source_ccd(m, mi, &option_types_found);
+    ret = multi_client_connect_source_ccd(m, mi, &option_types_found);
+    cc_succeeded = cc_check_return(&cc_succeeded_count, ret);
 
-    multi_client_connect_call_plugin_v1(m, mi, &option_types_found,
-                                        &cc_succeeded,
-                                        &cc_succeeded_count);
+    if (cc_succeeded)
+    {
+        ret = multi_client_connect_call_plugin_v1(m, mi, &option_types_found);
+        cc_succeeded = cc_check_return(&cc_succeeded_count, ret);
+    }
+
+    if (cc_succeeded)
+    {
+        ret = multi_client_connect_call_plugin_v2(m, mi, &option_types_found);
+        cc_succeeded = cc_check_return(&cc_succeeded_count, ret);
+    }
 
-    multi_client_connect_call_plugin_v2(m, mi, &option_types_found,
-                                        &cc_succeeded,
-                                        &cc_succeeded_count);
 
-    /*
-     * Check for client-connect script left by management interface client
-     */
     if (cc_succeeded)
     {
-        multi_client_connect_call_script(m, mi, &option_types_found,
-                                         &cc_succeeded,
-                                         &cc_succeeded_count);
+        ret = multi_client_connect_call_script(m, mi, &option_types_found);

src/openvpn/multi.c multi_connection_established(struct multi_context *m, struct multi_instance *mi) · line 2303

Quoted 10 lines
src/openvpn/multi.c +3 -4
+        cc_succeeded = cc_check_return(&cc_succeeded_count, ret);
     }
 
-#ifdef MANAGEMENT_DEF_AUTH
-    if (cc_succeeded && mi->cc_config)
+    if (cc_succeeded)
     {
-        multi_client_connect_mda(m, mi, &option_types_found);
-        ++cc_succeeded_count;
+

remove this empty line above

Quoted 42 lines
src/openvpn/multi.c +3 -5
+        ret = multi_client_connect_mda(m, mi, &option_types_found);
+        cc_succeeded = cc_check_return(&cc_succeeded_count, ret);
     }
-#endif
 
     /*
      * Check for "disable" directive in client-config-dir file
@@ -2282,13 +2312,11 @@ multi_connection_established(struct multi_context *m, struct multi_instance *mi)
     if (mi->context.options.disable)
     {
         msg(D_MULTI_ERRORS, "MULTI: client has been rejected due to "
-            " 'disable' directive");
+            "'disable' directive");
         cc_succeeded = false;
         cc_succeeded_count = 0;
     }
 
-
-
     if (cc_succeeded)
     {
         multi_client_connect_late_setup(m, mi, option_types_found);
@@ -2300,7 +2328,6 @@ multi_connection_established(struct multi_context *m, struct multi_instance *mi)
             cc_succeeded_count ? CAS_PARTIAL : CAS_FAILED;
     }
 
-
     /* increment number of current authenticated clients */
     ++m->n_clients;
     update_mstat_n_clients(m->n_clients);
src/openvpn/multi.h +3 -0
index c51107f4..4fb4d0b6 100644
@@ -187,6 +187,16 @@ struct multi_context {
     struct deferred_signal_schedule_entry deferred_shutdown_signal;
 };
 
+/**
+ * Return values used by the client connect call-back functions.
+ */

src/openvpn/multi.h struct multi_context { · line 202

Quoted 10 lines
src/openvpn/multi.h +7 -0
+enum client_connect_return
+{
+    CC_RET_FAILED,
+    CC_RET_SUCCEEDED,
+    CC_RET_SKIPPED
+};
+
 /*
  * Host route
  */

Apart from the few style comments I posted above, this patch looks good.

Using the return value instead of modifying arguments passed via pointer looks much saner and easier to read, imho, so more reasons to go this way.

Signature · 1 line
Antonio Quartulli
#19 lore raw

[Openvpn-devel] [PATCH applied] Re: client-connect: Refactor to use return values instead of modifying a passed-in flag

In reply to Arne Schwabe

Your patch has been applied to the master branch.

White space has been whacked as instructed :-)

Tested (yesterday) already on the server test rig, all good.

The code changes look good (though I find the patch granularity "too fine", with all the extra calls to cc_check_return() added just to have them removed again further down the series). I object to the use of the term "call-back" functions, because they aren't... it's just a series of helpers that are called via a function pointer array later on, but there is no "back" involved anywhere ("pass a function pointer down to a library that does a call-back into your code to do things").

commit 4f29b73b168904a764151f1acef0ea73c0fcece1

Author: Fabian Knittel
Date:   Sat Jul 11 11:36:46 2020 +0200
     client-connect: Refactor to use return values instead of modifying a passed-in flag

     Signed-off-by: Fabian Knittel <[email protected]...>
     Signed-off-by: Arne Schwabe <[email protected]...>
     Acked-by: Antonio Quartulli <[email protected]...>
     Message-Id: <[email protected]...>
     URL: https://www.mail-archive.com/[email protected]/msg20286.html
     Signed-off-by: Gert Doering <[email protected]...>

--
kind regards,

Gert Doering

#20 lore raw

PATCH v5 6/14 client-connect: Refactor client-connect handling to calling a bunch of hooks in a loop

In reply to Arne Schwabe

From: Fabian Knittel <[email protected]...>

This patch changes the calling of the client-connect functions into an array of hooks and a block of code that calls them in a loop.

Patch V5: Rebase on master.

---
 src/openvpn/multi.c | 43 +++++++++++++++++--------------------------
 1 file changed, 17 insertions(+), 26 deletions(-)
src/openvpn/multi.c +17 -26
index 9bb52ef7..83848fdc 100644
@@ -2241,6 +2241,10 @@ cc_check_return(int *cc_succeeded_count,
     }
 }
 
+typedef enum client_connect_return (*multi_client_connect_handler)
+    (struct multi_context *m, struct multi_instance *mi,
+     unsigned int *option_types_found);
+
 /*
  * Called as soon as the SSL/TLS connection is authenticated.
  *
@@ -2268,7 +2272,17 @@ multi_connection_established(struct multi_context *m, struct multi_instance *mi)
     {
         return;
     }
-    unsigned int option_types_found = 0;
+
+        multi_client_connect_handler handlers[] = {
+            multi_client_connect_source_ccd,
+            multi_client_connect_call_plugin_v1,
+            multi_client_connect_call_plugin_v2,
+            multi_client_connect_call_script,
+            multi_client_connect_mda,
+            NULL
+        };
+
+        unsigned int option_types_found = 0;
 
     int cc_succeeded = true; /* client connect script status */
     int cc_succeeded_count = 0;
@@ -2276,32 +2290,9 @@ multi_connection_established(struct multi_context *m, struct multi_instance *mi)
 
     multi_client_connect_early_setup(m, mi);
 
-    ret = multi_client_connect_source_ccd(m, mi, &option_types_found);
-    cc_succeeded = cc_check_return(&cc_succeeded_count, ret);
-
-    if (cc_succeeded)
-    {
-        ret = multi_client_connect_call_plugin_v1(m, mi, &option_types_found);
-        cc_succeeded = cc_check_return(&cc_succeeded_count, ret);
-    }
-
-    if (cc_succeeded)
-    {
-        ret = multi_client_connect_call_plugin_v2(m, mi, &option_types_found);
-        cc_succeeded = cc_check_return(&cc_succeeded_count, ret);
-    }
-
-
-    if (cc_succeeded)
+    for (int i = 0; cc_succeeded && handlers[i]; i++)
     {
-        ret = multi_client_connect_call_script(m, mi, &option_types_found);
-        cc_succeeded = cc_check_return(&cc_succeeded_count, ret);
-    }
-
-    if (cc_succeeded)
-    {
-
-        ret = multi_client_connect_mda(m, mi, &option_types_found);
+        ret = handlers[i](m, mi, &option_types_found);
         cc_succeeded = cc_check_return(&cc_succeeded_count, ret);
     }
 
Signature · 1 line
2.26.2
#21 lore raw

Re: [Openvpn-devel] [PATCH v5 06/14] client-connect: Refactor client-connect handling to calling a bunch of hooks in a loop

Hi,

Quoted 7 lines · Arne Schwabe

From: Fabian Knittel <[email protected]...>

This patch changes the calling of the client-connect functions into an array of hooks and a block of code that calls them in a loop.

23...
Test sets succeeded: 1 1a 1b 1d 2 2a 2b 2c 2d 3 4 5 6 8 8a 9.
Test sets failed: none.
24...
Test sets succeeded: 1 1a 1b 1c 1d 1e 2 2a 2b 2c 2d 2e 3 4 4a 5 6 8 8a 9. Test sets failed: none.
master...
Test sets succeeded: 1 1a 1b 1c 1d 1e 2 2a 2b 2c 2d 2e 3 4 5 6 7 7a 8 8a 9 2f 4b. Test sets failed: none.

I dot not test all variants yet, though, so this is not a sufficient test to say "everything works".

Generally speaking the patch looks quite reasonable.

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]...
Acked-by on v5 6/14
#22 lore raw

Re: [Openvpn-devel] [PATCH v5 06/14] client-connect: Refactor client-connect handling to calling a bunch of hooks in a loop

In reply to Arne Schwabe

Hi,

Quoted 20 lines · Arne Schwabe

From: Fabian Knittel <[email protected]...>

This patch changes the calling of the client-connect functions into an array of hooks and a block of code that calls them in a loop.

Patch V5: Rebase on master.

---
 src/openvpn/multi.c | 43 +++++++++++++++++--------------------------
 1 file changed, 17 insertions(+), 26 deletions(-)
src/openvpn/multi.c +0 -0
index 9bb52ef7..83848fdc 100644

src/openvpn/multi.c cc_check_return(int *cc_succeeded_count, · line 2247

Quoted 7 lines
src/openvpn/multi.c +4 -0
@@ -2241,6 +2241,10 @@ cc_check_return(int *cc_succeeded_count,
     }
 }
 
+typedef enum client_connect_return (*multi_client_connect_handler)
+    (struct multi_context *m, struct multi_instance *mi,
+     unsigned int *option_types_found);
+

how about something like this:

2519 typedef enum client_connect_return
2520 (*multi_client_connect_handler)(struct multi_context *m,
2521 struct multi_instance *mi,
2522 unsigned int *option_types_found);
2523

I find it easier to read (just my opinion)

Quoted 9 lines
src/openvpn/multi.c +1 -1
 /*
  * Called as soon as the SSL/TLS connection is authenticated.
  *
@@ -2268,7 +2272,17 @@ multi_connection_established(struct multi_context *m, struct multi_instance *mi)
     {
         return;
     }
-    unsigned int option_types_found = 0;
+

src/openvpn/multi.c multi_connection_established(struct multi_context *m, struct multi_instance *mi) · line 2285

Quoted 10 lines
src/openvpn/multi.c +10 -0
+        multi_client_connect_handler handlers[] = {
+            multi_client_connect_source_ccd,
+            multi_client_connect_call_plugin_v1,
+            multi_client_connect_call_plugin_v2,
+            multi_client_connect_call_script,
+            multi_client_connect_mda,
+            NULL
+        };
+
+        unsigned int option_types_found = 0;

something is wrong in the indentation of the chunk above: too many spaces.

Quoted 29 lines
src/openvpn/multi.c +1 -19
     int cc_succeeded = true; /* client connect script status */
     int cc_succeeded_count = 0;
@@ -2276,32 +2290,9 @@ multi_connection_established(struct multi_context *m, struct multi_instance *mi)
 
     multi_client_connect_early_setup(m, mi);
 
-    ret = multi_client_connect_source_ccd(m, mi, &option_types_found);
-    cc_succeeded = cc_check_return(&cc_succeeded_count, ret);
-
-    if (cc_succeeded)
-    {
-        ret = multi_client_connect_call_plugin_v1(m, mi, &option_types_found);
-        cc_succeeded = cc_check_return(&cc_succeeded_count, ret);
-    }
-
-    if (cc_succeeded)
-    {
-        ret = multi_client_connect_call_plugin_v2(m, mi, &option_types_found);
-        cc_succeeded = cc_check_return(&cc_succeeded_count, ret);
-    }
-
-
-    if (cc_succeeded)
+    for (int i = 0; cc_succeeded && handlers[i]; i++)
     {
-        ret = multi_client_connect_call_script(m, mi, &option_types_found);
-        cc_succeeded = cc_check_return(&cc_succeeded_count, ret);

src/openvpn/multi.c multi_connection_established(struct multi_context *m, struct multi_instance *mi) · line 2298

Quoted 10 lines
src/openvpn/multi.c +1 -6
-    }
-
-    if (cc_succeeded)
-    {
-
-        ret = multi_client_connect_mda(m, mi, &option_types_found);
+        ret = handlers[i](m, mi, &option_types_found);
         cc_succeeded = cc_check_return(&cc_succeeded_count, ret);
     }
 

Except for the indentation issue, the rest looks good. This patch simply makes the handlers invocation more generic and part of a loop.

Signature · 1 line
Antonio Quartulli
#23 lore raw

[Openvpn-devel] [PATCH applied] Re: client-connect: Refactor client-connect handling to calling a bunch of hooks in a loop

In reply to Arne Schwabe

Your patch has been applied to the master branch.

I have *not* reformatted the multi_client_connect_handler block - this is stuff that goes away in 08/14 again, so reformatting now is doubly futile.

(Speaking of patch granularity: the combination of 05+06+08 is causing three times work for something which is basically one change - as the result of 05 goes away in 06, and 06 gets rewritten in 08 again. This makes review and testing significantly more work than just doing the change)

commit 07a69fd2ca934c0521b0ee25f72e7f2385279a71

Author: Fabian Knittel
Date:   Sat Jul 11 11:36:47 2020 +0200
     client-connect: Refactor client-connect handling to calling a bunch of hooks in a loop

     Signed-off-by: Fabian Knittel <[email protected]...>
     Signed-off-by: Arne Schwabe <[email protected]...>
     Signed-off-by: Arne Schwabe <[email protected]...>
     Acked-by: Antonio Quartulli <[email protected]...>
     Message-Id: <[email protected]...>
     URL: https://www.mail-archive.com/[email protected]/msg20293.html
     Signed-off-by: Gert Doering <[email protected]...>

--
kind regards,

Gert Doering

#24 lore raw

PATCH v5 7/14 client-connect: Change cas_context from int to enum

In reply to Arne Schwabe

This deviates from Fabian's original patch that relied on the now removed connection_established bool as pointer being NULL or non NULL as implicit third state and makeing connection_established as a substate of (cas_context == CAS_PENDING)

Patch V5: extend cas_context with two new states instead adding an
extra mini state machine.

---
 src/openvpn/multi.c   |  2 +-
 src/openvpn/multi.h   |  1 +
 src/openvpn/openvpn.h | 24 +++++++++++++++++-------
 3 files changed, 19 insertions(+), 8 deletions(-)
src/openvpn/multi.c +1 -1
index 83848fdc..f9b8af80 100644
@@ -2163,7 +2163,7 @@ multi_client_connect_early_setup(struct multi_context *m,
  * Try to source a dynamic config file from the
  * --client-config-dir directory.
  */
-enum client_connect_return
+static enum client_connect_return
 multi_client_connect_source_ccd(struct multi_context *m,
                                 struct multi_instance *mi,
                                 unsigned int *option_types_found)
src/openvpn/multi.h +1 -0
index 4fb4d0b6..1d30dcc6 100644
@@ -62,6 +62,7 @@ struct deferred_signal_schedule_entry
     struct timeval wakeup;
 };
 
+
 /**
  * Server-mode state structure for one single VPN tunnel.
  *
src/openvpn/openvpn.h +17 -7
index a1308852..7c469b01 100644
@@ -210,6 +210,21 @@ struct context_1
 #endif
 };
 
+
+/* client authentication state, CAS_SUCCEEDED must be 0 since
+ * non multi code path still checks this variable but does not initialise it
+ * so the code depends on zero initialisation */
+enum client_connect_status {
+    CAS_SUCCEEDED=0,
+    CAS_PENDING,
+    CAS_FAILED,
+    CAS_PARTIAL,        /**< Variant of CAS_FAILED: at least one
+                         * client-connect script/plugin succeeded
+                         * while a later one in the chain failed
+                         * (we still need cleanup compared to FAILED)
+                         */
+};
+
 /**
  * Level 2 %context containing state that is reset on both \c SIGHUP and
  * \c SIGUSR1 restarts.
@@ -444,13 +459,8 @@ struct context_2
     int push_ifconfig_ipv6_netbits;
     struct in6_addr push_ifconfig_ipv6_remote;
 
-    /* client authentication state, CAS_SUCCEEDED must be 0 */
-#define CAS_SUCCEEDED 0
-#define CAS_PENDING   1
-#define CAS_FAILED    2
-#define CAS_PARTIAL   3  /* at least one client-connect script/plugin
-                          * succeeded while a later one in the chain failed */
-    int context_auth;
+
+    enum client_connect_status context_auth;
 
     struct event_timeout push_request_interval;
     int n_sent_push_requests;
Signature · 1 line
2.26.2
#25 lore raw

Re: [Openvpn-devel] [PATCH v5 07/14] client-connect: Change cas_context from int to enum

1x typo

Quoted 3 lines · Arne Schwabe

This deviates from Fabian's original patch that relied on the now removed connection_established bool as pointer being NULL or non NULL as implicit third state and makeing connection_established as a substate of

makeing -> making

Quoted 82 lines

(cas_context == CAS_PENDING)

Patch V5: extend cas_context with two new states instead adding an
extra mini state machine.

---
  src/openvpn/multi.c   |  2 +-
  src/openvpn/multi.h   |  1 +
  src/openvpn/openvpn.h | 24 +++++++++++++++++-------
  3 files changed, 19 insertions(+), 8 deletions(-)
src/openvpn/multi.c +1 -1
index 83848fdc..f9b8af80 100644
@@ -2163,7 +2163,7 @@ multi_client_connect_early_setup(struct multi_context *m,
   * Try to source a dynamic config file from the
   * --client-config-dir directory.
   */
-enum client_connect_return
+static enum client_connect_return
  multi_client_connect_source_ccd(struct multi_context *m,
                                  struct multi_instance *mi,
                                  unsigned int *option_types_found)
src/openvpn/multi.h +1 -0
index 4fb4d0b6..1d30dcc6 100644
@@ -62,6 +62,7 @@ struct deferred_signal_schedule_entry
      struct timeval wakeup;
  };
 
+
  /**
   * Server-mode state structure for one single VPN tunnel.
   *
src/openvpn/openvpn.h +17 -7
index a1308852..7c469b01 100644
@@ -210,6 +210,21 @@ struct context_1
  #endif
  };
 
+
+/* client authentication state, CAS_SUCCEEDED must be 0 since
+ * non multi code path still checks this variable but does not initialise it
+ * so the code depends on zero initialisation */
+enum client_connect_status {
+    CAS_SUCCEEDED=0,
+    CAS_PENDING,
+    CAS_FAILED,
+    CAS_PARTIAL,        /**< Variant of CAS_FAILED: at least one
+                         * client-connect script/plugin succeeded
+                         * while a later one in the chain failed
+                         * (we still need cleanup compared to FAILED)
+                         */
+};
+
  /**
   * Level 2 %context containing state that is reset on both \c SIGHUP and
   * \c SIGUSR1 restarts.
@@ -444,13 +459,8 @@ struct context_2
      int push_ifconfig_ipv6_netbits;
      struct in6_addr push_ifconfig_ipv6_remote;
 
-    /* client authentication state, CAS_SUCCEEDED must be 0 */
-#define CAS_SUCCEEDED 0
-#define CAS_PENDING   1
-#define CAS_FAILED    2
-#define CAS_PARTIAL   3  /* at least one client-connect script/plugin
-                          * succeeded while a later one in the chain failed */
-    int context_auth;
+
+    enum client_connect_status context_auth;
 
      struct event_timeout push_request_interval;
      int n_sent_push_requests;
#26 lore raw

Re: [Openvpn-devel] [PATCH v5 07/14] client-connect: Change cas_context from int to enum

In reply to Arne Schwabe

Hi,

Quoted 9 lines · Arne Schwabe

This deviates from Fabian's original patch that relied on the now removed connection_established bool as pointer being NULL or non NULL as implicit third state and makeing connection_established as a substate of (cas_context == CAS_PENDING)

Patch V5: extend cas_context with two new states instead adding an
extra mini state machine.

23...
Test sets succeeded: 1 1a 1b 1d 2 2a 2b 2c 2d 3 4 5 6 8 8a 9.
Test sets failed: none.
24...
Test sets succeeded: 1 1a 1b 1c 1d 1e 2 2b 2c 2d 2e 3 4 4a 5 6 8 8a 9. Test sets failed: 2a.
master...
Test sets succeeded: 1 1a 1b 1c 1d 1e 2 2a 2b 2c 2d 2e 3 4 5 6 7 7a 8 8a 9 2f 4b. Test sets failed: none.

For the changes in this patch, this is a reasonably conclusive test.

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]...
Acked-by on v5 7/14
#27 lore raw

Re: [Openvpn-devel] [PATCH v5 07/14] client-connect: Change cas_context from int to enum

In reply to Arne Schwabe

Hi,

Quoted 22 lines · Arne Schwabe

This deviates from Fabian's original patch that relied on the now removed connection_established bool as pointer being NULL or non NULL as implicit third state and makeing connection_established as a substate of (cas_context == CAS_PENDING)

Patch V5: extend cas_context with two new states instead adding an
extra mini state machine.

---
 src/openvpn/multi.c   |  2 +-
 src/openvpn/multi.h   |  1 +
 src/openvpn/openvpn.h | 24 +++++++++++++++++-------
 3 files changed, 19 insertions(+), 8 deletions(-)
src/openvpn/multi.c +0 -0
index 83848fdc..f9b8af80 100644

src/openvpn/multi.c multi_client_connect_early_setup(struct multi_context *m, · line 2166

Quoted 5 lines
src/openvpn/multi.c +1 -1
@@ -2163,7 +2163,7 @@ multi_client_connect_early_setup(struct multi_context *m,
  * Try to source a dynamic config file from the
  * --client-config-dir directory.
  */
-enum client_connect_return
+static enum client_connect_return

I don't think this change belongs to this patch .... ?

Quoted 20 lines
src/openvpn/multi.c +0 -0
 multi_client_connect_source_ccd(struct multi_context *m,
                                 struct multi_instance *mi,
                                 unsigned int *option_types_found)
src/openvpn/multi.h +1 -0
index 4fb4d0b6..1d30dcc6 100644
@@ -62,6 +62,7 @@ struct deferred_signal_schedule_entry
     struct timeval wakeup;
 };
 
+
 /**
  * Server-mode state structure for one single VPN tunnel.
  *
src/openvpn/openvpn.h +0 -0
index a1308852..7c469b01 100644

src/openvpn/openvpn.h struct context_1 · line 218

Quoted 9 lines
src/openvpn/openvpn.h +6 -0
@@ -210,6 +210,21 @@ struct context_1
 #endif
 };
 
+
+/* client authentication state, CAS_SUCCEEDED must be 0 since
+ * non multi code path still checks this variable but does not initialise it
+ * so the code depends on zero initialisation */
+enum client_connect_status {
+    CAS_SUCCEEDED=0,

Please add spaces around '='

Quoted 19 lines
src/openvpn/openvpn.h +9 -2
+    CAS_PENDING,
+    CAS_FAILED,
+    CAS_PARTIAL,        /**< Variant of CAS_FAILED: at least one
+                         * client-connect script/plugin succeeded
+                         * while a later one in the chain failed
+                         * (we still need cleanup compared to FAILED)
+                         */
+};
+
 /**
  * Level 2 %context containing state that is reset on both \c SIGHUP and
  * \c SIGUSR1 restarts.
@@ -444,13 +459,8 @@ struct context_2
     int push_ifconfig_ipv6_netbits;
     struct in6_addr push_ifconfig_ipv6_remote;
 
-    /* client authentication state, CAS_SUCCEEDED must be 0 */
-#define CAS_SUCCEEDED 0

src/openvpn/openvpn.h struct context_2 · line 466

Quoted 10 lines
src/openvpn/openvpn.h +2 -5
-#define CAS_PENDING   1
-#define CAS_FAILED    2
-#define CAS_PARTIAL   3  /* at least one client-connect script/plugin
-                          * succeeded while a later one in the chain failed */
-    int context_auth;
+
+    enum client_connect_status context_auth;
 
     struct event_timeout push_request_interval;
     int n_sent_push_requests;

The rest looks good and makes sense. Using enum is always better as the compiler (and the reader) has extra information about how a variable can be used.

I believe the first chunk with the +static should be moved somewhere else...but other than that and the missing-spaces comment:

Signature · 1 line
Antonio Quartulli
#28 lore raw

[Openvpn-devel] [PATCH applied] Re: client-connect: Change cas_context from int to enum

In reply to Arne Schwabe

Your patch has been applied to the master branch.

Fixed the typo in the commit message as requested.

Tested yesterday on client and server testbed, which tests all these states.

I have not modified the "static" hunk - it would be a better fit in another patch, but it's useful, reasonably harmless and does not warrant a new round with a "v6" for two patches just to move this hunk.

commit 4d500451e8b19deadb619d4ecf977363c6560a2e

Author: Arne Schwabe
Date:   Sat Jul 11 11:36:48 2020 +0200
     client-connect: Change cas_context from int to enum

     Signed-off-by: Arne Schwabe <[email protected]...>
     Signed-off-by: Arne Schwabe <[email protected]...>
     Acked-by: Antonio Quartulli <[email protected]...>
     Message-Id: <[email protected]...>
     URL: https://www.mail-archive.com/[email protected]/msg20292.html
     Signed-off-by: Gert Doering <[email protected]...>

--
kind regards,

Gert Doering

#29 lore raw

PATCH v5 8/14 client-connect: Add CC_RET_DEFERRED and cope with deferred client-connect

In reply to Arne Schwabe

From: Fabian Knittel <[email protected]...>

This patch moves the state, that was previously tracked within the multi_connection_established() function, into struct client_connect_state. The multi_connection_established() function can now be exited and re-entered as many times as necessary - without losing the client-connect handling state.

The patch also adds the new return value CC_RET_DEFERRED which indicates that the handler couldn't complete immediately, and needs to be called later. At that point multi_connection_established() will exit without indicating completion.

Each client-connect handler now has an (optional) additional call-back: The call-back for handling the deferred case. If the main call-back returns CC_RET_DEFERRED, the next call to the handler will be through the deferred call-back.

Patch V3: Use a static struct in multi_instance instead of using
malloc/free and use two states (deffered with and without result) instead of one to eliminate the counter that was only tested for > 0.

Patch V5: Use new states in context_auth instead of the extra state
that the patch series previously used.

---
 src/openvpn/multi.c   | 171 +++++++++++++++++++++++++++++++-----------
 src/openvpn/multi.h   |  15 +++-
 src/openvpn/openvpn.h |   9 +++
 3 files changed, 150 insertions(+), 45 deletions(-)
src/openvpn/multi.c +127 -44
index f9b8af80..ce73f8a1 100644
@@ -2218,32 +2218,51 @@ multi_client_connect_source_ccd(struct multi_context *m,
     return ret;
 }
 
-static inline bool
-cc_check_return(int *cc_succeeded_count,
-                enum client_connect_return ret)
+typedef enum client_connect_return (*multi_client_connect_handler)
+    (struct multi_context *m, struct multi_instance *mi,
+    unsigned int *option_types_found);
+
+struct client_connect_handlers
+{
+    multi_client_connect_handler main;
+    multi_client_connect_handler deferred;
+};
+
+static enum client_connect_return
+multi_client_connect_fail(struct multi_context *m, struct multi_instance *mi,
+                          unsigned int *option_types_found)
 {
-    if (ret == CC_RET_SUCCEEDED)
+    /* Called null call-back.  This should never happen. */
+    return CC_RET_FAILED;
+}
+
+static const struct client_connect_handlers client_connect_handlers[] = {
     {
-        (*cc_succeeded_count)++;
-        return true;
-    }
-    else if (ret == CC_RET_FAILED)
+        .main = multi_client_connect_source_ccd,
+        .deferred = multi_client_connect_fail
+    },
     {
-        return false;
-    }
-    else if (ret == CC_RET_SKIPPED)
+        .main = multi_client_connect_call_plugin_v1,
+        .deferred = multi_client_connect_fail
+    },
     {
-        return true;
-    }
-    else
+        .main = multi_client_connect_call_plugin_v2,
+        .deferred = multi_client_connect_fail
+    },
+    {
+        .main = multi_client_connect_call_script,
+        .deferred = multi_client_connect_fail
+    },
     {
-        ASSERT(0);
+        .main = multi_client_connect_mda,
+        .deferred = multi_client_connect_fail
+    },
+    {
+        .main = NULL,
+        .deferred = NULL
+                    /* End of list sentinel.  */
     }
-}
-
-typedef enum client_connect_return (*multi_client_connect_handler)
-    (struct multi_context *m, struct multi_instance *mi,
-     unsigned int *option_types_found);
+};
 
 /*
  * Called as soon as the SSL/TLS connection is authenticated.
@@ -2273,27 +2292,83 @@ multi_connection_established(struct multi_context *m, struct multi_instance *mi)
         return;
     }
 
-        multi_client_connect_handler handlers[] = {
-            multi_client_connect_source_ccd,
-            multi_client_connect_call_plugin_v1,
-            multi_client_connect_call_plugin_v2,
-            multi_client_connect_call_script,
-            multi_client_connect_mda,
-            NULL
-        };
-
-        unsigned int option_types_found = 0;
+    /* We are only called for the CAS_PENDING_x states, so we
+     * can ignore other states here */
+    bool from_deferred = (mi->context.c2.context_auth != CAS_PENDING);
 
-    int cc_succeeded = true; /* client connect script status */
-    int cc_succeeded_count = 0;
     enum client_connect_return ret;
 
-    multi_client_connect_early_setup(m, mi);
+    struct client_connect_defer_state *defer_state =
+        &(mi->client_connect_defer_state);
 
-    for (int i = 0; cc_succeeded && handlers[i]; i++)
+    /* We are called for the first time */
+    if (!from_deferred)
     {
-        ret = handlers[i](m, mi, &option_types_found);
-        cc_succeeded = cc_check_return(&cc_succeeded_count, ret);
+        defer_state->cur_handler_index = 0;
+        defer_state->option_types_found = 0;
+        /* Initially we have no handler that has returned a result */
+        mi->context.c2.context_auth = CAS_PENDING_DEFERRED;
+
+        multi_client_connect_early_setup(m, mi);
+    }
+
+    bool cc_succeeded = true;
+
+    while (cc_succeeded
+           && client_connect_handlers[defer_state->cur_handler_index]
+           .main != NULL)
+    {
+        multi_client_connect_handler handler;
+        if (from_deferred)
+        {
+            handler = client_connect_handlers
+                      [defer_state->cur_handler_index].deferred;
+            from_deferred = false;
+        }
+        else
+        {
+            handler = client_connect_handlers
+                      [defer_state->cur_handler_index].main;
+        }
+
+        ret = handler(m, mi, &(defer_state->option_types_found));
+        if (ret == CC_RET_SUCCEEDED)
+        {
+            /*
+             * Remember that we already had at least one handler
+             * returning a result should go to into deferred state
+             */
+            mi->context.c2.context_auth = CAS_PENDING_DEFERRED_PARTIAL;
+        }
+        else if (ret == CC_RET_SKIPPED)
+        {
+            /*
+             * Move on with the next handler without modifying any
+             * other state
+             */
+        }
+        else if (ret == CC_RET_DEFERRED)
+        {
+            /*
+             * we already set client_connect_status to DEFERRED_RESULT or
+             * DEFERRED_NO_RESULT and increased index. We just return
+             * from the function as having client_connect_status
+             */
+            return;
+        }
+        else if (ret == CC_RET_FAILED)
+        {
+            /*
+             * One handler failed. We abort the chain and set the final
+             * result to failed
+             */
+            cc_succeeded = false;
+        }
+        else
+        {
+            ASSERT(0);
+        }
+        (defer_state->cur_handler_index)++;
     }
 
     /*
@@ -2305,18 +2380,26 @@ multi_connection_established(struct multi_context *m, struct multi_instance *mi)
         msg(D_MULTI_ERRORS, "MULTI: client has been rejected due to "
             "'disable' directive");
         cc_succeeded = false;
-        cc_succeeded_count = 0;
     }
 
     if (cc_succeeded)
     {
-        multi_client_connect_late_setup(m, mi, option_types_found);
+        multi_client_connect_late_setup(m, mi,
+                                        defer_state->option_types_found);
     }
     else
     {
-        /* set context-level authentication flag */
-        mi->context.c2.context_auth =
-            cc_succeeded_count ? CAS_PARTIAL : CAS_FAILED;
+        /* set context-level authentication flag to failed but remember
+         * if had a handler succeed (for cleanup) */
+        if (mi->context.c2.context_auth == CAS_PENDING_DEFERRED_PARTIAL)
+        {
+            mi->context.c2.context_auth = CAS_PARTIAL;
+        }
+        else
+        {
+            mi->context.c2.context_auth = CAS_FAILED;
+        }
+
     }
 
     /* increment number of current authenticated clients */
@@ -2604,7 +2687,7 @@ multi_process_post(struct multi_context *m, struct multi_instance *mi, const uns
         {
             /* connection is "established" when SSL/TLS key negotiation succeeds
              * and (if specified) auth user/pass succeeds */
-            if (mi->context.c2.context_auth == CAS_PENDING
+            if (is_cas_pending(mi->context.c2.context_auth)
                 && CONNECTION_ESTABLISHED(&mi->context))
             {
                 multi_connection_established(m, mi);
@@ -3559,7 +3642,7 @@ management_client_auth(void *arg,
         {
             if (auth)
             {
-                if (mi->context.c2.context_auth == CAS_PENDING)
+                if (is_cas_pending(mi->context.c2.context_auth))
                 {
                     set_cc_config(mi, cc_config);
                     cc_config_owned = false;
@@ -3571,7 +3654,7 @@ management_client_auth(void *arg,
                 {
                     msg(D_MULTI_LOW, "MULTI: connection rejected: %s, CLI:%s", reason, np(client_reason));
                 }
-                if (mi->context.c2.context_auth != CAS_PENDING)
+                if (!is_cas_pending(mi->context.c2.context_auth))
                 {
                     send_auth_failed(&mi->context, client_reason); /* mid-session reauth failed */
                     multi_schedule_context_wakeup(m, mi);
src/openvpn/multi.h +14 -1
index 1d30dcc6..11da0209 100644
@@ -62,6 +62,18 @@ struct deferred_signal_schedule_entry
     struct timeval wakeup;
 };
 
+/**
+ * Detached client connection state.  This is the state that is tracked while
+ * the client connect hooks are executed.
+ */
+struct client_connect_defer_state
+{
+    /* Index of currently executed handler.  */
+    int cur_handler_index;
+    /* Remember which option classes where processed for delayed option
+     * handling. */
+    unsigned int option_types_found;
+};
 
 /**
  * Server-mode state structure for one single VPN tunnel.
@@ -108,7 +120,7 @@ struct multi_instance {
 
     struct context context;     /**< The context structure storing state
                                  *   for this VPN tunnel. */
-
+    struct client_connect_defer_state client_connect_defer_state;
 #ifdef ENABLE_ASYNC_PUSH
     int inotify_watch; /* watch descriptor for acf */
 #endif
@@ -195,6 +207,7 @@ enum client_connect_return
 {
     CC_RET_FAILED,
     CC_RET_SUCCEEDED,
+    CC_RET_DEFERRED,
     CC_RET_SKIPPED
 };
 
src/openvpn/openvpn.h +9 -0
index 7c469b01..ccc7f118 100644
@@ -217,6 +217,8 @@ struct context_1
 enum client_connect_status {
     CAS_SUCCEEDED=0,
     CAS_PENDING,
+    CAS_PENDING_DEFERRED,
+    CAS_PENDING_DEFERRED_PARTIAL,   /**< at least handler succeeded, no result yet*/
     CAS_FAILED,
     CAS_PARTIAL,        /**< Variant of CAS_FAILED: at least one
                          * client-connect script/plugin succeeded
@@ -225,6 +227,13 @@ enum client_connect_status {
                          */
 };
 
+static inline bool
+is_cas_pending(enum client_connect_status cas)
+{
+    return cas == CAS_PENDING || cas == CAS_PENDING_DEFERRED
+           || cas == CAS_PENDING_DEFERRED_PARTIAL;
+}
+
 /**
  * Level 2 %context containing state that is reset on both \c SIGHUP and
  * \c SIGUSR1 restarts.
Signature · 1 line
2.26.2
#30 lore raw

Re: [Openvpn-devel] [PATCH v5 08/14] client-connect: Add CC_RET_DEFERRED and cope with deferred client-connect

1x typo + 1x gram (in comments)

Quoted 17 lines · Arne Schwabe

From: Fabian Knittel <[email protected]...>

This patch moves the state, that was previously tracked within the multi_connection_established() function, into struct client_connect_state. The multi_connection_established() function can now be exited and re-entered as many times as necessary - without losing the client-connect handling state.

The patch also adds the new return value CC_RET_DEFERRED which indicates that the handler couldn't complete immediately, and needs to be called later. At that point multi_connection_established() will exit without indicating completion.

Each client-connect handler now has an (optional) additional call-back: The call-back for handling the deferred case. If the main call-back returns CC_RET_DEFERRED, the next call to the handler will be through the deferred call-back.

Quoted 4 lines

Patch V3: Use a static struct in multi_instance instead of using
malloc/free and use two states (deffered with and without

deffered -> deferred

Quoted 144 lines

result) instead of one to eliminate the counter that was only tested for > 0.

Patch V5: Use new states in context_auth instead of the extra state
that the patch series previously used.

---
  src/openvpn/multi.c   | 171 +++++++++++++++++++++++++++++++-----------
  src/openvpn/multi.h   |  15 +++-
  src/openvpn/openvpn.h |   9 +++
  3 files changed, 150 insertions(+), 45 deletions(-)
src/openvpn/multi.c +69 -36
index f9b8af80..ce73f8a1 100644
@@ -2218,32 +2218,51 @@ multi_client_connect_source_ccd(struct multi_context *m,
      return ret;
  }
 
-static inline bool
-cc_check_return(int *cc_succeeded_count,
-                enum client_connect_return ret)
+typedef enum client_connect_return (*multi_client_connect_handler)
+    (struct multi_context *m, struct multi_instance *mi,
+    unsigned int *option_types_found);
+
+struct client_connect_handlers
+{
+    multi_client_connect_handler main;
+    multi_client_connect_handler deferred;
+};
+
+static enum client_connect_return
+multi_client_connect_fail(struct multi_context *m, struct multi_instance *mi,
+                          unsigned int *option_types_found)
  {
-    if (ret == CC_RET_SUCCEEDED)
+    /* Called null call-back.  This should never happen. */
+    return CC_RET_FAILED;
+}
+
+static const struct client_connect_handlers client_connect_handlers[] = {
      {
-        (*cc_succeeded_count)++;
-        return true;
-    }
-    else if (ret == CC_RET_FAILED)
+        .main = multi_client_connect_source_ccd,
+        .deferred = multi_client_connect_fail
+    },
      {
-        return false;
-    }
-    else if (ret == CC_RET_SKIPPED)
+        .main = multi_client_connect_call_plugin_v1,
+        .deferred = multi_client_connect_fail
+    },
      {
-        return true;
-    }
-    else
+        .main = multi_client_connect_call_plugin_v2,
+        .deferred = multi_client_connect_fail
+    },
+    {
+        .main = multi_client_connect_call_script,
+        .deferred = multi_client_connect_fail
+    },
      {
-        ASSERT(0);
+        .main = multi_client_connect_mda,
+        .deferred = multi_client_connect_fail
+    },
+    {
+        .main = NULL,
+        .deferred = NULL
+                    /* End of list sentinel.  */
      }
-}
-
-typedef enum client_connect_return (*multi_client_connect_handler)
-    (struct multi_context *m, struct multi_instance *mi,
-     unsigned int *option_types_found);
+};
 
  /*
   * Called as soon as the SSL/TLS connection is authenticated.
@@ -2273,27 +2292,83 @@ multi_connection_established(struct multi_context *m, struct multi_instance *mi)
          return;
      }
 
-        multi_client_connect_handler handlers[] = {
-            multi_client_connect_source_ccd,
-            multi_client_connect_call_plugin_v1,
-            multi_client_connect_call_plugin_v2,
-            multi_client_connect_call_script,
-            multi_client_connect_mda,
-            NULL
-        };
-
-        unsigned int option_types_found = 0;
+    /* We are only called for the CAS_PENDING_x states, so we
+     * can ignore other states here */
+    bool from_deferred = (mi->context.c2.context_auth != CAS_PENDING);
 
-    int cc_succeeded = true; /* client connect script status */
-    int cc_succeeded_count = 0;
      enum client_connect_return ret;
 
-    multi_client_connect_early_setup(m, mi);
+    struct client_connect_defer_state *defer_state =
+        &(mi->client_connect_defer_state);
 
-    for (int i = 0; cc_succeeded && handlers[i]; i++)
+    /* We are called for the first time */
+    if (!from_deferred)
      {
-        ret = handlers[i](m, mi, &option_types_found);
-        cc_succeeded = cc_check_return(&cc_succeeded_count, ret);
+        defer_state->cur_handler_index = 0;
+        defer_state->option_types_found = 0;
+        /* Initially we have no handler that has returned a result */
+        mi->context.c2.context_auth = CAS_PENDING_DEFERRED;
+
+        multi_client_connect_early_setup(m, mi);
+    }
+
+    bool cc_succeeded = true;
+
+    while (cc_succeeded
+           && client_connect_handlers[defer_state->cur_handler_index]
+           .main != NULL)
+    {
+        multi_client_connect_handler handler;
+        if (from_deferred)
+        {
+            handler = client_connect_handlers
+                      [defer_state->cur_handler_index].deferred;
+            from_deferred = false;
+        }
+        else
+        {

src/openvpn/multi.c multi_connection_established(struct multi_context *m, struct multi_instance *mi) · line 2339

Quoted 10 lines
src/openvpn/multi.c +10 -0
+            handler = client_connect_handlers
+                      [defer_state->cur_handler_index].main;
+        }
+
+        ret = handler(m, mi, &(defer_state->option_types_found));
+        if (ret == CC_RET_SUCCEEDED)
+        {
+            /*
+             * Remember that we already had at least one handler
+             * returning a result should go to into deferred state

Grammar:
returning a result, so we should go into a deferred state

(My guess)

Quoted 162 lines
src/openvpn/multi.c +48 -8
+             */
+            mi->context.c2.context_auth = CAS_PENDING_DEFERRED_PARTIAL;
+        }
+        else if (ret == CC_RET_SKIPPED)
+        {
+            /*
+             * Move on with the next handler without modifying any
+             * other state
+             */
+        }
+        else if (ret == CC_RET_DEFERRED)
+        {
+            /*
+             * we already set client_connect_status to DEFERRED_RESULT or
+             * DEFERRED_NO_RESULT and increased index. We just return
+             * from the function as having client_connect_status
+             */
+            return;
+        }
+        else if (ret == CC_RET_FAILED)
+        {
+            /*
+             * One handler failed. We abort the chain and set the final
+             * result to failed
+             */
+            cc_succeeded = false;
+        }
+        else
+        {
+            ASSERT(0);
+        }
+        (defer_state->cur_handler_index)++;
      }
 
      /*
@@ -2305,18 +2380,26 @@ multi_connection_established(struct multi_context *m, struct multi_instance *mi)
          msg(D_MULTI_ERRORS, "MULTI: client has been rejected due to "
              "'disable' directive");
          cc_succeeded = false;
-        cc_succeeded_count = 0;
      }
 
      if (cc_succeeded)
      {
-        multi_client_connect_late_setup(m, mi, option_types_found);
+        multi_client_connect_late_setup(m, mi,
+                                        defer_state->option_types_found);
      }
      else
      {
-        /* set context-level authentication flag */
-        mi->context.c2.context_auth =
-            cc_succeeded_count ? CAS_PARTIAL : CAS_FAILED;
+        /* set context-level authentication flag to failed but remember
+         * if had a handler succeed (for cleanup) */
+        if (mi->context.c2.context_auth == CAS_PENDING_DEFERRED_PARTIAL)
+        {
+            mi->context.c2.context_auth = CAS_PARTIAL;
+        }
+        else
+        {
+            mi->context.c2.context_auth = CAS_FAILED;
+        }
+
      }
 
      /* increment number of current authenticated clients */
@@ -2604,7 +2687,7 @@ multi_process_post(struct multi_context *m, struct multi_instance *mi, const uns
          {
              /* connection is "established" when SSL/TLS key negotiation succeeds
               * and (if specified) auth user/pass succeeds */
-            if (mi->context.c2.context_auth == CAS_PENDING
+            if (is_cas_pending(mi->context.c2.context_auth)
                  && CONNECTION_ESTABLISHED(&mi->context))
              {
                  multi_connection_established(m, mi);
@@ -3559,7 +3642,7 @@ management_client_auth(void *arg,
          {
              if (auth)
              {
-                if (mi->context.c2.context_auth == CAS_PENDING)
+                if (is_cas_pending(mi->context.c2.context_auth))
                  {
                      set_cc_config(mi, cc_config);
                      cc_config_owned = false;
@@ -3571,7 +3654,7 @@ management_client_auth(void *arg,
                  {
                      msg(D_MULTI_LOW, "MULTI: connection rejected: %s, CLI:%s", reason, np(client_reason));
                  }
-                if (mi->context.c2.context_auth != CAS_PENDING)
+                if (!is_cas_pending(mi->context.c2.context_auth))
                  {
                      send_auth_failed(&mi->context, client_reason); /* mid-session reauth failed */
                      multi_schedule_context_wakeup(m, mi);
src/openvpn/multi.h +14 -1
index 1d30dcc6..11da0209 100644
@@ -62,6 +62,18 @@ struct deferred_signal_schedule_entry
      struct timeval wakeup;
  };
 
+/**
+ * Detached client connection state.  This is the state that is tracked while
+ * the client connect hooks are executed.
+ */
+struct client_connect_defer_state
+{
+    /* Index of currently executed handler.  */
+    int cur_handler_index;
+    /* Remember which option classes where processed for delayed option
+     * handling. */
+    unsigned int option_types_found;
+};
 
  /**
   * Server-mode state structure for one single VPN tunnel.
@@ -108,7 +120,7 @@ struct multi_instance {
 
      struct context context;     /**< The context structure storing state
                                   *   for this VPN tunnel. */
-
+    struct client_connect_defer_state client_connect_defer_state;
  #ifdef ENABLE_ASYNC_PUSH
      int inotify_watch; /* watch descriptor for acf */
  #endif
@@ -195,6 +207,7 @@ enum client_connect_return
  {
      CC_RET_FAILED,
      CC_RET_SUCCEEDED,
+    CC_RET_DEFERRED,
      CC_RET_SKIPPED
  };
 
src/openvpn/openvpn.h +9 -0
index 7c469b01..ccc7f118 100644
@@ -217,6 +217,8 @@ struct context_1
  enum client_connect_status {
      CAS_SUCCEEDED=0,
      CAS_PENDING,
+    CAS_PENDING_DEFERRED,
+    CAS_PENDING_DEFERRED_PARTIAL,   /**< at least handler succeeded, no result yet*/
      CAS_FAILED,
      CAS_PARTIAL,        /**< Variant of CAS_FAILED: at least one
                           * client-connect script/plugin succeeded
@@ -225,6 +227,13 @@ enum client_connect_status {
                           */
  };
 
+static inline bool
+is_cas_pending(enum client_connect_status cas)
+{
+    return cas == CAS_PENDING || cas == CAS_PENDING_DEFERRED
+           || cas == CAS_PENDING_DEFERRED_PARTIAL;
+}
+
  /**
   * Level 2 %context containing state that is reset on both \c SIGHUP and
   * \c SIGUSR1 restarts.
#31 lore raw

Re: [Openvpn-devel] [PATCH v5 08/14] client-connect: Add CC_RET_DEFERRED and cope with deferred client-connect

In reply to Arne Schwabe

Hi,

Quoted 6 lines · Arne Schwabe

From: Fabian Knittel <[email protected]...>

This patch moves the state, that was previously tracked within the multi_connection_established() function, into struct client_connect_state. The multi_connection_established() function can now be exited and re-entered as many times as necessary - without losing the client-connect handling state.

[..]

Server side test succeeded...

23...
Test sets succeeded: none.
Test sets failed: 1 1a 1b 1d 2 2a 2b 2c 2d 3 4 5 6 8 8a 9.
24...
Test sets succeeded: 1 1a 1b 1c 1d 1e 2 2a 2b 2c 2d 2e 3 4 4a 5 6 8 8a 9. Test sets failed: none.
master...
Test sets succeeded: 1 1a 1b 1c 1d 1e 2 2a 2b 2c 2d 2e 3 4 5 6 7 7a 8 8a 9 2f 4b. Test sets failed: none.

Didn't look too closely at the patch yet.

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]...
Acked-by on v5 8/14
#32 lore raw

Re: [Openvpn-devel] [PATCH v5 08/14] client-connect: Add CC_RET_DEFERRED and cope with deferred client-connect

In reply to Arne Schwabe

Hi,

Quoted 142 lines · Arne Schwabe

From: Fabian Knittel <[email protected]...>

This patch moves the state, that was previously tracked within the multi_connection_established() function, into struct client_connect_state. The multi_connection_established() function can now be exited and re-entered as many times as necessary - without losing the client-connect handling state.

The patch also adds the new return value CC_RET_DEFERRED which indicates that the handler couldn't complete immediately, and needs to be called later. At that point multi_connection_established() will exit without indicating completion.

Each client-connect handler now has an (optional) additional call-back: The call-back for handling the deferred case. If the main call-back returns CC_RET_DEFERRED, the next call to the handler will be through the deferred call-back.

Patch V3: Use a static struct in multi_instance instead of using
malloc/free and use two states (deffered with and without result) instead of one to eliminate the counter that was only tested for > 0.

Patch V5: Use new states in context_auth instead of the extra state
that the patch series previously used.

---
 src/openvpn/multi.c   | 171 +++++++++++++++++++++++++++++++-----------
 src/openvpn/multi.h   |  15 +++-
 src/openvpn/openvpn.h |   9 +++
 3 files changed, 150 insertions(+), 45 deletions(-)
src/openvpn/multi.c +46 -36
index f9b8af80..ce73f8a1 100644
@@ -2218,32 +2218,51 @@ multi_client_connect_source_ccd(struct multi_context *m,
     return ret;
 }
 
-static inline bool
-cc_check_return(int *cc_succeeded_count,
-                enum client_connect_return ret)
+typedef enum client_connect_return (*multi_client_connect_handler)
+    (struct multi_context *m, struct multi_instance *mi,
+    unsigned int *option_types_found);
+
+struct client_connect_handlers
+{
+    multi_client_connect_handler main;
+    multi_client_connect_handler deferred;
+};
+
+static enum client_connect_return
+multi_client_connect_fail(struct multi_context *m, struct multi_instance *mi,
+                          unsigned int *option_types_found)
 {
-    if (ret == CC_RET_SUCCEEDED)
+    /* Called null call-back.  This should never happen. */
+    return CC_RET_FAILED;
+}
+
+static const struct client_connect_handlers client_connect_handlers[] = {
     {
-        (*cc_succeeded_count)++;
-        return true;
-    }
-    else if (ret == CC_RET_FAILED)
+        .main = multi_client_connect_source_ccd,
+        .deferred = multi_client_connect_fail
+    },
     {
-        return false;
-    }
-    else if (ret == CC_RET_SKIPPED)
+        .main = multi_client_connect_call_plugin_v1,
+        .deferred = multi_client_connect_fail
+    },
     {
-        return true;
-    }
-    else
+        .main = multi_client_connect_call_plugin_v2,
+        .deferred = multi_client_connect_fail
+    },
+    {
+        .main = multi_client_connect_call_script,
+        .deferred = multi_client_connect_fail
+    },
     {
-        ASSERT(0);
+        .main = multi_client_connect_mda,
+        .deferred = multi_client_connect_fail
+    },
+    {
+        .main = NULL,
+        .deferred = NULL
+                    /* End of list sentinel.  */
     }
-}
-
-typedef enum client_connect_return (*multi_client_connect_handler)
-    (struct multi_context *m, struct multi_instance *mi,
-     unsigned int *option_types_found);
+};
 
 /*
  * Called as soon as the SSL/TLS connection is authenticated.
@@ -2273,27 +2292,83 @@ multi_connection_established(struct multi_context *m, struct multi_instance *mi)
         return;
     }
 
-        multi_client_connect_handler handlers[] = {
-            multi_client_connect_source_ccd,
-            multi_client_connect_call_plugin_v1,
-            multi_client_connect_call_plugin_v2,
-            multi_client_connect_call_script,
-            multi_client_connect_mda,
-            NULL
-        };
-
-        unsigned int option_types_found = 0;
+    /* We are only called for the CAS_PENDING_x states, so we
+     * can ignore other states here */
+    bool from_deferred = (mi->context.c2.context_auth != CAS_PENDING);
 
-    int cc_succeeded = true; /* client connect script status */
-    int cc_succeeded_count = 0;
     enum client_connect_return ret;
 
-    multi_client_connect_early_setup(m, mi);
+    struct client_connect_defer_state *defer_state =
+        &(mi->client_connect_defer_state);
 
-    for (int i = 0; cc_succeeded && handlers[i]; i++)
+    /* We are called for the first time */
+    if (!from_deferred)
     {
-        ret = handlers[i](m, mi, &option_types_found);
-        cc_succeeded = cc_check_return(&cc_succeeded_count, ret);

src/openvpn/multi.c multi_connection_established(struct multi_context *m, struct multi_instance *mi) · line 2316

Quoted 10 lines
src/openvpn/multi.c +10 -0
+        defer_state->cur_handler_index = 0;
+        defer_state->option_types_found = 0;
+        /* Initially we have no handler that has returned a result */
+        mi->context.c2.context_auth = CAS_PENDING_DEFERRED;
+
+        multi_client_connect_early_setup(m, mi);
+    }
+
+    bool cc_succeeded = true;
+

can we please add a variable for the index and make all these long lines saner? Now they are really ugly:

int idx = defer_state->cur_handler_index;
while (cc_succeeded

       && client_connect_handlers[idx].main != NULL)

and also the lines in the loop itself.

Quoted 45 lines
src/openvpn/multi.c +45 -0
+    while (cc_succeeded
+           && client_connect_handlers[defer_state->cur_handler_index]
+           .main != NULL)
+    {
+        multi_client_connect_handler handler;
+        if (from_deferred)
+        {
+            handler = client_connect_handlers
+                      [defer_state->cur_handler_index].deferred;
+            from_deferred = false;
+        }
+        else
+        {
+            handler = client_connect_handlers
+                      [defer_state->cur_handler_index].main;
+        }
+
+        ret = handler(m, mi, &(defer_state->option_types_found));
+        if (ret == CC_RET_SUCCEEDED)
+        {
+            /*
+             * Remember that we already had at least one handler
+             * returning a result should go to into deferred state
+             */
+            mi->context.c2.context_auth = CAS_PENDING_DEFERRED_PARTIAL;
+        }
+        else if (ret == CC_RET_SKIPPED)
+        {
+            /*
+             * Move on with the next handler without modifying any
+             * other state
+             */
+        }
+        else if (ret == CC_RET_DEFERRED)
+        {
+            /*
+             * we already set client_connect_status to DEFERRED_RESULT or
+             * DEFERRED_NO_RESULT and increased index. We just return
+             * from the function as having client_connect_status
+             */
+            return;
+        }
+        else if (ret == CC_RET_FAILED)
+        {
+            /*

src/openvpn/multi.c multi_connection_established(struct multi_context *m, struct multi_instance *mi) · line 2371

Quoted 10 lines
src/openvpn/multi.c +10 -0
+             * One handler failed. We abort the chain and set the final
+             * result to failed
+             */
+            cc_succeeded = false;
+        }
+        else
+        {
+            ASSERT(0);
+        }
+        (defer_state->cur_handler_index)++;

what are the parenthesis for?

Quoted 120 lines
src/openvpn/multi.c +16 -8
     }
 
     /*
@@ -2305,18 +2380,26 @@ multi_connection_established(struct multi_context *m, struct multi_instance *mi)
         msg(D_MULTI_ERRORS, "MULTI: client has been rejected due to "
             "'disable' directive");
         cc_succeeded = false;
-        cc_succeeded_count = 0;
     }
 
     if (cc_succeeded)
     {
-        multi_client_connect_late_setup(m, mi, option_types_found);
+        multi_client_connect_late_setup(m, mi,
+                                        defer_state->option_types_found);
     }
     else
     {
-        /* set context-level authentication flag */
-        mi->context.c2.context_auth =
-            cc_succeeded_count ? CAS_PARTIAL : CAS_FAILED;
+        /* set context-level authentication flag to failed but remember
+         * if had a handler succeed (for cleanup) */
+        if (mi->context.c2.context_auth == CAS_PENDING_DEFERRED_PARTIAL)
+        {
+            mi->context.c2.context_auth = CAS_PARTIAL;
+        }
+        else
+        {
+            mi->context.c2.context_auth = CAS_FAILED;
+        }
+
     }
 
     /* increment number of current authenticated clients */
@@ -2604,7 +2687,7 @@ multi_process_post(struct multi_context *m, struct multi_instance *mi, const uns
         {
             /* connection is "established" when SSL/TLS key negotiation succeeds
              * and (if specified) auth user/pass succeeds */
-            if (mi->context.c2.context_auth == CAS_PENDING
+            if (is_cas_pending(mi->context.c2.context_auth)
                 && CONNECTION_ESTABLISHED(&mi->context))
             {
                 multi_connection_established(m, mi);
@@ -3559,7 +3642,7 @@ management_client_auth(void *arg,
         {
             if (auth)
             {
-                if (mi->context.c2.context_auth == CAS_PENDING)
+                if (is_cas_pending(mi->context.c2.context_auth))
                 {
                     set_cc_config(mi, cc_config);
                     cc_config_owned = false;
@@ -3571,7 +3654,7 @@ management_client_auth(void *arg,
                 {
                     msg(D_MULTI_LOW, "MULTI: connection rejected: %s, CLI:%s", reason, np(client_reason));
                 }
-                if (mi->context.c2.context_auth != CAS_PENDING)
+                if (!is_cas_pending(mi->context.c2.context_auth))
                 {
                     send_auth_failed(&mi->context, client_reason); /* mid-session reauth failed */
                     multi_schedule_context_wakeup(m, mi);
src/openvpn/multi.h +14 -1
index 1d30dcc6..11da0209 100644
@@ -62,6 +62,18 @@ struct deferred_signal_schedule_entry
     struct timeval wakeup;
 };
 
+/**
+ * Detached client connection state.  This is the state that is tracked while
+ * the client connect hooks are executed.
+ */
+struct client_connect_defer_state
+{
+    /* Index of currently executed handler.  */
+    int cur_handler_index;
+    /* Remember which option classes where processed for delayed option
+     * handling. */
+    unsigned int option_types_found;
+};
 
 /**
  * Server-mode state structure for one single VPN tunnel.
@@ -108,7 +120,7 @@ struct multi_instance {
 
     struct context context;     /**< The context structure storing state
                                  *   for this VPN tunnel. */
-
+    struct client_connect_defer_state client_connect_defer_state;
 #ifdef ENABLE_ASYNC_PUSH
     int inotify_watch; /* watch descriptor for acf */
 #endif
@@ -195,6 +207,7 @@ enum client_connect_return
 {
     CC_RET_FAILED,
     CC_RET_SUCCEEDED,
+    CC_RET_DEFERRED,
     CC_RET_SKIPPED
 };
 
src/openvpn/openvpn.h +2 -0
index 7c469b01..ccc7f118 100644
@@ -217,6 +217,8 @@ struct context_1
 enum client_connect_status {
     CAS_SUCCEEDED=0,
     CAS_PENDING,
+    CAS_PENDING_DEFERRED,
+    CAS_PENDING_DEFERRED_PARTIAL,   /**< at least handler succeeded, no result yet*/
     CAS_FAILED,
     CAS_PARTIAL,        /**< Variant of CAS_FAILED: at least one
                          * client-connect script/plugin succeeded
@@ -225,6 +227,13 @@ enum client_connect_status {
                          */
 };
 

src/openvpn/openvpn.h enum client_connect_status { · line 239

Quoted 10 lines
src/openvpn/openvpn.h +7 -0
+static inline bool
+is_cas_pending(enum client_connect_status cas)
+{
+    return cas == CAS_PENDING || cas == CAS_PENDING_DEFERRED
+           || cas == CAS_PENDING_DEFERRED_PARTIAL;
+}
+
 /**
  * Level 2 %context containing state that is reset on both \c SIGHUP and
  * \c SIGUSR1 restarts.

Other than my stylistic comments the patch looks good and does what it says. IMHO the code is not the prettiest ever, but it gets difficult to suggest something different without an overhaul of the existing code.

Signature · 1 line
Antonio Quartulli
#33 lore raw

Re: [Openvpn-devel] [PATCH v5 08/14] client-connect: Add CC_RET_DEFERRED and cope with deferred client-connect

Am 14.07.20 um 14:32 schrieb Antonio Quartulli:

Quoted 8 lines

can we please add a variable for the index and make all these long lines saner? Now they are really ugly:

int idx = defer_state->cur_handler_index;
while (cc_succeeded

       && client_connect_handlers[idx].main != NULL)

and also the lines in the loop itself.

we can but we need to do add

defer_state->cur_handler_index = idx;

in that case so the idx is saved for the next time the function is called from a deferred context. Or you make it int* idx and do client_connect_handlers[*idx]

Arne

#34 lore raw

PATCH v5 9/14 client-connect: Add deferred support to the client-connect script handler

In reply to Arne Schwabe

From: Fabian Knittel <[email protected]...>

This patch introduces the concept of a return value file for the client-connect handlers. (This is very similar to the auth value file used during deferred authentication.) The file name is stored in the client_connect_state struct.

In addition, the patch also allows the storage of the client config file name in struct client_connect_state.

Both changes are used by the client-connect script handler to support deferred client-connection handling. The deferred return value file (deferred_ret_file) is passed to the actual script via the environment. If the script succeeds and writes the value for deferral into the deferred_ret_file, the handler knows to indicate deferral. Later on, the deferred handler checks whether the value of the deferred_ret_file has been updated to success or failure.

---
 src/openvpn/multi.c | 230 +++++++++++++++++++++++++++++++++++++++++---
 src/openvpn/multi.h |  12 +++
 2 files changed, 227 insertions(+), 15 deletions(-)
src/openvpn/multi.c +215 -15
index ce73f8a1..271d09d8 100644
@@ -1843,6 +1843,168 @@ multi_client_set_protocol_options(struct context *c)
     }
 }
 
+/**
+ * Delete the temporary file for the return value of client connect
+ * It also removes it from it from client_connect_defer_state and
+ * environment
+ */
+static void
+ccs_delete_deferred_ret_file(struct multi_instance *mi)
+{
+    struct client_connect_defer_state *ccs = &(mi->client_connect_defer_state);
+    if (ccs->deferred_ret_file)
+    {
+        setenv_del(mi->context.c2.es, "client_connect_deferred_file");
+        if (!platform_unlink(ccs->deferred_ret_file))
+        {
+            msg(D_MULTI_ERRORS, "MULTI: problem deleting temporary file: %s",
+                ccs->deferred_ret_file);
+        }
+        free(ccs->deferred_ret_file);
+        ccs->deferred_ret_file = NULL;
+    }
+}
+
+/**
+ * Create a temporary file for the return value of client connect
+ * and puts it into the client_connect_defer_state and environment
+ * as "client_connect_deferred_file"
+ *
+ * @return boolean value if creation was successfull
+ */
+static bool
+ccs_gen_deferred_ret_file(struct multi_instance *mi)
+{
+    struct client_connect_defer_state *ccs = &(mi->client_connect_defer_state);
+    struct gc_arena gc = gc_new();
+    const char *fn;
+
+    if (ccs->deferred_ret_file)
+    {
+        ccs_delete_deferred_ret_file(mi);
+    }
+
+    fn = platform_create_temp_file(mi->context.options.tmp_dir, "ccr", &gc);
+    if (!fn)
+    {
+        gc_free(&gc);
+        return false;
+    }
+    ccs->deferred_ret_file = string_alloc(fn, NULL);
+
+    setenv_str(mi->context.c2.es, "client_connect_deferred_file",
+               ccs->deferred_ret_file);
+
+    gc_free(&gc);
+    return true;
+}
+
+/**
+ * Tests whether the deferred return value file exists and returns the
+ * contained return value.
+ *
+ * @return CC_RET_SKIPPED if the file does not exist or is empty.
+ *         CC_RET_DEFERRED, CC_RET_SUCCEEDED or CC_RET_FAILED depending on
+ *         the value stored in the file.
+ */
+static enum client_connect_return
+ccs_test_deferred_ret_file(struct multi_instance *mi)
+{
+    struct client_connect_defer_state *ccs = &(mi->client_connect_defer_state);
+    enum client_connect_return ret = CC_RET_SKIPPED;
+    FILE *fp = fopen(ccs->deferred_ret_file, "r");
+    if (fp)
+    {
+        const int c = fgetc(fp);
+        switch (c)
+        {
+            case '0':
+                ret = CC_RET_FAILED;
+                break;
+
+            case '1':
+                ret = CC_RET_SUCCEEDED;
+                break;
+
+            case '2':
+                ret = CC_RET_DEFERRED;
+                break;
+
+            case EOF:
+                if (feof(fp))
+                {
+                    ret = CC_RET_SKIPPED;
+                    break;
+                }
+
+            /* Not EOF, but other error fall through to error state */
+            default:
+                /* We received an unknown/unexpected value.  Assume failure. */
+                msg(M_WARN, "WARNING: Unknown/unexcepted value in deferred"
+                    "client-connect resultfile");
+                ret = CC_RET_FAILED;
+        }
+        fclose(fp);
+    }
+    return ret;
+}
+
+/**
+ * Deletes the temporary file for the config directives of the  client connect
+ * script and removes it into the client_connect_defer_state and environment
+ *
+ */
+static void
+ccs_delete_config_file(struct multi_instance *mi)
+{
+    struct client_connect_defer_state *ccs = &(mi->client_connect_defer_state);
+    if (ccs->config_file)
+    {
+        setenv_del(mi->context.c2.es, "client_connect_config_file");
+        if (!platform_unlink(ccs->config_file))
+        {
+            msg(D_MULTI_ERRORS, "MULTI: problem deleting temporary file: %s",
+                ccs->config_file);
+        }
+        free(ccs->config_file);
+        ccs->config_file = NULL;
+    }
+}
+
+/**
+ * Create a temporary file for the config directives of the  client connect
+ * script and puts it into the client_connect_defer_state and environment
+ * as "client_connect_config_file"
+ *
+ * @return boolean value if creation was successfull
+ */
+static bool
+ccs_gen_config_file(struct multi_instance *mi)
+{
+    struct client_connect_defer_state *ccs = &(mi->client_connect_defer_state);
+    struct gc_arena gc = gc_new();
+    const char *fn;
+
+    if (ccs->config_file)
+    {
+        ccs_delete_config_file(mi);
+    }
+
+    fn = platform_create_temp_file(mi->context.options.tmp_dir, "cc", &gc);
+    if (!fn)
+    {
+        gc_free(&gc);
+        return false;
+    }
+    ccs->config_file = string_alloc(fn, NULL);
+
+    setenv_str(mi->context.c2.es, "client_connect_config_file",
+               ccs->config_file);
+
+    gc_free(&gc);
+    return true;
+}
+
 static enum client_connect_return
 multi_client_connect_call_plugin_v1(struct multi_context *m,
                                     struct multi_instance *mi,
@@ -1933,8 +2095,6 @@ multi_client_connect_call_plugin_v2(struct multi_context *m,
     return ret;
 }
 
-
-
 /**
  * Runs the --client-connect script if one is defined.
  */
@@ -1948,48 +2108,88 @@ multi_client_connect_call_script(struct multi_context *m,
     ASSERT(mi);
 
     enum client_connect_return ret = CC_RET_SKIPPED;
+    struct client_connect_defer_state *ccs = &(mi->client_connect_defer_state);
 
     if (mi->context.options.client_connect_script)
     {
         struct argv argv = argv_new();
         struct gc_arena gc = gc_new();
-        const char *dc_file = NULL;
 
         setenv_str(mi->context.c2.es, "script_type", "client-connect");
 
-        dc_file = platform_create_temp_file(mi->context.options.tmp_dir,
-                                            "cc", &gc);
-        if (!dc_file)
+        if (!ccs_gen_config_file(mi)
+            || !ccs_gen_deferred_ret_file(mi))
         {
             ret = CC_RET_FAILED;
             goto cleanup;
         }
 
         argv_parse_cmd(&argv, mi->context.options.client_connect_script);
-        argv_printf_cat(&argv, "%s", dc_file);
+        argv_printf_cat(&argv, "%s", ccs->config_file);
 
         if (openvpn_run_script(&argv, mi->context.c2.es, 0, "--client-connect"))
         {
-            multi_client_connect_post(m, mi, dc_file, option_types_found);
-            ret = CC_RET_SUCCEEDED;
+            if (ccs_test_deferred_ret_file(mi) == CC_RET_DEFERRED)
+            {
+                ret = CC_RET_DEFERRED;
+            }
+            else
+            {
+                multi_client_connect_post(m, mi, ccs->config_file,
+                                          option_types_found);
+                ret = CC_RET_SUCCEEDED;
+            }
         }
         else
         {
             ret = CC_RET_FAILED;
         }
-
-        if (!platform_unlink(dc_file))
+cleanup:
+        if (ret != CC_RET_DEFERRED)
         {
-            msg(D_MULTI_ERRORS, "MULTI: problem deleting temporary file: %s",
-                dc_file);
+            ccs_delete_config_file(mi);
+            ccs_delete_deferred_ret_file(mi);
         }
-cleanup:
         argv_free(&argv);
         gc_free(&gc);
     }
     return ret;
 }
 
+static enum client_connect_return
+multi_client_connect_script_deferred(struct multi_context *m,
+                                     struct multi_instance *mi,
+                                     unsigned int *option_types_found)
+{
+    ASSERT(mi);
+    ASSERT(option_types_found);
+    struct client_connect_defer_state *ccs = &(mi->client_connect_defer_state);
+    enum client_connect_return ret = CC_RET_SKIPPED;
+
+    ret = ccs_test_deferred_ret_file(mi);
+
+    if (ret == CC_RET_SKIPPED)
+    {
+        /*
+         * Skipped and deferred are equivalent in this context.
+         * skipped means that the called program has not yet
+         * written a return status implicitly needing more time
+         * while deferred is the explicit notifcation that it
+         * needs more time
+         */
+        ret = CC_RET_DEFERRED;
+    }
+
+    if (ret != CC_RET_DEFERRED)
+    {
+        ccs_delete_deferred_ret_file(mi);
+        multi_client_connect_post(m, mi, ccs->config_file,
+                                  option_types_found);
+        ccs_delete_config_file(mi);
+    }
+    return ret;
+}
+
 /**
  * Generates the data channel keys
  */
@@ -2251,7 +2451,7 @@ static const struct client_connect_handlers client_connect_handlers[] = {
     },
     {
         .main = multi_client_connect_call_script,
-        .deferred = multi_client_connect_fail
+        .deferred = multi_client_connect_script_deferred
     },
     {
         .main = multi_client_connect_mda,
src/openvpn/multi.h +12 -0
index 11da0209..3ebf6b9f 100644
@@ -73,6 +73,18 @@ struct client_connect_defer_state
     /* Remember which option classes where processed for delayed option
      * handling. */
     unsigned int option_types_found;
+
+    /**
+     * The temporrary file name that contains the return status of the
+     * client-connect script if it exits with defer as status
+     */
+    char *deferred_ret_file;
+
+    /**
+     * The temporary file name that contains the config directives
+     * returned by the client-connect script
+     */
+    char *config_file;
 };
 
 /**
Signature · 1 line
2.26.2
#35 lore raw

Re: [Openvpn-devel] [PATCH v5 09/14] client-connect: Add deferred support to the client-connect script handler

5x typo 2x gram

Quoted 28 lines · Arne Schwabe

From: Fabian Knittel <[email protected]...>

This patch introduces the concept of a return value file for the client-connect handlers. (This is very similar to the auth value file used during deferred authentication.) The file name is stored in the client_connect_state struct.

In addition, the patch also allows the storage of the client config file name in struct client_connect_state.

Both changes are used by the client-connect script handler to support deferred client-connection handling. The deferred return value file (deferred_ret_file) is passed to the actual script via the environment. If the script succeeds and writes the value for deferral into the deferred_ret_file, the handler knows to indicate deferral. Later on, the deferred handler checks whether the value of the deferred_ret_file has been updated to success or failure.

---
  src/openvpn/multi.c | 230 +++++++++++++++++++++++++++++++++++++++++---
  src/openvpn/multi.h |  12 +++
  2 files changed, 227 insertions(+), 15 deletions(-)
src/openvpn/multi.c +0 -0
index ce73f8a1..271d09d8 100644

src/openvpn/multi.c multi_client_set_protocol_options(struct context *c) · line 1848

Quoted 6 lines
src/openvpn/multi.c +3 -0
@@ -1843,6 +1843,168 @@ multi_client_set_protocol_options(struct context *c)
      }
  }
 
+/**
+ * Delete the temporary file for the return value of client connect
+ * It also removes it from it from client_connect_defer_state and

it from it from - 2x "it from"

Quoted 15 lines
src/openvpn/multi.c +15 -0
+ * environment
+ */
+static void
+ccs_delete_deferred_ret_file(struct multi_instance *mi)
+{
+    struct client_connect_defer_state *ccs = &(mi->client_connect_defer_state);
+    if (ccs->deferred_ret_file)
+    {
+        setenv_del(mi->context.c2.es, "client_connect_deferred_file");
+        if (!platform_unlink(ccs->deferred_ret_file))
+        {
+            msg(D_MULTI_ERRORS, "MULTI: problem deleting temporary file: %s",
+                ccs->deferred_ret_file);
+        }
+        free(ccs->deferred_ret_file);

src/openvpn/multi.c multi_client_set_protocol_options(struct context *c) · line 1873

Quoted 10 lines
src/openvpn/multi.c +10 -0
+        ccs->deferred_ret_file = NULL;
+    }
+}
+
+/**
+ * Create a temporary file for the return value of client connect
+ * and puts it into the client_connect_defer_state and environment
+ * as "client_connect_deferred_file"
+ *
+ * @return boolean value if creation was successfull

successfull -> successful

Quoted 57 lines
src/openvpn/multi.c +57 -0
+ */
+static bool
+ccs_gen_deferred_ret_file(struct multi_instance *mi)
+{
+    struct client_connect_defer_state *ccs = &(mi->client_connect_defer_state);
+    struct gc_arena gc = gc_new();
+    const char *fn;
+
+    if (ccs->deferred_ret_file)
+    {
+        ccs_delete_deferred_ret_file(mi);
+    }
+
+    fn = platform_create_temp_file(mi->context.options.tmp_dir, "ccr", &gc);
+    if (!fn)
+    {
+        gc_free(&gc);
+        return false;
+    }
+    ccs->deferred_ret_file = string_alloc(fn, NULL);
+
+    setenv_str(mi->context.c2.es, "client_connect_deferred_file",
+               ccs->deferred_ret_file);
+
+    gc_free(&gc);
+    return true;
+}
+
+/**
+ * Tests whether the deferred return value file exists and returns the
+ * contained return value.
+ *
+ * @return CC_RET_SKIPPED if the file does not exist or is empty.
+ *         CC_RET_DEFERRED, CC_RET_SUCCEEDED or CC_RET_FAILED depending on
+ *         the value stored in the file.
+ */
+static enum client_connect_return
+ccs_test_deferred_ret_file(struct multi_instance *mi)
+{
+    struct client_connect_defer_state *ccs = &(mi->client_connect_defer_state);
+    enum client_connect_return ret = CC_RET_SKIPPED;
+    FILE *fp = fopen(ccs->deferred_ret_file, "r");
+    if (fp)
+    {
+        const int c = fgetc(fp);
+        switch (c)
+        {
+            case '0':
+                ret = CC_RET_FAILED;
+                break;
+
+            case '1':
+                ret = CC_RET_SUCCEEDED;
+                break;
+
+            case '2':
+                ret = CC_RET_DEFERRED;

src/openvpn/multi.c multi_client_set_protocol_options(struct context *c) · line 1940

Quoted 10 lines
src/openvpn/multi.c +10 -0
+                break;
+
+            case EOF:
+                if (feof(fp))
+                {
+                    ret = CC_RET_SKIPPED;
+                    break;
+                }
+
+            /* Not EOF, but other error fall through to error state */

Not EOF but other error, fall through to error state

(Commas are important)

src/openvpn/multi.c multi_client_set_protocol_options(struct context *c) · line 1943

Quoted 3 lines
src/openvpn/multi.c +3 -0
+            default:
+                /* We received an unknown/unexpected value.  Assume failure. */
+                msg(M_WARN, "WARNING: Unknown/unexcepted value in deferred"

unexcepted -> either: unexpected or unaccepted

Quoted 26 lines
src/openvpn/multi.c +26 -0
+                    "client-connect resultfile");
+                ret = CC_RET_FAILED;
+        }
+        fclose(fp);
+    }
+    return ret;
+}
+
+/**
+ * Deletes the temporary file for the config directives of the  client connect
+ * script and removes it into the client_connect_defer_state and environment
+ *
+ */
+static void
+ccs_delete_config_file(struct multi_instance *mi)
+{
+    struct client_connect_defer_state *ccs = &(mi->client_connect_defer_state);
+    if (ccs->config_file)
+    {
+        setenv_del(mi->context.c2.es, "client_connect_config_file");
+        if (!platform_unlink(ccs->config_file))
+        {
+            msg(D_MULTI_ERRORS, "MULTI: problem deleting temporary file: %s",
+                ccs->config_file);
+        }
+        free(ccs->config_file);

src/openvpn/multi.c multi_client_set_protocol_options(struct context *c) · line 1979

Quoted 10 lines
src/openvpn/multi.c +10 -0
+        ccs->config_file = NULL;
+    }
+}
+
+/**
+ * Create a temporary file for the config directives of the  client connect
+ * script and puts it into the client_connect_defer_state and environment
+ * as "client_connect_config_file"
+ *
+ * @return boolean value if creation was successfull

successfull -> successful

Quoted 113 lines
src/openvpn/multi.c +55 -14
+ */
+static bool
+ccs_gen_config_file(struct multi_instance *mi)
+{
+    struct client_connect_defer_state *ccs = &(mi->client_connect_defer_state);
+    struct gc_arena gc = gc_new();
+    const char *fn;
+
+    if (ccs->config_file)
+    {
+        ccs_delete_config_file(mi);
+    }
+
+    fn = platform_create_temp_file(mi->context.options.tmp_dir, "cc", &gc);
+    if (!fn)
+    {
+        gc_free(&gc);
+        return false;
+    }
+    ccs->config_file = string_alloc(fn, NULL);
+
+    setenv_str(mi->context.c2.es, "client_connect_config_file",
+               ccs->config_file);
+
+    gc_free(&gc);
+    return true;
+}
+
  static enum client_connect_return
  multi_client_connect_call_plugin_v1(struct multi_context *m,
                                      struct multi_instance *mi,
@@ -1933,8 +2095,6 @@ multi_client_connect_call_plugin_v2(struct multi_context *m,
      return ret;
  }
 
-
-
  /**
   * Runs the --client-connect script if one is defined.
   */
@@ -1948,48 +2108,88 @@ multi_client_connect_call_script(struct multi_context *m,
      ASSERT(mi);
 
      enum client_connect_return ret = CC_RET_SKIPPED;
+    struct client_connect_defer_state *ccs = &(mi->client_connect_defer_state);
 
      if (mi->context.options.client_connect_script)
      {
          struct argv argv = argv_new();
          struct gc_arena gc = gc_new();
-        const char *dc_file = NULL;
 
          setenv_str(mi->context.c2.es, "script_type", "client-connect");
 
-        dc_file = platform_create_temp_file(mi->context.options.tmp_dir,
-                                            "cc", &gc);
-        if (!dc_file)
+        if (!ccs_gen_config_file(mi)
+            || !ccs_gen_deferred_ret_file(mi))
          {
              ret = CC_RET_FAILED;
              goto cleanup;
          }
 
          argv_parse_cmd(&argv, mi->context.options.client_connect_script);
-        argv_printf_cat(&argv, "%s", dc_file);
+        argv_printf_cat(&argv, "%s", ccs->config_file);
 
          if (openvpn_run_script(&argv, mi->context.c2.es, 0, "--client-connect"))
          {
-            multi_client_connect_post(m, mi, dc_file, option_types_found);
-            ret = CC_RET_SUCCEEDED;
+            if (ccs_test_deferred_ret_file(mi) == CC_RET_DEFERRED)
+            {
+                ret = CC_RET_DEFERRED;
+            }
+            else
+            {
+                multi_client_connect_post(m, mi, ccs->config_file,
+                                          option_types_found);
+                ret = CC_RET_SUCCEEDED;
+            }
          }
          else
          {
              ret = CC_RET_FAILED;
          }
-
-        if (!platform_unlink(dc_file))
+cleanup:
+        if (ret != CC_RET_DEFERRED)
          {
-            msg(D_MULTI_ERRORS, "MULTI: problem deleting temporary file: %s",
-                dc_file);
+            ccs_delete_config_file(mi);
+            ccs_delete_deferred_ret_file(mi);
          }
-cleanup:
          argv_free(&argv);
          gc_free(&gc);
      }
      return ret;
  }
 
+static enum client_connect_return
+multi_client_connect_script_deferred(struct multi_context *m,
+                                     struct multi_instance *mi,
+                                     unsigned int *option_types_found)
+{
+    ASSERT(mi);
+    ASSERT(option_types_found);
+    struct client_connect_defer_state *ccs = &(mi->client_connect_defer_state);
+    enum client_connect_return ret = CC_RET_SKIPPED;

src/openvpn/multi.c multi_client_connect_call_script(struct multi_context *m, · line 2177

Quoted 10 lines
src/openvpn/multi.c +10 -0
+
+    ret = ccs_test_deferred_ret_file(mi);
+
+    if (ret == CC_RET_SKIPPED)
+    {
+        /*
+         * Skipped and deferred are equivalent in this context.
+         * skipped means that the called program has not yet
+         * written a return status implicitly needing more time
+         * while deferred is the explicit notifcation that it

notifcation -> notification

Quoted 32 lines
src/openvpn/multi.c +16 -1
+         * needs more time
+         */
+        ret = CC_RET_DEFERRED;
+    }
+
+    if (ret != CC_RET_DEFERRED)
+    {
+        ccs_delete_deferred_ret_file(mi);
+        multi_client_connect_post(m, mi, ccs->config_file,
+                                  option_types_found);
+        ccs_delete_config_file(mi);
+    }
+    return ret;
+}
+
  /**
   * Generates the data channel keys
   */
@@ -2251,7 +2451,7 @@ static const struct client_connect_handlers client_connect_handlers[] = {
      },
      {
          .main = multi_client_connect_call_script,
-        .deferred = multi_client_connect_fail
+        .deferred = multi_client_connect_script_deferred
      },
      {
          .main = multi_client_connect_mda,
src/openvpn/multi.h +0 -0
index 11da0209..3ebf6b9f 100644

src/openvpn/multi.h struct client_connect_defer_state · line 78

Quoted 6 lines
src/openvpn/multi.h +3 -0
@@ -73,6 +73,18 @@ struct client_connect_defer_state
      /* Remember which option classes where processed for delayed option
       * handling. */
      unsigned int option_types_found;
+
+    /**
+     * The temporrary file name that contains the return status of the

temporrary -> temporary

Quoted 13 lines
src/openvpn/multi.h +9 -0
+     * client-connect script if it exits with defer as status
+     */
+    char *deferred_ret_file;
+
+    /**
+     * The temporary file name that contains the config directives
+     * returned by the client-connect script
+     */
+    char *config_file;
  };
 
  /**
#36 lore raw

Re: [Openvpn-devel] [PATCH v5 09/14] client-connect: Add deferred support to the client-connect script handler

In reply to Arne Schwabe

Hi,

Quoted 30 lines · Arne Schwabe

From: Fabian Knittel <[email protected]...>

This patch introduces the concept of a return value file for the client-connect handlers. (This is very similar to the auth value file used during deferred authentication.) The file name is stored in the client_connect_state struct.

In addition, the patch also allows the storage of the client config file name in struct client_connect_state.

Both changes are used by the client-connect script handler to support deferred client-connection handling. The deferred return value file (deferred_ret_file) is passed to the actual script via the environment. If the script succeeds and writes the value for deferral into the deferred_ret_file, the handler knows to indicate deferral. Later on, the deferred handler checks whether the value of the deferred_ret_file has been updated to success or failure.

---
 src/openvpn/multi.c | 230 +++++++++++++++++++++++++++++++++++++++++---
 src/openvpn/multi.h |  12 +++
 2 files changed, 227 insertions(+), 15 deletions(-)
src/openvpn/multi.c +0 -0
index ce73f8a1..271d09d8 100644
@@ -1843,6 +1843,168 @@ multi_client_set_protocol_options(struct context *c)
     }
 }

src/openvpn/multi.c multi_client_set_protocol_options(struct context *c) · line 1854

Quoted 10 lines
src/openvpn/multi.c +9 -0
 
+/**
+ * Delete the temporary file for the return value of client connect
+ * It also removes it from it from client_connect_defer_state and
+ * environment
+ */
+static void
+ccs_delete_deferred_ret_file(struct multi_instance *mi)
+{
+    struct client_connect_defer_state *ccs = &(mi->client_connect_defer_state);

parenthesis are not needed

Quoted 14 lines
src/openvpn/multi.c +14 -0
+    if (ccs->deferred_ret_file)
+    {
+        setenv_del(mi->context.c2.es, "client_connect_deferred_file");
+        if (!platform_unlink(ccs->deferred_ret_file))
+        {
+            msg(D_MULTI_ERRORS, "MULTI: problem deleting temporary file: %s",
+                ccs->deferred_ret_file);
+        }
+        free(ccs->deferred_ret_file);
+        ccs->deferred_ret_file = NULL;
+    }
+}
+
+/**

src/openvpn/multi.c multi_client_set_protocol_options(struct context *c) · line 1878

Quoted 10 lines
src/openvpn/multi.c +10 -0
+ * Create a temporary file for the return value of client connect
+ * and puts it into the client_connect_defer_state and environment
+ * as "client_connect_deferred_file"
+ *
+ * @return boolean value if creation was successfull
+ */
+static bool
+ccs_gen_deferred_ret_file(struct multi_instance *mi)
+{
+    struct client_connect_defer_state *ccs = &(mi->client_connect_defer_state);

parenthesis are not needed

Quoted 25 lines
src/openvpn/multi.c +25 -0
+    struct gc_arena gc = gc_new();
+    const char *fn;
+
+    if (ccs->deferred_ret_file)
+    {
+        ccs_delete_deferred_ret_file(mi);
+    }
+
+    fn = platform_create_temp_file(mi->context.options.tmp_dir, "ccr", &gc);
+    if (!fn)
+    {
+        gc_free(&gc);
+        return false;
+    }
+    ccs->deferred_ret_file = string_alloc(fn, NULL);
+
+    setenv_str(mi->context.c2.es, "client_connect_deferred_file",
+               ccs->deferred_ret_file);
+
+    gc_free(&gc);
+    return true;
+}
+
+/**
+ * Tests whether the deferred return value file exists and returns the

src/openvpn/multi.c multi_client_set_protocol_options(struct context *c) · line 1913

Quoted 10 lines
src/openvpn/multi.c +10 -0
+ * contained return value.
+ *
+ * @return CC_RET_SKIPPED if the file does not exist or is empty.
+ *         CC_RET_DEFERRED, CC_RET_SUCCEEDED or CC_RET_FAILED depending on
+ *         the value stored in the file.
+ */
+static enum client_connect_return
+ccs_test_deferred_ret_file(struct multi_instance *mi)
+{
+    struct client_connect_defer_state *ccs = &(mi->client_connect_defer_state);

parenthesis are not needed

src/openvpn/multi.c multi_client_set_protocol_options(struct context *c) · line 1915

Quoted 2 lines
src/openvpn/multi.c +2 -0
+    enum client_connect_return ret = CC_RET_SKIPPED;
+    FILE *fp = fopen(ccs->deferred_ret_file, "r");

I think we should always add an empty line between the first batch of variable declarations in a block and any code below.

Quoted 35 lines
src/openvpn/multi.c +35 -0
+    if (fp)
+    {
+        const int c = fgetc(fp);
+        switch (c)
+        {
+            case '0':
+                ret = CC_RET_FAILED;
+                break;
+
+            case '1':
+                ret = CC_RET_SUCCEEDED;
+                break;
+
+            case '2':
+                ret = CC_RET_DEFERRED;
+                break;
+
+            case EOF:
+                if (feof(fp))
+                {
+                    ret = CC_RET_SKIPPED;
+                    break;
+                }
+
+            /* Not EOF, but other error fall through to error state */
+            default:
+                /* We received an unknown/unexpected value.  Assume failure. */
+                msg(M_WARN, "WARNING: Unknown/unexcepted value in deferred"
+                    "client-connect resultfile");
+                ret = CC_RET_FAILED;
+        }
+        fclose(fp);
+    }
+    return ret;
+}

src/openvpn/multi.c multi_client_set_protocol_options(struct context *c) · line 1960

Quoted 10 lines
src/openvpn/multi.c +10 -0
+
+/**
+ * Deletes the temporary file for the config directives of the  client connect
+ * script and removes it into the client_connect_defer_state and environment
+ *
+ */
+static void
+ccs_delete_config_file(struct multi_instance *mi)
+{
+    struct client_connect_defer_state *ccs = &(mi->client_connect_defer_state);

parenthesis are not needed

Quoted 14 lines
src/openvpn/multi.c +14 -0
+    if (ccs->config_file)
+    {
+        setenv_del(mi->context.c2.es, "client_connect_config_file");
+        if (!platform_unlink(ccs->config_file))
+        {
+            msg(D_MULTI_ERRORS, "MULTI: problem deleting temporary file: %s",
+                ccs->config_file);
+        }
+        free(ccs->config_file);
+        ccs->config_file = NULL;
+    }
+}
+
+/**

src/openvpn/multi.c multi_client_set_protocol_options(struct context *c) · line 1984

Quoted 10 lines
src/openvpn/multi.c +10 -0
+ * Create a temporary file for the config directives of the  client connect
+ * script and puts it into the client_connect_defer_state and environment
+ * as "client_connect_config_file"
+ *
+ * @return boolean value if creation was successfull
+ */
+static bool
+ccs_gen_config_file(struct multi_instance *mi)
+{
+    struct client_connect_defer_state *ccs = &(mi->client_connect_defer_state);

parenthesis are not needed

Quoted 36 lines
src/openvpn/multi.c +23 -2
+    struct gc_arena gc = gc_new();
+    const char *fn;
+
+    if (ccs->config_file)
+    {
+        ccs_delete_config_file(mi);
+    }
+
+    fn = platform_create_temp_file(mi->context.options.tmp_dir, "cc", &gc);
+    if (!fn)
+    {
+        gc_free(&gc);
+        return false;
+    }
+    ccs->config_file = string_alloc(fn, NULL);
+
+    setenv_str(mi->context.c2.es, "client_connect_config_file",
+               ccs->config_file);
+
+    gc_free(&gc);
+    return true;
+}
+
 static enum client_connect_return
 multi_client_connect_call_plugin_v1(struct multi_context *m,
                                     struct multi_instance *mi,
@@ -1933,8 +2095,6 @@ multi_client_connect_call_plugin_v2(struct multi_context *m,
     return ret;
 }
 
-
-
 /**
  * Runs the --client-connect script if one is defined.
  */

src/openvpn/multi.c multi_client_connect_call_script(struct multi_context *m, · line 2111

Quoted 4 lines
src/openvpn/multi.c +1 -0
@@ -1948,48 +2108,88 @@ multi_client_connect_call_script(struct multi_context *m,
     ASSERT(mi);
 
     enum client_connect_return ret = CC_RET_SKIPPED;
+    struct client_connect_defer_state *ccs = &(mi->client_connect_defer_state);

parenthesis are not needed

Quoted 57 lines
src/openvpn/multi.c +17 -12
     if (mi->context.options.client_connect_script)
     {
         struct argv argv = argv_new();
         struct gc_arena gc = gc_new();
-        const char *dc_file = NULL;
 
         setenv_str(mi->context.c2.es, "script_type", "client-connect");
 
-        dc_file = platform_create_temp_file(mi->context.options.tmp_dir,
-                                            "cc", &gc);
-        if (!dc_file)
+        if (!ccs_gen_config_file(mi)
+            || !ccs_gen_deferred_ret_file(mi))
         {
             ret = CC_RET_FAILED;
             goto cleanup;
         }
 
         argv_parse_cmd(&argv, mi->context.options.client_connect_script);
-        argv_printf_cat(&argv, "%s", dc_file);
+        argv_printf_cat(&argv, "%s", ccs->config_file);
 
         if (openvpn_run_script(&argv, mi->context.c2.es, 0, "--client-connect"))
         {
-            multi_client_connect_post(m, mi, dc_file, option_types_found);
-            ret = CC_RET_SUCCEEDED;
+            if (ccs_test_deferred_ret_file(mi) == CC_RET_DEFERRED)
+            {
+                ret = CC_RET_DEFERRED;
+            }
+            else
+            {
+                multi_client_connect_post(m, mi, ccs->config_file,
+                                          option_types_found);
+                ret = CC_RET_SUCCEEDED;
+            }
         }
         else
         {
             ret = CC_RET_FAILED;
         }
-
-        if (!platform_unlink(dc_file))
+cleanup:
+        if (ret != CC_RET_DEFERRED)
         {
-            msg(D_MULTI_ERRORS, "MULTI: problem deleting temporary file: %s",
-                dc_file);
+            ccs_delete_config_file(mi);
+            ccs_delete_deferred_ret_file(mi);
         }
-cleanup:
         argv_free(&argv);
         gc_free(&gc);
     }
     return ret;

src/openvpn/multi.c multi_client_connect_call_script(struct multi_context *m, · line 2166

Quoted 10 lines
src/openvpn/multi.c +8 -0
 }
 
+static enum client_connect_return
+multi_client_connect_script_deferred(struct multi_context *m,
+                                     struct multi_instance *mi,
+                                     unsigned int *option_types_found)
+{
+    ASSERT(mi);
+    ASSERT(option_types_found);
+    struct client_connect_defer_state *ccs = &(mi->client_connect_defer_state);

parenthesis are not needed

src/openvpn/multi.c multi_client_connect_call_script(struct multi_context *m, · line 2177

Quoted 11 lines
src/openvpn/multi.c +11 -0
+    enum client_connect_return ret = CC_RET_SKIPPED;
+
+    ret = ccs_test_deferred_ret_file(mi);
+
+    if (ret == CC_RET_SKIPPED)
+    {
+        /*
+         * Skipped and deferred are equivalent in this context.
+         * skipped means that the called program has not yet
+         * written a return status implicitly needing more time
+         * while deferred is the explicit notifcation that it

typ0: notification

src/openvpn/multi.c multi_client_connect_call_script(struct multi_context *m, · line 2180

Quoted 3 lines
src/openvpn/multi.c +3 -0
+         * needs more time
+         */
+        ret = CC_RET_DEFERRED;

The comment above made me wonder..What happens if he client-connect script will never write to file due to a bug or a crash or anything?

Quoted 29 lines
src/openvpn/multi.c +13 -1
+    }
+
+    if (ret != CC_RET_DEFERRED)
+    {
+        ccs_delete_deferred_ret_file(mi);
+        multi_client_connect_post(m, mi, ccs->config_file,
+                                  option_types_found);
+        ccs_delete_config_file(mi);
+    }
+    return ret;
+}
+
 /**
  * Generates the data channel keys
  */
@@ -2251,7 +2451,7 @@ static const struct client_connect_handlers client_connect_handlers[] = {
     },
     {
         .main = multi_client_connect_call_script,
-        .deferred = multi_client_connect_fail
+        .deferred = multi_client_connect_script_deferred
     },
     {
         .main = multi_client_connect_mda,
src/openvpn/multi.h +0 -0
index 11da0209..3ebf6b9f 100644

src/openvpn/multi.h struct client_connect_defer_state · line 78

Quoted 6 lines
src/openvpn/multi.h +3 -0
@@ -73,6 +73,18 @@ struct client_connect_defer_state
     /* Remember which option classes where processed for delayed option
      * handling. */
     unsigned int option_types_found;
+
+    /**
+     * The temporrary file name that contains the return status of the

typ0: temporary

src/openvpn/multi.h struct client_connect_defer_state · line 90

Quoted 13 lines
src/openvpn/multi.h +9 -0
+     * client-connect script if it exits with defer as status
+     */
+    char *deferred_ret_file;
+
+    /**
+     * The temporary file name that contains the config directives
+     * returned by the client-connect script
+     */
+    char *config_file;
 };
 
 /**

The patch looks good. My only concern is reported above and is not really about this patch, but about the way the deferred handler is designed.

Signature · 1 line
Antonio Quartulli
#37 lore raw

PATCH v5 10/14 client-connect: Move adding inotify watch into its own function

In reply to Arne Schwabe

This make the code a bit better readable and also prepares resuing the function for client-connect return files

---
 src/openvpn/multi.c | 43 ++++++++++++++++++++++++++++---------------
 1 file changed, 28 insertions(+), 15 deletions(-)
src/openvpn/multi.c +28 -15
index 271d09d8..dafc85f1 100644
@@ -2828,6 +2828,32 @@ multi_schedule_context_wakeup(struct multi_context *m, struct multi_instance *mi
                        compute_wakeup_sigma(&mi->context.c2.timeval));
 }
 
+#if defined(ENABLE_ASYNC_PUSH) && defined(ENABLE_DEF_AUTH)
+static void
+add_inotify_file_watch(struct multi_context *m, struct multi_instance *mi,
+                       int inotify_fd, const char *file)
+{
+    /* watch acf file */
+    long watch_descriptor = inotify_add_watch(inotify_fd, file,
+                                              IN_CLOSE_WRITE | IN_ONESHOT);
+    if (watch_descriptor >= 0)
+    {
+        if (mi->inotify_watch != -1)
+        {
+            hash_remove(m->inotify_watchers,
+                        (void *) (unsigned long)mi->inotify_watch);
+        }
+        hash_add(m->inotify_watchers, (const uintptr_t *)watch_descriptor,
+                 mi, true);
+        mi->inotify_watch = watch_descriptor;
+    }
+    else
+    {
+        msg(M_NONFATAL | M_ERRNO, "MULTI: inotify_add_watch error");
+    }
+}
+#endif /* if defined(ENABLE_ASYNC_PUSH) && defined(ENABLE_DEF_AUTH) */
+
 /*
  * Figure instance-specific timers, convert
  * earliest to absolute time in mi->wakeup,
@@ -2865,21 +2891,8 @@ multi_process_post(struct multi_context *m, struct multi_instance *mi, const uns
         if (ks && ks->auth_control_file && was_unauthenticated
             && (ks->authenticated == KS_AUTH_DEFERRED))
         {
-            /* watch acf file */
-            long watch_descriptor = inotify_add_watch(m->top.c2.inotify_fd, ks->auth_control_file, IN_CLOSE_WRITE | IN_ONESHOT);
-            if (watch_descriptor >= 0)
-            {
-                if (mi->inotify_watch != -1)
-                {
-                    hash_remove(m->inotify_watchers, (void *) (unsigned long)mi->inotify_watch);
-                }
-                hash_add(m->inotify_watchers, (const uintptr_t *)watch_descriptor, mi, true);
-                mi->inotify_watch = watch_descriptor;
-            }
-            else
-            {
-                msg(M_NONFATAL | M_ERRNO, "MULTI: inotify_add_watch error");
-            }
+            add_inotify_file_watch(m, mi, m->top.c2.inotify_fd,
+                                   ks->auth_control_file);
         }
 #endif
 
Signature · 1 line
2.26.2
#38 lore raw

Re: [Openvpn-devel] [PATCH v5 10/14] client-connect: Move adding inotify watch into its own function

Quoted 1 line · Arne Schwabe

This make the code a bit better readable and also prepares resuing

resuing -> reusing

(Don't ask me why this is not re-using, which is how I would probably spell it and my teacher would laugh at me)

Grammar:
This make the code more readable

Quoted 69 lines

the function for client-connect return files

---
  src/openvpn/multi.c | 43 ++++++++++++++++++++++++++++---------------
  1 file changed, 28 insertions(+), 15 deletions(-)
src/openvpn/multi.c +28 -15
index 271d09d8..dafc85f1 100644
@@ -2828,6 +2828,32 @@ multi_schedule_context_wakeup(struct multi_context *m, struct multi_instance *mi
                         compute_wakeup_sigma(&mi->context.c2.timeval));
  }
 
+#if defined(ENABLE_ASYNC_PUSH) && defined(ENABLE_DEF_AUTH)
+static void
+add_inotify_file_watch(struct multi_context *m, struct multi_instance *mi,
+                       int inotify_fd, const char *file)
+{
+    /* watch acf file */
+    long watch_descriptor = inotify_add_watch(inotify_fd, file,
+                                              IN_CLOSE_WRITE | IN_ONESHOT);
+    if (watch_descriptor >= 0)
+    {
+        if (mi->inotify_watch != -1)
+        {
+            hash_remove(m->inotify_watchers,
+                        (void *) (unsigned long)mi->inotify_watch);
+        }
+        hash_add(m->inotify_watchers, (const uintptr_t *)watch_descriptor,
+                 mi, true);
+        mi->inotify_watch = watch_descriptor;
+    }
+    else
+    {
+        msg(M_NONFATAL | M_ERRNO, "MULTI: inotify_add_watch error");
+    }
+}
+#endif /* if defined(ENABLE_ASYNC_PUSH) && defined(ENABLE_DEF_AUTH) */
+
  /*
   * Figure instance-specific timers, convert
   * earliest to absolute time in mi->wakeup,
@@ -2865,21 +2891,8 @@ multi_process_post(struct multi_context *m, struct multi_instance *mi, const uns
          if (ks && ks->auth_control_file && was_unauthenticated
              && (ks->authenticated == KS_AUTH_DEFERRED))
          {
-            /* watch acf file */
-            long watch_descriptor = inotify_add_watch(m->top.c2.inotify_fd, ks->auth_control_file, IN_CLOSE_WRITE | IN_ONESHOT);
-            if (watch_descriptor >= 0)
-            {
-                if (mi->inotify_watch != -1)
-                {
-                    hash_remove(m->inotify_watchers, (void *) (unsigned long)mi->inotify_watch);
-                }
-                hash_add(m->inotify_watchers, (const uintptr_t *)watch_descriptor, mi, true);
-                mi->inotify_watch = watch_descriptor;
-            }
-            else
-            {
-                msg(M_NONFATAL | M_ERRNO, "MULTI: inotify_add_watch error");
-            }
+            add_inotify_file_watch(m, mi, m->top.c2.inotify_fd,
+                                   ks->auth_control_file);
          }
  #endif
 
Acked-by on v5 10/14
#39 lore raw

Re: [Openvpn-devel] [PATCH v5 10/14] client-connect: Move adding inotify watch into its own function

In reply to Arne Schwabe

Hi,

Quoted 4 lines · Arne Schwabe

This make the code a bit better readable and also prepares resuing the function for client-connect return files

This patch looks good and does what it says. No functional change is implemented, but it's all about moving a chunk of code in its own function and indenting it better than how it was.

Signature · 1 line
Antonio Quartulli
#40 lore raw

[Openvpn-devel] [PATCH applied] Re: client-connect: Move adding inotify watch into its own function

In reply to Arne Schwabe

Your patch has been applied to the master branch.

(I have merged this out of sequence while we still haggle about the least ugly way for 08 :-) - it's really "just moving this code parts", but due to the reformatting and variable renaming git can't see it. Compared manually. Also applied the typo and grammar changes from tincantech to the commit message)

commit 708d1694c7878c6c538c4ab0961eaaa36c007fd0

Author: Arne Schwabe
Date:   Sat Jul 11 11:36:51 2020 +0200
     client-connect: Move adding inotify watch into its own function

     Signed-off-by: Arne Schwabe <[email protected]...>
     Acked-by: Antonio Quartulli <[email protected]...>
     Message-Id: <[email protected]...>
     URL: https://www.mail-archive.com/[email protected]/msg20284.html
     Signed-off-by: Gert Doering <[email protected]...>

--
kind regards,

Gert Doering

#41 lore raw

PATCH v5 11/14 client-connect: Use inotify for the deferred client-connect status file

In reply to Arne Schwabe

As we never do client-connect and authentication at the same time it is safe to reuse the existing fields for client-connect return status file

---
 src/openvpn/multi.c | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)
src/openvpn/multi.c +13 -3
index dafc85f1..09a25a58 100644
@@ -2618,8 +2618,10 @@ multi_connection_established(struct multi_context *m, struct multi_instance *mi)
 
 #ifdef ENABLE_ASYNC_PUSH
 /*
- * Called when inotify event is fired, which happens when acf file is closed or deleted.
- * Continues authentication and sends push_reply.
+ * Called when inotify event is fired, which happens when acf
+ * or connect-status file is closed or deleted.
+ * Continues authentication and sends push_reply
+ * (or be deferred again by client-connect)
  */
 void
 multi_process_file_closed(struct multi_context *m, const unsigned int mpp_flags)
@@ -2905,7 +2907,15 @@ multi_process_post(struct multi_context *m, struct multi_instance *mi, const uns
             {
                 multi_connection_established(m, mi);
             }
-
+#if defined(ENABLE_ASYNC_PUSH) && defined(ENABLE_DEF_AUTH)
+            if (is_cas_pending(mi->context.c2.context_auth)
+                && mi->client_connect_defer_state.deferred_ret_file)
+            {
+                add_inotify_file_watch(m, mi, m->top.c2.inotify_fd,
+                                       mi->client_connect_defer_state.
+                                       deferred_ret_file);
+            }
+#endif
             /* tell scheduler to wake us up at some point in the future */
             multi_schedule_context_wakeup(m, mi);
         }
Signature · 1 line
2.26.2
#42 lore raw

PATCH v5 12/14 client-connect: Add deferred support to the client-connect plugin v1 handler

In reply to Arne Schwabe

From: Fabian Knittel <[email protected]...>

Uses the infrastructure provided and used in the previous patch to provide deferral support to the v1 client-connect plugin handler as well.

PATCH V3: Modify the API to also (optionally) call the plugin on a deferred call. This allows the plugin authors to be more flexible and make the V1 API more similar to the V2 API.

---
 include/openvpn-plugin.h.in | 29 +++++------
 src/openvpn/multi.c         | 97 ++++++++++++++++++++++++++++---------
 src/openvpn/plugin.c        |  3 ++
 3 files changed, 93 insertions(+), 36 deletions(-)
include/openvpn-plugin.h.in +15 -14
index 103844f7..99aa1678 100644
@@ -116,20 +116,21 @@ extern "C" {
  * FUNC: openvpn_plugin_client_destructor_v1 (top-level "generic" client)
  * FUNC: openvpn_plugin_close_v1
  */
-#define OPENVPN_PLUGIN_UP                    0
-#define OPENVPN_PLUGIN_DOWN                  1
-#define OPENVPN_PLUGIN_ROUTE_UP              2
-#define OPENVPN_PLUGIN_IPCHANGE              3
-#define OPENVPN_PLUGIN_TLS_VERIFY            4
-#define OPENVPN_PLUGIN_AUTH_USER_PASS_VERIFY 5
-#define OPENVPN_PLUGIN_CLIENT_CONNECT        6
-#define OPENVPN_PLUGIN_CLIENT_DISCONNECT     7
-#define OPENVPN_PLUGIN_LEARN_ADDRESS         8
-#define OPENVPN_PLUGIN_CLIENT_CONNECT_V2     9
-#define OPENVPN_PLUGIN_TLS_FINAL             10
-#define OPENVPN_PLUGIN_ENABLE_PF             11
-#define OPENVPN_PLUGIN_ROUTE_PREDOWN         12
-#define OPENVPN_PLUGIN_N                     13
+#define OPENVPN_PLUGIN_UP                        0
+#define OPENVPN_PLUGIN_DOWN                      1
+#define OPENVPN_PLUGIN_ROUTE_UP                  2
+#define OPENVPN_PLUGIN_IPCHANGE                  3
+#define OPENVPN_PLUGIN_TLS_VERIFY                4
+#define OPENVPN_PLUGIN_AUTH_USER_PASS_VERIFY     5
+#define OPENVPN_PLUGIN_CLIENT_CONNECT            6
+#define OPENVPN_PLUGIN_CLIENT_DISCONNECT         7
+#define OPENVPN_PLUGIN_LEARN_ADDRESS             8
+#define OPENVPN_PLUGIN_CLIENT_CONNECT_V2         9
+#define OPENVPN_PLUGIN_TLS_FINAL                10
+#define OPENVPN_PLUGIN_ENABLE_PF                11
+#define OPENVPN_PLUGIN_ROUTE_PREDOWN            12
+#define OPENVPN_PLUGIN_CLIENT_CONNECT_DEFER     13
+#define OPENVPN_PLUGIN_N                        14
 
 /*
  * Build a mask out of a set of plug-in types.
src/openvpn/multi.c +75 -22

139 lines not on this page · raw message

src/openvpn/plugin.c +3 -0
index 4de1d6b7..ea18592f 100644
@@ -104,6 +104,9 @@ plugin_type_name(const int type)
         case OPENVPN_PLUGIN_CLIENT_CONNECT_V2:
             return "PLUGIN_CLIENT_CONNECT";
 
+        case OPENVPN_PLUGIN_CLIENT_CONNECT_DEFER:
+            return "PLUGIN_CLIENT_CONNECT";
+
         case OPENVPN_PLUGIN_CLIENT_DISCONNECT:
             return "PLUGIN_CLIENT_DISCONNECT";
 
Signature · 1 line
2.26.2
#43 lore raw

Re: [Openvpn-devel] [PATCH v5 12/14] client-connect: Add deferred support to the client-connect plugin v1 handler

2x gram

Quoted 120 lines · Arne Schwabe

From: Fabian Knittel <[email protected]...>

Uses the infrastructure provided and used in the previous patch to provide deferral support to the v1 client-connect plugin handler as well.

PATCH V3: Modify the API to also (optionally) call the plugin on a deferred call. This allows the plugin authors to be more flexible and make the V1 API more similar to the V2 API.

---
  include/openvpn-plugin.h.in | 29 +++++------
  src/openvpn/multi.c         | 97 ++++++++++++++++++++++++++++---------
  src/openvpn/plugin.c        |  3 ++
  3 files changed, 93 insertions(+), 36 deletions(-)
include/openvpn-plugin.h.in +15 -14
index 103844f7..99aa1678 100644
@@ -116,20 +116,21 @@ extern "C" {
   * FUNC: openvpn_plugin_client_destructor_v1 (top-level "generic" client)
   * FUNC: openvpn_plugin_close_v1
   */
-#define OPENVPN_PLUGIN_UP                    0
-#define OPENVPN_PLUGIN_DOWN                  1
-#define OPENVPN_PLUGIN_ROUTE_UP              2
-#define OPENVPN_PLUGIN_IPCHANGE              3
-#define OPENVPN_PLUGIN_TLS_VERIFY            4
-#define OPENVPN_PLUGIN_AUTH_USER_PASS_VERIFY 5
-#define OPENVPN_PLUGIN_CLIENT_CONNECT        6
-#define OPENVPN_PLUGIN_CLIENT_DISCONNECT     7
-#define OPENVPN_PLUGIN_LEARN_ADDRESS         8
-#define OPENVPN_PLUGIN_CLIENT_CONNECT_V2     9
-#define OPENVPN_PLUGIN_TLS_FINAL             10
-#define OPENVPN_PLUGIN_ENABLE_PF             11
-#define OPENVPN_PLUGIN_ROUTE_PREDOWN         12
-#define OPENVPN_PLUGIN_N                     13
+#define OPENVPN_PLUGIN_UP                        0
+#define OPENVPN_PLUGIN_DOWN                      1
+#define OPENVPN_PLUGIN_ROUTE_UP                  2
+#define OPENVPN_PLUGIN_IPCHANGE                  3
+#define OPENVPN_PLUGIN_TLS_VERIFY                4
+#define OPENVPN_PLUGIN_AUTH_USER_PASS_VERIFY     5
+#define OPENVPN_PLUGIN_CLIENT_CONNECT            6
+#define OPENVPN_PLUGIN_CLIENT_DISCONNECT         7
+#define OPENVPN_PLUGIN_LEARN_ADDRESS             8
+#define OPENVPN_PLUGIN_CLIENT_CONNECT_V2         9
+#define OPENVPN_PLUGIN_TLS_FINAL                10
+#define OPENVPN_PLUGIN_ENABLE_PF                11
+#define OPENVPN_PLUGIN_ROUTE_PREDOWN            12
+#define OPENVPN_PLUGIN_CLIENT_CONNECT_DEFER     13
+#define OPENVPN_PLUGIN_N                        14
 
  /*
   * Build a mask out of a set of plug-in types.
src/openvpn/multi.c +25 -13
index 09a25a58..08eb44ba 100644
@@ -2008,56 +2008,109 @@ ccs_gen_config_file(struct multi_instance *mi)
  static enum client_connect_return
  multi_client_connect_call_plugin_v1(struct multi_context *m,
                                      struct multi_instance *mi,
-                                    unsigned int *option_types_found)
+                                    unsigned int *option_types_found,
+                                    bool deferred)
  {
      enum client_connect_return ret = CC_RET_SKIPPED;
  #ifdef ENABLE_PLUGIN
      ASSERT(m);
      ASSERT(mi);
      ASSERT(option_types_found);
+    struct client_connect_defer_state *ccs = &(mi->client_connect_defer_state);
 
      /* deprecated callback, use a file for passing back return info */
      if (plugin_defined(mi->context.plugins, OPENVPN_PLUGIN_CLIENT_CONNECT))
      {
          struct argv argv = argv_new();
-        struct gc_arena gc = gc_new();
-        const char *dc_file =
-            platform_create_temp_file(mi->context.options.tmp_dir, "cc", &gc);
+        int call;
 
-        if (!dc_file)
+        if (!deferred)
          {
-            ret = CC_RET_FAILED;
-            goto cleanup;
+            call = OPENVPN_PLUGIN_CLIENT_CONNECT;
+            if (!ccs_gen_config_file(mi)
+                || !ccs_gen_deferred_ret_file(mi))
+            {
+                ret = CC_RET_FAILED;
+                goto cleanup;
+            }
+        }
+        else
+        {
+            call = OPENVPN_PLUGIN_CLIENT_CONNECT_DEFER;
+            /* the initial call should have created these files */
+            ASSERT(ccs->config_file);
+            ASSERT(ccs->deferred_ret_file);
          }
 
-        argv_printf(&argv, "%s", dc_file);
-        if (plugin_call(mi->context.plugins, OPENVPN_PLUGIN_CLIENT_CONNECT,
-                        &argv, NULL, mi->context.c2.es)
-            != OPENVPN_PLUGIN_FUNC_SUCCESS)
+        argv_printf(&argv, "%s", ccs->config_file);
+        int plug_ret = plugin_call(mi->context.plugins, call,
+                                   &argv, NULL, mi->context.c2.es);
+        if (plug_ret == OPENVPN_PLUGIN_FUNC_SUCCESS)
          {
-            msg(M_WARN, "WARNING: client-connect plugin call failed");
-            ret = CC_RET_FAILED;
+            multi_client_connect_post(m, mi, ccs->config_file,
+                                      option_types_found);

src/openvpn/multi.c ccs_gen_config_file(struct multi_instance *mi) · line 2061

Quoted 10 lines
src/openvpn/multi.c +10 -0
+            ret = CC_RET_SUCCEEDED;
+        }
+        else if (plug_ret == OPENVPN_PLUGIN_FUNC_DEFERRED)
+        {
+            ret = CC_RET_DEFERRED;
+            /**
+             * Contrary to the plugin v2 API, we do not demand a working
+             * deferred plugin as all return can be handled by the files
+             * and plugin_call return success if a plugin is not defined
+             */

Just a nudge to consider rewording this:

deferred plugin because all returns can be handled by the files, and plugin_calls return success if a plugin is not defined.

Quoted 4 lines
src/openvpn/multi.c +0 -1
          }
          else
          {
-            multi_client_connect_post(m, mi, dc_file, option_types_found);

src/openvpn/multi.c ccs_gen_config_file(struct multi_instance *mi) · line 2072

Quoted 10 lines
src/openvpn/multi.c +6 -2
-            ret = CC_RET_SUCCEEDED;
+            msg(M_WARN, "WARNING: client-connect plugin call failed");
+            ret = CC_RET_FAILED;
          }
 
-        if (!platform_unlink(dc_file))
+
+        /**
+         * plugin api v1 client connect async feature has both plugin and
+         * file return status, so in case that the file has a code that

Either:
so in case the file has

or
so in cases where the file has

Probably the second.

Quoted 74 lines
src/openvpn/multi.c +34 -6
+         * demands override, we override our return code
+         */
+        int file_ret = ccs_test_deferred_ret_file(mi);
+
+        if (file_ret == CC_RET_FAILED)
          {
-            msg(D_MULTI_ERRORS, "MULTI: problem deleting temporary file: %s",
-                dc_file);
+            ret = CC_RET_FAILED;
+        }
+        else if (ret == CC_RET_SUCCEEDED && file_ret == CC_RET_DEFERRED)
+        {
+            ret = CC_RET_DEFERRED;
          }
-
  cleanup:
          argv_free(&argv);
-        gc_free(&gc);
+
+        if (ret != CC_RET_DEFERRED)
+        {
+            ccs_delete_config_file(mi);
+            ccs_delete_deferred_ret_file(mi);
+        }
      }
  #endif /* ifdef ENABLE_PLUGIN */
      return ret;
  }
 
+static enum client_connect_return
+multi_client_connect_call_plugin_v1_initial(struct multi_context *m,
+                                            struct multi_instance *mi,
+                                            unsigned int *option_types_found)
+{
+    return multi_client_connect_call_plugin_v1(m,mi, option_types_found, false);
+}
+
+static enum client_connect_return
+multi_client_connect_call_plugin_v1_deferred(struct multi_context *m,
+                                             struct multi_instance *mi,
+                                             unsigned int *option_types_found)
+{
+    return multi_client_connect_call_plugin_v1(m,mi, option_types_found, true);
+}
+
  static enum client_connect_return
  multi_client_connect_call_plugin_v2(struct multi_context *m,
                                      struct multi_instance *mi,
@@ -2442,8 +2495,8 @@ static const struct client_connect_handlers client_connect_handlers[] = {
          .deferred = multi_client_connect_fail
      },
      {
-        .main = multi_client_connect_call_plugin_v1,
-        .deferred = multi_client_connect_fail
+        .main = multi_client_connect_call_plugin_v1_initial,
+        .deferred = multi_client_connect_call_plugin_v1_deferred,
      },
      {
          .main = multi_client_connect_call_plugin_v2,
src/openvpn/plugin.c +3 -0
index 4de1d6b7..ea18592f 100644
@@ -104,6 +104,9 @@ plugin_type_name(const int type)
          case OPENVPN_PLUGIN_CLIENT_CONNECT_V2:
              return "PLUGIN_CLIENT_CONNECT";
 
+        case OPENVPN_PLUGIN_CLIENT_CONNECT_DEFER:
+            return "PLUGIN_CLIENT_CONNECT";
+
          case OPENVPN_PLUGIN_CLIENT_DISCONNECT:
              return "PLUGIN_CLIENT_DISCONNECT";
 
#44 lore raw

Re: [Openvpn-devel] [PATCH v5 12/14] client-connect: Add deferred support to the client-connect plugin v1 handler

In reply to Arne Schwabe

Hi,

Quoted 5 lines · Arne Schwabe

From: Fabian Knittel <[email protected]...>

Uses the infrastructure provided and used in the previous patch to provide deferral support to the v1 client-connect plugin handler as well.

Quoted 7 lines

PATCH V3: Modify the API to also (optionally) call the plugin on a deferred call. This allows the plugin authors to be more flexible and make the V1 API more similar to the V2 API.

I think this should get a feature-NAK.

Plugin v1 is old, and nobody should be developing for this API anymore.

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]...
#45 lore raw

PATCH v5 13/14 client-connect: Implement deferred connect support for plugin API v2

In reply to Arne Schwabe

The V2 API is simpler than the V1 API since there is no passing of data via files. This also means that with the current API the V2 API cannot support async notify via files. Adding a file just for async notify seems very hacky and when needed we should implement a better option when async is needed for the plugin V2 API.

---
 include/openvpn-plugin.h.in |  3 +-
 src/openvpn/multi.c         | 58 ++++++++++++++++++++++++++++++-------
 src/openvpn/plugin.c        |  3 ++
 3 files changed, 52 insertions(+), 12 deletions(-)
include/openvpn-plugin.h.in +2 -1
index 99aa1678..38fbe097 100644
@@ -130,7 +130,8 @@ extern "C" {
 #define OPENVPN_PLUGIN_ENABLE_PF                11
 #define OPENVPN_PLUGIN_ROUTE_PREDOWN            12
 #define OPENVPN_PLUGIN_CLIENT_CONNECT_DEFER     13
-#define OPENVPN_PLUGIN_N                        14
+#define OPENVPN_PLUGIN_CLIENT_CONNECT_DEFER_V2  14
+#define OPENVPN_PLUGIN_N                        15
 
 /*
  * Build a mask out of a set of plug-in types.
src/openvpn/multi.c +47 -11

94 lines not on this page · raw message

src/openvpn/plugin.c +3 -0
index ea18592f..80abb730 100644
@@ -107,6 +107,9 @@ plugin_type_name(const int type)
         case OPENVPN_PLUGIN_CLIENT_CONNECT_DEFER:
             return "PLUGIN_CLIENT_CONNECT";
 
+        case OPENVPN_PLUGIN_CLIENT_CONNECT_DEFER_V2:
+            return "PLUGIN_CLIENT_CONNECT";
+
         case OPENVPN_PLUGIN_CLIENT_DISCONNECT:
             return "PLUGIN_CLIENT_DISCONNECT";
 
Signature · 1 line
2.26.2
#46 lore raw

Re: [Openvpn-devel] [PATCH v5 13/14] client-connect: Implement deferred connect support for plugin API v2

Hi,

Quoted 5 lines · Arne Schwabe

The V2 API is simpler than the V1 API since there is no passing of data via files. This also means that with the current API the V2 API cannot support async notify via files. Adding a file just for async notify seems very hacky and when needed we should implement a better option when async is needed for the plugin V2 API.

I'm not sure I understand a word of this.

If we want async cc, we should do it in the V2 API - and since v2 plugin async auth works just fine (with file), I wonder why it is hard for async cc? Because we cannot pass back the returned config data?

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]...
#47 lore raw

Re: [Openvpn-devel] [PATCH v5 13/14] client-connect: Implement deferred connect support for plugin API v2

Am 13.07.20 um 17:40 schrieb Gert Doering:

Quoted 9 lines

Hi,

Quoted 5 lines · Arne Schwabe

The V2 API is simpler than the V1 API since there is no passing of data via files. This also means that with the current API the V2 API cannot support async notify via files. Adding a file just for async notify seems very hacky and when needed we should implement a better option when async is needed for the plugin V2 API.

Quoted 5 lines

I'm not sure I understand a word of this.

If we want async cc, we should do it in the V2 API - and since v2 plugin async auth works just fine (with file), I wonder why it is hard for async cc? Because we cannot pass back the returned config data?

Not async cc. That works fine. But async notification via file notify does not work without a file being involved.

v1 API returns config and status via files.

v2 returns status as return value (OPENVPN_PLUGIN_FUNC_SUCCESS, OPENVPN_PLUGIN_FUNC_DEFERRED...) and the config file via memory in plugin_return. So no file involved and no way to use inotify

Arne

#48 lore raw

Re: [Openvpn-devel] [PATCH v5 13/14] client-connect: Implement deferred connect support for plugin API v2

Hi,

Quoted 12 lines · Arne Schwabe
Quoted 11 lines
Quoted 5 lines

The V2 API is simpler than the V1 API since there is no passing of data via files. This also means that with the current API the V2 API cannot support async notify via files. Adding a file just for async notify seems very hacky and when needed we should implement a better option when async is needed for the plugin V2 API.

I'm not sure I understand a word of this.

If we want async cc, we should do it in the V2 API - and since v2 plugin async auth works just fine (with file), I wonder why it is hard for async cc? Because we cannot pass back the returned config data?

Quoted 8 lines

Not async cc. That works fine. But async notification via file notify does not work without a file being involved.

v1 API returns config and status via files.

v2 returns status as return value (OPENVPN_PLUGIN_FUNC_SUCCESS, OPENVPN_PLUGIN_FUNC_DEFERRED...) and the config file via memory in plugin_return. So no file involved and no way to use inotify

For the record, I'm all confused between lowercase and uppercase v1, v2, v3 - there is "openvpn_plugin_func_v1()"... "openvpn_plugin_func_v3", and for CLIENT_CONNECT - *only!* - there is also CLIENT_CONNECT_V2, but not for, like, async auth.

So, I withdraw what I said here, and need to read this stuff up in more detail. Look at existing client-connect plugins... (if there are any). Etc.

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]...
#49 lore raw

PATCH v5 14/14 client-connect: Add documentation for the deferred client connect feature

In reply to Arne Schwabe

Patch V5: Fix typos, clarify man page section about deferred client-connect
script. Add section to Changes.rst

---
 Changes.rst                 |  4 +++
 doc/openvpn.8               | 55 +++++++++++++++++++++++++++++++++++--
 include/openvpn-plugin.h.in | 21 ++++++++++----
 3 files changed, 71 insertions(+), 9 deletions(-)
Changes.rst +4 -0
index 42f0d190..47fa6883 100644
@@ -3,6 +3,10 @@ Overview of changes in 2.5
 
 New features
 ------------
+Deferred client-connect
+    client-connect and the connect plugin API allow now asynchronous/deferred
+    return of the configuration file in the same way as the auth-plugin.
+
 Client-specific tls-crypt keys (``--tls-crypt-v2``)
     ``tls-crypt-v2`` adds the ability to supply each client with a unique
     tls-crypt key.  This allows large organisations and VPN providers to profit
doc/openvpn.8 +52 -3

76 lines not on this page · raw message

include/openvpn-plugin.h.in +15 -6
index 38fbe097..64b20886 100644
@@ -557,12 +557,21 @@ OPENVPN_PLUGIN_DEF openvpn_plugin_handle_t OPENVPN_PLUGIN_FUNC(openvpn_plugin_op
  * OPENVPN_PLUGIN_FUNC_SUCCESS on success, OPENVPN_PLUGIN_FUNC_ERROR on failure
  *
  * In addition, OPENVPN_PLUGIN_FUNC_DEFERRED may be returned by
- * OPENVPN_PLUGIN_AUTH_USER_PASS_VERIFY.  This enables asynchronous
- * authentication where the plugin (or one of its agents) may indicate
- * authentication success/failure some number of seconds after the return
- * of the OPENVPN_PLUGIN_AUTH_USER_PASS_VERIFY handler by writing a single
- * char to the file named by auth_control_file in the environmental variable
- * list (envp).
+ * OPENVPN_PLUGIN_AUTH_USER_PASS_VERIFY, OPENVPN_PLUGIN_CLIENT_CONNECT and
+ * OPENVPN_PLUGIN_CLIENT_CONNECT_V2. This enables asynchronous
+ * authentication or client connect  where the plugin (or one of its agents)
+ * may indicate authentication success/failure or client configuration some
+ * number of seconds after the return of the function handler.
+ * For OPENVPN_PLUGIN_AUTH_USER_PASS_VERIFY and OPENVPN_PLUGIN_CLIENT_CONNECT
+ * this is done by writing a single char to the file named by
+ * auth_control_file/client_connect_deferred_file
+ * in the environmental variable list (envp).
+ *
+ * In addition the OPENVPN_PLUGIN_CLIENT_CONNECT_DEFER and
+ * OPENVPN_PLUGIN_CLIENT_CONNECT_DEFER_V2 are called when OpenVPN tries to
+ * get the deferred result. For a V2 call implementing this function is
+ * required as information is not passed by files. For the normal version
+ * the call is optional.
  *
  * first char of auth_control_file:
  * '0' -- indicates auth failure
Signature · 1 line
2.26.2
#50 lore raw

Re: [Openvpn-devel] [PATCH v5 14/14] client-connect: Add documentation for the deferred client connect feature

Because this is documentation I have been a little harder on grammar.

These are only suggestions to improve readability.

Quoted 15 lines · Arne Schwabe

Patch V5: Fix typos, clarify man page section about deferred client-connect
script. Add section to Changes.rst

---
  Changes.rst                 |  4 +++
  doc/openvpn.8               | 55 +++++++++++++++++++++++++++++++++++--
  include/openvpn-plugin.h.in | 21 ++++++++++----
  3 files changed, 71 insertions(+), 9 deletions(-)
Changes.rst +0 -0
index 42f0d190..47fa6883 100644

Changes.rst Overview of changes in 2.5 · line 7

Quoted 5 lines
Changes.rst +2 -0
@@ -3,6 +3,10 @@ Overview of changes in 2.5
 
  New features
  ------------
+Deferred client-connect
+    client-connect and the connect plugin API allow now asynchronous/deferred

allow now -> now allow

Quoted 10 lines
Changes.rst +2 -0
+    return of the configuration file in the same way as the auth-plugin.
+
  Client-specific tls-crypt keys (``--tls-crypt-v2``)
      ``tls-crypt-v2`` adds the ability to supply each client with a unique
      tls-crypt key.  This allows large organisations and VPN providers to profit
doc/openvpn.8 +0 -0
index 03ae5ac5..7a0080bf 100644

doc/openvpn.8 is significant. If · line 3428

Quoted 7 lines
doc/openvpn.8 +4 -0
@@ -3422,6 +3422,13 @@ is significant.  If
  .B script
  returns a non\-zero error status, it will cause the client
  to be disconnected.
+
+If a
+.B \-\-client\-connect cmd
+wants to defer the generating of the configuration the script, should

wants to defer the generating of the configuration then the script should

doc/openvpn.8 is significant. If · line 3430

Quoted 2 lines
doc/openvpn.8 +2 -0
+use the client_connect_deferred_file and client_connect_config_file
+environment variables and write status accordingly into these files

environment variables. And write status accordingly into these files

Quoted 27 lines
doc/openvpn.8 +10 -3
+(See the environment section below for more details).
  .\"*********************************************************
  .TP
  .B \-\-client\-disconnect cmd
@@ -3505,12 +3512,18 @@ This directory will be used by in the following cases:
 
  *
  .B \-\-client\-connect
-scripts to dynamically generate client\-specific
-configuration files.
+scripts and
+.B OPENVPN_PLUGIN_CLIENT_CONNECT
+plugin hook
+to dynamically generate client\-specific configuration files
+and return success/failure via client_connect_deferred_file
+when using deferred client connect method
 
  *
  .B OPENVPN_PLUGIN_AUTH_USER_PASS_VERIFY
-plugin hook to return success/failure via auth_control_file
+and
+
+plugin hook to return success/failure via auth_control_file/
  when using deferred auth method
 
  *

doc/openvpn.8 Set prior to execution of the · line 6675

Quoted 9 lines
doc/openvpn.8 +6 -0
@@ -6654,6 +6667,42 @@ Set prior to execution of the
  script.
  .\"*********************************************************
  .TP
+.B client_connect_config_file
+The path to the configuration file that should be written by
+the
+.B \-\-client\-connect
+script. The content of this environment variable is identical
+to the file as a argument of the called

as an argument

doc/openvpn.8 Set prior to execution of the · line 6688

Quoted 13 lines
doc/openvpn.8 +13 -0
+.B \-\-client\-connect
+script.
+.\"*********************************************************
+.TP
+.B client_connect_deferred_file
+This file can be optionally written to communicate a status
+code of the
+.TP
+.B \-\-client\-connect
+script. If used for deferring, this file must be written
+before the
+.B \-\-client\-connect
+script exits. The first character in the file has to be

must be either:

Quoted 4 lines
doc/openvpn.8 +4 -0
+'1' is to indicate normal script execution, '0' indicates an
+error (in the same way that a non zero exit status does) and
+'2' indicates that the script deferred returning the config
+file. When the script defers returning the configuration, it

doc/openvpn.8 Set prior to execution of the · line 6702

Quoted 10 lines
doc/openvpn.8 +10 -0
+must also write '2' to to the file to indicate the deferral.
+A background process or similar must then take care of writing the
+configuration to the file indicated by the
+.B
+client_connect_config_file
+environment variable and when finished, write the a '1' to this
+file (or '0' in case of an error).
+
+The absence of any character in the file when the script finishes
+executing is interpreted the same as '1'. This allows script that

This allows scripts

Quoted 23 lines
doc/openvpn.8 +3 -0
+are not written to support the defer mechanism to be used unmodified.
+.\"*********************************************************
+.TP
  .B common_name
  The X509 common name of an authenticated client.
  Set prior to execution of
include/openvpn-plugin.h.in +3 -6
index 38fbe097..64b20886 100644
@@ -557,12 +557,21 @@ OPENVPN_PLUGIN_DEF openvpn_plugin_handle_t OPENVPN_PLUGIN_FUNC(openvpn_plugin_op
   * OPENVPN_PLUGIN_FUNC_SUCCESS on success, OPENVPN_PLUGIN_FUNC_ERROR on failure
   *
   * In addition, OPENVPN_PLUGIN_FUNC_DEFERRED may be returned by
- * OPENVPN_PLUGIN_AUTH_USER_PASS_VERIFY.  This enables asynchronous
- * authentication where the plugin (or one of its agents) may indicate
- * authentication success/failure some number of seconds after the return
- * of the OPENVPN_PLUGIN_AUTH_USER_PASS_VERIFY handler by writing a single
- * char to the file named by auth_control_file in the environmental variable
- * list (envp).
+ * OPENVPN_PLUGIN_AUTH_USER_PASS_VERIFY, OPENVPN_PLUGIN_CLIENT_CONNECT and
+ * OPENVPN_PLUGIN_CLIENT_CONNECT_V2. This enables asynchronous
+ * authentication or client connect  where the plugin (or one of its agents)

include/openvpn-plugin.h.in OPENVPN_PLUGIN_DEF openvpn_plugin_handle_t OPENVPN_PLUGIN_FUNC(openvpn_plugin_op · line 572

Quoted 10 lines
include/openvpn-plugin.h.in +10 -0
+ * may indicate authentication success/failure or client configuration some
+ * number of seconds after the return of the function handler.
+ * For OPENVPN_PLUGIN_AUTH_USER_PASS_VERIFY and OPENVPN_PLUGIN_CLIENT_CONNECT
+ * this is done by writing a single char to the file named by
+ * auth_control_file/client_connect_deferred_file
+ * in the environmental variable list (envp).
+ *
+ * In addition the OPENVPN_PLUGIN_CLIENT_CONNECT_DEFER and
+ * OPENVPN_PLUGIN_CLIENT_CONNECT_DEFER_V2 are called when OpenVPN tries to
+ * get the deferred result. For a V2 call implementing this function is

For a V2 call implementing this function it is

include/openvpn-plugin.h.in OPENVPN_PLUGIN_DEF openvpn_plugin_handle_t OPENVPN_PLUGIN_FUNC(openvpn_plugin_op · line 573

Quoted 1 line
include/openvpn-plugin.h.in +1 -0
+ * required as information is not passed by files. For the normal version

required because information

Quoted 5 lines
include/openvpn-plugin.h.in +1 -0
+ * the call is optional.
   *
   * first char of auth_control_file:
   * '0' -- indicates auth failure
#51 lore raw

Re: [Openvpn-devel] [PATCH v5 14/14] client-connect: Add documentation for the deferred client connect feature

In reply to Arne Schwabe

So I'm looking into migrating this text over to the new .rst format ... and I have a question ...

Quoted 28 lines · Arne Schwabe
doc/openvpn.8 +11 -2
index 03ae5ac5..7a0080bf 100644
@@ -3422,6 +3422,13 @@ is significant.  If
 .B script
 returns a non\-zero error status, it will cause the client
 to be disconnected.
+
+If a
+.B \-\-client\-connect cmd
+wants to defer the generating of the configuration the script, should
+use the client_connect_deferred_file and client_connect_config_file
+environment variables and write status accordingly into these files
+(See the environment section below for more details).
 .\"*********************************************************
 .TP
 .B \-\-client\-disconnect cmd
@@ -3505,12 +3512,18 @@ This directory will be used by in the following cases:
 
 *
 .B \-\-client\-connect
-scripts to dynamically generate client\-specific
-configuration files.
+scripts and
+.B OPENVPN_PLUGIN_CLIENT_CONNECT
+plugin hook
+to dynamically generate client\-specific configuration files

doc/openvpn.8 This directory will be used by in the following cases: · line 3527

Quoted 10 lines
doc/openvpn.8 +5 -1
+and return success/failure via client_connect_deferred_file
+when using deferred client connect method
 
 *
 .B OPENVPN_PLUGIN_AUTH_USER_PASS_VERIFY
-plugin hook to return success/failure via auth_control_file
+and
+
+plugin hook to return success/failure via auth_control_file/
 when using deferred auth method

This part is oddly rendered, and it seems it lacks something ...

* OPENVPN_PLUGIN_AUTH_USER_PASS_VERIFY and

plugin hook to return success/failure via
auth_control_file/client_connect_deferred_file
when using deferred auth method

Is this what you intended to say?

* OPENVPN_PLUGIN_AUTH_USER_PASS_VERIFY plugin hook and --client-connect scripts to return success/failure via auth_control_file/client_connect_deferred_file when using deferred auth method

Signature · 4 lines
kind regards,

David Sommerseth
OpenVPN Inc
#52 lore raw

Re: [Openvpn-devel] [PATCH v5 14/14] client-connect: Add documentation for the deferred client connect feature

Hi,

Quoted 2 lines · David Sommerseth

So I'm looking into migrating this text over to the new .rst format ... and I have a question ...

This one *should* be identical to 6/6 from the "v7" series, but just for completeness - the "v5" series has been reworked completely and the last 6 patches been resent as v7.

Doc patch is "v7 6/6" now, same subject otherwise.

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]...
#53 lore raw

Re: [Openvpn-devel] [PATCH v5 14/14] client-connect: Add documentation for the deferred client connect feature

Quoted 11 lines · Gert Doering

Hi,

Quoted 2 lines · David Sommerseth

So I'm looking into migrating this text over to the new .rst format ... and I have a question ...

This one *should* be identical to 6/6 from the "v7" series, but just for completeness - the "v5" series has been reworked completely and the last 6 patches been resent as v7.

Doc patch is "v7 6/6" now, same subject otherwise.

Ahh, thanks! Yeah, it looks like it's the same man page. I suggest we don't do anything with the man page in this patch-set (except of clarifying the content) and I'll ensure it gets ported into a reasonable shape into the new man page structure.

Signature · 4 lines
kind regards,

David Sommerseth
OpenVPN Inc
Acked-by on v5 1/14
#54 lore raw
In reply to Arne Schwabe

The patch is trivial enough (it just allows "cipher" in ccd/ files, with no logic changes) - it's built on the changes in the previous patches, which makes it "just work".

Without the patch, trying to set & push a cipher from ccd/:

Jul 11 18:27:53 gentoo tap-udp-p2mp[12620]: Options error: option 'cipher' cannot be used in this context (ccd/freebsd-74-amd64)
Jul 11 18:27:55 gentoo tap-udp-p2mp[12620]: ... SENT CONTROL [freebsd-74-amd64]: 'PUSH_REPLY,...,cipher CAMELLIA-128-CBC,...,cipher AES-256-GCM' (status=1)

With the patch *and* forcing NCP on the server side by only allowing CAMELLIA-128-CBC:

  $ cat ccd/freebsd-74-amd64
  ncp-ciphers CAMELLIA-128-CBC
  cipher CAMELLIA-128-CBC

it will actually do that:

Jul 11 18:42:37 gentoo tap-udp-p2mp[13661]: Outgoing Data Channel: Cipher 'CAMELLIA-128-CBC' initialized with 128 bit key
Jul 11 18:42:37 gentoo tap-udp-p2mp[13661]: Incoming Data Channel: Cipher 'CAMELLIA-128-CBC' initialized with 128 bit key
Jul 11 18:42:38 gentoo tap-udp-p2mp[13661]: SENT CONTROL [freebsd-74-amd64]: 'PUSH_REPLY,...,peer-id 2,cipher CAMELLIA-128-CBC' (status=1)

(if I put "CAMELLIA and some of the AES-GCM variants" in there, I get the standard AES-256-GCM or AES-128-GCM variants - with no indication in the logs on why it doesn't want to take the cipher --> documenting this here, so it can be found by googling: if you want "cipher" to work in CCD/ files, you must also set "ncp-ciphers" accordingly).

Your patch has been applied to the master branch.

commit 6168f53d6b7274026d4f392a22e64524a9b264d6

Author: Arne Schwabe
Date:   Sat Jul 11 11:36:42 2020 +0200
     Allow changing fallback cipher from ccd files/client-connect

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

--
kind regards,

Gert Doering

#55 lore raw

Am 11.07.2020 um 18:48 schrieb Gert Doering:

Quoted 20 lines

The patch is trivial enough (it just allows "cipher" in ccd/ files, with no logic changes) - it's built on the changes in the previous patches, which makes it "just work".

Without the patch, trying to set & push a cipher from ccd/:

Jul 11 18:27:53 gentoo tap-udp-p2mp[12620]: Options error: option 'cipher' cannot be used in this context (ccd/freebsd-74-amd64)
Jul 11 18:27:55 gentoo tap-udp-p2mp[12620]: ... SENT CONTROL [freebsd-74-amd64]: 'PUSH_REPLY,...,cipher CAMELLIA-128-CBC,...,cipher AES-256-GCM' (status=1)

With the patch *and* forcing NCP on the server side by only allowing CAMELLIA-128-CBC:

  $ cat ccd/freebsd-74-amd64
  ncp-ciphers CAMELLIA-128-CBC
  cipher CAMELLIA-128-CBC

it will actually do that:

Quoted 8 lines
Jul 11 18:42:37 gentoo tap-udp-p2mp[13661]: Outgoing Data Channel: Cipher 'CAMELLIA-128-CBC' initialized with 128 bit key
Jul 11 18:42:37 gentoo tap-udp-p2mp[13661]: Incoming Data Channel: Cipher 'CAMELLIA-128-CBC' initialized with 128 bit key
Jul 11 18:42:38 gentoo tap-udp-p2mp[13661]: SENT CONTROL [freebsd-74-amd64]: 'PUSH_REPLY,...,peer-id 2,cipher CAMELLIA-128-CBC' (status=1)

(if I put "CAMELLIA and some of the AES-GCM variants" in there, I get the standard AES-256-GCM or AES-128-GCM variants - with no indication in the logs on why it doesn't want to take the cipher --> documenting this here, so it can be found by googling: if you want "cipher" to work in CCD/ files,

cipher only sets the fallback cipher if we find no common cipher. All ciphers in ncp-ciphers are still preferred to cipher. So to have the server pick the --cipher from the either general config or ccd config, none of the cipher in ncp-ciphers may be supported by the peer (so not in ncp-ciphers/ncp-ciphers and not as --cipher)

Arne

#56 lore raw

Hi,

Quoted 8 lines · Arne Schwabe
Quoted 8 lines

With the patch *and* forcing NCP on the server side by only allowing CAMELLIA-128-CBC:

  $ cat ccd/freebsd-74-amd64
  ncp-ciphers CAMELLIA-128-CBC
  cipher CAMELLIA-128-CBC

it will actually do that:

[..]

Quoted 6 lines

cipher only sets the fallback cipher if we find no common cipher. All ciphers in ncp-ciphers are still preferred to cipher. So to have the server pick the --cipher from the either general config or ccd config, none of the cipher in ncp-ciphers may be supported by the peer (so not in ncp-ciphers/ncp-ciphers and not as --cipher)

More details on the scenario:

The client here is a stock 2.4 client, with "nothing" in the config - so it sends IV_NCP=1, but no cipher list, and OCC cipher is "bf-cbc".

In ccd/, if I have *just* "ncp-ciphers CAMELLIA-128-CBC", it will actually fallback to "bf-cbc". Which matches your description: no common ciphers (IV_NCP=1 = AES-128-GCM:AES-256-GCM) -> fallback cipher (bf-cbc).

So, shorter: you're right :-) - and if we want to force a cipher for a NCP-capable client, it needs "cipher" *and* "ncp-ciphers" in ccd/, because otherwise NCP will just override our config.

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]...
#57 lore raw
In reply to Arne Schwabe

grammar:

Quoted 2 lines · Arne Schwabe

This allows to control the fallback cipher that is used when the client/server do have any common cipher on a per client basis.

client/server do not have any common cipher

Quoted 7 lines

The patch is similar to Steffan's
[PATCH v4] Allow changing cipher from a ccd file.

Steffan's old patch also moves the cipher negotiation to multi_established_connection() which I independently discovered and implemented in

implemented in:

(otherwise it looks like you forgot "in what" - That is if my interpretation is correct)

Quoted 37 lines

Extract process_incoming_push_reply from process_incoming_push_msg (#FIXME add commitsh when commited to master)

---
  src/openvpn/options.c | 2 +-
  src/openvpn/options.h | 2 +-
  2 files changed, 2 insertions(+), 2 deletions(-)
src/openvpn/options.c +1 -1
index b93fd4fe..bf2760e1 100644
@@ -7892,7 +7892,7 @@ add_option(struct options *options,
      }
      else if (streq(p[0], "cipher") && p[1] && !p[2])
      {
-        VERIFY_PERMISSION(OPT_P_NCP);
+        VERIFY_PERMISSION(OPT_P_NCP|OPT_P_INSTANCE);
          options->ciphername = p[1];
      }
      else if (streq(p[0], "ncp-ciphers") && p[1] && !p[2])
src/openvpn/options.h +1 -1
index c83a46aa..c37006d3 100644
@@ -677,7 +677,7 @@ struct options
  #define OPT_P_MTU             (1<<14) /* TODO */
  #define OPT_P_NICE            (1<<15)
  #define OPT_P_PUSH            (1<<16)
-#define OPT_P_INSTANCE        (1<<17)
+#define OPT_P_INSTANCE        (1<<17) /**< allowed in ccd, client-connect etc*/
  #define OPT_P_CONFIG          (1<<18)
  #define OPT_P_EXPLICIT_NOTIFY (1<<19)
  #define OPT_P_ECHO            (1<<20)
#58 lore raw

Hi,

Quoted 7 lines · tincanteksup

grammar:

Quoted 2 lines · Arne Schwabe

This allows to control the fallback cipher that is used when the client/server do have any common cipher on a per client basis.

client/server do not have any common cipher

Too late, that patch went in yesterday already.

Quoted 2 lines

(otherwise it looks like you forgot "in what" - That is if my interpretation is correct)

It's missing the commit ID here, which I added :-) - so the commit message is a bit more complete.

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]...