]> git.mxchange.org Git - friendica-addons.git/blob - ldapauth/ldapauth.php
Fix ldapauth formatting
[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
56 require_once('include/user.php');
57
58 use Friendica\Core\Config;
59
60 function ldapauth_install()
61 {
62         register_hook('authenticate', 'addon/ldapauth/ldapauth.php', 'ldapauth_hook_authenticate');
63 }
64
65 function ldapauth_uninstall()
66 {
67         unregister_hook('authenticate', 'addon/ldapauth/ldapauth.php', 'ldapauth_hook_authenticate');
68 }
69
70 function ldapauth_hook_authenticate($a, &$b)
71 {
72         if (ldapauth_authenticate($b['username'], $b['password'])) {
73                 $results = get_existing_account($b['username']);
74                 if (!empty($results)) {
75                         $b['user_record'] = $results[0];
76                         $b['authenticated'] = 1;
77                 }
78         }
79         return;
80 }
81
82 function ldapauth_authenticate($username, $password)
83 {
84         $ldap_server   = Config::get('ldapauth', 'ldap_server');
85         $ldap_binddn   = Config::get('ldapauth', 'ldap_binddn');
86         $ldap_bindpw   = Config::get('ldapauth', 'ldap_bindpw');
87         $ldap_searchdn = Config::get('ldapauth', 'ldap_searchdn');
88         $ldap_userattr = Config::get('ldapauth', 'ldap_userattr');
89         $ldap_group    = Config::get('ldapauth', 'ldap_group');
90         $ldap_autocreateaccount = Config::get('ldapauth', 'ldap_autocreateaccount');
91         $ldap_autocreateaccount_emailattribute = Config::get('ldapauth', 'ldap_autocreateaccount_emailattribute');
92         $ldap_autocreateaccount_nameattribute  = Config::get('ldapauth', 'ldap_autocreateaccount_nameattribute');
93
94         if (!((strlen($password)) && (function_exists('ldap_connect')) && (strlen($ldap_server)))) {
95                 logger("ldapauth: not configured or missing php-ldap module");
96                 return false;
97         }
98
99         $connect = @ldap_connect($ldap_server);
100
101         if ($connect === false) {
102                 logger("ldapauth: could not connect to $ldap_server");
103                 return false;
104         }
105
106         @ldap_set_option($connect, LDAP_OPT_PROTOCOL_VERSION, 3);
107         @ldap_set_option($connect, LDAP_OPT_REFERRALS, 0);
108         if ((@ldap_bind($connect, $ldap_binddn, $ldap_bindpw)) === false) {
109                 logger("ldapauth: could not bind $ldap_server as $ldap_binddn");
110                 return false;
111         }
112
113         $res = @ldap_search($connect, $ldap_searchdn, $ldap_userattr . '=' . $username);
114
115         if (!$res) {
116                 logger("ldapauth: $ldap_userattr=$username,$ldap_searchdn not found");
117                 return false;
118         }
119
120         $id = @ldap_first_entry($connect, $res);
121
122         if (!$id) {
123                 return false;
124         }
125
126         $dn = @ldap_get_dn($connect, $id);
127
128         if (!@ldap_bind($connect, $dn, $password)) {
129                 return false;
130         }
131
132         $emailarray = [];
133         $namearray = [];
134         if ($ldap_autocreateaccount == "true") {
135                 if (!strlen($ldap_autocreateaccount_emailattribute)) {
136                         $ldap_autocreateaccount_emailattribute = "mail";
137                 }
138                 if (!strlen($ldap_autocreateaccount_nameattribute)) {
139                         $ldap_autocreateaccount_nameattribute = "givenName";
140                 }
141                 $emailarray = @ldap_get_values($connect, $id, $ldap_autocreateaccount_emailattribute);
142                 $namearray = @ldap_get_values($connect, $id, $ldap_autocreateaccount_nameattribute);
143         }
144
145         if (!strlen($ldap_group)) {
146                 ldap_autocreateaccount($ldap_autocreateaccount, $username, $password, $emailarray[0], $namearray[0]);
147                 return true;
148         }
149
150         $r = @ldap_compare($connect, $ldap_group, 'member', $dn);
151         if ($r === -1) {
152                 $err = @ldap_error($connect);
153                 $eno = @ldap_errno($connect);
154                 @ldap_close($connect);
155
156                 if ($eno === 32) {
157                         logger("ldapauth: access control group Does Not Exist");
158                         return false;
159                 } elseif ($eno === 16) {
160                         logger('ldapauth: membership attribute does not exist in access control group');
161                         return false;
162                 } else {
163                         logger('ldapauth: error: ' . $err);
164                         return false;
165                 }
166         } elseif ($r === false) {
167                 @ldap_close($connect);
168                 return false;
169         }
170
171         ldap_autocreateaccount($ldap_autocreateaccount, $username, $password, $emailarray[0], $namearray[0]);
172         return true;
173 }
174
175 function ldap_autocreateaccount($ldap_autocreateaccount, $username, $password, $email, $name)
176 {
177         if ($ldap_autocreateaccount == "true") {
178                 $results = get_existing_account($username);
179                 if (empty($results)) {
180                         if (strlen($email) > 0 && strlen($name) > 0) {
181                                 $arr = array('username' => $name, 'nickname' => $username, 'email' => $email, 'password' => $password, 'verified' => 1);
182                                 $result = create_user($arr);
183                                 if ($result['success']) {
184                                         logger("ldapauth: account " . $username . " created");
185                                 } else {
186                                         logger("ldapauth: account " . $username . " was not created ! : " . implode($result));
187                                 }
188                         } else {
189                                 logger("ldapauth: unable to create account, no email or nickname found");
190                         }
191                 }
192         }
193 }
194
195 function get_existing_account($username)
196 {
197         return q("SELECT * FROM `user` WHERE `nickname` = '%s' AND `blocked` = 0 AND `verified` = 1 LIMIT 1", $username);
198 }