Code

Moved to new base selector
[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($ui->dn);
90       $this->cn= "";
91     } else {
92       $this->base= preg_replace ("/^[^,]+,".preg_quote(get_ou("serverRDN"), '/')."/i", "", $this->dn);
93     }
94     $this->netConfigDNS = new termDNS($this->config,$this,$this->objectclasses);
95     $this->netConfigDNS->set_acl_category("server");
96     $this->netConfigDNS->set_acl_base($this->base);
97     $this->netConfigDNS->MACisMust =TRUE;
99     /* Initialize kerberos host key plugin */
100     if(class_available("krbHostKeys")){
101       $this->kerberos_key_service = new krbHostKeys($this->config,$this);
102     }
104     /* Check if this host is currently in installation process*/
105     if($this->dn != "new" && class_available("gosaSupportDaemon") && class_available("DaemonEvent")){
106       $o = new gosaSupportDaemon();
107       $e_types = DaemonEvent::get_event_types(USER_EVENT | SYSTEM_EVENT | HIDDEN_EVENT);
108       $evts = $o->get_entries_by_mac(array($this->netConfigDNS->macAddress));
109       foreach($evts as $evt){
110         if(isset($e_types['QUEUED'][$evt['HEADERTAG']]) && $evt['STATUS'] == "processing" && 
111             $e_types['QUEUED'][$evt['HEADERTAG']] == "DaemonEvent_reinstall"){
112           $this->currently_installing =TRUE;
113         }
114       }
115     }
116        
117     /* Save dn for later references */
118     $this->orig_dn   = $this->dn;
119     $this->orig_cn   = $this->cn;
120     $this->orig_base = $this->base;
122     /* Instanciate base selector */
123     $this->baseSelector= new baseSelector($this->get_allowed_bases(), $this->base);
124     $this->baseSelector->setSubmitButton(false);
125     $this->baseSelector->setHeight(300);
126     $this->baseSelector->update(true);
127   }
130   function set_acl_base($base)
131   {
132     plugin::set_acl_base($base);
133     $this->netConfigDNS->set_acl_base($base);
134   }
137   function set_acl_category($cat)
138   {
139     plugin::set_acl_category($cat);
140     $this->netConfigDNS->set_acl_category($cat);
141   }
144   function execute()
145   {
146     /* Call parent execute */
147     plugin::execute();
149     if($this->is_account && !$this->view_logged){
150       $this->view_logged = TRUE;
151       new log("view","server/".get_class($this),$this->dn);
152     }
154     /* Do we represent a valid server? */
155     if (!$this->is_account && $this->parent === NULL){
156       $display= "<img alt=\"\" src=\"images/small-error.png\" align=middle>&nbsp;<b>".
157         msgPool::noValidExtension(_("server"))."</b>"; 
158       return($display);
159     }
161     if ((isset($_POST['action'])) && ($this->acl_is_writeable("FAIstate")) && isset($this->mapActions[$_POST['saction']]) ){
162       $action = $_POST['saction'];
163       
164       /* Check if we have an DaemonEvent for this action */
165       if(class_available("DaemonEvent")){
166         $events = DaemonEvent::get_event_types(SYSTEM_EVENT | HIDDEN_EVENT);
167         if(isset($events['TRIGGERED']["DaemonEvent_".$action])){
168           $evt = $events['TRIGGERED']["DaemonEvent_".$action];
169           $tmp = new $evt['CLASS_NAME']($this->config);
170           $tmp->add_targets(array($this->netConfigDNS->macAddress));
171           $tmp->set_type(TRIGGERED_EVENT);
172           $o_queue = new gosaSupportDaemon();
173           if(!$o_queue->append($tmp)){
174             msg_dialog::display(_("Service infrastructure"),msgPool::siError($o_queue->get_error()),ERROR_DIALOG);
175           }
176         }
177       }else{
178         msg_dialog::display(_("Event error"),
179                     sprintf(_("Event '%s' is not available!"),$action),ERROR_DIALOG);
180       }
181     }
183     /* Fill templating stuff */
184     $smarty= get_smarty();
185     $smarty->assign("usePrototype", "true");
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("base", $this->baseSelector->render());
195     /* Assign attributes */
196     foreach ($this->attributes as $attr){
197       $smarty->assign("$attr", $this->$attr);
198     }
200     $smarty->assign("staticAddress", "");
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("wakeup" => _("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     $smarty->assign("host_key","");
232     if(is_object($this->kerberos_key_service)){
233       $smarty->assign("host_key",$this->kerberos_key_service->execute_by_prefix("host/"));
234     }
236     return($smarty->fetch (get_template_path('server.tpl', TRUE)));
237   }
239   function remove_from_parent()
240   {
241     /* Cancel if there's nothing to do here */
242     if ((!$this->initially_was_account) || (!$this->acl_is_removeable())){
243       return;
244     }
246     /* Remove kerberos key dependencies too */
247     if(is_object($this->kerberos_key_service)){
248       $this->kerberos_key_service->remove_from_parent_by_prefix("host/");
249     }
251     $this->netConfigDNS->remove_from_parent();
252     $ldap= $this->config->get_ldap_link();
253     $ldap->rmdir($this->dn);
255     update_accessTo($this->orig_cn,"");
257     new log("remove","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
259     if (!$ldap->success()){
260       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_MOD, get_class()));
261     }
263     /* Delete references to object groups */
264     $ldap->cd ($this->config->current['BASE']);
265     $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".LDAP::prepare4filter($this->dn)."))", array("cn"));
266     while ($ldap->fetch()){
267       $og= new ogroup($this->config, $ldap->getDN());
268       unset($og->member[$this->dn]);
269       $og->save ();
270     }
272     /* Clean queue form entries with this mac 
273      */
274     if(class_available("gosaSupportDaemon") && tests::is_mac($this->netConfigDNS->orig_macAddress)){
275       $q = new gosaSupportDaemon();
276       $q->clean_queue_from_mac($this->netConfigDNS->orig_macAddress);
277     }
278     $this->handle_post_events("remove",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
279   }
282   /* Save data to object */
283   function save_object()
284   {
285     /* Save current base, to be able to revert to last base, 
286         if new base is invalid or not allowed to be selected */
287     $base_tmp = $this->base;
288     plugin::save_object();
289     $this->netConfigDNS->save_object();
291     /* Refresh base */
292     if ($this->acl_is_moveable($this->base)){
293       if (!$this->baseSelector->update()) {
294         msg_dialog::display(_("Error"), msgPool::permMove(), ERROR_DIALOG);
295       }
296       if ($this->base != $this->baseSelector->getBase()) {
297         $this->base= $this->baseSelector->getBase();
298         $this->is_modified= TRUE;
299       }
300     }
303     /* Hanle kerberos host key plugin */
304     if(is_object($this->kerberos_key_service)){
305       $this->kerberos_key_service->save_object_by_prefix("host/");
306     }
307   }
310   /* Check supplied data */
311   function check()
312   {
313     /* Call common method to give check the hook */
314     $message= plugin::check();
315     $message= array_merge($message, $this->netConfigDNS->check());
316     $this->dn= "cn=".$this->cn.",".get_ou('serverRDN').$this->base;
318     /* must: cn */
319     if ($this->cn == ""){
320       msgPool::required(_("Server name"));
321     }
322     
323     /* Check if given name is a valid host/dns name */
324     if(!tests::is_dns_name($this->cn)){
325       $message[]= msgPool::invalid(_("Server name"), $this->cn, "/[a-z0-9\.\-]/i");
326     }
328     if ($this->orig_dn != $this->dn){
329       $ldap= $this->config->get_ldap_link();
330       $ldap->cd ($this->base);
331       $ldap->search ("(cn=".$this->cn.")", array("cn"));
332       if ($ldap->count() != 0){
333         while ($attrs= $ldap->fetch()){
334           if ($attrs['dn'] != $this->orig_dn){
335             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'])){
336               $message[]= msgPool::duplicated(_("Server name"));
337               break;
338             }
339           }
340         }
341       }
342     }
344     /* Warn the user, that this host is currently installing */
345     if($this->currently_installing && !$this->currently_installing_warned && !preg_match("/".preg_quote(get_ou("systemIncomingRDN"), '/')."/i",$this->orig_dn)){
347       /* Force aborting without message dialog */
348       $message[] = "";
349       $this->currently_installing_warned = TRUE;
350       msg_dialog::display(_("Software deployment"), 
351           _("This host is currently installing, if you really want to save it, press 'OK'."),
352           CONFIRM_DIALOG);
353     }
355     /* Check if we are allowed to create or move this object
356      */
357     if($this->orig_dn == "new" && !$this->acl_is_createable($this->base)){
358       $message[] = msgPool::permCreate();
359     }elseif($this->orig_dn != "new" && $this->base != $this->orig_base && !$this->acl_is_moveable($this->base)){
360       $message[] = msgPool::permMove();
361     }
363     return ($message);
364   }
367   /* Save to LDAP */
368   function save()
369   {
370     /* Detect mode changes */
371     $activate= (isset($this->saved_attributes['gotoMode']) &&
372         $this->gotoMode != $this->saved_attributes['gotoMode'] &&
373         $this->gotoMode == "active" &&
374         tests::is_ip($this->netConfigDNS->ipHostNumber));
376     plugin::save();
378     /* Remove all empty values */
379     if ($this->orig_dn == 'new'){
380       $attrs= array();
381       foreach ($this->attrs as $key => $val){
382         if (is_array($val) && count($val) == 0){
383           continue;
384         }
385         $attrs[$key]= $val;
386       }
387       $this->attrs= $attrs;
388     }
390     /* Write back to ldap */
391     $ldap= $this->config->get_ldap_link();
392     if ($this->orig_dn == 'new'){
393       $ldap->cd($this->config->current['BASE']);
394       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
395       $ldap->cd($this->dn);
396       $ldap->add($this->attrs);
397       new log("create","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
398       $mode= "add";
399     } else {
400   
401       $ldap->cd($this->dn);
402       $this->cleanup();
403       $ldap->modify ($this->attrs); 
404       new log("modify","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
406       /* Update all accessTo/trust dependencies */
407       if($this->orig_cn != $this->cn){
408         update_accessTo($this->orig_cn,$this->cn);
409       }
411       $mode= "modify";
412     }
413     if (!$ldap->success()){
414       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_DEL, get_class()));
415     }
416     $this->netConfigDNS->cn = $this->cn;
417     $this->netConfigDNS->save();
419     /* Optionally execute a command after we're done */
420     $this->handle_post_events($mode,array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
422     if ($activate){
424       /* Send installation activation 
425        */
426       $events = DaemonEvent::get_event_types(SYSTEM_EVENT | HIDDEN_EVENT);
427       $o_queue = new gosaSupportDaemon();
428       if(isset($events['TRIGGERED']['DaemonEvent_installation_activation'])){
429         $evt = $events['TRIGGERED']['DaemonEvent_installation_activation'];
430         $tmp = new $evt['CLASS_NAME']($this->config);
431         $tmp->set_type(TRIGGERED_EVENT);
432         $tmp->add_targets(array($this->netConfigDNS->macAddress));
433         if(!$o_queue->append($tmp)){
434           msg_dialog::display(_("Service infrastructure"),msgPool::siError($o_queue->get_error()),ERROR_DIALOG);
435         }
436       }
437     }
439   }
442   /* Display generic part for server copy & paste */
443   function getCopyDialog()
444   {
445     $vars = array("cn");
447     $smarty = get_smarty();
448     $smarty->assign("cn" ,$this->cn);
449     $smarty->assign("object","server");
450     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
451     $ret = array();
452     $ret['string'] = $str;
453     $ret['status'] = "";
454     return($ret);
455   }
458   function saveCopyDialog()
459   {
460     if(isset($_POST['cn'])){
461       $this->cn = $_POST['cn'];
462     }
463   }
465   
466   function PrepareForCopyPaste($source)  
467   {
468     plugin::PrepareForCopyPaste($source);
469     if(isset($source['macAddress'][0])){
470       $this->netConfigDNS->macAddress = $source['macAddress'][0];
471     }
472     if(isset($source['ipHostNumber'][0])){
473       $this->netConfigDNS->ipHostNumber = $source['ipHostNumber'][0];
474     }
476   }
477   
480   /* Return plugin informations for acl handling */ 
481   static function plInfo()
482   {
483     return (array( 
484           "plShortName"   => _("Generic"),
485           "plDescription" => _("Server generic"),
486           "plSelfModify"  => FALSE,
487           "plDepends"     => array(),
488           "plPriority"    => 1,
489           "plSection"     => array("administration"),           
490           "plCategory"    => array("server" => array("description"  => _("Server"),
491                                                      "objectClass"  => "goServer")),
492           "plProvidedAcls"=> array(
493             "cn"            => _("Name"),
494             "description"   => _("Description"),
495             "base"          => _("Base"),
497             "gotoMode"      => _("Goto mode"),
498             "userPassword"=> _("Root password"),
499             "FAIstate"      => _("Action flag"))
500           ));
501   }
504   function is_modal_dialog()
505   {
506     return((isset($this->dialog) && $this->dialog) || (isset($this->netConfigDNS->dialog) && $this->netConfigDNS->dialog));
507   }
511 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
512 ?>