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 d6a76e8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 13 deletions.
12 changes: 10 additions & 2 deletions 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 @@ -146,7 +146,7 @@ impl<'a, C: conv::Conversation> Client<'a, C> {
pub fn get_env_list(&mut self) -> Vec<(String, String)> {
let pam_env = getenvlist(self.handle);
let mut env = vec![];
for (name, value) in pam_env {
for (name, value) in pam_env.to_vec() {
env.push((name, value));
}
env
Expand All @@ -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
28 changes: 17 additions & 11 deletions src/env.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
use libc::c_char;
use memchr::memchr;
use crate::types::*;
use pam_sys as raw;

use std::vec::IntoIter;
use std::ffi::{CStr, OsString};

#[derive(Clone)]
pub struct PamEnvList {
inner: IntoIter<(OsString, OsString)>
inner: Vec<(OsString, OsString)>
}

impl Iterator for PamEnvList {
type Item = (String, String);

fn next(&mut self) -> Option<(String, String)> {
self.inner.next().map(|(a, b)| (a.into_string().unwrap(), b.into_string().unwrap()))
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.inner.size_hint()
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
}
}

Expand All @@ -36,7 +38,11 @@ impl PamEnvList {
}

drop_env_list(ptr);
return PamEnvList { inner: result.into_iter() };
return PamEnvList { inner: result };
}

pub fn to_vec(&self) -> Vec<(String, String)> {
self.inner.clone().into_iter().map(|(a,b)| (a.into_string().unwrap(), b.into_string().unwrap())).collect()
}
}

Expand Down

0 comments on commit d6a76e8

Please sign in to comment.