Code

Added schema requrements
[gosa.git] / gosa-plugins / mit-krb5 / admin / systems / services / kerberos / class_goKrbServer.inc
1 <?php
2 /*
3    This code is part of GOsa (https://gosa.gonicus.de)
4    Copyright (C) 2008 Fabian Hickert
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  */
22 /*! brief   The kerberos service class 
23  */
24 class goKrbServer extends goService{
25         
26   var $cli_summary      = "This pluign is used within the ServerService Pluign \nand indicates that this server supports NTP service.";
27   var $cli_description  = "Some longer text\nfor help";
28   var $cli_parameters   = array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
30   /* This plugin only writes its objectClass */
31   var $objectclasses    = array("goKrbServer");
32   var $attributes       = array("goKrbRealm");
33   var $StatusFlag       = "goKrbServerStatus";
34  
35   /* This class can't be assigned twice so it conflicts with itsself */
36   var $conflicts        = array("goKrbServer");
38   var $DisplayName      = "";
39   var $dn               = NULL;
40   var $goKrbServerStatus= "";
41   var $cn               = "";
42   var $goKrbRealm       = "";
43   var $view_logged      = FALSE;
45   var $policies         = array();
46   var $id               = -1;
47   var $macAddress       = "" ;
50   /*! \brief Initializes the kerberos service 
51       @param  Object  GOsa configuration object.
52       @param  String  The object dn we are currently editing.
53       @param  Object  The parent object.
54    */ 
55   public function goKrbServer(&$config,$dn,$parent)
56   {
57     goService::goService($config,$dn);
58     $this->DisplayName = _("Kerberos service");
59     $this->parent = $parent;
61     /* Detect macAddress of this device 
62      */
63     $this->macAddress = "";
64     if(isset($this->parent->parent->netConfigDNS->macAddress)){
65       $this->macAddress = &$this->parent->parent->netConfigDNS->macAddress;
66     }
68     /* Get configured policies 
69      */ 
70     if(!empty($this->macAddress) && $this->is_account){
71       $o = new gosaSupportDaemon();
72       $tmp = $o->krb5_list_policies($this->macAddress);
73       if($o->is_error()){
74         msg_dialog::display(_("Service infrastructure"),msgPool::siError($o->get_error()),ERROR_DIALOG);
75       }else{
76         $this->policies = array();
77         foreach($tmp as $policy){
78           $this->policies[] = array("NAME" => $policy,"STATUS" => "LOADED","DATA" => array());
79         }
80       }
81     }
83     // Prepare lists
84     $this->principleList = new sortableListing();
85     $this->principleList->setDeleteable(true);
86     $this->principleList->setInstantDelete(false);
87     $this->principleList->setEditable(true);
88     $this->principleList->setWidth("100%");
89     $this->principleList->setHeight("100px");
90     $this->principleList->setHeader(array(_("Name")));
91     $this->principleList->setColspecs(array('*','40px'));
92     $this->principleList->setDefaultSortColumn(0);
94   }
97   /*! \brief  Create HTML output
98       @return String HMTL output.
99    */
100   public function execute()
101   { 
102     $smarty = get_smarty(); 
103     $smarty->assign("MIT_KRB", class_available("passwordMethodMIT"));
105     if(!$this->view_logged){
106       $this->view_logged = TRUE;
107       new log("view","server/".get_class($this),$this->dn);
108     }
110     /*******  
111       Display sub dialogs
112      *******/
114     /*  CANCEL Policy dialog 
115      */
116     if(isset($_POST['cancel_policy'])){
117       $this->dialog = NULL;
118     }
119     
120     /*  SAVE Policy dialog 
121      */
122     if($this->dialog instanceof krb5_policy && isset($_POST['save_policy'])){
123       $this->dialog->save_object();
124       $msgs = $this->dialog->check();
125       if(count($msgs)){
126         msg_dialog::displayChecks($msgs);
127       }else{
128         $this->AddPolicy($this->id,$this->dialog->save());
129         $this->dialog = NULL;
130       }
131     }
133     /* DISPLAY policy dialog
134      */
135     if($this->dialog instanceof krb5_policy){
136       $this->dialog->save_object();
137       return($this->dialog->execute());
138     } 
140     /*******
141       Create HTML output for this plugin
142      *******/  
144     $tmp = $this->plinfo();
145     foreach($tmp['plProvidedAcls'] as $name => $translation){
146       $smarty->assign($name."ACL",$this->getacl($name));
147     }
149     foreach($this->attributes as $attr){
150       $smarty->assign($attr,$this->$attr);
151     }
153     $this->principleList->setAcl($this->getAcl('goKrbPolicy'));
154     $data = $lData = array();
155     foreach($this->policies as $key => $policy){
156       if($policy['STATUS'] == "REMOVED") continue;
157       $data[$key] = $policy;
158       $lData[$key]= array('data' => array($policy['NAME']));
159     }
160     $this->principleList->setListData($data,$lData);
161     $this->principleList->update();
162     $smarty->assign("list",$this->principleList->render());
163     return($smarty->fetch(get_template_path("goKrbServer.tpl",TRUE,dirname(__FILE__))));
164   }
167   /* \brief  Return serice informations, which will be shown in the service overview.
168      @return  Array  Some service information.
169    */
170   public function getListEntry()
171   {
172     $fields               = goService::getListEntry();
173     $fields['Message']    = _("Kerberos service (kadmin access informations)");
174     #$fields['AllowEdit']  = true;
175     return($fields);
176   }
178   
179   /*! \brief Checks if all given values are valid 
180       @return Array   An array containing all error messages.
181    */
182   public function check()
183   { 
184     $message = plugin::check();
185     if (empty($this->goKrbRealm)){
186       $message[]= msgPool::required(_("Realm"));
187     }
188     if(count($this->policies) && (empty($this->macAddress) || !tests::is_mac($this->macAddress))){
189       $message[] = _("Cannot apply policy changes! The defined kerberos server has no mac address.");
190     }
191     return($message);
192   }
193   
195   /*! \brief Adds or updated a given policy.
196       @param  Integer ID  The policy to update, or -1 if it is a new one.
197       @param  Array  The policy settings.
198    */
199   public function AddPolicy($id,$policy)
200   {
201     if($id != -1 && $this->policies[$id]['NAME'] == $policy['NAME']){
203       /* Policy was edited 
204        */
205       if($this->policies[$id]['STATUS'] == "LOADED"){
206         $policy['STATUS'] = "EDITED";
207       }else{
208         $policy['STATUS'] = $this->policies[$id]['STATUS'];
209       }
210       $this->policies[$this->id] = $policy;
212     }elseif($id != -1 && $this->policies[$id]['NAME'] != $policy['NAME']){
214       /* Policy was renamed, remove old and add new policy  
215        */
216       $this->RemovePolicy($id);
217       $this->AddPolicy(-1,$policy);
218     }else{
220       /* Policy was added 
221        */
222       $name = $policy['NAME'];
223     
224       /* Check if there is already a policy with this name 
225           which was removed before
226        */
227       foreach($this->policies as $pid => $entry){
228         if($entry['NAME'] == $name && $entry['STATUS'] == "REMOVED"){
229           $id = $pid;
230           break;
231         }
232       }
233        
234       /* Update existing policy or create new one
235        */ 
236       if(isset($this->policies[$id])){
237         $policy['STATUS'] = "EDITED";
238         $this->policies[$id] = $policy;
239       }else{
240         $policy['STATUS'] = "ADDED";
241         $this->policies[] = $policy;
242       }
243     }
244   }
247   /*! \brief  Returns all used policy names.
248       @return Array A list of used policy names.
249    */
250   public function getPolicyNames()
251   {
252     $ret = array();
253     foreach($this->policies as $policy){
254       if($policy['STATUS'] == "REMOVED") continue;
255       $ret[] = $policy['NAME'];
256     }
257     return($ret);
258   }
260  
261   /*! \brief  Marks a policy as remvoed 
262       @param  Integer the Id of the policy to remove 
263    */ 
264   public function RemovePolicy($id)
265   {
266     /* Load policy information, if not done before 
267      */
268     if(!isset($this->policies[$id]))  return;
269     if($this->policies[$id]['STATUS'] == "LOADED" && empty($this->policies[$id]['DATA'])){
270       $o = new gosaSupportDaemon();
271       $this->policies[$id]['DATA'] = $o->krb5_get_policy($this->macAddress,$this->policies[$id]['NAME']);
272     }
273     if($this->policies[$id]['DATA']['POLICY_REFCNT']){
274       msg_dialog::display(_("Remove"),msgPool::stillInUse(_("Policy")),ERROR_DIALOG) ;
275     }else{
276       if($this->policies[$id]['STATUS'] == "ADDED"){
277         unset($this->policies[$id]);
278       }else{
279         $this->policies[$id]['STATUS'] = "REMOVED";
280       }
281     } 
282   }  
283   
285   /*! \brief  Save POSTed html variables
286    */
287   public function save_object()
288   {
289     if(isset($_POST['goKrbServerPosted'])){
290       plugin::save_object();
292       /* Add new Policy requested 
293        */
294       if(isset($_POST['policy_add']) && class_available("krb5_policy")){
295         $this->dialog = new krb5_policy($this->config,array(),$this);
296         $this->id = -1;
297       }
300       $this->principleList->save_object();
301       $action = $this->principleList->getAction();
302       if($action['action'] == 'delete'){
303           $id = $this->principleList->getKey($action['targets'][0]);
304           $this->RemovePolicy($id);
305       }
306       if($action['action'] == 'edit'){
307           $id = $this->principleList->getKey($action['targets'][0]);
308           if(isset($this->policies[$id])){
310               /* Load policy information, if not done before 
311                */ 
312               if($this->policies[$id]['STATUS'] == "LOADED" && empty($this->policies[$id]['DATA'])){
313                   $o = new gosaSupportDaemon();
314                   $this->policies[$id]['DATA'] = $o->krb5_get_policy($this->macAddress,$this->policies[$id]['NAME']);
315               }
317               /* Open dialog */
318               $this->id = $id;
319               $this->dialog = new krb5_policy($this->config,$this->policies[$id], $this);
320           }
321       }
322     }
323   } 
326   /*! \brief Save changes to ldap
327    */
328   public function save()
329   {
330     goService::save();
332     /* Send policy changes back to the si daemon 
333      */
334     $actions = array("del" => array(),"add_edit" => array());
335     foreach($this->policies as $policy){
337       /* Unset not used vars */
338       if(isset($policy['DATA']['MASK']))          unset($policy['DATA']['MASK']);
339       if(isset($policy['DATA']['POLICY_REFCNT'])) unset($policy['DATA']['POLICY_REFCNT']);
341       switch($policy['STATUS']){
342         case "REMOVED" : $actions['del'] [] = $policy;break; 
343         case "ADDED"   : 
344         case "EDITED"  : $actions['add_edit'] [] = $policy;break; 
345       }
346     }
348     $o = new gosaSupportDaemon();
349     $policies = $o->krb5_list_policies($this->macAddress);
351     /* Send remove policy event  
352      */
353     foreach($actions['del'] as $policy){
354       if(!in_array($policy['NAME'],$policies)) continue;
355       if(!$o->krb5_del_policy($this->macAddress,$policy['NAME'])){
356         msg_dialog::display(_("Service infrastructure"),msgPool::siError($o->get_error()),ERROR_DIALOG);
357       }
358     }
359     
360     /* Send add new/edit policy event
361      */
362     foreach($actions['add_edit'] as $policy){
363       if(in_array($policy['NAME'],$policies)){
364         if(!$o->krb5_set_policy($this->macAddress,$policy['NAME'],$policy['DATA'])){
365           msg_dialog::display(_("Service infrastructure"),msgPool::siError($o->get_error()),ERROR_DIALOG);
366         }
367       }else{
368         if(!$o->krb5_add_policy($this->macAddress,$policy['NAME'],$policy['DATA'])){
369           msg_dialog::display(_("Service infrastructure"),msgPool::siError($o->get_error()),ERROR_DIALOG);
370         }
371       }
372     }
373   }
376   /*! \brief Return plugin informations for acl handling 
377       @return Array   ACL infos.
378    */
379   static function plInfo()
380   {
381     return (array(
382           "plShortName"   => _("Kerberos"),
383           "plDescription" => _("Kerberos access information")." ("._("Services").")",
384           "plSelfModify"  => FALSE,
385           "plDepends"     => array(),
386           "plPriority"    => 95,
387           "plSection"     => array("administration"),
388           "plCategory"    => array("server"),
389           "plRequirements"=> array('ldapSchema' => array('goKrbServer' => '>=2.7')),
391           "plProvidedAcls"=> array(
392             "goKrbPolicy"   => _("Policies"),
393             "goKrbRealm"    => _("Realm")) 
394           ));
395   }
397 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
398 ?>