agentmux_srv_core/backend/remote/
askpass.rs1use std::collections::HashMap;
31use std::path::PathBuf;
32use std::sync::{Mutex, OnceLock};
33
34use sha2::{Digest, Sha256};
35
36pub const ENV_SECRET: &str = "AGENTMUX_ASKPASS_SECRET";
38
39pub const META_KEY_AGENT_BLOCK: &str = "ptyshell:askpassagentblock";
43pub const META_KEY_AGENT: &str = "ptyshell:askpassagent";
44
45#[derive(Debug, Clone, PartialEq, Eq)]
47pub struct AskpassGrant {
48 pub agent_block_id: String,
51 pub agent: String,
53 pub connection: String,
55 pub user_pane: bool,
58}
59
60fn registry() -> &'static Mutex<HashMap<[u8; 32], AskpassGrant>> {
61 static REG: OnceLock<Mutex<HashMap<[u8; 32], AskpassGrant>>> = OnceLock::new();
62 REG.get_or_init(|| Mutex::new(HashMap::new()))
63}
64
65fn digest(secret: &str) -> [u8; 32] {
68 Sha256::digest(secret.as_bytes()).into()
69}
70
71pub fn grant(grant: AskpassGrant) -> String {
73 let secret = format!(
74 "amxa_{}{}",
75 uuid::Uuid::new_v4().simple(),
76 uuid::Uuid::new_v4().simple()
77 );
78 registry()
79 .lock()
80 .unwrap_or_else(|e| e.into_inner())
81 .insert(digest(&secret), grant);
82 secret
83}
84
85pub fn lookup(secret: &str) -> Option<AskpassGrant> {
87 if secret.is_empty() {
88 return None;
89 }
90 registry()
91 .lock()
92 .unwrap_or_else(|e| e.into_inner())
93 .get(&digest(secret))
94 .cloned()
95}
96
97pub fn revoke(secret: &str) {
99 registry()
100 .lock()
101 .unwrap_or_else(|e| e.into_inner())
102 .remove(&digest(secret));
103}
104
105pub struct Revoke(pub String);
108
109impl Drop for Revoke {
110 fn drop(&mut self) {
111 revoke(&self.0);
112 }
113}
114
115#[derive(Debug, Clone, Copy, PartialEq, Eq)]
117pub enum PromptKind {
118 YesNo,
122 Secret,
124 Info,
127}
128
129pub fn classify(prompt: &str, hint: &str) -> PromptKind {
133 match hint.trim() {
134 "confirm" => return PromptKind::YesNo,
135 "none" => return PromptKind::Info,
136 _ => {}
137 }
138 let p = prompt.to_ascii_lowercase();
139 if p.contains("(yes/no") || p.contains("[yes/no") {
140 PromptKind::YesNo
141 } else {
142 PromptKind::Secret
143 }
144}
145
146pub fn ssh_env(
149 secret: &str,
150 program: &std::path::Path,
151 local_url: &str,
152 auth_key: &str,
153) -> Vec<(String, String)> {
154 vec![
155 ("SSH_ASKPASS".into(), program.to_string_lossy().into_owned()),
156 ("SSH_ASKPASS_REQUIRE".into(), "force".into()),
159 (
161 "DISPLAY".into(),
162 std::env::var("DISPLAY").unwrap_or_else(|_| ":0".into()),
163 ),
164 (ENV_SECRET.into(), secret.into()),
165 ("AGENTMUX_LOCAL_URL".into(), local_url.into()),
166 ("AGENTMUX_AUTH_KEY".into(), auth_key.into()),
167 ]
168}
169
170pub fn program() -> Option<PathBuf> {
173 use crate::backend::agent_tools::{find_in_tool_dirs, ToolDir};
174 find_in_tool_dirs("agentmux-bashwrap", &[ToolDir::Bundled, ToolDir::BesideSrv])
175 .or_else(|| which::which("agentmux-bashwrap").ok())
176}
177
178#[cfg(test)]
179mod tests {
180 use super::*;
181
182 fn g(conn: &str) -> AskpassGrant {
183 AskpassGrant {
184 agent_block_id: "blk".into(),
185 agent: "korp".into(),
186 connection: conn.into(),
187 user_pane: false,
188 }
189 }
190
191 #[test]
192 fn a_secret_answers_while_its_ssh_runs_and_not_after() {
193 let secret = grant(g("area54"));
194 assert!(secret.starts_with("amxa_") && secret.len() > 60);
195 assert_eq!(lookup(&secret), Some(g("area54")));
196 let other = grant(g("nas"));
197 assert_ne!(secret, other, "one secret per run");
198 revoke(&secret);
199 assert_eq!(lookup(&secret), None);
200 assert_eq!(
201 lookup(&other),
202 Some(g("nas")),
203 "revoking one leaves the others"
204 );
205 assert_eq!(lookup(""), None);
206 assert_eq!(lookup("amxa_guess"), None);
207 drop(Revoke(other.clone()));
208 assert_eq!(
209 lookup(&other),
210 None,
211 "the guard revokes when its ssh's waiter ends"
212 );
213 }
214
215 #[test]
216 fn a_host_key_question_is_yes_or_no_and_everything_else_is_secret() {
217 assert_eq!(
218 classify(
219 "Are you sure you want to continue connecting (yes/no/[fingerprint])? ",
220 ""
221 ),
222 PromptKind::YesNo
223 );
224 assert_eq!(
225 classify("Allow use of key id_ed25519? [yes/no]", ""),
226 PromptKind::YesNo
227 );
228 assert_eq!(classify("asaf@area54's password: ", ""), PromptKind::Secret);
229 assert_eq!(
230 classify("Enter passphrase for key '/home/u/.ssh/id_ed25519': ", ""),
231 PromptKind::Secret
232 );
233 assert_eq!(classify("Verification code: ", ""), PromptKind::Secret);
234 assert_eq!(
236 classify("Allow use of key id_ed25519?", "confirm"),
237 PromptKind::YesNo
238 );
239 assert_eq!(
240 classify("Confirm user presence for key ECDSA-SK", "none"),
241 PromptKind::Info
242 );
243 }
244
245 #[test]
246 fn the_ssh_env_forces_askpass_and_carries_only_its_secret() {
247 let env = ssh_env(
248 "amxa_x",
249 std::path::Path::new("/opt/agentmux-bashwrap"),
250 "http://127.0.0.1:1",
251 "k",
252 );
253 let get = |k: &str| env.iter().find(|(n, _)| n == k).map(|(_, v)| v.as_str());
254 assert_eq!(get("SSH_ASKPASS"), Some("/opt/agentmux-bashwrap"));
255 assert_eq!(get("SSH_ASKPASS_REQUIRE"), Some("force"));
256 assert_eq!(get(ENV_SECRET), Some("amxa_x"));
257 assert!(get("DISPLAY").is_some());
258 }
259}