From patchwork Wed Sep 30 03:13:12 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arne Schwabe X-Patchwork-Id: 1490 Return-Path: Delivered-To: patchwork@openvpn.net Delivered-To: patchwork@openvpn.net Received: from director7.mail.ord1d.rsapps.net ([172.31.255.6]) by backend30.mail.ord1d.rsapps.net with LMTP id 4H/5Mr6EdF8jDgAAIUCqbw (envelope-from ) for ; Wed, 30 Sep 2020 09:14:38 -0400 Received: from proxy19.mail.iad3b.rsapps.net ([172.31.255.6]) by director7.mail.ord1d.rsapps.net with LMTP id 6CfCMr6EdF8VFAAAovjBpQ (envelope-from ) for ; Wed, 30 Sep 2020 09:14:38 -0400 Received: from smtp27.gate.iad3b ([172.31.255.6]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) by proxy19.mail.iad3b.rsapps.net with LMTPS id APDELL6EdF/vVgAAIG4riQ (envelope-from ) for ; Wed, 30 Sep 2020 09:14:38 -0400 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: smtp27.gate.iad3b.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: e4130220-031e-11eb-ab3e-5254006b1ac1-1-1 Received: from [216.105.38.7] ([216.105.38.7:57204] helo=lists.sourceforge.net) by smtp27.gate.iad3b.rsapps.net (envelope-from ) (ecelerity 4.2.38.62370 r(:)) with ESMTPS (cipher=DHE-RSA-AES256-GCM-SHA384) id 92/6D-24529-EB4847F5; Wed, 30 Sep 2020 09:14:38 -0400 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 1kNbvL-0005ee-6b; Wed, 30 Sep 2020 13:13:35 +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 1kNbvJ-0005eN-I7 for openvpn-devel@lists.sourceforge.net; Wed, 30 Sep 2020 13:13:33 +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=wCASOwRGG+xlfxRVRXQdRIyd+zrI76fEbvkzCCuLYhk=; b=et5nZwoCocPYksIvOz9eoBAqht ovDzLPeOyj4RUT9DgL/29YOhgLPE/Yx6m7SLfefzZrvDZqfzWv6HIr/8viweRDnjQQvbuOqrrlKDQ 9cLsTYilosIOLMydcmRml+uzTV4UBqVmeKJXENlo34P75P+ZBzLCrkokIY0q+70QU17w=; 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=wCASOwRGG+xlfxRVRXQdRIyd+zrI76fEbvkzCCuLYhk=; b=CTri9bDsgFtVGfE6DM/KIIG6de tAUTqR4ewBhFJqiq7dgPJlcJ+Lam1TeGW5obkQ+vjVBP2UrI8kVvSlnxkV2Je9aVM78mdVXLcGJuC +I3JF/UVug0z7TC+bp89vdb1vSN/HS4ITqHgM74G88Q1oVbI0766KEzPZ92MjYL8HrC4=; Received: from mail.blinkt.de ([192.26.174.232]) by sfi-mx-3.v28.lw.sourceforge.com with esmtps (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.92.2) id 1kNbvF-00BNbA-RF for openvpn-devel@lists.sourceforge.net; Wed, 30 Sep 2020 13:13:33 +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 1kNbv4-0003o3-Jd for openvpn-devel@lists.sourceforge.net; Wed, 30 Sep 2020 15:13:18 +0200 Received: (nullmailer pid 1370 invoked by uid 10006); Wed, 30 Sep 2020 13:13:18 -0000 From: Arne Schwabe To: openvpn-devel@lists.sourceforge.net Date: Wed, 30 Sep 2020 15:13:12 +0200 Message-Id: <20200930131317.1299-8-arne@rfc2549.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200930131317.1299-1-arne@rfc2549.org> References: <20200930131317.1299-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: 1kNbvF-00BNbA-RF Subject: [Openvpn-devel] [PATCH 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 Signed-off-by: Arne Schwabe Acked-by: Lev Stipakov --- src/openvpn/platform.c | 35 +++++++++++++++++++++++++++++++++++ src/openvpn/platform.h | 5 ++++- src/openvpn/run_command.c | 25 ++++++++++++++++--------- src/openvpn/run_command.h | 14 ++++++++++---- 4 files changed, 65 insertions(+), 14 deletions(-) diff --git a/src/openvpn/platform.c b/src/openvpn/platform.c index 53d07f9c..5545a133 100644 --- a/src/openvpn/platform.c +++ b/src/openvpn/platform.c @@ -240,6 +240,41 @@ 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..c7f6aae1 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 a return 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..c1dd102c 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..e322ebd1 100644 --- a/src/openvpn/run_command.h +++ b/src/openvpn/run_command.h @@ -42,18 +42,24 @@ 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 if it + */ +static inline int openvpn_run_script(const struct argv *a, const struct env_set *es, const unsigned int flags, const char *hook) {