passwd: Reduce code nesting and minor cleanups

This commit is contained in:
bjorn3
2025-04-20 18:21:15 +02:00
parent 51d2db809f
commit 0a2dea6b04
+73 -77
View File
@@ -39,10 +39,8 @@ AUTHOR
"#; /* @MANEND */
fn main() {
let stdin = io::stdin();
let mut stdin = stdin.lock();
let stdout = io::stdout();
let mut stdout = stdout.lock();
let mut stdin = io::stdin().lock();
let mut stdout = io::stdout().lock();
let mut stderr = io::stderr();
let args = clap_app!(passwd =>
@@ -56,83 +54,81 @@ fn main() {
let uid = get_uid().unwrap_or_exit(1);
let mut users = AllUsers::authenticator(Config::default().writeable(true)).unwrap_or_exit(1);
{
let user = match args.value_of("LOGIN") {
Some(login) => users.get_mut_by_name(login).unwrap_or_else(|| {
eprintln!("passwd: user does not exist: {}", login);
exit(1);
}),
None => users.get_mut_by_id(uid).unwrap_or_else(|| {
eprintln!("passwd: you do not exist");
exit(1);
}),
};
let user = match args.value_of("LOGIN") {
Some(login) => users.get_mut_by_name(login).unwrap_or_else(|| {
eprintln!("passwd: user does not exist: {}", login);
exit(1);
}),
None => users.get_mut_by_id(uid).unwrap_or_else(|| {
eprintln!("passwd: you do not exist");
exit(1);
}),
};
if args.is_present("LOCK") {
user.unset_passwd();
} else if user.uid == uid || uid == 0 {
let msg = format!("changing password for '{}' \n", user.user);
stdout.write_all(&msg.as_bytes()).r#try(&mut stderr);
if args.is_present("LOCK") {
user.unset_passwd();
users.save().unwrap_or_exit(1);
return;
}
if user.uid != uid && uid != 0 {
eprintln!(
"passwd: you do not have permission to set the password of '{}'",
user.user
);
exit(1);
}
let msg = format!("changing password for '{}' \n", user.user);
stdout.write_all(&msg.as_bytes()).r#try(&mut stderr);
stdout.flush().r#try(&mut stderr);
let mut verified = false;
if user.is_passwd_blank() {
verified = true;
} else if user.is_passwd_unset() && uid != 0 {
verified = false;
} else if user.uid == uid || uid != 0 {
stdout.write_all(b"current password: ").r#try(&mut stderr);
stdout.flush().r#try(&mut stderr);
if let Some(password) = stdin.read_passwd(&mut stdout).r#try(&mut stderr) {
stdout.write(b"\n").r#try(&mut stderr);
stdout.flush().r#try(&mut stderr);
let mut verified = false;
if user.is_passwd_blank() {
verified = true;
} else if user.is_passwd_unset() && uid != 0 {
verified = false;
} else if user.uid == uid || uid != 0 {
stdout.write_all(b"current password: ").r#try(&mut stderr);
stdout.flush().r#try(&mut stderr);
if let Some(password) = stdin.read_passwd(&mut stdout).r#try(&mut stderr) {
stdout.write(b"\n").r#try(&mut stderr);
stdout.flush().r#try(&mut stderr);
verified = user.verify_passwd(&password)
}
} else {
verified = true;
}
if verified {
stdout.write_all(b"new password: ").r#try(&mut stderr);
stdout.flush().r#try(&mut stderr);
if let Some(new_password) = stdin.read_passwd(&mut stdout).r#try(&mut stderr) {
stdout.write(b"\nconfirm password: ").r#try(&mut stderr);
stdout.flush().r#try(&mut stderr);
if let Some(confirm_password) =
stdin.read_passwd(&mut stdout).r#try(&mut stderr)
{
stdout.write(b"\n").r#try(&mut stderr);
stdout.flush().r#try(&mut stderr);
if new_password == confirm_password {
user.set_passwd(&new_password).unwrap_or_exit(1);
} else {
eprintln!("passwd: new password does not match confirm password");
exit(1);
}
} else {
eprintln!("passwd: no confirm password provided");
exit(1);
}
} else {
eprintln!("passwd: no new password provided");
exit(1);
}
} else {
eprintln!("passwd: incorrect current password");
exit(1);
}
} else {
eprintln!(
"passwd: you do not have permission to set the password of '{}'",
user.user
);
exit(1);
verified = user.verify_passwd(&password)
}
} else {
verified = true;
}
if !verified {
eprintln!("passwd: incorrect current password");
exit(1);
}
stdout.write_all(b"new password: ").r#try(&mut stderr);
stdout.flush().r#try(&mut stderr);
let Some(new_password) = stdin.read_passwd(&mut stdout).r#try(&mut stderr) else {
eprintln!("passwd: no new password provided");
exit(1);
};
stdout.write(b"\nconfirm password: ").r#try(&mut stderr);
stdout.flush().r#try(&mut stderr);
let Some(confirm_password) = stdin.read_passwd(&mut stdout).r#try(&mut stderr) else {
eprintln!("\npasswd: no confirm password provided");
exit(1);
};
stdout.write(b"\n").r#try(&mut stderr);
stdout.flush().r#try(&mut stderr);
if new_password != confirm_password {
eprintln!("passwd: new password does not match confirm password");
exit(1);
}
user.set_passwd(&new_password).unwrap_or_exit(1);
users.save().unwrap_or_exit(1);
}