#user #group #shadow #user-password #information #passwd #function

passwd-rs

Crate/Library providing a functions to get information about groups, users and shadow passwords

2 unstable releases

0.2.0 Oct 26, 2022
0.1.0 Oct 26, 2022

#868 in Authentication

Download history 71/week @ 2024-07-23 126/week @ 2024-07-30 33/week @ 2024-08-06 28/week @ 2024-08-13 53/week @ 2024-08-20 30/week @ 2024-08-27 32/week @ 2024-09-03 54/week @ 2024-09-10 25/week @ 2024-09-17 38/week @ 2024-09-24 45/week @ 2024-10-01 37/week @ 2024-10-08 73/week @ 2024-10-15 87/week @ 2024-10-22 70/week @ 2024-10-29 136/week @ 2024-11-05

374 downloads per month
Used in yubico_helper

MIT license

13KB
305 lines

passwd-rs

Crate/Library providing a functions to get information about groups, users and shadow passwords.

Example

use passwd_rs::{Group, User, Shadow, AccountStatus};

fn main() -> std::io::Result<()> {
	let user = User::current_user()?;
	let password;
	if user.passwd.as_ref().unwrap().eq("x") {
		// WARN! This works only if program is executed as root
		let shadow = match Shadow::new_from_username(&user.name.clone()) {
			Err(e) => {
				if e.kind() == std::io::ErrorKind::PermissionDenied {
					println!("Must be run as root to access shadow passwords");
				} else { return Err(e) };

				return Ok(());
			},
			Ok(o) => o,
		};
		if let AccountStatus::Active(passwd) = shadow.passwd {
			password = passwd;
		} else {
			password = shadow.passwd.to_string();
		}
	} else {
		password = user.passwd.unwrap();
	}

	let group = Group::new_from_gid(user.gid.clone())?;

	println!("Group details:");
	println!("Name: {}", group.name);
	println!("ID: {}", group.gid);
	println!("Members: {}", group.display_members());
	println!();
	println!("User details:");
	println!("Name: {}", user.name);
	println!("ID: {}", user.uid);
	println!("Password: {}", password);

	Ok(())
}

No runtime deps