summaryrefslogtreecommitdiffstats
path: root/lib/api/kolab_api_service_domain.php
blob: 880310906c14e6bd82372f0b4c53b2f94e7ba384 (plain)
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
<?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>                     |
 +--------------------------------------------------------------------------+
*/

/**
 * Service providing domain mutations
 */
class kolab_api_service_domain extends kolab_api_service
{

    /**
     * Returns service capabilities.
     *
     * @param string $domain Domain name
     *
     * @return array Capabilities list
     */
    public function capabilities($domain)
    {
        $auth = Auth::get_instance();
        $conf = Conf::get_instance();

        $domain_base_dn = $conf->get('domain_base_dn');

        if (empty($domain_base_dn)) {
            return array();
        }

        $effective_rights = $auth->list_rights($domain_base_dn);
        $rights           = array();

        if (in_array('add', $effective_rights['entrylevelrights'])) {
            $rights['add'] = "w";
        }

        if (in_array('delete', $effective_rights['entrylevelrights'])) {
            $rights['delete'] = "w";
        }

        if (in_array('modrdn', $effective_rights['entrylevelrights'])) {
            $rights['edit'] = "w";
        }

        if (in_array('read', $effective_rights['entrylevelrights'])) {
            $rights['find'] = "r";
            $rights['info'] = "r";
        }

        $rights['effective_rights'] = "r";

        return $rights;
    }

    public function domain_add($getdata, $postdata)
    {
        Log::trace("domain.add(\$getdata = " . var_export($getdata, TRUE) . ", \$postdata = " . var_export($postdata, TRUE) . ")");

        $conf = Conf::get_instance();
        $dna  = $conf->get('domain_name_attribute');

        if (empty($dna)) {
            $dna = 'associateddomain';
        }

        if (empty($postdata[$dna])) {
            Log::error("domain.add called without '" . $dna . "' specified");
            return false;
        }

        $auth = Auth::get_instance($conf->get('kolab', 'primary_domain'));

        // parse input attributes
        $attributes       = $this->parse_input_attributes('domain', $postdata);
        $attributes[$dna] = (array) $attributes[$dna];
        $domain           = array_shift($attributes[$dna]);

        $this->_mod_domain_attrs($domain, $attributes);

        $result = $auth->domain_add($domain, $attributes);

        if ($result) {
            if ($id = $this->unique_attribute_value($result)) {
                $attributes['id'] = $id;
            }


            $this->_after_domain_created($attributes, $domain);

            return $attributes;
        }

        return false;
    }

    /**
     * Domain delete.
     *
     * @param array $get  GET parameters
     * @param array $post POST parameters
     *
     * @return bool True on success, False on failure
     * @throws kolab_api_exception
     */
    public function domain_delete($getdata, $postdata)
    {
        Log::trace("domain.delete(\$getdata = '" . var_export($getdata, TRUE) . "', \$postdata = '" . var_export($postdata, TRUE) . "')");

        if (empty($postdata['id'])) {
            Log::error("domain.delete called without a Domain ID");
            return false;
        }

        $auth = Auth::get_instance();

        // check if domain is empty
        if (empty($postdata['force']) || strtolower($postdata['force']) == 'false') {
            if (!$auth->domain_is_empty($postdata['id'])) {
                throw new kolab_api_exception(kolab_api_exception::DOMAIN_NOT_EMPTY);
            }
        }

        $result = $auth->domain_delete($postdata['id']);

        if ($result) {
            return $result;
        }

        return false;
    }

    public function domain_edit($getdata, $postdata)
    {
        Log::trace("domain.edit(\$getdata = '" . var_export($getdata, TRUE) . "', \$postdata = '" . var_export($postdata, TRUE) . "')");

        if (empty($postdata['id'])) {
            Log::error("domain.edit called without a Domain ID");
            return false;
        }

        $auth = Auth::get_instance();

        // check if domain is empty when changing status to deleted, as in domain.delete
        if ($postdata['inetdomainstatus'] == 'deleted'
            && (empty($postdata['force']) || strtolower($postdata['force']) == 'false')
        ) {
            $domain = $auth->domain_info($postdata['id']);
            if (!empty($domain) && $domain[key($domain)]['inetdomainstatus'] != 'deleted'
                && !$auth->domain_is_empty($domain)
            ) {
                throw new kolab_api_exception(kolab_api_exception::DOMAIN_NOT_EMPTY);
            }
        }

        $attributes = $this->parse_input_attributes('domain', $postdata, $postdata['type_id']);

        $this->_mod_domain_attrs(null, $attributes, $postdata['id']);

        $result = $auth->domain_edit($postdata['id'], $attributes, $postdata['type_id']);

        if ($result) {
            return $result;
        }

        return false;
    }

