Skip to content

Commit

Permalink
ExecOn: When running with no user logged on, not failing on impersona…
Browse files Browse the repository at this point in the history
…tion errors
  • Loading branch information
nirbar committed Apr 18, 2024
1 parent c59ff1e commit 5dbc8f3
Show file tree
Hide file tree
Showing 10 changed files with 23 additions and 25 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/github-actions-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ on:
psw_wix_version:
description: 'PanelSwWix4 version'
required: true
default: 6.0.0-psw-wix.0267-11
default: 6.0.0-psw-wix.0268-12
type: string

jobs:
Expand All @@ -37,7 +37,7 @@ jobs:
Add-Content -Path ${{ github.env }} -Value "PSW_WIX_VERSION=${{ env.DEFAULT_PSW_WIX_VERSION }}"
}
env:
DEFAULT_PSW_WIX_VERSION: '6.0.0-psw-wix.0267-11'
DEFAULT_PSW_WIX_VERSION: '6.0.0-psw-wix.0268-12'

- name: Prepare for build
run: |
Expand Down
2 changes: 1 addition & 1 deletion src/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
<Wix4Version Condition=" '$(Wix4Version)' == '' ">5.0.0</Wix4Version>
<PanelSwWix4Version Condition=" '$(PanelSwWix4Version)' == '' ">6.0.0-psw-wix.0267-11</PanelSwWix4Version>
<PanelSwWix4Version Condition=" '$(PanelSwWix4Version)' == '' ">6.0.0-psw-wix.0268-12</PanelSwWix4Version>
<SevenZapVersion Condition=" '$(SevenZapVersion)' == '' ">23.1.33</SevenZapVersion>
</PropertyGroup>
<ItemGroup>
Expand Down
20 changes: 10 additions & 10 deletions src/PanelSwCustomActions/ExecOnComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,6 @@ HRESULT CExecOnComponent::AddExec(const CWixString &szCommand, LPCWSTR szWorking
::com::panelsw::ca::Command* pCmd = nullptr;
ExecOnDetails* pDetails = nullptr;
::std::string* pAny = nullptr;
DWORD dwSessionId = WTS_CURRENT_SESSION;
bool bRes = true;

hr = AddCommand("CExecOnComponent", &pCmd);
Expand Down Expand Up @@ -572,10 +571,6 @@ HRESULT CExecOnComponent::AddExec(const CWixString &szCommand, LPCWSTR szWorking
}
}

bRes = ::ProcessIdToSessionId(::GetCurrentProcessId(), &dwSessionId);
ExitOnNullWithLastError(bRes, hr, "Failed to get current user's session id");
pDetails->set_sessionid(dwSessionId); // Defaults to WTS_CURRENT_SESSION (= -1).

pAny = pCmd->mutable_details();
ExitOnNull(pAny, hr, E_FAIL, "Failed allocating any");

Expand Down Expand Up @@ -656,7 +651,7 @@ HRESULT CExecOnComponent::ExecuteOne(const com::panelsw::ca::ExecOnDetails& deta
}

// We only impersonate for the duration of the process creation because I've encountered crashes when logging impersonated
hr = Impersonate(details.impersonate(), szDomain, szUser, szPassword, details.sessionid(), &szEnvironmentMultiSz, &ctxImpersonation);
hr = Impersonate(details.impersonate(), szDomain, szUser, szPassword, &szEnvironmentMultiSz, &ctxImpersonation);
ExitOnFailure(hr, "Failed to impersonate");

hr = SetEnvironment(&szEnvironmentMultiSz, details.environment());
Expand Down Expand Up @@ -1137,7 +1132,7 @@ HRESULT CExecOnComponent::LaunchProcess(IMPERSONATION_CONTEXT* pctxImpersonation
return hr;
}

