Mercurial > crates > nonstick
annotate src/conv.rs @ 53:52f1102e0715 v0.0.3
Bump version to v0.0.3.
| author | Paul Fisher <paul@pfish.zone> |
|---|---|
| date | Sat, 03 May 2025 18:42:07 -0400 |
| parents | 9d1160b02d2c |
| children | daa2cde64601 |
| rev | line source |
|---|---|
|
15
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
1 use libc::{c_char, c_int}; |
| 27 | 2 use std::ffi::{CStr, CString}; |
|
15
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
3 use std::ptr; |
|
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
4 |
| 48 | 5 use crate::constants::PamMessageStyle; |
| 6 use crate::constants::PamResultCode; | |
| 7 use crate::items::Item; | |
| 8 use crate::module::PamResult; | |
|
15
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
9 |
|
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
10 #[repr(C)] |
|
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
11 struct PamMessage { |
|
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
12 msg_style: PamMessageStyle, |
|
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
13 msg: *const c_char, |
|
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
14 } |
|
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
15 |
|
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
16 #[repr(C)] |
|
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
17 struct PamResponse { |
|
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
18 resp: *const c_char, |
| 34 | 19 resp_retcode: libc::c_int, // Unused - always zero |
|
15
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
20 } |
|
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
21 |
|
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
22 #[repr(C)] |
| 34 | 23 pub struct Inner { |
| 27 | 24 conv: extern "C" fn( |
| 25 num_msg: c_int, | |
| 26 pam_message: &&PamMessage, | |
| 27 pam_response: &mut *const PamResponse, | |
| 34 | 28 appdata_ptr: *const libc::c_void, |
| 27 | 29 ) -> PamResultCode, |
| 34 | 30 appdata_ptr: *const libc::c_void, |
|
15
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
31 } |
|
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
32 |
| 51 | 33 /// A `Conv`ersation channel with the user. |
| 34 /// | |
| 35 /// Communication is mediated by the PAM client (the application that invoked | |
| 36 /// pam). Messages sent will be relayed to the user by the client, and response | |
| 37 /// will be relayed back. | |
| 34 | 38 pub struct Conv<'a>(&'a Inner); |
| 39 | |
|
44
50371046c61a
Add support for pam_get_authtok and minor cleanups.
Paul Fisher <paul@pfish.zone>
parents:
34
diff
changeset
|
40 impl Conv<'_> { |
|
15
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
41 /// Sends a message to the pam client. |
|
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
42 /// |
|
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
43 /// This will typically result in the user seeing a message or a prompt. |
|
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
44 /// There are several message styles available: |
|
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
45 /// |
|
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
46 /// - PAM_PROMPT_ECHO_OFF |
|
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
47 /// - PAM_PROMPT_ECHO_ON |
|
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
48 /// - PAM_ERROR_MSG |
|
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
49 /// - PAM_TEXT_INFO |
|
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
50 /// - PAM_RADIO_TYPE |
|
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
51 /// - PAM_BINARY_PROMPT |
|
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
52 /// |
|
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
53 /// Note that the user experience will depend on how the client implements |
|
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
54 /// these message styles - and not all applications implement all message |
|
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
55 /// styles. |
| 34 | 56 pub fn send(&self, style: PamMessageStyle, msg: &str) -> PamResult<Option<&CStr>> { |
|
15
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
57 let mut resp_ptr: *const PamResponse = ptr::null(); |
|
28
81a9f0479e50
conv: fix bug where pam prompts were not being shown
holycleugh <holycleugh>
parents:
27
diff
changeset
|
58 let msg_cstr = CString::new(msg).unwrap(); |
|
15
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
59 let msg = PamMessage { |
|
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
60 msg_style: style, |
|
28
81a9f0479e50
conv: fix bug where pam prompts were not being shown
holycleugh <holycleugh>
parents:
27
diff
changeset
|
61 msg: msg_cstr.as_ptr(), |
|
15
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
62 }; |
|
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
63 |
| 34 | 64 let ret = (self.0.conv)(1, &&msg, &mut resp_ptr, self.0.appdata_ptr); |
|
15
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
65 |
|
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
66 if PamResultCode::PAM_SUCCESS == ret { |
|
29
c16564971c05
conv: fix segfault when decoding response of PAM_TEXT_INFO et al.
holycleugh <holycleugh>
parents:
28
diff
changeset
|
67 // PamResponse.resp is null for styles that don't return user input like PAM_TEXT_INFO |
|
c16564971c05
conv: fix segfault when decoding response of PAM_TEXT_INFO et al.
holycleugh <holycleugh>
parents:
28
diff
changeset
|
68 let response = unsafe { (*resp_ptr).resp }; |
|
c16564971c05
conv: fix segfault when decoding response of PAM_TEXT_INFO et al.
holycleugh <holycleugh>
parents:
28
diff
changeset
|
69 if response.is_null() { |
|
15
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
70 Ok(None) |
|
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
71 } else { |
| 34 | 72 Ok(Some(unsafe { CStr::from_ptr(response) })) |
|
15
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
73 } |
|
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
74 } else { |
|
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
75 Err(ret) |
|
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
76 } |
|
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
77 } |
|
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
78 } |
|
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
79 |
|
44
50371046c61a
Add support for pam_get_authtok and minor cleanups.
Paul Fisher <paul@pfish.zone>
parents:
34
diff
changeset
|
80 impl Item for Conv<'_> { |
| 34 | 81 type Raw = Inner; |
| 82 | |
| 83 fn type_id() -> crate::items::ItemType { | |
| 84 crate::items::ItemType::Conv | |
| 85 } | |
| 86 | |
| 87 unsafe fn from_raw(raw: *const Self::Raw) -> Self { | |
| 88 Self(&*raw) | |
| 89 } | |
| 90 | |
| 91 fn into_raw(self) -> *const Self::Raw { | |
| 92 self.0 as _ | |
|
15
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
93 } |
|
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
94 } |
