Mercurial > crates > nonstick
annotate src/conv.rs @ 60:05cc2c27334f
The Big Refactor: clean up docs and exports.
- Brings the most important symbols in the library to the root
  with `pub use` statements.
- Expands and updates documentation.
- Rearranges things extensively to make the external interface nicer
  and make the structure easier to understand.
- Renames a few things (e.g. `Result`).
| author | Paul Fisher <paul@pfish.zone> | 
|---|---|
| date | Wed, 21 May 2025 19:00:51 -0400 | 
| parents | 3f4a77aa88be | 
| children | d83623951070 | 
| rev | line source | 
|---|---|
| 60 
05cc2c27334f
The Big Refactor: clean up docs and exports.
 Paul Fisher <paul@pfish.zone> parents: 
59diff
changeset | 1 //! The [Conversation] struct, for interacting with the user. | 
| 
05cc2c27334f
The Big Refactor: clean up docs and exports.
 Paul Fisher <paul@pfish.zone> parents: 
59diff
changeset | 2 | 
| 15 
27730595f1ea
Adding pam-http module
 Anthony Nowell <anthony@algorithmia.com> parents: diff
changeset | 3 use libc::{c_char, c_int}; | 
| 27 | 4 use std::ffi::{CStr, CString}; | 
| 15 
27730595f1ea
Adding pam-http module
 Anthony Nowell <anthony@algorithmia.com> parents: diff
changeset | 5 use std::ptr; | 
| 
27730595f1ea
Adding pam-http module
 Anthony Nowell <anthony@algorithmia.com> parents: diff
changeset | 6 | 
| 59 
3f4a77aa88be
Fix string copyting and improve error situation.
 Paul Fisher <paul@pfish.zone> parents: 
56diff
changeset | 7 use crate::constants::ErrorCode; | 
| 56 
daa2cde64601
Big big refactor. Probably should have been multiple changes.
 Paul Fisher <paul@pfish.zone> parents: 
51diff
changeset | 8 use crate::constants::MessageStyle; | 
| 60 
05cc2c27334f
The Big Refactor: clean up docs and exports.
 Paul Fisher <paul@pfish.zone> parents: 
59diff
changeset | 9 use crate::constants::Result; | 
| 48 | 10 use crate::items::Item; | 
| 15 
27730595f1ea
Adding pam-http module
 Anthony Nowell <anthony@algorithmia.com> parents: diff
changeset | 11 | 
| 
27730595f1ea
Adding pam-http module
 Anthony Nowell <anthony@algorithmia.com> parents: diff
changeset | 12 #[repr(C)] | 
| 56 
daa2cde64601
Big big refactor. Probably should have been multiple changes.
 Paul Fisher <paul@pfish.zone> parents: 
51diff
changeset | 13 struct Message { | 
| 
daa2cde64601
Big big refactor. Probably should have been multiple changes.
 Paul Fisher <paul@pfish.zone> parents: 
51diff
changeset | 14 msg_style: MessageStyle, | 
| 15 
27730595f1ea
Adding pam-http module
 Anthony Nowell <anthony@algorithmia.com> parents: diff
changeset | 15 msg: *const c_char, | 
| 
27730595f1ea
Adding pam-http module
 Anthony Nowell <anthony@algorithmia.com> parents: diff
changeset | 16 } | 
| 
27730595f1ea
Adding pam-http module
 Anthony Nowell <anthony@algorithmia.com> parents: diff
changeset | 17 | 
| 
27730595f1ea
Adding pam-http module
 Anthony Nowell <anthony@algorithmia.com> parents: diff
changeset | 18 #[repr(C)] | 
| 56 
daa2cde64601
Big big refactor. Probably should have been multiple changes.
 Paul Fisher <paul@pfish.zone> parents: 
51diff
changeset | 19 struct Response { | 
| 15 
27730595f1ea
Adding pam-http module
 Anthony Nowell <anthony@algorithmia.com> parents: diff
changeset | 20 resp: *const c_char, | 
| 34 | 21 resp_retcode: libc::c_int, // Unused - always zero | 
| 15 
27730595f1ea
Adding pam-http module
 Anthony Nowell <anthony@algorithmia.com> parents: diff
changeset | 22 } | 
| 
27730595f1ea
Adding pam-http module
 Anthony Nowell <anthony@algorithmia.com> parents: diff
changeset | 23 | 
| 60 
05cc2c27334f
The Big Refactor: clean up docs and exports.
 Paul Fisher <paul@pfish.zone> parents: 
59diff
changeset | 24 #[doc(hidden)] | 
| 15 
27730595f1ea
Adding pam-http module
 Anthony Nowell <anthony@algorithmia.com> parents: diff
