Code

7e2023a27ae2f2264f4aca0038d534f0ad5d4fad
[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;
41   /* get acl's an put them into the userinfo object
42      attr subtreeACL (userdn:components, userdn:component1#sub1#sub2,component2,...) */
43   function userinfo(&$config, $userdn){
44     $this->config= &$config;
45     $ldap= $this->config->get_ldap_link();
46     $ldap->cat($userdn,array('sn', 'givenName', 'uid', 'gidNumber', 'preferredLanguage', 'gosaUnitTag'));
47     $attrs= $ldap->fetch();
49     if (isset($attrs['givenName'][0]) && isset($attrs['sn'][0])){
50       $this->cn= $attrs['givenName'][0]." ".$attrs['sn'][0];
51     } else {
52       $this->cn= $attrs['uid'][0];
53     }
54     if (isset($attrs['gidNumber'][0])){
55       $this->gidNumber= $attrs['gidNumber'][0];
56     }
58     /* Assign user language */
59     if (isset($attrs['preferredLanguage'][0])){
60       $this->language= $attrs['preferredLanguage'][0];
61     }
63     if (isset($attrs['gosaUnitTag'][0])){
64       $this->gosaUnitTag= $attrs['gosaUnitTag'][0];
65     }
67     $this->dn= $userdn;
68     $this->uid= $attrs['uid'][0];
69     $this->ip= $_SERVER['REMOTE_ADDR'];
71     $this->ignoreACL = ($this->config->get_cfg_value("ignoreAcl") == $this->dn);
73     /* Initialize ACL_CACHE */
74     $this->reset_acl_cache();
75   }
78   public function reset_acl_cache()
79   {
80     /* Initialize ACL_CACHE */
81     session::set('ACL_CACHE',array());
82   }
84   function loadACL()
85   {
86     $this->ACL= array();    
87     $this->groups= array();    
88     $this->result_cache =array();
89     $this->reset_acl_cache();
90     $ldap= $this->config->get_ldap_link();
91     $ldap->cd($this->config->current['BASE']);
93     /* Get member groups... */
94     $ldap->search("(&(objectClass=posixGroup)(memberUid=".$this->uid."))", array('dn'));
95     while ($attrs= $ldap->fetch()){
96       $this->groups[$attrs['dn']]= $attrs['dn'];
97     }
99     /* Crawl through ACLs and move relevant to the tree */
100     $ldap->search("(objectClass=gosaACL)", array('dn', 'gosaAclEntry'));
101     $aclp= array();
102     $aclc= array();
103     while ($attrs= $ldap->fetch()){
105       /* Insert links in ACL array */
106       $aclp[$attrs['dn']]= substr_count($attrs['dn'], ',');
107       $aclc[$attrs['dn']]= array();
108       $ol= array();
109       for($i= 0; $i<$attrs['gosaAclEntry']['count']; $i++){
110         $ol= array_merge($ol, @acl::explodeAcl($attrs['gosaAclEntry'][$i]));
111       }
112       $aclc[$attrs['dn']]= $ol;
113     }
115     /* Resolve roles here. 
116      */
117     foreach($aclc as $dn => $data){
118       foreach($data as $prio => $aclc_value)  {
119         if($aclc_value['type'] == "role"){
121           unset($aclc[$dn][$prio]);
123           $ldap->cat($aclc_value['acl'],array("gosaAclTemplate"));
124           $attrs = $ldap->fetch();
126           if(isset($attrs['gosaAclTemplate'])){
127             for($i= 0; $i<$attrs['gosaAclTemplate']['count']; $i++){
128               $tmp = @acl::explodeAcl($attrs['gosaAclTemplate'][$i]);  
130               foreach($tmp as $new_acl){
132                 /* Keep non role attributes here! */
133                 $new_acl['filter'] = $aclc_value['filter'];
134                 $new_acl['members'] = $aclc_value['members'];
135                 $aclc[$dn][] =$new_acl;
136               }
137             }      
138           }
139         }
140       }
141     }
143     /* ACL's read, sort for tree depth */
144     asort($aclp);
146     /* Sort in tree order */
147     foreach ($aclp as $dn => $acl){
148       /* Check if we need to keep this ACL */
149       foreach($aclc[$dn] as $idx => $type){
150         $interresting= FALSE;
151         
152         /* No members? This is good for all users... */
153         if (!count($type['members'])){
154           $interresting= TRUE;
155         } else {
157           /* Inspect members... */
158           foreach ($type['members'] as $grp => $grpdsc){
159             /* Some group inside the members that is relevant for us? */
160             if (in_array_ics(preg_replace('/^G:/', '', $grp), $this->groups)){
161               $interresting= TRUE;
162             }
164             /* User inside the members? */
165             if (preg_replace('/^U:/', '', $grp) == $this->dn){
166               $interresting= TRUE;
167             }
168           }
169         }
171         if ($interresting){
172           if (!isset($this->ACL[$dn])){
173             $this->ACL[$dn]= array();
174           }
175           $this->ACL[$dn][$idx]= $type;
176         }
177       }
179     }
180   }
183   function get_category_permissions($dn, $category, $any_acl = FALSE)
184   {
185     /* If we are forced to skip ACLs checks for the current user 
186         then return all permissions.
187      */
188     if($this->ignore_acl_for_current_user()){
189       return("rwcdm");
190     }
192     /* Ensure that we only cache relevant ACL settings 
193      */
194 #    while(!isset($this->ACL[$dn]) && preg_match("/,/",$dn)){
195 #      $dn = preg_replace("/^[^,]*+,/","",$dn);
196 #    }
198     $ACL_CACHE = &session::get("ACL_CACHE");
199     $id = $dn."+".$category."+".$any_acl;
200     if(isset($ACL_CACHE['CATEGORY_ACL'][$id])){
201       return($ACL_CACHE['CATEGORY_ACL'][$id]);
202     }
205     /* Get list of objectClasses and get the permissions for it */
206     $acl= "";
207     if (isset($this->ocMapping[$category])){
208       foreach($this->ocMapping[$category] as $oc){
209         $acl.= $this->get_permissions($dn, $category."/".$oc);
210         if($any_acl && !empty($acl)){
211           break;
212         }
213       }
214     }else{
215       trigger_error("ACL request for an invalid category (".$category.").");
216     }
217     $ACL_CACHE = &session::get("ACL_CACHE");
218     $ACL_CACHE['CATEGORY_ACL'][$id] = $acl;
219     return ($acl);
220   }
222   
223   /*! \brief Check if the given object (dn) is copyable
224       @param  String The object dn 
225       @param  String The acl  category (e.g. users) 
226       @param  String The acl  class (e.g. user) 
227       @return Boolean   TRUE if the given object is copyable else FALSE 
228   */
229   function is_copyable($dn, $object, $class)
230   {
231     return(preg_match("/r/",$this->has_complete_category_acls($dn, $object)));
232   }
235   /*! \brief Check if the given object (dn) is cutable
236       @param  String The object dn 
237       @param  String The acl  category (e.g. users) 
238       @param  String The acl  class (e.g. user) 
239       @return Boolean   TRUE if the given object is cutable else FALSE 
240   */
241   function is_cutable($dn, $object, $class)
242   {
243     $remove = preg_match("/d/",$this->get_permissions($dn,$object."/".$class));
244     $read   = preg_match("/r/",$this->has_complete_category_acls($dn, $object));
245     return($remove && $read);
246   }
249   /*! \brief  Checks if we are allowed to paste an object to the given destination ($dn)
250       @param  String The destination dn 
251       @param  String The acl  category (e.g. users) 
252       @param  String The acl  class (e.g. user) 
253       @return Boolean   TRUE if we are allowed to paste an object.
254   */
255   function is_pasteable($dn, $object)
256   {
257     return(preg_match("/w/",$this->has_complete_category_acls($dn, $object)));
258   }
261   /*! \brief  Checks if we are allowed to restore a snapshot for the given dn.
262       @param  String The destination dn 
263       @param  String The acl  category (e.g. users) 
264       @return Boolean   TRUE if we are allowed to restore a snapshot.
265   */
266   function allow_snapshot_restore($dn, $object)
267   {
268     if(!is_array($object)){
269       $object = array($object);
270     }
271     $r = $w = TRUE;
272     foreach($object as $category){
273       $w &= preg_match("/w/",$this->has_complete_category_acls($dn, $category));
274       $r &= preg_match("/r/",$this->has_complete_category_acls($dn, $category));
275     }
276     return($r && $w ); 
277   }  
280   /*! \brief  Checks if we are allowed to create a snapshot of the given dn.
281       @param  String The source dn 
282       @param  String The acl category (e.g. users) 
283       @return Boolean   TRUE if we are allowed to restore a snapshot.
284   */
285   function allow_snapshot_create($dn, $object)
286   {
287     if(!is_array($object)){
288       $object = array($object);
289     }
290     $r = TRUE;
291     foreach($object as $category){
292       $r &= preg_match("/r/",$this->has_complete_category_acls($dn, $category));
293     }
294     return($r) ; 
295   }  
298   function get_permissions($dn, $object, $attribute= "", $skip_write= FALSE)
299   {
300     /* If we are forced to skip ACLs checks for the current user 
301         then return all permissions.
302      */
303     if($this->ignore_acl_for_current_user()){
304       if($skip_write){
305         return("rcdm");
306       }
307       return("rwcdm");
308     }
310     /* Ensure that we only cache relevant ACL settings 
311      */
312 #    while(!isset($this->ACL[$dn]) && preg_match("/,/",$dn)){
313 #      $dn = preg_replace("/^[^,]+,/","",$dn);
314 #    }
316     /* Push cache answer? */
317     $ACL_CACHE = &session::get('ACL_CACHE');
318     if (isset($ACL_CACHE["$dn+$object+$attribute"])){
320       /* Remove write if needed */
321       if ($skip_write){
322         $ret = preg_replace('/w/', '', $ACL_CACHE["$dn+$object+$attribute"]);
323       }else{
324         $ret = $ACL_CACHE["$dn+$object+$attribute"];
325       } 
326       return($ret);
327     }
329     /* Get ldap object, for later filter checks 
330      */
331     $ldap = $this->config->get_ldap_link();
333     $acl= array("r" => "", "w" => "", "c" => "", "d" => "", "m" => "", "a" => "");
335     /* Build dn array */
336     $path= split(',', $dn);
337     $path= array_reverse($path);
339     /* Walk along the path to evaluate the acl */
340     $cpath= "";
341     foreach ($path as $element){
343       /* Clean potential ACLs for each level */
344                         if(isset($this->config->idepartments[$cpath])){
345         $acl= $this->cleanACL($acl);
346       }
348       if ($cpath == ""){
349         $cpath= $element;
350       } else {
351         $cpath= $element.','.$cpath;
352       }
354       if (isset($this->ACL[$cpath])){
356         /* Inspect this ACL, place the result into ACL */
357         foreach ($this->ACL[$cpath] as $subacl){
359           /* Reset? Just clean the ACL and turn over to the next one... */
360           if ($subacl['type'] == 'reset'){
361             $acl= $this->cleanACL($acl, TRUE);
362             continue;
363           }
365           if($subacl['type'] == "role") {
366             echo "role skipped";
367             continue;
368           }
370           /* With user filter */
371           if (isset($subacl['filter']) && !empty($subacl['filter'])){
372             $id = $dn."-".$subacl['filter'];
373             if(!isset($ACL_CACHE['FILTER'][$id])){
374               $ACL_CACHE['FILTER'][$id] = $ldap->object_match_filter($dn,$subacl['filter']);
375             }
376             if(!$ACL_CACHE['FILTER'][$id]){
377               continue;
378             }
379           }
381           /* Self ACLs? 
382            */
383           if($dn != $this->dn && isset($subacl['acl'][$object][0]) && strpos($subacl['acl'][$object][0],"s")){
384             continue;
385           }
387           /* If attribute is "", we want to know, if we've *any* permissions here... 
388               Merge global class ACLs [0] with attributes specific ACLs [attribute].
389            */
390           if ($attribute == "" && isset($subacl['acl'][$object])){
391             foreach($subacl['acl'][$object] as $attr => $dummy){
392               $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl'][$object][$attr]);
393             }
394             continue;
395           }
397           /* Per attribute ACL? */
398           if (isset($subacl['acl'][$object][$attribute])){
399             $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl'][$object][$attribute]);
400             continue;
401           }
403           /* Per object ACL? */
404           if (isset($subacl['acl'][$object][0])){
405             $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl'][$object][0]);
406             continue;
407           }
409           /* Global ACL? */
410           if (isset($subacl['acl']['all'][0])){
411             $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl']['all'][0]);
412             continue;
413           }
414         }
415       }
416     }
418     /* If the requested ACL is for a container object, then alter 
419         ACLs by applying cleanACL a last time.
420      */
421     if(isset($this->config->idepartments[$dn])){
422       $acl = $this->cleanACL($acl);
423     }
425     /* Assemble string */
426     $ret= "";
427     foreach ($acl as $key => $value){
428       if ($value !== ""){
429         $ret.= $key;
430       }
431     }
433     $ACL_CACHE["$dn+$object+$attribute"]= $ret;
435     /* Remove write if needed */
436     if ($skip_write){
437       $ret= preg_replace('/w/', '', $ret);
438     }
439     return ($ret);
440   }
443   /* Extract all departments that are accessible (direct or 'on the way' to an
444      accessible department) */
445   function get_module_departments($module, $skip_self_acls = FALSE )
446   {
447     /* If we are forced to skip ACLs checks for the current user 
448         then return all departments as valid.
449      */
450     if($this->ignore_acl_for_current_user()){
451       return(array_keys($this->config->idepartments));
452     }
454     /* Use cached results if possilbe */
455     $ACL_CACHE = &session::get('ACL_CACHE');
457     if(!is_array($module)){
458       $module = array($module);
459     }
461     global $plist;
462     $res = array();
463     foreach($module as $mod){
464       if(isset($ACL_CACHE['MODULE_DEPARTMENTS'][$mod])){
465         $res = array_merge($res,$ACL_CACHE['MODULE_DEPARTMENTS'][$mod]);
466         continue;
467       }
469       $deps = array();
471       /* Search for per object ACLs */
472       foreach($this->ACL as $dn => $infos){
473         foreach($infos as $info){
474           $found = FALSE;
475           foreach($info['acl'] as $cat => $data){
477             /* Skip self acls? */
478             if($skip_self_acls && isset($data['0']) && strpos($data['0'], "s")) continue;
479             if(preg_match("/^".normalizePreg($mod)."/",$cat)){
480               $found =TRUE;
481               break;
482             }
483           } 
485           if($found && !isset($this->config->idepartments[$dn])){
486             while(!isset($this->config->idepartments[$dn]) && strpos($dn, ",")){
487               $dn = preg_replace("/^[^,]+,/","",$dn);
488             }
489             if(isset($this->config->idepartments[$dn])){
490               $deps[] = $dn;
491             }
492           }
493         }
494       }
496       /* For all gosaDepartments */
497       foreach ($this->config->departments as $dn){
498         if(in_array($dn,$deps)) continue;
499         $acl = "";
500         if(strpos($mod, '/')){
501           $acl.=  $this->get_permissions($dn,$mod);
502         }else{
503           $acl.=  $this->get_category_permissions($dn,$mod,TRUE);
504         }
505         if(!empty($acl)) {
506           $deps[] = $dn;
507         }
508       }
510       $ACL_CACHE['MODULE_DEPARTMENTS'][$mod] = $deps;
511       $res = array_merge($res,$deps);
512     } 
513     return ($res);
514   }
517   function mergeACL($acl, $type, $newACL)
518   {
519                 $at= array("psub" => "p", "sub" => "s", "one" => "1");
521     if (strpos($newACL, 'w') !== FALSE && strpos($newACL, 'r') === FALSE){
522       $newACL .= "r";
523     }
525                 /* Ignore invalid characters */
526                 $newACL= preg_replace('/[^rwcdm]/', '', $newACL);
528     foreach(str_split($newACL) as $char){
530       /* Skip permanent and subtree entries */
531       if (preg_match('/[sp]/', $acl[$char])){
532         continue;
533       }
535                         if ($type == "base" && $acl[$char] != 1) {
536                                 $acl[$char]= 0;
537                         } else {
538         $acl[$char]= $at[$type];
539                         }
540     }
542     return ($acl);
543   }
546   function cleanACL($acl, $reset= FALSE)
547   {
548     foreach ($acl as $key => $value){
550       /* Continue, if value is empty or permanent */
551       if ($value == "" || $value == "p") {
552             continue;
553       }
555       /* Reset removes everything but 'p' */
556       if ($reset && $value != 'p'){
557         $acl[$key]= "";
558         continue;
559       }
561       /* Decrease tree level */
562       if (is_int($value)){
563         if ($value){
564           $acl[$key]--;
565         } else {
566           $acl[$key]= "";
567         }
568       }
569     }
571     return ($acl);
572   }
575   /* #FIXME This could be logical wrong or could be optimized in the future
576      Return combined acls for a given category. 
577      All acls will be combined like boolean AND 
578       As example ('rwcdm' + 'rcd' + 'wrm'= 'r') 
579     
580      Results will be cached in $this->result_cache.
581       $this->result_cache will be resetted if load_acls is called.
582   */
583   function has_complete_category_acls($dn,$category)
584   {
585     $acl    = "rwcdm";
586     $types  = "rwcdm";
588     if(!is_string($category)){
589       trigger_error("category must be string");   
590       $acl = "";
591     }else{
592       if(!isset($this->result_cache['has_complete_category_acls'][$dn][$category]))   {
593         if (isset($this->ocMapping[$category])){
594           foreach($this->ocMapping[$category] as $oc){
596             /* Skip objectClass '0' (e.g. users/0) get_permissions will ever return '' ??  */
597             if($oc == "0") continue;
598             $tmp =  $this->get_permissions($dn, $category."/".$oc);
599             for($i = 0, $l= strlen($types); $i < $l; $i++) {
600               if(!preg_match("/".$types[$i]."/",$tmp)){ 
601                 $acl = preg_replace("/".$types[$i]."/","",$acl);
602               }
603             }
604           }
605         }else{
606           trigger_error("Invalid type of category ".$category);
607           $acl = "";
608         }
609         $this->result_cache['has_complete_category_acls'][$dn][$category] = $acl;
610       }else{
611         $acl = $this->result_cache['has_complete_category_acls'][$dn][$category];
612       }
613     }
614     return($acl);
615   }
617  
618   /*! \brief  Returns TRUE if the current user is configured in IGNORE_ACL=".." in your gosa.conf 
619       @param  Return Boolean TRUE if we have to skip ACL checks else FALSE.
620    */ 
621   function ignore_acl_for_current_user()
622   {
623     return($this->ignoreACL);
624   }
628 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
629 ?>