Code

Updated redhat build script
[gosa.git] / gosa-core / include / class_userinfo.inc
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();
39   var $ignoreACl = FALSE;
40   var $ACLperPath = array();
41   var $ACLperPath_usesFilter = array();
43   /* get acl's an put them into the userinfo object
44      attr subtreeACL (userdn:components, userdn:component1#sub1#sub2,component2,...) */
45   function userinfo(&$config, $userdn){
46     $this->config= &$config;
47     $ldap= $this->config->get_ldap_link();
48     $ldap->cat($userdn,array('sn', 'givenName', 'uid', 'gidNumber', 'preferredLanguage', 'gosaUnitTag'));
49     $attrs= $ldap->fetch();
51     if (isset($attrs['givenName'][0]) && isset($attrs['sn'][0])){
52       $this->cn= $attrs['givenName'][0]." ".$attrs['sn'][0];
53     } else {
54       $this->cn= $attrs['uid'][0];
55     }
56     if (isset($attrs['gidNumber'][0])){
57       $this->gidNumber= $attrs['gidNumber'][0];
58     }
60     /* Assign user language */
61     if (isset($attrs['preferredLanguage'][0])){
62       $this->language= $attrs['preferredLanguage'][0];
63     }
65     if (isset($attrs['gosaUnitTag'][0])){
66       $this->gosaUnitTag= $attrs['gosaUnitTag'][0];
67     }
69     $this->dn= $userdn;
70     $this->uid= $attrs['uid'][0];
71     $this->ip= $_SERVER['REMOTE_ADDR'];
73     $this->ignoreACL = ($this->config->get_cfg_value("ignoreAcl") == $this->dn);
75     /* Initialize ACL_CACHE */
76     $this->reset_acl_cache();
77   }
80   public function reset_acl_cache()
81   {
82     /* Initialize ACL_CACHE */
83     session::global_set('ACL_CACHE',array());
84   }
86   function loadACL()
87   {
88     $this->ACL= array();    
89     $this->groups= array();    
90     $this->result_cache =array();
91     $this->reset_acl_cache();
92     $ldap= $this->config->get_ldap_link();
93     $ldap->cd($this->config->current['BASE']);
95     /* Get member groups... */
96     $ldap->search("(&(objectClass=posixGroup)(memberUid=".$this->uid."))", array('dn'));
97     while ($attrs= $ldap->fetch()){
98       $this->groups[$attrs['dn']]= $attrs['dn'];
99     }
101     /* Crawl through ACLs and move relevant to the tree */
102     $ldap->search("(objectClass=gosaACL)", array('dn', 'gosaAclEntry'));
103     $aclp= array();
104     $aclc= array();
105     while ($attrs= $ldap->fetch()){
107       /* Insert links in ACL array */
108       $aclp[$attrs['dn']]= substr_count($attrs['dn'], ',');
109       $aclc[$attrs['dn']]= array();
110       $ol= array();
111       for($i= 0; $i<$attrs['gosaAclEntry']['count']; $i++){
112         $ol= array_merge($ol, @acl::explodeAcl($attrs['gosaAclEntry'][$i]));
113       }
114       $aclc[$attrs['dn']]= $ol;
115     }
117     /* Resolve roles here. 
118      */
119     foreach($aclc as $dn => $data){
120       foreach($data as $prio => $aclc_value)  {
121         if($aclc_value['type'] == "role"){
123           unset($aclc[$dn][$prio]);
125           $ldap->cat($aclc_value['acl'],array("gosaAclTemplate"));
126           $attrs = $ldap->fetch();
128           if(isset($attrs['gosaAclTemplate'])){
129             for($i= 0; $i<$attrs['gosaAclTemplate']['count']; $i++){
130               $tmp = @acl::explodeAcl($attrs['gosaAclTemplate'][$i]);  
132               foreach($tmp as $new_acl){
134                 /* Keep non role attributes here! */
135                 $new_acl['filter'] = $aclc_value['filter'];
136                 $new_acl['members'] = $aclc_value['members'];
137                 $aclc[$dn][] =$new_acl;
138               }
139             }      
140           }
141         }
142       }
143     }
145     /* ACL's read, sort for tree depth */
146     asort($aclp);
148     /* Sort in tree order */
149     foreach ($aclp as $dn => $acl){
150       /* Check if we need to keep this ACL */
151       foreach($aclc[$dn] as $idx => $type){
152         $interresting= FALSE;
153         
154         /* No members? This is good for all users... */
155         if (!count($type['members'])){
156           $interresting= TRUE;
157         } else {
159           /* Inspect members... */
160           foreach ($type['members'] as $grp => $grpdsc){
161             /* Some group inside the members that is relevant for us? */
162             if (in_array_ics(preg_replace('/^G:/', '', $grp), $this->groups)){
163               $interresting= TRUE;
164             }
166             /* User inside the members? */
167             if (preg_replace('/^U:/', '', $grp) == $this->dn){
168               $interresting= TRUE;
169             }
170           }
171         }
173         if ($interresting){
174           if (!isset($this->ACL[$dn])){
175             $this->ACL[$dn]= array();
176           }
177           $this->ACL[$dn][$idx]= $type;
178         }
179       }
180     }
182     /* Create an array which represenet all relevant permissions settings 
183         per dn.
185       The array will look like this:
186       
187       .     ['ou=base']        ['ou=base']          = array(ACLs);
188       .     
189       .     ['ou=dep1,ou=base']['ou=dep1,ou=base']  = array(ACLs);
190       .                        ['ou=base']          = array(ACLs);
193       For object located in 'ou=dep1,ou=base' we have to both ACLs,
194        for objects in 'ou=base' we only have to apply on ACL.
195      */
196     $without_self_acl = $all_acl = array();
197     foreach($this->ACL as $dn => $acl){
198       $sdn =$dn;
199       $first= TRUE; // Run at least once 
200       while(strpos($dn,",") !== FALSE || $first){
201         $first = FALSE;
202         if(isset($this->ACL[$dn])){
203           $all_acl[$sdn][$dn] = $this->ACL[$dn];
204           $without_self_acl[$sdn][$dn] = $this->ACL[$dn]; 
205           foreach($without_self_acl[$sdn][$dn] as $acl_id => $acl_set){
206   
207             /* Remember which ACL set has speicial user filter 
208              */
209             if(isset($acl_set['filter']{1})){
210               $this->ACLperPath_usesFilter[$sdn] = TRUE;
211             }
212           
213             /* Remove all acl entries which are especially for the current user (self acl)
214              */
215             foreach($acl_set['acl'] as $object => $object_acls){
216               if(strpos($object_acls[0],"s")){
217                 unset($without_self_acl[$sdn][$dn][$acl_id]['acl'][$object]);
218               }
219             }
220           }
221         }
222         $dn = preg_replace("/^[^,]*+,/","",$dn);
223       }
224     } 
225     $this->ACLperPath =$without_self_acl;
227     /* Append Self entry */
228     $dn = $this->dn;
229     while(strpos($dn,",") && !isset($all_acl[$dn])){
230       $dn = preg_replace("/^[^,]*+,/","",$dn);
231     }
232     if(isset($all_acl[$dn])){
233       $this->ACLperPath[$this->dn] = $all_acl[$dn];
234     }
235   }
238   function get_category_permissions($dn, $category, $any_acl = FALSE)
239   {
240     return(@$this->get_permissions($dn,$category.'/0',""));
241   }
243   
244   /*! \brief Check if the given object (dn) is copyable
245       @param  String The object dn 
246       @param  String The acl  category (e.g. users) 
247       @param  String The acl  class (e.g. user) 
248       @return Boolean   TRUE if the given object is copyable else FALSE 
249   */
250   function is_copyable($dn, $object, $class)
251   {
252     return(preg_match("/r/",$this->has_complete_category_acls($dn, $object)));
253   }
256   /*! \brief Check if the given object (dn) is cutable
257       @param  String The object dn 
258       @param  String The acl  category (e.g. users) 
259       @param  String The acl  class (e.g. user) 
260       @return Boolean   TRUE if the given object is cutable else FALSE 
261   */
262   function is_cutable($dn, $object, $class)
263   {
264     $remove = preg_match("/d/",$this->get_permissions($dn,$object."/".$class));
265     $read   = preg_match("/r/",$this->has_complete_category_acls($dn, $object));
266     return($remove && $read);
267   }
270   /*! \brief  Checks if we are allowed to paste an object to the given destination ($dn)
271       @param  String The destination dn 
272       @param  String The acl  category (e.g. users) 
273       @param  String The acl  class (e.g. user) 
274       @return Boolean   TRUE if we are allowed to paste an object.
275   */
276   function is_pasteable($dn, $object)
277   {
278     return(preg_match("/w/",$this->has_complete_category_acls($dn, $object)));
279   }
282   /*! \brief  Checks if we are allowed to restore a snapshot for the given dn.
283       @param  String The destination dn 
284       @param  String The acl  category (e.g. users) 
285       @return Boolean   TRUE if we are allowed to restore a snapshot.
286   */
287   function allow_snapshot_restore($dn, $object)
288   {
289     if(!is_array($object)){
290       $object = array($object);
291     }
292     $r = $w = TRUE;
293     foreach($object as $category){
294       $w &= preg_match("/w/",$this->has_complete_category_acls($dn, $category));
295       $r &= preg_match("/r/",$this->has_complete_category_acls($dn, $category));
296     }
297     return($r && $w ); 
298   }  
301   /*! \brief  Checks if we are allowed to create a snapshot of the given dn.
302       @param  String The source dn 
303       @param  String The acl category (e.g. users) 
304       @return Boolean   TRUE if we are allowed to restore a snapshot.
305   */
306   function allow_snapshot_create($dn, $object)
307   {
308     if(!is_array($object)){
309       $object = array($object);
310     }
311     $r = TRUE;
312     foreach($object as $category){
313       $r &= preg_match("/r/",$this->has_complete_category_acls($dn, $category));
314     }
315     return($r) ; 
316   }  
319   function get_permissions($dn, $object, $attribute= "", $skip_write= FALSE)
320   {
321     /* If we are forced to skip ACLs checks for the current user 
322         then return all permissions.
323      */
324     if($this->ignore_acl_for_current_user()){
325       if($skip_write){
326         return("rcdm");
327       }
328       return("rwcdm");
329     }
331     /* Push cache answer? */
332     $ACL_CACHE = &session::global_get('ACL_CACHE');
333     if (isset($ACL_CACHE["$dn+$object+$attribute"])){
334       $ret = $ACL_CACHE["$dn+$object+$attribute"];
335       if($skip_write){
336         $ret = str_replace(array('w','c','d','m'), '',$ret);
337       }
338       return($ret);
339     }
341     /* Detect the set of ACLs we have to check for this object 
342      */
343     $adn = $dn;
344     while(!isset($this->ACLperPath[$adn]) && strpos($adn,",") !== FALSE){
345       $adn = preg_replace("/^[^,]*+,/","",$adn);
346     }
347     if(isset($this->ACLperPath[$adn])){
348       $ACL = $this->ACLperPath[$adn];
349     }else{
350       $ACL_CACHE["$dn+$object+$attribute"] = "";
351       return("");
352     }
353  
354     /* If we do not need to respect any user-filter settings 
355         we can skip the per object ACL checks.
356      */
357     $orig_dn= $dn;
358     if(!isset($this->ACLperPath_usesFilter[$adn])){
359       $dn = $adn;
360       if (isset($ACL_CACHE["$dn+$object+$attribute"])){
361         $ret = $ACL_CACHE["$dn+$object+$attribute"];
362         if(!isset($ACL_CACHE["$orig_dn+$object+$attribute"])){
363           $ACL_CACHE["$orig_dn+$object+$attribute"] = $ACL_CACHE["$dn+$object+$attribute"];
364         }
365         if($skip_write){
366           $ret = str_replace('w','',$ret);
367         }
368         return($ret);
369       }
370     }
371  
372     /* Get ldap object, for later filter checks 
373      */
374     $ldap = $this->config->get_ldap_link();
376     $acl= array("r" => "", "w" => "", "c" => "", "d" => "", "m" => "", "a" => "");
378     /* Build dn array */
379     $path= split(',', $dn);
380     $path= array_reverse($path);
382     /* Walk along the path to evaluate the acl */
383     $cpath= "";
384     foreach ($path as $element){
386       /* Clean potential ACLs for each level */
387                         if(isset($this->config->idepartments[$cpath])){
388         $acl= $this->cleanACL($acl);
389       }
391       if ($cpath == ""){
392         $cpath= $element;
393       } else {
394         $cpath= $element.','.$cpath;
395       }
397       if (isset($ACL[$cpath])){
399         /* Inspect this ACL, place the result into ACL */
400         foreach ($ACL[$cpath] as $subacl){
402           /* Reset? Just clean the ACL and turn over to the next one... */
403           if ($subacl['type'] == 'reset'){
404             $acl= $this->cleanACL($acl, TRUE);
405             continue;
406           }
408           if($subacl['type'] == "role") {
409             echo "role skipped";
410             continue;
411           }
413           /* With user filter */
414           if (isset($subacl['filter']) && !empty($subacl['filter'])){
415             $id = $dn."-".$subacl['filter'];
416             if(!isset($ACL_CACHE['FILTER'][$id])){
417               $ACL_CACHE['FILTER'][$id] = $ldap->object_match_filter($dn,$subacl['filter']);
418             }
419             if(!$ACL_CACHE['FILTER'][$id]){
420               continue;
421             }
422           }
424           /* Self ACLs? 
425            */
426           if($dn != $this->dn && isset($subacl['acl'][$object][0]) && (strpos($subacl['acl'][$object][0],"s") !== FALSE)){
427             continue;
428           }
430           /* If attribute is "", we want to know, if we've *any* permissions here... 
431               Merge global class ACLs [0] with attributes specific ACLs [attribute].
432            */
433           if ($attribute == "" && isset($subacl['acl'][$object])){
434             foreach($subacl['acl'][$object] as $attr => $dummy){
435               $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl'][$object][$attr]);
436             }
437             continue;
438           }
440           /* Per attribute ACL? */
441           if (isset($subacl['acl'][$object][$attribute])){
442             $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl'][$object][$attribute]);
443             continue;
444           }
446           /* Per object ACL? */
447           if (isset($subacl['acl'][$object][0])){
448             $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl'][$object][0]);
449             continue;
450           }
452           /* Global ACL? */
453           if (isset($subacl['acl']['all'][0])){
454             $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl']['all'][0]);
455             continue;
456           }
458           /* Category ACLs    (e.g. $object = "user/0")
459            */
460           if(strstr($object,"/0")){
461             $ocs = preg_replace("/\/0$/","",$object);
462             if(isset($this->ocMapping[$ocs])){
464               /* if $attribute is "", then check every single attribute for this object.
465                  if it is 0, then just check the object category ACL.
466                */
467               if($attribute == ""){    
468                 foreach($this->ocMapping[$ocs] as $oc){
469                   if (isset($subacl['acl'][$ocs.'/'.$oc])){
471                     if($dn != $this->dn && strpos($subacl['acl'][$ocs.'/'.$oc][0],"s") !== FALSE) continue;
473                     foreach($subacl['acl'][$ocs.'/'.$oc] as $attr => $dummy){
474                       $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl'][$ocs.'/'.$oc][$attr]);
475                     }
476                     continue;
477                   }
478                 }
479               }else{
480                 if(isset($subacl['acl'][$ocs.'/'.$oc][0])){
481                   if($dn != $this->dn && strpos($subacl['acl'][$ocs.'/'.$oc][0],"s") !== FALSE) continue;
482                   $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl'][$ocs.'/'.$oc][0]);
483                 }
484               }
485             }
486             continue;
487           }
488         }
489       }
490     }
492     /* If the requested ACL is for a container object, then alter 
493         ACLs by applying cleanACL a last time.
494      */
495     if(isset($this->config->idepartments[$dn])){
496       $acl = $this->cleanACL($acl);
497     }
499     /* Assemble string */
500     $ret= "";
501     foreach ($acl as $key => $value){
502       if ($value !== ""){
503         $ret.= $key;
504       }
505     }
507     $ACL_CACHE["$dn+$object+$attribute"]= $ret;
508     $ACL_CACHE["$orig_dn+$object+$attribute"]= $ret;
510     /* Remove write if needed */
511     if ($skip_write){
512       $ret = str_replace(array('w','c','d','m'), '',$ret);
513     }
514     return ($ret);
515   }
518   /* Extract all departments that are accessible (direct or 'on the way' to an
519      accessible department) */
520   function get_module_departments($module, $skip_self_acls = FALSE )
521   {
522     /* If we are forced to skip ACLs checks for the current user 
523         then return all departments as valid.
524      */
525     if($this->ignore_acl_for_current_user()){
526       return(array_keys($this->config->idepartments));
527     }
529     /* Use cached results if possilbe */
530     $ACL_CACHE = &session::global_get('ACL_CACHE');
532     if(!is_array($module)){
533       $module = array($module);
534     }
536     global $plist;
537     $res = array();
538     foreach($module as $mod){
539       if(isset($ACL_CACHE['MODULE_DEPARTMENTS'][$mod])){
540         $res = array_merge($res,$ACL_CACHE['MODULE_DEPARTMENTS'][$mod]);
541         continue;
542       }
544       $deps = array();
546       /* Search for per object ACLs */
547       foreach($this->ACL as $dn => $infos){
548         foreach($infos as $info){
549           $found = FALSE;
550           foreach($info['acl'] as $cat => $data){
552             /* Skip self acls? */
553             if($skip_self_acls && isset($data['0']) && (strpos($data['0'], "s") !== FALSE)) continue;
554             if(preg_match("/^".preg_quote($mod, '/')."/",$cat)){
555               $found =TRUE;
556               break;
557             }
558           } 
560           if($found && !isset($this->config->idepartments[$dn])){
561             while(!isset($this->config->idepartments[$dn]) && strpos($dn, ",")){
562               $dn = preg_replace("/^[^,]+,/","",$dn);
563             }
564             if(isset($this->config->idepartments[$dn])){
565               $deps[$dn] = $dn;
566             }
567           }
568         }
569       }
571       /* For all gosaDepartments */
572       foreach ($this->config->departments as $dn){
573         if(isset($deps[$dn])) continue;
574         $acl = "";
575         if(strpos($mod, '/')){
576           $acl.=  $this->get_permissions($dn,$mod);
577         }else{
578           $acl.=  $this->get_category_permissions($dn,$mod,TRUE);
579         }
580         if(!empty($acl)) {
581           $deps[$dn] = $dn;
582         }
583       }
585       $ACL_CACHE['MODULE_DEPARTMENTS'][$mod] = $deps;
586       $res = array_merge($res,$deps);
587     }
589     return (array_values($res));
590   }
593   function mergeACL($acl, $type, $newACL)
594   {
595                 $at= array("psub" => "p", "sub" => "s", "one" => "1");
597     if (strpos($newACL, 'w') !== FALSE && strpos($newACL, 'r') === FALSE){
598       $newACL .= "r";
599     }
601                 /* Ignore invalid characters */
602                 $newACL= preg_replace('/[^rwcdm]/', '', $newACL);
604     foreach(str_split($newACL) as $char){
606       /* Skip "self" ACLs without combination of rwcdm, they have no effect.
607          -self flag without read/write/create/...
608        */
609       if(empty($char)) continue;
611       /* Skip permanent and subtree entries */
612       if (preg_match('/[sp]/', $acl[$char])){
613         continue;
614       }
616                         if ($type == "base" && $acl[$char] != 1) {
617                                 $acl[$char]= 0;
618                         } else {
619         $acl[$char]= $at[$type];
620                         }
621     }
623     return ($acl);
624   }
627   function cleanACL($acl, $reset= FALSE)
628   {
629     foreach ($acl as $key => $value){
631       /* Continue, if value is empty or permanent */
632       if ($value == "" || $value == "p") {
633             continue;
634       }
636       /* Reset removes everything but 'p' */
637       if ($reset && $value != 'p'){
638         $acl[$key]= "";
639         continue;
640       }
642       /* Decrease tree level */
643       if (is_int($value)){
644         if ($value){
645           $acl[$key]--;
646         } else {
647           $acl[$key]= "";
648         }
649       }
650     }
652     return ($acl);
653   }
656   /* #FIXME This could be logical wrong or could be optimized in the future
657      Return combined acls for a given category. 
658      All acls will be combined like boolean AND 
659       As example ('rwcdm' + 'rcd' + 'wrm'= 'r') 
660     
661      Results will be cached in $this->result_cache.
662       $this->result_cache will be resetted if load_acls is called.
663   */
664   function has_complete_category_acls($dn,$category)
665   {
666     $acl    = "rwcdm";
667     $types  = "rwcdm";
669     if(!is_string($category)){
670       trigger_error("category must be string");   
671       $acl = "";
672     }else{
673       if(!isset($this->result_cache['has_complete_category_acls'][$dn][$category]))   {
674         if (isset($this->ocMapping[$category])){
675           foreach($this->ocMapping[$category] as $oc){
677             /* Skip objectClass '0' (e.g. users/0) get_permissions will ever return '' ??  */
678             if($oc == "0") continue;
679             $tmp =  $this->get_permissions($dn, $category."/".$oc);
680             for($i = 0, $l= strlen($types); $i < $l; $i++) {
681               if(!preg_match("/".$types[$i]."/",$tmp)){ 
682                 $acl = preg_replace("/".$types[$i]."/","",$acl);
683               }
684             }
685           }
686         }else{
687           trigger_error("Invalid type of category ".$category);
688           $acl = "";
689         }
690         $this->result_cache['has_complete_category_acls'][$dn][$category] = $acl;
691       }else{
692         $acl = $this->result_cache['has_complete_category_acls'][$dn][$category];
693       }
694     }
695     return($acl);
696   }
698  
699   /*! \brief  Returns TRUE if the current user is configured in IGNORE_ACL=".." in your gosa.conf 
700       @param  Return Boolean TRUE if we have to skip ACL checks else FALSE.
701    */ 
702   function ignore_acl_for_current_user()
703   {
704     return($this->ignoreACL);
705   }
709 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
710 ?>