Skip to content

Commit

Permalink
Merge pull request #822 from memorysafety/portable-c-binding
Browse files Browse the repository at this point in the history
Portable C binding
  • Loading branch information
pvdrz authored Jan 19, 2024
2 parents 2c9b476 + 3260252 commit 23e8b0b
Show file tree
Hide file tree
Showing 4 changed files with 188 additions and 207 deletions.
2 changes: 1 addition & 1 deletion src/pam/converse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ impl PamMessageStyle {
pub fn from_int(val: libc::c_int) -> Option<PamMessageStyle> {
use PamMessageStyle::*;

match val as libc::c_uint {
match val {
PAM_PROMPT_ECHO_OFF => Some(PromptEchoOff),
PAM_PROMPT_ECHO_ON => Some(PromptEchoOn),
PAM_ERROR_MSG => Some(ErrorMessage),
Expand Down
2 changes: 1 addition & 1 deletion src/pam/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ impl PamErrorType {
pub(super) fn from_int(errno: libc::c_int) -> PamErrorType {
use PamErrorType::*;

match errno as libc::c_uint {
match errno {
PAM_SUCCESS => Success,
PAM_OPEN_ERR => OpenError,
PAM_SYMBOL_ERR => SymbolError,
Expand Down
32 changes: 8 additions & 24 deletions src/pam/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ impl<C: Converser> PamContext<C> {
/// Get the PAM flag value for the silent flag
fn silent_flag(&self) -> i32 {
if self.silent {
PAM_SILENT as i32
PAM_SILENT
} else {
0
}
Expand All @@ -151,7 +151,7 @@ impl<C: Converser> PamContext<C> {
if self.allow_null_auth_token {
0
} else {
PAM_DISALLOW_NULL_AUTHTOK as i32
PAM_DISALLOW_NULL_AUTHTOK
}
}

Expand Down Expand Up @@ -196,18 +196,14 @@ impl<C: Converser> PamContext<C> {
pub fn set_user(&mut self, user: &str) -> PamResult<()> {
let c_user = CString::new(user)?;
pam_err(unsafe {
pam_set_item(
self.pamh,
PAM_USER as i32,
c_user.as_ptr() as *const libc::c_void,
)
pam_set_item(self.pamh, PAM_USER, c_user.as_ptr() as *const libc::c_void)
})
}

/// Get the user that is currently active in the PAM handle
pub fn get_user(&mut self) -> PamResult<String> {
let mut data = std::ptr::null();
pam_err(unsafe { pam_get_item(self.pamh, PAM_USER as i32, &mut data) })?;
pam_err(unsafe { pam_get_item(self.pamh, PAM_USER, &mut data) })?;

// safety check to make sure that we do not ready a null ptr into a cstr
if data.is_null() {
Expand All @@ -223,25 +219,13 @@ impl<C: Converser> PamContext<C> {
/// Set the TTY path for the current TTY that this PAM session started from.
pub fn set_tty<P: AsRef<OsStr>>(&mut self, tty_path: P) -> PamResult<()> {
let data = CString::new(tty_path.as_ref().as_bytes())?;
pam_err(unsafe {
pam_set_item(
self.pamh,
PAM_TTY as i32,
data.as_ptr() as *const libc::c_void,
)
})
pam_err(unsafe { pam_set_item(self.pamh, PAM_TTY, data.as_ptr() as *const libc::c_void) })
}

// Set the user that requested the actions in this PAM instance.
pub fn set_requesting_user(&mut self, user: &str) -> PamResult<()> {
let data = CString::new(user.as_bytes())?;
pam_err(unsafe {
pam_set_item(
self.pamh,
PAM_RUSER as i32,
data.as_ptr() as *const libc::c_void,
)
})
pam_err(unsafe { pam_set_item(self.pamh, PAM_RUSER, data.as_ptr() as *const libc::c_void) })
}

/// Re-initialize the credentials stored in PAM
Expand All @@ -266,7 +250,7 @@ impl<C: Converser> PamContext<C> {
let mut flags = 0;
flags |= self.silent_flag();
if expired_only {
flags |= PAM_CHANGE_EXPIRED_AUTHTOK as i32;
flags |= PAM_CHANGE_EXPIRED_AUTHTOK;
}
pam_err(unsafe { pam_chauthtok(self.pamh, flags) })
}
Expand Down Expand Up @@ -362,7 +346,7 @@ impl<C: Converser> Drop for PamContext<C> {
unsafe {
pam_end(
self.pamh,
self.last_pam_status.unwrap_or(PAM_SUCCESS as libc::c_int) | PAM_DATA_SILENT as i32,
self.last_pam_status.unwrap_or(PAM_SUCCESS as libc::c_int) | PAM_DATA_SILENT,
)
};
}
Expand Down
Loading

0 comments on commit 23e8b0b

Please sign in to comment.