Skip to content

Commit

Permalink
Retrieve user list from NSS (#72).
Browse files Browse the repository at this point in the history
  • Loading branch information
apognu committed Nov 5, 2023
1 parent de5f73b commit ad86321
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 34 deletions.
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ tokio = { version = "^1.2", default-features = false, features = [
] }
unic-langid = "^0.9"
zeroize = "^1.3"
# TODO: Change to published version when a new release is out.
uzers = { git = "https://github.com/rustadopt/uzers-rs", branch = "main" }

[profile.release]
lto = true
48 changes: 14 additions & 34 deletions src/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use std::{
use ini::Ini;
use lazy_static::lazy_static;
use nix::sys::utsname;
use uzers::os::unix::UserExt;

use crate::{
ui::{
Expand Down Expand Up @@ -158,41 +159,20 @@ pub fn delete_last_user_session_path(username: &str) {
}

pub fn get_users(min_uid: u16, max_uid: u16) -> Vec<User> {
match File::open("/etc/passwd") {
Err(_) => vec![],
Ok(file) => {
let file = BufReader::new(file);
let users = unsafe { uzers::all_users() };

let users: Vec<User> = users
.filter(|user| user.uid() >= min_uid as u32 && user.uid() <= max_uid as u32)
.map(|user| User {
username: user.name().to_string_lossy().to_string(),
name: match user.gecos() {
name if name.is_empty() => None,
name => Some(name.to_string_lossy().to_string()),
},
})
.collect();

let users: Vec<User> = file
.lines()
.filter_map(|line| {
line
.map(|line| {
let mut split = line.splitn(7, ':');
let username = split.next();
let uid = split.nth(1);
let name = split.nth(1);

match uid.map(|uid| uid.parse::<u16>()) {
Some(Ok(uid)) => match (username, name) {
(Some(username), Some("")) => Some((uid, username.to_string(), None)),
(Some(username), Some(name)) => Some((uid, username.to_string(), Some(name.to_string()))),
_ => None,
},

_ => None,
}
})
.ok()
.flatten()
.filter(|(uid, _, _)| uid >= &min_uid && uid <= &max_uid)
.map(|(_, username, name)| User { username, name })
})
.collect();

users
}
}
users
}

pub fn get_min_max_uids(min_uid: Option<u16>, max_uid: Option<u16>) -> (u16, u16) {
Expand Down

0 comments on commit ad86321

Please sign in to comment.