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
use ffi;
use pam::{self, PamConversation, PamFlag, PamHandle, PamReturnCode};
use users;
pub struct Authenticator<'a, 'b> {
pub close_on_drop: bool,
handle: &'a mut PamHandle,
credentials: Box<[&'b str; 2]>,
is_authenticated: bool,
has_open_session: bool,
last_code: PamReturnCode,
}
impl<'a, 'b> Authenticator<'a, 'b> {
pub fn new(service: &str) -> Option<Authenticator> {
use std::ptr;
let creds = Box::new([""; 2]);
let conv = PamConversation {
conv: Some(ffi::converse),
data_ptr: creds.as_ptr() as *mut ::libc::c_void,
};
let mut handle: *mut PamHandle = ptr::null_mut();
match pam::start(service, None, &conv, &mut handle) {
PamReturnCode::SUCCESS => unsafe {
Some(Authenticator {
close_on_drop: true,
handle: &mut *handle,
credentials: creds,
is_authenticated: false,
has_open_session: false,
last_code: PamReturnCode::SUCCESS,
})
},
_ => None,
}
}
pub fn set_credentials(&mut self, user: &'b str, password: &'b str) {
self.credentials[0] = user;
self.credentials[1] = password;
}
pub fn authenticate(&mut self) -> ::Result<()> {
self.last_code = pam::authenticate(self.handle, PamFlag::NONE);
if self.last_code != PamReturnCode::SUCCESS {
return Err(From::from(self.last_code));
}
self.is_authenticated = true;
self.last_code = pam::acct_mgmt(self.handle, PamFlag::NONE);
if self.last_code != PamReturnCode::SUCCESS {
return self.reset();
}
Ok(())
}
pub fn open_session(&mut self) -> ::Result<()> {
if !self.is_authenticated {
return Err(From::from(PamReturnCode::PERM_DENIED));
}
self.last_code = pam::setcred(self.handle, PamFlag::ESTABLISH_CRED);
if self.last_code != PamReturnCode::SUCCESS {
return self.reset();
}
self.last_code = pam::open_session(self.handle, PamFlag::NONE);
if self.last_code != PamReturnCode::SUCCESS {
return self.reset();
}
self.last_code = pam::setcred(self.handle, PamFlag::REINITIALIZE_CRED);
if self.last_code != PamReturnCode::SUCCESS {
return self.reset();
}
self.has_open_session = true;
self.initialize_environment()
}
fn initialize_environment(&mut self) -> ::Result<()> {
use users::os::unix::UserExt;
use std::env;
if let Some(mut env_list) = ::env::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.credentials[0])
.expect(&format!("Could not get user by name: {:?}", self.credentials[0]));
try!(self.set_env("USER", user.name()));
try!(self.set_env("LOGNAME", user.name()));
try!(self.set_env("HOME", user.home_dir().to_str().unwrap()));
try!(self.set_env("PWD", user.home_dir().to_str().unwrap()));
try!(self.set_env("SHELL", user.shell().to_str().unwrap()));
try!(self.set_env("PATH", "$PATH:/usr/local/sbin:/usr/local/bin:/usr/bin"));
Ok(())
}
fn set_env(&mut self, key: &str, value: &str) -> ::Result<()> {
use std::env;
env::set_var(key, value);
if pam::getenv(self.handle, key).is_none() {
let name_value = format!("{}={}", key, value);
match pam::putenv(self.handle, &name_value) {
PamReturnCode::SUCCESS => Ok(()),
code => Err(From::from(code)),
}
} else {
Ok(())
}
}
fn reset(&mut self) -> ::Result<()> {
pam::setcred(self.handle, PamFlag::DELETE_CRED);
self.is_authenticated = false;
Err(From::from(self.last_code))
}
}
impl<'a, 'b> Drop for Authenticator<'a, 'b> {
fn drop(&mut self) {
if self.has_open_session && self.close_on_drop {
pam::close_session(self.handle, PamFlag::NONE);
}
let code = pam::setcred(self.handle, PamFlag::DELETE_CRED);
pam::end(self.handle, code);
}
}