forked from fufesou/pam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.rs
197 lines (173 loc) · 6.96 KB
/
client.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
//! Authentication related structure and functions
use std::env;
use crate::{conv, enums::*, functions::*, types::*};
/// Main struct to authenticate a user
///
/// You need to create an instance of it to start an authentication process. If you
/// want a simple password-based authentication, you can use `Client::with_password`,
/// and to the following flow:
///
/// ```no_run
/// use pam::Client;
///
/// let mut client = Client::with_password("system-auth")
/// .expect("Failed to init PAM client.");
/// // Preset the login & password we will use for authentication
/// client.conversation_mut().set_credentials("login", "password");
/// // Actually try to authenticate:
/// client.authenticate().expect("Authentication failed!");
/// // Now that we are authenticated, it's possible to open a sesssion:
/// client.open_session().expect("Failed to open a session!");
/// ```
///
/// If you wish to customise the PAM conversation function, you should rather create your
/// client with `Client::with_handler`, providing a struct implementing the
/// `conv::Conversation` trait. You can then mutably access your conversation handler using the
/// `Client::handler_mut` method.
///
/// By default, the `Client` will close any opened session when dropped. If you don't
/// want this, you can change its `close_on_drop` field to `False`.
pub struct Client<'a, C: conv::Conversation> {
/// Flag indicating whether the Client should close the session on drop
pub close_on_drop: bool,
handle: &'a mut PamHandle,
conversation: Box<C>,
is_authenticated: bool,
has_open_session: bool,
last_code: PamReturnCode,
items_holder: Vec<Vec<u8>>,
}
impl<'a> Client<'a, conv::PasswordConv> {
/// Create a new `Client` with the given service name and a password-based conversation
pub fn with_password(service: &str) -> PamResult<Client<'a, conv::PasswordConv>> {
Client::with_conversation(service, conv::PasswordConv::new())
}
}
impl<'a, C: conv::Conversation> Client<'a, C> {
/// Create a new `Client` with the given service name and conversation handler
pub fn with_conversation(service: &str, conversation: C) -> PamResult<Client<'a, C>> {
let mut conversation = Box::new(conversation);
let conv = conv::into_pam_conv(&mut *conversation);
let handle = start(service, None, &conv)?;
Ok(Client {
close_on_drop: true,
handle,
conversation,
is_authenticated: false,
has_open_session: false,
last_code: PamReturnCode::Success,
items_holder: Vec::new(),
})
}
/// Immutable access to the conversation handler of this Client
pub fn conversation(&self) -> &C {
&*self.conversation
}
/// Mutable access to the conversation handler of this Client
pub fn conversation_mut(&mut self) -> &mut C {
&mut *self.conversation
}
/// Perform the authentication with the provided credentials
pub fn authenticate(&mut self) -> PamResult<()> {
self.last_code = authenticate(self.handle, PamFlag::None);
if self.last_code != PamReturnCode::Success {
// No need to reset here
return Err(From::from(self.last_code));
}
self.is_authenticated = true;
self.last_code = acct_mgmt(self.handle, PamFlag::None);
if self.last_code != PamReturnCode::Success {
// Probably not strictly neccessary but better be sure
return self.reset();
}
Ok(())
}
pub fn set_item(&mut self, item_type: PamItemType, item: &str) -> PamResult<()> {
let mut item = item.as_bytes().to_owned();
item.push(0);
set_item(self.handle, item_type, unsafe {
(item.as_ptr() as *const libc::c_void).as_ref().unwrap()
})?;
self.items_holder.push(item);
Ok(())
}
/// Open a session for a previously authenticated user and
/// initialize the environment appropriately (in PAM and regular enviroment variables).
pub fn open_session(&mut self) -> PamResult<()> {
if !self.is_authenticated {
//TODO: is this the right return code?
return Err(PamReturnCode::Perm_Denied.into());
}
self.last_code = setcred(self.handle, PamFlag::Establish_Cred);
if self.last_code != PamReturnCode::Success {
return self.reset();
}
self.last_code = open_session(self.handle, false);
if self.last_code != PamReturnCode::Success {
return self.reset();
}
// Follow openSSH and call pam_setcred before and after open_session
self.last_code = setcred(self.handle, PamFlag::Reinitialize_Cred);
if self.last_code != PamReturnCode::Success {
return self.reset();
}
self.has_open_session = true;
self.initialize_environment()
}
// Initialize the client environment with common variables.
// Currently always called from Client.open_session()
fn initialize_environment(&mut self) -> PamResult<()> {
use users::os::unix::UserExt;
let user = users::get_user_by_name(self.conversation.username()).unwrap_or_else(|| {
panic!(
"Could not get user by name: {:?}",
self.conversation.username()
)
});
// Set some common environment variables
self.set_env(
"USER",
user.name()
.to_str()
.expect("Unix usernames should be valid UTF-8"),
)?;
self.set_env(
"LOGNAME",
user.name()
.to_str()
.expect("Unix usernames should be valid UTF-8"),
)?;
self.set_env("HOME", user.home_dir().to_str().unwrap())?;
self.set_env("PWD", user.home_dir().to_str().unwrap())?;
self.set_env("SHELL", user.shell().to_str().unwrap())?;
// Note: We don't set PATH here, as this should be the job of `pam_env.so`
Ok(())
}
// Utility function to set an environment variable in PAM and the process
fn set_env(&mut self, key: &str, value: &str) -> PamResult<()> {
// Set regular environment variable
env::set_var(key, value);
// Set pam environment variable
if getenv(self.handle, key).is_ok() {
let name_value = format!("{}={}", key, value);
putenv(self.handle, &name_value)
} else {
Ok(())
}
}
// Utility function to reset the pam handle in case of intermediate errors
fn reset(&mut self) -> PamResult<()> {
setcred(self.handle, PamFlag::Delete_Cred);
self.is_authenticated = false;
Err(From::from(self.last_code))
}
}
impl<'a, C: conv::Conversation> Drop for Client<'a, C> {
fn drop(&mut self) {
if self.has_open_session && self.close_on_drop {
close_session(self.handle, false);
}
let code = setcred(self.handle, PamFlag::Delete_Cred);
end(self.handle, code);
}
}