changeset | 25 #[repr(C)] | 
| 34 | 26 pub struct Inner { | 
| 27 | 27 conv: extern "C" fn( | 
| 28 num_msg: c_int, | |
| 56 
daa2cde64601
Big big refactor. Probably should have been multiple changes.
 Paul Fisher <paul@pfish.zone> parents: 
51diff
changeset | 29 pam_message: &&Message, | 
| 
daa2cde64601
Big big refactor. Probably should have been multiple changes.
 Paul Fisher <paul@pfish.zone> parents: 
51diff
changeset | 30 pam_response: &mut *const Response, | 
| 34 | 31 appdata_ptr: *const libc::c_void, | 
| 56 
daa2cde64601
Big big refactor. Probably should have been multiple changes.
 Paul Fisher <paul@pfish.zone> parents: 
51diff
changeset | 32 ) -> c_int, | 
| 34 | 33 appdata_ptr: *const libc::c_void, | 
| 15 
27730595f1ea
Adding pam-http module
 Anthony Nowell <anthony@algorithmia.com> parents: diff
changeset | 34 } | 
| 
27730595f1ea
Adding pam-http module
 Anthony Nowell <anthony@algorithmia.com> parents: diff
changeset | 35 | 
| 60 
05cc2c27334f
The Big Refactor: clean up docs and exports.
 Paul Fisher <paul@pfish.zone> parents: 
59diff
changeset | 36 /// A communication channel with the user. | 
| 51 | 37 /// | 
| 60 
05cc2c27334f
The Big Refactor: clean up docs and exports.
 Paul Fisher <paul@pfish.zone> parents: 
59diff
changeset | 38 /// Use this to communicate with the user, if needed, beyond the standard | 
| 
05cc2c27334f
The Big Refactor: clean up docs and exports.
 Paul Fisher <paul@pfish.zone> parents: 
59diff
changeset | 39 /// things you can get/set with `get_user`/`get_authtok` and friends. | 
| 
05cc2c27334f
The Big Refactor: clean up docs and exports.
 Paul Fisher <paul@pfish.zone> parents: 
59diff
changeset | 40 /// The PAM client (i.e., the application that is logging in) will present | 
| 
05cc2c27334f
The Big Refactor: clean up docs and exports.
 Paul Fisher <paul@pfish.zone> parents: 
59diff
changeset | 41 /// the messages you send to the user and ask for responses. | 
| 
05cc2c27334f
The Big Refactor: clean up docs and exports.
 Paul Fisher <paul@pfish.zone> parents: 
59diff
changeset | 42 pub struct Conversation<'a>(&'a Inner); | 
| 34 | 43 | 
| 60 
05cc2c27334f
The Big Refactor: clean up docs and exports.
 Paul Fisher <paul@pfish.zone> parents: 
59diff
changeset | 44 impl Conversation<'_> { | 
| 
05cc2c27334f
The Big Refactor: clean up docs and exports.
 Paul Fisher <paul@pfish.zone> parents: 
59diff
changeset | 45 /// Sends a message to the PAM client. | 
| 15 
27730595f1ea
Adding pam-http module
 Anthony Nowell <anthony@algorithmia.com> parents: diff
changeset | 46 /// | 
| 
27730595f1ea
Adding pam-http module
 Anthony Nowell <anthony@algorithmia.com> parents: diff
changeset | 47 /// This will typically result in the user seeing a message or a prompt. | 
| 60 
05cc2c27334f
The Big Refactor: clean up docs and exports.
 Paul Fisher <paul@pfish.zone> parents: 
59diff
changeset | 48 /// For details, see what [MessageStyle]s are available. | 
| 15 
27730595f1ea
Adding pam-http module
 Anthony Nowell <anthony@algorithmia.com> parents: diff
changeset | 49 /// | 
| 60 
05cc2c27334f
The Big Refactor: clean up docs and exports.
 Paul Fisher <paul@pfish.zone> parents: 
59diff
changeset | 50 /// Note that the user experience will depend on how each style | 
| 
05cc2c27334f
The Big Refactor: clean up docs and exports.
 Paul Fisher <paul@pfish.zone> parents: 
59diff
changeset | 51 /// is implemented by the client, and that not all clients | 
| 
05cc2c27334f
The Big Refactor: clean up docs and exports.
 Paul Fisher <paul@pfish.zone> parents: 
59diff
changeset | 52 /// will implement all message styles. | 
| 
05cc2c27334f
The Big Refactor: clean up docs and exports.
 Paul Fisher <paul@pfish.zone> parents: 
