1mod encrypted_password;
2
3pub use encrypted_password::{EncryptedPassword, ProcessExt};
4use util::paths::PathExt;
5
6use std::sync::OnceLock;
7use std::{ffi::OsStr, time::Duration};
8
9use anyhow::{Context as _, Result};
10use futures::channel::{mpsc, oneshot};
11use futures::{
12 AsyncBufReadExt as _, AsyncWriteExt as _, FutureExt as _, SinkExt, StreamExt, io::BufReader,
13 select_biased,
14};
15use gpui::{AsyncApp, BackgroundExecutor, Task};
16use smol::fs;
17use util::{ResultExt as _, debug_panic};
18
19use crate::encrypted_password::decrypt;
20
21/// Path to the program used for askpass
22///
23/// On Unix and remote servers, this defaults to the current executable
24/// On Windows, this is set to the CLI variant of zed
25static ASKPASS_PROGRAM: OnceLock<std::path::PathBuf> = OnceLock::new();
26
27#[derive(PartialEq, Eq)]
28pub enum AskPassResult {
29 CancelledByUser,
30 Timedout,
31}
32
33pub struct AskPassDelegate {
34 tx: mpsc::UnboundedSender<(String, oneshot::Sender<EncryptedPassword>)>,
35 _task: Task<()>,
36}
37
38impl AskPassDelegate {
39 pub fn new(
40 cx: &mut AsyncApp,
41 password_prompt: impl Fn(String, oneshot::Sender<EncryptedPassword>, &mut AsyncApp)
42 + Send
43 + Sync
44 + 'static,
45 ) -> Self {
46 let (tx, mut rx) = mpsc::unbounded::<(String, oneshot::Sender<_>)>();
47 let task = cx.spawn(async move |cx: &mut AsyncApp| {
48 while let Some((prompt, channel)) = rx.next().await {
49 password_prompt(prompt, channel, cx);
50 }
51 });
52 Self { tx, _task: task }
53 }
54
55 pub async fn ask_password(&mut self, prompt: String) -> Option<EncryptedPassword> {
56 let (tx, rx) = oneshot::channel();
57 self.tx.send((prompt, tx)).await.ok()?;
58 rx.await.ok()
59 }
60}
61
62pub struct AskPassSession {
63 #[cfg(not(target_os = "windows"))]
64 script_path: std::path::PathBuf,
65 #[cfg(target_os = "windows")]
66 askpass_helper: String,
67 #[cfg(target_os = "windows")]
68 secret: std::sync::Arc<OnceLock<EncryptedPassword>>,
69 _askpass_task: Task<()>,
70 askpass_opened_rx: Option<oneshot::Receiver<()>>,
71 askpass_kill_master_rx: Option<oneshot::Receiver<()>>,
72}
73
74#[cfg(not(target_os = "windows"))]
75const ASKPASS_SCRIPT_NAME: &str = "askpass.sh";
76#[cfg(target_os = "windows")]
77const ASKPASS_SCRIPT_NAME: &str = "askpass.ps1";
78
79impl AskPassSession {
80 /// This will create a new AskPassSession.
81 /// You must retain this session until the master process exits.
82 #[must_use]
83 pub async fn new(executor: &BackgroundExecutor, mut delegate: AskPassDelegate) -> Result<Self> {
84 use net::async_net::UnixListener;
85 use util::fs::make_file_executable;
86
87 #[cfg(target_os = "windows")]
88 let secret = std::sync::Arc::new(OnceLock::new());
89 let temp_dir = tempfile::Builder::new().prefix("zed-askpass").tempdir()?;
90 let askpass_socket = temp_dir.path().join("askpass.sock");
91 let askpass_script_path = temp_dir.path().join(ASKPASS_SCRIPT_NAME);
92 let (askpass_opened_tx, askpass_opened_rx) = oneshot::channel::<()>();
93 let listener = UnixListener::bind(&askpass_socket).context("creating askpass socket")?;
94
95 let current_exec =
96 std::env::current_exe().context("Failed to determine current zed executable path.")?;
97
98 let askpass_program = ASKPASS_PROGRAM
99 .get_or_init(|| current_exec)
100 .try_shell_safe()
101 .context("Failed to shell-escape Askpass program path.")?
102 .to_string();
103
104 let (askpass_kill_master_tx, askpass_kill_master_rx) = oneshot::channel::<()>();
105 let mut kill_tx = Some(askpass_kill_master_tx);
106
107 #[cfg(target_os = "windows")]
108 let askpass_secret = secret.clone();
109 let askpass_task = executor.spawn(async move {
110 let mut askpass_opened_tx = Some(askpass_opened_tx);
111
112 while let Ok((mut stream, _)) = listener.accept().await {
113 if let Some(askpass_opened_tx) = askpass_opened_tx.take() {
114 askpass_opened_tx.send(()).ok();
115 }
116 let mut buffer = Vec::new();
117 let mut reader = BufReader::new(&mut stream);
118 if reader.read_until(b'\0', &mut buffer).await.is_err() {
119 buffer.clear();
120 }
121 let prompt = String::from_utf8_lossy(&buffer);
122 if let Some(password) = delegate.ask_password(prompt.into_owned()).await {
123 #[cfg(target_os = "windows")]
124 {
125 askpass_secret.get_or_init(|| password.clone());
126 }
127 if let Ok(decrypted) = decrypt(password) {
128 stream.write_all(decrypted.as_bytes()).await.log_err();
129 }
130 } else {
131 if let Some(kill_tx) = kill_tx.take() {
132 kill_tx.send(()).log_err();
133 }
134 // note: we expect the caller to drop this task when it's done.
135 // We need to keep the stream open until the caller is done to avoid
136 // spurious errors from ssh.
137 std::future::pending::<()>().await;
138 drop(stream);
139 }
140 }
141 drop(temp_dir)
142 });
143
144 // Create an askpass script that communicates back to this process.
145 let askpass_script = generate_askpass_script(&askpass_program, &askpass_socket);
146 fs::write(&askpass_script_path, askpass_script)
147 .await
148 .with_context(|| format!("creating askpass script at {askpass_script_path:?}"))?;
149 make_file_executable(&askpass_script_path).await?;
150 #[cfg(target_os = "windows")]
151 let askpass_helper = format!(
152 "powershell.exe -ExecutionPolicy Bypass -File {}",
153 askpass_script_path.display()
154 );
155
156 Ok(Self {
157 #[cfg(not(target_os = "windows"))]
158 script_path: askpass_script_path,
159
160 #[cfg(target_os = "windows")]
161 secret,
162 #[cfg(target_os = "windows")]
163 askpass_helper,
164
165 _askpass_task: askpass_task,
166 askpass_kill_master_rx: Some(askpass_kill_master_rx),
167 askpass_opened_rx: Some(askpass_opened_rx),
168 })
169 }
170
171 #[cfg(not(target_os = "windows"))]
172 pub fn script_path(&self) -> impl AsRef<OsStr> {
173 &self.script_path
174 }
175
176 #[cfg(target_os = "windows")]
177 pub fn script_path(&self) -> impl AsRef<OsStr> {
178 &self.askpass_helper
179 }
180
181 // This will run the askpass task forever, resolving as many authentication requests as needed.
182 // The caller is responsible for examining the result of their own commands and cancelling this
183 // future when this is no longer needed. Note that this can only be called once, but due to the
184 // drop order this takes an &mut, so you can `drop()` it after you're done with the master process.
185 pub async fn run(&mut self) -> AskPassResult {
186 // This is the default timeout setting used by VSCode.
187 let connection_timeout = Duration::from_secs(17);
188 let askpass_opened_rx = self.askpass_opened_rx.take().expect("Only call run once");
189 let askpass_kill_master_rx = self
190 .askpass_kill_master_rx
191 .take()
192 .expect("Only call run once");
193
194 select_biased! {
195 _ = askpass_opened_rx.fuse() => {
196 // Note: this await can only resolve after we are dropped.
197 askpass_kill_master_rx.await.ok();
198 AskPassResult::CancelledByUser
199 }
200
201 _ = futures::FutureExt::fuse(smol::Timer::after(connection_timeout)) => {
202 AskPassResult::Timedout
203 }
204 }
205 }
206
207 /// This will return the password that was last set by the askpass script.
208 #[cfg(target_os = "windows")]
209 pub fn get_password(&self) -> Option<EncryptedPassword> {
210 self.secret.get().cloned()
211 }
212}
213
214/// The main function for when Zed is running in netcat mode for use in askpass.
215/// Called from both the remote server binary and the zed binary in their respective main functions.
216pub fn main(socket: &str) {
217 use net::UnixStream;
218 use std::io::{self, Read, Write};
219 use std::process::exit;
220
221 let mut stream = match UnixStream::connect(socket) {
222 Ok(stream) => stream,
223 Err(err) => {
224 eprintln!("Error connecting to socket {}: {}", socket, err);
225 exit(1);
226 }
227 };
228
229 let mut buffer = Vec::new();
230 if let Err(err) = io::stdin().read_to_end(&mut buffer) {
231 eprintln!("Error reading from stdin: {}", err);
232 exit(1);
233 }
234
235 #[cfg(target_os = "windows")]
236 while buffer.last().is_some_and(|&b| b == b'\n' || b == b'\r') {
237 buffer.pop();
238 }
239 if buffer.last() != Some(&b'\0') {
240 buffer.push(b'\0');
241 }
242
243 if let Err(err) = stream.write_all(&buffer) {
244 eprintln!("Error writing to socket: {}", err);
245 exit(1);
246 }
247
248 let mut response = Vec::new();
249 if let Err(err) = stream.read_to_end(&mut response) {
250 eprintln!("Error reading from socket: {}", err);
251 exit(1);
252 }
253
254 if let Err(err) = io::stdout().write_all(&response) {
255 eprintln!("Error writing to stdout: {}", err);
256 exit(1);
257 }
258}
259
260pub fn set_askpass_program(path: std::path::PathBuf) {
261 if ASKPASS_PROGRAM.set(path).is_err() {
262 debug_panic!("askpass program has already been set");
263 }
264}
265
266#[inline]
267#[cfg(not(target_os = "windows"))]
268fn generate_askpass_script(askpass_program: &str, askpass_socket: &std::path::Path) -> String {
269 format!(
270 "{shebang}\n{print_args} | {askpass_program} --askpass={askpass_socket} 2> /dev/null \n",
271 askpass_socket = askpass_socket.display(),
272 print_args = "printf '%s\\0' \"$@\"",
273 shebang = "#!/bin/sh",
274 )
275}
276
277#[inline]
278#[cfg(target_os = "windows")]
279fn generate_askpass_script(askpass_program: &str, askpass_socket: &std::path::Path) -> String {
280 format!(
281 r#"
282 $ErrorActionPreference = 'Stop';
283 ($args -join [char]0) | & "{askpass_program}" --askpass={askpass_socket} 2> $null
284 "#,
285 askpass_socket = askpass_socket.display(),
286 )
287}