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 /* If we are forced to skip ACLs checks for the current user
186 then return all permissions.
187 */
188 if($this->ignore_acl_for_current_user()){
189 return("rwcdm");
190 }
192 $ACL_CACHE = &session::get("ACL_CACHE");
193 $id = $dn."+".$category."+".$any_acl;
194 if(isset($ACL_CACHE['CATEGORY_ACL'][$id])){
195 return($ACL_CACHE['CATEGORY_ACL'][$id]);
196 }
199 /* Get list of objectClasses and get the permissions for it */
200 $acl= "";
201 if (isset($this->ocMapping[$category])){
202 foreach($this->ocMapping[$category] as $oc){
203 $acl.= $this->get_permissions($dn, $category."/".$oc);
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 = TRUE;
266 foreach($object as $category){
267 $w &= preg_match("/w/",$this->has_complete_category_acls($dn, $category));
268 $r &= preg_match("/r/",$this->has_complete_category_acls($dn, $category));
269 }
270 return($r && $w );
271 }
274 /*! \brief Checks if we are allowed to create a snapshot of the given dn.
275 @param String The source dn
276 @param String The acl category (e.g. users)
277 @return Boolean TRUE if we are allowed to restore a snapshot.
278 */
279 function allow_snapshot_create($dn, $object)
280 {
281 if(!is_array($object)){
282 $object = array($object);
283 }
284 $r = TRUE;
285 foreach($object as $category){
286 $r &= preg_match("/r/",$this->has_complete_category_acls($dn, $category));
287 }
288 return($r) ;
289 }
292 function get_permissions($dn, $object, $attribute= "", $skip_write= FALSE)
293 {
294 /* If we are forced to skip ACLs checks for the current user
295 then return all permissions.
296 */
297 if($this->ignore_acl_for_current_user()){
298 if($skip_write){
299 return("rcdm");
300 }
301 return("rwcdm");
302 }
304 /* Push cache answer? */
305 $ACL_CACHE = &session::get('ACL_CACHE');
306 if (isset($ACL_CACHE["$dn+$object+$attribute"])){
308 /* Remove write if needed */
309 if ($skip_write){
310 $ret = preg_replace('/w/', '', $ACL_CACHE["$dn+$object+$attribute"]);
311 }else{
312 $ret = $ACL_CACHE["$dn+$object+$attribute"];
313 }
314 return($ret);
315 }
317 /* Get ldap object, for later filter checks
318 */
319 $ldap = $this->config->get_ldap_link();
321 $acl= array("r" => "", "w" => "", "c" => "", "d" => "", "m" => "", "a" => "");
323 /* Build dn array */
324 $path= split(',', $dn);
325 $path= array_reverse($path);
327 /* Walk along the path to evaluate the acl */
328 $cpath= "";
329 foreach ($path as $element){
331 /* Clean potential ACLs for each level */
332 if(isset($this->config->idepartments[$cpath])){
333 $acl= $this->cleanACL($acl);
334 }
336 if ($cpath == ""){
337 $cpath= $element;
338 } else {
339 $cpath= $element.','.$cpath;
340 }
342 if (isset($this->ACL[$cpath])){
344 /* Inspect this ACL, place the result into ACL */
345 foreach ($this->ACL[$cpath] as $subacl){
347 /* Reset? Just clean the ACL and turn over to the next one... */
348 if ($subacl['type'] == 'reset'){
349 $acl= $this->cleanACL($acl, TRUE);
350 continue;
351 }
353 if($subacl['type'] == "role") {
354 echo "role skipped";
355 continue;
356 }
358 /* With user filter */
359 if (isset($subacl['filter']) && !empty($subacl['filter'])){
360 $id = $dn."-".$subacl['filter'];
361 if(!isset($ACL_CACHE['FILTER'][$id])){
362 $ACL_CACHE['FILTER'][$id] = $ldap->object_match_filter($dn,$subacl['filter']);
363 }
364 if(!$ACL_CACHE['FILTER'][$id]){
365 continue;
366 }
367 }
369 /* Self ACLs?
370 */
371 if($dn != $this->dn && isset($subacl['acl'][$object][0]) && strpos($subacl['acl'][$object][0],"s")){
372 continue;
373 }
375 /* If attribute is "", we want to know, if we've *any* permissions here...
376 Merge global class ACLs [0] with attributes specific ACLs [attribute].
377 */
378 if ($attribute == "" && isset($subacl['acl'][$object])){
379 foreach($subacl['acl'][$object] as $attr => $dummy){
380 $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl'][$object][$attr]);
381 }
382 continue;
383 }
385 /* Per attribute ACL? */
386 if (isset($subacl['acl'][$object][$attribute])){
387 $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl'][$object][$attribute]);
388 continue;
389 }
391 /* Per object ACL? */
392 if (isset($subacl['acl'][$object][0])){
393 $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl'][$object][0]);
394 continue;
395 }
397 /* Global ACL? */
398 if (isset($subacl['acl']['all'][0])){
399 $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl']['all'][0]);
400 continue;
401 }
402 }
403 }
404 }
406 /* If the requested ACL is for a container object, then alter
407 ACLs by applying cleanACL a last time.
408 */
409 if(isset($this->config->idepartments[$dn])){
410 $acl = $this->cleanACL($acl);
411 }
413 /* Assemble string */
414 $ret= "";
415 foreach ($acl as $key => $value){
416 if ($value !== ""){
417 $ret.= $key;
418 }
419 }
421 $ACL_CACHE["$dn+$object+$attribute"]= $ret;
423 /* Remove write if needed */
424 if ($skip_write){
425 $ret= preg_replace('/w/', '', $ret);
426 }
427 return ($ret);
428 }
431 /* Extract all departments that are accessible (direct or 'on the way' to an
432 accessible department) */
433 function get_module_departments($module, $skip_self_acls = FALSE )
434 {
435 /* If we are forced to skip ACLs checks for the current user
436 then return all departments as valid.
437 */
438 if($this->ignore_acl_for_current_user()){
439 return(array_keys($this->config->idepartments));
440 }
442 /* Use cached results if possilbe */
443 $ACL_CACHE = &session::get('ACL_CACHE');
445 if(!is_array($module)){
446 $module = array($module);
447 }
449 global $plist;
450 $res = array();
451 foreach($module as $mod){
452 if(isset($ACL_CACHE['MODULE_DEPARTMENTS'][$mod])){
453 $res = array_merge($res,$ACL_CACHE['MODULE_DEPARTMENTS'][$mod]);
454 continue;
455 }
457 $deps = array();
459 /* Search for per object ACLs */
460 foreach($this->ACL as $dn => $infos){
461 foreach($infos as $info){
462 $found = FALSE;
463 foreach($info['acl'] as $cat => $data){
465 /* Skip self acls? */
466 if($skip_self_acls && isset($data['0']) && strpos($data['0'], "s")) continue;
467 if(preg_match("/^".normalizePreg($mod)."/",$cat)){
468 $found =TRUE;
469 break;
470 }
471 }
473 if($found && !isset($this->config->idepartments[$dn])){
474 while(!isset($this->config->idepartments[$dn]) && strpos($dn, ",")){
475 $dn = preg_replace("/^[^,]+,/","",$dn);
476 }
477 if(isset($this->config->idepartments[$dn])){
478 $deps[] = $dn;
479 }
480 }
481 }
482 }
484 /* For all gosaDepartments */
485 foreach ($this->config->departments as $dn){
486 if(in_array($dn,$deps)) continue;
487 $acl = "";
488 if(strpos($mod, '/')){
489 $acl.= $this->get_permissions($dn,$mod);
490 }else{
491 $acl.= $this->get_category_permissions($dn,$mod,TRUE);
492 }
493 if(!empty($acl)) {
494 $deps[] = $dn;
495 }
496 }
498 $ACL_CACHE['MODULE_DEPARTMENTS'][$mod] = $deps;
499 $res = array_merge($res,$deps);
500 }
501 return ($res);
502 }
505 function mergeACL($acl, $type, $newACL)
506 {
507 $at= array("psub" => "p", "sub" => "s", "one" => "1");
509 if (strpos($newACL, 'w') !== FALSE && strpos($newACL, 'r') === FALSE){
510 $newACL .= "r";
511 }
513 /* Ignore invalid characters */
514 $newACL= preg_replace('/[^rwcdm]/', '', $newACL);
516 foreach(str_split($newACL) as $char){
518 /* Skip permanent and subtree entries */
519 if (preg_match('/[sp]/', $acl[$char])){
520 continue;
521 }
523 if ($type == "base" && $acl[$char] != 1) {
524 $acl[$char]= 0;
525 } else {
526 $acl[$char]= $at[$type];
527 }
528 }
530 return ($acl);
531 }
534 function cleanACL($acl, $reset= FALSE)
535 {
536 foreach ($acl as $key => $value){
538 /* Continue, if value is empty or permanent */
539 if ($value == "" || $value == "p") {
540 continue;
541 }
543 /* Reset removes everything but 'p' */
544 if ($reset && $value != 'p'){
545 $acl[$key]= "";
546 continue;
547 }
549 /* Decrease tree level */
550 if (is_int($value)){
551 if ($value){
552 $acl[$key]--;
553 } else {
554 $acl[$key]= "";
555 }
556 }
557 }
559 return ($acl);
560 }
563 /* #FIXME This could be logical wrong or could be optimized in the future
564 Return combined acls for a given category.
565 All acls will be combined like boolean AND
566 As example ('rwcdm' + 'rcd' + 'wrm'= 'r')
568 Results will be cached in $this->result_cache.
569 $this->result_cache will be resetted if load_acls is called.
570 */
571 function has_complete_category_acls($dn,$category)
572 {
573 $acl = "rwcdm";
574 $types = "rwcdm";
576 if(!is_string($category)){
577 trigger_error("category must be string");
578 $acl = "";
579 }else{
580 if(!isset($this->result_cache['has_complete_category_acls'][$dn][$category])) {
581 if (isset($this->ocMapping[$category])){
582 foreach($this->ocMapping[$category] as $oc){
584 /* Skip objectClass '0' (e.g. users/0) get_permissions will ever return '' ?? */
585 if($oc == "0") continue;
586 $tmp = $this->get_permissions($dn, $category."/".$oc);
587 for($i = 0 ; $i < strlen($types); $i++) {
588 if(!preg_match("/".$types[$i]."/",$tmp)){
589 $acl = preg_replace("/".$types[$i]."/","",$acl);
590 }
591 }
592 }
593 }else{
594 trigger_error("Invalid type of category ".$category);
595 $acl = "";
596 }
597 $this->result_cache['has_complete_category_acls'][$dn][$category] = $acl;
598 }else{
599 $acl = $this->result_cache['has_complete_category_acls'][$dn][$category];
600 }
601 }
602 return($acl);
603 }
606 /*! \brief Returns TRUE if the current user is configured in IGNORE_ACL=".." in your gosa.conf
607 @param Return Boolean TRUE if we have to skip ACL checks else FALSE.
608 */
609 function ignore_acl_for_current_user()
610 {
611 return($this->ignoreACL);
612 }
614 }
616 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
617 ?>