Code

cb1d281221ab653cd040a701a0adddf3c6e05a22
[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){
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;
149         
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, $any_acl = FALSE)
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         if($any_acl && !empty($acl)) return($acl);
196       }
197     }else{
198       trigger_error("ACL request for an invalid category (".$category.").");
199     }
201     return ($acl);
202   }
204   
205   /*! \brief Check if the given object (dn) is copyable
206       @param  String The object dn 
207       @param  String The acl  category (e.g. users) 
208       @param  String The acl  class (e.g. user) 
209       @return Boolean   TRUE if the given object is copyable else FALSE 
210   */
211   function is_copyable($dn, $object, $class)
212   {
213     return(preg_match("/r/",$this->has_complete_category_acls($dn, $object)));
214   }
217   /*! \brief Check if the given object (dn) is cutable
218       @param  String The object dn 
219       @param  String The acl  category (e.g. users) 
220       @param  String The acl  class (e.g. user) 
221       @return Boolean   TRUE if the given object is cutable else FALSE 
222   */
223   function is_cutable($dn, $object, $class)
224   {
225     $remove = preg_match("/d/",$this->get_permissions($dn,$object."/".$class));
226     $read   = preg_match("/r/",$this->has_complete_category_acls($dn, $object));
227     return($remove && $read);
228   }
231   /*! \brief  Checks if we are allowed to paste an object to the given destination ($dn)
232       @param  String The destination dn 
233       @param  String The acl  category (e.g. users) 
234       @param  String The acl  class (e.g. user) 
235       @return Boolean   TRUE if we are allowed to paste an object.
236   */
237   function is_pasteable($dn, $object)
238   {
239     return(preg_match("/w/",$this->has_complete_category_acls($dn, $object)));
240   }
243   /*! \brief  Checks if we are allowed to restore a snapshot for the given dn.
244       @param  String The destination dn 
245       @param  String The acl  category (e.g. users) 
246       @return Boolean   TRUE if we are allowed to restore a snapshot.
247   */
248   function allow_snapshot_restore($dn, $object)
249   {
250     if(!is_array($object)){
251       $object = array($object);
252     }
253     $r = $w = $c = TRUE;
254     foreach($object as $category){
255       $w &= preg_match("/w/",$this->has_complete_category_acls($dn, $category));
256       $c &= preg_match("/c/",$this->has_complete_category_acls($dn, $category));
257       $r &= preg_match("/r/",$this->has_complete_category_acls($dn, $category));
258 #     print_a(array($category => array($r.$w.$c)));
259     }
260     return($r && $w ); 
261   }  
264   /*! \brief  Checks if we are allowed to create a snapshot of the given dn.
265       @param  String The source dn 
266       @param  String The acl category (e.g. users) 
267       @return Boolean   TRUE if we are allowed to restore a snapshot.
268   */
269   function allow_snapshot_create($dn, $object)
270   {
271     if(!is_array($object)){
272       $object = array($object);
273     }
274     $r = $w = $c = TRUE;
275     foreach($object as $category){
276       $w &= preg_match("/w/",$this->has_complete_category_acls($dn, $category));
277       $c &= preg_match("/c/",$this->has_complete_category_acls($dn, $category));
278       $r &= preg_match("/r/",$this->has_complete_category_acls($dn, $category));
279 #      print_a(array($category => array($r.$w.$c)));
280     }
281     return($r) ; 
282   }  
285   function get_permissions($dn, $object, $attribute= "", $skip_write= FALSE)
286   {
287     /* If we are forced to skip ACLs checks for the current user 
288         then return all permissions.
289      */
290     if($this->ignore_acl_for_current_user()){
291       if($skip_write){
292         return("rcdm");
293       }
294       return("rwcdm");
295     }
297     /* Push cache answer? */
298     $ACL_CACHE = &session::get('ACL_CACHE');
299     if (isset($ACL_CACHE["$dn+$object+$attribute"])){
301       /* Remove write if needed */
302       if ($skip_write){
303         $ret = preg_replace('/w/', '', $ACL_CACHE["$dn+$object+$attribute"]);
304       }else{
305         $ret = $ACL_CACHE["$dn+$object+$attribute"];
306       } 
307       return($ret);
308     }
310     /* Get ldap object, for later filter checks 
311      */
312     $ldap = $this->config->get_ldap_link();
314     $acl= array("r" => "", "w" => "", "c" => "", "d" => "", "m" => "", "a" => "");
316     /* Build dn array */
317     $path= split(',', $dn);
318     $path= array_reverse($path);
320     /* Walk along the path to evaluate the acl */
321     $cpath= "";
322     foreach ($path as $element){
324       /* Clean potential ACLs for each level */
325       if(in_array($cpath,$this->config->departments)){
326         $acl= $this->cleanACL($acl);
327       }
329       if ($cpath == ""){
330         $cpath= $element;
331       } else {
332         $cpath= $element.','.$cpath;
333       }
335       if (isset($this->ACL[$cpath])){
337         /* Inspect this ACL, place the result into ACL */
338         foreach ($this->ACL[$cpath] as $subacl){
340           /* Reset? Just clean the ACL and turn over to the next one... */
341           if ($subacl['type'] == 'reset'){
342             $acl= $this->cleanACL($acl, TRUE);
343             continue;
344           }
346           if($subacl['type'] == "role") {
347             echo "role skipped";
348             continue;
349           }
351           /* With user filter */
352           if (isset($subacl['filter']) && !empty($subacl['filter'])){
353             if(!$ldap->object_match_filter($dn,$subacl['filter'])){
354               continue;
355             }
356           }
358           /* Self ACLs? 
359            */
360           if(isset($subacl['acl'][$object][0]) && preg_match("/s/",$subacl['acl'][$object][0]) && $dn != $this->dn){
361             continue;
362           }
364           /* If attribute is "", we want to know, if we've *any* permissions here... 
365               Merge global class ACLs [0] with attributes specific ACLs [attribute].
366            */
367           if ($attribute == "" && isset($subacl['acl'][$object])){
368             foreach($subacl['acl'][$object] as $attr => $dummy){
369               $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl'][$object][$attr]);
370             }
371             continue;
372           }
374           /* Per attribute ACL? */
375           if (isset($subacl['acl'][$object][$attribute])){
376             $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl'][$object][$attribute]);
377             continue;
378           }
380           /* Per object ACL? */
381           if (isset($subacl['acl'][$object][0])){
382             $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl'][$object][0]);
383             continue;
384           }
386           /* Global ACL? */
387           if (isset($subacl['acl']['all'][0])){
388             $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl']['all'][0]);
389             continue;
390           }
391         }
392       }
393     }
395     /* If the requested ACL is for a container object, then alter 
396         ACLs by applying cleanACL a last time.
397      */
398     if(in_array($dn,$this->config->departments)){
399       $acl = $this->cleanACL($acl);
400     }
402     /* Assemble string */
403     $ret= "";
404     foreach ($acl as $key => $value){
405       if ($value !== ""){
406         $ret.= $key;
407       }
408     }
410     $ACL_CACHE["$dn+$object+$attribute"]= $ret;
412     /* Remove write if needed */
413     if ($skip_write){
414       $ret= preg_replace('/w/', '', $ret);
415     }
416     return ($ret);
417   }
420   /* Extract all departments that are accessible (direct or 'on the way' to an
421      accessible department) */
422   function get_module_departments($module, $skip_self_acls = FALSE )
423   {
425     /* If we are forced to skip ACLs checks for the current user 
426         then return all departments as valid.
427      */
428     if($this->ignore_acl_for_current_user()){
429       return(array_keys($this->config->idepartments));
430     }
432     /* Use cached results if possilbe */
433     $ACL_CACHE = session::get('ACL_CACHE');
436     if(!is_array($module)){
437       $module = array($module);
438     }
440     global $plist;
441     $res = array();
442     foreach($module as $mod){
443       if(isset($ACL_CACHE['MODULE_DEPARTMENTS'][$mod])){
444         $res = array_merge($res,$ACL_CACHE['MODULE_DEPARTMENTS'][$mod]);
445         continue;
446       }
448       $deps = array();
450 #      /* Search for per object ACLs. 
451 #       */
452 #      $this->config->get_departments();
453 #      $this->config->make_idepartments();
455       foreach($this->ACL as $dn => $infos){
456         foreach($infos as $info){
457           $found = FALSE;
458           foreach($info['acl'] as $cat => $data){
460             /* Skip self acls? */
461             if($skip_self_acls && isset($data['0']) && strpos($data['0'], "s")) continue;
462             if(preg_match("/^".normalizePreg($mod)."/",$cat)){
463               $found =TRUE;
464               break;
465             }
466           } 
468           if($found && !isset($this->config->idepartments[$dn])){
469             while(!isset($this->config->idepartments[$dn]) && strpos($dn, ",")){
470               $dn = preg_replace("/^[^,]+,/","",$dn);
471             }
472             if(isset($this->config->idepartments[$dn])){
473               $deps[] = $dn;
474             }
475           }
476         }
477       }
479       /* For all gosaDepartments */
480       foreach ($this->config->departments as $dn){
481         if(in_array($dn,$deps)) continue;
482         $acl = "";
483         if(strpos($mod, '/')){
484           $acl.=  $this->get_permissions($dn,$mod);
485         }else{
486           $acl.=  $this->get_category_permissions($dn,$mod,TRUE);
487         }
488         if(!empty($acl)) {
489           $deps[] = $dn;
490         }
491       }
493       $ACL_CACHE['MODULE_DEPARTMENTS'][$mod] = $deps;
494       $res = array_merge($res,$deps);
495     } 
496     return ($deps);
497   }
500   function mergeACL($acl, $type, $newACL)
501   {
502                 $at= array("psub" => "p", "sub" => "s", "one" => "1");
504     if (strpos($newACL, 'w') !== FALSE && strpos($newACL, 'r') === FALSE){
505       $newACL .= "r";
506     }
508                 /* Ignore invalid characters */
509                 $newACL= preg_replace('/[^rwcdm]/', '', $newACL);
511     foreach(str_split($newACL) as $char){
513       /* Skip permanent and subtree entries */
514       if (preg_match('/[sp]/', $acl[$char])){
515         continue;
516       }
518                         if ($type == "base" && $acl[$char] != 1) {
519                                 $acl[$char]= 0;
520                         } else {
521         $acl[$char]= $at[$type];
522                         }
523     }
525     return ($acl);
526   }
529   function cleanACL($acl, $reset= FALSE)
530   {
531     foreach ($acl as $key => $value){
533                         /* Continue, if value is empty or permanent */
534                         if ($value == "" || $value == "p") {
535                                 continue;
536                         }
538       /* Reset removes everything but 'p' */
539       if ($reset && $value != 'p'){
540         $acl[$key]= "";
541         continue;
542       }
544       /* Decrease tree level */
545       if (is_int($value)){
546         if ($value){
547           $acl[$key]--;
548         } else {
549           $acl[$key]= "";
550         }
551       }
552     }
554     return ($acl);
555   }
558   /* #FIXME This could be logical wrong or could be optimized in the future
559      Return combined acls for a given category. 
560      All acls will be combined like boolean AND 
561       As example ('rwcdm' + 'rcd' + 'wrm'= 'r') 
562     
563      Results will be cached in $this->result_cache.
564       $this->result_cache will be resetted if load_acls is called.
565   */
566   function has_complete_category_acls($dn,$category)
567   {
568     $acl    = "rwcdm";
569     $types  = "rwcdm";
571     if(!is_string($category)){
572       trigger_error("category must be string");   
573       $acl = "";
574     }else{
575       if(!isset($this->result_cache['has_complete_category_acls'][$dn][$category]))   {
576         if (isset($this->ocMapping[$category])){
577           foreach($this->ocMapping[$category] as $oc){
579             /* Skip objectClass '0' (e.g. users/0) get_permissions will ever return '' ??  */
580             if($oc == "0") continue;
581             $tmp =  $this->get_permissions($dn, $category."/".$oc);
582             for($i = 0 ; $i < strlen($types); $i++) {
583               if(!preg_match("/".$types[$i]."/",$tmp)){ 
584                 $acl = preg_replace("/".$types[$i]."/","",$acl);
585               }
586             }
587           }
588         }else{
589           trigger_error("Invalid type of category ".$category);
590           $acl = "";
591         }
592         $this->result_cache['has_complete_category_acls'][$dn][$category] = $acl;
593       }else{
594         $acl = $this->result_cache['has_complete_category_acls'][$dn][$category];
595       }
596     }
597     return($acl);
598   }
600  
601   /*! \brief  Returns TRUE if the current user is configured in IGNORE_ACL=".." in your gosa.conf 
602       @param  Return Boolean TRUE if we have to skip ACL checks else FALSE.
603    */ 
604   function ignore_acl_for_current_user()
605   {
606     return($this->config->get_cfg_value("ignoreAcl") == $this->dn);
607   }
611 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
612 ?>