Code

b2295c8ab0ddcf5e55ddfc52ab9af19b70db647a
[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 ("/^[^,]+,[^,]+,[^,]+,/", "", $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(_("Daemon"),sprintf(_("Something went wrong while talking to the daemon: %s."),
144                   $o_queue->get_error()),ERROR_DIALOG);
145           }
146         }
147       }else{
148         msg_dialog::display(_("Event error"),
149                     sprintf(_("Event '%s' is not available!"),$action),ERROR_DIALOG);
150       }
151     }
153     /* Base select dialog */
154     $once = true;
155     foreach($_POST as $name => $value){
156       if(preg_match("/^chooseBase/",$name) && $once ){
157         $once = false;
158         $this->dialog = new baseSelectDialog($this->config,$this,$this->get_allowed_bases());
159         $this->dialog->setCurrentBase($this->base);
160       }
161     }
163     /* Dialog handling */
164     if(is_object($this->dialog)){
165       /* Must be called before save_object */
166       $this->dialog->save_object();
168       if($this->dialog->isClosed()){
169         $this->dialog = false;
170       }elseif($this->dialog->isSelected()){
171   
172         /* Only accept allowed bases */
173         $tmp = $this->get_allowed_bases();
174         if($tmp[$this->dialog->isSelected()]){
175           $this->base = $this->dialog->isSelected();
176         }
177         $this->dialog= false;
178       }else{
179         return($this->dialog->execute());
180       }
181     }
183     /* Fill templating stuff */
184     $smarty= get_smarty();
186     $tmp = $this->plInfo();
187     foreach($tmp['plProvidedAcls'] as $name => $translated){
188       $smarty->assign($name."ACL",$this->getacl($name));
189     }
191     /* Assign base ACL */
192     $smarty->assign("bases"   , $this->get_allowed_bases());
194     /* Assign attributes */
195     foreach ($this->attributes as $attr){
196       $smarty->assign("$attr", $this->$attr);
197     }
199     $smarty->assign("staticAddress", "");
200     $smarty->assign("base_select", $this->base);
202     /* Assign status */
203     if (gosaSupportDaemon::ping($this->netConfigDNS->macAddress)){
204       $smarty->assign("actions", array("halt" => _("Switch off"), "reboot" => _("Reboot"),
205                                        "update" => _("System update"),
206                                        "reinstall" => _("Reinstall"),
207                                        "rescan" => _("Rescan hardware"),
208                                        "memcheck" => _("Memory test"),
209                                        "localboot" => _("Force localboot"),
210                                        "sysinfo"  => _("System analysis")));
211     } else {
212       $smarty->assign("actions", array("wake" => _("Wake up"),
213                                        "reinstall" => _("Reinstall"),
214                                        "update" => _("System update"),
215                                        "memcheck" => _("Memory test"),
216                                        "localboot" => _("Force localboot"),
217                                        "sysinfo"  => _("System analysis")));
218     }
220     /* Show main page */
221     $smarty->assign("fai_activated",$this->fai_activated);
223     $str = $this->netConfigDNS->execute();
224     if(is_object($this->netConfigDNS->dialog)){
225       return($str);
226     }
227     $smarty->assign("netconfig", $str);
228     $smarty->assign("modes", $this->modes);
229     $smarty->assign("currently_installing", $this->currently_installing);
231     return($smarty->fetch (get_template_path('server.tpl', TRUE)));
232   }
234   function remove_from_parent()
235   {
236     /* Cancel if there's nothing to do here */
237     if ((!$this->initially_was_account) || (!$this->acl_is_removeable())){
238       return;
239     }
241     $this->netConfigDNS->remove_from_parent();
242     $ldap= $this->config->get_ldap_link();
243     $ldap->rmdir($this->dn);
245     new log("remove","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
247     if (!$ldap->success()){
248       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_MOD, get_class()));
249     }
251     /* Delete references to object groups */
252     $ldap->cd ($this->config->current['BASE']);
253     $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".LDAP::prepare4filter($this->dn)."))", array("cn"));
254     while ($ldap->fetch()){
255       $og= new ogroup($this->config, $ldap->getDN());
256       unset($og->member[$this->dn]);
257       $og->save ();
258     }
260     /* Clean queue form entries with this mac 
261      */
262     if(class_available("gosaSupportDaemon") && tests::is_mac($this->netConfigDNS->orig_macAddress)){
263       $q = new gosaSupportDaemon();
264       $q->clean_queue_from_mac($this->netConfigDNS->orig_macAddress);
265     }
266     $this->handle_post_events("remove",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
267   }
270   /* Save data to object */
271   function save_object()
272   {
273     /* Save current base, to be able to revert to last base, 
274         if new base is invalid or not allowed to be selected */
275     $base_tmp = $this->base;
276     plugin::save_object();
277     $this->netConfigDNS->save_object();
279     /* Get base selection */
280     $tmp = $this->get_allowed_bases();
281     if(isset($_POST['base'])){
282       if($tmp[$_POST['base']]){
283         $this->base = $_POST['base'];
284       }else{
285         $this->base = $base_tmp;
286       }
287     }
288   }
291   /* Check supplied data */
292   function check()
293   {
294     /* Call common method to give check the hook */
295     $message= plugin::check();
296     $message= array_merge($message, $this->netConfigDNS->check());
297     $this->dn= "cn=".$this->cn.",".get_ou('serverou').$this->base;
299     /* must: cn */
300     if ($this->cn == ""){
301       msgPool::required(_("Server name"));
302     }
303     
304     /* Check if given name is a valid host/dns name */
305     if(!tests::is_dns_name($this->cn)){
306       $message[]= msgPool::invalid(_("Server name"), $this->cn, "/[a-z0-9\.\-]/i");
307     }
309     if ($this->orig_dn != $this->dn){
310       $ldap= $this->config->get_ldap_link();
311       $ldap->cd ($this->base);
312       $ldap->search ("(cn=".$this->cn.")", array("cn"));
313       if ($ldap->count() != 0){
314         while ($attrs= $ldap->fetch()){
315           if ($attrs['dn'] != $this->orig_dn){
316             if(!preg_match("/cn=dhcp,/",$attrs['dn']) && !preg_match("/,".get_ou('incomingou')."/",$attrs['dn']) && preg_match("/,".get_ou('serverou')."/",$attrs['dn'])){
317               $message[]= msgPool::duplicated(_("Server name"));
318               break;
319             }
320           }
321         }
322       }
323     }
325     /* Warn the user, that this host is currently installing */
326     if($this->currently_installing && !$this->currently_installing_warned){
327       $this->currently_installing_warned = TRUE;
328       $message[] = _("This host is currently installing, if you really want to save it, save again.");
329     }
331     return ($message);
332   }
335   /* Save to LDAP */
336   function save()
337   {
338     /* Detect mode changes */
339     $activate= (isset($this->saved_attributes['gotoMode']) &&
340         $this->gotoMode != $this->saved_attributes['gotoMode'] &&
341         $this->gotoMode == "active" &&
342         tests::is_ip($this->netConfigDNS->ipHostNumber));
344     plugin::save();
346     /* Remove all empty values */
347     if ($this->orig_dn == 'new'){
348       $attrs= array();
349       foreach ($this->attrs as $key => $val){
350         if (is_array($val) && count($val) == 0){
351           continue;
352         }
353         $attrs[$key]= $val;
354       }
355       $this->attrs= $attrs;
356     }
358     /* Write back to ldap */
359     $ldap= $this->config->get_ldap_link();
360     if ($this->orig_dn == 'new'){
361       $ldap->cd($this->config->current['BASE']);
362       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
363       $ldap->cd($this->dn);
364       $ldap->add($this->attrs);
365       new log("create","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
366       $mode= "add";
367     } else {
368    
369       /* cn is not case sensitive for ldap, but for php it is!! */ 
370       if($this->config->current['DNMODE'] == "cn"){
371         if (strtolower($this->orig_dn) != (strtolower($this->dn))){
372           $this->recursive_move($this->orig_dn, $this->dn);
373           plugin::save();
374         }
375       }else{
376         if ($this->orig_dn != $this->dn){
377           $this->recursive_move($this->orig_dn, $this->dn);
378           plugin::save();
379         }
380       }
381   
382       $ldap->cd($this->dn);
383       $this->cleanup();
384       $ldap->modify ($this->attrs); 
385       new log("modify","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
387       $mode= "modify";
388     }
389     if (!$ldap->success()){
390       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_DEL, get_class()));
391     }
392     $this->netConfigDNS->cn = $this->cn;
393     $this->netConfigDNS->save();
395     /* Optionally execute a command after we're done */
396     $this->handle_post_events($mode,array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
398     if ($activate){
400       /* Send installation activation 
401        */
402       $events = DaemonEvent::get_event_types(SYSTEM_EVENT | HIDDEN_EVENT);
403       $o_queue = new gosaSupportDaemon();
404       if(isset($events['TRIGGERED']['DaemonEvent_installation_activation'])){
405         $evt = $events['TRIGGERED']['DaemonEvent_installation_activation'];
406         $tmp = new $evt['CLASS_NAME']($this->config);
407         $tmp->set_type(TRIGGERED_EVENT);
408         $tmp->add_targets(array($this->netConfigDNS->macAddress));
409         if(!$o_queue->append($tmp)){
410           msg_dialog::display(_("Daemon"),sprintf(_("Something went wrong while talking to the daemon: %s."),
411                 $o_queue->get_error()),ERROR_DIALOG);
412         }
413       }
414     }
416   }
419   /* Display generic part for server copy & paste */
420   function getCopyDialog()
421   {
422     $vars = array("cn");
424     $smarty = get_smarty();
425     $smarty->assign("cn" ,$this->cn);
426     $smarty->assign("object","server");
427     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
428     $ret = array();
429     $ret['string'] = $str;
430     $ret['status'] = "";
431     return($ret);
432   }
435   function saveCopyDialog()
436   {
437     if(isset($_POST['cn'])){
438       $this->cn = $_POST['cn'];
439     }
440   }
442   
443   function PrepareForCopyPaste($source)  
444   {
445     plugin::PrepareForCopyPaste($source);
446     if(isset($source['macAddress'][0])){
447       $this->netConfigDNS->macAddress = $source['macAddress'][0];
448     }
449     if(isset($source['ipHostNumber'][0])){
450       $this->netConfigDNS->ipHostNumber = $source['ipHostNumber'][0];
451     }
453   }
454   
457   /* Return plugin informations for acl handling */ 
458   static function plInfo()
459   {
460     return (array( 
461           "plShortName"   => _("Generic"),
462           "plDescription" => _("Server generic"),
463           "plSelfModify"  => FALSE,
464           "plDepends"     => array(),
465           "plPriority"    => 1,
466           "plSection"     => array("administration"),           
467           "plCategory"    => array("server" => array("description"  => _("Server"),
468                                                      "objectClass"  => "goServer")),
469           "plProvidedAcls"=> array(
470             "cn"           => _("Name"),
471             "description"  => _("Description"),
472             "gotoMode"     => _("Goto mode"),
473             "base"         => _("Base"),
474             "FAIstate"     => _("Action flag"))
475           ));
476   }
479 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
480 ?>