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 aclManagement extends plugin
24 {
25 /* Plugin definitions */
26 var $plHeadline = "ACL";
27 var $plDescription = "Manage access control lists";
29 /* Dialog attributes */
30 var $acltabs = NULL;
31 var $ui = NULL;
32 var $acl = "";
33 var $DivListACL = NULL;
35 var $CopyPasteHandler;
36 var $start_pasting_copied_objects = FALSE;
37 var $dns = array();
39 function aclManagement(&$config, &$ui)
40 {
41 /* Save configuration for internal use */
42 $this->config = &$config;
43 $this->ui = &$ui;
45 /* Copy & Paste enabled ?*/
46 if((isset($this->config->data['MAIN']['ENABLECOPYPASTE']))&&(preg_match("/true/i",$this->config->data['MAIN']['ENABLECOPYPASTE']))){
47 $this->CopyPasteHandler = new CopyPasteHandler($this->config);
48 }
50 /* Creat dialog object */
51 $this->DivListACL = new divListACL($this->config,$this);
52 }
55 function execute()
56 {
57 /* Call parent execute */
58 plugin::execute();
60 session::set('LOCK_VARS_TO_USE',array("/^item_selected/","/^menu_action/","/^list/","/^id_/","/^list_acl_role_del/","/^list_acl_del/","/^menu_action/"));
62 $smarty = get_smarty(); // Smarty instance
63 $s_action = ""; // Contains the action to be taken
64 $s_entry = ""; // The value for s_action
66 /* Edit entry button pressed? */
67 if( isset($_GET['act']) && $_GET['act'] == "list_edit_entry" ){
68 $s_action= "edit";
69 $s_entry= validate($_GET['id']);
70 }
72 /* Edit entry button pressed? */
73 if( isset($_GET['act']) && $_GET['act'] == "list_edit_role" ){
74 $s_action= "edit_role";
75 $s_entry= validate($_GET['id']);
76 }
78 $types = array(
79 "del" =>"^list_acl_del",
80 "edit" =>"^list_acl_edit",
81 "del_role" =>"^list_acl_role_del",
82 "edit_role" =>"^list_acl_role_edit",
83 "copy" =>"^copy",
84 "cut" =>"^cut",
85 "copy_multiple"=> "^multiple_copy_objects",
86 "cut_multiple" => "^multiple_cut_objects",
87 "editPaste" =>"^editPaste",
88 "addrole" =>"^new_acl_role");
90 /* Test relevant POST values */
91 foreach($_POST as $key => $val){
93 /* Get every possible POST combination and set s_action/s_entry accordingly */
94 foreach($types as $act => $name){
96 if (preg_match("/".$name.".*/", $key)){
97 $s_action= $act;
98 $s_entry= preg_replace("/".$name."_/i", "", $key);
99 }
100 }
101 }
103 /* Remove coordinate prefix from POST, required by some browsers */
104 $s_entry= preg_replace("/_.$/", "", $s_entry);
106 /* Seperate possibly encoded tab and entry, default to tab "acl" */
107 if(preg_match("/.*-.*/", $s_entry)){
108 $s_tab= preg_replace("/^[^-]*-/i", "" ,$s_entry);
109 $s_entry= preg_replace("/-[^-]*$/i", "", $s_entry);
110 }else{
111 $s_tab= "generic";
112 }
115 /* handle C&P from layers menu */
116 if(isset($_POST['menu_action']) && preg_match("/^multiple_copy_systems/",$_POST['menu_action'])){
117 $s_action = "copy_multiple";
118 }
119 if(isset($_POST['menu_action']) && preg_match("/^multiple_cut_systems/",$_POST['menu_action'])){
120 $s_action = "cut_multiple";
121 }
122 if(isset($_POST['menu_action']) && preg_match("/^editPaste/",$_POST['menu_action'])){
123 $s_action = "editPaste";
124 }
126 /* Create options */
127 if(isset($_POST['menu_action']) && $_POST['menu_action'] == "new_acl_role"){
128 $s_action = "new";
129 }
131 /* handle remove from layers menu */
132 if(isset($_POST['menu_action']) && preg_match("/^remove_multiple/",$_POST['menu_action'])){
133 $s_action = "del_multiple";
134 }
137 /********************
138 Copy & Paste Handling ...
139 ********************/
141 /* Display the copy & paste dialog, if it is currently open */
142 $ret = $this->copyPasteHandling_from_queue($s_action,$s_entry);
143 if($ret){
144 return($ret);
145 }
148 /********************
149 * Add new Role entry
150 ********************/
152 if(($s_action == "new") && (!isset($this->acltabs->config))){
154 /* Get 'dn' from posted acl, must be unique */
155 $this->dn= "new";
157 /* Check permissions */
158 if(preg_match("/c/",$this->ui->get_permissions($this->DivListACL->selectedBase,"acl/acl"))){
160 /* Register acltabs to trigger edit dialog */
161 $this->acltabs= new aclroletab($this->config, NULL,$this->dn);
162 $this->acltabs->set_acl_base($this->DivListACL->selectedBase);
163 }else{
164 msg_dialog::display(_("Permission error"), msgPool::permCreate(), ERROR_DIALOG);
165 }
166 }
168 /********************
169 Edit existing entry
170 ********************/
172 if (($s_action=="edit" || $s_action=="edit_role") && (!isset($this->acltabs->config))){
174 /* Get 'dn' from posted acl, must be unique */
175 $this->dn= $this->list[trim($s_entry)]['dn'];
177 /* Check permissions */
178 if(preg_match("/r/",$this->ui->get_permissions($this->dn,"acl/acl"))){
180 /* Check locking, save current plugin in 'back_plugin', so
181 the dialog knows where to return. */
182 if (($acl= get_lock($this->dn)) != ""){
183 return(gen_locked_message ($acl, $this->dn));
184 }
186 /* Lock the current entry, so everyone will get the above dialog */
187 add_lock ($this->dn, $this->ui->dn);
189 /* Register acltabs to trigger edit dialog */
190 if($s_action=="edit_role"){
191 $this->acltabs= new aclroletab($this->config, NULL,$this->dn);
192 $this->acltabs-> set_acl_base($this->dn);
193 }else{
194 $this->acltabs= new acltab($this->config, NULL,$this->dn);
195 $this->acltabs-> set_acl_base($this->dn);
196 }
198 /* Set ACL and move DN to the headline */
199 session::set('objectinfo',$this->dn);
200 }else{
201 msg_dialog::display(_("Permission error"), msgPool::permModify(), ERROR_DIALOG);
202 }
203 }
206 /********************
207 Edit canceled
208 ********************/
209 if(isset($_POST['delete_lock'])){
210 $this->remove_lock();
211 session::un_set('objectinfo');
212 }
214 /* Reset all relevant data, if we get a _cancel request */
215 if (isset($_POST['edit_cancel'])){
216 $this->remove_lock();
217 $this->acltabs= NULL;
218 session::un_set('objectinfo');
219 }
222 /********************
223 Delete entry requested, display confirm dialog
224 ********************/
226 /* Remove acl was requested */
227 if ($s_action=="del_role"){
229 /* Get 'dn' from posted acl */
230 $this->dn= $this->list[trim($s_entry)]['dn'];
232 /* Check permissions */
233 if(preg_match("/d/",$this->ui->get_permissions($this->dn,"aclroles/aclrole"))){
235 /* Check locking, save current plugin in 'back_plugin', so
236 the dialog knows where to return. */
237 if (($acl= get_lock($this->dn)) != ""){
238 return(gen_locked_message ($acl, $this->dn));
239 }
241 /* Lock the current entry, so nobody will edit it during deletion */
242 add_lock ($this->dn, $this->ui->dn);
243 $smarty->assign("info", msgPool::deleteInfo(@LDAP::fix($this->dn)));
244 $smarty->assign("is_role",true);
246 return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
247 } else {
249 /* Obviously the acl isn't allowed to delete. Show message and
250 clean session. */
251 msg_dialog::display(_("Permission error"), msgPool::permDelete(), ERROR_DIALOG);
252 }
253 }
256 /********************
257 Delete entry confirmed
258 ********************/
260 /* Confirmation for deletion has been passed. Acl should be deleted. */
261 if (isset($_POST['delete_acl_role_confirmed'])){
263 /* Check permissions */
264 if(preg_match("/d/",$this->ui->get_permissions($this->dn,"aclroles/aclrole"))){
266 /* Delete request is permitted, perform LDAP action */
267 $this->acltabs= new aclroletab($this->config, NULL,$this->dn);
268 $this->acltabs->delete();
269 unset ($this->acltabs);
270 $this->acltabs= NULL;
271 } else {
273 /* Normally this shouldn't be reached, send some extra
274 logs to notify the administrator */
275 msg_dialog::display(_("Permission error"), msgPool::permDelete(), ERROR_DIALOG);
277 if(isset($this->ui->uid)){
278 new log("security","aclroles/".get_class($this),$this->dn,array(),"Warning: '".$this->ui->uid."' tried to trick acl role deletion.");
279 }
280 }
282 /* Remove lock file after successfull deletion */
283 $this->remove_lock();
284 }
286 /********************
287 Delete entry requested, display confirm dialog
288 ********************/
290 /* Remove acl was requested */
291 if ($s_action=="del" || $s_action == "del_multiple"){
293 /* Get 'dn' from posted acl */
294 if($s_action == "del"){
295 $this->dns = array( $this->list[trim($s_entry)]['dn']);
296 }else{
297 $this->dns = array();
298 foreach($this->list_get_selected_items() as $id){
299 $this->dns[] = $this->list[$id]['dn'];
300 }
301 }
303 $disallowed = array();
304 foreach($this->dns as $key => $dn){
305 $acl = $this->ui->get_permissions($dn, "aclroles/aclrole");
306 if(!preg_match("/d/",$acl)){
307 unset($this->dns[$key]);
308 $disallowed[] = $dn;
309 }
310 }
312 if(count($disallowed)){
313 msg_dialog::display(_("Permission"),msgPool::permDelete($disallowed),INFO_DIALOG);
314 }
316 if(count($this->dns)){
319 /* Display lock messages */
320 if ($user= get_multiple_locks($this->dns)){
321 return(gen_locked_message($user,$this->dns));
322 }
324 $dns_names = array();
325 foreach($this->dns as $key => $dn){
327 /* Check permissions */
328 if(!preg_match("/d/",$this->ui->get_permissions($dn,"acl/acl"))){
330 /* Obviously the acl isn't allowed to delete. Show message and
331 clean session. */
332 msg_dialog::display(_("Permission error"), msgPool::permDelete(), ERROR_DIALOG);
333 unset($this->dns[$key]);
334 continue;
335 }
337 $dns_names[] = LDAP::fix($dn);
338 }
340 if(count($this->dns)){
341 add_lock($this->dns,$this->ui->dn);
342 $smarty->assign("info", msgPool::deleteInfo($dns_names));
343 $smarty->assign("is_role",false);
344 return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
345 }
346 }
347 }
350 /********************
351 Delete entry confirmed
352 ********************/
354 /* Confirmation for deletion has been passed. Acl should be deleted. */
355 if (isset($_POST['delete_acl_confirm'])){
357 /* Check permissions */
358 if(preg_match("/d/",$this->ui->get_permissions($this->dn,"acl/acl"))){
360 /* Delete request is permitted, perform LDAP action */
361 $this->acltabs= new acl($this->config, NULL,$this->dn);
362 $this->acltabs->remove_from_parent();
363 unset ($this->acltabs);
364 $this->acltabs= NULL;
365 } else {
367 /* Normally this shouldn't be reached, send some extra
368 logs to notify the administrator */
369 msg_dialog::display(_("Permission error"),msgPool::permDelete(), ERROR_DIALOG);
371 if(isset($this->ui->uid)){
372 new log("security","aclroles/".get_class($this),$this->dn,array(),"Warning: '".$this->ui->uid."' tried to trick acl role deletion.");
374 }
375 }
377 /* Remove lock file after successfull deletion */
378 $this->remove_lock();
379 }
382 /********************
383 Delete entry Canceled
384 ********************/
386 /* Delete acl canceled? */
387 if (isset($_POST['delete_acl_cancel'])){
388 $this->remove_lock();
389 }
392 /********************
393 Edit entry finished (Save)
394 ********************/
396 /* Finish acl edit is triggered by the tabulator dialog, so
397 the acl wants to save edited data. Check and save at this
398 point. */
399 if ((isset($_POST['edit_finish']) || isset($_POST['edit_apply'])) && (isset($this->acltabs->config))){
401 /* Check tabs, will feed message array */
402 $this->acltabs->save_object();
403 $message= $this->acltabs->check();
405 /* Save, or display error message? */
406 if (count($message) == 0){
408 /* Save acl data to ldap */
409 if($this->acltabs->save() == 1){
410 return;
411 }
413 if (!isset($_POST['edit_apply'])){
415 /* ACl has been saved successfully, remove lock from LDAP. */
416 if ($this->dn != "new"){
417 $this->remove_lock();
418 }
420 unset ($this->acltabs);
421 $this->acltabs= NULL;
422 session::un_set('objectinfo');
423 }
424 } else {
425 /* Ok. There seem to be errors regarding to the tab data,
426 show message and continue as usual. */
427 msg_dialog::displayChecks($message);
428 }
429 }
432 /********************
433 Display subdialog
434 ********************/
437 /* Show tab dialog if object is present */
438 if(isset($this->acltabs->config)){
440 /* Save object */
441 $this->acltabs->save_object();
442 $display= $this->acltabs->execute();
444 /* Don't show buttons if tab dialog requests this */
445 if(isset($this->acltabs)){
447 /* Skip displaying save/cancel if there is a sub dialog open */
448 if (!isset($this->acltabs->dialog) || !$this->acltabs->dialog){
449 $display.= "<p style=\"text-align:right\">\n";
450 // $display.= "<input type=submit name=\"edit_finish\" style=\"width:80px\" value=\"".msgPool::okButton()."\">\n";
451 $display.= " \n";
453 /* Skip Apply if it is a new entry */
454 #if ($this->dn != "new"){
455 # $display.= "<input type=submit name=\"edit_apply\" value=\"".msgPool::applyButton()."\">\n";
456 # $display.= " \n";
457 #}
459 // $display.= "<input type=submit name=\"edit_cancel\" value=\"".msgPool::cancelButton()."\">\n";
460 $display.= "</p>";
461 }
462 }
463 return ($display);
464 }
466 /* Check if there is a snapshot dialog open */
467 $base = $this->DivListACL->selectedBase;
468 if($str = $this->showSnapshotDialog($base,$this->get_used_snapshot_bases())){
469 return($str);
470 }
472 /* Return rendered main page */
473 /* Display dialog with system list */
474 $this->DivListACL->parent = $this;
475 $this->DivListACL->execute();
477 /* Add departments if subsearch is disabled */
478 if(!$this->DivListACL->SubSearch){
479 $this->DivListACL->AddDepartments($this->DivListACL->selectedBase,3,1);
480 }
481 $this->reload();
482 $this->DivListACL->setEntries($this->list);
483 return($this->DivListACL->Draw());
484 }
487 function reload()
488 {
489 /* Get divlist informations from filter part */
490 $Regex = $this->DivListACL -> Regex;
491 $SubSearch = $this->DivListACL -> SubSearch;
492 $base = $this->DivListACL -> selectedBase;
493 $Attrs = array("ou","cn","description","gosaAclEntry","objectClass");
494 $res = array();
495 $tmp = array(); // Will contain temporary results
496 $ldap = $this->config->get_ldap_link();
497 $Filter = "(&(objectClass=gosaACL)(gosaAclEntry=*)(|(cn=".$Regex.")(ou=".$Regex.")))";
498 $FilterRoles= "(&(objectClass=gosaRole)(|(cn=".$Regex.")(ou=".$Regex.")))";
500 /* Fetch following structures, this will be used if !$SubSearch */
501 $fetch_this = array(
502 "ME" => array("TYPE" => "cat" , "FLAGS" => GL_SIZELIMIT ,"BASE"=>""),
503 "SYSTEMS" => array("TYPE" => "search" , "FLAGS" => GL_SIZELIMIT | GL_SUBSEARCH ,"BASE"=>get_ou('systemsou')),
504 "APPS" => array("TYPE" => "search" , "FLAGS" => GL_SIZELIMIT | GL_SUBSEARCH ,"BASE"=>get_ou('applicationou')),
505 "PEOPLE" => array("TYPE" => "search" , "FLAGS" => GL_SIZELIMIT | GL_SUBSEARCH ,"BASE"=>get_people_ou()),
506 "GROUPS" => array("TYPE" => "search" , "FLAGS" => GL_SIZELIMIT | GL_SUBSEARCH ,"BASE"=>get_groups_ou()));
508 /* Subsearch ? */
509 if($SubSearch){
511 /* Get all object in this base */
512 $Flags = GL_SIZELIMIT | GL_SUBSEARCH;
513 $fetch_base = $base;
514 $tmp = get_list($Filter, "acl", $fetch_base, $Attrs, $Flags);
515 $tmp2 = get_list($FilterRoles, "acl", $fetch_base, $Attrs, $Flags);
516 foreach($tmp as $entry){
517 $res[] = $entry;
518 }
519 foreach($tmp2 as $entry){
520 $res[] = $entry;
521 }
523 }else{
525 $tmp_roles = get_list($FilterRoles, "acl", get_ou('aclroleou').$base, $Attrs,GL_SIZELIMIT);
527 foreach($tmp_roles as $entry){
528 $res[] = $entry;
529 }
531 /* Walk through all possible bases */
532 foreach($fetch_this as $type => $data){
534 /* Get requried attributes */
535 $Flags = $data['FLAGS'];
536 $fetch_base = $data['BASE'].$base;
537 $Type = $data['TYPE'];
539 /* Check if method is cat or search */
540 if($Type == "search"){
541 $tmp = get_list($Filter, "acl", $fetch_base, $Attrs, $Flags);
542 foreach($tmp as $entry){
543 $res[$entry['dn']] = $entry;
544 }
545 }else{
546 $ldap->cat($fetch_base,$Attrs);
547 $attrs = $ldap->fetch();
548 if($attrs && isset($attrs['gosaAclEntry'])){
549 $re2 = preg_replace("/\*/",".*",$Regex);
551 if(!isset($attrs['ou']) && !isset($attrs['cn'])){
552 $namingAttr= preg_replace("/^[^=]*+=([^,]*),.*$/","\\1",$fetch_base);
553 if(preg_match("/".$re2."/i",$namingAttr)){
554 $res[$attrs['dn']] = $attrs;
555 }
556 }elseif( (isset($attrs['cn'][0]) && preg_match("/".$re2."/i",$attrs['cn'][0]))
557 ||(isset($attrs['ou'][0]) && preg_match("/".$re2."/i",$attrs['ou'][0]))){
558 $res[$attrs['dn']] = $attrs;
559 }
560 }
561 }
562 }
563 }
564 $this->list = array_values($res);
565 }
568 function copyPasteHandling_from_queue($s_action,$s_entry)
569 {
570 /* Check if Copy & Paste is disabled */
571 if(!is_object($this->CopyPasteHandler)){
572 return("");
573 }
575 $ui = get_userinfo();
577 /* Add a single entry to queue */
578 if($s_action == "cut" || $s_action == "copy"){
580 /* Cleanup object queue */
581 $this->CopyPasteHandler->cleanup_queue();
582 $dn = $this->list[$s_entry]['dn'];
584 /* We can only copy & cut roles */
585 if(isset($this->list[$id]['objectClass']) && in_array("gosaRole",$this->list[$s_entry]['objectClass'])){
586 if($s_action == "copy" && preg_match("/r/",$ui->has_complete_category_acls($dn,"aclroles"))){
587 $this->CopyPasteHandler->add_to_queue($dn,$s_action,"aclroletab","ACLROLETAB","aclroles");
588 }
589 if($s_action == "cut" && preg_match("/(r.*d|d.*r)/",$ui->has_complete_category_acls($dn,"aclroles"))){
590 $this->CopyPasteHandler->add_to_queue($dn,$s_action,"aclroletab","ACLROLETAB","aclroles");
591 }
592 }
593 }
595 /* Add entries to queue */
596 if($s_action == "copy_multiple" || $s_action == "cut_multiple"){
598 /* Cleanup object queue */
599 $this->CopyPasteHandler->cleanup_queue();
601 /* Add new entries to CP queue */
602 foreach($this->list_get_selected_items() as $id){
603 $dn = $this->list[$id]['dn'];
605 if(isset($this->list[$id]['objectClass']) && in_array("gosaRole",$this->list[$id]['objectClass'])){
607 if($s_action == "copy_multiple" && preg_match("/r/",$ui->has_complete_category_acls($dn,"aclroles"))){
608 $this->CopyPasteHandler->add_to_queue($dn,"copy","aclroletab","ACLROLETAB","aclroles");
609 }
610 if($s_action == "cut_multiple" && preg_match("/(r.*d|d.*r)/",$ui->has_complete_category_acls($dn,"aclroles"))){
611 $this->CopyPasteHandler->add_to_queue($dn,"cut","aclroletab","ACLROLETAB","aclroles");
612 }
613 }
614 }
615 }
617 /* Start pasting entries */
618 if($s_action == "editPaste"){
619 $this->start_pasting_copied_objects = TRUE;
620 }
621 /* Return C&P dialog */
622 if($this->start_pasting_copied_objects && $this->CopyPasteHandler->entries_queued()){
624 /* Get dialog */
625 $this->CopyPasteHandler->SetVar("base",$this->DivListACL->selectedBase);
626 $data = $this->CopyPasteHandler->execute();
628 /* Return dialog data */
629 if(!empty($data)){
630 return($data);
631 }
632 }
634 /* Automatically disable status for pasting */
635 if(!$this->CopyPasteHandler->entries_queued()){
636 $this->start_pasting_copied_objects = FALSE;
637 }
638 return("");
639 }
642 function list_get_selected_items()
643 {
644 $ids = array();
645 foreach($_POST as $name => $value){
646 if(preg_match("/^item_selected_[0-9]*$/",$name)){
647 $id = preg_replace("/^item_selected_/","",$name);
648 $ids[$id] = $id;
649 }
650 }
651 return($ids);
652 }
655 function remove_lock()
656 {
657 /* Remove acl lock if a DN is marked as "currently edited" */
658 if (isset($this->acltabs->dn)){
659 del_lock ($this->acltabs->dn);
660 }
661 del_lock ($this->dn);
662 del_lock ($this->dns);
663 }
666 function save_object()
667 {
668 /* Handle divlist filter && department selection*/
669 if(!is_object($this->acltabs)){
670 $this->DivListACL->save_object();
671 }
672 if(is_object($this->CopyPasteHandler)){
673 $this->CopyPasteHandler->save_object();
674 }
675 }
677 /* A set of disabled and therefore overloaded functions. They are
678 not needed in this class. */
679 function remove_from_parent() { }
680 function check() { }
682 function save() {
683 echo "SAVE ACL";
684 }
686 function adapt_from_template($dn, $skip= array()) { }
687 function password_change_needed() { }
690 /* Return departments, that will be included within snapshot detection */
691 function get_used_snapshot_bases()
692 {
693 return(array());
694 }
696 }
697 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
698 ?>