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