e89e6da123b717bd3cee52d39ef047177b6673e6
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;
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 }
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 {
424 /* If we are forced to skip ACLs checks for the current user
425 then return all departments as valid.
426 */
427 if($this->ignore_acl_for_current_user()){
428 return(array_keys($this->config->idepartments));
429 }
431 /* Use cached results if possilbe */
432 $ACL_CACHE = session::get('ACL_CACHE');
434 if(!is_array($module)){
435 $module = array($module);
436 }
438 global $plist;
439 $res = array();
440 foreach($module as $mod){
441 if(isset($ACL_CACHE['MODULE_DEPARTMENTS'][$mod])){
442 $res = array_merge($res,$ACL_CACHE['MODULE_DEPARTMENTS'][$mod]);
443 continue;
444 }
446 $deps = array();
448 # /* Search for per object ACLs.
449 # */
450 # $this->config->get_departments();
451 # $this->config->make_idepartments();
453 foreach($this->ACL as $dn => $infos){
454 foreach($infos as $info){
455 $found = FALSE;
456 foreach($info['acl'] as $cat => $data){
458 /* Skip self acls? */
459 if($skip_self_acls && isset($data['0']) && strpos($data['0'], "s")) continue;
460 if(preg_match("/^".normalizePreg($mod)."/",$cat)){
461 $found =TRUE;
462 break;
463 }
464 }
466 if($found && !isset($this->config->idepartments[$dn])){
467 while(!isset($this->config->idepartments[$dn]) && strpos($dn, ",")){
468 $dn = preg_replace("/^[^,]+,/","",$dn);
469 }
470 if(isset($this->config->idepartments[$dn])){
471 $deps[] = $dn;
472 }
473 }
474 }
475 }
477 /* For all gosaDepartments */
478 foreach ($this->config->departments as $dn){
479 if(in_array($dn,$deps)) continue;
480 $acl = "";
481 if(strpos($mod, '/')){
482 $acl.= $this->get_permissions($dn,$mod);
483 }else{
484 $acl.= $this->get_category_permissions($dn,$mod,TRUE);
485 }
486 if(!empty($acl)) {
487 $deps[] = $dn;
488 }
489 }
491 $ACL_CACHE['MODULE_DEPARTMENTS'][$mod] = $deps;
492 $res = array_merge($res,$deps);
493 }
494 return ($deps);
495 }
498 function mergeACL($acl, $type, $newACL)
499 {
500 $at= array("psub" => "p", "sub" => "s", "one" => "1");
502 if (strpos($newACL, 'w') !== FALSE && strpos($newACL, 'r') === FALSE){
503 $newACL .= "r";
504 }
506 /* Ignore invalid characters */
507 $newACL= preg_replace('/[^rwcdm]/', '', $newACL);
509 foreach(str_split($newACL) as $char){
511 /* Skip permanent and subtree entries */
512 if (preg_match('/[sp]/', $acl[$char])){
513 continue;
514 }
516 if ($type == "base" && $acl[$char] != 1) {
517 $acl[$char]= 0;
518 } else {
519 $acl[$char]= $at[$type];
520 }
521 }
523 return ($acl);
524 }
527 function cleanACL($acl, $reset= FALSE)
528 {
529 foreach ($acl as $key => $value){
531 /* Continue, if value is empty or permanent */
532 if ($value == "" || $value == "p") {
533 continue;
534 }
536 /* Reset removes everything but 'p' */
537 if ($reset && $value != 'p'){
538 $acl[$key]= "";
539 continue;
540 }
542 /* Decrease tree level */
543 if (is_int($value)){
544 if ($value){
545 $acl[$key]--;
546 } else {
547 $acl[$key]= "";
548 }
549 }
550 }
552 return ($acl);
553 }
556 /* #FIXME This could be logical wrong or could be optimized in the future
557 Return combined acls for a given category.
558 All acls will be combined like boolean AND
559 As example ('rwcdm' + 'rcd' + 'wrm'= 'r')
561 Results will be cached in $this->result_cache.
562 $this->result_cache will be resetted if load_acls is called.
563 */
564 function has_complete_category_acls($dn,$category)
565 {
566 $acl = "rwcdm";
567 $types = "rwcdm";
569 if(!is_string($category)){
570 trigger_error("category must be string");
571 $acl = "";
572 }else{
573 if(!isset($this->result_cache['has_complete_category_acls'][$dn][$category])) {
574 if (isset($this->ocMapping[$category])){
575 foreach($this->ocMapping[$category] as $oc){
577 /* Skip objectClass '0' (e.g. users/0) get_permissions will ever return '' ?? */
578 if($oc == "0") continue;
579 $tmp = $this->get_permissions($dn, $category."/".$oc);
580 for($i = 0 ; $i < strlen($types); $i++) {
581 if(!preg_match("/".$types[$i]."/",$tmp)){
582 $acl = preg_replace("/".$types[$i]."/","",$acl);
583 }
584 }
585 }
586 }else{
587 trigger_error("Invalid type of category ".$category);
588 $acl = "";
589 }
590 $this->result_cache['has_complete_category_acls'][$dn][$category] = $acl;
591 }else{
592 $acl = $this->result_cache['has_complete_category_acls'][$dn][$category];
593 }
594 }
595 return($acl);
596 }
599 /*! \brief Returns TRUE if the current user is configured in IGNORE_ACL=".." in your gosa.conf
600 @param Return Boolean TRUE if we have to skip ACL checks else FALSE.
601 */
602 function ignore_acl_for_current_user()
603 {
604 return($this->config->get_cfg_value("ignoreAcl") == $this->dn);
605 }
607 }
609 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
610 ?>