1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
extern crate libc;
extern crate toml;
extern crate ldap3;
extern crate serde;
#[macro_use]
extern crate serde_derive;
extern crate rand;

mod pam;
mod config;

use rand::{thread_rng, Rng};
use std::collections::BTreeSet;
use ldap3::{LdapConn, Scope, SearchEntry};

// Re-export the PAM callbacks
pub use pam::callbacks::*;
use pam::{PamResultCode, get_user, set_user};
use config::Config;


#[allow(dead_code)]
fn spare() {
    println!("");
    panic!("");
}

pub fn acct_mgmt(pamh: pam::PamHandleT, args: Vec<String>, silent: bool) -> PamResultCode {

    let user = match get_user(pamh) {
        Ok(u) => u,
        Err(_) => return PamResultCode::PAM_AUTH_ERR,
    };

    if args.len() != 1 {
        return PamResultCode::PAM_SERVICE_ERR;
    }

    let config = match Config::load(&args[0]) {
        Ok(c) => c,
        Err(e) => {
            if !silent {
                println!("ERROR: {:?}", e);
            }
            return PamResultCode::PAM_SERVICE_ERR;
        }
    };

    let lconn = match ldap_connect(&config.ldap) {
        Ok(c) => c,
        Err(_) => return PamResultCode::PAM_SERVICE_ERR,
    };

    let groups = match get_user_groups(&lconn, &config.ldap, &user) {
        Ok(g) => g,
        Err(e) => return e,
    };

    let mut ret = PamResultCode::PAM_AUTH_ERR;
    for (group, mapped_user) in config.mappings.iter() {
        if groups.contains(group) {
            if !silent {
                println!("Mapping {} -> {}", user, mapped_user);
            }
            match set_user(pamh, mapped_user.clone()) {
                Ok(_) => {
                    ret = PamResultCode::PAM_SUCCESS;
                    break;
                }
                Err(_) => {
                    ret = PamResultCode::PAM_SERVICE_ERR;
                    break;
                }
            }
        };
    }
    lconn.unbind().ok();
    ret
}

fn extract_ldap_servers<'a>(uri: &'a String) -> Vec<&'a str> {
    uri.split(',').collect::<Vec<_>>()
}

#[test]
fn test_extract_ldap_servers() {
    assert_eq!(
        extract_ldap_servers(&String::from("asdf,qwer")),
        vec!["asdf", "qwer"]
    );
}

fn ldap_connect(ldap: &config::LdapConfig) -> Result<LdapConn, ()> {
    // Because of the current limitations of the LDAP3 library that
    // does not allow us to set TCP connect timeout try to shuffle
    // the server list on connection to partially mitigate the connect delay
    // in case a server is temporarily down.
    let servers = {
        let mut servers = extract_ldap_servers(&ldap.uri);
        let mut rng = thread_rng();
        rng.shuffle(servers.as_mut_slice());
        servers
    };
    for server in servers {
        // println!("Trying to connect to {}", server);
        let lconn = match LdapConn::new(server) {
            Ok(c) => {
                // println!("Connected to {}", server);
                c
            }
            Err(_) => {
                // println!("Failed to connect to {}, trying next.", server);
                continue;
            }
        };
        match lconn.simple_bind(&ldap.user, &ldap.pass) {
            Ok(_) => {
                // println!("Simple Bind Succeeded");
                return Ok(lconn);
            }
            Err(_) => {
                // println!("Simple Bind Failed, trying next.");
                continue;
            }
        }
    }
    // println!("Server list exhausted.");
    Err(())
}

fn get_user_groups(
    lconn: &LdapConn,
    config: &config::LdapConfig,
    user: &String,
) -> Result<BTreeSet<String>, PamResultCode> {
    lconn
        .search(
            &config.user_base_dn,
            Scope::Subtree,
            &format!("({}={})", config.uid_attribute, user),
            vec![config.group_attribute.clone()],
        )
        .map(|result_tuple| {
            result_tuple
                .0
                .into_iter()
                .map(|r| SearchEntry::construct(r))
                .flat_map(|e| e.attrs)
                .filter_map(|e| if e.0 == config.group_attribute {
                    Some(e.1)
                } else {
                    None
                })
                .flat_map(|e| e)
                .filter(|e| {
                    e.to_lowercase().ends_with(
                        &config.group_base_dn.to_lowercase(),
                    )
                })
                .filter_map(|e| if let Some(end) = e.find(",") {
                    Some(e[3..end].to_owned())
                } else {
                    None
                })
                .collect::<BTreeSet<_>>()
        })
        .map_err(|_| PamResultCode::PAM_SERVICE_ERR)
}