1 <?php
2 /*
3 * This code is part of GOsa (http://www.gosa-project.org)
4 * Copyright (C) 2003-2008 GONICUS GmbH
5 *
6 * ID: $$Id$$
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
23 class userinfo
24 {
25 var $dn;
26 var $ip;
27 var $username;
28 var $cn;
29 var $uid;
30 var $gidNumber= -1;
31 var $language= "";
32 var $config;
33 var $gosaUnitTag= "";
34 var $subtreeACL= array();
35 var $ACL= array();
36 var $ocMapping= array();
37 var $groups= array();
38 var $result_cache =array();
40 /* get acl's an put them into the userinfo object
41 attr subtreeACL (userdn:components, userdn:component1#sub1#sub2,component2,...) */
42 function userinfo(&$config, $userdn){
43 $this->config= &$config;
44 $ldap= $this->config->get_ldap_link();
45 $ldap->cat($userdn,array('sn', 'givenName', 'uid', 'gidNumber', 'preferredLanguage', 'gosaUnitTag'));
46 $attrs= $ldap->fetch();
48 if (isset($attrs['givenName'][0]) && isset($attrs['sn'][0])){
49 $this->cn= $attrs['givenName'][0]." ".$attrs['sn'][0];
50 } else {
51 $this->cn= $attrs['uid'][0];
52 }
53 if (isset($attrs['gidNumber'][0])){
54 $this->gidNumber= $attrs['gidNumber'][0];
55 }
57 /* Assign user language */
58 if (isset($attrs['preferredLanguage'][0])){
59 $this->language= $attrs['preferredLanguage'][0];
60 }
62 if (isset($attrs['gosaUnitTag'][0])){
63 $this->gosaUnitTag= $attrs['gosaUnitTag'][0];
64 }
66 $this->dn= $userdn;
67 $this->uid= $attrs['uid'][0];
68 $this->ip= $_SERVER['REMOTE_ADDR'];
70 /* Initialize ACL_CACHE */
71 session::set('ACL_CACHE',array());
72 $this->reset_acl_cache();
73 }
76 public function reset_acl_cache()
77 {
78 /* Initialize ACL_CACHE */
79 session::set('ACL_CACHE',array());
80 }
82 function loadACL()
83 {
84 $this->ACL= array();
85 $this->groups= array();
86 $this->result_cache =array();
87 $this->reset_acl_cache();
88 $ldap= $this->config->get_ldap_link();
89 $ldap->cd($this->config->current['BASE']);
91 /* Get member groups... */
92 $ldap->search("(&(objectClass=posixGroup)(memberUid=".$this->uid."))", array('dn'));
93 while ($attrs= $ldap->fetch()){
94 $this->groups[$attrs['dn']]= $attrs['dn'];
95 }
97 /* Crawl through ACLs and move relevant to the tree */
98 $ldap->search("(objectClass=gosaACL)", array('dn', 'gosaAclEntry'));
99 $aclp= array();
100 $aclc= array();
101 while ($attrs= $ldap->fetch()){
103 /* Insert links in ACL array */
104 $aclp[$attrs['dn']]= substr_count($attrs['dn'], ',');
105 $aclc[$attrs['dn']]= array();
106 $ol= array();
107 for($i= 0; $i<$attrs['gosaAclEntry']['count']; $i++){
108 $ol= array_merge($ol, @acl::explodeAcl($attrs['gosaAclEntry'][$i]));
109 }
110 $aclc[$attrs['dn']]= $ol;
111 }
113 /* Resolve roles here.
114 */
115 foreach($aclc as $dn => $data){
116 foreach($data as $prio => $aclc_value) {
117 if($aclc_value['type'] == "role"){
119 unset($aclc[$dn][$prio]);
121 $ldap->cat($aclc_value['acl'],array("gosaAclTemplate"));
122 $attrs = $ldap->fetch();
124 if(isset($attrs['gosaAclTemplate'])){
125 for($i= 0; $i<$attrs['gosaAclTemplate']['count']; $i++){
126 $tmp = @acl::explodeAcl($attrs['gosaAclTemplate'][$i]);
128 foreach($tmp as $new_acl){
130 /* Keep non role attributes here! */
131 $new_acl['filter'] = $aclc_value['filter'];
132 $new_acl['members'] = $aclc_value['members'];
133 $aclc[$dn][] =$new_acl;
134 }
135 }
136 }
137 }
138 }
139 }
141 /* ACL's read, sort for tree depth */
142 asort($aclp);
144 /* Sort in tree order */
145 foreach ($aclp as $dn => $acl){
146 /* Check if we need to keep this ACL */
147 foreach($aclc[$dn] as $idx => $type){
148 $interresting= FALSE;
150 /* No members? This is good for all users... */
151 if (!count($type['members'])){
152 $interresting= TRUE;
153 } else {
155 /* Inspect members... */
156 foreach ($type['members'] as $grp => $grpdsc){
157 /* Some group inside the members that is relevant for us? */
158 if (in_array_ics(preg_replace('/^G:/', '', $grp), $this->groups)){
159 $interresting= TRUE;
160 }
162 /* User inside the members? */
163 if (preg_replace('/^U:/', '', $grp) == $this->dn){
164 $interresting= TRUE;
165 }
166 }
167 }
169 if ($interresting){
170 if (!isset($this->ACL[$dn])){
171 $this->ACL[$dn]= array();
172 }
173 $this->ACL[$dn][$idx]= $type;
174 }
175 }
177 }
178 }
181 function get_category_permissions($dn, $category)
182 {
183 /* If we are forced to skip ACLs checks for the current user
184 then return all permissions.
185 */
186 if($this->ignore_acl_for_current_user()){
187 return("rwcdm");
188 }
190 /* Get list of objectClasses and get the permissions for it */
191 $acl= "";
192 if (isset($this->ocMapping[$category])){
193 foreach($this->ocMapping[$category] as $oc){
194 $acl.= $this->get_permissions($dn, $category."/".$oc);
195 }
196 }else{
197 trigger_error("ACL request for an invalid category (".$category.").");
198 }
200 return ($acl);
201 }
204 /*! \brief Check if the given object (dn) is copyable
205 @param String The object dn
206 @param String The acl category (e.g. users)
207 @param String The acl class (e.g. user)
208 @return Boolean TRUE if the given object is copyable else FALSE
209 */
210 function is_copyable($dn, $object, $class)
211 {
212 return(preg_match("/r/",$this->has_complete_category_acls($dn, $object)));
213 }
216 /*! \brief Check if the given object (dn) is cutable
217 @param String The object dn
218 @param String The acl category (e.g. users)
219 @param String The acl class (e.g. user)
220 @return Boolean TRUE if the given object is cutable else FALSE
221 */
222 function is_cutable($dn, $object, $class)
223 {
224 $remove = preg_match("/d/",$this->get_permissions($dn,$object."/".$class));
225 $read = preg_match("/r/",$this->has_complete_category_acls($dn, $object));
226 return($remove && $read);
227 }
230 /*! \brief Checks if we are allowed to paste an object to the given destination ($dn)
231 @param String The destination dn
232 @param String The acl category (e.g. users)
233 @param String The acl class (e.g. user)
234 @return Boolean TRUE if we are allowed to paste an object.
235 */
236 function is_pasteable($dn, $object)
237 {
238 return(preg_match("/w/",$this->has_complete_category_acls($dn, $object)));
239 }
242 /*! \brief Checks if we are allowed to restore a snapshot for the given dn.
243 @param String The destination dn
244 @param String The acl category (e.g. users)
245 @return Boolean TRUE if we are allowed to restore a snapshot.
246 */
247 function allow_snapshot_restore($dn, $object)
248 {
249 if(!is_array($object)){
250 $object = array($object);
251 }
252 $r = $w = $c = TRUE;
253 foreach($object as $category){
254 $w &= preg_match("/w/",$this->has_complete_category_acls($dn, $category));
255 $c &= preg_match("/c/",$this->has_complete_category_acls($dn, $category));
256 $r &= preg_match("/r/",$this->has_complete_category_acls($dn, $category));
257 # print_a(array($category => array($r.$w.$c)));
258 }
259 return($r && $w );
260 }
263 /*! \brief Checks if we are allowed to create a snapshot of the given dn.
264 @param String The source dn
265 @param String The acl category (e.g. users)
266 @return Boolean TRUE if we are allowed to restore a snapshot.
267 */
268 function allow_snapshot_create($dn, $object)
269 {
270 if(!is_array($object)){
271 $object = array($object);
272 }
273 $r = $w = $c = TRUE;
274 foreach($object as $category){
275 $w &= preg_match("/w/",$this->has_complete_category_acls($dn, $category));
276 $c &= preg_match("/c/",$this->has_complete_category_acls($dn, $category));
277 $r &= preg_match("/r/",$this->has_complete_category_acls($dn, $category));
278 # print_a(array($category => array($r.$w.$c)));
279 }
280 return($r) ;
281 }
284 function get_permissions($dn, $object, $attribute= "", $skip_write= FALSE)
285 {
286 /* If we are forced to skip ACLs checks for the current user
287 then return all permissions.
288 */
289 if($this->ignore_acl_for_current_user()){
290 if($skip_write){
291 return("rcdm");
292 }
293 return("rwcdm");
294 }
296 /* Push cache answer? */
297 $ACL_CACHE = &session::get('ACL_CACHE');
298 if (isset($ACL_CACHE["$dn+$object+$attribute"])){
300 /* Remove write if needed */
301 if ($skip_write){
302 $ret = preg_replace('/w/', '', $ACL_CACHE["$dn+$object+$attribute"]);
303 }else{
304 $ret = $ACL_CACHE["$dn+$object+$attribute"];
305 }
306 return($ret);
307 }
309 /* Get ldap object, for later filter checks
310 */
311 $ldap = $this->config->get_ldap_link();
313 $acl= array("r" => "", "w" => "", "c" => "", "d" => "", "m" => "", "a" => "");
315 /* Build dn array */
316 $path= split(',', $dn);
317 $path= array_reverse($path);
319 /* Walk along the path to evaluate the acl */
320 $cpath= "";
321 foreach ($path as $element){
323 /* Clean potential ACLs for each level */
324 if(in_array($cpath,$this->config->departments)){
325 $acl= $this->cleanACL($acl);
326 }
328 if ($cpath == ""){
329 $cpath= $element;
330 } else {
331 $cpath= $element.','.$cpath;
332 }
334 if (isset($this->ACL[$cpath])){
336 /* Inspect this ACL, place the result into ACL */
337 foreach ($this->ACL[$cpath] as $subacl){
339 /* Reset? Just clean the ACL and turn over to the next one... */
340 if ($subacl['type'] == 'reset'){
341 $acl= $this->cleanACL($acl, TRUE);
342 continue;
343 }
345 if($subacl['type'] == "role") {
346 echo "role skipped";
347 continue;
348 }
350 /* With user filter */
351 if (isset($subacl['filter']) && !empty($subacl['filter'])){
352 if(!$ldap->object_match_filter($dn,$subacl['filter'])){
353 continue;
354 }
355 }
357 /* Self ACLs?
358 */
359 if(isset($subacl['acl'][$object][0]) && preg_match("/s/",$subacl['acl'][$object][0]) && $dn != $this->dn){
360 continue;
361 }
363 /* If attribute is "", we want to know, if we've *any* permissions here...
364 Merge global class ACLs [0] with attributes specific ACLs [attribute].
365 */
366 if ($attribute == "" && isset($subacl['acl'][$object])){
367 foreach($subacl['acl'][$object] as $attr => $dummy){
368 $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl'][$object][$attr]);
369 }
370 continue;
371 }
373 /* Per attribute ACL? */
374 if (isset($subacl['acl'][$object][$attribute])){
375 $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl'][$object][$attribute]);
376 continue;
377 }
379 /* Per object ACL? */
380 if (isset($subacl['acl'][$object][0])){
381 $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl'][$object][0]);
382 continue;
383 }
385 /* Global ACL? */
386 if (isset($subacl['acl']['all'][0])){
387 $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl']['all'][0]);
388 continue;
389 }
390 }
391 }
392 }
394 /* If the requested ACL is for a container object, then alter
395 ACLs by applying cleanACL a last time.
396 */
397 if(in_array($dn,$this->config->departments)){
398 $acl = $this->cleanACL($acl);
399 }
401 /* Assemble string */
402 $ret= "";
403 foreach ($acl as $key => $value){
404 if ($value !== ""){
405 $ret.= $key;
406 }
407 }
409 $ACL_CACHE["$dn+$object+$attribute"]= $ret;
411 /* Remove write if needed */
412 if ($skip_write){
413 $ret= preg_replace('/w/', '', $ret);
414 }
415 return ($ret);
416 }
419 /* Extract all departments that are accessible (direct or 'on the way' to an
420 accessible department) */
421 function get_module_departments($module, $skip_self_acls = FALSE )
422 {
424 /* If we are forced to skip ACLs checks for the current user
425 then return all departments as valid.
426 */
427 if($this->ignore_acl_for_current_user()){
428 return(array_keys($this->config->idepartments));
429 }
431 /* Use cached results if possilbe */
432 $ACL_CACHE = session::get('ACL_CACHE');
433 if(isset($ACL_CACHE['MODULE_DEPARTMENTS'][serialize($module)])){
434 return($ACL_CACHE['MODULE_DEPARTMENTS'][serialize($module)]);
435 }
437 global $plist;
439 $objects= array();
440 $deps= array();
442 /* Extract all relevant objects for this module from plist */
443 foreach ($plist->info as $object => $info){
444 if (!isset($info['plCategory'])){
445 continue;
446 }
447 foreach ($info['plCategory'] as $idx => $data){
448 if (preg_match('/^[0-9]+$/', $idx)){
449 if ($data == $module){
450 $objects[$object]= $object;
451 }
452 } else {
453 if ($idx == $module){
454 $objects[$object]= $object;
455 }
456 }
457 }
458 }
460 /* Search for per object ACLs.
461 */
462 $this->config->get_departments();
463 $this->config->make_idepartments();
465 foreach($this->ACL as $dn => $infos){
466 foreach($infos as $info){
467 $found = FALSE;
468 foreach($info['acl'] as $cat => $data){
470 /* Skip self acls? */
471 if($skip_self_acls && isset($data['0']) && strpos($data['0'], "s")) continue;
473 if(is_array($module)){
474 foreach($module as $mod){
475 if(preg_match("/^".normalizePreg($mod)."/",$cat)){
476 $found =TRUE;
477 break;
478 }
479 }
480 }else{
481 if(preg_match("/^".normalizePreg($module)."/",$cat)){
482 $found =TRUE;
483 break;
484 }
485 }
486 }
488 if($found && !isset($this->config->idepartments[$dn])){
489 while(!isset($this->config->idepartments[$dn]) && strpos($dn, ",")){
490 $dn = preg_replace("/^[^,]+,/","",$dn);
491 }
492 if(isset($this->config->idepartments[$dn])){
493 $deps[] = $dn;
494 }
495 }
496 }
497 }
499 /* For all gosaDepartments */
500 foreach ($this->config->departments as $dn){
501 if(!is_array($module)){
502 $module = array($module);
503 }
504 $acl = "";
505 foreach($module as $mod){
506 if(strpos($mod, '/')){
507 $acl.= $this->get_permissions($dn,$mod);
508 }else{
509 $acl.= $this->get_category_permissions($dn,$mod);
510 }
511 }
512 if($acl !== "") $deps[] = $dn;
513 }
515 $ACL_CACHE = &session::get('ACL_CACHE');
516 $ACL_CACHE['MODULE_DEPARTMENTS'][serialize($module)] = $deps;
517 return ($deps);
518 }
521 function mergeACL($acl, $type, $newACL)
522 {
523 if (strpos($newACL, 'w') !== FALSE && strpos($newACL, 'r') === FALSE){
524 $newACL .= "r";
525 }
527 foreach(str_split($newACL) as $char){
529 /* Ignore invalid characters */
530 if (!preg_match('/[rwcdm]/', $char)){
531 continue;
532 }
534 /* Skip permanent and subtree entries */
535 if (preg_match('/[sp]/', $acl[$char])){
536 continue;
537 }
539 switch ($type){
540 case 'psub':
541 $acl[$char]= 'p';
542 break;
544 case 'sub':
545 $acl[$char]= 's';
546 break;
548 case 'one':
549 $acl[$char]= 1;
550 break;
552 case 'base':
553 if ($acl[$char] != 1){
554 $acl[$char]= 0;
555 }
556 break;
557 }
558 }
560 return ($acl);
561 }
564 function cleanACL($acl, $reset= FALSE)
565 {
566 foreach ($acl as &$value){
568 /* Reset removes everything but 'p' */
569 if ($reset && $value != 'p'){
570 $value= "";
571 continue;
572 }
574 /* Decrease tree level */
575 if (is_int($value)){
576 if ($value){
577 $value--;
578 } else {
579 $value= "";
580 }
581 }
582 }
584 return ($acl);
585 }
588 /* #FIXME This could be logical wrong or could be optimized in the future
589 Return combined acls for a given category.
590 All acls will be combined like boolean AND
591 As example ('rwcdm' + 'rcd' + 'wrm'= 'r')
593 Results will be cached in $this->result_cache.
594 $this->result_cache will be resetted if load_acls is called.
595 */
596 function has_complete_category_acls($dn,$category)
597 {
598 $acl = "rwcdm";
599 $types = "rwcdm";
601 if(!is_string($category)){
602 trigger_error("category must be string");
603 $acl = "";
604 }else{
605 if(!isset($this->result_cache['has_complete_category_acls'][$dn][$category])) {
606 if (isset($this->ocMapping[$category])){
607 foreach($this->ocMapping[$category] as $oc){
609 /* Skip objectClass '0' (e.g. users/0) get_permissions will ever return '' ?? */
610 if($oc == "0") continue;
611 $tmp = $this->get_permissions($dn, $category."/".$oc);
612 for($i = 0 ; $i < strlen($types); $i++) {
613 if(!preg_match("/".$types[$i]."/",$tmp)){
614 $acl = preg_replace("/".$types[$i]."/","",$acl);
615 }
616 }
617 }
618 }else{
619 trigger_error("Invalid type of category ".$category);
620 $acl = "";
621 }
622 $this->result_cache['has_complete_category_acls'][$dn][$category] = $acl;
623 }else{
624 $acl = $this->result_cache['has_complete_category_acls'][$dn][$category];
625 }
626 }
627 return($acl);
628 }
631 /*! \brief Returns TRUE if the current user is configured in IGNORE_ACL=".." in your gosa.conf
632 @param Return Boolean TRUE if we have to skip ACL checks else FALSE.
633 */
634 function ignore_acl_for_current_user()
635 {
636 return($this->config->get_cfg_value("ignoreAcl") == $this->dn);
637 }
639 }
641 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
642 ?>