1 <?php
2 /*
3 This code is part of GOsa (https://gosa.gonicus.de)
4 Copyright (C) 2003-2005 Cajus Pollmeier
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
21 class userinfo
22 {
23 var $dn;
24 var $ip;
25 var $username;
26 var $cn;
27 var $uid;
28 var $gidNumber= -1;
29 var $language= "";
30 var $config;
31 var $gosaUnitTag= "";
32 var $subtreeACL= array();
33 var $ACL= array();
34 var $ocMapping= array();
35 var $groups= array();
36 var $result_cache =array();
38 /* get acl's an put them into the userinfo object
39 attr subtreeACL (userdn:components, userdn:component1#sub1#sub2,component2,...) */
40 function userinfo(&$config, $userdn){
41 $this->config= &$config;
42 $ldap= $this->config->get_ldap_link();
43 $ldap->cat($userdn,array('sn', 'givenName', 'uid', 'gidNumber', 'preferredLanguage', 'gosaUnitTag'));
44 $attrs= $ldap->fetch();
46 if (isset($attrs['givenName'][0]) && isset($attrs['sn'][0])){
47 $this->cn= $attrs['givenName'][0]." ".$attrs['sn'][0];
48 } else {
49 $this->cn= $attrs['uid'][0];
50 }
51 if (isset($attrs['gidNumber'][0])){
52 $this->gidNumber= $attrs['gidNumber'][0];
53 }
55 /* Assign user language */
56 if (isset($attrs['preferredLanguage'][0])){
57 $this->language= $attrs['preferredLanguage'][0];
58 }
60 if (isset($attrs['gosaUnitTag'][0])){
61 $this->gosaUnitTag= $attrs['gosaUnitTag'][0];
62 }
64 $this->dn= $userdn;
65 $this->uid= $attrs['uid'][0];
66 $this->ip= $_SERVER['REMOTE_ADDR'];
67 }
70 function loadACL()
71 {
72 $this->ACL= array();
73 $this->groups= array();
74 $this->result_cache =array();
75 $ldap= $this->config->get_ldap_link();
76 $ldap->cd($this->config->current['BASE']);
78 /* Get member groups... */
79 $ldap->search("(&(objectClass=posixGroup)(memberUid=".$this->uid."))", array('dn'));
80 while ($attrs= $ldap->fetch()){
81 $this->groups[$attrs['dn']]= $attrs['dn'];
82 }
84 /* Crawl through ACLs and move relevant to the tree */
85 $ldap->search("(objectClass=gosaACL)", array('dn', 'gosaAclEntry'));
86 $aclp= array();
87 $aclc= array();
88 while ($attrs= $ldap->fetch()){
90 /* Insert links in ACL array */
91 $aclp[$attrs['dn']]= substr_count($attrs['dn'], ',');
92 $aclc[$attrs['dn']]= array();
93 $ol= array();
94 for($i= 0; $i<$attrs['gosaAclEntry']['count']; $i++){
95 $ol= array_merge($ol, @acl::explodeAcl($attrs['gosaAclEntry'][$i]));
96 }
97 $aclc[$attrs['dn']]= $ol;
98 }
100 /* Resolve roles here.
101 */
102 foreach($aclc as $dn => $data){
103 foreach($data as $prio => $aclc_value) {
104 if($aclc_value['type'] == "role"){
106 unset($aclc[$dn][$prio]);
108 $ldap->cat($aclc_value['acl'],array("gosaAclTemplate"));
109 $attrs = $ldap->fetch();
111 if(isset($attrs['gosaAclTemplate'])){
112 for($i= 0; $i<$attrs['gosaAclTemplate']['count']; $i++){
113 $tmp = @acl::explodeAcl($attrs['gosaAclTemplate'][$i]);
115 foreach($tmp as $new_acl){
116 $new_acl['members'] = $aclc_value['members'];
117 $aclc[$dn][] =$new_acl;
118 }
119 }
120 }
121 }
122 }
123 }
125 /* ACL's read, sort for tree depth */
126 asort($aclp);
128 /* Sort in tree order */
129 foreach ($aclp as $dn => $acl){
130 /* Check if we need to keep this ACL */
131 foreach($aclc[$dn] as $idx => $type){
132 $interresting= FALSE;
134 /* No members? This is good for all users... */
135 if (!count($type['members'])){
136 $interresting= TRUE;
137 } else {
139 /* Inspect members... */
140 foreach ($type['members'] as $grp => $grpdsc){
141 /* Some group inside the members that is relevant for us? */
142 if (in_array_ics(preg_replace('/^G:/', '', $grp), $this->groups)){
143 $interresting= TRUE;
144 }
146 /* User inside the members? */
147 if (preg_replace('/^U:/', '', $grp) == $this->dn){
148 $interresting= TRUE;
149 }
150 }
151 }
153 if ($interresting){
154 if (!isset($this->ACL[$dn])){
155 $this->ACL[$dn]= array();
156 }
157 $this->ACL[$dn][$idx]= $type;
158 }
159 }
161 }
162 }
165 function get_category_permissions($dn, $category)
166 {
167 /* Get list of objectClasses and get the permissions for it */
168 $acl= "";
169 if (isset($this->ocMapping[$category])){
170 foreach($this->ocMapping[$category] as $oc){
171 $acl.= $this->get_permissions($dn, $category."/".$oc);
172 }
173 }
175 return ($acl);
176 }
179 function get_permissions($dn, $object, $attribute= "", $skip_write= FALSE)
180 {
181 $acl= array("r" => "", "w" => "", "c" => "", "d" => "", "m" => "", "a" => "");
183 /* Build dn array */
184 $path= split(',', $dn);
185 $path= array_reverse($path);
187 /* Walk along the path to evaluate the acl */
188 $cpath= "";
189 foreach ($path as $element){
191 /* Clean potential ACLs for each level */
192 $acl= $this->cleanACL($acl);
194 if ($cpath == ""){
195 $cpath= $element;
196 } else {
197 $cpath= $element.','.$cpath;
198 }
199 if (isset($this->ACL[$cpath])){
201 /* Inspect this ACL, place the result into ACL */
202 foreach ($this->ACL[$cpath] as $subacl){
204 /* Reset? Just clean the ACL and turn over to the next one... */
205 if ($subacl['type'] == 'reset'){
206 $acl= $this->cleanACL($acl, TRUE);
207 continue;
208 }
210 if($subacl['type'] == "role") {
211 echo "role skipped";
212 continue;
213 }
215 /* Per attribute ACL? */
216 if (isset($subacl['acl'][$object][$attribute])){
217 $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl'][$object][$attribute]);
218 continue;
219 }
221 /* Per object ACL? */
222 if (isset($subacl['acl'][$object][0])){
223 $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl'][$object][0]);
224 continue;
225 }
227 /* Global ACL? */
228 if (isset($subacl['acl']['all'][0])){
229 $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl']['all'][0]);
230 continue;
231 }
233 /* If attribute is "", we want to know, if we've *any* permissions here... */
234 if ($attribute == "" && isset($subacl['acl'][$object])){
235 foreach($subacl['acl'][$object] as $attr => $dummy){
236 $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl'][$object][$attr]);
237 }
238 continue;
239 }
241 }
242 }
243 }
245 /* Assemble string */
246 $ret= "";
247 foreach ($acl as $key => $value){
248 if ($value != ""){
249 $ret.= $key;
250 }
251 }
253 /* Remove write if needed */
254 if ($skip_write){
255 $ret= preg_replace('/w/', '', $ret);
256 }
258 return ($ret);
259 }
262 /* Extract all departments that are accessible (direct or 'on the way' to an
263 accessible department) */
264 function get_module_departments($module)
265 {
266 global $plist;
268 $objects= array();
269 $deps= array();
271 /* Extract all relevant objects for this module from plist */
272 foreach ($plist->info as $object => $info){
273 if (!isset($info['plCategory'])){
274 continue;
275 }
276 foreach ($info['plCategory'] as $idx => $data){
277 if (preg_match('/^[0-9]+$/', $idx)){
278 if ($data == $module){
279 $objects[$object]= $object;
280 }
281 } else {
282 if ($idx == $module){
283 $objects[$object]= $object;
284 }
285 }
286 }
287 }
289 /* For all gosaDepartments */
290 foreach ($this->config->departments as $dn){
291 $acl= array("r" => "", "w" => "", "c" => "", "d" => "", "m" => "", "a" => "");
293 /* Build dn array */
294 $path= split(',', $dn);
295 $path= array_reverse($path);
297 /* Walk along the path to evaluate the acl */
298 $cpath= "";
299 foreach ($path as $element){
301 /* Clean potential ACLs for each level */
302 $acl= $this->cleanACL($acl);
304 if ($cpath == ""){
305 $cpath= $element;
306 } else {
307 $cpath= $element.','.$cpath;
308 }
309 if (isset($this->ACL[$cpath])){
311 /* Inspect this ACL, place the result into ACL */
312 foreach ($this->ACL[$cpath] as $subacl){
314 /* Reset? Just clean the ACL and turn over to the next one... */
315 if ($subacl['type'] == 'reset'){
316 $acl= $this->cleanACL($acl, TRUE);
317 continue;
318 }
320 if($subacl['type'] == 'role'){
321 echo "role skipped";
322 continue;
323 }
325 /* Per object ACL? */
326 foreach ($objects as $object){
327 if (isset($subacl['acl']["$module/$object"])){
328 foreach($subacl['acl']["$module/$object"] as $attribute => $dcl){
329 $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl']["$module/$object"][$attribute]);
330 }
331 }
332 }
334 /* Global ACL? */
335 if (isset($subacl['acl']["$module/all"][0])){
336 $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl']["$module/all"][0]);
337 continue;
338 }
340 /* Global ACL? */
341 if (isset($subacl['acl']["all"][0])){
342 $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl']["all"][0]);
343 continue;
344 }
345 }
346 }
347 }
349 /* Add department, if we have (some) permissions for the required module */
350 foreach ($acl as $val){
351 if ($val != ""){
352 $deps[]= $dn;
353 break;
354 }
355 }
356 }
358 return ($deps);
359 }
362 function mergeACL($acl, $type, $newACL)
363 {
364 if(preg_match("/w/",$newACL) && !preg_match("/r/",$newACL)){
365 $newACL .= "r";
366 }
367 foreach(str_split($newACL) as $char){
369 /* Ignore invalid characters */
370 if (!preg_match('/[rwcdm]/', $char)){
371 continue;
372 }
374 /* Skip permanent and subtree entries */
375 if (preg_match('/[sp]/', $acl[$char])){
376 continue;
377 }
379 switch ($type){
380 case 'psub':
381 $acl[$char]= 'p';
382 break;
384 case 'sub':
385 $acl[$char]= 's';
386 break;
388 case 'one':
389 $acl[$char]= 1;
390 break;
392 case 'base':
393 if ($acl[$char] != 1){
394 $acl[$char]= 0;
395 }
396 break;
397 }
398 }
400 return ($acl);
401 }
404 function cleanACL($acl, $reset= FALSE)
405 {
406 foreach ($acl as &$value){
408 /* Reset removes everything but 'p' */
409 if ($reset && $value != 'p'){
410 $value= "";
411 continue;
412 }
414 /* Decrease tree level */
415 if (is_int($value)){
416 if ($value){
417 $value--;
418 } else {
419 $value= "";
420 }
421 }
422 }
424 return ($acl);
425 }
428 /* #FIXME This could be logical wrong or could be optimized in the future
429 Return combined acls for a given category.
430 All acls will be combined like boolean AND
431 As example ('rwcdm' + 'rcd' + 'wrm'= 'r')
433 Results will be cached in $this->result_cache.
434 $this->result_cache will be resetted if load_acls is called.
435 */
436 function has_complete_category_acls($dn,$category)
437 {
438 $acl = "rwcdm";
439 $types = "rwcdm";
441 if(!is_string($category)){
442 trigger_error("category must be string");
443 $acl = "";
444 }else{
445 if(!isset($this->result_cache['has_complete_category_acls'][$dn][$category])) {
446 if (isset($this->ocMapping[$category])){
447 foreach($this->ocMapping[$category] as $oc){
449 /* Skip objectClass '0' (e.g. users/0) get_permissions will ever return '' ?? */
450 if($oc == "0") continue;
451 $tmp = $this->get_permissions($dn, $category."/".$oc);
452 for($i = 0 ; $i < strlen($types); $i++) {
453 if(!preg_match("/".$types[$i]."/",$tmp)){
454 $acl = preg_replace("/".$types[$i]."/","",$acl);
455 }
456 }
457 }
458 }else{
459 trigger_error("Invalid type of category ".$category);
460 $acl = "";
461 }
462 $this->result_cache['has_complete_category_acls'][$dn][$category] = $acl;
463 }else{
464 $acl = $this->result_cache['has_complete_category_acls'][$dn][$category];
465 }
466 }
467 return($acl);
468 }
469 }
471 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
472 ?>