| 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 React from "react"; |
| 8 |
import client from "../client"; |
| 9 |
|
| 10 |
import { DescriptionList } from "@patternfly/react-core/dist/esm/components/DescriptionList/index.js"; |
| 11 |
import { CardBody } from "@patternfly/react-core/dist/esm/components/Card/index.js"; |
| 12 |
import { useObject, useEvent } from "hooks"; |
| 13 |
import * as python from "python.js"; |
| 14 |
import * as timeformat from "timeformat"; |
| 15 |
|
| 16 |
import { dialog_open, TextInput, PassInput } from "../dialog.jsx"; |
| 17 |
import { block_name, encode_filename, decode_filename, parse_options, unparse_options, extract_option, edit_crypto_config } from "../utils.js"; |
| 18 |
import { StorageCard, StorageDescription, new_card } from "../pages.jsx"; |
| 19 |
import luksmeta_monitor_hack_py from "./luksmeta-monitor-hack.py"; |
| 20 |
import { is_mounted } from "../filesystem/utils.jsx"; |
| 21 |
import { StorageLink } from "../storage-controls.jsx"; |
| 22 |
import { CryptoKeyslots } from "./keyslots.jsx"; |
| 23 |
|
| 24 |
const _ = cockpit.gettext; |
| 25 |
|
| 26 |
export function make_encryption_card(next, block) { |
| 27 |
return new_card({ |
| 28 |
title: _("Encryption"), |
| 29 |
next, |
| 30 |
type_extra: _("encrypted"), |
| 31 |
component: EncryptionCard, |
| 32 |
props: { block }, |
| 33 |
}); |
| 34 |
} |
| 35 |
|
| 36 |
function monitor_luks(block) { |
| 37 |
const self = { |
| 38 |
stop, |
| 39 |
|
| 40 |
luks_version: null, |
| 41 |
slots: null, |
| 42 |
slot_error: null, |
| 43 |
max_slots: null, |
| 44 |
}; |
| 45 |
|
| 46 |
cockpit.event_target(self); |
| 47 |
|
| 48 |
const dev = decode_filename(block.Device); |
| 49 |
const channel = python.spawn(luksmeta_monitor_hack_py, [dev], { superuser: "require" }); |
| 50 |
let buf = ""; |
| 51 |
|
| 52 |
channel.stream(output => { |
| 53 |
buf += output; |
| 54 |
const lines = buf.split("\n"); |
| 55 |
buf = lines[lines.length - 1]; |
| 56 |
if (lines.length >= 2) { |
| 57 |
const data = JSON.parse(lines[lines.length - 2]); |
| 58 |
self.slots = data.slots; |
| 59 |
self.luks_version = data.version; |
| 60 |
self.max_slots = data.max_slots; |
| 61 |
self.dispatchEvent("changed"); |
| 62 |
} |
| 63 |
}); |
| 64 |
|
| 65 |
channel.catch(err => { |
| 66 |
self.slots = []; |
| 67 |
self.slot_error = err; |
| 68 |
self.dispatchEvent("changed"); |
| 69 |
}); |
| 70 |
|
| 71 |
function stop() { |
| 72 |
channel.close(); |
| 73 |
} |
| 74 |
|
| 75 |
return self; |
| 76 |
} |
| 77 |
|
| 78 |
function parse_tag_mtime(tag) { |
| 79 |
if (tag && tag.indexOf("1:") == 0) { |
| 80 |
try { |
| 81 |
const parts = tag.split("-")[1].split("."); |
| 82 |
// s:ns → ms |
| 83 |
const mtime = parseInt(parts[0]) * 1000 + parseInt(parts[1]) * 1e-6; |
| 84 |
return cockpit.format(_("Last modified: $0"), timeformat.dateTime(mtime)); |
| 85 |
} catch { |
| 86 |
return null; |
| 87 |
} |
| 88 |
} else |
| 89 |
return null; |
| 90 |
} |
| 91 |
|
| 92 |
function monitor_mtime(path) { |
| 93 |
const self = { |
| 94 |
stop, |
| 95 |
|
| 96 |
mtime: 0 |
| 97 |
}; |
| 98 |
|
| 99 |
cockpit.event_target(self); |
| 100 |
|
| 101 |
let file = null; |
| 102 |
if (path) { |
| 103 |
file = cockpit.file(path, { superuser: "require" }); |
| 104 |
file.watch((_, tag) => { self.mtime = parse_tag_mtime(tag); self.dispatchEvent("changed") }, |
| 105 |
{ read: false }); |
| 106 |
} |
| 107 |
|
| 108 |
function stop() { |
| 109 |
if (file) |
| 110 |
file.close(); |
| 111 |
} |
| 112 |
|
| 113 |
return self; |
| 114 |
} |
| 115 |
|
| 116 |
const EncryptionCard = ({ card, block }) => { |
| 117 |
const luks_info = useObject(() => monitor_luks(block), |
| 118 |
m => m.stop(), |
| 119 |
[block]); |
| 120 |
useEvent(luks_info, "changed"); |
| 121 |
|
| 122 |
let old_options; |
| 123 |
let passphrase_path; |
| 124 |
const old_config = block.Configuration.find(c => c[0] == "crypttab"); |
| 125 |
if (old_config) { |
| 126 |
old_options = (decode_filename(old_config[1].options.v) |
| 127 |
.split(",") |
| 128 |
.filter(function (s) { return s.indexOf("x-parent") !== 0 }) |
| 129 |
.join(",")); |
| 130 |
passphrase_path = decode_filename(old_config[1]["passphrase-path"].v); |
| 131 |
} |
| 132 |
|
| 133 |
const stored_passphrase_info = useObject(() => monitor_mtime(passphrase_path), |
| 134 |
m => m.stop(), |
| 135 |
[passphrase_path]); |
| 136 |
useEvent(stored_passphrase_info, "changed"); |
| 137 |
|
| 138 |
const split_options = parse_options(old_options); |
| 139 |
let opt_noauto = extract_option(split_options, "noauto"); |
| 140 |
const extra_options = unparse_options(split_options); |
| 141 |
|
| 142 |
function edit_stored_passphrase() { |
| 143 |
edit_crypto_config(block, function (config, commit) { |
| 144 |
dialog_open({ |
| 145 |
Title: _("Stored passphrase"), |
| 146 |
Fields: [ |
| 147 |
PassInput("passphrase", _("Stored passphrase"), |
| 148 |
{ |
| 149 |
value: (config && config['passphrase-contents'] |
| 150 |
? decode_filename(config['passphrase-contents'].v) |
| 151 |
: "") |
| 152 |
}) |
| 153 |
], |
| 154 |
Action: { |
| 155 |
Title: _("Save"), |
| 156 |
action: function (vals) { |
| 157 |
config["passphrase-contents"] = { |
| 158 |
t: 'ay', |
| 159 |
v: encode_filename(vals.passphrase) |
| 160 |
}; |
| 161 |
delete config["passphrase-path"]; |
| 162 |
return commit(); |
| 163 |
} |
| 164 |
} |
| 165 |
}); |
| 166 |
}); |
| 167 |
} |
| 168 |
|
| 169 |
function edit_options() { |
| 170 |
const fsys_config = client.blocks_crypto[block.path]?.ChildConfiguration.find(c => c[0] == "fstab"); |
| 171 |
const content_block = client.blocks_cleartext[block.path]; |
| 172 |
const is_fsys = fsys_config || (content_block && content_block.IdUsage == "filesystem"); |
| 173 |
|
| 174 |
edit_crypto_config(block, function (config, commit) { |
| 175 |
dialog_open({ |
| 176 |
Title: _("Encryption options"), |
| 177 |
Fields: [ |
| 178 |
TextInput("options", "", { value: extra_options }), |
| 179 |
], |
| 180 |
isFormHorizontal: false, |
| 181 |
Action: { |
| 182 |
Title: _("Save"), |
| 183 |
action: function (vals) { |
| 184 |
let opts = []; |
| 185 |
if (is_fsys && content_block) |
| 186 |
opt_noauto = !is_mounted(client, content_block); |
| 187 |
if (opt_noauto) |
| 188 |
opts.push("noauto"); |
| 189 |
opts = opts.concat(parse_options(vals.options)); |
| 190 |
config.options = { |
| 191 |
t: 'ay', |
| 192 |
v: encode_filename(unparse_options(opts)) |
| 193 |
}; |
| 194 |
return commit(); |
| 195 |
} |
| 196 |
} |
| 197 |
}); |
| 198 |
}); |
| 199 |
} |
| 200 |
|
| 201 |
const cleartext = client.blocks_cleartext[block.path]; |
| 202 |
|
| 203 |
const option_parts = []; |
| 204 |
if (extra_options) |
| 205 |
option_parts.push(extra_options); |
| 206 |
const options = option_parts.join(", "); |
| 207 |
|
| 208 |
return ( |
| 209 |
<StorageCard card={card}> |
| 210 |
<CardBody> |
| 211 |
<DescriptionList className="pf-m-horizontal-on-sm wide-label"> |
| 212 |
<StorageDescription title={_("Encryption type")}> |
| 213 |
{ luks_info.luks_version ? "LUKS" + luks_info.luks_version : "-" } |
| 214 |
</StorageDescription> |
| 215 |
<StorageDescription title={_("Cleartext device")}> |
| 216 |
{cleartext ? block_name(cleartext) : "-"} |
| 217 |
</StorageDescription> |
| 218 |
<StorageDescription title={_("Stored passphrase")} |
| 219 |
value={ passphrase_path ? stored_passphrase_info.mtime || _("yes") : _("none") } |
| 220 |
action={<StorageLink onClick={edit_stored_passphrase}>{_("edit")}</StorageLink>} /> |
| 221 |
<StorageDescription title={_("Options")} |
| 222 |
value={options || _("none")} |
| 223 |
action={<StorageLink onClick={edit_options}>{_("edit")}</StorageLink>} /> |
| 224 |
</DescriptionList> |
| 225 |
</CardBody> |
| 226 |
<CryptoKeyslots client={client} block={block} |
| 227 |
slots={luks_info.slots} slot_error={luks_info.slot_error} |
| 228 |
max_slots={luks_info.max_slots} /> |
| 229 |
</StorageCard>); |
| 230 |
}; |