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'));
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 }
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> <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'];
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()){
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());
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 $this->netConfigDNS->remove_from_parent();
257 $ldap= $this->config->get_ldap_link();
258 $ldap->rmdir($this->dn);
260 update_accessTo($this->orig_cn,"");
262 new log("remove","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
264 if (!$ldap->success()){
265 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_MOD, get_class()));
266 }
268 /* Delete references to object groups */
269 $ldap->cd ($this->config->current['BASE']);
270 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".LDAP::prepare4filter($this->dn)."))", array("cn"));
271 while ($ldap->fetch()){
272 $og= new ogroup($this->config, $ldap->getDN());
273 unset($og->member[$this->dn]);
274 $og->save ();
275 }
277 /* Clean queue form entries with this mac
278 */
279 if(class_available("gosaSupportDaemon") && tests::is_mac($this->netConfigDNS->orig_macAddress)){
280 $q = new gosaSupportDaemon();
281 $q->clean_queue_from_mac($this->netConfigDNS->orig_macAddress);
282 }
283 $this->handle_post_events("remove",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
284 }
287 /* Save data to object */
288 function save_object()
289 {
290 /* Save current base, to be able to revert to last base,
291 if new base is invalid or not allowed to be selected */
292 $base_tmp = $this->base;
293 plugin::save_object();
294 $this->netConfigDNS->save_object();
296 /* Get base selection */
297 $tmp = $this->get_allowed_bases();
298 if(isset($_POST['base'])){
299 if($tmp[$_POST['base']]){
300 $this->base = $_POST['base'];
301 }else{
302 $this->base = $base_tmp;
303 }
304 }
306 /* Hanle kerberos host key plugin */
307 if(is_object($this->kerberos_key_service)){
308 $this->kerberos_key_service->save_object();
309 }
310 }
313 /* Check supplied data */
314 function check()
315 {
316 /* Call common method to give check the hook */
317 $message= plugin::check();
318 $message= array_merge($message, $this->netConfigDNS->check());
319 $this->dn= "cn=".$this->cn.",".get_ou('serverou').$this->base;
321 /* must: cn */
322 if ($this->cn == ""){
323 msgPool::required(_("Server name"));
324 }
326 /* Check if given name is a valid host/dns name */
327 if(!tests::is_dns_name($this->cn)){
328 $message[]= msgPool::invalid(_("Server name"), $this->cn, "/[a-z0-9\.\-]/i");
329 }
331 if ($this->orig_dn != $this->dn){
332 $ldap= $this->config->get_ldap_link();
333 $ldap->cd ($this->base);
334 $ldap->search ("(cn=".$this->cn.")", array("cn"));
335 if ($ldap->count() != 0){
336 while ($attrs= $ldap->fetch()){
337 if ($attrs['dn'] != $this->orig_dn){
338 if(!preg_match("/cn=dhcp,/",$attrs['dn']) && !preg_match("/,".get_ou('incomingou')."/",$attrs['dn']) && preg_match("/,".get_ou('serverou')."/",$attrs['dn'])){
339 $message[]= msgPool::duplicated(_("Server name"));
340 break;
341 }
342 }
343 }
344 }
345 }
347 /* Warn the user, that this host is currently installing */
348 if($this->currently_installing && !$this->currently_installing_warned && !preg_match("/".normalizePreg(get_ou("incomingou"))."/",$this->orig_dn)){
350 /* Force aborting without message dialog */
351 $message[] = "";
352 $this->currently_installing_warned = TRUE;
353 msg_dialog::display(_("Software deployment"),
354 _("This host is currently installing, if you really want to save it, press 'OK'."),
355 CONFIRM_DIALOG);
356 }
358 /* Check if we are allowed to create or move this object
359 */
360 if($this->orig_dn == "new" && !$this->acl_is_createable($this->base)){
361 $message[] = msgPool::permCreate();
362 }elseif($this->orig_dn != "new" && $this->base != $this->orig_base && !$this->acl_is_moveable($this->base)){
363 $message[] = msgPool::permMove();
364 }
366 return ($message);
367 }
370 /* Save to LDAP */
371 function save()
372 {
373 /* Detect mode changes */
374 $activate= (isset($this->saved_attributes['gotoMode']) &&
375 $this->gotoMode != $this->saved_attributes['gotoMode'] &&
376 $this->gotoMode == "active" &&
377 tests::is_ip($this->netConfigDNS->ipHostNumber));
379 plugin::save();
381 /* Remove all empty values */
382 if ($this->orig_dn == 'new'){
383 $attrs= array();
384 foreach ($this->attrs as $key => $val){
385 if (is_array($val) && count($val) == 0){
386 continue;
387 }
388 $attrs[$key]= $val;
389 }
390 $this->attrs= $attrs;
391 }
393 /* Write back to ldap */
394 $ldap= $this->config->get_ldap_link();
395 if ($this->orig_dn == 'new'){
396 $ldap->cd($this->config->current['BASE']);
397 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
398 $ldap->cd($this->dn);
399 $ldap->add($this->attrs);
400 new log("create","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
401 $mode= "add";
402 } else {
404 $ldap->cd($this->dn);
405 $this->cleanup();
406 $ldap->modify ($this->attrs);
407 new log("modify","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
409 /* Update all accessTo/trust dependencies */
410 if($this->orig_cn != $this->cn){
411 update_accessTo($this->orig_cn,$this->cn);
412 }
414 $mode= "modify";
415 }
416 if (!$ldap->success()){
417 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_DEL, get_class()));
418 }
419 $this->netConfigDNS->cn = $this->cn;
420 $this->netConfigDNS->save();
422 /* Optionally execute a command after we're done */
423 $this->handle_post_events($mode,array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
425 if ($activate){
427 /* Send installation activation
428 */
429 $events = DaemonEvent::get_event_types(SYSTEM_EVENT | HIDDEN_EVENT);
430 $o_queue = new gosaSupportDaemon();
431 if(isset($events['TRIGGERED']['DaemonEvent_installation_activation'])){
432 $evt = $events['TRIGGERED']['DaemonEvent_installation_activation'];
433 $tmp = new $evt['CLASS_NAME']($this->config);
434 $tmp->set_type(TRIGGERED_EVENT);
435 $tmp->add_targets(array($this->netConfigDNS->macAddress));
436 if(!$o_queue->append($tmp)){
437 msg_dialog::display(_("Service infrastructure"),msgPool::siError($o_queue->get_error()),ERROR_DIALOG);
438 }
439 }
440 }
442 }
445 /* Display generic part for server copy & paste */
446 function getCopyDialog()
447 {
448 $vars = array("cn");
450 $smarty = get_smarty();
451 $smarty->assign("cn" ,$this->cn);
452 $smarty->assign("object","server");
453 $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
454 $ret = array();
455 $ret['string'] = $str;
456 $ret['status'] = "";
457 return($ret);
458 }
461 function saveCopyDialog()
462 {
463 if(isset($_POST['cn'])){
464 $this->cn = $_POST['cn'];
465 }
466 }
469 function PrepareForCopyPaste($source)
470 {
471 plugin::PrepareForCopyPaste($source);
472 if(isset($source['macAddress'][0])){
473 $this->netConfigDNS->macAddress = $source['macAddress'][0];
474 }
475 if(isset($source['ipHostNumber'][0])){
476 $this->netConfigDNS->ipHostNumber = $source['ipHostNumber'][0];
477 }
479 }
483 /* Return plugin informations for acl handling */
484 static function plInfo()
485 {
486 return (array(
487 "plShortName" => _("Generic"),
488 "plDescription" => _("Server generic"),
489 "plSelfModify" => FALSE,
490 "plDepends" => array(),
491 "plPriority" => 1,
492 "plSection" => array("administration"),
493 "plCategory" => array("server" => array("description" => _("Server"),
494 "objectClass" => "goServer")),
495 "plProvidedAcls"=> array(
496 "cn" => _("Name"),
497 "description" => _("Description"),
498 "gotoMode" => _("Goto mode"),
499 "base" => _("Base"),
500 "FAIstate" => _("Action flag"))
501 ));
502 }
503 }
505 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
506 ?>