Mercurial > crates > nonstick
annotate src/module.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 //! Functions for use in pam modules. |
2 | |
3 use libc::{c_char}; | |
4 use std::{mem, ptr}; | |
5 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
|
6 use std::marker::{PhantomData}; |
1 | 7 |
8 use constants; | |
9 use constants::*; | |
10 | |
11 /// Opaque type, used as a pointer when making pam API calls. | |
12 /// | |
13 /// A module is invoked via an external function such as `pam_sm_authenticate`. | |
14 /// Such a call provides a pam handle pointer. The same pointer should be given | |
15 /// as an argument when making API calls. | |
16 #[allow(missing_copy_implementations)] | |
17 pub enum PamHandleT {} | |
18 | |
19 #[allow(missing_copy_implementations)] | |
20 enum PamItemT {} | |
21 | |
22 #[allow(missing_copy_implementations)] | |
23 pub enum PamDataT {} | |
24 | |
25 #[link(name = "pam")] | |
26 extern { | |
27 fn pam_get_data(pamh: *const PamHandleT, | |
28 module_data_name: *const c_char, | |
6
2ec97116d72c
Updates for rustc 1.0.0-beta
Jesse Hallett <jesse@galois.com>
parents:
1
diff
changeset
|
29 data: &mut *const PamDataT, |
1 | 30 ) -> PamResultCode; |
31 | |
32 fn pam_set_data(pamh: *const PamHandleT, | |
33 module_data_name: *const c_char, | |
34 data: Box<PamDataT>, | |
35 cleanup: extern fn (pamh: *const PamHandleT, | |
36 data: Box<PamDataT>, | |
37 error_status: PamResultCode | |
38 ), | |
39 ) -> PamResultCode; | |
40 | |
41 fn pam_get_item(pamh: *const PamHandleT, | |
42 item_type: PamItemType, | |
6
2ec97116d72c
Updates for rustc 1.0.0-beta
Jesse Hallett <jesse@galois.com>
parents:
1
diff
changeset
|
43 item: &mut *const PamItemT, |
1 | 44 ) -> PamResultCode; |
45 | |
46 fn pam_set_item(pamh: *mut PamHandleT, | |
47 item_type: PamItemType, | |
48 item: &PamItemT, | |
49 ) -> PamResultCode; | |
50 | |
51 fn pam_get_user(pamh: *const PamHandleT, | |
52 user: & *mut c_char, | |
53 prompt: *const c_char, | |
54 ) -> PamResultCode; | |
55 } | |
56 | |
57 pub type PamResult<T> = Result<T, PamResultCode>; | |
58 | |
59 /// Type-level mapping for safely retrieving values with `get_item`. | |
60 /// | |
61 /// See `pam_get_item` in | |
62 /// http://www.linux-pam.org/Linux-PAM-html/mwg-expected-by-module-item.html | |
63 pub trait PamItem { | |
64 /// Maps a Rust type to a pam constant. | |
65 /// | |
66 /// For example, the type PamConv maps to the constant PAM_CONV. The pam | |
67 /// API contract specifies that when the API function `pam_get_item` is | |
68 /// called with the constant PAM_CONV, it will return a value of type | |
69 /// `PamConv`. | |
7
9380392b9a60
Changes type marker parameter type from Option<T> to PhantomData<T>
Jesse Hallett <jesse@galois.com>
parents:
6
diff
changeset
|
70 fn item_type(_: PhantomData<Self>) -> PamItemType; |
1 | 71 } |
72 | |
73 /// Gets some value, identified by `key`, that has been set by the module | |
74 /// previously. | |
75 /// | |
76 /// See `pam_get_data` in | |
77 /// http://www.linux-pam.org/Linux-PAM-html/mwg-expected-by-module-item.html | |
78 pub unsafe fn get_data<'a, T>(pamh: &'a PamHandleT, key: &str) -> PamResult<&'a T> { | |
79 let c_key = CString::new(key).unwrap().as_ptr(); | |
6
2ec97116d72c
Updates for rustc 1.0.0-beta
Jesse Hallett <jesse@galois.com>
parents:
1
diff
changeset
|
80 let mut ptr: *const PamDataT = ptr::null(); |
1 | 81 let res = pam_get_data(pamh, c_key, &mut ptr); |
82 if constants::PAM_SUCCESS == res && !ptr.is_null() { | |
6
2ec97116d72c
Updates for rustc 1.0.0-beta
Jesse Hallett <jesse@galois.com>
parents:
1
diff
changeset
|
83 let typed_ptr: *const T = mem::transmute(ptr); |
2ec97116d72c
Updates for rustc 1.0.0-beta
Jesse Hallett <jesse@galois.com>
parents:
1
diff
changeset
|
84 let data: &T = &*typed_ptr; |
1 | 85 Ok(data) |
86 } | |
87 else { | |
88 Err(res) | |
89 } | |
90 } | |
91 | |
92 /// Stores a value that can be retrieved later with `get_data`. The value lives | |
93 /// as long as the current pam cycle. | |
94 /// | |
95 /// See `pam_set_data` in | |
96 /// http://www.linux-pam.org/Linux-PAM-html/mwg-expected-by-module-item.html | |
97 pub fn set_data<T>(pamh: &PamHandleT, key: &str, data: Box<T>) -> PamResult<()> { | |
98 let c_key = CString::new(key).unwrap().as_ptr(); | |
99 let res = unsafe { | |
100 let c_data: Box<PamDataT> = mem::transmute(data); | |
101 pam_set_data(pamh, c_key, c_data, cleanup::<T>) | |
102 }; | |
103 if constants::PAM_SUCCESS == res { Ok(()) } else { Err(res) } | |
104 } | |
105 | |
106 #[no_mangle] | |
107 pub extern fn cleanup<T>(_: *const PamHandleT, c_data: Box<PamDataT>, _: PamResultCode) { | |
108 unsafe { | |
109 let data: Box<T> = mem::transmute(c_data); | |
110 mem::drop(data); | |
111 } | |
112 } | |
113 | |
114 /// Retrieves a value that has been set, possibly by the pam client. This is | |
115 /// particularly useful for getting a `PamConv` reference. | |
116 /// | |
117 /// See `pam_get_item` in | |
118 /// http://www.linux-pam.org/Linux-PAM-html/mwg-expected-by-module-item.html | |
119 pub fn get_item<'a, T: PamItem>(pamh: &'a PamHandleT) -> PamResult<&'a T> { | |
6
2ec97116d72c
Updates for rustc 1.0.0-beta
Jesse Hallett <jesse@galois.com>
parents:
1
diff
changeset
|
120 let mut ptr: *const PamItemT = ptr::null(); |
1 | 121 let (res, item) = unsafe { |
7
9380392b9a60
Changes type marker parameter type from Option<T> to PhantomData<T>
Jesse Hallett <jesse@galois.com>
parents:
6
diff
changeset
|
122 let r = pam_get_item(pamh, PamItem::item_type(PhantomData::<T>), &mut ptr); |
6
2ec97116d72c
Updates for rustc 1.0.0-beta
Jesse Hallett <jesse@galois.com>
parents:
1
diff
changeset
|
123 let typed_ptr: *const T = mem::transmute(ptr); |
2ec97116d72c
Updates for rustc 1.0.0-beta
Jesse Hallett <jesse@galois.com>
parents:
1
diff
changeset
|
124 let t: &T = &*typed_ptr; |
1 | 125 (r, t) |
126 }; | |
127 if constants::PAM_SUCCESS == res { Ok(item) } else { Err(res) } | |
128 } | |
129 | |
130 /// Retrieves the name of the user who is authenticating or logging in. | |
131 /// | |
132 /// This is really a specialization of `get_item`. | |
133 /// | |
134 /// See `pam_get_user` in | |
135 /// http://www.linux-pam.org/Linux-PAM-html/mwg-expected-by-module-item.html | |
136 pub fn get_user<'a>(pamh: &'a PamHandleT, prompt: Option<&str>) -> PamResult<String> { | |
137 let ptr: *mut c_char = ptr::null_mut(); | |
138 let c_prompt = match prompt { | |
139 Some(p) => CString::new(p).unwrap().as_ptr(), | |
140 None => ptr::null(), | |
141 }; | |
142 let res = unsafe { pam_get_user(pamh, &ptr, c_prompt) }; | |
143 if constants::PAM_SUCCESS == res && !ptr.is_null() { | |
144 let const_ptr = ptr as *const c_char; | |
145 let bytes = unsafe { CStr::from_ptr(const_ptr).to_bytes() }; | |
146 String::from_utf8(bytes.to_vec()) | |
147 .map_err(|_| PAM_CONV_ERR) | |
148 } | |
149 else { | |
150 Err(res) | |
151 } | |
152 } |