From patchwork Tue Nov 20 23:10:13 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arne Schwabe X-Patchwork-Id: 620 X-Patchwork-Delegate: a@unstable.cc 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 aOJ/F6lQ9VsKSAAAIUCqbw for ; Wed, 21 Nov 2018 07:33:45 -0500 Received: from proxy15.mail.ord1d.rsapps.net ([172.30.191.6]) by director9.mail.ord1d.rsapps.net with LMTP id kDBkF6lQ9Vt3TAAAalYnBA ; Wed, 21 Nov 2018 07:33:45 -0500 Received: from smtp3.gate.ord1d ([172.30.191.6]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) by proxy15.mail.ord1d.rsapps.net with LMTP id KA41F6lQ9VthDQAAAY1PeQ ; Wed, 21 Nov 2018 07:33:45 -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: smtp3.gate.ord1d.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: af80cdfa-ed89-11e8-a0cf-5254006d4589-1-1 Received: from [216.105.38.7] ([216.105.38.7:28283] helo=lists.sourceforge.net) by smtp3.gate.ord1d.rsapps.net (envelope-from ) (ecelerity 4.2.38.62370 r(:)) with ESMTPS (cipher=DHE-RSA-AES256-GCM-SHA384) id A9/34-05889-8A055FB5; Wed, 21 Nov 2018 07:33:45 -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 1gPRgZ-0003gc-RB; Wed, 21 Nov 2018 12:32:51 +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 1gPRgY-0003gO-Jv for openvpn-devel@lists.sourceforge.NET; Wed, 21 Nov 2018 12:32:50 +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:Cc: To:From:Sender:Reply-To: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=gyfWjsg4o3F+qYLmtgEzfv9kGT2HnB9TnQ7CXvi0f3o=; b=EaIAynsTXl0nHkVU1CzUlIAFu/ 1U5K7Xw6swW0ulKLd8gX45rnHozq7clUOsi9sJJpdgicEa7Ex/EwAWPt5gVlJ+RBHvpKtYSvxakBq CKyYOQegvqfu49ANVFdlvL2w/ifV9XlgMke7MNAgjNYDlUFBSGa5wYgnzNwhiHgtq0Os=; 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:Cc:To:From:Sender:Reply-To :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=gyfWjsg4o3F+qYLmtgEzfv9kGT2HnB9TnQ7CXvi0f3o=; b=U49qcxK2i8oMxe3U3VK8DU589j TkUdvvPevJCANL21rSxzw1tEW4lDQTOAutCg0AcVgRSA2ti5G7XuiWUzvmVEFkJ0fb/k29JHroInA KVLo2uawLXi9JoWaXJsiJ5TQud3GvgXNfrs/WwWmD9oj25u9QhrDKnOZOusQuGQZHLcg=; 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.90_1) id 1gPRgJ-008sMu-Ca for openvpn-devel@lists.sourceforge.NET; Wed, 21 Nov 2018 12:32:50 +0000 Received: from kamera.blinkt.de ([2001:638:502:390:20c:29ff:fec8:535c]) by mail.blinkt.de with smtp (Exim 4.91 (FreeBSD)) (envelope-from ) id 1gPPSe-000GWg-Ig; Wed, 21 Nov 2018 11:10:20 +0100 Received: (nullmailer pid 1867 invoked by uid 10006); Wed, 21 Nov 2018 10:10:20 -0000 From: Arne Schwabe To: openvpn-devel@lists.sourceforge.net Date: Wed, 21 Nov 2018 11:10:13 +0100 Message-Id: <20181121101019.1801-8-arne@rfc2549.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20181121101019.1801-1-arne@rfc2549.org> References: <20181121101019.1801-1-arne@rfc2549.org> X-Spam-Report: Spam Filtering performed by mx.sourceforge.net. See http://spamassassin.org/tag/ for more details. 0.0 HEADER_FROM_DIFFERENT_DOMAINS From and EnvelopeFrom 2nd level mail domains are different X-Headers-End: 1gPRgJ-008sMu-Ca Subject: [Openvpn-devel] [PATCH v4 07/13] client-connect: Add CC_RET_DEFERRED and cope with deferred client-connect 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 From: Fabian Knittel 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. Signed-off-by: Fabian Knittel 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. Signed-off-by: Arne Schwabe --- src/openvpn/multi.c | 159 +++++++++++++++++++++++++++++++++----------- src/openvpn/multi.h | 18 ++++- 2 files changed, 137 insertions(+), 40 deletions(-) diff --git a/src/openvpn/multi.c b/src/openvpn/multi.c index 1093d8aa..e333476d 100644 --- a/src/openvpn/multi.c +++ b/src/openvpn/multi.c @@ -2107,28 +2107,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 (*client_connect_handler) + (struct multi_context *m, struct multi_instance *mi, + unsigned int *option_types_found); + +struct client_connect_handlers +{ + client_connect_handler main; + 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 + }, { - ASSERT(0); + .main = multi_client_connect_call_script, + .deferred = multi_client_connect_fail + }, + { + .main = multi_client_connect_mda, + .deferred = multi_client_connect_fail + }, + { + .main = NULL, + .deferred = NULL + /* End of list sentinel. */ } -} +}; /* * Called as soon as the SSL/TLS connection authenticates. @@ -2145,32 +2168,86 @@ multi_connection_established(struct multi_context *m, struct multi_instance *mi) if (tls_authentication_status(mi->context.c2.tls_multi, 0) == TLS_AUTHENTICATION_SUCCEEDED) { - typedef enum client_connect_return - (*multi_client_connect_handler) - (struct multi_context *m, struct multi_instance *mi, - unsigned int *option_types_found); + bool from_deferred; - 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 - }; + enum client_connect_return ret; - unsigned int option_types_found = 0; + struct client_connect_defer_state *defer_state = + &(mi->client_connect_defer_state); - int cc_succeeded = true; /* client connect script status */ - int cc_succeeded_count = 0; - enum client_connect_return ret; + /* We are called for the first time */ + if (mi->client_connect_status == CC_STATUS_NOT_ESTABLISHED) + { + defer_state->cur_handler_index = 0; + defer_state->option_types_found = 0; + /* Initially we have no handler that has returned a result */ + mi->client_connect_status = CC_STATUS_DEFERRED_NO_RESULT; + from_deferred = false; + } + else + { + from_deferred = true; + } multi_client_connect_early_setup(m, mi); - for (int i = 0; cc_succeeded && handlers[i]; i++) + bool cc_succeeded = true; + + while (cc_succeeded + && client_connect_handlers[defer_state->cur_handler_index] + .main != NULL) { - ret = handlers[i](m, mi, &option_types_found); - cc_succeeded = cc_check_return(&cc_succeeded_count, ret); + 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 defered state + */ + mi->client_connect_status = CC_STATUS_DEFERRED_RESULT; + } + 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)++; } /* @@ -2182,21 +2259,25 @@ 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, + mi->client_connect_defer_state. + option_types_found); + } else { + bool at_least_one_cc_succeeded = + (mi->client_connect_status == CC_STATUS_DEFERRED_RESULT); /* set context-level authentication flag */ mi->context.c2.context_auth = - cc_succeeded_count ? CAS_PARTIAL : CAS_FAILED; + at_least_one_cc_succeeded ? CAS_PARTIAL : CAS_FAILED; } - /* set flag so we don't get called again */ + /* set flag so we do not get called again */ mi->client_connect_status = CC_STATUS_ESTABLISHED; /* increment number of current authenticated clients */ diff --git a/src/openvpn/multi.h b/src/openvpn/multi.h index bd4882ea..252b1d3d 100644 --- a/src/openvpn/multi.h +++ b/src/openvpn/multi.h @@ -63,9 +63,24 @@ 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; +}; + enum client_connect_status { CC_STATUS_NOT_ESTABLISHED, + CC_STATUS_DEFERRED_NO_RESULT, + CC_STATUS_DEFERRED_RESULT, CC_STATUS_ESTABLISHED }; @@ -117,7 +132,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 @@ -204,6 +219,7 @@ enum client_connect_return { CC_RET_FAILED, CC_RET_SUCCEEDED, + CC_RET_DEFERRED, CC_RET_SKIPPED };