comparison src/libpam/environ.rs @ 103:dfcd96a74ac4 default tip

write a truly prodigious amount of documentation adds a bunch of links to the OpenPAM man pages and the XSSO spec as well as just a bunch of prose and stuff.
author Paul Fisher <paul@pfish.zone>
date Wed, 25 Jun 2025 00:59:24 -0400
parents 3f11b8d30f63
children
comparison
equal deleted inserted replaced
102:94eb11cb1798 103:dfcd96a74ac4
1 #![allow(unused_variables)] // for now
2 use crate::constants::{ErrorCode, Result}; 1 use crate::constants::{ErrorCode, Result};
3 use crate::environ::{EnvironMap, EnvironMapMut}; 2 use crate::environ::{EnvironMap, EnvironMapMut};
4 use crate::libpam::memory::CHeapString; 3 use crate::libpam::memory::CHeapString;
5 use crate::libpam::{memory, pam_ffi, LibPamHandle}; 4 use crate::libpam::{memory, pam_ffi, LibPamHandle};
6 use std::ffi::{c_char, CStr, CString, OsStr, OsString}; 5 use std::ffi::{c_char, CStr, CString, OsStr, OsString};
29 } 28 }
30 } 29 }
31 30
32 fn environ_set(&mut self, key: &OsStr, value: Option<&OsStr>) -> Result<Option<OsString>> { 31 fn environ_set(&mut self, key: &OsStr, value: Option<&OsStr>) -> Result<Option<OsString>> {
33 let old = self.environ_get(key); 32 let old = self.environ_get(key);
33 if old.is_none() && value.is_none() {
34 // pam_putenv returns an error if we try to remove a non-existent
35 // environment variable, so just avoid that entirely.
36 return Ok(None)
37 }
34 let total_len = key.len() + value.map(OsStr::len).unwrap_or_default() + 2; 38 let total_len = key.len() + value.map(OsStr::len).unwrap_or_default() + 2;
35 let mut result = Vec::with_capacity(total_len); 39 let mut result = Vec::with_capacity(total_len);
36 result.extend(key.as_bytes()); 40 result.extend(key.as_bytes());
37 if let Some(value) = value { 41 if let Some(value) = value {
38 result.push(b'='); 42 result.push(b'=');
43 ErrorCode::result_from(unsafe { pam_ffi::pam_putenv(self, put.as_ptr()) })?; 47 ErrorCode::result_from(unsafe { pam_ffi::pam_putenv(self, put.as_ptr()) })?;
44 Ok(old) 48 Ok(old)
45 } 49 }
46 50
47 fn environ_iter(&self) -> Result<impl Iterator<Item = (OsString, OsString)>> { 51 fn environ_iter(&self) -> Result<impl Iterator<Item = (OsString, OsString)>> {
48 // SAFETY: This is a valid PAM handle. 52 // SAFETY: This is a valid PAM handle. It will return valid data.
49 unsafe { 53 unsafe {
50 NonNull::new(pam_ffi::pam_getenvlist( 54 NonNull::new(pam_ffi::pam_getenvlist(
51 (self as *const LibPamHandle).cast_mut(), 55 (self as *const LibPamHandle).cast_mut(),
52 )) 56 ))
53 .map(|ptr| EnvList::from_ptr(ptr.cast())) 57 .map(|ptr| EnvList::from_ptr(ptr.cast()))