Code

Updated generic terminal services
[gosa.git] / gosa-plugins / goto / admin / systems / goto / class_terminalGeneric.inc
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= "disabled";
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["disabled"]= _("Locked");
121     $this->modes["enabled"]= _("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>&nbsp;<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();
295     
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     }
338   
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     }
348     
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);
377   
378         new log("remove","terminal/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
379   
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     }
415     
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     }
422     
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     plugin::save();
505     /* Strip out 'default' values */
506     foreach (array("gotoTerminalPath", "gotoSwapServer", "gotoSyslogServer") as $val){
507       if(isset($this->attrs[$val])){
508         if ($this->attrs[$val] == "default"){
509           $this->attrs[$val]= array();
510         }
511       }
512     }
514     /* Add missing arrays */
515     foreach (array("ghScsiDev", "ghIdeDev", "ghNetNic") as $val){
516       if (isset ($this->$val) && count ($this->$val) != 0){
517         $this->attrs["$val"]= $this->$val;
518       }
519     }
521     /* Remove all empty values */
522     if ($this->orig_dn == 'new'){
523       $attrs= array();
524       foreach ($this->attrs as $key => $val){
525         if (is_array($val) && count($val) == 0){
526           continue;
527         }
528         $attrs[$key]= $val;
529       }
530       $this->attrs= $attrs;
531     }
533     /* Set ntpServers */
534     $this->attrs['gotoNtpServer'] = array();
535     if(!$this->inheritTimeServer){
536       foreach($this->gotoNtpServer as $server){
537         $this->attrs['gotoNtpServer'][] = $server;
538       }
539     }
541     /* Append gosaAdministrativeUnitTag to objectClass if gosaUnitTag isset */
542     if(($this->gosaUnitTag) && (!in_array_ics("gosaAdministrativeUnitTag",$this->attrs['objectClass']))){
543       $this->attrs['objectClass'][] = "gosaAdministrativeUnitTag";
544     }
546     /* Write back to ldap */
547     $ldap= $this->config->get_ldap_link();
548     if ($this->orig_dn == 'new'){
549       $ldap->cd($this->config->current['BASE']);
550       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
551       $ldap->cd($this->dn);
552       if (!count($this->attrs['gotoNtpServer'])){
553         unset($this->attrs['gotoNtpServer']);
554       }
555       $ldap->add($this->attrs);
556       new log("create","terminal/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
557       $this->handle_post_events("add",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
558     } else {
559       $ldap->cd($this->dn);
560       $this->cleanup();
561       $ldap->modify ($this->attrs); 
562       new log("modify","terminal/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
563       $this->handle_post_events("modify",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
564     }
565     
566     /* cn=default and macAddress=- indicates that this is a template */
567     if($this->cn == "default"){
568       $this->netConfigDNS->macAddress = "-";
569     }
571     $this->netConfigDNS->cn = $this->cn;
572     $this->netConfigDNS->save();
573     show_ldap_error($ldap->get_error(), sprintf(_("Saving of object system terminal/generic with dn '%s' failed."),$this->dn));
574   }
580   /* Display generic part for server copy & paste */
581   function getCopyDialog()
582   {
583     $vars = array("cn");
584     $smarty = get_smarty();
585     $smarty->assign("cn" ,$this->cn);
586     $smarty->assign("object","terminal");
587     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
588     $ret = array();
589     $ret['string'] = $str;
590     $ret['status'] = "";
591     return($ret);
592   }
595   function saveCopyDialog()
596   {
597     if(isset($_POST['cn'])){
598       $this->cn = $_POST['cn'];
599     }
600   }
603   function PrepareForCopyPaste($source)
604   {
605     plugin::PrepareForCopyPaste($source);
606     if(isset($source['macAddress'][0])){
607       $this->netConfigDNS->macAddress = $source['macAddress'][0];
608     }
609     if(isset($source['ipHostNumber'][0])){
610       $this->netConfigDNS->ipHostNumber = $source['ipHostNumber'][0];
611     }
613     /* Create used ntp server array */
614     $this->gotoNtpServer= array();
615     if(isset($source['gotoNtpServer'])){
616       $this->inheritTimeServer = false;
617       unset($source['gotoNtpServer']['count']);
618       foreach($source['gotoNtpServer'] as $server){
619         $this->gotoNtpServer[$server] = $server;
620       }
621     }
623     /* Set inherit checkbox state */
624     if((in_array("default",$this->gotoNtpServer)) || (count($this->gotoNtpServer)==0)){
625       $this->inheritTimeServer = true;
626       $this->gotoNtpServer=array();
627     }
628   }
631   /* Return plugin informations for acl handling */
632   static function plInfo()
633   {
634     return (array(
635           "plShortName"   => _("Terminal"),
636           "plDescription" => _("Terminal generic"),
637           "plSelfModify"  => FALSE,
638           "plDepends"     => array(),
639           "plPriority"    => 1,
640           "plSection"     => array("administration"),
641           "plCategory"    => array("terminal" => array( "description"  => _("Terminal"),
642                                                         "objectClass"  => "gotoTerminal")),
643           "plProvidedAcls"=> array(
644             "gotoMode"            => _("Mode"),
645             "gotoTerminalPath"    => _("Root server"),
646             "gotoSwapServer"      => _("Swap server"),
647             "gotoSyslogServer"    => _("Syslog server enabled"),
648             "gotoNtpServer"       => _("Ntp server settings"),
649             "base"                => _("Base"),
650             "cn"                  => _("Name"),
651             "gotoRootPasswd"      => _("Root password"),
652             "FAIstate"            => _("Action flag"))
653           ));
654   }
657   function set_everything_to_inherited()
658   {
659     $this->gotoTerminalPath  = "default";
660     $this->gotoSwapServer    = "default" ;
661     $this->gotoSyslogServer  = "default";
662     $this->inheritTimeServer = TRUE;
664     /* Set workstation service attributes to inherited */
665     if($this->member_of_ogroup && isset($this->parent->by_object['termservice'])){
666       foreach(array("gotoXKbLayout","gotoXKbModel","gotoXKbVariant",
667             "gotoXResolution","gotoXColordepth","gotoXMouseType","gotoXMouseport") as $name){
668         $this->parent->by_object['termservice']->$name = "default";
669       }
670     }
672     /* Set workstation startup attributes to inherited */
673     if($this->member_of_ogroup && isset($this->parent->by_object['termstartup'])){
674       $this->parent->by_object['termstartup']->gotoBootKernel = "default-inherited";
675       $this->parent->by_object['termstartup']->gotoLdapServer = "default-inherited";
676     }
677   }
680 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
681 ?>