Use short form array syntax everywhere
[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 .htconfig.php file
30  * e.g.:
31  *
32  * // ldap hostname server - required
33  * $a->config['ldapauth']['ldap_server'] = 'host.example.com';
34  * // dn to search users - required
35  * $a->config['ldapauth']['ldap_searchdn'] = 'ou=users,dc=example,dc=com';
36  * // attribute to find username - required
37  * $a->config['ldapauth']['ldap_userattr'] = 'uid';
38  *
39  * // admin dn - optional - only if ldap server dont have anonymous access
40  * $a->config['ldapauth']['ldap_binddn'] = 'cn=admin,dc=example,dc=com';
41  * // admin password - optional - only if ldap server dont have anonymous access
42  * $a->config['ldapauth']['ldap_bindpw'] = 'password';
43  *
44  * // for create Friendica account if user exist in ldap
45  * //     required an email and a simple (beautiful) nickname on user ldap object
46  * //   active account creation - optional - default none
47  * $a->config['ldapauth']['ldap_autocreateaccount'] = 'true';
48  * //   attribute to get email - optional - default : 'mail'
49  * $a->config['ldapauth']['ldap_autocreateaccount_emailattribute'] = 'mail';
50  * //   attribute to get nickname - optional - default : 'givenName'
51  * $a->config['ldapauth']['ldap_autocreateaccount_nameattribute'] = 'cn';
52  *
53  * ...etc.
54  */
55 use Friendica\Core\Config;
56 use Friendica\Model\User;
57
58 function ldapauth_install()
59 {
60         register_hook('authenticate', 'addon/ldapauth/ldapauth.php', 'ldapauth_hook_authenticate');
61 }
62
63 function ldapauth_uninstall()
64 {
65         unregister_hook('authenticate', 'addon/ldapauth/ldapauth.php', 'ldapauth_hook_authenticate');
66 }
67
68 function ldapauth_hook_authenticate($a, &$b)
69 {
70         if (ldapauth_authenticate($b['username'], $b['password'])) {
71                 $results = get_existing_account($b['username']);
72                 if (!empty($results)) {
73                         $b['user_record'] = $results[0];
74                         $b['authenticated'] = 1;
75                 }
76         }
77         return;
78 }
79
80 function ldapauth_authenticate($username, $password)
81 {
82         $ldap_server   = Config::get('ldapauth', 'ldap_server');
83         $ldap_binddn   = Config::get('ldapauth', 'ldap_binddn');
84         $ldap_bindpw   = Config::get('ldapauth', 'ldap_bindpw');
85         $ldap_searchdn = Config::get('ldapauth', 'ldap_searchdn');
86         $ldap_userattr = Config::get('ldapauth', 'ldap_userattr');
87         $ldap_group    = Config::get('ldapauth', 'ldap_group');
88         $ldap_autocreateaccount = Config::get('ldapauth', 'ldap_autocreateaccount');
89         $ldap_autocreateaccount_emailattribute = Config::get('ldapauth', 'ldap_autocreateaccount_emailattribute');
90         $ldap_autocreateaccount_nameattribute  = Config::get('ldapauth', 'ldap_autocreateaccount_nameattribute');
91
92         if (!(strlen($password) && function_exists('ldap_connect') && strlen($ldap_server))) {
93                 logger("ldapauth: not configured or missing php-ldap module");
94                 return false;
95         }
96
97         $connect = @ldap_connect($ldap_server);
98
99         if ($connect === false) {
100                 logger("ldapauth: could not connect to $ldap_server");
101                 return false;
102         }
103
104         @ldap_set_option($connect, LDAP_OPT_PROTOCOL_VERSION, 3);
105         @ldap_set_option($connect, LDAP_OPT_REFERRALS, 0);
106         if ((@ldap_bind($connect, $ldap_binddn, $ldap_bindpw)) === false) {
107                 logger("ldapauth: could not bind $ldap_server as $ldap_binddn");
108                 return false;
109         }
110
111         $res = @ldap_search($connect, $ldap_searchdn, $ldap_userattr . '=' . $username);
112
113         if (!$res) {
114                 logger("ldapauth: $ldap_userattr=$username,$ldap_searchdn not found");
115                 return false;
116         }
117
118         $id = @ldap_first_entry($connect, $res);
119
120         if (!$id) {
121                 return false;
122         }
123
124         $dn = @ldap_get_dn($connect, $id);
125
126         if (!@ldap_bind($connect, $dn, $password)) {
127                 return false;
128         }
129
130         $emailarray = [];
131         $namearray = [];
132         if ($ldap_autocreateaccount == "true") {
133                 if (!strlen($ldap_autocreateaccount_emailattribute)) {
134                         $ldap_autocreateaccount_emailattribute = "mail";
135                 }
136                 if (!strlen($ldap_autocreateaccount_nameattribute)) {
137                         $ldap_autocreateaccount_nameattribute = "givenName";
138                 }
139                 $emailarray = @ldap_get_values($connect, $id, $ldap_autocreateaccount_emailattribute);
140                 $namearray = @ldap_get_values($connect, $id, $ldap_autocreateaccount_nameattribute);
141         }
142
143         if (!strlen($ldap_group)) {
144                 ldap_autocreateaccount($ldap_autocreateaccount, $username, $password, $emailarray[0], $namearray[0]);
145                 return true;
146         }
147
148         $r = @ldap_compare($connect, $ldap_group, 'member', $dn);
149         if ($r === -1) {
150                 $err = @ldap_error($connect);
151                 $eno = @ldap_errno($connect);
152                 @ldap_close($connect);
153
154                 if ($eno === 32) {
155                         logger("ldapauth: access control group Does Not Exist");
156                         return false;
157                 } elseif ($eno === 16) {
158                         logger('ldapauth: membership attribute does not exist in access control group');
159                         return false;
160                 } else {
161                         logger('ldapauth: error: ' . $err);
162                         return false;
163                 }
164         } elseif ($r === false) {
165                 @ldap_close($connect);
166                 return false;
167         }
168
169         ldap_autocreateaccount($ldap_autocreateaccount, $username, $password, $emailarray[0], $namearray[0]);
170         return true;
171 }
172
173 function ldap_autocreateaccount($ldap_autocreateaccount, $username, $password, $email, $name)
174 {
175         if ($ldap_autocreateaccount == "true") {
176                 $results = get_existing_account($username);
177                 if (empty($results)) {
178                         if (strlen($email) > 0 && strlen($name) > 0) {
179                                 $arr = ['username' => $name, 'nickname' => $username, 'email' => $email, 'password' => $password, 'verified' => 1];
180
181                                 try {
182                                         User::create($arr);
183                                         logger("ldapauth: account " . $username . " created");
184                                 } catch (Exception $ex) {
185                                         logger("ldapauth: account " . $username . " was not created ! : " . $ex->getMessage());
186                                 }
187                         } else {
188                                 logger("ldapauth: unable to create account, no email or nickname found");
189                         }
190                 }
191         }
192 }
193
194 function get_existing_account($username)
195 {
196         return q("SELECT * FROM `user` WHERE `nickname` = '%s' AND `blocked` = 0 AND `verified` = 1 LIMIT 1", $username);
197 }