annotate src/constants.rs @ 60:05cc2c27334f

The Big Refactor: clean up docs and exports. - Brings the most important symbols in the library to the root with `pub use` statements. - Expands and updates documentation. - Rearranges things extensively to make the external interface nicer and make the structure easier to understand. - Renames a few things (e.g. `Result`).
author Paul Fisher <paul@pfish.zone>
date Wed, 21 May 2025 19:00:51 -0400
parents 3f4a77aa88be
children a7aa5ca0d00d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
60
05cc2c27334f The Big Refactor: clean up docs and exports.
Paul Fisher <paul@pfish.zone>
parents: 59
diff changeset
1 //! Constants and enum values from the PAM library.
05cc2c27334f The Big Refactor: clean up docs and exports.
Paul Fisher <paul@pfish.zone>
parents: 59
diff changeset
2
56
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
3 use bitflags::bitflags;
15
27730595f1ea Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff changeset
4 use libc::{c_int, c_uint};
59
3f4a77aa88be Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents: 56
diff changeset
5 use num_derive::FromPrimitive;
3f4a77aa88be Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents: 56
diff changeset
6 use num_traits::FromPrimitive;
3f4a77aa88be Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents: 56
diff changeset
7 use std::any;
3f4a77aa88be Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents: 56
diff changeset
8 use std::marker::PhantomData;
15
27730595f1ea Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff changeset
9
56
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
10 bitflags! {
60
05cc2c27334f The Big Refactor: clean up docs and exports.
Paul Fisher <paul@pfish.zone>
parents: 59
diff changeset
11 /// The available PAM flags.
05cc2c27334f The Big Refactor: clean up docs and exports.
Paul Fisher <paul@pfish.zone>
parents: 59
diff changeset
12 ///
05cc2c27334f The Big Refactor: clean up docs and exports.
Paul Fisher <paul@pfish.zone>
parents: 59
diff changeset
13 /// See `/usr/include/security/_pam_types.h` for more details.
56
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
14 #[derive(Debug, PartialEq)]
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
15 #[repr(transparent)]
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
16 pub struct Flags: c_uint {
60
05cc2c27334f The Big Refactor: clean up docs and exports.
Paul Fisher <paul@pfish.zone>
parents: 59
diff changeset
17 /// Authentication service should not generate any messages.
56
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
18 const SILENT = 0x8000;
60
05cc2c27334f The Big Refactor: clean up docs and exports.
Paul Fisher <paul@pfish.zone>
parents: 59
diff changeset
19 /// The service should return [ErrorCode::AuthError] if the user
05cc2c27334f The Big Refactor: clean up docs and exports.
Paul Fisher <paul@pfish.zone>
parents: 59
diff changeset
20 /// has a null authentication token.
56
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
21 const DISALLOW_NULL_AUTHTOK = 0x0001;
60
05cc2c27334f The Big Refactor: clean up docs and exports.
Paul Fisher <paul@pfish.zone>
parents: 59
diff changeset
22 /// Set user credentials for an authentication service.
56
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
23 const ESTABLISH_CRED = 0x0002;
60
05cc2c27334f The Big Refactor: clean up docs and exports.
Paul Fisher <paul@pfish.zone>
parents: 59
diff changeset
24 /// Delete user credentials associated with
05cc2c27334f The Big Refactor: clean up docs and exports.
Paul Fisher <paul@pfish.zone>
parents: 59
diff changeset
25 /// an authentication service.
56
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
26 const DELETE_CRED = 0x0004;
60
05cc2c27334f The Big Refactor: clean up docs and exports.
Paul Fisher <paul@pfish.zone>
parents: 59
diff changeset
27 /// Reinitialize user credentials.
56
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
28 const REINITIALIZE_CRED = 0x0008;
60
05cc2c27334f The Big Refactor: clean up docs and exports.
Paul Fisher <paul@pfish.zone>
parents: 59
diff changeset
29 /// Extend the lifetime of user credentials.
56
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
30 const REFRESH_CRED = 0x0010;
60
05cc2c27334f The Big Refactor: clean up docs and exports.
Paul Fisher <paul@pfish.zone>
parents: 59
diff changeset
31 /// The password service should only update those passwords
05cc2c27334f The Big Refactor: clean up docs and exports.
Paul Fisher <paul@pfish.zone>
parents: 59
diff changeset
32 /// that have aged. If this flag is _not_ passed,
05cc2c27334f The Big Refactor: clean up docs and exports.
Paul Fisher <paul@pfish.zone>
parents: 59
diff changeset
33 /// the password service should update all passwords.
05cc2c27334f The Big Refactor: clean up docs and exports.
Paul Fisher <paul@pfish.zone>
parents: 59
diff changeset
34 const CHANGE_EXPIRED_AUTHTOK = 0x0020;
56
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
35 }
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
36 }
15
27730595f1ea Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff changeset
37
56
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
38 /// 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
39 #[derive(Debug, PartialEq, FromPrimitive)]
56
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
40 #[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
41 pub enum MessageStyle {
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
42 /// 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
43 PromptEchoOff = 1,
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
44 /// 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
45 PromptEchoOn = 2,
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
46 /// An error message.
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
47 ErrorMsg = 3,
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
48 /// An informational message.
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
49 TextInfo = 4,
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
50 /// 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
51 RadioType = 5,
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
52 /// 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
53 /// 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
54 BinaryPrompt = 7,
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
55 }
15
27730595f1ea Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff changeset
56
59
3f4a77aa88be Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents: 56
diff changeset
57 impl TryFrom<c_int> for MessageStyle {
3f4a77aa88be Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents: 56
diff changeset
58 type Error = InvalidEnum<Self>;
60
05cc2c27334f The Big Refactor: clean up docs and exports.
Paul Fisher <paul@pfish.zone>
parents: 59
diff changeset
59 fn try_from(value: c_int) -> std::result::Result<Self, Self::Error> {
59
3f4a77aa88be Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents: 56
diff changeset
60 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
61 }
3f4a77aa88be Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents: 56
diff changeset
62 }
3f4a77aa88be Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents: 56
diff changeset
63
56
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
64 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
65 fn from(val: MessageStyle) -> Self {
59
3f4a77aa88be Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents: 56
diff changeset
66 val as Self
56
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
67 }
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
68 }
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
69
60
05cc2c27334f The Big Refactor: clean up docs and exports.
Paul Fisher <paul@pfish.zone>
parents: 59
diff changeset
70 /// The Linux-PAM error return values. Success is an Ok [Result].
05cc2c27334f The Big Refactor: clean up docs and exports.
Paul Fisher <paul@pfish.zone>
parents: 59
diff changeset
71 ///
56
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
72 /// 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
73 /// 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
74 /// `/usr/include/security/_pam_types.h`.
15
27730595f1ea Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff changeset
75 #[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
76 #[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
77 #[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
78 pub enum ErrorCode {
55
676675c3d434 Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents: 45
diff changeset
79 #[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
80 OpenError = 1,
55
676675c3d434 Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents: 45
diff changeset
81 #[error("symbol not found")]
56
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
82 SymbolError = 2,
55
676675c3d434 Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents: 45
diff changeset
83 #[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
84 ServiceError = 3,
55
676675c3d434 Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents: 45
diff changeset
85 #[error("system error")]
56
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
86 SystemError = 4,
55
676675c3d434 Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents: 45
diff changeset
87 #[error("memory buffer error")]
56
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
88 BufferError = 5,
55
676675c3d434 Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents: 45
diff changeset
89 #[error("permission denied")]
56
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
90 PermissionDenied = 6,
55
676675c3d434 Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents: 45
diff changeset
91 #[error("authentication failure")]
56
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
92 AuthenticationError = 7,
55
676675c3d434 Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents: 45
diff changeset
93 #[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
94 CredentialsInsufficient = 8,
55
676675c3d434 Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents: 45
diff changeset
95 #[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
96 AuthInfoUnavailable = 9,
55
676675c3d434 Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents: 45
diff changeset
97 #[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
98 UserUnknown = 10,
55
676675c3d434 Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents: 45
diff changeset
99 #[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
100 MaxTries = 11,
55
676675c3d434 Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents: 45
diff changeset
101 #[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
102 NewAuthTokRequired = 12,
55
676675c3d434 Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents: 45
diff changeset
103 #[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
104 AccountExpired = 13,
55
676675c3d434 Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents: 45
diff changeset
105 #[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
106 SessionError = 14,
55
676675c3d434 Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents: 45
diff changeset
107 #[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
108 CredentialsUnavailable = 15,
55
676675c3d434 Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents: 45
diff changeset
109 #[error("user credentials expired")]
56
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
110 CredentialsExpired = 16,
55
676675c3d434 Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents: 45
diff changeset
111 #[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
112 CredentialsError = 17,
55
676675c3d434 Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents: 45
diff changeset
113 #[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
114 NoModuleData = 18,
55
676675c3d434 Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents: 45
diff changeset
115 #[error("conversation error")]
56
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
116 ConversationError = 19,
55
676675c3d434 Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents: 45
diff changeset
117 #[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
118 AuthTokError = 20,
55
676675c3d434 Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents: 45
diff changeset
119 #[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
120 AuthTokRecoveryError = 21,
55
676675c3d434 Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents: 45
diff changeset
121 #[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
122 AuthTokLockBusy = 22,
55
676675c3d434 Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents: 45
diff changeset
123 #[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
124 AuthTokDisableAging = 23,
55
676675c3d434 Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents: 45
diff changeset
125 #[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
126 TryAgain = 24,
55
676675c3d434 Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents: 45
diff changeset
127 #[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
128 Ignore = 25,
55
676675c3d434 Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents: 45
diff changeset
129 #[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
130 Abort = 26,
55
676675c3d434 Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents: 45
diff changeset
131 #[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
132 AuthTokExpired = 27,
55
676675c3d434 Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents: 45
diff changeset
133 #[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
134 ModuleUnknown = 28,
55
676675c3d434 Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents: 45
diff changeset
135 #[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
136 BadItem = 29,
55
676675c3d434 Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents: 45
diff changeset
137 #[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
138 ConversationAgain = 30,
55
676675c3d434 Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents: 45
diff changeset
139 #[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
140 Incomplete = 31,
15
27730595f1ea Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff changeset
141 }
56
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
142
60
05cc2c27334f The Big Refactor: clean up docs and exports.
Paul Fisher <paul@pfish.zone>
parents: 59
diff changeset
143 /// A PAM-specific Result type with an [ErrorCode] error.
05cc2c27334f The Big Refactor: clean up docs and exports.
Paul Fisher <paul@pfish.zone>
parents: 59
diff changeset
144 pub type Result<T> = std::result::Result<T, ErrorCode>;
05cc2c27334f The Big Refactor: clean up docs and exports.
Paul Fisher <paul@pfish.zone>
parents: 59
diff changeset
145
56
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
146 impl ErrorCode {
60
05cc2c27334f The Big Refactor: clean up docs and exports.
Paul Fisher <paul@pfish.zone>
parents: 59
diff changeset
147 /// Converts this [Result] into a C-compatible result code.
05cc2c27334f The Big Refactor: clean up docs and exports.
Paul Fisher <paul@pfish.zone>
parents: 59
diff changeset
148 pub fn result_to_c<T>(value: Result<T>) -> c_int {
56
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
149 match value {
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
150 Ok(_) => 0, // PAM_SUCCESS
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
151 Err(otherwise) => otherwise.into(),
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
152 }
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
153 }
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
154
60
05cc2c27334f The Big Refactor: clean up docs and exports.
Paul Fisher <paul@pfish.zone>
parents: 59
diff changeset
155 /// Converts a C result code into a [Result], with success as Ok.
59
3f4a77aa88be Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents: 56
diff changeset
156 /// Invalid values are returned as a [Self::SystemError].
60
05cc2c27334f The Big Refactor: clean up docs and exports.
Paul Fisher <paul@pfish.zone>
parents: 59
diff changeset
157 pub fn result_from(value: c_int) -> Result<()> {
56
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
158 match value {
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
159 0 => Ok(()),
59
3f4a77aa88be Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents: 56
diff changeset
160 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
161 }
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
162 }
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
163 }
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
164
59
3f4a77aa88be Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents: 56
diff changeset
165 impl TryFrom<c_int> for ErrorCode {
3f4a77aa88be Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents: 56
diff changeset
166 type Error = InvalidEnum<Self>;
3f4a77aa88be Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents: 56
diff changeset
167
60
05cc2c27334f The Big Refactor: clean up docs and exports.
Paul Fisher <paul@pfish.zone>
parents: 59
diff changeset
168 fn try_from(value: c_int) -> std::result::Result<Self, Self::Error> {
59
3f4a77aa88be Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents: 56
diff changeset
169 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
170 }
3f4a77aa88be Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents: 56
diff changeset
171 }
3f4a77aa88be Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents: 56
diff changeset
172
56
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
173 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
174 fn from(val: ErrorCode) -> Self {
59
3f4a77aa88be Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents: 56
diff changeset
175 val as Self
3f4a77aa88be Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents: 56
diff changeset
176 }
3f4a77aa88be Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents: 56
diff changeset
177 }
3f4a77aa88be Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents: 56
diff changeset
178
3f4a77aa88be Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents: 56
diff changeset
179 /// 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
180 #[derive(thiserror::Error)]
60
05cc2c27334f The Big Refactor: clean up docs and exports.
Paul Fisher <paul@pfish.zone>
parents: 59
diff changeset
181 #[error("{0} is not a valid {type}", type = any::type_name::<T>())]
59
3f4a77aa88be Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents: 56
diff changeset
182 #[derive(Debug, PartialEq)]
60
05cc2c27334f The Big Refactor: clean up docs and exports.
Paul Fisher <paul@pfish.zone>
parents: 59
diff changeset
183 pub struct InvalidEnum<T>(c_int, PhantomData<T>);
59
3f4a77aa88be Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents: 56
diff changeset
184
60
05cc2c27334f The Big Refactor: clean up docs and exports.
Paul Fisher <paul@pfish.zone>
parents: 59
diff changeset
185 impl<T> From<InvalidEnum<T>> for c_int {
05cc2c27334f The Big Refactor: clean up docs and exports.
Paul Fisher <paul@pfish.zone>
parents: 59
diff changeset
186 fn from(value: InvalidEnum<T>) -> Self {
05cc2c27334f The Big Refactor: clean up docs and exports.
Paul Fisher <paul@pfish.zone>
parents: 59
diff changeset
187 value.0
05cc2c27334f The Big Refactor: clean up docs and exports.
Paul Fisher <paul@pfish.zone>
parents: 59
diff changeset
188 }
05cc2c27334f The Big Refactor: clean up docs and exports.
Paul Fisher <paul@pfish.zone>
parents: 59
diff changeset
189 }
05cc2c27334f The Big Refactor: clean up docs and exports.
Paul Fisher <paul@pfish.zone>
parents: 59
diff changeset
190
05cc2c27334f The Big Refactor: clean up docs and exports.
Paul Fisher <paul@pfish.zone>
parents: 59
diff changeset
191 impl<T> From<c_int> for InvalidEnum<T> {
05cc2c27334f The Big Refactor: clean up docs and exports.
Paul Fisher <paul@pfish.zone>
parents: 59
diff changeset
192 fn from(value: c_int) -> Self {
59
3f4a77aa88be Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents: 56
diff changeset
193 Self(value, PhantomData)
56
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
194 }
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
195 }
59
3f4a77aa88be Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents: 56
diff changeset
196
3f4a77aa88be Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents: 56
diff changeset
197 #[cfg(test)]
3f4a77aa88be Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents: 56
diff changeset
198 mod tests {
3f4a77aa88be Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents: 56
diff changeset
199 use super::*;
3f4a77aa88be Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents: 56
diff changeset
200
3f4a77aa88be Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents: 56
diff changeset
201 #[test]
3f4a77aa88be Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents: 56
diff changeset
202 fn test_enums() {
3f4a77aa88be Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents: 56
diff changeset
203 assert_eq!(Ok(MessageStyle::ErrorMsg), 3.try_into());
60
05cc2c27334f The Big Refactor: clean up docs and exports.
Paul Fisher <paul@pfish.zone>
parents: 59
diff changeset
204 assert_eq!(Err(InvalidEnum::from(999)), ErrorCode::try_from(999));
59
3f4a77aa88be Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents: 56
diff changeset
205 assert_eq!(Ok(()), ErrorCode::result_from(0));
3f4a77aa88be Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents: 56
diff changeset
206 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
207 assert_eq!(Err(ErrorCode::SystemError), ErrorCode::result_from(423));
60
05cc2c27334f The Big Refactor: clean up docs and exports.
Paul Fisher <paul@pfish.zone>
parents: 59
diff changeset
208 assert!(InvalidEnum::<MessageStyle>(33, PhantomData)
05cc2c27334f The Big Refactor: clean up docs and exports.
Paul Fisher <paul@pfish.zone>
parents: 59
diff changeset
209 .to_string()
05cc2c27334f The Big Refactor: clean up docs and exports.
Paul Fisher <paul@pfish.zone>
parents: 59
diff changeset
210 .starts_with("33 is not a valid "));
59
3f4a77aa88be Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents: 56
diff changeset
211 }
3f4a77aa88be Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents: 56
diff changeset
212 }