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 var $acl_module = array("acl","aclroles");
41 function aclManagement(&$config, &$ui)
42 {
43 /* Save configuration for internal use */
44 $this->config = &$config;
45 $this->ui = &$ui;
47 /* Copy & Paste enabled ?*/
48 if((isset($this->config->data['MAIN']['ENABLECOPYPASTE']))&&(preg_match("/true/i",$this->config->data['MAIN']['ENABLECOPYPASTE']))){
49 $this->CopyPasteHandler = new CopyPasteHandler($this->config);
50 }
52 /* Creat dialog object */
53 $this->DivListACL = new divListACL($this->config,$this);
54 }
57 function execute()
58 {
59 /* Call parent execute */
60 plugin::execute();
62 session::set('LOCK_VARS_TO_USE',array("/^item_selected/","/^menu_action/","/^list/","/^id_/","/^list_acl_role_del/","/^list_acl_del/","/^menu_action/"));
64 $smarty = get_smarty(); // Smarty instance
65 $s_action = ""; // Contains the action to be taken
66 $s_entry = ""; // The value for s_action
68 /* Edit entry button pressed? */
69 if( isset($_GET['act']) && $_GET['act'] == "list_edit_entry" ){
70 $s_action= "edit";
71 $s_entry= validate($_GET['id']);
72 }
74 /* Edit entry button pressed? */
75 if( isset($_GET['act']) && $_GET['act'] == "list_edit_role" ){
76 $s_action= "edit_role";
77 $s_entry= validate($_GET['id']);
78 }
80 $types = array(
81 "del" =>"^list_acl_del",
82 "edit" =>"^list_acl_edit",
83 "del_role" =>"^list_acl_role_del",
84 "edit_role" =>"^list_acl_role_edit",
85 "copy" =>"^copy",
86 "cut" =>"^cut",
87 "copy_multiple"=> "^multiple_copy_objects",
88 "cut_multiple" => "^multiple_cut_objects",
89 "editPaste" =>"^editPaste",
90 "addrole" =>"^new_acl_role");
92 /* Test relevant POST values */
93 foreach($_POST as $key => $val){
95 /* Get every possible POST combination and set s_action/s_entry accordingly */
96 foreach($types as $act => $name){
98 if (preg_match("/".$name.".*/", $key)){
99 $s_action= $act;
100 $s_entry= preg_replace("/".$name."_/i", "", $key);
101 }
102 }
103 }
105 /* Remove coordinate prefix from POST, required by some browsers */
106 $s_entry= preg_replace("/_.$/", "", $s_entry);
108 /* Seperate possibly encoded tab and entry, default to tab "acl" */
109 if(preg_match("/.*-.*/", $s_entry)){
110 $s_tab= preg_replace("/^[^-]*-/i", "" ,$s_entry);
111 $s_entry= preg_replace("/-[^-]*$/i", "", $s_entry);
112 }else{
113 $s_tab= "generic";
114 }
117 /* handle C&P from layers menu */
118 if(isset($_POST['menu_action']) && preg_match("/^multiple_copy_systems/",$_POST['menu_action'])){
119 $s_action = "copy_multiple";
120 }
121 if(isset($_POST['menu_action']) && preg_match("/^multiple_cut_systems/",$_POST['menu_action'])){
122 $s_action = "cut_multiple";
123 }
124 if(isset($_POST['menu_action']) && preg_match("/^editPaste/",$_POST['menu_action'])){
125 $s_action = "editPaste";
126 }
128 /* Create options */
129 if(isset($_POST['menu_action']) && $_POST['menu_action'] == "new_acl_role"){
130 $s_action = "new";
131 }
133 /* handle remove from layers menu */
134 if(isset($_POST['menu_action']) && preg_match("/^remove_multiple/",$_POST['menu_action'])){
135 $s_action = "del_multiple";
136 }
139 /********************
140 Copy & Paste Handling ...
141 ********************/
143 /* Display the copy & paste dialog, if it is currently open */
144 $ret = $this->copyPasteHandling_from_queue($s_action,$s_entry);
145 if($ret){
146 return($ret);
147 }
150 /********************
151 * Add new Role entry
152 ********************/
154 if(($s_action == "new") && (!isset($this->acltabs->config))){
156 /* Get 'dn' from posted acl, must be unique */
157 $this->dn= "new";
159 /* Check permissions */
160 if(preg_match("/c/",$this->ui->get_permissions($this->DivListACL->selectedBase,"aclroles/aclrole"))){
162 /* Register acltabs to trigger edit dialog */
163 $this->acltabs= new aclroletab($this->config, NULL,$this->dn);
164 $this->acltabs->set_acl_base($this->DivListACL->selectedBase);
165 }else{
166 msg_dialog::display(_("Permission error"), msgPool::permCreate(), ERROR_DIALOG);
167 }
168 }
170 /********************
171 Edit existing entry
172 ********************/
174 if (($s_action=="edit" || $s_action=="edit_role") && (!isset($this->acltabs->config))){
176 /* Get 'dn' from posted acl, must be unique */
177 $this->dn= $this->list[trim($s_entry)]['dn'];
179 if(in_array("gosaRole",$this->list[trim($s_entry)]['objectClass'])){
180 $acl = "aclroles/aclrole";
181 }else{
182 $acl = "acl/acl";
183 }
185 /* Check permissions */
186 if(preg_match("/r/",$this->ui->get_permissions($this->dn,$acl))){
188 /* Check locking, save current plugin in 'back_plugin', so
189 the dialog knows where to return. */
190 if (($acl= get_lock($this->dn)) != ""){
191 return(gen_locked_message ($acl, $this->dn));
192 }
194 /* Lock the current entry, so everyone will get the above dialog */
195 add_lock ($this->dn, $this->ui->dn);
197 /* Register acltabs to trigger edit dialog */
198 if($s_action=="edit_role"){
199 $this->acltabs= new aclroletab($this->config, NULL,$this->dn);
200 $this->acltabs-> set_acl_base($this->dn);
201 }else{
202 $this->acltabs= new acltab($this->config, NULL,$this->dn);
203 $this->acltabs-> set_acl_base($this->dn);
204 }
206 /* Set ACL and move DN to the headline */
207 session::set('objectinfo',$this->dn);
208 }else{
209 msg_dialog::display(_("Permission error"), msgPool::permModify(), ERROR_DIALOG);
210 }
211 }
214 /********************
215 Edit canceled
216 ********************/
217 if(isset($_POST['delete_lock'])){
218 $this->remove_lock();
219 session::un_set('objectinfo');
220 }
222 /* Reset all relevant data, if we get a _cancel request */
223 if (isset($_POST['edit_cancel'])){
224 $this->remove_lock();
225 $this->acltabs= NULL;
226 session::un_set('objectinfo');
227 }
230 /********************
231 Delete entry requested, display confirm dialog
232 ********************/
234 /* Remove acl was requested */
235 if ($s_action=="del_role"){
237 /* Get 'dn' from posted acl */
238 $this->dn= $this->list[trim($s_entry)]['dn'];
240 /* Check permissions */
241 if(preg_match("/d/",$this->ui->get_permissions($this->dn,"aclroles/aclrole"))){
243 /* Check locking, save current plugin in 'back_plugin', so
244 the dialog knows where to return. */
245 if (($acl= get_lock($this->dn)) != ""){
246 return(gen_locked_message ($acl, $this->dn));
247 }
249 /* Lock the current entry, so nobody will edit it during deletion */
250 add_lock ($this->dn, $this->ui->dn);
251 $smarty->assign("info", msgPool::deleteInfo(@LDAP::fix($this->dn)));
252 $smarty->assign("is_role",true);
254 return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
255 } else {
257 /* Obviously the acl isn't allowed to delete. Show message and
258 clean session. */
259 msg_dialog::display(_("Permission error"), msgPool::permDelete(), ERROR_DIALOG);
260 }
261 }
264 /********************
265 Delete entry confirmed
266 ********************/
268 /* Confirmation for deletion has been passed. Acl should be deleted. */
269 if (isset($_POST['delete_acl_role_confirmed'])){
271 /* Check permissions */
272 if(preg_match("/d/",$this->ui->get_permissions($this->dn,"aclroles/aclrole"))){
274 /* Delete request is permitted, perform LDAP action */
275 $this->acltabs= new aclroletab($this->config, NULL,$this->dn);
276 $this->acltabs->delete();
277 unset ($this->acltabs);
278 $this->acltabs= NULL;
279 } else {
281 /* Normally this shouldn't be reached, send some extra
282 logs to notify the administrator */
283 msg_dialog::display(_("Permission error"), msgPool::permDelete(), ERROR_DIALOG);
285 if(isset($this->ui->uid)){
286 new log("security","aclroles/".get_class($this),$this->dn,array(),"Warning: '".$this->ui->uid."' tried to trick acl role deletion.");
287 }
288 }
290 /* Remove lock file after successfull deletion */
291 $this->remove_lock();
292 }
294 /********************
295 Delete entry requested, display confirm dialog
296 ********************/
298 /* Remove acl was requested */
299 if ($s_action=="del" || $s_action == "del_multiple"){
301 /* Get 'dn' from posted acl */
302 if($s_action == "del"){
303 $this->dns = array( $this->list[trim($s_entry)]['dn']);
304 }else{
305 $this->dns = array();
306 foreach($this->list_get_selected_items() as $id){
307 $this->dns[] = $this->list[$id]['dn'];
308 }
309 }
311 $disallowed = array();
312 foreach($this->dns as $key => $dn){
313 $acl = $this->ui->get_permissions($dn, "aclroles/aclrole");
314 if(!preg_match("/d/",$acl)){
315 unset($this->dns[$key]);
316 $disallowed[] = $dn;
317 }
318 }
320 if(count($disallowed)){
321 msg_dialog::display(_("Permission"),msgPool::permDelete($disallowed),INFO_DIALOG);
322 }
324 if(count($this->dns)){
327 /* Display lock messages */
328 if ($user= get_multiple_locks($this->dns)){
329 return(gen_locked_message($user,$this->dns));
330 }
332 $dns_names = array();
333 foreach($this->dns as $key => $dn){
335 /* Check permissions */
336 if(!preg_match("/d/",$this->ui->get_permissions($dn,"aclroles/aclrole"))){
338 /* Obviously the acl isn't allowed to delete. Show message and
339 clean session. */
340 msg_dialog::display(_("Permission error"), msgPool::permDelete(), ERROR_DIALOG);
341 unset($this->dns[$key]);
342 continue;
343 }
345 $dns_names[] = LDAP::fix($dn);
346 }
348 if(count($this->dns)){
349 add_lock($this->dns,$this->ui->dn);
350 $smarty->assign("info", msgPool::deleteInfo($dns_names));
351 $smarty->assign("is_role",false);
352 return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
353 }
354 }
355 }
358 /********************
359 Delete entry confirmed
360 ********************/
362 /* Confirmation for deletion has been passed. Acl should be deleted. */
363 if (isset($_POST['delete_acl_confirm'])){
365 /* Check permissions */
366 if(preg_match("/d/",$this->ui->get_permissions($this->dn,"aclroles/aclrole"))){
368 /* Delete request is permitted, perform LDAP action */
369 $this->acltabs= new acl($this->config, NULL,$this->dn);
370 $this->acltabs->remove_from_parent();
371 unset ($this->acltabs);
372 $this->acltabs= NULL;
373 } else {
375 /* Normally this shouldn't be reached, send some extra
376 logs to notify the administrator */
377 msg_dialog::display(_("Permission error"),msgPool::permDelete(), ERROR_DIALOG);
379 if(isset($this->ui->uid)){
380 new log("security","aclroles/".get_class($this),$this->dn,array(),"Warning: '".$this->ui->uid."' tried to trick acl role deletion.");
382 }
383 }
385 /* Remove lock file after successfull deletion */
386 $this->remove_lock();
387 }
390 /********************
391 Delete entry Canceled
392 ********************/
394 /* Delete acl canceled? */
395 if (isset($_POST['delete_acl_cancel'])){
396 $this->remove_lock();
397 }
400 /********************
401 Edit entry finished (Save)
402 ********************/
404 /* Finish acl edit is triggered by the tabulator dialog, so
405 the acl wants to save edited data. Check and save at this
406 point. */
407 if ((isset($_POST['edit_finish']) || isset($_POST['edit_apply'])) && (isset($this->acltabs->config))){
409 /* Check tabs, will feed message array */
410 $this->acltabs->save_object();
411 $message= $this->acltabs->check();
413 /* Save, or display error message? */
414 if (count($message) == 0){
416 /* Save acl data to ldap */
417 if($this->acltabs->save() == 1){
418 return;
419 }
421 if (!isset($_POST['edit_apply'])){
423 /* ACl has been saved successfully, remove lock from LDAP. */
424 if ($this->dn != "new"){
425 $this->remove_lock();
426 }
428 unset ($this->acltabs);
429 $this->acltabs= NULL;
430 session::un_set('objectinfo');
431 }
432 } else {
433 /* Ok. There seem to be errors regarding to the tab data,
434 show message and continue as usual. */
435 msg_dialog::displayChecks($message);
436 }
437 }
440 /********************
441 Display subdialog
442 ********************/
445 /* Show tab dialog if object is present */
446 if(isset($this->acltabs->config)){
448 /* Save object */
449 $this->acltabs->save_object();
450 $display= $this->acltabs->execute();
452 /* Don't show buttons if tab dialog requests this */
453 if(isset($this->acltabs)){
455 /* Skip displaying save/cancel if there is a sub dialog open */
456 if (!isset($this->acltabs->dialog) || !$this->acltabs->dialog){
457 $display.= "<p style=\"text-align:right\">\n";
458 // $display.= "<input type=submit name=\"edit_finish\" style=\"width:80px\" value=\"".msgPool::okButton()."\">\n";
459 $display.= " \n";
461 /* Skip Apply if it is a new entry */
462 #if ($this->dn != "new"){
463 # $display.= "<input type=submit name=\"edit_apply\" value=\"".msgPool::applyButton()."\">\n";
464 # $display.= " \n";
465 #}
467 // $display.= "<input type=submit name=\"edit_cancel\" value=\"".msgPool::cancelButton()."\">\n";
468 $display.= "</p>";
469 }
470 }
471 return ($display);
472 }
474 /* Check if there is a snapshot dialog open */
475 $base = $this->DivListACL->selectedBase;
476 if($str = $this->showSnapshotDialog($base,$this->get_used_snapshot_bases(),$this)){
477 return($str);
478 }
480 /* Return rendered main page */
481 /* Display dialog with system list */
482 $this->DivListACL->parent = $this;
483 $this->DivListACL->execute();
485 /* Add departments if subsearch is disabled */
486 if(!$this->DivListACL->SubSearch){
487 $this->DivListACL->AddDepartments($this->DivListACL->selectedBase,3,1);
488 }
489 $this->reload();
490 $this->DivListACL->setEntries($this->list);
491 return($this->DivListACL->Draw());
492 }
495 function reload()
496 {
497 /* Get divlist informations from filter part */
498 $Regex = $this->DivListACL -> Regex;
499 $SubSearch = $this->DivListACL -> SubSearch;
500 $base = $this->DivListACL -> selectedBase;
501 $Attrs = array("ou","cn","description","gosaAclEntry","objectClass");
502 $res = array();
503 $tmp = array(); // Will contain temporary results
504 $ldap = $this->config->get_ldap_link();
505 $Filter = "(&(objectClass=gosaACL)(gosaAclEntry=*)(|(cn=".$Regex.")(ou=".$Regex.")))";
506 $FilterRoles= "(&(objectClass=gosaRole)(|(cn=".$Regex.")(ou=".$Regex.")))";
508 /* Fetch following structures, this will be used if !$SubSearch */
509 $fetch_this = array(
510 "ME" => array("TYPE" => "cat" , "FLAGS" => GL_SIZELIMIT ,"BASE"=>""),
511 "SYSTEMS" => array("TYPE" => "search" , "FLAGS" => GL_SIZELIMIT | GL_SUBSEARCH ,"BASE"=>get_ou('systemsou')),
512 "APPS" => array("TYPE" => "search" , "FLAGS" => GL_SIZELIMIT | GL_SUBSEARCH ,"BASE"=>get_ou('applicationou')),
513 "PEOPLE" => array("TYPE" => "search" , "FLAGS" => GL_SIZELIMIT | GL_SUBSEARCH ,"BASE"=>get_people_ou()),
514 "GROUPS" => array("TYPE" => "search" , "FLAGS" => GL_SIZELIMIT | GL_SUBSEARCH ,"BASE"=>get_groups_ou()));
516 /* Append the ldap base to be able to set alcs if there were currently no acls set
517 */
518 $res[$this->config->current["BASE"]] = array("dn"=>$this->config->current["BASE"],"ou"=>".","objectClass"=>array("gosaACL"));
520 /* Subsearch ? */
521 if($SubSearch){
523 /* Get all object in this base */
524 $Flags = GL_SIZELIMIT | GL_SUBSEARCH;
525 $fetch_base = $base;
526 $tmp = get_list($Filter, "acl", $fetch_base, $Attrs, $Flags);
527 $tmp2 = get_list($FilterRoles, "acl", $fetch_base, $Attrs, $Flags);
528 foreach($tmp as $entry){
529 $res[] = $entry;
530 }
531 foreach($tmp2 as $entry){
532 $res[] = $entry;
533 }
535 }else{
537 $tmp_roles = get_list($FilterRoles, "acl", get_ou('aclroleou').$base, $Attrs,GL_SIZELIMIT);
539 foreach($tmp_roles as $entry){
540 $res[] = $entry;
541 }
543 /* Walk through all possible bases */
544 foreach($fetch_this as $type => $data){
546 /* Get requried attributes */
547 $Flags = $data['FLAGS'];
548 $fetch_base = $data['BASE'].$base;
549 $Type = $data['TYPE'];
551 /* Check if method is cat or search */
552 if($Type == "search"){
553 $tmp = get_list($Filter, "acl", $fetch_base, $Attrs, $Flags);
554 foreach($tmp as $entry){
555 $res[$entry['dn']] = $entry;
556 }
557 }else{
558 $ldap->cat($fetch_base,$Attrs);
559 $attrs = $ldap->fetch();
560 if($attrs && isset($attrs['gosaAclEntry'])){
561 $re2 = preg_replace("/\*/",".*",$Regex);
563 if(!isset($attrs['ou']) && !isset($attrs['cn'])){
564 $namingAttr= preg_replace("/^[^=]*+=([^,]*),.*$/","\\1",$fetch_base);
565 if(preg_match("/".$re2."/i",$namingAttr)){
566 $res[$attrs['dn']] = $attrs;
567 }
568 }elseif( (isset($attrs['cn'][0]) && preg_match("/".$re2."/i",$attrs['cn'][0]))
569 ||(isset($attrs['ou'][0]) && preg_match("/".$re2."/i",$attrs['ou'][0]))){
570 $res[$attrs['dn']] = $attrs;
571 }
572 }
573 }
574 }
575 }
576 $this->list = array_values($res);
577 }
580 function copyPasteHandling_from_queue($s_action,$s_entry)
581 {
582 /* Check if Copy & Paste is disabled */
583 if(!is_object($this->CopyPasteHandler)){
584 return("");
585 }
587 $ui = get_userinfo();
589 /* Add a single entry to queue */
590 if($s_action == "cut" || $s_action == "copy"){
592 /* Cleanup object queue */
593 $this->CopyPasteHandler->cleanup_queue();
594 $dn = $this->list[$s_entry]['dn'];
596 /* We can only copy & cut roles */
597 if(isset($this->list[$s_entry]['objectClass']) && in_array("gosaRole",$this->list[$s_entry]['objectClass'])){
598 if($s_action == "copy" && $ui->is_copyable($dn,"aclroles","aclrole")){
599 $this->CopyPasteHandler->add_to_queue($dn,$s_action,"aclroletab","ACLROLETAB","aclroles");
600 }
601 if($s_action == "cut" && $ui->is_cutable($dn,"aclroles","aclrole")){
602 $this->CopyPasteHandler->add_to_queue($dn,$s_action,"aclroletab","ACLROLETAB","aclroles");
603 }
604 }
605 }
607 /* Add entries to queue */
608 if($s_action == "copy_multiple" || $s_action == "cut_multiple"){
610 /* Cleanup object queue */
611 $this->CopyPasteHandler->cleanup_queue();
613 /* Add new entries to CP queue */
614 foreach($this->list_get_selected_items() as $id){
615 $dn = $this->list[$id]['dn'];
617 if(isset($this->list[$id]['objectClass']) && in_array("gosaRole",$this->list[$id]['objectClass'])){
619 if($s_action == "copy_multiple" && $ui->is_copyable($dn,"aclroles","aclrole")){
620 $this->CopyPasteHandler->add_to_queue($dn,"copy","aclroletab","ACLROLETAB","aclroles");
621 }
622 if($s_action == "cut_multiple" && $ui->is_cutable($dn,"aclroles","aclrole")){
623 $this->CopyPasteHandler->add_to_queue($dn,"cut","aclroletab","ACLROLETAB","aclroles");
624 }
625 }
626 }
627 }
629 /* Start pasting entries */
630 if($s_action == "editPaste"){
631 $this->start_pasting_copied_objects = TRUE;
632 }
633 /* Return C&P dialog */
634 if($this->start_pasting_copied_objects && $this->CopyPasteHandler->entries_queued()){
636 /* Get dialog */
637 $this->CopyPasteHandler->SetVar("base",$this->DivListACL->selectedBase);
638 $data = $this->CopyPasteHandler->execute();
640 /* Return dialog data */
641 if(!empty($data)){
642 return($data);
643 }
644 }
646 /* Automatically disable status for pasting */
647 if(!$this->CopyPasteHandler->entries_queued()){
648 $this->start_pasting_copied_objects = FALSE;
649 }
650 return("");
651 }
654 function list_get_selected_items()
655 {
656 $ids = array();
657 foreach($_POST as $name => $value){
658 if(preg_match("/^item_selected_[0-9]*$/",$name)){
659 $id = preg_replace("/^item_selected_/","",$name);
660 $ids[$id] = $id;
661 }
662 }
663 return($ids);
664 }
667 function remove_lock()
668 {
669 /* Remove acl lock if a DN is marked as "currently edited" */
670 if (isset($this->acltabs->dn)){
671 del_lock ($this->acltabs->dn);
672 }
673 del_lock ($this->dn);
674 del_lock ($this->dns);
675 }
678 function save_object()
679 {
680 /* Handle divlist filter && department selection*/
681 if(!is_object($this->acltabs)){
682 $this->DivListACL->save_object();
683 }
684 if(is_object($this->CopyPasteHandler)){
685 $this->CopyPasteHandler->save_object();
686 }
687 }
689 /* A set of disabled and therefore overloaded functions. They are
690 not needed in this class. */
691 function remove_from_parent() { }
692 function check() { }
694 function save() {
695 echo "SAVE ACL";
696 }
698 function adapt_from_template($dn, $skip= array()) { }
699 function password_change_needed() { }
702 /* Return departments, that will be included within snapshot detection */
703 function get_used_snapshot_bases()
704 {
705 return(array());
706 }
708 }
709 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
710 ?>