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