HRESULT CExecOnComponent::Impersonate(BOOL bImpersonate, LPCWSTR szDomain, LPCWSTR szUser, LPCWSTR szPassword, DWORD dwSessionId, CWixString* pszEnvironmentMultiSz, IMPERSONATION_CONTEXT *pctxImpersonate)
HRESULT CExecOnComponent::Impersonate(BOOL bImpersonate, LPCWSTR szDomain, LPCWSTR szUser, LPCWSTR szPassword, CWixString* pszEnvironmentMultiSz, IMPERSONATION_CONTEXT *pctxImpersonate)
{
HRESULT hr = S_OK;
BOOL bRes = TRUE;
Expand Down Expand Up @@ -1175,14 +1170,19 @@ HRESULT CExecOnComponent::Impersonate(BOOL bImpersonate, LPCWSTR szDomain, LPCWS
{
DWORD dwNameSize = 0;

bRes = ::WTSQuerySessionInformation(NULL, dwSessionId, WTS_INFO_CLASS::WTSUserName, &szSessionUserName, &dwNameSize);
bRes = ::WTSQuerySessionInformation(WTS_CURRENT_SERVER_HANDLE, WTS_CURRENT_SESSION, WTS_INFO_CLASS::WTSUserName, &szSessionUserName, &dwNameSize);
ExitOnNullWithLastError(bRes, hr, "Failed to get user name");
WcaLog(LOGLEVEL::LOGMSG_VERBOSE, "Impersonating '%ls'", szSessionUserName);

hr = szTempUserName.Copy(szSessionUserName);
ExitOnFailure(hr, "Failed to copy user name");

bRes = ::WTSQueryUserToken(dwSessionId, &pctxImpersonate->hUserToken);
bRes = ::WTSQueryUserToken(WTS_CURRENT_SESSION, &pctxImpersonate->hUserToken);
if (!bRes && (!szSessionUserName || !*szSessionUserName))
{
bRes = TRUE;
WcaLogError(HRESULT_FROM_WIN32(::GetLastError()), "Can't impersonate because we're running with no client logged on");
}
ExitOnNullWithLastError(bRes, hr, "Failed to get user token");
}

Expand Down Expand Up @@ -1244,7 +1244,7 @@ HRESULT CExecOnComponent::Impersonate(BOOL bImpersonate, LPCWSTR szDomain, LPCWS
hr = FileWriteHandle(hTempFile, (LPBYTE)pbDeferredExePackage, cbDeferredExePackage);
ExitOnFailure(hr, "Failed to write DeferredExePackage.exe");

bRes = ::WTSQueryUserToken(dwSessionId, &pctxImpersonate->hUserToken);
bRes = ::WTSQueryUserToken(WTS_CURRENT_SESSION, &pctxImpersonate->hUserToken);
ExitOnNullWithLastError(bRes, hr, "Failed to get user token");
}

Expand Down
2 changes: 1 addition & 1 deletion src/PanelSwCustomActions/ExecOnComponent.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class CExecOnComponent :

HRESULT ExecuteOne(const com::panelsw::ca::ExecOnDetails &details);

HRESULT Impersonate(BOOL bImpersonate, LPCWSTR szDomain, LPCWSTR szUser, LPCWSTR szPassword, DWORD dwSessionId, CWixString* pszEnvironmentMultiSz, IMPERSONATION_CONTEXT* pctxImpersonation);
HRESULT Impersonate(BOOL bImpersonate, LPCWSTR szDomain, LPCWSTR szUser, LPCWSTR szPassword, CWixString* pszEnvironmentMultiSz, IMPERSONATION_CONTEXT* pctxImpersonation);
void Unimpersonate(IMPERSONATION_CONTEXT* pctxImpersonation);

HRESULT SetEnvironment(CWixString *pszEnvironmentMultiSz, const ::google::protobuf::Map<std::string, com::panelsw::ca::ObfuscatedString> &customEnv);
Expand Down
1 change: 0 additions & 1 deletion src/ProtoCaLib/execOnDetails.proto
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,4 @@ message ExecOnDetails{

// Impersonate current user
bool impersonate = 12;
uint32 sessionId = 13;
}
2 changes: 1 addition & 1 deletion src/TidyBuild.custom.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<Project ToolsVersion="16.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildThisFileDirectory)TidyBuild.user.props" Condition="Exists('$(MSBuildThisFileDirectory)TidyBuild.user.props')"/>
<PropertyGroup>
<FullVersion>5.0.0</FullVersion>
<FullVersion>5.0.1</FullVersion>
<FullVersion Condition=" '$(GITHUB_RUN_NUMBER)'!='' ">$(FullVersion).$(GITHUB_RUN_NUMBER)</FullVersion>
<ProductName>PanelSwWixExtension</ProductName>
<Manufacturer>Panel::Software</Manufacturer>
Expand Down
7 changes: 3 additions & 4 deletions src/UnitTests/ExecOnComponentUT/ExecOnComponentUT.wxs
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,10 @@
<Component Condition="TEST_IMPERSONATION=1">
<File Source="$(sys.SOURCEFILEPATH)" Id="ExecOnComponentUT1.wxs" Name="ExecOnComponentUT1.wxs" />

<PanelSW:ExecOn BeforeStartServices="yes" OnInstall="yes" ErrorHandling="prompt" Command='"notepad"' Impersonate='no'/>
<PanelSW:ExecOn BeforeStartServices="yes" OnInstall="yes" ErrorHandling="prompt" Command='"notepad"' Impersonate='yes'/>
<PanelSW:ExecOn BeforeStartServices="yes" OnInstall="yes" ErrorHandling="prompt" Command='"notepad"' User='me'/>
<PanelSW:ExecOn BeforeStartServices="yes" OnInstall="yes" ErrorHandling="prompt" Command='"CMD" /C SET' Impersonate='no'/>
<PanelSW:ExecOn BeforeStartServices="yes" OnInstall="yes" ErrorHandling="prompt" Command='"CMD" /C SET' Impersonate='yes'/>
<PanelSW:ExecOn BeforeStartServices="yes" OnInstall="yes" ErrorHandling="prompt" Command='"CMD" /C SET' User='me'/>
<PanelSW:ExecOn BeforeStartServices="yes" OnInstall="yes" ErrorHandling="prompt" Command='"CMD" /C SET' User='me'/>

</Component>

<Component Condition="TEST_FAILURE=1">
Expand Down
4 changes: 2 additions & 2 deletions src/UnitTests/ForceVersionUT/ForceVersionUT.wixproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="WixToolset.Sdk">
<Project Sdk="PanelSwWix4.Sdk">
<PropertyGroup>
<EnableDefaultCompileItems>false</EnableDefaultCompileItems>
</PropertyGroup>
Expand All @@ -14,7 +14,7 @@
<PackageReference Include="WixToolset.Util.wixext" />
<PackageReference Include="TidyBuild" />
<WixExtension Include="PanelSwWixExtension">
<HintPath>..\..\build\AnyCPU\bin\$(Configuration)\PanelSwWixExtension\PanelSwWixExtension.dll</HintPath>
<HintPath>$(BuildFolder)..\AnyCPU\bin\$(Configuration)\PanelSwWixExtension\PanelSwWixExtension.dll</HintPath>
<Name>PanelSwWixExtension</Name>
</WixExtension>
</ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions src/UnitTests/ForceVersionUT/ForceVersionUT.wxs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<Wix xmlns="http://wixtoolset.org/schemas/v4/wxs" xmlns:PanelSW="http://schemas.panel-sw.co.il/wix/WixExtension">
<Package Name="AlwaysOverwriteFileUT" Language="1033" Version="$(var.JetVersion)" Manufacturer="$(var.JetManufacturer)" UpgradeCode="{B997AF76-8F98-40D1-B123-552C82C40B37}" InstallerVersion="200">
<Package Name="ForceVersionUT" Language="1033" Version="$(var.JetVersion)" Manufacturer="$(var.JetManufacturer)" UpgradeCode="{B997AF76-8F98-40D1-B123-552C82C40B37}" InstallerVersion="200">
<MajorUpgrade DowngradeErrorMessage="A newer version of [ProductName] is already installed." AllowSameVersionUpgrades="yes" Schedule="afterInstallExecute"/>
<Feature Id="ProductFeature" Title="UnitTestSetup" Level="1">
<ComponentGroupRef Id="ProductComponents" />
Expand All @@ -17,7 +17,7 @@
</File>
</Component>
<Component>
<File Id="PanelSwWixExtension.dll" Source="$(sys.SOURCEFILEDIR)..\..\build\Any CPU\bin\$(var.Configuration)\PanelSwWixExtension\PanelSwWixExtension.dll">
<File Id="PanelSwWixExtension.dll" Source="$(sys.SOURCEFILEDIR)..\..\..\build\AnyCPU\bin\$(var.Configuration)\PanelSwWixExtension\PanelSwWixExtension.dll">
<PanelSW:ForceVersion />
</File>
</Component>
Expand Down
2 changes: 1 addition & 1 deletion src/global.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"msbuild-sdks": {
"WixToolset.Sdk": "5.0.0",
"PanelSwWix4.Sdk": "6.0.0-psw-wix.0267-11",
"PanelSwWix4.Sdk": "6.0.0-psw-wix.0268-12",
"Microsoft.Build.Traversal": "4.0.0"
}
}

0 comments on commit 5dbc8f3

Please sign in to comment.