1c889b7b9f7705ea49a114cb9f40e63b6418f6b0
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'));
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 }
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> <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'];
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()){
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 }
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 $ldap->cd ($this->base);
347 $ldap->search ("(cn=".$this->cn.")", array("cn"));
348 if ($ldap->count() != 0){
349 while ($attrs= $ldap->fetch()){
350 if ($attrs['dn'] != $this->orig_dn){
351 if(!preg_match("/cn=dhcp,/",$attrs['dn']) && !preg_match("/,".get_ou('systemIncomingRDN')."/",$attrs['dn']) && preg_match("/,".get_ou('serverRDN')."/",$attrs['dn'])){
352 $message[]= msgPool::duplicated(_("Server name"));
353 break;
354 }
355 }
356 }
357 }
358 }
360 /* Warn the user, that this host is currently installing */
361 if($this->currently_installing && !$this->currently_installing_warned && !preg_match("/".preg_quote(get_ou("systemIncomingRDN"), '/')."/",$this->orig_dn)){
363 /* Force aborting without message dialog */
364 $message[] = "";
365 $this->currently_installing_warned = TRUE;
366 msg_dialog::display(_("Software deployment"),
367 _("This host is currently installing, if you really want to save it, press 'OK'."),
368 CONFIRM_DIALOG);
369 }
371 /* Check if we are allowed to create or move this object
372 */
373 if($this->orig_dn == "new" && !$this->acl_is_createable($this->base)){
374 $message[] = msgPool::permCreate();
375 }elseif($this->orig_dn != "new" && $this->base != $this->orig_base && !$this->acl_is_moveable($this->base)){
376 $message[] = msgPool::permMove();
377 }
379 return ($message);
380 }
383 /* Save to LDAP */
384 function save()
385 {
386 /* Detect mode changes */
387 $activate= (isset($this->saved_attributes['gotoMode']) &&
388 $this->gotoMode != $this->saved_attributes['gotoMode'] &&
389 $this->gotoMode == "active" &&
390 tests::is_ip($this->netConfigDNS->ipHostNumber));
392 plugin::save();
394 /* Remove all empty values */
395 if ($this->orig_dn == 'new'){
396 $attrs= array();
397 foreach ($this->attrs as $key => $val){
398 if (is_array($val) && count($val) == 0){
399 continue;
400 }
401 $attrs[$key]= $val;
402 }
403 $this->attrs= $attrs;
404 }
406 /* Write back to ldap */
407 $ldap= $this->config->get_ldap_link();
408 if ($this->orig_dn == 'new'){
409 $ldap->cd($this->config->current['BASE']);
410 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
411 $ldap->cd($this->dn);
412 $ldap->add($this->attrs);
413 new log("create","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
414 $mode= "add";
415 } else {
417 $ldap->cd($this->dn);
418 $this->cleanup();
419 $ldap->modify ($this->attrs);
420 new log("modify","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
422 /* Update all accessTo/trust dependencies */
423 if($this->orig_cn != $this->cn){
424 update_accessTo($this->orig_cn,$this->cn);
425 }
427 $mode= "modify";
428 }
429 if (!$ldap->success()){
430 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_DEL, get_class()));
431 }
432 $this->netConfigDNS->cn = $this->cn;
433 $this->netConfigDNS->save();
435 /* Optionally execute a command after we're done */
436 $this->handle_post_events($mode,array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
438 if ($activate){
440 /* Send installation activation
441 */
442 $events = DaemonEvent::get_event_types(SYSTEM_EVENT | HIDDEN_EVENT);
443 $o_queue = new gosaSupportDaemon();
444 if(isset($events['TRIGGERED']['DaemonEvent_installation_activation'])){
445 $evt = $events['TRIGGERED']['DaemonEvent_installation_activation'];
446 $tmp = new $evt['CLASS_NAME']($this->config);
447 $tmp->set_type(TRIGGERED_EVENT);
448 $tmp->add_targets(array($this->netConfigDNS->macAddress));
449 if(!$o_queue->append($tmp)){
450 msg_dialog::display(_("Service infrastructure"),msgPool::siError($o_queue->get_error()),ERROR_DIALOG);
451 }
452 }
453 }
455 }
458 /* Display generic part for server copy & paste */
459 function getCopyDialog()
460 {
461 $vars = array("cn");
463 $smarty = get_smarty();
464 $smarty->assign("cn" ,$this->cn);
465 $smarty->assign("object","server");
466 $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
467 $ret = array();
468 $ret['string'] = $str;
469 $ret['status'] = "";
470 return($ret);
471 }
474 function saveCopyDialog()
475 {
476 if(isset($_POST['cn'])){
477 $this->cn = $_POST['cn'];
478 }
479 }
482 function PrepareForCopyPaste($source)
483 {
484 plugin::PrepareForCopyPaste($source);
485 if(isset($source['macAddress'][0])){
486 $this->netConfigDNS->macAddress = $source['macAddress'][0];
487 }
488 if(isset($source['ipHostNumber'][0])){
489 $this->netConfigDNS->ipHostNumber = $source['ipHostNumber'][0];
490 }
492 }
496 /* Return plugin informations for acl handling */
497 static function plInfo()
498 {
499 return (array(
500 "plShortName" => _("Generic"),
501 "plDescription" => _("Server generic"),
502 "plSelfModify" => FALSE,
503 "plDepends" => array(),
504 "plPriority" => 1,
505 "plSection" => array("administration"),
506 "plCategory" => array("server" => array("description" => _("Server"),
507 "objectClass" => "goServer")),
508 "plProvidedAcls"=> array(
509 "cn" => _("Name"),
510 "description" => _("Description"),
511 "base" => _("Base"),
513 "gotoMode" => _("Goto mode"),
514 "userPassword"=> _("Root password"),
515 "FAIstate" => _("Action flag"))
516 ));
517 }
518 }
520 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
521 ?>