1 <?php
3 class termgeneric extends plugin
4 {
5 /* CLI vars */
6 var $cli_summary= "Manage terminal base objects";
7 var $cli_description= "Some longer text\nfor help";
8 var $cli_parameters= array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
10 /* Generic terminal attributes */
11 var $gotoMode= "locked";
12 var $gotoTerminalPath= "";
13 var $gotoSwapServer= "";
14 var $gotoSyslogServer= "";
15 var $gotoSyslogServers = array();
16 var $gotoNtpServer= array();
17 var $gotoNtpServers= array();
18 var $gotoSndModule= "";
19 var $gotoFloppyEnable= "";
20 var $gotoCdromEnable= "";
21 var $ghCpuType= "-";
22 var $ghMemSize= "-";
23 var $ghUsbSupport= "-";
24 var $ghNetNic= array();
25 var $ghIdeDev= array();
26 var $ghScsiDev= array();
27 var $ghGfxAdapter= "-";
28 var $ghSoundAdapter= "-";
29 var $gotoLastUser= "-";
30 var $netConfigDNS;
31 /* Needed values and lists */
32 var $base= "";
33 var $cn= "";
34 var $orig_dn= "";
36 var $inheritTimeServer = true;
38 /* Plugin side filled */
39 var $modes= array();
41 /* attribute list for save action */
42 var $ignore_account= TRUE;
43 var $attributes= array("gotoMode", "gotoTerminalPath",
44 "gotoSwapServer", "gotoSyslogServer", "gotoNtpServer",
45 "gotoFloppyEnable", "gotoCdromEnable", "cn", "gotoSndModule",
46 "ghCpuType", "ghMemSize","ghUsbSupport",
47 "ghGfxAdapter", "ghSoundAdapter", "gotoLastUser");
48 var $objectclasses= array("top", "gotoTerminal", "GOhard");
50 var $mapActions = array("reboot" => "",
51 "instant_update" => "softupdate",
52 "update" => "sceduledupdate",
53 "reinstall" => "install",
54 "rescan" => "",
55 "memcheck" => "memcheck",
56 "sysinfo" => "sysinfo");
58 var $fai_activated = FALSE;
59 var $view_logged = FALSE;
61 var $member_of_ogroup = FALSE;
63 function termgeneric (&$config, $dn= NULL, $parent= NULL)
64 {
65 /* Check if FAI is activated */
66 $tmp= $config->search("faiManagement", "CLASS",array('menu','tabs'));
67 if(!empty($tmp)){
68 $this->fai_activated = TRUE;
69 }
71 plugin::plugin ($config, $dn, $parent);
73 if(!isset($this->parent->by_object['ogroup'])){
74 $ldap = $this->config->get_ldap_link();
75 $ldap->cd ($this->config->current['BASE']);
76 $ldap->search("(&(objectClass=gotoWorkstationTemplate)(member=".LDAP::prepare4filter($this->dn)."))",array("cn"));
77 $this->member_of_ogroup = $ldap->count() >= 1;
78 }
80 $this->netConfigDNS = new termDNS($this->config,$this,$this->objectclasses);
81 /* Read arrays */
82 foreach (array("ghNetNic", "ghIdeDev", "ghScsiDev") as $val){
83 if (!isset($this->attrs[$val])){
84 continue;
85 }
86 for ($i= 0; $i<$this->attrs[$val]['count']; $i++){
87 array_push($this->$val, $this->attrs[$val][$i]);
88 }
89 }
91 /* Create used ntp server array */
92 $this->gotoNtpServer= array();
93 if(isset($this->attrs['gotoNtpServer'])){
94 $this->inheritTimeServer = false;
95 unset($this->attrs['gotoNtpServer']['count']);
96 foreach($this->attrs['gotoNtpServer'] as $server){
97 $this->gotoNtpServer[$server] = $server;
98 }
99 }
101 /* Set inherit checkbox state */
102 if((in_array("default",$this->gotoNtpServer)) || (count($this->gotoNtpServer)==0)){
103 $this->inheritTimeServer = true;
104 $this->gotoNtpServer=array();
105 }
107 /* You can't inherit the NTP service, if we are not member in an object group */
108 if(!$this->member_of_ogroup){
109 $this->inheritTimeServer = FALSE;
110 }
112 /* Create available ntp options */
113 $this->gotoNtpServers = $this->config->data['SERVERS']['NTP'];
114 foreach($this->gotoNtpServers as $key => $server){
115 if($server == "default"){
116 unset($this->gotoNtpServers[$key]);
117 }
118 }
120 $this->modes["locked"]= _("Locked");
121 $this->modes["active"]= _("Activated");
123 /* Set base */
124 if ($this->dn == "new"){
125 $ui= get_userinfo();
126 $this->base= dn2base($ui->dn);
127 } else {
128 $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
129 }
131 /* Create an array of all Syslog servers */
132 $tmp = $this->config->data['SERVERS']['SYSLOG'];
133 foreach($tmp as $server){
134 $visible = $server;
135 if($server == "default" && $this->member_of_ogroup) {
136 $visible = "["._("inherited")."]";
137 }
138 $this->gotoSyslogServers[$server] = $visible;
139 }
141 $this->orig_dn= $this->dn;
142 }
144 function set_acl_base($base)
145 {
146 plugin::set_acl_base($base);
147 $this->netConfigDNS->set_acl_base($base);
148 }
150 function set_acl_category($cat)
151 {
152 plugin::set_acl_category($cat);
153 $this->netConfigDNS->set_acl_category($cat);
154 }
156 function execute()
157 {
158 /* Call parent execute */
159 plugin::execute();
161 if($this->is_account && !$this->view_logged){
162 $this->view_logged = TRUE;
163 new log("view","terminal/".get_class($this),$this->dn);
164 }
166 /* Do we need to flip is_account state? */
167 if (isset($_POST['modify_state'])){
168 $this->is_account= !$this->is_account;
169 }
171 if (isset($_POST['action']) && $this->acl_is_writeable("FAIstate")){
173 /* Set FAIstate */
174 if($this->fai_activated && $this->dn != "new"){
175 $ldap = $this->config->get_ldap_link();
176 $ldap->cd($this->config->current['BASE']);
177 $ldap->cat($this->dn,array("objectClass"));
178 $res = $ldap->fetch();
180 $attrs = array();
181 $attrs['FAIstate'] = "";
182 if(isset($this->mapActions[$_POST['saction']])){
183 $attrs['FAIstate'] = $this->mapActions[$_POST ['saction']];
184 }
186 for($i = 0; $i < $res['objectClass']['count'] ; $i ++){
187 $attrs['objectClass'][] = $res['objectClass'][$i];
188 }
190 if(($attrs['FAIstate'] != "") && (!in_array("FAIobject",$attrs['objectClass']))){
191 $attrs['objectClass'][] = "FAIobject";
192 }
194 if($attrs['FAIstate'] == ""){
195 #FIXME we should check if FAIobject is used anymore
196 $attrs['FAIstate'] = array();
197 }
199 $ldap->cd($this->dn);
200 $ldap->modify($attrs);
201 show_ldap_error($ldap->get_error(), sprintf(_("Saving of system terminal/generic (FAIstate) with dn '%s' failed."),$this->dn));
202 }
204 switch($_POST['saction']){
205 case 'wake':
206 $cmd= $this->config->search("termgeneric", "WAKECMD",array('tabs'));
208 if ($cmd == ""){
209 print_red(_("No WAKECMD definition found in your gosa.conf"));
210 } else {
211 exec ($cmd." ".$this->netConfigDNS->macAddress, $dummy, $retval);
212 if ($retval != 0){
213 print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
214 }
215 }
216 break;
218 case 'reboot':
219 $cmd= $this->config->search("termgeneric", "REBOOTCMD",array('tabs'));
220 if ($cmd == ""){
221 print_red(_("No REBOOTCMD definition found in your gosa.conf"));
222 } else {
223 exec ($cmd." ".$this->cn, $dummy, $retval);
224 if ($retval != 0){
225 print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
226 }
227 }
228 break;
230 case 'halt':
231 $cmd= $this->config->search("termgeneric", "HALTCMD",array('tabs'));
232 if ($cmd == ""){
233 print_red(_("No HALTCMD definition found in your gosa.conf"));
234 } else {
235 exec ($cmd." ".$this->cn, $dummy, $retval);
236 if ($retval != 0){
237 print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
238 }
239 }
240 break;
241 }
242 }
244 /* Base select dialog */
245 $once = true;
246 foreach($_POST as $name => $value){
247 if(preg_match("/^chooseBase/",$name) && $once && $this->acl_is_moveable()){
248 $once = false;
249 $this->dialog = new baseSelectDialog($this->config,$this,$this->get_allowed_bases());
250 $this->dialog->setCurrentBase($this->base);
251 }
252 }
254 /* Dialog handling */
255 if(is_object($this->dialog)){
256 /* Must be called before save_object */
257 $this->dialog->save_object();
259 if($this->dialog->isClosed()){
260 $this->dialog = false;
261 }elseif($this->dialog->isSelected()){
263 /* A new base was selected, check if it is a valid one */
264 $tmp = $this->get_allowed_bases();
265 if(isset($tmp[$this->dialog->isSelected()])){
266 $this->base = $this->dialog->isSelected();
267 }
268 $this->dialog= false;
269 }else{
270 return($this->dialog->execute());
271 }
272 }
274 /* Do we represent a valid terminal? */
275 if (!$this->is_account && $this->parent === NULL){
276 $display= "<img alt=\"\" src=\"images/stop.png\" align=middle> <b>".
277 _("This 'dn' has no terminal features.")."</b>";
278 return($display);
279 }
281 /* Add new ntp Server to our list */
282 if((isset($_POST['addNtpServer'])) && (isset($_POST['gotoNtpServers'])) && $this->acl_is_writeable("gotoNtpServer")){
283 $this->gotoNtpServer[$_POST['gotoNtpServers']] = $_POST['gotoNtpServers'];
284 }
286 /* Delete selected NtpServer for list of used servers */
287 if((isset($_POST['delNtpServer'])) && (isset($_POST['gotoNtpServerSelected'])) && $this->acl_is_writeable("gotoNtpServer")){
288 foreach($_POST['gotoNtpServerSelected'] as $name){
289 unset($this->gotoNtpServer[$name]);
290 }
291 }
293 /* Fill templating stuff */
294 $smarty= get_smarty();
296 $tmp = $this->plInfo();
297 foreach($tmp['plProvidedAcls'] as $name => $translation){
298 $smarty->assign($name."ACL",$this->getacl($name));
299 }
301 $smarty->assign("cn", $this->cn);
302 $smarty->assign("staticAddress", "");
304 $smarty->assign("bases", $this->get_allowed_bases());
306 /* tell smarty the inherit checkbox state */
307 $smarty->assign("inheritTimeServer",$this->inheritTimeServer);
309 /* Check if terminal is online */
310 $query= "fping -q -r 1 -t 500 ".$this->cn;
311 exec ($query, $dummy, $retval);
313 /* Offline */
314 if ($retval == 0){
315 $smarty->assign("actions", array( "halt" => _("Switch off"),
316 "reboot" => _("Reboot"),
317 "memcheck" => _("Memory test"),
318 "sysinfo" => _("System analysis")));
319 } else {
320 $smarty->assign("actions", array("wake" => _("Wake up"),
321 "memcheck" => _("Memory test"),
322 "sysinfo" => _("System analysis")));
323 }
325 /* Arrays */
326 $smarty->assign("modes", $this->modes);
328 $tmp2 = array();
329 foreach($this->config->data['SERVERS']['NFS'] as $server){
330 if($server != "default"){
331 $tmp2[$server]= $server;
332 }else{
333 if($this->member_of_ogroup){
334 $tmp2[$server]="["._("inherited")."]";
335 }
336 }
337 }
339 $smarty->assign("nfsservers", $tmp2);
340 $smarty->assign("syslogservers", $this->gotoSyslogServers);
342 $tmp = array();
343 foreach($this->gotoNtpServers as $server){
344 if(!in_array($server,$this->gotoNtpServer)){
345 $tmp[$server] = $server;
346 }
347 }
349 $smarty->assign("ntpservers", $tmp);
350 $smarty->assign("fai_activated",$this->fai_activated);
352 /* Variables */
353 foreach(array("base", "gotoMode", "gotoTerminalPath", "gotoSwapServer","gotoSyslogServer", "gotoNtpServer") as $val){
354 $smarty->assign($val."_select", $this->$val);
355 }
357 $smarty->assign("member_of_ogroup",$this->member_of_ogroup);
359 /* Show main page */
360 $str = $this->netConfigDNS->execute();
361 if(is_object($this->netConfigDNS->dialog)){
362 return($str);
363 }
364 $smarty->assign("netconfig", $str);
365 return($smarty->fetch (get_template_path('terminal.tpl', TRUE, dirname(__FILE__))));
366 }
368 function remove_from_parent()
369 {
370 if($this->acl_is_removeable()){
371 $ldap= $this->config->get_ldap_link();
372 $ldap->cd($this->dn);
373 $ldap->cat($this->dn, array('dn'));
374 if($ldap->count()){
375 $this->netConfigDNS->remove_from_parent();
376 $ldap->rmDir($this->dn);
378 new log("remove","terminal/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
380 show_ldap_error($ldap->get_error(), sprintf(_("Removing of object system terminal/generic with dn '%s' failed."),$this->dn));
382 /* Optionally execute a command after we're done */
383 $this->handle_post_events("remove",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
385 /* Delete references to object groups */
386 $ldap->cd ($this->config->current['BASE']);
387 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".LDAP::prepare4filter($this->dn)."))", array("cn"));
388 while ($ldap->fetch()){
389 $og= new ogroup($this->config, $ldap->getDN());
390 unset($og->member[$this->dn]);
391 $og->save ();
392 }
393 }
394 }
395 }
398 /* Save data to object */
399 function save_object()
400 {
401 /* Create a base backup and reset the
402 base directly after calling plugin::save_object();
403 Base will be set seperatly a few lines below */
404 $base_tmp = $this->base;
405 plugin::save_object();
406 $this->base = $base_tmp;
408 /* Set new base if allowed */
409 $tmp = $this->get_allowed_bases();
410 if(isset($_POST['base'])){
411 if(isset($tmp[$_POST['base']])){
412 $this->base= $_POST['base'];
413 }
414 }
416 $this->netConfigDNS->save_object();
418 /* Save terminal path to parent since it is used by termstartup, too */
419 if(isset($this->parent->by_object['termstartup'])){
420 $this->parent->by_object['termstartup']->gotoTerminalPath= $this->gotoTerminalPath;
421 }
423 if(isset($_POST['termgeneric_posted'])){
424 if(isset($_POST["inheritTimeServer"]) && $this->member_of_ogroup){
425 $this->inheritTimeServer = true;
426 }else{
427 $this->inheritTimeServer = false;
428 }
429 }
431 if(isset($_POST["inheritAll"])){
432 $this->set_everything_to_inherited();
433 }
435 }
438 /* Check supplied data */
439 function check()
440 {
441 /* Call common method to give check the hook */
442 $message= plugin::check();
444 /* Skip IP & Mac checks if this is a template */
445 if($this->cn != "default"){
446 $message= array_merge($message, $this->netConfigDNS->check());
447 }
449 /* Permissions for that base? */
450 $this->dn= "cn=".$this->cn.",".get_ou('terminalou').$this->base;
452 if ($this->cn == ""){
453 $message[]= _("The required field 'Terminal name' is not set.");
454 }
456 /* Check if given name is a valid host/dns name */
457 if(!tests::is_dns_name($this->cn) ){
458 $message[] = _("Please specify a valid name for this object.");
459 }
461 if ($this->orig_dn == 'new'){
462 $ldap= $this->config->get_ldap_link();
463 $ldap->cd ($this->base);
465 /* It is possible to have a 'default' terminal on every base */
466 if($this->cn == "default"){
467 $ldap->cat($this->dn);
468 }else{
469 $ldap->search ("(&(objectClass=gotoTerminal)(cn=".$this->cn."))", array("cn"));
470 }
471 if ($ldap->count() != 0){
472 while ($attrs= $ldap->fetch()){
473 if (preg_match("/cn=dhcp,/",$attrs['dn']) || preg_match ("/,".normalizePreg(get_ou('incomingou'))."/", $ldap->getDN())){
474 continue;
475 } else {
476 if ($attrs['dn'] != $this->orig_dn){
477 $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
478 break;
479 }
480 }
481 }
482 }
483 }
485 /* Check for valid ntpServer selection */
486 if((!$this->inheritTimeServer) && (!count($this->gotoNtpServer))){
487 $message[]= _("There must be at least one NTP server selected, or the inherit mode activated.");
488 }
490 return ($message);
491 }
494 /* Save to LDAP */
495 function save()
496 {
498 /* Move object if requested */
499 if( $this->orig_dn != 'new' && $this->dn != $this->orig_dn){
500 $this->move($this->orig_dn, $this->dn);
501 }
503 /* Detect mode changes */
504 $activate= (isset($this->saved_attributes['gotoMode']) &&
505 $this->gotoMode != $this->saved_attributes['gotoMode'] &&
506 $this->gotoMode == "enabled" &&
507 tests::is_ip($this->netConfigDNS->ipHostNumber));
509 plugin::save();
511 /* Strip out 'default' values */
512 foreach (array("gotoTerminalPath", "gotoSwapServer", "gotoSyslogServer") as $val){
513 if(isset($this->attrs[$val])){
514 if ($this->attrs[$val] == "default"){
515 $this->attrs[$val]= array();
516 }
517 }
518 }
520 /* Add missing arrays */
521 foreach (array("ghScsiDev", "ghIdeDev", "ghNetNic") as $val){
522 if (isset ($this->$val) && count ($this->$val) != 0){
523 $this->attrs["$val"]= $this->$val;
524 }
525 }
527 /* Remove all empty values */
528 if ($this->orig_dn == 'new'){
529 $attrs= array();
530 foreach ($this->attrs as $key => $val){
531 if (is_array($val) && count($val) == 0){
532 continue;
533 }
534 $attrs[$key]= $val;
535 }
536 $this->attrs= $attrs;
537 }
539 /* Set ntpServers */
540 $this->attrs['gotoNtpServer'] = array();
541 if(!$this->inheritTimeServer){
542 foreach($this->gotoNtpServer as $server){
543 $this->attrs['gotoNtpServer'][] = $server;
544 }
545 }
547 /* Append gosaAdministrativeUnitTag to objectClass if gosaUnitTag isset */
548 if(($this->gosaUnitTag) && (!in_array_ics("gosaAdministrativeUnitTag",$this->attrs['objectClass']))){
549 $this->attrs['objectClass'][] = "gosaAdministrativeUnitTag";
550 }
552 /* Write back to ldap */
553 $ldap= $this->config->get_ldap_link();
554 if ($this->orig_dn == 'new'){
555 $ldap->cd($this->config->current['BASE']);
556 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
557 $ldap->cd($this->dn);
558 if (!count($this->attrs['gotoNtpServer'])){
559 unset($this->attrs['gotoNtpServer']);
560 }
561 $ldap->add($this->attrs);
562 new log("create","terminal/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
563 $this->handle_post_events("add",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
564 } else {
565 $ldap->cd($this->dn);
566 $this->cleanup();
567 $ldap->modify ($this->attrs);
568 new log("modify","terminal/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
569 $this->handle_post_events("modify",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
570 }
572 /* cn=default and macAddress=- indicates that this is a template */
573 if($this->cn == "default"){
574 $this->netConfigDNS->macAddress = "-";
575 }
577 $this->netConfigDNS->cn = $this->cn;
578 $this->netConfigDNS->save();
579 show_ldap_error($ldap->get_error(), sprintf(_("Saving of object system terminal/generic with dn '%s' failed."),$this->dn));
581 if ($activate){
582 gosaSupportDaemon::send("gosa_set_activated_for_installation", $this->netConfigDNS->ipHostNumber);
583 }
584 }
587 /* Display generic part for server copy & paste */
588 function getCopyDialog()
589 {
590 $vars = array("cn");
591 $smarty = get_smarty();
592 $smarty->assign("cn" ,$this->cn);
593 $smarty->assign("object","terminal");
594 $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
595 $ret = array();
596 $ret['string'] = $str;
597 $ret['status'] = "";
598 return($ret);
599 }
602 function saveCopyDialog()
603 {
604 if(isset($_POST['cn'])){
605 $this->cn = $_POST['cn'];
606 }
607 }
610 function PrepareForCopyPaste($source)
611 {
612 plugin::PrepareForCopyPaste($source);
613 if(isset($source['macAddress'][0])){
614 $this->netConfigDNS->macAddress = $source['macAddress'][0];
615 }
616 if(isset($source['ipHostNumber'][0])){
617 $this->netConfigDNS->ipHostNumber = $source['ipHostNumber'][0];
618 }
620 /* Create used ntp server array */
621 $this->gotoNtpServer= array();
622 if(isset($source['gotoNtpServer'])){
623 $this->inheritTimeServer = false;
624 unset($source['gotoNtpServer']['count']);
625 foreach($source['gotoNtpServer'] as $server){
626 $this->gotoNtpServer[$server] = $server;
627 }
628 }
630 /* Set inherit checkbox state */
631 if((in_array("default",$this->gotoNtpServer)) || (count($this->gotoNtpServer)==0)){
632 $this->inheritTimeServer = true;
633 $this->gotoNtpServer=array();
634 }
635 }
638 /* Return plugin informations for acl handling */
639 static function plInfo()
640 {
641 return (array(
642 "plShortName" => _("Terminal"),
643 "plDescription" => _("Terminal generic"),
644 "plSelfModify" => FALSE,
645 "plDepends" => array(),
646 "plPriority" => 1,
647 "plSection" => array("administration"),
648 "plCategory" => array("terminal" => array( "description" => _("Terminal"),
649 "objectClass" => "gotoTerminal")),
650 "plProvidedAcls"=> array(
651 "gotoMode" => _("Mode"),
652 "gotoTerminalPath" => _("Root server"),
653 "gotoSwapServer" => _("Swap server"),
654 "gotoSyslogServer" => _("Syslog server enabled"),
655 "gotoNtpServer" => _("Ntp server settings"),
656 "base" => _("Base"),
657 "cn" => _("Name"),
658 "gotoRootPasswd" => _("Root password"),
659 "FAIstate" => _("Action flag"))
660 ));
661 }
664 function set_everything_to_inherited()
665 {
666 $this->gotoTerminalPath = "default";
667 $this->gotoSwapServer = "default" ;
668 $this->gotoSyslogServer = "default";
669 $this->inheritTimeServer = TRUE;
671 /* Set workstation service attributes to inherited */
672 if($this->member_of_ogroup && isset($this->parent->by_object['termservice'])){
673 foreach(array("gotoXKbLayout","gotoXKbModel","gotoXKbVariant",
674 "gotoXResolution","gotoXColordepth","gotoXMouseType","gotoXMouseport") as $name){
675 $this->parent->by_object['termservice']->$name = "default";
676 }
677 }
679 /* Set workstation startup attributes to inherited */
680 if($this->member_of_ogroup && isset($this->parent->by_object['termstartup'])){
681 $this->parent->by_object['termstartup']->gotoBootKernel = "default-inherited";
682 $this->parent->by_object['termstartup']->gotoLdapServer = "default-inherited";
683 }
684 }
685 }
687 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
688 ?>