File: class_acl.inc

package info (click to toggle)
fusiondirectory 1.0.8.2-5
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 28,984 kB
  • sloc: php: 74,645; xml: 3,645; perl: 1,555; pascal: 705; sh: 135; sql: 45; makefile: 19
file content (419 lines) | stat: -rw-r--r-- 12,137 bytes parent folder | download | duplicates (2)
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
<?php
/*
  This code is part of FusionDirectory (http://www.fusiondirectory.org/)
  Copyright (C) 2003-2010  Cajus Pollmeier
  Copyright (C) 2011-2013  FusionDirectory

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 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 General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
*/

/*!
 * \file class_acl.inc
 * Source code for Class ACL
 */

/*!
 * \brief This class contains all the function needed to manage acl
 * \see class_plugin
 */
class acl extends plugin
{
  /* attribute list for save action */
  var $attributes     = array('gosaAclEntry');
  var $objectclasses  = array('gosaAcl');

  /* Helpers */
  var $dialogState      = "head";
  var $gosaAclEntry     = array();
  var $aclType          = "";
  var $aclObject        = "";
  var $aclContents      = array();
  var $aclFilter        = "";
  var $aclMyObjects     = array();
  var $roles            = array();
  var $recipients       = array();
  var $currentIndex     = 0;
  var $wasNewEntry      = FALSE;
  var $savedAclContents = array();
  var $acl_category     = "acl/";

  /*!
   * \brief Acl contructor
   *
   * \param String $config Configuration file for ACL
   *
   * \param String $parent
   *
   * \param String $dn The DN
   */
  function acl (&$config, $dn = NULL, $baseobject = NULL)
  {
    /* Include config object */
    parent::__construct($config, $dn, $baseobject);

    /* Load ACL's */
    $this->gosaAclEntry = array();
    if (isset($this->attrs['gosaAclEntry'])) {
      for ($i = 0; $i < $this->attrs['gosaAclEntry']['count']; $i++) {
        $acl = $this->attrs['gosaAclEntry'][$i];
        $this->gosaAclEntry = array_merge($this->gosaAclEntry, acl::explodeACL($acl));
      }
    }
    ksort($this->gosaAclEntry);

    /* Save parent - we've to know more about it than other plugins... */
    if (($baseobject !== NULL) && isset($baseobject->parent)) {
      $this->parent =& $baseobject->parent;
    }

    $ldap = $config->get_ldap_link();

    /* Roles TODO - use objects::ls?*/
    $ldap->cd($config->current['BASE']);
    $ldap->search('(objectClass=gosaRole)', array('cn', 'description','gosaAclTemplate','dn'));
    while ($attrs = $ldap->fetch()) {
      $role_id = $attrs['dn'];

      $this->roles[$role_id]['acls']  = acl::explodeRole($attrs['gosaAclTemplate']);
      $this->roles[$role_id]['cn']    = $attrs['cn'][0];
      if (isset($attrs['description'][0])) {
        $this->roles[$role_id]['description'] = $attrs['description'][0];
      }
    }

    /* Finally - we want to get saved... */
    $this->is_account = TRUE;
  }

  /*!
   *  \brief Function sort an array by elements priority
   *
   *  \param Array $list Array to be sorted
   */
  function sort_by_priority($list)
  {
    uksort($list,
      function ($a, $b)
      {
        $infos_a = pluglist::pluginInfos(preg_replace('|^[^/]*/|', '', $a));
        $infos_b = pluglist::pluginInfos(preg_replace('|^[^/]*/|', '', $b));
        $pa = (isset($infos_a['plPriority'])?$infos_a['plPriority']:0);
        $pb = (isset($infos_b['plPriority'])?$infos_b['plPriority']:0);
        if ($pa == $pb) {
          return 0;
        }
        return ($pa < $pb ? -1 : 1);
      }
    );

    return $list;
  }

  /*!
   * \brief Explode a role
   *
   * \param string $acl ACL to be exploded
   */
  static function explodeRole($role)
  {
    if (!is_array($role)) {
      $role = array($role);
    }
    unset($role['count']);
    $result = array();
    foreach ($role as $aclTemplate) {
      $list = explode(':', $aclTemplate, 2);
      $result[$list[0]] = self::extractACL($list[1]);
    }
    ksort($result);
    return $result;
  }

