summaryrefslogtreecommitdiff
path: root/src-repo/src/pam_any_conversation.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src-repo/src/pam_any_conversation.rs')
-rw-r--r--src-repo/src/pam_any_conversation.rs48
1 files changed, 48 insertions, 0 deletions
diff --git a/src-repo/src/pam_any_conversation.rs b/src-repo/src/pam_any_conversation.rs
new file mode 100644
index 0000000..825cb6a
--- /dev/null
+++ b/src-repo/src/pam_any_conversation.rs
@@ -0,0 +1,48 @@
+use std::ffi::{CStr, CString};
+use std::sync::{Arc, Mutex};
+use std::thread;
+
+use crate::pam_client::Conversation;
+
+use crate::raw_conv::RawConv;
+
+pub struct PamAnyConversation {
+ pub service_display_name: String,
+ pub user: String,
+ pub conv: Arc<Mutex<RawConv>>,
+}
+
+impl Conversation for PamAnyConversation {
+ fn prompt_echo(&mut self, _msg: &CStr) -> Result<CString, ()> {
+ CString::new(self.user.as_str()).map_err(|_| ())
+ }
+
+ fn prompt_blind(&mut self, msg: &CStr) -> Result<CString, ()> {
+ let msg = msg.to_str().map_err(|_| ())?;
+ let conv = self.conv.lock().map_err(|_| ())?;
+ let response = conv.send_prompt(&format!("[{}] {}", self.service_display_name, msg))?;
+ response.ok_or(())
+ }
+
+ fn info(&mut self, msg: &CStr) {
+ let msg = msg.to_str().unwrap_or("");
+ let msg = format!("[{}] {}", self.service_display_name, msg);
+ let conv = self.conv.clone();
+ thread::spawn(move || {
+ if let Ok(conv) = conv.lock() {
+ let _ = conv.send_info(&msg);
+ }
+ });
+ }
+
+ fn error(&mut self, msg: &CStr) {
+ let msg = msg.to_str().unwrap_or("");
+ let msg = format!("[{}] {}", self.service_display_name, msg);
+ let conv = self.conv.clone();
+ thread::spawn(move || {
+ if let Ok(conv) = conv.lock() {
+ let _ = conv.send_error(&msg);
+ }
+ });
+ }
+}