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 "memcheck" => "memcheck",
62 "sysinfo" => "sysinfo");
65 var $fai_activated = FALSE;
67 var $member_of_ogroup = FALSE;
69 function workgeneric (&$config, $dn= NULL, $parent= NULL)
70 {
71 $tmp= $config->search("faiManagement", "CLASS",array('menu','tabs'));
72 if(!empty($tmp)){
73 $this->fai_activated = TRUE;
74 }
76 plugin::plugin ($config, $dn, $parent);
78 if(!isset($this->parent->by_object['ogroup'])){
79 $ldap = $this->config->get_ldap_link();
80 $ldap->cd ($this->config->current['BASE']);
81 $ldap->search("(&(objectClass=gotoWorkstationTemplate)(member=".LDAP::prepare4filter($this->dn)."))",array("cn"));
82 $this->member_of_ogroup = $ldap->count() >= 1;
83 }
85 $this->netConfigDNS = new termDNS($this->config,$this,$this->objectclasses);
87 /* Read arrays */
88 foreach (array("ghNetNic", "ghIdeDev", "ghScsiDev") as $val){
89 if (!isset($this->attrs[$val])){
90 continue;
91 }
92 for ($i= 0; $i<$this->attrs[$val]['count']; $i++){
93 array_push($this->$val, $this->attrs[$val][$i]);
94 }
95 }
97 /* Create used ntp server array */
98 $this->gotoNtpServer= array();
99 if(isset($this->attrs['gotoNtpServer'])){
100 $this->inheritTimeServer = false;
101 unset($this->attrs['gotoNtpServer']['count']);
102 foreach($this->attrs['gotoNtpServer'] as $server){
103 $this->gotoNtpServer[$server] = $server;
104 }
105 }
107 /* Set inherit checkbox state */
108 if((in_array("default",$this->gotoNtpServer)) || (count($this->gotoNtpServer) == 0)){
109 $this->inheritTimeServer = true;
110 $this->gotoNtpServer=array();
111 }
113 /* You can't inherit the NTP service, if we are not member in an object group */
114 if(!$this->member_of_ogroup){
115 $this->inheritTimeServer = FALSE;
116 }
118 /* Create available ntp options */
119 $tmp = $this->config->data['SERVERS']['NTP'];
120 $this->gotoNtpServers = array();
121 foreach($tmp as $key => $server){
122 if($server == "default") continue;
123 $this->gotoNtpServers[$server] = $server;
124 }
126 $this->modes["active"]= _("Activated");
127 $this->modes["locked"]= _("Locked");
129 /* Set base */
130 if ($this->dn == "new"){
131 $ui= get_userinfo();
132 $this->base= dn2base($ui->dn);
133 } else {
134 $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
135 }
137 /* Create an array of all Syslog servers */
138 $tmp = $this->config->data['SERVERS']['SYSLOG'];
139 foreach($tmp as $server){
140 $visible = $server;
141 if($server == "default" && $this->member_of_ogroup) {
142 $visible = "["._("inherited")."]";
143 }
144 $this->gotoSyslogServers[$server] = $visible;
145 }
147 /* Save 'dn' for later referal */
148 $this->orig_dn= $this->dn;
149 }
152 function set_acl_base($base)
153 {
154 plugin::set_acl_base($base);
155 $this->netConfigDNS->set_acl_base($base);
156 }
158 function set_acl_category($cat)
159 {
160 plugin::set_acl_category($cat);
161 $this->netConfigDNS->set_acl_category($cat);
162 }
164 function execute()
165 {
166 /* Call parent execute */
167 plugin::execute();
169 if($this->is_account && !$this->view_logged){
170 $this->view_logged = TRUE;
171 new log("view","workstation/".get_class($this),$this->dn);
172 }
174 /* Do we need to flip is_account state? */
175 if(isset($_POST['modify_state'])){
176 if($this->is_account && $this->acl_is_removeable()){
177 $this->is_account= FALSE;
178 }elseif(!$this->is_account && $this->acl_is_createable()){
179 $this->is_account= TRUE;
180 }
181 }
183 if ((isset($_POST['action'])) && ($this->acl_is_writeable("FAIstate"))){
184 $cmd= $this->config->search("workgeneric", "ACTIONCMD",array('tabs'));
185 if ($cmd == ""){
186 print_red(_("No ACTIONCMD definition found in your gosa.conf"));
187 } else {
188 exec ($cmd." ".$this->netConfigDNS->macAddress." ".escapeshellarg($_POST['saction']), $dummy, $retval);
189 if ($retval != 0){
190 print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
191 } elseif ($_POST['saction'] != "wake") {
193 /* Set FAIstate */
194 if($this->fai_activated && $this->dn != "new"){
195 $ldap = $this->config->get_ldap_link();
196 $ldap->cd($this->config->current['BASE']);
197 $ldap->cat($this->dn,array("objectClass"));
198 $res = $ldap->fetch();
200 $attrs = array();
201 $attrs['FAIstate'] = $this->FAIstate;
202 if(isset($this->mapActions[$_POST['saction']]) && $this->mapActions[$_POST['saction']] != ""){
203 $attrs['FAIstate'] = $this->mapActions[$_POST ['saction']];
204 }
206 for($i = 0; $i < $res['objectClass']['count'] ; $i ++){
207 $attrs['objectClass'][] = $res['objectClass'][$i];
208 }
210 if(($attrs['FAIstate'] != "") && (!in_array("FAIobject",$attrs['objectClass']))){
211 $attrs['objectClass'][] = "FAIobject";
212 }
214 if($attrs['FAIstate'] == ""){
215 #FIXME we should check if FAIobject is not used anymore
216 $attrs['FAIstate'] = array();
217 }
219 $ldap->cd($this->dn);
220 $ldap->modify($attrs);
221 show_ldap_error($ldap->get_error(), sprintf(_("Saving of system workstation/generic (FAIstate) with dn '%s' failed."),$this->dn));
223 }
224 $this->didAction= TRUE;
225 }
226 }
227 }
229 /* Do we represent a valid terminal? */
230 if (!$this->is_account && $this->parent === NULL){
231 $display= "<img alt=\"\" src=\"images/stop.png\" align=middle> <b>".
232 _("This 'dn' has no workstation features.")."</b>";
233 return($display);
234 }
236 /* Base select dialog */
237 $once = true;
238 foreach($_POST as $name => $value){
239 if(preg_match("/^chooseBase/",$name) && $once && $this->acl_is_writeable("base")){
240 $once = false;
241 $this->dialog = new baseSelectDialog($this->config,$this,$this->get_allowed_bases());
242 $this->dialog->setCurrentBase($this->base);
243 }
244 }
246 /* Dialog handling */
247 if(is_object($this->dialog)){
248 /* Must be called before save_object */
249 $this->dialog->save_object();
251 if($this->dialog->isClosed()){
252 $this->dialog = false;
253 }elseif($this->dialog->isSelected()){
255 /* A new base was selected, check if it is a valid one */
256 $tmp = $this->get_allowed_bases();
257 if(isset($tmp[$this->dialog->isSelected()])){
258 $this->base = $this->dialog->isSelected();
259 }
261 $this->dialog= false;
262 }else{
263 return($this->dialog->execute());
264 }
265 }
267 /* Add new ntp Server to our list */
268 if((isset($_POST['addNtpServer'])) && (isset($_POST['gotoNtpServers'])) && $this->acl_is_writeable("gotoNtpServer")){
269 $this->gotoNtpServer[$_POST['gotoNtpServers']] = $_POST['gotoNtpServers'];
270 }
272 /* Delete selected NtpServer for list of used servers */
273 if((isset($_POST['delNtpServer'])) && (isset($_POST['gotoNtpServerSelected'])) && $this->acl_is_writeable("gotoNtpServer")){
274 foreach($_POST['gotoNtpServerSelected'] as $name){
275 unset($this->gotoNtpServer[$name]);
276 }
277 }
279 /* Fill templating stuff */
280 $smarty= get_smarty();
282 /* Set acls */
283 $tmp = $this->plInfo();
284 foreach($tmp['plProvidedAcls'] as $name => $translation){
285 $smarty->assign($name."ACL",$this->getacl($name));
286 }
288 $smarty->assign("cn", $this->cn);
289 $smarty->assign("l", $this->l);
290 $smarty->assign("bases", $this->get_allowed_bases());
291 $smarty->assign("staticAddress", "");
293 $tmp = array();
294 foreach($this->gotoNtpServers as $server){
295 if(!in_array($server,$this->gotoNtpServer)){
296 $tmp[$server] = $server;
297 }
298 }
299 $smarty->assign("gotoNtpServers",$tmp);
301 /* Check if workstation is online */
302 $query= "fping -q -r 1 -t 500 ".$this->cn;
303 exec ($query, $dummy, $retval);
305 /* Offline */
306 if ($retval == 0){
307 $smarty->assign("actions", array("halt" => _("Switch off"), "reboot" => _("Reboot"),
308 "instant_update" => _("Instant update"),
309 "update" => _("Scheduled update"),
310 "reinstall" => _("Reinstall"),
311 "rescan" => _("Rescan hardware"),
312 "memcheck" => _("Memory test"),
313 "localboot" => _("Force localboot"),
314 "sysinfo" => _("System analysis")));
315 } else {
316 $smarty->assign("actions", array("wake" => _("Wake up"),
317 "reinstall" => _("Reinstall"),
318 "update" => _("Scheduled update"),
319 "memcheck" => _("Memory test"),
320 "localboot" => _("Force localboot"),
321 "sysinfo" => _("System analysis")));
322 }
323 /* Arrays */
324 $smarty->assign("modes", $this->modes);
325 $smarty->assign("nfsservers", $this->config->data['SERVERS']['NFS']);
326 $smarty->assign("syslogservers", $this->gotoSyslogServers);
327 $smarty->assign("fai_activated",$this->fai_activated);
329 $ntpser = array();
330 foreach($this->gotoNtpServers as $server){
331 if(!in_array($server,$this->gotoNtpServer)){
332 $ntpser[$server] = $server;
333 }
334 }
335 $smarty->assign("gotoNtpServers", $ntpser);
337 /* Variables */
338 foreach(array("base", "gotoMode", "gotoSyslogServer", "gotoNtpServer") as $val){
339 $smarty->assign($val."_select", $this->$val);
340 }
342 /* tell smarty the inherit checkbox state */
343 $smarty->assign("inheritTimeServer",$this->inheritTimeServer);
344 $smarty->assign("member_of_ogroup",$this->member_of_ogroup);
346 $str = $this->netConfigDNS->execute();
347 if(is_object($this->netConfigDNS->dialog)){
348 return($str);
349 }
350 $smarty->assign("netconfig", $str);
352 /* Show main page */
353 return($smarty->fetch (get_template_path('workstation.tpl', TRUE, dirname(__FILE__))));
354 }
356 function remove_from_parent()
357 {
358 if($this->acl_is_removeable()){
360 $this->netConfigDNS->remove_from_parent();
361 $ldap= $this->config->get_ldap_link();
362 $ldap->rmdir($this->dn);
363 new log("remove","workstation/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
364 show_ldap_error($ldap->get_error(), sprintf(_("Removing of system workstation/generic with dn '%s' failed."),$this->dn));
366 /* Optionally execute a command after we're done */
367 $this->handle_post_events("remove", array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
369 /* Delete references to object groups */
370 $ldap->cd ($this->config->current['BASE']);
371 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".LDAP::prepare4filter($this->dn)."))", array("cn"));
372 while ($ldap->fetch()){
373 $og= new ogroup($this->config, $ldap->getDN());
374 unset($og->member[$this->dn]);
375 $og->save ();
376 }
377 }
379 if(isset($_POST["inheritAll"])){
380 $this->set_everything_to_inherited();
381 }
382 }
385 /* Save data to object */
386 function save_object()
387 {
389 /* Create a base backup and reset the
390 base directly after calling plugin::save_object();
391 Base will be set seperatly a few lines below */
392 $base_tmp = $this->base;
393 plugin::save_object();
394 $this->base = $base_tmp;
396 /* Save base, since this is no LDAP attribute */
397 $tmp = $this->get_allowed_bases();
398 if(isset($_POST['base'])){
399 if(isset($tmp[$_POST['base']])){
400 $this->base= $_POST['base'];
401 }
402 }
404 $this->netConfigDNS->save_object();
406 /* Set inherit mode */
407 if((isset($_POST['workgeneric_posted'])) && ($this->acl_is_writeable("gotoNtpServer"))){
408 if(isset($_POST["inheritTimeServer"]) && $this->member_of_ogroup){
409 $this->inheritTimeServer = true;
410 }else{
411 $this->inheritTimeServer = false;
412 }
413 }
415 if(isset($_POST["inheritAll"])){
416 $this->set_everything_to_inherited();
417 }
418 }
421 /* Check supplied data */
422 function check()
423 {
424 /* Call common method to give check the hook */
425 $message= plugin::check();
427 /* Skip IP & Mac checks if this is a template */
428 if($this->cn != "wdefault"){
429 $message= array_merge($message, $this->netConfigDNS->check());
430 }
432 $this->dn= "cn=".$this->cn.",".get_ou('workstationou').$this->base;
434 if ($this->cn == ""){
435 $message[]= _("The required field 'Workstation name' is not set.");
436 }
438 /* Check if given name is a valid host/dns name */
439 if(!tests::is_dns_name($this->cn)){
440 $message[] = _("Please specify a valid name for this object.");
441 }
443 if ($this->orig_dn != $this->dn){
444 $ldap= $this->config->get_ldap_link();
445 $ldap->cd ($this->base);
447 if($this->cn == "wdefault"){
448 $ldap->cat($this->dn);
449 }else{
450 $ldap->search ("(&(cn=".$this->cn.")(objectClass=gotoWorkstation))", array("cn"));
451 }
452 if ($ldap->count() != 0){
453 while ($attrs= $ldap->fetch()){
454 if (preg_match("/cn=dhcp,/",$attrs['dn']) || preg_match ("/,".get_ou('incomingou')."/", $ldap->getDN())){
455 continue;
456 } else {
457 if ($attrs['dn'] != $this->orig_dn){
458 $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
459 break;
460 }
461 }
462 }
463 }
464 }
466 /* Check for valid ntpServer selection */
467 if((!$this->inheritTimeServer) && (!count($this->gotoNtpServer))){
468 $message[]= _("There must be at least one NTP server selected, or the inherit mode activated.");
469 }
471 return ($message);
472 }
475 /* Save to LDAP */
476 function save()
477 {
478 plugin::save();
480 /* Strip out 'default' values */
481 foreach (array("gotoSyslogServer") as $val){
483 if (isset($this->attrs[$val]) && $this->attrs[$val] == "default"){
484 $this->attrs[$val]= array();
485 }
486 }
488 /* Add missing arrays */
489 foreach (array("ghScsiDev", "ghIdeDev", "ghNetNic") as $val){
490 if (isset ($this->$val) && count ($this->$val) != 0){
491 $this->attrs["$val"]= $this->$val;
492 }
493 }
495 /* Remove all empty values */
496 if ($this->orig_dn == 'new'){
497 $attrs= array();
498 foreach ($this->attrs as $key => $val){
499 if (is_array($val) && count($val) == 0){
500 continue;
501 }
502 $attrs[$key]= $val;
503 }
504 $this->attrs= $attrs;
505 }
507 /* Update ntp server settings */
508 if($this->inheritTimeServer){
509 if($this->is_new){
510 if(isset($this->attrs['gotoNtpServer'])){
511 unset($this->attrs['gotoNtpServer']);
512 }
513 }else{
514 $this->attrs['gotoNtpServer'] = array();
515 }
516 }else{
517 /* Set ntpServers */
518 $this->attrs['gotoNtpServer'] = array();
519 foreach($this->gotoNtpServer as $server){
520 $this->attrs['gotoNtpServer'][] = $server;
521 }
522 }
524 if(($this->gosaUnitTag) && (!in_array_ics("gosaAdministrativeUnitTag",$this->attrs['objectClass']))){
525 $this->attrs['objectClass'][] = "gosaAdministrativeUnitTag";
526 }
528 /* Write back to ldap */
529 $ldap= $this->config->get_ldap_link();
530 if ($this->orig_dn == 'new'){
531 $ldap->cd($this->config->current['BASE']);
532 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
533 $ldap->cd($this->dn);
534 $ldap->add($this->attrs);
535 new log("create","workstation/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
536 show_ldap_error($ldap->get_error(), sprintf(_("Saving of system workstation/generic with dn '%s' failed."),$this->dn));
537 if(!$this->didAction){
538 $this->handle_post_events("add",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
539 }
540 } else {
541 if ($this->orig_dn != $this->dn){
542 $this->move($this->orig_dn, $this->dn);
543 }
544 $ldap->cd($this->dn);
545 $this->cleanup();
546 $ldap->modify ($this->attrs);
547 new log("modify","workstation/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
549 if(!$this->didAction){
550 $this->handle_post_events("modify",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
551 }
552 }
554 /* cn=default and macAddress=- indicates that this is a template */
555 if($this->cn == "wdefault"){
556 $this->netConfigDNS->macAddress = "-";
557 }
559 $this->netConfigDNS->cn = $this->cn;
560 $this->netConfigDNS->save();
561 show_ldap_error($ldap->get_error(), sprintf(_("Saving of system workstation/generic with dn '%s' failed."),$this->dn));
562 }
565 /* Display generic part for server copy & paste */
566 function getCopyDialog()
567 {
568 $vars = array("cn");
569 $smarty = get_smarty();
570 $smarty->assign("cn" ,$this->cn);
571 $smarty->assign("object","workstation");
572 $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
573 $ret = array();
574 $ret['string'] = $str;
575 $ret['status'] = "";
576 return($ret);
577 }
580 function saveCopyDialog()
581 {
582 if(isset($_POST['cn'])){
583 $this->cn = $_POST['cn'];
584 }
585 }
588 function PrepareForCopyPaste($source)
589 {
590 plugin::PrepareForCopyPaste($source);
591 if(isset($source['macAddress'][0])){
592 $this->netConfigDNS->macAddress = $source['macAddress'][0];
593 }
594 if(isset($source['ipHostNumber'][0])){
595 $this->netConfigDNS->ipHostNumber = $source['ipHostNumber'][0];
596 }
598 /* Create used ntp server array */
599 $this->gotoNtpServer= array();
600 if(isset($source['gotoNtpServer'])){
601 $this->inheritTimeServer = false;
602 unset($source['gotoNtpServer']['count']);
603 foreach($source['gotoNtpServer'] as $server){
604 $this->gotoNtpServer[$server] = $server;
605 }
606 }
608 /* Set inherit checkbox state */
609 if((in_array("default",$this->gotoNtpServer)) || (count($this->gotoNtpServer)==0)){
610 $this->inheritTimeServer = true;
611 $this->gotoNtpServer=array();
612 }
613 }
616 /* Return plugin informations for acl handling
617 #FIXME FAIscript seams to ununsed within this class... */
618 static function plInfo()
619 {
620 return (array(
621 "plShortName" => _("Generic"),
622 "plDescription" => _("Workstation generic"),
623 "plSelfModify" => FALSE,
624 "plDepends" => array(),
625 "plPriority" => 0,
626 "plSection" => array("administration"),
627 "plCategory" => array("workstation" => array("description" => _("Workstation"),
628 "objectClass" => "gotoWorkstation")),
629 "plProvidedAcls"=> array(
630 "cn" => _("Workstation name"),
631 "l" => _("Location") ,
632 "base" => _("Base") ,
633 "gotoMode" => _("Goto mode"),
634 "gotoSyslogServer" => _("Syslog server"),
635 "gotoNtpServer" => _("Ntp server"),
636 "gotoRootPasswd" => _("Root password"),
637 "FAIstate" => _("Action flag"))
638 ));
639 }
641 function set_everything_to_inherited()
642 {
643 $this->gotoSyslogServer = "default";
644 $this->inheritTimeServer = TRUE;
646 /* Set workstation service attributes to inherited */
647 if($this->member_of_ogroup && isset($this->parent->by_object['workservice'])){
648 foreach(array("gotoXKbLayout","gotoXKbModel","gotoXKbVariant",
649 "gotoXResolution","gotoXColordepth","gotoXMouseType","gotoXMouseport") as $name){
650 $this->parent->by_object['workservice']->$name = "default";
651 }
652 }
654 /* Set workstation startup attributes to inherited */
655 if($this->member_of_ogroup && isset($this->parent->by_object['workstartup'])){
656 $this->parent->by_object['workstartup']->gotoBootKernel = "default-inherited";
657 $this->parent->by_object['workstartup']->gotoLdapServer = "default-inherited";
658 $this->parent->by_object['workstartup']->FAIdebianMirror= "inherited";
659 }
660 }
661 }
662 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
663 ?>