Mercurial > crates > nonstick
comparison src/macros.rs @ 45:ce47901aab7a
Rename to “nonstick”, move to root, update docs and license.
- Renames the crate to “nonstick”.
- Moves the main library to the root of the repository.
- Removes the example PAM modules.
- Updates copyright information in LICENSE file.
- Updates the README.
| author | Paul Fisher <paul@pfish.zone> |
|---|---|
| date | Tue, 15 Apr 2025 00:50:23 -0400 |
| parents | pam/src/macros.rs@ec70822cbdef |
| children | a921b72743e4 |
comparison
equal
deleted
inserted
replaced
| 44:50371046c61a | 45:ce47901aab7a |
|---|---|
| 1 /// Macro to generate the `extern "C"` entrypoint bindings needed by PAM | |
| 2 /// | |
| 3 /// You can call `pam_hooks!(SomeType);` for any type that implements `PamHooks` | |
| 4 /// | |
| 5 /// ## Examples: | |
| 6 /// | |
| 7 /// Here is full example of a PAM module that would authenticate and authorize everybody: | |
| 8 /// | |
| 9 /// ``` | |
| 10 /// #[macro_use] extern crate pam; | |
| 11 /// | |
| 12 /// use pam::module::{PamHooks, PamHandle}; | |
| 13 /// use pam::constants::{PamResultCode, PamFlag}; | |
| 14 /// use std::ffi::CStr; | |
| 15 /// | |
| 16 /// # fn main() {} | |
| 17 /// struct MyPamModule; | |
| 18 /// pam_hooks!(MyPamModule); | |
| 19 /// | |
| 20 /// impl PamHooks for MyPamModule { | |
| 21 /// fn sm_authenticate(pamh: &mut PamHandle, args: Vec<&CStr>, flags: PamFlag) -> PamResultCode { | |
| 22 /// println!("Everybody is authenticated!"); | |
| 23 /// PamResultCode::PAM_SUCCESS | |
| 24 /// } | |
| 25 /// | |
| 26 /// fn acct_mgmt(pamh: &mut PamHandle, args: Vec<&CStr>, flags: PamFlag) -> PamResultCode { | |
| 27 /// println!("Everybody is authorized!"); | |
| 28 /// PamResultCode::PAM_SUCCESS | |
| 29 /// } | |
| 30 /// } | |
| 31 /// ``` | |
| 32 #[macro_export] | |
| 33 macro_rules! pam_hooks { | |
| 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}; | |
| 39 use $crate::constants::{PamFlag, PamResultCode}; | |
| 40 use $crate::module::{PamHandle, PamHooks}; | |
| 41 | |
| 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 } | |
| 47 | |
| 48 #[no_mangle] | |
| 49 pub extern "C" fn pam_sm_acct_mgmt( | |
| 50 pamh: &mut PamHandle, | |
| 51 flags: PamFlag, | |
| 52 argc: c_int, | |
| 53 argv: *const *const c_char, | |
| 54 ) -> PamResultCode { | |
| 55 let args = extract_argv(argc, argv); | |
| 56 super::$ident::acct_mgmt(pamh, args, flags) | |
| 57 } | |
| 58 | |
| 59 #[no_mangle] | |
| 60 pub extern "C" fn pam_sm_authenticate( | |
| 61 pamh: &mut PamHandle, | |
| 62 flags: PamFlag, | |
| 63 argc: c_int, | |
| 64 argv: *const *const c_char, | |
| 65 ) -> PamResultCode { | |
| 66 let args = extract_argv(argc, argv); | |
| 67 super::$ident::sm_authenticate(pamh, args, flags) | |
| 68 } | |
| 69 | |
| 70 #[no_mangle] | |
| 71 pub extern "C" fn pam_sm_chauthtok( | |
| 72 pamh: &mut PamHandle, | |
| 73 flags: PamFlag, | |
| 74 argc: c_int, | |
| 75 argv: *const *const c_char, | |
| 76 ) -> PamResultCode { | |
| 77 let args = extract_argv(argc, argv); | |
| 78 super::$ident::sm_chauthtok(pamh, args, flags) | |
| 79 } | |
| 80 | |
| 81 #[no_mangle] | |
| 82 pub extern "C" fn pam_sm_close_session( | |
| 83 pamh: &mut PamHandle, | |
| 84 flags: PamFlag, | |
| 85 argc: c_int, | |
| 86 argv: *const *const c_char, | |
| 87 ) -> PamResultCode { | |
| 88 let args = extract_argv(argc, argv); | |
| 89 super::$ident::sm_close_session(pamh, args, flags) | |
| 90 } | |
| 91 | |
| 92 #[no_mangle] | |
| 93 pub extern "C" fn pam_sm_open_session( | |
| 94 pamh: &mut PamHandle, | |
| 95 flags: PamFlag, | |
| 96 argc: c_int, | |
| 97 argv: *const *const c_char, | |
| 98 ) -> PamResultCode { | |
| 99 let args = extract_argv(argc, argv); | |
| 100 super::$ident::sm_open_session(pamh, args, flags) | |
| 101 } | |
| 102 | |
| 103 #[no_mangle] | |
| 104 pub extern "C" fn pam_sm_setcred( | |
| 105 pamh: &mut PamHandle, | |
| 106 flags: PamFlag, | |
| 107 argc: c_int, | |
| 108 argv: *const *const c_char, | |
| 109 ) -> PamResultCode { | |
| 110 let args = extract_argv(argc, argv); | |
| 111 super::$ident::sm_setcred(pamh, args, flags) | |
| 112 } | |
| 113 } | |
| 114 }; | |
| 115 } | |
| 116 | |
| 117 #[macro_export] | |
| 118 macro_rules! pam_try { | |
| 119 ($r:expr) => { | |
| 120 match $r { | |
| 121 Ok(t) => t, | |
| 122 Err(e) => return e, | |
| 123 } | |
| 124 }; | |
| 125 ($r:expr, $e:expr) => { | |
| 126 match $r { | |
| 127 Ok(t) => t, | |
| 128 Err(_) => return $e, | |
| 129 } | |
| 130 }; | |
| 131 } | |
| 132 | |
| 133 #[cfg(test)] | |
| 134 pub mod test { | |
| 135 use module::PamHooks; | |
| 136 | |
| 137 struct Foo; | |
| 138 impl PamHooks for Foo {} | |
| 139 | |
| 140 pam_hooks!(Foo); | |
| 141 } |
