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 = search_config($config->data,"faiManagement","CLASS");
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=".$this->dn."))",array("cn"));
82 $this->member_of_ogroup = $ldap->count() >= 1;
83 }
85 $this->netConfigDNS = new termDNS($this->config,$this->dn,$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= search_config($this->config->data['TABS'], "workgeneric", "ACTIONCMD");
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 /* Show main page */
347 $smarty->assign("netconfig", $this->netConfigDNS->execute());
348 return($smarty->fetch (get_template_path('workstation.tpl', TRUE)));
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=".$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 }
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();
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.",ou=workstations,ou=systems,".$this->base;
429 if ($this->cn == ""){
430 $message[]= _("The required field 'Workstation name' is not set.");
431 }
433 if ($this->orig_dn != $this->dn){
434 $ldap= $this->config->get_ldap_link();
435 $ldap->cd ($this->base);
437 if($this->cn == "wdefault"){
438 $ldap->cat($this->dn);
439 }else{
440 $ldap->search ("(&(cn=".$this->cn.")(objectClass=gotoWorkstation))", array("cn"));
441 }
442 if ($ldap->count() != 0){
443 while ($attrs= $ldap->fetch()){
444 if (preg_match("/cn=dhcp,/",$attrs['dn']) || preg_match ("/,ou=incoming,/", $ldap->getDN())){
445 continue;
446 } else {
447 if ($attrs['dn'] != $this->orig_dn){
448 $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
449 break;
450 }
451 }
452 }
453 }
454 }
456 /* Check for valid ntpServer selection */
457 if((!$this->inheritTimeServer) && (!count($this->gotoNtpServer))){
458 $message[]= _("There must be at least one NTP server selected, or the inherit mode activated.");
459 }
461 return ($message);
462 }
465 /* Save to LDAP */
466 function save()
467 {
468 plugin::save();
470 /* Strip out 'default' values */
471 foreach (array("gotoSyslogServer") as $val){
473 if (isset($this->attrs[$val]) && $this->attrs[$val] == "default"){
474 $this->attrs[$val]= array();
475 }
476 }
478 /* Add missing arrays */
479 foreach (array("ghScsiDev", "ghIdeDev", "ghNetNic") as $val){
480 if (isset ($this->$val) && count ($this->$val) != 0){
481 $this->attrs["$val"]= $this->$val;
482 }
483 }
485 /* Remove all empty values */
486 if ($this->orig_dn == 'new'){
487 $attrs= array();
488 foreach ($this->attrs as $key => $val){
489 if (is_array($val) && count($val) == 0){
490 continue;
491 }
492 $attrs[$key]= $val;
493 }
494 $this->attrs= $attrs;
495 }
497 /* Update ntp server settings */
498 if($this->inheritTimeServer){
499 if($this->is_new){
500 if(isset($this->attrs['gotoNtpServer'])){
501 unset($this->attrs['gotoNtpServer']);
502 }
503 }else{
504 $this->attrs['gotoNtpServer'] = array();
505 }
506 }else{
507 /* Set ntpServers */
508 $this->attrs['gotoNtpServer'] = array();
509 foreach($this->gotoNtpServer as $server){
510 $this->attrs['gotoNtpServer'][] = $server;
511 }
512 }
514 if(($this->gosaUnitTag) && (!in_array_ics("gosaAdministrativeUnitTag",$this->attrs['objectClass']))){
515 $this->attrs['objectClass'][] = "gosaAdministrativeUnitTag";
516 }
518 /* Write back to ldap */
519 $ldap= $this->config->get_ldap_link();
520 if ($this->orig_dn == 'new'){
521 $ldap->cd($this->config->current['BASE']);
522 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
523 $ldap->cd($this->dn);
524 $ldap->add($this->attrs);
525 new log("create","workstation/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
526 show_ldap_error($ldap->get_error(), sprintf(_("Saving of system workstation/generic with dn '%s' failed."),$this->dn));
527 if(!$this->didAction){
528 $this->handle_post_events("add",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
529 }
530 } else {
531 if ($this->orig_dn != $this->dn){
532 $this->move($this->orig_dn, $this->dn);
533 }
534 $ldap->cd($this->dn);
535 $this->cleanup();
536 $ldap->modify ($this->attrs);
537 new log("modify","workstation/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
539 if(!$this->didAction){
540 $this->handle_post_events("modify",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
541 }
542 }
544 /* cn=default and macAddress=- indicates that this is a template */
545 if($this->cn == "wdefault"){
546 $this->netConfigDNS->macAddress = "-";
547 }
549 $this->netConfigDNS->cn = $this->cn;
550 $this->netConfigDNS->save($this->dn);
551 show_ldap_error($ldap->get_error(), sprintf(_("Saving of system workstation/generic with dn '%s' failed."),$this->dn));
552 }
555 /* Display generic part for server copy & paste */
556 function getCopyDialog()
557 {
558 $vars = array("cn");
559 $smarty = get_smarty();
560 $smarty->assign("cn" ,$this->cn);
561 $smarty->assign("object","workstation");
562 $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
563 $ret = array();
564 $ret['string'] = $str;
565 $ret['status'] = "";
566 return($ret);
567 }
570 function saveCopyDialog()
571 {
572 if(isset($_POST['cn'])){
573 $this->cn = $_POST['cn'];
574 }
575 }
578 function PrepareForCopyPaste($source)
579 {
580 plugin::PrepareForCopyPaste($source);
581 if(isset($source['macAddress'][0])){
582 $this->netConfigDNS->macAddress = $source['macAddress'][0];
583 }
584 if(isset($source['ipHostNumber'][0])){
585 $this->netConfigDNS->ipHostNumber = $source['ipHostNumber'][0];
586 }
588 /* Create used ntp server array */
589 $this->gotoNtpServer= array();
590 if(isset($source['gotoNtpServer'])){
591 $this->inheritTimeServer = false;
592 unset($source['gotoNtpServer']['count']);
593 foreach($source['gotoNtpServer'] as $server){
594 $this->gotoNtpServer[$server] = $server;
595 }
596 }
598 /* Set inherit checkbox state */
599 if((in_array("default",$this->gotoNtpServer)) || (count($this->gotoNtpServer)==0)){
600 $this->inheritTimeServer = true;
601 $this->gotoNtpServer=array();
602 }
603 }
606 /* Return plugin informations for acl handling
607 #FIXME FAIscript seams to ununsed within this class... */
608 function plInfo()
609 {
610 return (array(
611 "plShortName" => _("Generic"),
612 "plDescription" => _("Workstation generic"),
613 "plSelfModify" => FALSE,
614 "plDepends" => array(),
615 "plPriority" => 0,
616 "plSection" => array("administration"),
617 "plCategory" => array("workstation" => array("description" => _("Workstation"),
618 "objectClass" => "gotoWorkstation")),
619 "plProvidedAcls"=> array(
620 "cn" => _("Workstation name"),
621 "l" => _("Location") ,
622 "base" => _("Base") ,
623 "gotoMode" => _("Goto mode"),
624 "gotoSyslogServer" => _("Syslog server"),
625 "gotoNtpServer" => _("Ntp server"),
626 "gotoRootPasswd" => _("Root password"),
627 "FAIstate" => _("Action flag"))
628 ));
629 }
631 function set_everything_to_inherited()
632 {
633 $this->gotoSyslogServer = "default";
634 $this->inheritTimeServer = TRUE;
636 /* Set workstation service attributes to inherited */
637 if($this->member_of_ogroup && isset($this->parent->by_object['workservice'])){
638 foreach(array("gotoXKbLayout","gotoXKbModel","gotoXKbVariant",
639 "gotoXResolution","gotoXColordepth","gotoXMouseType","gotoXMouseport") as $name){
640 $this->parent->by_object['workservice']->$name = "default";
641 }
642 }
644 /* Set workstation startup attributes to inherited */
645 if($this->member_of_ogroup && isset($this->parent->by_object['workstartup'])){
646 $this->parent->by_object['workstartup']->gotoBootKernel = "default-inherited";
647 $this->parent->by_object['workstartup']->gotoLdapServer = "default-inherited";
648 $this->parent->by_object['workstartup']->FAIdebianMirror= "inherited";
649 }
650 }
651 }
652 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
653 ?>