2011-09-25 10:56:03 +02:00
|
|
|
<?php
|
2017-12-04 04:38:03 +01:00
|
|
|
|
2011-09-25 10:56:03 +02:00
|
|
|
/**
|
|
|
|
* Name: LDAP Authenticate
|
|
|
|
* Description: Authenticate a user against an LDAP directory
|
2015-02-04 00:35:24 +01:00
|
|
|
* Version: 1.1
|
2011-09-25 10:56:03 +02:00
|
|
|
* Author: Mike Macgirvin <http://macgirvin.com/profile/mike>
|
2015-02-04 00:35:24 +01:00
|
|
|
* Author: aymhce
|
2011-09-25 10:56:03 +02:00
|
|
|
*/
|
2017-12-04 04:38:03 +01:00
|
|
|
|
2011-09-25 10:56:03 +02:00
|
|
|
/**
|
2012-01-31 12:06:20 +01:00
|
|
|
* Friendica addon
|
2017-12-04 04:38:03 +01:00
|
|
|
*
|
2011-09-25 10:56:03 +02:00
|
|
|
* Module: LDAP Authenticate
|
|
|
|
*
|
|
|
|
* Authenticate a user against an LDAP directory
|
|
|
|
* Useful for Windows Active Directory and other LDAP-based organisations
|
|
|
|
* to maintain a single password across the organisation.
|
|
|
|
*
|
|
|
|
* Optionally authenticates only if a member of a given group in the directory.
|
|
|
|
*
|
2017-12-04 04:38:03 +01:00
|
|
|
* By default, the person must have registered with Friendica using the normal registration
|
2012-01-31 12:06:20 +01:00
|
|
|
* procedures in order to have a Friendica user record, contact, and profile.
|
2015-02-04 00:35:24 +01:00
|
|
|
* However, it's possible with an option to automate the creation of a Friendica basic account.
|
2011-09-25 10:56:03 +02:00
|
|
|
*
|
|
|
|
* Note when using with Windows Active Directory: you may need to set TLS_CACERT in your site
|
2017-12-04 04:38:03 +01:00
|
|
|
* ldap.conf file to the signing cert for your LDAP server.
|
|
|
|
*
|
2018-11-25 08:13:45 +01:00
|
|
|
* The configuration options for this module may be set in the config/addon.config.php file
|
2011-09-25 10:56:03 +02:00
|
|
|
* e.g.:
|
|
|
|
*
|
2018-06-28 05:14:29 +02:00
|
|
|
* [ldapauth]
|
|
|
|
* ; ldap hostname server - required
|
|
|
|
* ldap_server = host.example.com
|
|
|
|
* ; dn to search users - required
|
|
|
|
* ldap_searchdn = ou=users,dc=example,dc=com
|
|
|
|
* ; attribute to find username - required
|
|
|
|
* ldap_userattr = uid
|
2015-02-04 16:55:07 +01:00
|
|
|
*
|
2018-06-28 05:14:29 +02:00
|
|
|
* ; admin dn - optional - only if ldap server dont have anonymous access
|
|
|
|
* ldap_binddn = cn=admin,dc=example,dc=com
|
|
|
|
* ; admin password - optional - only if ldap server dont have anonymous access
|
|
|
|
* ldap_bindpw = password
|
2011-09-25 10:56:03 +02:00
|
|
|
*
|
2018-06-28 05:14:29 +02:00
|
|
|
* ; for create Friendica account if user exist in ldap
|
|
|
|
* ; required an email and a simple (beautiful) nickname on user ldap object
|
|
|
|
* ; active account creation - optional - default none
|
|
|
|
* ldap_autocreateaccount = true
|
|
|
|
* ; attribute to get email - optional - default : 'mail'
|
|
|
|
* ldap_autocreateaccount_emailattribute = mail
|
|
|
|
* ; attribute to get nickname - optional - default : 'givenName'
|
|
|
|
* ldap_autocreateaccount_nameattribute = cn
|
2015-02-04 16:55:07 +01:00
|
|
|
*
|
|
|
|
* ...etc.
|
2011-09-25 10:56:03 +02:00
|
|
|
*/
|
2019-02-03 22:22:03 +01:00
|
|
|
|
2017-11-07 00:55:24 +01:00
|
|
|
use Friendica\Core\Config;
|
2018-12-26 08:28:16 +01:00
|
|
|
use Friendica\Core\Hook;
|
2018-10-30 00:40:18 +01:00
|
|
|
use Friendica\Core\Logger;
|
2017-12-04 04:38:52 +01:00
|
|
|
use Friendica\Model\User;
|
2019-06-23 19:56:21 +02:00
|
|
|
use Friendica\Util\ConfigFileLoader;
|
2017-11-07 00:55:24 +01:00
|
|
|
|
2017-12-04 04:38:03 +01:00
|
|
|
function ldapauth_install()
|
|
|
|
{
|
2018-12-26 08:28:16 +01:00
|
|
|
Hook::register('load_config', 'addon/ldapauth/ldapauth.php', 'ldapauth_load_config');
|
|
|
|
Hook::register('authenticate', 'addon/ldapauth/ldapauth.php', 'ldapauth_hook_authenticate');
|
2011-09-25 10:56:03 +02:00
|
|
|
}
|
|
|
|
|
2017-12-04 04:38:03 +01:00
|
|
|
function ldapauth_uninstall()
|
|
|
|
{
|
2018-12-26 08:28:16 +01:00
|
|
|
Hook::unregister('load_config', 'addon/ldapauth/ldapauth.php', 'ldapauth_load_config');
|
|
|
|
Hook::unregister('authenticate', 'addon/ldapauth/ldapauth.php', 'ldapauth_hook_authenticate');
|
2018-06-28 05:14:29 +02:00
|
|
|
}
|
|
|
|
|
2019-03-24 12:54:26 +01:00
|
|
|
function ldapauth_load_config(\Friendica\App $a, ConfigFileLoader $loader)
|
2018-06-28 05:14:29 +02:00
|
|
|
{
|
2019-02-10 20:10:59 +01:00
|
|
|
$a->getConfigCache()->load($loader->loadAddonConfig('ldapauth'));
|
2011-09-25 10:56:03 +02:00
|
|
|
}
|
|
|
|
|
2017-12-04 04:38:03 +01:00
|
|
|
function ldapauth_hook_authenticate($a, &$b)
|
|
|
|
{
|
|
|
|
if (ldapauth_authenticate($b['username'], $b['password'])) {
|
|
|
|
$results = get_existing_account($b['username']);
|
|
|
|
if (!empty($results)) {
|
|
|
|
$b['user_record'] = $results[0];
|
|
|
|
$b['authenticated'] = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
2011-09-25 10:56:03 +02:00
|
|
|
}
|
|
|
|
|
2017-12-04 04:38:03 +01:00
|
|
|
function ldapauth_authenticate($username, $password)
|
|
|
|
{
|
|
|
|
$ldap_server = Config::get('ldapauth', 'ldap_server');
|
|
|
|
$ldap_binddn = Config::get('ldapauth', 'ldap_binddn');
|
|
|
|
$ldap_bindpw = Config::get('ldapauth', 'ldap_bindpw');
|
|
|
|
$ldap_searchdn = Config::get('ldapauth', 'ldap_searchdn');
|
|
|
|
$ldap_userattr = Config::get('ldapauth', 'ldap_userattr');
|
|
|
|
$ldap_group = Config::get('ldapauth', 'ldap_group');
|
|
|
|
$ldap_autocreateaccount = Config::get('ldapauth', 'ldap_autocreateaccount');
|
|
|
|
$ldap_autocreateaccount_emailattribute = Config::get('ldapauth', 'ldap_autocreateaccount_emailattribute');
|
|
|
|
$ldap_autocreateaccount_nameattribute = Config::get('ldapauth', 'ldap_autocreateaccount_nameattribute');
|
|
|
|
|
2017-12-04 08:47:08 +01:00
|
|
|
if (!(strlen($password) && function_exists('ldap_connect') && strlen($ldap_server))) {
|
2018-10-30 00:40:18 +01:00
|
|
|
Logger::log("ldapauth: not configured or missing php-ldap module");
|
2017-12-04 04:38:03 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$connect = @ldap_connect($ldap_server);
|
|
|
|
|
|
|
|
if ($connect === false) {
|
2018-10-30 00:40:18 +01:00
|
|
|
Logger::log("ldapauth: could not connect to $ldap_server");
|
2017-12-04 04:38:03 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@ldap_set_option($connect, LDAP_OPT_PROTOCOL_VERSION, 3);
|
|
|
|
@ldap_set_option($connect, LDAP_OPT_REFERRALS, 0);
|
|
|
|
if ((@ldap_bind($connect, $ldap_binddn, $ldap_bindpw)) === false) {
|
2018-10-30 00:40:18 +01:00
|
|
|
Logger::log("ldapauth: could not bind $ldap_server as $ldap_binddn");
|
2017-12-04 04:38:03 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$res = @ldap_search($connect, $ldap_searchdn, $ldap_userattr . '=' . $username);
|
|
|
|
|
|
|
|
if (!$res) {
|
2018-10-30 00:40:18 +01:00
|
|
|
Logger::log("ldapauth: $ldap_userattr=$username,$ldap_searchdn not found");
|
2017-12-04 04:38:03 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$id = @ldap_first_entry($connect, $res);
|
|
|
|
|
|
|
|
if (!$id) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$dn = @ldap_get_dn($connect, $id);
|
|
|
|
|
|
|
|
if (!@ldap_bind($connect, $dn, $password)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$emailarray = [];
|
|
|
|
$namearray = [];
|
|
|
|
if ($ldap_autocreateaccount == "true") {
|
|
|
|
if (!strlen($ldap_autocreateaccount_emailattribute)) {
|
|
|
|
$ldap_autocreateaccount_emailattribute = "mail";
|
|
|
|
}
|
|
|
|
if (!strlen($ldap_autocreateaccount_nameattribute)) {
|
|
|
|
$ldap_autocreateaccount_nameattribute = "givenName";
|
|
|
|
}
|
|
|
|
$emailarray = @ldap_get_values($connect, $id, $ldap_autocreateaccount_emailattribute);
|
|
|
|
$namearray = @ldap_get_values($connect, $id, $ldap_autocreateaccount_nameattribute);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!strlen($ldap_group)) {
|
|
|
|
ldap_autocreateaccount($ldap_autocreateaccount, $username, $password, $emailarray[0], $namearray[0]);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$r = @ldap_compare($connect, $ldap_group, 'member', $dn);
|
|
|
|
if ($r === -1) {
|
|
|
|
$err = @ldap_error($connect);
|
|
|
|
$eno = @ldap_errno($connect);
|
|
|
|
@ldap_close($connect);
|
|
|
|
|
|
|
|
if ($eno === 32) {
|
2018-10-30 00:40:18 +01:00
|
|
|
Logger::log("ldapauth: access control group Does Not Exist");
|
2017-12-04 04:38:03 +01:00
|
|
|
return false;
|
|
|
|
} elseif ($eno === 16) {
|
2018-10-30 00:40:18 +01:00
|
|
|
Logger::log('ldapauth: membership attribute does not exist in access control group');
|
2017-12-04 04:38:03 +01:00
|
|
|
return false;
|
|
|
|
} else {
|
2018-10-30 00:40:18 +01:00
|
|
|
Logger::log('ldapauth: error: ' . $err);
|
2017-12-04 04:38:03 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} elseif ($r === false) {
|
|
|
|
@ldap_close($connect);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
ldap_autocreateaccount($ldap_autocreateaccount, $username, $password, $emailarray[0], $namearray[0]);
|
|
|
|
return true;
|
2011-09-25 10:56:03 +02:00
|
|
|
}
|
2015-02-04 00:35:24 +01:00
|
|
|
|
2017-12-04 04:38:03 +01:00
|
|
|
function ldap_autocreateaccount($ldap_autocreateaccount, $username, $password, $email, $name)
|
|
|
|
{
|
|
|
|
if ($ldap_autocreateaccount == "true") {
|
|
|
|
$results = get_existing_account($username);
|
|
|
|
if (empty($results)) {
|
|
|
|
if (strlen($email) > 0 && strlen($name) > 0) {
|
2018-01-15 14:15:33 +01:00
|
|
|
$arr = ['username' => $name, 'nickname' => $username, 'email' => $email, 'password' => $password, 'verified' => 1];
|
2017-12-13 20:01:25 +01:00
|
|
|
|
|
|
|
try {
|
|
|
|
User::create($arr);
|
2018-10-30 00:40:18 +01:00
|
|
|
Logger::log("ldapauth: account " . $username . " created");
|
2017-12-13 20:01:25 +01:00
|
|
|
} catch (Exception $ex) {
|
2018-10-30 00:40:18 +01:00
|
|
|
Logger::log("ldapauth: account " . $username . " was not created ! : " . $ex->getMessage());
|
2017-12-04 04:38:03 +01:00
|
|
|
}
|
|
|
|
} else {
|
2018-10-30 00:40:18 +01:00
|
|
|
Logger::log("ldapauth: unable to create account, no email or nickname found");
|
2017-12-04 04:38:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-02-04 00:35:24 +01:00
|
|
|
}
|
|
|
|
|
2017-12-04 04:38:03 +01:00
|
|
|
function get_existing_account($username)
|
|
|
|
{
|
|
|
|
return q("SELECT * FROM `user` WHERE `nickname` = '%s' AND `blocked` = 0 AND `verified` = 1 LIMIT 1", $username);
|
2015-02-04 00:35:24 +01:00
|
|
|
}
|