| 1 |
/* |
| 2 |
* Copyright (C) 2013 Red Hat, Inc. |
| 3 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
*/ |
| 5 |
import '../lib/patternfly/patternfly-6-cockpit.scss'; |
| 6 |
import 'polyfills'; // once per application |
| 7 |
import 'cockpit-dark-theme'; // once per page |
| 8 |
|
| 9 |
import React, { useMemo, useState } from 'react'; |
| 10 |
import { createRoot } from 'react-dom/client'; |
| 11 |
|
| 12 |
import { debounce } from 'throttle-debounce'; |
| 13 |
|
| 14 |
import cockpit from 'cockpit'; |
| 15 |
import { superuser } from "superuser"; |
| 16 |
import { getLastlog2 } from "logins"; |
| 17 |
import { usePageLocation, useLoggedInUser, useFile, useInit } from "hooks.js"; |
| 18 |
import { etc_passwd_syntax, etc_group_syntax, etc_shells_syntax } from "pam_user_parser.js"; |
| 19 |
import { EmptyStatePanel } from "cockpit-components-empty-state.jsx"; |
| 20 |
|
| 21 |
import { AccountsMain } from "./accounts-list.js"; |
| 22 |
import { AccountDetails, AccountNotFound } from "./account-details.js"; |
| 23 |
|
| 24 |
import "./users.scss"; |
| 25 |
import { fsinfo } from 'cockpit/fsinfo'; |
| 26 |
|
| 27 |
superuser.reload_page_on_change(); |
| 28 |
|
| 29 |
export const admins = ['sudo', 'root', 'wheel']; |
| 30 |
const sortGroups = groups => { |
| 31 |
return groups.sort((a, b) => { |
| 32 |
if (a.isAdmin) |
| 33 |
return -1; |
| 34 |
if (b.isAdmin) |
| 35 |
return 1; |
| 36 |
if (a.members === b.members) |
| 37 |
return a.name.localeCompare(b.name); |
| 38 |
else |
| 39 |
return b.members - a.members; |
| 40 |
}); |
| 41 |
}; |
| 42 |
|
| 43 |
function AccountsPage() { |
| 44 |
const [isGroupsExpanded, setIsGroupsExpanded] = useState(false); |
| 45 |
const { path } = usePageLocation(); |
| 46 |
const accounts = useFile("/etc/passwd", { syntax: etc_passwd_syntax }); |
| 47 |
const groups = useFile("/etc/group", { syntax: etc_group_syntax }); |
| 48 |
const shells = useFile("/etc/shells", { syntax: etc_shells_syntax }); |
| 49 |
const current_user_info = useLoggedInUser(); |
| 50 |
|
| 51 |
// Handle the case where logindef == null, i.e. the file does not exist. |
| 52 |
// While that's unusual, "empty /etc" is a goal, and it shouldn't crash the page. |
| 53 |
const [min_gid, setMinGid] = useState(500); |
| 54 |
const [max_gid, setMaxGid] = useState(60000); |
| 55 |
const [min_uid, setMinUid] = useState(500); |
| 56 |
const [max_uid, setMaxUid] = useState(60000); |
| 57 |
const [details, setDetails] = useState(null); |
| 58 |
|
| 59 |
useInit(async () => { |
| 60 |
const logind_client = cockpit.dbus("org.freedesktop.login1"); |
| 61 |
|
| 62 |
const debouncedGetLoginDetails = debounce(100, () => { |
| 63 |
getLoginDetails(logind_client).then(setDetails); |
| 64 |
}); |
| 65 |
|
| 66 |
/* We are mostly interested in UserNew/UserRemoved. But SessionRemoved happens immediately after logout, |
| 67 |
* while UserRemoved lags behind due to the "State: closing" period when the user's systemd instance |
| 68 |
* etc. are being cleaned up. Also, there's not that many signals and this is debounced, so just react to all |
| 69 |
* of them. See https://www.freedesktop.org/wiki/Software/systemd/logind/ */ |
| 70 |
logind_client.subscribe({ |
| 71 |
interface: "org.freedesktop.login1.Manager", |
| 72 |
path: "/org/freedesktop/login1", |
| 73 |
}, debouncedGetLoginDetails); |
| 74 |
|
| 75 |
let handleUtmp; |
| 76 |
|
| 77 |
// Watch /etc/shadow to register lock/unlock/expire changes; but avoid reading it, it's sensitive data |
| 78 |
const handleShadow = cockpit.file("/etc/shadow", { superuser: "try" }); |
| 79 |
handleShadow.watch(() => debouncedGetLoginDetails(), { read: false }); |
| 80 |
|
| 81 |
let handleLogindef; |
| 82 |
try { |
| 83 |
await fsinfo("/etc/login.defs", []); |
| 84 |
handleLogindef = cockpit.file("/etc/login.defs"); |
| 85 |
} catch (ex) { |
| 86 |
handleLogindef = cockpit.file("/usr/etc/login.defs"); |
| 87 |
} |
| 88 |
|
| 89 |
handleLogindef.watch((logindef) => { |
| 90 |
if (logindef === null) |
| 91 |
return; |
| 92 |
|
| 93 |
const minGid = parseInt(logindef.match(/^GID_MIN\s+(\d+)/m)[1]); |
| 94 |
const maxGid = parseInt(logindef.match(/^GID_MAX\s+(\d+)/m)[1]); |
| 95 |
const minUid = parseInt(logindef.match(/^UID_MIN\s+(\d+)/m)[1]); |
| 96 |
const maxUid = parseInt(logindef.match(/^UID_MAX\s+(\d+)/m)[1]); |
| 97 |
|
| 98 |
if (minGid) |
| 99 |
setMinGid(minGid); |
| 100 |
if (maxGid) |
| 101 |
setMaxGid(maxGid); |
| 102 |
if (minUid) |
| 103 |
setMinUid(minUid); |
| 104 |
if (maxUid) |
| 105 |
setMaxUid(maxUid); |
| 106 |
}); |
| 107 |
|
| 108 |
return [logind_client, handleUtmp, handleShadow, handleLogindef]; |
| 109 |
}, [], null, handles => handles.forEach(handle => handle.close())); |
| 110 |
|
| 111 |
const accountsInfo = useMemo(() => { |
| 112 |
if (accounts && details) |
| 113 |
return accounts.map(account => { |
| 114 |
return Object.assign({}, account, details[account.name]); |
| 115 |
}); |
| 116 |
else |
| 117 |
return []; |
| 118 |
}, [accounts, details]); // FIXME: We only want to update this when details is updated, not accounts |
| 119 |
|
| 120 |
const groupsExtraInfo = useMemo(() => sortGroups( |
| 121 |
(groups || []).map(group => { |
| 122 |
const userlistPrimary = accountsInfo.filter(account => account.gid === group.gid).map(account => account.name); |
| 123 |
const userlist = group.userlist.filter(el => el !== ""); |
| 124 |
return ({ |
| 125 |
...group, |
| 126 |
userlistPrimary, |
| 127 |
userlist, |
| 128 |
members: userlist.length + userlistPrimary.length, |
| 129 |
isAdmin: admins.includes(group.name), |
| 130 |
isUserCreatedGroup: group.gid >= min_gid && group.gid <= max_gid |
| 131 |
}); |
| 132 |
}) |
| 133 |
), [groups, accountsInfo, min_gid, max_gid]); |
| 134 |
|
| 135 |
if (groupsExtraInfo.length == 0 || accountsInfo.length == 0) { |
| 136 |
return <EmptyStatePanel loading />; |
| 137 |
} else if (path.length === 0) { |
| 138 |
return ( |
| 139 |
<AccountsMain |
| 140 |
accountsInfo={accountsInfo} |
| 141 |
current_user={current_user_info?.name} |
| 142 |
groups={groupsExtraInfo || []} |
| 143 |
isGroupsExpanded={isGroupsExpanded} |
| 144 |
setIsGroupsExpanded={setIsGroupsExpanded} |
| 145 |
min_gid={min_gid} |
| 146 |
max_gid={max_gid} |
| 147 |
min_uid={min_uid} |
| 148 |
max_uid={max_uid} |
| 149 |
shells={shells} |
| 150 |
/> |
| 151 |
); |
| 152 |
} else if (path.length === 1) { |
| 153 |
const account = accountsInfo?.find(account => account.name === path[0]); |
| 154 |
if (!account) |
| 155 |
return <AccountNotFound />; |
| 156 |
return ( |
| 157 |
<AccountDetails account={account} isLoading={accountsInfo.length === 0} groups={groupsExtraInfo} |
| 158 |
current_user={current_user_info?.name} shells={shells} /> |
| 159 |
); |
| 160 |
} else return null; |
| 161 |
} |
| 162 |
|
| 163 |
async function getLoginDetails(logind_client) { |
| 164 |
const details = {}; |
| 165 |
|
| 166 |
// currently logged in |
| 167 |
try { |
| 168 |
// out args: uso (uid, name, logind object) |
| 169 |
const [users] = await logind_client.call( |
| 170 |
"/org/freedesktop/login1", "org.freedesktop.login1.Manager", "ListUsers", |
| 171 |
null, { type: "", flags: "", timeout: 5000 }); |
| 172 |
await Promise.all(users.map(async ([_, name, objpath]) => { |
| 173 |
const [active] = await logind_client.call( |
| 174 |
objpath, "org.freedesktop.DBus.Properties", "Get", |
| 175 |
["org.freedesktop.login1.User", "State"], |
| 176 |
{ type: "ss", flags: "", timeout: 5000 }); |
| 177 |
if (active.v !== "closing") |
| 178 |
details[name] = { ...details[name], loggedIn: true }; |
| 179 |
})); |
| 180 |
} catch (err) { |
| 181 |
console.warn("Unexpected error when getting logged in accounts", err); |
| 182 |
} |
| 183 |
|
| 184 |
// locked password |
| 185 |
|
| 186 |
// shadow-utils passwd supports an --all flag which is lacking on RHEL and |
| 187 |
// stable Fedora releases. Available at least on Fedora since |
| 188 |
// shadow-utils-4.14.0-5.fc40 (currently known as rawhide). |
| 189 |
try { |
| 190 |
const locked_statuses = await cockpit.spawn(["passwd", "-S", "--all"], { superuser: "require", err: "message", environ: ["LC_ALL=C"] }); |
| 191 |
// Slice off the last empty line |
| 192 |
for (const line of locked_statuses.trim().split('\n')) { |
| 193 |
const name = line.split(" ")[0]; |
| 194 |
const status = line.split(" ")[1]; |
| 195 |
details[name] = { ...details[name], isLocked: status === "L" }; |
| 196 |
} |
| 197 |
} catch (err) { |
| 198 |
if (err.message?.includes("bad argument --all")) { |
| 199 |
// Fallback for old passwd |
| 200 |
try { |
| 201 |
const shadow = await cockpit.file("/etc/shadow", { superuser: "require", err: "message" }).read(); |
| 202 |
for (const line of shadow.split('\n')) { |
| 203 |
const [name, hash] = line.split(":"); |
| 204 |
if (name && hash) |
| 205 |
details[name] = { ...details[name], isLocked: hash.startsWith("!") }; |
| 206 |
} |
| 207 |
} catch (err) { |
| 208 |
console.warn("Unexpected error when getting locked accounts from /etc/shadow:", err.toString()); |
| 209 |
} |
| 210 |
} else { |
| 211 |
console.warn("Unexpected error when getting locked account information:", err.toString()); |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
// last logged in |
| 216 |
|
| 217 |
try { |
| 218 |
// merge lastlog2 into details |
| 219 |
for (const [name, last] of Object.entries(await getLastlog2())) |
| 220 |
details[name] = { ...details[name], lastLogin: last.time * 1000 }; |
| 221 |
} catch (err) { |
| 222 |
// fall back to legacy lastlog |
| 223 |
try { |
| 224 |
const out = await cockpit.spawn(["lastlog"], { environ: ["LC_ALL=C"] }); |
| 225 |
await Promise.all(out.split('\n').slice(1, -1).map(async line => { |
| 226 |
if (line.includes('**Never logged in**')) |
| 227 |
return; |
| 228 |
|
| 229 |
const splitLine = line.trim().split(/[ \t]+/); |
| 230 |
const name = splitLine[0]; |
| 231 |
const date_fields = splitLine.slice(-5); |
| 232 |
// this is impossible to parse with Date() (e.g. Firefox does not work with all time zones), so call `date` to parse it |
| 233 |
try { |
| 234 |
const out = await cockpit.spawn(["date", "+%s", "-d", date_fields.join(' ')], |
| 235 |
{ environ: ["LC_ALL=C"], err: "out" }); |
| 236 |
details[name] = { ...details[name], lastLogin: parseInt(out) * 1000 }; |
| 237 |
} catch (e) { |
| 238 |
console.warn(`Failed to parse date from lastlog line '${line}': ${e.toString()}`); |
| 239 |
} |
| 240 |
})); |
| 241 |
} catch (ex) { |
| 242 |
console.warn(`Failed to run lastlog: ${ex.toString()}`); |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
return details; |
| 247 |
} |
| 248 |
|
| 249 |
function init() { |
| 250 |
const root = createRoot(document.getElementById("page")); |
| 251 |
root.render(<AccountsPage />); |
| 252 |
document.body.removeAttribute("hidden"); |
| 253 |
} |
| 254 |
|
| 255 |
document.addEventListener("DOMContentLoaded", init); |