Mercurial > crates > nonstick
comparison pam/src/items.rs @ 34:ec70822cbdef
Overhaul
author | Andy Caldwell <andrew.caldwell@metaswitch.com> |
---|---|
date | Sun, 24 Apr 2022 03:42:11 +0100 |
parents | 27730595f1ea |
children |
comparison
equal
deleted
inserted
replaced
32:ea5f195f035f | 34:ec70822cbdef |
---|---|
1 use constants::{PamItemType, PAM_SERVICE, PAM_USER, PAM_USER_PROMPT, PAM_TTY, PAM_RUSER, PAM_RHOST, | 1 #[repr(u32)] |
2 PAM_AUTHTOK, PAM_OLDAUTHTOK}; | 2 pub enum ItemType { |
3 use module::PamItem; | 3 /// The service name |
4 pub use conv::PamConv; | 4 Service = 1, |
5 | 5 /// The user name |
6 | 6 User = 2, |
7 pub struct PamService {} | 7 /// The tty name |
8 | 8 Tty = 3, |
9 impl PamItem for PamService { | 9 /// The remote host name |
10 fn item_type() -> PamItemType { | 10 RHost = 4, |
11 PAM_SERVICE | 11 /// The pam_conv structure |
12 } | 12 Conv = 5, |
13 /// The authentication token (password) | |
14 AuthTok = 6, | |
15 /// The old authentication token | |
16 OldAuthTok = 7, | |
17 /// The remote user name | |
18 RUser = 8, | |
19 /// the prompt for getting a username | |
20 UserPrompt = 9, | |
21 /// app supplied function to override failure delays | |
22 FailDelay = 10, | |
23 /// X :display name | |
24 XDisplay = 11, | |
25 /// X :server authentication data | |
26 XAuthData = 12, | |
27 /// The type for pam_get_authtok | |
28 AuthTokType = 13, | |
13 } | 29 } |
14 | 30 |
15 pub struct PamUser {} | 31 // A type that can be requested by `pam::Handle::get_item`. |
32 pub trait Item { | |
33 /// The `repr(C)` type that is returned (by pointer) by the underlying `pam_get_item` function. | |
34 type Raw; | |
16 | 35 |
17 impl PamItem for PamUser { | 36 /// The `ItemType` for this type |
18 fn item_type() -> PamItemType { | 37 fn type_id() -> ItemType; |
19 PAM_USER | 38 |
20 } | 39 /// The function to convert from the pointer to the C-representation to this safer wrapper type |
40 /// | |
41 /// # Safety | |
42 /// | |
43 /// This function can assume the pointer is a valid pointer to a `Self::Raw` instance. | |
44 unsafe fn from_raw(raw: *const Self::Raw) -> Self; | |
45 | |
46 /// The function to convert from this wrapper type to a C-compatible pointer. | |
47 fn into_raw(self) -> *const Self::Raw; | |
21 } | 48 } |
22 | 49 |
23 pub struct PamUserPrompt {} | 50 macro_rules! cstr_item { |
51 ($name:ident) => { | |
52 #[derive(Debug)] | |
53 pub struct $name<'s>(pub &'s std::ffi::CStr); | |
24 | 54 |
25 impl PamItem for PamUserPrompt { | 55 impl<'s> std::ops::Deref for $name<'s> { |
26 fn item_type() -> PamItemType { | 56 type Target = &'s std::ffi::CStr; |
27 PAM_USER_PROMPT | 57 fn deref(&self) -> &Self::Target { |
28 } | 58 &self.0 |
59 } | |
60 } | |
61 | |
62 impl<'s> Item for $name<'s> { | |
63 type Raw = libc::c_char; | |
64 | |
65 fn type_id() -> ItemType { | |
66 ItemType::$name | |
67 } | |
68 | |
69 unsafe fn from_raw(raw: *const Self::Raw) -> Self { | |
70 Self(std::ffi::CStr::from_ptr(raw)) | |
71 } | |
72 | |
73 fn into_raw(self) -> *const Self::Raw { | |
74 self.0.as_ptr() | |
75 } | |
76 } | |
77 }; | |
29 } | 78 } |
30 | 79 |
31 pub struct PamTty {} | 80 cstr_item!(Service); |
32 | 81 cstr_item!(User); |
33 impl PamItem for PamTty { | 82 cstr_item!(Tty); |
34 fn item_type() -> PamItemType { | 83 cstr_item!(RHost); |
35 PAM_TTY | 84 // Conv |
36 } | 85 cstr_item!(AuthTok); |
37 } | 86 cstr_item!(OldAuthTok); |
38 | 87 cstr_item!(RUser); |
39 pub struct PamRUser {} | 88 cstr_item!(UserPrompt); |
40 | |
41 impl PamItem for PamRUser { | |
42 fn item_type() -> PamItemType { | |
43 PAM_RUSER | |
44 } | |
45 } | |
46 | |
47 pub struct PamRHost {} | |
48 | |
49 impl PamItem for PamRHost { | |
50 fn item_type() -> PamItemType { | |
51 PAM_RHOST | |
52 } | |
53 } | |
54 | |
55 pub struct PamAuthTok {} | |
56 | |
57 impl PamItem for PamAuthTok { | |
58 fn item_type() -> PamItemType { | |
59 PAM_AUTHTOK | |
60 } | |
61 } | |
62 | |
63 pub struct PamOldAuthTok {} | |
64 | |
65 impl PamItem for PamOldAuthTok { | |
66 fn item_type() -> PamItemType { | |
67 PAM_OLDAUTHTOK | |
68 } | |
69 } |