Code

Moved to mac based target
[gosa.git] / gosa-plugins / goto / admin / systems / goto / class_workstationGeneric.inc
1 <?php
3 class workgeneric extends plugin
4 {
5   /* CLI vars */
6   var $cli_summary= "Manage workstation 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 $gotoSyslogServer= "";
13   var $gotoSyslogServers= array();
14   var $gotoNtpServer= array();
15   var $gotoNtpServers= array();
16   var $gotoSndModule= "";
17   var $gotoFloppyEnable= "";
18   var $gotoCdromEnable= "";
19   var $ghCpuType= "-";
20   var $ghMemSize= "-";
21   var $ghUsbSupport= "-";
22   var $ghNetNic= array();
23   var $ghIdeDev= array();
24   var $ghScsiDev= array();
25   var $ghGfxAdapter= "-";
26   var $ghSoundAdapter= "-";
27   var $gotoLastUser= "-";
28   var $FAIscript= "";
29   var $didAction= FALSE;
30   var $FAIstate= "";
31   var $view_logged = FALSE;
33   /* Needed values and lists */
34   var $base= "";
35   var $cn= "";
36   var $l= "";
37   var $orig_dn= "";
39   /* Plugin side filled */
40   var $modes= array();
42   var $netConfigDNS;
44   var $inheritTimeServer = true;
46   /* attribute list for save action */
47   var $ignore_account= TRUE;
48   var $attributes= array("gotoMode", "gotoSyslogServer", "gotoNtpServer",
49       "gotoFloppyEnable", "gotoCdromEnable", "cn", "gotoSndModule",
50       "ghCpuType", "ghMemSize", "ghUsbSupport",
51       "ghGfxAdapter", "ghSoundAdapter", "gotoLastUser", "l","FAIscript");
52   var $objectclasses= array("top", "gotoWorkstation", "GOhard");
54   var $mapActions   = array("reboot"          => "",
55                             "localboot"       => "localboot",
56                             "halt"            => "",
57                             "instant_update"  => "softupdate",
58                             "update"          => "scheduledupdate",
59                             "reinstall"       => "install",
60                             "rescan"          => "",
61                             "wake"            => "",
62                             "memcheck"        => "memcheck",
63                             "sysinfo"         => "sysinfo");
65   
66   var $fai_activated = FALSE;
68   var $member_of_ogroup = FALSE;
70   function workgeneric (&$config, $dn= NULL, $parent= NULL)
71   {
72     $tmp= $config->search("faiManagement", "CLASS",array('menu','tabs'));
73     if(!empty($tmp)){
74       $this->fai_activated = TRUE;
75     }
77     plugin::plugin ($config, $dn, $parent);
79     if(!isset($this->parent->by_object['ogroup'])){
80       $ldap = $this->config->get_ldap_link();
81       $ldap->cd ($this->config->current['BASE']);
82       $ldap->search("(&(objectClass=gotoWorkstationTemplate)(member=".LDAP::prepare4filter($this->dn)."))",array("cn"));
83       $this->member_of_ogroup = $ldap->count() >= 1;
84     }
86     $this->netConfigDNS = new termDNS($this->config,$this,$this->objectclasses);
88     /* Read arrays */
89     foreach (array("ghNetNic", "ghIdeDev", "ghScsiDev") as $val){
90       if (!isset($this->attrs[$val])){
91         continue;
92       }
93       for ($i= 0; $i<$this->attrs[$val]['count']; $i++){
94         array_push($this->$val, $this->attrs[$val][$i]);
95       }
96     }
98     /* Create used ntp server array */
99     $this->gotoNtpServer= array();
100     if(isset($this->attrs['gotoNtpServer'])){
101       $this->inheritTimeServer = false;
102       unset($this->attrs['gotoNtpServer']['count']);
103       foreach($this->attrs['gotoNtpServer'] as $server){
104         $this->gotoNtpServer[$server] = $server;
105       }
106     }
108     /* Set inherit checkbox state */
109     if((in_array("default",$this->gotoNtpServer)) || (count($this->gotoNtpServer) == 0)){
110       $this->inheritTimeServer = true;
111       $this->gotoNtpServer=array();
112     }
114     /* You can't inherit the NTP service, if we are not member in an object group */
115     if(!$this->member_of_ogroup){
116       $this->inheritTimeServer = FALSE;
117     }
119     /* Create available ntp options */
120     $tmp = $this->config->data['SERVERS']['NTP'];
121     $this->gotoNtpServers = array();
122     foreach($tmp as $key => $server){
123       if($server == "default") continue;
124       $this->gotoNtpServers[$server] = $server;
125     }
127     $this->modes["active"]= _("Activated");
128     $this->modes["locked"]= _("Locked");
130     /* Set base */
131     if ($this->dn == "new"){
132       $ui= get_userinfo();
133       $this->base= dn2base($ui->dn);
134     } else {
135       $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
136     }
138     /* Create an array of all Syslog servers */
139     $tmp = $this->config->data['SERVERS']['SYSLOG'];
140     foreach($tmp as $server){
141       $visible = $server;
142       if($server == "default" && $this->member_of_ogroup) {
143         $visible = "["._("inherited")."]";
144       }
145       $this->gotoSyslogServers[$server] = $visible;
146     }
148     /* Save 'dn' for later referal */
149     $this->orig_dn= $this->dn;
150   }
153   function set_acl_base($base)
154   {
155     plugin::set_acl_base($base);
156     $this->netConfigDNS->set_acl_base($base);
157   }
159   function set_acl_category($cat)
160   {
161     plugin::set_acl_category($cat);
162     $this->netConfigDNS->set_acl_category($cat);
163   }
165   function execute()
166   {
167     /* Call parent execute */
168     plugin::execute();
170     if($this->is_account && !$this->view_logged){
171       $this->view_logged = TRUE;
172       new log("view","workstation/".get_class($this),$this->dn);
173     }
175     /* Do we need to flip is_account state? */
176     if(isset($_POST['modify_state'])){
177       if($this->is_account && $this->acl_is_removeable()){
178         $this->is_account= FALSE;
179       }elseif(!$this->is_account && $this->acl_is_createable()){
180         $this->is_account= TRUE;
181       }
182     }
184     if ((isset($_POST['action'])) && ($this->acl_is_writeable("FAIstate")) && isset($this->mapActions[$_POST['saction']]) ){
185         gosaSupportDaemon::send("gosa_trigger_action_".$_POST['saction'], $this->netConfigDNS->macAddress, array("macAddress" => $this->netConfigDNS->macAddress));
187         if ($_POST['saction'] != "wake") {
189           /* Set FAIstate */
190           if($this->fai_activated && $this->dn != "new"){
191             $ldap = $this->config->get_ldap_link();
192             $ldap->cd($this->config->current['BASE']);
193             $ldap->cat($this->dn,array("objectClass"));
194             $res = $ldap->fetch();
196             $attrs = array();
197             $attrs['FAIstate'] = $this->FAIstate;
198             if(isset($this->mapActions[$_POST['saction']]) && $this->mapActions[$_POST['saction']] != ""){
199               $attrs['FAIstate'] = $this->mapActions[$_POST ['saction']];
200             }
202             for($i = 0; $i < $res['objectClass']['count'] ; $i ++){
203               $attrs['objectClass'][] = $res['objectClass'][$i];
204             }
206             if(($attrs['FAIstate'] != "") && (!in_array("FAIobject",$attrs['objectClass']))){
207               $attrs['objectClass'][] = "FAIobject";
208             }
210             if($attrs['FAIstate'] == ""){
211               #FIXME we should check if FAIobject is not used anymore
212               $attrs['FAIstate'] = array();
213             }
215             $ldap->cd($this->dn);
216             $ldap->modify($attrs);
217             show_ldap_error($ldap->get_error(), sprintf(_("Saving of system workstation/generic (FAIstate) with dn '%s' failed."),$this->dn));
219           }
220           $this->didAction= TRUE;
221         }
222     }
224     /* Do we represent a valid terminal? */
225     if (!$this->is_account && $this->parent === NULL){
226       $display= "<img alt=\"\" src=\"images/stop.png\" align=middle>&nbsp;<b>".
227         _("This 'dn' has no workstation features.")."</b>";
228       return($display);
229     }
231     /* Base select dialog */
232     $once = true;
233     foreach($_POST as $name => $value){
234       if(preg_match("/^chooseBase/",$name) && $once && $this->acl_is_writeable("base")){
235         $once = false;
236         $this->dialog = new baseSelectDialog($this->config,$this,$this->get_allowed_bases());
237         $this->dialog->setCurrentBase($this->base);
238       }
239     }
241     /* Dialog handling */
242     if(is_object($this->dialog)){
243       /* Must be called before save_object */
244       $this->dialog->save_object();
246       if($this->dialog->isClosed()){
247         $this->dialog = false;
248       }elseif($this->dialog->isSelected()){
250         /* A new base was selected, check if it is a valid one */
251         $tmp = $this->get_allowed_bases();
252         if(isset($tmp[$this->dialog->isSelected()])){
253           $this->base = $this->dialog->isSelected();
254         }
256         $this->dialog= false;
257       }else{
258         return($this->dialog->execute());
259       }
260     }
262     /* Add new ntp Server to our list */ 
263     if((isset($_POST['addNtpServer'])) && (isset($_POST['gotoNtpServers'])) && $this->acl_is_writeable("gotoNtpServer")){
264       $this->gotoNtpServer[$_POST['gotoNtpServers']] = $_POST['gotoNtpServers'];
265     }
267     /* Delete selected NtpServer for list of used servers  */
268     if((isset($_POST['delNtpServer'])) && (isset($_POST['gotoNtpServerSelected'])) && $this->acl_is_writeable("gotoNtpServer")){
269       foreach($_POST['gotoNtpServerSelected'] as $name){
270         unset($this->gotoNtpServer[$name]);
271       }
272     }
274     /* Fill templating stuff */
275     $smarty= get_smarty();
277     /* Set acls */
278     $tmp = $this->plInfo();
279     foreach($tmp['plProvidedAcls'] as $name => $translation){
280       $smarty->assign($name."ACL",$this->getacl($name));
281     }
283     $smarty->assign("cn", $this->cn);
284     $smarty->assign("l", $this->l);
285     $smarty->assign("bases", $this->get_allowed_bases());
286     $smarty->assign("staticAddress", "");
288     $tmp = array();
289     foreach($this->gotoNtpServers as $server){
290       if(!in_array($server,$this->gotoNtpServer)){
291         $tmp[$server] = $server;
292       }
293     }
294     $smarty->assign("gotoNtpServers",$tmp);
295         
296     /* Check if workstation is online */
297     $query= "fping -q -r 1 -t 500 ".$this->cn;
298     exec ($query, $dummy, $retval);
300     /* Offline */
301     if ($retval == 0){
302       $smarty->assign("actions", array("halt" => _("Switch off"), "reboot" => _("Reboot"),
303                                        "instant_update" => _("Instant update"),
304                                        "update" => _("Scheduled update"),
305                                        "reinstall" => _("Reinstall"),
306                                        "rescan" => _("Rescan hardware"),
307                                        "memcheck" => _("Memory test"),
308                                        "localboot" => _("Force localboot"),
309                                        "sysinfo"  => _("System analysis")));
310     } else {
311       $smarty->assign("actions", array("wake" => _("Wake up"),
312                                        "reinstall" => _("Reinstall"),
313                                        "update" => _("Scheduled update"),
314                                        "memcheck" => _("Memory test"),
315                                        "localboot" => _("Force localboot"),
316                                        "sysinfo"  => _("System analysis")));
317     }
318     /* Arrays */
319     $smarty->assign("modes", $this->modes);
320     $smarty->assign("nfsservers", $this->config->data['SERVERS']['NFS']);
321     $smarty->assign("syslogservers", $this->gotoSyslogServers);
322     $smarty->assign("fai_activated",$this->fai_activated);
324     $ntpser = array();
325     foreach($this->gotoNtpServers as $server){
326       if(!in_array($server,$this->gotoNtpServer)){
327         $ntpser[$server] = $server;
328       }
329     }
330     $smarty->assign("gotoNtpServers", $ntpser);
332     /* Variables */
333     foreach(array("base", "gotoMode", "gotoSyslogServer", "gotoNtpServer") as $val){
334       $smarty->assign($val."_select", $this->$val);
335     }
337     /* tell smarty the inherit checkbox state */
338     $smarty->assign("inheritTimeServer",$this->inheritTimeServer);
339     $smarty->assign("member_of_ogroup",$this->member_of_ogroup);
341     $str = $this->netConfigDNS->execute();
342     if(is_object($this->netConfigDNS->dialog)){
343       return($str);
344     }
345     $smarty->assign("netconfig", $str);
347     /* Show main page */
348     return($smarty->fetch (get_template_path('workstation.tpl', TRUE, dirname(__FILE__))));
349   }
351   function remove_from_parent()
352   {
353     if($this->acl_is_removeable()){
355       $this->netConfigDNS->remove_from_parent();
356       $ldap= $this->config->get_ldap_link();
357       $ldap->rmdir($this->dn);
358       new log("remove","workstation/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
359       show_ldap_error($ldap->get_error(), sprintf(_("Removing of system workstation/generic with dn '%s' failed."),$this->dn));
361       /* Optionally execute a command after we're done */
362       $this->handle_post_events("remove", array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
364       /* Delete references to object groups */
365       $ldap->cd ($this->config->current['BASE']);
366       $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".LDAP::prepare4filter($this->dn)."))", array("cn"));
367       while ($ldap->fetch()){
368         $og= new ogroup($this->config, $ldap->getDN());
369         unset($og->member[$this->dn]);
370         $og->save ();
371       }
372     }
374     if(isset($_POST["inheritAll"])){
375       $this->set_everything_to_inherited();
376     }
377   }
380   /* Save data to object */
381   function save_object()
382   {
384     /* Create a base backup and reset the
385        base directly after calling plugin::save_object();
386        Base will be set seperatly a few lines below */
387     $base_tmp = $this->base;
388     plugin::save_object();
389     $this->base = $base_tmp;
391     /* Save base, since this is no LDAP attribute */
392     $tmp = $this->get_allowed_bases();
393     if(isset($_POST['base'])){
394       if(isset($tmp[$_POST['base']])){
395         $this->base= $_POST['base'];
396       }
397     }
399     $this->netConfigDNS->save_object();
401     /* Set inherit mode */
402     if((isset($_POST['workgeneric_posted'])) && ($this->acl_is_writeable("gotoNtpServer"))){
403       if(isset($_POST["inheritTimeServer"]) && $this->member_of_ogroup){
404         $this->inheritTimeServer = true;
405       }else{
406         $this->inheritTimeServer = false;
407       }
408     }
409     
410     if(isset($_POST["inheritAll"])){
411       $this->set_everything_to_inherited();
412     }
413   }
416   /* Check supplied data */
417   function check()
418   {
419     /* Call common method to give check the hook */
420     $message= plugin::check();
421   
422     /* Skip IP & Mac checks if this is a template */
423     if($this->cn != "wdefault"){
424       $message= array_merge($message, $this->netConfigDNS->check());
425     }
427     $this->dn= "cn=".$this->cn.",".get_ou('workstationou').$this->base;
429     if ($this->cn == ""){
430       $message[]= _("The required field 'Workstation name' is not set.");
431     }
433     /* Check if given name is a valid host/dns name */
434     if(!tests::is_dns_name($this->cn)){
435       $message[] = _("Please specify a valid name for this object.");
436     }
438     if ($this->orig_dn != $this->dn){
439       $ldap= $this->config->get_ldap_link();
440       $ldap->cd ($this->base);
442       if($this->cn == "wdefault"){
443         $ldap->cat($this->dn);
444       }else{
445         $ldap->search ("(&(cn=".$this->cn.")(objectClass=gotoWorkstation))", array("cn"));
446       }
447       if ($ldap->count() != 0){
448         while ($attrs= $ldap->fetch()){
449           if (preg_match("/cn=dhcp,/",$attrs['dn']) || preg_match ("/,".get_ou('incomingou')."/", $ldap->getDN())){
450             continue;
451           } else {
452             if ($attrs['dn'] != $this->orig_dn){
453               $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
454               break;
455             }
456           }
457         }
458       }
459     }
461     /* Check for valid ntpServer selection */
462     if((!$this->inheritTimeServer) && (!count($this->gotoNtpServer))){
463       $message[]= _("There must be at least one NTP server selected, or the inherit mode activated.");
464     }
466     return ($message);
467   }
470   /* Save to LDAP */
471   function save()
472   {
473     /* Detect mode changes */
474     $activate= (isset($this->saved_attributes['gotoMode']) &&
475         $this->gotoMode != $this->saved_attributes['gotoMode'] &&
476         $this->gotoMode == "active" &&
477         tests::is_ip($this->netConfigDNS->ipHostNumber));
479     plugin::save();
481     /* Strip out 'default' values */
482     foreach (array("gotoSyslogServer") as $val){
484       if (isset($this->attrs[$val]) && $this->attrs[$val] == "default"){
485         $this->attrs[$val]= array();
486       }
487     }
489     /* Add missing arrays */
490     foreach (array("ghScsiDev", "ghIdeDev", "ghNetNic") as $val){
491       if (isset ($this->$val) && count ($this->$val) != 0){
492         $this->attrs["$val"]= $this->$val;
493       }
494     }
496     /* Remove all empty values */
497     if ($this->orig_dn == 'new'){
498       $attrs= array();
499       foreach ($this->attrs as $key => $val){
500         if (is_array($val) && count($val) == 0){
501           continue;
502         }
503         $attrs[$key]= $val;
504       }
505       $this->attrs= $attrs;
506     }
508     /* Update ntp server settings */
509     if($this->inheritTimeServer){
510       if($this->is_new){
511         if(isset($this->attrs['gotoNtpServer'])){
512           unset($this->attrs['gotoNtpServer']);
513         }
514       }else{
515         $this->attrs['gotoNtpServer'] = array();
516       }
517     }else{
518       /* Set ntpServers */
519       $this->attrs['gotoNtpServer'] = array();
520       foreach($this->gotoNtpServer as $server){
521         $this->attrs['gotoNtpServer'][] = $server;
522       }
523     }
525     if(($this->gosaUnitTag) && (!in_array_ics("gosaAdministrativeUnitTag",$this->attrs['objectClass']))){
526       $this->attrs['objectClass'][] = "gosaAdministrativeUnitTag";
527     }
529     /* Write back to ldap */
530     $ldap= $this->config->get_ldap_link();
531     if ($this->orig_dn == 'new'){
532       $ldap->cd($this->config->current['BASE']);
533       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
534       $ldap->cd($this->dn);
535       $ldap->add($this->attrs);
536       new log("create","workstation/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
537       show_ldap_error($ldap->get_error(), sprintf(_("Saving of system workstation/generic with dn '%s' failed."),$this->dn));
538       if(!$this->didAction){
539         $this->handle_post_events("add",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
540       }
541     } else {
542       if ($this->orig_dn != $this->dn){
543         $this->move($this->orig_dn, $this->dn);
544       }
545       $ldap->cd($this->dn);
546       $this->cleanup();
547       $ldap->modify ($this->attrs); 
548       new log("modify","workstation/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
550       if(!$this->didAction){
551         $this->handle_post_events("modify",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
552       }
553     }
555     /* cn=default and macAddress=- indicates that this is a template */
556     if($this->cn == "wdefault"){
557       $this->netConfigDNS->macAddress = "-";
558     }
560     $this->netConfigDNS->cn = $this->cn;
561     $this->netConfigDNS->save();
562     show_ldap_error($ldap->get_error(), sprintf(_("Saving of system workstation/generic with dn '%s' failed."),$this->dn));
564     if ($activate){
565       gosaSupportDaemon::send("gosa_set_activated_for_installation", $this->netConfigDNS->macAddress);
566     }
567   }
570   /* Display generic part for server copy & paste */
571   function getCopyDialog()
572   {
573     $vars = array("cn");
574     $smarty = get_smarty();
575     $smarty->assign("cn" ,$this->cn);
576     $smarty->assign("object","workstation");
577     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
578     $ret = array();
579     $ret['string'] = $str;
580     $ret['status'] = "";
581     return($ret);
582   }
585   function saveCopyDialog()
586   {
587     if(isset($_POST['cn'])){
588       $this->cn = $_POST['cn'];
589     }
590   }
593   function PrepareForCopyPaste($source)
594   {
595     plugin::PrepareForCopyPaste($source);
596     if(isset($source['macAddress'][0])){
597       $this->netConfigDNS->macAddress = $source['macAddress'][0];
598     }
599     if(isset($source['ipHostNumber'][0])){
600       $this->netConfigDNS->ipHostNumber = $source['ipHostNumber'][0];
601     }
603     /* Create used ntp server array */
604     $this->gotoNtpServer= array();
605     if(isset($source['gotoNtpServer'])){
606       $this->inheritTimeServer = false;
607       unset($source['gotoNtpServer']['count']);
608       foreach($source['gotoNtpServer'] as $server){
609         $this->gotoNtpServer[$server] = $server;
610       }
611     }
613     /* Set inherit checkbox state */
614     if((in_array("default",$this->gotoNtpServer)) || (count($this->gotoNtpServer)==0)){
615       $this->inheritTimeServer = true;
616       $this->gotoNtpServer=array();
617     }
618   }
621   /* Return plugin informations for acl handling 
622       #FIXME FAIscript seams to ununsed within this class... */ 
623   static function plInfo()
624   {
625     return (array(  
626           "plShortName"   => _("Generic"),
627           "plDescription" => _("Workstation generic"),
628           "plSelfModify"  => FALSE,
629           "plDepends"     => array(),
630           "plPriority"    => 0,
631           "plSection"     => array("administration"),
632           "plCategory"    => array("workstation" => array("description"  => _("Workstation"),
633                                                           "objectClass"  => "gotoWorkstation")),
634           "plProvidedAcls"=> array(
635             "cn"                  => _("Workstation name"),
636             "l"                   => _("Location") ,
637             "base"                => _("Base") ,
638             "gotoMode"            => _("Goto mode"), 
639             "gotoSyslogServer"    => _("Syslog server"), 
640             "gotoNtpServer"       => _("Ntp server"), 
641             "gotoRootPasswd"      => _("Root password"),
642             "FAIstate"            => _("Action flag"))
643           ));
644   }
646   function set_everything_to_inherited()
647   {
648     $this->gotoSyslogServer  = "default";
649     $this->inheritTimeServer = TRUE;
651     /* Set workstation service attributes to inherited */
652     if($this->member_of_ogroup && isset($this->parent->by_object['workservice'])){
653       foreach(array("gotoXKbLayout","gotoXKbModel","gotoXKbVariant",
654             "gotoXResolution","gotoXColordepth","gotoXMouseType","gotoXMouseport") as $name){
655         $this->parent->by_object['workservice']->$name = "default"; 
656       }
657     }
659     /* Set workstation startup attributes to inherited */
660     if($this->member_of_ogroup && isset($this->parent->by_object['workstartup'])){
661       $this->parent->by_object['workstartup']->gotoBootKernel = "default-inherited";
662       $this->parent->by_object['workstartup']->gotoLdapServer = "default-inherited";
663       $this->parent->by_object['workstartup']->FAIdebianMirror= "inherited";
664     }
665   }
667   // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
668 ?>