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::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;
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:
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 while(strpos($dn,",") !== FALSE){
201 if(isset($this->ACL[$dn])){
202 $all_acl[$sdn][$dn] = $this->ACL[$dn];
203 $without_self_acl[$sdn][$dn] = $this->ACL[$dn];
204 foreach($without_self_acl[$sdn][$dn] as $acl_id => $acl_set){
206 /* Remember which ACL set has speicial user filter
207 */
208 if(isset($acl_set['filter']{1})){
209 $this->ACLperPath_usesFilter[$sdn] = TRUE;
210 }
212 /* Remove all acl entries which are especially for the current user (self acl)
213 */
214 foreach($acl_set['acl'] as $object => $object_acls){
215 if(strpos($object_acls[0],"s")){
216 unset($without_self_acl[$sdn][$dn][$acl_id]['acl'][$object]);
217 }
218 }
219 }
220 }
221 $dn = preg_replace("/^[^,]*+,/","",$dn);
222 }
223 }
224 $this->ACLperPath =$without_self_acl;
226 /* Append Self entry */
227 $dn = $this->dn;
228 while(strpos($dn,",") && !isset($all_acl[$dn])){
229 $dn = preg_replace("/^[^,]*+,/","",$dn);
230 }
231 if(isset($all_acl[$dn])){
232 $this->ACLperPath[$this->dn] = $all_acl[$dn];
233 }
234 }
237 function get_category_permissions($dn, $category, $any_acl = FALSE)
238 {
239 return(@$this->get_permissions($dn,$category.'/0',""));
240 }
243 /*! \brief Check if the given object (dn) is copyable
244 @param String The object dn
245 @param String The acl category (e.g. users)
246 @param String The acl class (e.g. user)
247 @return Boolean TRUE if the given object is copyable else FALSE
248 */
249 function is_copyable($dn, $object, $class)
250 {
251 return(preg_match("/r/",$this->has_complete_category_acls($dn, $object)));
252 }
255 /*! \brief Check if the given object (dn) is cutable
256 @param String The object dn
257 @param String The acl category (e.g. users)
258 @param String The acl class (e.g. user)
259 @return Boolean TRUE if the given object is cutable else FALSE
260 */
261 function is_cutable($dn, $object, $class)
262 {
263 $remove = preg_match("/d/",$this->get_permissions($dn,$object."/".$class));
264 $read = preg_match("/r/",$this->has_complete_category_acls($dn, $object));
265 return($remove && $read);
266 }
269 /*! \brief Checks if we are allowed to paste an object to the given destination ($dn)
270 @param String The destination dn
271 @param String The acl category (e.g. users)
272 @param String The acl class (e.g. user)
273 @return Boolean TRUE if we are allowed to paste an object.
274 */
275 function is_pasteable($dn, $object)
276 {
277 return(preg_match("/w/",$this->has_complete_category_acls($dn, $object)));
278 }
281 /*! \brief Checks if we are allowed to restore a snapshot for the given dn.
282 @param String The destination dn
283 @param String The acl category (e.g. users)
284 @return Boolean TRUE if we are allowed to restore a snapshot.
285 */
286 function allow_snapshot_restore($dn, $object)
287 {
288 if(!is_array($object)){
289 $object = array($object);
290 }
291 $r = $w = TRUE;
292 foreach($object as $category){
293 $w &= preg_match("/w/",$this->has_complete_category_acls($dn, $category));
294 $r &= preg_match("/r/",$this->has_complete_category_acls($dn, $category));
295 }
296 return($r && $w );
297 }
300 /*! \brief Checks if we are allowed to create a snapshot of the given dn.
301 @param String The source dn
302 @param String The acl category (e.g. users)
303 @return Boolean TRUE if we are allowed to restore a snapshot.
304 */
305 function allow_snapshot_create($dn, $object)
306 {
307 if(!is_array($object)){
308 $object = array($object);
309 }
310 $r = TRUE;
311 foreach($object as $category){
312 $r &= preg_match("/r/",$this->has_complete_category_acls($dn, $category));
313 }
314 return($r) ;
315 }
318 function get_permissions($dn, $object, $attribute= "", $skip_write= FALSE)
319 {
320 /* If we are forced to skip ACLs checks for the current user
321 then return all permissions.
322 */
323 if($this->ignore_acl_for_current_user()){
324 if($skip_write){
325 return("rcdm");
326 }
327 return("rwcdm");
328 }
330 /* Push cache answer? */
331 $ACL_CACHE = &session::get('ACL_CACHE');
332 if (isset($ACL_CACHE["$dn+$object+$attribute"])){
333 $ret = $ACL_CACHE["$dn+$object+$attribute"];
334 if($skip_write){
335 $ret = str_replace(array('w','c','d','m'), '',$ret);
336 }
337 return($ret);
338 }
340 /* Detect the set of ACLs we have to check for this object
341 */
342 $adn = $dn;
343 while(!isset($this->ACLperPath[$adn]) && strpos($adn,",") !== FALSE){
344 $adn = preg_replace("/^[^,]*+,/","",$adn);
345 }
346 if(isset($this->ACLperPath[$adn])){
347 $ACL = $this->ACLperPath[$adn];
348 }else{
349 $ACL_CACHE["$dn+$object+$attribute"] = "";
350 return("");
351 }
353 /* If we do not need to respect any user-filter settings
354 we can skip the per object ACL checks.
355 */
356 $orig_dn= $dn;
357 if(!isset($this->ACLperPath_usesFilter[$adn])){
358 $dn = $adn;
359 if (isset($ACL_CACHE["$dn+$object+$attribute"])){
360 $ret = $ACL_CACHE["$dn+$object+$attribute"];
361 if(!isset($ACL_CACHE["$orig_dn+$object+$attribute"])){
362 $ACL_CACHE["$orig_dn+$object+$attribute"] = $ACL_CACHE["$dn+$object+$attribute"];
363 }
364 if($skip_write){
365 $ret = str_replace('w','',$ret);
366 }
367 return($ret);
368 }
369 }
371 /* Get ldap object, for later filter checks
372 */
373 $ldap = $this->config->get_ldap_link();
375 $acl= array("r" => "", "w" => "", "c" => "", "d" => "", "m" => "", "a" => "");
377 /* Build dn array */
378 $path= split(',', $dn);
379 $path= array_reverse($path);
381 /* Walk along the path to evaluate the acl */
382 $cpath= "";
383 foreach ($path as $element){
385 /* Clean potential ACLs for each level */
386 if(isset($this->config->idepartments[$cpath])){
387 $acl= $this->cleanACL($acl);
388 }
390 if ($cpath == ""){
391 $cpath= $element;
392 } else {
393 $cpath= $element.','.$cpath;
394 }
396 if (isset($ACL[$cpath])){
398 /* Inspect this ACL, place the result into ACL */
399 foreach ($ACL[$cpath] as $subacl){
401 /* Reset? Just clean the ACL and turn over to the next one... */
402 if ($subacl['type'] == 'reset'){
403 $acl= $this->cleanACL($acl, TRUE);
404 continue;
405 }
407 if($subacl['type'] == "role") {
408 echo "role skipped";
409 continue;
410 }
412 /* With user filter */
413 if (isset($subacl['filter']) && !empty($subacl['filter'])){
414 $id = $dn."-".$subacl['filter'];
415 if(!isset($ACL_CACHE['FILTER'][$id])){
416 $ACL_CACHE['FILTER'][$id] = $ldap->object_match_filter($dn,$subacl['filter']);
417 }
418 if(!$ACL_CACHE['FILTER'][$id]){
419 continue;
420 }
421 }
423 /* Self ACLs?
424 */
425 if($dn != $this->dn && isset($subacl['acl'][$object][0]) && (strpos($subacl['acl'][$object][0],"s") !== FALSE)){
426 continue;
427 }
429 /* If attribute is "", we want to know, if we've *any* permissions here...
430 Merge global class ACLs [0] with attributes specific ACLs [attribute].
431 */
432 if ($attribute == "" && isset($subacl['acl'][$object])){
433 foreach($subacl['acl'][$object] as $attr => $dummy){
434 $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl'][$object][$attr]);
435 }
436 continue;
437 }
439 /* Per attribute ACL? */
440 if (isset($subacl['acl'][$object][$attribute])){
441 $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl'][$object][$attribute]);
442 continue;
443 }
445 /* Per object ACL? */
446 if (isset($subacl['acl'][$object][0])){
447 $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl'][$object][0]);
448 continue;
449 }
451 /* Global ACL? */
452 if (isset($subacl['acl']['all'][0])){
453 $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl']['all'][0]);
454 continue;
455 }
457 /* Category ACLs (e.g. $object = "user/0")
458 */
459 if(strstr($object,"/0")){
460 $ocs = preg_replace("/\/0$/","",$object);
461 if(isset($this->ocMapping[$ocs])){
463 /* if $attribute is "", then check every single attribute for this object.
464 if it is 0, then just check the object category ACL.
465 */
466 if($attribute == ""){
467 foreach($this->ocMapping[$ocs] as $oc){
468 if (isset($subacl['acl'][$ocs.'/'.$oc])){
470 if($dn != $this->dn && strpos($subacl['acl'][$ocs.'/'.$oc][0],"s") !== FALSE) continue;
472 foreach($subacl['acl'][$ocs.'/'.$oc] as $attr => $dummy){
473 $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl'][$ocs.'/'.$oc][$attr]);
474 }
475 continue;
476 }
477 }
478 }else{
479 if(isset($subacl['acl'][$ocs.'/'.$oc][0])){
480 if($dn != $this->dn && strpos($subacl['acl'][$ocs.'/'.$oc][0],"s") !== FALSE) continue;
481 $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl'][$ocs.'/'.$oc][0]);
482 }
483 }
484 }
485 continue;
486 }
487 }
488 }
489 }
491 /* If the requested ACL is for a container object, then alter
492 ACLs by applying cleanACL a last time.
493 */
494 if(isset($this->config->idepartments[$dn])){
495 $acl = $this->cleanACL($acl);
496 }
498 /* Assemble string */
499 $ret= "";
500 foreach ($acl as $key => $value){
501 if ($value !== ""){
502 $ret.= $key;
503 }
504 }
506 $ACL_CACHE["$dn+$object+$attribute"]= $ret;
507 $ACL_CACHE["$orig_dn+$object+$attribute"]= $ret;
509 /* Remove write if needed */
510 if ($skip_write){
511 $ret = str_replace(array('w','c','d','m'), '',$ret);
512 }
513 return ($ret);
514 }
517 /* Extract all departments that are accessible (direct or 'on the way' to an
518 accessible department) */
519 function get_module_departments($module, $skip_self_acls = FALSE )
520 {
521 /* If we are forced to skip ACLs checks for the current user
522 then return all departments as valid.
523 */
524 if($this->ignore_acl_for_current_user()){
525 return(array_keys($this->config->idepartments));
526 }
528 /* Use cached results if possilbe */
529 $ACL_CACHE = &session::get('ACL_CACHE');
531 if(!is_array($module)){
532 $module = array($module);
533 }
535 global $plist;
536 $res = array();
537 foreach($module as $mod){
538 if(isset($ACL_CACHE['MODULE_DEPARTMENTS'][$mod])){
539 $res = array_merge($res,$ACL_CACHE['MODULE_DEPARTMENTS'][$mod]);
540 continue;
541 }
543 $deps = array();
545 /* Search for per object ACLs */
546 foreach($this->ACL as $dn => $infos){
547 foreach($infos as $info){
548 $found = FALSE;
549 foreach($info['acl'] as $cat => $data){
551 /* Skip self acls? */
552 if($skip_self_acls && isset($data['0']) && (strpos($data['0'], "s") !== FALSE)) continue;
553 if(preg_match("/^".preg_quote($mod, '/')."/",$cat)){
554 $found =TRUE;
555 break;
556 }
557 }
559 if($found && !isset($this->config->idepartments[$dn])){
560 while(!isset($this->config->idepartments[$dn]) && strpos($dn, ",")){
561 $dn = preg_replace("/^[^,]+,/","",$dn);
562 }
563 if(isset($this->config->idepartments[$dn])){
564 $deps[$dn] = $dn;
565 }
566 }
567 }
568 }
570 /* For all gosaDepartments */
571 foreach ($this->config->departments as $dn){
572 if(isset($deps[$dn])) continue;
573 $acl = "";
574 if(strpos($mod, '/')){
575 $acl.= $this->get_permissions($dn,$mod);
576 }else{
577 $acl.= $this->get_category_permissions($dn,$mod,TRUE);
578 }
579 if(!empty($acl)) {
580 $deps[$dn] = $dn;
581 }
582 }
584 $ACL_CACHE['MODULE_DEPARTMENTS'][$mod] = $deps;
585 $res = array_merge($res,$deps);
586 }
588 return (array_values($res));
589 }
592 function mergeACL($acl, $type, $newACL)
593 {
594 $at= array("psub" => "p", "sub" => "s", "one" => "1");
596 if (strpos($newACL, 'w') !== FALSE && strpos($newACL, 'r') === FALSE){
597 $newACL .= "r";
598 }
600 /* Ignore invalid characters */
601 $newACL= preg_replace('/[^rwcdm]/', '', $newACL);
603 foreach(str_split($newACL) as $char){
605 /* Skip permanent and subtree entries */
606 if (preg_match('/[sp]/', $acl[$char])){
607 continue;
608 }
610 if ($type == "base" && $acl[$char] != 1) {
611 $acl[$char]= 0;
612 } else {
613 $acl[$char]= $at[$type];
614 }
615 }
617 return ($acl);
618 }
621 function cleanACL($acl, $reset= FALSE)
622 {
623 foreach ($acl as $key => $value){
625 /* Continue, if value is empty or permanent */
626 if ($value == "" || $value == "p") {
627 continue;
628 }
630 /* Reset removes everything but 'p' */
631 if ($reset && $value != 'p'){
632 $acl[$key]= "";
633 continue;
634 }
636 /* Decrease tree level */
637 if (is_int($value)){
638 if ($value){
639 $acl[$key]--;
640 } else {
641 $acl[$key]= "";
642 }
643 }
644 }
646 return ($acl);
647 }
650 /* #FIXME This could be logical wrong or could be optimized in the future
651 Return combined acls for a given category.
652 All acls will be combined like boolean AND
653 As example ('rwcdm' + 'rcd' + 'wrm'= 'r')
655 Results will be cached in $this->result_cache.
656 $this->result_cache will be resetted if load_acls is called.
657 */
658 function has_complete_category_acls($dn,$category)
659 {
660 $acl = "rwcdm";
661 $types = "rwcdm";
663 if(!is_string($category)){
664 trigger_error("category must be string");
665 $acl = "";
666 }else{
667 if(!isset($this->result_cache['has_complete_category_acls'][$dn][$category])) {
668 if (isset($this->ocMapping[$category])){
669 foreach($this->ocMapping[$category] as $oc){
671 /* Skip objectClass '0' (e.g. users/0) get_permissions will ever return '' ?? */
672 if($oc == "0") continue;
673 $tmp = $this->get_permissions($dn, $category."/".$oc);
674 for($i = 0, $l= strlen($types); $i < $l; $i++) {
675 if(!preg_match("/".$types[$i]."/",$tmp)){
676 $acl = preg_replace("/".$types[$i]."/","",$acl);
677 }
678 }
679 }
680 }else{
681 trigger_error("Invalid type of category ".$category);
682 $acl = "";
683 }
684 $this->result_cache['has_complete_category_acls'][$dn][$category] = $acl;
685 }else{
686 $acl = $this->result_cache['has_complete_category_acls'][$dn][$category];
687 }
688 }
689 return($acl);
690 }
693 /*! \brief Returns TRUE if the current user is configured in IGNORE_ACL=".." in your gosa.conf
694 @param Return Boolean TRUE if we have to skip ACL checks else FALSE.
695 */
696 function ignore_acl_for_current_user()
697 {
698 return($this->ignoreACL);
699 }
701 }
703 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
704 ?>