Mercurial > crates > nonstick
annotate src/conv.rs @ 14:51b097c12d3c
make PamResultCode an enum
| author | Anthony Nowell <anthony@algorithmia.com> |
|---|---|
| date | Sat, 23 Sep 2017 14:30:18 -0600 |
| parents | 30831c70e5c0 |
| children |
| rev | line source |
|---|---|
| 1 | 1 use libc::{c_char, c_int}; |
|
8
a83c56216e21
Ran everything through rustfmt.
Marc Brinkmann <git@marcbrinkmann.de>
parents:
7
diff
changeset
|
2 use std::ptr; |
| 1 | 3 use std::ffi::{CStr, CString}; |
| 4 | |
|
14
51b097c12d3c
make PamResultCode an enum
Anthony Nowell <anthony@algorithmia.com>
parents:
12
diff
changeset
|
5 use constants::PamResultCode; |
| 1 | 6 use constants::*; |
| 7 use module::{PamItem, PamResult}; | |
| 8 | |
| 9 #[allow(missing_copy_implementations)] | |
| 10 pub enum AppDataPtr {} | |
| 11 | |
| 12 #[repr(C)] | |
| 13 struct PamMessage { | |
| 14 msg_style: PamMessageStyle, | |
|
8
a83c56216e21
Ran everything through rustfmt.
Marc Brinkmann <git@marcbrinkmann.de>
parents:
7
diff
changeset
|
15 msg: *const c_char, |
| 1 | 16 } |
| 17 | |
| 18 #[repr(C)] | |
| 19 struct PamResponse { | |
| 20 resp: *const c_char, | |
| 21 resp_retcode: AlwaysZero, | |
| 22 } | |
| 23 | |
| 24 /// `PamConv` acts as a channel for communicating with user. | |
| 25 /// | |
| 26 /// Communication is mediated by the pam client (the application that invoked | |
| 27 /// pam). Messages sent will be relayed to the user by the client, and response | |
| 28 /// will be relayed back. | |
| 29 #[repr(C)] | |
| 30 pub struct PamConv { | |
|
8
a83c56216e21
Ran everything through rustfmt.
Marc Brinkmann <git@marcbrinkmann.de>
parents:
7
diff
changeset
|
31 conv: extern "C" fn(num_msg: c_int, |
|
a83c56216e21
Ran everything through rustfmt.
Marc Brinkmann <git@marcbrinkmann.de>
parents:
7
diff
changeset
|
32 pam_message: &&PamMessage, |
|
a83c56216e21
Ran everything through rustfmt.
Marc Brinkmann <git@marcbrinkmann.de>
parents:
7
diff
changeset
|
33 pam_response: &mut *const PamResponse, |
|
a83c56216e21
Ran everything through rustfmt.
Marc Brinkmann <git@marcbrinkmann.de>
parents:
7
diff
changeset
|
34 appdata_ptr: *const AppDataPtr) |
|
a83c56216e21
Ran everything through rustfmt.
Marc Brinkmann <git@marcbrinkmann.de>
parents:
7
diff
changeset
|
35 -> PamResultCode, |
| 1 | 36 appdata_ptr: *const AppDataPtr, |
| 37 } | |
| 38 | |
| 39 impl PamConv { | |
| 40 /// Sends a message to the pam client. | |
| 41 /// | |
| 42 /// This will typically result in the user seeing a message or a prompt. | |
| 43 /// There are several message styles available: | |
| 44 /// | |
| 45 /// - PAM_PROMPT_ECHO_OFF | |
| 46 /// - PAM_PROMPT_ECHO_ON | |
| 47 /// - PAM_ERROR_MSG | |
| 48 /// - PAM_TEXT_INFO | |
| 49 /// - PAM_RADIO_TYPE | |
| 50 /// - PAM_BINARY_PROMPT | |
| 51 /// | |
| 52 /// Note that the user experience will depend on how the client implements | |
| 53 /// these message styles - and not all applications implement all message | |
| 54 /// styles. | |
| 55 pub fn send(&self, style: PamMessageStyle, msg: &str) -> PamResult<Option<String>> { | |
|
6
2ec97116d72c
Updates for rustc 1.0.0-beta
Jesse Hallett <jesse@galois.com>
parents:
1
diff
changeset
|
56 let mut resp_ptr: *const PamResponse = ptr::null(); |
| 1 | 57 let msg = PamMessage { |
| 58 msg_style: style, | |
| 59 msg: CString::new(msg).unwrap().as_ptr(), | |
| 60 }; | |
| 61 | |
|
6
2ec97116d72c
Updates for rustc 1.0.0-beta
Jesse Hallett <jesse@galois.com>
parents:
1
diff
changeset
|
62 let ret = (self.conv)(1, &&msg, &mut resp_ptr, self.appdata_ptr); |
| 1 | 63 |
|
14
51b097c12d3c
make PamResultCode an enum
Anthony Nowell <anthony@algorithmia.com>
parents:
12
diff
changeset
|
64 if PamResultCode::PAM_SUCCESS == ret { |
|
6
2ec97116d72c
Updates for rustc 1.0.0-beta
Jesse Hallett <jesse@galois.com>
parents:
1
diff
changeset
|
65 if resp_ptr.is_null() { |
|
2ec97116d72c
Updates for rustc 1.0.0-beta
Jesse Hallett <jesse@galois.com>
parents:
1
diff
changeset
|
66 Ok(None) |
|
8
a83c56216e21
Ran everything through rustfmt.
Marc Brinkmann <git@marcbrinkmann.de>
parents:
7
diff
changeset
|
67 } else { |
|
6
2ec97116d72c
Updates for rustc 1.0.0-beta
Jesse Hallett <jesse@galois.com>
parents:
1
diff
changeset
|
68 let bytes = unsafe { CStr::from_ptr((*resp_ptr).resp).to_bytes() }; |
|
2ec97116d72c
Updates for rustc 1.0.0-beta
Jesse Hallett <jesse@galois.com>
parents:
1
diff
changeset
|
69 Ok(String::from_utf8(bytes.to_vec()).ok()) |
|
2ec97116d72c
Updates for rustc 1.0.0-beta
Jesse Hallett <jesse@galois.com>
parents:
1
diff
changeset
|
70 } |
| 1 | 71 } else { |
| 72 Err(ret) | |
| 73 } | |
| 74 } | |
| 75 } | |
| 76 | |
| 77 impl PamItem for PamConv { | |
|
12
30831c70e5c0
Remove PhantomData usage.
Marc Brinkmann <git@marcbrinkmann.de>
parents:
8
diff
changeset
|
78 fn item_type() -> PamItemType { |
|
8
a83c56216e21
Ran everything through rustfmt.
Marc Brinkmann <git@marcbrinkmann.de>
parents:
7
diff
changeset
|
79 PAM_CONV |
|
a83c56216e21
Ran everything through rustfmt.
Marc Brinkmann <git@marcbrinkmann.de>
parents:
7
diff
changeset
|
80 } |
| 1 | 81 } |
