friendica-addons/ldapauth/ldapauth.php

185 lines
6.3 KiB
PHP
Raw Normal View History

2011-09-25 04:56:03 -04:00
<?php
/**
* Name: LDAP Authenticate
* Description: Authenticate a user against an LDAP directory
* Version: 1.1
2011-09-25 04:56:03 -04:00
* Author: Mike Macgirvin <http://macgirvin.com/profile/mike>
* Author: aymhce
2011-09-25 04:56:03 -04:00
*/
/**
2012-01-31 06:06:20 -05:00
* Friendica addon
2011-09-25 04:56:03 -04: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.
*
* By default, the person must have registered with Friendica using the normal registration
2012-01-31 06:06:20 -05:00
* 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.
2011-09-25 04:56:03 -04:00
*
* 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.
*
2015-02-04 10:55:07 -05:00
* The configuration options for this module may be set in the .htconfig.php file
2011-09-25 04:56:03 -04:00
* e.g.:
*
2015-02-04 10:55:07 -05:00
* // ldap hostname server - required
2011-09-25 04:56:03 -04:00
* $a->config['ldapauth']['ldap_server'] = 'host.example.com';
2015-02-04 10:55:07 -05:00
* // dn to search users - required
* $a->config['ldapauth']['ldap_searchdn'] = 'ou=users,dc=example,dc=com';
* // attribute to find username - required
* $a->config['ldapauth']['ldap_userattr'] = 'uid';
*
* // admin dn - optional - only if ldap server dont have anonymous access
* $a->config['ldapauth']['ldap_binddn'] = 'cn=admin,dc=example,dc=com';
* // admin password - optional - only if ldap server dont have anonymous access
* $a->config['ldapauth']['ldap_bindpw'] = 'password';
2011-09-25 04:56:03 -04:00
*
2015-02-04 10:55:07 -05: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
* $a->config['ldapauth']['ldap_autocreateaccount'] = 'true';
* // attribute to get email - optional - default : 'mail'
* $a->config['ldapauth']['ldap_autocreateaccount_emailattribute'] = 'mail';
* // attribute to get nickname - optional - default : 'givenName'
* $a->config['ldapauth']['ldap_autocreateaccount_nameattribute'] = 'givenName';
*
* ...etc.
2011-09-25 04:56:03 -04:00
*/
require_once('include/user.php');
2011-09-25 04:56:03 -04:00
function ldapauth_install() {
register_hook('authenticate', 'addon/ldapauth/ldapauth.php', 'ldapauth_hook_authenticate');
}
function ldapauth_uninstall() {
unregister_hook('authenticate', 'addon/ldapauth/ldapauth.php', 'ldapauth_hook_authenticate');
}
function ldapauth_hook_authenticate($a,&$b) {
if(ldapauth_authenticate($b['username'],$b['password']) && is_existing_account($b['username'])) {
$b['user_record'] = $results[0];
$b['authenticated'] = 1;
2011-09-25 04:56:03 -04:00
}
return;
}
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');
$ldap_autocreateaccount = get_config('ldapauth','ldap_autocreateaccount');
$ldap_autocreateaccount_emailattribute = get_config('ldapauth','ldap_autocreateaccount_emailattribute');
$ldap_autocreateaccount_nameattribute = get_config('ldapauth','ldap_autocreateaccount_nameattribute');
if(! strlen($ldap_autocreateaccount_emailattribute))
$ldap_autocreateaccount_emailattribute = "mail";
if(! strlen($ldap_autocreateaccount_nameattribute))
$ldap_autocreateaccount_nameattribute = "givenName";
2011-09-25 04:56:03 -04:00
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;
$emailarray = @ldap_get_values($connect, $id, $ldap_autocreateaccount_emailattribute);
$namearray = @ldap_get_values($connect, $id, $ldap_autocreateaccount_nameattribute);
2011-09-25 04:56:03 -04:00
if(! strlen($ldap_group)){
ldap_autocreateaccount($ldap_autocreateaccount,$username,$password,$emailarray[0],$namearray[0]);
2011-09-25 04:56:03 -04:00
return true;
}
2011-09-25 04:56:03 -04:00
$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]);
2011-09-25 04:56:03 -04:00
return true;
}
function ldap_autocreateaccount($ldap_autocreateaccount,$username,$password,$email,$name) {
if($ldap_autocreateaccount == "true" && !is_existing_account($username)){
if (strlen($email) > 0 && strlen($name) > 0){
$arr = array('username'=>$name,'nickname'=>$username,'email'=>$email,'password'=>$password,'verified'=>1);
$result = create_user($arr);
if ($result['success']){
logger("ldapauth: account " . $username . " created");
}else{
logger("ldapauth: account " . $username . " was not created ! : " . implode($result));
}
}else{
logger("ldapauth: unable to create account, no email or nickname found");
}
}
}
function is_existing_account($username){
$results = q("SELECT * FROM `user` WHERE `nickname` = '%s' AND `blocked` = 0 AND `verified` = 1 LIMIT 1",$username);
if(count($results)) {
return true;
}
return false;
}