Code

Updated server generic.
[gosa.git] / gosa-plugins / systems / admin / systems / class_servGeneric.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 class servgeneric extends plugin
24 {
25   /* Generic terminal attributes */
26   var $ignore_account= TRUE;
27   var $interfaces= array();
29   /* Needed values and lists */
30   var $base= "";
31   var $cn= "";
32   var $l= "";
33   var $description= "";
34   var $orig_dn= "";
35   var $orig_cn= "";
36   var $orig_base= "";
38   /* attribute list for save action */
39   var $attributes= array("cn", "description","gotoMode");
40   var $objectclasses= array("top", "GOhard", "goServer");
42   var $gotoMode= "locked";
44   var $netConfigDNS;
45   var $modes = array();
46   var $ui   ;
47   var $validActions   = array("reboot" => "", "update" => "", "localboot" => "", "reinstall" => "", "rescan" => "",
48                             "wakeup" => "", "memcheck" => "", "sysinfo" => "");
50   var $fai_activated  =FALSE;
51   var $view_logged = FALSE;
53   var $currently_installing = FALSE;
54   var $currently_installing_warned = FALSE;
56   var $kerberos_key_service = NULL;
58   var $mapActions   = array("reboot"          => "",
59                             "instant_update"  => "softupdate",
60                             "localboot"       => "localboot",
61                             "update"          => "sceduledupdate",
62                             "reinstall"       => "install",
63                             "rescan"          => "",
64                             "wakeup"            => "",
65                             "memcheck"        => "memcheck",
66                             "sysinfo"         => "sysinfo");
69   function servgeneric (&$config, $dn= NULL, $parent= NULL)
70   {
71     /* Check if FAI is activated */
72     $tmp = $config->search("faiManagement", "CLASS",array('menu','tabs'));
73     
74     if(!empty($tmp)){
75       $this->fai_activated = TRUE;
76     }
78     plugin::plugin ($config, $dn, $parent);
80     /* Initialize kerberos host key plugin */
81     if(class_available("krbHostKeys")){
82       $this->kerberos_key_service = new krbHostKeys($this->config,$this);
83     }
85     $this->ui = get_userinfo();
86     $this->modes["active"]= _("Activated");
87     $this->modes["locked"]= _("Locked");
89     /* Set base */
90     if ($this->dn == "new"){
91       $ui= get_userinfo();
92       $this->base= dn2base($ui->dn);
93       $this->cn= "";
94     } else {
95       $this->base= preg_replace ("/^[^,]+,".preg_quote(get_ou("serverRDN"), '/')."/", "", $this->dn);
96     }
97     $this->netConfigDNS = new termDNS($this->config,$this,$this->objectclasses);
98     $this->netConfigDNS->set_acl_category("server");
99     $this->netConfigDNS->set_acl_base($this->base);
100     $this->netConfigDNS->MACisMust =TRUE;
102     /* Check if this host is currently in installation process*/
103     if($this->dn != "new" && class_available("gosaSupportDaemon") && class_available("DaemonEvent")){
104       $o = new gosaSupportDaemon();
105       $e_types = DaemonEvent::get_event_types(USER_EVENT | SYSTEM_EVENT | HIDDEN_EVENT);
106       $evts = $o->get_entries_by_mac(array($this->netConfigDNS->macAddress));
107       foreach($evts as $evt){
108         if(isset($e_types['QUEUED'][$evt['HEADERTAG']]) && $evt['STATUS'] == "processing" && 
109             $e_types['QUEUED'][$evt['HEADERTAG']] == "DaemonEvent_reinstall"){
110           $this->currently_installing =TRUE;
111         }
112       }
113     }
114        
115     /* Save dn for later references */
116     $this->orig_dn   = $this->dn;
117     $this->orig_cn   = $this->cn;
118     $this->orig_base = $this->base;
119   }
122   function set_acl_base($base)
123   {
124     plugin::set_acl_base($base);
125     $this->netConfigDNS->set_acl_base($base);
126   }
129   function set_acl_category($cat)
130   {
131     plugin::set_acl_category($cat);
132     $this->netConfigDNS->set_acl_category($cat);
133   }
136   function execute()
137   {
138     /* Call parent execute */
139     plugin::execute();
141     if($this->is_account && !$this->view_logged){
142       $this->view_logged = TRUE;
143       new log("view","server/".get_class($this),$this->dn);
144     }
146     /* Do we represent a valid server? */
147     if (!$this->is_account && $this->parent === NULL){
148       $display= "<img alt=\"\" src=\"images/small-error.png\" align=middle>&nbsp;<b>".
149         msgPool::noValidExtension(_("server"))."</b>"; 
150       return($display);
151     }
153     if ((isset($_POST['action'])) && ($this->acl_is_writeable("FAIstate")) && isset($this->mapActions[$_POST['saction']]) ){
154       $action = $_POST['saction'];
155       
156       /* Check if we have an DaemonEvent for this action */
157       if(class_available("DaemonEvent")){
158         $events = DaemonEvent::get_event_types(SYSTEM_EVENT | HIDDEN_EVENT);
159         if(isset($events['TRIGGERED']["DaemonEvent_".$action])){
160           $evt = $events['TRIGGERED']["DaemonEvent_".$action];
161           $tmp = new $evt['CLASS_NAME']($this->config);
162           $tmp->add_targets(array($this->netConfigDNS->macAddress));
163           $tmp->set_type(TRIGGERED_EVENT);
164           $o_queue = new gosaSupportDaemon();
165           if(!$o_queue->append($tmp)){
166             msg_dialog::display(_("Service infrastructure"),msgPool::siError($o_queue->get_error()),ERROR_DIALOG);
167           }
168         }
169       }else{
170         msg_dialog::display(_("Event error"),
171                     sprintf(_("Event '%s' is not available!"),$action),ERROR_DIALOG);
172       }
173     }
175     /* Base select dialog */
176     $once = true;
177     foreach($_POST as $name => $value){
178       if(preg_match("/^chooseBase/",$name) && $once ){
179         $once = false;
180         $this->dialog = new baseSelectDialog($this->config,$this,$this->get_allowed_bases());
181         $this->dialog->setCurrentBase($this->base);
182       }
183     }
185     /* Dialog handling */
186     if(is_object($this->dialog)){
187       /* Must be called before save_object */
188       $this->dialog->save_object();
190       if($this->dialog->isClosed()){
191         $this->dialog = false;
192       }elseif($this->dialog->isSelected()){
193   
194         /* Only accept allowed bases */
195         $tmp = $this->get_allowed_bases();
196         if($tmp[$this->dialog->isSelected()]){
197           $this->base = $this->dialog->isSelected();
198         }
199         $this->dialog= false;
200       }else{
201         return($this->dialog->execute());
202       }
203     }
205     /* Fill templating stuff */
206     $smarty= get_smarty();
208     $tmp = $this->plInfo();
209     foreach($tmp['plProvidedAcls'] as $name => $translated){
210       $smarty->assign($name."ACL",$this->getacl($name));
211     }
213     /* Assign base ACL */
214     $smarty->assign("bases"   , $this->get_allowed_bases());
216     /* Assign attributes */
217     foreach ($this->attributes as $attr){
218       $smarty->assign("$attr", $this->$attr);
219     }
221     $smarty->assign("staticAddress", "");
222     $smarty->assign("base_select", $this->base);
224     /* Assign status */
225     if (gosaSupportDaemon::ping($this->netConfigDNS->macAddress)){
226       $smarty->assign("actions", array("halt" => _("Switch off"), "reboot" => _("Reboot"),
227                                        "update" => _("System update"),
228                                        "reinstall" => _("Reinstall"),
229                                        "rescan" => _("Rescan hardware"),
230                                        "memcheck" => _("Memory test"),
231                                        "localboot" => _("Force localboot"),
232                                        "sysinfo"  => _("System analysis")));
233     } else {
234       $smarty->assign("actions", array("wakeup" => _("Wake up"),
235                                        "reinstall" => _("Reinstall"),
236                                        "update" => _("System update"),
237                                        "memcheck" => _("Memory test"),
238                                        "localboot" => _("Force localboot"),
239                                        "sysinfo"  => _("System analysis")));
240     }
242     /* Show main page */
243     $smarty->assign("fai_activated",$this->fai_activated);
245     $str = $this->netConfigDNS->execute();
246     if(is_object($this->netConfigDNS->dialog)){
247       return($str);
248     }
249     $smarty->assign("netconfig", $str);
250     $smarty->assign("modes", $this->modes);
251     $smarty->assign("currently_installing", $this->currently_installing);
253     $smarty->assign("host_key","");
254     if(is_object($this->kerberos_key_service)){
255       $smarty->assign("host_key",$this->kerberos_key_service->execute_by_prefix("host/"));
256     }
258     return($smarty->fetch (get_template_path('server.tpl', TRUE)));
259   }
261   function remove_from_parent()
262   {
263     /* Cancel if there's nothing to do here */
264     if ((!$this->initially_was_account) || (!$this->acl_is_removeable())){
265       return;
266     }
268     /* Remove kerberos key dependencies too */
269     if(is_object($this->kerberos_key_service)){
270       $this->kerberos_key_service->remove_from_parent_by_prefix("host/");
271     }
273     $this->netConfigDNS->remove_from_parent();
274     $ldap= $this->config->get_ldap_link();
275     $ldap->rmdir($this->dn);
277     update_accessTo($this->orig_cn,"");
279     new log("remove","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
281     if (!$ldap->success()){
282       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_MOD, get_class()));
283     }
285     /* Delete references to object groups */
286     $ldap->cd ($this->config->current['BASE']);
287     $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".LDAP::prepare4filter($this->dn)."))", array("cn"));
288     while ($ldap->fetch()){
289       $og= new ogroup($this->config, $ldap->getDN());
290       unset($og->member[$this->dn]);
291       $og->save ();
292     }
294     /* Clean queue form entries with this mac 
295      */
296     if(class_available("gosaSupportDaemon") && tests::is_mac($this->netConfigDNS->orig_macAddress)){
297       $q = new gosaSupportDaemon();
298       $q->clean_queue_from_mac($this->netConfigDNS->orig_macAddress);
299     }
300     $this->handle_post_events("remove",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
301   }
304   /* Save data to object */
305   function save_object()
306   {
307     /* Save current base, to be able to revert to last base, 
308         if new base is invalid or not allowed to be selected */
309     $base_tmp = $this->base;
310     plugin::save_object();
311     $this->netConfigDNS->save_object();
313     /* Get base selection */
314     $tmp = $this->get_allowed_bases();
315     if(isset($_POST['base'])){
316       if($tmp[$_POST['base']]){
317         $this->base = $_POST['base'];
318       }else{
319         $this->base = $base_tmp;
320       }
321     }
323     /* Hanle kerberos host key plugin */
324     if(is_object($this->kerberos_key_service)){
325       $this->kerberos_key_service->save_object_by_prefix("host/");
326     }
327   }
330   /* Check supplied data */
331   function check()
332   {
333     /* Call common method to give check the hook */
334     $message= plugin::check();
335     $message= array_merge($message, $this->netConfigDNS->check());
336     $this->dn= "cn=".$this->cn.",".get_ou('serverRDN').$this->base;
338     /* must: cn */
339     if ($this->cn == ""){
340       msgPool::required(_("Server name"));
341     }
342     
343     /* Check if given name is a valid host/dns name */
344     if(!tests::is_dns_name($this->cn)){
345       $message[]= msgPool::invalid(_("Server name"), $this->cn, "/[a-z0-9\.\-]/i");
346     }
348     if ($this->orig_dn != $this->dn){
349       $ldap= $this->config->get_ldap_link();
350       $ldap->cd ($this->base);
351       $ldap->search ("(cn=".$this->cn.")", array("cn"));
352       if ($ldap->count() != 0){
353         while ($attrs= $ldap->fetch()){
354           if ($attrs['dn'] != $this->orig_dn){
355             if(!preg_match("/cn=dhcp,/",$attrs['dn']) && !preg_match("/,".get_ou('systemIncomingRDN')."/",$attrs['dn']) && preg_match("/,".get_ou('serverRDN')."/",$attrs['dn'])){
356               $message[]= msgPool::duplicated(_("Server name"));
357               break;
358             }
359           }
360         }
361       }
362     }
364     /* Warn the user, that this host is currently installing */
365     if($this->currently_installing && !$this->currently_installing_warned && !preg_match("/".preg_quote(get_ou("systemIncomingRDN"), '/')."/",$this->orig_dn)){
367       /* Force aborting without message dialog */
368       $message[] = "";
369       $this->currently_installing_warned = TRUE;
370       msg_dialog::display(_("Software deployment"), 
371           _("This host is currently installing, if you really want to save it, press 'OK'."),
372           CONFIRM_DIALOG);
373     }
375     /* Check if we are allowed to create or move this object
376      */
377     if($this->orig_dn == "new" && !$this->acl_is_createable($this->base)){
378       $message[] = msgPool::permCreate();
379     }elseif($this->orig_dn != "new" && $this->base != $this->orig_base && !$this->acl_is_moveable($this->base)){
380       $message[] = msgPool::permMove();
381     }
383     return ($message);
384   }
387   /* Save to LDAP */
388   function save()
389   {
390     /* Detect mode changes */
391     $activate= (isset($this->saved_attributes['gotoMode']) &&
392         $this->gotoMode != $this->saved_attributes['gotoMode'] &&
393         $this->gotoMode == "active" &&
394         tests::is_ip($this->netConfigDNS->ipHostNumber));
396     plugin::save();
398     /* Remove all empty values */
399     if ($this->orig_dn == 'new'){
400       $attrs= array();
401       foreach ($this->attrs as $key => $val){
402         if (is_array($val) && count($val) == 0){
403           continue;
404         }
405         $attrs[$key]= $val;
406       }
407       $this->attrs= $attrs;
408     }
410     /* Write back to ldap */
411     $ldap= $this->config->get_ldap_link();
412     if ($this->orig_dn == 'new'){
413       $ldap->cd($this->config->current['BASE']);
414       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
415       $ldap->cd($this->dn);
416       $ldap->add($this->attrs);
417       new log("create","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
418       $mode= "add";
419     } else {
420   
421       $ldap->cd($this->dn);
422       $this->cleanup();
423       $ldap->modify ($this->attrs); 
424       new log("modify","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
426       /* Update all accessTo/trust dependencies */
427       if($this->orig_cn != $this->cn){
428         update_accessTo($this->orig_cn,$this->cn);
429       }
431       $mode= "modify";
432     }
433     if (!$ldap->success()){
434       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_DEL, get_class()));
435     }
436     $this->netConfigDNS->cn = $this->cn;
437     $this->netConfigDNS->save();
439     /* Optionally execute a command after we're done */
440     $this->handle_post_events($mode,array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
442     if ($activate){
444       /* Send installation activation 
445        */
446       $events = DaemonEvent::get_event_types(SYSTEM_EVENT | HIDDEN_EVENT);
447       $o_queue = new gosaSupportDaemon();
448       if(isset($events['TRIGGERED']['DaemonEvent_installation_activation'])){
449         $evt = $events['TRIGGERED']['DaemonEvent_installation_activation'];
450         $tmp = new $evt['CLASS_NAME']($this->config);
451         $tmp->set_type(TRIGGERED_EVENT);
452         $tmp->add_targets(array($this->netConfigDNS->macAddress));
453         if(!$o_queue->append($tmp)){
454           msg_dialog::display(_("Service infrastructure"),msgPool::siError($o_queue->get_error()),ERROR_DIALOG);
455         }
456       }
457     }
459   }
462   /* Display generic part for server copy & paste */
463   function getCopyDialog()
464   {
465     $vars = array("cn");
467     $smarty = get_smarty();
468     $smarty->assign("cn" ,$this->cn);
469     $smarty->assign("object","server");
470     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
471     $ret = array();
472     $ret['string'] = $str;
473     $ret['status'] = "";
474     return($ret);
475   }
478   function saveCopyDialog()
479   {
480     if(isset($_POST['cn'])){
481       $this->cn = $_POST['cn'];
482     }
483   }
485   
486   function PrepareForCopyPaste($source)  
487   {
488     plugin::PrepareForCopyPaste($source);
489     if(isset($source['macAddress'][0])){
490       $this->netConfigDNS->macAddress = $source['macAddress'][0];
491     }
492     if(isset($source['ipHostNumber'][0])){
493       $this->netConfigDNS->ipHostNumber = $source['ipHostNumber'][0];
494     }
496   }
497   
500   /* Return plugin informations for acl handling */ 
501   static function plInfo()
502   {
503     return (array( 
504           "plShortName"   => _("Generic"),
505           "plDescription" => _("Server generic"),
506           "plSelfModify"  => FALSE,
507           "plDepends"     => array(),
508           "plPriority"    => 1,
509           "plSection"     => array("administration"),           
510           "plCategory"    => array("server" => array("description"  => _("Server"),
511                                                      "objectClass"  => "goServer")),
512           "plProvidedAcls"=> array(
513             "cn"            => _("Name"),
514             "description"   => _("Description"),
515             "base"          => _("Base"),
517             "gotoMode"      => _("Goto mode"),
518             "userPassword"=> _("Root password"),
519             "FAIstate"      => _("Action flag"))
520           ));
521   }
524 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
525 ?>