From patchwork Thu Mar 4 00:40:52 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arne Schwabe X-Patchwork-Id: 1605 X-Patchwork-Delegate: gert@greenie.muc.de Return-Path: Delivered-To: patchwork@openvpn.net Delivered-To: patchwork@openvpn.net Received: from director9.mail.ord1d.rsapps.net ([172.30.191.6]) by backend30.mail.ord1d.rsapps.net with LMTP id QFtNMH3HQGDPSAAAIUCqbw (envelope-from ) for ; Thu, 04 Mar 2021 06:41:49 -0500 Received: from proxy17.mail.ord1d.rsapps.net ([172.30.191.6]) by director9.mail.ord1d.rsapps.net with LMTP id WK0VMH3HQGAbaAAAalYnBA (envelope-from ) for ; Thu, 04 Mar 2021 06:41:49 -0500 Received: from smtp15.gate.ord1c ([172.30.191.6]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) by proxy17.mail.ord1d.rsapps.net with LMTPS id KI7dL33HQGAFXgAAWC7mWg (envelope-from ) for ; Thu, 04 Mar 2021 06:41:49 -0500 X-Spam-Threshold: 95 X-Spam-Score: 0 X-Spam-Flag: NO X-Virus-Scanned: OK X-Orig-To: openvpnslackdevel@openvpn.net X-Originating-Ip: [216.105.38.7] Authentication-Results: smtp15.gate.ord1c.rsapps.net; iprev=pass policy.iprev="216.105.38.7"; spf=pass smtp.mailfrom="openvpn-devel-bounces@lists.sourceforge.net" smtp.helo="lists.sourceforge.net"; dkim=fail (signature verification failed) header.d=sourceforge.net; dkim=fail (signature verification failed) header.d=sf.net; dmarc=none (p=nil; dis=none) header.from=rfc2549.org X-Suspicious-Flag: YES X-Classification-ID: 9b0287e2-7cde-11eb-a620-bc305bf03694-1-1 Received: from [216.105.38.7] ([216.105.38.7:54644] helo=lists.sourceforge.net) by smtp15.gate.ord1c.rsapps.net (envelope-from ) (ecelerity 4.2.38.62370 r(:)) with ESMTPS (cipher=DHE-RSA-AES256-GCM-SHA384) id 3B/63-23666-D77C0406; Thu, 04 Mar 2021 06:41:49 -0500 Received: from [127.0.0.1] (helo=sfs-ml-1.v29.lw.sourceforge.com) by sfs-ml-1.v29.lw.sourceforge.com with esmtp (Exim 4.90_1) (envelope-from ) id 1lHmLo-0003zE-Mk; Thu, 04 Mar 2021 11:41:04 +0000 Received: from [172.30.20.202] (helo=mx.sourceforge.net) by sfs-ml-1.v29.lw.sourceforge.com with esmtps (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lHmLn-0003ys-MD for openvpn-devel@lists.sourceforge.net; Thu, 04 Mar 2021 11:41:03 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sourceforge.net; s=x; h=References:In-Reply-To:Message-Id:Date:Subject:To: From:Sender:Reply-To:Cc:MIME-Version:Content-Type:Content-Transfer-Encoding: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=P91idZV4/11HmpUahSozOz9VRDg8c8spF9kjNIPszyQ=; b=cySXJb/TeT48bGZD1DtN2yq9Ym ZH3kAOdFWMjapUW3xej25CL6yakW+9GxxU366VgIx7coagL1MSFKdUFUCjmSuY0TCsC67FpdwjM8N Ric8W+MBKcifU1h6ycKJ92aVI46Qh6/Za1kAN5leLFsBM8MmrrxPQtfHSo7R+QuZtm0s=; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sf.net; s=x ; h=References:In-Reply-To:Message-Id:Date:Subject:To:From:Sender:Reply-To:Cc :MIME-Version:Content-Type:Content-Transfer-Encoding:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=P91idZV4/11HmpUahSozOz9VRDg8c8spF9kjNIPszyQ=; b=gBJJdxVYCwGVPrBYcGI0kzwzhy zZcAh1YFXMuQNSO8IxQWC06sipoGpu430FcfvI1fBQs9nix5uOTD3b3kN1eCn4sgxIP8ZU+C31iBz bXK0hO7jO6iK/MmoA2d4sCyoDxXpI6DoahWmhaD66Hse+nPfcwSw1eK6tEa1R5mv/JNM=; Received: from mail.blinkt.de ([192.26.174.232]) by sfi-mx-2.v28.lw.sourceforge.com with esmtps (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.92.3) id 1lHmLj-0004QX-DE for openvpn-devel@lists.sourceforge.net; Thu, 04 Mar 2021 11:41:03 +0000 Received: from kamera.blinkt.de ([2001:638:502:390:20c:29ff:fec8:535c]) by mail.blinkt.de with smtp (Exim 4.94 (FreeBSD)) (envelope-from ) id 1lHmLd-000NYD-0E for openvpn-devel@lists.sourceforge.net; Thu, 04 Mar 2021 12:40:53 +0100 Received: (nullmailer pid 8621 invoked by uid 10006); Thu, 04 Mar 2021 11:40:52 -0000 From: Arne Schwabe To: openvpn-devel@lists.sourceforge.net Date: Thu, 4 Mar 2021 12:40:52 +0100 Message-Id: <20210304114052.8577-1-arne@rfc2549.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210226134829.3589-1-arne@rfc2549.org> References: <20210226134829.3589-1-arne@rfc2549.org> X-Spam-Report: Spam Filtering performed by mx.sourceforge.net. See http://spamassassin.org/tag/ for more details. 0.0 URIBL_BLOCKED ADMINISTRATOR NOTICE: The query to URIBL was blocked. See http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more information. [URIs: rfc2549.org] 0.2 HEADER_FROM_DIFFERENT_DOMAINS From and EnvelopeFrom 2nd level mail domains are different 0.0 SPF_NONE SPF: sender does not publish an SPF Record 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record X-Headers-End: 1lHmLj-0004QX-DE Subject: [Openvpn-devel] [PATCH v4] Implement deferred auth for scripts X-BeenThere: openvpn-devel@lists.sourceforge.net X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: openvpn-devel-bounces@lists.sourceforge.net X-getmail-retrieved-from-mailbox: Inbox This patch also refactors the if condition that checks the result of the authentication since that has become quite unreadable. It renames s1/s2 and extracts some parts of the condition into individual variables to make the condition better understandle Patch v2: add refactoring of the if condition Patch v4: fix documentation not mentioning method as 2nd line Signed-off-by: Arne Schwabe Acked-By: David Sommerseth --- Changes.rst | 12 +++++ doc/man-sections/script-options.rst | 14 ++++- src/openvpn/ssl.c | 2 - src/openvpn/ssl_verify.c | 84 +++++++++++++++++++++-------- 4 files changed, 87 insertions(+), 25 deletions(-) diff --git a/Changes.rst b/Changes.rst index 62008e8d..74728d2a 100644 --- a/Changes.rst +++ b/Changes.rst @@ -15,6 +15,17 @@ Compatibility with OpenSSL in FIPS mode requirements/recommendation of FIPS 140-2. This just allows OpenVPN to be run on a system that be configured OpenSSL in FIPS mode. +Deferred auth support for scripts + The ``--auth-user-pass-verify`` script supports now deferred authentication. + +Pending auth support for plugins and scripts + Both auth plugin and script can now signal pending authentication to + the client when using deferred authentication. The new ``client-crresponse`` + script option and ``OPENVPN_PLUGIN_CLIENT_CRRESPONSE`` plugin function can + be used to parse a client response to a ``CR_TEXT`` two factor challenge. + + See ``sample/sample-scripts/totpauth.py`` for an example. + Deprecated features ------------------- ``inetd`` has been removed @@ -22,6 +33,7 @@ Deprecated features and TAP mode only. + Overview of changes in 2.5 ========================== diff --git a/doc/man-sections/script-options.rst b/doc/man-sections/script-options.rst index 03b3dd77..22990f4f 100644 --- a/doc/man-sections/script-options.rst +++ b/doc/man-sections/script-options.rst @@ -90,7 +90,19 @@ SCRIPT HOOKS The script should examine the username and password, returning a success exit code (:code:`0`) if the client's authentication request is to be - accepted, or a failure code (:code:`1`) to reject the client. + accepted, a failure code (:code:`1`) to reject the client, or a that + the authentication is deferred (:code:`2`). If the authentication is + deferred, the script must fork/start a background or another non-blocking + operation to continue the authentication in the background. When finshing + the authentication, a :code:`1` or :code:`0` must be written to the + file specified by the :code:`auth_control_file`. + + When deferred authentication is in use, the script can also request + pending authentication by writing to the file specified by the + :code:`auth_pending_file`. The first line must be the timeout in + seconds, the required method on the second line (e.g. crtext) and + third line must be the EXTRA as documented in the + ``client-pending-auth`` section of `doc/management.txt`. This directive is designed to enable a plugin-style interface for extending OpenVPN's authentication capabilities. diff --git a/src/openvpn/ssl.c b/src/openvpn/ssl.c index f958a605..51164a66 100644 --- a/src/openvpn/ssl.c +++ b/src/openvpn/ssl.c @@ -992,9 +992,7 @@ key_state_free(struct key_state *ks, bool clear) packet_id_free(&ks->crypto_options.packet_id); -#ifdef PLUGIN_DEF_AUTH key_state_rm_auth_control_files(ks); -#endif if (clear) { diff --git a/src/openvpn/ssl_verify.c b/src/openvpn/ssl_verify.c index b9647c1a..b59a0230 100644 --- a/src/openvpn/ssl_verify.c +++ b/src/openvpn/ssl_verify.c @@ -1188,14 +1188,14 @@ tls_authenticate_key(struct tls_multi *multi, const unsigned int mda_key_id, con /* * Verify the user name and password using a script */ -static bool +static int verify_user_pass_script(struct tls_session *session, struct tls_multi *multi, const struct user_pass *up) { struct gc_arena gc = gc_new(); struct argv argv = argv_new(); const char *tmp_file = ""; - bool ret = false; + int retval = OPENVPN_PLUGIN_FUNC_ERROR; /* Set environmental variables prior to calling script */ setenv_str(session->opt->es, "script_type", "user-pass-verify"); @@ -1223,25 +1223,59 @@ verify_user_pass_script(struct tls_session *session, struct tls_multi *multi, /* pass temp file name to script */ argv_printf_cat(&argv, "%s", tmp_file); } - else - { - msg(D_TLS_ERRORS, "TLS Auth Error: could not create write " - "username/password to temp file"); - } } else { + setenv_str(session->opt->es, "username", up->username); setenv_str(session->opt->es, "password", up->password); } + /* generate filename for deferred auth control file */ + if (!key_state_gen_auth_control_files(&session->key[KS_PRIMARY], + session->opt)) + { + msg(D_TLS_ERRORS, "TLS Auth Error (%s): " + "could not create deferred auth control file", __func__); + return OPENVPN_PLUGIN_FUNC_ERROR; + } + /* call command */ - ret = openvpn_run_script(&argv, session->opt->es, 0, - "--auth-user-pass-verify"); + int script_ret = openvpn_run_script(&argv, session->opt->es, S_EXITCODE, + "--auth-user-pass-verify"); + switch (script_ret) + { + case 0: + retval = OPENVPN_PLUGIN_FUNC_SUCCESS; + break; + case 2: + retval = OPENVPN_PLUGIN_FUNC_DEFERRED; + break; + default: + retval = OPENVPN_PLUGIN_FUNC_ERROR; + break; + } + if (retval == OPENVPN_PLUGIN_FUNC_DEFERRED) + { + /* Check if we the plugin has written the pending auth control + * file and send the pending auth to the client */ + if(!key_state_check_auth_pending_file(&session->key[KS_PRIMARY], + multi)) + { + retval = OPENVPN_PLUGIN_FUNC_ERROR; + key_state_rm_auth_control_files(&session->key[KS_PRIMARY]); + } + } + else + { + /* purge auth control filename (and file itself) for non-deferred returns */ + key_state_rm_auth_control_files(&session->key[KS_PRIMARY]); + } if (!session->opt->auth_user_pass_verify_script_via_file) { setenv_del(session->opt->es, "password"); } + done: if (tmp_file && strlen(tmp_file) > 0) { @@ -1250,7 +1284,7 @@ done: argv_free(&argv); gc_free(&gc); - return ret; + return retval; } /* @@ -1371,8 +1405,6 @@ void verify_user_pass(struct user_pass *up, struct tls_multi *multi, struct tls_session *session) { - int s1 = OPENVPN_PLUGIN_FUNC_SUCCESS; - bool s2 = true; struct key_state *ks = &session->key[KS_PRIMARY]; /* primary key */ #ifdef ENABLE_MANAGEMENT @@ -1432,30 +1464,32 @@ verify_user_pass(struct user_pass *up, struct tls_multi *multi, } } + int plugin_status = OPENVPN_PLUGIN_FUNC_SUCCESS; + int script_status = OPENVPN_PLUGIN_FUNC_SUCCESS; /* Set the environment variables used by all auth variants */ if (!set_verify_user_pass_env(up, multi, session)) { skip_auth = true; - s1 = OPENVPN_PLUGIN_FUNC_ERROR; + plugin_status = OPENVPN_PLUGIN_FUNC_ERROR; } /* call plugin(s) and/or script */ if (!skip_auth) { #ifdef ENABLE_MANAGEMENT - if (man_def_auth==KMDA_DEF) + if (man_def_auth == KMDA_DEF) { man_def_auth = verify_user_pass_management(session, multi, up); } #endif if (plugin_defined(session->opt->plugins, OPENVPN_PLUGIN_AUTH_USER_PASS_VERIFY)) { - s1 = verify_user_pass_plugin(session, multi, up); + plugin_status = verify_user_pass_plugin(session, multi, up); } if (session->opt->auth_user_pass_verify_script) { - s2 = verify_user_pass_script(session, multi, up); + script_status = verify_user_pass_script(session, multi, up); } } @@ -1466,19 +1500,25 @@ verify_user_pass(struct user_pass *up, struct tls_multi *multi, msg(D_TLS_ERRORS, "TLS Auth Error: --username-as-common name specified and username is longer than the maximum permitted Common Name length of %d characters", TLS_USERNAME_LEN); - s1 = OPENVPN_PLUGIN_FUNC_ERROR; + plugin_status = OPENVPN_PLUGIN_FUNC_ERROR; + script_status = OPENVPN_PLUGIN_FUNC_ERROR; } /* auth succeeded? */ - if ((s1 == OPENVPN_PLUGIN_FUNC_SUCCESS - || s1 == OPENVPN_PLUGIN_FUNC_DEFERRED - ) && s2 + bool plugin_ok = plugin_status == OPENVPN_PLUGIN_FUNC_SUCCESS + || plugin_status == OPENVPN_PLUGIN_FUNC_DEFERRED; + + bool script_ok = script_status == OPENVPN_PLUGIN_FUNC_SUCCESS + || script_status == OPENVPN_PLUGIN_FUNC_DEFERRED; + + if (script_ok && plugin_ok && tls_lock_username(multi, up->username) #ifdef ENABLE_MANAGEMENT && man_def_auth != KMDA_ERROR #endif - && tls_lock_username(multi, up->username)) + ) { ks->authenticated = KS_AUTH_TRUE; - if (s1 == OPENVPN_PLUGIN_FUNC_DEFERRED) + if (plugin_status == OPENVPN_PLUGIN_FUNC_DEFERRED + || script_status == OPENVPN_PLUGIN_FUNC_DEFERRED) { ks->authenticated = KS_AUTH_DEFERRED; }