Mercurial > crates > nonstick
annotate pam-sober/src/lib.rs @ 19:d654aa0655e5
Making PamHandle a struct with methods
| author | Anthony Nowell <anthony@algorithmia.com> |
|---|---|
| date | Mon, 25 Sep 2017 23:42:35 -0600 |
| parents | 53efbcff805d |
| children | 734ca62159fb |
| rev | line source |
|---|---|
| 17 | 1 extern crate pam; |
| 2 extern crate rand; | |
| 3 | |
| 4 pub mod ffi; | |
| 5 | |
|
19
d654aa0655e5
Making PamHandle a struct with methods
Anthony Nowell <anthony@algorithmia.com>
parents:
17
diff
changeset
|
6 use pam::module::PamHandle; |
| 17 | 7 use pam::constants::{PamResultCode, PAM_PROMPT_ECHO_OFF}; |
| 8 use pam::conv::PamConv; | |
| 9 use rand::Rng; | |
| 10 use std::str::FromStr; | |
| 11 | |
| 12 macro_rules! pam_try { | |
| 13 ($e:expr) => ( | |
| 14 match $e { | |
| 15 Ok(v) => v, | |
| 16 Err(e) => return e, | |
| 17 } | |
| 18 ); | |
| 19 ($e:expr, $err:expr) => ( | |
| 20 match $e { | |
| 21 Ok(v) => v, | |
| 22 Err(e) => { | |
| 23 println!("Error: {}", e); | |
| 24 return $err; | |
| 25 } | |
| 26 } | |
| 27 ); | |
| 28 } | |
| 29 | |
| 30 // This function performs the task of authenticating the user. | |
|
19
d654aa0655e5
Making PamHandle a struct with methods
Anthony Nowell <anthony@algorithmia.com>
parents:
17
diff
changeset
|
31 pub fn sm_authenticate(pamh: &PamHandle, args: Vec<String>, silent: bool) -> PamResultCode { |
| 17 | 32 println!("Let's auth over HTTP"); |
| 33 | |
| 34 /* TODO: use args to change difficulty ;-) | |
| 35 let args: HashMap<&str, &str> = args.iter().map(|s| { | |
| 36 let mut parts = s.splitn(2, "="); | |
| 37 (parts.next().unwrap(), parts.next().unwrap_or("")) | |
| 38 }).collect(); | |
| 39 */ | |
| 40 | |
| 41 // TODO: maybe we can change difficulty base on user? | |
|
19
d654aa0655e5
Making PamHandle a struct with methods
Anthony Nowell <anthony@algorithmia.com>
parents:
17
diff
changeset
|
42 // let user = pam_try!(pam.get_user(None)); |
| 17 | 43 |
|
19
d654aa0655e5
Making PamHandle a struct with methods
Anthony Nowell <anthony@algorithmia.com>
parents:
17
diff
changeset
|
44 let conv = match pamh.get_item::<PamConv>() { |
| 17 | 45 Ok(conv) => conv, |
| 46 Err(err) => { | |
| 47 println!("Couldn't get pam_conv"); | |
| 48 return err; | |
| 49 } | |
| 50 }; | |
| 51 | |
| 52 let mut rng = rand::thread_rng(); | |
| 53 let a = rng.gen::<u32>() % 100; | |
| 54 let b = rng.gen::<u32>() % 100; | |
| 55 let math = format!("{} + {} = ", a, b); | |
| 56 | |
| 57 // This println kinda helps debugging since the test script doesn't echo | |
| 58 println!("{}", math); | |
| 59 | |
| 60 let password = pam_try!(conv.send(PAM_PROMPT_ECHO_OFF, &math)); | |
| 61 | |
| 62 if password.and_then(|p| u32::from_str(&p).ok()) == Some(a+b) { | |
| 63 return PamResultCode::PAM_SUCCESS; | |
| 64 } | |
| 65 | |
| 66 println!("You failed the PAM sobriety test."); | |
| 67 return PamResultCode::PAM_AUTH_ERR; | |
| 68 } | |
| 69 | |
| 70 // This function performs the task of altering the credentials of the user with respect to the | |
| 71 // corresponding authorization scheme. Generally, an authentication module may have access to more | |
| 72 // information about a user than their authentication token. This function is used to make such | |
| 73 // information available to the application. It should only be called after the user has been | |
| 74 // authenticated but before a session has been established. | |
|
19
d654aa0655e5
Making PamHandle a struct with methods
Anthony Nowell <anthony@algorithmia.com>
parents:
17
diff
changeset
|
75 pub fn sm_setcred(_pamh: &PamHandle, _args: Vec<String>, _silent: bool) -> PamResultCode { |
| 17 | 76 println!("set credentials"); |
| 77 PamResultCode::PAM_SUCCESS | |
| 78 } | |
| 79 | |
| 80 // This function performs the task of establishing whether the user is permitted to gain access at | |
| 81 // this time. It should be understood that the user has previously been validated by an | |
| 82 // authentication module. This function checks for other things. Such things might be: the time of | |
| 83 // day or the date, the terminal line, remote hostname, etc. This function may also determine | |
| 84 // things like the expiration on passwords, and respond that the user change it before continuing. | |
|
19
d654aa0655e5
Making PamHandle a struct with methods
Anthony Nowell <anthony@algorithmia.com>
parents:
17
diff
changeset
|
85 pub fn acct_mgmt(_pamh: &PamHandle, _args: Vec<String>, _silent: bool) -> PamResultCode { |
| 17 | 86 println!("account management"); |
| 87 PamResultCode::PAM_SUCCESS | |
| 88 } |
