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.
184 */
185 $tmp = array();
186 foreach($this->ACL as $dn => $acl){
187 $sdn =$dn;
188 while(strpos($dn,",") !== FALSE){
189 if(isset($this->ACL[$dn])){
190 $tmp[$sdn][$dn] = $this->ACL[$dn];
191 foreach($this->ACL[$dn] as $aclset){
192 if(isset($aclset['filter']{1})){
193 $this->ACLperPath_usesFilter[$sdn] = TRUE;
194 }
195 }
196 }
197 $dn = preg_replace("/^[^,]*+,/","",$dn);
198 }
199 }
200 $this->ACLperPath =$tmp;
201 }
204 function get_category_permissions($dn, $category, $any_acl = FALSE)
205 {
206 return(@$this->get_permissions($dn,$category.'/0',""));
207 }
210 /*! \brief Check if the given object (dn) is copyable
211 @param String The object dn
212 @param String The acl category (e.g. users)
213 @param String The acl class (e.g. user)
214 @return Boolean TRUE if the given object is copyable else FALSE
215 */
216 function is_copyable($dn, $object, $class)
217 {
218 return(preg_match("/r/",$this->has_complete_category_acls($dn, $object)));
219 }
222 /*! \brief Check if the given object (dn) is cutable
223 @param String The object dn
224 @param String The acl category (e.g. users)
225 @param String The acl class (e.g. user)
226 @return Boolean TRUE if the given object is cutable else FALSE
227 */
228 function is_cutable($dn, $object, $class)
229 {
230 $remove = preg_match("/d/",$this->get_permissions($dn,$object."/".$class));
231 $read = preg_match("/r/",$this->has_complete_category_acls($dn, $object));
232 return($remove && $read);
233 }
236 /*! \brief Checks if we are allowed to paste an object to the given destination ($dn)
237 @param String The destination dn
238 @param String The acl category (e.g. users)
239 @param String The acl class (e.g. user)
240 @return Boolean TRUE if we are allowed to paste an object.
241 */
242 function is_pasteable($dn, $object)
243 {
244 return(preg_match("/w/",$this->has_complete_category_acls($dn, $object)));
245 }
248 /*! \brief Checks if we are allowed to restore a snapshot for the given dn.
249 @param String The destination dn
250 @param String The acl category (e.g. users)
251 @return Boolean TRUE if we are allowed to restore a snapshot.
252 */
253 function allow_snapshot_restore($dn, $object)
254 {
255 if(!is_array($object)){
256 $object = array($object);
257 }
258 $r = $w = TRUE;
259 foreach($object as $category){
260 $w &= preg_match("/w/",$this->has_complete_category_acls($dn, $category));
261 $r &= preg_match("/r/",$this->has_complete_category_acls($dn, $category));
262 }
263 return($r && $w );
264 }
267 /*! \brief Checks if we are allowed to create a snapshot of the given dn.
268 @param String The source dn
269 @param String The acl category (e.g. users)
270 @return Boolean TRUE if we are allowed to restore a snapshot.
271 */
272 function allow_snapshot_create($dn, $object)
273 {
274 if(!is_array($object)){
275 $object = array($object);
276 }
277 $r = TRUE;
278 foreach($object as $category){
279 $r &= preg_match("/r/",$this->has_complete_category_acls($dn, $category));
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"])){
300 $ret = $ACL_CACHE["$dn+$object+$attribute"];
301 if($skip_write){
302 $ret = str_replace('w', '',$ret);
303 }
304 return($ret);
305 }
307 /* Detect the set of ACLs we have to check for this object
308 */
309 $adn = $dn;
310 while(!isset($this->ACLperPath[$adn]) && strpos($adn,",") !== FALSE){
311 $adn = preg_replace("/^[^,]*+,/","",$adn);
312 }
313 if(isset($this->ACLperPath[$adn])){
314 $ACL = $this->ACLperPath[$adn];
315 }else{
316 $ACL_CACHE["$dn+$object+$attribute"] = "";
317 return("");
318 }
320 /* If we do not need to respect any user-filter settings
321 we can skip the per object ACL checks.
322 */
323 $orig_dn= $dn;
324 if(!isset($this->ACLperPath_usesFilter[$adn])){
325 $dn = $adn;
326 if (isset($ACL_CACHE["$dn+$object+$attribute"])){
327 $ret = $ACL_CACHE["$dn+$object+$attribute"];
328 if(!isset($ACL_CACHE["$orig_dn+$object+$attribute"])){
329 $ACL_CACHE["$orig_dn+$object+$attribute"] = $ACL_CACHE["$dn+$object+$attribute"];
330 }
331 if($skip_write){
332 $ret = str_replace('w','',$ret);
333 }
334 return($ret);
335 }
336 }
338 /* Get ldap object, for later filter checks
339 */
340 $ldap = $this->config->get_ldap_link();
342 $acl= array("r" => "", "w" => "", "c" => "", "d" => "", "m" => "", "a" => "");
344 /* Build dn array */
345 $path= split(',', $dn);
346 $path= array_reverse($path);
348 /* Walk along the path to evaluate the acl */
349 $cpath= "";
350 foreach ($path as $element){
352 /* Clean potential ACLs for each level */
353 if(isset($this->config->idepartments[$cpath])){
354 $acl= $this->cleanACL($acl);
355 }
357 if ($cpath == ""){
358 $cpath= $element;
359 } else {
360 $cpath= $element.','.$cpath;
361 }
363 if (isset($ACL[$cpath])){
365 /* Inspect this ACL, place the result into ACL */
366 foreach ($ACL[$cpath] as $subacl){
368 /* Reset? Just clean the ACL and turn over to the next one... */
369 if ($subacl['type'] == 'reset'){
370 $acl= $this->cleanACL($acl, TRUE);
371 continue;
372 }
374 if($subacl['type'] == "role") {
375 echo "role skipped";
376 continue;
377 }
379 /* With user filter */
380 if (isset($subacl['filter']) && !empty($subacl['filter'])){
381 $id = $dn."-".$subacl['filter'];
382 if(!isset($ACL_CACHE['FILTER'][$id])){
383 $ACL_CACHE['FILTER'][$id] = $ldap->object_match_filter($dn,$subacl['filter']);
384 }
385 if(!$ACL_CACHE['FILTER'][$id]){
386 continue;
387 }
388 }
390 /* Self ACLs?
391 */
392 if($dn != $this->dn && isset($subacl['acl'][$object][0]) && strpos($subacl['acl'][$object][0],"s")){
393 continue;
394 }
396 /* If attribute is "", we want to know, if we've *any* permissions here...
397 Merge global class ACLs [0] with attributes specific ACLs [attribute].
398 */
399 if ($attribute == "" && isset($subacl['acl'][$object])){
400 foreach($subacl['acl'][$object] as $attr => $dummy){
401 $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl'][$object][$attr]);
402 }
403 continue;
404 }
406 /* Per attribute ACL? */
407 if (isset($subacl['acl'][$object][$attribute])){
408 $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl'][$object][$attribute]);
409 continue;
410 }
412 /* Per object ACL? */
413 if (isset($subacl['acl'][$object][0])){
414 $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl'][$object][0]);
415 continue;
416 }
418 /* Global ACL? */
419 if (isset($subacl['acl']['all'][0])){
420 $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl']['all'][0]);
421 continue;
422 }
424 /* Category ACLs (e.g. $object = "user/0")
425 */
426 if(strstr($object,"/0")){
427 $ocs = preg_replace("/\/0$/","",$object);
428 if(isset($this->ocMapping[$ocs])){
430 /* if $attribute is "", then check every single attribute for this object.
431 if it is 0, then just check the object category ACL.
432 */
433 if($attribute == ""){
434 foreach($this->ocMapping[$ocs] as $oc){
435 if (isset($subacl['acl'][$ocs.'/'.$oc])){
436 foreach($subacl['acl'][$ocs.'/'.$oc] as $attr => $dummy){
437 $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl'][$ocs.'/'.$oc][$attr]);
438 }
439 continue;
440 }
441 }
442 }else{
443 if(isset($subacl['acl'][$ocs.'/'.$oc][0])){
444 $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl'][$ocs.'/'.$oc][0]);
445 }
446 }
447 }
448 continue;
449 }
450 }
451 }
452 }
454 /* If the requested ACL is for a container object, then alter
455 ACLs by applying cleanACL a last time.
456 */
457 if(isset($this->config->idepartments[$dn])){
458 $acl = $this->cleanACL($acl);
459 }
461 /* Assemble string */
462 $ret= "";
463 foreach ($acl as $key => $value){
464 if ($value !== ""){
465 $ret.= $key;
466 }
467 }
469 $ACL_CACHE["$dn+$object+$attribute"]= $ret;
470 $ACL_CACHE["$orig_dn+$object+$attribute"]= $ret;
472 /* Remove write if needed */
473 if ($skip_write){
474 $ret= str_replace('w', '', $ret);
475 }
476 return ($ret);
477 }
480 /* Extract all departments that are accessible (direct or 'on the way' to an
481 accessible department) */
482 function get_module_departments($module, $skip_self_acls = FALSE )
483 {
484 /* If we are forced to skip ACLs checks for the current user
485 then return all departments as valid.
486 */
487 if($this->ignore_acl_for_current_user()){
488 return(array_keys($this->config->idepartments));
489 }
491 /* Use cached results if possilbe */
492 $ACL_CACHE = &session::get('ACL_CACHE');
494 if(!is_array($module)){
495 $module = array($module);
496 }
498 global $plist;
499 $res = array();
500 foreach($module as $mod){
501 if(isset($ACL_CACHE['MODULE_DEPARTMENTS'][$mod])){
502 $res = array_merge($res,$ACL_CACHE['MODULE_DEPARTMENTS'][$mod]);
503 continue;
504 }
506 $deps = array();
508 /* Search for per object ACLs */
509 foreach($this->ACL as $dn => $infos){
510 foreach($infos as $info){
511 $found = FALSE;
512 foreach($info['acl'] as $cat => $data){
514 /* Skip self acls? */
515 if($skip_self_acls && isset($data['0']) && strpos($data['0'], "s")) continue;
516 if(preg_match("/^".preg_quote($mod, '/')."/",$cat)){
517 $found =TRUE;
518 break;
519 }
520 }
522 if($found && !isset($this->config->idepartments[$dn])){
523 while(!isset($this->config->idepartments[$dn]) && strpos($dn, ",")){
524 $dn = preg_replace("/^[^,]+,/","",$dn);
525 }
526 if(isset($this->config->idepartments[$dn])){
527 $deps[] = $dn;
528 }
529 }
530 }
531 }
533 /* For all gosaDepartments */
534 foreach ($this->config->departments as $dn){
535 if(in_array($dn,$deps)) continue;
536 $acl = "";
537 if(strpos($mod, '/')){
538 $acl.= $this->get_permissions($dn,$mod);
539 }else{
540 $acl.= $this->get_category_permissions($dn,$mod,TRUE);
541 }
542 if(!empty($acl)) {
543 $deps[] = $dn;
544 }
545 }
547 $ACL_CACHE['MODULE_DEPARTMENTS'][$mod] = $deps;
548 $res = array_merge($res,$deps);
549 }
550 return ($res);
551 }
554 function mergeACL($acl, $type, $newACL)
555 {
556 $at= array("psub" => "p", "sub" => "s", "one" => "1");
558 if (strpos($newACL, 'w') !== FALSE && strpos($newACL, 'r') === FALSE){
559 $newACL .= "r";
560 }
562 /* Ignore invalid characters */
563 $newACL= preg_replace('/[^rwcdm]/', '', $newACL);
565 foreach(str_split($newACL) as $char){
567 /* Skip permanent and subtree entries */
568 if (preg_match('/[sp]/', $acl[$char])){
569 continue;
570 }
572 if ($type == "base" && $acl[$char] != 1) {
573 $acl[$char]= 0;
574 } else {
575 $acl[$char]= $at[$type];
576 }
577 }
579 return ($acl);
580 }
583 function cleanACL($acl, $reset= FALSE)
584 {
585 foreach ($acl as $key => $value){
587 /* Continue, if value is empty or permanent */
588 if ($value == "" || $value == "p") {
589 continue;
590 }
592 /* Reset removes everything but 'p' */
593 if ($reset && $value != 'p'){
594 $acl[$key]= "";
595 continue;
596 }
598 /* Decrease tree level */
599 if (is_int($value)){
600 if ($value){
601 $acl[$key]--;
602 } else {
603 $acl[$key]= "";
604 }
605 }
606 }
608 return ($acl);
609 }
612 /* #FIXME This could be logical wrong or could be optimized in the future
613 Return combined acls for a given category.
614 All acls will be combined like boolean AND
615 As example ('rwcdm' + 'rcd' + 'wrm'= 'r')
617 Results will be cached in $this->result_cache.
618 $this->result_cache will be resetted if load_acls is called.
619 */
620 function has_complete_category_acls($dn,$category)
621 {
622 $acl = "rwcdm";
623 $types = "rwcdm";
625 if(!is_string($category)){
626 trigger_error("category must be string");
627 $acl = "";
628 }else{
629 if(!isset($this->result_cache['has_complete_category_acls'][$dn][$category])) {
630 if (isset($this->ocMapping[$category])){
631 foreach($this->ocMapping[$category] as $oc){
633 /* Skip objectClass '0' (e.g. users/0) get_permissions will ever return '' ?? */
634 if($oc == "0") continue;
635 $tmp = $this->get_permissions($dn, $category."/".$oc);
636 for($i = 0, $l= strlen($types); $i < $l; $i++) {
637 if(!preg_match("/".$types[$i]."/",$tmp)){
638 $acl = preg_replace("/".$types[$i]."/","",$acl);
639 }
640 }
641 }
642 }else{
643 trigger_error("Invalid type of category ".$category);
644 $acl = "";
645 }
646 $this->result_cache['has_complete_category_acls'][$dn][$category] = $acl;
647 }else{
648 $acl = $this->result_cache['has_complete_category_acls'][$dn][$category];
649 }
650 }
651 return($acl);
652 }
655 /*! \brief Returns TRUE if the current user is configured in IGNORE_ACL=".." in your gosa.conf
656 @param Return Boolean TRUE if we have to skip ACL checks else FALSE.
657 */
658 function ignore_acl_for_current_user()
659 {
660 return($this->ignoreACL);
661 }
663 }
665 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
666 ?>