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;
57 var $baseSelector;
59 var $mapActions = array("reboot" => "",
60 "instant_update" => "softupdate",
61 "localboot" => "localboot",
62 "update" => "sceduledupdate",
63 "reinstall" => "install",
64 "rescan" => "",
65 "wakeup" => "",
66 "memcheck" => "memcheck",
67 "sysinfo" => "sysinfo");
70 function servgeneric (&$config, $dn= NULL, $parent= NULL)
71 {
72 /* Check if FAI is activated */
73 $tmp = $config->search("faiManagement", "CLASS",array('menu','tabs'));
75 if(!empty($tmp)){
76 $this->fai_activated = TRUE;
77 }
79 plugin::plugin ($config, $dn, $parent);
81 /* Initialize */
82 $this->ui = get_userinfo();
83 $this->modes["active"]= _("Activated");
84 $this->modes["locked"]= _("Locked");
86 /* Set base */
87 if ($this->dn == "new"){
88 $ui= get_userinfo();
89 $this->base= dn2base(session::global_is_set("CurrentMainBase")?"cn=dummy,".session::global_get("CurrentMainBase"):$ui->dn);
90 $this->cn= "";
91 } elseif(preg_match("/".preg_quote(get_ou("systemIncomingRDN"), '/')."/i", $this->dn)){
92 $this->base= preg_replace ("/^[^,]+,".preg_quote(get_ou("systemIncomingRDN"), '/')."/i", "", $this->dn);
93 } else {
94 $this->base= preg_replace ("/^[^,]+,".preg_quote(get_ou("serverRDN"), '/')."/i", "", $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);
99 $this->netConfigDNS->MACisMust =TRUE;
101 /* Initialize kerberos host key plugin */
102 if(class_available("krbHostKeys")){
103 $this->kerberos_key_service = new krbHostKeys($this->config,$this);
104 }
106 /* Check if this host is currently in installation process*/
107 if($this->dn != "new" && class_available("gosaSupportDaemon") && class_available("DaemonEvent")){
108 $o = new gosaSupportDaemon();
109 $e_types = DaemonEvent::get_event_types(USER_EVENT | SYSTEM_EVENT | HIDDEN_EVENT);
110 $evts = $o->get_entries_by_mac(array($this->netConfigDNS->macAddress));
111 foreach($evts as $evt){
112 if(isset($e_types['QUEUED'][$evt['HEADERTAG']]) && $evt['STATUS'] == "processing" &&
113 $e_types['QUEUED'][$evt['HEADERTAG']] == "DaemonEvent_reinstall"){
114 $this->currently_installing =TRUE;
115 }
116 }
117 }
119 /* Save dn for later references */
120 $this->orig_dn = $this->dn;
121 $this->orig_cn = $this->cn;
122 $this->orig_base = $this->base;
124 /* Instanciate base selector */
125 $this->baseSelector= new baseSelector($this->get_allowed_bases(), $this->base);
126 $this->baseSelector->setSubmitButton(false);
127 $this->baseSelector->setHeight(300);
128 $this->baseSelector->update(true);
129 }
132 function set_acl_base($base)
133 {
134 plugin::set_acl_base($base);
135 $this->netConfigDNS->set_acl_base($base);
136 }
139 function set_acl_category($cat)
140 {
141 plugin::set_acl_category($cat);
142 $this->netConfigDNS->set_acl_category($cat);
143 }
146 function execute()
147 {
148 /* Call parent execute */
149 plugin::execute();
151 if($this->is_account && !$this->view_logged){
152 $this->view_logged = TRUE;
153 new log("view","server/".get_class($this),$this->dn);
154 }
156 /* Do we represent a valid server? */
157 if (!$this->is_account && $this->parent === NULL){
158 $display= "<img alt=\"\" src=\"images/small-error.png\" align=middle> <b>".
159 msgPool::noValidExtension(_("server"))."</b>";
160 return($display);
161 }
163 if ((isset($_POST['action'])) && ($this->acl_is_writeable("FAIstate")) && isset($this->mapActions[$_POST['saction']]) ){
164 $action = $_POST['saction'];
166 /* Check if we have an DaemonEvent for this action */
167 if(class_available("DaemonEvent")){
168 $events = DaemonEvent::get_event_types(SYSTEM_EVENT | HIDDEN_EVENT);
169 if(isset($events['TRIGGERED']["DaemonEvent_".$action])){
170 $evt = $events['TRIGGERED']["DaemonEvent_".$action];
171 $tmp = new $evt['CLASS_NAME']($this->config);
172 $tmp->add_targets(array($this->netConfigDNS->macAddress));
173 $tmp->set_type(TRIGGERED_EVENT);
174 $o_queue = new gosaSupportDaemon();
175 if(!$o_queue->append($tmp)){
176 msg_dialog::display(_("Service infrastructure"),msgPool::siError($o_queue->get_error()),ERROR_DIALOG);
177 }
178 }
179 }else{
180 msg_dialog::display(_("Event error"),
181 sprintf(_("Event '%s' is not available!"),$action),ERROR_DIALOG);
182 }
183 }
185 /* Fill templating stuff */
186 $smarty= get_smarty();
187 $smarty->assign("usePrototype", "true");
189 $tmp = $this->plInfo();
190 foreach($tmp['plProvidedAcls'] as $name => $translated){
191 $smarty->assign($name."ACL",$this->getacl($name));
192 }
194 /* Assign base ACL */
195 $smarty->assign("base", $this->baseSelector->render());
197 /* Assign attributes */
198 foreach ($this->attributes as $attr){
199 $smarty->assign("$attr", $this->$attr);
200 }
202 /* Assign status */
203 if (gosaSupportDaemon::ping($this->netConfigDNS->macAddress)){
204 $smarty->assign("actions", array("halt" => _("Switch off"), "reboot" => _("Reboot"),
205 "update" => _("System update"),
206 "reinstall" => _("Reinstall"),
207 "rescan" => _("Rescan hardware"),
208 "memcheck" => _("Memory test"),
209 "localboot" => _("Force localboot"),
210 "sysinfo" => _("System analysis")));
211 } else {
212 $smarty->assign("actions", array("wakeup" => _("Wake up"),
213 "reinstall" => _("Reinstall"),
214 "update" => _("System update"),
215 "memcheck" => _("Memory test"),
216 "localboot" => _("Force localboot"),
217 "sysinfo" => _("System analysis")));
218 }
220 /* Show main page */
221 $smarty->assign("fai_activated",$this->fai_activated);
223 $str = $this->netConfigDNS->execute();
224 if(is_object($this->netConfigDNS->dialog)){
225 return($str);
226 }
227 $smarty->assign("netconfig", $str);
228 $smarty->assign("modes", $this->modes);
229 $smarty->assign("currently_installing", $this->currently_installing);
231 $smarty->assign("host_key","");
232 if(is_object($this->kerberos_key_service)){
233 $smarty->assign("host_key",$this->kerberos_key_service->execute_by_prefix("host/"));
234 }
236 return($smarty->fetch (get_template_path('server.tpl', TRUE)));
237 }
239 function remove_from_parent()
240 {
241 /* Cancel if there's nothing to do here */
242 if ((!$this->initially_was_account) || (!$this->acl_is_removeable())){
243 return;
244 }
246 /* Remove kerberos key dependencies too */
247 if(is_object($this->kerberos_key_service)){
248 $this->kerberos_key_service->remove_from_parent_by_prefix("host/");
249 }
251 $this->netConfigDNS->remove_from_parent();
252 $ldap= $this->config->get_ldap_link();
253 $ldap->rmdir($this->dn);
255 update_accessTo($this->orig_cn,"");
257 new log("remove","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
259 if (!$ldap->success()){
260 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_MOD, get_class()));
261 }
263 /* Delete references to object groups */
264 $ldap->cd ($this->config->current['BASE']);
265 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".LDAP::prepare4filter($this->dn)."))", array("cn"));
266 while ($ldap->fetch()){
267 $og= new ogroup($this->config, $ldap->getDN());
268 unset($og->member[$this->dn]);
269 $og->save ();
270 }
272 /* Clean queue form entries with this mac
273 */
274 if(class_available("gosaSupportDaemon") && tests::is_mac($this->netConfigDNS->orig_macAddress)){
275 $q = new gosaSupportDaemon();
276 $q->clean_queue_from_mac($this->netConfigDNS->orig_macAddress);
277 }
278 $this->handle_post_events("remove",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
279 }
282 /* Save data to object */
283 function save_object()
284 {
285 /* Save current base, to be able to revert to last base,
286 if new base is invalid or not allowed to be selected */
287 $base_tmp = $this->base;
288 plugin::save_object();
289 $this->netConfigDNS->save_object();
291 /* Refresh base */
292 if ($this->acl_is_moveable($this->base)){
293 if (!$this->baseSelector->update()) {
294 msg_dialog::display(_("Error"), msgPool::permMove(), ERROR_DIALOG);
295 }
296 if ($this->base != $this->baseSelector->getBase()) {
297 $this->base= $this->baseSelector->getBase();
298 $this->is_modified= TRUE;
299 }
300 }
303 /* Hanle kerberos host key plugin */
304 if(is_object($this->kerberos_key_service)){
305 $this->kerberos_key_service->save_object_by_prefix("host/");
306 }
307 }
310 /* Check supplied data */
311 function check()
312 {
313 /* Call common method to give check the hook */
314 $message= plugin::check();
315 $message= array_merge($message, $this->netConfigDNS->check());
316 $this->dn= "cn=".$this->cn.",".get_ou('serverRDN').$this->base;
318 /* must: cn */
319 if ($this->cn == ""){
320 msgPool::required(_("Server name"));
321 }
323 /* Check if given name is a valid host/dns name */
324 if(!tests::is_dns_name($this->cn)){
325 $message[]= msgPool::invalid(_("Server name"), $this->cn, "/[a-z0-9\.\-]/i");
326 }
328 // Check if a wrong base was supplied
329 if(!$this->baseSelector->checkLastBaseUpdate()){
330 $message[]= msgPool::check_base();;
331 }
333 if ($this->orig_dn != $this->dn){
334 $ldap= $this->config->get_ldap_link();
335 $ldap->cd ($this->base);
336 $ldap->search ("(cn=".$this->cn.")", array("cn"));
337 if ($ldap->count() != 0){
338 while ($attrs= $ldap->fetch()){
339 if ($attrs['dn'] != $this->orig_dn){
340 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'])){
341 $message[]= msgPool::duplicated(_("Server name"));
342 break;
343 }
344 }
345 }
346 }
347 }
349 /* Warn the user, that this host is currently installing */
350 if($this->currently_installing && !$this->currently_installing_warned && !preg_match("/".preg_quote(get_ou("systemIncomingRDN"), '/')."/i",$this->orig_dn)){
352 /* Force aborting without message dialog */
353 $message[] = "";
354 $this->currently_installing_warned = TRUE;
355 msg_dialog::display(_("Software deployment"),
356 _("This host is currently installing, if you really want to save it, press 'OK'."),
357 CONFIRM_DIALOG);
358 }
360 /* Check if we are allowed to create or move this object
361 */
362 if($this->orig_dn == "new" && !$this->acl_is_createable($this->base)){
363 $message[] = msgPool::permCreate();
364 }elseif($this->orig_dn != "new" && $this->base != $this->orig_base && !$this->acl_is_moveable($this->base)){
365 $message[] = msgPool::permMove();
366 }
368 return ($message);
369 }
372 /* Save to LDAP */
373 function save()
374 {
375 /* Detect mode changes */
376 $activate= (isset($this->saved_attributes['gotoMode']) &&
377 $this->gotoMode != $this->saved_attributes['gotoMode'] &&
378 $this->gotoMode == "active" &&
379 tests::is_ip($this->netConfigDNS->ipHostNumber));
381 plugin::save();
383 /* Remove all empty values */
384 if ($this->orig_dn == 'new'){
385 $attrs= array();
386 foreach ($this->attrs as $key => $val){
387 if (is_array($val) && count($val) == 0){
388 continue;
389 }
390 $attrs[$key]= $val;
391 }
392 $this->attrs= $attrs;
393 }
395 /* Write back to ldap */
396 $ldap= $this->config->get_ldap_link();
397 if ($this->orig_dn == 'new'){
398 $ldap->cd($this->config->current['BASE']);
399 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
400 $ldap->cd($this->dn);
401 $ldap->add($this->attrs);
402 new log("create","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
403 $mode= "add";
404 } else {
406 $ldap->cd($this->dn);
407 $this->cleanup();
408 $ldap->modify ($this->attrs);
409 new log("modify","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
411 /* Update all accessTo/trust dependencies */
412 if($this->orig_cn != $this->cn){
413 update_accessTo($this->orig_cn,$this->cn);
414 }
416 $mode= "modify";
417 }
418 if (!$ldap->success()){
419 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_DEL, get_class()));
420 }
421 $this->netConfigDNS->cn = $this->cn;
422 $this->netConfigDNS->save();
424 /* Optionally execute a command after we're done */
425 $this->handle_post_events($mode,array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
427 if ($activate){
429 /* Send installation activation
430 */
431 $events = DaemonEvent::get_event_types(SYSTEM_EVENT | HIDDEN_EVENT);
432 $o_queue = new gosaSupportDaemon();
433 if(isset($events['TRIGGERED']['DaemonEvent_installation_activation'])){
434 $evt = $events['TRIGGERED']['DaemonEvent_installation_activation'];
435 $tmp = new $evt['CLASS_NAME']($this->config);
436 $tmp->set_type(TRIGGERED_EVENT);
437 $tmp->add_targets(array($this->netConfigDNS->macAddress));
438 if(!$o_queue->append($tmp)){
439 msg_dialog::display(_("Service infrastructure"),msgPool::siError($o_queue->get_error()),ERROR_DIALOG);
440 }
441 }
442 }
444 }
447 /* Display generic part for server copy & paste */
448 function getCopyDialog()
449 {
450 $vars = array("cn");
452 $smarty = get_smarty();
453 $smarty->assign("cn" ,$this->cn);
454 $smarty->assign("object","server");
455 $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
456 $ret = array();
457 $ret['string'] = $str;
458 $ret['status'] = "";
459 return($ret);
460 }
463 function saveCopyDialog()
464 {
465 if(isset($_POST['cn'])){
466 $this->cn = $_POST['cn'];
467 }
468 }
471 function PrepareForCopyPaste($source)
472 {
473 plugin::PrepareForCopyPaste($source);
474 if(isset($source['macAddress'][0])){
475 $this->netConfigDNS->macAddress = $source['macAddress'][0];
476 }
477 if(isset($source['ipHostNumber'][0])){
478 $this->netConfigDNS->ipHostNumber = $source['ipHostNumber'][0];
479 }
481 }
485 /* Return plugin informations for acl handling */
486 static function plInfo()
487 {
488 return (array(
489 "plShortName" => _("Generic"),
490 "plDescription" => _("Server generic"),
491 "plSelfModify" => FALSE,
492 "plDepends" => array(),
493 "plPriority" => 1,
494 "plSection" => array("administration"),
495 "plCategory" => array("server" => array("description" => _("Server"),
496 "objectClass" => "goServer")),
497 "plProvidedAcls"=> array(
498 "cn" => _("Name"),
499 "description" => _("Description"),
500 "base" => _("Base"),
502 "gotoMode" => _("Goto mode"),
503 "userPassword"=> _("Root password"),
504 "FAIstate" => _("Action flag"))
505 ));
506 }
509 function is_modal_dialog()
510 {
511 return((isset($this->dialog) && $this->dialog) || (isset($this->netConfigDNS->dialog) && $this->netConfigDNS->dialog));
512 }
514 }
516 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
517 ?>