Fail Windows elevate on UAC cancel
Start-Process failures left $p null, then exit $null made PowerShell return 0, so install/uninstall looked successful. Make launch errors terminating and exit nonzero before reading ExitCode. Signed-off-by: Ludovic Vielle <ludovic@probo.com>
This commit is contained in:
@@ -28,6 +28,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"go.probo.inc/probo/pkg/deviceagent/checks"
|
"go.probo.inc/probo/pkg/deviceagent/checks"
|
||||||
|
"golang.org/x/sys/windows"
|
||||||
)
|
)
|
||||||
|
|
||||||
func runElevatedInstall(opts InstallOptions, enrollmentToken string) error {
|
func runElevatedInstall(opts InstallOptions, enrollmentToken string) error {
|
||||||
@@ -42,31 +43,7 @@ func runElevatedInstall(opts InstallOptions, enrollmentToken string) error {
|
|||||||
args = append(args, "--dir", opts.ConfigDir)
|
args = append(args, "--dir", opts.ConfigDir)
|
||||||
}
|
}
|
||||||
|
|
||||||
argList := make([]string, len(args))
|
return runPowerShellCommand(elevatedStartProcess(opts.ExePath, args))
|
||||||
for i, arg := range args {
|
|
||||||
argList[i] = "'" + escapePowerShellSingleQuoted(arg) + "'"
|
|
||||||
}
|
|
||||||
|
|
||||||
script := fmt.Sprintf(
|
|
||||||
`$p = Start-Process -FilePath %s -ArgumentList @(%s) -Verb RunAs -Wait -PassThru; if ($p.ExitCode -ne 0) { exit $p.ExitCode }`,
|
|
||||||
"'"+escapePowerShellSingleQuoted(opts.ExePath)+"'",
|
|
||||||
strings.Join(argList, ","),
|
|
||||||
)
|
|
||||||
|
|
||||||
candidates := checks.CommandCandidates("powershell.exe")
|
|
||||||
if len(candidates) == 0 {
|
|
||||||
return fmt.Errorf("command %q not available at expected absolute path", "powershell.exe")
|
|
||||||
}
|
|
||||||
|
|
||||||
out, err := exec.Command(
|
|
||||||
candidates[0],
|
|
||||||
"-NoProfile",
|
|
||||||
"-NonInteractive",
|
|
||||||
"-Command",
|
|
||||||
script,
|
|
||||||
).CombinedOutput()
|
|
||||||
|
|
||||||
return commandError(out, err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func runElevatedUninstall(opts UninstallOptions) error {
|
func runElevatedUninstall(opts UninstallOptions) error {
|
||||||
@@ -75,17 +52,26 @@ func runElevatedUninstall(opts UninstallOptions) error {
|
|||||||
args = append(args, "--dir", opts.ConfigDir)
|
args = append(args, "--dir", opts.ConfigDir)
|
||||||
}
|
}
|
||||||
|
|
||||||
argList := make([]string, len(args))
|
return runPowerShellCommand(elevatedStartProcess(opts.ExePath, args))
|
||||||
for i, arg := range args {
|
|
||||||
argList[i] = "'" + escapePowerShellSingleQuoted(arg) + "'"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
script := fmt.Sprintf(
|
// elevatedStartProcess builds a PowerShell script that launches exePath elevated
|
||||||
`$p = Start-Process -FilePath %s -ArgumentList @(%s) -Verb RunAs -Wait -PassThru; if ($p.ExitCode -ne 0) { exit $p.ExitCode }`,
|
// via UAC. Launch failures (including UAC cancel) are terminating and exit
|
||||||
"'"+escapePowerShellSingleQuoted(opts.ExePath)+"'",
|
// nonzero before $p.ExitCode is inspected.
|
||||||
|
func elevatedStartProcess(exePath string, args []string) string {
|
||||||
|
argList := make([]string, len(args))
|
||||||
|
for i, arg := range args {
|
||||||
|
argList[i] = "'" + escapePowerShellSingleQuoted(windows.EscapeArg(arg)) + "'"
|
||||||
|
}
|
||||||
|
|
||||||
|
return fmt.Sprintf(
|
||||||
|
`$ErrorActionPreference = 'Stop'; $p = Start-Process -FilePath %s -ArgumentList @(%s) -Verb RunAs -Wait -PassThru; if ($null -eq $p) { exit 1 }; if ($p.ExitCode -ne 0) { exit $p.ExitCode }`,
|
||||||
|
"'"+escapePowerShellSingleQuoted(exePath)+"'",
|
||||||
strings.Join(argList, ","),
|
strings.Join(argList, ","),
|
||||||
)
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
func runPowerShellCommand(script string) error {
|
||||||
candidates := checks.CommandCandidates("powershell.exe")
|
candidates := checks.CommandCandidates("powershell.exe")
|
||||||
if len(candidates) == 0 {
|
if len(candidates) == 0 {
|
||||||
return fmt.Errorf("command %q not available at expected absolute path", "powershell.exe")
|
return fmt.Errorf("command %q not available at expected absolute path", "powershell.exe")
|
||||||
|
|||||||
Reference in New Issue
Block a user