312b6f6cd104363367287dc0947d3b948e519e35
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 function termgeneric ($config, $dn= NULL, $parent= NULL)
62 {
63 /* Check if FAI is activated */
64 $tmp = search_config($config->data,"faiManagement","CLASS");
65 if(!empty($tmp)){
66 $this->fai_activated = TRUE;
67 }
69 plugin::plugin ($config, $dn, $parent);
70 $this->netConfigDNS = new termDNS($this->config,$this->dn,$this->objectclasses);
71 /* Read arrays */
72 foreach (array("ghNetNic", "ghIdeDev", "ghScsiDev") as $val){
73 if (!isset($this->attrs[$val])){
74 continue;
75 }
76 for ($i= 0; $i<$this->attrs[$val]['count']; $i++){
77 array_push($this->$val, $this->attrs[$val][$i]);
78 }
79 }
81 /* Create used ntp server array */
82 $this->gotoNtpServer= array();
83 if(isset($this->attrs['gotoNtpServer'])){
84 $this->inheritTimeServer = false;
85 unset($this->attrs['gotoNtpServer']['count']);
86 foreach($this->attrs['gotoNtpServer'] as $server){
87 $this->gotoNtpServer[$server] = $server;
88 }
89 }
91 /* Set inherit checkbox state */
92 if((in_array("default",$this->gotoNtpServer)) || (count($this->gotoNtpServer)==0)){
93 $this->inheritTimeServer = true;
94 $this->gotoNtpServer=array();
95 }
97 /* Create available ntp options */
98 $this->gotoNtpServers = $this->config->data['SERVERS']['NTP'];
99 foreach($this->gotoNtpServers as $key => $server){
100 if($server == "default"){
101 unset($this->gotoNtpServers[$key]);
102 }
103 }
105 $this->modes["disabled"]= _("disabled");
106 $this->modes["text"]= _("text");
107 $this->modes["graphic"]= _("graphic");
109 /* Set base */
110 if ($this->dn == "new"){
111 $ui= get_userinfo();
112 $this->base= dn2base($ui->dn);
113 } else {
114 $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
115 }
117 /* Create an array of all Syslog servers */
118 $tmp = $this->config->data['SERVERS']['SYSLOG'];
119 foreach($tmp as $server){
120 $visible = $server;
121 if($server == "default") {
122 $visible = "["._("inherited")."]";
123 }
124 $this->gotoSyslogServers[$server] = $visible;
125 }
127 $this->orig_dn= $this->dn;
128 }
130 function set_acl_base($base)
131 {
132 plugin::set_acl_base($base);
133 $this->netConfigDNS->set_acl_base($base);
134 }
136 function set_acl_category($cat)
137 {
138 plugin::set_acl_category($cat);
139 $this->netConfigDNS->set_acl_category($cat);
140 }
142 function execute()
143 {
144 /* Call parent execute */
145 plugin::execute();
147 if($this->is_account && !$this->view_logged){
148 $this->view_logged = TRUE;
149 @log::log("view","terminal/".get_class($this),$this->dn);
150 }
152 /* Do we need to flip is_account state? */
153 if (isset($_POST['modify_state'])){
154 $this->is_account= !$this->is_account;
155 }
157 if (isset($_POST['action']) && $this->acl_is_writeable("FAIstate")){
159 /* Set FAIstate */
160 if($this->fai_activated && $this->dn != "new"){
161 $ldap = $this->config->get_ldap_link();
162 $ldap->cd($this->config->current['BASE']);
163 $ldap->cat($this->dn,array("objectClass"));
164 $res = $ldap->fetch();
166 $attrs = array();
167 $attrs['FAIstate'] = "";
168 if(isset($this->mapActions[$_POST['saction']])){
169 $attrs['FAIstate'] = $this->mapActions[$_POST ['saction']];
170 }
172 for($i = 0; $i < $res['objectClass']['count'] ; $i ++){
173 $attrs['objectClass'][] = $res['objectClass'][$i];
174 }
176 if(($attrs['FAIstate'] != "") && (!in_array("FAIobject",$attrs['objectClass']))){
177 $attrs['objectClass'][] = "FAIobject";
178 }
180 if($attrs['FAIstate'] == ""){
181 #FIXME we should check if FAIobject is used anymore
182 $attrs['FAIstate'] = array();
183 }
185 $ldap->cd($this->dn);
186 $ldap->modify($attrs);
187 show_ldap_error($ldap->get_error(), sprintf(_("Saving of system terminal/generic (FAIstate) with dn '%s' failed."),$this->dn));
188 }
190 switch($_POST['saction']){
191 case 'wake':
192 $cmd= search_config($this->config->data['TABS'], "termgeneric", "WAKECMD");
193 if ($cmd == ""){
194 print_red(_("No WAKECMD definition found in your gosa.conf"));
195 } else {
196 exec ($cmd." ".$this->netConfigDNS->macAddress, $dummy, $retval);
197 if ($retval != 0){
198 print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
199 }
200 }
201 break;
203 case 'reboot':
204 $cmd= search_config($this->config->data['TABS'], "termgeneric", "REBOOTCMD");
205 if ($cmd == ""){
206 print_red(_("No REBOOTCMD definition found in your gosa.conf"));
207 } else {
208 exec ($cmd." ".$this->cn, $dummy, $retval);
209 if ($retval != 0){
210 print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
211 }
212 }
213 break;
215 case 'halt':
216 $cmd= search_config($this->config->data['TABS'], "termgeneric", "HALTCMD");
217 if ($cmd == ""){
218 print_red(_("No HALTCMD definition found in your gosa.conf"));
219 } else {
220 exec ($cmd." ".$this->cn, $dummy, $retval);
221 if ($retval != 0){
222 print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
223 }
224 }
225 break;
226 }
227 }
229 /* Base select dialog */
230 $once = true;
231 foreach($_POST as $name => $value){
232 if(preg_match("/^chooseBase/",$name) && $once && $this->acl_is_moveable()){
233 $once = false;
234 $this->dialog = new baseSelectDialog($this->config,$this,$this->get_allowed_bases());
235 $this->dialog->setCurrentBase($this->base);
236 }
237 }
239 /* Dialog handling */
240 if(is_object($this->dialog)){
241 /* Must be called before save_object */
242 $this->dialog->save_object();
244 if($this->dialog->isClosed()){
245 $this->dialog = false;
246 }elseif($this->dialog->isSelected()){
248 /* A new base was selected, check if it is a valid one */
249 $tmp = $this->get_allowed_bases();
250 if(isset($tmp[$this->dialog->isSelected()])){
251 $this->base = $this->dialog->isSelected();
252 }
253 $this->dialog= false;
254 }else{
255 return($this->dialog->execute());
256 }
257 }
259 /* Do we represent a valid terminal? */
260 if (!$this->is_account && $this->parent == NULL){
261 $display= "<img alt=\"\" src=\"images/stop.png\" align=middle> <b>".
262 _("This 'dn' has no terminal features.")."</b>";
263 return($display);
264 }
266 /* Add new ntp Server to our list */
267 if((isset($_POST['addNtpServer'])) && (isset($_POST['gotoNtpServers'])) && $this->acl_is_writeable("gotoNtpServer")){
268 $this->gotoNtpServer[$_POST['gotoNtpServers']] = $_POST['gotoNtpServers'];
269 }
271 /* Delete selected NtpServer for list of used servers */
272 if((isset($_POST['delNtpServer'])) && (isset($_POST['gotoNtpServerSelected'])) && $this->acl_is_writeable("gotoNtpServer")){
273 foreach($_POST['gotoNtpServerSelected'] as $name){
274 unset($this->gotoNtpServer[$name]);
275 }
276 }
278 /* Fill templating stuff */
279 $smarty= get_smarty();
281 $tmp = $this->plInfo();
282 foreach($tmp['plProvidedAcls'] as $name => $translation){
283 $smarty->assign($name."ACL",$this->getacl($name));
284 }
286 $smarty->assign("cn", $this->cn);
287 $smarty->assign("staticAddress", "");
289 $smarty->assign("bases", $this->get_allowed_bases());
291 /* tell smarty the inherit checkbox state */
292 $smarty->assign("inheritTimeServer",$this->inheritTimeServer);
294 /* Check if terminal is online */
295 $query= "fping -q -r 1 -t 500 ".$this->cn;
296 exec ($query, $dummy, $retval);
298 /* Offline */
299 if ($retval == 0){
300 $smarty->assign("actions", array( "halt" => _("Switch off"),
301 "reboot" => _("Reboot"),
302 "memcheck" => _("Memory test"),
303 "sysinfo" => _("System analysis")));
304 } else {
305 $smarty->assign("actions", array("wake" => _("Wake up"),
306 "memcheck" => _("Memory test"),
307 "sysinfo" => _("System analysis")));
308 }
310 /* Arrays */
311 $smarty->assign("modes", $this->modes);
313 $tmp2 = array();
314 foreach($this->config->data['SERVERS']['NFS'] as $server){
315 if($server != "default"){
316 $tmp2[$server]= $server;
317 }else{
318 $tmp2[$server]="["._("inherited")."]";
319 }
320 }
322 $smarty->assign("nfsservers", $tmp2);
323 $smarty->assign("syslogservers", $this->gotoSyslogServers);
325 $tmp = array();
326 foreach($this->gotoNtpServers as $server){
327 if(!in_array($server,$this->gotoNtpServer)){
328 $tmp[$server] = $server;
329 }
330 }
332 $smarty->assign("ntpservers", $tmp);
333 $smarty->assign("fai_activated",$this->fai_activated);
335 /* Variables */
336 foreach(array("base", "gotoMode", "gotoTerminalPath", "gotoSwapServer","gotoSyslogServer", "gotoNtpServer") as $val){
337 $smarty->assign($val."_select", $this->$val);
338 }
340 /* Show main page */
341 $smarty->assign("netconfig", $this->netConfigDNS->execute());
342 return($smarty->fetch (get_template_path('terminal.tpl', TRUE)));
343 }
345 function remove_from_parent()
346 {
347 if($this->acl_is_removeable()){
348 $ldap= $this->config->get_ldap_link();
349 $ldap->cd($this->dn);
350 $ldap->cat($this->dn, array('dn'));
351 if($ldap->count()){
352 $this->netConfigDNS->remove_from_parent();
353 $ldap->rmDir($this->dn);
355 @log::log("remove","terminal/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
357 show_ldap_error($ldap->get_error(), sprintf(_("Removing of object system terminal/generic with dn '%s' failed."),$this->dn));
359 /* Optionally execute a command after we're done */
360 $this->handle_post_events("remove",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
362 /* Delete references to object groups */
363 $ldap->cd ($this->config->current['BASE']);
364 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
365 while ($ldap->fetch()){
366 $og= new ogroup($this->config, $ldap->getDN());
367 unset($og->member[$this->dn]);
368 $og->save ();
369 }
370 }
371 }
372 }
375 /* Save data to object */
376 function save_object()
377 {
378 /* Create a base backup and reset the
379 base directly after calling plugin::save_object();
380 Base will be set seperatly a few lines below */
381 $base_tmp = $this->base;
382 plugin::save_object();
383 $this->base = $base_tmp;
385 /* Set new base if allowed */
386 $tmp = $this->get_allowed_bases();
387 if(isset($_POST['base'])){
388 if(isset($tmp[$_POST['base']])){
389 $this->base= $_POST['base'];
390 }
391 }
393 $this->netConfigDNS->save_object();
395 /* Save terminal path to parent since it is used by termstartup, too */
396 if(isset($this->parent->by_object['termstartup'])){
397 $this->parent->by_object['termstartup']->gotoTerminalPath= $this->gotoTerminalPath;
398 }
400 if(isset($_POST['termgeneric_posted'])){
401 if(isset($_POST["inheritTimeServer"])){
402 $this->inheritTimeServer = true;
403 }else{
404 $this->inheritTimeServer = false;
405 }
406 }
407 }
410 /* Check supplied data */
411 function check()
412 {
413 /* Call common method to give check the hook */
414 $message= plugin::check();
416 /* Skip IP & Mac checks if this is a template */
417 if($this->cn != "default"){
418 $message= array_merge($message, $this->netConfigDNS->check());
419 }
421 /* Permissions for that base? */
422 $this->dn= "cn=".$this->cn.",ou=terminals,ou=systems,".$this->base;
424 if ($this->cn == ""){
425 $message[]= _("The required field 'Terminal name' is not set.");
426 }
428 if ($this->orig_dn == 'new'){
429 $ldap= $this->config->get_ldap_link();
430 $ldap->cd ($this->base);
432 /* It is possible to have a 'default' terminal on every base */
433 if($this->cn == "default"){
434 $ldap->cat($this->dn);
435 }else{
436 $ldap->search ("(&(objectClass=gotoTerminal)(cn=".$this->cn."))", array("cn"));
437 }
438 if ($ldap->count() != 0){
439 while ($attrs= $ldap->fetch()){
440 if (preg_match ("/,ou=incoming,/", $ldap->getDN())){
441 continue;
442 } else {
443 if ($attrs['dn'] != $this->orig_dn){
444 $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
445 break;
446 }
447 }
448 }
449 }
450 }
452 /* Check for valid ntpServer selection */
453 if((!$this->inheritTimeServer) && (!count($this->gotoNtpServer))){
454 $message[]= _("There must be at least one NTP server selected, or the inherit mode activated.");
455 }
457 return ($message);
458 }
461 /* Save to LDAP */
462 function save()
463 {
465 /* Move object if requested */
466 if( $this->orig_dn != 'new' && $this->dn != $this->orig_dn){
467 $this->move($this->orig_dn, $this->dn);
468 }
470 plugin::save();
472 /* Strip out 'default' values */
473 foreach (array("gotoTerminalPath", "gotoSwapServer", "gotoSyslogServer") as $val){
475 if ($this->attrs[$val] == "default"){
476 $this->attrs[$val]= array();
477 }
478 }
480 /* Add missing arrays */
481 foreach (array("ghScsiDev", "ghIdeDev", "ghNetNic") as $val){
482 if (isset ($this->$val) && count ($this->$val) != 0){
483 $this->attrs["$val"]= $this->$val;
484 }
485 }
487 /* Remove all empty values */
488 if ($this->orig_dn == 'new'){
489 $attrs= array();
490 foreach ($this->attrs as $key => $val){
491 if (is_array($val) && count($val) == 0){
492 continue;
493 }
494 $attrs[$key]= $val;
495 }
496 $this->attrs= $attrs;
497 }
499 /* Set ntpServers */
500 $this->attrs['gotoNtpServer'] = array();
501 if(!$this->inheritTimeServer){
502 foreach($this->gotoNtpServer as $server){
503 $this->attrs['gotoNtpServer'][] = $server;
504 }
505 }
507 /* Append gosaAdministrativeUnitTag to objectClass if gosaUnitTag isset */
508 if(($this->gosaUnitTag) && (!in_array_ics("gosaAdministrativeUnitTag",$this->attrs['objectClass']))){
509 $this->attrs['objectClass'][] = "gosaAdministrativeUnitTag";
510 }
512 /* Write back to ldap */
513 $ldap= $this->config->get_ldap_link();
514 if ($this->orig_dn == 'new'){
515 $ldap->cd($this->config->current['BASE']);
516 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
517 $ldap->cd($this->dn);
518 if (!count($this->attrs['gotoNtpServer'])){
519 unset($this->attrs['gotoNtpServer']);
520 }
521 $ldap->add($this->attrs);
522 @log::log("create","terminal/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
523 $this->handle_post_events("add",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
524 } else {
525 $ldap->cd($this->dn);
526 $this->cleanup();
527 $ldap->modify ($this->attrs);
528 @log::log("modify","terminal/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
529 $this->handle_post_events("modify",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
530 }
532 /* cn=default and macAddress=- indicates that this is a template */
533 if($this->cn == "default"){
534 $this->netConfigDNS->macAddress = "-";
535 }
537 $this->netConfigDNS->cn = $this->cn;
538 $this->netConfigDNS->save($this->dn);
539 show_ldap_error($ldap->get_error(), sprintf(_("Saving of object system terminal/generic with dn '%s' failed."),$this->dn));
540 }
543 /* Return plugin informations for acl handling */
544 function plInfo()
545 {
546 return (array(
547 "plShortName" => _("Terminal"),
548 "plDescription" => _("Terminal generic"),
549 "plSelfModify" => FALSE,
550 "plDepends" => array(),
551 "plPriority" => 1,
552 "plSection" => array("administration"),
553 "plCategory" => array("terminal" => array( "description" => _("Terminal"),
554 "objectClass" => "gotoTerminal")),
555 "plProvidedAcls"=> array(
556 "gotoMode" => _("Mode"),
557 "gotoTerminalPath" => _("Root server"),
558 "gotoSwapServer" => _("Swap server"),
559 "gotoSyslogServer" => _("Syslog server enabled"),
560 "gotoNtpServer" => _("Ntp server settings"),
561 "base" => _("Base"),
562 "cn" => _("Name"),
563 "gotoRootPasswd" => _("Root password"),
564 "FAIstate" => _("Action flag"))
565 ));
566 }
567 }
569 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
570 ?>