forked from fufesou/pam
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FEAT] Add experimental support for writing PAM modules
- Loading branch information
Showing
5 changed files
with
133 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,124 @@ | ||
//! Functions and structures that can be used to implement a PAM module | ||
//! | ||
//! Inspired by anowell/pam-rs | ||
use crate::{PamHandle, PamReturnCode}; | ||
use std::ffi::CStr; | ||
use std::os::raw::c_uint; | ||
|
||
// FIXME: Find a solution for the flags containing ORed integers | ||
#[allow(unused_variables)] | ||
/// Trait representing a PAM module. | ||
/// | ||
/// Modules should override the desired functions and call the macro `impl_pam_module`. | ||
/// This exports the respective functions at the expected symbols prefixed with `pam_sm_`. | ||
pub trait PamModule { | ||
fn account_management(handle: &PamHandle, args: Vec<&CStr>, flags: c_uint) -> PamReturnCode { | ||
PamReturnCode::Ignore | ||
} | ||
fn authenticate(handle: &PamHandle, args: Vec<&CStr>, flags: c_uint) -> PamReturnCode { | ||
PamReturnCode::Ignore | ||
} | ||
fn change_auth_token(handle: &PamHandle, args: Vec<&CStr>, flags: c_uint) -> PamReturnCode { | ||
PamReturnCode::Ignore | ||
} | ||
fn close_session(handle: &PamHandle, args: Vec<&CStr>, flags: c_uint) -> PamReturnCode { | ||
PamReturnCode::Ignore | ||
} | ||
fn open_session(handle: &PamHandle, args: Vec<&CStr>, flags: c_uint) -> PamReturnCode { | ||
PamReturnCode::Ignore | ||
} | ||
fn set_credentials(handle: &PamHandle, args: Vec<&CStr>, flags: c_uint) -> PamReturnCode { | ||
PamReturnCode::Ignore | ||
} | ||
} | ||
|
||
#[macro_export] | ||
macro_rules! impl_pam_module { | ||
($struct:ident) => { | ||
pub use _pam_module_::*; | ||
mod _pam_module_ { | ||
use crate::{module::PamModule, PamHandle, PamReturnCode}; | ||
use std::ffi::CStr; | ||
use std::os::raw::{c_char, c_int, c_uint}; | ||
|
||
fn convert_args<'a>(argc: c_int, argv: *const *const c_char) -> Vec<&'a CStr> { | ||
(0..argc) | ||
.map(|i| unsafe { CStr::from_ptr(*argv.offset(i as isize)) }) | ||
.collect() | ||
} | ||
|
||
#[no_mangle] | ||
pub extern "C" fn pam_sm_acct_mgmt( | ||
handle: &PamHandle, | ||
flags: c_uint, | ||
argc: c_int, | ||
argv: *const *const c_char, | ||
) -> PamReturnCode { | ||
let args = convert_args(argc, argv); | ||
super::$struct::account_management(handle, args, flags) | ||
} | ||
#[no_mangle] | ||
pub extern "C" fn pam_sm_authenticate( | ||
handle: &PamHandle, | ||
flags: c_uint, | ||
argc: c_int, | ||
argv: *const *const c_char, | ||
) -> PamReturnCode { | ||
let args = convert_args(argc, argv); | ||
super::$struct::authenticate(handle, args, flags) | ||
} | ||
#[no_mangle] | ||
pub extern "C" fn pam_sm_chauthtok( | ||
handle: &PamHandle, | ||
flags: c_uint, | ||
argc: c_int, | ||
argv: *const *const c_char, | ||
) -> PamReturnCode { | ||
let args = convert_args(argc, argv); | ||
super::$struct::change_auth_token(handle, args, flags) | ||
} | ||
#[no_mangle] | ||
pub extern "C" fn pam_sm_close_session( | ||
handle: &PamHandle, | ||
flags: c_uint, | ||
argc: c_int, | ||
argv: *const *const c_char, | ||
) -> PamReturnCode { | ||
let args = convert_args(argc, argv); | ||
super::$struct::close_session(handle, args, flags) | ||
} | ||
#[no_mangle] | ||
pub extern "C" fn pam_sm_open_session( | ||
handle: &PamHandle, | ||
flags: c_uint, | ||
argc: c_int, | ||
argv: *const *const c_char, | ||
) -> PamReturnCode { | ||
let args = convert_args(argc, argv); | ||
super::$struct::open_session(handle, args, flags) | ||
} | ||
#[no_mangle] | ||
pub extern "C" fn pam_sm_setcred( | ||
handle: &PamHandle, | ||
flags: c_uint, | ||
argc: c_int, | ||
argv: *const *const c_char, | ||
) -> PamReturnCode { | ||
let args = convert_args(argc, argv); | ||
super::$struct::set_credentials(handle, args, flags) | ||
} | ||
} | ||
}; | ||
} | ||
|
||
#[cfg(test)] | ||
pub mod test { | ||
use super::PamModule; | ||
use crate::impl_pam_module; | ||
|
||
pub struct TestModule; | ||
impl PamModule for TestModule {} | ||
|
||
impl_pam_module!(TestModule); | ||
} |