Initial repo created
[timetracker.git] / WEB-INF / lib / auth / Auth_ldap.class.php
1 <?php
2 // +----------------------------------------------------------------------+
3 // | Anuko Time Tracker
4 // +----------------------------------------------------------------------+
5 // | Copyright (c) Anuko International Ltd. (https://www.anuko.com)
6 // +----------------------------------------------------------------------+
7 // | LIBERAL FREEWARE LICENSE: This source code document may be used
8 // | by anyone for any purpose, and freely redistributed alone or in
9 // | combination with other software, provided that the license is obeyed.
10 // |
11 // | There are only two ways to violate the license:
12 // |
13 // | 1. To redistribute this code in source form, with the copyright
14 // |    notice or license removed or altered. (Distributing in compiled
15 // |    forms without embedded copyright notices is permitted).
16 // |
17 // | 2. To redistribute modified versions of this code in *any* form
18 // |    that bears insufficient indications that the modifications are
19 // |    not the work of the original author(s).
20 // |
21 // | This license applies to this document only, not any other software
22 // | that it may be combined with.
23 // |
24 // +----------------------------------------------------------------------+
25 // | Contributors:
26 // | https://www.anuko.com/content/time_tracker/open_source/credits.htm
27 // +----------------------------------------------------------------------+
28
29 // NOTES:
30 //
31 // Auth_ldap.class.php was originally written for LDAP authentication with Windows Active Directory.
32 // It June 2011, it was extended to include support for OpenLDAP. The difference in the code is in the format
33 // of user identification that we pass to ldap_bind().
34 //
35 // Windows AD accepts username@domain.com while OpenLDAP needs something like "uid=username,ou=people,dc=domain,dc=com".
36 // Therefore, some branching in the code.
37 //
38 // In April 2012, a previously mandatory search for group membership was put in a conditional block (if ($member_of) -
39 // when mandatory membership in groups is actually defined in config.php).
40 // This made the module work with Sun Directory Server when NO GROUP MEMBERSHIP is specified.
41 // Note 1: search is likely to fail with Sun DS if 'member_of' => array()); is used in config.php.
42 // Note 2: search is likely to not work properly with OpenLDAP as well because of Windows specific filtering code in there
43 // (we are looking for matches for Windows-specific samaccountname property). Search needs to be redone during the next
44 // refactoring effort.
45
46
47 /**
48 * Auth_ldap class to authenticate users against an LDAP server (Windows AD, OpenLDAP, and others).
49 * @package TimeTracker
50 */
51 class Auth_ldap extends Auth {
52   var $params;
53
54   function Auth_ldap($params)
55   {
56     $this->params = $params;
57     if (isset($GLOBALS['smarty'])) {
58       $GLOBALS['smarty']->assign('Auth_ldap_params', $this->params);
59     }
60   }
61
62   function ldap_escape($str){
63     $illegal = array("(", ")", "#");
64     $legal = array();
65     foreach ($illegal as $id => $char) {
66       $legal[$id] = "\\".$char;
67     }
68     $str = str_replace($illegal, $legal,$str); //replace them
69     return $str;
70   }
71
72   /**
73    * Authenticate user against LDAP server.
74    *
75    * @param string $login
76    * @param string $password
77    * @return mixed
78    */
79   function authenticate($login, $password)
80   {
81     if (!function_exists('ldap_bind')) {
82       die ('php_ldap extension not loaded!');
83     }
84
85     if (empty($this->params['server']) || empty($this->params['base_dn'])) {
86       die('You must set server and base_dn in AUTH_MODULE_PARAMS in config.php');
87     }
88
89     $member_of = @$this->params['member_of'];
90
91     $lc = ldap_connect($this->params['server']);
92
93     if (defined('AUTH_DEBUG') && isTrue(AUTH_DEBUG)) {
94       echo '<br />';
95       echo '$lc='; var_dump($lc); echo '<br />';
96       echo 'ldap_error()='; echo ldap_error($lc); echo '<br />';
97     }
98
99     if (!$lc) return false;
100     
101     ldap_set_option($lc, LDAP_OPT_PROTOCOL_VERSION, 3);
102     ldap_set_option($lc, LDAP_OPT_REFERRALS, 0);
103     if (defined('AUTH_DEBUG') && isTrue(AUTH_DEBUG)) {
104       ldap_set_option($lc, LDAP_OPT_DEBUG_LEVEL, 7);
105     }
106     
107     // We need to handle Windows AD and OpenLDAP differently.
108     if ($this->params['type'] != 'openldap') {
109       
110       // check if the user specified full login
111       if (strpos($login, '@') === false) {
112         // append default domain
113         $login .= '@' . $this->params['default_domain'];
114       }
115
116
117       if (defined('AUTH_DEBUG') && isTrue(AUTH_DEBUG)) {
118         echo '$login='; var_dump($login); echo '<br />';
119       }
120
121       $lb = @ldap_bind($lc, $login, $password);
122     
123       if (defined('AUTH_DEBUG') && isTrue(AUTH_DEBUG)) {
124         echo '$lb='; var_dump($lb); echo '<br />';
125         echo 'ldap_error()='; echo ldap_error($lc); echo '<br />';
126       }
127
128       if (!$lb) {
129         ldap_unbind($lc);
130         return false;
131       }
132
133           if ($member_of) {
134         // get groups
135
136         $filter = 'samaccountname='.Auth_ldap::ldap_escape($login);
137         $fields = array('samaccountname', 'mail', 'memberof', 'department', 'displayname', 'telephonenumber', 'primarygroupid');
138         $sr = @ldap_search($lc, $this->params['base_dn'], $filter, $fields);
139
140         if (defined('AUTH_DEBUG') && isTrue(AUTH_DEBUG)) {
141           echo '$sr='; var_dump($sr); echo '<br />';
142           echo 'ldap_error()='; echo ldap_error($lc); echo '<br />';
143         }
144
145         // if search failed it's likely that account is disabled
146         if (!$sr) {
147           ldap_unbind($lc);
148           return false;
149         }
150
151         $entries = @ldap_get_entries($lc, $sr);
152
153         if (defined('AUTH_DEBUG') && isTrue(AUTH_DEBUG)) {
154           echo '$entries='; var_dump($entries); echo '<br />';
155           echo 'ldap_error()='; echo ldap_error($lc); echo '<br />';
156         }
157
158         if ($entries === false) {
159           ldap_unbind($lc);
160           return false;
161         }
162
163         $groups = array();
164
165         // extract group names from
166         // assuming the groups are in format: CN=<group_name>,...
167         for ($i = 0; $i < @$entries[0]['memberof']['count']; $i++) {
168           $grp = $entries[0]['memberof'][$i];
169           $grp_fields = explode(',', $grp);
170           $groups[] = substr($grp_fields[0], 3);
171         }
172
173         if (defined('AUTH_DEBUG') && isTrue(AUTH_DEBUG)) {
174           echo '$member_of'; var_dump($member_of); echo '<br />';
175         };
176
177         // check for group membership
178             foreach ($member_of as $check_grp) {
179           if (!in_array($check_grp, $groups)) {
180             ldap_unbind($lc);
181             return false;
182           }
183         }
184       }
185
186       ldap_unbind($lc);
187
188       // handle special case - admin account, strip domain part
189       if (strpos($login, 'admin@') !== false) {
190         $login = substr($login, 0, 5);
191       }
192
193       return array('login' => $login, 'data' => $entries, 'member_of' => $groups);
194     } else {
195         
196       // Assuming OpenLDAP server.
197       $login_oldap = 'uid='.$login.','.$this->params['base_dn'];
198
199       if (defined('AUTH_DEBUG') && isTrue(AUTH_DEBUG)) {
200         echo '$login_oldap='; var_dump($login_oldap); echo '<br />';
201       }
202       
203       // check if the user specified full login
204       if (strpos($login, '@') === false) {
205         // append default domain
206         $login .= '@' . $this->params['default_domain'];
207       }
208
209       $lb = @ldap_bind($lc, $login_oldap, $password);
210     
211       if (defined('AUTH_DEBUG') && isTrue(AUTH_DEBUG)) {
212         echo '$lb='; var_dump($lb); echo '<br />';
213         echo 'ldap_error()='; echo ldap_error($lc); echo '<br />';
214       }
215
216       if (!$lb) {
217         ldap_unbind($lc);
218         return false;
219       }
220
221           if ($member_of) {
222         // get groups
223
224         $filter = 'samaccountname='.Auth_ldap::ldap_escape($login_oldap);
225         $fields = array('samaccountname', 'mail', 'memberof', 'department', 'displayname', 'telephonenumber', 'primarygroupid');
226         $sr = @ldap_search($lc, $this->params['base_dn'], $filter, $fields);
227
228         if (defined('AUTH_DEBUG') && isTrue(AUTH_DEBUG)) {
229           echo '$sr='; var_dump($sr); echo '<br />';
230           echo 'ldap_error()='; echo ldap_error($lc); echo '<br />';
231         }
232
233         // if search failed it's likely that account is disabled
234         if (!$sr) {
235           ldap_unbind($lc);
236           return false;
237         }
238
239         $entries = @ldap_get_entries($lc, $sr);
240
241         if (defined('AUTH_DEBUG') && isTrue(AUTH_DEBUG)) {
242           echo '$entries='; var_dump($entries); echo '<br />';
243           echo 'ldap_error()='; echo ldap_error($lc); echo '<br />';
244         }
245
246         if ($entries === false) {
247           ldap_unbind($lc);
248           return false;
249         }
250
251         $groups = array();
252
253         // extract group names from
254         // assuming the groups are in format: CN=<group_name>,...
255         for ($i = 0; $i < @$entries[0]['memberof']['count']; $i++) {
256           $grp = $entries[0]['memberof'][$i];
257           $grp_fields = explode(',', $grp);
258           $groups[] = substr($grp_fields[0], 3);
259         }
260
261         if (defined('AUTH_DEBUG') && isTrue(AUTH_DEBUG)) {
262           echo '$member_of'; var_dump($member_of); echo '<br />';
263         };
264
265         // check for group membership
266         foreach ($member_of as $check_grp) {
267           if (!in_array($check_grp, $groups)) {
268             ldap_unbind($lc);
269             return false;
270           }
271         }
272       }
273
274       ldap_unbind($lc);
275
276       // handle special case - admin account, strip domain part
277       if (strpos($login, 'admin@') !== false) {
278         $login = substr($login, 0, 5);
279       }
280
281       return array('login' => $login, 'data' => $entries, 'member_of' => $groups);
282     }
283   }
284
285   function isPasswordExternal() {
286     return true;
287   }
288 }