    public function domain_effective_rights($getdata, $postdata)
    {
        $auth = Auth::get_instance();
        $conf = Conf::get_instance();
        $dna  = $conf->get('domain_name_attribute');

        if (empty($dna)) {
            $dna = 'associateddomain';
        }

        // TODO: Input validation
        if (!empty($getdata[$dna])) {
            $entry_dn    = $getdata[$dna];
            $unique_attr = self::unique_attribute();
            $domain      = $auth->domain_find_by_attribute(array($unique_attr => $entry_dn));

            if (!empty($domain)) {
                $entry_dn = key($domain);
            }
        }
        else {
            $entry_dn = $conf->get('ldap', 'domain_base_dn');
        }

        // TODO: Fix searching the correct base_dn... Perhaps find the entry
        // first.
        $effective_rights = $auth->list_rights($entry_dn);

        return $effective_rights;
    }

    public function domain_find($getdata, $postdata)
    {
        $conf = Conf::get_instance();
        $dna  = $conf->get('domain_name_attribute');

        if (empty($dna)) {
            $dna = 'associateddomain';
        }

        if (empty($getdata[$dna])) {
            Log::error("domain.find called without a '" . $dna . "' parameter");
            return false;
        }

        $auth   = Auth::get_instance();
        $domain = $auth->domain_find_by_attribute(array($dna => $getdata[$dna]));

        if (!empty($domain)) {
            return $domain;
        }

        return false;
    }

    /**
     * Domain information.
     *
     * @param array $get   GET parameters
     * @param array $post  POST parameters
     *
     * @return array|bool Domain attributes, False on error
     */
    public function domain_info($getdata, $postdata)
    {
        Log::trace("domain.info(\$getdata = '" . var_export($getdata, TRUE) . "', \$postdata = '" . var_export($postdata, TRUE) . "')");

        if (empty($getdata['id'])) {
            Log::error("domain.info called without a Domain ID");
            return false;
        }

        $auth   = Auth::get_instance();
        $attrs  = $this->object_attributes('domain');
        $result = $auth->domain_info($getdata['id'], $attrs);

        // normalize result
        $result = $this->parse_result_attributes('domain', $result);

        if (empty($result['id'])) {
            $result['id'] = $getdata['id'];
        }

        if ($result) {
            return $result;
        }

        return false;
    }

    /**
     * Modify hosted domain attributes
     */
    protected function _mod_domain_attrs($domain, &$attributes, $olddn = null)
    {
        // Generate attributes (aci, inetdomainbasedn) for hosted domains
        $conf = Conf::get_instance();
        if ($conf->get('kolab_wap', 'hosted_root_dn')) {

            $domain_name_attribute = $conf->get('ldap', 'domain_name_attribute');
            $hosted_root_dn        = $conf->get('kolab_wap', 'hosted_root_dn');
            $mgmt_root_dn          = $conf->get('kolab_wap', 'mgmt_root_dn');

            if (empty($mgmt_root_dn)) {
                $mgmt_root_dn = $conf->get('root_dn');
            }

            if (empty($domain_name_attribute)) {
                $domain_name_attribute = 'associateddomain';
            }

            if (!is_array($attributes[$domain_name_attribute])) {
                $attributes[$domain_name_attribute] = (array) $attributes[$domain_name_attribute];
            }

            if (empty($domain)) {
                $domain = $attributes[$domain_name_attribute][0];
            }

            if (!in_array($domain, $attributes[$domain_name_attribute])) {
                array_unshift($attributes[$domain_name_attribute], $domain);
            }

            $domain_root_dn = 'ou=' . $domain . ',' . $hosted_root_dn;

            $aci  = array(
                '(targetattr = "*")'
                . '(version 3.0; acl "Deny Unauthorized"; deny (all)'
                . '(userdn != "ldap:///uid=kolab-service,ou=Special Users,' . $mgmt_root_dn . ' || '
                . 'ldap:///ou=People,' . $domain_root_dn . '??sub?(objectclass=inetorgperson)") AND NOT '
                . 'roledn = "ldap:///cn=kolab-admin,' . $mgmt_root_dn . '";)',

                '(targetattr != "userPassword")'
                . '(version 3.0;acl "Search Access";allow (read,compare,search)'
                . '(userdn = "ldap:///uid=kolab-service,ou=Special Users,' . $mgmt_root_dn . ' || '
                . 'ldap:///ou=People,' . $domain_root_dn . '??sub?(objectclass=inetorgperson)");)',

                '(targetattr = "*")'
                . '(version 3.0;acl "Kolab Administrators";allow (all)'
                . '(roledn = "ldap:///cn=kolab-admin,' . $domain_root_dn . ' || '
                . 'ldap:///cn=kolab-admin,' . $mgmt_root_dn . '");)'
            );

            // Preserve important old aci and inetdomainbasedn attributes of the
            // management and hosted domain.
            if (!empty($olddn)) {
                $auth = Auth::get_instance();
                $info = $auth->domain_info($olddn);
                if (!empty($info)) {
                    $oldattributes = reset($info);
                    if (!empty($oldattributes['inetdomainbasedn'])
                        && $oldattributes['inetdomainbasedn'] == $hosted_root_dn
                        || "dc=".implode(",dc=",explode(".",$domain)) == $mgmt_root_dn
                    ) {
                        $aci = $oldattributes['aci'];
                        if (!empty($oldattributes['inetdomainbasedn'])) {
                            $domain_root_dn = $oldattributes['inetdomainbasedn'];
                        } else {
                            $domain_root_dn = null;
                        }
                    }
                }
            }

            $attributes['aci'] = $aci;
            if (!empty($domain_root_dn)) {
                $attributes['inetdomainbasedn'] = $domain_root_dn;
            }

            $this->is_hosted = true;
        }
    }

