| 1 |
/* |
| 2 |
* Copyright (C) 2020 Red Hat, Inc. |
| 3 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
*/ |
| 5 |
|
| 6 |
import React, { useState, useEffect, useRef, useMemo } from 'react'; |
| 7 |
|
| 8 |
import { Button } from "@patternfly/react-core/dist/esm/components/Button/index.js"; |
| 9 |
import { Checkbox } from "@patternfly/react-core/dist/esm/components/Checkbox/index.js"; |
| 10 |
import { Card, CardBody, CardHeader, CardTitle } from '@patternfly/react-core/dist/esm/components/Card/index.js'; |
| 11 |
import { EmptyState, EmptyStateActions, EmptyStateFooter, EmptyStateVariant } from "@patternfly/react-core/dist/esm/components/EmptyState/index.js"; |
| 12 |
import { Flex, FlexItem } from "@patternfly/react-core/dist/esm/layouts/Flex/index.js"; |
| 13 |
import { HelperText, HelperTextItem } from "@patternfly/react-core/dist/esm/components/HelperText/index.js"; |
| 14 |
import { Label, LabelGroup } from "@patternfly/react-core/dist/esm/components/Label/index.js"; |
| 15 |
import { Page, PageBreadcrumb, PageSection } from "@patternfly/react-core/dist/esm/components/Page/index.js"; |
| 16 |
import { Gallery } from "@patternfly/react-core/dist/esm/layouts/Gallery/index.js"; |
| 17 |
import { Breadcrumb, BreadcrumbItem } from "@patternfly/react-core/dist/esm/components/Breadcrumb/index.js"; |
| 18 |
import { Form, FormGroup } from "@patternfly/react-core/dist/esm/components/Form/index.js"; |
| 19 |
import { TextInput } from "@patternfly/react-core/dist/esm/components/TextInput/index.js"; |
| 20 |
import { Spinner } from "@patternfly/react-core/dist/esm/components/Spinner/index.js"; |
| 21 |
import { Popover } from "@patternfly/react-core/dist/esm/components/Popover/index.js"; |
| 22 |
import { ExclamationCircleIcon, HelpIcon, UndoIcon } from '@patternfly/react-icons'; |
| 23 |
|
| 24 |
import cockpit from 'cockpit'; |
| 25 |
import { superuser } from "superuser"; |
| 26 |
import * as timeformat from "timeformat"; |
| 27 |
import { apply_modal_dialog } from "cockpit-components-dialog.jsx"; |
| 28 |
import { MultiTypeaheadSelect } from "cockpit-components-multi-typeahead-select"; |
| 29 |
|
| 30 |
import { FormHelper } from "cockpit-components-form-helper"; |
| 31 |
import { show_unexpected_error } from "./dialog-utils.js"; |
| 32 |
import { delete_account_dialog } from "./delete-account-dialog.js"; |
| 33 |
import { account_expiration_dialog, password_expiration_dialog } from "./expiration-dialogs.js"; |
| 34 |
import { account_shell_dialog } from "./shell-dialog.js"; |
| 35 |
import { set_password_dialog, reset_password_dialog } from "./password-dialogs.js"; |
| 36 |
import { AccountLogs } from "./account-logs-panel.jsx"; |
| 37 |
import { AuthorizedKeys } from "./authorized-keys-panel.js"; |
| 38 |
import { get_locked } from "./utils.js"; |
| 39 |
|
| 40 |
const _ = cockpit.gettext; |
| 41 |
|
| 42 |
function get_expire(name) { |
| 43 |
function parse_expire(data) { |
| 44 |
let account_expiration = ''; |
| 45 |
let account_date = null; |
| 46 |
|
| 47 |
let password_expiration = ''; |
| 48 |
let password_days = null; |
| 49 |
|
| 50 |
data.split('\n').forEach(line => { |
| 51 |
const fields = line.split(': '); |
| 52 |
if (fields[0] && fields[0].indexOf("Password expires") === 0) { |
| 53 |
if (fields[1].indexOf("never") === 0) { |
| 54 |
password_expiration = _("Never expire password"); |
| 55 |
} else if (fields[1].indexOf("password must be changed") === 0) { |
| 56 |
password_expiration = _("Password must be changed"); |
| 57 |
} else { |
| 58 |
password_expiration = cockpit.format(_("Require password change on $0"), timeformat.date(new Date(fields[1]))); |
| 59 |
} |
| 60 |
} else if (fields[0] && fields[0].indexOf("Account expires") === 0) { |
| 61 |
if (fields[1].indexOf("never") === 0) { |
| 62 |
account_expiration = _("Never expire account"); |
| 63 |
} else { |
| 64 |
account_date = new Date(fields[1] + " 12:00:00 UTC"); |
| 65 |
account_expiration = cockpit.format(_("Expire account on $0"), timeformat.date(new Date(fields[1]))); |
| 66 |
} |
| 67 |
} else if (fields[0] && fields[0].indexOf("Maximum number of days between password change") === 0) { |
| 68 |
password_days = fields[1]; |
| 69 |
} |
| 70 |
}); |
| 71 |
|
| 72 |
// 99999 traditionally meant "never" but the modern value for |
| 73 |
// that is -1. |
| 74 |
// |
| 75 |
// Modern versions of "chage" will not treat 99999 any special |
| 76 |
// and will output bogus expiration dates somewhere in 2300. |
| 77 |
// But let's recognize both, since old accounts will still use |
| 78 |
// 99999, and /etc/login.def also still uses 99999 as the |
| 79 |
// default for new accounts. |
| 80 |
// |
| 81 |
// Password expiration is being removed completely from |
| 82 |
// shadow-utils, and we probably should continue to recognize |
| 83 |
// 99999 as "never" until we stop supporting password expiry |
| 84 |
// entirely. |
| 85 |
|
| 86 |
if (parseInt(password_days) >= 99999 || parseInt(password_days) < 0) { |
| 87 |
password_days = null; |
| 88 |
password_expiration = _("Never expire password"); |
| 89 |
} |
| 90 |
|
| 91 |
return { |
| 92 |
account_text: account_expiration, |
| 93 |
account_date, |
| 94 |
password_text: password_expiration, |
| 95 |
password_days |
| 96 |
}; |
| 97 |
} |
| 98 |
|
| 99 |
return cockpit.spawn(["chage", "-l", name], |
| 100 |
{ environ: ["LC_ALL=C"], err: "message", superuser: "try" }) |
| 101 |
.catch(() => "") |
| 102 |
.then(parse_expire); |
| 103 |
} |
| 104 |
|
| 105 |
export function AccountNotFound() { |
| 106 |
return ( |
| 107 |
<EmptyState headingLevel="h1" icon={ExclamationCircleIcon} titleText={_("Account not available or cannot be edited.")} variant={EmptyStateVariant.sm} id="account-failure"> |
| 108 |
<EmptyStateFooter> |
| 109 |
<EmptyStateActions> |
| 110 |
<Breadcrumb> |
| 111 |
<BreadcrumbItem to="#/">{_("Back to accounts")}</BreadcrumbItem> |
| 112 |
</Breadcrumb> |
| 113 |
</EmptyStateActions> |
| 114 |
</EmptyStateFooter> |
| 115 |
</EmptyState> |
| 116 |
); |
| 117 |
} |
| 118 |
|
| 119 |
export function AccountDetails({ account, groups, isLoading, current_user, shells }) { |
| 120 |
const [expiration, setExpiration] = useState(null); |
| 121 |
|
| 122 |
const user = useMemo(() => account.name, [account.name]); |
| 123 |
const [isLocked, setIsLocked] = useState(account.isLocked); |
| 124 |
|
| 125 |
const [editedRealName, setEditedRealName] = useState(null); |
| 126 |
const [realNameError, setRealNameError] = useState(""); |
| 127 |
const [comittingRealName, setCommittingRealName] = useState(false); |
| 128 |
const [disableLockedEdit, setDisableLockedEdit] = useState(false); |
| 129 |
|
| 130 |
useEffect(() => { |
| 131 |
get_expire(account.name).then(setExpiration); |
| 132 |
}, [account]); |
| 133 |
|
| 134 |
// Only update isLocked field if account changes value |
| 135 |
useEffect(() => { |
| 136 |
setIsLocked(account.isLocked); |
| 137 |
}, [account.isLocked]); |
| 138 |
|
| 139 |
function changeRealName() { |
| 140 |
setRealNameError(""); |
| 141 |
|
| 142 |
if (editedRealName === null || editedRealName === undefined) |
| 143 |
return; |
| 144 |
|
| 145 |
if (editedRealName.includes(':')) { |
| 146 |
setRealNameError(_("The full name must not contain colons.")); |
| 147 |
return; |
| 148 |
} |
| 149 |
|
| 150 |
setCommittingRealName(true); |
| 151 |
|
| 152 |
cockpit.spawn(["/usr/sbin/usermod", user, "--comment", editedRealName], |
| 153 |
{ superuser: "try", err: "message" }) |
| 154 |
.then(() => { |
| 155 |
setEditedRealName(null); |
| 156 |
setCommittingRealName(false); |
| 157 |
}) |
| 158 |
.catch(error => { |
| 159 |
setEditedRealName(null); |
| 160 |
setCommittingRealName(false); |
| 161 |
show_unexpected_error(error); |
| 162 |
}); |
| 163 |
} |
| 164 |
|
| 165 |
function change_locked(value, dont_retry_if_stuck) { |
| 166 |
setIsLocked(value); |
| 167 |
setDisableLockedEdit(true); |
| 168 |
|
| 169 |
cockpit.spawn(["/usr/sbin/usermod", user, value ? "--lock" : "--unlock"], |
| 170 |
{ superuser: "require", err: "message" }) |
| 171 |
.then(() => { |
| 172 |
get_locked(user) |
| 173 |
.then(locked => { |
| 174 |
/* if we care about what the lock state should be and it doesn't match, try to change again |
| 175 |
this is a workaround for different ways of handling a locked account |
| 176 |
https://github.com/cockpit-project/cockpit/issues/1216 |
| 177 |
https://bugzilla.redhat.com/show_bug.cgi?id=853153 |
| 178 |
*/ |
| 179 |
if (locked != value && !dont_retry_if_stuck) { |
| 180 |
console.log("Account locked state doesn't match desired value, trying again."); |
| 181 |
// only retry once to avoid uncontrolled recursion |
| 182 |
change_locked(value, true); |
| 183 |
} |
| 184 |
}); |
| 185 |
}) |
| 186 |
.catch(error => { |
| 187 |
show_unexpected_error(error); |
| 188 |
setIsLocked(!value); |
| 189 |
}) |
| 190 |
.finally(() => { |
| 191 |
setDisableLockedEdit(false); |
| 192 |
}); |
| 193 |
} |
| 194 |
|
| 195 |
function logout_account() { |
| 196 |
cockpit.spawn(["loginctl", "terminate-user", user], |
| 197 |
{ superuser: "try", err: "message" }) |
| 198 |
.then(() => { |
| 199 |
get_expire(user).then(setExpiration); |
| 200 |
}) |
| 201 |
.catch(show_unexpected_error); |
| 202 |
} |
| 203 |
|
| 204 |
if (isLoading) { |
| 205 |
return ( |
| 206 |
<EmptyState headingLevel="h1" titleText={_("Loading...")} variant={EmptyStateVariant.sm}> |
| 207 |
<EmptyStateFooter><Spinner size="xl" /></EmptyStateFooter> |
| 208 |
</EmptyState> |
| 209 |
); |
| 210 |
} |
| 211 |
|
| 212 |
if (!expiration) |
| 213 |
return null; |
| 214 |
|
| 215 |
const self_mod_allowed = (user == current_user || !!superuser.allowed); |
| 216 |
|
| 217 |
let title_name = account.gecos; |
| 218 |
if (title_name) |
| 219 |
title_name = title_name.split(',')[0]; |
| 220 |
else |
| 221 |
title_name = account.name; |
| 222 |
|
| 223 |
let last_login; |
| 224 |
if (account.loggedIn) |
| 225 |
last_login = _("Logged in"); |
| 226 |
else if (!account.lastLogin) |
| 227 |
last_login = _("Never"); |
| 228 |
else |
| 229 |
last_login = timeformat.dateTime(new Date(account.lastLogin)); |
| 230 |
|
| 231 |
const actions = superuser.allowed && ( |
| 232 |
<> |
| 233 |
<Button variant="secondary" onClick={() => logout_account()} id="account-logout" |
| 234 |
isDisabled={!account.loggedIn || account.uid == 0 || user === current_user}> |
| 235 |
{_("Terminate session")} |
| 236 |
</Button> |
| 237 |
{ "\n" } |
| 238 |
<Button isDisabled={account.uid == 0 || user === current_user} variant="danger" id="account-delete" |
| 239 |
onClick={() => delete_account_dialog(account)}> |
| 240 |
{_("Delete")} |
| 241 |
</Button> |
| 242 |
</> |
| 243 |
); |
| 244 |
|
| 245 |
return ( |
| 246 |
<Page id="account" className="pf-m-no-sidebar"> |
| 247 |
<PageBreadcrumb hasBodyWrapper={false} stickyOnBreakpoint={{ default: "top" }}> |
| 248 |
<Breadcrumb> |
| 249 |
<BreadcrumbItem to="#/">{_("Accounts")}</BreadcrumbItem> |
| 250 |
<BreadcrumbItem isActive>{title_name}</BreadcrumbItem> |
| 251 |
</Breadcrumb> |
| 252 |
</PageBreadcrumb> |
| 253 |
<PageSection hasBodyWrapper={false}> |
| 254 |
<Gallery hasGutter> |
| 255 |
<Card isPlain className="account-details" id="account-details"> |
| 256 |
<CardHeader actions={{ actions }}> |
| 257 |
<CardTitle id="account-title" component="h2">{title_name}</CardTitle> |
| 258 |
</CardHeader> |
| 259 |
<CardBody> |
| 260 |
<Form isHorizontal onSubmit={apply_modal_dialog}> |
| 261 |
<FormGroup fieldId="account-real-name" hasNoPaddingTop={!superuser.allowed} label={_("Full name")}> |
| 262 |
{ superuser.allowed |
| 263 |
? <> |
| 264 |
<TextInput id="account-real-name" |
| 265 |
isDisabled={comittingRealName || account.uid == 0} |
| 266 |
value={editedRealName !== null ? editedRealName : account.gecos} |
| 267 |
onKeyDown={event => { |
| 268 |
if (event.key == "Enter") { |
| 269 |
event.target.blur(); |
| 270 |
} |
| 271 |
}} |
| 272 |
onChange={(_event, value) => setEditedRealName(value)} |
| 273 |
validated={realNameError !== "" ? "error" : "default"} |
| 274 |
onBlur={() => changeRealName()} /> |
| 275 |
<FormHelper fieldId="account-real-name" helperTextInvalid={realNameError} /> |
| 276 |
</> |
| 277 |
: <output id="account-real-name">{account.gecos}</output>} |
| 278 |
</FormGroup> |
| 279 |
<FormGroup fieldId="account-user-name" hasNoPaddingTop label={_("User name")}> |
| 280 |
<output id="account-user-name">{account.name}</output> |
| 281 |
</FormGroup> |
| 282 |
<AccountGroupsSelect key={account.name} loggedIn={account.loggedIn} name={account.name} groups={groups} /> |
| 283 |
<FormGroup fieldId="account-last-login" hasNoPaddingTop label={_("Last login")}> |
| 284 |
<output id="account-last-login">{last_login}</output> |
| 285 |
</FormGroup> |
| 286 |
<FormGroup fieldId="account-locked" label={_("Options")} hasNoPaddingTop> |
| 287 |
<Flex spaceItems={{ default: 'spaceItemsSm' }} alignItems={{ default: 'alignItemsCenter' }}> |
| 288 |
<FlexItem spacer={{ default: 'spacerNone' }}> |
| 289 |
<Checkbox id="account-locked" |
| 290 |
ouiaSafe={disableLockedEdit} |
| 291 |
isDisabled={!superuser.allowed || disableLockedEdit || user == current_user || isLocked == null} |
| 292 |
isChecked={isLocked} |
| 293 |
onChange={(_event, checked) => change_locked(checked)} |
| 294 |
label={_("Disallow interactive password")} /> |
| 295 |
</FlexItem> |
| 296 |
|
| 297 |
<Popover bodyContent={_("Other authentication methods are still available even when interactive password authentication is not allowed.")} |
| 298 |
showClose={false}> |
| 299 |
<HelpIcon /> |
| 300 |
</Popover> |
| 301 |
<span id="account-expiration-text"> |
| 302 |
{expiration.account_text} |
| 303 |
</span> |
| 304 |
<Button onClick={() => account_expiration_dialog(account, expiration.account_date)} |
| 305 |
isDisabled={!superuser.allowed} |
| 306 |
variant="link" |
| 307 |
isInline |
| 308 |
id="account-expiration-button"> |
| 309 |
{_("edit")} |
| 310 |
</Button> |
| 311 |
</Flex> |
| 312 |
</FormGroup> |
| 313 |
{ self_mod_allowed && |
| 314 |
<FormGroup fieldId="account-set-password" label={_("Password")}> |
| 315 |
<div className="account-column-one"> |
| 316 |
{ self_mod_allowed && |
| 317 |
<Button variant="secondary" id="account-set-password" |
| 318 |
onClick={() => set_password_dialog(account, current_user)}> |
| 319 |
{_("Set password")} |
| 320 |
</Button> |
| 321 |
} |
| 322 |
{ "\n" } |
| 323 |
{ superuser.allowed && |
| 324 |
<Button variant="secondary" id="password-reset-button" |
| 325 |
onClick={() => reset_password_dialog(account)}> |
| 326 |
{_("Force change")} |
| 327 |
</Button> |
| 328 |
} |
| 329 |
</div> |
| 330 |
<Flex flex={{ default: 'inlineFlex' }}> |
| 331 |
<span id="password-expiration-text"> |
| 332 |
{expiration.password_text} |
| 333 |
</span> |
| 334 |
<Button onClick={() => password_expiration_dialog(account, expiration.password_days)} |
| 335 |
isDisabled={!superuser.allowed} |
| 336 |
variant="link" |
| 337 |
isInline |
| 338 |
id="password-expiration-button"> |
| 339 |
{_("edit")} |
| 340 |
</Button> |
| 341 |
</Flex> |
| 342 |
</FormGroup> |
| 343 |
} |
| 344 |
{ account.home && <FormGroup fieldId="account-home-dir" hasNoPaddingTop label={_("Home directory")}> |
| 345 |
<output id="account-home-dir">{account.home}</output> |
| 346 |
</FormGroup> } |
| 347 |
{ account.shell && <FormGroup fieldId="account-shell" hasNoPaddingTop label={_("Shell")}> |
| 348 |
<Flex flex={{ default: 'inlineFlex' }}> |
| 349 |
<output id="account-shell">{account.shell}</output> |
| 350 |
<Button onClick={() => account_shell_dialog(account, shells)} |
| 351 |
isDisabled={!superuser.allowed} |
| 352 |
variant="link" |
| 353 |
isInline |
| 354 |
id="change-shell-button"> |
| 355 |
{_("change")} |
| 356 |
</Button> |
| 357 |
</Flex> |
| 358 |
</FormGroup> } |
| 359 |
</Form> |
| 360 |
</CardBody> |
| 361 |
</Card> |
| 362 |
<AuthorizedKeys name={account.name} home={account.home} allow_mods={self_mod_allowed} /> |
| 363 |
<AccountLogs name={account.name} /> |
| 364 |
</Gallery> |
| 365 |
</PageSection> |
| 366 |
</Page> |
| 367 |
); |
| 368 |
} |
| 369 |
|
| 370 |
export const AccountGroupsSelect = ({ name, loggedIn, groups }) => { |
| 371 |
const [selected, setSelected] = useState(); |
| 372 |
const [primaryGroupName, setPrimaryGroupName] = useState(); |
| 373 |
const [loading, setLoading] = useState(true); |
| 374 |
const [modifyingGroup, setModifyingGroup] = useState(false); |
| 375 |
const [history, setHistory] = useState([]); |
| 376 |
const previousValue = useRef(null); |
| 377 |
|
| 378 |
useEffect(() => { |
| 379 |
const usedGroups = groups.filter(group => group.userlist.includes(name)); |
| 380 |
const primaryGroup = groups.find(group => group.userlistPrimary.includes(name)); |
| 381 |
const _primaryGroupName = primaryGroup?.name; |
| 382 |
const _selected = usedGroups.map(group => group.name); |
| 383 |
if (primaryGroup) |
| 384 |
_selected.push(_primaryGroupName); |
| 385 |
|
| 386 |
previousValue.current = _selected; |
| 387 |
setSelected(_selected); |
| 388 |
setLoading(false); |
| 389 |
setPrimaryGroupName(_primaryGroupName); |
| 390 |
}, [groups, setSelected, name, previousValue]); |
| 391 |
|
| 392 |
const undoGroupChanges = () => { |
| 393 |
const undoItem = history[history.length - 1]; |
| 394 |
if (undoItem.type === 'added') { |
| 395 |
removeGroup(undoItem.name, true).then(() => setHistory(history.slice(0, -1))); |
| 396 |
} else if (undoItem.type === 'removed') { |
| 397 |
addGroup(undoItem.name, true).then(() => setHistory(history.slice(0, -1))); |
| 398 |
} |
| 399 |
}; |
| 400 |
|
| 401 |
const removeGroup = (group, isUndo) => { |
| 402 |
if (!isUndo) |
| 403 |
setHistory([...history, { type: 'removed', name: group }]); |
| 404 |
|
| 405 |
setModifyingGroup(true); |
| 406 |
return cockpit.spawn(["gpasswd", "-d", name, group], { superuser: "require", err: "message" }) |
| 407 |
.then(() => { |
| 408 |
setModifyingGroup(false); |
| 409 |
}, show_unexpected_error); |
| 410 |
}; |
| 411 |
|
| 412 |
const addGroup = (group, isUndo) => { |
| 413 |
if (!isUndo) |
| 414 |
setHistory([...history, { type: 'added', name: group }]); |
| 415 |
|
| 416 |
setModifyingGroup(true); |
| 417 |
return cockpit.spawn(["gpasswd", "-a", name, group], { superuser: "require", err: "message" }) |
| 418 |
.then(() => { |
| 419 |
setModifyingGroup(false); |
| 420 |
}, show_unexpected_error); |
| 421 |
}; |
| 422 |
|
| 423 |
const chipGroupComponent = () => { |
| 424 |
return ( |
| 425 |
<LabelGroup numLabels={10}> |
| 426 |
{(selected || []).map((currentLabel, index) => { |
| 427 |
return ( |
| 428 |
<Label key={currentLabel} |
| 429 |
color={groups.find(group => group.name === currentLabel).isAdmin ? "yellow" : "blue"} |
| 430 |
> |
| 431 |
{currentLabel} |
| 432 |
</Label> |
| 433 |
); |
| 434 |
})} |
| 435 |
</LabelGroup> |
| 436 |
); |
| 437 |
}; |
| 438 |
|
| 439 |
return ( |
| 440 |
<FormGroup |
| 441 |
fieldId="account-groups" |
| 442 |
id="account-groups-form-group" |
| 443 |
label={_("Groups")} |
| 444 |
validated={history.length > 0 ? "warning" : "default"} |
| 445 |
> |
| 446 |
{superuser.allowed |
| 447 |
? <MultiTypeaheadSelect |
| 448 |
isScrollable |
| 449 |
isDisabled={loading || modifyingGroup} |
| 450 |
onAdd={val => addGroup(val)} |
| 451 |
onRemove={val => removeGroup(val)} |
| 452 |
options={groups.map((option, index) => { |
| 453 |
return { |
| 454 |
value: option.name, |
| 455 |
content: option.name, |
| 456 |
color: option.isAdmin ? "yellow" : "blue", |
| 457 |
isDisabled: option.name == primaryGroupName, |
| 458 |
}; |
| 459 |
})} |
| 460 |
selected={selected || []} |
| 461 |
toggleProps={{ id: "account-groups" }} /> |
| 462 |
: chipGroupComponent()} |
| 463 |
{(history.length > 0) |
| 464 |
? <HelperText className="pf-v6-c-form__helper-text"> |
| 465 |
<Flex> |
| 466 |
{loggedIn && <HelperTextItem id="account-groups-helper" variant="warning">{_("The user must log out and log back in for the new configuration to take effect.")}</HelperTextItem>} |
| 467 |
{history.length > 0 && <Button variant="link" id="group-undo-btn" isInline icon={<UndoIcon />} onClick={undoGroupChanges}>{_("Undo")}</Button>} |
| 468 |
</Flex> |
| 469 |
</HelperText> |
| 470 |
: '' |
| 471 |
} |
| 472 |
</FormGroup> |
| 473 |
); |
| 474 |
}; |