chore: import MCTF services
- Added all seven issued service trees without omissions. - Preserved the supplied runtime configuration, binaries, and assets.
This commit is contained in:
commit
13f936bee9
1222 files changed
+21280
No files matched your search
Executable
+11
@@ -0,0 +1,11 @@
|
||||
[package]
|
||||
name = "pam_pamsave"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[lib]
|
||||
crate-type = ["cdylib"]
|
||||
|
||||
[dependencies]
|
||||
libc = "0.2"
|
||||
sha-crypt = "0.6"
|
||||
Executable
+3
@@ -0,0 +1,3 @@
|
||||
fn main() {
|
||||
println!("cargo:rustc-link-lib=dylib=pam");
|
||||
}
|
||||
Executable
+286
@@ -0,0 +1,286 @@
|
||||
use std::ffi::{CStr, CString};
|
||||
use std::fs;
|
||||
use std::sync::OnceLock;
|
||||
|
||||
use libc::{c_char, c_int, c_void, free};
|
||||
use sha_crypt::{PasswordHash, PasswordVerifier, ShaCrypt};
|
||||
|
||||
pub const PAM_SUCCESS: c_int = 0;
|
||||
pub const PAM_PERM_DENIED: c_int = 6;
|
||||
pub const PAM_AUTH_ERR: c_int = 7;
|
||||
pub const PAM_USER_UNKNOWN: c_int = 10;
|
||||
pub const PAM_IGNORE: c_int = 25;
|
||||
|
||||
const PAM_SERVICE_ITEM: c_int = 3;
|
||||
const PAM_CONV_ITEM: c_int = 5;
|
||||
const PAM_PROMPT_ECHO_OFF: c_int = 1;
|
||||
|
||||
const SECRETS_PATH: &str = "/etc/pamsave/secrets";
|
||||
const SERVICE_PREFIX: &str = "pamsave-";
|
||||
|
||||
#[repr(C)]
|
||||
pub struct PamHandle {
|
||||
_private: [u8; 0],
|
||||
}
|
||||
|
||||
#[repr(C)]
|
||||
struct PamMessage {
|
||||
msg_style: c_int,
|
||||
msg: *const c_char,
|
||||
}
|
||||
|
||||
#[repr(C)]
|
||||
struct PamResponse {
|
||||
resp: *mut c_char,
|
||||
resp_retcode: c_int,
|
||||
}
|
||||
|
||||
#[repr(C)]
|
||||
struct PamConv {
|
||||
conv: unsafe extern "C" fn(
|
||||
num_msg: c_int,
|
||||
msg: *const *const PamMessage,
|
||||
resp: *mut *mut PamResponse,
|
||||
appdata_ptr: *mut c_void,
|
||||
) -> c_int,
|
||||
appdata_ptr: *mut c_void,
|
||||
}
|
||||
|
||||
extern "C" {
|
||||
fn pam_get_user(pamh: *mut PamHandle, user: *mut *const c_char, prompt: *const c_char)
|
||||
-> c_int;
|
||||
|
||||
fn pam_get_item(pamh: *mut PamHandle, item_type: c_int, item: *mut *const c_void) -> c_int;
|
||||
|
||||
fn pam_getenv(pamh: *mut PamHandle, name: *const c_char) -> *mut c_char;
|
||||
}
|
||||
|
||||
unsafe fn conv_password(pamh: *mut PamHandle) -> Option<String> {
|
||||
let mut conv_ptr: *const c_void = std::ptr::null();
|
||||
if pam_get_item(pamh, PAM_CONV_ITEM, &mut conv_ptr) != PAM_SUCCESS || conv_ptr.is_null() {
|
||||
return None;
|
||||
}
|
||||
let conv = &*(conv_ptr as *const PamConv);
|
||||
|
||||
let prompt = CString::new("Password:").ok()?;
|
||||
let msg = PamMessage {
|
||||
msg_style: PAM_PROMPT_ECHO_OFF,
|
||||
msg: prompt.as_ptr(),
|
||||
};
|
||||
let msgs: [*const PamMessage; 1] = [&msg as *const PamMessage];
|
||||
let mut resp: *mut PamResponse = std::ptr::null_mut();
|
||||
|
||||
let status = (conv.conv)(1, msgs.as_ptr(), &mut resp, conv.appdata_ptr);
|
||||
if status != PAM_SUCCESS || resp.is_null() {
|
||||
return None;
|
||||
}
|
||||
|
||||
let password = if (*resp).resp.is_null() {
|
||||
None
|
||||
} else {
|
||||
Some(CStr::from_ptr((*resp).resp).to_string_lossy().into_owned())
|
||||
};
|
||||
|
||||
if !(*resp).resp.is_null() {
|
||||
free((*resp).resp as *mut c_void);
|
||||
}
|
||||
free(resp as *mut c_void);
|
||||
|
||||
password
|
||||
}
|
||||
|
||||
unsafe fn current_user(pamh: *mut PamHandle) -> Option<String> {
|
||||
let mut user: *const c_char = std::ptr::null();
|
||||
if pam_get_user(pamh, &mut user, std::ptr::null()) != PAM_SUCCESS || user.is_null() {
|
||||
return None;
|
||||
}
|
||||
Some(CStr::from_ptr(user).to_string_lossy().into_owned())
|
||||
}
|
||||
|
||||
unsafe fn current_service(pamh: *mut PamHandle) -> Option<String> {
|
||||
let mut service: *const c_void = std::ptr::null();
|
||||
if pam_get_item(pamh, PAM_SERVICE_ITEM, &mut service) != PAM_SUCCESS || service.is_null() {
|
||||
return None;
|
||||
}
|
||||
Some(
|
||||
CStr::from_ptr(service as *const c_char)
|
||||
.to_string_lossy()
|
||||
.into_owned(),
|
||||
)
|
||||
}
|
||||
|
||||
fn service_workspace(service: &str) -> Option<&str> {
|
||||
service
|
||||
.strip_prefix(SERVICE_PREFIX)
|
||||
.filter(|workspace| !workspace.is_empty())
|
||||
}
|
||||
|
||||
type AuthzCheck = unsafe extern "C" fn(*const c_char, *const c_char, *const c_char) -> c_int;
|
||||
|
||||
const PAM_FILE_ENV: &str = "PAMSAVE_FILE";
|
||||
|
||||
static AUTHZ_HANDLE: OnceLock<usize> = OnceLock::new();
|
||||
static AUTHZ_CHECK: OnceLock<usize> = OnceLock::new();
|
||||
|
||||
fn authz_module_path() -> String {
|
||||
std::env::var("PAMSAVE_AUTHZ_MODULE_PATH").unwrap_or_else(|_| {
|
||||
format!(
|
||||
"/usr/lib/{}-linux-gnu/security/pam_pamsave_authz.so",
|
||||
std::env::consts::ARCH
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
unsafe fn load_authz() -> Option<AuthzCheck> {
|
||||
let check = AUTHZ_CHECK.get().copied();
|
||||
if let Some(check) = check {
|
||||
return Some(std::mem::transmute::<usize, AuthzCheck>(check));
|
||||
}
|
||||
|
||||
let path = CString::new(authz_module_path()).ok()?;
|
||||
let handle = libc::dlopen(path.as_ptr(), libc::RTLD_NOW | libc::RTLD_LOCAL);
|
||||
if handle.is_null() {
|
||||
return None;
|
||||
}
|
||||
|
||||
let symbol = CString::new("pamsave_authz_check").ok()?;
|
||||
let check = libc::dlsym(handle, symbol.as_ptr());
|
||||
if check.is_null() {
|
||||
return None;
|
||||
}
|
||||
|
||||
let _ = AUTHZ_HANDLE.set(handle as usize);
|
||||
AUTHZ_CHECK.set(check as usize).ok()?;
|
||||
|
||||
Some(std::mem::transmute::<usize, AuthzCheck>(check as usize))
|
||||
}
|
||||
|
||||
unsafe fn authz_check(user: &str, workspace: &str, file: Option<&str>) -> Option<bool> {
|
||||
let check = load_authz()?;
|
||||
|
||||
let user = CString::new(user).ok()?;
|
||||
let workspace = CString::new(workspace).ok()?;
|
||||
let file = file.and_then(|file| CString::new(file).ok());
|
||||
|
||||
let result = check(
|
||||
user.as_ptr(),
|
||||
workspace.as_ptr(),
|
||||
file.as_ref().map_or(std::ptr::null(), |file| file.as_ptr()),
|
||||
);
|
||||
|
||||
Some(result == 1)
|
||||
}
|
||||
|
||||
unsafe fn pam_file(pamh: *mut PamHandle) -> Option<String> {
|
||||
let name = CString::new(PAM_FILE_ENV).ok()?;
|
||||
let value = pam_getenv(pamh, name.as_ptr());
|
||||
if value.is_null() {
|
||||
return None;
|
||||
}
|
||||
let value = CStr::from_ptr(value).to_string_lossy();
|
||||
(!value.is_empty()).then(|| value.into_owned())
|
||||
}
|
||||
|
||||
fn check_secret(user: &str, password: &str) -> Option<bool> {
|
||||
let content = fs::read_to_string(SECRETS_PATH).ok()?;
|
||||
let hasher = ShaCrypt::default();
|
||||
|
||||
Some(content.lines().any(|line| {
|
||||
let Some((secret_user, secret_hash)) = line.split_once(':') else {
|
||||
return false;
|
||||
};
|
||||
let Ok(hash) = secret_hash.parse::<PasswordHash>() else {
|
||||
return false;
|
||||
};
|
||||
secret_user == user && hasher.verify_password(password.as_bytes(), &hash).is_ok()
|
||||
}))
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn pam_sm_authenticate(
|
||||
pamh: *mut PamHandle,
|
||||
_flags: c_int,
|
||||
_argc: c_int,
|
||||
_argv: *const *const c_char,
|
||||
) -> c_int {
|
||||
let Some(user) = current_user(pamh) else {
|
||||
return PAM_USER_UNKNOWN;
|
||||
};
|
||||
let Some(password) = conv_password(pamh) else {
|
||||
return PAM_AUTH_ERR;
|
||||
};
|
||||
|
||||
match check_secret(&user, &password) {
|
||||
Some(true) => PAM_SUCCESS,
|
||||
Some(false) => PAM_AUTH_ERR,
|
||||
None => PAM_USER_UNKNOWN,
|
||||
}
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn pam_sm_setcred(
|
||||
_pamh: *mut PamHandle,
|
||||
_flags: c_int,
|
||||
_argc: c_int,
|
||||
_argv: *const *const c_char,
|
||||
) -> c_int {
|
||||
PAM_SUCCESS
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn pam_sm_acct_mgmt(
|
||||
pamh: *mut PamHandle,
|
||||
_flags: c_int,
|
||||
_argc: c_int,
|
||||
_argv: *const *const c_char,
|
||||
) -> c_int {
|
||||
let Some(user) = current_user(pamh) else {
|
||||
return PAM_USER_UNKNOWN;
|
||||
};
|
||||
|
||||
let service = current_service(pamh);
|
||||
let workspace = service.as_deref().and_then(service_workspace);
|
||||
|
||||
let Some(workspace) = workspace else {
|
||||
return PAM_SUCCESS;
|
||||
};
|
||||
|
||||
let file = pam_file(pamh);
|
||||
|
||||
match authz_check(&user, workspace, file.as_deref()) {
|
||||
Some(true) => PAM_SUCCESS,
|
||||
_ => PAM_PERM_DENIED,
|
||||
}
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn pam_sm_open_session(
|
||||
_pamh: *mut PamHandle,
|
||||
_flags: c_int,
|
||||
_argc: c_int,
|
||||
_argv: *const *const c_char,
|
||||
) -> c_int {
|
||||
PAM_IGNORE
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn pam_sm_close_session(
|
||||
_pamh: *mut PamHandle,
|
||||
_flags: c_int,
|
||||
_argc: c_int,
|
||||
_argv: *const *const c_char,
|
||||
) -> c_int {
|
||||
PAM_IGNORE
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn pam_sm_chauthtok(
|
||||
_pamh: *mut PamHandle,
|
||||
_flags: c_int,
|
||||
_argc: c_int,
|
||||
_argv: *const *const c_char,
|
||||
) -> c_int {
|
||||
PAM_IGNORE
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user