Code

Apply patch for #5572
[gosa.git] / trunk / 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;
58   var $mapActions   = array("reboot"          => "",
59                             "instant_update"  => "softupdate",
60                             "localboot"       => "localboot",
61                             "update"          => "scheduledupdate",
62                             "reinstall"       => "install",
63                             "rescan"          => "",
64                             "wakeup"            => "",
65                           );
68   function servgeneric (&$config, $dn= NULL, $parent= NULL)
69   {
70     /* Check if FAI is activated */
71     $tmp = $config->search("faiManagement", "CLASS",array('menu','tabs'));
72     
73     if(!empty($tmp)){
74       $this->fai_activated = TRUE;
75     }
77     plugin::plugin ($config, $dn, $parent);
79     /* Initialize kerberos host key plugin */
80     if(class_available("krbHostKeys")){
81       $this->kerberos_key_service = new krbHostKeys($this->config,$this);
82     }
84     $this->ui = get_userinfo();
85     $this->modes["active"]= _("Activated");
86     $this->modes["locked"]= _("Locked");
88     /* Set base */
89     if ($this->dn == "new"){
90       $ui= get_userinfo();
91       $this->base= dn2base($ui->dn);
92       $this->cn= "";
93     } else {
94       $this->base= preg_replace ("/^[^,]+,".preg_quote(get_ou("serverRDN"), '/')."/", "", $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);
100     /* Check if this host is currently in installation process*/
101     if($this->dn != "new" && class_available("gosaSupportDaemon") && class_available("DaemonEvent")){
102       $o = new gosaSupportDaemon();
103       $e_types = DaemonEvent::get_event_types(USER_EVENT | SYSTEM_EVENT | HIDDEN_EVENT);
104       $evts = $o->get_entries_by_mac(array($this->netConfigDNS->macAddress));
105       foreach($evts as $evt){
106         if(isset($e_types['QUEUED'][$evt['HEADERTAG']]) && $evt['STATUS'] == "processing" && 
107             $e_types['QUEUED'][$evt['HEADERTAG']] == "DaemonEvent_reinstall"){
108           $this->currently_installing =TRUE;
109         }
110       }
111     }
112        
113     /* Save dn for later references */
114     $this->orig_dn   = $this->dn;
115     $this->orig_cn   = $this->cn;
116     $this->orig_base = $this->base;
117   }
120   function set_acl_base($base)
121   {
122     plugin::set_acl_base($base);
123     $this->netConfigDNS->set_acl_base($base);
124   }
127   function set_acl_category($cat)
128   {
129     plugin::set_acl_category($cat);
130     $this->netConfigDNS->set_acl_category($cat);
131   }
134   function execute()
135   {
136     /* Call parent execute */
137     plugin::execute();
139     if($this->is_account && !$this->view_logged){
140       $this->view_logged = TRUE;
141       new log("view","server/".get_class($this),$this->dn);
142     }
144     /* Do we represent a valid server? */
145     if (!$this->is_account && $this->parent === NULL){
146       $display= "<img alt=\"\" src=\"images/small-error.png\" align=middle>&nbsp;<b>".
147         msgPool::noValidExtension(_("server"))."</b>"; 
148       return($display);
149     }
151     if ((isset($_POST['action'])) && ($this->acl_is_writeable("FAIstate")) && isset($this->mapActions[$_POST['saction']]) ){
152       $action = $_POST['saction'];
153       
154       /* Check if we have an DaemonEvent for this action */
155       if(class_available("DaemonEvent")){
156         $events = DaemonEvent::get_event_types(SYSTEM_EVENT | HIDDEN_EVENT);
157         if(isset($events['TRIGGERED']["DaemonEvent_".$action])){
158           $evt = $events['TRIGGERED']["DaemonEvent_".$action];
159           $tmp = new $evt['CLASS_NAME']($this->config);
160           $tmp->add_targets(array($this->netConfigDNS->macAddress));
161           $tmp->set_type(TRIGGERED_EVENT);
162           $o_queue = new gosaSupportDaemon();
163           if(!$o_queue->append($tmp)){
164             msg_dialog::display(_("Service infrastructure"),msgPool::siError($o_queue->get_error()),ERROR_DIALOG);
165           }
166         }
167       }else{
168         msg_dialog::display(_("Event error"),
169                     sprintf(_("Event '%s' is not available!"),$action),ERROR_DIALOG);
170       }
171     }
173     /* Base select dialog */
174     $once = true;
175     foreach($_POST as $name => $value){
176       if(preg_match("/^chooseBase/",$name) && $once ){
177         $once = false;
178         $this->dialog = new baseSelectDialog($this->config,$this,$this->get_allowed_bases());
179         $this->dialog->setCurrentBase($this->base);
180       }
181     }
183     /* Dialog handling */
184     if(is_object($this->dialog)){
185       /* Must be called before save_object */
186       $this->dialog->save_object();
188       if($this->dialog->isClosed()){
189         $this->dialog = false;
190       }elseif($this->dialog->isSelected()){
191   
192         /* Only accept allowed bases */
193         $tmp = $this->get_allowed_bases();
194         if($tmp[$this->dialog->isSelected()]){
195           $this->base = $this->dialog->isSelected();
196         }
197         $this->dialog= false;
198       }else{
199         return($this->dialog->execute());
200       }
201     }
203     /* Fill templating stuff */
204     $smarty= get_smarty();
206     $tmp = $this->plInfo();
207     foreach($tmp['plProvidedAcls'] as $name => $translated){
208       $smarty->assign($name."ACL",$this->getacl($name));
209     }
211     /* Assign base ACL */
212     $smarty->assign("bases"   , $this->get_allowed_bases());
214     /* Assign attributes */
215     foreach ($this->attributes as $attr){
216       $smarty->assign("$attr", $this->$attr);
217     }
219     $smarty->assign("staticAddress", "");
220     $smarty->assign("base_select", $this->base);
222     /* Assign status */
223     if (gosaSupportDaemon::ping($this->netConfigDNS->macAddress)){
224       $smarty->assign("actions", array("halt" => _("Switch off"), "reboot" => _("Reboot"),
225                                        "update" => _("System update"),
226                                        "reinstall" => _("Reinstall"),
227                                        "rescan" => _("Rescan hardware"),
228                                        "localboot" => _("Force localboot"),
229                                      ));
230     } else {
231       $smarty->assign("actions", array("wakeup" => _("Wake up"),
232                                        "reinstall" => _("Reinstall"),
233                                        "update" => _("System update"),
234                                        "localboot" => _("Force localboot")
235                                      ));
236     }
238     /* Show main page */
239     $smarty->assign("fai_activated",$this->fai_activated);
241     $str = $this->netConfigDNS->execute();
242     if(is_object($this->netConfigDNS->dialog)){
243       return($str);
244     }
245     $smarty->assign("netconfig", $str);
246     $smarty->assign("modes", $this->modes);
247     $smarty->assign("currently_installing", $this->currently_installing);
249     $smarty->assign("host_key","");
250     if(is_object($this->kerberos_key_service)){
251       $smarty->assign("host_key",$this->kerberos_key_service->execute_by_prefix("host/"));
252     }
254     return($smarty->fetch (get_template_path('server.tpl', TRUE)));
255   }
257   function remove_from_parent()
258   {
259     /* Cancel if there's nothing to do here */
260     if ((!$this->initially_was_account) || (!$this->acl_is_removeable())){
261       return;
262     }
264     /* Remove kerberos key dependencies too */
265     if(is_object($this->kerberos_key_service)){
266       $this->kerberos_key_service->remove_from_parent_by_prefix("host/");
267     }
269     $this->netConfigDNS->remove_from_parent();
270     $ldap= $this->config->get_ldap_link();
271     $ldap->rmdir($this->dn);
273     update_accessTo($this->orig_cn,"");
275     new log("remove","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
277     if (!$ldap->success()){
278       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_MOD, get_class()));
279     }
281     /* Delete references to object groups */
282     $ldap->cd ($this->config->current['BASE']);
283     $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".LDAP::prepare4filter($this->dn)."))", array("cn"));
284     while ($ldap->fetch()){
285       $og= new ogroup($this->config, $ldap->getDN());
286       unset($og->member[$this->dn]);
287       $og->save ();
288     }
290     /* Clean queue form entries with this mac 
291      */
292     if(class_available("gosaSupportDaemon") && tests::is_mac($this->netConfigDNS->orig_macAddress)){
293       $q = new gosaSupportDaemon();
294       $q->clean_queue_from_mac($this->netConfigDNS->orig_macAddress);
295     }
296     $this->handle_post_events("remove",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
297   }
300   /* Save data to object */
301   function save_object()
302   {
303     /* Save current base, to be able to revert to last base, 
304         if new base is invalid or not allowed to be selected */
305     $base_tmp = $this->base;
306     plugin::save_object();
307     $this->netConfigDNS->save_object();
309     /* Get base selection */
310     $tmp = $this->get_allowed_bases();
311     if(isset($_POST['base'])){
312       if($tmp[$_POST['base']]){
313         $this->base = $_POST['base'];
314       }else{
315         $this->base = $base_tmp;
316       }
317     }
319     /* Hanle kerberos host key plugin */
320     if(is_object($this->kerberos_key_service)){
321       $this->kerberos_key_service->save_object_by_prefix("host/");
322     }
323   }
326   /* Check supplied data */
327   function check()
328   {
329     /* Call common method to give check the hook */
330     $message= plugin::check();
331     $message= array_merge($message, $this->netConfigDNS->check());
332     $this->dn= "cn=".$this->cn.",".get_ou('serverRDN').$this->base;
334     /* must: cn */
335     if ($this->cn == ""){
336       msgPool::required(_("Server name"));
337     }
338     
339     /* Check if given name is a valid host/dns name */
340     if(!tests::is_dns_name($this->cn)){
341       $message[]= msgPool::invalid(_("Server name"), $this->cn, "/[a-z0-9\.\-]/i");
342     }
344     if ($this->orig_dn != $this->dn){
345       $ldap= $this->config->get_ldap_link();
346       # Do not allow objects with the same name in other departments, either
347       $ldap->cd ($this->config->current['BASE']);
348       $ldap->search ("(cn=".$this->cn.")", array("cn", "gosaUnitTag"));
349       if ($ldap->count() != 0){
350         while ($attrs= $ldap->fetch()){
351           if ($attrs['dn'] != $this->orig_dn){
352             if(!preg_match("/cn=dhcp,/",$attrs['dn']) && !preg_match("/,".get_ou('systemIncomingRDN')."/",$attrs['dn']) && preg_match("/,".get_ou('serverRDN')."/",$attrs['dn'])){
353               if (isset($this->config->current['HONOURUNITTAGS']) &&
354                   preg_match('/true/i', $this->config->current['HONOURUNITTAGS']) &&
355                   $attrs['gosaUnitTag'][0] != $this->get_gosaUnitTag()) {
356                 # the new/moved object is in a different administrative unit, this is not a duplicate
357                 continue;
358               } else {
359                 $message[]= msgPool::duplicated(_("Server Name"));
360                 break;
361               }
362             }
363           }
364         }
365       }
366     }
368     /* Warn the user, that this host is currently installing */
369     if($this->currently_installing && !$this->currently_installing_warned && !preg_match("/".preg_quote(get_ou("systemIncomingRDN"), '/')."/",$this->orig_dn)){
371       /* Force aborting without message dialog */
372       $message[] = "";
373       $this->currently_installing_warned = TRUE;
374       msg_dialog::display(_("Software deployment"), 
375           _("This host is currently installing, if you really want to save it, press 'OK'."),
376           CONFIRM_DIALOG);
377     }
379     /* Check if we are allowed to create or move this object
380      */
381     if($this->orig_dn == "new" && !$this->acl_is_createable($this->base)){
382       $message[] = msgPool::permCreate();
383     }elseif($this->orig_dn != "new" && $this->base != $this->orig_base && !$this->acl_is_moveable($this->base)){
384       $message[] = msgPool::permMove();
385     }
387     return ($message);
388   }
391   /* Save to LDAP */
392   function save()
393   {
394     /* Detect mode changes */
395     $activate= (isset($this->saved_attributes['gotoMode']) &&
396         $this->gotoMode != $this->saved_attributes['gotoMode'] &&
397         $this->gotoMode == "active" &&
398         tests::is_ip($this->netConfigDNS->ipHostNumber));
400     plugin::save();
402     /* Remove all empty values */
403     if ($this->orig_dn == 'new'){
404       $attrs= array();
405       foreach ($this->attrs as $key => $val){
406         if (is_array($val) && count($val) == 0){
407           continue;
408         }
409         $attrs[$key]= $val;
410       }
411       $this->attrs= $attrs;
412     }
414     /* Write back to ldap */
415     $ldap= $this->config->get_ldap_link();
416     if ($this->orig_dn == 'new'){
417       $ldap->cd($this->config->current['BASE']);
418       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
419       $ldap->cd($this->dn);
420       $ldap->add($this->attrs);
421       new log("create","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
422       $mode= "add";
423     } else {
424   
425       $ldap->cd($this->dn);
426       $this->cleanup();
427       $ldap->modify ($this->attrs); 
428       new log("modify","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
430       /* Update all accessTo/trust dependencies */
431       if($this->orig_cn != $this->cn){
432         update_accessTo($this->orig_cn,$this->cn);
433       }
435       $mode= "modify";
436     }
437     if (!$ldap->success()){
438       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_DEL, get_class()));
439     }
440     $this->netConfigDNS->cn = $this->cn;
441     $this->netConfigDNS->save();
443     /* Optionally execute a command after we're done */
444     $this->handle_post_events($mode,array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
446     if ($activate){
448       /* Send installation activation 
449        */
450       $events = DaemonEvent::get_event_types(SYSTEM_EVENT | HIDDEN_EVENT);
451       $o_queue = new gosaSupportDaemon();
452       if(isset($events['TRIGGERED']['DaemonEvent_installation_activation'])){
453         $evt = $events['TRIGGERED']['DaemonEvent_installation_activation'];
454         $tmp = new $evt['CLASS_NAME']($this->config);
455         $tmp->set_type(TRIGGERED_EVENT);
456         $tmp->add_targets(array($this->netConfigDNS->macAddress));
457         if(!$o_queue->append($tmp)){
458           msg_dialog::display(_("Service infrastructure"),msgPool::siError($o_queue->get_error()),ERROR_DIALOG);
459         }
460       }
461     }
463   }
466   /* Display generic part for server copy & paste */
467   function getCopyDialog()
468   {
469     $vars = array("cn");
471     $smarty = get_smarty();
472     $smarty->assign("cn" ,$this->cn);
473     $smarty->assign("object","server");
474     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
475     $ret = array();
476     $ret['string'] = $str;
477     $ret['status'] = "";
478     return($ret);
479   }
482   function saveCopyDialog()
483   {
484     if(isset($_POST['cn'])){
485       $this->cn = $_POST['cn'];
486     }
487   }
489   
490   function PrepareForCopyPaste($source)  
491   {
492     plugin::PrepareForCopyPaste($source);
493     if(isset($source['macAddress'][0])){
494       $this->netConfigDNS->macAddress = $source['macAddress'][0];
495     }
496     if(isset($source['ipHostNumber'][0])){
497       $this->netConfigDNS->ipHostNumber = $source['ipHostNumber'][0];
498     }
500   }
501   
504   /* Return plugin informations for acl handling */ 
505   static function plInfo()
506   {
507     return (array( 
508           "plShortName"   => _("Generic"),
509           "plDescription" => _("Server generic"),
510           "plSelfModify"  => FALSE,
511           "plDepends"     => array(),
512           "plPriority"    => 1,
513           "plSection"     => array("administration"),           
514           "plCategory"    => array("server" => array("description"  => _("Server"),
515                                                      "objectClass"  => "goServer")),
516           "plProvidedAcls"=> array(
517             "cn"            => _("Name"),
518             "description"   => _("Description"),
519             "base"          => _("Base"),
521             "gotoMode"      => _("Goto mode"),
522             "userPassword"=> _("Root password"),
523             "FAIstate"      => _("Action flag"))
524           ));
525   }
528 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
529 ?>