Debian Codegraph @

pkg/systemd/overview-cards/configurationCard.jsx

raw
1 /*
2 * Copyright (C) 2019 Red Hat, Inc.
3 * SPDX-License-Identifier: LGPL-2.1-or-later
4 */
5 import React, { useState } from 'react';
6 import { Card, CardBody, CardTitle } from "@patternfly/react-core/dist/esm/components/Card/index.js";
7 import { Button } from "@patternfly/react-core/dist/esm/components/Button/index.js";
8 import {
9 Modal, ModalBody, ModalFooter, ModalHeader
10 } from '@patternfly/react-core/dist/esm/components/Modal/index.js';
11 import { Alert } from "@patternfly/react-core/dist/esm/components/Alert/index.js";
12 import { Form, FormGroup, FormHelperText } from "@patternfly/react-core/dist/esm/components/Form/index.js";
13 import { HelperText, HelperTextItem, } from "@patternfly/react-core/dist/esm/components/HelperText/index.js";
14 import { List, ListItem } from "@patternfly/react-core/dist/esm/components/List/index.js";
15 import { TextInput } from "@patternfly/react-core/dist/esm/components/TextInput/index.js";
16
17 import host_keys_script from "./ssh-list-host-keys.sh";
18 import cockpit from "cockpit";
19 import { superuser } from "superuser";
20 import { useObject, useEvent } from "hooks.js";
21 import { EmptyStatePanel } from "cockpit-components-empty-state.jsx";
22 import { ServerTimeConfig } from 'serverTime.js';
23 import { RealmdClient, RealmButton } from "./realmd.jsx";
24 import { TunedPerformanceProfile } from './tuned-dialog.jsx';
25 import { CryptoPolicyRow } from './cryptoPolicies.jsx';
26 import { useDialogs } from "dialogs.jsx";
27 import { useInit } from "hooks";
28
29 import "./configurationCard.scss";
30
31 const _ = cockpit.gettext;
32
33 export const ConfigurationCard = ({ hostname }) => {
34 const Dialogs = useDialogs();
35 const realmd_client = useObject(() => new RealmdClient(), null, []);
36 useEvent(realmd_client, "changed");
37
38 const hostname_button = (superuser.allowed && realmd_client.allowHostnameChange())
39 ? (
40 <Button id="system_information_hostname_button" variant="link"
41 onClick={() => Dialogs.show(<PageSystemInformationChangeHostname />)}
42 isInline aria-label="edit hostname">
43 {hostname !== "" ? _("edit") : _("Set hostname")}
44 </Button>)
45 : null;
46
47 return (
48 <Card className="system-configuration">
49 <CardTitle>{_("Configuration")}</CardTitle>
50 <CardBody>
51 <table className="pf-v6-c-table pf-m-grid-md pf-m-compact">
52 <tbody className="pf-v6-c-table__tbody">
53 <tr className="pf-v6-c-table__tr">
54 <th className="pf-v6-c-table__th" scope="row">{_("Hostname")}</th>
55 <td className="pf-v6-c-table__td">
56 {hostname && <span id="system_information_hostname_text">{hostname}</span>}
57 <span>{hostname_button}</span>
58 </td>
59 </tr>
60
61 <tr className="pf-v6-c-table__tr">
62 <th className="pf-v6-c-table__th" scope="row">{_("System time")}</th>
63 <td className="pf-v6-c-table__td" data-label="system-time"><ServerTimeConfig /></td>
64 </tr>
65
66 <tr className="pf-v6-c-table__tr">
67 <th className="pf-v6-c-table__th" scope="row">{_("Domain")}</th>
68 <td className="pf-v6-c-table__td"><RealmButton realmd_client={realmd_client} /></td>
69 </tr>
70
71 <tr className="pf-v6-c-table__tr">
72 <th className="pf-v6-c-table__th" scope="row">{_("Performance profile")}</th>
73 <td className="pf-v6-c-table__td"><TunedPerformanceProfile /></td>
74 </tr>
75
76 <CryptoPolicyRow />
77
78 <tr className="pf-v6-c-table__tr">
79 <th className="pf-v6-c-table__th" scope="row">{_("Secure shell keys")}</th>
80 <td className="pf-v6-c-table__td">
81 <Button variant="link" isInline id="system-ssh-keys-link"
82 onClick={() => Dialogs.show(<SystemInformationSshKeys />)}>
83 {_("Show fingerprints")}
84 </Button>
85 </td>
86 </tr>
87 </tbody>
88 </table>
89 </CardBody>
90 </Card>
91 );
92 };
93
94 const SystemInformationSshKeys = () => {
95 const Dialogs = useDialogs();
96 const [keys, setKeys] = useState([]);
97 const [error, setError] = useState("");
98 const [loading, setLoading] = useState(true);
99
100 function keysUpdate() {
101 cockpit.script(host_keys_script, [], { superuser: "try", err: "message" })
102 .then(data => {
103 const seen = {};
104 const keys = {};
105
106 data.trim().split("\n")
107 .forEach(line => {
108 if (!line)
109 return;
110
111 const parts = line.trim().split(" ");
112 const fp = parts[1];
113 if (!seen[fp]) {
114 seen[fp] = fp;
115 let title = parts[parts.length - 1];
116 if (title) {
117 const m = title.match(/^\((.*)\)$/);
118 if (m && m[1])
119 title = m[1];
120 }
121 if (!keys[title])
122 keys[title] = [];
123 keys[title].push(fp);
124 }
125 });
126
127 let arr = Object.keys(keys);
128 arr.sort();
129 arr = arr.map(function(k) {
130 return { title: k, fps: keys[k] };
131 });
132
133 setKeys(arr);
134 setLoading(false);
135 setError("");
136 })
137 .catch(function(ex) {
138 setLoading(false);
139 setError(cockpit.format(_("failed to list ssh host keys: $0"), ex.message));
140 });
141 }
142
143 function create_keyUpdater() {
144 /*
145 * Yes, we do refresh the keys while the dialog is open.
146 * It may occur that sshd is not running at the point when
147 * we try, or in rare cases the keys may change.
148 */
149 keysUpdate();
150 return window.setInterval(keysUpdate, 10 * 1000);
151 }
152
153 function destroy_keyUpdater(interval) {
154 window.clearInterval(interval);
155 }
156
157 useObject(create_keyUpdater, destroy_keyUpdater, []);
158
159 let body = null;
160 if (error)
161 body = <Alert variant='danger' isInline title={_("Loading of SSH keys failed")}>
162 <p>{_("Error message")}: {error}</p>
163 </Alert>;
164 else if (loading)
165 body = <EmptyStatePanel loading title={ _("Loading keys...") } />;
166 else if (!keys.length)
167 body = <EmptyStatePanel title={ _("No host keys found.") } />;
168 else
169 body = <List isPlain isBordered>
170 {keys.map(key =>
171 <ListItem key={key.title}>
172 <h4>{key.title}</h4>
173 {key.fps.map((fp, i) => <div key={i}><small>{fp}</small></div>)}
174 </ListItem>
175 )}
176 </List>;
177
178 return (
179 <Modal isOpen position="top" variant="medium"
180 onClose={Dialogs.close}
181 id="system_information_ssh_keys"
182 >
183 <ModalHeader title={_("Machine SSH key fingerprints")} />
184 <ModalBody>
185 {body}
186 </ModalBody>
187 <ModalFooter>
188 <Button variant='secondary' onClick={Dialogs.close}>{_("Close")}</Button>
189 </ModalFooter>
190 </Modal>
191 );
192 };
193
194 const PageSystemInformationChangeHostname = () => {
195 const Dialogs = useDialogs();
196 const [update_from_pretty, set_update_from_pretty] = useState(true);
197 const [init_hostname, set_init_hostname] = useState("");
198 const [hostname, set_hostname] = useState("");
199 const [proxy, set_proxy] = useState(null);
200 const [pretty, set_pretty] = useState("");
201 const [init_pretty, set_init_pretty] = useState("");
202 const [error, set_error] = useState([]);
203
204 useInit(() => {
205 const client = cockpit.dbus('org.freedesktop.hostname1', { superuser: "try" });
206 const hostname_proxy = client.proxy();
207
208 hostname_proxy.wait()
209 .then(() => {
210 const initial_hostname = hostname_proxy.StaticHostname || "";
211 const initial_pretty_hostname = hostname_proxy.PrettyHostname || "";
212
213 set_proxy(hostname_proxy);
214 set_hostname(initial_hostname);
215 set_init_hostname(initial_hostname);
216 set_pretty(initial_pretty_hostname);
217 set_init_pretty(initial_pretty_hostname);
218 });
219 });
220
221 function onPrettyChanged(value) {
222 // Whenever the pretty host name has changed (e.g. the user has edited it), we compute a new
223 // simple host name (e.g. 7bit ASCII, no special chars/spaces, lower case) from it
224
225 set_pretty(value);
226
227 if (update_from_pretty) {
228 const old_hostname = hostname;
229 const first_dot = old_hostname.indexOf(".");
230 let new_hostname = value
231 .toLowerCase()
232 .replace(/['".]+/g, "")
233 .replace(/[^a-zA-Z0-9]+/g, "-");
234 new_hostname = new_hostname.substring(0, 64);
235 if (first_dot >= 0)
236 new_hostname = new_hostname + old_hostname.substring(first_dot);
237 set_hostname(new_hostname);
238 }
239 }
240
241 function onHostnameChanged(value) {
242 const error = [];
243 if (value.length > 64)
244 error.push(_("Real host name must be 64 characters or less"));
245 if (value.match(/[.a-z0-9-]*/)[0] !== value || value.indexOf("..") !== -1)
246 error.push(_("Real host name can only contain lower-case characters, digits, dashes, and periods (with populated subdomains)"));
247
248 set_hostname(value);
249 set_update_from_pretty(false);
250 set_error(error);
251 }
252
253 function onSubmit(event) {
254 const one = proxy.call("SetStaticHostname", [hostname, true]);
255 const two = proxy.call("SetPrettyHostname", [pretty, true]);
256
257 Promise.all([one, two]).then(Dialogs.close);
258
259 if (event)
260 event.preventDefault();
261 return false;
262 }
263
264 const disabled = error.length || (init_hostname == hostname && init_pretty == pretty);
265 return (
266 <Modal isOpen position="top" variant="medium"
267 onClose={Dialogs.close}
268 id="system_information_change_hostname"
269 >
270 <ModalHeader title={_("Change host name")} />
271 <ModalBody>
272 <Form isHorizontal onSubmit={onSubmit}>
273 <FormGroup fieldId="sich-pretty-hostname" label={_("Pretty host name")}>
274 <TextInput id="sich-pretty-hostname" value={pretty} onChange={(_event, value) => onPrettyChanged(value)} />
275 </FormGroup>
276 <FormGroup fieldId="sich-hostname" label={_("Real host name")}>
277 <TextInput id="sich-hostname" value={hostname} onChange={(_event, value) => onHostnameChanged(value)} validated={error.length ? "error" : "default"} />
278 {error.length > 0 && <FormHelperText>
279 <HelperText>
280 {error.map((err, i) =>
281 <HelperTextItem key={i} variant="error">
282 {err}
283 </HelperTextItem>
284 )}
285 </HelperText>
286 </FormHelperText>}
287 </FormGroup>
288 </Form>
289 </ModalBody>
290 <ModalFooter>
291 <Button variant='primary' isDisabled={disabled} onClick={onSubmit}>{_("Change")}</Button>
292 <Button variant='link' onClick={Dialogs.close}>{_("Cancel")}</Button>
293 </ModalFooter>
294 </Modal>
295 );
296 };