Mercurial > crates > nonstick
annotate src/module.rs @ 59:3f4a77aa88be
Fix string copyting and improve error situation.
This change is too big and includes several things:
- Fix copying strings from PAM by fixing const and mut on pam funcs.
- Improve error enums by simplifying conversions and removing
unnecessary and ambiguous "success" variants.
- Make a bunch of casts nicer.
- Assorted other cleanup.
author | Paul Fisher <paul@pfish.zone> |
---|---|
date | Wed, 21 May 2025 00:27:18 -0400 |
parents | daa2cde64601 |
children | 05cc2c27334f |
rev | line source |
---|---|
15
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
1 //! Functions for use in pam modules. |
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
2 |
59
3f4a77aa88be
Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents:
56
diff
changeset
|
3 use crate::constants::{ErrorCode, Flags, PamResult}; |
51 | 4 use crate::items::{Item, ItemType}; |
15
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
5 use libc::c_char; |
59
3f4a77aa88be
Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents:
56
diff
changeset
|
6 use secure_string::SecureString; |
56
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
51
diff
changeset
|
7 use std::ffi::{c_int, CStr, CString}; |
15
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
8 |
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
9 /// Opaque type, used as a pointer when making pam API calls. |
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
10 /// |
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
11 /// A module is invoked via an external function such as `pam_sm_authenticate`. |
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
12 /// Such a call provides a pam handle pointer. The same pointer should be given |
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
13 /// as an argument when making API calls. |
34 | 14 #[repr(C)] |
15 pub struct PamHandle { | |
16 _data: [u8; 0], | |
17 } | |
15
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
18 |
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
19 #[link(name = "pam")] |
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
20 extern "C" { |
34 | 21 fn pam_get_data( |
22 pamh: *const PamHandle, | |
23 module_data_name: *const c_char, | |
24 data: &mut *const libc::c_void, | |
56
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
51
diff
changeset
|
25 ) -> c_int; |
15
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
26 |
34 | 27 fn pam_set_data( |
28 pamh: *const PamHandle, | |
29 module_data_name: *const c_char, | |
59
3f4a77aa88be
Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents:
56
diff
changeset
|
30 data: *const libc::c_void, |
34 | 31 cleanup: extern "C" fn( |
32 pamh: *const PamHandle, | |
33 data: *mut libc::c_void, | |
56
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
51
diff
changeset
|
34 error_status: c_int, |
34 | 35 ), |
56
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
51
diff
changeset
|
36 ) -> c_int; |
15
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
37 |
34 | 38 fn pam_get_item( |
39 pamh: *const PamHandle, | |
56
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
51
diff
changeset
|
40 item_type: c_int, |
34 | 41 item: &mut *const libc::c_void, |
56
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
51
diff
changeset
|
42 ) -> c_int; |
15
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
43 |
56
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
51
diff
changeset
|
44 fn pam_set_item(pamh: *mut PamHandle, item_type: c_int, item: *const libc::c_void) -> c_int; |
15
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
45 |
59
3f4a77aa88be
Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents:
56
diff
changeset
|
46 fn pam_get_user( |
3f4a77aa88be
Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents:
56
diff
changeset
|
47 pamh: *const PamHandle, |
3f4a77aa88be
Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents:
56
diff
changeset
|
48 user: &mut *const c_char, |
3f4a77aa88be
Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents:
56
diff
changeset
|
49 prompt: *const c_char, |
3f4a77aa88be
Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents:
56
diff
changeset
|
50 ) -> c_int; |
44
50371046c61a
Add support for pam_get_authtok and minor cleanups.
Paul Fisher <paul@pfish.zone>
parents:
34
diff
changeset
|
51 |
50371046c61a
Add support for pam_get_authtok and minor cleanups.
Paul Fisher <paul@pfish.zone>
parents:
34
diff
changeset
|
52 fn pam_get_authtok( |
50371046c61a
Add support for pam_get_authtok and minor cleanups.
Paul Fisher <paul@pfish.zone>
parents:
34
diff
changeset
|
53 pamh: *const PamHandle, |
56
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
51
diff
changeset
|
54 item_type: c_int, |
59
3f4a77aa88be
Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents:
56
diff
changeset
|
55 data: &mut *const c_char, |
44
50371046c61a
Add support for pam_get_authtok and minor cleanups.
Paul Fisher <paul@pfish.zone>
parents:
34
diff
changeset
|
56 prompt: *const c_char, |
56
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
51
diff
changeset
|
57 ) -> c_int; |
44
50371046c61a
Add support for pam_get_authtok and minor cleanups.
Paul Fisher <paul@pfish.zone>
parents:
34
diff
changeset
|
58 |
15
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
59 } |
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
60 |
56
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
51
diff
changeset
|
61 /// Function called at the end of a PAM session that is called to clean up |
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
51
diff
changeset
|
62 /// a value previously provided to PAM in a `pam_set_data` call. |
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
51
diff
changeset
|
63 /// |
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
51
diff
changeset
|
64 /// You should never call this yourself. |
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
51
diff
changeset
|
65 extern "C" fn cleanup<T>(_: *const PamHandle, c_data: *mut libc::c_void, _: c_int) { |
19
d654aa0655e5
Making PamHandle a struct with methods
Anthony Nowell <anthony@algorithmia.com>
parents:
15
diff
changeset
|
66 unsafe { |
59
3f4a77aa88be
Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents:
56
diff
changeset
|
67 let _data: Box<T> = Box::from_raw(c_data.cast()); |
19
d654aa0655e5
Making PamHandle a struct with methods
Anthony Nowell <anthony@algorithmia.com>
parents:
15
diff
changeset
|
68 } |
d654aa0655e5
Making PamHandle a struct with methods
Anthony Nowell <anthony@algorithmia.com>
parents:
15
diff
changeset
|
69 } |
d654aa0655e5
Making PamHandle a struct with methods
Anthony Nowell <anthony@algorithmia.com>
parents:
15
diff
changeset
|
70 |
d654aa0655e5
Making PamHandle a struct with methods
Anthony Nowell <anthony@algorithmia.com>
parents:
15
diff
changeset
|
71 impl PamHandle { |
d654aa0655e5
Making PamHandle a struct with methods
Anthony Nowell <anthony@algorithmia.com>
parents:
15
diff
changeset
|
72 /// Gets some value, identified by `key`, that has been set by the module |
d654aa0655e5
Making PamHandle a struct with methods
Anthony Nowell <anthony@algorithmia.com>
parents:
15
diff
changeset
|
73 /// previously. |
d654aa0655e5
Making PamHandle a struct with methods
Anthony Nowell <anthony@algorithmia.com>
parents:
15
diff
changeset
|
74 /// |
44
50371046c61a
Add support for pam_get_authtok and minor cleanups.
Paul Fisher <paul@pfish.zone>
parents:
34
diff
changeset
|
75 /// See the [`pam_get_data` manual page]( |
50371046c61a
Add support for pam_get_authtok and minor cleanups.
Paul Fisher <paul@pfish.zone>
parents:
34
diff
changeset
|
76 /// https://www.man7.org/linux/man-pages/man3/pam_get_data.3.html). |
34 | 77 /// |
78 /// # Errors | |
79 /// | |
80 /// Returns an error if the underlying PAM function call fails. | |
81 /// | |
82 /// # Safety | |
83 /// | |
84 /// The data stored under the provided key must be of type `T` otherwise the | |
44
50371046c61a
Add support for pam_get_authtok and minor cleanups.
Paul Fisher <paul@pfish.zone>
parents:
34
diff
changeset
|
85 /// behaviour of this function is undefined. |
51 | 86 /// |
87 /// The data, if present, is owned by the current PAM conversation. | |
88 pub unsafe fn get_data<T>(&self, key: &str) -> PamResult<Option<&T>> { | |
56
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
51
diff
changeset
|
89 let c_key = CString::new(key).map_err(|_| ErrorCode::ConversationError)?; |
34 | 90 let mut ptr: *const libc::c_void = std::ptr::null(); |
56
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
51
diff
changeset
|
91 ErrorCode::result_from(pam_get_data(self, c_key.as_ptr(), &mut ptr))?; |
51 | 92 match ptr.is_null() { |
93 true => Ok(None), | |
94 false => { | |
59
3f4a77aa88be
Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents:
56
diff
changeset
|
95 let typed_ptr = ptr.cast(); |
51 | 96 Ok(Some(&*typed_ptr)) |
97 } | |
19
d654aa0655e5
Making PamHandle a struct with methods
Anthony Nowell <anthony@algorithmia.com>
parents:
15
diff
changeset
|
98 } |
15
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
99 } |
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
100 |
51 | 101 /// Stores a value that can be retrieved later with `get_data`. |
102 /// The conversation takes ownership of the data. | |
19
d654aa0655e5
Making PamHandle a struct with methods
Anthony Nowell <anthony@algorithmia.com>
parents:
15
diff
changeset
|
103 /// |
44
50371046c61a
Add support for pam_get_authtok and minor cleanups.
Paul Fisher <paul@pfish.zone>
parents:
34
diff
changeset
|
104 /// See the [`pam_set_data` manual page]( |
50371046c61a
Add support for pam_get_authtok and minor cleanups.
Paul Fisher <paul@pfish.zone>
parents:
34
diff
changeset
|
105 /// https://www.man7.org/linux/man-pages/man3/pam_set_data.3.html). |
34 | 106 /// |
107 /// # Errors | |
108 /// | |
109 /// Returns an error if the underlying PAM function call fails. | |
51 | 110 pub fn set_data<T>(&mut self, key: &str, data: Box<T>) -> PamResult<()> { |
56
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
51
diff
changeset
|
111 let c_key = CString::new(key).map_err(|_| ErrorCode::ConversationError)?; |
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
51
diff
changeset
|
112 let ret = unsafe { |
34 | 113 pam_set_data( |
114 self, | |
115 c_key.as_ptr(), | |
59
3f4a77aa88be
Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents:
56
diff
changeset
|
116 Box::into_raw(data).cast(), |
34 | 117 cleanup::<T>, |
118 ) | |
19
d654aa0655e5
Making PamHandle a struct with methods
Anthony Nowell <anthony@algorithmia.com>
parents:
15
diff
changeset
|
119 }; |
56
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
51
diff
changeset
|
120 ErrorCode::result_from(ret) |
15
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
121 } |
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
122 |
51 | 123 /// Retrieves a value that has been set, possibly by the pam client. |
124 /// This is particularly useful for getting a `PamConv` reference. | |
125 /// | |
126 /// These items are *references to PAM memory* | |
127 /// which are *owned by the conversation*. | |
19
d654aa0655e5
Making PamHandle a struct with methods
Anthony Nowell <anthony@algorithmia.com>
parents:
15
diff
changeset
|
128 /// |
44
50371046c61a
Add support for pam_get_authtok and minor cleanups.
Paul Fisher <paul@pfish.zone>
parents:
34
diff
changeset
|
129 /// See the [`pam_get_item` manual page]( |
50371046c61a
Add support for pam_get_authtok and minor cleanups.
Paul Fisher <paul@pfish.zone>
parents:
34
diff
changeset
|
130 /// https://www.man7.org/linux/man-pages/man3/pam_get_item.3.html). |
34 | 131 /// |
132 /// # Errors | |
133 /// | |
134 /// Returns an error if the underlying PAM function call fails. | |
135 pub fn get_item<T: crate::items::Item>(&self) -> PamResult<Option<T>> { | |
136 let mut ptr: *const libc::c_void = std::ptr::null(); | |
51 | 137 let out = unsafe { |
56
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
51
diff
changeset
|
138 let ret = pam_get_item(self, T::type_id().into(), &mut ptr); |
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
51
diff
changeset
|
139 ErrorCode::result_from(ret)?; |
59
3f4a77aa88be
Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents:
56
diff
changeset
|
140 let typed_ptr: *const T::Raw = ptr.cast(); |
51 | 141 match typed_ptr.is_null() { |
142 true => None, | |
143 false => Some(T::from_raw(typed_ptr)), | |
144 } | |
19
d654aa0655e5
Making PamHandle a struct with methods
Anthony Nowell <anthony@algorithmia.com>
parents:
15
diff
changeset
|
145 }; |
51 | 146 Ok(out) |
15
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
147 } |
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
148 |
51 | 149 /// Sets an item in the pam context. It can be retrieved using `get_item`. |
19
d654aa0655e5
Making PamHandle a struct with methods
Anthony Nowell <anthony@algorithmia.com>
parents:
15
diff
changeset
|
150 /// |
44
50371046c61a
Add support for pam_get_authtok and minor cleanups.
Paul Fisher <paul@pfish.zone>
parents:
34
diff
changeset
|
151 /// See the [`pam_set_item` manual page]( |
50371046c61a
Add support for pam_get_authtok and minor cleanups.
Paul Fisher <paul@pfish.zone>
parents:
34
diff
changeset
|
152 /// https://www.man7.org/linux/man-pages/man3/pam_set_item.3.html). |
34 | 153 /// |
154 /// # Errors | |
155 /// | |
156 /// Returns an error if the underlying PAM function call fails. | |
51 | 157 pub fn set_item<T: Item>(&mut self, item: T) -> PamResult<()> { |
59
3f4a77aa88be
Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents:
56
diff
changeset
|
158 let ret = unsafe { pam_set_item(self, T::type_id().into(), item.into_raw().cast()) }; |
56
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
51
diff
changeset
|
159 ErrorCode::result_from(ret) |
15
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
160 } |
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
161 |
19
d654aa0655e5
Making PamHandle a struct with methods
Anthony Nowell <anthony@algorithmia.com>
parents:
15
diff
changeset
|
162 /// Retrieves the name of the user who is authenticating or logging in. |
d654aa0655e5
Making PamHandle a struct with methods
Anthony Nowell <anthony@algorithmia.com>
parents:
15
diff
changeset
|
163 /// |
d654aa0655e5
Making PamHandle a struct with methods
Anthony Nowell <anthony@algorithmia.com>
parents:
15
diff
changeset
|
164 /// This is really a specialization of `get_item`. |
d654aa0655e5
Making PamHandle a struct with methods
Anthony Nowell <anthony@algorithmia.com>
parents:
15
diff
changeset
|
165 /// |
44
50371046c61a
Add support for pam_get_authtok and minor cleanups.
Paul Fisher <paul@pfish.zone>
parents:
34
diff
changeset
|
166 /// See the [`pam_get_user` manual page]( |
50371046c61a
Add support for pam_get_authtok and minor cleanups.
Paul Fisher <paul@pfish.zone>
parents:
34
diff
changeset
|
167 /// https://www.man7.org/linux/man-pages/man3/pam_get_user.3.html). |
34 | 168 /// |
169 /// # Errors | |
170 /// | |
171 /// Returns an error if the underlying PAM function call fails. | |
19
d654aa0655e5
Making PamHandle a struct with methods
Anthony Nowell <anthony@algorithmia.com>
parents:
15
diff
changeset
|
172 pub fn get_user(&self, prompt: Option<&str>) -> PamResult<String> { |
51 | 173 let prompt = option_cstr(prompt)?; |
59
3f4a77aa88be
Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents:
56
diff
changeset
|
174 let mut output: *const c_char = std::ptr::null_mut(); |
3f4a77aa88be
Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents:
56
diff
changeset
|
175 let ret = unsafe { pam_get_user(self, &mut output, prompt_ptr(prompt.as_ref())) }; |
56
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
51
diff
changeset
|
176 ErrorCode::result_from(ret)?; |
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
51
diff
changeset
|
177 copy_pam_string(output) |
15
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
178 } |
44
50371046c61a
Add support for pam_get_authtok and minor cleanups.
Paul Fisher <paul@pfish.zone>
parents:
34
diff
changeset
|
179 |
50371046c61a
Add support for pam_get_authtok and minor cleanups.
Paul Fisher <paul@pfish.zone>
parents:
34
diff
changeset
|
180 /// Retrieves the authentication token from the user. |
50371046c61a
Add support for pam_get_authtok and minor cleanups.
Paul Fisher <paul@pfish.zone>
parents:
34
diff
changeset
|
181 /// |
50371046c61a
Add support for pam_get_authtok and minor cleanups.
Paul Fisher <paul@pfish.zone>
parents:
34
diff
changeset
|
182 /// This is really a specialization of `get_item`. |
50371046c61a
Add support for pam_get_authtok and minor cleanups.
Paul Fisher <paul@pfish.zone>
parents:
34
diff
changeset
|
183 /// |
50371046c61a
Add support for pam_get_authtok and minor cleanups.
Paul Fisher <paul@pfish.zone>
parents:
34
diff
changeset
|
184 /// See the [`pam_get_authtok` manual page]( |
50371046c61a
Add support for pam_get_authtok and minor cleanups.
Paul Fisher <paul@pfish.zone>
parents:
34
diff
changeset
|
185 /// https://www.man7.org/linux/man-pages/man3/pam_get_authtok.3.html). |
50371046c61a
Add support for pam_get_authtok and minor cleanups.
Paul Fisher <paul@pfish.zone>
parents:
34
diff
changeset
|
186 /// |
50371046c61a
Add support for pam_get_authtok and minor cleanups.
Paul Fisher <paul@pfish.zone>
parents:
34
diff
changeset
|
187 /// # Errors |
50371046c61a
Add support for pam_get_authtok and minor cleanups.
Paul Fisher <paul@pfish.zone>
parents:
34
diff
changeset
|
188 /// |
50371046c61a
Add support for pam_get_authtok and minor cleanups.
Paul Fisher <paul@pfish.zone>
parents:
34
diff
changeset
|
189 /// Returns an error if the underlying PAM function call fails. |
56
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
51
diff
changeset
|
190 pub fn get_authtok(&self, prompt: Option<&str>) -> PamResult<SecureString> { |
51 | 191 let prompt = option_cstr(prompt)?; |
59
3f4a77aa88be
Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents:
56
diff
changeset
|
192 let mut output: *const c_char = std::ptr::null_mut(); |
51 | 193 let res = unsafe { |
194 pam_get_authtok( | |
195 self, | |
56
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
51
diff
changeset
|
196 ItemType::AuthTok.into(), |
59
3f4a77aa88be
Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents:
56
diff
changeset
|
197 &mut output, |
51 | 198 prompt_ptr(prompt.as_ref()), |
199 ) | |
44
50371046c61a
Add support for pam_get_authtok and minor cleanups.
Paul Fisher <paul@pfish.zone>
parents:
34
diff
changeset
|
200 }; |
56
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
51
diff
changeset
|
201 ErrorCode::result_from(res)?; |
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
51
diff
changeset
|
202 copy_pam_string(output).map(SecureString::from) |
51 | 203 } |
204 } | |
205 | |
206 /// Safely converts a `&str` option to a `CString` option. | |
207 fn option_cstr(prompt: Option<&str>) -> PamResult<Option<CString>> { | |
208 prompt | |
209 .map(CString::new) | |
210 .transpose() | |
56
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
51
diff
changeset
|
211 .map_err(|_| ErrorCode::ConversationError) |
51 | 212 } |
213 | |
214 /// The pointer to the prompt CString, or null if absent. | |
56
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
51
diff
changeset
|
215 pub(crate) fn prompt_ptr(prompt: Option<&CString>) -> *const c_char { |
51 | 216 match prompt { |
217 Some(c_str) => c_str.as_ptr(), | |
218 None => std::ptr::null(), | |
44
50371046c61a
Add support for pam_get_authtok and minor cleanups.
Paul Fisher <paul@pfish.zone>
parents:
34
diff
changeset
|
219 } |
50371046c61a
Add support for pam_get_authtok and minor cleanups.
Paul Fisher <paul@pfish.zone>
parents:
34
diff
changeset
|
220 } |
50371046c61a
Add support for pam_get_authtok and minor cleanups.
Paul Fisher <paul@pfish.zone>
parents:
34
diff
changeset
|
221 |
50371046c61a
Add support for pam_get_authtok and minor cleanups.
Paul Fisher <paul@pfish.zone>
parents:
34
diff
changeset
|
222 /// Creates an owned copy of a string that is returned from a |
50371046c61a
Add support for pam_get_authtok and minor cleanups.
Paul Fisher <paul@pfish.zone>
parents:
34
diff
changeset
|
223 /// <code>pam_get_<var>whatever</var></code> function. |
56
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
51
diff
changeset
|
224 pub(crate) fn copy_pam_string(result_ptr: *const c_char) -> PamResult<String> { |
44
50371046c61a
Add support for pam_get_authtok and minor cleanups.
Paul Fisher <paul@pfish.zone>
parents:
34
diff
changeset
|
225 // We really shouldn't get a null pointer back here, but if we do, return nothing. |
50371046c61a
Add support for pam_get_authtok and minor cleanups.
Paul Fisher <paul@pfish.zone>
parents:
34
diff
changeset
|
226 if result_ptr.is_null() { |
51 | 227 return Ok(String::new()); |
44
50371046c61a
Add support for pam_get_authtok and minor cleanups.
Paul Fisher <paul@pfish.zone>
parents:
34
diff
changeset
|
228 } |
51 | 229 let bytes = unsafe { CStr::from_ptr(result_ptr) }; |
56
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
51
diff
changeset
|
230 bytes |
51 | 231 .to_str() |
56
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
51
diff
changeset
|
232 .map(String::from) |
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
51
diff
changeset
|
233 .map_err(|_| ErrorCode::ConversationError) |
22
4263c1d83d5b
Refactor PamHooks into modules mod
Anthony Nowell <anthony@algorithmia.com>
parents:
19
diff
changeset
|
234 } |
4263c1d83d5b
Refactor PamHooks into modules mod
Anthony Nowell <anthony@algorithmia.com>
parents:
19
diff
changeset
|
235 |
4263c1d83d5b
Refactor PamHooks into modules mod
Anthony Nowell <anthony@algorithmia.com>
parents:
19
diff
changeset
|
236 /// Provides functions that are invoked by the entrypoints generated by the |
4263c1d83d5b
Refactor PamHooks into modules mod
Anthony Nowell <anthony@algorithmia.com>
parents:
19
diff
changeset
|
237 /// [`pam_hooks!` macro](../macro.pam_hooks.html). |
4263c1d83d5b
Refactor PamHooks into modules mod
Anthony Nowell <anthony@algorithmia.com>
parents:
19
diff
changeset
|
238 /// |
44
50371046c61a
Add support for pam_get_authtok and minor cleanups.
Paul Fisher <paul@pfish.zone>
parents:
34
diff
changeset
|
239 /// All hooks are ignored by PAM dispatch by default given the default return value of `PAM_IGNORE`. |
45
ce47901aab7a
Rename to “nonstick”, move to root, update docs and license.
Paul Fisher <paul@pfish.zone>
parents:
44
diff
changeset
|
240 /// Override any functions that you want to handle with your module. See [PAM’s root manual page]( |
ce47901aab7a
Rename to “nonstick”, move to root, update docs and license.
Paul Fisher <paul@pfish.zone>
parents:
44
diff
changeset
|
241 /// https://www.man7.org/linux/man-pages/man3/pam.3.html). |
22
4263c1d83d5b
Refactor PamHooks into modules mod
Anthony Nowell <anthony@algorithmia.com>
parents:
19
diff
changeset
|
242 #[allow(unused_variables)] |
4263c1d83d5b
Refactor PamHooks into modules mod
Anthony Nowell <anthony@algorithmia.com>
parents:
19
diff
changeset
|
243 pub trait PamHooks { |
4263c1d83d5b
Refactor PamHooks into modules mod
Anthony Nowell <anthony@algorithmia.com>
parents:
19
diff
changeset
|
244 /// This function performs the task of establishing whether the user is permitted to gain access at |
4263c1d83d5b
Refactor PamHooks into modules mod
Anthony Nowell <anthony@algorithmia.com>
parents:
19
diff
changeset
|
245 /// this time. It should be understood that the user has previously been validated by an |
4263c1d83d5b
Refactor PamHooks into modules mod
Anthony Nowell <anthony@algorithmia.com>
parents:
19
diff
changeset
|
246 /// authentication module. This function checks for other things. Such things might be: the time of |
4263c1d83d5b
Refactor PamHooks into modules mod
Anthony Nowell <anthony@algorithmia.com>
parents:
19
diff
changeset
|
247 /// day or the date, the terminal line, remote hostname, etc. This function may also determine |
4263c1d83d5b
Refactor PamHooks into modules mod
Anthony Nowell <anthony@algorithmia.com>
parents:
19
diff
changeset
|
248 /// things like the expiration on passwords, and respond that the user change it before continuing. |
56
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
51
diff
changeset
|
249 fn acct_mgmt(handle: &mut PamHandle, args: Vec<&CStr>, flags: Flags) -> PamResult<()> { |
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
51
diff
changeset
|
250 Err(ErrorCode::Ignore) |
34 | 251 } |
22
4263c1d83d5b
Refactor PamHooks into modules mod
Anthony Nowell <anthony@algorithmia.com>
parents:
19
diff
changeset
|
252 |
4263c1d83d5b
Refactor PamHooks into modules mod
Anthony Nowell <anthony@algorithmia.com>
parents:
19
diff
changeset
|
253 /// This function performs the task of authenticating the user. |
56
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
51
diff
changeset
|
254 fn sm_authenticate(handle: &mut PamHandle, args: Vec<&CStr>, flags: Flags) -> PamResult<()> { |
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
51
diff
changeset
|
255 Err(ErrorCode::Ignore) |
34 | 256 } |
22
4263c1d83d5b
Refactor PamHooks into modules mod
Anthony Nowell <anthony@algorithmia.com>
parents:
19
diff
changeset
|
257 |
34 | 258 /// This function is used to (re-)set the authentication token of the user. |
259 /// | |
260 /// The PAM library calls this function twice in succession. The first time with | |
261 /// `PAM_PRELIM_CHECK` and then, if the module does not return `PAM_TRY_AGAIN`, subsequently with | |
262 /// `PAM_UPDATE_AUTHTOK`. It is only on the second call that the authorization token is | |
263 /// (possibly) changed. | |
56
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
51
diff
changeset
|
264 fn sm_chauthtok(handle: &mut PamHandle, args: Vec<&CStr>, flags: Flags) -> PamResult<()> { |
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
51
diff
changeset
|
265 Err(ErrorCode::Ignore) |
34 | 266 } |
22
4263c1d83d5b
Refactor PamHooks into modules mod
Anthony Nowell <anthony@algorithmia.com>
parents:
19
diff
changeset
|
267 |
34 | 268 /// This function is called to terminate a session. |
56
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
51
diff
changeset
|
269 fn sm_close_session(handle: &mut PamHandle, args: Vec<&CStr>, flags: Flags) -> PamResult<()> { |
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
51
diff
changeset
|
270 Err(ErrorCode::Ignore) |
34 | 271 } |
22
4263c1d83d5b
Refactor PamHooks into modules mod
Anthony Nowell <anthony@algorithmia.com>
parents:
19
diff
changeset
|
272 |
34 | 273 /// This function is called to commence a session. |
56
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
51
diff
changeset
|
274 fn sm_open_session(handle: &mut PamHandle, args: Vec<&CStr>, flags: Flags) -> PamResult<()> { |
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
51
diff
changeset
|
275 Err(ErrorCode::Ignore) |
34 | 276 } |
22
4263c1d83d5b
Refactor PamHooks into modules mod
Anthony Nowell <anthony@algorithmia.com>
parents:
19
diff
changeset
|
277 |
4263c1d83d5b
Refactor PamHooks into modules mod
Anthony Nowell <anthony@algorithmia.com>
parents:
19
diff
changeset
|
278 /// This function performs the task of altering the credentials of the user with respect to the |
4263c1d83d5b
Refactor PamHooks into modules mod
Anthony Nowell <anthony@algorithmia.com>
parents:
19
diff
changeset
|
279 /// corresponding authorization scheme. Generally, an authentication module may have access to more |
4263c1d83d5b
Refactor PamHooks into modules mod
Anthony Nowell <anthony@algorithmia.com>
parents:
19
diff
changeset
|
280 /// information about a user than their authentication token. This function is used to make such |
4263c1d83d5b
Refactor PamHooks into modules mod
Anthony Nowell <anthony@algorithmia.com>
parents:
19
diff
changeset
|
281 /// information available to the application. It should only be called after the user has been |
4263c1d83d5b
Refactor PamHooks into modules mod
Anthony Nowell <anthony@algorithmia.com>
parents:
19
diff
changeset
|
282 /// authenticated but before a session has been established. |
56
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
51
diff
changeset
|
283 fn sm_setcred(handle: &mut PamHandle, args: Vec<&CStr>, flags: Flags) -> PamResult<()> { |
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
51
diff
changeset
|
284 Err(ErrorCode::Ignore) |
34 | 285 } |
286 } |