-
Notifications
You must be signed in to change notification settings - Fork 10
/
lib.rs
312 lines (285 loc) · 10.7 KB
/
lib.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#[macro_use]
extern crate pam;
mod auth_keys;
mod error;
mod logger;
mod sign_verify;
mod ssh_agent_auth;
mod pam_items;
use log::*;
use pam::constants::{PamFlag, PamResultCode};
use pam::items::User;
use pam::module::{PamHandle, PamHooks};
use ssh_agent::proto::KeyTypeEnum;
use ssh_agent::proto::public_key::PublicKey;
use syslog::{BasicLogger, Facility, Formatter3164};
use std::ffi::CStr;
use std::str::FromStr;
use self::error::RsshErr;
use self::logger::ConsoleLogger;
type ErrType = Box<dyn std::error::Error>;
struct PamRssh;
pam_hooks!(PamRssh);
fn is_key_authorized(key: &PublicKey, authorized_keys: &Vec<PublicKey>) -> bool {
for item in authorized_keys {
if item == key {
return true;
}
}
false
}
fn get_username_from_pam(pamh: &PamHandle) -> Result<&str, ErrType> {
let user_item: User = pamh.get_item().map_err(|_| RsshErr::GetUserErr)?.ok_or(RsshErr::GetUserErr)?;
let user = user_item.to_str().map_err(|_| RsshErr::UsernameDecodeErr)?;
Ok(user)
}
fn read_authorized_keys(pamh: &PamHandle, auth_key_file: &str) -> Result<Vec<PublicKey>, ErrType> {
if auth_key_file.is_empty() {
let user = get_username_from_pam(pamh)?;
info!("Reading authorized_keys of user `{}`", user);
auth_keys::parse_user_authorized_keys(&user)
} else {
info!("Reading configured authorized_keys file: {}", auth_key_file);
auth_keys::parse_authorized_keys(auth_key_file)
}
}
fn retrieve_authorized_keys_from_cmd(pamh: &PamHandle, auth_key_cmd: &str, run_as_user: &str) -> Result<Vec<PublicKey>, ErrType> {
let auth_user = get_username_from_pam(pamh)?;
debug!("Run command `{}` as user `{}`", auth_key_cmd, run_as_user);
let content = auth_keys::run_authorized_keys_cmd(&auth_key_cmd,
auth_user,
if run_as_user.is_empty() { auth_user } else { run_as_user })?;
auth_keys::parse_content_of_authorized_keys(&content)
}
fn authenticate_via_agent(
agent: &mut ssh_agent_auth::AgentClient,
pubkey: &PublicKey,
) -> Result<(), ErrType> {
let challenge = sign_verify::gen_challenge()?;
let sig = agent.sign_data(&challenge, pubkey)?;
let verified = sign_verify::verify_signature(&challenge, pubkey, &sig)?;
if verified {
Ok(())
} else {
Err(RsshErr::SignVerifyErr.into_ptr())
}
}
fn setup_logger() {
let formatter = Formatter3164 {
facility: Facility::LOG_AUTH,
hostname: None,
process: "pam_rssh".into(),
pid: std::process::id() as u32,
};
syslog::unix(formatter)
.ok()
.and_then(|logger| log::set_boxed_logger(Box::new(BasicLogger::new(logger))).ok())
.or_else(|| log::set_boxed_logger(Box::new(ConsoleLogger)).ok())
.map(|()| log::set_max_level(log::LevelFilter::Warn));
}
fn substitute_variables(kv: &Vec<&str>, variables: &pam_items::PamItemsMap) -> Result<String, ErrType> {
subst::substitute(kv[1], variables)
.or(Err(RsshErr::OptVarErr(kv[0].to_string()).into_ptr()))
}
fn non_empty_option_check(kv: &Vec<&str>) -> Result<(), ErrType> {
if kv.len() == 1 || kv[1].is_empty() {
return Err(RsshErr::OptValEmptyErr(kv[0].to_string()).into_ptr())
}
Ok(())
}
impl PamHooks for PamRssh {
fn sm_authenticate(pamh: &mut PamHandle, args: Vec<&CStr>, _flags: PamFlag) -> PamResultCode {
/* if (flags & pam::constants::PAM_SILENT) == 0 */
{
setup_logger();
}
let pam_vars = pam_items::PamItemsMap::new(&pamh);
let mut ssh_agent_addr = String::new();
let mut auth_key_file = String::new();
let mut authorized_keys_command = String::new();
let mut authorized_keys_command_user = String::new();
for carg in args {
let kv: Vec<&str> = carg.to_str().unwrap_or("").splitn(2, '=').collect();
if kv.len() == 0 {
continue;
}
trace!("Parsing option {:?}", kv);
let mut parse_options = || -> Result<(), ErrType> {
match kv[0] {
"loglevel" => {
non_empty_option_check(&kv)?;
match log::Level::from_str(kv[1]) {
Ok(level) => log::set_max_level(level.to_level_filter()),
Err(_) => { return Err(RsshErr::InvalidLogLvlErr.into_ptr()) }
}
}
"debug" => log::set_max_level(log::LevelFilter::Debug),
"ssh_agent_addr" => {
non_empty_option_check(&kv)?;
ssh_agent_addr = substitute_variables(&kv, &pam_vars)?;
}
"auth_key_file" => {
non_empty_option_check(&kv)?;
auth_key_file = substitute_variables(&kv, &pam_vars)?;
}
"authorized_keys_command" => {
non_empty_option_check(&kv)?;
authorized_keys_command = substitute_variables(&kv, &pam_vars)?;
}
"authorized_keys_command_user" => {
non_empty_option_check(&kv)?;
authorized_keys_command_user = substitute_variables(&kv, &pam_vars)?;
}
_ => {
return Err(RsshErr::OptNameErr(kv[0].to_string()).into_ptr());
}
}
Ok(())
};
if let Err(opt_err) = parse_options() {
error!("{}", opt_err);
return PamResultCode::PAM_SYSTEM_ERR;
}
}
let addr_from_env;
if ssh_agent_addr.is_empty() {
let agent_addr_os = std::env::var_os("SSH_AUTH_SOCK");
if let Some(a) = agent_addr_os {
addr_from_env = a;
ssh_agent_addr = addr_from_env.to_str().unwrap_or("").to_string();
}
debug!("SSH-Agent address: {}", ssh_agent_addr);
if ssh_agent_addr.is_empty() {
error!("SSH agent socket address not configured");
return PamResultCode::PAM_AUTHINFO_UNAVAIL;
}
}
let authorized_keys_result = if authorized_keys_command.is_empty() {
read_authorized_keys(pamh, &auth_key_file)
} else {
retrieve_authorized_keys_from_cmd(pamh, &authorized_keys_command, &authorized_keys_command_user)
};
let authorized_keys = match authorized_keys_result {
Ok(u) => {
info!("Got {} entries from authorized_keys", u.len());
u
},
Err(e) => {
error!("read_authorized_keys: {}", e);
return PamResultCode::PAM_CRED_INSUFFICIENT;
}
};
let mut agent = ssh_agent_auth::AgentClient::new(ssh_agent_addr.as_str());
let result = agent.list_identities().and_then(|client_keys| {
debug!("SSH-Agent reports {} keys", client_keys.len());
for (i, key) in client_keys.iter().enumerate() {
if !is_key_authorized(&key, &authorized_keys) {
debug!("Key[{}] {} is not authorized", i, key.key_type());
continue;
}
debug!("Key[{}] is authorized", i);
match authenticate_via_agent(&mut agent, &key) {
Ok(_) => {
info!("Successful authentication");
return Ok(true);
}
Err(e) => {
warn!("Failed to authenticate key {}: {}", i, e);
continue; // try next key
}
}
}
warn!("None of these keys passed authentication");
Ok(false)
});
match result {
Ok(true) => PamResultCode::PAM_SUCCESS,
Ok(false) => PamResultCode::PAM_AUTH_ERR,
Err(e) => {
error!("{}", e);
PamResultCode::PAM_AUTH_ERR
}
}
}
// Always return PAM_SUCCESS for sm_setcred, just like pam-u2f
fn sm_setcred(_pamh: &mut PamHandle, _args: Vec<&CStr>, _flags: PamFlag) -> PamResultCode {
info!("set-credentials is not implemented");
PamResultCode::PAM_SUCCESS
}
}
#[cfg(test)]
mod tests {
use super::ssh_agent_auth::AgentClient;
use log::debug;
fn init_log() {
let _ = log::set_boxed_logger(Box::new(super::logger::ConsoleLogger))
.map(|()| log::set_max_level(log::LevelFilter::Info));
}
fn enable_debug_log() {
log::set_max_level(log::LevelFilter::Debug)
}
#[test]
fn sshagent_list_identities() {
init_log();
enable_debug_log();
let mut agent = AgentClient::new(env!("SSH_AUTH_SOCK"));
let result = agent.list_identities();
debug!("result={:?}", result);
assert!(result.is_ok());
let keys = result.unwrap();
assert!(keys.len() > 0);
for item in keys {
debug!("key: {:?}", item);
}
}
#[test]
fn sshagent_auth() {
init_log();
enable_debug_log();
let mut agent = AgentClient::new(env!("SSH_AUTH_SOCK"));
let result = agent.list_identities();
debug!("result={:?}", result);
assert!(result.is_ok());
let keys = result.unwrap();
assert!(keys.len() > 0);
for item in keys {
let data: &[u8] = &[3, 5, 6, 7];
let sig_ret = agent.sign_data(data, &item);
debug!("sig_ret={:?}", sig_ret);
assert!(sig_ret.is_ok());
let sig = sig_ret.unwrap();
let verify_ret = super::sign_verify::verify_signature(&data, &item, &sig);
debug!("verify_ret={:?}", verify_ret);
assert!(verify_ret.is_ok());
assert!(verify_ret.unwrap());
}
}
#[test]
fn sshagent_more_auth() {
init_log();
let mut agent = AgentClient::new(env!("SSH_AUTH_SOCK"));
let result = agent.list_identities();
assert!(result.is_ok());
let keys = result.unwrap();
assert!(keys.len() > 0);
for _ in 0..1000 {
for item in &keys {
let auth_ret = super::authenticate_via_agent(&mut agent, &item);
assert!(auth_ret.is_ok());
}
}
}
#[test]
fn parse_user_authorized_keys() {
init_log();
enable_debug_log();
let username = env!("USER");
let result = super::auth_keys::parse_user_authorized_keys(&username);
assert!(result.is_ok());
let keys = result.unwrap();
assert!(keys.len() > 0);
for item in keys {
debug!("key: {:?}", item);
}
}
}