08565d4d76ae74a09efc36026edee2350828b321
1 <?php
3 class termgeneric extends plugin
4 {
5 /* Generic terminal attributes */
6 var $gotoMode= "locked";
7 var $gotoTerminalPath= "";
8 var $gotoSwapServer= "";
9 var $gotoSyslogServer= "";
10 var $gotoSyslogServers = array();
11 var $gotoNtpServer= array();
12 var $gotoNtpServers= array();
13 var $gotoSndModule= "";
14 var $gotoFloppyEnable= "";
15 var $gotoCdromEnable= "";
16 var $ghCpuType= "-";
17 var $ghMemSize= "-";
18 var $ghUsbSupport= "-";
19 var $ghNetNic= array();
20 var $ghIdeDev= array();
21 var $ghScsiDev= array();
22 var $ghGfxAdapter= "-";
23 var $ghSoundAdapter= "-";
24 var $gotoLastUser= "-";
25 var $netConfigDNS;
27 /* Needed values and lists */
28 var $base= "";
29 var $cn= "";
30 var $description= "";
31 var $orig_dn= "";
32 var $orig_cn= "";
33 var $orig_base= "";
35 var $inheritTimeServer = true;
37 /* Plugin side filled */
38 var $modes= array();
40 /* attribute list for save action */
41 var $ignore_account= TRUE;
42 var $attributes= array("gotoMode", "gotoTerminalPath",
43 "gotoSwapServer", "gotoSyslogServer", "gotoNtpServer",
44 "gotoFloppyEnable", "gotoCdromEnable", "cn", "gotoSndModule",
45 "ghCpuType", "ghMemSize","ghUsbSupport", "description",
46 "ghGfxAdapter", "ghSoundAdapter", "gotoLastUser");
47 var $objectclasses= array("top", "gotoTerminal", "GOhard");
49 var $validActions = array("reboot" => "", "rescan" => "", "wake" => "", "memcheck" => "", "sysinfo" => "");
51 var $fai_activated = FALSE;
52 var $view_logged = FALSE;
54 var $member_of_ogroup = FALSE;
56 var $kerberos_key_service = NULL;
59 function termgeneric (&$config, $dn= NULL, $parent= NULL)
60 {
61 /* Check if FAI is activated */
62 $tmp= $config->search("faiManagement", "CLASS",array('menu','tabs'));
63 if(!empty($tmp)){
64 $this->fai_activated = TRUE;
65 }
67 plugin::plugin ($config, $dn, $parent);
69 if(class_available("krb_host_keys")){
70 $this->kerberos_key_service = new krb_host_keys($this->config,$this);
71 }
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 for($i = 0 ; $i < $this->attrs['gotoNtpServer']['count']; $i++ ){
96 $server = $this->attrs['gotoNtpServer'][$i];
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 ("/^[^,]+,".normalizePreg(get_ou("terminalou"))."/", "", $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 $this->orig_cn= $this->cn;
143 $this->orig_base= $this->base;
144 }
146 function set_acl_base($base)
147 {
148 plugin::set_acl_base($base);
149 $this->netConfigDNS->set_acl_base($base);
150 }
152 function set_acl_category($cat)
153 {
154 plugin::set_acl_category($cat);
155 $this->netConfigDNS->set_acl_category($cat);
156 }
158 function execute()
159 {
160 /* Call parent execute */
161 plugin::execute();
163 if($this->is_account && !$this->view_logged){
164 $this->view_logged = TRUE;
165 new log("view","terminal/".get_class($this),$this->dn);
166 }
168 /* Do we need to flip is_account state? */
169 if (isset($_POST['modify_state'])){
170 $this->is_account= !$this->is_account;
171 }
173 if (isset($_POST['action']) && $this->acl_is_writeable("FAIstate") && isset($this->validActions[$_POST['saction']])){
174 $action = $_POST['saction'];
176 /* Check if we have an DaemonEvent for this action */
177 if(class_available("DaemonEvent_".$action)){
178 $events = DaemonEvent::get_event_types(SYSTEM_EVENT | HIDDEN_EVENT);
179 if(isset($events['TRIGGERED']["DaemonEvent_".$action])){
180 $evt = $events['TRIGGERED']["DaemonEvent_".$action];
181 $tmp = new $evt['CLASS_NAME']($this->config);
182 $tmp->add_targets(array($this->netConfigDNS->macAddress));
183 $tmp->set_type(TRIGGERED_EVENT);
184 $o_queue = new gosaSupportDaemon();
185 if(!$o_queue->append($tmp)){
186 msg_dialog::display(_("Service infrastructure"),msgPool::siError($o_queue->get_error()),ERROR_DIALOG);
187 }
188 }
189 }else{
190 msg_dialog::display(_("Event error"),
191 sprintf(_("Event '%s' is not available!"),$action),ERROR_DIALOG);
192 }
194 }
196 /* Base select dialog */
197 $once = true;
198 foreach($_POST as $name => $value){
199 if(preg_match("/^chooseBase/",$name) && $once && $this->acl_is_moveable()){
200 $once = false;
201 $this->dialog = new baseSelectDialog($this->config,$this,$this->get_allowed_bases());
202 $this->dialog->setCurrentBase($this->base);
203 }
204 }
206 /* Dialog handling */
207 if(is_object($this->dialog)){
208 /* Must be called before save_object */
209 $this->dialog->save_object();
211 if($this->dialog->isClosed()){
212 $this->dialog = false;
213 }elseif($this->dialog->isSelected()){
215 /* A new base was selected, check if it is a valid one */
216 $tmp = $this->get_allowed_bases();
217 if(isset($tmp[$this->dialog->isSelected()])){
218 $this->base = $this->dialog->isSelected();
219 }
220 $this->dialog= false;
221 }else{
222 return($this->dialog->execute());
223 }
224 }
226 /* Do we represent a valid terminal? */
227 if (!$this->is_account && $this->parent === NULL){
228 $display= "<img alt=\"\" src=\"images/small-error.png\" align=middle> <b>".
229 msgPool::noValidExtension(_("terminal"))."</b>";
230 return($display);
231 }
233 /* Add new ntp Server to our list */
234 if((isset($_POST['addNtpServer'])) && (isset($_POST['gotoNtpServers'])) && $this->acl_is_writeable("gotoNtpServer")){
235 $this->gotoNtpServer[$_POST['gotoNtpServers']] = $_POST['gotoNtpServers'];
236 }
238 /* Delete selected NtpServer for list of used servers */
239 if((isset($_POST['delNtpServer'])) && (isset($_POST['gotoNtpServerSelected'])) && $this->acl_is_writeable("gotoNtpServer")){
240 foreach($_POST['gotoNtpServerSelected'] as $name){
241 unset($this->gotoNtpServer[$name]);
242 }
243 }
245 /* Fill templating stuff */
246 $smarty= get_smarty();
248 $tmp = $this->plInfo();
249 foreach($tmp['plProvidedAcls'] as $name => $translation){
250 $smarty->assign($name."ACL",$this->getacl($name));
251 }
253 $smarty->assign("cn", $this->cn);
254 $smarty->assign("description", $this->description);
255 $smarty->assign("staticAddress", "");
257 $smarty->assign("bases", $this->get_allowed_bases());
259 /* tell smarty the inherit checkbox state */
260 $smarty->assign("inheritTimeServer",$this->inheritTimeServer);
262 /* Check if terminal is online */
263 if (gosaSupportDaemon::ping($this->netConfigDNS->macAddress)){
264 $smarty->assign("actions", array( "halt" => _("Switch off"),
265 "reboot" => _("Reboot"),
266 "memcheck" => _("Memory test"),
267 "sysinfo" => _("System analysis")));
268 } else {
269 $smarty->assign("actions", array("wake" => _("Wake up"),
270 "memcheck" => _("Memory test"),
271 "sysinfo" => _("System analysis")));
272 }
274 /* Arrays */
275 $smarty->assign("modes", $this->modes);
277 $tmp2 = array();
278 foreach($this->config->data['SERVERS']['NFS'] as $server){
279 if($server != "default"){
280 $tmp2[$server]= $server;
281 }else{
282 if($this->member_of_ogroup){
283 $tmp2[$server]="["._("inherited")."]";
284 }
285 }
286 }
288 $smarty->assign("nfsservers", $tmp2);
289 $smarty->assign("syslogservers", $this->gotoSyslogServers);
291 $tmp = array();
292 foreach($this->gotoNtpServers as $server){
293 if(!in_array($server,$this->gotoNtpServer)){
294 $tmp[$server] = $server;
295 }
296 }
298 $smarty->assign("ntpservers", $tmp);
299 $smarty->assign("fai_activated",$this->fai_activated);
301 /* Variables */
302 foreach(array("base", "gotoMode", "gotoTerminalPath", "gotoSwapServer","gotoSyslogServer", "gotoNtpServer") as $val){
303 $smarty->assign($val."_select", $this->$val);
304 }
306 $smarty->assign("member_of_ogroup",$this->member_of_ogroup);
308 /* Show main page */
309 $str = $this->netConfigDNS->execute();
310 if(is_object($this->netConfigDNS->dialog)){
311 return($str);
312 }
313 $smarty->assign("netconfig", $str);
315 /* Display kerberos host key options */
316 $smarty->assign("host_key","");
317 if(is_object($this->kerberos_key_service)){
318 $smarty->assign("host_key",$this->kerberos_key_service->execute_by_prefix("host/"));
319 }
321 return($smarty->fetch (get_template_path('terminal.tpl', TRUE, dirname(__FILE__))));
322 }
324 function remove_from_parent()
325 {
326 if($this->acl_is_removeable()){
327 $ldap= $this->config->get_ldap_link();
328 $ldap->cd($this->dn);
329 $ldap->cat($this->dn, array('dn'));
330 if($ldap->count()){
331 $this->netConfigDNS->remove_from_parent();
332 $ldap->rmDir($this->dn);
334 new log("remove","terminal/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
336 if (!$ldap->success()){
337 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_DEL, get_class()));
338 }
340 /* Remove kerberos key dependencies too */
341 if(is_object($this->kerberos_key_service)){
342 $this->kerberos_key_service->remove_from_parent_by_prefix("host/");
343 }
345 /* Optionally execute a command after we're done */
346 $this->handle_post_events("remove",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
348 /* Delete references to object groups */
349 $ldap->cd ($this->config->current['BASE']);
350 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".LDAP::prepare4filter($this->dn)."))", array("cn"));
351 while ($ldap->fetch()){
352 $og= new ogroup($this->config, $ldap->getDN());
353 unset($og->member[$this->dn]);
354 $og->save ();
355 }
357 /* Remove all accessTo/trust dependencies */
358 update_accessTo($this->cn,"");
359 }
361 /* Clean queue form entries with this mac
362 */
363 if(class_available("gosaSupportDaemon") && tests::is_mac($this->netConfigDNS->orig_macAddress)){
364 $q = new gosaSupportDaemon();
365 $q->clean_queue_from_mac($this->netConfigDNS->orig_macAddress);
366 }
367 }
368 }
371 /* Save data to object */
372 function save_object()
373 {
374 /* Create a base backup and reset the
375 base directly after calling plugin::save_object();
376 Base will be set seperatly a few lines below */
377 $base_tmp = $this->base;
378 plugin::save_object();
379 $this->base = $base_tmp;
381 /* Set new base if allowed */
382 $tmp = $this->get_allowed_bases();
383 if(isset($_POST['base'])){
384 if(isset($tmp[$_POST['base']])){
385 $this->base= $_POST['base'];
386 }
387 }
389 $this->netConfigDNS->save_object();
391 /* Save terminal path to parent since it is used by termstartup, too */
392 if(isset($this->parent->by_object['termstartup'])){
393 $this->parent->by_object['termstartup']->gotoTerminalPath= $this->gotoTerminalPath;
394 }
396 if(isset($_POST['termgeneric_posted'])){
397 if(isset($_POST["inheritTimeServer"]) && $this->member_of_ogroup){
398 $this->inheritTimeServer = true;
399 }else{
400 $this->inheritTimeServer = false;
401 }
402 }
404 if(isset($_POST["inheritAll"])){
405 $this->set_everything_to_inherited();
406 }
408 /* Hanle kerberos host key plugin */
409 if(is_object($this->kerberos_key_service)){
410 $this->kerberos_key_service->save_object_by_prefix("host/");
411 }
412 }
415 /* Check supplied data */
416 function check()
417 {
418 /* Call common method to give check the hook */
419 $message= plugin::check();
421 /* Skip IP & Mac checks if this is a template */
422 if($this->cn != "default"){
423 $message= array_merge($message, $this->netConfigDNS->check());
424 }
426 /* Permissions for that base? */
427 $this->dn= "cn=".$this->cn.",".get_ou('terminalou').$this->base;
429 if ($this->cn == ""){
430 $message[]= msgPool::required(_("Name"));
431 }
433 /* Check if given name is a valid host/dns name */
434 if(!tests::is_dns_name($this->cn) ){
435 $message[] = msgPool::invalid(_("Name"));
436 }
438 if ($this->orig_dn == 'new'){
439 $ldap= $this->config->get_ldap_link();
440 $ldap->cd ($this->base);
442 /* It is possible to have a 'default' terminal on every base */
443 if($this->cn == "default"){
444 $ldap->cat($this->dn);
445 }else{
446 $ldap->search ("(&(objectClass=gotoTerminal)(cn=".$this->cn."))", array("cn"));
447 }
448 if ($ldap->count() != 0){
449 while ($attrs= $ldap->fetch()){
450 if (preg_match("/cn=dhcp,/",$attrs['dn']) || preg_match ("/,".normalizePreg(get_ou('incomingou'))."/", $ldap->getDN())){
451 continue;
452 } else {
453 if ($attrs['dn'] != $this->orig_dn){
454 $message[]= msgPool::duplicated(_("Name"));
455 break;
456 }
457 }
458 }
459 }
460 }
462 /* Check for valid ntpServer selection */
463 if((!$this->inheritTimeServer) && (!count($this->gotoNtpServer))){
464 $message[]= msgPool::required(_("NTP server"));
465 }
467 /* Check if we are allowed to create or move this object
468 */
469 if($this->orig_dn == "new" && !$this->acl_is_createable($this->base)){
470 $message[] = msgPool::permCreate();
471 }elseif($this->orig_dn != "new" && $this->base != $this->orig_base && !$this->acl_is_moveable($this->base)){
472 $message[] = msgPool::permMove();
473 }
475 return ($message);
476 }
479 /* Save to LDAP */
480 function save()
481 {
482 /* Detect mode changes */
483 $activate= (isset($this->saved_attributes['gotoMode']) &&
484 $this->gotoMode != $this->saved_attributes['gotoMode'] &&
485 $this->gotoMode == "enabled" &&
486 tests::is_ip($this->netConfigDNS->ipHostNumber));
488 plugin::save();
490 /* Strip out 'default' values */
491 foreach (array("gotoTerminalPath", "gotoSwapServer", "gotoSyslogServer") as $val){
492 if(isset($this->attrs[$val])){
493 if ($this->attrs[$val] == "default"){
494 $this->attrs[$val]= array();
495 }
496 }
497 }
499 /* Add missing arrays */
500 foreach (array("ghScsiDev", "ghIdeDev", "ghNetNic") as $val){
501 if (isset ($this->$val) && count ($this->$val) != 0){
502 $this->attrs["$val"]= $this->$val;
503 }
504 }
506 /* Remove all empty values */
507 if ($this->orig_dn == 'new'){
508 $attrs= array();
509 foreach ($this->attrs as $key => $val){
510 if (is_array($val) && count($val) == 0){
511 continue;
512 }
513 $attrs[$key]= $val;
514 }
515 $this->attrs= $attrs;
516 }
518 /* Set ntpServers */
519 $this->attrs['gotoNtpServer'] = array();
520 if(!$this->inheritTimeServer){
521 foreach($this->gotoNtpServer as $server){
522 $this->attrs['gotoNtpServer'][] = $server;
523 }
524 }
526 /* Write back to ldap */
527 $ldap= $this->config->get_ldap_link();
528 if ($this->orig_dn == 'new'){
529 $ldap->cd($this->config->current['BASE']);
530 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
531 $ldap->cd($this->dn);
532 if (!count($this->attrs['gotoNtpServer'])){
533 unset($this->attrs['gotoNtpServer']);
534 }
535 $ldap->add($this->attrs);
536 new log("create","terminal/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
537 $this->handle_post_events("add",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
538 } else {
539 $ldap->cd($this->dn);
540 $this->cleanup();
541 $ldap->modify ($this->attrs);
542 new log("modify","terminal/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
543 $this->handle_post_events("modify",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
545 /* Update all accessTo/trust dependencies */
546 if($this->orig_cn != $this->cn){
547 update_accessTo($this->orig_cn,$this->cn);
548 }
549 }
551 /* cn=default and macAddress=- indicates that this is a template */
552 if($this->cn == "default"){
553 $this->netConfigDNS->macAddress = "-";
554 }
556 $this->netConfigDNS->cn = $this->cn;
557 $this->netConfigDNS->save();
558 if (!$ldap->success()){
559 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
560 }
562 /* Send installation activation
563 */
564 if ($activate && class_available("DaemonEvent")){
565 $events = DaemonEvent::get_event_types(SYSTEM_EVENT | HIDDEN_EVENT);
566 $o_queue = new gosaSupportDaemon();
567 if(isset($events['TRIGGERED']['DaemonEvent_installation_activation'])){
568 $evt = $events['TRIGGERED']['DaemonEvent_installation_activation'];
569 $tmp = new $evt['CLASS_NAME']($this->config);
570 $tmp->set_type(TRIGGERED_EVENT);
571 $tmp->add_targets(array($this->netConfigDNS->macAddress));
572 if(!$o_queue->append($tmp)){
573 msg_dialog::display(_("Service infrastructure"),msgPool::siError($o_queue->get_error()),ERROR_DIALOG);
574 }
575 }
576 }
577 }
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 "description" => _("Description"),
652 "gotoRootPasswd" => _("Root password"),
653 "FAIstate" => _("Action flag"))
654 ));
655 }
658 function set_everything_to_inherited()
659 {
660 $this->gotoTerminalPath = "default";
661 $this->gotoSwapServer = "default" ;
662 $this->gotoSyslogServer = "default";
663 $this->inheritTimeServer = TRUE;
665 /* Set workstation service attributes to inherited */
666 if($this->member_of_ogroup && isset($this->parent->by_object['termservice'])){
667 foreach(array("gotoXKbLayout","gotoXKbModel","gotoXKbVariant",
668 "gotoXResolution","gotoXColordepth","gotoXMouseType","gotoXMouseport") as $name){
669 $this->parent->by_object['termservice']->$name = "default";
670 }
671 }
673 /* Set workstation startup attributes to inherited */
674 if($this->member_of_ogroup && isset($this->parent->by_object['termstartup'])){
675 $this->parent->by_object['termstartup']->gotoBootKernel = "default-inherited";
676 $this->parent->by_object['termstartup']->gotoLdapServer = "default-inherited";
677 }
678 }
679 }
681 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
682 ?>