annotate src/constants.rs @ 77:351bdc13005e

Update the libpam module to work with the new structure.
author Paul Fisher <paul@pfish.zone>
date Sun, 08 Jun 2025 01:03:46 -0400
parents 58f9d2a4df38
children 5aa1a010f1e8
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;
71
58f9d2a4df38 Reorganize everything again???
Paul Fisher <paul@pfish.zone>
parents: 70
diff changeset
9 use std::result::Result as StdResult;
15
27730595f1ea Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff changeset
10
56
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
11 bitflags! {
60
05cc2c27334f The Big Refactor: clean up docs and exports.
Paul Fisher <paul@pfish.zone>
parents: 59
diff changeset
12 /// The available PAM flags.
05cc2c27334f The Big Refactor: clean up docs and exports.
Paul Fisher <paul@pfish.zone>
parents: 59
diff changeset
13 ///
64
bbe84835d6db More organization; add lots of docs.
Paul Fisher <paul@pfish.zone>
parents: 63
diff changeset
14 /// See `/usr/include/security/_pam_types.h` and
bbe84835d6db More organization; add lots of docs.
Paul Fisher <paul@pfish.zone>
parents: 63
diff changeset
15 /// See `/usr/include/security/pam_modules.h` for more details.
56
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
16 #[derive(Debug, PartialEq)]
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
17 #[repr(transparent)]
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
18 pub struct Flags: c_uint {
64
bbe84835d6db More organization; add lots of docs.
Paul Fisher <paul@pfish.zone>
parents: 63
diff changeset
19 /// The module 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
20 const SILENT = 0x8000;
64
bbe84835d6db More organization; add lots of docs.
Paul Fisher <paul@pfish.zone>
parents: 63
diff changeset
21
bbe84835d6db More organization; add lots of docs.
Paul Fisher <paul@pfish.zone>
parents: 63
diff changeset
22 /// The module should return [ErrorCode::AuthError]
bbe84835d6db More organization; add lots of docs.
Paul Fisher <paul@pfish.zone>
parents: 63
diff changeset
23 /// if the user has an empty authentication token
bbe84835d6db More organization; add lots of docs.
Paul Fisher <paul@pfish.zone>
parents: 63
diff changeset
24 /// rather than immediately accepting them.
56
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
25 const DISALLOW_NULL_AUTHTOK = 0x0001;
64
bbe84835d6db More organization; add lots of docs.
Paul Fisher <paul@pfish.zone>
parents: 63
diff changeset
26
bbe84835d6db More organization; add lots of docs.
Paul Fisher <paul@pfish.zone>
parents: 63
diff changeset
27 // Flag used for `set_credentials`.
bbe84835d6db More organization; add lots of docs.
Paul Fisher <paul@pfish.zone>
parents: 63
diff changeset
28
60
05cc2c27334f The Big Refactor: clean up docs and exports.
Paul Fisher <paul@pfish.zone>
parents: 59
diff changeset
29 /// Set user credentials for an authentication service.
64
bbe84835d6db More organization; add lots of docs.
Paul Fisher <paul@pfish.zone>
parents: 63
diff changeset
30 const ESTABLISH_CREDENTIALS = 0x0002;
60
05cc2c27334f The Big Refactor: clean up docs and exports.
Paul Fisher <paul@pfish.zone>
parents: 59
diff changeset
31 /// Delete user credentials associated with
05cc2c27334f The Big Refactor: clean up docs and exports.
Paul Fisher <paul@pfish.zone>
parents: 59
diff changeset
32 /// an authentication service.
64
bbe84835d6db More organization; add lots of docs.
Paul Fisher <paul@pfish.zone>
parents: 63
diff changeset
33 const DELETE_CREDENTIALS = 0x0004;
60
05cc2c27334f The Big Refactor: clean up docs and exports.
Paul Fisher <paul@pfish.zone>
parents: 59
diff changeset
34 /// Reinitialize user credentials.
64
bbe84835d6db More organization; add lots of docs.
Paul Fisher <paul@pfish.zone>
parents: 63
diff changeset
35 const REINITIALIZE_CREDENTIALS = 0x0008;
60
05cc2c27334f The Big Refactor: clean up docs and exports.
Paul Fisher <paul@pfish.zone>
parents: 59
diff changeset
36 /// Extend the lifetime of user credentials.
64
bbe84835d6db More organization; add lots of docs.
Paul Fisher <paul@pfish.zone>
parents: 63
diff changeset
37 const REFRESH_CREDENTIALS = 0x0010;
bbe84835d6db More organization; add lots of docs.
Paul Fisher <paul@pfish.zone>
parents: 63
diff changeset
38
bbe84835d6db More organization; add lots of docs.
Paul Fisher <paul@pfish.zone>
parents: 63
diff changeset
39 // Flags used for password changing.
bbe84835d6db More organization; add lots of docs.
Paul Fisher <paul@pfish.zone>
parents: 63
diff changeset
40
60
05cc2c27334f The Big Refactor: clean up docs and exports.
Paul Fisher <paul@pfish.zone>
parents: 59
diff changeset
41 /// 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
42 /// 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
43 /// the password service should update all passwords.
64
bbe84835d6db More organization; add lots of docs.
Paul Fisher <paul@pfish.zone>
parents: 63
diff changeset
44 ///
bbe84835d6db More organization; add lots of docs.
Paul Fisher <paul@pfish.zone>
parents: 63
diff changeset
45 /// This flag is only used by `change_authtok`.
60
05cc2c27334f The Big Refactor: clean up docs and exports.
Paul Fisher <paul@pfish.zone>
parents: 59
diff changeset
46 const CHANGE_EXPIRED_AUTHTOK = 0x0020;
64
bbe84835d6db More organization; add lots of docs.
Paul Fisher <paul@pfish.zone>
parents: 63
diff changeset
47
bbe84835d6db More organization; add lots of docs.
Paul Fisher <paul@pfish.zone>
parents: 63
diff changeset
48 /// This is a preliminary check for password changing.
bbe84835d6db More organization; add lots of docs.
Paul Fisher <paul@pfish.zone>
parents: 63
diff changeset
49 /// The password should not be changed.
bbe84835d6db More organization; add lots of docs.
Paul Fisher <paul@pfish.zone>
parents: 63
diff changeset
50 ///
bbe84835d6db More organization; add lots of docs.
Paul Fisher <paul@pfish.zone>
parents: 63
diff changeset
51 /// This is only used between PAM and a module.
bbe84835d6db More organization; add lots of docs.
Paul Fisher <paul@pfish.zone>
parents: 63
diff changeset
52 /// Applications may not use this flag.
bbe84835d6db More organization; add lots of docs.
Paul Fisher <paul@pfish.zone>
parents: 63
diff changeset
53 ///
bbe84835d6db More organization; add lots of docs.
Paul Fisher <paul@pfish.zone>
parents: 63
diff changeset
54 /// This flag is only used by `change_authtok`.
bbe84835d6db More organization; add lots of docs.
Paul Fisher <paul@pfish.zone>
parents: 63
diff changeset
55 const PRELIMINARY_CHECK = 0x4000;
bbe84835d6db More organization; add lots of docs.
Paul Fisher <paul@pfish.zone>
parents: 63
diff changeset
56 /// The password should actuallyPR be updated.
bbe84835d6db More organization; add lots of docs.
Paul Fisher <paul@pfish.zone>
parents: 63
diff changeset
57 /// This and [Self::PRELIMINARY_CHECK] are mutually exclusive.
bbe84835d6db More organization; add lots of docs.
Paul Fisher <paul@pfish.zone>
parents: 63
diff changeset
58 ///
bbe84835d6db More organization; add lots of docs.
Paul Fisher <paul@pfish.zone>
parents: 63
diff changeset
59 /// This is only used between PAM and a module.
bbe84835d6db More organization; add lots of docs.
Paul Fisher <paul@pfish.zone>
parents: 63
diff changeset
60 /// Applications may not use this flag.
bbe84835d6db More organization; add lots of docs.
Paul Fisher <paul@pfish.zone>
parents: 63
diff changeset
61 ///
bbe84835d6db More organization; add lots of docs.
Paul Fisher <paul@pfish.zone>
parents: 63
diff changeset
62 /// This flag is only used by `change_authtok`.
bbe84835d6db More organization; add lots of docs.
Paul Fisher <paul@pfish.zone>
parents: 63
diff changeset
63 const UPDATE_AUTHTOK = 0x2000;
56
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
64 }
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
65 }
15
27730595f1ea Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff changeset
66
60
05cc2c27334f The Big Refactor: clean up docs and exports.
Paul Fisher <paul@pfish.zone>
parents: 59
diff changeset
67 /// 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
68 ///
56
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
69 /// 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
70 /// 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
71 /// `/usr/include/security/_pam_types.h`.
15
27730595f1ea Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff changeset
72 #[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
73 #[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
74 #[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
75 pub enum ErrorCode {
55
676675c3d434 Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents: 45
diff changeset
76 #[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
77 OpenError = 1,
55
676675c3d434 Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents: 45
diff changeset
78 #[error("symbol not found")]
56
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
79 SymbolError = 2,
55
676675c3d434 Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents: 45
diff changeset
80 #[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
81 ServiceError = 3,
55
676675c3d434 Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents: 45
diff changeset
82 #[error("system error")]
56
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
83 SystemError = 4,
55
676675c3d434 Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents: 45
diff changeset
84 #[error("memory buffer error")]
56
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
85 BufferError = 5,
55
676675c3d434 Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents: 45
diff changeset
86 #[error("permission denied")]
56
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
87 PermissionDenied = 6,
55
676675c3d434 Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents: 45
diff changeset
88 #[error("authentication failure")]
56
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
89 AuthenticationError = 7,
55
676675c3d434 Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents: 45
diff changeset
90 #[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
91 CredentialsInsufficient = 8,
55
676675c3d434 Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents: 45
diff changeset
92 #[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
93 AuthInfoUnavailable = 9,
55
676675c3d434 Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents: 45
diff changeset
94 #[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
95 UserUnknown = 10,
55
676675c3d434 Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents: 45
diff changeset
96 #[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
97 MaxTries = 11,
55
676675c3d434 Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents: 45
diff changeset
98 #[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
99 NewAuthTokRequired = 12,
55
676675c3d434 Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents: 45
diff changeset
100 #[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
101 AccountExpired = 13,
55
676675c3d434 Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents: 45
diff changeset
102 #[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
103 SessionError = 14,
55
676675c3d434 Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents: 45
diff changeset
104 #[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
105 CredentialsUnavailable = 15,
55
676675c3d434 Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents: 45
diff changeset
106 #[error("user credentials expired")]
56
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
107 CredentialsExpired = 16,
55
676675c3d434 Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents: 45
diff changeset
108 #[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
109 CredentialsError = 17,
55
676675c3d434 Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents: 45
diff changeset
110 #[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
111 NoModuleData = 18,
55
676675c3d434 Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents: 45
diff changeset
112 #[error("conversation error")]
56
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
113 ConversationError = 19,
55
676675c3d434 Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents: 45
diff changeset
114 #[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
115 AuthTokError = 20,
55
676675c3d434 Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents: 45
diff changeset
116 #[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
117 AuthTokRecoveryError = 21,
55
676675c3d434 Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents: 45
diff changeset
118 #[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
119 AuthTokLockBusy = 22,
55
676675c3d434 Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents: 45
diff changeset
120 #[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
121 AuthTokDisableAging = 23,
64
bbe84835d6db More organization; add lots of docs.
Paul Fisher <paul@pfish.zone>
parents: 63
diff changeset
122 #[error("preliminary password check failed")]
56
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
123 TryAgain = 24,
55
676675c3d434 Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents: 45
diff changeset
124 #[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
125 Ignore = 25,
55
676675c3d434 Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents: 45
diff changeset
126 #[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
127 Abort = 26,
55
676675c3d434 Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents: 45
diff changeset
128 #[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
129 AuthTokExpired = 27,
55
676675c3d434 Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents: 45
diff changeset
130 #[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
131 ModuleUnknown = 28,
55
676675c3d434 Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents: 45
diff changeset
132 #[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
133 BadItem = 29,
55
676675c3d434 Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents: 45
diff changeset
134 #[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
135 ConversationAgain = 30,
55
676675c3d434 Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents: 45
diff changeset
136 #[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
137 Incomplete = 31,
15
27730595f1ea Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff changeset
138 }
56
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
139
60
05cc2c27334f The Big Refactor: clean up docs and exports.
Paul Fisher <paul@pfish.zone>
parents: 59
diff changeset
140 /// A PAM-specific Result type with an [ErrorCode] error.
71
58f9d2a4df38 Reorganize everything again???
Paul Fisher <paul@pfish.zone>
parents: 70
diff changeset
141 pub type Result<T> = StdResult<T, ErrorCode>;
60
05cc2c27334f The Big Refactor: clean up docs and exports.
Paul Fisher <paul@pfish.zone>
parents: 59
diff changeset
142
56
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
143 impl ErrorCode {
60
05cc2c27334f The Big Refactor: clean up docs and exports.
Paul Fisher <paul@pfish.zone>
parents: 59
diff changeset
144 /// 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
145 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
146 match value {
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
147 Ok(_) => 0, // PAM_SUCCESS
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
148 Err(otherwise) => otherwise.into(),
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
149 }
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
150 }
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
151
60
05cc2c27334f The Big Refactor: clean up docs and exports.
Paul Fisher <paul@pfish.zone>
parents: 59
diff changeset
152 /// 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
153 /// 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
154 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
155 match value {
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
156 0 => Ok(()),
59
3f4a77aa88be Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents: 56
diff changeset
157 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
158 }
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
159 }
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
160 }
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
161
59
3f4a77aa88be Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents: 56
diff changeset
162 impl TryFrom<c_int> for ErrorCode {
3f4a77aa88be Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents: 56
diff changeset
163 type Error = InvalidEnum<Self>;
3f4a77aa88be Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents: 56
diff changeset
164
71
58f9d2a4df38 Reorganize everything again???
Paul Fisher <paul@pfish.zone>
parents: 70
diff changeset
165 fn try_from(value: c_int) -> StdResult<Self, Self::Error> {
59
3f4a77aa88be Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents: 56
diff changeset
166 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
167 }
3f4a77aa88be Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents: 56
diff changeset
168 }
3f4a77aa88be Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents: 56
diff changeset
169
56
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
170 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
171 fn from(val: ErrorCode) -> Self {
59
3f4a77aa88be Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents: 56
diff changeset
172 val as Self
3f4a77aa88be Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents: 56
diff changeset
173 }
3f4a77aa88be Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents: 56
diff changeset
174 }
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 /// Error returned when attempting to coerce an invalid C integer into an enum.
77
351bdc13005e Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents: 71
diff changeset
177 #[derive(Debug, PartialEq, thiserror::Error)]
60
05cc2c27334f The Big Refactor: clean up docs and exports.
Paul Fisher <paul@pfish.zone>
parents: 59
diff changeset
178 #[error("{0} is not a valid {type}", type = any::type_name::<T>())]
05cc2c27334f The Big Refactor: clean up docs and exports.
Paul Fisher <paul@pfish.zone>
parents: 59
diff changeset
179 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
180
60
05cc2c27334f The Big Refactor: clean up docs and exports.
Paul Fisher <paul@pfish.zone>
parents: 59
diff changeset
181 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
182 fn from(value: InvalidEnum<T>) -> Self {
05cc2c27334f The Big Refactor: clean up docs and exports.
Paul Fisher <paul@pfish.zone>
parents: 59
diff changeset
183 value.0
05cc2c27334f The Big Refactor: clean up docs and exports.
Paul Fisher <paul@pfish.zone>
parents: 59
diff changeset
184 }
05cc2c27334f The Big Refactor: clean up docs and exports.
Paul Fisher <paul@pfish.zone>
parents: 59
diff changeset
185 }
05cc2c27334f The Big Refactor: clean up docs and exports.
Paul Fisher <paul@pfish.zone>
parents: 59
diff changeset
186
05cc2c27334f The Big Refactor: clean up docs and exports.
Paul Fisher <paul@pfish.zone>
parents: 59
diff changeset
187 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
188 fn from(value: c_int) -> Self {
59
3f4a77aa88be Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents: 56
diff changeset
189 Self(value, PhantomData)
56
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
190 }
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
191 }
59
3f4a77aa88be Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents: 56
diff changeset
192
70
9f8381a1c09c Implement low-level conversation primitives.
Paul Fisher <paul@pfish.zone>
parents: 64
diff changeset
193 /// Returned when text that should not have any `\0` bytes in it does.
9f8381a1c09c Implement low-level conversation primitives.
Paul Fisher <paul@pfish.zone>
parents: 64
diff changeset
194 /// Analogous to [`std::ffi::NulError`], but the data it was created from
9f8381a1c09c Implement low-level conversation primitives.
Paul Fisher <paul@pfish.zone>
parents: 64
diff changeset
195 /// is borrowed.
59
3f4a77aa88be Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents: 56
diff changeset
196 #[cfg(test)]
3f4a77aa88be Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents: 56
diff changeset
197 mod tests {
3f4a77aa88be Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents: 56
diff changeset
198 use super::*;
3f4a77aa88be Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents: 56
diff changeset
199
3f4a77aa88be Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents: 56
diff changeset
200 #[test]
3f4a77aa88be Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents: 56
diff changeset
201 fn test_enums() {
63
a7aa5ca0d00d Move MessageStyle to conv, the only place it is used.
Paul Fisher <paul@pfish.zone>
parents: 60
diff changeset
202 assert_eq!(Ok(ErrorCode::ServiceError), 3.try_into());
60
05cc2c27334f The Big Refactor: clean up docs and exports.
Paul Fisher <paul@pfish.zone>
parents: 59
diff changeset
203 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
204 assert_eq!(Ok(()), ErrorCode::result_from(0));
3f4a77aa88be Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents: 56
diff changeset
205 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
206 assert_eq!(Err(ErrorCode::SystemError), ErrorCode::result_from(423));
63
a7aa5ca0d00d Move MessageStyle to conv, the only place it is used.
Paul Fisher <paul@pfish.zone>
parents: 60
diff changeset
207 assert!(InvalidEnum::<ErrorCode>(33, PhantomData)
60
05cc2c27334f The Big Refactor: clean up docs and exports.
Paul Fisher <paul@pfish.zone>
parents: 59
diff changeset
208 .to_string()
05cc2c27334f The Big Refactor: clean up docs and exports.
Paul Fisher <paul@pfish.zone>
parents: 59
diff changeset
209 .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
210 }
3f4a77aa88be Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents: 56
diff changeset
211 }