Code

Updated defaultFaiRelease property
[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;
57   var $baseSelector;
59   var $mapActions   = array("reboot"          => "",
60                             "instant_update"  => "softupdate",
61                             "localboot"       => "localboot",
62                             "update"          => "sceduledupdate",
63                             "reinstall"       => "install",
64                             "rescan"          => "",
65                             "wakeup"            => "",
66                             "memcheck"        => "memcheck",
67                             "sysinfo"         => "sysinfo");
70   function servgeneric (&$config, $dn= NULL, $parent= NULL)
71   {
72     /* Check if FAI is activated */
73     $tmp = $config->search("faiManagement", "CLASS",array('menu','tabs'));
74     
75     if(!empty($tmp)){
76       $this->fai_activated = TRUE;
77     }
79     plugin::plugin ($config, $dn, $parent);
81     /* Initialize */
82     $this->ui = get_userinfo();
83     $this->modes["active"]= _("Activated");
84     $this->modes["locked"]= _("Locked");
86     /* Set base */
87     if ($this->dn == "new"){
88       $ui= get_userinfo();
89       $this->base= dn2base(session::global_is_set("CurrentMainBase")?"cn=dummy,".session::global_get("CurrentMainBase"):$ui->dn);
90       $this->cn= "";
91     } elseif(preg_match("/".preg_quote(get_ou("systemIncomingRDN"), '/')."/i", $this->dn)){
92       $this->base= preg_replace ("/^[^,]+,".preg_quote(get_ou("systemIncomingRDN"), '/')."/i", "", $this->dn);
93     } else {
94       $this->base= preg_replace ("/^[^,]+,".preg_quote(get_ou("serverRDN"), '/')."/i", "", $this->dn);
95     }
96     $this->netConfigDNS = new termDNS($this->config,$this,$this->objectclasses);
97     $this->netConfigDNS->set_acl_category("server");
98     $this->netConfigDNS->set_acl_base($this->base);
99     $this->netConfigDNS->MACisMust =TRUE;
101     /* Initialize kerberos host key plugin */
102     if(class_available("krbHostKeys")){
103       $this->kerberos_key_service = new krbHostKeys($this->config,$this);
104     }
106     /* Check if this host is currently in installation process*/
107     if($this->dn != "new" && class_available("gosaSupportDaemon") && class_available("DaemonEvent")){
108       $o = new gosaSupportDaemon();
109       $e_types = DaemonEvent::get_event_types(USER_EVENT | SYSTEM_EVENT | HIDDEN_EVENT);
110       $evts = $o->get_entries_by_mac(array($this->netConfigDNS->macAddress));
111       foreach($evts as $evt){
112         if(isset($e_types['QUEUED'][$evt['HEADERTAG']]) && $evt['STATUS'] == "processing" && 
113             $e_types['QUEUED'][$evt['HEADERTAG']] == "DaemonEvent_reinstall"){
114           $this->currently_installing =TRUE;
115         }
116       }
117     }
118        
119     /* Save dn for later references */
120     $this->orig_dn   = $this->dn;
121     $this->orig_cn   = $this->cn;
122     $this->orig_base = $this->base;
124     /* Instanciate base selector */
125     $this->baseSelector= new baseSelector($this->get_allowed_bases(), $this->base);
126     $this->baseSelector->setSubmitButton(false);
127     $this->baseSelector->setHeight(300);
128     $this->baseSelector->update(true);
129   }
132   function set_acl_base($base)
133   {
134     plugin::set_acl_base($base);
135     $this->netConfigDNS->set_acl_base($base);
136   }
139   function set_acl_category($cat)
140   {
141     plugin::set_acl_category($cat);
142     $this->netConfigDNS->set_acl_category($cat);
143   }
146   function execute()
147   {
148     
150     /* Call parent execute */
151     plugin::execute();
153     if($this->is_account && !$this->view_logged){
154       $this->view_logged = TRUE;
155       new log("view","server/".get_class($this),$this->dn);
156     }
158     /* Do we represent a valid server? */
159     if (!$this->is_account && $this->parent === NULL){
160       $display= "<img alt=\"\" src=\"images/small-error.png\" align=middle>&nbsp;<b>".
161         msgPool::noValidExtension(_("server"))."</b>"; 
162       return($display);
163     }
165     if ((isset($_POST['action'])) && ($this->acl_is_writeable("FAIstate")) && isset($this->mapActions[$_POST['saction']]) ){
166       $action = $_POST['saction'];
167       
168       /* Check if we have an DaemonEvent for this action */
169       if(class_available("DaemonEvent")){
170         $events = DaemonEvent::get_event_types(SYSTEM_EVENT | HIDDEN_EVENT);
171         if(isset($events['TRIGGERED']["DaemonEvent_".$action])){
172           $evt = $events['TRIGGERED']["DaemonEvent_".$action];
173           $tmp = new $evt['CLASS_NAME']($this->config);
174           $tmp->add_targets(array($this->netConfigDNS->macAddress));
175           $tmp->set_type(TRIGGERED_EVENT);
176           $o_queue = new gosaSupportDaemon();
177           if(!$o_queue->append($tmp)){
178             msg_dialog::display(_("Service infrastructure"),msgPool::siError($o_queue->get_error()),ERROR_DIALOG);
179           }
180         }
181       }else{
182         msg_dialog::display(_("Event error"),
183                     sprintf(_("Event '%s' is not available!"),$action),ERROR_DIALOG);
184       }
185     }
187     /* Fill templating stuff */
188     $smarty= get_smarty();
191     $tmp = $this->plInfo();
192     foreach($tmp['plProvidedAcls'] as $name => $translated){
193       $smarty->assign($name."ACL",$this->getacl($name));
194     }
196     /* Assign base ACL */
197     $smarty->assign("base", $this->baseSelector->render());
199     /* Assign attributes */
200     foreach ($this->attributes as $attr){
201       $smarty->assign("$attr", $this->$attr);
202     }
204     /* Assign status */
205     if (gosaSupportDaemon::ping($this->netConfigDNS->macAddress)){
206       $smarty->assign("actions", array("halt" => _("Switch off"), "reboot" => _("Reboot"),
207                                        "update" => _("System update"),
208                                        "reinstall" => _("Reinstall"),
209                                        "rescan" => _("Rescan hardware"),
210                                        "memcheck" => _("Memory test"),
211                                        "localboot" => _("Force localboot"),
212                                        "sysinfo"  => _("System analysis")));
213     } else {
214       $smarty->assign("actions", array("wakeup" => _("Wake up"),
215                                        "reinstall" => _("Reinstall"),
216                                        "update" => _("System update"),
217                                        "memcheck" => _("Memory test"),
218                                        "localboot" => _("Force localboot"),
219                                        "sysinfo"  => _("System analysis")));
220     }
222     /* Show main page */
223     $smarty->assign("fai_activated",$this->fai_activated);
225     $str = $this->netConfigDNS->execute();
226     $this->dialog = $this->is_modal_dialog();
227     if(is_object($this->netConfigDNS->dialog)){
228       return($str);
229     }
230     $smarty->assign("netconfig", $str);
231     $smarty->assign("modes", $this->modes);
232     $smarty->assign("currently_installing", $this->currently_installing);
234     $smarty->assign("host_key","");
235     if(is_object($this->kerberos_key_service)){
236       $smarty->assign("host_key",$this->kerberos_key_service->execute_by_prefix("host/"));
237     }
239     return($smarty->fetch (get_template_path('server.tpl', TRUE)));
240   }
242   function remove_from_parent()
243   {
244     /* Cancel if there's nothing to do here */
245     if ((!$this->initially_was_account) || (!$this->acl_is_removeable())){
246       return;
247     }
249     /* Remove kerberos key dependencies too */
250     if(is_object($this->kerberos_key_service)){
251       $this->kerberos_key_service->remove_from_parent_by_prefix("host/");
252     }
254     $this->netConfigDNS->remove_from_parent();
255     $ldap= $this->config->get_ldap_link();
256     $ldap->rmdir($this->dn);
258     update_accessTo($this->orig_cn,"");
260     new log("remove","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
262     if (!$ldap->success()){
263       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_MOD, get_class()));
264     }
266     /* Delete references to object groups */
267     $ldap->cd ($this->config->current['BASE']);
268     $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".LDAP::prepare4filter($this->dn)."))", array("cn"));
269     while ($ldap->fetch()){
270       $og= new ogroup($this->config, $ldap->getDN());
271       unset($og->member[$this->dn]);
272       $og->save ();
273     }
275     /* Clean queue form entries with this mac 
276      */
277     if(class_available("gosaSupportDaemon") && tests::is_mac($this->netConfigDNS->orig_macAddress)){
278       $q = new gosaSupportDaemon();
279       $q->clean_queue_from_mac($this->netConfigDNS->orig_macAddress);
280     }
281     $this->handle_post_events("remove",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
282   }
285   /* Save data to object */
286   function save_object()
287   {
288     /* Save current base, to be able to revert to last base, 
289         if new base is invalid or not allowed to be selected */
290     $base_tmp = $this->base;
291     plugin::save_object();
292     $this->netConfigDNS->save_object();
294     /* Refresh base */
295     if ($this->acl_is_moveable($this->base)){
296       if (!$this->baseSelector->update()) {
297         msg_dialog::display(_("Error"), msgPool::permMove(), ERROR_DIALOG);
298       }
299       if ($this->base != $this->baseSelector->getBase()) {
300         $this->base= $this->baseSelector->getBase();
301         $this->is_modified= TRUE;
302       }
303     }
306     /* Hanle kerberos host key plugin */
307     if(is_object($this->kerberos_key_service)){
308       $this->kerberos_key_service->save_object_by_prefix("host/");
309     }
310   }
313   /* Check supplied data */
314   function check()
315   {
316     /* Call common method to give check the hook */
317     $message= plugin::check();
318     $message= array_merge($message, $this->netConfigDNS->check());
319     $this->dn= "cn=".$this->cn.",".get_ou('serverRDN').$this->base;
321     /* must: cn */
322     if ($this->cn == ""){
323       msgPool::required(_("Server name"));
324     }
325     
326     /* Check if given name is a valid host/dns name */
327     if(!tests::is_dns_name($this->cn)){
328       $message[]= msgPool::invalid(_("Server name"), $this->cn, "/[a-z0-9\.\-]/i");
329     }
331     // Check if a wrong base was supplied
332     if(!$this->baseSelector->checkLastBaseUpdate()){
333       $message[]= msgPool::check_base();;
334     }
336     if ($this->orig_dn != $this->dn){
337       $ldap= $this->config->get_ldap_link();
338       $ldap->cd ($this->base);
339       $ldap->search ("(cn=".$this->cn.")", array("cn"));
340       if ($ldap->count() != 0){
341         while ($attrs= $ldap->fetch()){
342           if ($attrs['dn'] != $this->orig_dn){
343             if(!preg_match("/cn=dhcp,/",$attrs['dn']) && !preg_match("/,".preg_quote(get_ou('systemIncomingRDN'), '/')."/i",$attrs['dn']) && preg_match("/,".preg_quote(get_ou('serverRDN'), '/')."/i",$attrs['dn'])){
344               $message[]= msgPool::duplicated(_("Server name"));
345               break;
346             }
347           }
348         }
349       }
350     }
352     /* Warn the user, that this host is currently installing */
353     if($this->currently_installing && !$this->currently_installing_warned && !preg_match("/".preg_quote(get_ou("systemIncomingRDN"), '/')."/i",$this->orig_dn)){
355       /* Force aborting without message dialog */
356       $message[] = "";
357       $this->currently_installing_warned = TRUE;
358       msg_dialog::display(_("Software deployment"), 
359           _("This host is currently installing, if you really want to save it, press 'OK'."),
360           CONFIRM_DIALOG);
361     }
363     /* Check if we are allowed to create or move this object
364      */
365     if($this->orig_dn == "new" && !$this->acl_is_createable($this->base)){
366       $message[] = msgPool::permCreate();
367     }elseif($this->orig_dn != "new" && $this->base != $this->orig_base && !$this->acl_is_moveable($this->base)){
368       $message[] = msgPool::permMove();
369     }
371     return ($message);
372   }
375   /* Save to LDAP */
376   function save()
377   {
378     /* Detect mode changes */
379     $activate= (isset($this->saved_attributes['gotoMode']) &&
380         $this->gotoMode != $this->saved_attributes['gotoMode'] &&
381         $this->gotoMode == "active" &&
382         tests::is_ip($this->netConfigDNS->ipHostNumber));
384     plugin::save();
386     /* Remove all empty values */
387     if ($this->orig_dn == 'new'){
388       $attrs= array();
389       foreach ($this->attrs as $key => $val){
390         if (is_array($val) && count($val) == 0){
391           continue;
392         }
393         $attrs[$key]= $val;
394       }
395       $this->attrs= $attrs;
396     }
398     /* Write back to ldap */
399     $ldap= $this->config->get_ldap_link();
400     if ($this->orig_dn == 'new'){
401       $ldap->cd($this->config->current['BASE']);
402       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
403       $ldap->cd($this->dn);
404       $ldap->add($this->attrs);
405       new log("create","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
406       $mode= "add";
407     } else {
408   
409       $ldap->cd($this->dn);
410       $this->cleanup();
411       $ldap->modify ($this->attrs); 
412       new log("modify","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
414       /* Update all accessTo/trust dependencies */
415       if($this->orig_cn != $this->cn){
416         update_accessTo($this->orig_cn,$this->cn);
417       }
419       $mode= "modify";
420     }
421     if (!$ldap->success()){
422       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_DEL, get_class()));
423     }
424     $this->netConfigDNS->cn = $this->cn;
425     $this->netConfigDNS->save();
427     /* Optionally execute a command after we're done */
428     $this->handle_post_events($mode,array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
430     if ($activate){
432       /* Send installation activation 
433        */
434       $events = DaemonEvent::get_event_types(SYSTEM_EVENT | HIDDEN_EVENT);
435       $o_queue = new gosaSupportDaemon();
436       if(isset($events['TRIGGERED']['DaemonEvent_installation_activation'])){
437         $evt = $events['TRIGGERED']['DaemonEvent_installation_activation'];
438         $tmp = new $evt['CLASS_NAME']($this->config);
439         $tmp->set_type(TRIGGERED_EVENT);
440         $tmp->add_targets(array($this->netConfigDNS->macAddress));
441         if(!$o_queue->append($tmp)){
442           msg_dialog::display(_("Service infrastructure"),msgPool::siError($o_queue->get_error()),ERROR_DIALOG);
443         }
444       }
445     }
447   }
450   /* Display generic part for server copy & paste */
451   function getCopyDialog()
452   {
453     $vars = array("cn");
455     $smarty = get_smarty();
456     $smarty->assign("cn" ,$this->cn);
457     $smarty->assign("object","server");
458     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
459     $ret = array();
460     $ret['string'] = $str;
461     $ret['status'] = "";
462     return($ret);
463   }
466   function saveCopyDialog()
467   {
468     if(isset($_POST['cn'])){
469       $this->cn = $_POST['cn'];
470     }
471   }
473   
474   function PrepareForCopyPaste($source)  
475   {
476     plugin::PrepareForCopyPaste($source);
477     if(isset($source['macAddress'][0])){
478       $this->netConfigDNS->macAddress = $source['macAddress'][0];
479     }
480     if(isset($source['ipHostNumber'][0])){
481       $this->netConfigDNS->ipHostNumber = $source['ipHostNumber'][0];
482     }
484   }
485   
488   /* Return plugin informations for acl handling */ 
489   static function plInfo()
490   {
491     return (array( 
492           "plShortName"   => _("Generic"),
493           "plDescription" => _("Server generic"),
494           "plSelfModify"  => FALSE,
495           "plDepends"     => array(),
496           "plPriority"    => 1,
497           "plSection"     => array("administration"),           
498           "plCategory"    => array("server" => array("description"  => _("Server"),
499                                                      "objectClass"  => "goServer")),
500           "plProvidedAcls"=> array(
501             "cn"            => _("Name"),
502             "description"   => _("Description"),
503             "base"          => _("Base"),
505             "gotoMode"      => _("Goto mode"),
506             "userPassword"=> _("Root password"),
507             "FAIstate"      => _("Action flag"))
508           ));
509   }
512   function is_modal_dialog()
513   {
514     return((isset($this->dialog) && $this->dialog) || (isset($this->netConfigDNS->dialog) && $this->netConfigDNS->dialog));
515   }
519 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
520 ?>