From patchwork Mon Jan 25 01:56:23 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arne Schwabe X-Patchwork-Id: 1570 Return-Path: Delivered-To: patchwork@openvpn.net Delivered-To: patchwork@openvpn.net Received: from director14.mail.ord1d.rsapps.net ([172.27.255.1]) by backend30.mail.ord1d.rsapps.net with LMTP id SAlGEzTADmBPRgAAIUCqbw (envelope-from ) for ; Mon, 25 Jan 2021 07:57:24 -0500 Received: from proxy9.mail.iad3a.rsapps.net ([172.27.255.1]) by director14.mail.ord1d.rsapps.net with LMTP id GCsYEzTADmDDOAAAeJ7fFg (envelope-from ) for ; Mon, 25 Jan 2021 07:57:24 -0500 Received: from smtp7.gate.iad3a ([172.27.255.1]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) by proxy9.mail.iad3a.rsapps.net with LMTPS id KG5tCzTADmBUfAAAGuSQww (envelope-from ) for ; Mon, 25 Jan 2021 07:57:24 -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: smtp7.gate.iad3a.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: ddd51a16-5f0c-11eb-8d4b-525400bbebb8-1-1 Received: from [216.105.38.7] ([216.105.38.7:36906] helo=lists.sourceforge.net) by smtp7.gate.iad3a.rsapps.net (envelope-from ) (ecelerity 4.2.38.62370 r(:)) with ESMTPS (cipher=DHE-RSA-AES256-GCM-SHA384) id 8A/46-07439-330CE006; Mon, 25 Jan 2021 07:57:23 -0500 Received: from [127.0.0.1] (helo=sfs-ml-4.v29.lw.sourceforge.com) by sfs-ml-4.v29.lw.sourceforge.com with esmtp (Exim 4.90_1) (envelope-from ) id 1l41QE-0001lv-5L; Mon, 25 Jan 2021 12:56:46 +0000 Received: from [172.30.20.202] (helo=mx.sourceforge.net) by sfs-ml-4.v29.lw.sourceforge.com with esmtps (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.90_1) (envelope-from ) id 1l41QC-0001li-2a for openvpn-devel@lists.sourceforge.net; Mon, 25 Jan 2021 12:56:44 +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=ZqO1RGit4BRcaSI1bd1kzqk5uBPNI0zggKdfvfPJ4uc=; b=LAoS3b6/zZVIJoRv/eMfe6eezQ x19PygfAzOC77fmDbr7mOauo/Wuej2PAMdX8QbS+pY0XHWJPyy5FsHYNqHW6/eMIehXx4FJIbFc5f IUuyJvxb5SIMpBicWGn6pdFS1JIR/wT37KX0TksGXCwN4TYwF8FlaIo9jHpo/WSvfLqk=; 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=ZqO1RGit4BRcaSI1bd1kzqk5uBPNI0zggKdfvfPJ4uc=; b=ZcnaMi2R01w/OZPEYzg4NfQ5UE wacOSNf3nZD59oj5mn0ccljSej148yPP6xWG7Nw6bQpSDFGeUQWObhd3cQk3Tfa7XBQCOFk80NtoQ ln6rWryarfYSUHtumyP/L+6wLfPSiKRTQEJAdqe9099GssSMCLRQAmFv7Lhu48Wp4dPA=; Received: from mail.blinkt.de ([192.26.174.232]) by sfi-mx-1.v28.lw.sourceforge.com with esmtps (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.92.2) id 1l41Q4-002esp-0V for openvpn-devel@lists.sourceforge.net; Mon, 25 Jan 2021 12:56:43 +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 1l41Px-0007X6-2Y for openvpn-devel@lists.sourceforge.net; Mon, 25 Jan 2021 13:56:29 +0100 Received: (nullmailer pid 30426 invoked by uid 10006); Mon, 25 Jan 2021 12:56:29 -0000 From: Arne Schwabe To: openvpn-devel@lists.sourceforge.net Date: Mon, 25 Jan 2021 13:56:23 +0100 Message-Id: <20210125125628.30364-7-arne@rfc2549.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210125125628.30364-1-arne@rfc2549.org> References: <20210125125628.30364-1-arne@rfc2549.org> X-Spam-Report: Spam Filtering performed by mx.sourceforge.net. See http://spamassassin.org/tag/ for more details. 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: 1l41Q4-002esp-0V Subject: [Openvpn-devel] [PATCH v2 06/11] Add S_EXITCODE flag for openvpn_run_script to report exit code 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 allows to use script that have more than just fail/sucess but also deferred as status Patch v2: minor style fixes, improve doxygen comments Signed-off-by: Arne Schwabe Acked-by: Lev Stipakov --- src/openvpn/platform.c | 34 ++++++++++++++++++++++++++++++++++ src/openvpn/platform.h | 5 ++++- src/openvpn/run_command.c | 25 ++++++++++++++++--------- src/openvpn/run_command.h | 15 +++++++++++---- 4 files changed, 65 insertions(+), 14 deletions(-) diff --git a/src/openvpn/platform.c b/src/openvpn/platform.c index 53d07f9c..ef688c23 100644 --- a/src/openvpn/platform.c +++ b/src/openvpn/platform.c @@ -240,6 +240,40 @@ platform_system_ok(int stat) #endif } +#ifdef _WIN32 +int +platform_ret_code(int stat) +{ + if (stat >= 0 && stat < 255) + { + return stat; + } + else + { + return -1; + } +} +#else +int +platform_ret_code(int stat) +{ + if (!WIFEXITED(stat) || stat == -1) + { + return -1; + } + + int status = WEXITSTATUS(stat); + if (status >= 0 && status < 255) + { + return status; + } + else + { + return -1; + } +} +#endif + int platform_access(const char *path, int mode) { diff --git a/src/openvpn/platform.h b/src/openvpn/platform.h index 091fc9c4..01f3200c 100644 --- a/src/openvpn/platform.h +++ b/src/openvpn/platform.h @@ -119,9 +119,12 @@ void platform_mlockall(bool print_msg); /* Disable paging */ int platform_chdir(const char *dir); -/* interpret the status code returned by execve() */ +/** interpret the status code returned by execve() */ bool platform_system_ok(int stat); +/** Return an exit code if valid and between 0 and 255, -1 otherwise */ +int platform_ret_code(int stat); + int platform_access(const char *path, int mode); void platform_sleep_milliseconds(unsigned int n); diff --git a/src/openvpn/run_command.c b/src/openvpn/run_command.c index 4c4adf97..8f99ec88 100644 --- a/src/openvpn/run_command.c +++ b/src/openvpn/run_command.c @@ -191,27 +191,34 @@ openvpn_execve(const struct argv *a, const struct env_set *es, const unsigned in /* * Wrapper around openvpn_execve */ -bool +int openvpn_execve_check(const struct argv *a, const struct env_set *es, const unsigned int flags, const char *error_message) { struct gc_arena gc = gc_new(); const int stat = openvpn_execve(a, es, flags); int ret = false; - if (platform_system_ok(stat)) + if (flags & S_EXITCODE) + { + ret = platform_ret_code(stat); + if (ret != -1) { + goto done; + } + } + else if (platform_system_ok(stat)) { ret = true; + goto done; } - else + if (error_message) { - if (error_message) - { - msg(((flags & S_FATAL) ? M_FATAL : M_WARN), "%s: %s", - error_message, - system_error_message(stat, &gc)); - } + msg(((flags & S_FATAL) ? M_FATAL : M_WARN), "%s: %s", + error_message, + system_error_message(stat, &gc)); } +done: gc_free(&gc); + return ret; } diff --git a/src/openvpn/run_command.h b/src/openvpn/run_command.h index 7ccb13c6..da33d92b 100644 --- a/src/openvpn/run_command.h +++ b/src/openvpn/run_command.h @@ -42,18 +42,25 @@ int script_security(void); void script_security_set(int level); /* openvpn_execve flags */ -#define S_SCRIPT (1<<0) -#define S_FATAL (1<<1) +#define S_SCRIPT (1<<0) +#define S_FATAL (1<<1) +/** Instead of returning 1/0 for success/fail, + * return exit code when between 0 and 255 and -1 otherwise */ +#define S_EXITCODE (1<<2) /* wrapper around the execve() call */ int openvpn_popen(const struct argv *a, const struct env_set *es); bool openvpn_execve_allowed(const unsigned int flags); -bool openvpn_execve_check(const struct argv *a, const struct env_set *es, +int openvpn_execve_check(const struct argv *a, const struct env_set *es, const unsigned int flags, const char *error_message); -static inline bool +/** + * Will run a script and return the exit code of the script if between + * 0 and 255, -1 otherwise + */ +static inline int openvpn_run_script(const struct argv *a, const struct env_set *es, const unsigned int flags, const char *hook) {