<commit_before>'use strict';

exports._verify_user = function (userdn, passwd, cb) {
    var plugin = this;
    if (!this.pool) {
        throw new Error('LDAP Pool not found!');
    }
    this.pool._create_client(function (err, client) {
        if (err) {
            plugin.logdebug("Login failed, could not get connection: " + err);
            return cb(false);
        }
        client.bind(userdn, passwd, function(err) {
            if (err) {
                plugin.logdebug("Login failed, could not bind '" + userdn + "': " + err);
                return cb(false);
            }
            else {
                client.unbind();
                return cb(true);
            }
        });
    });
};

exports._get_search_conf = function(user) {
    var plugin = this;
    var filter = plugin.cfg.filter || '(&(objectclass=*)(uid=%u))';
    filter = filter.replace(/%u/g, user);
    var config = {
        basedn: this.pool.config.basedn,
        filter: filter,
        scope: plugin.cfg.scope || 'sub',
        attributes: ['dn', plugin.cfg.mail_attribute || 'mail']
    };
    if (config.basedn === undefined) {
        plugin.logerror("Undefined basedn. Please check your configuration!");
    }
    return config;
};

exports._get_dn_for_uid = function (uid, callback) {
    if (!this.pool) {
        throw new Error('LDAP Pool not found!');
    }
    var plugin = this;
    var onError = function(err) {
        plugin.logerror('Could not get DN for UID "' + uid + '": ' +  err);
        callback(err);
    };
    var dnSearch = function (err, client) {
        var config = plugin._get_search_conf(uid);
        if (err) {
            onError(err);
        }
        else {
            client.search(config.basedn, config, function(search_error, res) {
                if (search_error) { onError(search_error); }
                var userdn=[];
                res.on('searchEntry', function(entry) {
                    userdn.push(entry.object.dn);
                });
                res.on('error', onError);
                res.on('end', function() {
                    callback(null, userdn);
                });
            });
        }
    };
    this.pool.get(dnSearch);
};

exports.hook_capabilities = function (next, connection) {
    // Don't offer AUTH capabilities by default unless session is encrypted
    if (connection.using_tls) {
        var methods = [ 'LOGIN' ];
        connection.capabilities.push('AUTH LOGIN');
        connection.notes.allowed_auth_methods = methods;
    }
    next();
};

exports.register = function() {
    this.inherits('auth/auth_base');
    var plugin = this;
    plugin.register_hook('init_master',  'init_auth_ldap_ext');
    plugin.register_hook('init_child',   'init_auth_ldap_ext');
    var load_auth_ldap_ext_ini = function() {
        plugin.loginfo("loading auth_ext_ldap.ini");
        plugin.cfg = plugin.config.get('auth_ldap_ext.ini', 'ini', load_auth_ldap_ext_ini);
    };
    load_auth_ldap_ext_ini();<commit_msg>allow for PLAIN too<commit_after>'use strict';

exports._verify_user = function (userdn, passwd, cb) {
    var plugin = this;
    if (!this.pool) {
        throw new Error('LDAP Pool not found!');
    }
    this.pool._create_client(function (err, client) {
        if (err) {
            plugin.logdebug("Login failed, could not get connection: " + err);
            return cb(false);
        }
        client.bind(userdn, passwd, function(err) {
            if (err) {
                plugin.logdebug("Login failed, could not bind '" + userdn + "': " + err);
                return cb(false);
            }
            else {
                client.unbind();
                return cb(true);
            }
        });
    });
};

exports._get_search_conf = function(user) {
    var plugin = this;
    var filter = plugin.cfg.filter || '(&(objectclass=*)(uid=%u))';
    filter = filter.replace(/%u/g, user);
    var config = {
        basedn: this.pool.config.basedn,
        filter: filter,
        scope: plugin.cfg.scope || 'sub',
        attributes: ['dn', plugin.cfg.mail_attribute || 'mail']
    };
    if (config.basedn === undefined) {
        plugin.logerror("Undefined basedn. Please check your configuration!");
    }
    return config;
};

exports._get_dn_for_uid = function (uid, callback) {
    if (!this.pool) {
        throw new Error('LDAP Pool not found!');
    }
    var plugin = this;
    var onError = function(err) {
        plugin.logerror('Could not get DN for UID "' + uid + '": ' +  err);
        callback(err);
    };
    var dnSearch = function (err, client) {
        var config = plugin._get_search_conf(uid);
        if (err) {
            onError(err);
        }
        else {
            client.search(config.basedn, config, function(search_error, res) {
                if (search_error) { onError(search_error); }
                var userdn=[];
                res.on('searchEntry', function(entry) {
                    userdn.push(entry.object.dn);
                });
                res.on('error', onError);
                res.on('end', function() {
                    callback(null, userdn);
                });
            });
        }
    };
    this.pool.get(dnSearch);
};

exports.hook_capabilities = function (next, connection) {
    // Don't offer AUTH capabilities by default unless session is encrypted
    if (connection.using_tls) {
        var methods = [ 'PLAIN', 'LOGIN' ];
        connection.capabilities.push('AUTH ' + methods.join(' '));
        connection.notes.allowed_auth_methods = methods;
    }
    next();
};

exports.register = function() {
    this.inherits('auth/auth_base');
    var plugin = this;
    plugin.register_hook('init_master',  'init_auth_ldap_ext');
    plugin.register_hook('init_child',   'init_auth_ldap_ext');
    var load_auth_ldap_ext_ini = function() {
        plugin.loginfo("loading auth_ext_ldap.ini");
        plugin.cfg = plugin.config.get('auth_ldap_ext.ini', 'ini', load_auth_ldap_ext_ini);
    };
    load_auth_ldap_ext_ini();
{'fraction_non_alphanumeric': 0.13837209302325582, 'fraction_numerical': 0.0, 'mean_word_length': 2.070372259051504, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1183109', 'n_tokens_mistral': 1823, 'n_tokens_neox': 1789, 'n_words': 379}