Code

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