|
| 1 | +#[macro_use] |
| 2 | +extern crate clap; |
| 3 | + |
| 4 | +extern crate openldap; |
| 5 | + |
| 6 | +use openldap::errors::*; |
| 7 | +use openldap::*; |
| 8 | +use std::ptr; |
| 9 | + |
| 10 | +#[derive(Clap)] |
| 11 | +#[clap( |
| 12 | + name = "LDAP simple_bind_authentication with start_tls authentication example", |
| 13 | + author = "Mathias Myrland <[email protected]>", |
| 14 | + version = "0.1.0" |
| 15 | +)] |
| 16 | +struct AuthOpts { |
| 17 | + #[clap(short = "u")] |
| 18 | + user: String, |
| 19 | + |
| 20 | + #[clap(short = "p")] |
| 21 | + password: String, |
| 22 | +} |
| 23 | + |
| 24 | +fn ldap_with_start_tls(ldap_uri: &str) -> Result<RustLDAP, LDAPError> { |
| 25 | + let ldap = RustLDAP::new(ldap_uri).unwrap(); |
| 26 | + |
| 27 | + ldap.set_option( |
| 28 | + codes::options::LDAP_OPT_PROTOCOL_VERSION, |
| 29 | + &codes::versions::LDAP_VERSION3, |
| 30 | + ); |
| 31 | + |
| 32 | + // WARNING: Normally you would want to verify the server certificate to avoid |
| 33 | + // man in the middle attacks, but for this testing scenario we're using a |
| 34 | + // generated self signed certificate from the docker container. |
| 35 | + // |
| 36 | + // To set up certificate validation, use the LDAP_OPT_X_TLS_CACERT* options |
| 37 | + ldap.set_option( |
| 38 | + codes::options::LDAP_OPT_X_TLS_REQUIRE_CERT, |
| 39 | + &codes::options::LDAP_OPT_X_TLS_NEVER, |
| 40 | + ); |
| 41 | + |
| 42 | + ldap.set_option(openldap::codes::options::LDAP_OPT_X_TLS_NEWCTX, &0); |
| 43 | + |
| 44 | + ldap.start_tls(None, None)?; |
| 45 | + |
| 46 | + Ok(ldap) |
| 47 | +} |
| 48 | + |
| 49 | +fn do_simple_bind( |
| 50 | + ldap: &RustLDAP, |
| 51 | + ldap_manager_user: &str, |
| 52 | + ldap_manager_pass: &str, |
| 53 | +) -> Result<(), LDAPError> { |
| 54 | + let bind_result = ldap.simple_bind(ldap_manager_user, ldap_manager_pass)?; |
| 55 | + |
| 56 | + match bind_result { |
| 57 | + v if v == openldap::codes::results::LDAP_SUCCESS => Ok(()), |
| 58 | + _ => Err(LDAPError::from(String::from( |
| 59 | + "Authentication with simple bind failed", |
| 60 | + ))), |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +fn ldap_dn_lookup(ldap: &RustLDAP, who: &str) -> Result<String, LDAPError> { |
| 65 | + // Show all DNs matching the description "Human" |
| 66 | + // ldap_search is a powerful query language, look at |
| 67 | + // https://confluence.atlassian.com/kb/how-to-write-ldap-search-filters-792496933.html |
| 68 | + // for an overview |
| 69 | + // |
| 70 | + // This particular filter allows the user to sign in with either |
| 71 | + // uid or email |
| 72 | + let filter = format!("(|(uid={})(mail={}))", who, who); |
| 73 | + |
| 74 | + match ldap.ldap_search( |
| 75 | + "ou=people,dc=planetexpress,dc=com", |
| 76 | + codes::scopes::LDAP_SCOPE_SUBTREE, |
| 77 | + Some(filter.as_str()), |
| 78 | + Some(vec!["dn"]), |
| 79 | + true, |
| 80 | + None, |
| 81 | + None, |
| 82 | + ptr::null_mut(), |
| 83 | + -1, |
| 84 | + ) { |
| 85 | + Ok(search_results) => { |
| 86 | + for result_map in search_results { |
| 87 | + for result_tuple in result_map { |
| 88 | + println!("Found result map with key {}", result_tuple.0); |
| 89 | + for result_data in result_tuple.1 { |
| 90 | + println!("\t {}", result_data); |
| 91 | + return Ok(result_data); |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + Err(LDAPError::from(String::from( |
| 97 | + "Authentication with simple bind failed", |
| 98 | + ))) |
| 99 | + } |
| 100 | + _ => Err(LDAPError::from(String::from( |
| 101 | + "Authentication with simple bind failed", |
| 102 | + ))), |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +fn main() { |
| 107 | + let options = AuthOpts::parse(); |
| 108 | + let user_to_authenticate = options.user; |
| 109 | + let pwd_to_authenticate = options.password; |
| 110 | + |
| 111 | + let ldap_uri = "ldap://localhost:389"; |
| 112 | + let ldap_manager_dn = "cn=Hubert J. Farnsworth,ou=people,dc=planetexpress,dc=com"; |
| 113 | + let ldap_manager_pass = "professor"; |
| 114 | + |
| 115 | + let ldap = ldap_with_start_tls(ldap_uri).unwrap(); |
| 116 | + |
| 117 | + // Bind to the LDAP server with the manager account, |
| 118 | + // this is done to perform a search for the DN to |
| 119 | + // use when authenticating the user attempting to |
| 120 | + // sign in. Obviously, the manager credentials should |
| 121 | + // be kept secret, and not be put under version control. |
| 122 | + // In our test scenario, the professor is the manager. |
| 123 | + do_simple_bind(&ldap, ldap_manager_dn, ldap_manager_pass).unwrap(); |
| 124 | + |
| 125 | + if let Ok(fry_dn) = ldap_dn_lookup(&ldap, user_to_authenticate.as_str()) { |
| 126 | + // Now, perform a bind with the DN we found matching the user attempting to sign in |
| 127 | + // and the password provided in the authentication request |
| 128 | + do_simple_bind(&ldap, fry_dn.as_str(), pwd_to_authenticate.as_str()).unwrap(); |
| 129 | + |
| 130 | + println!("Successfully signed in as fry"); |
| 131 | + } |
| 132 | +} |
0 commit comments