summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorTomasz Kramkowski <tomasz@kramkow.ski>2023-01-29 17:23:14 +0000
committerTomasz Kramkowski <tomasz@kramkow.ski>2023-01-29 17:23:14 +0000
commitdca986f599e1abe4270a06efaf91759a926f2424 (patch)
treeaee9f548eb5f39fbb5f36581b7b28fe500034559 /src
parent63aab7ac772622173f38309cc9ed5b0cd11ea58f (diff)
downloadpam_usercg_rust-dca986f599e1abe4270a06efaf91759a926f2424.tar.gz
pam_usercg_rust-dca986f599e1abe4270a06efaf91759a926f2424.tar.xz
pam_usercg_rust-dca986f599e1abe4270a06efaf91759a926f2424.zip
Remove users dependency
Diffstat (limited to 'src')
-rw-r--r--src/lib.rs9
-rw-r--r--src/passwd.rs11
2 files changed, 17 insertions, 3 deletions
diff --git a/src/lib.rs b/src/lib.rs
index e7b6edc..c104a23 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -1,9 +1,11 @@
// Copyright (C) 2023 Tomasz Kramkowski <tomasz@kramkow.ski>
// SPDX-License-Identifier: MIT
+mod passwd;
+
use cap_std::fs::{Dir, OpenOptions};
use pam::module::PamHandle;
-use std::ffi::{c_char, c_int};
+use std::ffi::{c_char, c_int, CString};
use std::io::{ErrorKind, Write};
use std::panic;
use std::path::Path;
@@ -31,8 +33,9 @@ impl From<std::io::Error> for SessionError {
fn open_session(h: &PamHandle, mountpoint: &str) -> Result<(), SessionError> {
let user = h.get_user(None).or(Err(SessionError))?;
- let user = users::get_user_by_name(&user).ok_or(SessionError)?;
- let uid = user.uid().to_string();
+ let user = CString::new(user).or(Err(SessionError))?;
+ let uid = passwd::get_uid_by_name(&user).ok_or(SessionError)?;
+ let uid = uid.to_string();
let d = Dir::open_ambient_dir(mountpoint, cap_std::ambient_authority())?;
let d = create_and_open_dir(&d, "user")?;
let d = create_and_open_dir(&d, &uid)?;
diff --git a/src/passwd.rs b/src/passwd.rs
new file mode 100644
index 0000000..40ec947
--- /dev/null
+++ b/src/passwd.rs
@@ -0,0 +1,11 @@
+use libc::uid_t;
+use std::ffi::CStr;
+
+pub fn get_uid_by_name<S: AsRef<CStr> + ?Sized>(name: &S) -> Option<uid_t> {
+ let passwd = unsafe { libc::getpwnam(name.as_ref().as_ptr()) };
+ if !passwd.is_null() {
+ Some(unsafe { *passwd }.pw_uid)
+ } else {
+ None
+ }
+}