Code

5bf6e46ecc3f2e1a57a128790e1a6cb75d6b31db
[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         _("This 'dn' has no server features.")."</b>";
127       return($display);
128     }
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(_("Daemon"),sprintf(_("Something went wrong while talking to the daemon: %s."),
146                   $o_queue->get_error()),ERROR_DIALOG);
147           }
148         }
149       }else{
150         msg_dialog::display(_("Event error"),
151                     sprintf(_("Event '%s' is not available!"),$action),ERROR_DIALOG);
152       }
154     }
156     /* Base select dialog */
157     $once = true;
158     foreach($_POST as $name => $value){
159       if(preg_match("/^chooseBase/",$name) && $once ){
160         $once = false;
161         $this->dialog = new baseSelectDialog($this->config,$this,$this->get_allowed_bases());
162         $this->dialog->setCurrentBase($this->base);
163       }
164     }
166     /* Dialog handling */
167     if(is_object($this->dialog)){
168       /* Must be called before save_object */
169       $this->dialog->save_object();
171       if($this->dialog->isClosed()){
172         $this->dialog = false;
173       }elseif($this->dialog->isSelected()){
174   
175         /* Only accept allowed bases */
176         $tmp = $this->get_allowed_bases();
177         if($tmp[$this->dialog->isSelected()]){
178           $this->base = $this->dialog->isSelected();
179         }
180         $this->dialog= false;
181       }else{
182         return($this->dialog->execute());
183       }
184     }
186     /* Fill templating stuff */
187     $smarty= get_smarty();
189     $tmp = $this->plInfo();
190     foreach($tmp['plProvidedAcls'] as $name => $translated){
191       $smarty->assign($name."ACL",$this->getacl($name));
192     }
194     /* Assign base ACL */
195     $smarty->assign("bases"   , $this->get_allowed_bases());
197     /* Assign attributes */
198     foreach ($this->attributes as $attr){
199       $smarty->assign("$attr", $this->$attr);
200     }
202     $smarty->assign("staticAddress", "");
203     $smarty->assign("base_select", $this->base);
205     /* Assign status */
206     if (gosaSupportDaemon::ping($this->netConfigDNS->macAddress)){
207       $smarty->assign("actions", array("halt" => _("Switch off"), "reboot" => _("Reboot"),
208                                        "update" => _("System update"),
209                                        "reinstall" => _("Reinstall"),
210                                        "rescan" => _("Rescan hardware"),
211                                        "memcheck" => _("Memory test"),
212                                        "localboot" => _("Force localboot"),
213                                        "sysinfo"  => _("System analysis")));
214     } else {
215       $smarty->assign("actions", array("wake" => _("Wake up"),
216                                        "reinstall" => _("Reinstall"),
217                                        "update" => _("System update"),
218                                        "memcheck" => _("Memory test"),
219                                        "localboot" => _("Force localboot"),
220                                        "sysinfo"  => _("System analysis")));
221     }
223     /* Show main page */
224     $smarty->assign("fai_activated",$this->fai_activated);
226     $str = $this->netConfigDNS->execute();
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     return($smarty->fetch (get_template_path('server.tpl', TRUE)));
235   }
237   function remove_from_parent()
238   {
239     /* Cancel if there's nothing to do here */
240     if ((!$this->initially_was_account) || (!$this->acl_is_removeable())){
241       return;
242     }
244     $this->netConfigDNS->remove_from_parent();
245     $ldap= $this->config->get_ldap_link();
246     $ldap->rmdir($this->dn);
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){
330       $this->currently_installing_warned = TRUE;
331       $message[] = _("This host is currently installing, if you really want to save it, save again.");
332     }
334     return ($message);
335   }
338   /* Save to LDAP */
339   function save()
340   {
341     /* Detect mode changes */
342     $activate= (isset($this->saved_attributes['gotoMode']) &&
343         $this->gotoMode != $this->saved_attributes['gotoMode'] &&
344         $this->gotoMode == "active" &&
345         tests::is_ip($this->netConfigDNS->ipHostNumber));
347     plugin::save();
349     /* Remove all empty values */
350     if ($this->orig_dn == 'new'){
351       $attrs= array();
352       foreach ($this->attrs as $key => $val){
353         if (is_array($val) && count($val) == 0){
354           continue;
355         }
356         $attrs[$key]= $val;
357       }
358       $this->attrs= $attrs;
359     }
361     /* Write back to ldap */
362     $ldap= $this->config->get_ldap_link();
363     if ($this->orig_dn == 'new'){
364       $ldap->cd($this->config->current['BASE']);
365       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
366       $ldap->cd($this->dn);
367       $ldap->add($this->attrs);
368       new log("create","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
369       $mode= "add";
370     } else {
371    
372       /* cn is not case sensitive for ldap, but for php it is!! */ 
373       if($this->config->current['DNMODE'] == "cn"){
374         if (strtolower($this->orig_dn) != (strtolower($this->dn))){
375           $this->recursive_move($this->orig_dn, $this->dn);
376           plugin::save();
377         }
378       }else{
379         if ($this->orig_dn != $this->dn){
380           $this->recursive_move($this->orig_dn, $this->dn);
381           plugin::save();
382         }
383       }
384   
385       $ldap->cd($this->dn);
386       $this->cleanup();
387       $ldap->modify ($this->attrs); 
388       new log("modify","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
390       $mode= "modify";
391     }
392     if (!$ldap->success()){
393       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_DEL, get_class()));
394     }
395     $this->netConfigDNS->cn = $this->cn;
396     $this->netConfigDNS->save();
398     /* Optionally execute a command after we're done */
399     $this->handle_post_events($mode,array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
401     if ($activate){
403       /* Send installation activation 
404        */
405       $events = DaemonEvent::get_event_types(SYSTEM_EVENT | HIDDEN_EVENT);
406       $o_queue = new gosaSupportDaemon();
407       if(isset($events['TRIGGERED']['DaemonEvent_installation_activation'])){
408         $evt = $events['TRIGGERED']['DaemonEvent_installation_activation'];
409         $tmp = new $evt['CLASS_NAME']($this->config);
410         $tmp->set_type(TRIGGERED_EVENT);
411         $tmp->add_targets(array($this->netConfigDNS->macAddress));
412         if(!$o_queue->append($tmp)){
413           msg_dialog::display(_("Daemon"),sprintf(_("Something went wrong while talking to the daemon: %s."),
414                 $o_queue->get_error()),ERROR_DIALOG);
415         }
416       }
417     }
419   }
422   /* Display generic part for server copy & paste */
423   function getCopyDialog()
424   {
425     $vars = array("cn");
427     $smarty = get_smarty();
428     $smarty->assign("cn" ,$this->cn);
429     $smarty->assign("object","server");
430     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
431     $ret = array();
432     $ret['string'] = $str;
433     $ret['status'] = "";
434     return($ret);
435   }
438   function saveCopyDialog()
439   {
440     if(isset($_POST['cn'])){
441       $this->cn = $_POST['cn'];
442     }
443   }
445   
446   function PrepareForCopyPaste($source)  
447   {
448     plugin::PrepareForCopyPaste($source);
449     if(isset($source['macAddress'][0])){
450       $this->netConfigDNS->macAddress = $source['macAddress'][0];
451     }
452     if(isset($source['ipHostNumber'][0])){
453       $this->netConfigDNS->ipHostNumber = $source['ipHostNumber'][0];
454     }
456   }
457   
460   /* Return plugin informations for acl handling */ 
461   static function plInfo()
462   {
463     return (array( 
464           "plShortName"   => _("Generic"),
465           "plDescription" => _("Server generic"),
466           "plSelfModify"  => FALSE,
467           "plDepends"     => array(),
468           "plPriority"    => 1,
469           "plSection"     => array("administration"),           
470           "plCategory"    => array("server" => array("description"  => _("Server"),
471                                                      "objectClass"  => "goServer")),
472           "plProvidedAcls"=> array(
473             "cn"           => _("Name"),
474             "description"  => _("Description"),
475             "gotoMode"     => _("Goto mode"),
476             "base"         => _("Base"),
477             "FAIstate"     => _("Action flag"))
478           ));
479   }
482 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
483 ?>