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();
37 /* get acl's an put them into the userinfo object
38 attr subtreeACL (userdn:components, userdn:component1#sub1#sub2,component2,...) */
39 function userinfo($config, $userdn){
40 $this->config= $config;
41 $ldap= $this->config->get_ldap_link();
42 $ldap->cat($userdn,array('sn', 'givenName', 'uid', 'gidNumber', 'preferredLanguage', 'gosaUnitTag'));
43 $attrs= $ldap->fetch();
45 if (isset($attrs['givenName'][0]) && isset($attrs['sn'][0])){
46 $this->cn= $attrs['givenName'][0]." ".$attrs['sn'][0];
47 } else {
48 $this->cn= $attrs['uid'][0];
49 }
50 if (isset($attrs['gidNumber'][0])){
51 $this->gidNumber= $attrs['gidNumber'][0];
52 }
54 /* Assign user language */
55 if (isset($attrs['preferredLanguage'][0])){
56 $this->language= $attrs['preferredLanguage'][0];
57 }
59 if (isset($attrs['gosaUnitTag'][0])){
60 $this->gosaUnitTag= $attrs['gosaUnitTag'][0];
61 }
63 $this->dn= $userdn;
64 $this->uid= $attrs['uid'][0];
65 $this->ip= $_SERVER['REMOTE_ADDR'];
66 }
69 function loadACL()
70 {
72 #--------------------------------------------------------------------------OLD-BUT-ACTIVE-----------------------------
73 $ldap= $this->config->get_ldap_link();
75 /* Load ACL's from all groups we're in */
76 $this->subtreeACL= array();
77 $ldap->cd($this->config->current['BASE']);
78 if ($this->gidNumber == -1){
79 $ldap->search("(&(objectClass=posixGroup)(objectClass=gosaObject)".
80 "(memberUid=$this->username))");
81 } else {
82 $ldap->search("(&(objectClass=posixGroup)(objectClass=gosaObject)".
83 "(|(memberUid=$this->username)(gidNumber=$this->gidNumber)))");
84 }
86 while($attrs = $ldap->fetch()){
87 $base= preg_replace('/^[^,]+,ou=[^,]+,/i', "",$ldap->getDN());
88 $base= preg_replace("/[ ]*,[ ]*/", ",", $base);
90 for ($i= 0; $i<$attrs["gosaSubtreeACL"]["count"]; $i++){
91 $this->subtreeACL[$base][]= $attrs["gosaSubtreeACL"][$i];
92 }
93 }
94 #echo "NEW ACL LOADING --------------------------------------------------------------------------------------------<br>";
96 $this->ACL= array();
97 $this->groups= array();
98 $ldap= $this->config->get_ldap_link();
99 $ldap->cd($this->config->current['BASE']);
101 /* Get member groups... */
102 $ldap->search("(&(objectClass=posixGroup)(memberUid=".$this->uid."))", array('dn'));
103 while ($attrs= $ldap->fetch()){
104 $this->groups[$attrs['dn']]= $attrs['dn'];
105 }
107 /* Crawl through ACLs and move relevant to the tree */
108 $ldap->search("(objectClass=gosaACL)", array('dn', 'gosaAclEntry'));
109 $aclp= array();
110 $aclc= array();
111 while ($attrs= $ldap->fetch()){
113 /* Insert links in ACL array */
114 $aclp[$attrs['dn']]= substr_count($attrs['dn'], ',');
115 $aclc[$attrs['dn']]= array();
116 $ol= array();
117 for($i= 0; $i<$attrs['gosaAclEntry']['count']; $i++){
118 $ol= array_merge($ol, @acl::explodeAcl($attrs['gosaAclEntry'][$i]));
119 }
120 $aclc[$attrs['dn']]= $ol;
121 }
123 /* ACL's read, sort for tree depth */
124 asort($aclp);
126 /* Sort in tree order */
127 foreach ($aclp as $dn => $acl){
128 /* Check if we need to keep this ACL */
129 foreach($aclc[$dn] as $idx => $type){
130 $interresting= FALSE;
132 /* No members? This is good for all users... */
133 if (!count($type['members'])){
134 $interresting= TRUE;
135 } else {
137 /* Inspect members... */
138 foreach ($type['members'] as $grp => $grpdsc){
139 /* Some group inside the members that is relevant for us? */
140 if (in_array_ics(preg_replace('/^G:/', '', $grp), $this->groups)){
141 $interresting= TRUE;
142 }
144 /* User inside the members? */
145 if (preg_replace('/^U:/', '', $grp) == $this->dn){
146 $interresting= TRUE;
147 }
148 }
149 }
151 if ($interresting){
152 if (!isset($this->ACL[$dn])){
153 $this->ACL[$dn]= array();
154 }
155 $this->ACL[$dn][$idx]= $type;
156 }
157 }
159 }
161 }
164 function get_category_permissions($dn, $category)
165 {
166 /* Get list of objectClasses and get the permissions for it */
167 $acl= "";
168 if (isset($this->ocMapping[$category])){
169 foreach($this->ocMapping[$category] as $oc){
170 $acl.= $this->get_permissions($dn, $category."/".$oc);
171 }
172 }
174 return ($acl);
175 }
178 function get_permissions($dn, $object, $attribute= "", $skip_write= FALSE)
179 {
180 $acl= array("r" => "", "w" => "", "c" => "", "d" => "", "m" => "", "a" => "");
182 /* Build dn array */
183 $path= split(',', $dn);
184 $path= array_reverse($path);
186 /* Walk along the path to evaluate the acl */
187 $cpath= "";
188 foreach ($path as $element){
190 /* Clean potential ACLs for each level */
191 $acl= $this->cleanACL($acl);
193 if ($cpath == ""){
194 $cpath= $element;
195 } else {
196 $cpath= $element.','.$cpath;
197 }
198 if (isset($this->ACL[$cpath])){
200 /* Inspect this ACL, place the result into ACL */
201 foreach ($this->ACL[$cpath] as $subacl){
203 /* Reset? Just clean the ACL and turn over to the next one... */
204 if ($subacl['type'] == 'reset'){
205 $acl= $this->cleanACL($acl, TRUE);
206 continue;
207 }
209 /* Per attribute ACL? */
210 if (isset($subacl['acl'][$object][$attribute])){
211 $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl'][$object][$attribute]);
212 continue;
213 }
215 /* Per object ACL? */
216 if (isset($subacl['acl'][$object][0])){
217 $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl'][$object][0]);
218 continue;
219 }
221 /* Global ACL? */
222 if (isset($subacl['acl']['all'][0])){
223 $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl']['all'][0]);
224 continue;
225 }
227 /* If attribute is "", we want to know, if we've *any* permissions here... */
228 if ($attribute == "" && isset($subacl['acl'][$object])){
229 echo "Global $object<br>";
230 foreach($subacl['acl'][$object] as $attr => $dummy){
231 $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl'][$object][$attr]);
232 }
233 continue;
234 }
236 }
237 }
238 }
240 /* Assemble string */
241 $ret= "";
242 foreach ($acl as $key => $value){
243 if ($value != ""){
244 $ret.= $key;
245 }
246 }
248 /* Remove write if needed */
249 if ($skip_write){
250 $ret= preg_replace('/w/', '', $ret);
251 }
253 return ($ret);
254 }
257 /* Extract all departments that are accessible (direct or 'on the way' to an
258 accessible department) */
259 function get_module_departments($module)
260 {
261 global $plist;
263 $objects= array();
264 $deps= array();
266 /* Extract all relevant objects for this module from plist */
267 foreach ($plist->info as $object => $info){
268 if (!isset($info['plCategory'])){
269 continue;
270 }
271 foreach ($info['plCategory'] as $idx => $data){
272 if (preg_match('/^[0-9]+$/', $idx)){
273 if ($data == $module){
274 $objects[$object]= $object;
275 }
276 } else {
277 if ($idx == $module){
278 $objects[$object]= $object;
279 }
280 }
281 }
282 }
284 /* For all gosaDepartments */
285 foreach ($this->config->departments as $dn){
286 $acl= array("r" => "", "w" => "", "c" => "", "d" => "", "m" => "", "a" => "");
288 /* Build dn array */
289 $path= split(',', $dn);
290 $path= array_reverse($path);
292 /* Walk along the path to evaluate the acl */
293 $cpath= "";
294 foreach ($path as $element){
296 /* Clean potential ACLs for each level */
297 $acl= $this->cleanACL($acl);
299 if ($cpath == ""){
300 $cpath= $element;
301 } else {
302 $cpath= $element.','.$cpath;
303 }
304 if (isset($this->ACL[$cpath])){
306 /* Inspect this ACL, place the result into ACL */
307 foreach ($this->ACL[$cpath] as $subacl){
309 /* Reset? Just clean the ACL and turn over to the next one... */
310 if ($subacl['type'] == 'reset'){
311 $acl= $this->cleanACL($acl, TRUE);
312 continue;
313 }
315 /* Per object ACL? */
316 foreach ($objects as $object){
317 if (isset($subacl['acl']["$module/$object"])){
318 foreach($subacl['acl']["$module/$object"] as $attribute => $dcl){
319 $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl']["$module/$object"][$attribute]);
320 }
321 }
322 }
324 /* Global ACL? */
325 if (isset($subacl['acl']["$module/all"][0])){
326 $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl']["$module/all"][0]);
327 continue;
328 }
330 /* Global ACL? */
331 if (isset($subacl['acl']["all"][0])){
332 $acl= $this->mergeACL($acl, $subacl['type'], $subacl['acl']["all"][0]);
333 continue;
334 }
335 }
336 }
337 }
339 /* Add department, if we have (some) permissions for the required module */
340 foreach ($acl as $val){
341 if ($val != ""){
342 $deps[]= $dn;
343 break;
344 }
345 }
346 }
348 return ($deps);
349 }
352 function mergeACL($acl, $type, $newACL)
353 {
354 foreach(str_split($newACL) as $char){
356 /* Ignore invalid characters */
357 if (!preg_match('/[rwcdm]/', $char)){
358 continue;
359 }
361 /* Skip permanent and subtree entries */
362 if (preg_match('/[sp]/', $acl[$char])){
363 continue;
364 }
366 switch ($type){
367 case 'psub':
368 $acl[$char]= 'p';
369 break;
371 case 'sub':
372 $acl[$char]= 's';
373 break;
375 case 'one':
376 $acl[$char]= 1;
377 break;
379 case 'base':
380 if ($acl[$char] != 1){
381 $acl[$char]= 0;
382 }
383 break;
384 }
385 }
387 return ($acl);
388 }
391 function cleanACL($acl, $reset= FALSE)
392 {
393 foreach ($acl as $key => $value){
395 /* Reset removes everything but 'p' */
396 if ($reset && $value != 'p'){
397 $acl[$key]= "";
398 continue;
399 }
401 /* Decrease tree level */
402 if (preg_match('/^[0-9]+$/', $value)){
403 if ($value > 0){
404 $acl[$key]= $value - 1;
405 } else {
406 $acl[$key]= "";
407 }
408 }
409 }
411 return ($acl);
412 }
414 }
416 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
417 ?>