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;
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 }
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 => $w.$c.$r));
255 }
256 return($r);
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 $w = preg_match("/w/",$this->has_complete_category_acls($dn, $object));
268 $c = preg_match("/c/",$this->has_complete_category_acls($dn, $object));
269 $r = preg_match("/r/",$this->has_complete_category_acls($dn, $object));
270 # print_a(array($object => $w.$c.$r));
271 return($r && $w && $c) ;
272 }
275 function get_permissions($dn, $object, $attribute= "", $skip_write= FALSE)
276 {
277 /* If we are forced to skip ACLs checks for the current user
278 then return all permissions.
279 */
280 if($this->ignore_acl_for_current_user()){
281 return("rwcdm");
282 }
284 /* Push cache answer? */
285 $ACL_CACHE = &session::get('ACL_CACHE');
286 if (isset($ACL_CACHE["$dn+$object+$attribute"])){
288 /* Remove write if needed */
289 if ($skip_write){
290 $ret = preg_replace('/w/', '', $ACL_CACHE["$dn+$object+$attribute"]);
291 }else{
292 $ret = $ACL_CACHE["$dn+$object+$attribute"];
293 }
294 return($ret);
295 }
297 /* Get ldap object, for later filter checks
298 */
299 $ldap = $this->config->get_ldap_link();
301 $acl= array("r" => "", "w" => "", "c" => "", "d" => "", "m" => "", "a" => "");
303 /* Build dn array */
304 $path= split(',', $dn);
305 $path= array_reverse($path);
307 /* Walk along the path to evaluate the acl */
308 $cpath= "";
309 foreach ($path as $element){
311 /* Clean potential ACLs for each level */
312 $acl= $this->cleanACL($acl);
314 if ($cpath == ""){
315 $cpath= $element;
316 } else {
317 $cpath= $element.','.$cpath;
318 }
319 if (isset($this->ACL[$cpath])){
321 /* Inspect this ACL, place the result into ACL */
322 foreach ($this->ACL[$cpath] as $subacl){
324 /* Reset? Just clean the ACL and turn over to the next one... */
325 if ($subacl['type'] == 'reset'){
326 $acl= $this->cleanACL($acl, TRUE);
327 continue;
328 }
330 if($subacl['type'] == "role") {
331 echo "role skipped";
332 continue;
333 }
335 /* With user filter */
336 if (isset($subacl['filter']) && !empty($subacl['filter'])){
337 $sdn = preg_replace("/^[^,]*+,/","",$dn);
338 $ldap->cd($sdn);
339 $ldap->ls($subacl['filter'],$sdn);
340 if(!$ldap->count()){
341 continue;
342 }else{
343 $found = FALSE;
344 while($attrs = $ldap->fetch()){
345 if($attrs['dn'] == $dn){
346 $found = TRUE;
347 break;
348 }
349 }
350 if(!$found){
351 continue;
352 }
353 }
354 }
356 /* Per attribute ACL? */
357 if (isset($subacl['acl'][$object][$attribute])){
358 $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl'][$object][$attribute]);
359 continue;
360 }
362 /* Per object ACL? */
363 if (isset($subacl['acl'][$object][0])){
364 $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl'][$object][0]);
365 continue;
366 }
368 /* Global ACL? */
369 if (isset($subacl['acl']['all'][0])){
370 $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl']['all'][0]);
371 continue;
372 }
374 /* If attribute is "", we want to know, if we've *any* permissions here... */
375 if ($attribute == "" && isset($subacl['acl'][$object])){
376 foreach($subacl['acl'][$object] as $attr => $dummy){
377 $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl'][$object][$attr]);
378 }
379 continue;
380 }
382 }
383 }
384 }
386 /* Assemble string */
387 $ret= "";
388 foreach ($acl as $key => $value){
389 if ($value !== ""){
390 $ret.= $key;
391 }
392 }
394 $ACL_CACHE["$dn+$object+$attribute"]= $ret;
396 /* Remove write if needed */
397 if ($skip_write){
398 $ret= preg_replace('/w/', '', $ret);
399 }
400 return ($ret);
401 }
404 /* Extract all departments that are accessible (direct or 'on the way' to an
405 accessible department) */
406 function get_module_departments($module)
407 {
409 /* If we are forced to skip ACLs checks for the current user
410 then return all departments as valid.
411 */
412 if($this->ignore_acl_for_current_user()){
413 return(array_keys($this->config->idepartments));
414 }
416 /* Use cached results if possilbe */
417 $ACL_CACHE = session::get('ACL_CACHE');
418 if(isset($ACL_CACHE['MODULE_DEPARTMENTS'][serialize($module)])){
419 return($ACL_CACHE['MODULE_DEPARTMENTS'][serialize($module)]);
420 }
422 global $plist;
424 $objects= array();
425 $deps= array();
427 /* Extract all relevant objects for this module from plist */
428 foreach ($plist->info as $object => $info){
429 if (!isset($info['plCategory'])){
430 continue;
431 }
432 foreach ($info['plCategory'] as $idx => $data){
433 if (preg_match('/^[0-9]+$/', $idx)){
434 if ($data == $module){
435 $objects[$object]= $object;
436 }
437 } else {
438 if ($idx == $module){
439 $objects[$object]= $object;
440 }
441 }
442 }
443 }
445 /* For all gosaDepartments */
446 foreach ($this->config->departments as $dn){
447 $acl= array("r" => "", "w" => "", "c" => "", "d" => "", "m" => "", "a" => "");
449 /* Build dn array */
450 $path= split(',', $dn);
451 $path= array_reverse($path);
453 /* Walk along the path to evaluate the acl */
454 $cpath= "";
455 foreach ($path as $element){
457 /* Clean potential ACLs for each level */
458 $acl= $this->cleanACL($acl);
460 if ($cpath == ""){
461 $cpath= $element;
462 } else {
463 $cpath= $element.','.$cpath;
464 }
465 if (isset($this->ACL[$cpath])){
467 /* Inspect this ACL, place the result into ACL */
468 foreach ($this->ACL[$cpath] as $subacl){
470 /* Reset? Just clean the ACL and turn over to the next one... */
471 if ($subacl['type'] == 'reset'){
472 $acl= $this->cleanACL($acl, TRUE);
473 continue;
474 }
476 if($subacl['type'] == 'role'){
477 echo "role skipped";
478 continue;
479 }
481 /* Per object ACL? */
482 foreach ($objects as $object){
483 if (isset($subacl['acl']["$module/$object"])){
484 foreach($subacl['acl']["$module/$object"] as $attribute => $dcl){
485 $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl']["$module/$object"][$attribute]);
486 }
487 }
488 }
490 /* Global ACL? */
491 if (isset($subacl['acl']["$module/all"][0])){
492 $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl']["$module/all"][0]);
493 continue;
494 }
496 /* Global ACL? */
497 if (isset($subacl['acl']["all"][0])){
498 $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl']["all"][0]);
499 continue;
500 }
501 }
502 }
503 }
505 /* Add department, if we have (some) permissions for the required module */
506 foreach ($acl as $val){
507 if ($val != ""){
508 $deps[]= $dn;
509 break;
510 }
511 }
512 }
514 $ACL_CACHE = &session::get('ACL_CACHE');
515 $ACL_CACHE['MODULE_DEPARTMENTS'][serialize($module)] = $deps;
516 return ($deps);
517 }
520 function mergeACL($acl, $type, $newACL)
521 {
522 if (strpos($newACL, 'w') !== FALSE && strpos($newACL, 'r') === FALSE){
523 $newACL .= "r";
524 }
526 foreach(str_split($newACL) as $char){
528 /* Ignore invalid characters */
529 if (!preg_match('/[rwcdm]/', $char)){
530 continue;
531 }
533 /* Skip permanent and subtree entries */
534 if (preg_match('/[sp]/', $acl[$char])){
535 continue;
536 }
538 switch ($type){
539 case 'psub':
540 $acl[$char]= 'p';
541 break;
543 case 'sub':
544 $acl[$char]= 's';
545 break;
547 case 'one':
548 $acl[$char]= 1;
549 break;
551 case 'base':
552 if ($acl[$char] != 1){
553 $acl[$char]= 0;
554 }
555 break;
556 }
557 }
559 return ($acl);
560 }
563 function cleanACL($acl, $reset= FALSE)
564 {
565 foreach ($acl as &$value){
567 /* Reset removes everything but 'p' */
568 if ($reset && $value != 'p'){
569 $value= "";
570 continue;
571 }
573 /* Decrease tree level */
574 if (is_int($value)){
575 if ($value){
576 $value--;
577 } else {
578 $value= "";
579 }
580 }
581 }
583 return ($acl);
584 }
587 /* #FIXME This could be logical wrong or could be optimized in the future
588 Return combined acls for a given category.
589 All acls will be combined like boolean AND
590 As example ('rwcdm' + 'rcd' + 'wrm'= 'r')
592 Results will be cached in $this->result_cache.
593 $this->result_cache will be resetted if load_acls is called.
594 */
595 function has_complete_category_acls($dn,$category)
596 {
597 $acl = "rwcdm";
598 $types = "rwcdm";
600 if(!is_string($category)){
601 trigger_error("category must be string");
602 $acl = "";
603 }else{
604 if(!isset($this->result_cache['has_complete_category_acls'][$dn][$category])) {
605 if (isset($this->ocMapping[$category])){
606 foreach($this->ocMapping[$category] as $oc){
608 /* Skip objectClass '0' (e.g. users/0) get_permissions will ever return '' ?? */
609 if($oc == "0") continue;
610 $tmp = $this->get_permissions($dn, $category."/".$oc);
611 for($i = 0 ; $i < strlen($types); $i++) {
612 if(!preg_match("/".$types[$i]."/",$tmp)){
613 $acl = preg_replace("/".$types[$i]."/","",$acl);
614 }
615 }
616 }
617 }else{
618 trigger_error("Invalid type of category ".$category);
619 $acl = "";
620 }
621 $this->result_cache['has_complete_category_acls'][$dn][$category] = $acl;
622 }else{
623 $acl = $this->result_cache['has_complete_category_acls'][$dn][$category];
624 }
625 }
626 return($acl);
627 }
630 /*! \brief Returns TRUE if the current user is configured in IGNORE_ACL=".." in your gosa.conf
631 @param Return Boolean TRUE if we have to skip ACL checks else FALSE.
632 */
633 function ignore_acl_for_current_user()
634 {
635 return(isset($this->config->current['IGNORE_ACL']) && $this->config->current['IGNORE_ACL'] == $this->dn);
636 }
638 }
640 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
641 ?>