From patchwork Mon May 10 21:57:39 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lev Stipakov X-Patchwork-Id: 1800 Return-Path: Delivered-To: patchwork@openvpn.net Delivered-To: patchwork@openvpn.net Received: from director12.mail.ord1d.rsapps.net ([172.30.191.6]) by backend30.mail.ord1d.rsapps.net with LMTP id brSAM0I5mmCCPQAAIUCqbw (envelope-from ) for ; Tue, 11 May 2021 03:58:58 -0400 Received: from proxy2.mail.ord1d.rsapps.net ([172.30.191.6]) by director12.mail.ord1d.rsapps.net with LMTP id gA7QMkI5mmDFSAAAIasKDg (envelope-from ) for ; Tue, 11 May 2021 03:58:58 -0400 Received: from smtp18.gate.ord1c ([172.30.191.6]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) by proxy2.mail.ord1d.rsapps.net with LMTPS id GGt3MkI5mmALHQAAfawv4w (envelope-from ) for ; Tue, 11 May 2021 03:58:58 -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: smtp18.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; dkim=fail (signature verification failed) header.d=gmail.com; dmarc=fail (p=none; dis=none) header.from=gmail.com X-Suspicious-Flag: YES X-Classification-ID: bd5d7538-b22e-11eb-8352-bc305bf00c68-1-1 Received: from [216.105.38.7] ([216.105.38.7:48060] helo=lists.sourceforge.net) by smtp18.gate.ord1c.rsapps.net (envelope-from ) (ecelerity 4.2.38.62370 r(:)) with ESMTPS (cipher=DHE-RSA-AES256-GCM-SHA384) id 65/EF-10648-2493A906; Tue, 11 May 2021 03:58:58 -0400 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 1lgNHK-0008Dc-9A; Tue, 11 May 2021 07:58:06 +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 1lgNHI-0008DM-D7 for openvpn-devel@lists.sourceforge.net; Tue, 11 May 2021 07:58:04 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sourceforge.net; s=x; h=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:In-Reply-To:References:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=FGUBxUJ8d/47AQ3X6lgYshskuAAt/1zsjkMDII20lKs=; b=PBm/Nga9WPY/yVBg3ts9wNhbQm /oXaUk1Zm3CCBsSbsqeA529KQxSlznW1jZlSmcuT6Xx88CyyMmSC8rFRoemi28iBEsA5rrkVO+bcP wLZSBgIUA80krGp0KbV4MBh8zYKvlZzQ74/4VdWsXrWkuzskYAD0f7sylz+4jBOacnXw=; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sf.net; s=x ; h=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: In-Reply-To:References:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=FGUBxUJ8d/47AQ3X6lgYshskuAAt/1zsjkMDII20lKs=; b=RygAIh2YRUN41sYuamFcq1dMZL 1iXge7SIu+J6nJOr0QTDxs/eXvUv9d9LWhB+AqqVcFkNIQX7cy+/TzEOjZDlH8vIBtuMVVKmz1+1V 3eitrcxMlrtusorGwE/pnWvkQe0f8ym3cAyCfB+Fh1tRw0b35W3ktSLtbZvDQscVnldU=; Received: from mail-wm1-f51.google.com ([209.85.128.51]) by sfi-mx-1.v28.lw.sourceforge.com with esmtps (TLSv1.2:ECDHE-RSA-AES128-GCM-SHA256:128) (Exim 4.92.2) id 1lgNHF-003Y8C-7t for openvpn-devel@lists.sourceforge.net; Tue, 11 May 2021 07:58:06 +0000 Received: by mail-wm1-f51.google.com with SMTP id a10-20020a05600c068ab029014dcda1971aso654565wmn.3 for ; Tue, 11 May 2021 00:58:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=FGUBxUJ8d/47AQ3X6lgYshskuAAt/1zsjkMDII20lKs=; b=Jv4KaF0b4GRD+T1fvG2Vf61fMqKtRMrYLTlZexWbMAKYxvGu8H/xtatLPuhHQ13pjj vKbIXYl3N+OZv2Nl1md4SopA39pxx7fzgjKVvYd8TZ3Sr5T4JH6CGtZ7btaR1CA+7T0L dSnWbIpCko1sbOyADexSldPEUi1LJIZk7/pglj/0W4PHFDmssoVA0Z0Cof/QO6tsK4/M tSMq9ukgyRD4YLxVBn3JaFS2KJs3xrCpPZl5EIQuKUz3+XkXpdemJ6Hvdg+T8Y1e9a0W zjIiMq2mjNCkIMWhQ1bU8QuIpXW1PLYtMk/W7PAYsWYrejstP1KkcKdTwR7Gjhyyw+D2 +ieQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=FGUBxUJ8d/47AQ3X6lgYshskuAAt/1zsjkMDII20lKs=; b=GrMROtdeu54oCZ43L8whbOU6bCN0Z5IDpAkv3BM1mttU5pSu05nNRd3aCQwJh1jmxb go9aEkudi1Om5zKguiO1yHaL2HwBNxIP9uN4G9fOnutwcjW5RDFSVr0hudEM8eh0Taps OX3ACRFRmqhfS9kK+q8XieRgc81uoHWC6CGdLcR9DWW5wYOxjrZOEjkbLLFMblk+/Rtb BigE8Qv7pmlJnNtdNPY6MxTT2ttoAh94vGXrqTOGVSqc7Cve0HlKugwFL2bhQbzLxZ3P HiPBIQ57eWpH4akOlsKDtAalFZGEu+gT/0U8MGxVlXoGaEtbdIkBZGEESFWmFklr+GWJ DUFA== X-Gm-Message-State: AOAM532w122wKNYF9EW0z7eQgGGMPRb9W3CGVOT3E4pDKoYaT+/OPVDB Hj/vvTUCjcWKLu4DmsB3j2mzx+iG3Pm4Wwj6 X-Google-Smtp-Source: ABdhPJylBtVoNrKoUMNyKsoHI5DYTIKqrR02mUQXh3+RPraSGCFhdOXxgS+FFk/lxFJYdbB66WRO8g== X-Received: by 2002:a05:600c:2102:: with SMTP id u2mr3926088wml.124.1620719874370; Tue, 11 May 2021 00:57:54 -0700 (PDT) Received: from LAPTOP-4L3N7KFS.localdomain (nat1.panoulu.net. [185.38.2.1]) by smtp.gmail.com with ESMTPSA id w5sm21449951wrm.10.2021.05.11.00.57.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 11 May 2021 00:57:53 -0700 (PDT) From: Lev Stipakov To: openvpn-devel@lists.sourceforge.net Date: Tue, 11 May 2021 10:57:39 +0300 Message-Id: <20210511075739.158-1-lstipakov@gmail.com> X-Mailer: git-send-email 2.17.1 X-Spam-Report: Spam Filtering performed by mx.sourceforge.net. See http://spamassassin.org/tag/ for more details. -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at https://www.dnswl.org/, no trust [209.85.128.51 listed in list.dnswl.org] 0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail provider (lstipakov[at]gmail.com) -0.0 RCVD_IN_MSPIKE_H2 RBL: Average reputation (+2) [209.85.128.51 listed in wl.mailspike.net] -0.0 SPF_PASS SPF: sender matches SPF record 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid X-Headers-End: 1lgNHF-003Y8C-7t Subject: [Openvpn-devel] [PATCH] openvpnmsica: properly schedule reboot in the end of installation 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: , Cc: Lev Stipakov MIME-Version: 1.0 Errors-To: openvpn-devel-bounces@lists.sourceforge.net X-getmail-retrieved-from-mailbox: Inbox From: Lev Stipakov A deferred custom action can not set the reboot status via MsiSetMode. To schedule reboot: - get user's temp directory in immediate custom action (EvaluateTUNTAPAdapters) which is called in the beginning of installation and pass it to the deffered action - in deferred action (ProcessDeferredAction) create a special empty file in user's temp directory if reboot is required - in the end of installation call immediate custom action (CheckAndScheduleReboot) which checks the existence of special file and uses MsiSetMode() to schedule reboot Signed-off-by: Lev Stipakov Acked-by: Gert Doering --- src/openvpnmsica/openvpnmsica.c | 90 ++++++++++++++++++++++++++++++++- src/openvpnmsica/openvpnmsica.h | 13 +++++ 2 files changed, 101 insertions(+), 2 deletions(-) diff --git a/src/openvpnmsica/openvpnmsica.c b/src/openvpnmsica/openvpnmsica.c index de1cf65c..96652117 100644 --- a/src/openvpnmsica/openvpnmsica.c +++ b/src/openvpnmsica/openvpnmsica.c @@ -59,6 +59,7 @@ #define MSICA_ADAPTER_TICK_SIZE (16*1024) /** Amount of tick space to reserve for one TAP/TUN adapter creation/deletition. */ +#define FILE_NEED_REBOOT L".ovpn_need_reboot" /** * Joins an argument sequence and sets it to the MSI property. @@ -956,6 +957,19 @@ cleanup_hRecord: } } + /* save path to user's temp dir to be used later by deferred actions */ + TCHAR tmpDir[MAX_PATH]; + GetTempPath(MAX_PATH, tmpDir); + + TCHAR str[MAX_PATH + 7]; + _stprintf_s(str, _countof(str), TEXT("tmpdir=%") TEXT(PRIsLPTSTR), tmpDir); + msica_arg_seq_add_tail(&seqInstall, str); + msica_arg_seq_add_tail(&seqInstallCommit, str); + msica_arg_seq_add_tail(&seqInstallRollback, str); + msica_arg_seq_add_tail(&seqUninstall, str); + msica_arg_seq_add_tail(&seqUninstallCommit, str); + msica_arg_seq_add_tail(&seqUninstallRollback, str); + /* Store deferred custom action parameters. */ if ((uiResult = setup_sequence(hInstall, TEXT("InstallTUNTAPAdapters" ), &seqInstall )) != ERROR_SUCCESS || (uiResult = setup_sequence(hInstall, TEXT("InstallTUNTAPAdaptersCommit" ), &seqInstallCommit )) != ERROR_SUCCESS @@ -1015,6 +1029,33 @@ parse_guid( } +/** + * Create empty file in user's temp directory. The existence of this file + * is checked in the end of installation by ScheduleReboot immediate custom action + * which schedules reboot. + * + * @param szTmpDir path to user's temp dirctory + * + */ +static void +CreateRebootFile(_In_z_ LPCWSTR szTmpDir) +{ + TCHAR path[MAX_PATH]; + swprintf_s(path, _countof(path), L"%s%s", szTmpDir, FILE_NEED_REBOOT); + + msg(M_WARN, "%s: Reboot required, create reboot indication file \"%" PRIsLPTSTR "\"", __FUNCTION__, path); + + HANDLE file = CreateFile(path, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); + if (file == INVALID_HANDLE_VALUE) + { + msg(M_NONFATAL | M_ERRNO, "%s: CreateFile(\"%" PRIsLPTSTR "\") failed", __FUNCTION__, path); + } + else + { + CloseHandle(file); + } +} + UINT __stdcall ProcessDeferredAction(_In_ MSIHANDLE hInstall) { @@ -1026,6 +1067,7 @@ ProcessDeferredAction(_In_ MSIHANDLE hInstall) UINT uiResult; BOOL bIsCoInitialized = SUCCEEDED(CoInitialize(NULL)); + WCHAR tmpDir[MAX_PATH] = {0}; OPENVPNMSICA_SAVE_MSI_SESSION(hInstall); @@ -1168,6 +1210,10 @@ ProcessDeferredAction(_In_ MSIHANDLE hInstall) } dwResult = tap_enable_adapter(NULL, &guid, FALSE, &bRebootRequired); } + else if (wcsncmp(szArg[i], L"tmpdir=", 7) == 0) + { + wcscpy_s(tmpDir, _countof(tmpDir), szArg[i] + 7); + } else { goto invalid_argument; @@ -1194,9 +1240,9 @@ invalid_argument: } cleanup: - if (bRebootRequired) + if (bRebootRequired && wcslen(tmpDir) > 0) { - MsiSetMode(hInstall, MSIRUNMODE_REBOOTATEND, TRUE); + CreateRebootFile(tmpDir); } MsiCloseHandle(hRecordProg); LocalFree(szArg); @@ -1209,3 +1255,43 @@ cleanup_CoInitialize: } return uiResult; } + +UINT __stdcall +CheckAndScheduleReboot(_In_ MSIHANDLE hInstall) +{ +#ifdef _MSC_VER +#pragma comment(linker, DLLEXP_EXPORT) +#endif + + debug_popup(TEXT(__FUNCTION__)); + + UINT ret = ERROR_SUCCESS; + BOOL bIsCoInitialized = SUCCEEDED(CoInitialize(NULL)); + + OPENVPNMSICA_SAVE_MSI_SESSION(hInstall); + + /* get user-specific temp path, to where we create reboot indication file */ + TCHAR tempPath[MAX_PATH]; + GetTempPath(MAX_PATH, tempPath); + + /* check if reboot file exists */ + TCHAR path[MAX_PATH]; + _stprintf_s(path, _countof(path), L"%s%s", tempPath, FILE_NEED_REBOOT); + WIN32_FIND_DATA data = { 0 }; + HANDLE searchHandle = FindFirstFile(path, &data); + if (searchHandle != INVALID_HANDLE_VALUE) + { + msg(M_WARN, "%s: Reboot file exists, schedule reboot", __FUNCTION__); + + FindClose(searchHandle); + DeleteFile(path); + + MsiSetMode(hInstall, MSIRUNMODE_REBOOTATEND, TRUE); + } + + if (bIsCoInitialized) + { + CoUninitialize(); + } + return ret; +} diff --git a/src/openvpnmsica/openvpnmsica.h b/src/openvpnmsica/openvpnmsica.h index 221d03ca..5af68470 100644 --- a/src/openvpnmsica/openvpnmsica.h +++ b/src/openvpnmsica/openvpnmsica.h @@ -146,6 +146,19 @@ EvaluateTUNTAPAdapters(_In_ MSIHANDLE hInstall); DLLEXP_DECL UINT __stdcall ProcessDeferredAction(_In_ MSIHANDLE hInstall); + +/** + * Schedule reboot after installation if reboot + * indication file is found in user's temp directory + * + * @param hInstall Handle to the installation provided to the DLL custom action + * + * @return ERROR_SUCCESS on success; An error code otherwise + * See: https://msdn.microsoft.com/en-us/library/windows/desktop/aa368072.aspx + */ +DLLEXP_DECL UINT __stdcall +CheckAndScheduleReboot(_In_ MSIHANDLE hInstall); + #ifdef __cplusplus } #endif