X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;ds=sidebyside;f=ldapauth%2Fldapauth.php;h=514fb1d1a89ade5565a827ce1292409e7cfec57e;hb=6bc8103ca2b341b4a7d15996a64502ac00d5c626;hp=b87f669d5f5c7b0895c7784344a5507b3ef816a4;hpb=86a45576b2b15a0be18bb0708d88cf022cdcf4e6;p=friendica-addons.git diff --git a/ldapauth/ldapauth.php b/ldapauth/ldapauth.php old mode 100755 new mode 100644 index b87f669d..514fb1d1 --- a/ldapauth/ldapauth.php +++ b/ldapauth/ldapauth.php @@ -1,14 +1,16 @@ + * Author: aymhce */ - + /** * Friendica addon - * + * * Module: LDAP Authenticate * * Authenticate a user against an LDAP directory @@ -17,114 +19,188 @@ * * Optionally authenticates only if a member of a given group in the directory. * - * The person must have registered with Friendica using the normal registration + * By default, the person must have registered with Friendica using the normal registration * procedures in order to have a Friendica user record, contact, and profile. + * However, it's possible with an option to automate the creation of a Friendica basic account. * * Note when using with Windows Active Directory: you may need to set TLS_CACERT in your site - * ldap.conf file to the signing cert for your LDAP server. - * - * The required configuration options for this module may be set in the .htconfig.php file + * ldap.conf file to the signing cert for your LDAP server. + * + * The configuration options for this module may be set in the config/addon.ini.php file * e.g.: * - * $a->config['ldapauth']['ldap_server'] = 'host.example.com'; - * ...etc. + * [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 * + * ; 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 + * + * ; 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 + * + * ...etc. */ +use Friendica\Core\Addon; +use Friendica\Core\Config; +use Friendica\Model\User; + +function ldapauth_install() +{ + Addon::registerHook('load_config', 'addon/ldapauth/ldapauth.php', 'ldapauth_load_config'); + Addon::registerHook('authenticate', 'addon/ldapauth/ldapauth.php', 'ldapauth_hook_authenticate'); +} +function ldapauth_uninstall() +{ + Addon::unregisterHook('load_config', 'addon/ldapauth/ldapauth.php', 'ldapauth_load_config'); + Addon::unregisterHook('authenticate', 'addon/ldapauth/ldapauth.php', 'ldapauth_hook_authenticate'); +} +function ldapauth_load_config(\Friendica\App $a) +{ + $a->loadConfigFile(__DIR__. '/config/ldapauth.ini.php'); +} -function ldapauth_install() { - register_hook('authenticate', 'addon/ldapauth/ldapauth.php', 'ldapauth_hook_authenticate'); +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; } +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'); + + if (!(strlen($password) && function_exists('ldap_connect') && strlen($ldap_server))) { + logger("ldapauth: not configured or missing php-ldap module"); + return false; + } + + $connect = @ldap_connect($ldap_server); -function ldapauth_uninstall() { - unregister_hook('authenticate', 'addon/ldapauth/ldapauth.php', 'ldapauth_hook_authenticate'); -} + if ($connect === false) { + logger("ldapauth: could not connect to $ldap_server"); + 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) { + logger("ldapauth: could not bind $ldap_server as $ldap_binddn"); + return false; + } + $res = @ldap_search($connect, $ldap_searchdn, $ldap_userattr . '=' . $username); -function ldapauth_hook_authenticate($a,&$b) { - if(ldapauth_authenticate($b['username'],$b['password'])) { - $results = q("SELECT * FROM `user` WHERE `nickname` = '%s' AND `blocked` = 0 AND `verified` = 1 LIMIT 1", - dbesc($b['username']) - ); - if(count($results)) { - $b['user_record'] = $results[0]; - $b['authenticated'] = 1; + if (!$res) { + logger("ldapauth: $ldap_userattr=$username,$ldap_searchdn not found"); + 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); } - return; + + 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) { + logger("ldapauth: access control group Does Not Exist"); + return false; + } elseif ($eno === 16) { + logger('ldapauth: membership attribute does not exist in access control group'); + return false; + } else { + logger('ldapauth: error: ' . $err); + return false; + } + } elseif ($r === false) { + @ldap_close($connect); + return false; + } + + ldap_autocreateaccount($ldap_autocreateaccount, $username, $password, $emailarray[0], $namearray[0]); + return true; } +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) { + $arr = ['username' => $name, 'nickname' => $username, 'email' => $email, 'password' => $password, 'verified' => 1]; + + try { + User::create($arr); + logger("ldapauth: account " . $username . " created"); + } catch (Exception $ex) { + logger("ldapauth: account " . $username . " was not created ! : " . $ex->getMessage()); + } + } else { + logger("ldapauth: unable to create account, no email or nickname found"); + } + } + } +} -function ldapauth_authenticate($username,$password) { - - $ldap_server = get_config('ldapauth','ldap_server'); - $ldap_binddn = get_config('ldapauth','ldap_binddn'); - $ldap_bindpw = get_config('ldapauth','ldap_bindpw'); - $ldap_searchdn = get_config('ldapauth','ldap_searchdn'); - $ldap_userattr = get_config('ldapauth','ldap_userattr'); - $ldap_group = get_config('ldapauth','ldap_group'); - - if(! ((strlen($password)) - && (function_exists('ldap_connect')) - && (strlen($ldap_server)))) - return false; - - $connect = @ldap_connect($ldap_server); - - if(! $connect) - 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) { - return false; - } - - $res = @ldap_search($connect,$ldap_searchdn, $ldap_userattr . '=' . $username); - - if(! $res) { - 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; - - if(! strlen($ldap_group)) - 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) { - logger("ldapauth: access control group Does Not Exist"); - return false; - } - elseif ($eno === 16) { - logger('ldapauth: membership attribute does not exist in access control group'); - return false; - } - else { - logger('ldapauth: error: ' . $err); - return false; - } - } - elseif ($r === false) { - @ldap_close($connect); - return false; - } - - return true; +function get_existing_account($username) +{ + return q("SELECT * FROM `user` WHERE `nickname` = '%s' AND `blocked` = 0 AND `verified` = 1 LIMIT 1", $username); }