Mercurial > crates > nonstick
annotate src/conv.rs @ 7:9380392b9a60
Changes type marker parameter type from Option<T> to PhantomData<T>
| author | Jesse Hallett <jesse@galois.com> |
|---|---|
| date | Fri, 03 Apr 2015 23:33:20 -0700 |
| parents | 2ec97116d72c |
| children | a83c56216e21 |
| rev | line source |
|---|---|
| 1 | 1 use libc::{c_char, c_int}; |
| 2 use std::{ptr}; | |
| 3 use std::ffi::{CStr, CString}; | |
|
7
9380392b9a60
Changes type marker parameter type from Option<T> to PhantomData<T>
Jesse Hallett <jesse@galois.com>
parents:
6
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, | |
| 16 msg: *const c_char, | |
| 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 { | |
| 32 conv: extern fn(num_msg: c_int, | |
| 33 pam_message: &&PamMessage, | |
|
6
2ec97116d72c
Updates for rustc 1.0.0-beta
Jesse Hallett <jesse@galois.com>
parents:
1
diff
changeset
|
34 pam_response: &mut *const PamResponse, |
| 1 | 35 appdata_ptr: *const AppDataPtr |
| 36 ) -> PamResultCode, | |
| 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) |
|
2ec97116d72c
Updates for rustc 1.0.0-beta
Jesse Hallett <jesse@galois.com>
parents:
1
diff
changeset
|
68 } |
|
2ec97116d72c
Updates for rustc 1.0.0-beta
Jesse Hallett <jesse@galois.com>
parents:
1
diff
changeset
|
69 else { |
|
2ec97116d72c
Updates for rustc 1.0.0-beta
Jesse Hallett <jesse@galois.com>
parents:
1
diff
changeset
|
70 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
|
71 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
|
72 } |
| 1 | 73 } else { |
| 74 Err(ret) | |
| 75 } | |
| 76 } | |
| 77 } | |
| 78 | |
| 79 impl PamItem for PamConv { | |
|
7
9380392b9a60
Changes type marker parameter type from Option<T> to PhantomData<T>
Jesse Hallett <jesse@galois.com>
parents:
6
diff
changeset
|
80 fn item_type(_: PhantomData<Self>) -> PamItemType { PAM_CONV } |
| 1 | 81 } |
