]> git.mxchange.org Git - friendica-addons.git/blob - ldapauth/ldapauth.php
cf6b4b4a830ffebbd2c28be95ee74217132ff791
[friendica-addons.git] / ldapauth / ldapauth.php
1 <?php
2
3 /**
4  * Name: LDAP Authenticate
5  * Description: Authenticate a user against an LDAP directory
6  * Version: 1.1
7  * Author: Mike Macgirvin <http://macgirvin.com/profile/mike>
8  * Author: aymhce
9  */
10
11 /**
12  * Friendica addon
13  *
14  * Module: LDAP Authenticate
15  *
16  * Authenticate a user against an LDAP directory
17  * Useful for Windows Active Directory and other LDAP-based organisations
18  * to maintain a single password across the organisation.
19  *
20  * Optionally authenticates only if a member of a given group in the directory.
21  *
22  * By default, the person must have registered with Friendica using the normal registration
23  * procedures in order to have a Friendica user record, contact, and profile.
24  * However, it's possible with an option to automate the creation of a Friendica basic account.
25  *
26  * Note when using with Windows Active Directory: you may need to set TLS_CACERT in your site
27  * ldap.conf file to the signing cert for your LDAP server.
28  *
29  * The configuration options for this module may be set in the config/addon.config.php file
30  * e.g.:
31  *
32  * [ldapauth]
33  * ; ldap hostname server - required
34  * ldap_server = host.example.com
35  * ; dn to search users - required
36  * ldap_searchdn = ou=users,dc=example,dc=com
37  * ; attribute to find username - required
38  * ldap_userattr = uid
39  *
40  * ; admin dn - optional - only if ldap server dont have anonymous access
41  * ldap_binddn = cn=admin,dc=example,dc=com
42  * ; admin password - optional - only if ldap server dont have anonymous access
43  * ldap_bindpw = password
44  *
45  * ; for create Friendica account if user exist in ldap
46  * ;     required an email and a simple (beautiful) nickname on user ldap object
47  * ;   active account creation - optional - default none
48  * ldap_autocreateaccount = true
49  * ;   attribute to get email - optional - default : 'mail'
50  * ldap_autocreateaccount_emailattribute = mail
51  * ;   attribute to get nickname - optional - default : 'givenName'
52  * ldap_autocreateaccount_nameattribute = cn
53  *
54  * ...etc.
55  */
56
57 use Friendica\Core\Config;
58 use Friendica\Core\Hook;
59 use Friendica\Core\Logger;
60 use Friendica\Model\User;
61
62 function ldapauth_install()
63 {
64         Hook::register('load_config',  'addon/ldapauth/ldapauth.php', 'ldapauth_load_config');
65         Hook::register('authenticate', 'addon/ldapauth/ldapauth.php', 'ldapauth_hook_authenticate');
66 }
67
68 function ldapauth_uninstall()
69 {
70         Hook::unregister('load_config',  'addon/ldapauth/ldapauth.php', 'ldapauth_load_config');
71         Hook::unregister('authenticate', 'addon/ldapauth/ldapauth.php', 'ldapauth_hook_authenticate');
72 }
73
74 function ldapauth_load_config(\Friendica\App $a, Config\ConfigCacheLoader $loader)
75 {
76         $a->getConfig()->loadConfigArray($loader->loadConfigFile('ldapauth', true));
77 }
78
79 function ldapauth_hook_authenticate($a, &$b)
80 {
81         if (ldapauth_authenticate($b['username'], $b['password'])) {
82                 $results = get_existing_account($b['username']);
83                 if (!empty($results)) {
84                         $b['user_record'] = $results[0];
85                         $b['authenticated'] = 1;
86                 }
87         }
88         return;
89 }
90
91 function ldapauth_authenticate($username, $password)
92 {
93         $ldap_server   = Config::get('ldapauth', 'ldap_server');
94         $ldap_binddn   = Config::get('ldapauth', 'ldap_binddn');
95         $ldap_bindpw   = Config::get('ldapauth', 'ldap_bindpw');
96         $ldap_searchdn = Config::get('ldapauth', 'ldap_searchdn');
97         $ldap_userattr = Config::get('ldapauth', 'ldap_userattr');
98         $ldap_group    = Config::get('ldapauth', 'ldap_group');
99         $ldap_autocreateaccount = Config::get('ldapauth', 'ldap_autocreateaccount');
100         $ldap_autocreateaccount_emailattribute = Config::get('ldapauth', 'ldap_autocreateaccount_emailattribute');
101         $ldap_autocreateaccount_nameattribute  = Config::get('ldapauth', 'ldap_autocreateaccount_nameattribute');
102
103         if (!(strlen($password) && function_exists('ldap_connect') && strlen($ldap_server))) {
104                 Logger::log("ldapauth: not configured or missing php-ldap module");
105                 return false;
106         }
107
108         $connect = @ldap_connect($ldap_server);
109
110         if ($connect === false) {
111                 Logger::log("ldapauth: could not connect to $ldap_server");
112                 return false;
113         }
114
115         @ldap_set_option($connect, LDAP_OPT_PROTOCOL_VERSION, 3);
116         @ldap_set_option($connect, LDAP_OPT_REFERRALS, 0);
117         if ((@ldap_bind($connect, $ldap_binddn, $ldap_bindpw)) === false) {
118                 Logger::log("ldapauth: could not bind $ldap_server as $ldap_binddn");
119                 return false;
120         }
121
122         $res = @ldap_search($connect, $ldap_searchdn, $ldap_userattr . '=' . $username);
123
124         if (!$res) {
125                 Logger::log("ldapauth: $ldap_userattr=$username,$ldap_searchdn not found");
126                 return false;
127         }
128
129         $id = @ldap_first_entry($connect, $res);
130
131         if (!$id) {
132                 return false;
133         }
134
135         $dn = @ldap_get_dn($connect, $id);
136
137         if (!@ldap_bind($connect, $dn, $password)) {
138                 return false;
139         }
140
141         $emailarray = [];
142         $namearray = [];
143         if ($ldap_autocreateaccount == "true") {
144                 if (!strlen($ldap_autocreateaccount_emailattribute)) {
145                         $ldap_autocreateaccount_emailattribute = "mail";
146                 }
147                 if (!strlen($ldap_autocreateaccount_nameattribute)) {
148                         $ldap_autocreateaccount_nameattribute = "givenName";
149                 }
150                 $emailarray = @ldap_get_values($connect, $id, $ldap_autocreateaccount_emailattribute);
151                 $namearray = @ldap_get_values($connect, $id, $ldap_autocreateaccount_nameattribute);
152         }
153
154         if (!strlen($ldap_group)) {
155                 ldap_autocreateaccount($ldap_autocreateaccount, $username, $password, $emailarray[0], $namearray[0]);
156                 return true;
157         }
158
159         $r = @ldap_compare($connect, $ldap_group, 'member', $dn);
160         if ($r === -1) {
161                 $err = @ldap_error($connect);
162                 $eno = @ldap_errno($connect);
163                 @ldap_close($connect);
164
165                 if ($eno === 32) {
166                         Logger::log("ldapauth: access control group Does Not Exist");
167                         return false;
168                 } elseif ($eno === 16) {
169                         Logger::log('ldapauth: membership attribute does not exist in access control group');
170                         return false;
171                 } else {
172                         Logger::log('ldapauth: error: ' . $err);
173                         return false;
174                 }
175         } elseif ($r === false) {
176                 @ldap_close($connect);
177                 return false;
178         }
179
180         ldap_autocreateaccount($ldap_autocreateaccount, $username, $password, $emailarray[0], $namearray[0]);
181         return true;
182 }
183
184 function ldap_autocreateaccount($ldap_autocreateaccount, $username, $password, $email, $name)
185 {
186         if ($ldap_autocreateaccount == "true") {
187                 $results = get_existing_account($username);
188                 if (empty($results)) {
189                         if (strlen($email) > 0 && strlen($name) > 0) {
190                                 $arr = ['username' => $name, 'nickname' => $username, 'email' => $email, 'password' => $password, 'verified' => 1];
191
192                                 try {
193                                         User::create($arr);
194                                         Logger::log("ldapauth: account " . $username . " created");
195                                 } catch (Exception $ex) {
196                                         Logger::log("ldapauth: account " . $username . " was not created ! : " . $ex->getMessage());
197                                 }
198                         } else {
199                                 Logger::log("ldapauth: unable to create account, no email or nickname found");
200                         }
201                 }
202         }
203 }
204
205 function get_existing_account($username)
206 {
207         return q("SELECT * FROM `user` WHERE `nickname` = '%s' AND `blocked` = 0 AND `verified` = 1 LIMIT 1", $username);
208 }