comparison src/environ.rs @ 100:3f11b8d30f63

Implement environment variable management. This actually wires up the environment variable handling to libpam, so that applications and modules can manage the environment through the authentication process.
author Paul Fisher <paul@pfish.zone>
date Tue, 24 Jun 2025 17:08:01 -0400
parents b87100c5eed4
children
comparison
equal deleted inserted replaced
99:8840fa6534f6 100:3f11b8d30f63
1 //! Traits and stuff for managing the environment of a PAM handle. 1 //! Traits and stuff for managing the environment of a PAM handle.
2 //! 2 //!
3 //! PAM modules can set environment variables to apply to a user session. 3 //! PAM modules can set environment variables to apply to a user session.
4 //! This module manages the interface for doing all of that. 4 //! This module manages the interface for doing all of that.
5 5
6 use crate::constants::Result;
6 use std::ffi::{OsStr, OsString}; 7 use std::ffi::{OsStr, OsString};
7 8
8 /// A key/value map for environment variables, as [`OsString`]s. 9 /// A key/value map for environment variables, as [`OsString`]s.
9 /// 10 ///
10 /// This is a limited subset of what [`HashMap`](std::collections::HashMap) 11 /// This is a limited subset of what [`HashMap`](std::collections::HashMap)
11 /// can do. Notably, we do *not* support mutable iteration. 12 /// can do. Notably, we do *not* support mutable iteration.
12 pub trait EnvironMap { 13 pub trait EnvironMap<'a> {
13 /// Gets the environment variable of the given key. 14 /// Gets the environment variable of the given key.
14 fn get(&self, val: &OsStr) -> Option<&OsStr>; 15 fn get(&self, key: impl AsRef<OsStr>) -> Option<OsString>;
15 16
16 /// Iterates over the key/value pairs of the environment. 17 /// Iterates over the key/value pairs of the environment.
17 fn iter(&self) -> impl Iterator<Item = (&OsStr, &OsStr)>; 18 fn iter(&self) -> Result<impl Iterator<Item = (OsString, OsString)>>;
18 } 19 }
19 20
20 pub trait EnvironMapMut: EnvironMap { 21 pub trait EnvironMapMut<'a>: EnvironMap<'a> {
21 /// Sets the environment variable with the given key, 22 /// Sets the environment variable with the given key,
22 /// returning the old one if present. 23 /// returning the old one if present.
23 fn insert(&mut self, key: &OsStr, val: &OsStr) -> Option<OsString>; 24 fn insert(
25 &mut self,
26 key: impl AsRef<OsStr>,
27 val: impl AsRef<OsStr>,
28 ) -> Result<Option<OsString>>;
24 29
25 /// Removes the environment variable from the map, returning its old value 30 /// Removes the environment variable from the map, returning its old value
26 /// if present. 31 /// if present.
27 fn remove(&mut self, key: &OsStr) -> Option<OsString>; 32 fn remove(&mut self, key: impl AsRef<OsStr>) -> Result<Option<OsString>>;
28 } 33 }