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(isset($this->config->idepartments[$cpath])){
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(isset($this->config->idepartments[$dn])){
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 foreach($this->ACL as $dn => $infos){
450 foreach($infos as $info){
451 $found = FALSE;
452 foreach($info['acl'] as $cat => $data){
454 /* Skip self acls? */
455 if($skip_self_acls && isset($data['0']) && strpos($data['0'], "s")) continue;
456 if(preg_match("/^".normalizePreg($mod)."/",$cat)){
457 $found =TRUE;
458 break;
459 }
460 }
462 if($found && !isset($this->config->idepartments[$dn])){
463 while(!isset($this->config->idepartments[$dn]) && strpos($dn, ",")){
464 $dn = preg_replace("/^[^,]+,/","",$dn);
465 }
466 if(isset($this->config->idepartments[$dn])){
467 $deps[] = $dn;
468 }
469 }
470 }
471 }
473 /* For all gosaDepartments */
474 foreach ($this->config->departments as $dn){
475 if(in_array($dn,$deps)) continue;
476 $acl = "";
477 if(strpos($mod, '/')){
478 $acl.= $this->get_permissions($dn,$mod);
479 }else{
480 $acl.= $this->get_category_permissions($dn,$mod,TRUE);
481 }
482 if(!empty($acl)) {
483 $deps[] = $dn;
484 }
485 }
487 $ACL_CACHE['MODULE_DEPARTMENTS'][$mod] = $deps;
488 session::set('ACL_CACHE',$ACL_CACHE);
490 $res = array_merge($res,$deps);
491 }
492 return ($res);
493 }
496 function mergeACL($acl, $type, $newACL)
497 {
498 $at= array("psub" => "p", "sub" => "s", "one" => "1");
500 if (strpos($newACL, 'w') !== FALSE && strpos($newACL, 'r') === FALSE){
501 $newACL .= "r";
502 }
504 /* Ignore invalid characters */
505 $newACL= preg_replace('/[^rwcdm]/', '', $newACL);
507 foreach(str_split($newACL) as $char){
509 /* Skip permanent and subtree entries */
510 if (preg_match('/[sp]/', $acl[$char])){
511 continue;
512 }
514 if ($type == "base" && $acl[$char] != 1) {
515 $acl[$char]= 0;
516 } else {
517 $acl[$char]= $at[$type];
518 }
519 }
521 return ($acl);
522 }
525 function cleanACL($acl, $reset= FALSE)
526 {
527 foreach ($acl as $key => $value){
529 /* Continue, if value is empty or permanent */
530 if ($value == "" || $value == "p") {
531 continue;
532 }
534 /* Reset removes everything but 'p' */
535 if ($reset && $value != 'p'){
536 $acl[$key]= "";
537 continue;
538 }
540 /* Decrease tree level */
541 if (is_int($value)){
542 if ($value){
543 $acl[$key]--;
544 } else {
545 $acl[$key]= "";
546 }
547 }
548 }
550 return ($acl);
551 }
554 /* #FIXME This could be logical wrong or could be optimized in the future
555 Return combined acls for a given category.
556 All acls will be combined like boolean AND
557 As example ('rwcdm' + 'rcd' + 'wrm'= 'r')
559 Results will be cached in $this->result_cache.
560 $this->result_cache will be resetted if load_acls is called.
561 */
562 function has_complete_category_acls($dn,$category)
563 {
564 $acl = "rwcdm";
565 $types = "rwcdm";
567 if(!is_string($category)){
568 trigger_error("category must be string");
569 $acl = "";
570 }else{
571 if(!isset($this->result_cache['has_complete_category_acls'][$dn][$category])) {
572 if (isset($this->ocMapping[$category])){
573 foreach($this->ocMapping[$category] as $oc){
575 /* Skip objectClass '0' (e.g. users/0) get_permissions will ever return '' ?? */
576 if($oc == "0") continue;
577 $tmp = $this->get_permissions($dn, $category."/".$oc);
578 for($i = 0 ; $i < strlen($types); $i++) {
579 if(!preg_match("/".$types[$i]."/",$tmp)){
580 $acl = preg_replace("/".$types[$i]."/","",$acl);
581 }
582 }
583 }
584 }else{
585 trigger_error("Invalid type of category ".$category);
586 $acl = "";
587 }
588 $this->result_cache['has_complete_category_acls'][$dn][$category] = $acl;
589 }else{
590 $acl = $this->result_cache['has_complete_category_acls'][$dn][$category];
591 }
592 }
593 return($acl);
594 }
597 /*! \brief Returns TRUE if the current user is configured in IGNORE_ACL=".." in your gosa.conf
598 @param Return Boolean TRUE if we have to skip ACL checks else FALSE.
599 */
600 function ignore_acl_for_current_user()
601 {
602 return($this->config->get_cfg_value("ignoreAcl") == $this->dn);
603 }
605 }
607 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
608 ?>