|
| 1 | +import * as childProcess from "node:child_process"; |
1 | 2 | import { appendFile, rm } from "node:fs/promises"; |
2 | 3 | import { tmpdir } from "node:os"; |
3 | 4 | import path from "node:path"; |
4 | 5 |
|
| 6 | +import * as vscode from "vscode"; |
5 | 7 | import type { CancellationToken, LogOutputChannel } from "vscode"; |
6 | 8 |
|
7 | | -import { spawn, SpawnError } from "./spawn.ts"; |
| 9 | +import { pipeToLogOutputChannel, spawn, SpawnError } from "./spawn.ts"; |
8 | 10 |
|
9 | 11 | export interface SpawnElevatedDarwinOptions { |
10 | 12 | script: string; |
@@ -50,28 +52,117 @@ export interface SpawnElevatedLinuxOptions { |
50 | 52 | cancellationToken?: CancellationToken; |
51 | 53 | } |
52 | 54 |
|
53 | | -export async function spawnElevatedLinux( |
| 55 | +async function tryPkexec( |
54 | 56 | options: SpawnElevatedLinuxOptions, |
55 | | -): Promise<{ cancelled: boolean }> { |
| 57 | +): Promise<{ success: boolean; cancelled: boolean }> { |
| 58 | + const scriptArg = JSON.stringify(options.script); |
56 | 59 | try { |
57 | | - await spawn("pkexec", ["sh", "-c", `${JSON.stringify(options.script)}`], { |
| 60 | + await spawn("pkexec", ["sh", "-c", scriptArg], { |
58 | 61 | outputChannel: options.outputChannel, |
59 | 62 | outputLabel: options.outputLabel, |
60 | 63 | cancellationToken: options.cancellationToken, |
61 | 64 | }); |
62 | | - return { |
63 | | - cancelled: false, |
64 | | - }; |
| 65 | + return { success: true, cancelled: false }; |
65 | 66 | } catch (error) { |
66 | | - if (error instanceof SpawnError && error.code === 126) { |
67 | | - return { cancelled: true }; |
| 67 | + if (error instanceof SpawnError) { |
| 68 | + // User cancelled (pkexec returns 126) |
| 69 | + if (error.code === 126) { |
| 70 | + return { success: true, cancelled: true }; |
| 71 | + } |
| 72 | + // Other failures (no polkit agent, TTY error, etc.) - signal to try fallback |
| 73 | + return { success: false, cancelled: false }; |
68 | 74 | } |
69 | | - |
70 | | - options.outputChannel.error(error instanceof Error ? error : String(error)); |
71 | 75 | throw error; |
72 | 76 | } |
73 | 77 | } |
74 | 78 |
|
| 79 | +async function spawnWithSudoPassword( |
| 80 | + options: SpawnElevatedLinuxOptions, |
| 81 | +): Promise<{ cancelled: boolean }> { |
| 82 | + const password = await vscode.window.showInputBox({ |
| 83 | + prompt: "Enter your sudo password", |
| 84 | + password: true, |
| 85 | + ignoreFocusOut: true, |
| 86 | + }); |
| 87 | + |
| 88 | + if (password === undefined) { |
| 89 | + return { cancelled: true }; |
| 90 | + } |
| 91 | + |
| 92 | + const outputLabel = options.outputLabel ? `[${options.outputLabel}]: ` : ""; |
| 93 | + |
| 94 | + return new Promise((resolve, reject) => { |
| 95 | + let stderrOutput = ""; |
| 96 | + |
| 97 | + const child = childProcess.spawn( |
| 98 | + "sudo", |
| 99 | + ["-S", "sh", "-c", options.script], |
| 100 | + { |
| 101 | + stdio: ["pipe", "pipe", "pipe"], |
| 102 | + }, |
| 103 | + ); |
| 104 | + |
| 105 | + // Capture stderr to detect wrong password |
| 106 | + child.stderr?.on("data", (data: Buffer) => { |
| 107 | + stderrOutput += data.toString(); |
| 108 | + }); |
| 109 | + |
| 110 | + // Write password to stdin and close it |
| 111 | + child.stdin.write(`${password}\n`); |
| 112 | + child.stdin.end(); |
| 113 | + |
| 114 | + // Redirect the child process stdout/stderr to VSCode's output channel for visibility |
| 115 | + pipeToLogOutputChannel(child, options.outputChannel, outputLabel); |
| 116 | + |
| 117 | + const disposeCancel = options.cancellationToken?.onCancellationRequested( |
| 118 | + () => { |
| 119 | + child.kill("SIGINT"); |
| 120 | + reject(new Error("Command cancelled")); |
| 121 | + }, |
| 122 | + ); |
| 123 | + |
| 124 | + child.on("close", (code) => { |
| 125 | + disposeCancel?.dispose(); |
| 126 | + if (code === 0) { |
| 127 | + resolve({ cancelled: false }); |
| 128 | + } else { |
| 129 | + // Detect wrong password from stderr |
| 130 | + const isWrongPassword = |
| 131 | + stderrOutput.includes("Sorry, try again") || |
| 132 | + stderrOutput.includes("incorrect password") || |
| 133 | + stderrOutput.includes("Authentication failure"); |
| 134 | + |
| 135 | + const errorMessage = isWrongPassword |
| 136 | + ? "Incorrect sudo password" |
| 137 | + : `sudo command failed with exit code ${code}`; |
| 138 | + |
| 139 | + reject(new Error(errorMessage)); |
| 140 | + } |
| 141 | + }); |
| 142 | + |
| 143 | + child.on("error", (error) => { |
| 144 | + disposeCancel?.dispose(); |
| 145 | + reject(error); |
| 146 | + }); |
| 147 | + }); |
| 148 | +} |
| 149 | + |
| 150 | +export async function spawnElevatedLinux( |
| 151 | + options: SpawnElevatedLinuxOptions, |
| 152 | +): Promise<{ cancelled: boolean }> { |
| 153 | + // Try pkexec first (works with graphical polkit agents) |
| 154 | + const pkexecResult = await tryPkexec(options); |
| 155 | + if (pkexecResult.success) { |
| 156 | + return { cancelled: pkexecResult.cancelled }; |
| 157 | + } |
| 158 | + |
| 159 | + // Fallback: prompt for sudo password via VS Code input |
| 160 | + options.outputChannel.info( |
| 161 | + "pkexec not available, falling back to sudo password prompt", |
| 162 | + ); |
| 163 | + return spawnWithSudoPassword(options); |
| 164 | +} |
| 165 | + |
75 | 166 | export async function spawnElevatedWindows({ |
76 | 167 | script, |
77 | 168 | outputChannel, |
|
0 commit comments