Debian Codegraph @

pkg/storaged/crypto/actions.jsx

raw
1 /*
2 * Copyright (C) 2023 Red Hat, Inc.
3 * SPDX-License-Identifier: LGPL-2.1-or-later
4 */
5
6 import cockpit from "cockpit";
7 import client from "../client";
8
9 import { get_existing_passphrase, unlock_with_type } from "./keyslots.jsx";
10 import { set_crypto_auto_option, get_active_usage, teardown_active_usage, block_name } from "../utils.js";
11 import { dialog_open, PassInput, init_teardown_usage, TeardownMessage, BlockingMessage } from "../dialog.jsx";
12
13 const _ = cockpit.gettext;
14
15 export function unlock(block) {
16 const crypto = client.blocks_crypto[block.path];
17 if (!crypto)
18 return;
19
20 function unlock_with_passphrase() {
21 const crypto = client.blocks_crypto[block.path];
22 if (!crypto)
23 return;
24
25 dialog_open({
26 Title: _("Unlock"),
27 Fields: [
28 PassInput("passphrase", _("Passphrase"), {})
29 ],
30 Action: {
31 Title: _("Unlock"),
32 action: async function (vals) {
33 await unlock_with_type(client, block, vals.passphrase, null);
34 await set_crypto_auto_option(block, true);
35 }
36 }
37 });
38 }
39
40 return get_existing_passphrase(block, true).then(type => {
41 return (unlock_with_type(client, block, null, type)
42 .then(() => set_crypto_auto_option(block, true))
43 .catch(() => unlock_with_passphrase()));
44 });
45 }
46
47 export function lock(block) {
48 const crypto = client.blocks_crypto[block.path];
49 if (!crypto)
50 return;
51
52 const name = block_name(block);
53 const usage = get_active_usage(client, block.path, _("lock"));
54
55 if (usage.Blocking) {
56 dialog_open({
57 Title: cockpit.format(_("$0 is in use"), name),
58 Body: BlockingMessage(usage)
59 });
60 return;
61 }
62
63 dialog_open({
64 Title: cockpit.format(_("Lock $0?"), name),
65 Teardown: TeardownMessage(usage),
66 Action: {
67 Title: _("Lock"),
68 action: async function () {
69 await teardown_active_usage(client, usage);
70 await crypto.Lock({});
71 await set_crypto_auto_option(block, false);
72 }
73 },
74 Inits: [
75 init_teardown_usage(client, usage)
76 ]
77 });
78 }
79
80 export function std_lock_action(backing_block, content_block) {
81 if (backing_block == content_block)
82 return null;
83
84 return { title: _("Lock"), action: () => lock(backing_block) };
85 }