4b2f11b2e2d6ea089e0fb319efc2884c63580ddc
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 {
73 #--------------------------------------------------------------------------OLD-BUT-ACTIVE-----------------------------
74 $ldap= $this->config->get_ldap_link();
76 /* Load ACL's from all groups we're in */
77 $this->subtreeACL= array();
78 $ldap->cd($this->config->current['BASE']);
79 if ($this->gidNumber == -1){
80 $ldap->search("(&(objectClass=posixGroup)(objectClass=gosaObject)".
81 "(memberUid=$this->username))");
82 } else {
83 $ldap->search("(&(objectClass=posixGroup)(objectClass=gosaObject)".
84 "(|(memberUid=$this->username)(gidNumber=$this->gidNumber)))");
85 }
87 while($attrs = $ldap->fetch()){
88 $base= preg_replace('/^[^,]+,ou=[^,]+,/i', "",$ldap->getDN());
89 $base= preg_replace("/[ ]*,[ ]*/", ",", $base);
91 for ($i= 0; $i<$attrs["gosaSubtreeACL"]["count"]; $i++){
92 $this->subtreeACL[$base][]= $attrs["gosaSubtreeACL"][$i];
93 }
94 }
95 #echo "NEW ACL LOADING --------------------------------------------------------------------------------------------<br>";
97 $this->ACL= array();
98 $this->groups= array();
99 $this->result_cache =array();
100 $ldap= $this->config->get_ldap_link();
101 $ldap->cd($this->config->current['BASE']);
103 /* Get member groups... */
104 $ldap->search("(&(objectClass=posixGroup)(memberUid=".$this->uid."))", array('dn'));
105 while ($attrs= $ldap->fetch()){
106 $this->groups[$attrs['dn']]= $attrs['dn'];
107 }
109 /* Crawl through ACLs and move relevant to the tree */
110 $ldap->search("(objectClass=gosaACL)", array('dn', 'gosaAclEntry'));
111 $aclp= array();
112 $aclc= array();
113 while ($attrs= $ldap->fetch()){
115 /* Insert links in ACL array */
116 $aclp[$attrs['dn']]= substr_count($attrs['dn'], ',');
117 $aclc[$attrs['dn']]= array();
118 $ol= array();
119 for($i= 0; $i<$attrs['gosaAclEntry']['count']; $i++){
120 $ol= array_merge($ol, @acl::explodeAcl($attrs['gosaAclEntry'][$i]));
121 }
122 $aclc[$attrs['dn']]= $ol;
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 }
163 }
166 function get_category_permissions($dn, $category)
167 {
168 /* Get list of objectClasses and get the permissions for it */
169 $acl= "";
170 if (isset($this->ocMapping[$category])){
171 foreach($this->ocMapping[$category] as $oc){
172 $acl.= $this->get_permissions($dn, $category."/".$oc);
173 }
174 }
176 return ($acl);
177 }
180 function get_permissions($dn, $object, $attribute= "", $skip_write= FALSE)
181 {
182 $acl= array("r" => "", "w" => "", "c" => "", "d" => "", "m" => "", "a" => "");
184 /* Build dn array */
185 $path= split(',', $dn);
186 $path= array_reverse($path);
188 /* Walk along the path to evaluate the acl */
189 $cpath= "";
190 foreach ($path as $element){
192 /* Clean potential ACLs for each level */
193 $acl= $this->cleanACL($acl);
195 if ($cpath == ""){
196 $cpath= $element;
197 } else {
198 $cpath= $element.','.$cpath;
199 }
200 if (isset($this->ACL[$cpath])){
202 /* Inspect this ACL, place the result into ACL */
203 foreach ($this->ACL[$cpath] as $subacl){
205 /* Reset? Just clean the ACL and turn over to the next one... */
206 if ($subacl['type'] == 'reset'){
207 $acl= $this->cleanACL($acl, TRUE);
208 continue;
209 }
211 /* Per attribute ACL? */
212 if (isset($subacl['acl'][$object][$attribute])){
213 $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl'][$object][$attribute]);
214 continue;
215 }
217 /* Per object ACL? */
218 if (isset($subacl['acl'][$object][0])){
219 $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl'][$object][0]);
220 continue;
221 }
223 /* Global ACL? */
224 if (isset($subacl['acl']['all'][0])){
225 $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl']['all'][0]);
226 continue;
227 }
229 /* If attribute is "", we want to know, if we've *any* permissions here... */
230 if ($attribute == "" && isset($subacl['acl'][$object])){
231 foreach($subacl['acl'][$object] as $attr => $dummy){
232 $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl'][$object][$attr]);
233 }
234 continue;
235 }
237 }
238 }
239 }
241 /* Assemble string */
242 $ret= "";
243 foreach ($acl as $key => $value){
244 if ($value != ""){
245 $ret.= $key;
246 }
247 }
249 /* Remove write if needed */
250 if ($skip_write){
251 $ret= preg_replace('/w/', '', $ret);
252 }
254 return ($ret);
255 }
258 /* Extract all departments that are accessible (direct or 'on the way' to an
259 accessible department) */
260 function get_module_departments($module)
261 {
262 global $plist;
264 $objects= array();
265 $deps= array();
267 /* Extract all relevant objects for this module from plist */
268 foreach ($plist->info as $object => $info){
269 if (!isset($info['plCategory'])){
270 continue;
271 }
272 foreach ($info['plCategory'] as $idx => $data){
273 if (preg_match('/^[0-9]+$/', $idx)){
274 if ($data == $module){
275 $objects[$object]= $object;
276 }
277 } else {
278 if ($idx == $module){
279 $objects[$object]= $object;
280 }
281 }
282 }
283 }
285 /* For all gosaDepartments */
286 foreach ($this->config->departments as $dn){
287 $acl= array("r" => "", "w" => "", "c" => "", "d" => "", "m" => "", "a" => "");
289 /* Build dn array */
290 $path= split(',', $dn);
291 $path= array_reverse($path);
293 /* Walk along the path to evaluate the acl */
294 $cpath= "";
295 foreach ($path as $element){
297 /* Clean potential ACLs for each level */
298 $acl= $this->cleanACL($acl);
300 if ($cpath == ""){
301 $cpath= $element;
302 } else {
303 $cpath= $element.','.$cpath;
304 }
305 if (isset($this->ACL[$cpath])){
307 /* Inspect this ACL, place the result into ACL */
308 foreach ($this->ACL[$cpath] as $subacl){
310 /* Reset? Just clean the ACL and turn over to the next one... */
311 if ($subacl['type'] == 'reset'){
312 $acl= $this->cleanACL($acl, TRUE);
313 continue;
314 }
316 /* Per object ACL? */
317 foreach ($objects as $object){
318 if (isset($subacl['acl']["$module/$object"])){
319 foreach($subacl['acl']["$module/$object"] as $attribute => $dcl){
320 $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl']["$module/$object"][$attribute]);
321 }
322 }
323 }
325 /* Global ACL? */
326 if (isset($subacl['acl']["$module/all"][0])){
327 $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl']["$module/all"][0]);
328 continue;
329 }
331 /* Global ACL? */
332 if (isset($subacl['acl']["all"][0])){
333 $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl']["all"][0]);
334 continue;
335 }
336 }
337 }
338 }
340 /* Add department, if we have (some) permissions for the required module */
341 foreach ($acl as $val){
342 if ($val != ""){
343 $deps[]= $dn;
344 break;
345 }
346 }
347 }
349 return ($deps);
350 }
353 function mergeACL($acl, $type, $newACL)
354 {
355 if(preg_match("/w/",$newACL) && !preg_match("/r/",$newACL)){
356 $newACL .= "r";
357 }
358 foreach(str_split($newACL) as $char){
360 /* Ignore invalid characters */
361 if (!preg_match('/[rwcdm]/', $char)){
362 continue;
363 }
365 /* Skip permanent and subtree entries */
366 if (preg_match('/[sp]/', $acl[$char])){
367 continue;
368 }
370 switch ($type){
371 case 'psub':
372 $acl[$char]= 'p';
373 break;
375 case 'sub':
376 $acl[$char]= 's';
377 break;
379 case 'one':
380 $acl[$char]= 1;
381 break;
383 case 'base':
384 if ($acl[$char] != 1){
385 $acl[$char]= 0;
386 }
387 break;
388 }
389 }
391 return ($acl);
392 }
395 function cleanACL($acl, $reset= FALSE)
396 {
397 foreach ($acl as $key => $value){
399 /* Reset removes everything but 'p' */
400 if ($reset && $value != 'p'){
401 $acl[$key]= "";
402 continue;
403 }
405 /* Decrease tree level */
406 if (preg_match('/^[0-9]+$/', $value)){
407 if ($value > 0){
408 $acl[$key]= $value - 1;
409 } else {
410 $acl[$key]= "";
411 }
412 }
413 }
415 return ($acl);
416 }
419 /* #FIXME This could be logical wrong or could be optimized in the future
420 Return combined acls for a given category.
421 All acls will be combined like boolean AND
422 As example ('rwcdm' + 'rcd' + 'wrm'= 'r')
424 Results will be cached in $this->result_cache.
425 $this->result_cache will be resetted if load_acls is called.
426 */
427 function has_complete_category_acls($dn,$category)
428 {
429 $acl = "rwcdm";
430 $types = "rwcdm";
433 if(!is_string($category)){
434 trigger_error("category must be string");
435 $acl = "";
436 }else{
437 if(!isset($this->result_cache['has_complete_category_acls'][$dn][$category])) {
438 if (isset($this->ocMapping[$category])){
439 foreach($this->ocMapping[$category] as $oc){
441 /* Skip objectClass '0' (e.g. users/0) get_permissions will ever return '' ?? */
442 if($oc == "0") continue;
443 $tmp = $this->get_permissions($dn, $category."/".$oc);
444 for($i = 0 ; $i < strlen($types); $i++) {
445 if(!preg_match("/".$types[$i]."/",$tmp)){
446 $acl = preg_replace("/".$types[$i]."/","",$acl);
447 }
448 }
449 }
450 }else{
451 trigger_error("Invalid type of category ".$category);
452 $acl = "";
453 }
454 $this->result_cache['has_complete_category_acls'][$dn][$category] = $acl;
455 }else{
456 $acl = $this->result_cache['has_complete_category_acls'][$dn][$category];
457 }
458 }
459 return($acl);
460 }
461 }
463 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
464 ?>