  /*!
   * \brief Explode an acl
   *
   * \param string $acl ACL to be exploded
   */
  static function explodeACL($acl)
  {
    $list = explode(':', $acl);
    if (count($list) == 5) {
      list($index, $type,$role,$members,$filter) = $list;
      $filter = base64_decode($filter);
    } else {
      $filter = "";
      list($index, $type,$role,$members) = $list;
    }

    $a = array( $index => array(
      'type'    => $type,
      'filter'  => $filter,
      'members' => acl::extractMembers($members),
      'acl'     => base64_decode($role),
    ));

    /* Handle unknown types */
    if (!in_array($type, array('subtree', 'base'))) {
      msg_dialog::display(_("Internal error"), sprintf(_("Unkown ACL type '%s'!\nYou might need to run \"fusiondirectory-setup --migrate-acls\" to migrate your acls to the new format."), $type), ERROR_DIALOG);
      $a = array();
    }
    return $a;
  }

  /*!
   * \brief Extract members of an acl
   *
   * \param $acl The acl to be extracted members part
   *
   * \return an array with members
   */
  static function extractMembers($ms)
  {
    global $config;
    $a = array();

    /* Seperate by ',' and place it in an array */
    $ma = explode(',', $ms);

    /* Decode dn's, fill with informations from LDAP */
    $ldap = $config->get_ldap_link();
    foreach ($ma as $memberdn) {
      // Check for wildcard here
      $dn = base64_decode($memberdn);
      if ($dn != "*") {
        $ldap->cat($dn, array('cn', 'objectClass', 'description', 'uid'));

        /* Found entry... */
        if ($ldap->count()) {
          $attrs = $ldap->fetch();
          if (in_array_ics('gosaAccount', $attrs['objectClass'])) {
            $a['U:'.$dn] = $attrs['cn'][0]." [".$attrs['uid'][0]."]";
          } else {
            $a['G:'.$dn] = $attrs['cn'][0];
            if (isset($attrs['description'][0])) {
              $a['G:'.$dn] .= " [".$attrs['description'][0]."]";
            }
          }
          /* ... or not */
        } else {
          $a['U:'.$dn] = sprintf(_("Unknown entry '%s'!"), $dn);
        }
      } else {
        $a['G:*'] = sprintf(_("All users"));
      }
    }

    return $a;
  }

  /*!
   * \brief Extract an acl
   *
   * \param string $acl The acl to be extracted
   */
  static function extractACL($acl)
  {
    /* Rip acl off the string, seperate by ',' and place it in an array */
    $as = preg_replace('/^[^:]+:[^:]+:[^:]*:([^:]*).*$/', '\1', $acl);
    $aa = explode(',', $as);
    $a  = array();

    /* Dis-assemble single ACLs */
    foreach ($aa as $sacl) {

      /* Dis-assemble field ACLs */
      $ao       = explode('#', $sacl);
      $gobject  = "";
      foreach ($ao as $idx => $ssacl) {

        /* First is department with global acl */
        $object = preg_replace('/^([^;]+);.*$/', '\1', $ssacl);
        $gacl   = preg_replace('/^[^;]+;(.*)$/', '\1', $ssacl);
        if ($idx == 0) {
          /* Create hash for this object */
          $gobject      = $object;
          $a[$gobject]  = array();

          /* Append ACL if set */
          if ($gacl != "") {
            $a[$gobject] = array($gacl);
          }
        } else {
          /* All other entries get appended... */
          list($field, $facl)   = explode(';', $ssacl);
          $a[$gobject][$field]  = $facl;
        }

      }
    }

    return $a;
  }

  /*!
   * \brief Prepare for Copy & Paste
   *
   * \see plugin::PrepareForCopyPaste($source)
   *
   * \param string $source Source to prepare for copy and paste
   */
  function PrepareForCopyPaste($source)
  {
    plugin::PrepareForCopyPaste($source);

    $dn                 = $source['dn'];
    $acl_c              = new acl($this->config, $dn);
    $this->gosaAclEntry = $acl_c->gosaAclEntry;
  }

