Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix env module #30

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 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;

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

/// Main struct to authenticate a user
///
Expand Down Expand Up @@ -124,6 +124,15 @@ impl<'a, C: conv::Conversation> Client<'a, C> {
fn initialize_environment(&mut self) -> PamResult<()> {
use users::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 = users::get_user_by_name(self.conversation.username()).unwrap_or_else(|| {
panic!(
"Could not get user by name: {:?}",
Expand Down
11 changes: 7 additions & 4 deletions src/env.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
use libc::c_char;
use pam_sys::{getenvlist, raw, PamHandle};
use crate::types::*;
use pam_sys as raw;

use std::ffi::CStr;

pub struct PamEnvList {
ptr: *const *const c_char,
ptr: *mut *mut c_char,
}

pub fn get_pam_env(handle: &mut PamHandle) -> Option<PamEnvList> {
let env = getenvlist(handle);
let env = unsafe {
raw::pam_getenvlist(handle)
};
if !env.is_null() {
Some(PamEnvList { ptr: env })
} else {
Expand All @@ -22,7 +25,7 @@ impl PamEnvList {

let mut idx = 0;
loop {
let env_ptr: *const *const c_char = unsafe { self.ptr.offset(idx) };
let env_ptr: *mut *mut c_char = unsafe { self.ptr.offset(idx) };
if unsafe { !(*env_ptr).is_null() } {
idx += 1;

Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ mod conv;
mod enums;
mod functions;
mod types;
mod env;

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

Expand Down