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
use std::ffi::{CStr, CString};
use std::ptr;
use libc::{c_char, c_int};
use crate::pam::constants::{PamResultCode, *};
use crate::pam::module::{PamItem, PamResult};
#[allow(missing_copy_implementations)]
pub enum AppDataPtr {}
#[repr(C)]
struct PamMessage {
msg_style: PamMessageStyle,
msg: *const c_char,
}
#[repr(C)]
struct PamResponse {
resp: *const c_char,
resp_retcode: AlwaysZero,
}
#[repr(C)]
pub struct PamConv {
conv: extern "C" fn(
num_msg: c_int,
pam_message: &&PamMessage,
pam_response: &mut *const PamResponse,
appdata_ptr: *const AppDataPtr,
) -> PamResultCode,
appdata_ptr: *const AppDataPtr,
}
impl PamConv {
pub fn send(&self, style: PamMessageStyle, msg: &str) -> PamResult<Option<String>> {
let mut resp_ptr: *const PamResponse = ptr::null();
let msg_cstr = CString::new(msg).unwrap();
let msg = PamMessage {
msg_style: style,
msg: msg_cstr.as_ptr(),
};
let ret = (self.conv)(1, &&msg, &mut resp_ptr, self.appdata_ptr);
if PamResultCode::PAM_SUCCESS == ret {
let response = unsafe { (*resp_ptr).resp };
if response.is_null() {
Ok(None)
} else {
let bytes = unsafe { CStr::from_ptr(response).to_bytes() };
Ok(String::from_utf8(bytes.to_vec()).ok())
}
} else {
Err(ret)
}
}
}
impl PamItem for PamConv {
fn item_type() -> PamItemType {
PAM_CONV
}
}