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 }
175 }
178 function get_category_permissions($dn, $category)
179 {
180 /* Get list of objectClasses and get the permissions for it */
181 $acl= "";
182 if (isset($this->ocMapping[$category])){
183 foreach($this->ocMapping[$category] as $oc){
184 $acl.= $this->get_permissions($dn, $category."/".$oc);
185 }
186 }
188 return ($acl);
189 }
192 function get_permissions($dn, $object, $attribute= "", $skip_write= FALSE)
193 {
194 /* Push cache answer? */
195 $ACL_CACHE = &session::get('ACL_CACHE');
196 if (isset($ACL_CACHE["$dn+$object+$attribute"])){
198 /* Remove write if needed */
199 if ($skip_write){
200 $ret = preg_replace('/w/', '', $ACL_CACHE["$dn+$object+$attribute"]);
201 }else{
202 $ret = $ACL_CACHE["$dn+$object+$attribute"];
203 }
204 return($ret);
205 }
207 $acl= array("r" => "", "w" => "", "c" => "", "d" => "", "m" => "", "a" => "");
209 /* Build dn array */
210 $path= split(',', $dn);
211 $path= array_reverse($path);
213 /* Walk along the path to evaluate the acl */
214 $cpath= "";
215 foreach ($path as $element){
217 /* Clean potential ACLs for each level */
218 $acl= $this->cleanACL($acl);
220 if ($cpath == ""){
221 $cpath= $element;
222 } else {
223 $cpath= $element.','.$cpath;
224 }
225 if (isset($this->ACL[$cpath])){
227 /* Inspect this ACL, place the result into ACL */
228 foreach ($this->ACL[$cpath] as $subacl){
230 /* Reset? Just clean the ACL and turn over to the next one... */
231 if ($subacl['type'] == 'reset'){
232 $acl= $this->cleanACL($acl, TRUE);
233 continue;
234 }
236 if($subacl['type'] == "role") {
237 echo "role skipped";
238 continue;
239 }
241 /* Per attribute ACL? */
242 if (isset($subacl['acl'][$object][$attribute])){
243 $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl'][$object][$attribute]);
244 continue;
245 }
247 /* Per object ACL? */
248 if (isset($subacl['acl'][$object][0])){
249 $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl'][$object][0]);
250 continue;
251 }
253 /* Global ACL? */
254 if (isset($subacl['acl']['all'][0])){
255 $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl']['all'][0]);
256 continue;
257 }
259 /* If attribute is "", we want to know, if we've *any* permissions here... */
260 if ($attribute == "" && isset($subacl['acl'][$object])){
261 foreach($subacl['acl'][$object] as $attr => $dummy){
262 $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl'][$object][$attr]);
263 }
264 continue;
265 }
267 }
268 }
269 }
271 /* Assemble string */
272 $ret= "";
273 foreach ($acl as $key => $value){
274 if ($value != ""){
275 $ret.= $key;
276 }
277 }
279 $ACL_CACHE["$dn+$object+$attribute"]= $ret;
281 /* Remove write if needed */
282 if ($skip_write){
283 $ret= preg_replace('/w/', '', $ret);
284 }
285 return ($ret);
286 }
289 /* Extract all departments that are accessible (direct or 'on the way' to an
290 accessible department) */
291 function get_module_departments($module)
292 {
293 /* Use cached results if possilbe */
294 $ACL_CACHE = session::get('ACL_CACHE');
295 if(isset($ACL_CACHE['MODULE_DEPARTMENTS'][serialize($module)])){
296 return($ACL_CACHE['MODULE_DEPARTMENTS'][serialize($module)]);
297 }
299 global $plist;
301 $objects= array();
302 $deps= array();
304 /* Extract all relevant objects for this module from plist */
305 foreach ($plist->info as $object => $info){
306 if (!isset($info['plCategory'])){
307 continue;
308 }
309 foreach ($info['plCategory'] as $idx => $data){
310 if (preg_match('/^[0-9]+$/', $idx)){
311 if ($data == $module){
312 $objects[$object]= $object;
313 }
314 } else {
315 if ($idx == $module){
316 $objects[$object]= $object;
317 }
318 }
319 }
320 }
322 /* For all gosaDepartments */
323 foreach ($this->config->departments as $dn){
324 $acl= array("r" => "", "w" => "", "c" => "", "d" => "", "m" => "", "a" => "");
326 /* Build dn array */
327 $path= split(',', $dn);
328 $path= array_reverse($path);
330 /* Walk along the path to evaluate the acl */
331 $cpath= "";
332 foreach ($path as $element){
334 /* Clean potential ACLs for each level */
335 $acl= $this->cleanACL($acl);
337 if ($cpath == ""){
338 $cpath= $element;
339 } else {
340 $cpath= $element.','.$cpath;
341 }
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 /* Per object ACL? */
359 foreach ($objects as $object){
360 if (isset($subacl['acl']["$module/$object"])){
361 foreach($subacl['acl']["$module/$object"] as $attribute => $dcl){
362 $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl']["$module/$object"][$attribute]);
363 }
364 }
365 }
367 /* Global ACL? */
368 if (isset($subacl['acl']["$module/all"][0])){
369 $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl']["$module/all"][0]);
370 continue;
371 }
373 /* Global ACL? */
374 if (isset($subacl['acl']["all"][0])){
375 $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl']["all"][0]);
376 continue;
377 }
378 }
379 }
380 }
382 /* Add department, if we have (some) permissions for the required module */
383 foreach ($acl as $val){
384 if ($val != ""){
385 $deps[]= $dn;
386 break;
387 }
388 }
389 }
391 $ACL_CACHE = &session::get('ACL_CACHE');
392 $ACL_CACHE['MODULE_DEPARTMENTS'][serialize($module)] = $deps;
393 return ($deps);
394 }
397 function mergeACL($acl, $type, $newACL)
398 {
399 if (strpos($newACL, 'w') !== FALSE && strpos($newACL, 'r') === FALSE){
400 $newACL .= "r";
401 }
403 foreach(str_split($newACL) as $char){
405 /* Ignore invalid characters */
406 if (!preg_match('/[rwcdm]/', $char)){
407 continue;
408 }
410 /* Skip permanent and subtree entries */
411 if (preg_match('/[sp]/', $acl[$char])){
412 continue;
413 }
415 switch ($type){
416 case 'psub':
417 $acl[$char]= 'p';
418 break;
420 case 'sub':
421 $acl[$char]= 's';
422 break;
424 case 'one':
425 $acl[$char]= 1;
426 break;
428 case 'base':
429 if ($acl[$char] != 1){
430 $acl[$char]= 0;
431 }
432 break;
433 }
434 }
436 return ($acl);
437 }
440 function cleanACL($acl, $reset= FALSE)
441 {
442 foreach ($acl as &$value){
444 /* Reset removes everything but 'p' */
445 if ($reset && $value != 'p'){
446 $value= "";
447 continue;
448 }
450 /* Decrease tree level */
451 if (is_int($value)){
452 if ($value){
453 $value--;
454 } else {
455 $value= "";
456 }
457 }
458 }
460 return ($acl);
461 }
464 /* #FIXME This could be logical wrong or could be optimized in the future
465 Return combined acls for a given category.
466 All acls will be combined like boolean AND
467 As example ('rwcdm' + 'rcd' + 'wrm'= 'r')
469 Results will be cached in $this->result_cache.
470 $this->result_cache will be resetted if load_acls is called.
471 */
472 function has_complete_category_acls($dn,$category)
473 {
474 $acl = "rwcdm";
475 $types = "rwcdm";
477 if(!is_string($category)){
478 trigger_error("category must be string");
479 $acl = "";
480 }else{
481 if(!isset($this->result_cache['has_complete_category_acls'][$dn][$category])) {
482 if (isset($this->ocMapping[$category])){
483 foreach($this->ocMapping[$category] as $oc){
485 /* Skip objectClass '0' (e.g. users/0) get_permissions will ever return '' ?? */
486 if($oc == "0") continue;
487 $tmp = $this->get_permissions($dn, $category."/".$oc);
488 for($i = 0 ; $i < strlen($types); $i++) {
489 if(!preg_match("/".$types[$i]."/",$tmp)){
490 $acl = preg_replace("/".$types[$i]."/","",$acl);
491 }
492 }
493 }
494 }else{
495 trigger_error("Invalid type of category ".$category);
496 $acl = "";
497 }
498 $this->result_cache['has_complete_category_acls'][$dn][$category] = $acl;
499 }else{
500 $acl = $this->result_cache['has_complete_category_acls'][$dn][$category];
501 }
502 }
503 return($acl);
504 }
505 }
507 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
508 ?>