Merge branch 'master' of git.flanchan.moe:flanchan/lolicron into master

master
Avril 4 years ago
commit 92d88ce74a
Signed by: flanchan
GPG Key ID: 284488987C31F630

@ -22,6 +22,8 @@ use util::*;
mod log;
mod sys;
mod interval;
mod config;

@ -1 +1,76 @@
//! passwd queries
use super::*;
use std::sync::Mutex;
use libc::{
getpwent,
endpwent
};
/// Safe wrapper around C's passwd struct. Doesn't implement the
/// `passwd', `gecos' or `expire' fields because we don't use them.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Passwd {
name: String,
uid: u32,
gid: u32,
home: String,
shell: String,
}
/// Returns a vector of the system's users
pub fn get_users() -> std::vec::Vec<Passwd> {
lazy_static::lazy_static! {
static ref MUTEX: Mutex<()> = Mutex::new(());
}
let mut users = vec!();
unsafe {
macro_rules! from_cstr {
($x:expr, $fmt:literal, $usr:expr) => {
match std::ffi::CStr::from_ptr($x).to_owned().into_string() {
Ok(v) => v,
Err(v) => {
warn!($fmt, $usr, v);
continue;
}
}
}
}
let _lock = MUTEX.lock().expect("poisoned");
loop {
let passwd = getpwent();
// FFI returns NULL when no more entries
if passwd.is_null() {
break;
}
let my_passwd = *passwd;
let name = from_cstr!(
my_passwd.pw_name,
"{}User {} has invalid UTF-8 characters, skipping",
"" // Lol
);
users.push(Passwd{
gid: 1,
uid: 2,
home: from_cstr!(
my_passwd.pw_dir,
"User {}'s home ({}) has invalid UTF-8 characters, skipping",
name
),
shell: from_cstr!(
my_passwd.pw_shell,
"User {}'s shell ({}) has invalid UTF-8 characters, skipping",
name
),
name
});
}
endpwent();
}
return users;
}

Loading…
Cancel
Save