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