Mercurial > crates > nonstick
annotate 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 |
rev | line source |
---|---|
34 | 1 #[repr(u32)] |
2 pub enum ItemType { | |
3 /// The service name | |
4 Service = 1, | |
5 /// The user name | |
6 User = 2, | |
7 /// The tty name | |
8 Tty = 3, | |
9 /// The remote host name | |
10 RHost = 4, | |
11 /// The pam_conv structure | |
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, | |
15
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
29 } |
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
30 |
34 | 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; | |
35 | |
36 /// The `ItemType` for this type | |
37 fn type_id() -> ItemType; | |
15
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
38 |
34 | 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; | |
15
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
45 |
34 | 46 /// The function to convert from this wrapper type to a C-compatible pointer. |
47 fn into_raw(self) -> *const Self::Raw; | |
15
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
48 } |
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
49 |
34 | 50 macro_rules! cstr_item { |
51 ($name:ident) => { | |
52 #[derive(Debug)] | |
53 pub struct $name<'s>(pub &'s std::ffi::CStr); | |
54 | |
55 impl<'s> std::ops::Deref for $name<'s> { | |
56 type Target = &'s std::ffi::CStr; | |
57 fn deref(&self) -> &Self::Target { | |
58 &self.0 | |
59 } | |
60 } | |
15
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
61 |
34 | 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 }; | |
15
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 |
34 | 80 cstr_item!(Service); |
81 cstr_item!(User); | |
82 cstr_item!(Tty); | |
83 cstr_item!(RHost); | |
84 // Conv | |
85 cstr_item!(AuthTok); | |
86 cstr_item!(OldAuthTok); | |
87 cstr_item!(RUser); | |
88 cstr_item!(UserPrompt); |