Debian Codegraph @

pkg/users/authorized-keys.js

raw
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 import cockpit from "cockpit";
3
4 import lister from "./ssh-list-public-keys.sh";
5 import adder from "./ssh-add-public-key.sh";
6
7 const _ = cockpit.gettext;
8
9 function AuthorizedKeys (user_name, home_dir) {
10 const self = this;
11 const dir = home_dir + "/.ssh";
12 const filename = dir + "/authorized_keys";
13 let file = null;
14 let watch = null;
15 let last_tag = null;
16
17 cockpit.event_target(self);
18
19 self.keys = [];
20 self.state = "loading";
21
22 function process_failure (ex) {
23 self.keys = [];
24 if (ex.problem == "access-denied") {
25 self.state = ex.problem;
26 } else if (ex.problem == "not-found") {
27 self.state = "ready";
28 } else {
29 self.state = "failed";
30 console.warn("Error processing authentication keys: " + ex);
31 }
32 self.dispatchEvent("changed");
33 }
34
35 function update_keys(keys, tag) {
36 if (tag !== last_tag)
37 return;
38
39 self.keys = keys;
40 self.state = "ready";
41 self.dispatchEvent("changed");
42 }
43
44 /*
45 * Splits up a strings like:
46 *
47 * 2048 SHA256:AAAAB3NzaC1yc2EAAAADAQ Comment Here (RSA)
48 * 2048 SHA256:AAAAB3NzaC1yc2EAAAADAQ (RSA)
49 */
50 const PUBKEY_RE = /^(\S+)\s+(\S+)\s+(.*)\((\S+)\)$/;
51
52 function parse_pubkeys(input) {
53 const keys = [];
54
55 return cockpit.script(lister)
56 .input(input + "\n")
57 .then(output => {
58 const lines = output.split("\n");
59
60 for (let i = 0; i + 1 < lines.length; i += 2) {
61 const obj = { raw: lines[i + 1] };
62 keys.push(obj);
63 const match = lines[i].trim().match(PUBKEY_RE);
64 obj.valid = !!match && !!obj.raw;
65 if (match) {
66 obj.size = match[1];
67 obj.fp = match[2];
68 obj.comment = match[3].trim();
69 if (obj.comment == "authorized_keys" || obj.comment == "no comment")
70 obj.comment = null;
71 obj.algorithm = match[4];
72
73 /* Old ssh-keygen versions need us to find the comment ourselves */
74 if (!obj.comment && obj.raw)
75 obj.comment = obj.raw
76 .split(" ")
77 .splice(0, 2)
78 .join(" ") || null;
79 }
80 }
81 return keys;
82 })
83 .catch(ex => { // not-covered: OS error
84 console.warn("Failed to list public keys:", ex.toString()); // not-covered: OS error
85 return []; // not-covered: OS error
86 });
87 }
88
89 function parse_keys(content, tag, ex) {
90 last_tag = tag;
91
92 if (ex)
93 return process_failure(ex);
94
95 if (!content)
96 return update_keys([], tag);
97
98 parse_pubkeys(content)
99 .then(keys => update_keys(keys, tag));
100 }
101
102 self.add_key = function(key) {
103 return parse_pubkeys(key)
104 .then(keys => {
105 const obj = keys[0];
106 if (obj?.valid) {
107 return cockpit
108 .script(adder, [user_name, home_dir], { superuser: "try", err: "message" })
109 .input(obj.raw + "\n")
110 // eslint-disable-next-line prefer-promise-reject-errors
111 .catch(ex => Promise.reject(_("Error saving authorized keys: ") + ex)); // not-covered: OS error
112 } else {
113 return Promise.reject(_("The key you provided was not valid."));
114 }
115 });
116 };
117
118 self.remove_key = function(key) {
119 return file.modify(function(content) {
120 let lines = null;
121 const new_lines = [];
122
123 if (!content)
124 return null;
125
126 lines = content.trim().split('\n');
127 for (let i = 0; i < lines.length; i++) {
128 if (lines[i] === key)
129 key = undefined;
130 else
131 new_lines.push(lines[i]);
132 }
133 return new_lines.join("\n") || null;
134 });
135 };
136
137 self.close = function() {
138 if (watch)
139 watch.remove();
140
141 if (file)
142 file.close();
143 };
144
145 file = cockpit.file(filename, { superuser: 'try' });
146 watch = file.watch(parse_keys);
147 }
148
149 export function instance(user_name, home_dir) {
150 return new AuthorizedKeys(user_name, home_dir);
151 }