Code

Added kerberos service key support to the service plugin.
[gosa.git] / gosa-plugins / systems / admin / systems / class_serverService.inc
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 define("START_SERVICE",1);
24 define("STOP_SERVICE",2);
25 define("RESTART_SERVICE",3);
27 define("SERVICE_STOPPED",_("Stop"));
28 define("SERVICE_STARTED",_("Start"));
29 define("SERVICE_RESTARTED",_("Restart"));
31 define("ALL_SERVICES",100);
33 class ServerService extends plugin
34 {
35   /* attribute list for save action */
36   var $ignore_account   = TRUE;
37   var $attributes       = array();
38   var $objectclasses    = array();
40   var $divList          = NULL;
42   var $plugins          = array();
43   var $pluign_names     = array();
45   var $current          = "";
46   var $backup           = NULL;
47   var $acl              ;
48   var $cn;
49   var $parent           ;
51   function ServerService (&$config, $dn,$parent)
52   {
53     plugin::plugin($config);
54     $this->parent = $parent;
55     $this->dn= $dn;
57     /* Adapt parent attributes */
58     if(isset($this->parent->attrs)){
59       $this->attrs = $this->parent->attrs;
60     }
62     foreach ($config->data['TABS']['SERVERSERVICE'] as $plug){
64       if(class_available($plug['CLASS'])){
66         $name= $plug['CLASS'];
67         $this->plugin_names[]= $name;
68         $this->plugins[$name]= new $name($config, $dn, $this);
70         /* Initialize kerberos key support */ 
71         if(isset($this->plugins[$name]->krb_service_prefix) && !empty($this->plugins[$name]->krb_service_prefix)){
72           $this->plugins[$name]->krb_host_key = new krb_host_keys($this->config,$this->parent);
73           $this->plugins[$name]->krb_host_key->ServiceKey(TRUE);
74         }
76         /* Capture all service objectClases, necessary for acl handling */ 
77         if(isset($this->plugins[$name]->objectclasses)){
78           foreach($this->plugins[$name]->objectclasses as $oc){
79             $this->objectclasses[] = $oc;
80           }
81         }
82       }else{
83         trigger_error("Service class missing: ".$plug['CLASS']);
84       }
85     }
86     $this->divList = new divListSystemService($config,$this);
87         }
89   function set_acl_base($base)
90   {
91     plugin::set_acl_base($base);
92     foreach($this->plugins as $name => $obj){
93       $this->plugins[$name]->set_acl_base($base);
94     }
95   }
97   function set_acl_category($category)
98   {
99     plugin::set_acl_category($category);
100     foreach($this->plugins as $name => $obj){
101       $this->plugins[$name]->set_acl_category($category);
102     }
103   }
105   function execute()
106   {
107     /* Variable initialisation */  
108     $s_action = "";
109     $s_entry  = "";
111     /* Walk through posts and check if there are some introductions for us */
112     $PossiblePosts = array("addNewService"  => "", 
113                            "startServices"  => "",
114                            "stopServices"   => "",
115                            "restartServices"  => "",
116                            "removeServices" => "",
118                            "StartSingleService"  => "/^StartSingleService_(.*)_[xy]$/",
119                            "StopSingleService"   => "/^StopSingleService_(.*)_[xy]$/",
120                            "RestartSingleService"  => "/^RestartSingleService_(.*)_[xy]$/",
121                            "RemoveSingleService" => "/^RemoveSingleService_(.*)_[xy]$/",
122                            "EditSingleService"   => "/^EditSingleService_(.*)_[xy]$/");
124   
125     $once = true;
126     foreach($_POST as $name => $value){
127       foreach($PossiblePosts as $pregCheck => $idPreg) {
128         if(preg_match("/^".$pregCheck."/",$name) && $once){
129           $once     = false;
130           $s_action = $pregCheck;
131           
132           if(!empty($idPreg)){
133             $s_entry = preg_replace($idPreg,"\\1",$name);
134           }
135         }
136       }
137     }
140     /* Handle state changes for services */
141     $map =  array(    "startServices"         => array("type" => START_SERVICE ,   "service" => ALL_SERVICES),
142                       "stopServices"          => array("type" => STOP_SERVICE  ,   "service" => ALL_SERVICES),
143                       "restartServices"       => array("type" => RESTART_SERVICE , "service" => ALL_SERVICES),                 
144                       "StartSingleService"    => array("type" => START_SERVICE ,   "service" => $s_entry),
145                       "StopSingleService"     => array("type" => STOP_SERVICE ,    "service" => $s_entry),
146                       "RestartSingleService"  => array("type" => RESTART_SERVICE , "service" => $s_entry));
147     if(isset($map[$s_action])){
148       $type     = $map[$s_action]['type'];
149       $service  = $map[$s_action]['service'];
150       $this->ServiceStatusUpdate($type,$service);
151     }
153     /* Handle actions linked via href */
154     if(isset($_GET['act']) && $_GET['act'] == "open" && isset($_GET['id'])){
155       $id = $_GET['id'];
156       if(isset($this->plugins[$id])){
157         $s_entry = $id;
158         $s_action = "EditSingleService";
159       } 
160     }
162     /* Open service add dialog */
163     if($s_action == "addNewService"){
164       $this->dialog = new ServiceAddDialog($this->config,$this->dn,$this);
165     }
168     /* Remove service */
169     if($s_action == "RemoveSingleService"){
171       /* Create resetted obj */
172       $new_obj = new $s_entry($this->config,$this->dn, $this);
173       $new_obj -> set_acl_base($this->acl_base);
174       $new_obj -> set_acl_category(preg_replace("/\/$/","",$this->acl_category));
175       $tmp     = $new_obj->getListEntry();
177       if($tmp['AllowRemove']){
179         /* Check if we are allowed to remove this service
180          */
181         $str = $this->plugins[$s_entry]->allow_remove();
183         if(empty($str)){
184           $this->plugins[$s_entry] = $new_obj;
185           $this->plugins[$s_entry]->is_account = false;
186         }else{
187           msg_dialog::display(_("Error"), $str, ERROR_DIALOG);
188         }
189       }
190     }
193     /* Edit a service and make a backup from all attributes, 
194        to be able to restore old values after aborting dialog */ 
195     if($s_action == "EditSingleService"){
196       $this->backup   = get_object_vars($this->plugins[$s_entry]);
197       $this->dialog   = $this->plugins[$s_entry];
198       $this->current  = $s_entry;
199     }
201    
202     /* Abort service add */
203     if(isset($_POST['CancelServiceAdd'])){
204       $this->dialog   = FALSE;
205       $this->backup   = NULL;
206       $this->current  = "";
207     }
209  
210     /* Abort dialog 
211        Restore vars with values before editing */
212     if(isset($_POST['CancelService']) && !empty($this->current)){
213       if($this->backup == NULL){
214         $this->plugins[$this->current] = new $this->current($this->config,$this->dn);
215         $this->plugins[$this->current]-> set_acl_base($this->acl_base);
216         $this->plugins[$this->current]-> set_acl_category(preg_replace("/\/$/","",$this->acl_category));
218       }else{
219         foreach($this->backup as $name => $value){
220           $this->plugins[$this->current]->$name = $value;
221         }
222       }
223       $this->dialog   = FALSE;
224       $this->backup   = NULL;
225       $this->current  = ""; 
226     }
227   
229     /* Abort dialog */
230     if(isset($_POST['SaveService']) && is_object($this->dialog)){
231 #      $this->dialog->save_object();
232       $msgs = $this->dialog->check();
233       if(count($msgs)){
234         foreach($msgs as $msg){
235           msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
236         }
237       }else{
238         $this->plugins[$this->current] = $this->dialog;
239         $tmp  = get_object_vars($this->dialog);
240         foreach($tmp as $name => $value){
241           $this->plugins[$this->current]->$name = $value;
242         }
243         $this->current = "";
244         $this->dialog = FALSE;
245         $this->backup = NULL;
246       }
247     }
250     /* Abort dialog */
251     if((isset($_POST['SaveServiceAdd'])) && (!empty($_POST['ServiceName']))){
252       $serv = $_POST['ServiceName'];    
253       $this->plugins[$serv]->is_account = true;
254       $this->dialog  = $this->plugins[$serv];
255       $this->current = $serv;
256     }
259     /* There is currently a subdialog open, display this dialog */
260     if(is_object($this->dialog)){
261 #      $this->dialog->save_object();
262       $add ="";
263       if(isset($this->dialog->krb_host_key)){
264         $add = $this->dialog->krb_host_key->execute();
265       }
266       return($add.$this->dialog->execute());
267     }
270     /* Dispaly services overview */
271     $this->divList->execute();
272     $list = array();
274     foreach($this->plugins as $name => $obj){
275       if($obj->is_account){
276         $list[$name] = $this->plugins[$name]->getListEntry(); 
277       }
278     }
279     $this->divList -> setEntries($list);
280     return("<table style='width:100%;'><tr><td>".$this->divList->Draw()."</td></tr></table>");
281   }
284   /* Get all used services 
285       CLASSNAME => _($this->plugins[*]->DisplayName);   */
286   function getAllUsedServices()
287   {
288     $ret = array();
289     foreach($this->plugins as $name => $obj){
290       if($obj->is_account){
291         if(isset($obj->DisplayName)){
292           $ret[$name] = $obj->DisplayName;
293         }else{
294           $ret[$name] = $name;
295         }
296       }
297     }
298     return($ret);
299   }
302   /* Get all unused services 
303       CLASSNAME => _($this->plugins[*]->DisplayName);  */
304   function getAllUnusedServices()
305   {
306     $tmp = $this->getAllUsedServices();
307     $pool_of_ocs =array();
308     foreach($tmp as $name => $value){
309       if(isset($this->plugins[$name]->conflicts)){
310         $pool_of_ocs[]= get_class($this->plugins[$name]);
311       }
312     }
313    
314     $ret = array();
315     foreach($this->plugins as $name => $obj){
317       /* Skip all pluigns that will lead into conflicts */
318       $skip = false;
319       if(isset($obj->conflicts)){
320         foreach($obj->conflicts as $oc){
321           if(in_array_ics($oc,$pool_of_ocs)){
322             $skip = true;
323           }
324         }
325       }
327       /* Only show createable services */
328       if(!$obj->acl_is_createable()){
329         $skip = true;
330       }
332       if(!$skip){
333         if(isset($obj->DisplayName)){
334           $ret[$name] = $obj->DisplayName;
335         }else{
336           $ret[$name] = $name;
337         }
338       }
339     }
340     return($ret);
341   }
342  
343  
344   /* This function sets the status var for each used 
345      service && calls an external hook if specified in gosa.conf*/
346   function ServiceStatusUpdate($method , $service)
347   {
348     /* Skip if this is a new server */
349     if($this->dn == "new"){
350       msg_dialog::display(_("Information"), _("Cannot update service status until it has been saved!"), INFO_DIALOG);
351       return;
352     }
354     $action = "";
355     if($method == START_SERVICE){
356       $action = SERVICE_STARTED;
357     }elseif($method== STOP_SERVICE){
358       $action = SERVICE_STOPPED;
359     }elseif($method == RESTART_SERVICE){
360       $action = SERVICE_RESTARTED;
361     }else{
362       msg_dialog::display(_("Error"), _("Cannot update service status!"), ERROR_DIALOG);
363       return;
364     }
365     
366     $caseVars = array("cn","dn");
367     if($service == ALL_SERVICES){
368       foreach($this->plugins as $name => $obj){
369         foreach($caseVars as $var){
370           if(isset($this->$var)){
371             $this->plugins[$name]->$var = $this->$var;  
372           }
373         }
375         /* check if services can be restarted */
376         $map =array(SERVICE_STARTED=> "AllowStart" ,
377                     SERVICE_STOPPED => "AllowStop",
378                     SERVICE_RESTARTED => "AllowRestart");
380         /* get plugins informations, restart/start/stop actions allowed ?*/
381         $tmp = $this->plugins[$name]->getListEntry();
383         /* Check if given action is allowed for this service */
384         if($tmp[$map[$action]]){
385           if($this->plugins[$name]->initially_was_account && $this->plugins[$name]->is_account){
386             $this->plugins[$name]->setStatus($action);
387           }
388         }
389       }
390     }else{
391       foreach($caseVars as $var){
392         if(isset($this->$var)){
393           $this->plugins[$service]->$var = $this->$var;  
394         }
395       }
396       if($this->plugins[$service]->is_account){
397         $this->plugins[$service]->setStatus($action);
398       }
399     }
400   }
403   function check()
404   {
405     $message = plugin::check();
406     return $message;
407   }
410   function save_object()
411   {
412     foreach($this->plugins as $name => $obj){
413       if($obj->is_account){
414         $this->plugins[$name]->save_object();
415       }
416     }
417   }
420   function remove_from_parent()
421   {
422     $caseVars = array("cn","dn");
423     foreach($this->plugins as $name => $obj){
424       foreach($caseVars as $var){
425         if(isset($this->$var)){
426           $this->plugins[$name]->$var = $this->$var;  
427         }
428       }
429       if($this->plugins[$name]->initially_was_account){
430         $this->plugins[$name]->remove_from_parent();
431       }
432     }
433   }
436   function save()
437   {
438     $caseVars = array("cn","dn");
439     foreach($this->plugins as $name => $obj){
441       foreach($caseVars as $var){
442         if(isset($this->$var)){
443           $this->plugins[$name]->$var = $this->$var;
444         }
445       }
447       if($this->plugins[$name]->is_account){
448         $this->plugins[$name]->save();
449       }else{
450         if($this->plugins[$name]->initially_was_account){
451           $this->plugins[$name]->remove_from_parent();
452         }
453       }
454     }
455   }
457   
458   function PrepareForCopyPaste($source)
459   {
460     plugin::PrepareForCopyPaste($source);
462     foreach($this->plugins as $name => $plugin){
463       $this->plugins[$name]->PrepareForCopyPaste($source);
464     }
465   }
468   /* Check if all plugins allow a remove ..  */
469   function allow_remove()
470   {
471     foreach($this->plugins as $name => $obj){
472       $str = $obj->allow_remove();
473       if(!empty($str)){
474         return($str);
475       }
476     }
477   }
478   
480 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
481 ?>