Code

34634669f6820a9cc018ef91773df2f297b9f89b
[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();
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){
129                 $new_acl['members'] = $aclc_value['members'];
130                 $aclc[$dn][] =$new_acl;
131               }
132             }      
133           }
134         }
135       }
136     }
138     /* ACL's read, sort for tree depth */
139     asort($aclp);
141     /* Sort in tree order */
142     foreach ($aclp as $dn => $acl){
143       /* Check if we need to keep this ACL */
144       foreach($aclc[$dn] as $idx => $type){
145         $interresting= FALSE;
146         
147         /* No members? This is good for all users... */
148         if (!count($type['members'])){
149           $interresting= TRUE;
150         } else {
152           /* Inspect members... */
153           foreach ($type['members'] as $grp => $grpdsc){
154             /* Some group inside the members that is relevant for us? */
155             if (in_array_ics(preg_replace('/^G:/', '', $grp), $this->groups)){
156               $interresting= TRUE;
157             }
159             /* User inside the members? */
160             if (preg_replace('/^U:/', '', $grp) == $this->dn){
161               $interresting= TRUE;
162             }
163           }
164         }
166         if ($interresting){
167           if (!isset($this->ACL[$dn])){
168             $this->ACL[$dn]= array();
169           }
170           $this->ACL[$dn][$idx]= $type;
171         }
172       }
174     }
175   }
178   function get_category_permissions($dn, $category)
179   {
180     /* If we are forced to skip ACLs checks for the current user 
181         then return all permissions.
182      */
183     if($this->ignore_acl_for_current_user()){
184       return("rwcdm");
185     }
187     /* Get list of objectClasses and get the permissions for it */
188     $acl= "";
189     if (isset($this->ocMapping[$category])){
190       foreach($this->ocMapping[$category] as $oc){
191         $acl.= $this->get_permissions($dn, $category."/".$oc);
192       }
193     }else{
194       trigger_error("ACL request for an invalid category (".$category.").");
195     }
197     return ($acl);
198   }
200   
201   /*! \brief Check if the given object (dn) is copyable
202       @param  String The object dn 
203       @param  String The acl  category (e.g. users) 
204       @param  String The acl  class (e.g. user) 
205       @return Boolean   TRUE if the given object is copyable else FALSE 
206   */
207   function is_copyable($dn, $object, $class)
208   {
209     return(preg_match("/r/",$this->has_complete_category_acls($dn, $object)));
210   }
213   /*! \brief Check if the given object (dn) is cutable
214       @param  String The object dn 
215       @param  String The acl  category (e.g. users) 
216       @param  String The acl  class (e.g. user) 
217       @return Boolean   TRUE if the given object is cutable else FALSE 
218   */
219   function is_cutable($dn, $object, $class)
220   {
221     $remove = preg_match("/d/",$this->get_permissions($dn,$object."/".$class));
222     $read   = preg_match("/r/",$this->has_complete_category_acls($dn, $object));
223     return($remove && $read);
224   }
227   /*! \brief  Checks if we are allowed to paste an object to the given destination ($dn)
228       @param  String The destination dn 
229       @param  String The acl  category (e.g. users) 
230       @param  String The acl  class (e.g. user) 
231       @return Boolean   TRUE if we are allowed to paste an object.
232   */
233   function is_pasteable($dn, $object)
234   {
235     return(preg_match("/w/",$this->has_complete_category_acls($dn, $object)));
236   }
239   /*! \brief  Checks if we are allowed to restore a snapshot for the given dn.
240       @param  String The destination dn 
241       @param  String The acl  category (e.g. users) 
242       @return Boolean   TRUE if we are allowed to restore a snapshot.
243   */
244   function allow_snapshot_restore($dn, $object)
245   {
246     if(!is_array($object)){
247       $object = array($object);
248     }
249     $r = $w = $c = TRUE;
250     foreach($object as $category){
251       $w &= preg_match("/w/",$this->has_complete_category_acls($dn, $category));
252       $c &= preg_match("/c/",$this->has_complete_category_acls($dn, $category));
253       $r &= preg_match("/r/",$this->has_complete_category_acls($dn, $category));
254 #     print_a(array($category => array($r.$w.$c)));
255     }
256     return($r && $w ); 
257   }  
260   /*! \brief  Checks if we are allowed to create a snapshot of the given dn.
261       @param  String The source dn 
262       @param  String The acl category (e.g. users) 
263       @return Boolean   TRUE if we are allowed to restore a snapshot.
264   */
265   function allow_snapshot_create($dn, $object)
266   {
267     if(!is_array($object)){
268       $object = array($object);
269     }
270     $r = $w = $c = TRUE;
271     foreach($object as $category){
272       $w &= preg_match("/w/",$this->has_complete_category_acls($dn, $category));
273       $c &= preg_match("/c/",$this->has_complete_category_acls($dn, $category));
274       $r &= preg_match("/r/",$this->has_complete_category_acls($dn, $category));
275 #      print_a(array($category => array($r.$w.$c)));
276     }
277     return($r) ; 
278   }  
281   function get_permissions($dn, $object, $attribute= "", $skip_write= FALSE)
282   {
283     /* If we are forced to skip ACLs checks for the current user 
284         then return all permissions.
285      */
286     if($this->ignore_acl_for_current_user()){
287       return("rwcdm");
288     }
290     /* Push cache answer? */
291     $ACL_CACHE = &session::get('ACL_CACHE');
292     if (isset($ACL_CACHE["$dn+$object+$attribute"])){
294       /* Remove write if needed */
295       if ($skip_write){
296         $ret = preg_replace('/w/', '', $ACL_CACHE["$dn+$object+$attribute"]);
297       }else{
298         $ret = $ACL_CACHE["$dn+$object+$attribute"];
299       } 
300       return($ret);
301     }
303     /* Get ldap object, for later filter checks 
304      */
305     $ldap = $this->config->get_ldap_link();
307     $acl= array("r" => "", "w" => "", "c" => "", "d" => "", "m" => "", "a" => "");
309     /* Build dn array */
310     $path= split(',', $dn);
311     $path= array_reverse($path);
314     /* Walk along the path to evaluate the acl */
315     $cpath= "";
316     foreach ($path as $element){
318       /* Clean potential ACLs for each level */
319       if(in_array($cpath,$this->config->departments)){
320         $acl= $this->cleanACL($acl);
321       }
323       if ($cpath == ""){
324         $cpath= $element;
325       } else {
326         $cpath= $element.','.$cpath;
327       }
329       if (isset($this->ACL[$cpath])){
331         /* Inspect this ACL, place the result into ACL */
332         foreach ($this->ACL[$cpath] as $subacl){
334           /* Reset? Just clean the ACL and turn over to the next one... */
335           if ($subacl['type'] == 'reset'){
336             $acl= $this->cleanACL($acl, TRUE);
337             continue;
338           }
340           if($subacl['type'] == "role") {
341             echo "role skipped";
342             continue;
343           }
345           /* With user filter */
346           if (isset($subacl['filter']) && !empty($subacl['filter'])){
347             $sdn = preg_replace("/^[^,]*+,/","",$dn);
348             $ldap->cd($sdn);
349             $ldap->ls($subacl['filter'],$sdn);
350             if(!$ldap->count()){
351               continue;
352             }else{
353               $found = FALSE; 
354               while($attrs = $ldap->fetch()){
355                 if($attrs['dn'] == $dn){
356                   $found = TRUE;
357                   break;
358                 }
359               }
360               if(!$found){
361                 continue;
362               }
363             }
364           }
366           /* Self ACLs? 
367            */
368           if(isset($subacl['acl'][$object][0]) && preg_match("/s/",$subacl['acl'][$object][0]) && $dn != $this->dn){
369             continue;
370           }
372           /* If attribute is "", we want to know, if we've *any* permissions here... */
373           if ($attribute == "" && isset($subacl['acl'][$object])){
374             foreach($subacl['acl'][$object] as $attr => $dummy){
375               $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl'][$object][$attr]);
376             }
377             continue;
378           }
380           /* Per attribute ACL? */
381           if (isset($subacl['acl'][$object][$attribute])){
382             $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl'][$object][$attribute]);
383             continue;
384           }
386           /* Per object ACL? */
387           if (isset($subacl['acl'][$object][0])){
388             $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl'][$object][0]);
389             continue;
390           }
392           /* Global ACL? */
393           if (isset($subacl['acl']['all'][0])){
394             $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl']['all'][0]);
395             continue;
396           }
397         }
398       }
399     }
401     /* If the requested ACL is for a container object, then alter 
402         ACLs by applying cleanACL a last time.
403      */
404     if(in_array($dn,$this->config->departments)){
405       $acl = $this->cleanACL($acl);
406     }
408     /* Assemble string */
409     $ret= "";
410     foreach ($acl as $key => $value){
411       if ($value !== ""){
412         $ret.= $key;
413       }
414     }
416     $ACL_CACHE["$dn+$object+$attribute"]= $ret;
418     /* Remove write if needed */
419     if ($skip_write){
420       $ret= preg_replace('/w/', '', $ret);
421     }
422     return ($ret);
423   }
426   /* Extract all departments that are accessible (direct or 'on the way' to an
427      accessible department) */
428   function get_module_departments($module)
429   {
430     
431     /* If we are forced to skip ACLs checks for the current user 
432         then return all departments as valid.
433      */
434     if($this->ignore_acl_for_current_user()){
435       return(array_keys($this->config->idepartments));
436     }
438     /* Use cached results if possilbe */
439     $ACL_CACHE = session::get('ACL_CACHE');
440     if(isset($ACL_CACHE['MODULE_DEPARTMENTS'][serialize($module)])){
441       return($ACL_CACHE['MODULE_DEPARTMENTS'][serialize($module)]);
442     }
444     global $plist;
446     $objects= array();
447     $deps= array();
449     /* Extract all relevant objects for this module from plist */
450     foreach ($plist->info as $object => $info){
451       if (!isset($info['plCategory'])){
452         continue;
453       }
454       foreach ($info['plCategory'] as $idx => $data){
455         if (preg_match('/^[0-9]+$/', $idx)){
456           if ($data == $module){
457             $objects[$object]= $object;
458           }
459         } else {
460           if ($idx == $module){
461             $objects[$object]= $object;
462           }
463         }
464       }
465     }
467     
469     /* Search for per object ACLs. 
470      */
471     $this->config->get_departments();
472     $this->config->make_idepartments();
474     foreach($this->ACL as $dn => $infos){
475       foreach($infos as $info){
476         $found = FALSE;
477         foreach($info['acl'] as $cat => $data){
478           if(is_array($module)){
479             foreach($module as $mod){
480               if(preg_match("/^".normalizePreg($mod)."/",$cat)){
481                 $found =TRUE;
482                 break;
483               }
484             }
485           }else{
486             if(preg_match("/^".normalizePreg($module)."/",$cat)){
487               $found =TRUE;
488               break;
489             }
490           }
491         } 
493         if($found && !isset($this->config->idepartments[$dn])){
494           while(!isset($this->config->idepartments[$dn]) && preg_match("/,/",$dn)){
495             $dn = preg_replace("/^[^,]+,/","",$dn);
496           }
497           if(isset($this->config->idepartments[$dn])){
498             $deps[] = $dn;
499           }
500         }
501       }
502     }
504     /* For all gosaDepartments */
505     foreach ($this->config->departments as $dn){
506       $acl= array("r" => "", "w" => "", "c" => "", "d" => "", "m" => "", "a" => "");
508       /* Build dn array */
509       $path= split(',', $dn);
510       $path= array_reverse($path);
512       /* Walk along the path to evaluate the acl */
513       $cpath= "";
514       foreach ($path as $element){
516         /* Clean potential ACLs for each level */
517         $acl= $this->cleanACL($acl);
519         if ($cpath == ""){
520           $cpath= $element;
521         } else {
522           $cpath= $element.','.$cpath;
523         }
524         if (isset($this->ACL[$cpath])){
526           /* Inspect this ACL, place the result into ACL */
527           foreach ($this->ACL[$cpath] as $subacl){
529             /* Reset? Just clean the ACL and turn over to the next one... */
530             if ($subacl['type'] == 'reset'){
531               $acl= $this->cleanACL($acl, TRUE);
532               continue;
533             }
534     
535             if($subacl['type'] == 'role'){
536               echo "role skipped";
537               continue;
538             }
540             /* Per object ACL? */
541             foreach ($objects as $object){
542               if (isset($subacl['acl']["$module/$object"])){
543                 foreach($subacl['acl']["$module/$object"] as $attribute => $dcl){
544                   $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl']["$module/$object"][$attribute]);
545                 }
546               }
547             }
549             /* Global ACL? */
550             if (isset($subacl['acl']["$module/all"][0])){
551               $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl']["$module/all"][0]);
552               continue;
553             }
555             /* Global ACL? */
556             if (isset($subacl['acl']["all"][0])){
557               $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl']["all"][0]);
558               continue;
559             }
560           }
561         }
562       }
564       /* Add department, if we have (some) permissions for the required module */
565       foreach ($acl as $val){
566         if ($val != ""){
567           $deps[]= $dn;
568           break;
569         }
570       }
571     }
572     
573     $ACL_CACHE = &session::get('ACL_CACHE');
574     $ACL_CACHE['MODULE_DEPARTMENTS'][serialize($module)] = $deps;
575     return ($deps);
576   }
579   function mergeACL($acl, $type, $newACL)
580   {
581     if (strpos($newACL, 'w') !== FALSE && strpos($newACL, 'r') === FALSE){
582       $newACL .= "r";
583     }
585     foreach(str_split($newACL) as $char){
587       /* Ignore invalid characters */
588       if (!preg_match('/[rwcdm]/', $char)){
589         continue;
590       }
592       /* Skip permanent and subtree entries */
593       if (preg_match('/[sp]/', $acl[$char])){
594         continue;
595       }
597       switch ($type){
598         case 'psub':
599           $acl[$char]= 'p';
600           break;
602         case 'sub':
603           $acl[$char]= 's';
604           break;
606         case 'one':
607           $acl[$char]= 1;
608           break;
610         case 'base':
611           if ($acl[$char] != 1){
612             $acl[$char]= 0;
613           }
614           break;
615       }
616     }
618     return ($acl);
619   }
622   function cleanACL($acl, $reset= FALSE)
623   {
624     foreach ($acl as &$value){
626       /* Reset removes everything but 'p' */
627       if ($reset && $value != 'p'){
628         $value= "";
629         continue;
630       }
632       /* Decrease tree level */
633       if (is_int($value)){
634         if ($value){
635           $value--;
636         } else {
637           $value= "";
638         }
639       }
640     }
642     return ($acl);
643   }
646   /* #FIXME This could be logical wrong or could be optimized in the future
647      Return combined acls for a given category. 
648      All acls will be combined like boolean AND 
649       As example ('rwcdm' + 'rcd' + 'wrm'= 'r') 
650     
651      Results will be cached in $this->result_cache.
652       $this->result_cache will be resetted if load_acls is called.
653   */
654   function has_complete_category_acls($dn,$category)
655   {
656     $acl    = "rwcdm";
657     $types  = "rwcdm";
659     if(!is_string($category)){
660       trigger_error("category must be string");   
661       $acl = "";
662     }else{
663       if(!isset($this->result_cache['has_complete_category_acls'][$dn][$category]))   {
664         if (isset($this->ocMapping[$category])){
665           foreach($this->ocMapping[$category] as $oc){
667             /* Skip objectClass '0' (e.g. users/0) get_permissions will ever return '' ??  */
668             if($oc == "0") continue;
669             $tmp =  $this->get_permissions($dn, $category."/".$oc);
670             for($i = 0 ; $i < strlen($types); $i++) {
671               if(!preg_match("/".$types[$i]."/",$tmp)){ 
672                 $acl = preg_replace("/".$types[$i]."/","",$acl);
673               }
674             }
675           }
676         }else{
677           trigger_error("Invalid type of category ".$category);
678           $acl = "";
679         }
680         $this->result_cache['has_complete_category_acls'][$dn][$category] = $acl;
681       }else{
682         $acl = $this->result_cache['has_complete_category_acls'][$dn][$category];
683       }
684     }
685     return($acl);
686   }
688  
689   /*! \brief  Returns TRUE if the current user is configured in IGNORE_ACL=".." in your gosa.conf 
690       @param  Return Boolean TRUE if we have to skip ACL checks else FALSE.
691    */ 
692   function ignore_acl_for_current_user()
693   {
694     return(isset($this->config->current['IGNORE_ACL']) && $this->config->current['IGNORE_ACL'] == $this->dn);
695   }
699 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
700 ?>