Mercurial > crates > nonstick
annotate src/macros.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 |
| rev | line source |
|---|---|
|
22
4263c1d83d5b
Refactor PamHooks into modules mod
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
1 /// Macro to generate the `extern "C"` entrypoint bindings needed by PAM |
|
4263c1d83d5b
Refactor PamHooks into modules mod
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
2 /// |
|
4263c1d83d5b
Refactor PamHooks into modules mod
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
3 /// You can call `pam_hooks!(SomeType);` for any type that implements `PamHooks` |
|
4263c1d83d5b
Refactor PamHooks into modules mod
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
4 /// |
|
4263c1d83d5b
Refactor PamHooks into modules mod
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
5 /// ## Examples: |
|
4263c1d83d5b
Refactor PamHooks into modules mod
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
6 /// |
|
4263c1d83d5b
Refactor PamHooks into modules mod
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
7 /// Here is full example of a PAM module that would authenticate and authorize everybody: |
|
4263c1d83d5b
Refactor PamHooks into modules mod
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
8 /// |
|
4263c1d83d5b
Refactor PamHooks into modules mod
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:
48
diff
changeset
|
10 /// #[macro_use] extern crate nonstick; |
|
22
4263c1d83d5b
Refactor PamHooks into modules mod
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
11 /// |
|
56
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
48
diff
changeset
|
12 /// use nonstick::module::{PamHooks, PamHandle}; |
|
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
48
diff
changeset
|
13 /// use nonstick::constants::{PamResult, Flags}; |
|
22
4263c1d83d5b
Refactor PamHooks into modules mod
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
14 /// use std::ffi::CStr; |
|
4263c1d83d5b
Refactor PamHooks into modules mod
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
15 /// |
|
4263c1d83d5b
Refactor PamHooks into modules mod
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
16 /// # fn main() {} |
|
4263c1d83d5b
Refactor PamHooks into modules mod
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
17 /// struct MyPamModule; |
|
4263c1d83d5b
Refactor PamHooks into modules mod
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
18 /// pam_hooks!(MyPamModule); |
|
4263c1d83d5b
Refactor PamHooks into modules mod
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
19 /// |
|
4263c1d83d5b
Refactor PamHooks into modules mod
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
20 /// impl PamHooks for MyPamModule { |
|
56
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
48
diff
changeset
|
21 /// fn acct_mgmt(pamh: &mut PamHandle, args: Vec<&CStr>, flags: Flags) -> PamResult<()> { |
|
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
48
diff
changeset
|
22 /// println!("Everybody is authorized!"); |
|
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
48
diff
changeset
|
23 /// Ok(()) |
|
22
4263c1d83d5b
Refactor PamHooks into modules mod
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
24 /// } |
|
4263c1d83d5b
Refactor PamHooks into modules mod
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
25 /// |
|
56
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
48
diff
changeset
|
26 /// fn sm_authenticate(pamh: &mut PamHandle, args: Vec<&CStr>, flags: Flags) -> PamResult<()> { |
|
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
48
diff
changeset
|
27 /// println!("Everybody is authenticated!"); |
|
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
48
diff
changeset
|
28 /// Ok(()) |
|
22
4263c1d83d5b
Refactor PamHooks into modules mod
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
29 /// } |
|
4263c1d83d5b
Refactor PamHooks into modules mod
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
30 /// } |
|
4263c1d83d5b
Refactor PamHooks into modules mod
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
31 /// ``` |
|
4263c1d83d5b
Refactor PamHooks into modules mod
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
32 #[macro_export] |
|
4263c1d83d5b
Refactor PamHooks into modules mod
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
33 macro_rules! pam_hooks { |
| 34 | 34 ($ident:ident) => { |
| 35 pub use self::pam_hooks_scope::*; | |
| 36 mod pam_hooks_scope { | |
| 37 use std::ffi::CStr; | |
| 38 use std::os::raw::{c_char, c_int}; | |
|
59
3f4a77aa88be
Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents:
56
diff
changeset
|
39 use $crate::constants::{ErrorCode, Flags}; |
| 34 | 40 use $crate::module::{PamHandle, PamHooks}; |
|
22
4263c1d83d5b
Refactor PamHooks into modules mod
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
41 |
| 34 | 42 fn extract_argv<'a>(argc: c_int, argv: *const *const c_char) -> Vec<&'a CStr> { |
| 43 (0..argc) | |
| 44 .map(|o| unsafe { CStr::from_ptr(*argv.offset(o as isize) as *const c_char) }) | |
| 45 .collect() | |
| 46 } | |
|
22
4263c1d83d5b
Refactor PamHooks into modules mod
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
47 |
| 34 | 48 #[no_mangle] |
| 49 pub extern "C" fn pam_sm_acct_mgmt( | |
| 50 pamh: &mut PamHandle, | |
|
56
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
48
diff
changeset
|
51 flags: Flags, |
| 34 | 52 argc: c_int, |
| 53 argv: *const *const c_char, | |
|
56
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
48
diff
changeset
|
54 ) -> c_int { |
| 34 | 55 let args = extract_argv(argc, argv); |
|
56
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
48
diff
changeset
|
56 ErrorCode::result_to_c(super::$ident::acct_mgmt(pamh, args, flags)) |
| 34 | 57 } |
|
22
4263c1d83d5b
Refactor PamHooks into modules mod
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
58 |
| 34 | 59 #[no_mangle] |
| 60 pub extern "C" fn pam_sm_authenticate( | |
| 61 pamh: &mut PamHandle, | |
|
56
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
48
diff
changeset
|
62 flags: Flags, |
| 34 | 63 argc: c_int, |
| 64 argv: *const *const c_char, | |
|
56
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
48
diff
changeset
|
65 ) -> c_int { |
| 34 | 66 let args = extract_argv(argc, argv); |
|
56
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
48
diff
changeset
|
67 ErrorCode::result_to_c(super::$ident::sm_authenticate(pamh, args, flags)) |
| 34 | 68 } |
| 69 | |
| 70 #[no_mangle] | |
| 71 pub extern "C" fn pam_sm_chauthtok( | |
| 72 pamh: &mut PamHandle, | |
|
56
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
48
diff
changeset
|
73 flags: Flags, |
| 34 | 74 argc: c_int, |
| 75 argv: *const *const c_char, | |
|
56
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
48
diff
changeset
|
76 ) -> c_int { |
| 34 | 77 let args = extract_argv(argc, argv); |
|
56
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
48
diff
changeset
|
78 ErrorCode::result_to_c(super::$ident::sm_chauthtok(pamh, args, flags)) |
| 34 | 79 } |
|
22
4263c1d83d5b
Refactor PamHooks into modules mod
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
80 |
| 34 | 81 #[no_mangle] |
| 82 pub extern "C" fn pam_sm_close_session( | |
| 83 pamh: &mut PamHandle, | |
|
56
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
48
diff
changeset
|
84 flags: Flags, |
| 34 | 85 argc: c_int, |
| 86 argv: *const *const c_char, | |
|
56
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
48
diff
changeset
|
87 ) -> c_int { |
| 34 | 88 let args = extract_argv(argc, argv); |
|
56
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
48
diff
changeset
|
89 ErrorCode::result_to_c(super::$ident::sm_close_session(pamh, args, flags)) |
| 34 | 90 } |
|
22
4263c1d83d5b
Refactor PamHooks into modules mod
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
91 |
| 34 | 92 #[no_mangle] |
| 93 pub extern "C" fn pam_sm_open_session( | |
| 94 pamh: &mut PamHandle, | |
|
56
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
48
diff
changeset
|
95 flags: Flags, |
| 34 | 96 argc: c_int, |
| 97 argv: *const *const c_char, | |
|
56
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
48
diff
changeset
|
98 ) -> c_int { |
| 34 | 99 let args = extract_argv(argc, argv); |
|
56
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
48
diff
changeset
|
100 ErrorCode::result_to_c(super::$ident::sm_open_session(pamh, args, flags)) |
| 34 | 101 } |
|
22
4263c1d83d5b
Refactor PamHooks into modules mod
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
102 |
| 34 | 103 #[no_mangle] |
| 104 pub extern "C" fn pam_sm_setcred( | |
| 105 pamh: &mut PamHandle, | |
|
56
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
48
diff
changeset
|
106 flags: Flags, |
| 34 | 107 argc: c_int, |
| 108 argv: *const *const c_char, | |
|
56
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
48
diff
changeset
|
109 ) -> c_int { |
| 34 | 110 let args = extract_argv(argc, argv); |
|
56
daa2cde64601
Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents:
48
diff
changeset
|
111 ErrorCode::result_to_c(super::$ident::sm_setcred(pamh, args, flags)) |
| 34 | 112 } |
| 113 } | |
| 114 }; | |
| 115 } | |
|
22
4263c1d83d5b
Refactor PamHooks into modules mod
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
116 |
|
4263c1d83d5b
Refactor PamHooks into modules mod
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
117 #[cfg(test)] |
|
4263c1d83d5b
Refactor PamHooks into modules mod
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
118 pub mod test { |
| 48 | 119 use crate::module::PamHooks; |
|
22
4263c1d83d5b
Refactor PamHooks into modules mod
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
120 |
| 34 | 121 struct Foo; |
| 122 impl PamHooks for Foo {} | |
|
22
4263c1d83d5b
Refactor PamHooks into modules mod
Anthony Nowell <anthony@algorithmia.com>
parents:
diff
changeset
|
123 |
| 34 | 124 pam_hooks!(Foo); |
| 125 } |
