Skip to content

Commit

Permalink
env: fix module
Browse files Browse the repository at this point in the history
Squashed commit of the following:

commit 7a3af02
Author: Fabrice Desclaux <fabrice.desclaux@cea.fr>
Date:   Mon May 9 13:54:32 2022 +0200

    Set pam env in local process

commit 5564ad8
Author: Fabrice Desclaux <fabrice.desclaux@cea.fr>
Date:   Mon May 9 13:54:12 2022 +0200

    Fix env module

1wilkens#30

Co-authored-by: Fabrice Desclaux <fabrice.desclaux@cea.fr>
  • Loading branch information
fx-chun and serpilliere committed Jun 26, 2024
1 parent 9ffe2e0 commit 906572a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/client.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Authentication related structure and functions
use std::{env, ffi::CStr, os::raw::c_char};

use crate::{conv, enums::*, functions::*, types::*};
use crate::{conv, enums::*, functions::*, types::*, env::*};

/// Main struct to authenticate a user
///
Expand Down Expand Up @@ -157,6 +157,14 @@ impl<'a, C: conv::Conversation> Client<'a, C> {
fn initialize_environment(&mut self) -> PamResult<()> {
use uzers::os::unix::UserExt;

// Set PAM environment in the local process
if let Some(mut env_list) = get_pam_env(self.handle) {
let env = env_list.to_vec();
for (key, value) in env {
env::set_var(&key, &value);
}
}

let user = uzers::get_user_by_name(&self.get_user()?).unwrap_or_else(|| {
panic!(
"Could not get user by name: {:?}",
Expand Down
13 changes: 13 additions & 0 deletions src/env.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use libc::c_char;
use memchr::memchr;
use crate::types::*;
use pam_sys as raw;

use std::vec::IntoIter;
use std::ffi::{CStr, OsString};
Expand All @@ -19,6 +21,17 @@ impl Iterator for PamEnvList {
}
}

pub fn get_pam_env(handle: &mut PamHandle) -> Option<PamEnvList> {
let env = unsafe {
raw::pam_getenvlist(handle)
};
if !env.is_null() {
Some(PamEnvList::from_ptr(env as *const *const c_char))
} else {
None
}
}

impl PamEnvList {
pub(crate) fn from_ptr(ptr: *const *const c_char) -> PamEnvList {
let mut result = Vec::new();
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ mod enums;
mod env;
mod functions;
mod types;
mod env;

pub use crate::{enums::*, functions::*, types::*};

Expand Down

0 comments on commit 906572a

Please sign in to comment.