  /*!
   * \brief Removes object from parent
   */
  function remove_from_parent()
  {
    parent::remove_from_parent();

    /* include global link_info */
    $ldap = $this->config->get_ldap_link();

    $ldap->cd($this->dn);
    $this->cleanup();
    $ldap->modify($this->attrs);

    new log('remove', 'acls/'.get_class($this), $this->dn, array_keys($this->attrs), $ldap->get_error());

    /* Optionally execute a command after we're done */
    $this->handle_post_events('remove');
  }


  /*
   * \brief Return plugin informations for acl handling
   */
  static function plInfo()
  {
    return array(
      'plShortName'   => _('ACL'),
      'plDescription' => _('Manage access control lists'),
      'plSelfModify'  => FALSE,
      'plPriority'    => 0,
      'plCategory'    => array('acl' => array('description'  => _('ACL').'&nbsp;&amp;&nbsp;'._('ACL roles'),
                                                      'objectClass'  => array('gosaAcl','gosaRole'))),
      'plObjectType'  => array(),

      'plProvidedAcls'  => array()
    );
  }


  /*!
   * \brief Remove acls defined for $src
   */
  function remove_acl()
  {
    acl::remove_acl_for($this->dn);
  }


  /*!
   * \brief Remove acls defined for $src
   *
   * \param String $dn The DN
   *
   * FIXME
   */
  static function remove_acl_for($dn)
  {
    global $config;

    $ldap = $config->get_ldap_link();
    $ldap->cd($config->current['BASE']);
    $ldap->search("(&(objectClass=gosaAcl)(gosaAclEntry=*".base64_encode($dn)."*))", array("gosaAclEntry","dn"));
    $new_entries = array();
    while ($attrs = $ldap->fetch()) {
      if (!isset($attrs['gosaAclEntry'])) {
        continue;
      }
      unset($attrs['gosaAclEntry']['count']);

      // Remove entry directly
      foreach ($attrs['gosaAclEntry'] as $entry) {
        $parts        = explode(':', $entry);
        $members      = explode(',', $parts[2]);
        $new_members  = array();
        foreach ($members as $member) {
          if (base64_decode($member) != $dn) {
            $new_members[] = $member;
          } else {
            fusiondirectory_log("modify", "user/acl", $attrs['dn'], array(), sprintf("Removed acl for %s on object %s.", $dn, $attrs['dn']));
          }
        }

        /* We can completely remove the entry if there are no members anymore */
        if (count($new_members)) {
          $parts[2]       = implode(",", $new_members);
          $new_entries[]  = implode(":", $parts);
        }
      }

      // There should be a modification, so write it back
      $ldap->cd($attrs['dn']);
      $new_attrs = array("gosaAclEntry" => $new_entries);
      $ldap->modify($new_attrs);
      if (!$ldap->success()) {
        msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $dn, LDAP_MOD, get_class()), ERROR_DIALOG);
      }
    }
  }

  /*!
   * \brief Update the acl membership
   *
   * \param String $src The source
   *
   * \param String $dst The destination
   */
  static function update_acl_membership($src, $dst)
  {
    global $config;
    $ldap = $config->get_ldap_link();
    $ldap->cd($config->current['BASE']);
    $ldap->search("(&(objectClass=gosaAcl)(gosaAclEntry=*".base64_encode($src)."*))", array("gosaAclEntry","dn"));
    while ($attrs = $ldap->fetch()) {
      $acl = new acl($config, $attrs['dn']);
      foreach ($acl->gosaAclEntry as $id => $entry) {
        foreach ($entry['members'] as $m_id => $member) {
          if ($m_id == "U:".$src) {
            unset($acl->gosaAclEntry[$id]['members'][$m_id]);
            $new = "U:".$dst;

            $acl->gosaAclEntry[$id]['members'][$new] = $new;
            fusiondirectory_log("modify", "user/acl", $attrs['dn'], array(), sprintf("Updated acl for user %s on object %s.", $src, $attrs['dn']));
          }
          if ($m_id == "G:".$src) {
            unset($acl->gosaAclEntry[$id]['members'][$m_id]);
            $new = "G:".$dst;

            $acl->gosaAclEntry[$id]['members'][$new] = $new;
            fusiondirectory_log("modify", "group/acl", $attrs['dn'], array(), sprintf("Updated acl for group %s on object %s.", $src, $attrs['dn']));
          }
        }
      }
      $acl->save();
    }
  }
}

?>