| 1 |
/* |
| 2 |
* Copyright (C) 2015 Red Hat, Inc. |
| 3 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
*/ |
| 5 |
|
| 6 |
import cockpit, { SpawnOptions } from "cockpit"; |
| 7 |
|
| 8 |
// @ts-expect-error: magic verbatim string import, not a JS module |
| 9 |
import lister from "credentials-ssh-private-keys.sh"; |
| 10 |
// @ts-expect-error: magic verbatim string import, not a JS module |
| 11 |
import remove_key from "credentials-ssh-remove-key.sh"; |
| 12 |
|
| 13 |
const _ = cockpit.gettext; |
| 14 |
|
| 15 |
export interface Key { |
| 16 |
type: string; |
| 17 |
comment: string; |
| 18 |
data: string; |
| 19 |
name?: string; |
| 20 |
loaded?: boolean; |
| 21 |
agent_only?: boolean; |
| 22 |
size?: number | null; |
| 23 |
fingerprint?: string; |
| 24 |
} |
| 25 |
|
| 26 |
export class KeyLoadError extends Error { |
| 27 |
sent_password: boolean; |
| 28 |
|
| 29 |
constructor(sent_password: boolean, message: string) { |
| 30 |
super(message); |
| 31 |
this.sent_password = sent_password; |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
export class Keys extends EventTarget { |
| 36 |
path: string | null = null; |
| 37 |
items: Record<string, Key> = { }; |
| 38 |
|
| 39 |
#p_have_path: Promise<void>; |
| 40 |
|
| 41 |
constructor() { |
| 42 |
super(); |
| 43 |
this.#p_have_path = cockpit.user() |
| 44 |
.then(user => { |
| 45 |
this.path = user.home + '/.ssh'; |
| 46 |
this.#refresh(); |
| 47 |
}); |
| 48 |
} |
| 49 |
|
| 50 |
#proc: cockpit.Spawn<string> | null = null; |
| 51 |
#timeout: number | null = null; |
| 52 |
|
| 53 |
#refresh(): void { |
| 54 |
if (this.#proc || !this.path) |
| 55 |
return; |
| 56 |
|
| 57 |
if (this.#timeout) |
| 58 |
window.clearTimeout(this.#timeout); |
| 59 |
this.#timeout = null; |
| 60 |
|
| 61 |
this.#proc = cockpit.script(lister, [this.path], { err: "message" }); |
| 62 |
this.#proc |
| 63 |
.then(data => this.#process(data)) |
| 64 |
.catch(ex => console.warn("failed to list keys in home directory: " + ex.message)) |
| 65 |
.finally(() => { |
| 66 |
this.#proc = null; |
| 67 |
|
| 68 |
if (!this.#timeout) |
| 69 |
this.#timeout = window.setTimeout(() => this.#refresh(), 5000); |
| 70 |
}); |
| 71 |
} |
| 72 |
|
| 73 |
#process(data: string): void { |
| 74 |
const blocks = data.split('\v'); |
| 75 |
let key: Key | undefined; |
| 76 |
const items = { }; |
| 77 |
|
| 78 |
/* First block is the data from ssh agent */ |
| 79 |
blocks[0].trim().split("\n") |
| 80 |
.forEach(line => { |
| 81 |
key = this.#parse_key(line, items); |
| 82 |
if (key) |
| 83 |
key.loaded = true; |
| 84 |
}); |
| 85 |
|
| 86 |
/* Next come individual triples of blocks */ |
| 87 |
blocks.slice(1).forEach((block, i) => { |
| 88 |
switch (i % 3) { |
| 89 |
case 0: |
| 90 |
key = this.#parse_key(block, items); |
| 91 |
break; |
| 92 |
case 1: |
| 93 |
if (key) { |
| 94 |
block = block.trim(); |
| 95 |
if (block.slice(-4) === ".pub") |
| 96 |
key.name = block.slice(0, -4); |
| 97 |
else if (block) |
| 98 |
key.name = block; |
| 99 |
else |
| 100 |
key.agent_only = true; |
| 101 |
} |
| 102 |
break; |
| 103 |
case 2: |
| 104 |
if (key) |
| 105 |
this.#parse_info(block, key); |
| 106 |
break; |
| 107 |
} |
| 108 |
}); |
| 109 |
|
| 110 |
this.items = items; |
| 111 |
this.dispatchEvent(new CustomEvent("changed")); |
| 112 |
} |
| 113 |
|
| 114 |
#parse_key(line: string, items: Record<string, Key>): Key | undefined { |
| 115 |
const parts = line.trim().split(" "); |
| 116 |
let id; |
| 117 |
let type; |
| 118 |
let comment; |
| 119 |
|
| 120 |
/* SSHv1 keys */ |
| 121 |
if (!isNaN(parseInt(parts[0], 10))) { |
| 122 |
id = parts[2]; |
| 123 |
type = "RSA1"; |
| 124 |
comment = parts.slice(3).join(" "); |
| 125 |
} else if (parts[0].indexOf("ssh-") === 0) { |
| 126 |
id = parts[1]; |
| 127 |
type = parts[0].substring(4).toUpperCase(); |
| 128 |
comment = parts.slice(2).join(" "); |
| 129 |
} else if (parts[0].indexOf("ecdsa-") === 0) { |
| 130 |
id = parts[1]; |
| 131 |
type = "ECDSA"; |
| 132 |
comment = parts.slice(2).join(" "); |
| 133 |
} else { |
| 134 |
return; |
| 135 |
} |
| 136 |
|
| 137 |
let key = items[id]; |
| 138 |
if (key) { |
| 139 |
key.type = type; |
| 140 |
key.comment = comment; |
| 141 |
key.data = line; |
| 142 |
} else { |
| 143 |
key = items[id] = { |
| 144 |
type, |
| 145 |
comment, |
| 146 |
data: line, |
| 147 |
}; |
| 148 |
} |
| 149 |
|
| 150 |
return key; |
| 151 |
} |
| 152 |
|
| 153 |
#parse_info(line: string, key: Key): void { |
| 154 |
const parts = line.trim().split(" ") |
| 155 |
.filter(n => !!n); |
| 156 |
|
| 157 |
key.size = parseInt(parts[0], 10); |
| 158 |
if (isNaN(key.size)) |
| 159 |
key.size = null; |
| 160 |
|
| 161 |
key.fingerprint = parts[1]; |
| 162 |
|
| 163 |
if (!key.name && parts[2] && parts[2].indexOf("/") !== -1) |
| 164 |
key.name = parts[2]; |
| 165 |
} |
| 166 |
|
| 167 |
async #run_keygen(file: string, new_type: string | null, old_pass: string | null, new_pass: string): Promise<void> { |
| 168 |
const old_exps = [/.*Enter old passphrase: $/]; |
| 169 |
const new_exps = [/.*Enter passphrase.*/, /.*Enter new passphrase.*/, /.*Enter same passphrase again: $/]; |
| 170 |
const bad_exps = [/.*failed: passphrase is too short.*/]; |
| 171 |
|
| 172 |
let buffer = ""; |
| 173 |
let sent_new = false; |
| 174 |
let failure = _("No such file or directory"); |
| 175 |
|
| 176 |
// Exactly one of new_type or old_pass must be given |
| 177 |
console.assert((new_type == null) != (old_pass == null)); |
| 178 |
|
| 179 |
const cmd = ["ssh-keygen", "-f", file]; |
| 180 |
if (new_type) |
| 181 |
cmd.push("-t", new_type); |
| 182 |
else |
| 183 |
cmd.push("-p"); |
| 184 |
|
| 185 |
await this.#p_have_path; |
| 186 |
cockpit.assert(this.path); |
| 187 |
|
| 188 |
const proc = cockpit.spawn(cmd, { pty: true, environ: ["LC_ALL=C"], err: "out", directory: this.path }); |
| 189 |
|
| 190 |
proc.stream(data => { |
| 191 |
buffer += data; |
| 192 |
if (old_pass && old_exps.some(exp => exp.test(buffer))) { |
| 193 |
buffer = ""; |
| 194 |
failure = _("Old password not accepted"); |
| 195 |
proc.input(old_pass + "\n", true); |
| 196 |
return; |
| 197 |
} |
| 198 |
|
| 199 |
if (new_exps.some(exp => exp.test(buffer))) { |
| 200 |
buffer = ""; |
| 201 |
proc.input(new_pass + "\n", true); |
| 202 |
failure = _("Failed to change password"); |
| 203 |
sent_new = true; |
| 204 |
return; |
| 205 |
} |
| 206 |
|
| 207 |
if (sent_new && bad_exps.some(exp => exp.test(buffer))) { |
| 208 |
failure = _("New password was not accepted"); |
| 209 |
} |
| 210 |
}); |
| 211 |
|
| 212 |
const timeout = window.setTimeout(() => { |
| 213 |
failure = _("Prompting via ssh-keygen timed out"); |
| 214 |
proc.close("terminated"); |
| 215 |
}, 10 * 1000); |
| 216 |
|
| 217 |
try { |
| 218 |
await proc; |
| 219 |
} catch (ex) { |
| 220 |
if (ex instanceof cockpit.ProcessError && ex.exit_status) |
| 221 |
throw new Error(failure); |
| 222 |
throw ex; |
| 223 |
} finally { |
| 224 |
window.clearInterval(timeout); |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
async change(name: string, old_pass: string, new_pass: string): Promise<void> { |
| 229 |
await this.#run_keygen(name, null, old_pass, new_pass); |
| 230 |
} |
| 231 |
|
| 232 |
async create(name: string, type: string, new_pass: string): Promise<void> { |
| 233 |
// ensure ~/.ssh directory exists |
| 234 |
await cockpit.script('dir=$(dirname "$1"); test -e "$dir" || mkdir -m 700 "$dir"', [name]); |
| 235 |
await this.#run_keygen(name, type, null, new_pass); |
| 236 |
} |
| 237 |
|
| 238 |
async get_pubkey(name: string): Promise<string> { |
| 239 |
return await cockpit.file(name + ".pub").read(); |
| 240 |
} |
| 241 |
|
| 242 |
async load(name: string, password: string): Promise<void> { |
| 243 |
const ask_exp = /.*Enter passphrase for .*/; |
| 244 |
const perm_exp = /.*UNPROTECTED PRIVATE KEY FILE.*/; |
| 245 |
const bad_exp = /.*Bad passphrase.*/; |
| 246 |
|
| 247 |
let buffer = ""; |
| 248 |
let output = ""; |
| 249 |
let failure = _("Not a valid private key"); |
| 250 |
let sent_password = false; |
| 251 |
|
| 252 |
await this.#p_have_path; |
| 253 |
cockpit.assert(this.path); |
| 254 |
|
| 255 |
const proc = cockpit.spawn(["ssh-add", name], |
| 256 |
{ pty: true, environ: ["LC_ALL=C"], err: "out", directory: this.path }); |
| 257 |
|
| 258 |
const timeout = window.setTimeout(() => { |
| 259 |
failure = _("Prompting via ssh-add timed out"); |
| 260 |
proc.close("terminated"); |
| 261 |
}, 10 * 1000); |
| 262 |
|
| 263 |
proc.stream(data => { |
| 264 |
buffer += data; |
| 265 |
output += data; |
| 266 |
if (perm_exp.test(buffer)) { |
| 267 |
failure = _("Invalid file permissions"); |
| 268 |
buffer = ""; |
| 269 |
} else if (ask_exp.test(buffer)) { |
| 270 |
buffer = ""; |
| 271 |
failure = _("Password not accepted"); |
| 272 |
proc.input(password + "\n", true); |
| 273 |
sent_password = true; |
| 274 |
} else if (bad_exp.test(buffer)) { |
| 275 |
buffer = ""; |
| 276 |
proc.input("\n", true); |
| 277 |
} |
| 278 |
}); |
| 279 |
|
| 280 |
try { |
| 281 |
await proc; |
| 282 |
this.#refresh(); |
| 283 |
} catch (error) { |
| 284 |
console.log(output); |
| 285 |
let ex: KeyLoadError | unknown; |
| 286 |
if (error instanceof cockpit.ProcessError && error.exit_status) { |
| 287 |
ex = new KeyLoadError(sent_password, failure); |
| 288 |
} else if (error instanceof Error) { |
| 289 |
ex = new KeyLoadError(sent_password, error.message); |
| 290 |
} else { |
| 291 |
ex = error; |
| 292 |
} |
| 293 |
throw ex; |
| 294 |
} finally { |
| 295 |
window.clearTimeout(timeout); |
| 296 |
} |
| 297 |
} |
| 298 |
|
| 299 |
async unload(key: Key): Promise<void> { |
| 300 |
await this.#p_have_path; |
| 301 |
cockpit.assert(this.path); |
| 302 |
|
| 303 |
const options: SpawnOptions & { binary?: false; } = { pty: true, err: "message", directory: this.path }; |
| 304 |
|
| 305 |
if (key.name && !key.agent_only) |
| 306 |
await cockpit.spawn(["ssh-add", "-d", key.name], options); |
| 307 |
else |
| 308 |
await cockpit.script(remove_key, [key.data], options); |
| 309 |
|
| 310 |
this.#refresh(); |
| 311 |
} |
| 312 |
|
| 313 |
close() { |
| 314 |
if (this.#proc) |
| 315 |
this.#proc.close(); |
| 316 |
if (this.#timeout) |
| 317 |
window.clearTimeout(this.#timeout); |
| 318 |
this.#timeout = null; |
| 319 |
} |
| 320 |
} |
| 321 |
|
| 322 |
export function keys_instance() { |
| 323 |
return new Keys(); |
| 324 |
} |