59diff
changeset | 53 pub fn send(&self, style: MessageStyle, msg: &str) -> Result<Option<&CStr>> { | 
| 56 
daa2cde64601
Big big refactor. Probably should have been multiple changes.
 Paul Fisher <paul@pfish.zone> parents: 
51diff
changeset | 54 let mut resp_ptr: *const Response = ptr::null(); | 
| 28 
81a9f0479e50
conv: fix bug where pam prompts were not being shown
 holycleugh <holycleugh> parents: 
27diff
changeset | 55 let msg_cstr = CString::new(msg).unwrap(); | 
| 56 
daa2cde64601
Big big refactor. Probably should have been multiple changes.
 Paul Fisher <paul@pfish.zone> parents: 
51diff
changeset | 56 let msg = Message { | 
| 15 
27730595f1ea
Adding pam-http module
 Anthony Nowell <anthony@algorithmia.com> parents: diff
changeset | 57 msg_style: style, | 
| 28 
81a9f0479e50
conv: fix bug where pam prompts were not being shown
 holycleugh <holycleugh> parents: 
27diff
changeset | 58 msg: msg_cstr.as_ptr(), | 
| 15 
27730595f1ea
Adding pam-http module
 Anthony Nowell <anthony@algorithmia.com> parents: diff
changeset | 59 }; | 
| 59 
3f4a77aa88be
Fix string copyting and improve error situation.
 Paul Fisher <paul@pfish.zone> parents: 
56diff
changeset | 60 // TODO: These need to be freed! | 
| 34 | 61 let ret = (self.0.conv)(1, &&msg, &mut resp_ptr, self.0.appdata_ptr); | 
| 56 
daa2cde64601
Big big refactor. Probably should have been multiple changes.
 Paul Fisher <paul@pfish.zone> parents: 
51diff
changeset | 62 ErrorCode::result_from(ret)?; | 
| 15 
27730595f1ea
Adding pam-http module
 Anthony Nowell <anthony@algorithmia.com> parents: diff
changeset | 63 | 
| 56 
daa2cde64601
Big big refactor. Probably should have been multiple changes.
 Paul Fisher <paul@pfish.zone> parents: 
51diff
changeset | 64 let result = unsafe { | 
| 
daa2cde64601
Big big refactor. Probably should have been multiple changes.
 Paul Fisher <paul@pfish.zone> parents: 
51diff
changeset | 65 match (*resp_ptr).resp { | 
| 
daa2cde64601
Big big refactor. Probably should have been multiple changes.
 Paul Fisher <paul@pfish.zone> parents: 
51diff
changeset | 66 p if p.is_null() => None, | 
| 
daa2cde64601
Big big refactor. Probably should have been multiple changes.
 Paul Fisher <paul@pfish.zone> parents: 
51diff
changeset | 67 p => Some(CStr::from_ptr(p)), | 
| 15 
27730595f1ea
Adding pam-http module
 Anthony Nowell <anthony@algorithmia.com> parents: diff
changeset | 68 } | 
| 56 
daa2cde64601
Big big refactor. Probably should have been multiple changes.
 Paul Fisher <paul@pfish.zone> parents: 
51diff
changeset | 69 }; | 
| 
daa2cde64601
Big big refactor. Probably should have been multiple changes.
 Paul Fisher <paul@pfish.zone> parents: 
51diff
changeset | 70 Ok(result) | 
| 15 
27730595f1ea
Adding pam-http module
 Anthony Nowell <anthony@algorithmia.com> parents: diff
changeset | 71 } | 
| 
27730595f1ea
Adding pam-http module
 Anthony Nowell <anthony@algorithmia.com> parents: diff
changeset | 72 } | 
| 
27730595f1ea
Adding pam-http module
 Anthony Nowell <anthony@algorithmia.com> parents: diff
changeset | 73 | 
| 60 
05cc2c27334f
The Big Refactor: clean up docs and exports.
 Paul Fisher <paul@pfish.zone> parents: 
59diff
changeset | 74 impl Item for Conversation<'_> { | 
| 34 | 75 type Raw = Inner; | 
| 76 | |
| 77 fn type_id() -> crate::items::ItemType { | |
| 56 
daa2cde64601
Big big refactor. Probably should have been multiple changes.
 Paul Fisher <paul@pfish.zone> parents: 
51diff
changeset | 78 crate::items::ItemType::Conversation | 
| 34 | 79 } | 
| 80 | |
| 81 unsafe fn from_raw(raw: *const Self::Raw) -> Self { | |
| 82 Self(&*raw) | |
| 83 } | |
| 84 | |
| 85 fn into_raw(self) -> *const Self::Raw { | |
| 86 self.0 as _ | |
| 15 
27730595f1ea
Adding pam-http module
 Anthony Nowell <anthony@algorithmia.com> parents: diff
changeset | 87 } | 
| 
27730595f1ea
Adding pam-http module
 Anthony Nowell <anthony@algorithmia.com> parents: diff
changeset | 88 } | 
