Code

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