1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
|
<?php
/*
+--------------------------------------------------------------------------+
| This file is part of the Kolab Web Admin Panel |
| |
| Copyright (C) 2011-2012, Kolab Systems AG |
| |
| This program is free software: you can redistribute it and/or modify |
| it under the terms of the GNU Affero General Public License as published |
| by the Free Software Foundation, either version 3 of the License, or |
| (at your option) any later version. |
| |
| This program is distributed in the hope that it will be useful, |
| but WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public License |
| along with this program. If not, see <http://www.gnu.org/licenses/> |
+--------------------------------------------------------------------------+
| Author: Aleksander Machniak <machniak@kolabsys.com> |
| Author: Jeroen van Meeuwen <vanmeeuwen@kolabsys.com> |
+--------------------------------------------------------------------------+
*/
class Auth {
static private $instance = array();
private $_auth = array();
private $conf;
private $domains = array();
/**
* Return an instance of Auth, associated with $domain.
*
* If $domain is not specified, the 'kolab' 'primary_domain' is used.
*/
static function get_instance($domain = NULL)
{
Log::trace("Auth::get_instance(\$domain = " . var_export($domain, TRUE) . ")");
$conf = Conf::get_instance();
if (empty($domain)) {
if (!empty($_SESSION['user'])) {
$domain = $_SESSION['user']->get_domain();
} else {
$domain = $conf->get('primary_domain');
}
}
if (!isset(self::$instance[$domain])) {
self::$instance[$domain] = new Auth($domain);
}
return self::$instance[$domain];
}
public function __construct($domain = NULL)
{
if (!$this->conf) {
$this->conf = Conf::get_instance();
}
if ($domain === NULL) {
$domain = $this->conf->get('primary_domain');
}
$this->domain = $domain;
$this->connect($domain);
}
/**
* Authenticate $username with $password.
*
* The following forms for a username exist:
*
* - "cn=Directory Manager"
*
* This is considered a DN, as it succeeds to parse as such. The
* very name of this user may have already caused you to suspect
* that the user is not associated with one domain per-se. It is
* our intention this user does therefor not have a
* $_SESSION['user']->domain, but instead a
* $_SESSION['user']->working_domain. In any case, obtain the
* current domain for any user through
* $_SESSION['user']->get_domain().
*
* NOTE/TODO: For now, even cn=Directory Manager is set to the
* default domain. I wish there was more time...
*
* - "user@domain.tld"
*
* While it may seem obvious, this user is to be authenticated
* against the 'domain.tld' realm.
*
* - "user"
*
* This user is to be authenticated against the 'kolab'
* 'primary_domain'.
*
* @param string $username User name (DN or mail)
* @param string $password User password
*
* @return bool|string User ID or False on failure
*/
public function authenticate($username, $password, $domain = null)
{
Log::info("Authentication request for $username against " . $this->domain);
if ($domain == NULL) {
$domain = $this->domain;
}
// TODO: Debug logging for the use of a current or the creation of
// a new authentication class instance.
$result = $this->_auth[$this->domain]->authenticate($username, $password, $domain);
return $result;
}
public function connect($domain = NULL)
{
if (empty($domain)) {
if (!empty($_SESSION['user'])) {
$domain = $_SESSION['user']->get_domain();
Log::trace("Using domain from session: $domain");
} else {
$domain = $this->conf->get('primary_domain');
Log::trace("Using primary_domain: " . $domain);
}
Log::trace("Domain to connect to not specified, connecting to $domain");
} else {
Log::trace("Domain to connect to set to $domain");
}
if ($domain) {
$auth_method = strtoupper($this->conf->get($domain, 'auth_mechanism'));
}
if (empty($auth_method)) {
// Use the default authentication technology
$auth_method = strtoupper($this->conf->get('kolab', 'auth_mechanism'));
}
if (!$auth_method) {
// Use LDAP by default
$auth_method = 'LDAP';
}
if (!isset($this->_auth[$domain])) {
Log::trace("Creating $auth_method for domain $domain");
require_once 'Auth/' . $auth_method . '.php';
$this->_auth[$domain] = new $auth_method($domain);
} else {
Log::trace("Auth for $domain already available");
}
}
/**
* Return Auth instance for specified domain
*/
private function auth_instance($domain = null)
{
if (empty($domain)) {
if (!empty($_SESSION['user'])) {
$domain = $_SESSION['user']->get_domain();
Log::trace("Using domain from session: " . $domain);
} else {
$domain = $this->conf->get('primary_domain');
Log::trace("Using primary_domain: " . $domain);
}
}
if (!isset($this->_auth[$domain])) {
$this->connect($domain);
}
return $this->_auth[$domain];
}
// TODO: Dummy function to be removed
public function attr_details($attribute)
{
$conf = Conf::get_instance();
$domain = $conf->get('kolab', 'primary_domain');
return $this->auth_instance($domain)->attribute_details((array)$attribute);
}
// TODO: Dummy function to be removed
public function attrs_allowed($objectclasses = array())
{
$conf = Conf::get_instance();
$domain = $conf->get('kolab', 'primary_domain');
return $this->auth_instance($domain)->allowed_attributes($objectclasses);
}
public function allowed_attributes($objectclasses = array())
{
return $this->auth_instance()->allowed_attributes((array)$objectclasses);
}
public function attribute_details($attributes = array())
{
return $this->auth_instance()->attribute_details((array)$attributes);
}
public function domain_add($domain, $domain_attrs)
{
return $this->auth_instance()->domain_add($domain, $domain_attrs);
}
public function domain_edit($domain, $attributes, $typeid = null)
{
return $this->auth_instance()->domain_edit($domain, $attributes, $typeid);
}
public function domain_delete($domain)
{
return $this->auth_instance()->domain_delete($domain);
}
public function domain_find_by_attribute($attribute)
{
return $this->auth_instance()->domain_find_by_attribute($attribute);
}
public function domain_info($domaindata)
{
return $this->auth_instance()->domain_info($domaindata);
}
public function find_recipient($address)
{
return $this->auth_instance()->find_recipient($address);
}
public function find_user_groups($member_dn)
{
return $this->auth_instance()->find_user_groups($member_dn);
}
public function get_entry_attribute($subject, $attribute)
{
$entry = $this->auth_instance()->get_attributes($subject, (array)$attribute);
return $entry[$attribute];
}
public function get_entry_attributes($subject, $attributes)
{
return $this->auth_instance()->get_attributes($subject, $attributes);
}
public function group_add($attributes, $typeid = null)
{
return $this->auth_instance()->group_add($attributes, $typeid);
}
public function group_edit($group, $attributes, $typeid = null)
{
return $this->auth_instance()->group_edit($group, $attributes, $typeid);
}
public function group_delete($subject)
{
return $this->auth_instance()->group_delete($subject);
}
public function group_find_by_attribute($attribute)
{
return $this->auth_instance()->group_find_by_attribute($attribute);
}
public function group_info($groupdata)
{
return $this->auth_instance()->group_info($groupdata);
}
public function group_members_list($groupdata, $recurse = true)
{
return $this->auth_instance()->group_members_list($groupdata, $recurse);
}
public function list_domains($attributes = array(), $search = array(), $params = array())
{
return $this->auth_instance()->list_domains($attributes, $search, $params);
}
public function list_rights($subject)
{
return $this->auth_instance()->effective_rights($subject);
}
public function list_users($domain = NULL, $attributes = array(), $search = array(), $params = array())
{
return $this->auth_instance($domain)->list_users($attributes, $search, $params);
}
public function list_groups($domain = NULL, $attributes = array(), $search = array(), $params = array())
{
return $this->auth_instance($domain)->list_groups($attributes, $search, $params);
}
public function list_resources($domain = NULL, $attributes = array(), $search = array(), $params = array())
{
return $this->auth_instance($domain)->list_resources($attributes, $search, $params);
}
public function list_roles($domain = NULL, $attributes = array(), $search = array(), $params = array())
{
return $this->auth_instance($domain)->list_roles($attributes, $search, $params);
}
public function list_sharedfolders($domain = NULL, $attributes = array(), $search = array(), $params = array())
{
return $this->auth_instance($domain)->list_sharedfolders($attributes, $search, $params);
}
public function primary_for_valid_domain($domain)
{
$this->domains = $this->list_domains();
if (array_key_exists($domain, $this->domains)) {
return $domain;
}
else if (in_array($domain, $this->domains)) {
// We know it's not a key!
foreach ($this->domains as $parent_domain => $child_domains) {
if (in_array($domain, $child_domains)) {
return $parent_domain;
}
}
return FALSE;
}
else {
return FALSE;
}
}
public function resource_add($attributes, $typeid = null)
{
return $this->auth_instance()->resource_add($attributes, $typeid);
}
public function resource_edit($resource, $attributes, $typeid = null)
{
return $this->auth_instance()->resource_edit($resource, $attributes, $typeid);
}
public function resource_delete($subject)
{
return $this->auth_instance()->resource_delete($subject);
}
public function resource_find_by_attribute($attribute)
{
return $this->auth_instance()->resource_find_by_attribute($attribute);
}
public function resource_info($resourcedata)
{
return $this->auth_instance()->resource_info($resourcedata);
}
public function resource_members_list($resourcedata, $recurse = true)
{
return $this->auth_instance()->resource_members_list($resourcedata, $recurse);
}
public function role_add($role)
{
return $this->auth_instance()->role_add($role);
}
public function role_edit($role, $attributes, $typeid = null)
{
return $this->auth_instance()->role_edit($role, $attributes, $typeid);
}
public function role_delete($role)
{
return $this->auth_instance()->role_delete($role);
}
public function role_find_by_attribute($attribute)
{
return $this->auth_instance()->role_find_by_attribute($attribute);
}
public function role_info($roledata)
{
return $this->auth_instance()->role_info($roledata);
}
public function sharedfolder_add($attributes, $typeid = null)
{
return $this->auth_instance()->sharedfolder_add($attributes, $typeid);
}
public function sharedfolder_edit($sharedfolder, $attributes, $typeid = null)
{
return $this->auth_instance()->sharedfolder_edit($sharedfolder, $attributes, $typeid);
}
public function sharedfolder_delete($subject)
{
return $this->auth_instance()->sharedfolder_delete($subject);
}
public function sharedfolder_find_by_attribute($attribute)
{
return $this->auth_instance()->sharedfolder_find_by_attribute($attribute);
}
public function sharedfolder_info($sharedfolderdata)
{
return $this->auth_instance()->sharedfolder_info($sharedfolderdata);
}
public function search()
{
return call_user_func_array(Array($this->auth_instance(), 'search'), func_get_args());
}
public function subject_base_dn($key, $type)
{
// first try strict match
$base_dn = $this->auth_instance()->subject_base_dn($key . '_' . $type, true);
if (!$base_dn) {
$base_dn = $this->auth_instance()->subject_base_dn($type);
}
return $base_dn;
}
public function user_add($attributes, $typeid = null)
{
return $this->auth_instance()->user_add($attributes, $typeid);
}
public function user_edit($user, $attributes, $typeid = null)
{
return $this->auth_instance()->user_edit($user, $attributes, $typeid);
}
public function user_delete($userdata)
{
return $this->auth_instance()->user_delete($userdata);
}
public function user_find_by_attribute($attribute)
{
return $this->auth_instance()->user_find_by_attribute($attribute);
}
public function user_info($userdata)
{
return $this->auth_instance()->user_info($userdata);
}
public function schema_attributes($object_classes)
{
return $this->auth_instance()->attributes_allowed($object_classes);
}
public function schema_classes()
{
return $this->auth_instance()->classes_allowed();
}
}
|