Code

Updated acls for terminalGeneric and network dns stuff
[gosa.git] / plugins / admin / systems / 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;
60   function termgeneric ($config, $dn= NULL, $parent= NULL)
61   {
62     /* Check if FAI is activated */
63     $tmp = search_config($config->data,"faiManagement","CLASS");
64     if(!empty($tmp)){
65       $this->fai_activated = TRUE;
66     }
68     plugin::plugin ($config, $dn, $parent);
69     $this->netConfigDNS = new termDNS($this->config,$this->dn,$this->objectclasses);
70     /* Read arrays */
71     foreach (array("ghNetNic", "ghIdeDev", "ghScsiDev") as $val){
72       if (!isset($this->attrs[$val])){
73         continue;
74       }
75       for ($i= 0; $i<$this->attrs[$val]['count']; $i++){
76         array_push($this->$val, $this->attrs[$val][$i]);
77       }
78     }
80     /* Create used ntp server array */
81     $this->gotoNtpServer= array();
82     if(isset($this->attrs['gotoNtpServer'])){
83       $this->inheritTimeServer = false;
84       unset($this->attrs['gotoNtpServer']['count']);
85       foreach($this->attrs['gotoNtpServer'] as $server){
86         $this->gotoNtpServer[$server] = $server;
87       }
88     }
90     /* Set inherit checkbox state */
91     if((in_array("default",$this->gotoNtpServer)) || (count($this->gotoNtpServer)==0)){
92       $this->inheritTimeServer = true;
93       $this->gotoNtpServer=array();
94     }
96     /* Create available ntp options */
97     $this->gotoNtpServers = $this->config->data['SERVERS']['NTP'];
98     foreach($this->gotoNtpServers as $key => $server){
99       if($server == "default"){
100         unset($this->gotoNtpServers[$key]);
101       }
102     }
104     $this->modes["disabled"]= _("disabled");
105     $this->modes["text"]= _("text");
106     $this->modes["graphic"]= _("graphic");
108     /* Set base */
109     if ($this->dn == "new"){
110       $ui= get_userinfo();
111       $this->base= dn2base($ui->dn);
112     } else {
113       $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
114     }
116     /* Create an array of all Syslog servers */
117     $tmp = $this->config->data['SERVERS']['SYSLOG'];
118     foreach($tmp as $server){
119       $visible = $server;
120       if($server == "default") {
121         $visible = "["._("inherited")."]";
122       }
123       $this->gotoSyslogServers[$server] = $visible;
124     }
126     $this->orig_dn= $this->dn;
127   }
129   function execute()
130   {
131     /* Call parent execute */
132     plugin::execute();
134     $this->netConfigDNS->acl = $this->acl;
136     /* Do we need to flip is_account state? */
137     if (isset($_POST['modify_state'])){
138       $this->is_account= !$this->is_account;
139     }
141     if (isset($_POST['action']) && chkacl($this->acl,"FAIstate") ==""){
143       if($this->fai_activated && $this->dn != "new"){
144         /* Set FAIstate */
145         $ldap = $this->config->get_ldap_link();
146         $ldap->cd($this->config->current['BASE']);
147         $ldap->cat($this->dn,array("objectClass"));
148         $res = $ldap->fetch();
150         $attrs = array();
151         $attrs['FAIstate'] = "";
152         if(isset($this->mapActions[$_POST['saction']])){
153           $attrs['FAIstate'] = $this->mapActions[$_POST ['saction']];
154         }
156         for($i = 0; $i < $res['objectClass']['count'] ; $i ++){
157           $attrs['objectClass'][] = $res['objectClass'][$i];
158         }
160         if(($attrs['FAIstate'] != "") && (!in_array("FAIobject",$attrs['objectClass']))){
161           $attrs['objectClass'][] = "FAIobject";
162         }
164         if($attrs['FAIstate'] == ""){
165 #FIXME we should check if FAIobject is used anymore
166           $attrs['FAIstate'] = array();
167         }
169         $ldap->cd($this->dn);
170         $ldap->modify($attrs);
171         show_ldap_error($ldap->get_error());
172       }
174       switch($_POST['saction']){
175         case 'wake':
176           $cmd= search_config($this->config->data['TABS'], "termgeneric", "WAKECMD");
177           if ($cmd == ""){
178             print_red(_("No WAKECMD definition found in your gosa.conf"));
179           } else {
180             exec ($cmd." ".$this->netConfigDNS->macAddress, $dummy, $retval);
181             if ($retval != 0){
182               print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
183             }
184           }
185           break;
187         case 'reboot':
188           $cmd= search_config($this->config->data['TABS'], "termgeneric", "REBOOTCMD");
189           if ($cmd == ""){
190             print_red(_("No REBOOTCMD definition found in your gosa.conf"));
191           } else {
192             exec ($cmd." ".$this->cn, $dummy, $retval);
193             if ($retval != 0){
194               print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
195             }
196           }
197           break;
199         case 'halt':
200           $cmd= search_config($this->config->data['TABS'], "termgeneric", "HALTCMD");
201           if ($cmd == ""){
202             print_red(_("No HALTCMD definition found in your gosa.conf"));
203           } else {
204             exec ($cmd." ".$this->cn, $dummy, $retval);
205             if ($retval != 0){
206               print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
207             }
208           }
209           break;
210       }
211     }
213     /* Base select dialog */
214     $once = true;
215     foreach($_POST as $name => $value){
216       if(preg_match("/^chooseBase/",$name) && $once && chkacl($this->acl,"base") == ""){
217         $once = false;
218         $this->dialog = new baseSelectDialog($this->config);
219         $this->dialog->setCurrentBase($this->base);
220       }
221     }
223     /* Dialog handling */
224     if(is_object($this->dialog)){
225       /* Must be called before save_object */
226       $this->dialog->save_object();
228       if($this->dialog->isClosed()){
229         $this->dialog = false;
230       }elseif($this->dialog->isSelected()){
231         $this->base = $this->dialog->isSelected();
232         $this->dialog= false;
233       }else{
234         return($this->dialog->execute());
235       }
236     }
238     /* Do we represent a valid terminal? */
239     if (!$this->is_account && $this->parent == NULL){
240       $display= "<img alt=\"\" src=\"images/stop.png\" align=middle>&nbsp;<b>".
241         _("This 'dn' has no terminal features.")."</b>";
242       return($display);
243     }
245     /* Add new ntp Server to our list */
246     if((isset($_POST['addNtpServer'])) && (isset($_POST['gotoNtpServers'])) && chkacl($this->acl,"gotoNtpServer")== ""){
247       $this->gotoNtpServer[$_POST['gotoNtpServers']] = $_POST['gotoNtpServers'];
248     }
250     /* Delete selected NtpServer for list of used servers  */
251     if((isset($_POST['delNtpServer'])) && (isset($_POST['gotoNtpServerSelected'])) && chkacl($this->acl,"gotoNtpServer")== ""){
252       foreach($_POST['gotoNtpServerSelected'] as $name){
253         unset($this->gotoNtpServer[$name]);
254       } 
255     }
257     /* Fill templating stuff */
258     $smarty= get_smarty();
259     $smarty->assign("cn", $this->cn);
260     $smarty->assign("staticAddress", "");
262     $smarty->assign("bases", $this->config->idepartments);
264     /* tell smarty the inherit checkbox state */
265     $smarty->assign("inheritTimeServer",$this->inheritTimeServer);
267     /* Check if terminal is online */
268     $query= "fping -q -r 1 -t 500 ".$this->cn;
269     exec ($query, $dummy, $retval);
271     /* Offline */
272     if ($retval == 0){
273       $smarty->assign("actions", array( "halt" => _("Switch off"), 
274                                         "reboot" => _("Reboot"),
275                                         "memcheck" => _("Memory test"),
276                                         "sysinfo"  => _("System analysis")));
277     } else {
278       $smarty->assign("actions", array("wake" => _("Wake up"),
279                                        "memcheck" => _("Memory test"),
280                                        "sysinfo"  => _("System analysis")));
281     }
283     /* Arrays */
284     $smarty->assign("modes", $this->modes);
286     $tmp2 = array(); 
287     foreach($this->config->data['SERVERS']['NFS'] as $server){
288       if($server != "default"){
289         $tmp2[$server]= $server;
290       }else{
291         $tmp2[$server]="["._("inherited")."]";
292       }
293     }
294   
295     $smarty->assign("nfsservers",     $tmp2);
297     
299     $smarty->assign("syslogservers",  $this->gotoSyslogServers);
300     $smarty->assign("ntpservers",     $this->gotoNtpServers);
302     /* Variables */
303     foreach(array("base", "gotoMode", "gotoTerminalPath", "gotoSwapServer",
304           "gotoSyslogServer", "gotoNtpServer") as $val){
306       $smarty->assign($val."_select", $this->$val);
307       $smarty->assign($val."ACL", chkacl($this->acl, $val));
308     }
310     /* Show main page */
311     $this->netConfigDNS->cn= $this->cn;
312     $smarty->assign("netconfig", $this->netConfigDNS->execute());
313     $smarty->assign("fai_activated",$this->fai_activated);
314     $smarty->assign("actionACL", chkacl($this->acl, 'action'));
315     return($smarty->fetch (get_template_path('terminal.tpl', TRUE)));
316   }
318   function remove_from_parent()
319   {
320     
321     $this->netConfigDNS->acl = $this->acl;
322     $ldap= $this->config->get_ldap_link();
323     $ldap->cd($this->dn);
324     $ldap->cat($this->dn, array('dn'));
325     if($ldap->count()){
326       $this->netConfigDNS->remove_from_parent();
327       $ldap->rmDir($this->dn);
328       show_ldap_error($ldap->get_error(), _("Removing terminal failed"));
330       /* Optionally execute a command after we're done */
331       $this->handle_post_events("remove", array("macAddress" => $this->netConfigDNS->macAddress));
333       /* Delete references to object groups */
334       $ldap->cd ($this->config->current['BASE']);
335       $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
336       while ($ldap->fetch()){
337         $og= new ogroup($this->config, $ldap->getDN());
338         unset($og->member[$this->dn]);
339         $og->save ();
340       }
341     }
342   }
345   /* Save data to object */
346   function save_object()
347   {
348     plugin::save_object();
349     $this->netConfigDNS->save_object();
350     /* Save base, since this is no LDAP attribute */
351     if (isset($_POST['base']) && chkacl($this->acl, "create") == ""){
352       $this->base= $_POST['base'];
353     }
355     /* Save terminal path to parent since it is used by termstartup, too */
356     if(isset($this->parent->by_object['termstartup'])){
357     $this->parent->by_object['termstartup']->gotoTerminalPath=
358       $this->gotoTerminalPath;
359     }
360   
361     if(isset($_POST['termgeneric_posted'])){
362       if(isset($_POST["inheritTimeServer"])){
363         $this->inheritTimeServer = true;
364       }else{
365         $this->inheritTimeServer = false;
366       }
367     }  
368   }
371   /* Check supplied data */
372   function check()
373   {
374     /* Call common method to give check the hook */
375     $message= plugin::check();
377     if($this->cn != "default"){
378       $message= array_merge($message, $this->netConfigDNS->check());
379     }
381     /* Permissions for that base? */
382     $this->dn= "cn=".$this->cn.",ou=terminals,ou=systems,".$this->base;
384     $ui= get_userinfo();
385     $acl= get_permissions ($this->dn, $ui->subtreeACL);
386     $acl= get_module_permission($acl, "group", $this->dn);
387     if (chkacl($acl, "create") != ""){
388       $message[]= _("You have no permissions to create a terminal on this 'Base'.");
389     }
391     if ($this->cn == "" && chkacl ($this->acl, "cn") == ""){
392       $message[]= _("The required field 'Terminal name' is not set.");
393     }
395     if ($this->orig_dn == 'new'){
396       $ldap= $this->config->get_ldap_link();
397       $ldap->cd ($this->config->current['BASE']);
398  
399       /* It is possible to have a 'default' terminal on every base */
400       if($this->cn == "default"){
401         $ldap->cat($this->dn);
402       }else{
403         $ldap->search ("(&(objectClass=gotoTerminal)(cn=".$this->cn."))", array("cn"));
404       }
405   
406       /* Check if there are some other terminals found */
407       if ($ldap->count() != 0){
408         while ($attrs= $ldap->fetch()){
409           if (preg_match ("/,ou=incoming,/", $ldap->getDN())){
410             continue;
411           } else {
412             if ($attrs['dn'] != $this->orig_dn){
413               $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
414               break;
415             }
416           }
417         }
418       }
419     }
421     /* Check for valid ntpServer selection */
422     if((!$this->inheritTimeServer) && (!count($this->gotoNtpServer))){
423       $message[]= _("There must be at least one NTP server selected, or the inherit mode activated.");
424     }
426     return ($message);
427   }
430   /* Save to LDAP */
431   function save()
432   {
433     $this->netConfigDNS->acl = $this->acl;
435     /* Move object if requested */
436     if( $this->orig_dn != 'new' && $this->dn != $this->orig_dn){
437       $this->move($this->orig_dn, $this->dn);
438     }
439  
440     plugin::save();
442     /* Strip out 'default' values */
443     foreach (array("gotoTerminalPath", "gotoSwapServer", "gotoSyslogServer") as $val){
445       if (!isset($this->attrs[$val]) || $this->attrs[$val] == "default"){
446         $this->attrs[$val]= array();
447       }
448     }
450     /* Add missing arrays */
451     foreach (array("ghScsiDev", "ghIdeDev", "ghNetNic") as $val){
452       if (isset ($this->$val) && count ($this->$val) != 0){
453         $this->attrs["$val"]= $this->$val;
454       }
455     }
457     /* Remove all empty values */
458     if ($this->orig_dn == 'new'){
459       $attrs= array();
460       foreach ($this->attrs as $key => $val){
461         if (is_array($val) && count($val) == 0){
462           continue;
463         }
464         $attrs[$key]= $val;
465       }
466       $this->attrs= $attrs;
467     }
469     /* Set ntpServers */
470     $this->attrs['gotoNtpServer'] = array();
471     if(!$this->inheritTimeServer){
472       foreach($this->gotoNtpServer as $server){
473         $this->attrs['gotoNtpServer'][] = $server;
474       }
475     }
477     /* Append gosaAdministrativeUnitTag to objectClass if gosaUnitTag isset */
478     if(($this->gosaUnitTag) && (!in_array_ics("gosaAdministrativeUnitTag",$this->attrs['objectClass']))){
479       $this->attrs['objectClass'][] = "gosaAdministrativeUnitTag";
480     }
482     /* Write back to ldap */
483     $ldap= $this->config->get_ldap_link();
484     if ($this->orig_dn == 'new'){
485       $ldap->cd($this->config->current['BASE']);
486       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
487       $ldap->cd($this->dn);
488       if (!count($this->attrs['gotoNtpServer'])){
489         unset($this->attrs['gotoNtpServer']);
490       }
491       $ldap->add($this->attrs);
492       $this->handle_post_events("add");
493     } else {
494       $ldap->cd($this->dn);
495       $this->cleanup();
496       $ldap->modify ($this->attrs); 
497       $this->handle_post_events("modify");
498     }
499     
500     /* cn=default and macAddress=- indicates that this is a template */ 
501     if($this->cn == "default"){
502       $this->netConfigDNS->macAddress = "-";
503     }
504   
505     $this->netConfigDNS->cn = $this->cn;
506     $this->netConfigDNS->save($this->dn);
507     show_ldap_error($ldap->get_error(), _("Saving terminal failed"));
509     /* Optionally execute a command after we're done */
510     $this->postcreate();
511   }
515 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
516 ?>