Code

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