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