-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathusermod.rs
More file actions
564 lines (507 loc) · 18.2 KB
/
Copy pathusermod.rs
File metadata and controls
564 lines (507 loc) · 18.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
// This file is part of the shadow-rs package.
//
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.
// spell-checker:ignore usermod
//! `usermod` — modify a user account.
//!
//! Drop-in replacement for GNU shadow-utils `usermod(8)`.
use std::fmt;
use std::path::Path;
use clap::{Arg, ArgAction, Command};
use uucore::error::{UError, UResult};
use shadow_core::audit;
use shadow_core::group::{self};
use shadow_core::lock::FileLock;
use shadow_core::passwd::{self};
use shadow_core::shadow::{self};
use shadow_core::sysroot::SysRoot;
use shadow_core::{atomic, nscd, validate};
mod options {
pub const COMMENT: &str = "comment";
pub const HOME: &str = "home";
pub const EXPIREDATE: &str = "expiredate";
pub const INACTIVE: &str = "inactive";
pub const GID: &str = "gid";
pub const GROUPS: &str = "groups";
pub const APPEND: &str = "append";
pub const LOCK: &str = "lock";
pub const UNLOCK: &str = "unlock";
pub const LOGIN: &str = "login";
pub const SHELL: &str = "shell";
pub const UID: &str = "uid";
pub const PASSWORD: &str = "password";
pub const ROOT: &str = "root";
pub const PREFIX: &str = "prefix";
pub const USER: &str = "USER";
}
#[derive(Debug)]
enum UsermodError {
CantUpdate(String),
UserNotFound(String),
UidInUse(String),
AlreadyPrinted(i32),
}
impl fmt::Display for UsermodError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::CantUpdate(msg) | Self::UserNotFound(msg) | Self::UidInUse(msg) => {
f.write_str(msg)
}
Self::AlreadyPrinted(_) => Ok(()),
}
}
}
impl std::error::Error for UsermodError {}
impl UError for UsermodError {
fn code(&self) -> i32 {
match self {
Self::CantUpdate(_) => 1,
Self::UserNotFound(_) => 6,
Self::UidInUse(_) => 4,
Self::AlreadyPrinted(c) => *c,
}
}
}
#[uucore::main]
#[allow(clippy::too_many_lines)]
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let _ = shadow_core::hardening::harden_process();
let matches = match uu_app().try_get_matches_from(args) {
Ok(m) => m,
Err(e) => {
e.print().ok();
if !e.use_stderr() {
return Ok(());
}
return Err(UsermodError::AlreadyPrinted(2).into());
}
};
let Some(login) = matches.get_one::<String>(options::USER) else {
return Err(UsermodError::AlreadyPrinted(2).into());
};
let prefix = matches
.get_one::<String>(options::PREFIX)
.or_else(|| matches.get_one::<String>(options::ROOT))
.map(Path::new);
let root = SysRoot::new(prefix);
if !rustix::process::getuid().is_root() {
return Err(UsermodError::CantUpdate(shadow_core::os_error::permission_denied()).into());
}
// Block signals for the passwd lock→write critical section only.
// Dropped before recursive_chown so long-running operations remain interruptible.
let signals = shadow_core::hardening::SignalBlocker::block_critical()
.map_err(|e| UsermodError::CantUpdate(format!("cannot block signals: {e}")))?;
// Modify /etc/passwd.
let passwd_path = root.passwd_path();
let lock = FileLock::acquire(&passwd_path)
.map_err(|e| UsermodError::CantUpdate(format!("cannot lock: {e}")))?;
let mut entries = passwd::read_passwd_file(&passwd_path)
.map_err(|e| UsermodError::CantUpdate(format!("{e}")))?;
let Some(idx) = entries.iter().position(|e| e.name == *login) else {
drop(lock);
return Err(UsermodError::UserNotFound(format!("user '{login}' does not exist")).into());
};
// Save the old UID and home dir before mutation so we can chown if needed.
let old_uid = entries[idx].uid;
let home_for_chown = entries[idx].home.clone();
let home_is_changing = matches.get_one::<String>(options::HOME).is_some();
// Check UID collision before mutating.
if let Some(&uid) = matches.get_one::<u32>(options::UID) {
if entries.iter().any(|e| e.uid == uid && e.name != *login) {
drop(lock);
return Err(UsermodError::UidInUse(format!("UID {uid} already in use")).into());
}
entries[idx].uid = uid;
}
if let Some(c) = matches.get_one::<String>(options::COMMENT) {
entries[idx].gecos.clone_from(c);
}
if let Some(h) = matches.get_one::<String>(options::HOME) {
entries[idx].home.clone_from(h);
}
if let Some(s) = matches.get_one::<String>(options::SHELL) {
entries[idx].shell.clone_from(s);
}
if let Some(&gid) = matches.get_one::<u32>(options::GID) {
entries[idx].gid = gid;
}
let new_login = matches.get_one::<String>(options::LOGIN);
if let Some(new_name) = new_login {
validate::validate_username(new_name)
.map_err(|e| UsermodError::CantUpdate(format!("invalid login name: {e}")))?;
entries[idx].name.clone_from(new_name);
}
let new_uid = entries[idx].uid;
atomic::atomic_write(&passwd_path, |f| passwd::write_passwd(&entries, f))
.map_err(|e| UsermodError::CantUpdate(format!("{e}")))?;
drop(lock);
// Restore signals before potentially long-running recursive chown.
drop(signals);
// If the UID changed and the home directory was not explicitly moved,
// recursively chown the existing home directory to the new UID.
// Only files owned by old_uid are touched (files owned by other users
// are left alone, matching GNU shadow-utils behavior).
if new_uid != old_uid && !home_is_changing && !home_for_chown.is_empty() {
let home_path = root.resolve(&home_for_chown);
if home_path.exists() {
recursive_chown(&home_path, old_uid, new_uid);
}
}
// Shadow modifications.
let shadow_path = root.shadow_path();
let do_lock = matches.get_flag(options::LOCK);
let do_unlock = matches.get_flag(options::UNLOCK);
let expire = matches.get_one::<String>(options::EXPIREDATE);
let inactive = matches.get_one::<i64>(options::INACTIVE);
let new_password = matches.get_one::<String>(options::PASSWORD);
if let Some(pw) = new_password
&& pw.contains([':', '\n', '\r'])
{
return Err(UsermodError::CantUpdate(
"invalid password hash: must not contain ':', '\\n', or '\\r'".into(),
)
.into());
}
let login_changing = new_login.is_some();
if shadow_path.exists()
&& (do_lock
|| do_unlock
|| expire.is_some()
|| inactive.is_some()
|| new_password.is_some()
|| login_changing)
{
let slock = FileLock::acquire(&shadow_path)
.map_err(|e| UsermodError::CantUpdate(format!("cannot lock shadow: {e}")))?;
let mut se = shadow::read_shadow_file(&shadow_path)
.map_err(|e| UsermodError::CantUpdate(format!("{e}")))?;
let Some(s) = se.iter_mut().find(|e| e.name == *login) else {
drop(slock);
return Err(UsermodError::CantUpdate(format!(
"user '{login}' not found in shadow file"
))
.into());
};
if do_lock {
s.lock();
}
if do_unlock {
s.unlock();
}
if let Some(exp) = expire {
s.expire_date = if exp == "-1" || exp.is_empty() {
None
} else {
Some(exp.parse::<i64>().map_err(|_| {
UsermodError::CantUpdate(format!(
"invalid expire date '{exp}' (expected days since epoch)"
))
})?)
};
}
if let Some(&i) = inactive {
s.inactive_days = if i < 0 { None } else { Some(i) };
}
if let Some(pw) = new_password {
s.passwd.clone_from(pw);
s.last_change = Some(shadow::days_since_epoch().map_err(|e| {
UsermodError::CantUpdate(format!("cannot determine current date: {e}"))
})?);
}
if let Some(new_name) = new_login {
s.name.clone_from(new_name);
}
atomic::atomic_write(&shadow_path, |f| shadow::write_shadow(&se, f))
.map_err(|e| UsermodError::CantUpdate(format!("{e}")))?;
drop(slock);
}
// Rename user in group membership lists when --login changes the name.
if let Some(new_name) = new_login {
let group_path = root.group_path();
if group_path.exists() {
let glock = FileLock::acquire(&group_path)
.map_err(|e| UsermodError::CantUpdate(format!("cannot lock group: {e}")))?;
let mut ge = group::read_group_file(&group_path)
.map_err(|e| UsermodError::CantUpdate(format!("{e}")))?;
let mut changed = false;
for g in &mut ge {
if let Some(m) = g.members.iter_mut().find(|m| **m == *login) {
m.clone_from(new_name);
changed = true;
}
}
if changed {
atomic::atomic_write(&group_path, |f| group::write_group(&ge, f))
.map_err(|e| UsermodError::CantUpdate(format!("{e}")))?;
}
drop(glock);
}
}
// Group modifications.
if let Some(groups_str) = matches.get_one::<String>(options::GROUPS) {
let group_path = root.group_path();
if group_path.exists() {
let append = matches.get_flag(options::APPEND);
let new_groups: Vec<&str> = groups_str.split(',').map(str::trim).collect();
let glock = FileLock::acquire(&group_path)
.map_err(|e| UsermodError::CantUpdate(format!("cannot lock group: {e}")))?;
let mut ge = group::read_group_file(&group_path)
.map_err(|e| UsermodError::CantUpdate(format!("{e}")))?;
// Validate all requested groups exist before mutating anything.
for gname in &new_groups {
if !ge.iter().any(|g| g.name == *gname) {
drop(glock);
return Err(UsermodError::CantUpdate(format!(
"group '{gname}' does not exist"
))
.into());
}
}
if !append {
for g in &mut ge {
g.members.retain(|m| m != login);
}
}
for gname in &new_groups {
if let Some(g) = ge.iter_mut().find(|g| g.name == *gname)
&& !g.members.iter().any(|m| m == login)
{
g.members.push(login.clone());
}
}
atomic::atomic_write(&group_path, |f| group::write_group(&ge, f))
.map_err(|e| UsermodError::CantUpdate(format!("{e}")))?;
drop(glock);
}
}
nscd::invalidate_cache("passwd");
nscd::invalidate_cache("group");
audit::log_user_event("MOD_USER", login, new_uid, true);
Ok(())
}
/// Recursively chown all files and directories under `path` that are owned by
/// `old_uid` to `new_uid`. Files owned by other users are left untouched.
///
/// Uses `fchownat` with `AT_SYMLINK_NOFOLLOW` so symlinks themselves are
/// re-owned without following them.
fn recursive_chown(path: &Path, old_uid: u32, new_uid: u32) {
use std::os::unix::fs::MetadataExt;
if let Ok(entries) = std::fs::read_dir(path) {
for entry in entries.flatten() {
let entry_path = entry.path();
if let Ok(meta) = std::fs::symlink_metadata(&entry_path) {
if meta.uid() == old_uid {
let _ = rustix::fs::chownat(
rustix::fs::CWD,
&entry_path,
Some(rustix::process::Uid::from_raw(new_uid)),
None,
rustix::fs::AtFlags::SYMLINK_NOFOLLOW,
);
}
if meta.is_dir() {
recursive_chown(&entry_path, old_uid, new_uid);
}
}
}
}
// Also chown the directory itself.
if let Ok(meta) = std::fs::symlink_metadata(path)
&& meta.uid() == old_uid
{
let _ = rustix::fs::chownat(
rustix::fs::CWD,
path,
Some(rustix::process::Uid::from_raw(new_uid)),
None,
rustix::fs::AtFlags::SYMLINK_NOFOLLOW,
);
}
}
#[must_use]
#[allow(clippy::too_many_lines)]
pub fn uu_app() -> Command {
Command::new("usermod")
.about("Edit a user account's fields")
.override_usage("usermod [options] LOGIN")
.version(shadow_core::cli::VERSION)
.after_help(shadow_core::cli::AFTER_HELP)
.arg(
Arg::new(options::COMMENT)
.short('c')
.long("comment")
.value_name("COMMENT")
.help("Replace the GECOS comment"),
)
.arg(
Arg::new(options::HOME)
.short('d')
.long("home")
.value_name("HOME_DIR")
.help("Replace the home directory path"),
)
.arg(
Arg::new(options::EXPIREDATE)
.short('e')
.long("expiredate")
.value_name("EXPIRE_DATE")
.help("Set the account expiration date"),
)
.arg(
Arg::new(options::INACTIVE)
.short('f')
.long("inactive")
.value_name("INACTIVE")
.value_parser(clap::value_parser!(i64))
.help("Days the password may stay expired before disabling the account"),
)
.arg(
Arg::new(options::GID)
.short('g')
.long("gid")
.value_name("GROUP")
.value_parser(clap::value_parser!(u32))
.help("Replace the primary group (numeric GID)"),
)
.arg(
Arg::new(options::GROUPS)
.short('G')
.long("groups")
.value_name("GROUPS")
.help("Replace supplementary groups (comma-separated)"),
)
.arg(
Arg::new(options::APPEND)
.short('a')
.long("append")
.help("Add to the supplementary groups instead of replacing them (only effective with -G)")
.action(ArgAction::SetTrue),
)
.arg(
Arg::new(options::LOCK)
.short('L')
.long("lock")
.help("Disable login by locking the password")
.conflicts_with(options::UNLOCK)
.action(ArgAction::SetTrue),
)
.arg(
Arg::new(options::UNLOCK)
.short('U')
.long("unlock")
.help("Re-enable login by unlocking the password")
.action(ArgAction::SetTrue),
)
.arg(
Arg::new(options::LOGIN)
.short('l')
.long("login")
.value_name("NEW_LOGIN")
.help("Rename the account"),
)
.arg(
Arg::new(options::PASSWORD)
.short('p')
.long("password")
.value_name("PASSWORD")
.help("Replace the password field with a crypt(3) hash"),
)
.arg(
Arg::new(options::SHELL)
.short('s')
.long("shell")
.value_name("SHELL")
.help("Replace the login shell"),
)
.arg(
Arg::new(options::UID)
.short('u')
.long("uid")
.value_name("UID")
.value_parser(clap::value_parser!(u32))
.help("Replace the numeric UID"),
)
.arg(
Arg::new(options::ROOT)
.short('R')
.long("root")
.value_name("ROOT_DIR")
.help("Locate the system files under ROOT_DIR instead of /"),
)
.arg(
Arg::new(options::PREFIX)
.short('P')
.long("prefix")
.value_name("PREFIX_DIR")
.help("Directory prefix"),
)
.arg(
Arg::new(options::USER)
.required(true)
.index(1)
.help("Login name"),
)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_app_builds() {
uu_app().debug_assert();
}
#[test]
fn test_user_required() {
assert!(uu_app().try_get_matches_from(["usermod"]).is_err());
}
#[test]
fn test_lock_unlock_conflict() {
assert!(
uu_app()
.try_get_matches_from(["usermod", "-L", "-U", "u"])
.is_err()
);
}
#[test]
fn test_append_groups() {
let m = uu_app()
.try_get_matches_from(["usermod", "-a", "-G", "sudo,docker", "u"])
.unwrap();
assert!(m.get_flag(options::APPEND));
assert_eq!(
m.get_one::<String>(options::GROUPS).map(String::as_str),
Some("sudo,docker")
);
}
fn skip_unless_root() -> bool {
!rustix::process::geteuid().is_root()
}
#[test]
fn test_modify_shell_with_prefix() {
if skip_unless_root() {
return;
}
let dir = tempfile::tempdir().unwrap();
let etc = dir.path().join("etc");
std::fs::create_dir_all(&etc).unwrap();
std::fs::write(
etc.join("passwd"),
"testuser:x:1000:1000:Test:/home/testuser:/bin/bash\n",
)
.unwrap();
let code = uumain(
vec![
"usermod".into(),
"-s".into(),
"/bin/zsh".into(),
"-P".into(),
dir.path().as_os_str().to_owned(),
"testuser".into(),
]
.into_iter(),
);
assert_eq!(code, 0);
let content = std::fs::read_to_string(etc.join("passwd")).unwrap();
assert!(content.contains("/bin/zsh"));
}
}