Mercurial > crates > nonstick
annotate src/constants.rs @ 59:3f4a77aa88be
Fix string copyting and improve error situation.
This change is too big and includes several things:
- Fix copying strings from PAM by fixing const and mut on pam funcs.
- Improve error enums by simplifying conversions and removing
unnecessary and ambiguous "success" variants.
- Make a bunch of casts nicer.
- Assorted other cleanup.
author | Paul Fisher <paul@pfish.zone> |
---|---|
date | Wed, 21 May 2025 00:27:18 -0400 |
parents | daa2cde64601 |
children | 05cc2c27334f |
rev | line source |
---|---|
56
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
55
diff
changeset
|
1 use bitflags::bitflags; |
15
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
2 use libc::{c_int, c_uint}; |
59
3f4a77aa88be
Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents:
56
diff
changeset
|
3 use num_derive::FromPrimitive; |
3f4a77aa88be
Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents:
56
diff
changeset
|
4 use num_traits::FromPrimitive; |
3f4a77aa88be
Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents:
56
diff
changeset
|
5 use std::any; |
3f4a77aa88be
Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents:
56
diff
changeset
|
6 use std::marker::PhantomData; |
15
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
7 // TODO: Import constants from C header file at compile time. |
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
8 |
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
9 // The Linux-PAM flags |
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
10 // see /usr/include/security/_pam_types.h |
56
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
55
diff
changeset
|
11 bitflags! { |
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
55
diff
changeset
|
12 #[derive(Debug, PartialEq)] |
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
55
diff
changeset
|
13 #[repr(transparent)] |
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
55
diff
changeset
|
14 pub struct Flags: c_uint { |
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
55
diff
changeset
|
15 const SILENT = 0x8000; |
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
55
diff
changeset
|
16 const DISALLOW_NULL_AUTHTOK = 0x0001; |
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
55
diff
changeset
|
17 const ESTABLISH_CRED = 0x0002; |
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
55
diff
changeset
|
18 const DELETE_CRED = 0x0004; |
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
55
diff
changeset
|
19 const REINITIALIZE_CRED = 0x0008; |
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
55
diff
changeset
|
20 const REFRESH_CRED = 0x0010; |
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
55
diff
changeset
|
21 const CHANGE_EXPIRED_AUTHTOK= 0x0020; |
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
55
diff
changeset
|
22 } |
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
55
diff
changeset
|
23 } |
15
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
24 |
56
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
55
diff
changeset
|
25 /// Styles of message that are shown to the user. |
59
3f4a77aa88be
Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents:
56
diff
changeset
|
26 #[derive(Debug, PartialEq, FromPrimitive)] |
56
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
55
diff
changeset
|
27 #[non_exhaustive] // non-exhaustive because C might give us back anything! |
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
55
diff
changeset
|
28 pub enum MessageStyle { |
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
55
diff
changeset
|
29 /// Requests information from the user; will be masked when typing. |
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
55
diff
changeset
|
30 PromptEchoOff = 1, |
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
55
diff
changeset
|
31 /// Requests information from the user; will not be masked. |
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
55
diff
changeset
|
32 PromptEchoOn = 2, |
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
55
diff
changeset
|
33 /// An error message. |
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
55
diff
changeset
|
34 ErrorMsg = 3, |
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
55
diff
changeset
|
35 /// An informational message. |
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
55
diff
changeset
|
36 TextInfo = 4, |
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
55
diff
changeset
|
37 /// Yes/No/Maybe conditionals. Linux-PAM specific. |
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
55
diff
changeset
|
38 RadioType = 5, |
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
55
diff
changeset
|
39 /// For server–client non-human interaction. |
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
55
diff
changeset
|
40 /// NOT part of the X/Open PAM specification. |
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
55
diff
changeset
|
41 BinaryPrompt = 7, |
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
55
diff
changeset
|
42 } |
15
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
43 |
59
3f4a77aa88be
Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents:
56
diff
changeset
|
44 impl TryFrom<c_int> for MessageStyle { |
3f4a77aa88be
Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents:
56
diff
changeset
|
45 type Error = InvalidEnum<Self>; |
3f4a77aa88be
Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents:
56
diff
changeset
|
46 fn try_from(value: c_int) -> Result<Self, Self::Error> { |
3f4a77aa88be
Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents:
56
diff
changeset
|
47 Self::from_i32(value).ok_or(value.into()) |
3f4a77aa88be
Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents:
56
diff
changeset
|
48 } |
3f4a77aa88be
Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents:
56
diff
changeset
|
49 } |
3f4a77aa88be
Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents:
56
diff
changeset
|
50 |
56
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
55
diff
changeset
|
51 impl From<MessageStyle> for c_int { |
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
55
diff
changeset
|
52 fn from(val: MessageStyle) -> Self { |
59
3f4a77aa88be
Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents:
56
diff
changeset
|
53 val as Self |
56
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
55
diff
changeset
|
54 } |
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
55
diff
changeset
|
55 } |
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
55
diff
changeset
|
56 |
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
55
diff
changeset
|
57 /// The Linux-PAM error return values. |
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
55
diff
changeset
|
58 /// Success is instead represented by the `Ok` entry of a `Result`. |
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
55
diff
changeset
|
59 /// Most abbreviations (except `AuthTok` and `Max`) are now full words. |
55
676675c3d434
Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents:
45
diff
changeset
|
60 /// For more detailed information, see |
56
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
55
diff
changeset
|
61 /// `/usr/include/security/_pam_types.h`. |
15
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
62 #[allow(non_camel_case_types, dead_code)] |
59
3f4a77aa88be
Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents:
56
diff
changeset
|
63 #[derive(Copy, Clone, Debug, PartialEq, thiserror::Error, FromPrimitive)] |
56
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
55
diff
changeset
|
64 #[non_exhaustive] // C might give us anything! |
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
55
diff
changeset
|
65 pub enum ErrorCode { |
55
676675c3d434
Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents:
45
diff
changeset
|
66 #[error("dlopen() failure when dynamically loading a service module")] |
56
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
55
diff
changeset
|
67 OpenError = 1, |
55
676675c3d434
Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents:
45
diff
changeset
|
68 #[error("symbol not found")] |
56
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
55
diff
changeset
|
69 SymbolError = 2, |
55
676675c3d434
Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents:
45
diff
changeset
|
70 #[error("error in service module")] |
56
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
55
diff
changeset
|
71 ServiceError = 3, |
55
676675c3d434
Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents:
45
diff
changeset
|
72 #[error("system error")] |
56
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
55
diff
changeset
|
73 SystemError = 4, |
55
676675c3d434
Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents:
45
diff
changeset
|
74 #[error("memory buffer error")] |
56
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
55
diff
changeset
|
75 BufferError = 5, |
55
676675c3d434
Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents:
45
diff
changeset
|
76 #[error("permission denied")] |
56
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
55
diff
changeset
|
77 PermissionDenied = 6, |
55
676675c3d434
Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents:
45
diff
changeset
|
78 #[error("authentication failure")] |
56
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
55
diff
changeset
|
79 AuthenticationError = 7, |
55
676675c3d434
Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents:
45
diff
changeset
|
80 #[error("cannot access authentication data due to insufficient credentials")] |
56
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
55
diff
changeset
|
81 CredentialsInsufficient = 8, |
55
676675c3d434
Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents:
45
diff
changeset
|
82 #[error("underlying authentication service cannot retrieve authentication information")] |
56
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
55
diff
changeset
|
83 AuthInfoUnavailable = 9, |
55
676675c3d434
Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents:
45
diff
changeset
|
84 #[error("user not known to the underlying authentication module")] |
56
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
55
diff
changeset
|
85 UserUnknown = 10, |
55
676675c3d434
Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents:
45
diff
changeset
|
86 #[error("retry limit reached; do not attempt further")] |
56
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
55
diff
changeset
|
87 MaxTries = 11, |
55
676675c3d434
Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents:
45
diff
changeset
|
88 #[error("new authentication token required")] |
56
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
55
diff
changeset
|
89 NewAuthTokRequired = 12, |
55
676675c3d434
Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents:
45
diff
changeset
|
90 #[error("user account has expired")] |
56
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
55
diff
changeset
|
91 AccountExpired = 13, |
55
676675c3d434
Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents:
45
diff
changeset
|
92 #[error("cannot make/remove an entry for the specified session")] |
56
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
55
diff
changeset
|
93 SessionError = 14, |
55
676675c3d434
Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents:
45
diff
changeset
|
94 #[error("underlying authentication service cannot retrieve user credentials")] |
56
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
55
diff
changeset
|
95 CredentialsUnavailable = 15, |
55
676675c3d434
Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents:
45
diff
changeset
|
96 #[error("user credentials expired")] |
56
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
55
diff
changeset
|
97 CredentialsExpired = 16, |
55
676675c3d434
Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents:
45
diff
changeset
|
98 #[error("failure setting user credentials")] |
56
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
55
diff
changeset
|
99 CredentialsError = 17, |
55
676675c3d434
Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents:
45
diff
changeset
|
100 #[error("no module-specific data is present")] |
56
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
55
diff
changeset
|
101 NoModuleData = 18, |
55
676675c3d434
Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents:
45
diff
changeset
|
102 #[error("conversation error")] |
56
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
55
diff
changeset
|
103 ConversationError = 19, |
55
676675c3d434
Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents:
45
diff
changeset
|
104 #[error("authentication token manipulation error")] |
56
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
55
diff
changeset
|
105 AuthTokError = 20, |
55
676675c3d434
Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents:
45
diff
changeset
|
106 #[error("authentication information cannot be recovered")] |
56
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
55
diff
changeset
|
107 AuthTokRecoveryError = 21, |
55
676675c3d434
Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents:
45
diff
changeset
|
108 #[error("authentication token lock busy")] |
56
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
55
diff
changeset
|
109 AuthTokLockBusy = 22, |
55
676675c3d434
Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents:
45
diff
changeset
|
110 #[error("authentication token aging disabled")] |
56
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
55
diff
changeset
|
111 AuthTokDisableAging = 23, |
55
676675c3d434
Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents:
45
diff
changeset
|
112 #[error("preliminary check by password service")] |
56
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
55
diff
changeset
|
113 TryAgain = 24, |
55
676675c3d434
Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents:
45
diff
changeset
|
114 #[error("ignore underlying account module, regardless of control flag")] |
56
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
55
diff
changeset
|
115 Ignore = 25, |
55
676675c3d434
Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents:
45
diff
changeset
|
116 #[error("critical error; this module should fail now")] |
56
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
55
diff
changeset
|
117 Abort = 26, |
55
676675c3d434
Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents:
45
diff
changeset
|
118 #[error("authentication token has expired")] |
56
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
55
diff
changeset
|
119 AuthTokExpired = 27, |
55
676675c3d434
Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents:
45
diff
changeset
|
120 #[error("module is not known")] |
56
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
55
diff
changeset
|
121 ModuleUnknown = 28, |
55
676675c3d434
Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents:
45
diff
changeset
|
122 #[error("bad item passed to pam_[whatever]_item")] |
56
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
55
diff
changeset
|
123 BadItem = 29, |
55
676675c3d434
Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents:
45
diff
changeset
|
124 #[error("conversation function is event-driven and data is not available yet")] |
56
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
55
diff
changeset
|
125 ConversationAgain = 30, |
55
676675c3d434
Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents:
45
diff
changeset
|
126 #[error("call this function again to complete authentication stack")] |
56
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
55
diff
changeset
|
127 Incomplete = 31, |
15
27730595f1ea
Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
128 } |
56
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
55
diff
changeset
|
129 |
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
55
diff
changeset
|
130 pub type PamResult<T> = Result<T, ErrorCode>; |
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
55
diff
changeset
|
131 impl ErrorCode { |
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
55
diff
changeset
|
132 /// Converts a PamResult into the result code that C wants. |
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
55
diff
changeset
|
133 pub fn result_to_c(value: PamResult<()>) -> c_int { |
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
55
diff
changeset
|
134 match value { |
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
55
diff
changeset
|
135 Ok(_) => 0, // PAM_SUCCESS |
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
55
diff
changeset
|
136 Err(otherwise) => otherwise.into(), |
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
55
diff
changeset
|
137 } |
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
55
diff
changeset
|
138 } |
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
55
diff
changeset
|
139 |
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
55
diff
changeset
|
140 /// Converts a C result code into a PamResult, with success as Ok. |
59
3f4a77aa88be
Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents:
56
diff
changeset
|
141 /// Invalid values are returned as a [Self::SystemError]. |
56
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
55
diff
changeset
|
142 pub fn result_from(value: c_int) -> PamResult<()> { |
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
55
diff
changeset
|
143 match value { |
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
55
diff
changeset
|
144 0 => Ok(()), |
59
3f4a77aa88be
Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents:
56
diff
changeset
|
145 value => Err(value.try_into().unwrap_or(Self::SystemError)), |
56
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
55
diff
changeset
|
146 } |
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
55
diff
changeset
|
147 } |
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
55
diff
changeset
|
148 } |
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
55
diff
changeset
|
149 |
59
3f4a77aa88be
Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents:
56
diff
changeset
|
150 impl TryFrom<c_int> for ErrorCode { |
3f4a77aa88be
Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents:
56
diff
changeset
|
151 type Error = InvalidEnum<Self>; |
3f4a77aa88be
Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents:
56
diff
changeset
|
152 |
3f4a77aa88be
Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents:
56
diff
changeset
|
153 fn try_from(value: c_int) -> Result<Self, Self::Error> { |
3f4a77aa88be
Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents:
56
diff
changeset
|
154 Self::from_i32(value).ok_or(value.into()) |
3f4a77aa88be
Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents:
56
diff
changeset
|
155 } |
3f4a77aa88be
Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents:
56
diff
changeset
|
156 } |
3f4a77aa88be
Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents:
56
diff
changeset
|
157 |
56
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
55
diff
changeset
|
158 impl From<ErrorCode> for c_int { |
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
55
diff
changeset
|
159 fn from(val: ErrorCode) -> Self { |
59
3f4a77aa88be
Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents:
56
diff
changeset
|
160 val as Self |
3f4a77aa88be
Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents:
56
diff
changeset
|
161 } |
3f4a77aa88be
Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents:
56
diff
changeset
|
162 } |
3f4a77aa88be
Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents:
56
diff
changeset
|
163 |
3f4a77aa88be
Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents:
56
diff
changeset
|
164 /// Error returned when attempting to coerce an invalid C integer into an enum. |
3f4a77aa88be
Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents:
56
diff
changeset
|
165 #[derive(thiserror::Error)] |
3f4a77aa88be
Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents:
56
diff
changeset
|
166 #[error("{} is not a valid {}", .0, any::type_name::<T>())] |
3f4a77aa88be
Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents:
56
diff
changeset
|
167 #[derive(Debug, PartialEq)] |
3f4a77aa88be
Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents:
56
diff
changeset
|
168 pub struct InvalidEnum<T>(std::ffi::c_int, PhantomData<T>); |
3f4a77aa88be
Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents:
56
diff
changeset
|
169 |
3f4a77aa88be
Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents:
56
diff
changeset
|
170 impl<T> From<std::ffi::c_int> for InvalidEnum<T> { |
3f4a77aa88be
Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents:
56
diff
changeset
|
171 fn from(value: std::ffi::c_int) -> Self { |
3f4a77aa88be
Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents:
56
diff
changeset
|
172 Self(value, PhantomData) |
56
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
55
diff
changeset
|
173 } |
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
55
diff
changeset
|
174 } |
59
3f4a77aa88be
Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents:
56
diff
changeset
|
175 |
3f4a77aa88be
Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents:
56
diff
changeset
|
176 #[cfg(test)] |
3f4a77aa88be
Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents:
56
diff
changeset
|
177 mod tests { |
3f4a77aa88be
Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents:
56
diff
changeset
|
178 use super::*; |
3f4a77aa88be
Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents:
56
diff
changeset
|
179 |
3f4a77aa88be
Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents:
56
diff
changeset
|
180 #[test] |
3f4a77aa88be
Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents:
56
diff
changeset
|
181 fn test_enums() { |
3f4a77aa88be
Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents:
56
diff
changeset
|
182 assert_eq!(Ok(MessageStyle::ErrorMsg), 3.try_into()); |
3f4a77aa88be
Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents:
56
diff
changeset
|
183 assert_eq!(Err(999.into()), ErrorCode::try_from(999)); |
3f4a77aa88be
Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents:
56
diff
changeset
|
184 assert_eq!(Ok(()), ErrorCode::result_from(0)); |
3f4a77aa88be
Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents:
56
diff
changeset
|
185 assert_eq!(Err(ErrorCode::Abort), ErrorCode::result_from(26)); |
3f4a77aa88be
Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents:
56
diff
changeset
|
186 assert_eq!(Err(ErrorCode::SystemError), ErrorCode::result_from(423)); |
3f4a77aa88be
Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents:
56
diff
changeset
|
187 } |
3f4a77aa88be
Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents:
56
diff
changeset
|
188 } |