-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
ShellExecuteInstallerHandler.cpp
592 lines (500 loc) · 24 KB
/
ShellExecuteInstallerHandler.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#include "pch.h"
#include "ShellExecuteInstallerHandler.h"
#include <AppInstallerFileLogger.h>
#include <AppInstallerRuntime.h>
#include <winget/Filesystem.h>
using namespace AppInstaller::CLI;
using namespace AppInstaller::Utility;
using namespace AppInstaller::Manifest;
using namespace AppInstaller::Repository;
namespace AppInstaller::CLI::Workflow
{
namespace
{
// ShellExecutes the given path.
std::optional<DWORD> InvokeShellExecuteEx(const std::filesystem::path& filePath, const std::string& args, bool useRunAs, int show, IProgressCallback& progress)
{
AICLI_LOG(CLI, Info, << "Starting: '" << filePath.u8string() << "' with arguments '" << args << '\'');
SHELLEXECUTEINFOW execInfo = { 0 };
execInfo.cbSize = sizeof(execInfo);
execInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
execInfo.lpFile = filePath.c_str();
std::wstring argsUtf16 = Utility::ConvertToUTF16(args);
execInfo.lpParameters = argsUtf16.c_str();
execInfo.nShow = show;
// This installer must be run elevated, but we are not currently.
// Have ShellExecute elevate the installer since it won't do so itself.
if (useRunAs)
{
execInfo.lpVerb = L"runas";
}
THROW_LAST_ERROR_IF(!ShellExecuteExW(&execInfo) || !execInfo.hProcess);
wil::unique_process_handle process{ execInfo.hProcess };
// Wait for installation to finish
while (!progress.IsCancelledBy(CancelReason::User))
{
DWORD waitResult = WaitForSingleObject(process.get(), 250);
if (waitResult == WAIT_OBJECT_0)
{
break;
}
if (waitResult != WAIT_TIMEOUT)
{
THROW_LAST_ERROR_MSG("Unexpected WaitForSingleObjectResult: %lu", waitResult);
}
}
if (progress.IsCancelledBy(CancelReason::Any))
{
return {};
}
else
{
DWORD exitCode = 0;
GetExitCodeProcess(process.get(), &exitCode);
return exitCode;
}
}
std::optional<DWORD> InvokeShellExecute(const std::filesystem::path& filePath, const std::string& args, IProgressCallback& progress)
{
// Some installers force UI. Setting to SW_HIDE will hide installer UI and installation will never complete.
// Verified setting to SW_SHOW does not hurt silent mode since no UI will be shown.
return InvokeShellExecuteEx(filePath, args, false, SW_SHOW, progress);
}
// Gets the escaped installer args.
std::string GetInstallerArgsTemplate(Execution::Context& context)
{
bool isUpdate = WI_IsFlagSet(context.GetFlags(), Execution::ContextFlag::InstallerExecutionUseUpdate);
bool isRepair = WI_IsFlagSet(context.GetFlags(), Execution::ContextFlag::InstallerExecutionUseRepair);
const auto& installer = context.Get<Execution::Data::Installer>();
const auto& installerSwitches = installer->Switches;
std::string installerArgs = {};
// Construct install experience arg.
// SilentWithProgress is default, so look for it first.
auto experienceArgsItr = installerSwitches.find(InstallerSwitchType::SilentWithProgress);
if (context.Args.Contains(Execution::Args::Type::Interactive))
{
// If interactive requested, always use Interactive (or nothing). If the installer supports
// interactive it is usually the default, and thus it is cumbersome to put a blank entry in
// the manifest.
experienceArgsItr = installerSwitches.find(InstallerSwitchType::Interactive);
}
// If no SilentWithProgress exists, or Silent requested, try to find Silent.
else if (experienceArgsItr == installerSwitches.end() || context.Args.Contains(Execution::Args::Type::Silent))
{
auto silentItr = installerSwitches.find(InstallerSwitchType::Silent);
// If Silent requested, but doesn't exist, then continue using SilentWithProgress.
if (silentItr != installerSwitches.end())
{
experienceArgsItr = silentItr;
}
}
if (experienceArgsItr != installerSwitches.end())
{
installerArgs += experienceArgsItr->second;
}
// Construct log path arg.
if (installerSwitches.find(InstallerSwitchType::Log) != installerSwitches.end())
{
installerArgs += ' ' + installerSwitches.at(InstallerSwitchType::Log);
}
// Construct repair arg. Custom switches and other args are not applicable for repair scenario so we can return here.
if (isRepair)
{
if (installerSwitches.find(InstallerSwitchType::Repair) != installerSwitches.end())
{
installerArgs += ' ' + installerSwitches.at(InstallerSwitchType::Repair);
}
return installerArgs;
}
// Construct custom arg.
if (installerSwitches.find(InstallerSwitchType::Custom) != installerSwitches.end())
{
installerArgs += ' ' + installerSwitches.at(InstallerSwitchType::Custom);
}
// Construct custom arg passed in by cli arg
if (context.Args.Contains(Execution::Args::Type::CustomSwitches))
{
std::string_view customSwitches = context.Args.GetArg(Execution::Args::Type::CustomSwitches);
// Since these arguments are appended to the installer at runtime, it doesn't make sense to append them if empty or whitespace
if (!Utility::IsEmptyOrWhitespace(customSwitches))
{
installerArgs += ' ' + std::string{ customSwitches };
}
}
// Construct update arg if applicable
if (isUpdate && installerSwitches.find(InstallerSwitchType::Update) != installerSwitches.end())
{
installerArgs += ' ' + installerSwitches.at(InstallerSwitchType::Update);
}
// Construct install location arg if necessary.
if (context.Args.Contains(Execution::Args::Type::InstallLocation) &&
installerSwitches.find(InstallerSwitchType::InstallLocation) != installerSwitches.end())
{
installerArgs += ' ' + installerSwitches.at(InstallerSwitchType::InstallLocation);
}
return installerArgs;
}
// Applies values to the template.
void PopulateInstallerArgsTemplate(Execution::Context& context, std::string& installerArgs)
{
// Populate <LogPath> with value from command line or temp path.
std::string logPath;
if (context.Args.Contains(Execution::Args::Type::Log))
{
logPath = context.Args.GetArg(Execution::Args::Type::Log);
}
else
{
const auto& manifest = context.Get<Execution::Data::Manifest>();
auto path = Runtime::GetPathTo(Runtime::PathName::DefaultLogLocation);
path /= Logging::FileLogger::DefaultPrefix();
path += '-';
path += Utility::ConvertToUTF16(manifest.Id + '.' + manifest.Version);
path += '-';
path += Utility::GetCurrentTimeForFilename();
path += Logging::FileLogger::DefaultExt();
logPath = path.u8string();
}
if (Utility::FindAndReplace(installerArgs, std::string(ARG_TOKEN_LOGPATH), logPath))
{
context.Add<Execution::Data::LogPath>(Utility::ConvertToUTF16(logPath));
}
// Populate <InstallPath> with value from command line.
if (context.Args.Contains(Execution::Args::Type::InstallLocation))
{
Utility::FindAndReplace(installerArgs, std::string(ARG_TOKEN_INSTALLPATH), context.Args.GetArg(Execution::Args::Type::InstallLocation));
}
// Todo: language token support will be implemented later
}
// Gets the arguments for uninstalling an MSI with MsiExec
std::string GetMsiExecUninstallArgs(Execution::Context& context, const Utility::LocIndString& productCode)
{
std::string args = "/x" + productCode.get();
// https://learn.microsoft.com/en-us/windows/win32/msi/standard-installer-command-line-options
if (context.Args.Contains(Execution::Args::Type::Silent))
{
args += " /quiet /norestart";
}
else if (!context.Args.Contains(Execution::Args::Type::Interactive))
{
args += " /passive /norestart";
}
return args;
}
// Gets the arguments for repairing an MSI with MsiExec
std::string GetMsiExecRepairArgs(Execution::Context& context, const Utility::LocIndString& productCode)
{
// https://learn.microsoft.com/en-us/windows/win32/msi/command-line-options
// Available Options for '/f [p|o|e|d|c|a|u|m|s|v] <Product.msi | ProductCode>'
// Default parameter for '/f' is 'omus'
// o - Reinstall all files regardless of version
// m - Rewrite all required registry entries (This is the default option)
// u - Rewrite all required user-specific registry entries (This is the default option)
// s - Overwrite all existing shortcuts (This is the default option)
std::string args = "/f " + productCode.get();
// https://learn.microsoft.com/en-us/windows/win32/msi/standard-installer-command-line-options
if (context.Args.Contains(Execution::Args::Type::Silent))
{
args += " /quiet /norestart";
}
else if (!context.Args.Contains(Execution::Args::Type::Interactive))
{
args += " /passive /norestart";
}
return args;
}
}
void ShellExecuteInstallImpl(Execution::Context& context)
{
bool isRepair = WI_IsFlagSet(context.GetFlags(), Execution::ContextFlag::InstallerExecutionUseRepair);
if (isRepair)
{
context.Reporter.Info() << Resource::String::RepairFlowStartingPackageRepair << std::endl;
}
else
{
context.Reporter.Info() << Resource::String::InstallFlowStartingPackageInstall << std::endl;
}
const auto& installer = context.Get<Execution::Data::Installer>();
const std::string& installerArgs = context.Get<Execution::Data::InstallerArgs>();
// Inform of elevation requirements
bool isElevated = Runtime::IsRunningAsAdmin();
// The installer will run elevated, either by direct request or through the installer itself doing so.
if ((installer->ElevationRequirement == ElevationRequirementEnum::ElevationRequired ||
installer->ElevationRequirement == ElevationRequirementEnum::ElevatesSelf)
&& !isElevated)
{
context.Reporter.Warn() << Resource::String::InstallerElevationExpected << std::endl;
}
// Some installers force UI. Setting to SW_HIDE will hide installer UI and installation will never complete.
// Verified setting to SW_SHOW does not hurt silent mode since no UI will be shown.
auto installResult = context.Reporter.ExecuteWithProgress(
std::bind(InvokeShellExecuteEx,
context.Get<Execution::Data::InstallerPath>(),
installerArgs,
installer->ElevationRequirement == ElevationRequirementEnum::ElevationRequired && !isElevated,
SW_SHOW,
std::placeholders::_1));
if (!installResult)
{
if (isRepair)
{
context.Reporter.Warn() << Resource::String::RepairAbandoned << std::endl;
}
else
{
context.Reporter.Warn() << Resource::String::InstallAbandoned << std::endl;
}
AICLI_TERMINATE_CONTEXT(E_ABORT);
}
else
{
context.Add<Execution::Data::OperationReturnCode>(installResult.value());
}
}
void GetInstallerArgs(Execution::Context& context)
{
// If override switch is specified, use the override value as installer args.
if (context.Args.Contains(Execution::Args::Type::Override))
{
context.Add<Execution::Data::InstallerArgs>(std::string{ context.Args.GetArg(Execution::Args::Type::Override) });
return;
}
std::string installerArgs = GetInstallerArgsTemplate(context);
PopulateInstallerArgsTemplate(context, installerArgs);
AICLI_LOG(CLI, Info, << "Installer args: " << installerArgs);
context.Add<Execution::Data::InstallerArgs>(std::move(installerArgs));
}
void ShellExecuteUninstallImpl(Execution::Context& context)
{
context.Reporter.Info() << Resource::String::UninstallFlowStartingPackageUninstall << std::endl;
std::wstring commandUtf16 = Utility::ConvertToUTF16(context.Get<Execution::Data::UninstallString>());
// Parse the command string as application and command line for CreateProcess
wil::unique_cotaskmem_string app = nullptr;
wil::unique_cotaskmem_string args = nullptr;
THROW_IF_FAILED(SHEvaluateSystemCommandTemplate(commandUtf16.c_str(), &app, NULL, &args));
auto uninstallResult = context.Reporter.ExecuteWithProgress(
std::bind(InvokeShellExecute,
std::filesystem::path(app.get()),
Utility::ConvertToUTF8(args.get()),
std::placeholders::_1));
if (!uninstallResult)
{
context.Reporter.Warn() << Resource::String::UninstallAbandoned << std::endl;
AICLI_TERMINATE_CONTEXT(E_ABORT);
}
else
{
context.Add<Execution::Data::OperationReturnCode>(uninstallResult.value());
}
}
void ShellExecuteRepairImpl(Execution::Context& context)
{
context.Reporter.Info() << Resource::String::RepairFlowStartingPackageRepair << std::endl;
std::wstring commandUtf16 = Utility::ConvertToUTF16(context.Get<Execution::Data::RepairString>());
// When running as admin, block attempt to repair user scope installed package.
// [NOTE:] This check is to address the security concern related to above scenario.
if (Runtime::IsRunningAsAdmin())
{
auto installedPackageVersion = context.Get<Execution::Data::InstalledPackageVersion>();
const std::string installedScopeString = installedPackageVersion->GetMetadata()[PackageVersionMetadata::InstalledScope];
auto scopeEnum = ConvertToScopeEnum(installedScopeString);
if (scopeEnum == ScopeEnum::User)
{
context.Reporter.Error() << Resource::String::NoAdminRepairForUserScopePackage << std::endl;
AICLI_TERMINATE_CONTEXT(APPINSTALLER_CLI_ERROR_ADMIN_CONTEXT_REPAIR_PROHIBITED);
}
}
// Parse the command string as application and command line for CreateProcess
wil::unique_cotaskmem_string app = nullptr;
wil::unique_cotaskmem_string args = nullptr;
THROW_IF_FAILED(SHEvaluateSystemCommandTemplate(commandUtf16.c_str(), &app, NULL, &args));
auto repairResult = context.Reporter.ExecuteWithProgress(
std::bind(InvokeShellExecute,
std::filesystem::path(app.get()),
Utility::ConvertToUTF8(args.get()),
std::placeholders::_1));
if (!repairResult)
{
context.Reporter.Error() << Resource::String::RepairAbandoned << std::endl;
AICLI_TERMINATE_CONTEXT(E_ABORT);
}
else
{
context.Add<Execution::Data::OperationReturnCode>(repairResult.value());
}
}
void ShellExecuteMsiExecUninstall(Execution::Context& context)
{
const auto& productCodes = context.Get<Execution::Data::ProductCodes>();
context.Reporter.Info() << Resource::String::UninstallFlowStartingPackageUninstall << std::endl;
const std::filesystem::path msiexecPath{ ExpandEnvironmentVariables(L"%windir%\\system32\\msiexec.exe") };
for (const auto& productCode : productCodes)
{
AICLI_LOG(CLI, Info, << "Removing: " << productCode);
auto uninstallResult = context.Reporter.ExecuteWithProgress(
std::bind(InvokeShellExecute,
msiexecPath,
GetMsiExecUninstallArgs(context, productCode),
std::placeholders::_1));
if (!uninstallResult)
{
context.Reporter.Error() << Resource::String::UninstallAbandoned << std::endl;
AICLI_TERMINATE_CONTEXT(E_ABORT);
}
else
{
context.Add<Execution::Data::OperationReturnCode>(uninstallResult.value());
}
}
}
void ShellExecuteMsiExecRepair(Execution::Context& context)
{
const auto& productCodes = context.Get<Execution::Data::ProductCodes>();
context.Reporter.Info() << Resource::String::RepairFlowStartingPackageRepair << std::endl;
const std::filesystem::path msiexecPath{ ExpandEnvironmentVariables(L"%windir%\\system32\\msiexec.exe") };
for (const auto& productCode : productCodes)
{
AICLI_LOG(CLI, Info, << "Repairing: " << productCode);
auto repairResult = context.Reporter.ExecuteWithProgress(
std::bind(InvokeShellExecute,
msiexecPath,
GetMsiExecRepairArgs(context, productCode),
std::placeholders::_1));
if (!repairResult)
{
context.Reporter.Error() << Resource::String::RepairAbandoned << std::endl;
AICLI_TERMINATE_CONTEXT(E_ABORT);
}
else
{
context.Add<Execution::Data::OperationReturnCode>(repairResult.value());
}
}
}
#ifndef AICLI_DISABLE_TEST_HOOKS
std::optional<DWORD> s_EnableWindowsFeatureResult_Override{};
void TestHook_SetEnableWindowsFeatureResult_Override(std::optional<DWORD>&& result)
{
s_EnableWindowsFeatureResult_Override = std::move(result);
}
std::optional<DWORD> s_DoesWindowsFeatureExistResult_Override{};
void TestHook_SetDoesWindowsFeatureExistResult_Override(std::optional<DWORD>&& result)
{
s_DoesWindowsFeatureExistResult_Override = std::move(result);
}
#endif
std::filesystem::path GetDismExecutablePath()
{
return AppInstaller::Filesystem::GetExpandedPath("%windir%\\system32\\dism.exe");
}
std::optional<DWORD> DoesWindowsFeatureExist(Execution::Context& context, std::string_view featureName)
{
#ifndef AICLI_DISABLE_TEST_HOOKS
if (s_DoesWindowsFeatureExistResult_Override)
{
return s_DoesWindowsFeatureExistResult_Override;
}
#endif
std::string args = "/Online /Get-FeatureInfo /FeatureName:" + std::string{ featureName };
auto dismExecPath = GetDismExecutablePath();
auto getFeatureInfoResult = context.Reporter.ExecuteWithProgress(
std::bind(InvokeShellExecuteEx,
dismExecPath,
args,
false,
SW_HIDE,
std::placeholders::_1));
return getFeatureInfoResult;
}
std::optional<DWORD> EnableWindowsFeature(Execution::Context& context, std::string_view featureName)
{
#ifndef AICLI_DISABLE_TEST_HOOKS
if (s_EnableWindowsFeatureResult_Override)
{
return s_EnableWindowsFeatureResult_Override;
}
#endif
std::string args = "/Online /Enable-Feature /NoRestart /FeatureName:" + std::string{ featureName };
auto dismExecPath = GetDismExecutablePath();
AICLI_LOG(Core, Info, << "Enabling Windows Feature [" << featureName << "]");
auto enableFeatureResult = context.Reporter.ExecuteWithProgress(
std::bind(InvokeShellExecuteEx,
dismExecPath,
args,
false,
SW_HIDE,
std::placeholders::_1));
return enableFeatureResult;
}
void ShellExecuteEnableWindowsFeature::operator()(Execution::Context& context) const
{
Utility::LocIndView locIndFeatureName{ m_featureName };
std::optional<DWORD> doesFeatureExistResult = DoesWindowsFeatureExist(context, m_featureName);
if (!doesFeatureExistResult)
{
AICLI_TERMINATE_CONTEXT(E_ABORT);
}
else if (doesFeatureExistResult.value() != ERROR_SUCCESS)
{
context.Add<Execution::Data::OperationReturnCode>(doesFeatureExistResult.value());
return;
}
context.Reporter.Info() << Resource::String::EnablingWindowsFeature(locIndFeatureName) << std::endl;
std::optional<DWORD> enableFeatureResult = EnableWindowsFeature(context, m_featureName);
if (!enableFeatureResult)
{
AICLI_TERMINATE_CONTEXT(E_ABORT);
}
else
{
context.Add<Execution::Data::OperationReturnCode>(enableFeatureResult.value());
}
}
#ifndef AICLI_DISABLE_TEST_HOOKS
std::optional<DWORD> s_ExtractArchiveWithTarResult_Override{};
void TestHook_SetExtractArchiveWithTarResult_Override(std::optional<DWORD>&& result)
{
s_ExtractArchiveWithTarResult_Override = std::move(result);
}
#endif
void ShellExecuteExtractArchive::operator()(Execution::Context& context) const
{
auto tarExecPath = AppInstaller::Filesystem::GetExpandedPath("%windir%\\system32\\tar.exe");
std::string args = "-xf \"" + m_archivePath.u8string() + "\" -C \"" + m_destPath.u8string() + "\"";
std::optional<DWORD> extractArchiveResult;
#ifndef AICLI_DISABLE_TEST_HOOKS
if (s_ExtractArchiveWithTarResult_Override)
{
extractArchiveResult = *s_ExtractArchiveWithTarResult_Override;
}
else
#endif
{
extractArchiveResult = context.Reporter.ExecuteWithProgress(
std::bind(InvokeShellExecuteEx,
tarExecPath,
args,
false,
SW_HIDE,
std::placeholders::_1));
}
if (!extractArchiveResult)
{
AICLI_TERMINATE_CONTEXT(E_ABORT);
}
if (extractArchiveResult.value() == ERROR_SUCCESS)
{
AICLI_LOG(CLI, Info, << "Successfully extracted archive");
context.Reporter.Info() << Resource::String::ExtractArchiveSucceeded << std::endl;
}
else
{
AICLI_LOG(CLI, Info, << "Failed to extract archive with exit code " << extractArchiveResult.value());
context.Reporter.Error() << Resource::String::ExtractArchiveFailed << std::endl;
AICLI_TERMINATE_CONTEXT(APPINSTALLER_CLI_ERROR_EXTRACT_ARCHIVE_FAILED);
}
}
}