294f8e537f1bea00d1bb16243e1732e0943ea818
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 /* Create available ntp options */
114 $tmp = $this->config->data['SERVERS']['NTP'];
115 $this->gotoNtpServers = array();
116 foreach($tmp as $key => $server){
117 if($server == "default") continue;
118 $this->gotoNtpServers[$server] = $server;
119 }
121 $this->modes["active"]= _("Activated");
122 $this->modes["locked"]= _("Locked");
124 /* Set base */
125 if ($this->dn == "new"){
126 $ui= get_userinfo();
127 $this->base= dn2base($ui->dn);
128 } else {
129 $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
130 }
132 /* Create an array of all Syslog servers */
133 $tmp = $this->config->data['SERVERS']['SYSLOG'];
134 foreach($tmp as $server){
135 $visible = $server;
136 if($server == "default") {
137 $visible = "["._("inherited")."]";
138 }
139 $this->gotoSyslogServers[$server] = $visible;
140 }
142 /* Save 'dn' for later referal */
143 $this->orig_dn= $this->dn;
144 }
147 function set_acl_base($base)
148 {
149 plugin::set_acl_base($base);
150 $this->netConfigDNS->set_acl_base($base);
151 }
153 function set_acl_category($cat)
154 {
155 plugin::set_acl_category($cat);
156 $this->netConfigDNS->set_acl_category($cat);
157 }
159 function execute()
160 {
161 /* Call parent execute */
162 plugin::execute();
164 if($this->is_account && !$this->view_logged){
165 $this->view_logged = TRUE;
166 new log("view","workstation/".get_class($this),$this->dn);
167 }
169 /* Do we need to flip is_account state? */
170 if(isset($_POST['modify_state'])){
171 if($this->is_account && $this->acl_is_removeable()){
172 $this->is_account= FALSE;
173 }elseif(!$this->is_account && $this->acl_is_createable()){
174 $this->is_account= TRUE;
175 }
176 }
178 if ((isset($_POST['action'])) && ($this->acl_is_writeable("FAIstate"))){
179 $cmd= search_config($this->config->data['TABS'], "workgeneric", "ACTIONCMD");
180 if ($cmd == ""){
181 print_red(_("No ACTIONCMD definition found in your gosa.conf"));
182 } else {
183 exec ($cmd." ".$this->netConfigDNS->macAddress." ".escapeshellarg($_POST['saction']), $dummy, $retval);
184 if ($retval != 0){
185 print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
186 } elseif ($_POST['saction'] != "wake") {
188 /* Set FAIstate */
189 if($this->fai_activated && $this->dn != "new"){
190 $ldap = $this->config->get_ldap_link();
191 $ldap->cd($this->config->current['BASE']);
192 $ldap->cat($this->dn,array("objectClass"));
193 $res = $ldap->fetch();
195 $attrs = array();
196 $attrs['FAIstate'] = $this->FAIstate;
197 if(isset($this->mapActions[$_POST['saction']]) && $this->mapActions[$_POST['saction']] != ""){
198 $attrs['FAIstate'] = $this->mapActions[$_POST ['saction']];
199 }
201 for($i = 0; $i < $res['objectClass']['count'] ; $i ++){
202 $attrs['objectClass'][] = $res['objectClass'][$i];
203 }
205 if(($attrs['FAIstate'] != "") && (!in_array("FAIobject",$attrs['objectClass']))){
206 $attrs['objectClass'][] = "FAIobject";
207 }
209 if($attrs['FAIstate'] == ""){
210 #FIXME we should check if FAIobject is not used anymore
211 $attrs['FAIstate'] = array();
212 }
214 $ldap->cd($this->dn);
215 $ldap->modify($attrs);
216 show_ldap_error($ldap->get_error(), sprintf(_("Saving of system workstation/generic (FAIstate) with dn '%s' failed."),$this->dn));
218 }
219 $this->didAction= TRUE;
220 }
221 }
222 }
224 /* Do we represent a valid terminal? */
225 if (!$this->is_account && $this->parent == NULL){
226 $display= "<img alt=\"\" src=\"images/stop.png\" align=middle> <b>".
227 _("This 'dn' has no workstation features.")."</b>";
228 return($display);
229 }
231 /* Base select dialog */
232 $once = true;
233 foreach($_POST as $name => $value){
234 if(preg_match("/^chooseBase/",$name) && $once && $this->acl_is_writeable("base")){
235 $once = false;
236 $this->dialog = new baseSelectDialog($this->config,$this,$this->get_allowed_bases());
237 $this->dialog->setCurrentBase($this->base);
238 }
239 }
241 /* Dialog handling */
242 if(is_object($this->dialog)){
243 /* Must be called before save_object */
244 $this->dialog->save_object();
246 if($this->dialog->isClosed()){
247 $this->dialog = false;
248 }elseif($this->dialog->isSelected()){
250 /* A new base was selected, check if it is a valid one */
251 $tmp = $this->get_allowed_bases();
252 if(isset($tmp[$this->dialog->isSelected()])){
253 $this->base = $this->dialog->isSelected();
254 }
256 $this->dialog= false;
257 }else{
258 return($this->dialog->execute());
259 }
260 }
262 /* Add new ntp Server to our list */
263 if((isset($_POST['addNtpServer'])) && (isset($_POST['gotoNtpServers'])) && $this->acl_is_writeable("gotoNtpServer")){
264 $this->gotoNtpServer[$_POST['gotoNtpServers']] = $_POST['gotoNtpServers'];
265 }
267 /* Delete selected NtpServer for list of used servers */
268 if((isset($_POST['delNtpServer'])) && (isset($_POST['gotoNtpServerSelected'])) && $this->acl_is_writeable("gotoNtpServer")){
269 foreach($_POST['gotoNtpServerSelected'] as $name){
270 unset($this->gotoNtpServer[$name]);
271 }
272 }
274 /* Fill templating stuff */
275 $smarty= get_smarty();
277 /* Set acls */
278 $tmp = $this->plInfo();
279 foreach($tmp['plProvidedAcls'] as $name => $translation){
280 $smarty->assign($name."ACL",$this->getacl($name));
281 }
283 $smarty->assign("cn", $this->cn);
284 $smarty->assign("l", $this->l);
285 $smarty->assign("bases", $this->get_allowed_bases());
286 $smarty->assign("staticAddress", "");
288 $tmp = array();
289 foreach($this->gotoNtpServers as $server){
290 if(!in_array($server,$this->gotoNtpServer)){
291 $tmp[$server] = $server;
292 }
293 }
294 $smarty->assign("gotoNtpServers",$tmp);
296 /* Check if workstation is online */
297 $query= "fping -q -r 1 -t 500 ".$this->cn;
298 exec ($query, $dummy, $retval);
300 /* Offline */
301 if ($retval == 0){
302 $smarty->assign("actions", array("halt" => _("Switch off"), "reboot" => _("Reboot"),
303 "instant_update" => _("Instant update"),
304 "update" => _("Scheduled update"),
305 "reinstall" => _("Reinstall"),
306 "rescan" => _("Rescan hardware"),
307 "memcheck" => _("Memory test"),
308 "localboot" => _("Force localboot"),
309 "sysinfo" => _("System analysis")));
310 } else {
311 $smarty->assign("actions", array("wake" => _("Wake up"),
312 "reinstall" => _("Reinstall"),
313 "update" => _("Scheduled update"),
314 "memcheck" => _("Memory test"),
315 "localboot" => _("Force localboot"),
316 "sysinfo" => _("System analysis")));
317 }
318 /* Arrays */
319 $smarty->assign("modes", $this->modes);
320 $smarty->assign("nfsservers", $this->config->data['SERVERS']['NFS']);
321 $smarty->assign("syslogservers", $this->gotoSyslogServers);
322 $smarty->assign("fai_activated",$this->fai_activated);
324 $ntpser = array();
325 foreach($this->gotoNtpServers as $server){
326 if(!in_array($server,$this->gotoNtpServer)){
327 $ntpser[$server] = $server;
328 }
329 }
330 $smarty->assign("gotoNtpServers", $ntpser);
332 /* Variables */
333 foreach(array("base", "gotoMode", "gotoSyslogServer", "gotoNtpServer") as $val){
334 $smarty->assign($val."_select", $this->$val);
335 }
337 /* tell smarty the inherit checkbox state */
338 $smarty->assign("inheritTimeServer",$this->inheritTimeServer);
339 $smarty->assign("member_of_ogroup",$this->member_of_ogroup);
341 /* Show main page */
342 $smarty->assign("netconfig", $this->netConfigDNS->execute());
343 return($smarty->fetch (get_template_path('workstation.tpl', TRUE)));
344 }
346 function remove_from_parent()
347 {
348 if($this->acl_is_removeable()){
350 $this->netConfigDNS->remove_from_parent();
351 $ldap= $this->config->get_ldap_link();
352 $ldap->rmdir($this->dn);
353 new log("remove","workstation/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
354 show_ldap_error($ldap->get_error(), sprintf(_("Removing of system workstation/generic with dn '%s' failed."),$this->dn));
356 /* Optionally execute a command after we're done */
357 $this->handle_post_events("remove", array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
359 /* Delete references to object groups */
360 $ldap->cd ($this->config->current['BASE']);
361 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
362 while ($ldap->fetch()){
363 $og= new ogroup($this->config, $ldap->getDN());
364 unset($og->member[$this->dn]);
365 $og->save ();
366 }
367 }
369 if(isset($_POST["inheritAll"])){
370 $this->set_everything_to_inherited();
371 }
372 }
375 /* Save data to object */
376 function save_object()
377 {
379 /* Create a base backup and reset the
380 base directly after calling plugin::save_object();
381 Base will be set seperatly a few lines below */
382 $base_tmp = $this->base;
383 plugin::save_object();
384 $this->base = $base_tmp;
386 /* Save base, since this is no LDAP attribute */
387 $tmp = $this->get_allowed_bases();
388 if(isset($_POST['base'])){
389 if(isset($tmp[$_POST['base']])){
390 $this->base= $_POST['base'];
391 }
392 }
394 $this->netConfigDNS->save_object();
396 /* Set inherit mode */
397 if((isset($_POST['workgeneric_posted'])) && ($this->acl_is_writeable("gotoNtpServer"))){
398 if(isset($_POST["inheritTimeServer"])){
399 $this->inheritTimeServer = true;
400 }else{
401 $this->inheritTimeServer = false;
402 }
403 }
405 }
408 /* Check supplied data */
409 function check()
410 {
411 /* Call common method to give check the hook */
412 $message= plugin::check();
414 /* Skip IP & Mac checks if this is a template */
415 if($this->cn != "wdefault"){
416 $message= array_merge($message, $this->netConfigDNS->check());
417 }
419 $this->dn= "cn=".$this->cn.",ou=workstations,ou=systems,".$this->base;
421 if ($this->cn == ""){
422 $message[]= _("The required field 'Workstation name' is not set.");
423 }
425 if ($this->orig_dn != $this->dn){
426 $ldap= $this->config->get_ldap_link();
427 $ldap->cd ($this->base);
429 if($this->cn == "wdefault"){
430 $ldap->cat($this->dn);
431 }else{
432 $ldap->search ("(&(cn=".$this->cn.")(objectClass=gotoWorkstation))", array("cn"));
433 }
434 if ($ldap->count() != 0){
435 while ($attrs= $ldap->fetch()){
436 if (preg_match ("/,ou=incoming,/", $ldap->getDN())){
437 continue;
438 } else {
439 if ($attrs['dn'] != $this->orig_dn){
440 $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
441 break;
442 }
443 }
444 }
445 }
446 }
448 /* Check for valid ntpServer selection */
449 if((!$this->inheritTimeServer) && (!count($this->gotoNtpServer))){
450 $message[]= _("There must be at least one NTP server selected, or the inherit mode activated.");
451 }
453 return ($message);
454 }
457 /* Save to LDAP */
458 function save()
459 {
460 plugin::save();
462 /* Strip out 'default' values */
463 foreach (array("gotoSyslogServer") as $val){
465 if (isset($this->attrs[$val]) && $this->attrs[$val] == "default"){
466 $this->attrs[$val]= array();
467 }
468 }
470 /* Add missing arrays */
471 foreach (array("ghScsiDev", "ghIdeDev", "ghNetNic") as $val){
472 if (isset ($this->$val) && count ($this->$val) != 0){
473 $this->attrs["$val"]= $this->$val;
474 }
475 }
477 /* Remove all empty values */
478 if ($this->orig_dn == 'new'){
479 $attrs= array();
480 foreach ($this->attrs as $key => $val){
481 if (is_array($val) && count($val) == 0){
482 continue;
483 }
484 $attrs[$key]= $val;
485 }
486 $this->attrs= $attrs;
487 }
489 /* Update ntp server settings */
490 if($this->inheritTimeServer){
491 if($this->is_new){
492 if(isset($this->attrs['gotoNtpServer'])){
493 unset($this->attrs['gotoNtpServer']);
494 }
495 }else{
496 $this->attrs['gotoNtpServer'] = array();
497 }
498 }else{
499 /* Set ntpServers */
500 $this->attrs['gotoNtpServer'] = array();
501 foreach($this->gotoNtpServer as $server){
502 $this->attrs['gotoNtpServer'][] = $server;
503 }
504 }
506 if(($this->gosaUnitTag) && (!in_array_ics("gosaAdministrativeUnitTag",$this->attrs['objectClass']))){
507 $this->attrs['objectClass'][] = "gosaAdministrativeUnitTag";
508 }
510 /* Write back to ldap */
511 $ldap= $this->config->get_ldap_link();
512 if ($this->orig_dn == 'new'){
513 $ldap->cd($this->config->current['BASE']);
514 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
515 $ldap->cd($this->dn);
516 $ldap->add($this->attrs);
517 new log("create","workstation/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
518 show_ldap_error($ldap->get_error(), sprintf(_("Saving of system workstation/generic with dn '%s' failed."),$this->dn));
519 if(!$this->didAction){
520 $this->handle_post_events("add",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
521 }
522 } else {
523 if ($this->orig_dn != $this->dn){
524 $this->move($this->orig_dn, $this->dn);
525 }
526 $ldap->cd($this->dn);
527 $this->cleanup();
528 $ldap->modify ($this->attrs);
529 new log("modify","workstation/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
531 if(!$this->didAction){
532 $this->handle_post_events("modify",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
533 }
534 }
536 /* cn=default and macAddress=- indicates that this is a template */
537 if($this->cn == "wdefault"){
538 $this->netConfigDNS->macAddress = "-";
539 }
541 $this->netConfigDNS->cn = $this->cn;
542 $this->netConfigDNS->save($this->dn);
543 show_ldap_error($ldap->get_error(), sprintf(_("Saving of system workstation/generic with dn '%s' failed."),$this->dn));
544 }
547 /* Display generic part for server copy & paste */
548 function getCopyDialog()
549 {
550 $vars = array("cn");
551 $smarty = get_smarty();
552 $smarty->assign("cn" ,$this->cn);
553 $smarty->assign("object","workstation");
554 $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
555 $ret = array();
556 $ret['string'] = $str;
557 $ret['status'] = "";
558 return($ret);
559 }
562 function saveCopyDialog()
563 {
564 if(isset($_POST['cn'])){
565 $this->cn = $_POST['cn'];
566 }
567 }
570 function PrepareForCopyPaste($source)
571 {
572 plugin::PrepareForCopyPaste($source);
573 if(isset($source['macAddress'][0])){
574 $this->netConfigDNS->macAddress = $source['macAddress'][0];
575 }
576 if(isset($source['ipHostNumber'][0])){
577 $this->netConfigDNS->ipHostNumber = $source['ipHostNumber'][0];
578 }
580 /* Create used ntp server array */
581 $this->gotoNtpServer= array();
582 if(isset($source['gotoNtpServer'])){
583 $this->inheritTimeServer = false;
584 unset($source['gotoNtpServer']['count']);
585 foreach($source['gotoNtpServer'] as $server){
586 $this->gotoNtpServer[$server] = $server;
587 }
588 }
590 /* Set inherit checkbox state */
591 if((in_array("default",$this->gotoNtpServer)) || (count($this->gotoNtpServer)==0)){
592 $this->inheritTimeServer = true;
593 $this->gotoNtpServer=array();
594 }
595 }
598 /* Return plugin informations for acl handling
599 #FIXME FAIscript seams to ununsed within this class... */
600 function plInfo()
601 {
602 return (array(
603 "plShortName" => _("Generic"),
604 "plDescription" => _("Workstation generic"),
605 "plSelfModify" => FALSE,
606 "plDepends" => array(),
607 "plPriority" => 0,
608 "plSection" => array("administration"),
609 "plCategory" => array("workstation" => array("description" => _("Workstation"),
610 "objectClass" => "gotoWorkstation")),
611 "plProvidedAcls"=> array(
612 "cn" => _("Workstation name"),
613 "l" => _("Location") ,
614 "base" => _("Base") ,
615 "gotoMode" => _("Goto mode"),
616 "gotoSyslogServer" => _("Syslog server"),
617 "gotoNtpServer" => _("Ntp server"),
618 "gotoRootPasswd" => _("Root password"),
619 "FAIstate" => _("Action flag"))
620 ));
621 }
623 function set_everything_to_inherited()
624 {
625 $this->gotoSyslogServer = "default";
626 $this->inheritTimeServer = TRUE;
628 /* Set workstation service attributes to inherited */
629 if($this->member_of_ogroup && isset($this->parent->by_object['workservice'])){
630 foreach(array("gotoXKbLayout","gotoXKbModel","gotoXKbVariant",
631 "gotoXResolution","gotoXColordepth","gotoXMouseType","gotoXMouseType") as $name){
632 $this->parent->by_object['workservice']->$name = "default";
633 }
634 }
636 /* Set workstation startup attributes to inherited */
637 if($this->member_of_ogroup && isset($this->parent->by_object['workstartup'])){
638 $this->parent->by_object['workstartup']->gotoBootKernel = "default-inherited";
639 $this->parent->by_object['workstartup']->gotoLdapServer = "default-inherited";
640 $this->parent->by_object['workstartup']->FAIdebianMirror= "inherited";
641 }
642 }
643 }
644 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
645 ?>