Code

Updated system Management
[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= "";
36   /* attribute list for save action */
37   var $attributes= array("cn", "description","gotoMode");
38   var $objectclasses= array("top", "GOhard", "goServer");
40   var $gotoMode= "locked";
42   var $netConfigDNS;
43   var $modes = array();
44   var $ui   ;
45   var $validActions   = array("reboot" => "", "update" => "", "localboot" => "", "reinstall" => "", "rescan" => "",
46                             "wake" => "", "memcheck" => "", "sysinfo" => "");
48   var $fai_activated  =FALSE;
49   var $view_logged = FALSE;
51   var $currently_installing = FALSE;
52   var $currently_installing_warned = FALSE;
54   function servgeneric (&$config, $dn= NULL, $parent= NULL)
55   {
56     /* Check if FAI is activated */
57     $tmp = $config->search("faiManagement", "CLASS",array('menu','tabs'));
58     
59     if(!empty($tmp)){
60       $this->fai_activated = TRUE;
61     }
63     plugin::plugin ($config, $dn, $parent);
65     $this->ui = get_userinfo();
66     $this->modes["active"]= _("Activated");
67     $this->modes["locked"]= _("Locked");
69     /* Set base */
70     if ($this->dn == "new"){
71       $ui= get_userinfo();
72       $this->base= dn2base($ui->dn);
73       $this->cn= "";
74     } else {
75       $this->base= preg_replace ("/^[^,]+,".normalizePreg(get_ou("serverou"))."/", "", $this->dn);
76     }
77     $this->netConfigDNS = new termDNS($this->config,$this,$this->objectclasses);
78     $this->netConfigDNS->set_acl_category("server");
79     $this->netConfigDNS->set_acl_base($this->base);
81     /* Check if this host is currently in installation process*/
82     if(class_available("gosaSupportDaemon") && class_available("DaemonEvent")){
83       $o = new gosaSupportDaemon();
84       $e_types = DaemonEvent::get_event_types(USER_EVENT | SYSTEM_EVENT | HIDDEN_EVENT);
85       $evts = $o->get_entries_by_mac(array($this->netConfigDNS->macAddress));
86       foreach($evts as $evt){
87         if(isset($e_types['QUEUED'][$evt['HEADERTAG']]) && $evt['STATUS'] == "processing" && 
88             $e_types['QUEUED'][$evt['HEADERTAG']] == "DaemonEvent_reinstall"){
89           $this->currently_installing =TRUE;
90         }
91       }
92     }
93        
94     /* Save dn for later references */
95     $this->orig_dn= $this->dn;
96   }
99   function set_acl_base($base)
100   {
101     plugin::set_acl_base($base);
102     $this->netConfigDNS->set_acl_base($base);
103   }
106   function set_acl_category($cat)
107   {
108     plugin::set_acl_category($cat);
109     $this->netConfigDNS->set_acl_category($cat);
110   }
113   function execute()
114   {
115     /* Call parent execute */
116     plugin::execute();
118     if($this->is_account && !$this->view_logged){
119       $this->view_logged = TRUE;
120       new log("view","server/".get_class($this),$this->dn);
121     }
123     /* Do we represent a valid server? */
124     if (!$this->is_account && $this->parent === NULL){
125       $display= "<img alt=\"\" src=\"images/stop.png\" align=middle>&nbsp;<b>".
126         msgPool::noValidExtension(_("server"))."</b>"; 
127       return($display);
128     }
130     if ((isset($_POST['action'])) && ($this->acl_is_writeable("FAIstate")) && isset($this->mapActions[$_POST['saction']]) ){
131       $action = $_POST['saction'];
132       
133       /* Check if we have an DaemonEvent for this action */
134       if(class_available("DaemonEvent")){
135         $events = DaemonEvent::get_event_types(SYSTEM_EVENT | HIDDEN_EVENT);
136         if(isset($events['TRIGGERED']["DaemonEvent_".$action])){
137           $evt = $events['TRIGGERED']["DaemonEvent_".$action];
138           $tmp = new $evt['CLASS_NAME']($this->config);
139           $tmp->add_targets(array($this->netConfigDNS->macAddress));
140           $tmp->set_type(TRIGGERED_EVENT);
141           $o_queue = new gosaSupportDaemon();
142           if(!$o_queue->append($tmp)){
143             msg_dialog::display(_("Service infrastructure"),msgPool::siError($o_queue->get_error()),ERROR_DIALOG);
144           }
145         }
146       }else{
147         msg_dialog::display(_("Event error"),
148                     sprintf(_("Event '%s' is not available!"),$action),ERROR_DIALOG);
149       }
150     }
152     /* Base select dialog */
153     $once = true;
154     foreach($_POST as $name => $value){
155       if(preg_match("/^chooseBase/",$name) && $once ){
156         $once = false;
157         $this->dialog = new baseSelectDialog($this->config,$this,$this->get_allowed_bases());
158         $this->dialog->setCurrentBase($this->base);
159       }
160     }
162     /* Dialog handling */
163     if(is_object($this->dialog)){
164       /* Must be called before save_object */
165       $this->dialog->save_object();
167       if($this->dialog->isClosed()){
168         $this->dialog = false;
169       }elseif($this->dialog->isSelected()){
170   
171         /* Only accept allowed bases */
172         $tmp = $this->get_allowed_bases();
173         if($tmp[$this->dialog->isSelected()]){
174           $this->base = $this->dialog->isSelected();
175         }
176         $this->dialog= false;
177       }else{
178         return($this->dialog->execute());
179       }
180     }
182     /* Fill templating stuff */
183     $smarty= get_smarty();
185     $tmp = $this->plInfo();
186     foreach($tmp['plProvidedAcls'] as $name => $translated){
187       $smarty->assign($name."ACL",$this->getacl($name));
188     }
190     /* Assign base ACL */
191     $smarty->assign("bases"   , $this->get_allowed_bases());
193     /* Assign attributes */
194     foreach ($this->attributes as $attr){
195       $smarty->assign("$attr", $this->$attr);
196     }
198     $smarty->assign("staticAddress", "");
199     $smarty->assign("base_select", $this->base);
201     /* Assign status */
202     if (gosaSupportDaemon::ping($this->netConfigDNS->macAddress)){
203       $smarty->assign("actions", array("halt" => _("Switch off"), "reboot" => _("Reboot"),
204                                        "update" => _("System update"),
205                                        "reinstall" => _("Reinstall"),
206                                        "rescan" => _("Rescan hardware"),
207                                        "memcheck" => _("Memory test"),
208                                        "localboot" => _("Force localboot"),
209                                        "sysinfo"  => _("System analysis")));
210     } else {
211       $smarty->assign("actions", array("wake" => _("Wake up"),
212                                        "reinstall" => _("Reinstall"),
213                                        "update" => _("System update"),
214                                        "memcheck" => _("Memory test"),
215                                        "localboot" => _("Force localboot"),
216                                        "sysinfo"  => _("System analysis")));
217     }
219     /* Show main page */
220     $smarty->assign("fai_activated",$this->fai_activated);
222     $str = $this->netConfigDNS->execute();
223     if(is_object($this->netConfigDNS->dialog)){
224       return($str);
225     }
226     $smarty->assign("netconfig", $str);
227     $smarty->assign("modes", $this->modes);
228     $smarty->assign("currently_installing", $this->currently_installing);
230     return($smarty->fetch (get_template_path('server.tpl', TRUE)));
231   }
233   function remove_from_parent()
234   {
235     /* Cancel if there's nothing to do here */
236     if ((!$this->initially_was_account) || (!$this->acl_is_removeable())){
237       return;
238     }
240     $this->netConfigDNS->remove_from_parent();
241     $ldap= $this->config->get_ldap_link();
242     $ldap->rmdir($this->dn);
244     new log("remove","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
246     if (!$ldap->success()){
247       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_MOD, get_class()));
248     }
250     /* Delete references to object groups */
251     $ldap->cd ($this->config->current['BASE']);
252     $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".LDAP::prepare4filter($this->dn)."))", array("cn"));
253     while ($ldap->fetch()){
254       $og= new ogroup($this->config, $ldap->getDN());
255       unset($og->member[$this->dn]);
256       $og->save ();
257     }
259     /* Clean queue form entries with this mac 
260      */
261     if(class_available("gosaSupportDaemon") && tests::is_mac($this->netConfigDNS->orig_macAddress)){
262       $q = new gosaSupportDaemon();
263       $q->clean_queue_from_mac($this->netConfigDNS->orig_macAddress);
264     }
265     $this->handle_post_events("remove",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
266   }
269   /* Save data to object */
270   function save_object()
271   {
272     /* Save current base, to be able to revert to last base, 
273         if new base is invalid or not allowed to be selected */
274     $base_tmp = $this->base;
275     plugin::save_object();
276     $this->netConfigDNS->save_object();
278     /* Get base selection */
279     $tmp = $this->get_allowed_bases();
280     if(isset($_POST['base'])){
281       if($tmp[$_POST['base']]){
282         $this->base = $_POST['base'];
283       }else{
284         $this->base = $base_tmp;
285       }
286     }
287   }
290   /* Check supplied data */
291   function check()
292   {
293     /* Call common method to give check the hook */
294     $message= plugin::check();
295     $message= array_merge($message, $this->netConfigDNS->check());
296     $this->dn= "cn=".$this->cn.",".get_ou('serverou').$this->base;
298     /* must: cn */
299     if ($this->cn == ""){
300       msgPool::required(_("Server name"));
301     }
302     
303     /* Check if given name is a valid host/dns name */
304     if(!tests::is_dns_name($this->cn)){
305       $message[]= msgPool::invalid(_("Server name"), $this->cn, "/[a-z0-9\.\-]/i");
306     }
308     if ($this->orig_dn != $this->dn){
309       $ldap= $this->config->get_ldap_link();
310       $ldap->cd ($this->base);
311       $ldap->search ("(cn=".$this->cn.")", array("cn"));
312       if ($ldap->count() != 0){
313         while ($attrs= $ldap->fetch()){
314           if ($attrs['dn'] != $this->orig_dn){
315             if(!preg_match("/cn=dhcp,/",$attrs['dn']) && !preg_match("/,".get_ou('incomingou')."/",$attrs['dn']) && preg_match("/,".get_ou('serverou')."/",$attrs['dn'])){
316               $message[]= msgPool::duplicated(_("Server name"));
317               break;
318             }
319           }
320         }
321       }
322     }
324     /* Warn the user, that this host is currently installing */
325     if($this->currently_installing && !$this->currently_installing_warned && !preg_match("/".normalizePreg(get_ou("incomingou"))."/",$this->orig_dn)){
326       $this->currently_installing_warned = TRUE;
327       $message[] = _("This host is currently installing, if you really want to save it, save again.");
328     }
330     return ($message);
331   }
334   /* Save to LDAP */
335   function save()
336   {
337     /* Detect mode changes */
338     $activate= (isset($this->saved_attributes['gotoMode']) &&
339         $this->gotoMode != $this->saved_attributes['gotoMode'] &&
340         $this->gotoMode == "active" &&
341         tests::is_ip($this->netConfigDNS->ipHostNumber));
343     plugin::save();
345     /* Remove all empty values */
346     if ($this->orig_dn == 'new'){
347       $attrs= array();
348       foreach ($this->attrs as $key => $val){
349         if (is_array($val) && count($val) == 0){
350           continue;
351         }
352         $attrs[$key]= $val;
353       }
354       $this->attrs= $attrs;
355     }
357     /* Write back to ldap */
358     $ldap= $this->config->get_ldap_link();
359     if ($this->orig_dn == 'new'){
360       $ldap->cd($this->config->current['BASE']);
361       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
362       $ldap->cd($this->dn);
363       $ldap->add($this->attrs);
364       new log("create","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
365       $mode= "add";
366     } else {
367    
368       /* cn is not case sensitive for ldap, but for php it is!! */ 
369       if($this->config->current['DNMODE'] == "cn"){
370         if (strtolower($this->orig_dn) != (strtolower($this->dn))){
371           $this->recursive_move($this->orig_dn, $this->dn);
372           plugin::save();
373         }
374       }else{
375         if ($this->orig_dn != $this->dn){
376           $this->recursive_move($this->orig_dn, $this->dn);
377           plugin::save();
378         }
379       }
380   
381       $ldap->cd($this->dn);
382       $this->cleanup();
383       $ldap->modify ($this->attrs); 
384       new log("modify","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
386       $mode= "modify";
387     }
388     if (!$ldap->success()){
389       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_DEL, get_class()));
390     }
391     $this->netConfigDNS->cn = $this->cn;
392     $this->netConfigDNS->save();
394     /* Optionally execute a command after we're done */
395     $this->handle_post_events($mode,array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
397     if ($activate){
399       /* Send installation activation 
400        */
401       $events = DaemonEvent::get_event_types(SYSTEM_EVENT | HIDDEN_EVENT);
402       $o_queue = new gosaSupportDaemon();
403       if(isset($events['TRIGGERED']['DaemonEvent_installation_activation'])){
404         $evt = $events['TRIGGERED']['DaemonEvent_installation_activation'];
405         $tmp = new $evt['CLASS_NAME']($this->config);
406         $tmp->set_type(TRIGGERED_EVENT);
407         $tmp->add_targets(array($this->netConfigDNS->macAddress));
408         if(!$o_queue->append($tmp)){
409           msg_dialog::display(_("Service infrastructure"),msgPool::siError($o_queue->get_error()),ERROR_DIALOG);
410         }
411       }
412     }
414   }
417   /* Display generic part for server copy & paste */
418   function getCopyDialog()
419   {
420     $vars = array("cn");
422     $smarty = get_smarty();
423     $smarty->assign("cn" ,$this->cn);
424     $smarty->assign("object","server");
425     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
426     $ret = array();
427     $ret['string'] = $str;
428     $ret['status'] = "";
429     return($ret);
430   }
433   function saveCopyDialog()
434   {
435     if(isset($_POST['cn'])){
436       $this->cn = $_POST['cn'];
437     }
438   }
440   
441   function PrepareForCopyPaste($source)  
442   {
443     plugin::PrepareForCopyPaste($source);
444     if(isset($source['macAddress'][0])){
445       $this->netConfigDNS->macAddress = $source['macAddress'][0];
446     }
447     if(isset($source['ipHostNumber'][0])){
448       $this->netConfigDNS->ipHostNumber = $source['ipHostNumber'][0];
449     }
451   }
452   
455   /* Return plugin informations for acl handling */ 
456   static function plInfo()
457   {
458     return (array( 
459           "plShortName"   => _("Generic"),
460           "plDescription" => _("Server generic"),
461           "plSelfModify"  => FALSE,
462           "plDepends"     => array(),
463           "plPriority"    => 1,
464           "plSection"     => array("administration"),           
465           "plCategory"    => array("server" => array("description"  => _("Server"),
466                                                      "objectClass"  => "goServer")),
467           "plProvidedAcls"=> array(
468             "cn"           => _("Name"),
469             "description"  => _("Description"),
470             "gotoMode"     => _("Goto mode"),
471             "base"         => _("Base"),
472             "FAIstate"     => _("Action flag"))
473           ));
474   }
477 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
478 ?>