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 }
176 }
179 function get_category_permissions($dn, $category)
180 {
181 /* If we are forced to skip ACLs checks for the current user
182 then return all permissions.
183 */
184 if($this->ignore_acl_for_current_user()){
185 return("rwcdm");
186 }
188 /* Get list of objectClasses and get the permissions for it */
189 $acl= "";
190 if (isset($this->ocMapping[$category])){
191 foreach($this->ocMapping[$category] as $oc){
192 $acl.= $this->get_permissions($dn, $category."/".$oc);
193 }
194 }else{
195 trigger_error("ACL request for an invalid category (".$category.").");
196 }
198 return ($acl);
199 }
202 function get_permissions($dn, $object, $attribute= "", $skip_write= FALSE)
203 {
204 /* If we are forced to skip ACLs checks for the current user
205 then return all permissions.
206 */
207 if($this->ignore_acl_for_current_user()){
208 return("rwcdm");
209 }
211 /* Push cache answer? */
212 $ACL_CACHE = &session::get('ACL_CACHE');
213 if (isset($ACL_CACHE["$dn+$object+$attribute"])){
215 /* Remove write if needed */
216 if ($skip_write){
217 $ret = preg_replace('/w/', '', $ACL_CACHE["$dn+$object+$attribute"]);
218 }else{
219 $ret = $ACL_CACHE["$dn+$object+$attribute"];
220 }
221 return($ret);
222 }
224 /* Get ldap object, for later filter checks
225 */
226 $ldap = $this->config->get_ldap_link();
228 $acl= array("r" => "", "w" => "", "c" => "", "d" => "", "m" => "", "a" => "");
230 /* Build dn array */
231 $path= split(',', $dn);
232 $path= array_reverse($path);
234 /* Walk along the path to evaluate the acl */
235 $cpath= "";
236 foreach ($path as $element){
238 /* Clean potential ACLs for each level */
239 $acl= $this->cleanACL($acl);
241 if ($cpath == ""){
242 $cpath= $element;
243 } else {
244 $cpath= $element.','.$cpath;
245 }
246 if (isset($this->ACL[$cpath])){
248 /* Inspect this ACL, place the result into ACL */
249 foreach ($this->ACL[$cpath] as $subacl){
251 /* Reset? Just clean the ACL and turn over to the next one... */
252 if ($subacl['type'] == 'reset'){
253 $acl= $this->cleanACL($acl, TRUE);
254 continue;
255 }
257 if($subacl['type'] == "role") {
258 echo "role skipped";
259 continue;
260 }
262 /* With user filter */
263 if (isset($subacl['filter']) && !empty($subacl['filter'])){
264 $sdn = preg_replace("/^[^,]*+,/","",$dn);
265 $ldap->cd($sdn);
266 $ldap->ls($subacl['filter'],$sdn);
267 if(!$ldap->count()){
268 continue;
269 }else{
270 $found = FALSE;
271 while($attrs = $ldap->fetch()){
272 if($attrs['dn'] == $dn){
273 $found = TRUE;
274 break;
275 }
276 }
277 if(!$found){
278 continue;
279 }
280 }
281 }
283 /* Per attribute ACL? */
284 if (isset($subacl['acl'][$object][$attribute])){
285 $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl'][$object][$attribute]);
286 continue;
287 }
289 /* Per object ACL? */
290 if (isset($subacl['acl'][$object][0])){
291 $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl'][$object][0]);
292 continue;
293 }
295 /* Global ACL? */
296 if (isset($subacl['acl']['all'][0])){
297 $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl']['all'][0]);
298 continue;
299 }
301 /* If attribute is "", we want to know, if we've *any* permissions here... */
302 if ($attribute == "" && isset($subacl['acl'][$object])){
303 foreach($subacl['acl'][$object] as $attr => $dummy){
304 $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl'][$object][$attr]);
305 }
306 continue;
307 }
309 }
310 }
311 }
313 /* Assemble string */
314 $ret= "";
315 foreach ($acl as $key => $value){
316 if ($value !== ""){
317 $ret.= $key;
318 }
319 }
321 $ACL_CACHE["$dn+$object+$attribute"]= $ret;
323 /* Remove write if needed */
324 if ($skip_write){
325 $ret= preg_replace('/w/', '', $ret);
326 }
327 return ($ret);
328 }
331 /* Extract all departments that are accessible (direct or 'on the way' to an
332 accessible department) */
333 function get_module_departments($module)
334 {
336 /* If we are forced to skip ACLs checks for the current user
337 then return all departments as valid.
338 */
339 if($this->ignore_acl_for_current_user()){
340 return(array_keys($this->config->idepartments));
341 }
343 /* Use cached results if possilbe */
344 $ACL_CACHE = session::get('ACL_CACHE');
345 if(isset($ACL_CACHE['MODULE_DEPARTMENTS'][serialize($module)])){
346 return($ACL_CACHE['MODULE_DEPARTMENTS'][serialize($module)]);
347 }
349 global $plist;
351 $objects= array();
352 $deps= array();
354 /* Extract all relevant objects for this module from plist */
355 foreach ($plist->info as $object => $info){
356 if (!isset($info['plCategory'])){
357 continue;
358 }
359 foreach ($info['plCategory'] as $idx => $data){
360 if (preg_match('/^[0-9]+$/', $idx)){
361 if ($data == $module){
362 $objects[$object]= $object;
363 }
364 } else {
365 if ($idx == $module){
366 $objects[$object]= $object;
367 }
368 }
369 }
370 }
372 /* For all gosaDepartments */
373 foreach ($this->config->departments as $dn){
374 $acl= array("r" => "", "w" => "", "c" => "", "d" => "", "m" => "", "a" => "");
376 /* Build dn array */
377 $path= split(',', $dn);
378 $path= array_reverse($path);
380 /* Walk along the path to evaluate the acl */
381 $cpath= "";
382 foreach ($path as $element){
384 /* Clean potential ACLs for each level */
385 $acl= $this->cleanACL($acl);
387 if ($cpath == ""){
388 $cpath= $element;
389 } else {
390 $cpath= $element.','.$cpath;
391 }
392 if (isset($this->ACL[$cpath])){
394 /* Inspect this ACL, place the result into ACL */
395 foreach ($this->ACL[$cpath] as $subacl){
397 /* Reset? Just clean the ACL and turn over to the next one... */
398 if ($subacl['type'] == 'reset'){
399 $acl= $this->cleanACL($acl, TRUE);
400 continue;
401 }
403 if($subacl['type'] == 'role'){
404 echo "role skipped";
405 continue;
406 }
408 /* Per object ACL? */
409 foreach ($objects as $object){
410 if (isset($subacl['acl']["$module/$object"])){
411 foreach($subacl['acl']["$module/$object"] as $attribute => $dcl){
412 $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl']["$module/$object"][$attribute]);
413 }
414 }
415 }
417 /* Global ACL? */
418 if (isset($subacl['acl']["$module/all"][0])){
419 $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl']["$module/all"][0]);
420 continue;
421 }
423 /* Global ACL? */
424 if (isset($subacl['acl']["all"][0])){
425 $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl']["all"][0]);
426 continue;
427 }
428 }
429 }
430 }
432 /* Add department, if we have (some) permissions for the required module */
433 foreach ($acl as $val){
434 if ($val != ""){
435 $deps[]= $dn;
436 break;
437 }
438 }
439 }
441 $ACL_CACHE = &session::get('ACL_CACHE');
442 $ACL_CACHE['MODULE_DEPARTMENTS'][serialize($module)] = $deps;
443 return ($deps);
444 }
447 function mergeACL($acl, $type, $newACL)
448 {
449 if (strpos($newACL, 'w') !== FALSE && strpos($newACL, 'r') === FALSE){
450 $newACL .= "r";
451 }
453 foreach(str_split($newACL) as $char){
455 /* Ignore invalid characters */
456 if (!preg_match('/[rwcdm]/', $char)){
457 continue;
458 }
460 /* Skip permanent and subtree entries */
461 if (preg_match('/[sp]/', $acl[$char])){
462 continue;
463 }
465 switch ($type){
466 case 'psub':
467 $acl[$char]= 'p';
468 break;
470 case 'sub':
471 $acl[$char]= 's';
472 break;
474 case 'one':
475 $acl[$char]= 1;
476 break;
478 case 'base':
479 if ($acl[$char] != 1){
480 $acl[$char]= 0;
481 }
482 break;
483 }
484 }
486 return ($acl);
487 }
490 function cleanACL($acl, $reset= FALSE)
491 {
492 foreach ($acl as &$value){
494 /* Reset removes everything but 'p' */
495 if ($reset && $value != 'p'){
496 $value= "";
497 continue;
498 }
500 /* Decrease tree level */
501 if (is_int($value)){
502 if ($value){
503 $value--;
504 } else {
505 $value= "";
506 }
507 }
508 }
510 return ($acl);
511 }
514 /* #FIXME This could be logical wrong or could be optimized in the future
515 Return combined acls for a given category.
516 All acls will be combined like boolean AND
517 As example ('rwcdm' + 'rcd' + 'wrm'= 'r')
519 Results will be cached in $this->result_cache.
520 $this->result_cache will be resetted if load_acls is called.
521 */
522 function has_complete_category_acls($dn,$category)
523 {
524 $acl = "rwcdm";
525 $types = "rwcdm";
527 if(!is_string($category)){
528 trigger_error("category must be string");
529 $acl = "";
530 }else{
531 if(!isset($this->result_cache['has_complete_category_acls'][$dn][$category])) {
532 if (isset($this->ocMapping[$category])){
533 foreach($this->ocMapping[$category] as $oc){
535 /* Skip objectClass '0' (e.g. users/0) get_permissions will ever return '' ?? */
536 if($oc == "0") continue;
537 $tmp = $this->get_permissions($dn, $category."/".$oc);
538 for($i = 0 ; $i < strlen($types); $i++) {
539 if(!preg_match("/".$types[$i]."/",$tmp)){
540 $acl = preg_replace("/".$types[$i]."/","",$acl);
541 }
542 }
543 }
544 }else{
545 trigger_error("Invalid type of category ".$category);
546 $acl = "";
547 }
548 $this->result_cache['has_complete_category_acls'][$dn][$category] = $acl;
549 }else{
550 $acl = $this->result_cache['has_complete_category_acls'][$dn][$category];
551 }
552 }
553 return($acl);
554 }
557 /*! \brief Returns TRUE if the current user is configured in IGNORE_ACL=".." in your gosa.conf
558 @param Return Boolean TRUE if we have to skip ACL checks else FALSE.
559 */
560 function ignore_acl_for_current_user()
561 {
562 return(isset($this->config->current['IGNORE_ACL']) && $this->config->current['IGNORE_ACL'] == $this->dn);
563 }
565 }
567 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
568 ?>