pam_client/
conv_null.rs

1//! Null conversation handler
2
3/***********************************************************************
4 * Author: Christoph Grenz <christophg+gitorious @ grenz-bonn.de>      *
5 *                                                                     *
6 * Due to the lack of originality the Null conversation handler        *
7 * implementation is given to the public domain. To the extent         *
8 * possible under law, the author has waived all copyright and related *
9 * or neighboring rights to this Source Code Form.                     *
10 * https://creativecommons.org/publicdomain/zero/1.0/legalcode         *
11 ***********************************************************************/
12
13#![forbid(unsafe_code)]
14
15use super::ConversationHandler;
16use crate::error::ErrorCode;
17use std::ffi::{CStr, CString};
18
19/// Null implementation of `ConversationHandler`
20///
21/// When a PAM module asks for any user interaction an error is returned.
22/// Error and info messages are ignored.
23///
24/// This handler may be used for testing and for environments where no user
25/// interaction is possible, no credentials can be stored beforehand and
26/// failing is the only answer if some PAM module needs input.
27#[derive(Debug, Clone)]
28#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
29pub struct Conversation {}
30
31impl Conversation {
32	/// Creates a new null conversation handler
33	#[must_use]
34	pub const fn new() -> Self {
35		Self {}
36	}
37}
38
39impl Default for Conversation {
40	fn default() -> Self {
41		Self::new()
42	}
43}
44
45impl ConversationHandler for Conversation {
46	fn prompt_echo_on(&mut self, _msg: &CStr) -> Result<CString, ErrorCode> {
47		Err(ErrorCode::CONV_ERR)
48	}
49
50	fn prompt_echo_off(&mut self, _msg: &CStr) -> Result<CString, ErrorCode> {
51		Err(ErrorCode::CONV_ERR)
52	}
53
54	fn text_info(&mut self, _msg: &CStr) {}
55
56	fn error_msg(&mut self, _msg: &CStr) {}
57}
58
59#[cfg(test)]
60mod tests {
61	use super::*;
62
63	#[test]
64	fn test() {
65		let text = CString::new("test").unwrap();
66		let mut c = Conversation::default();
67		assert_eq!(format!("{:?}", c), format!("{:?}", c.clone()));
68
69		assert!(c.prompt_echo_on(&text).is_err());
70		assert!(c.prompt_echo_off(&text).is_err());
71		assert!(c.radio_prompt(&text).is_err());
72		assert!(c.binary_prompt(0, &[]).is_err());
73		c.text_info(&text);
74		c.error_msg(&text);
75	}
76}