    /**
     * Create LDAP object related to the new hosted domain
     */
    protected function _after_domain_created($attributes, $domain)
    {
        if (!$this->is_hosted) {
            return;
        }

        $conf         = Conf::get_instance();
        $ou_service   = $this->controller->get_service('ou');
        $role_service = $this->controller->get_service('role');

        $hosted_root_dn = $conf->get('kolab_wap', 'hosted_root_dn');
        $mgmt_root_dn   = $conf->get('kolab_wap', 'mgmt_root_dn');
        $domain_root_dn = 'ou=' . $domain . ',' . $hosted_root_dn;

        if (empty($mgmt_root_dn)) {
            $mgmt_root_dn = $conf->get('root_dn');
        }

        $ou_domain = array(
            'ou'            => $domain,
            'base_dn'       => $hosted_root_dn,
            'description'   => $domain,
            'type_id'       => 1,
        );

        $ou_domain['aci'] = array(
            '(targetattr = "*")'
            . '(version 3.0;acl "Deny Unauthorized"; deny (all)'
            . '(userdn != "ldap:///uid=kolab-service,ou=Special Users,' . $mgmt_root_dn . ' || '
            . 'ldap:///ou=People,' . $domain_root_dn . '??sub?(objectclass=inetorgperson)") AND NOT '
            . 'roledn = "ldap:///cn=kolab-admin,' . $mgmt_root_dn . '";)',

            '(targetattr != "userPassword")'
            . '(version 3.0;acl "Search Access";allow (read,compare,search,write)'
            . '(userdn = "ldap:///uid=kolab-service,ou=Special Users,' . $mgmt_root_dn . ' || '
            . 'ldap:///ou=People,' . $domain_root_dn . '??sub?(objectclass=inetorgperson)");)',

            '(targetattr = "*")'
            . '(version 3.0;acl "Kolab Administrators";allow (all)'
            . '(roledn = "ldap:///cn=kolab-admin,' . $domain_root_dn . ' || '
            . 'ldap:///cn=kolab-admin,' . $mgmt_root_dn . '");)',

            '(target = "ldap:///ou=*,' . $domain_root_dn . '")(targetattr="objectclass || aci || ou")'
            . '(version 3.0;acl "Allow Domain sub-OU Registration"; allow (add)'
            . '(userdn = "ldap:///uid=kolab-service,ou=Special Users,' . $mgmt_root_dn . '");)',

            '(target = "ldap:///uid=*,ou=People,' . $domain_root_dn . '")(targetattr="*")'
            . '(version 3.0;acl "Allow Domain First User Registration"; allow (add)'
            . '(userdn = "ldap:///uid=kolab-service,ou=Special Users,' . $mgmt_root_dn . '");)',

            '(target = "ldap:///cn=*,' . $domain_root_dn . '")(targetattr="objectclass || cn")'
            . '(version 3.0;acl "Allow Domain Role Registration"; allow (add)'
            . '(userdn = "ldap:///uid=kolab-service,ou=Special Users,' . $mgmt_root_dn . '");)',
        );

        $ou_service->ou_add(null, $ou_domain);

        // Add OU trees

        foreach (array('Groups', 'People', 'Resources', 'Shared Folders') as $item) {
            $ou = array(
                'ou'            => $item,
                'base_dn'       => $domain_root_dn,
                'type_id'       => 1,
                'description'   => $item,
            );

            $ou_service->ou_add(null, $ou);
        }

        // Add an admin role

        $role = array(
            'cn'            => 'kolab-admin',
            'description'   => 'Domain Administrator',
            'type_id'       => 1,
            'base_dn'       => $domain_root_dn,
        );

        $role_service->role_add(null, $role);
    }
}