Code

b2fcb6131ee403bf374ba07d77a51056e43cfd1b
[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(session::global_is_set("CurrentMainBase")?"cn=dummy,".session::global_get("CurrentMainBase"):$ui->dn);
90       $this->cn= "";
91     } elseif(preg_match("/".preg_quote(get_ou("systemIncomingRDN"), '/')."/i", $this->dn)){
92       $this->base= preg_replace ("/^[^,]+,".preg_quote(get_ou("systemIncomingRDN"), '/')."/i", "", $this->dn);
93     } else {
94       $this->base= preg_replace ("/^[^,]+,".preg_quote(get_ou("serverRDN"), '/')."/i", "", $this->dn);
95     }
96     $this->netConfigDNS = new termDNS($this->config,$this,$this->objectclasses);
97     $this->netConfigDNS->set_acl_category("server");
98     $this->netConfigDNS->set_acl_base($this->base);
99     $this->netConfigDNS->MACisMust =TRUE;
101     /* Initialize kerberos host key plugin */
102     if(class_available("krbHostKeys")){
103       $this->kerberos_key_service = new krbHostKeys($this->config,$this);
104     }
106     /* Check if this host is currently in installation process*/
107     if($this->dn != "new" && class_available("gosaSupportDaemon") && class_available("DaemonEvent")){
108       $o = new gosaSupportDaemon();
109       $e_types = DaemonEvent::get_event_types(USER_EVENT | SYSTEM_EVENT | HIDDEN_EVENT);
110       $evts = $o->get_entries_by_mac(array($this->netConfigDNS->macAddress));
111       foreach($evts as $evt){
112         if(isset($e_types['QUEUED'][$evt['HEADERTAG']]) && $evt['STATUS'] == "processing" && 
113             $e_types['QUEUED'][$evt['HEADERTAG']] == "DaemonEvent_reinstall"){
114           $this->currently_installing =TRUE;
115         }
116       }
117     }
118        
119     /* Save dn for later references */
120     $this->orig_dn   = $this->dn;
121     $this->orig_cn   = $this->cn;
122     $this->orig_base = $this->base;
124     /* Instanciate base selector */
125     $this->baseSelector= new baseSelector($this->get_allowed_bases(), $this->base);
126     $this->baseSelector->setSubmitButton(false);
127     $this->baseSelector->setHeight(300);
128     $this->baseSelector->update(true);
129   }
132   function set_acl_base($base)
133   {
134     plugin::set_acl_base($base);
135     $this->netConfigDNS->set_acl_base($base);
136   }
139   function set_acl_category($cat)
140   {
141     plugin::set_acl_category($cat);
142     $this->netConfigDNS->set_acl_category($cat);
143   }
146   function execute()
147   {
148     /* Call parent execute */
149     plugin::execute();
151     if($this->is_account && !$this->view_logged){
152       $this->view_logged = TRUE;
153       new log("view","server/".get_class($this),$this->dn);
154     }
156     /* Do we represent a valid server? */
157     if (!$this->is_account && $this->parent === NULL){
158       $display= "<img alt=\"\" src=\"images/small-error.png\" align=middle>&nbsp;<b>".
159         msgPool::noValidExtension(_("server"))."</b>"; 
160       return($display);
161     }
163     if ((isset($_POST['action'])) && ($this->acl_is_writeable("FAIstate")) && isset($this->mapActions[$_POST['saction']]) ){
164       $action = $_POST['saction'];
165       
166       /* Check if we have an DaemonEvent for this action */
167       if(class_available("gosaSupportDaemon") && class_available("DaemonEvent")){
168         $events = DaemonEvent::get_event_types(SYSTEM_EVENT | HIDDEN_EVENT);
169         if(isset($events['TRIGGERED']["DaemonEvent_".$action])){
170           $evt = $events['TRIGGERED']["DaemonEvent_".$action];
171           $tmp = new $evt['CLASS_NAME']($this->config);
172           $tmp->add_targets(array($this->netConfigDNS->macAddress));
173           $tmp->set_type(TRIGGERED_EVENT);
174           $o_queue = new gosaSupportDaemon();
175           if(!$o_queue->append($tmp)){
176             msg_dialog::display(_("Service infrastructure"),msgPool::siError($o_queue->get_error()),ERROR_DIALOG);
177           }
178         }
179       }else{
180         msg_dialog::display(_("Event error"),
181                     sprintf(_("Event '%s' is not available!"),$action),ERROR_DIALOG);
182       }
183     }
185     /* Fill templating stuff */
186     $smarty= get_smarty();
187     $smarty->assign("usePrototype", "true");
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("base", $this->baseSelector->render());
197     /* Assign attributes */
198     foreach ($this->attributes as $attr){
199       $smarty->assign("$attr", $this->$attr);
200     }
202     $smarty->assign("staticAddress", "");
204     /* Assign status */
205     if (gosaSupportDaemon::ping($this->netConfigDNS->macAddress)){
206       $smarty->assign("actions", array("halt" => _("Switch off"), "reboot" => _("Reboot"),
207                                        "update" => _("System update"),
208                                        "reinstall" => _("Reinstall"),
209                                        "rescan" => _("Rescan hardware"),
210                                        "memcheck" => _("Memory test"),
211                                        "localboot" => _("Force localboot"),
212                                        "sysinfo"  => _("System analysis")));
213     } else {
214       $smarty->assign("actions", array("wakeup" => _("Wake up"),
215                                        "reinstall" => _("Reinstall"),
216                                        "update" => _("System update"),
217                                        "memcheck" => _("Memory test"),
218                                        "localboot" => _("Force localboot"),
219                                        "sysinfo"  => _("System analysis")));
220     }
222     /* Show main page */
223     $smarty->assign("fai_activated",$this->fai_activated);
225     $str = $this->netConfigDNS->execute();
226     if(is_object($this->netConfigDNS->dialog)){
227       return($str);
228     }
229     $smarty->assign("netconfig", $str);
230     $smarty->assign("modes", $this->modes);
231     $smarty->assign("currently_installing", $this->currently_installing);
233     $smarty->assign("host_key","");
234     if(is_object($this->kerberos_key_service)){
235       $smarty->assign("host_key",$this->kerberos_key_service->execute_by_prefix("host/"));
236     }
238     return($smarty->fetch (get_template_path('server.tpl', TRUE)));
239   }
241   function remove_from_parent()
242   {
243     /* Cancel if there's nothing to do here */
244     if ((!$this->initially_was_account) || (!$this->acl_is_removeable())){
245       return;
246     }
248     /* Remove kerberos key dependencies too */
249     if(is_object($this->kerberos_key_service)){
250       $this->kerberos_key_service->remove_from_parent_by_prefix("host/");
251     }
253     $this->netConfigDNS->remove_from_parent();
254     $ldap= $this->config->get_ldap_link();
255     $ldap->rmdir($this->dn);
257     update_accessTo($this->orig_cn,"");
259     new log("remove","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
261     if (!$ldap->success()){
262       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_MOD, get_class()));
263     }
265     /* Delete references to object groups */
266     $ldap->cd ($this->config->current['BASE']);
267     $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".LDAP::prepare4filter($this->dn)."))", array("cn"));
268     while ($ldap->fetch()){
269       $og= new ogroup($this->config, $ldap->getDN());
270       unset($og->member[$this->dn]);
271       $og->save ();
272     }
274     /* Clean queue form entries with this mac 
275      */
276     if(class_available("gosaSupportDaemon") && tests::is_mac($this->netConfigDNS->orig_macAddress)){
277       $q = new gosaSupportDaemon();
278       $q->clean_queue_from_mac($this->netConfigDNS->orig_macAddress);
279     }
280     $this->handle_post_events("remove",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
281   }
284   /* Save data to object */
285   function save_object()
286   {
287     /* Save current base, to be able to revert to last base, 
288         if new base is invalid or not allowed to be selected */
289     $base_tmp = $this->base;
290     plugin::save_object();
291     $this->netConfigDNS->save_object();
293     /* Refresh base */
294     if ($this->acl_is_moveable($this->base)){
295       if (!$this->baseSelector->update()) {
296         msg_dialog::display(_("Error"), msgPool::permMove(), ERROR_DIALOG);
297       }
298       if ($this->base != $this->baseSelector->getBase()) {
299         $this->base= $this->baseSelector->getBase();
300         $this->is_modified= TRUE;
301       }
302     }
305     /* Hanle kerberos host key plugin */
306     if(is_object($this->kerberos_key_service)){
307       $this->kerberos_key_service->save_object_by_prefix("host/");
308     }
309   }
312   /* Check supplied data */
313   function check()
314   {
315     /* Call common method to give check the hook */
316     $message= plugin::check();
317     $message= array_merge($message, $this->netConfigDNS->check());
318     $this->dn= "cn=".$this->cn.",".get_ou('serverRDN').$this->base;
320     /* must: cn */
321     if ($this->cn == ""){
322       msgPool::required(_("Server name"));
323     }
324     
325     /* Check if given name is a valid host/dns name */
326     if(!tests::is_dns_name($this->cn)){
327       $message[]= msgPool::invalid(_("Server name"), $this->cn, "/[a-z0-9\.\-]/i");
328     }
330     // Check if a wrong base was supplied
331     if(!$this->baseSelector->checkLastBaseUpdate()){
332       $message[]= msgPool::check_base();;
333     }
335     if ($this->orig_dn != $this->dn){
336       $ldap= $this->config->get_ldap_link();
337       $ldap->cd ($this->base);
338       $ldap->search ("(cn=".$this->cn.")", array("cn"));
339       if ($ldap->count() != 0){
340         while ($attrs= $ldap->fetch()){
341           if ($attrs['dn'] != $this->orig_dn){
342             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'])){
343               $message[]= msgPool::duplicated(_("Server name"));
344               break;
345             }
346           }
347         }
348       }
349     }
351     /* Warn the user, that this host is currently installing */
352     if($this->currently_installing && !$this->currently_installing_warned && !preg_match("/".preg_quote(get_ou("systemIncomingRDN"), '/')."/i",$this->orig_dn)){
354       /* Force aborting without message dialog */
355       $message[] = "";
356       $this->currently_installing_warned = TRUE;
357       msg_dialog::display(_("Software deployment"), 
358           _("This host is currently installing, if you really want to save it, press 'OK'."),
359           CONFIRM_DIALOG);
360     }
362     /* Check if we are allowed to create or move this object
363      */
364     if($this->orig_dn == "new" && !$this->acl_is_createable($this->base)){
365       $message[] = msgPool::permCreate();
366     }elseif($this->orig_dn != "new" && $this->base != $this->orig_base && !$this->acl_is_moveable($this->base)){
367       $message[] = msgPool::permMove();
368     }
370     return ($message);
371   }
374   /* Save to LDAP */
375   function save()
376   {
377     /* Detect mode changes */
378     $activate= (isset($this->saved_attributes['gotoMode']) &&
379         $this->gotoMode != $this->saved_attributes['gotoMode'] &&
380         $this->gotoMode == "active" &&
381         tests::is_ip($this->netConfigDNS->ipHostNumber));
383     plugin::save();
385     /* Remove all empty values */
386     if ($this->orig_dn == 'new'){
387       $attrs= array();
388       foreach ($this->attrs as $key => $val){
389         if (is_array($val) && count($val) == 0){
390           continue;
391         }
392         $attrs[$key]= $val;
393       }
394       $this->attrs= $attrs;
395     }
397     /* Write back to ldap */
398     $ldap= $this->config->get_ldap_link();
399     if ($this->orig_dn == 'new'){
400       $ldap->cd($this->config->current['BASE']);
401       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
402       $ldap->cd($this->dn);
403       $ldap->add($this->attrs);
404       new log("create","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
405       $mode= "add";
406     } else {
407   
408       $ldap->cd($this->dn);
409       $this->cleanup();
410       $ldap->modify ($this->attrs); 
411       new log("modify","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
413       /* Update all accessTo/trust dependencies */
414       if($this->orig_cn != $this->cn){
415         update_accessTo($this->orig_cn,$this->cn);
416       }
418       $mode= "modify";
419     }
420     if (!$ldap->success()){
421       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_DEL, get_class()));
422     }
423     $this->netConfigDNS->cn = $this->cn;
424     $this->netConfigDNS->save();
426     /* Optionally execute a command after we're done */
427     $this->handle_post_events($mode,array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
429     if($activate && class_available("gosaSupportDaemon") && class_available("DaemonEvent")){
431       /* Send installation activation 
432        */
433       $events = DaemonEvent::get_event_types(SYSTEM_EVENT | HIDDEN_EVENT);
434       $o_queue = new gosaSupportDaemon();
435       if(isset($events['TRIGGERED']['DaemonEvent_installation_activation'])){
436         $evt = $events['TRIGGERED']['DaemonEvent_installation_activation'];
437         $tmp = new $evt['CLASS_NAME']($this->config);
438         $tmp->set_type(TRIGGERED_EVENT);
439         $tmp->add_targets(array($this->netConfigDNS->macAddress));
440         if(!$o_queue->append($tmp)){
441           msg_dialog::display(_("Service infrastructure"),msgPool::siError($o_queue->get_error()),ERROR_DIALOG);
442         }
443       }
444     }
446   }
449   /* Display generic part for server copy & paste */
450   function getCopyDialog()
451   {
452     $vars = array("cn");
454     $smarty = get_smarty();
455     $smarty->assign("cn" ,$this->cn);
456     $smarty->assign("object","server");
457     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
458     $ret = array();
459     $ret['string'] = $str;
460     $ret['status'] = "";
461     return($ret);
462   }
465   function saveCopyDialog()
466   {
467     if(isset($_POST['cn'])){
468       $this->cn = $_POST['cn'];
469     }
470   }
472   
473   function PrepareForCopyPaste($source)  
474   {
475     plugin::PrepareForCopyPaste($source);
476     if(isset($source['macAddress'][0])){
477       $this->netConfigDNS->macAddress = $source['macAddress'][0];
478     }
479     if(isset($source['ipHostNumber'][0])){
480       $this->netConfigDNS->ipHostNumber = $source['ipHostNumber'][0];
481     }
483   }
484   
487   /* Return plugin informations for acl handling */ 
488   static function plInfo()
489   {
490     return (array( 
491           "plShortName"   => _("Generic"),
492           "plDescription" => _("Server generic"),
493           "plSelfModify"  => FALSE,
494           "plDepends"     => array(),
495           "plPriority"    => 1,
496           "plSection"     => array("administration"),           
497           "plCategory"    => array("server" => array("description"  => _("Server"),
498                                                      "objectClass"  => "goServer")),
499           "plProvidedAcls"=> array(
500             "cn"            => _("Name"),
501             "description"   => _("Description"),
502             "base"          => _("Base"),
504             "gotoMode"      => _("Goto mode"),
505             "userPassword"=> _("Root password"),
506             "FAIstate"      => _("Action flag"))
507           ));
508   }
511   function is_modal_dialog()
512   {
513     return((isset($this->dialog) && $this->dialog) || (isset($this->netConfigDNS->dialog) && $this->netConfigDNS->dialog));
514   }
518 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
519 ?>