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