| 1 |
/* |
| 2 |
* Copyright (C) 2020 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 { superuser } from "superuser"; |
| 9 |
import { Form, FormGroup } from "@patternfly/react-core/dist/esm/components/Form/index.js"; |
| 10 |
import { TextInput } from "@patternfly/react-core/dist/esm/components/TextInput/index.js"; |
| 11 |
|
| 12 |
import { has_errors } from "./dialog-utils.js"; |
| 13 |
import { show_modal_dialog, apply_modal_dialog } from "cockpit-components-dialog.jsx"; |
| 14 |
import { password_quality, PasswordFormFields } from "cockpit-components-password.jsx"; |
| 15 |
import { FormHelper } from "cockpit-components-form-helper"; |
| 16 |
|
| 17 |
const _ = cockpit.gettext; |
| 18 |
|
| 19 |
function passwd_self(old_pass, new_pass) { |
| 20 |
const old_exps = [ |
| 21 |
/Current password: $/, |
| 22 |
/Current Password: $/, |
| 23 |
/.*\(current\) UNIX password: $/, |
| 24 |
]; |
| 25 |
const new_exps = [ |
| 26 |
/.*New password: $/, |
| 27 |
/.*Retype new password: $/, |
| 28 |
/.*Enter new \w*\s?password: $/, |
| 29 |
/.*Retype new \w*\s?password: $/ |
| 30 |
]; |
| 31 |
const bad_exps = [ |
| 32 |
/.*BAD PASSWORD:.*/ |
| 33 |
]; |
| 34 |
const too_new_exps = [ |
| 35 |
/.*must wait longer to change.*/ |
| 36 |
]; |
| 37 |
|
| 38 |
return new Promise((resolve, reject) => { |
| 39 |
let buffer = ""; |
| 40 |
let sent_new = false; |
| 41 |
let failure = _("Old password not accepted"); |
| 42 |
|
| 43 |
const timeout = window.setTimeout(function() { |
| 44 |
failure = _("Prompting via passwd timed out"); |
| 45 |
proc.close("timeout"); |
| 46 |
}, 10 * 1000); |
| 47 |
|
| 48 |
const proc = cockpit.spawn(["passwd"], { pty: true, environ: ["LC_ALL=C"], err: "out" }) |
| 49 |
.always(function() { |
| 50 |
window.clearInterval(timeout); |
| 51 |
}) |
| 52 |
.done(function() { |
| 53 |
resolve(); |
| 54 |
}) |
| 55 |
.fail(function(ex) { |
| 56 |
if (ex.exit_status || ex.problem == "timeout") |
| 57 |
ex = new Error(failure); |
| 58 |
reject(ex); |
| 59 |
}) |
| 60 |
.stream(function(data) { |
| 61 |
buffer += data; |
| 62 |
|
| 63 |
for (let i = 0; i < too_new_exps.length; i++) { |
| 64 |
if (too_new_exps[i].test(buffer)) { |
| 65 |
failure = _("You must wait longer to change your password"); |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
if (sent_new) { |
| 70 |
for (let i = 0; i < bad_exps.length; i++) { |
| 71 |
if (bad_exps[i].test(buffer)) { |
| 72 |
failure = _("New password was not accepted"); |
| 73 |
} |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
for (let i = 0; i < old_exps.length; i++) { |
| 78 |
if (old_exps[i].test(buffer)) { |
| 79 |
buffer = ""; |
| 80 |
this.input(old_pass + "\n", true); |
| 81 |
return; |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
for (let i = 0; i < new_exps.length; i++) { |
| 86 |
if (new_exps[i].test(buffer)) { |
| 87 |
buffer = ""; |
| 88 |
this.input(new_pass + "\n", true); |
| 89 |
failure = _("Failed to change password"); |
| 90 |
sent_new = true; |
| 91 |
return; |
| 92 |
} |
| 93 |
} |
| 94 |
}); |
| 95 |
}); |
| 96 |
} |
| 97 |
|
| 98 |
export function passwd_change(user, new_pass) { |
| 99 |
return new Promise((resolve, reject) => { |
| 100 |
cockpit.spawn(["chpasswd"], { superuser: "require", err: "out" }) |
| 101 |
.input(user + ":" + new_pass) |
| 102 |
.done(function() { |
| 103 |
resolve(); |
| 104 |
}) |
| 105 |
.fail(function(ex, response) { |
| 106 |
if (ex.exit_status) { |
| 107 |
console.log(ex); |
| 108 |
if (response) |
| 109 |
ex = new Error(response); |
| 110 |
else |
| 111 |
ex = new Error(_("Failed to change password")); |
| 112 |
} |
| 113 |
reject(ex); |
| 114 |
}); |
| 115 |
}); |
| 116 |
} |
| 117 |
|
| 118 |
function SetPasswordDialogBody({ state, errors, change }) { |
| 119 |
const { need_old, password_old, current_user } = state; |
| 120 |
|
| 121 |
return ( |
| 122 |
<Form isHorizontal onSubmit={apply_modal_dialog}> |
| 123 |
{ need_old && |
| 124 |
<> |
| 125 |
<input hidden disabled value={current_user} /> |
| 126 |
<FormGroup label={_("Old password")} |
| 127 |
fieldId="account-set-password-old"> |
| 128 |
<TextInput className="check-passwords" type="password" id="account-set-password-old" |
| 129 |
autoComplete="current-password" value={password_old} onChange={(_event, value) => change("password_old", value)} /> |
| 130 |
<FormHelper helperTextInvalid={errors?.password_old} /> |
| 131 |
</FormGroup> |
| 132 |
</> } |
| 133 |
<PasswordFormFields password_label={_("New password")} |
| 134 |
password_confirm_label={_("Confirm new password")} |
| 135 |
error_password={errors?.password} |
| 136 |
error_password_confirm={errors?.password_confirm} |
| 137 |
idPrefix="account-set-password" |
| 138 |
change={change} /> |
| 139 |
</Form> |
| 140 |
); |
| 141 |
} |
| 142 |
|
| 143 |
export function set_password_dialog(account, current_user) { |
| 144 |
let dlg = null; |
| 145 |
|
| 146 |
const change_self = (account.name == current_user && !superuser.allowed); |
| 147 |
|
| 148 |
const state = { |
| 149 |
need_old: change_self, |
| 150 |
current_user, |
| 151 |
password_old: "", |
| 152 |
password: "", |
| 153 |
password_confirm: "", |
| 154 |
confirm_weak: false, |
| 155 |
}; |
| 156 |
|
| 157 |
let errors = { }; |
| 158 |
|
| 159 |
let old_password = null; |
| 160 |
|
| 161 |
function change(field, value) { |
| 162 |
state[field] = value; |
| 163 |
|
| 164 |
if (state.password != old_password) { |
| 165 |
state.confirm_weak = false; |
| 166 |
old_password = state.password; |
| 167 |
errors = { }; |
| 168 |
} |
| 169 |
|
| 170 |
update(); |
| 171 |
} |
| 172 |
|
| 173 |
function validate(force, password, password_confirm) { |
| 174 |
const errs = { }; |
| 175 |
|
| 176 |
if (password != password_confirm) |
| 177 |
errs.password_confirm = _("The passwords do not match"); |
| 178 |
|
| 179 |
if (password.length > 256) |
| 180 |
errs.password = _("Password is longer than 256 characters"); |
| 181 |
|
| 182 |
return password_quality(password, force) |
| 183 |
.catch(ex => { |
| 184 |
errs.password = (ex.message || ex.toString()).replaceAll("\n", " "); |
| 185 |
}) |
| 186 |
.then(() => { |
| 187 |
errors = errs; |
| 188 |
return !has_errors(errs); |
| 189 |
}); |
| 190 |
} |
| 191 |
|
| 192 |
function passwd_check(force, password, password_confirm, password_old) { |
| 193 |
return validate(force, password, password_confirm).then(valid => { |
| 194 |
if (valid) { |
| 195 |
if (change_self) |
| 196 |
return passwd_self(password_old, password); |
| 197 |
else |
| 198 |
return passwd_change(account.name, password); |
| 199 |
} else { |
| 200 |
if (!errors.password_confirm && state.password.length <= 256) { |
| 201 |
state.confirm_weak = true; |
| 202 |
} |
| 203 |
update(); |
| 204 |
return Promise.reject(); |
| 205 |
} |
| 206 |
}); |
| 207 |
} |
| 208 |
|
| 209 |
function update() { |
| 210 |
const props = { |
| 211 |
id: "account-set-password-dialog", |
| 212 |
title: _("Set password"), |
| 213 |
body: <SetPasswordDialogBody state={state} errors={errors} change={change} /> |
| 214 |
}; |
| 215 |
|
| 216 |
const footer = { |
| 217 |
actions: [ |
| 218 |
{ |
| 219 |
caption: _("Set password"), |
| 220 |
style: "primary", |
| 221 |
clicked: () => { |
| 222 |
return passwd_check(false, state.password, state.password_confirm, state.password_old); |
| 223 |
}, |
| 224 |
disabled: state.confirm_weak |
| 225 |
} |
| 226 |
] |
| 227 |
}; |
| 228 |
if (state.confirm_weak) { |
| 229 |
footer.actions.push( |
| 230 |
{ |
| 231 |
caption: _("Set weak password"), |
| 232 |
style: "warning", |
| 233 |
clicked: () => { |
| 234 |
return passwd_check(true, state.password, state.password_confirm, state.password_old); |
| 235 |
} |
| 236 |
} |
| 237 |
); |
| 238 |
} |
| 239 |
|
| 240 |
if (!dlg) |
| 241 |
dlg = show_modal_dialog(props, footer); |
| 242 |
else { |
| 243 |
dlg.setProps(props); |
| 244 |
dlg.setFooterProps(footer); |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
update(); |
| 249 |
} |
| 250 |
|
| 251 |
export function reset_password_dialog(account) { |
| 252 |
const msg = cockpit.format(_("The account '$0' will be forced to change their password on next login"), |
| 253 |
account.name); |
| 254 |
|
| 255 |
const props = { |
| 256 |
id: "password-reset", |
| 257 |
title: _("Force password change"), |
| 258 |
body: <p>{msg}</p> |
| 259 |
}; |
| 260 |
|
| 261 |
const footer = { |
| 262 |
actions: [ |
| 263 |
{ |
| 264 |
caption: _("Reset password"), |
| 265 |
style: "primary", |
| 266 |
clicked: () => { |
| 267 |
return cockpit.spawn(["passwd", "-e", account.name], |
| 268 |
{ superuser: "require", err: "message" }); |
| 269 |
} |
| 270 |
} |
| 271 |
] |
| 272 |
}; |
| 273 |
|
| 274 |
show_modal_dialog(props, footer); |
| 275 |
} |