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