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

passwd-rs

提供获取组、用户和影子密码信息的函数的库

2个不稳定版本

0.2.0 2022年10月26日
0.1.0 2022年10月26日

#658 in 身份验证

Download history 28/week @ 2024-04-01 1/week @ 2024-04-15 31/week @ 2024-04-29 51/week @ 2024-05-06 27/week @ 2024-05-13 4/week @ 2024-05-20 18/week @ 2024-05-27 67/week @ 2024-06-03 37/week @ 2024-06-10 112/week @ 2024-06-17 59/week @ 2024-06-24

276 每月下载量
yubico_helper 中使用

MIT 许可证

13KB
305

passwd-rs

提供获取组、用户和影子密码信息的函数的库。

示例

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(())
}

无运行时依赖