]> git.mxchange.org Git - friendica.git/blob - addon/ldapauth/ldapauth.php
edit to display page
[friendica.git] / addon / ldapauth / ldapauth.php
1 <?php
2
3 /**
4  * Friendika addon
5  * 
6  * Module: LDAP Authenticate
7  *
8  * Authenticate a user against an LDAP directory
9  * Useful for Windows Active Directory and other LDAP-based organisations
10  * to maintain a single password across the organisation.
11  *
12  * Optionally authenticates only if a member of a given group in the directory.
13  *
14  * The person must have registered with Friendika using the normal registration 
15  * procedures in order to have a Friendika user record, contact, and profile.
16  *
17  * Note when using with Windows Active Directory: you may need to set TLS_CACERT in your site
18  * ldap.conf file to the signing cert for your LDAP server. 
19  * 
20  * The required configuration options for this module may be set in the .htconfig.php file
21  * e.g.:
22  *
23  * $a->config['ldapauth']['ldap_server'] = 'host.example.com';
24  * ...etc.
25  *
26  */
27
28
29
30 function ldapauth_install() {
31         register_hook('authenticate', 'addon/ldapauth/ldapauth.php', 'ldapauth_hook_authenticate');
32 }
33
34
35 function ldapauth_uninstall() {
36         unregister_hook('authenticate', 'addon/ldapauth/ldapauth.php', 'ldapauth_hook_authenticate');
37 }
38
39
40 function ldapauth_hook_authenticate($a,&$b) {
41         if(ldapauth_authenticate($b['username'],$b['password'])) {
42                 $results = q("SELECT * FROM `user` WHERE `nickname` = '%s' AND `blocked` = 0 AND `verified` = 1 LIMIT 1",
43                                 dbesc($b['username'])
44                 );
45                 if(count($results)) {
46                                 $b['user_record'] = $results[0];
47                                 $b['authenticated'] = 1;
48                 }
49         }
50         return;
51 }
52
53
54 function ldapauth_authenticate($username,$password) {
55
56     $ldap_server   = get_config('ldapauth','ldap_server');
57     $ldap_binddn   = get_config('ldapauth','ldap_binddn');
58     $ldap_bindpw   = get_config('ldapauth','ldap_bindpw');
59     $ldap_searchdn = get_config('ldapauth','ldap_searchdn');
60     $ldap_userattr = get_config('ldapauth','ldap_userattr');
61     $ldap_group    = get_config('ldapauth','ldap_group');
62
63     if(! ((strlen($password))
64             && (function_exists('ldap_connect'))
65             && (strlen($ldap_server))))
66             return false;
67
68     $connect = @ldap_connect($ldap_server);
69
70     if(! $connect)
71         return false;
72
73     @ldap_set_option($connect, LDAP_OPT_PROTOCOL_VERSION,3);
74     @ldap_set_option($connect, LDAP_OPT_REFERRALS, 0);
75     if((@ldap_bind($connect,$ldap_binddn,$ldap_bindpw)) === false) {
76         return false;
77     }
78
79     $res = @ldap_search($connect,$ldap_searchdn, $ldap_userattr . '=' . $username);
80
81     if(! $res) {
82         return false;
83     }
84
85     $id = @ldap_first_entry($connect,$res);
86
87     if(! $id) {
88         return false;
89     }
90
91     $dn = @ldap_get_dn($connect,$id);
92
93     if(! @ldap_bind($connect,$dn,$password))
94         return false;
95
96     if(! strlen($ldap_group))
97         return true;
98
99     $r = @ldap_compare($connect,$ldap_group,'member',$dn);
100     if ($r === -1) {
101         $err = @ldap_error($connect);
102         $eno = @ldap_errno($connect);
103         @ldap_close($connect);
104
105         if ($eno === 32) {
106             logger("ldapauth: access control group Does Not Exist");
107             return false;
108         }
109         elseif ($eno === 16) {
110             logger('ldapauth: membership attribute does not exist in access control group');
111             return false;
112         }
113         else {
114             logger('ldapauth: error: ' . $err);
115             return false;
116         }
117     }
118     elseif ($r === false) {
119         @ldap_close($connect);
120         return false;
121     }
122
123     return true;
124 }